2009-07-30 16:03:45 -06:00
/ * P : 9 0 0
2009-07-30 16:03:45 -06:00
* This i s t h e S w i t c h e r : c o d e w h i c h s i t s a t 0 x F F C 0 0 0 0 0 ( o r 0 x F F E 0 0 0 0 0 ) a s t r i d e
* both t h e H o s t a n d G u e s t t o d o t h e l o w - l e v e l G u e s t < - > H o s t s w i t c h . I t i s a s
* simple a s i t c a n b e m a d e , b u t i t ' s n a t u r a l l y v e r y s p e c i f i c t o x86 .
2007-07-26 10:41:02 -07:00
*
* You h a v e n o w c o m p l e t e d P r e p a r a t i o n . I f t h i s h a s w h e t y o u r a p p e t i t e ; if you
* are f e e l i n g i n v i g o r a t e d a n d r e f r e s h e d t h e n t h e n e x t , m o r e c h a l l e n g i n g s t a g e
2009-07-30 16:03:45 -06:00
* can b e f o u n d i n " m a k e G u e s t " .
: * /
2007-07-19 01:49:23 -07:00
2009-07-30 16:03:45 -06:00
/ * M : 0 1 2
* Lguest i s m e a n t t o b e s i m p l e : m y r u l e o f t h u m b i s t h a t 1 % m o r e L O C m u s t
2007-10-25 15:02:50 +10:00
* gain a t l e a s t 1 % m o r e p e r f o r m a n c e . S i n c e n e i t h e r L O C n o r p e r f o r m a n c e c a n b e
* measured b e f o r e h a n d , i t g e n e r a l l y m e a n s i m p l e m e n t i n g a f e a t u r e t h e n d e c i d i n g
* if i t ' s w o r t h i t . A n d o n c e i t ' s i m p l e m e n t e d , w h o c a n s a y n o ?
*
* This i s w h y I h a v e n ' t i m p l e m e n t e d t h i s i d e a m y s e l f . I w a n t t o , b u t I
* haven' t . Y o u c o u l d , t h o u g h .
*
* The m a i n p l a c e w h e r e l g u e s t p e r f o r m a n c e s u c k s i s G u e s t p a g e f a u l t i n g . W h e n
* a G u e s t u s e r s p a c e p r o c e s s h i t s a n u n m a p p e d p a g e w e s w i t c h b a c k t o t h e H o s t ,
* walk t h e p a g e t a b l e s , f i n d i t ' s n o t m a p p e d , s w i t c h b a c k t o t h e G u e s t p a g e
* fault h a n d l e r , w h i c h c a l l s a h y p e r c a l l t o s e t t h e p a g e t a b l e e n t r y , t h e n
* finally r e t u r n s t o u s e r s p a c e . T h a t ' s t w o r o u n d - t r i p s .
*
* If w e h a d a s m a l l w a l k e r i n t h e S w i t c h e r , w e c o u l d q u i c k l y c h e c k t h e G u e s t
* page t a b l e a n d i f t h e p a g e i s n ' t m a p p e d , i m m e d i a t e l y r e f l e c t t h e f a u l t b a c k
* into t h e G u e s t . T h i s m e a n s t h e S w i t c h e r w o u l d h a v e t o k n o w t h e t o p o f t h e
* Guest p a g e t a b l e a n d t h e p a g e f a u l t h a n d l e r a d d r e s s .
*
* For s i m p l i c i t y , t h e G u e s t s h o u l d o n l y h a n d l e t h e c a s e w h e r e t h e p r i v i l e g e
* level o f t h e f a u l t i s 3 a n d p r o b a b l y o n l y n o t p r e s e n t o r w r i t e f a u l t s . I t
* should a l s o d e t e c t r e c u r s i v e f a u l t s , a n d h a n d t h e o r i g i n a l f a u l t t o t h e
* Host ( w h i c h i s a c t u a l l y r e a l l y e a s y ) .
*
* Two q u e s t i o n s r e m a i n . W o u l d t h e p e r f o r m a n c e g a i n o u t w e i g h t h e c o m p l e x i t y ?
2009-07-30 16:03:45 -06:00
* And w h o w o u l d w r i t e t h e v e r s e d o c u m e n t i n g i t ?
: * /
2007-10-25 15:02:50 +10:00
2009-07-30 16:03:45 -06:00
/ * M : 0 1 1
* Lguest6 4 h a n d l e s N M I . T h i s g a v e m e N M I e n v y ( u n t i l I l o o k e d a t t h e i r
2007-10-25 15:02:50 +10:00
* code) . I t ' s w o r t h d o i n g t h o u g h , s i n c e i t w o u l d l e t u s u s e o p r o f i l e i n t h e
2009-07-30 16:03:45 -06:00
* Host w h e n a G u e s t i s r u n n i n g .
: * /
2007-10-25 15:02:50 +10:00
2007-07-26 10:41:04 -07:00
/ * S : 1 0 0
* Welcome t o t h e S w i t c h e r i t s e l f !
*
* This f i l e c o n t a i n s t h e l o w - l e v e l c o d e w h i c h c h a n g e s t h e C P U t o r u n t h e G u e s t
* code, a n d r e t u r n s t o t h e H o s t w h e n s o m e t h i n g h a p p e n s . U n d e r s t a n d t h i s , a n d
* you u n d e r s t a n d t h e h e a r t o f o u r j o u r n e y .
*
* Because t h i s i s i n a s s e m b l e r r a t h e r t h a n C , o u r t a l e s w i t c h e s f r o m p r o s e t o
* verse. F i r s t I t r i e d l i m e r i c k s :
*
* There o n c e w a s a n e a x r e g ,
* To w h i c h o u r p o i n t e r w a s f e d ,
* It n e e d e d a n a d d ,
* Which a s m - o f f s e t s . h h a d
* But t h i s l i m e r i c k i s h u r t i n g m y h e a d .
*
* Next I t r i e d h a i k u s , b u t f i t t i n g t h e r e q u i r e d r e f e r e n c e t o t h e s e a s o n s i n
* every s t a n z a w a s q u i c k l y b e c o m i n g t i r e s o m e :
*
* The % e a x r e g
* Holds " s t r u c t l g u e s t _ p a g e s " n o w :
* Cherry b l o s s o m s f a l l .
*
* Then I s t a r t e d w i t h H e r o i c V e r s e , b u t t h e r h y m i n g r e q u i r e m e n t l e e c h e d a w a y
* the c o n t e n t d e n s i t y a n d l e d t o s o m e u n i q u e l y a w f u l o b l i q u e r h y m e s :
*
* These c o n s t a n t s a r e c o m i n g f r o m s t r u c t o f f s e t s
* For u s e w i t h i n t h e a s m s w i t c h e r t e x t .
*
* Finally, I s e t t l e d f o r s o m e t h i n g b e t w e e n h e r o i c h e x a m e t e r , a n d n o r m a l p r o s e
* with i n a p p r o p r i a t e l i n e b r e a k s . A n y w a y , i t a i n t n o S h a k e s p e a r e .
* /
/ / Not a l l k e r n e l h e a d e r s w o r k f r o m a s s e m b l e r
/ / But t h e s e o n e s a r e n e e d e d : t h e E N T R Y ( ) d e f i n e
/ / And c o n s t a n t s e x t r a c t e d f r o m s t r u c t o f f s e t s
/ / To a v o i d m a g i c n u m b e r s a n d b r e a k a g e :
/ / Should t h e y c h a n g e t h e c o m p i l e r c a n ' t s a v e u s
/ / Down h e r e i n t h e d e p t h s o f a s s e m b l e r c o d e .
2007-07-19 01:49:23 -07:00
# include < l i n u x / l i n k a g e . h >
# include < a s m / a s m - o f f s e t s . h >
2007-08-09 20:57:13 +10:00
# include < a s m / p a g e . h >
2007-10-22 11:03:28 +10:00
# include < a s m / s e g m e n t . h >
# include < a s m / l g u e s t . h >
2007-07-19 01:49:23 -07:00
2007-07-26 10:41:04 -07:00
/ / We m a r k t h e s t a r t o f t h e c o d e t o c o p y
/ / It' s p l a c e d i n . t e x t t h o i t ' s n e v e r r u n h e r e
/ / You' l l s e e t h e t r i c k m a c r o a t t h e e n d
/ / Which i n t e r l e a v e s d a t a a n d t e x t t o e f f e c t .
2007-07-19 01:49:23 -07:00
.text
ENTRY( s t a r t _ s w i t c h e r _ t e x t )
2007-07-26 10:41:04 -07:00
/ / When w e r e a c h s w i t c h _ t o _ g u e s t w e h a v e j u s t l e f t
/ / The s a f e a n d c o m f o r t i n g s h o r e s o f C c o d e
/ / % eax h a s t h e " s t r u c t l g u e s t _ p a g e s " t o u s e
/ / Where w e s a v e s t a t e a n d s t i l l s e e i t f r o m t h e G u e s t
/ / And % e b x h o l d s t h e G u e s t s h a d o w p a g e t a b l e :
/ / Once s e t w e h a v e t r u l y l e f t H o s t b e h i n d .
2007-07-19 01:49:23 -07:00
ENTRY( s w i t c h _ t o _ g u e s t )
2007-07-26 10:41:04 -07:00
/ / We t o l d g c c a l l i t s r e g s c o u l d f a d e ,
/ / Clobbered b y o u r j o u r n e y i n t o t h e G u e s t
/ / We c o u l d h a v e s a v e d t h e m , i f w e t r i e d
/ / But t i m e i s o u r m a s t e r a n d c y c l e s c o u n t .
/ / Segment r e g i s t e r s m u s t b e s a v e d f o r t h e H o s t
/ / We p u s h t h e m o n t h e H o s t s t a c k f o r l a t e r
2007-07-19 01:49:23 -07:00
pushl % e s
pushl % d s
pushl % g s
pushl % f s
2007-07-26 10:41:04 -07:00
/ / But t h e c o m p i l e r i s f i c k l e , a n d h e e d s
/ / No w a r n i n g o f % e b p c l o b b e r s
/ / When f r a m e p o i n t e r s a r e u s e d . T h a t r e g i s t e r
/ / Must b e s a v e d a n d r e s t o r e d o r c h a o s s t r i k e s .
2007-07-19 01:49:23 -07:00
pushl % e b p
2007-07-26 10:41:04 -07:00
/ / The H o s t ' s s t a c k i s d o n e , n o w s a v e i t a w a y
/ / In o u r " s t r u c t l g u e s t _ p a g e s " a t o f f s e t
/ / Distilled i n t o a s m - o f f s e t s . h
2007-07-19 01:49:23 -07:00
movl % e s p , L G U E S T _ P A G E S _ h o s t _ s p ( % e a x )
2007-07-26 10:41:04 -07:00
/ / All s a v e d a n d t h e r e ' s n o w f i v e s t e p s b e f o r e u s :
/ / Stack, G D T , I D T , T S S
2007-10-25 15:02:50 +10:00
/ / Then l a s t o f a l l t h e p a g e t a b l e s a r e f l i p p e d .
2007-07-26 10:41:04 -07:00
/ / Yet b e w a r e t h a t o u r s t a c k p o i n t e r m u s t b e
/ / Always v a l i d l e s t a n N M I h i t s
/ / % edx d o e s t h e d u t y h e r e a s w e j u g g l e
/ / % eax i s l g u e s t _ p a g e s : o u r s t a c k l i e s w i t h i n .
2007-07-19 01:49:23 -07:00
movl % e a x , % e d x
addl $ L G U E S T _ P A G E S _ r e g s , % e d x
movl % e d x , % e s p
2007-07-26 10:41:04 -07:00
/ / The G u e s t ' s G D T w e s o c a r e f u l l y
/ / Placed i n t h e " s t r u c t l g u e s t _ p a g e s " b e f o r e
2007-07-19 01:49:23 -07:00
lgdt L G U E S T _ P A G E S _ g u e s t _ g d t _ d e s c ( % e a x )
2007-07-26 10:41:04 -07:00
/ / The G u e s t ' s I D T w e d i d p a r t i a l l y
2007-10-25 15:02:50 +10:00
/ / Copy t o " s t r u c t l g u e s t _ p a g e s " a s w e l l .
2007-07-19 01:49:23 -07:00
lidt L G U E S T _ P A G E S _ g u e s t _ i d t _ d e s c ( % e a x )
2007-07-26 10:41:04 -07:00
/ / The T S S e n t r y w h i c h c o n t r o l s t r a p s
/ / Must b e l o a d e d u p w i t h " l t r " n o w :
2007-10-25 15:02:50 +10:00
/ / The G D T e n t r y t h a t T S S u s e s
/ / Changes t y p e w h e n w e l o a d i t : d a m n I n t e l !
2007-07-26 10:41:04 -07:00
/ / For a f t e r w e s w i t c h o v e r o u r p a g e t a b l e s
2007-10-25 15:02:50 +10:00
/ / That e n t r y w i l l b e r e a d - o n l y : w e ' d c r a s h .
2007-07-19 01:49:23 -07:00
movl $ ( G D T _ E N T R Y _ T S S * 8 ) , % e d x
ltr % d x
2007-07-26 10:41:04 -07:00
/ / Look b a c k n o w , b e f o r e w e t a k e t h i s l a s t s t e p !
/ / The H o s t ' s T S S e n t r y w a s a l s o m a r k e d u s e d ;
2007-10-25 15:02:50 +10:00
/ / Let' s c l e a r i t a g a i n f o r o u r r e t u r n .
2007-07-26 10:41:04 -07:00
/ / The G D T d e s c r i p t o r o f t h e H o s t
/ / Points t o t h e t a b l e a f t e r t w o " s i z e " b y t e s
2007-07-19 01:49:23 -07:00
movl ( L G U E S T _ P A G E S _ h o s t _ g d t _ d e s c + 2 ) ( % e a x ) , % e d x
2007-10-25 15:02:50 +10:00
/ / Clear " u s e d " f r o m t y p e f i e l d ( b y t e 5 , b i t 2 )
2007-07-19 01:49:23 -07:00
andb $ 0 x F D , ( G D T _ E N T R Y _ T S S * 8 + 5 ) ( % e d x )
2007-07-26 10:41:04 -07:00
/ / Once o u r p a g e t a b l e ' s s w i t c h e d , t h e G u e s t i s l i v e !
/ / The H o s t f a d e s a s w e r u n t h i s f i n a l s t e p .
/ / Our " s t r u c t l g u e s t _ p a g e s " i s n o w r e a d - o n l y .
2007-07-19 01:49:23 -07:00
movl % e b x , % c r3
2007-07-26 10:41:04 -07:00
/ / The p a g e t a b l e c h a n g e d i d o n e t r i c k y t h i n g :
/ / The G u e s t ' s r e g i s t e r p a g e h a s b e e n m a p p e d
2007-10-25 15:02:50 +10:00
/ / Writable u n d e r o u r % e s p ( s t a c k ) - -
2007-07-26 10:41:04 -07:00
/ / We c a n s i m p l y p o p o f f a l l G u e s t r e g s .
2007-10-22 11:03:29 +10:00
popl % e a x
2007-07-19 01:49:23 -07:00
popl % e b x
popl % e c x
popl % e d x
popl % e s i
popl % e d i
popl % e b p
popl % g s
popl % f s
popl % d s
popl % e s
2007-07-26 10:41:04 -07:00
/ / Near t h e b a s e o f t h e s t a c k l u r k t w o s t r a n g e f i e l d s
/ / Which w e f i l l a s w e e x i t t h e G u e s t
/ / These a r e t h e t r a p n u m b e r a n d i t s e r r o r
/ / We c a n s i m p l y s t e p p a s t t h e m o n o u r w a y .
2007-07-19 01:49:23 -07:00
addl $ 8 , % e s p
2007-07-26 10:41:04 -07:00
/ / The l a s t f i v e s t a c k s l o t s h o l d r e t u r n a d d r e s s
2007-10-25 15:02:50 +10:00
/ / And e v e r y t h i n g n e e d e d t o s w i t c h p r i v i l e g e
/ / From S w i t c h e r ' s l e v e l 0 t o G u e s t ' s 1 ,
2007-07-26 10:41:04 -07:00
/ / And t h e s t a c k w h e r e t h e G u e s t h a d l a s t l e f t i t .
/ / Interrupts a r e t u r n e d b a c k o n : w e a r e G u e s t .
2007-07-19 01:49:23 -07:00
iret
2008-03-28 11:05:53 -05:00
/ / We t r e a d t w o p a t h s t o s w i t c h b a c k t o t h e H o s t
2007-10-25 15:02:50 +10:00
/ / Yet b o t h m u s t s a v e G u e s t s t a t e a n d r e s t o r e H o s t
2007-07-26 10:41:04 -07:00
/ / So w e p u t t h e r o u t i n e i n a m a c r o .
2007-07-19 01:49:23 -07:00
# define S W I T C H _ T O _ H O S T \
2007-07-26 10:41:04 -07:00
/ * We s a v e t h e G u e s t s t a t e : a l l r e g i s t e r s f i r s t \
* Laid o u t j u s t a s " s t r u c t l g u e s t _ r e g s " d e f i n e s * / \
2007-07-19 01:49:23 -07:00
pushl % e s ; \
pushl % d s ; \
pushl % f s ; \
pushl % g s ; \
pushl % e b p ; \
pushl % e d i ; \
pushl % e s i ; \
pushl % e d x ; \
pushl % e c x ; \
pushl % e b x ; \
2007-10-22 11:03:29 +10:00
pushl % e a x ; \
2007-07-26 10:41:04 -07:00
/ * Our s t a c k a n d o u r c o d e a r e u s i n g s e g m e n t s \
* Set i n t h e T S S a n d I D T \
* Yet i f w e w e r e t o t o u c h d a t a w e ' d u s e \
* Whatever d a t a s e g m e n t t h e G u e s t h a d . \
* Load t h e l g u e s t d s s e g m e n t f o r n o w . * / \
2007-07-19 01:49:23 -07:00
movl $ ( L G U E S T _ D S ) , % e a x ; \
movl % e a x , % d s ; \
2007-07-26 10:41:04 -07:00
/ * So w h e r e a r e w e ? W h i c h C P U , w h i c h s t r u c t ? \
2007-08-09 20:57:13 +10:00
* The s t a c k i s o u r c l u e : o u r T S S s t a r t s \
* It a t t h e e n d o f " s t r u c t l g u e s t _ p a g e s " . \
* Or w e m a y h a v e s t u m b l e d w h i l e r e s t o r i n g \
* Our G u e s t s e g m e n t r e g s w h i l e i n s w i t c h _ t o _ g u e s t , \
* The f a u l t p u s h e d a t o p t h a t p a r t - u n w o u n d s t a c k . \
* If w e r o u n d t h e s t a c k d o w n t o t h e p a g e s t a r t \
* We' r e a t t h e s t a r t o f " s t r u c t l g u e s t _ p a g e s " . * / \
2007-07-19 01:49:23 -07:00
movl % e s p , % e a x ; \
2007-08-09 20:57:13 +10:00
andl $ ( ~ ( 1 < < P A G E _ S H I F T - 1 ) ) , % e a x ; \
2007-07-26 10:41:04 -07:00
/ * Save o u r t r a p n u m b e r : t h e s w i t c h w i l l o b s c u r e i t \
2007-10-25 15:02:50 +10:00
* ( In t h e H o s t t h e G u e s t r e g s a r e n o t m a p p e d h e r e ) \
2007-07-26 10:41:04 -07:00
* % ebx h o l d s i t s a f e f o r d e l i v e r _ t o _ h o s t * / \
2007-07-19 01:49:23 -07:00
movl L G U E S T _ P A G E S _ r e g s _ t r a p n u m ( % e a x ) , % e b x ; \
2007-07-26 10:41:04 -07:00
/ * The H o s t G D T , I D T a n d s t a c k ! \
* All t h e s e l i e s a f e l y h i d d e n f r o m t h e G u e s t : \
* We m u s t r e t u r n t o t h e H o s t p a g e t a b l e s \
* ( Hence t h a t w a s s a v e d i n s t r u c t l g u e s t _ p a g e s ) * / \
2007-07-19 01:49:23 -07:00
movl L G U E S T _ P A G E S _ h o s t _ c r3 ( % e a x ) , % e d x ; \
movl % e d x , % c r3 ; \
2007-07-26 10:41:04 -07:00
/ * As b e f o r e , w h e n w e l o o k e d b a c k a t t h e H o s t \
* As w e l e f t a n d m a r k e d T S S u n u s e d \
* So m u s t w e n o w f o r t h e G u e s t l e f t b e h i n d . * / \
2007-07-19 01:49:23 -07:00
andb $ 0 x F D , ( L G U E S T _ P A G E S _ g u e s t _ g d t + G D T _ E N T R Y _ T S S * 8 + 5 ) ( % e a x ) ; \
2007-07-26 10:41:04 -07:00
/* Switch to Host's GDT, IDT. */ \
2007-07-19 01:49:23 -07:00
lgdt L G U E S T _ P A G E S _ h o s t _ g d t _ d e s c ( % e a x ) ; \
lidt L G U E S T _ P A G E S _ h o s t _ i d t _ d e s c ( % e a x ) ; \
2007-10-25 15:02:50 +10:00
/* Restore the Host's stack where its saved regs lie */ \
2007-07-19 01:49:23 -07:00
movl L G U E S T _ P A G E S _ h o s t _ s p ( % e a x ) , % e s p ; \
2007-10-25 15:02:50 +10:00
/* Last the TSS: our Host is returned */ \
2007-07-19 01:49:23 -07:00
movl $ ( G D T _ E N T R Y _ T S S * 8 ) , % e d x ; \
ltr % d x ; \
2007-07-26 10:41:04 -07:00
/* Restore now the regs saved right at the first. */ \
2007-07-19 01:49:23 -07:00
popl % e b p ; \
popl % f s ; \
popl % g s ; \
popl % d s ; \
popl % e s
2007-10-25 15:02:50 +10:00
/ / The f i r s t p a t h i s t r o d w h e n t h e G u e s t h a s t r a p p e d :
/ / ( Which t r a p i t w a s h a s b e e n p u s h e d o n t h e s t a c k ) .
2007-07-26 10:41:04 -07:00
/ / We n e e d o n l y s w i t c h b a c k , a n d t h e H o s t w i l l d e c o d e
/ / Why w e c a m e h o m e , a n d w h a t n e e d s t o b e d o n e .
2007-07-19 01:49:23 -07:00
return_to_host :
SWITCH_ T O _ H O S T
iret
2007-10-25 15:02:50 +10:00
/ / We a r e l e a d t o t h e s e c o n d p a t h l i k e s o :
2007-07-26 10:41:04 -07:00
/ / An i n t e r r u p t , w i t h s o m e c a u s e e x t e r n a l
/ / Has a j e r k e d u s r u d e l y f r o m t h e G u e s t ' s c o d e
/ / Again w e m u s t r e t u r n h o m e t o t h e H o s t
2007-07-19 01:49:23 -07:00
deliver_to_host :
SWITCH_ T O _ H O S T
2007-07-26 10:41:04 -07:00
/ / But n o w w e m u s t g o h o m e v i a t h a t p l a c e
/ / Where t h a t i n t e r r u p t w a s s u p p o s e d t o g o
/ / Had w e n o t b e e n e n s c o n c e d , r u n n i n g t h e G u e s t .
2007-10-25 15:02:50 +10:00
/ / Here w e s e e t h e t r i c k n e s s o f r u n _ g u e s t _ o n c e ( ) :
2007-07-26 10:41:04 -07:00
/ / The H o s t s t a c k i s f o r m e d l i k e a n i n t e r r u p t
/ / With E I P , C S a n d E F L A G S l a y e r e d .
/ / Interrupt h a n d l e r s e n d w i t h " i r e t "
/ / And t h a t w i l l t a k e u s h o m e a t l o n g l o n g l a s t .
/ / But f i r s t w e m u s t f i n d t h e h a n d l e r t o c a l l !
/ / The I D T d e s c r i p t o r f o r t h e H o s t
/ / Has t w o b y t e s f o r s i z e , a n d f o u r f o r a d d r e s s :
/ / % edx w i l l h o l d i t f o r u s f o r n o w .
2007-07-19 01:49:23 -07:00
movl ( L G U E S T _ P A G E S _ h o s t _ i d t _ d e s c + 2 ) ( % e a x ) , % e d x
2007-07-26 10:41:04 -07:00
/ / We n o w k n o w t h e t a b l e a d d r e s s w e n e e d ,
/ / And s a v e d t h e t r a p ' s n u m b e r i n s i d e % e b x .
/ / Yet t h e p o i n t e r t o t h e h a n d l e r i s s m e a r e d
/ / Across t h e b i t s o f t h e t a b l e e n t r y .
/ / What o r a c l e c a n t e l l u s h o w t o e x t r a c t
/ / From s u c h a c o n v o l u t e d e n c o d i n g ?
/ / I c o n s u l t e d g c c , a n d i t g a v e
/ / These i n s t r u c t i o n s , w h i c h I g l a d l y c r e d i t :
2007-07-19 01:49:23 -07:00
leal ( % e d x ,% e b x ,8 ) , % e a x
movzwl ( % e a x ) ,% e d x
movl 4 ( % e a x ) , % e a x
xorw % a x , % a x
orl % e a x , % e d x
2007-07-26 10:41:04 -07:00
/ / Now t h e a d d r e s s o f t h e h a n d l e r ' s i n % e d x
2007-10-25 15:02:50 +10:00
/ / We c a l l i t n o w : i t s " i r e t " d r o p s u s h o m e .
2007-07-19 01:49:23 -07:00
jmp * % e d x
2007-07-26 10:41:04 -07:00
/ / Every i n t e r r u p t c a n c o m e t o u s h e r e
/ / But w e m u s t t r u l y t e l l e a c h a p a r t .
/ / They n u m b e r t w o h u n d r e d a n d f i f t y s i x
/ / And e a c h m u s t l a n d i n a d i f f e r e n t s p o t ,
/ / Push i t s n u m b e r o n s t a c k , a n d j o i n t h e s t r e a m .
/ / And w o r s e , a m e r e s i x o f t h e t r a p s s t a n d a p a r t
/ / And p u s h o n t h e i r s t a c k a n a d d i t i o n :
/ / An e r r o r n u m b e r , t h i r t y t w o b i t s l o n g
/ / So w e p u n i s h t h e o t h e r t w o f i f t y
/ / And m a k e t h e m p u s h a z e r o s o t h e y m a t c h .
/ / Yet t w o f i f t y s i x e n t r i e s i s l o n g
/ / And a l l w i l l l o o k m o s t t h e s a m e a s t h e l a s t
/ / So w e c r e a t e a m a c r o w h i c h c a n m a k e
/ / As m a n y e n t r i e s a s w e n e e d t o f i l l .
/ / Note t h e c h a n g e t o . d a t a t h e n . t e x t :
/ / We p l a n t t h e a d d r e s s o f e a c h e n t r y
/ / Into a ( d a t a ) t a b l e f o r t h e H o s t
/ / To k n o w w h e r e e a c h G u e s t i n t e r r u p t s h o u l d g o .
2007-07-19 01:49:23 -07:00
.macro IRQ_STUB N T A R G E T
.data ; .long 1f; .text; 1:
2007-07-26 10:41:04 -07:00
/ / Trap e i g h t , t e n t h r o u g h f o u r t e e n a n d s e v e n t e e n
/ / Supply a n e r r o r n u m b e r . E l s e z e r o .
2007-07-19 01:49:23 -07:00
.if ( \ N < > 8 ) & & ( \ N < 1 0 | | \ N > 1 4 ) & & ( \ N < > 1 7 )
pushl $ 0
.endif
pushl $ \ N
jmp \ T A R G E T
ALIGN
.endm
2007-07-26 10:41:04 -07:00
/ / This m a c r o c r e a t e s n u m e r o u s e n t r i e s
/ / Using G A S m a c r o s w h i c h o u t - p o w e r C ' s .
2007-07-19 01:49:23 -07:00
.macro IRQ_STUBS FIRST L A S T T A R G E T
irq= \ F I R S T
.rept \ LAST- \ F I R S T + 1
IRQ_ S T U B i r q \ T A R G E T
irq=irq + 1
.endr
.endm
2007-07-26 10:41:04 -07:00
/ / Here' s t h e m a r k e r f o r o u r p o i n t e r t a b l e
/ / Laid i n t h e d a t a s e c t i o n j u s t b e f o r e
/ / Each m a c r o p l a c e s t h e a d d r e s s o f c o d e
/ / Forming a n a r r a y : e a c h o n e p o i n t s t o t e x t
/ / Which h a n d l e s i n t e r r u p t i n i t s t u r n .
2007-07-19 01:49:23 -07:00
.data
.global default_idt_entries
default_idt_entries :
.text
2007-07-26 10:41:04 -07:00
/ / The f i r s t t w o t r a p s g o s t r a i g h t b a c k t o t h e H o s t
IRQ_ S T U B S 0 1 r e t u r n _ t o _ h o s t
/ / We' l l s a y n o t h i n g , y e t , a b o u t N M I
IRQ_ S T U B 2 h a n d l e _ n m i
/ / Other t r a p s a l s o r e t u r n t o t h e H o s t
IRQ_ S T U B S 3 3 1 r e t u r n _ t o _ h o s t
/ / All i n t e r r u p t s g o v i a t h e i r h a n d l e r s
IRQ_ S T U B S 3 2 1 2 7 d e l i v e r _ t o _ h o s t
/ / ' Cept s y s t e m c a l l s c o m i n g f r o m u s e r s p a c e
/ / Are t o g o t o t h e G u e s t , n e v e r t h e H o s t .
IRQ_ S T U B 1 2 8 r e t u r n _ t o _ h o s t
IRQ_ S T U B S 1 2 9 2 5 5 d e l i v e r _ t o _ h o s t
/ / The N M I , w h a t a f a b u l o u s b e a s t
/ / Which s w o o p s i n a n d s t o p s u s n o m a t t e r t h a t
/ / We' r e s u s p e n d e d b e t w e e n h e a v e n a n d h e l l ,
/ / ( Or m o r e l i k e l y b e t w e e n t h e H o s t a n d G u e s t )
/ / When i n i t c o m e s ! W e a r e d a z e d a n d c o n f u s e d
/ / So w e d o t h e s i m p l e s t t h i n g w h i c h o n e c a n .
/ / Though w e ' v e p u s h e d t h e t r a p n u m b e r a n d z e r o
/ / We d i s c a r d t h e m , r e t u r n , a n d h o p e w e l i v e .
2007-07-19 01:49:23 -07:00
handle_nmi :
addl $ 8 , % e s p
iret
2007-07-26 10:41:04 -07:00
/ / We a r e d o n e ; all that's left is Mastery
/ / And " m a k e M a s t e r y " i s a j o u r n e y l o n g
/ / Designed t o m a k e y o u r f i n g e r s i t c h t o c o d e .
/ / Here e n d s t h e t e x t , t h e f i l e a n d p o e m .
2007-07-19 01:49:23 -07:00
ENTRY( e n d _ s w i t c h e r _ t e x t )