2017-07-17 16:10:32 -05:00
/ *
* AMD M e m o r y E n c r y p t i o n S u p p o r t
*
* Copyright ( C ) 2 0 1 6 A d v a n c e d M i c r o D e v i c e s , I n c .
*
* Author : Tom L e n d a c k y < t h o m a s . l e n d a c k y @amd.com>
*
* This p r o g r a m i s f r e e s o f t w a r e ; you can redistribute it and/or modify
* it u n d e r t h e t e r m s o f t h e G N U G e n e r a l P u b l i c L i c e n s e v e r s i o n 2 a s
* published b y t h e F r e e S o f t w a r e F o u n d a t i o n .
* /
# include < l i n u x / l i n k a g e . h >
# include < a s m / p g t a b l e . h >
# include < a s m / p a g e . h >
# include < a s m / p r o c e s s o r - f l a g s . h >
# include < a s m / m s r - i n d e x . h >
.text
.code64
ENTRY( s m e _ e n c r y p t _ e x e c u t e )
/ *
* Entry p a r a m e t e r s :
* RDI - v i r t u a l a d d r e s s f o r t h e e n c r y p t e d k e r n e l m a p p i n g
* RSI - v i r t u a l a d d r e s s f o r t h e d e c r y p t e d k e r n e l m a p p i n g
* RDX - l e n g t h o f k e r n e l
* RCX - v i r t u a l a d d r e s s o f t h e e n c r y p t i o n w o r k a r e a , i n c l u d i n g :
* - stack p a g e ( P A G E _ S I Z E )
* - encryption r o u t i n e p a g e ( P A G E _ S I Z E )
* - intermediate c o p y b u f f e r ( P M D _ P A G E _ S I Z E )
* R8 - p h y s c i a l a d d r e s s o f t h e p a g e t a b l e s t o u s e f o r e n c r y p t i o n
* /
2017-08-27 18:39:24 +02:00
push % r b p
movq % r s p , % r b p / * R B P n o w h a s o r i g i n a l s t a c k p o i n t e r * /
2017-07-17 16:10:32 -05:00
/* Set up a one page stack in the non-encrypted memory area */
movq % r c x , % r a x / * W o r k a r e a s t a c k p a g e * /
leaq P A G E _ S I Z E ( % r a x ) , % r s p / * S e t n e w s t a c k p o i n t e r * /
addq $ P A G E _ S I Z E , % r a x / * W o r k a r e a e n c r y p t i o n r o u t i n e * /
push % r12
movq % r d i , % r10 / * E n c r y p t e d k e r n e l * /
movq % r s i , % r11 / * D e c r y p t e d k e r n e l * /
movq % r d x , % r12 / * K e r n e l l e n g t h * /
/* Copy encryption routine into the workarea */
movq % r a x , % r d i / * W o r k a r e a e n c r y p t i o n r o u t i n e * /
leaq _ _ e n c _ c o p y ( % r i p ) , % r s i / * E n c r y p t i o n r o u t i n e * /
movq $ ( . L _ _ e n c _ c o p y _ e n d - _ _ e n c _ c o p y ) , % r c x / * E n c r y p t i o n r o u t i n e l e n g t h * /
rep m o v s b
/* Setup registers for call */
movq % r10 , % r d i / * E n c r y p t e d k e r n e l * /
movq % r11 , % r s i / * D e c r y p t e d k e r n e l * /
movq % r8 , % r d x / * P a g e t a b l e s u s e d f o r e n c r y p t i o n * /
movq % r12 , % r c x / * K e r n e l l e n g t h * /
movq % r a x , % r8 / * W o r k a r e a e n c r y p t i o n r o u t i n e * /
addq $ P A G E _ S I Z E , % r8 / * W o r k a r e a i n t e r m e d i a t e c o p y b u f f e r * /
call * % r a x / * C a l l t h e e n c r y p t i o n r o u t i n e * /
pop % r12
movq % r b p , % r s p / * R e s t o r e o r i g i n a l s t a c k p o i n t e r * /
2017-08-27 18:39:24 +02:00
pop % r b p
2017-07-17 16:10:32 -05:00
ret
ENDPROC( s m e _ e n c r y p t _ e x e c u t e )
ENTRY( _ _ e n c _ c o p y )
/ *
* Routine u s e d t o e n c r y p t k e r n e l .
* This r o u t i n e m u s t b e r u n o u t s i d e o f t h e k e r n e l p r o p e r s i n c e
* the k e r n e l w i l l b e e n c r y p t e d d u r i n g t h e p r o c e s s . S o t h i s
* routine i s d e f i n e d h e r e a n d t h e n c o p i e d t o a n a r e a o u t s i d e
* of t h e k e r n e l w h e r e i t w i l l r e m a i n a n d r u n d e c r y p t e d
* during e x e c u t i o n .
*
* On e n t r y t h e r e g i s t e r s m u s t b e :
* RDI - v i r t u a l a d d r e s s f o r t h e e n c r y p t e d k e r n e l m a p p i n g
* RSI - v i r t u a l a d d r e s s f o r t h e d e c r y p t e d k e r n e l m a p p i n g
* RDX - a d d r e s s o f t h e p a g e t a b l e s t o u s e f o r e n c r y p t i o n
* RCX - l e n g t h o f k e r n e l
* R8 - i n t e r m e d i a t e c o p y b u f f e r
*
* RAX - p o i n t s t o t h i s r o u t i n e
*
* The k e r n e l w i l l b e e n c r y p t e d b y c o p y i n g f r o m t h e n o n - e n c r y p t e d
* kernel s p a c e t o a n i n t e r m e d i a t e b u f f e r a n d t h e n c o p y i n g f r o m t h e
* intermediate b u f f e r b a c k t o t h e e n c r y p t e d k e r n e l s p a c e . T h e p h y s i c a l
* addresses o f t h e t w o k e r n e l s p a c e m a p p i n g s a r e t h e s a m e w h i c h
* results i n t h e k e r n e l b e i n g e n c r y p t e d " i n p l a c e " .
* /
/* Enable the new page tables */
mov % r d x , % c r3
/* Flush any global TLBs */
mov % c r4 , % r d x
andq $ ~ X 8 6 _ C R 4 _ P G E , % r d x
mov % r d x , % c r4
orq $ X 8 6 _ C R 4 _ P G E , % r d x
mov % r d x , % c r4
2018-01-10 13:25:56 -06:00
push % r15
2018-01-10 13:26:26 -06:00
push % r12
2018-01-10 13:25:56 -06:00
movq % r c x , % r9 / * S a v e k e r n e l l e n g t h * /
movq % r d i , % r10 / * S a v e e n c r y p t e d k e r n e l a d d r e s s * /
movq % r s i , % r11 / * S a v e d e c r y p t e d k e r n e l a d d r e s s * /
2017-07-17 16:10:32 -05:00
/* Set the PAT register PA5 entry to write-protect */
movl $ M S R _ I A 3 2 _ C R _ P A T , % e c x
rdmsr
2018-01-10 13:25:56 -06:00
mov % r d x , % r15 / * S a v e o r i g i n a l P A T v a l u e * /
2017-07-17 16:10:32 -05:00
andl $ 0 x f f f f00 f f , % e d x / * C l e a r P A 5 * /
orl $ 0 x00 0 0 0 5 0 0 , % e d x / * S e t P A 5 t o W P * /
wrmsr
wbinvd / * I n v a l i d a t e a n y c a c h e e n t r i e s * /
2018-01-10 13:26:26 -06:00
/* Copy/encrypt up to 2MB at a time */
movq $ P M D _ P A G E _ S I Z E , % r12
2017-07-17 16:10:32 -05:00
1 :
2018-01-10 13:26:26 -06:00
cmpq % r12 , % r9
jnb 2 f
movq % r9 , % r12
2 :
2017-07-17 16:10:32 -05:00
movq % r11 , % r s i / * S o u r c e - d e c r y p t e d k e r n e l * /
movq % r8 , % r d i / * D e s t - i n t e r m e d i a t e c o p y b u f f e r * /
2018-01-10 13:26:26 -06:00
movq % r12 , % r c x
2017-07-17 16:10:32 -05:00
rep m o v s b
movq % r8 , % r s i / * S o u r c e - i n t e r m e d i a t e c o p y b u f f e r * /
movq % r10 , % r d i / * D e s t - e n c r y p t e d k e r n e l * /
2018-01-10 13:26:26 -06:00
movq % r12 , % r c x
2017-07-17 16:10:32 -05:00
rep m o v s b
2018-01-10 13:26:26 -06:00
addq % r12 , % r11
addq % r12 , % r10
subq % r12 , % r9 / * K e r n e l l e n g t h d e c r e m e n t * /
2017-07-17 16:10:32 -05:00
jnz 1 b / * K e r n e l l e n g t h n o t z e r o ? * /
/* Restore PAT register */
movl $ M S R _ I A 3 2 _ C R _ P A T , % e c x
rdmsr
2018-01-10 13:25:56 -06:00
mov % r15 , % r d x / * R e s t o r e o r i g i n a l P A T v a l u e * /
2017-07-17 16:10:32 -05:00
wrmsr
2018-01-10 13:26:26 -06:00
pop % r12
2018-01-10 13:25:56 -06:00
pop % r15
2017-07-17 16:10:32 -05:00
ret
.L__enc_copy_end :
ENDPROC( _ _ e n c _ c o p y )