2020-10-28 17:46:57 +01:00
/* SPDX-License-Identifier: GPL-2.0-only */
/ *
* sev_ v e r i f y _ c b i t . S - C o d e f o r v e r i f i c a t i o n o f t h e C - b i t p o s i t i o n r e p o r t e d
* by t h e H y p e r v i s o r w h e n r u n n i n g w i t h S E V e n a b l e d .
*
* Copyright ( c ) 2 0 2 0 J o e r g R o e d e l ( j r o e d e l @suse.de)
*
* sev_ v e r i f y _ c b i t ( ) i s c a l l e d b e f o r e s w i t c h i n g t o a n e w l o n g - m o d e p a g e - t a b l e
* at b o o t .
*
* Verify t h a t t h e C - b i t p o s i t i o n i s c o r r e c t b y w r i t i n g a r a n d o m v a l u e t o
* an e n c r y p t e d m e m o r y l o c a t i o n w h i l e o n t h e c u r r e n t p a g e - t a b l e . T h e n i t
* switches t o t h e n e w p a g e - t a b l e t o v e r i f y t h e m e m o r y c o n t e n t i s s t i l l t h e
* same. A f t e r t h a t i t s w i t c h e s b a c k t o t h e c u r r e n t p a g e - t a b l e a n d w h e n t h e
* check s u c c e e d e d i t r e t u r n s . I f t h e c h e c k f a i l e d t h e c o d e i n v a l i d a t e s t h e
* stack p o i n t e r a n d g o e s i n t o a h l t l o o p . T h e s t a c k - p o i n t e r i s i n v a l i d a t e d t o
* make s u r e n o i n t e r r u p t o r e x c e p t i o n c a n g e t t h e C P U o u t o f t h e h l t l o o p .
*
* New p a g e - t a b l e p o i n t e r i s e x p e c t e d i n % r d i ( f i r s t p a r a m e t e r )
*
* /
SYM_ F U N C _ S T A R T ( s e v _ v e r i f y _ c b i t )
# ifdef C O N F I G _ A M D _ M E M _ E N C R Y P T
/* First check if a C-bit was detected */
movq s m e _ m e _ m a s k ( % r i p ) , % r s i
testq % r s i , % r s i
jz 3 f
/* sme_me_mask != 0 could mean SME or SEV - Check also for SEV */
movq s e v _ s t a t u s ( % r i p ) , % r s i
testq % r s i , % r s i
jz 3 f
/* Save CR4 in %rsi */
movq % c r4 , % r s i
/* Disable Global Pages */
movq % r s i , % r d x
andq $ ( ~ X 8 6 _ C R 4 _ P G E ) , % r d x
movq % r d x , % c r4
/ *
* Verified t h a t r u n n i n g u n d e r S E V - n o w g e t a r a n d o m v a l u e u s i n g
* RDRAND. T h i s i n s t r u c t i o n i s m a n d a t o r y w h e n r u n n i n g a s a n S E V g u e s t .
*
* Don' t b a i l o u t o f t h e l o o p i f R D R A N D r e t u r n s e r r o r s . I t i s b e t t e r t o
* prevent f o r w a r d p r o g r e s s t h a n t o w o r k w i t h a n o n - r a n d o m v a l u e h e r e .
* /
1 : rdrand % r d x
jnc 1 b
/* Store value to memory and keep it in %rdx */
movq % r d x , s e v _ c h e c k _ d a t a ( % r i p )
/* Backup current %cr3 value to restore it later */
movq % c r3 , % r c x
/* Switch to new %cr3 - This might unmap the stack */
movq % r d i , % c r3
/ *
* Compare v a l u e i n % r d x w i t h m e m o r y l o c a t i o n . I f C - b i t i s i n c o r r e c t
* this w o u l d r e a d t h e e n c r y p t e d d a t a a n d m a k e t h e c h e c k f a i l .
* /
cmpq % r d x , s e v _ c h e c k _ d a t a ( % r i p )
/* Restore old %cr3 */
movq % r c x , % c r3
/* Restore previous CR4 */
movq % r s i , % c r4
/* Check CMPQ result */
je 3 f
/ *
* The c h e c k f a i l e d , p r e v e n t a n y f o r w a r d p r o g r e s s t o p r e v e n t R O P
* attacks, i n v a l i d a t e t h e s t a c k a n d g o i n t o a h l t l o o p .
* /
xorq % r s p , % r s p
subq $ 0 x10 0 0 , % r s p
2 : hlt
jmp 2 b
3 :
# endif
/* Return page-table pointer */
movq % r d i , % r a x
2021-12-04 14:43:40 +01:00
RET
2020-10-28 17:46:57 +01:00
SYM_ F U N C _ E N D ( s e v _ v e r i f y _ c b i t )