2007-07-19 12:49:22 +04:00
# include < l i n u x / l i n k a g e . h >
# include < l i n u x / l g u e s t . h >
# include < a s m / a s m - o f f s e t s . h >
# include < a s m / t h r e a d _ i n f o . h >
2007-07-20 16:12:56 +04:00
# include < a s m / p r o c e s s o r - f l a g s . h >
2007-07-19 12:49:22 +04:00
2007-07-26 21:41:02 +04:00
/ * G : 0 2 0 This i s w h e r e w e b e g i n : w e h a v e a m a g i c s i g n a t u r e w h i c h t h e l a u n c h e r
* looks f o r . T h e p l a n i s t h a t t h e L i n u x b o o t p r o t o c o l w i l l b e e x t e n d e d w i t h a
2007-07-19 12:49:22 +04:00
* " platform t y p e " f i e l d w h i c h w i l l g u i d e u s h e r e f r o m t h e n o r m a l e n t r y p o i n t ,
2007-07-26 21:41:02 +04:00
* but f o r t h e m o m e n t t h i s s u f f i c e s . T h e n o r m a l b o o t c o d e u s e s % e s i f o r t h e
* boot h e a d e r , s o w e d o t o o . W e c o n v e r t i t t o a v i r t u a l a d d r e s s b y a d d i n g
* PAGE_ O F F S E T , a n d h a n d i t t o l g u e s t _ i n i t ( ) a s i t s a r g u m e n t ( i e . % e a x ) .
2007-07-19 12:49:22 +04:00
*
2007-07-26 21:41:02 +04:00
* The . s e c t i o n l i n e p u t s t h i s c o d e i n . i n i t . t e x t s o i t w i l l b e d i s c a r d e d a f t e r
* boot. * /
2007-07-19 12:49:22 +04:00
.section .init .text , " ax" , @progbits
.ascii " GenuineLguest"
/* Set up initial stack. */
movl $ ( i n i t _ t h r e a d _ u n i o n + T H R E A D _ S I Z E ) ,% e s p
2007-07-19 12:49:23 +04:00
movl % e s i , % e a x
addl $ _ _ P A G E _ O F F S E T , % e a x
2007-07-19 12:49:22 +04:00
jmp l g u e s t _ i n i t
2007-07-26 21:41:02 +04:00
/ * G : 0 5 5 We c r e a t e a m a c r o w h i c h p u t s t h e a s s e m b l e r c o d e b e t w e e n l g s t a r t _ a n d
2007-09-25 08:24:44 +04:00
* lgend_ m a r k e r s . T h e s e t e m p l a t e s a r e p u t i n t h e . t e x t s e c t i o n : t h e y c a n ' t b e
* discarded a f t e r b o o t a s w e m a y n e e d t o p a t c h m o d u l e s , t o o . * /
.text
2007-07-19 12:49:22 +04:00
# define L G U E S T _ P A T C H ( n a m e , i n s n s . . . ) \
lgstart_ ## n a m e : i n s n s ; l g e n d _ # # n a m e : ; \
.globl lgstart_ ## n a m e ; . g l o b l l g e n d _ # # n a m e
LGUEST_ P A T C H ( c l i , m o v l $ 0 , l g u e s t _ d a t a + L G U E S T _ D A T A _ i r q _ e n a b l e d )
LGUEST_ P A T C H ( s t i , m o v l $ X 8 6 _ E F L A G S _ I F , l g u e s t _ d a t a + L G U E S T _ D A T A _ i r q _ e n a b l e d )
LGUEST_ P A T C H ( p o p f , m o v l % e a x , l g u e s t _ d a t a + L G U E S T _ D A T A _ i r q _ e n a b l e d )
LGUEST_ P A T C H ( p u s h f , m o v l l g u e s t _ d a t a + L G U E S T _ D A T A _ i r q _ e n a b l e d , % e a x )
2007-07-26 21:41:02 +04:00
/*:*/
2007-07-19 12:49:22 +04:00
/* These demark the EIP range where host should never deliver interrupts. */
.global lguest_noirq_start
.global lguest_noirq_end
2007-07-26 21:41:05 +04:00
/ * M : 0 0 4 When t h e H o s t r e f l e c t s a t r a p o r i n j e c t s a n i n t e r r u p t i n t o t h e G u e s t ,
* it s e t s t h e e f l a g s i n t e r r u p t b i t o n t h e s t a c k b a s e d o n
* lguest_ d a t a . i r q _ e n a b l e d , s o t h e G u e s t i r e t l o g i c d o e s t h e r i g h t t h i n g w h e n
* restoring i t . H o w e v e r , w h e n t h e H o s t s e t s t h e G u e s t u p f o r d i r e c t t r a p s ,
* such a s s y s t e m c a l l s , t h e p r o c e s s o r i s t h e o n e t o p u s h e f l a g s o n t o t h e
* stack, a n d t h e i n t e r r u p t b i t w i l l b e 1 ( i n r e a l i t y , i n t e r r u p t s a r e a l w a y s
* enabled i n t h e G u e s t ) .
*
* This t u r n s o u t t o b e h a r m l e s s : t h e o n l y t r a p w h i c h s h o u l d h a p p e n u n d e r L i n u x
* with i n t e r r u p t s d i s a b l e d i s P a g e F a u l t ( d u e t o o u r l a z y m a p p i n g o f v m a l l o c
* regions) , w h i c h h a s t o b e r e f l e c t e d t h r o u g h t h e H o s t a n y w a y . I f a n o t h e r
* trap * d o e s * g o o f f w h e n i n t e r r u p t s a r e d i s a b l e d , t h e G u e s t w i l l p a n i c , a n d
* we' l l n e v e r g e t t o t h i s i r e t ! : * /
2007-07-26 21:41:02 +04:00
/ * G : 0 4 5 There i s o n e f i n a l p a r a v i r t _ o p t h a t t h e G u e s t i m p l e m e n t s , a n d g l a n c i n g
* at i t y o u c a n s e e w h y I l e f t i t t o l a s t . I t ' s * c o o l * ! I t ' s i n * a s s e m b l e r * !
*
* The " i r e t " i n s t r u c t i o n i s u s e d t o r e t u r n f r o m a n i n t e r r u p t o r t r a p . T h e
* stack l o o k s l i k e t h i s :
* old a d d r e s s
* old c o d e s e g m e n t & p r i v i l e g e l e v e l
* old p r o c e s s o r f l a g s ( " e f l a g s " )
*
* The " i r e t " i n s t r u c t i o n p o p s t h o s e v a l u e s o f f t h e s t a c k a n d r e s t o r e s t h e m a l l
* at o n c e . T h e o n l y p r o b l e m i s t h a t e f l a g s i n c l u d e s t h e I n t e r r u p t F l a g w h i c h
* the G u e s t c a n ' t c h a n g e : t h e C P U w i l l s i m p l y i g n o r e i t w h e n w e d o a n " i r e t " .
* So w e h a v e t o c o p y e f l a g s f r o m t h e s t a c k t o l g u e s t _ d a t a . i r q _ e n a b l e d b e f o r e
* we d o t h e " i r e t " .
*
* There a r e t w o p r o b l e m s w i t h t h i s : f i r s t l y , w e n e e d t o u s e a r e g i s t e r t o d o
* the c o p y a n d s e c o n d l y , t h e w h o l e t h i n g n e e d s t o b e a t o m i c . T h e f i r s t
* problem i s e a s y t o s o l v e : p u s h % e a x o n t h e s t a c k s o w e c a n u s e i t , a n d t h e n
* restore i t a t t h e e n d j u s t b e f o r e t h e r e a l " i r e t " .
*
* The s e c o n d i s h a r d e r : c o p y i n g e f l a g s t o l g u e s t _ d a t a . i r q _ e n a b l e d w i l l t u r n
* interrupts o n b e f o r e w e ' r e f i n i s h e d , s o w e c o u l d b e i n t e r r u p t e d b e f o r e w e
* return t o u s e r s p a c e o r w h e r e v e r . O u r s o l u t i o n t o t h i s i s t o s u r r o u n d t h e
* code w i t h l g u e s t _ n o i r q _ s t a r t : a n d l g u e s t _ n o i r q _ e n d : l a b e l s . W e t e l l t h e
* Host t h a t i t i s * n e v e r * t o i n t e r r u p t u s t h e r e , e v e n i f i n t e r r u p t s s e e m t o b e
* enabled. * /
2007-07-19 12:49:22 +04:00
ENTRY( l g u e s t _ i r e t )
pushl % e a x
movl 1 2 ( % e s p ) , % e a x
lguest_noirq_start :
2007-07-26 21:41:02 +04:00
/ * Note t h e % s s : s e g m e n t p r e f i x h e r e . N o r m a l d a t a a c c e s s e s u s e t h e
* " ds" s e g m e n t , b u t t h a t w i l l h a v e a l r e a d y b e e n r e s t o r e d f o r w h a t e v e r
* we' r e r e t u r n i n g t o ( s u c h a s u s e r s p a c e ) : w e c a n ' t t r u s t i t . T h e % s s :
* prefix m a k e s s u r e w e u s e t h e s t a c k s e g m e n t , w h i c h i s s t i l l v a l i d . * /
2007-07-19 12:49:22 +04:00
movl % e a x ,% s s : l g u e s t _ d a t a + L G U E S T _ D A T A _ i r q _ e n a b l e d
popl % e a x
iret
lguest_noirq_end :