2019-06-03 07:44:50 +02:00
/* SPDX-License-Identifier: GPL-2.0-only */
2012-10-19 17:46:27 +01:00
/ *
* Hypervisor s t u b
*
* Copyright ( C ) 2 0 1 2 A R M L t d .
* Author : Marc Z y n g i e r < m a r c . z y n g i e r @arm.com>
* /
# include < l i n u x / i n i t . h >
# include < l i n u x / l i n k a g e . h >
# include < a s m / a s s e m b l e r . h >
2021-02-08 09:57:13 +00:00
# include < a s m / e l 2 _ s e t u p . h >
2016-04-27 17:47:03 +01:00
# include < a s m / k v m _ a r m . h >
2016-04-27 17:47:04 +01:00
# include < a s m / k v m _ a s m . h >
2012-10-19 17:46:27 +01:00
# include < a s m / p t r a c e . h >
# include < a s m / v i r t . h >
.text
2019-01-24 16:32:56 +00:00
.pushsection .hyp .text , " ax"
2012-10-19 17:46:27 +01:00
.align 11
2020-05-01 12:54:29 +01:00
SYM_ C O D E _ S T A R T ( _ _ h y p _ s t u b _ v e c t o r s )
2012-10-19 17:46:27 +01:00
ventry e l 2 _ s y n c _ i n v a l i d / / S y n c h r o n o u s E L 2 t
ventry e l 2 _ i r q _ i n v a l i d / / I R Q E L 2 t
ventry e l 2 _ f i q _ i n v a l i d / / F I Q E L 2 t
ventry e l 2 _ e r r o r _ i n v a l i d / / E r r o r E L 2 t
2021-04-08 14:10:09 +01:00
ventry e l x _ s y n c / / S y n c h r o n o u s E L 2 h
2012-10-19 17:46:27 +01:00
ventry e l 2 _ i r q _ i n v a l i d / / I R Q E L 2 h
ventry e l 2 _ f i q _ i n v a l i d / / F I Q E L 2 h
ventry e l 2 _ e r r o r _ i n v a l i d / / E r r o r E L 2 h
2021-04-08 14:10:09 +01:00
ventry e l x _ s y n c / / S y n c h r o n o u s 6 4 - b i t E L 1
2012-10-19 17:46:27 +01:00
ventry e l 1 _ i r q _ i n v a l i d / / I R Q 6 4 - b i t E L 1
ventry e l 1 _ f i q _ i n v a l i d / / F I Q 6 4 - b i t E L 1
ventry e l 1 _ e r r o r _ i n v a l i d / / E r r o r 6 4 - b i t E L 1
ventry e l 1 _ s y n c _ i n v a l i d / / S y n c h r o n o u s 3 2 - b i t E L 1
ventry e l 1 _ i r q _ i n v a l i d / / I R Q 3 2 - b i t E L 1
ventry e l 1 _ f i q _ i n v a l i d / / F I Q 3 2 - b i t E L 1
ventry e l 1 _ e r r o r _ i n v a l i d / / E r r o r 3 2 - b i t E L 1
2020-05-01 12:54:29 +01:00
SYM_ C O D E _ E N D ( _ _ h y p _ s t u b _ v e c t o r s )
2012-10-19 17:46:27 +01:00
.align 11
2021-04-08 14:10:09 +01:00
SYM_ C O D E _ S T A R T _ L O C A L ( e l x _ s y n c )
2017-04-03 19:38:04 +01:00
cmp x0 , #H V C _ S E T _ V E C T O R S
2021-02-08 09:57:13 +00:00
b. n e 1 f
2016-04-27 17:47:03 +01:00
msr v b a r _ e l 2 , x1
b 9 f
2021-02-08 09:57:13 +00:00
1 : cmp x0 , #H V C _ V H E _ R E S T A R T
b. e q m u t a t e _ t o _ v h e
2016-06-23 17:54:48 +00:00
2 : cmp x0 , #H V C _ S O F T _ R E S T A R T
b. n e 3 f
mov x0 , x2
mov x2 , x4
mov x4 , x1
mov x1 , x3
br x4 / / n o r e t u r n
2017-04-03 19:37:39 +01:00
3 : cmp x0 , #H V C _ R E S E T _ V E C T O R S
beq 9 f / / N o t h i n g t o r e s e t !
2016-04-27 17:47:04 +01:00
/* Someone called kvm_call_hyp() against the hyp-stub... */
2020-03-04 11:36:31 +02:00
mov_ q x0 , H V C _ S T U B _ E R R
2017-04-03 19:38:05 +01:00
eret
2016-04-27 17:47:03 +01:00
2017-04-03 19:38:05 +01:00
9 : mov x0 , x z r
eret
2021-04-08 14:10:09 +01:00
SYM_ C O D E _ E N D ( e l x _ s y n c )
2012-10-19 17:46:27 +01:00
2021-02-08 09:57:13 +00:00
/ / nVHE? N o w a y ! G i v e m e t h e r e a l t h i n g !
SYM_ C O D E _ S T A R T _ L O C A L ( m u t a t e _ t o _ v h e )
/ / Sanity c h e c k : M M U * m u s t * b e o f f
mrs x1 , s c t l r _ e l 2
tbnz x1 , #0 , 1 f
/ / Needs t o b e V H E c a p a b l e , o b v i o u s l y
mrs x1 , i d _ a a64 m m f r1 _ e l 1
ubfx x1 , x1 , #I D _ A A 64 M M F R 1 _ V H E _ S H I F T , #4
cbz x1 , 1 f
2021-02-08 09:57:24 +00:00
/ / Check w h e t h e r V H E i s d i s a b l e d f r o m t h e c o m m a n d l i n e
adr_ l x1 , i d _ a a64 m m f r1 _ o v e r r i d e
ldr x2 , [ x1 , F T R _ O V R _ V A L _ O F F S E T ]
ldr x1 , [ x1 , F T R _ O V R _ M A S K _ O F F S E T ]
ubfx x2 , x2 , #I D _ A A 64 M M F R 1 _ V H E _ S H I F T , #4
ubfx x1 , x1 , #I D _ A A 64 M M F R 1 _ V H E _ S H I F T , #4
cmp x1 , x z r
and x2 , x2 , x1
csinv x2 , x2 , x z r , n e
2021-02-24 09:37:36 +00:00
cbnz x2 , 2 f
2021-02-08 09:57:24 +00:00
2021-02-24 09:37:36 +00:00
1 : mov_ q x0 , H V C _ S T U B _ E R R
eret
2 :
2021-02-08 09:57:13 +00:00
/ / Engage t h e V H E m a g i c !
mov_ q x0 , H C R _ H O S T _ V H E _ F L A G S
msr h c r _ e l 2 , x0
isb
/ / Use t h e E L 1 a l l o c a t e d s t a c k , p e r - c p u o f f s e t
mrs x0 , s p _ e l 1
mov s p , x0
mrs x0 , t p i d r _ e l 1
msr t p i d r _ e l 2 , x0
/ / FP c o n f i g u r a t i o n , v e c t o r s
mrs_ s x0 , S Y S _ C P A C R _ E L 1 2
msr c p a c r _ e l 1 , x0
mrs_ s x0 , S Y S _ V B A R _ E L 1 2
msr v b a r _ e l 1 , x0
2021-04-05 17:42:54 +01:00
/ / Use E L 2 t r a n s l a t i o n s f o r S P E & T R B E a n d d i s a b l e a c c e s s f r o m E L 1
2021-02-08 09:57:16 +00:00
mrs x0 , m d c r _ e l 2
bic x0 , x0 , #( M D C R _ E L 2 _ E 2 P B _ M A S K < < M D C R _ E L 2 _ E 2 P B _ S H I F T )
2021-04-05 17:42:54 +01:00
bic x0 , x0 , #( M D C R _ E L 2 _ E 2 T B _ M A S K < < M D C R _ E L 2 _ E 2 T B _ S H I F T )
2021-02-08 09:57:16 +00:00
msr m d c r _ e l 2 , x0
2021-02-08 09:57:13 +00:00
/ / Transfer t h e M M s t a t e f r o m E L 1 t o E L 2
mrs_ s x0 , S Y S _ T C R _ E L 1 2
msr t c r _ e l 1 , x0
mrs_ s x0 , S Y S _ T T B R 0 _ E L 1 2
msr t t b r0 _ e l 1 , x0
mrs_ s x0 , S Y S _ T T B R 1 _ E L 1 2
msr t t b r1 _ e l 1 , x0
mrs_ s x0 , S Y S _ M A I R _ E L 1 2
msr m a i r _ e l 1 , x0
isb
2021-02-24 09:37:36 +00:00
/ / Hack t h e e x c e p t i o n r e t u r n t o s t a y a t E L 2
mrs x0 , s p s r _ e l 1
and x0 , x0 , #~ P S R _ M O D E _ M A S K
mov x1 , #P S R _ M O D E _ E L 2 h
orr x0 , x0 , x1
msr s p s r _ e l 1 , x0
b e n t e r _ v h e
SYM_ C O D E _ E N D ( m u t a t e _ t o _ v h e )
/ / At t h e p o i n t w h e r e w e r e a c h e n t e r _ v h e ( ) , w e r u n w i t h
/ / the M M U o f f ( w h i c h i s e n f o r c e d b y m u t a t e _ t o _ v h e ( ) ) .
/ / We t h u s n e e d t o b e i n t h e i d m a p , o r e v e r y t h i n g w i l l
/ / explode w h e n e n a b l i n g t h e M M U .
.pushsection .idmap .text , " ax"
SYM_ C O D E _ S T A R T _ L O C A L ( e n t e r _ v h e )
2021-02-08 09:57:13 +00:00
/ / Invalidate T L B s b e f o r e e n a b l i n g t h e M M U
tlbi v m a l l e 1
dsb n s h
2021-02-24 09:37:38 +00:00
isb
2021-02-08 09:57:13 +00:00
/ / Enable t h e E L 2 S 1 M M U , a s s e t u p f r o m E L 1
mrs_ s x0 , S Y S _ S C T L R _ E L 1 2
set_ s c t l r _ e l 1 x0
/ / Disable t h e E L 1 S 1 M M U f o r a g o o d m e a s u r e
mov_ q x0 , I N I T _ S C T L R _ E L 1 _ M M U _ O F F
msr_ s S Y S _ S C T L R _ E L 1 2 , x0
mov x0 , x z r
2021-02-24 09:37:36 +00:00
eret
SYM_ C O D E _ E N D ( e n t e r _ v h e )
.popsection
2021-02-08 09:57:13 +00:00
2012-10-19 17:46:27 +01:00
.macro invalid_vector label
2020-05-01 12:54:29 +01:00
SYM_ C O D E _ S T A R T _ L O C A L ( \ l a b e l )
2012-10-19 17:46:27 +01:00
b \ l a b e l
2020-05-01 12:54:29 +01:00
SYM_ C O D E _ E N D ( \ l a b e l )
2012-10-19 17:46:27 +01:00
.endm
invalid_ v e c t o r e l 2 _ s y n c _ i n v a l i d
invalid_ v e c t o r e l 2 _ i r q _ i n v a l i d
invalid_ v e c t o r e l 2 _ f i q _ i n v a l i d
invalid_ v e c t o r e l 2 _ e r r o r _ i n v a l i d
invalid_ v e c t o r e l 1 _ s y n c _ i n v a l i d
invalid_ v e c t o r e l 1 _ i r q _ i n v a l i d
invalid_ v e c t o r e l 1 _ f i q _ i n v a l i d
invalid_ v e c t o r e l 1 _ e r r o r _ i n v a l i d
2021-01-28 17:38:50 +00:00
.popsection
2012-10-19 17:46:27 +01:00
/ *
* __hyp_set_vectors : Call t h i s a f t e r b o o t t o s e t t h e i n i t i a l h y p e r v i s o r
* vectors a s p a r t o f h y p e r v i s o r i n s t a l l a t i o n . O n a n S M P s y s t e m , t h i s s h o u l d
* be c a l l e d o n e a c h C P U .
*
* x0 m u s t b e t h e p h y s i c a l a d d r e s s o f t h e n e w v e c t o r t a b l e , a n d m u s t b e
* 2 KB a l i g n e d .
*
* Before c a l l i n g t h i s , y o u m u s t c h e c k t h a t t h e s t u b h y p e r v i s o r i s i n s t a l l e d
* everywhere, b y w a i t i n g f o r a n y s e c o n d a r y C P U s t o b e b r o u g h t u p a n d t h e n
* checking t h a t i s _ h y p _ m o d e _ a v a i l a b l e ( ) i s t r u e .
*
* If n o t , t h e r e i s a p r e - e x i s t i n g h y p e r v i s o r , s o m e C P U s f a i l e d t o b o o t , o r
* something e l s e w e n t w r o n g . . . i n s u c h c a s e s , t r y i n g t o i n s t a l l a n e w
* hypervisor i s u n l i k e l y t o w o r k a s d e s i r e d .
*
* When y o u c a l l i n t o y o u r s h i n y n e w h y p e r v i s o r , s p _ e l 2 w i l l c o n t a i n j u n k ,
* so y o u w i l l n e e d t o s e t t h a t t o s o m e t h i n g s e n s i b l e a t t h e n e w h y p e r v i s o r ' s
* initialisation e n t r y p o i n t .
* /
2020-05-01 12:54:29 +01:00
SYM_ F U N C _ S T A R T ( _ _ h y p _ s e t _ v e c t o r s )
2016-04-27 17:47:03 +01:00
mov x1 , x0
mov x0 , #H V C _ S E T _ V E C T O R S
2016-04-27 17:47:02 +01:00
hvc #0
ret
2020-05-01 12:54:29 +01:00
SYM_ F U N C _ E N D ( _ _ h y p _ s e t _ v e c t o r s )
2017-04-03 19:37:39 +01:00
2020-05-01 12:54:29 +01:00
SYM_ F U N C _ S T A R T ( _ _ h y p _ r e s e t _ v e c t o r s )
2017-04-03 19:37:39 +01:00
mov x0 , #H V C _ R E S E T _ V E C T O R S
hvc #0
ret
2020-05-01 12:54:29 +01:00
SYM_ F U N C _ E N D ( _ _ h y p _ r e s e t _ v e c t o r s )
2021-02-08 09:57:14 +00:00
/ *
* Entry p o i n t t o s w i t c h t o V H E i f d e e m e d c a p a b l e
* /
SYM_ F U N C _ S T A R T ( s w i t c h _ t o _ v h e )
/ / Need t o h a v e b o o t e d a t E L 2
adr_ l x1 , _ _ b o o t _ c p u _ m o d e
ldr w0 , [ x1 ]
cmp w0 , #B O O T _ C P U _ M O D E _ E L 2
b. n e 1 f
/ / and s t i l l b e a t E L 1
mrs x0 , C u r r e n t E L
cmp x0 , #C u r r e n t E L _ E L 1
b. n e 1 f
/ / Turn t h e w o r l d u p s i d e d o w n
mov x0 , #H V C _ V H E _ R E S T A R T
hvc #0
1 :
ret
SYM_ F U N C _ E N D ( s w i t c h _ t o _ v h e )