2005-04-16 15:20:36 -07:00
/ *
2015-06-08 09:49:11 +02:00
* Copyright ( C ) 1 9 9 1 ,1 9 9 2 L i n u s T o r v a l d s
2005-04-16 15:20:36 -07:00
*
2015-06-08 09:49:11 +02:00
* entry_ 3 2 . S c o n t a i n s t h e s y s t e m - c a l l a n d l o w - l e v e l f a u l t a n d t r a p h a n d l i n g r o u t i n e s .
2005-04-16 15:20:36 -07:00
*
2015-10-05 17:48:13 -07:00
* Stack l a y o u t w h i l e r u n n i n g C c o d e :
2015-06-08 09:49:11 +02:00
* ptrace n e e d s t o h a v e a l l r e g i s t e r s o n t h e s t a c k .
* If t h e o r d e r h e r e i s c h a n g e d , i t n e e d s t o b e
* updated i n f o r k . c : c o p y _ p r o c e s s ( ) , s i g n a l . c : d o _ s i g n a l ( ) ,
2005-04-16 15:20:36 -07:00
* ptrace. c a n d p t r a c e . h
*
* 0 ( % esp) - % e b x
* 4 ( % esp) - % e c x
* 8 ( % esp) - % e d x
2015-06-08 22:35:33 +02:00
* C( % e s p ) - % e s i
2005-04-16 15:20:36 -07:00
* 1 0 ( % esp) - % e d i
* 1 4 ( % esp) - % e b p
* 1 8 ( % esp) - % e a x
* 1 C( % e s p ) - % d s
* 2 0 ( % esp) - % e s
2007-02-13 13:26:20 +01:00
* 2 4 ( % esp) - % f s
2009-02-09 22:17:40 +09:00
* 2 8 ( % esp) - % g s s a v e d i f f ! C O N F I G _ X 8 6 _ 3 2 _ L A Z Y _ G S
* 2 C( % e s p ) - o r i g _ e a x
* 3 0 ( % esp) - % e i p
* 3 4 ( % esp) - % c s
* 3 8 ( % esp) - % e f l a g s
* 3 C( % e s p ) - % o l d e s p
* 4 0 ( % esp) - % o l d s s
2005-04-16 15:20:36 -07:00
* /
# include < l i n u x / l i n k a g e . h >
2012-01-03 14:23:06 -05:00
# include < l i n u x / e r r . h >
2005-04-16 15:20:36 -07:00
# include < a s m / t h r e a d _ i n f o . h >
2006-07-03 00:24:43 -07:00
# include < a s m / i r q f l a g s . h >
2005-04-16 15:20:36 -07:00
# include < a s m / e r r n o . h >
# include < a s m / s e g m e n t . h >
# include < a s m / s m p . h >
2009-02-13 11:14:01 -08:00
# include < a s m / p a g e _ t y p e s . h >
2006-12-07 02:14:01 +01:00
# include < a s m / p e r c p u . h >
2008-03-25 22:16:32 +03:00
# include < a s m / p r o c e s s o r - f l a g s . h >
2008-06-21 23:47:27 +05:30
# include < a s m / f t r a c e . h >
2008-05-02 20:10:09 +02:00
# include < a s m / i r q _ v e c t o r s . h >
2016-01-26 22:12:04 +01:00
# include < a s m / c p u f e a t u r e s . h >
2011-08-25 16:10:33 -04:00
# include < a s m / a l t e r n a t i v e - a s m . h >
2012-04-20 12:19:50 -07:00
# include < a s m / a s m . h >
2012-09-21 13:58:10 -07:00
# include < a s m / s m a p . h >
2005-04-16 15:20:36 -07:00
2011-03-07 19:10:39 +01:00
.section .entry .text , " ax"
2006-12-07 02:14:08 +01:00
/ *
* We u s e m a c r o s f o r l o w - l e v e l o p e r a t i o n s w h i c h n e e d t o b e o v e r r i d d e n
* for p a r a v i r t u a l i z a t i o n . T h e f o l l o w i n g w i l l n e v e r c l o b b e r a n y r e g i s t e r s :
* INTERRUPT_ R E T U R N ( a k a . " i r e t " )
* GET_ C R 0 _ I N T O _ E A X ( a k a . " m o v l % c r0 , % e a x " )
2008-06-25 00:19:26 -04:00
* ENABLE_ I N T E R R U P T S _ S Y S E X I T ( a k a " s t i ; sysexit").
2006-12-07 02:14:08 +01:00
*
* For D I S A B L E _ I N T E R R U P T S / E N A B L E _ I N T E R R U P T S ( a k a " c l i " / " s t i " ) , y o u m u s t
* specify w h a t r e g i s t e r s c a n b e o v e r w r i t t e n ( C L B R _ N O N E , C L B R _ E A X / E D X / E C X / A N Y ) .
* Allowing a r e g i s t e r t o b e c l o b b e r e d c a n s h r i n k t h e p a r a v i r t r e p l a c e m e n t
* enough t o p a t c h i n l i n e , i n c r e a s i n g p e r f o r m a n c e .
* /
2005-04-16 15:20:36 -07:00
# ifdef C O N F I G _ P R E E M P T
2015-06-08 09:49:11 +02:00
# define p r e e m p t _ s t o p ( c l o b b e r s ) D I S A B L E _ I N T E R R U P T S ( c l o b b e r s ) ; TRACE_IRQS_OFF
2005-04-16 15:20:36 -07:00
# else
2015-06-08 09:49:11 +02:00
# define p r e e m p t _ s t o p ( c l o b b e r s )
# define r e s u m e _ k e r n e l r e s t o r e _ a l l
2005-04-16 15:20:36 -07:00
# endif
2006-07-03 00:24:43 -07:00
.macro TRACE_IRQS_IRET
# ifdef C O N F I G _ T R A C E _ I R Q F L A G S
2015-06-08 09:49:11 +02:00
testl $ X 8 6 _ E F L A G S _ I F , P T _ E F L A G S ( % e s p ) # i n t e r r u p t s o f f ?
jz 1 f
2006-07-03 00:24:43 -07:00
TRACE_ I R Q S _ O N
1 :
# endif
.endm
2009-02-09 22:17:40 +09:00
/ *
* User g s s a v e / r e s t o r e
*
* % gs i s u s e d f o r u s e r l a n d T L S a n d k e r n e l o n l y u s e s i t f o r s t a c k
* canary w h i c h i s r e q u i r e d t o b e a t % g s : 2 0 b y g c c . R e a d t h e c o m m e n t
* at t h e t o p o f s t a c k p r o t e c t o r . h f o r m o r e i n f o .
*
* Local l a b e l s 9 8 a n d 9 9 a r e u s e d .
* /
# ifdef C O N F I G _ X 8 6 _ 3 2 _ L A Z Y _ G S
/* unfortunately push/pop can't be no-op */
.macro PUSH_GS
2015-06-08 09:49:11 +02:00
pushl $ 0
2009-02-09 22:17:40 +09:00
.endm
.macro POP_GS pop=0
2015-06-08 09:49:11 +02:00
addl $ ( 4 + \ p o p ) , % e s p
2009-02-09 22:17:40 +09:00
.endm
.macro POP_GS_EX
.endm
/* all the rest are no-op */
.macro PTGS_TO_GS
.endm
.macro PTGS_TO_GS_EX
.endm
.macro GS_TO_REG reg
.endm
.macro REG_TO_PTGS reg
.endm
.macro SET_KERNEL_GS reg
.endm
# else / * C O N F I G _ X 8 6 _ 3 2 _ L A Z Y _ G S * /
.macro PUSH_GS
2015-06-08 09:49:11 +02:00
pushl % g s
2009-02-09 22:17:40 +09:00
.endm
.macro POP_GS pop=0
2015-06-08 09:49:11 +02:00
98 : popl % g s
2009-02-09 22:17:40 +09:00
.if \ pop < > 0
2015-06-08 22:35:33 +02:00
add $ \ p o p , % e s p
2009-02-09 22:17:40 +09:00
.endif
.endm
.macro POP_GS_EX
.pushsection .fixup , " ax"
2015-06-08 09:49:11 +02:00
99 : movl $ 0 , ( % e s p )
jmp 9 8 b
2009-02-09 22:17:40 +09:00
.popsection
2015-06-08 09:49:11 +02:00
_ ASM_ E X T A B L E ( 9 8 b , 9 9 b )
2009-02-09 22:17:40 +09:00
.endm
.macro PTGS_TO_GS
2015-06-08 09:49:11 +02:00
98 : mov P T _ G S ( % e s p ) , % g s
2009-02-09 22:17:40 +09:00
.endm
.macro PTGS_TO_GS_EX
.pushsection .fixup , " ax"
2015-06-08 09:49:11 +02:00
99 : movl $ 0 , P T _ G S ( % e s p )
jmp 9 8 b
2009-02-09 22:17:40 +09:00
.popsection
2015-06-08 09:49:11 +02:00
_ ASM_ E X T A B L E ( 9 8 b , 9 9 b )
2009-02-09 22:17:40 +09:00
.endm
.macro GS_TO_REG reg
2015-06-08 09:49:11 +02:00
movl % g s , \ r e g
2009-02-09 22:17:40 +09:00
.endm
.macro REG_TO_PTGS reg
2015-06-08 09:49:11 +02:00
movl \ r e g , P T _ G S ( % e s p )
2009-02-09 22:17:40 +09:00
.endm
.macro SET_KERNEL_GS reg
2015-06-08 09:49:11 +02:00
movl $ ( _ _ K E R N E L _ S T A C K _ C A N A R Y ) , \ r e g
movl \ r e g , % g s
2009-02-09 22:17:40 +09:00
.endm
2015-06-08 09:49:11 +02:00
# endif / * C O N F I G _ X 8 6 _ 3 2 _ L A Z Y _ G S * /
2009-02-09 22:17:40 +09:00
2015-10-05 17:48:14 -07:00
.macro SAVE_ALL pt_ r e g s _ a x = % e a x
2009-02-09 22:17:40 +09:00
cld
2009-02-09 22:17:40 +09:00
PUSH_ G S
2015-06-08 09:49:11 +02:00
pushl % f s
pushl % e s
pushl % d s
2015-10-05 17:48:14 -07:00
pushl \ p t _ r e g s _ a x
2015-06-08 09:49:11 +02:00
pushl % e b p
pushl % e d i
pushl % e s i
pushl % e d x
pushl % e c x
pushl % e b x
movl $ ( _ _ U S E R _ D S ) , % e d x
movl % e d x , % d s
movl % e d x , % e s
movl $ ( _ _ K E R N E L _ P E R C P U ) , % e d x
movl % e d x , % f s
2009-02-09 22:17:40 +09:00
SET_ K E R N E L _ G S % e d x
2009-02-09 22:17:40 +09:00
.endm
2005-04-16 15:20:36 -07:00
2009-02-09 22:17:40 +09:00
.macro RESTORE_INT_REGS
2015-06-08 09:49:11 +02: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 % e a x
2009-02-09 22:17:40 +09:00
.endm
2005-04-16 15:20:36 -07:00
2009-02-09 22:17:40 +09:00
.macro RESTORE_REGS pop=0
2009-02-09 22:17:40 +09:00
RESTORE_ I N T _ R E G S
2015-06-08 09:49:11 +02:00
1 : popl % d s
2 : popl % e s
3 : popl % f s
2009-02-09 22:17:40 +09:00
POP_ G S \ p o p
2009-02-09 22:17:40 +09:00
.pushsection .fixup , " ax"
2015-06-08 09:49:11 +02:00
4 : movl $ 0 , ( % e s p )
jmp 1 b
5 : movl $ 0 , ( % e s p )
jmp 2 b
6 : movl $ 0 , ( % e s p )
jmp 3 b
[PATCH] i386: Use %gs as the PDA base-segment in the kernel
This patch is the meat of the PDA change. This patch makes several related
changes:
1: Most significantly, %gs is now used in the kernel. This means that on
entry, the old value of %gs is saved away, and it is reloaded with
__KERNEL_PDA.
2: entry.S constructs the stack in the shape of struct pt_regs, and this
is passed around the kernel so that the process's saved register
state can be accessed.
Unfortunately struct pt_regs doesn't currently have space for %gs
(or %fs). This patch extends pt_regs to add space for gs (no space
is allocated for %fs, since it won't be used, and it would just
complicate the code in entry.S to work around the space).
3: Because %gs is now saved on the stack like %ds, %es and the integer
registers, there are a number of places where it no longer needs to
be handled specially; namely context switch, and saving/restoring the
register state in a signal context.
4: And since kernel threads run in kernel space and call normal kernel
code, they need to be created with their %gs == __KERNEL_PDA.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Andi Kleen <ak@suse.de>
Cc: Chuck Ebbert <76306.1226@compuserve.com>
Cc: Zachary Amsden <zach@vmware.com>
Cc: Jan Beulich <jbeulich@novell.com>
Cc: Andi Kleen <ak@suse.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
2006-12-07 02:14:02 +01:00
.popsection
2015-06-08 09:49:11 +02:00
_ ASM_ E X T A B L E ( 1 b , 4 b )
_ ASM_ E X T A B L E ( 2 b , 5 b )
_ ASM_ E X T A B L E ( 3 b , 6 b )
2009-02-09 22:17:40 +09:00
POP_ G S _ E X
2009-02-09 22:17:40 +09:00
.endm
2005-04-16 15:20:36 -07:00
ENTRY( r e t _ f r o m _ f o r k )
2015-06-08 09:49:11 +02:00
pushl % e a x
call s c h e d u l e _ t a i l
2005-04-16 15:20:36 -07:00
GET_ T H R E A D _ I N F O ( % e b p )
2015-06-08 09:49:11 +02:00
popl % e a x
pushl $ 0 x02 0 2 # R e s e t k e r n e l e f l a g s
x86/debug: Remove perpetually broken, unmaintainable dwarf annotations
So the dwarf2 annotations in low level assembly code have
become an increasing hindrance: unreadable, messy macros
mixed into some of the most security sensitive code paths
of the Linux kernel.
These debug info annotations don't even buy the upstream
kernel anything: dwarf driven stack unwinding has caused
problems in the past so it's out of tree, and the upstream
kernel only uses the much more robust framepointers based
stack unwinding method.
In addition to that there's a steady, slow bitrot going
on with these annotations, requiring frequent fixups.
There's no tooling and no functionality upstream that
keeps it correct.
So burn down the sick forest, allowing new, healthier growth:
27 files changed, 350 insertions(+), 1101 deletions(-)
Someone who has the willingness and time to do this
properly can attempt to reintroduce dwarf debuginfo in x86
assembly code plus dwarf unwinding from first principles,
with the following conditions:
- it should be maximally readable, and maximally low-key to
'ordinary' code reading and maintenance.
- find a build time method to insert dwarf annotations
automatically in the most common cases, for pop/push
instructions that manipulate the stack pointer. This could
be done for example via a preprocessing step that just
looks for common patterns - plus special annotations for
the few cases where we want to depart from the default.
We have hundreds of CFI annotations, so automating most of
that makes sense.
- it should come with build tooling checks that ensure that
CFI annotations are sensible. We've seen such efforts from
the framepointer side, and there's no reason it couldn't be
done on the dwarf side.
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Frédéric Weisbecker <fweisbec@gmail.com
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jan Beulich <JBeulich@suse.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-05-28 12:21:47 +02:00
popfl
2015-10-05 17:48:13 -07:00
/* When we fork, we trace the syscall return in the child, too. */
movl % e s p , % e a x
call s y s c a l l _ r e t u r n _ s l o w p a t h
jmp r e s t o r e _ a l l
2007-02-13 13:26:24 +01:00
END( r e t _ f r o m _ f o r k )
2005-04-16 15:20:36 -07:00
2012-10-10 21:35:42 -04:00
ENTRY( r e t _ f r o m _ k e r n e l _ t h r e a d )
2015-06-08 09:49:11 +02:00
pushl % e a x
call s c h e d u l e _ t a i l
2012-08-02 23:05:11 +04:00
GET_ T H R E A D _ I N F O ( % e b p )
2015-06-08 09:49:11 +02:00
popl % e a x
pushl $ 0 x02 0 2 # R e s e t k e r n e l e f l a g s
x86/debug: Remove perpetually broken, unmaintainable dwarf annotations
So the dwarf2 annotations in low level assembly code have
become an increasing hindrance: unreadable, messy macros
mixed into some of the most security sensitive code paths
of the Linux kernel.
These debug info annotations don't even buy the upstream
kernel anything: dwarf driven stack unwinding has caused
problems in the past so it's out of tree, and the upstream
kernel only uses the much more robust framepointers based
stack unwinding method.
In addition to that there's a steady, slow bitrot going
on with these annotations, requiring frequent fixups.
There's no tooling and no functionality upstream that
keeps it correct.
So burn down the sick forest, allowing new, healthier growth:
27 files changed, 350 insertions(+), 1101 deletions(-)
Someone who has the willingness and time to do this
properly can attempt to reintroduce dwarf debuginfo in x86
assembly code plus dwarf unwinding from first principles,
with the following conditions:
- it should be maximally readable, and maximally low-key to
'ordinary' code reading and maintenance.
- find a build time method to insert dwarf annotations
automatically in the most common cases, for pop/push
instructions that manipulate the stack pointer. This could
be done for example via a preprocessing step that just
looks for common patterns - plus special annotations for
the few cases where we want to depart from the default.
We have hundreds of CFI annotations, so automating most of
that makes sense.
- it should come with build tooling checks that ensure that
CFI annotations are sensible. We've seen such efforts from
the framepointer side, and there's no reason it couldn't be
done on the dwarf side.
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Frédéric Weisbecker <fweisbec@gmail.com
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jan Beulich <JBeulich@suse.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-05-28 12:21:47 +02:00
popfl
2015-06-08 09:49:11 +02:00
movl P T _ E B P ( % e s p ) , % e a x
call * P T _ E B X ( % e s p )
movl $ 0 , P T _ E A X ( % e s p )
2015-10-05 17:48:13 -07:00
/ *
* Kernel t h r e a d s r e t u r n t o u s e r s p a c e a s i f r e t u r n i n g f r o m a s y s c a l l .
* We s h o u l d c h e c k w h e t h e r a n y t h i n g a c t u a l l y u s e s t h i s p a t h a n d , i f s o ,
* consider s w i t c h i n g i t o v e r t o r e t _ f r o m _ f o r k .
* /
movl % e s p , % e a x
call s y s c a l l _ r e t u r n _ s l o w p a t h
jmp r e s t o r e _ a l l
2012-10-10 21:35:42 -04:00
ENDPROC( r e t _ f r o m _ k e r n e l _ t h r e a d )
2012-08-02 23:05:11 +04:00
2005-04-16 15:20:36 -07:00
/ *
* Return t o u s e r m o d e i s n o t a s c o m p l e x a s a l l t h i s l o o k s ,
* but w e w a n t t h e d e f a u l t p a t h f o r a s y s t e m c a l l r e t u r n t o
* go a s q u i c k l y a s p o s s i b l e w h i c h i s w h y s o m e o f t h i s i s
* less c l e a r t h a n i t o t h e r w i s e s h o u l d b e .
* /
# userspace r e s u m p t i o n s t u b b y p a s s i n g s y s c a l l e x i t t r a c i n g
ALIGN
ret_from_exception :
2006-12-07 02:14:08 +01:00
preempt_ s t o p ( C L B R _ A N Y )
2005-04-16 15:20:36 -07:00
ret_from_intr :
GET_ T H R E A D _ I N F O ( % e b p )
2012-03-22 21:39:25 +01:00
# ifdef C O N F I G _ V M 8 6
2015-06-08 09:49:11 +02:00
movl P T _ E F L A G S ( % e s p ) , % e a x # m i x E F L A G S a n d C S
movb P T _ C S ( % e s p ) , % a l
andl $ ( X 8 6 _ E F L A G S _ V M | S E G M E N T _ R P L _ M A S K ) , % e a x
2012-03-22 21:39:25 +01:00
# else
/ *
2012-08-02 23:05:11 +04:00
* We c a n b e c o m i n g h e r e f r o m c h i l d s p a w n e d b y k e r n e l _ t h r e a d ( ) .
2012-03-22 21:39:25 +01:00
* /
2015-06-08 09:49:11 +02:00
movl P T _ C S ( % e s p ) , % e a x
andl $ S E G M E N T _ R P L _ M A S K , % e a x
2012-03-22 21:39:25 +01:00
# endif
2015-06-08 09:49:11 +02:00
cmpl $ U S E R _ R P L , % e a x
jb r e s u m e _ k e r n e l # n o t r e t u r n i n g t o v 8086 o r u s e r s p a c e
[PATCH] i386: Use %gs as the PDA base-segment in the kernel
This patch is the meat of the PDA change. This patch makes several related
changes:
1: Most significantly, %gs is now used in the kernel. This means that on
entry, the old value of %gs is saved away, and it is reloaded with
__KERNEL_PDA.
2: entry.S constructs the stack in the shape of struct pt_regs, and this
is passed around the kernel so that the process's saved register
state can be accessed.
Unfortunately struct pt_regs doesn't currently have space for %gs
(or %fs). This patch extends pt_regs to add space for gs (no space
is allocated for %fs, since it won't be used, and it would just
complicate the code in entry.S to work around the space).
3: Because %gs is now saved on the stack like %ds, %es and the integer
registers, there are a number of places where it no longer needs to
be handled specially; namely context switch, and saving/restoring the
register state in a signal context.
4: And since kernel threads run in kernel space and call normal kernel
code, they need to be created with their %gs == __KERNEL_PDA.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Andi Kleen <ak@suse.de>
Cc: Chuck Ebbert <76306.1226@compuserve.com>
Cc: Zachary Amsden <zach@vmware.com>
Cc: Jan Beulich <jbeulich@novell.com>
Cc: Andi Kleen <ak@suse.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
2006-12-07 02:14:02 +01:00
2005-04-16 15:20:36 -07:00
ENTRY( r e s u m e _ u s e r s p a c e )
2015-07-31 14:41:09 -07:00
DISABLE_ I N T E R R U P T S ( C L B R _ A N Y )
2008-06-06 10:14:08 +02:00
TRACE_ I R Q S _ O F F
2015-07-31 14:41:09 -07:00
movl % e s p , % e a x
call p r e p a r e _ e x i t _ t o _ u s e r m o d e
2015-06-08 09:49:11 +02:00
jmp r e s t o r e _ a l l
2007-02-13 13:26:24 +01:00
END( r e t _ f r o m _ e x c e p t i o n )
2005-04-16 15:20:36 -07:00
# ifdef C O N F I G _ P R E E M P T
ENTRY( r e s u m e _ k e r n e l )
2006-12-07 02:14:08 +01:00
DISABLE_ I N T E R R U P T S ( C L B R _ A N Y )
2005-04-16 15:20:36 -07:00
need_resched :
2015-06-08 09:49:11 +02:00
cmpl $ 0 , P E R _ C P U _ V A R ( _ _ p r e e m p t _ c o u n t )
jnz r e s t o r e _ a l l
testl $ X 8 6 _ E F L A G S _ I F , P T _ E F L A G S ( % e s p ) # i n t e r r u p t s o f f ( e x c e p t i o n p a t h ) ?
jz r e s t o r e _ a l l
call p r e e m p t _ s c h e d u l e _ i r q
jmp n e e d _ r e s c h e d
2007-02-13 13:26:24 +01:00
END( r e s u m e _ k e r n e l )
2005-04-16 15:20:36 -07:00
# endif
2015-06-08 09:49:11 +02:00
# SYSENTER c a l l h a n d l e r s t u b
2015-06-08 08:33:56 +02:00
ENTRY( e n t r y _ S Y S E N T E R _ 3 2 )
2015-06-08 09:49:11 +02:00
movl T S S _ s y s e n t e r _ s p0 ( % e s p ) , % e s p
2005-04-16 15:20:36 -07:00
sysenter_past_esp :
2015-10-05 17:48:15 -07:00
pushl $ _ _ U S E R _ D S / * p t _ r e g s - > s s * /
2015-12-16 23:18:48 -08:00
pushl % e b p / * p t _ r e g s - > s p ( s t a s h e d i n b p ) * /
2015-10-05 17:48:15 -07:00
pushfl / * p t _ r e g s - > f l a g s ( e x c e p t I F = 0 ) * /
2016-02-23 13:19:29 -08:00
ASM_ C L A C / * C l e a r A C a f t e r s a v i n g F L A G S * /
2015-10-05 17:48:15 -07:00
orl $ X 8 6 _ E F L A G S _ I F , ( % e s p ) / * F i x I F * /
pushl $ _ _ U S E R _ C S / * p t _ r e g s - > c s * /
pushl $ 0 / * p t _ r e g s - > i p = 0 ( p l a c e h o l d e r ) * /
pushl % e a x / * p t _ r e g s - > o r i g _ a x * /
SAVE_ A L L p t _ r e g s _ a x = $ - E N O S Y S / * s a v e r e s t * /
2006-07-03 00:24:43 -07:00
/ *
2015-10-05 17:48:15 -07:00
* User m o d e i s t r a c e d a s t h o u g h I R Q s a r e o n , a n d S Y S E N T E R
* turned t h e m o f f .
[PATCH] vdso: randomize the i386 vDSO by moving it into a vma
Move the i386 VDSO down into a vma and thus randomize it.
Besides the security implications, this feature also helps debuggers, which
can COW a vma-backed VDSO just like a normal DSO and can thus do
single-stepping and other debugging features.
It's good for hypervisors (Xen, VMWare) too, which typically live in the same
high-mapped address space as the VDSO, hence whenever the VDSO is used, they
get lots of guest pagefaults and have to fix such guest accesses up - which
slows things down instead of speeding things up (the primary purpose of the
VDSO).
There's a new CONFIG_COMPAT_VDSO (default=y) option, which provides support
for older glibcs that still rely on a prelinked high-mapped VDSO. Newer
distributions (using glibc 2.3.3 or later) can turn this option off. Turning
it off is also recommended for security reasons: attackers cannot use the
predictable high-mapped VDSO page as syscall trampoline anymore.
There is a new vdso=[0|1] boot option as well, and a runtime
/proc/sys/vm/vdso_enabled sysctl switch, that allows the VDSO to be turned
on/off.
(This version of the VDSO-randomization patch also has working ELF
coredumping, the previous patch crashed in the coredumping code.)
This code is a combined work of the exec-shield VDSO randomization
code and Gerd Hoffmann's hypervisor-centric VDSO patch. Rusty Russell
started this patch and i completed it.
[akpm@osdl.org: cleanups]
[akpm@osdl.org: compile fix]
[akpm@osdl.org: compile fix 2]
[akpm@osdl.org: compile fix 3]
[akpm@osdl.org: revernt MAXMEM change]
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Arjan van de Ven <arjan@infradead.org>
Cc: Gerd Hoffmann <kraxel@suse.de>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Zachary Amsden <zach@vmware.com>
Cc: Andi Kleen <ak@muc.de>
Cc: Jan Beulich <jbeulich@novell.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-06-27 02:53:50 -07:00
* /
2006-07-03 00:24:43 -07:00
TRACE_ I R Q S _ O F F
2015-10-05 17:48:15 -07:00
movl % e s p , % e a x
call d o _ f a s t _ s y s c a l l _ 3 2
2015-11-19 16:55:45 -05:00
/* XEN PV guests always use IRET path */
ALTERNATIVE " t e s t l % e a x , % e a x ; jz .Lsyscall_32_done", \
" jmp . L s y s c a l l _ 3 2 _ d o n e " , X 8 6 _ F E A T U R E _ X E N P V
2015-10-05 17:48:15 -07:00
/* Opportunistic SYSEXIT */
TRACE_ I R Q S _ O N / * U s e r m o d e t r a c e s a s I R Q s o n . * /
movl P T _ E I P ( % e s p ) , % e d x / * p t _ r e g s - > i p * /
movl P T _ O L D E S P ( % e s p ) , % e c x / * p t _ r e g s - > s p * /
2015-10-16 15:42:55 -07:00
1 : mov P T _ F S ( % e s p ) , % f s
PTGS_ T O _ G S
2015-10-05 17:48:15 -07:00
popl % e b x / * p t _ r e g s - > b x * /
addl $ 2 * 4 , % e s p / * s k i p p t _ r e g s - > c x a n d p t _ r e g s - > d x * /
popl % e s i / * p t _ r e g s - > s i * /
popl % e d i / * p t _ r e g s - > d i * /
popl % e b p / * p t _ r e g s - > b p * /
popl % e a x / * p t _ r e g s - > a x * /
/ *
* Return b a c k t o t h e v D S O , w h i c h w i l l p o p e c x a n d e d x .
* Don' t b o t h e r w i t h D S a n d E S ( t h e y a l r e a d y c o n t a i n _ _ U S E R _ D S ) .
* /
2015-11-19 16:55:46 -05:00
sti
sysexit
2008-06-24 04:16:52 -07:00
2015-06-08 09:49:11 +02:00
.pushsection .fixup , " ax"
2 : movl $ 0 , P T _ F S ( % e s p )
jmp 1 b
[PATCH] i386: Use %gs as the PDA base-segment in the kernel
This patch is the meat of the PDA change. This patch makes several related
changes:
1: Most significantly, %gs is now used in the kernel. This means that on
entry, the old value of %gs is saved away, and it is reloaded with
__KERNEL_PDA.
2: entry.S constructs the stack in the shape of struct pt_regs, and this
is passed around the kernel so that the process's saved register
state can be accessed.
Unfortunately struct pt_regs doesn't currently have space for %gs
(or %fs). This patch extends pt_regs to add space for gs (no space
is allocated for %fs, since it won't be used, and it would just
complicate the code in entry.S to work around the space).
3: Because %gs is now saved on the stack like %ds, %es and the integer
registers, there are a number of places where it no longer needs to
be handled specially; namely context switch, and saving/restoring the
register state in a signal context.
4: And since kernel threads run in kernel space and call normal kernel
code, they need to be created with their %gs == __KERNEL_PDA.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Andi Kleen <ak@suse.de>
Cc: Chuck Ebbert <76306.1226@compuserve.com>
Cc: Zachary Amsden <zach@vmware.com>
Cc: Jan Beulich <jbeulich@novell.com>
Cc: Andi Kleen <ak@suse.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
2006-12-07 02:14:02 +01:00
.popsection
2015-06-08 09:49:11 +02:00
_ ASM_ E X T A B L E ( 1 b , 2 b )
2009-02-09 22:17:40 +09:00
PTGS_ T O _ G S _ E X
2015-06-08 08:33:56 +02:00
ENDPROC( e n t r y _ S Y S E N T E R _ 3 2 )
2005-04-16 15:20:36 -07:00
# system c a l l h a n d l e r s t u b
2015-06-08 08:42:03 +02:00
ENTRY( e n t r y _ I N T 8 0 _ 3 2 )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-10-05 17:48:14 -07:00
pushl % e a x / * p t _ r e g s - > o r i g _ a x * /
2015-10-05 17:48:15 -07:00
SAVE_ A L L p t _ r e g s _ a x = $ - E N O S Y S / * s a v e r e s t * /
2015-10-05 17:48:14 -07:00
/ *
2015-10-16 15:42:54 -07:00
* User m o d e i s t r a c e d a s t h o u g h I R Q s a r e o n . U n l i k e t h e 6 4 - b i t
* case, I N T 8 0 i s a t r a p g a t e o n 3 2 - b i t k e r n e l s , s o i n t e r r u p t s
* are a l r e a d y o n ( u n l e s s u s e r c o d e i s m e s s i n g a r o u n d w i t h i o p l ) .
2015-10-05 17:48:14 -07:00
* /
movl % e s p , % e a x
2015-10-16 15:42:54 -07:00
call d o _ s y s c a l l _ 3 2 _ i r q s _ o n
2015-10-05 17:48:15 -07:00
.Lsyscall_32_done :
2005-04-16 15:20:36 -07:00
restore_all :
i386: fix return to 16-bit stack from NMI handler
Returning to a task with a 16-bit stack requires special care: the iret
instruction does not restore the high word of esp in that case. The
espfix code fixes this, but currently is not invoked on NMIs. This means
that a running task gets the upper word of esp clobbered due intervening
NMIs. To reproduce, compile and run the following program with the nmi
watchdog enabled (nmi_watchdog=2 on the command line). Using gdb you can
see that the high bits of esp contain garbage, while the low bits are
still correct.
This patch puts the espfix code back into the NMI code path.
The patch is slightly complicated due to the irqtrace infrastructure not
being NMI-safe. The NMI return path cannot call TRACE_IRQS_IRET.
Otherwise, the tail of the normal iret-code is correct for the nmi code
path too. To be able to share this code-path, the TRACE_IRQS_IRET was
move up a bit. The espfix code exists after the TRACE_IRQS_IRET, but
this code explicitly disables interrupts. This short interrupts-off
section is now not traced anymore. The return-to-kernel path now always
includes the preliminary test to decide if the espfix code should be
called. This is never the case, but doing it this way keeps the patch as
simple as possible and the few extra instructions should not affect
timing in any significant way.
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <asm/ldt.h>
int modify_ldt(int func, void *ptr, unsigned long bytecount)
{
return syscall(SYS_modify_ldt, func, ptr, bytecount);
}
/* this is assumed to be usable */
#define SEGBASEADDR 0x10000
#define SEGLIMIT 0x20000
/* 16-bit segment */
struct user_desc desc = {
.entry_number = 0,
.base_addr = SEGBASEADDR,
.limit = SEGLIMIT,
.seg_32bit = 0,
.contents = 0, /* ??? */
.read_exec_only = 0,
.limit_in_pages = 0,
.seg_not_present = 0,
.useable = 1
};
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
/* map a 64 kb segment */
char *pointer = mmap((void *)SEGBASEADDR, SEGLIMIT+1,
PROT_EXEC|PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (pointer == NULL) {
printf("could not map space\n");
return 0;
}
/* write ldt, new mode */
int err = modify_ldt(0x11, &desc, sizeof(desc));
if (err) {
printf("error modifying ldt: %i\n", err);
return 0;
}
for (int i=0; i<1000; i++) {
asm volatile (
"pusha\n\t"
"mov %ss, %eax\n\t" /* preserve ss:esp */
"mov %esp, %ebp\n\t"
"push $7\n\t" /* index 0, ldt, user mode */
"push $65536-4096\n\t" /* esp */
"lss (%esp), %esp\n\t" /* switch to new stack */
"push %eax\n\t" /* save old ss:esp on new stack */
"push %ebp\n\t"
"add $17*65536, %esp\n\t" /* set high bits */
"mov %esp, %edx\n\t"
"mov $10000000, %ecx\n\t" /* wait... */
"1: loop 1b\n\t" /* ... a bit */
"cmp %esp, %edx\n\t"
"je 1f\n\t"
"ud2\n\t" /* esp changed inexplicably! */
"1:\n\t"
"sub $17*65536, %esp\n\t" /* restore high bits */
"lss (%esp), %esp\n\t" /* restore old ss:esp */
"popa\n\t");
printf("\rx%ix", i);
}
return 0;
}
Signed-off-by: Alexander van Heukelum <heukelum@fastmail.fm>
Acked-by: Stas Sergeev <stsp@aknet.ru>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-06-18 00:35:57 +02:00
TRACE_ I R Q S _ I R E T
restore_all_notrace :
2014-05-04 10:36:22 -07:00
# ifdef C O N F I G _ X 8 6 _ E S P F I X 3 2
x86/entry/32: Introduce and use X86_BUG_ESPFIX instead of paravirt_enabled
x86_64 has very clean espfix handling on paravirt: espfix64 is set
up in native_iret, so paravirt systems that override iret bypass
espfix64 automatically. This is robust and straightforward.
x86_32 is messier. espfix is set up before the IRET paravirt patch
point, so it can't be directly conditionalized on whether we use
native_iret. We also can't easily move it into native_iret without
regressing performance due to a bizarre consideration. Specifically,
on 64-bit kernels, the logic is:
if (regs->ss & 0x4)
setup_espfix;
On 32-bit kernels, the logic is:
if ((regs->ss & 0x4) && (regs->cs & 0x3) == 3 &&
(regs->flags & X86_EFLAGS_VM) == 0)
setup_espfix;
The performance of setup_espfix itself is essentially irrelevant, but
the comparison happens on every IRET so its performance matters. On
x86_64, there's no need for any registers except flags to implement
the comparison, so we fold the whole thing into native_iret. On
x86_32, we don't do that because we need a free register to
implement the comparison efficiently. We therefore do espfix setup
before restoring registers on x86_32.
This patch gets rid of the explicit paravirt_enabled check by
introducing X86_BUG_ESPFIX on 32-bit systems and using an ALTERNATIVE
to skip espfix on paravirt systems where iret != native_iret. This is
also messy, but it's at least in line with other things we do.
This improves espfix performance by removing a branch, but no one
cares. More importantly, it removes a paravirt_enabled user, which is
good because paravirt_enabled is ill-defined and is going away.
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Reviewed-by: Borislav Petkov <bp@suse.de>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Luis R. Rodriguez <mcgrof@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: boris.ostrovsky@oracle.com
Cc: david.vrabel@citrix.com
Cc: konrad.wilk@oracle.com
Cc: lguest@lists.ozlabs.org
Cc: xen-devel@lists.xensource.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-02-29 15:50:19 -08:00
ALTERNATIVE " j m p r e s t o r e _ n o c h e c k " , " " , X 8 6 _ B U G _ E S P F I X
2015-06-08 09:49:11 +02:00
movl P T _ E F L A G S ( % e s p ) , % e a x # m i x E F L A G S , S S a n d C S
/ *
* Warning : PT_ O L D S S ( % e s p ) c o n t a i n s t h e w r o n g / r a n d o m v a l u e s i f w e
* are r e t u r n i n g t o t h e k e r n e l .
* See c o m m e n t s i n p r o c e s s . c : c o p y _ t h r e a d ( ) f o r d e t a i l s .
* /
movb P T _ O L D S S ( % e s p ) , % a h
movb P T _ C S ( % e s p ) , % a l
andl $ ( X 8 6 _ E F L A G S _ V M | ( S E G M E N T _ T I _ M A S K < < 8 ) | S E G M E N T _ R P L _ M A S K ) , % e a x
cmpl $ ( ( S E G M E N T _ L D T < < 8 ) | U S E R _ R P L ) , % e a x
je l d t _ s s # r e t u r n i n g t o u s e r - s p a c e w i t h L D T S S
2014-05-04 10:36:22 -07:00
# endif
2005-04-16 15:20:36 -07:00
restore_nocheck :
2015-06-08 09:49:11 +02:00
RESTORE_ R E G S 4 # s k i p o r i g _ e a x / e r r o r _ c o d e
2008-02-13 23:29:53 +02:00
irq_return :
2008-02-09 23:24:08 +01:00
INTERRUPT_ R E T U R N
2015-06-08 09:49:11 +02:00
.section .fixup , " ax"
ENTRY( i r e t _ e x c )
pushl $ 0 # n o e r r o r c o d e
pushl $ d o _ i r e t _ e r r o r
jmp e r r o r _ c o d e
2005-04-16 15:20:36 -07:00
.previous
2015-06-08 09:49:11 +02:00
_ ASM_ E X T A B L E ( i r q _ r e t u r n , i r e t _ e x c )
2005-04-16 15:20:36 -07:00
2014-05-04 10:36:22 -07:00
# ifdef C O N F I G _ X 8 6 _ E S P F I X 3 2
2005-04-16 15:20:36 -07:00
ldt_ss :
i386: fix/simplify espfix stack switching, move it into assembly
The espfix code triggers if we have a protected mode userspace
application with a 16-bit stack. On returning to userspace, with iret,
the CPU doesn't restore the high word of the stack pointer. This is an
"official" bug, and the work-around used in the kernel is to temporarily
switch to a 32-bit stack segment/pointer pair where the high word of the
pointer is equal to the high word of the userspace stackpointer.
The current implementation uses THREAD_SIZE to determine the cut-off,
but there is no good reason not to use the more natural 64kb... However,
implementing this by simply substituting THREAD_SIZE with 65536 in
patch_espfix_desc crashed the test application. patch_espfix_desc tries
to do what is described above, but gets it subtly wrong if the userspace
stack pointer is just below a multiple of THREAD_SIZE: an overflow
occurs to bit 13... With a bit of luck, when the kernelspace
stackpointer is just below a 64kb-boundary, the overflow then ripples
trough to bit 16 and userspace will see its stack pointer changed by
65536.
This patch moves all espfix code into entry_32.S. Selecting a 16-bit
cut-off simplifies the code. The game with changing the limit dynamically
is removed too. It complicates matters and I see no value in it. Changing
only the top 16-bit word of ESP is one instruction and it also implies
that only two bytes of the ESPFIX GDT entry need to be changed and this
can be implemented in just a handful simple to understand instructions.
As a side effect, the operation to compute the original ESP from the
ESPFIX ESP and the GDT entry simplifies a bit too, and the remaining
three instructions have been expanded inline in entry_32.S.
impact: can now reliably run userspace with ESP=xxxxfffc on 16-bit
stack segment
Signed-off-by: Alexander van Heukelum <heukelum@fastmail.fm>
Acked-by: Stas Sergeev <stsp@aknet.ru>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-06-18 00:35:58 +02:00
/ *
* Setup a n d s w i t c h t o E S P F I X s t a c k
*
* We' r e r e t u r n i n g t o u s e r s p a c e w i t h a 1 6 b i t s t a c k . T h e C P U w i l l n o t
* restore t h e h i g h w o r d o f E S P f o r u s o n e x e c u t i n g i r e t . . . T h i s i s a n
* " official" b u g o f a l l t h e x86 - c o m p a t i b l e C P U s , w h i c h w e c a n w o r k
* around t o m a k e d o s e m u a n d w i n e h a p p y . W e d o t h i s b y p r e l o a d i n g t h e
* high w o r d o f E S P w i t h t h e h i g h w o r d o f t h e u s e r s p a c e E S P w h i l e
* compensating f o r t h e o f f s e t b y c h a n g i n g t o t h e E S P F I X s e g m e n t w i t h
* a b a s e a d d r e s s t h a t m a t c h e s f o r t h e d i f f e r e n c e .
* /
2010-07-31 12:48:23 -04:00
# define G D T _ E S P F I X _ S S P E R _ C P U _ V A R ( g d t _ p a g e ) + ( G D T _ E N T R Y _ E S P F I X _ S S * 8 )
2015-06-08 09:49:11 +02:00
mov % e s p , % e d x / * l o a d k e r n e l e s p * /
mov P T _ O L D E S P ( % e s p ) , % e a x / * l o a d u s e r s p a c e e s p * /
mov % d x , % a x / * e a x : n e w k e r n e l e s p * /
2015-06-08 22:35:33 +02:00
sub % e a x , % e d x / * o f f s e t ( l o w w o r d i s 0 ) * /
shr $ 1 6 , % e d x
2015-06-08 09:49:11 +02:00
mov % d l , G D T _ E S P F I X _ S S + 4 / * b i t s 1 6 . . 2 3 * /
mov % d h , G D T _ E S P F I X _ S S + 7 / * b i t s 2 4 . . 3 1 * /
pushl $ _ _ E S P F I X _ S S
pushl % e a x / * n e w k e r n e l e s p * /
/ *
* Disable i n t e r r u p t s , b u t d o n o t i r q t r a c e t h i s s e c t i o n : w e
i386: fix return to 16-bit stack from NMI handler
Returning to a task with a 16-bit stack requires special care: the iret
instruction does not restore the high word of esp in that case. The
espfix code fixes this, but currently is not invoked on NMIs. This means
that a running task gets the upper word of esp clobbered due intervening
NMIs. To reproduce, compile and run the following program with the nmi
watchdog enabled (nmi_watchdog=2 on the command line). Using gdb you can
see that the high bits of esp contain garbage, while the low bits are
still correct.
This patch puts the espfix code back into the NMI code path.
The patch is slightly complicated due to the irqtrace infrastructure not
being NMI-safe. The NMI return path cannot call TRACE_IRQS_IRET.
Otherwise, the tail of the normal iret-code is correct for the nmi code
path too. To be able to share this code-path, the TRACE_IRQS_IRET was
move up a bit. The espfix code exists after the TRACE_IRQS_IRET, but
this code explicitly disables interrupts. This short interrupts-off
section is now not traced anymore. The return-to-kernel path now always
includes the preliminary test to decide if the espfix code should be
called. This is never the case, but doing it this way keeps the patch as
simple as possible and the few extra instructions should not affect
timing in any significant way.
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <asm/ldt.h>
int modify_ldt(int func, void *ptr, unsigned long bytecount)
{
return syscall(SYS_modify_ldt, func, ptr, bytecount);
}
/* this is assumed to be usable */
#define SEGBASEADDR 0x10000
#define SEGLIMIT 0x20000
/* 16-bit segment */
struct user_desc desc = {
.entry_number = 0,
.base_addr = SEGBASEADDR,
.limit = SEGLIMIT,
.seg_32bit = 0,
.contents = 0, /* ??? */
.read_exec_only = 0,
.limit_in_pages = 0,
.seg_not_present = 0,
.useable = 1
};
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
/* map a 64 kb segment */
char *pointer = mmap((void *)SEGBASEADDR, SEGLIMIT+1,
PROT_EXEC|PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (pointer == NULL) {
printf("could not map space\n");
return 0;
}
/* write ldt, new mode */
int err = modify_ldt(0x11, &desc, sizeof(desc));
if (err) {
printf("error modifying ldt: %i\n", err);
return 0;
}
for (int i=0; i<1000; i++) {
asm volatile (
"pusha\n\t"
"mov %ss, %eax\n\t" /* preserve ss:esp */
"mov %esp, %ebp\n\t"
"push $7\n\t" /* index 0, ldt, user mode */
"push $65536-4096\n\t" /* esp */
"lss (%esp), %esp\n\t" /* switch to new stack */
"push %eax\n\t" /* save old ss:esp on new stack */
"push %ebp\n\t"
"add $17*65536, %esp\n\t" /* set high bits */
"mov %esp, %edx\n\t"
"mov $10000000, %ecx\n\t" /* wait... */
"1: loop 1b\n\t" /* ... a bit */
"cmp %esp, %edx\n\t"
"je 1f\n\t"
"ud2\n\t" /* esp changed inexplicably! */
"1:\n\t"
"sub $17*65536, %esp\n\t" /* restore high bits */
"lss (%esp), %esp\n\t" /* restore old ss:esp */
"popa\n\t");
printf("\rx%ix", i);
}
return 0;
}
Signed-off-by: Alexander van Heukelum <heukelum@fastmail.fm>
Acked-by: Stas Sergeev <stsp@aknet.ru>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-06-18 00:35:57 +02:00
* will s o o n e x e c u t e i r e t a n d t h e t r a c e r w a s a l r e a d y s e t t o
2015-06-08 09:49:11 +02:00
* the i r q s t a t e a f t e r t h e I R E T :
* /
2006-12-07 02:14:08 +01:00
DISABLE_ I N T E R R U P T S ( C L B R _ E A X )
2015-06-08 09:49:11 +02:00
lss ( % e s p ) , % e s p / * s w i t c h t o e s p f i x s e g m e n t * /
jmp r e s t o r e _ n o c h e c k
2014-05-04 10:36:22 -07:00
# endif
2015-06-08 08:42:03 +02:00
ENDPROC( e n t r y _ I N T 8 0 _ 3 2 )
2005-04-16 15:20:36 -07:00
2009-02-09 22:17:40 +09:00
.macro FIXUP_ESPFIX_STACK
i386: fix/simplify espfix stack switching, move it into assembly
The espfix code triggers if we have a protected mode userspace
application with a 16-bit stack. On returning to userspace, with iret,
the CPU doesn't restore the high word of the stack pointer. This is an
"official" bug, and the work-around used in the kernel is to temporarily
switch to a 32-bit stack segment/pointer pair where the high word of the
pointer is equal to the high word of the userspace stackpointer.
The current implementation uses THREAD_SIZE to determine the cut-off,
but there is no good reason not to use the more natural 64kb... However,
implementing this by simply substituting THREAD_SIZE with 65536 in
patch_espfix_desc crashed the test application. patch_espfix_desc tries
to do what is described above, but gets it subtly wrong if the userspace
stack pointer is just below a multiple of THREAD_SIZE: an overflow
occurs to bit 13... With a bit of luck, when the kernelspace
stackpointer is just below a 64kb-boundary, the overflow then ripples
trough to bit 16 and userspace will see its stack pointer changed by
65536.
This patch moves all espfix code into entry_32.S. Selecting a 16-bit
cut-off simplifies the code. The game with changing the limit dynamically
is removed too. It complicates matters and I see no value in it. Changing
only the top 16-bit word of ESP is one instruction and it also implies
that only two bytes of the ESPFIX GDT entry need to be changed and this
can be implemented in just a handful simple to understand instructions.
As a side effect, the operation to compute the original ESP from the
ESPFIX ESP and the GDT entry simplifies a bit too, and the remaining
three instructions have been expanded inline in entry_32.S.
impact: can now reliably run userspace with ESP=xxxxfffc on 16-bit
stack segment
Signed-off-by: Alexander van Heukelum <heukelum@fastmail.fm>
Acked-by: Stas Sergeev <stsp@aknet.ru>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-06-18 00:35:58 +02:00
/ *
* Switch b a c k f o r E S P F I X s t a c k t o t h e n o r m a l z e r o b a s e d s t a c k
*
* We c a n ' t c a l l C f u n c t i o n s u s i n g t h e E S P F I X s t a c k . T h i s c o d e r e a d s
* the h i g h w o r d o f t h e s e g m e n t b a s e f r o m t h e G D T a n d s w i c h e s t o t h e
* normal s t a c k a n d a d j u s t s E S P w i t h t h e m a t c h i n g o f f s e t .
* /
2014-05-04 10:36:22 -07:00
# ifdef C O N F I G _ X 8 6 _ E S P F I X 3 2
i386: fix/simplify espfix stack switching, move it into assembly
The espfix code triggers if we have a protected mode userspace
application with a 16-bit stack. On returning to userspace, with iret,
the CPU doesn't restore the high word of the stack pointer. This is an
"official" bug, and the work-around used in the kernel is to temporarily
switch to a 32-bit stack segment/pointer pair where the high word of the
pointer is equal to the high word of the userspace stackpointer.
The current implementation uses THREAD_SIZE to determine the cut-off,
but there is no good reason not to use the more natural 64kb... However,
implementing this by simply substituting THREAD_SIZE with 65536 in
patch_espfix_desc crashed the test application. patch_espfix_desc tries
to do what is described above, but gets it subtly wrong if the userspace
stack pointer is just below a multiple of THREAD_SIZE: an overflow
occurs to bit 13... With a bit of luck, when the kernelspace
stackpointer is just below a 64kb-boundary, the overflow then ripples
trough to bit 16 and userspace will see its stack pointer changed by
65536.
This patch moves all espfix code into entry_32.S. Selecting a 16-bit
cut-off simplifies the code. The game with changing the limit dynamically
is removed too. It complicates matters and I see no value in it. Changing
only the top 16-bit word of ESP is one instruction and it also implies
that only two bytes of the ESPFIX GDT entry need to be changed and this
can be implemented in just a handful simple to understand instructions.
As a side effect, the operation to compute the original ESP from the
ESPFIX ESP and the GDT entry simplifies a bit too, and the remaining
three instructions have been expanded inline in entry_32.S.
impact: can now reliably run userspace with ESP=xxxxfffc on 16-bit
stack segment
Signed-off-by: Alexander van Heukelum <heukelum@fastmail.fm>
Acked-by: Stas Sergeev <stsp@aknet.ru>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-06-18 00:35:58 +02:00
/* fixup the stack */
2015-06-08 09:49:11 +02:00
mov G D T _ E S P F I X _ S S + 4 , % a l / * b i t s 1 6 . . 2 3 * /
mov G D T _ E S P F I X _ S S + 7 , % a h / * b i t s 2 4 . . 3 1 * /
2015-06-08 22:35:33 +02:00
shl $ 1 6 , % e a x
2015-06-08 09:49:11 +02:00
addl % e s p , % e a x / * t h e a d j u s t e d s t a c k p o i n t e r * /
pushl $ _ _ K E R N E L _ D S
pushl % e a x
lss ( % e s p ) , % e s p / * s w i t c h t o t h e n o r m a l s t a c k s e g m e n t * /
2014-05-04 10:36:22 -07:00
# endif
2009-02-09 22:17:40 +09:00
.endm
.macro UNWIND_ESPFIX_STACK
2014-05-04 10:36:22 -07:00
# ifdef C O N F I G _ X 8 6 _ E S P F I X 3 2
2015-06-08 09:49:11 +02:00
movl % s s , % e a x
2009-02-09 22:17:40 +09:00
/* see if on espfix stack */
2015-06-08 09:49:11 +02:00
cmpw $ _ _ E S P F I X _ S S , % a x
jne 2 7 f
movl $ _ _ K E R N E L _ D S , % e a x
movl % e a x , % d s
movl % e a x , % e s
2009-02-09 22:17:40 +09:00
/* switch to normal stack */
FIXUP_ E S P F I X _ S T A C K
27 :
2014-05-04 10:36:22 -07:00
# endif
2009-02-09 22:17:40 +09:00
.endm
2005-04-16 15:20:36 -07:00
/ *
2015-04-03 21:49:13 +02:00
* Build t h e e n t r y s t u b s w i t h s o m e a s s e m b l e r m a g i c .
* We p a c k 1 s t u b i n t o e v e r y 8 - b y t e b l o c k .
2005-04-16 15:20:36 -07:00
* /
2015-04-03 21:49:13 +02:00
.align 8
2005-04-16 15:20:36 -07:00
ENTRY( i r q _ e n t r i e s _ s t a r t )
2015-04-03 21:49:13 +02:00
vector=FIRST_EXTERNAL_VECTOR
.rept ( FIRST_ S Y S T E M _ V E C T O R - F I R S T _ E X T E R N A L _ V E C T O R )
2015-06-08 09:49:11 +02:00
pushl $ ( ~ v e c t o r + 0 x80 ) / * N o t e : a l w a y s i n s i g n e d b y t e r a n g e * /
2015-04-03 21:49:13 +02:00
vector=vector + 1
jmp c o m m o n _ i n t e r r u p t
.align 8
.endr
2007-02-13 13:26:24 +01:00
END( i r q _ e n t r i e s _ s t a r t )
2006-07-03 00:24:43 -07:00
/ *
* the C P U a u t o m a t i c a l l y d i s a b l e s i n t e r r u p t s w h e n e x e c u t i n g a n I R Q v e c t o r ,
* so I R Q - f l a g s t r a c i n g h a s t o f o l l o w t h a t :
* /
2008-11-11 13:24:58 -08:00
.p2align CONFIG_X86_L1_CACHE_SHIFT
2005-04-16 15:20:36 -07:00
common_interrupt :
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
addl $ - 0 x80 , ( % e s p ) / * A d j u s t v e c t o r i n t o t h e [ - 2 5 6 , - 1 ] r a n g e * /
2005-04-16 15:20:36 -07:00
SAVE_ A L L
2006-07-03 00:24:43 -07:00
TRACE_ I R Q S _ O F F
2015-06-08 09:49:11 +02:00
movl % e s p , % e a x
call d o _ I R Q
jmp r e t _ f r o m _ i n t r
2007-02-13 13:26:24 +01:00
ENDPROC( c o m m o n _ i n t e r r u p t )
2005-04-16 15:20:36 -07:00
2009-01-21 17:26:06 +09:00
# define B U I L D _ I N T E R R U P T 3 ( n a m e , n r , f n ) \
2005-04-16 15:20:36 -07:00
ENTRY( n a m e ) \
2012-09-21 13:58:10 -07:00
ASM_ C L A C ; \
2015-06-08 09:49:11 +02:00
pushl $ ~ ( n r ) ; \
2006-06-26 13:57:44 +02:00
SAVE_ A L L ; \
2006-07-03 00:24:43 -07:00
TRACE_ I R Q S _ O F F \
2015-06-08 09:49:11 +02:00
movl % e s p , % e a x ; \
call f n ; \
jmp r e t _ f r o m _ i n t r ; \
2007-02-13 13:26:24 +01:00
ENDPROC( n a m e )
2005-04-16 15:20:36 -07:00
x86, trace: Add irq vector tracepoints
[Purpose of this patch]
As Vaibhav explained in the thread below, tracepoints for irq vectors
are useful.
http://www.spinics.net/lists/mm-commits/msg85707.html
<snip>
The current interrupt traces from irq_handler_entry and irq_handler_exit
provide when an interrupt is handled. They provide good data about when
the system has switched to kernel space and how it affects the currently
running processes.
There are some IRQ vectors which trigger the system into kernel space,
which are not handled in generic IRQ handlers. Tracing such events gives
us the information about IRQ interaction with other system events.
The trace also tells where the system is spending its time. We want to
know which cores are handling interrupts and how they are affecting other
processes in the system. Also, the trace provides information about when
the cores are idle and which interrupts are changing that state.
<snip>
On the other hand, my usecase is tracing just local timer event and
getting a value of instruction pointer.
I suggested to add an argument local timer event to get instruction pointer before.
But there is another way to get it with external module like systemtap.
So, I don't need to add any argument to irq vector tracepoints now.
[Patch Description]
Vaibhav's patch shared a trace point ,irq_vector_entry/irq_vector_exit, in all events.
But there is an above use case to trace specific irq_vector rather than tracing all events.
In this case, we are concerned about overhead due to unwanted events.
So, add following tracepoints instead of introducing irq_vector_entry/exit.
so that we can enable them independently.
- local_timer_vector
- reschedule_vector
- call_function_vector
- call_function_single_vector
- irq_work_entry_vector
- error_apic_vector
- thermal_apic_vector
- threshold_apic_vector
- spurious_apic_vector
- x86_platform_ipi_vector
Also, introduce a logic switching IDT at enabling/disabling time so that a time penalty
makes a zero when tracepoints are disabled. Detailed explanations are as follows.
- Create trace irq handlers with entering_irq()/exiting_irq().
- Create a new IDT, trace_idt_table, at boot time by adding a logic to
_set_gate(). It is just a copy of original idt table.
- Register the new handlers for tracpoints to the new IDT by introducing
macros to alloc_intr_gate() called at registering time of irq_vector handlers.
- Add checking, whether irq vector tracing is on/off, into load_current_idt().
This has to be done below debug checking for these reasons.
- Switching to debug IDT may be kicked while tracing is enabled.
- On the other hands, switching to trace IDT is kicked only when debugging
is disabled.
In addition, the new IDT is created only when CONFIG_TRACING is enabled to avoid being
used for other purposes.
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Link: http://lkml.kernel.org/r/51C323ED.5050708@hds.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
2013-06-20 11:46:53 -04:00
# ifdef C O N F I G _ T R A C I N G
2015-06-08 09:49:11 +02:00
# define T R A C E _ B U I L D _ I N T E R R U P T ( n a m e , n r ) B U I L D _ I N T E R R U P T 3 ( t r a c e _ ## n a m e , n r , s m p _ t r a c e _ # # n a m e )
x86, trace: Add irq vector tracepoints
[Purpose of this patch]
As Vaibhav explained in the thread below, tracepoints for irq vectors
are useful.
http://www.spinics.net/lists/mm-commits/msg85707.html
<snip>
The current interrupt traces from irq_handler_entry and irq_handler_exit
provide when an interrupt is handled. They provide good data about when
the system has switched to kernel space and how it affects the currently
running processes.
There are some IRQ vectors which trigger the system into kernel space,
which are not handled in generic IRQ handlers. Tracing such events gives
us the information about IRQ interaction with other system events.
The trace also tells where the system is spending its time. We want to
know which cores are handling interrupts and how they are affecting other
processes in the system. Also, the trace provides information about when
the cores are idle and which interrupts are changing that state.
<snip>
On the other hand, my usecase is tracing just local timer event and
getting a value of instruction pointer.
I suggested to add an argument local timer event to get instruction pointer before.
But there is another way to get it with external module like systemtap.
So, I don't need to add any argument to irq vector tracepoints now.
[Patch Description]
Vaibhav's patch shared a trace point ,irq_vector_entry/irq_vector_exit, in all events.
But there is an above use case to trace specific irq_vector rather than tracing all events.
In this case, we are concerned about overhead due to unwanted events.
So, add following tracepoints instead of introducing irq_vector_entry/exit.
so that we can enable them independently.
- local_timer_vector
- reschedule_vector
- call_function_vector
- call_function_single_vector
- irq_work_entry_vector
- error_apic_vector
- thermal_apic_vector
- threshold_apic_vector
- spurious_apic_vector
- x86_platform_ipi_vector
Also, introduce a logic switching IDT at enabling/disabling time so that a time penalty
makes a zero when tracepoints are disabled. Detailed explanations are as follows.
- Create trace irq handlers with entering_irq()/exiting_irq().
- Create a new IDT, trace_idt_table, at boot time by adding a logic to
_set_gate(). It is just a copy of original idt table.
- Register the new handlers for tracpoints to the new IDT by introducing
macros to alloc_intr_gate() called at registering time of irq_vector handlers.
- Add checking, whether irq vector tracing is on/off, into load_current_idt().
This has to be done below debug checking for these reasons.
- Switching to debug IDT may be kicked while tracing is enabled.
- On the other hands, switching to trace IDT is kicked only when debugging
is disabled.
In addition, the new IDT is created only when CONFIG_TRACING is enabled to avoid being
used for other purposes.
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Link: http://lkml.kernel.org/r/51C323ED.5050708@hds.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
2013-06-20 11:46:53 -04:00
# else
2015-06-08 09:49:11 +02:00
# define T R A C E _ B U I L D _ I N T E R R U P T ( n a m e , n r )
x86, trace: Add irq vector tracepoints
[Purpose of this patch]
As Vaibhav explained in the thread below, tracepoints for irq vectors
are useful.
http://www.spinics.net/lists/mm-commits/msg85707.html
<snip>
The current interrupt traces from irq_handler_entry and irq_handler_exit
provide when an interrupt is handled. They provide good data about when
the system has switched to kernel space and how it affects the currently
running processes.
There are some IRQ vectors which trigger the system into kernel space,
which are not handled in generic IRQ handlers. Tracing such events gives
us the information about IRQ interaction with other system events.
The trace also tells where the system is spending its time. We want to
know which cores are handling interrupts and how they are affecting other
processes in the system. Also, the trace provides information about when
the cores are idle and which interrupts are changing that state.
<snip>
On the other hand, my usecase is tracing just local timer event and
getting a value of instruction pointer.
I suggested to add an argument local timer event to get instruction pointer before.
But there is another way to get it with external module like systemtap.
So, I don't need to add any argument to irq vector tracepoints now.
[Patch Description]
Vaibhav's patch shared a trace point ,irq_vector_entry/irq_vector_exit, in all events.
But there is an above use case to trace specific irq_vector rather than tracing all events.
In this case, we are concerned about overhead due to unwanted events.
So, add following tracepoints instead of introducing irq_vector_entry/exit.
so that we can enable them independently.
- local_timer_vector
- reschedule_vector
- call_function_vector
- call_function_single_vector
- irq_work_entry_vector
- error_apic_vector
- thermal_apic_vector
- threshold_apic_vector
- spurious_apic_vector
- x86_platform_ipi_vector
Also, introduce a logic switching IDT at enabling/disabling time so that a time penalty
makes a zero when tracepoints are disabled. Detailed explanations are as follows.
- Create trace irq handlers with entering_irq()/exiting_irq().
- Create a new IDT, trace_idt_table, at boot time by adding a logic to
_set_gate(). It is just a copy of original idt table.
- Register the new handlers for tracpoints to the new IDT by introducing
macros to alloc_intr_gate() called at registering time of irq_vector handlers.
- Add checking, whether irq vector tracing is on/off, into load_current_idt().
This has to be done below debug checking for these reasons.
- Switching to debug IDT may be kicked while tracing is enabled.
- On the other hands, switching to trace IDT is kicked only when debugging
is disabled.
In addition, the new IDT is created only when CONFIG_TRACING is enabled to avoid being
used for other purposes.
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Link: http://lkml.kernel.org/r/51C323ED.5050708@hds.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
2013-06-20 11:46:53 -04:00
# endif
2015-06-08 09:49:11 +02:00
# define B U I L D _ I N T E R R U P T ( n a m e , n r ) \
BUILD_ I N T E R R U P T 3 ( n a m e , n r , s m p _ ## n a m e ) ; \
x86, trace: Add irq vector tracepoints
[Purpose of this patch]
As Vaibhav explained in the thread below, tracepoints for irq vectors
are useful.
http://www.spinics.net/lists/mm-commits/msg85707.html
<snip>
The current interrupt traces from irq_handler_entry and irq_handler_exit
provide when an interrupt is handled. They provide good data about when
the system has switched to kernel space and how it affects the currently
running processes.
There are some IRQ vectors which trigger the system into kernel space,
which are not handled in generic IRQ handlers. Tracing such events gives
us the information about IRQ interaction with other system events.
The trace also tells where the system is spending its time. We want to
know which cores are handling interrupts and how they are affecting other
processes in the system. Also, the trace provides information about when
the cores are idle and which interrupts are changing that state.
<snip>
On the other hand, my usecase is tracing just local timer event and
getting a value of instruction pointer.
I suggested to add an argument local timer event to get instruction pointer before.
But there is another way to get it with external module like systemtap.
So, I don't need to add any argument to irq vector tracepoints now.
[Patch Description]
Vaibhav's patch shared a trace point ,irq_vector_entry/irq_vector_exit, in all events.
But there is an above use case to trace specific irq_vector rather than tracing all events.
In this case, we are concerned about overhead due to unwanted events.
So, add following tracepoints instead of introducing irq_vector_entry/exit.
so that we can enable them independently.
- local_timer_vector
- reschedule_vector
- call_function_vector
- call_function_single_vector
- irq_work_entry_vector
- error_apic_vector
- thermal_apic_vector
- threshold_apic_vector
- spurious_apic_vector
- x86_platform_ipi_vector
Also, introduce a logic switching IDT at enabling/disabling time so that a time penalty
makes a zero when tracepoints are disabled. Detailed explanations are as follows.
- Create trace irq handlers with entering_irq()/exiting_irq().
- Create a new IDT, trace_idt_table, at boot time by adding a logic to
_set_gate(). It is just a copy of original idt table.
- Register the new handlers for tracpoints to the new IDT by introducing
macros to alloc_intr_gate() called at registering time of irq_vector handlers.
- Add checking, whether irq vector tracing is on/off, into load_current_idt().
This has to be done below debug checking for these reasons.
- Switching to debug IDT may be kicked while tracing is enabled.
- On the other hands, switching to trace IDT is kicked only when debugging
is disabled.
In addition, the new IDT is created only when CONFIG_TRACING is enabled to avoid being
used for other purposes.
Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Link: http://lkml.kernel.org/r/51C323ED.5050708@hds.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
2013-06-20 11:46:53 -04:00
TRACE_ B U I L D _ I N T E R R U P T ( n a m e , n r )
2009-01-21 17:26:06 +09:00
2005-04-16 15:20:36 -07:00
/* The include is where all of the SMP etc. interrupts come from */
2009-01-28 19:34:09 +01:00
# include < a s m / e n t r y _ a r c h . h >
2005-04-16 15:20:36 -07:00
ENTRY( c o p r o c e s s o r _ e r r o r )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0
pushl $ d o _ c o p r o c e s s o r _ e r r o r
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( c o p r o c e s s o r _ e r r o r )
2005-04-16 15:20:36 -07:00
ENTRY( s i m d _ c o p r o c e s s o r _ e r r o r )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0
2010-03-21 09:00:43 -04:00
# ifdef C O N F I G _ X 8 6 _ I N V D _ B U G
/* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
2015-06-08 09:49:11 +02:00
ALTERNATIVE " p u s h l $ d o _ g e n e r a l _ p r o t e c t i o n " , \
" pushl $ d o _ s i m d _ c o p r o c e s s o r _ e r r o r " , \
2015-01-18 12:35:55 +01:00
X8 6 _ F E A T U R E _ X M M
2010-03-21 09:00:43 -04:00
# else
2015-06-08 09:49:11 +02:00
pushl $ d o _ s i m d _ c o p r o c e s s o r _ e r r o r
2010-03-21 09:00:43 -04:00
# endif
2015-06-08 09:49:11 +02:00
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( s i m d _ c o p r o c e s s o r _ e r r o r )
2005-04-16 15:20:36 -07:00
ENTRY( d e v i c e _ n o t _ a v a i l a b l e )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ - 1 # m a r k t h i s a s a n i n t
pushl $ d o _ d e v i c e _ n o t _ a v a i l a b l e
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( d e v i c e _ n o t _ a v a i l a b l e )
2005-04-16 15:20:36 -07:00
2006-12-07 02:14:07 +01:00
# ifdef C O N F I G _ P A R A V I R T
ENTRY( n a t i v e _ i r e t )
2008-02-09 23:24:08 +01:00
iret
2012-04-20 12:19:50 -07:00
_ ASM_ E X T A B L E ( n a t i v e _ i r e t , i r e t _ e x c )
2007-02-13 13:26:24 +01:00
END( n a t i v e _ i r e t )
2006-12-07 02:14:07 +01:00
# endif
2005-04-16 15:20:36 -07:00
ENTRY( o v e r f l o w )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0
pushl $ d o _ o v e r f l o w
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( o v e r f l o w )
2005-04-16 15:20:36 -07:00
ENTRY( b o u n d s )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0
pushl $ d o _ b o u n d s
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( b o u n d s )
2005-04-16 15:20:36 -07:00
ENTRY( i n v a l i d _ o p )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0
pushl $ d o _ i n v a l i d _ o p
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( i n v a l i d _ o p )
2005-04-16 15:20:36 -07:00
ENTRY( c o p r o c e s s o r _ s e g m e n t _ o v e r r u n )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0
pushl $ d o _ c o p r o c e s s o r _ s e g m e n t _ o v e r r u n
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( c o p r o c e s s o r _ s e g m e n t _ o v e r r u n )
2005-04-16 15:20:36 -07:00
ENTRY( i n v a l i d _ T S S )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ d o _ i n v a l i d _ T S S
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( i n v a l i d _ T S S )
2005-04-16 15:20:36 -07:00
ENTRY( s e g m e n t _ n o t _ p r e s e n t )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ d o _ s e g m e n t _ n o t _ p r e s e n t
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( s e g m e n t _ n o t _ p r e s e n t )
2005-04-16 15:20:36 -07:00
ENTRY( s t a c k _ s e g m e n t )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ d o _ s t a c k _ s e g m e n t
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( s t a c k _ s e g m e n t )
2005-04-16 15:20:36 -07:00
ENTRY( a l i g n m e n t _ c h e c k )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ d o _ a l i g n m e n t _ c h e c k
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( a l i g n m e n t _ c h e c k )
2005-04-16 15:20:36 -07:00
[PATCH] x86: error_code is not safe for kprobes
This patch moves the entry.S:error_entry to .kprobes.text section,
since code marked unsafe for kprobes jumps directly to entry.S::error_entry,
that must be marked unsafe as well.
This patch also moves all the ".previous.text" asm directives to ".previous"
for kprobes section.
AK: Following a similar i386 patch from Chuck Ebbert
AK: Also merged Jeremy's fix in.
+From: Jeremy Fitzhardinge <jeremy@goop.org>
KPROBE_ENTRY does a .section .kprobes.text, and expects its users to
do a .previous at the end of the function.
Unfortunately, if any code within the function switches sections, for
example .fixup, then the .previous ends up putting all subsequent code
into .fixup. Worse, any subsequent .fixup code gets intermingled with
the code its supposed to be fixing (which is also in .fixup). It's
surprising this didn't cause more havok.
The fix is to use .pushsection/.popsection, so this stuff nests
properly. A further cleanup would be to get rid of all
.section/.previous pairs, since they're inherently fragile.
+From: Chuck Ebbert <76306.1226@compuserve.com>
Because code marked unsafe for kprobes jumps directly to
entry.S::error_code, that must be marked unsafe as well.
The easiest way to do that is to move the page fault entry
point to just before error_code and let it inherit the same
section.
Also moved all the ".previous" asm directives for kprobes
sections to column 1 and removed ".text" from them.
Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>
Signed-off-by: Andi Kleen <ak@suse.de>
2006-09-26 10:52:34 +02:00
ENTRY( d i v i d e _ e r r o r )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0 # n o e r r o r c o d e
pushl $ d o _ d i v i d e _ e r r o r
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( d i v i d e _ e r r o r )
2005-04-16 15:20:36 -07:00
# ifdef C O N F I G _ X 8 6 _ M C E
ENTRY( m a c h i n e _ c h e c k )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0
pushl m a c h i n e _ c h e c k _ v e c t o r
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( m a c h i n e _ c h e c k )
2005-04-16 15:20:36 -07:00
# endif
ENTRY( s p u r i o u s _ i n t e r r u p t _ b u g )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ 0
pushl $ d o _ s p u r i o u s _ i n t e r r u p t _ b u g
jmp e r r o r _ c o d e
2007-02-13 13:26:24 +01:00
END( s p u r i o u s _ i n t e r r u p t _ b u g )
2005-04-16 15:20:36 -07:00
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
# ifdef C O N F I G _ X E N
2015-06-08 09:49:11 +02:00
/ *
* Xen d o e s n ' t s e t % e s p t o b e p r e c i s e l y w h a t t h e n o r m a l S Y S E N T E R
* entry p o i n t e x p e c t s , s o f i x i t u p b e f o r e u s i n g t h e n o r m a l p a t h .
* /
2008-03-17 16:37:17 -07:00
ENTRY( x e n _ s y s e n t e r _ t a r g e t )
2015-06-08 09:49:11 +02:00
addl $ 5 * 4 , % e s p / * r e m o v e x e n - p r o v i d e d f r a m e * /
jmp s y s e n t e r _ p a s t _ e s p
2008-03-17 16:37:17 -07:00
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
ENTRY( x e n _ h y p e r v i s o r _ c a l l b a c k )
2015-06-08 09:49:11 +02:00
pushl $ - 1 / * o r i g _ a x = - 1 = > n o t a s y s t e m c a l l * /
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
SAVE_ A L L
TRACE_ I R Q S _ O F F
xen: use iret directly when possible
Most of the time we can simply use the iret instruction to exit the
kernel, rather than having to use the iret hypercall - the only
exception is if we're returning into vm86 mode, or from delivering an
NMI (which we don't support yet).
When running native, iret has the behaviour of testing for a pending
interrupt atomically with re-enabling interrupts. Unfortunately
there's no way to do this with Xen, so there's a window in which we
could get a recursive exception after enabling events but before
actually returning to userspace.
This causes a problem: if the nested interrupt causes one of the
task's TIF_WORK_MASK flags to be set, they will not be checked again
before returning to userspace. This means that pending work may be
left pending indefinitely, until the process enters and leaves the
kernel again. The net effect is that a pending signal or reschedule
event could be delayed for an unbounded amount of time.
To deal with this, the xen event upcall handler checks to see if the
EIP is within the critical section of the iret code, after events
are (potentially) enabled up to the iret itself. If its within this
range, it calls the iret critical section fixup, which adjusts the
stack to deal with any unrestored registers, and then shifts the
stack frame up to replace the previous invocation.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
2007-07-17 18:37:07 -07:00
2015-06-08 09:49:11 +02:00
/ *
* Check t o s e e i f w e g o t t h e e v e n t i n t h e c r i t i c a l
* region i n x e n _ i r e t _ d i r e c t , a f t e r w e ' v e r e e n a b l e d
* events a n d c h e c k e d f o r p e n d i n g e v e n t s . T h i s s i m u l a t e s
* iret i n s t r u c t i o n ' s b e h a v i o u r w h e r e i t d e l i v e r s a
* pending i n t e r r u p t w h e n e n a b l i n g i n t e r r u p t s :
* /
movl P T _ E I P ( % e s p ) , % e a x
cmpl $ x e n _ i r e t _ s t a r t _ c r i t , % e a x
jb 1 f
cmpl $ x e n _ i r e t _ e n d _ c r i t , % e a x
jae 1 f
xen: use iret directly when possible
Most of the time we can simply use the iret instruction to exit the
kernel, rather than having to use the iret hypercall - the only
exception is if we're returning into vm86 mode, or from delivering an
NMI (which we don't support yet).
When running native, iret has the behaviour of testing for a pending
interrupt atomically with re-enabling interrupts. Unfortunately
there's no way to do this with Xen, so there's a window in which we
could get a recursive exception after enabling events but before
actually returning to userspace.
This causes a problem: if the nested interrupt causes one of the
task's TIF_WORK_MASK flags to be set, they will not be checked again
before returning to userspace. This means that pending work may be
left pending indefinitely, until the process enters and leaves the
kernel again. The net effect is that a pending signal or reschedule
event could be delayed for an unbounded amount of time.
To deal with this, the xen event upcall handler checks to see if the
EIP is within the critical section of the iret code, after events
are (potentially) enabled up to the iret itself. If its within this
range, it calls the iret critical section fixup, which adjusts the
stack to deal with any unrestored registers, and then shifts the
stack frame up to replace the previous invocation.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
2007-07-17 18:37:07 -07:00
2015-06-08 09:49:11 +02:00
jmp x e n _ i r e t _ c r i t _ f i x u p
2008-03-17 16:37:17 -07:00
ENTRY( x e n _ d o _ u p c a l l )
2015-06-08 09:49:11 +02:00
1 : mov % e s p , % e a x
call x e n _ e v t c h n _ d o _ u p c a l l
2015-02-19 15:23:17 +00:00
# ifndef C O N F I G _ P R E E M P T
2015-06-08 09:49:11 +02:00
call x e n _ m a y b e _ p r e e m p t _ h c a l l
2015-02-19 15:23:17 +00:00
# endif
2015-06-08 09:49:11 +02:00
jmp r e t _ f r o m _ i n t r
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
ENDPROC( x e n _ h y p e r v i s o r _ c a l l b a c k )
2015-06-08 09:49:11 +02:00
/ *
* Hypervisor u s e s t h i s f o r a p p l i c a t i o n f a u l t s w h i l e i t e x e c u t e s .
* We g e t h e r e f o r t w o r e a s o n s :
* 1 . Fault w h i l e r e l o a d i n g D S , E S , F S o r G S
* 2 . Fault w h i l e e x e c u t i n g I R E T
* Category 1 w e f i x u p b y r e a t t e m p t i n g t h e l o a d , a n d z e r o i n g t h e s e g m e n t
* register i f t h e l o a d f a i l s .
* Category 2 w e f i x u p b y j u m p i n g t o d o _ i r e t _ e r r o r . W e c a n n o t u s e t h e
* normal L i n u x r e t u r n p a t h i n t h i s c a s e b e c a u s e i f w e u s e t h e I R E T h y p e r c a l l
* to p o p t h e s t a c k f r a m e w e e n d u p i n a n i n f i n i t e l o o p o f f a i l s a f e c a l l b a c k s .
* We d i s t i n g u i s h b e t w e e n c a t e g o r i e s b y m a i n t a i n i n g a s t a t u s v a l u e i n E A X .
* /
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
ENTRY( x e n _ f a i l s a f e _ c a l l b a c k )
2015-06-08 09:49:11 +02:00
pushl % e a x
movl $ 1 , % e a x
1 : mov 4 ( % e s p ) , % d s
2 : mov 8 ( % e s p ) , % e s
3 : mov 1 2 ( % e s p ) , % f s
4 : mov 1 6 ( % e s p ) , % g s
2012-10-19 17:29:07 +01:00
/ * EAX = = 0 = > C a t e g o r y 1 ( B a d s e g m e n t )
EAX ! = 0 = > C a t e g o r y 2 ( B a d I R E T ) * /
2015-06-08 09:49:11 +02:00
testl % e a x , % e a x
popl % e a x
lea 1 6 ( % e s p ) , % e s p
jz 5 f
jmp i r e t _ e x c
5 : pushl $ - 1 / * o r i g _ a x = - 1 = > n o t a s y s t e m c a l l * /
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
SAVE_ A L L
2015-06-08 09:49:11 +02:00
jmp r e t _ f r o m _ e x c e p t i o n
.section .fixup , " ax"
6 : xorl % e a x , % e a x
movl % e a x , 4 ( % e s p )
jmp 1 b
7 : xorl % e a x , % e a x
movl % e a x , 8 ( % e s p )
jmp 2 b
8 : xorl % e a x , % e a x
movl % e a x , 1 2 ( % e s p )
jmp 3 b
9 : xorl % e a x , % e a x
movl % e a x , 1 6 ( % e s p )
jmp 4 b
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
.previous
2015-06-08 09:49:11 +02:00
_ ASM_ E X T A B L E ( 1 b , 6 b )
_ ASM_ E X T A B L E ( 2 b , 7 b )
_ ASM_ E X T A B L E ( 3 b , 8 b )
_ ASM_ E X T A B L E ( 4 b , 9 b )
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
ENDPROC( x e n _ f a i l s a f e _ c a l l b a c k )
2013-02-03 17:22:39 -08:00
BUILD_ I N T E R R U P T 3 ( x e n _ h v m _ c a l l b a c k _ v e c t o r , H Y P E R V I S O R _ C A L L B A C K _ V E C T O R ,
2010-05-14 12:40:51 +01:00
xen_ e v t c h n _ d o _ u p c a l l )
2015-06-08 09:49:11 +02:00
# endif / * C O N F I G _ X E N * /
2013-02-03 17:22:39 -08:00
# if I S _ E N A B L E D ( C O N F I G _ H Y P E R V )
BUILD_ I N T E R R U P T 3 ( h y p e r v _ c a l l b a c k _ v e c t o r , H Y P E R V I S O R _ C A L L B A C K _ V E C T O R ,
hyperv_ v e c t o r _ h a n d l e r )
# endif / * C O N F I G _ H Y P E R V * /
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen
implementation, including:
- booting and setup
- pagetable setup
- privileged instructions
- segmentation
- interrupt flags
- upcalls
- multicall batching
BOOTING AND SETUP
The vmlinux image is decorated with ELF notes which tell the Xen
domain builder what the kernel's requirements are; the domain builder
then constructs the address space accordingly and starts the kernel.
Xen has its own entrypoint for the kernel (contained in an ELF note).
The ELF notes are set up by xen-head.S, which is included into head.S.
In principle it could be linked separately, but it seems to provoke
lots of binutils bugs.
Because the domain builder starts the kernel in a fairly sane state
(32-bit protected mode, paging enabled, flat segments set up), there's
not a lot of setup needed before starting the kernel proper. The main
steps are:
1. Install the Xen paravirt_ops, which is simply a matter of a
structure assignment.
2. Set init_mm to use the Xen-supplied pagetables (analogous to the
head.S generated pagetables in a native boot).
3. Reserve address space for Xen, since it takes a chunk at the top
of the address space for its own use.
4. Call start_kernel()
PAGETABLE SETUP
Once we hit the main kernel boot sequence, it will end up calling back
via paravirt_ops to set up various pieces of Xen specific state. One
of the critical things which requires a bit of extra care is the
construction of the initial init_mm pagetable. Because Xen places
tight constraints on pagetables (an active pagetable must always be
valid, and must always be mapped read-only to the guest domain), we
need to be careful when constructing the new pagetable to keep these
constraints in mind. It turns out that the easiest way to do this is
use the initial Xen-provided pagetable as a template, and then just
insert new mappings for memory where a mapping doesn't already exist.
This means that during pagetable setup, it uses a special version of
xen_set_pte which ignores any attempt to remap a read-only page as
read-write (since Xen will map its own initial pagetable as RO), but
lets other changes to the ptes happen, so that things like NX are set
properly.
PRIVILEGED INSTRUCTIONS AND SEGMENTATION
When the kernel runs under Xen, it runs in ring 1 rather than ring 0.
This means that it is more privileged than user-mode in ring 3, but it
still can't run privileged instructions directly. Non-performance
critical instructions are dealt with by taking a privilege exception
and trapping into the hypervisor and emulating the instruction, but
more performance-critical instructions have their own specific
paravirt_ops. In many cases we can avoid having to do any hypercalls
for these instructions, or the Xen implementation is quite different
from the normal native version.
The privileged instructions fall into the broad classes of:
Segmentation: setting up the GDT and the GDT entries, LDT,
TLS and so on. Xen doesn't allow the GDT to be directly
modified; all GDT updates are done via hypercalls where the new
entries can be validated. This is important because Xen uses
segment limits to prevent the guest kernel from damaging the
hypervisor itself.
Traps and exceptions: Xen uses a special format for trap entrypoints,
so when the kernel wants to set an IDT entry, it needs to be
converted to the form Xen expects. Xen sets int 0x80 up specially
so that the trap goes straight from userspace into the guest kernel
without going via the hypervisor. sysenter isn't supported.
Kernel stack: The esp0 entry is extracted from the tss and provided to
Xen.
TLB operations: the various TLB calls are mapped into corresponding
Xen hypercalls.
Control registers: all the control registers are privileged. The most
important is cr3, which points to the base of the current pagetable,
and we handle it specially.
Another instruction we treat specially is CPUID, even though its not
privileged. We want to control what CPU features are visible to the
rest of the kernel, and so CPUID ends up going into a paravirt_op.
Xen implements this mainly to disable the ACPI and APIC subsystems.
INTERRUPT FLAGS
Xen maintains its own separate flag for masking events, which is
contained within the per-cpu vcpu_info structure. Because the guest
kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely
ignored (and must be, because even if a guest domain disables
interrupts for itself, it can't disable them overall).
(A note on terminology: "events" and interrupts are effectively
synonymous. However, rather than using an "enable flag", Xen uses a
"mask flag", which blocks event delivery when it is non-zero.)
There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which
are implemented to manage the Xen event mask state. The only thing
worth noting is that when events are unmasked, we need to explicitly
see if there's a pending event and call into the hypervisor to make
sure it gets delivered.
UPCALLS
Xen needs a couple of upcall (or callback) functions to be implemented
by each guest. One is the event upcalls, which is how events
(interrupts, effectively) are delivered to the guests. The other is
the failsafe callback, which is used to report errors in either
reloading a segment register, or caused by iret. These are
implemented in i386/kernel/entry.S so they can jump into the normal
iret_exc path when necessary.
MULTICALL BATCHING
Xen provides a multicall mechanism, which allows multiple hypercalls
to be issued at once in order to mitigate the cost of trapping into
the hypervisor. This is particularly useful for context switches,
since the 4-5 hypercalls they would normally need (reload cr3, update
TLS, maybe update LDT) can be reduced to one. This patch implements a
generic batching mechanism for hypercalls, which gets used in many
places in the Xen code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Ian Pratt <ian.pratt@xensource.com>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Cc: Adrian Bunk <bunk@stusta.de>
2007-07-17 18:37:04 -07:00
2008-10-06 19:06:12 -04:00
# ifdef C O N F I G _ F U N C T I O N _ T R A C E R
2008-05-12 21:20:43 +02:00
# ifdef C O N F I G _ D Y N A M I C _ F T R A C E
ENTRY( m c o u n t )
ret
END( m c o u n t )
ENTRY( f t r a c e _ c a l l e r )
2015-06-08 09:49:11 +02:00
pushl % e a x
pushl % e c x
pushl % e d x
pushl $ 0 / * P a s s N U L L a s r e g s p o i n t e r * /
movl 4 * 4 ( % e s p ) , % e a x
movl 0 x4 ( % e b p ) , % e d x
movl f u n c t i o n _ t r a c e _ o p , % e c x
subl $ M C O U N T _ I N S N _ S I Z E , % e a x
2008-05-12 21:20:43 +02:00
.globl ftrace_call
ftrace_call :
2015-06-08 09:49:11 +02:00
call f t r a c e _ s t u b
2008-05-12 21:20:43 +02:00
2015-06-08 09:49:11 +02:00
addl $ 4 , % e s p / * s k i p N U L L p o i n t e r * /
popl % e d x
popl % e c x
popl % e a x
2012-06-05 20:00:11 -04:00
ftrace_ret :
2008-11-26 00:16:24 -05:00
# ifdef C O N F I G _ F U N C T I O N _ G R A P H _ T R A C E R
.globl ftrace_graph_call
ftrace_graph_call :
2015-06-08 09:49:11 +02:00
jmp f t r a c e _ s t u b
2008-11-26 00:16:24 -05:00
# endif
2008-05-12 21:20:43 +02:00
.globl ftrace_stub
ftrace_stub :
ret
END( f t r a c e _ c a l l e r )
2012-06-05 20:00:11 -04:00
ENTRY( f t r a c e _ r e g s _ c a l l e r )
pushf / * p u s h f l a g s b e f o r e c o m p a r e ( i n c s l o c a t i o n ) * /
/ *
* i3 8 6 d o e s n o t s a v e S S a n d E S P w h e n c o m i n g f r o m k e r n e l .
* Instead, t o g e t s p , & r e g s - > s p i s u s e d ( s e e p t r a c e . h ) .
* Unfortunately, t h a t m e a n s e f l a g s m u s t b e a t t h e s a m e l o c a t i o n
* as t h e c u r r e n t r e t u r n i p i s . W e m o v e t h e r e t u r n i p i n t o t h e
* ip l o c a t i o n , a n d m o v e f l a g s i n t o t h e r e t u r n i p l o c a t i o n .
* /
2015-06-08 09:49:11 +02:00
pushl 4 ( % e s p ) / * s a v e r e t u r n i p i n t o i p s l o t * /
pushl $ 0 / * L o a d 0 i n t o o r i g _ a x * /
pushl % g s
pushl % f s
pushl % e s
pushl % d s
pushl % e a x
pushl % e b p
pushl % e d i
pushl % e s i
pushl % e d x
pushl % e c x
pushl % e b x
movl 1 3 * 4 ( % e s p ) , % e a x / * G e t t h e s a v e d f l a g s * /
movl % e a x , 1 4 * 4 ( % e s p ) / * M o v e s a v e d f l a g s i n t o r e g s - > f l a g s l o c a t i o n * /
/* clobbering return ip */
movl $ _ _ K E R N E L _ C S , 1 3 * 4 ( % e s p )
movl 1 2 * 4 ( % e s p ) , % e a x / * L o a d i p ( 1 s t p a r a m e t e r ) * /
subl $ M C O U N T _ I N S N _ S I Z E , % e a x / * A d j u s t i p * /
movl 0 x4 ( % e b p ) , % e d x / * L o a d p a r e n t i p ( 2 n d p a r a m e t e r ) * /
movl f u n c t i o n _ t r a c e _ o p , % e c x / * S a v e f t r a c e _ p o s i n 3 r d p a r a m e t e r * /
pushl % e s p / * S a v e p t _ r e g s a s 4 t h p a r a m e t e r * /
2012-06-05 20:00:11 -04:00
GLOBAL( f t r a c e _ r e g s _ c a l l )
2015-06-08 09:49:11 +02:00
call f t r a c e _ s t u b
addl $ 4 , % e s p / * S k i p p t _ r e g s * /
movl 1 4 * 4 ( % e s p ) , % e a x / * M o v e f l a g s b a c k i n t o c s * /
movl % e a x , 1 3 * 4 ( % e s p ) / * N e e d e d t o k e e p a d d l f r o m m o d i f y i n g f l a g s * /
movl 1 2 * 4 ( % e s p ) , % e a x / * G e t r e t u r n i p f r o m r e g s - > i p * /
movl % e a x , 1 4 * 4 ( % e s p ) / * P u t r e t u r n i p b a c k f o r r e t * /
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 % e a x
popl % d s
popl % e s
popl % f s
popl % g s
addl $ 8 , % e s p / * S k i p o r i g _ a x a n d i p * /
popf / * P o p f l a g s a t e n d ( n o a d d l t o c o r r u p t f l a g s ) * /
jmp f t r a c e _ r e t
2012-06-05 20:00:11 -04:00
popf
2015-06-08 09:49:11 +02:00
jmp f t r a c e _ s t u b
2008-05-12 21:20:43 +02:00
# else / * ! C O N F I G _ D Y N A M I C _ F T R A C E * /
2008-05-12 21:20:42 +02:00
ENTRY( m c o u n t )
2015-06-08 09:49:11 +02:00
cmpl $ _ _ P A G E _ O F F S E T , % e s p
jb f t r a c e _ s t u b / * P a g i n g n o t e n a b l e d y e t ? * /
2013-08-30 17:29:29 -07:00
2015-06-08 09:49:11 +02:00
cmpl $ f t r a c e _ s t u b , f t r a c e _ t r a c e _ f u n c t i o n
jnz t r a c e
2008-11-25 21:07:04 +01:00
# ifdef C O N F I G _ F U N C T I O N _ G R A P H _ T R A C E R
2015-06-08 09:49:11 +02:00
cmpl $ f t r a c e _ s t u b , f t r a c e _ g r a p h _ r e t u r n
jnz f t r a c e _ g r a p h _ c a l l e r
2008-12-02 23:50:05 -05:00
2015-06-08 09:49:11 +02:00
cmpl $ f t r a c e _ g r a p h _ e n t r y _ s t u b , f t r a c e _ g r a p h _ e n t r y
jnz f t r a c e _ g r a p h _ c a l l e r
2008-11-11 07:03:45 +01:00
# endif
2008-05-12 21:20:42 +02:00
.globl ftrace_stub
ftrace_stub :
ret
/* taken from glibc */
trace :
2015-06-08 09:49:11 +02:00
pushl % e a x
pushl % e c x
pushl % e d x
movl 0 x c ( % e s p ) , % e a x
movl 0 x4 ( % e b p ) , % e d x
subl $ M C O U N T _ I N S N _ S I Z E , % e a x
call * f t r a c e _ t r a c e _ f u n c t i o n
popl % e d x
popl % e c x
popl % e a x
jmp f t r a c e _ s t u b
2008-05-12 21:20:42 +02:00
END( m c o u n t )
2008-05-12 21:20:43 +02:00
# endif / * C O N F I G _ D Y N A M I C _ F T R A C E * /
2008-10-06 19:06:12 -04:00
# endif / * C O N F I G _ F U N C T I O N _ T R A C E R * /
2008-05-12 21:20:42 +02:00
2008-11-25 21:07:04 +01:00
# ifdef C O N F I G _ F U N C T I O N _ G R A P H _ T R A C E R
ENTRY( f t r a c e _ g r a p h _ c a l l e r )
2015-06-08 09:49:11 +02:00
pushl % e a x
pushl % e c x
pushl % e d x
movl 0 x c ( % e s p ) , % e a x
lea 0 x4 ( % e b p ) , % e d x
movl ( % e b p ) , % e c x
subl $ M C O U N T _ I N S N _ S I Z E , % e a x
call p r e p a r e _ f t r a c e _ r e t u r n
popl % e d x
popl % e c x
popl % e a x
2008-11-16 06:02:06 +01:00
ret
2008-11-25 21:07:04 +01:00
END( f t r a c e _ g r a p h _ c a l l e r )
2008-11-11 07:03:45 +01:00
.globl return_to_handler
return_to_handler :
2015-06-08 09:49:11 +02:00
pushl % e a x
pushl % e d x
movl % e b p , % e a x
call f t r a c e _ r e t u r n _ t o _ h a n d l e r
movl % e a x , % e c x
popl % e d x
popl % e a x
jmp * % e c x
2008-11-16 06:02:06 +01:00
# endif
2008-05-12 21:20:42 +02:00
2013-10-30 16:37:00 -04:00
# ifdef C O N F I G _ T R A C I N G
ENTRY( t r a c e _ p a g e _ f a u l t )
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ t r a c e _ d o _ p a g e _ f a u l t
jmp e r r o r _ c o d e
2013-10-30 16:37:00 -04:00
END( t r a c e _ p a g e _ f a u l t )
# endif
2008-11-24 15:38:45 +01:00
ENTRY( p a g e _ f a u l t )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ d o _ p a g e _ f a u l t
2008-11-24 15:38:45 +01:00
ALIGN
error_code :
2009-02-09 22:17:40 +09:00
/* the function address is in %gs's slot on the stack */
2015-06-08 09:49:11 +02:00
pushl % f s
pushl % e s
pushl % d s
pushl % e a x
pushl % e b p
pushl % e d i
pushl % e s i
pushl % e d x
pushl % e c x
pushl % e b x
2008-11-24 15:38:45 +01:00
cld
2015-06-08 09:49:11 +02:00
movl $ ( _ _ K E R N E L _ P E R C P U ) , % e c x
movl % e c x , % f s
2008-11-24 15:38:45 +01:00
UNWIND_ E S P F I X _ S T A C K
2009-02-09 22:17:40 +09:00
GS_ T O _ R E G % e c x
2015-06-08 09:49:11 +02:00
movl P T _ G S ( % e s p ) , % e d i # g e t t h e f u n c t i o n a d d r e s s
movl P T _ O R I G _ E A X ( % e s p ) , % e d x # g e t t h e e r r o r c o d e
movl $ - 1 , P T _ O R I G _ E A X ( % e s p ) # n o s y s c a l l t o r e s t a r t
2009-02-09 22:17:40 +09:00
REG_ T O _ P T G S % e c x
SET_ K E R N E L _ G S % e c x
2015-06-08 09:49:11 +02:00
movl $ ( _ _ U S E R _ D S ) , % e c x
movl % e c x , % d s
movl % e c x , % e s
2008-11-24 15:38:45 +01:00
TRACE_ I R Q S _ O F F
2015-06-08 09:49:11 +02:00
movl % e s p , % e a x # p t _ r e g s p o i n t e r
call * % e d i
jmp r e t _ f r o m _ e x c e p t i o n
2008-11-24 15:38:45 +01:00
END( p a g e _ f a u l t )
/ *
* Debug t r a p s a n d N M I c a n h a p p e n a t t h e o n e S Y S E N T E R i n s t r u c t i o n
* that s e t s u p t h e r e a l k e r n e l s t a c k . C h e c k h e r e , s i n c e w e c a n ' t
* allow t h e w r o n g s t a c k t o b e u s e d .
*
* " TSS_ s y s e n t e r _ s p0 + 1 2 " i s b e c a u s e t h e N M I / d e b u g h a n d l e r w i l l h a v e
* already p u s h e d 3 w o r d s i f i t h i t s o n t h e s y s e n t e r i n s t r u c t i o n :
* eflags, c s a n d e i p .
*
* We j u s t l o a d t h e r i g h t s t a c k , a n d p u s h t h e t h r e e ( k n o w n ) v a l u e s
* by h a n d o n t o t h e n e w s t a c k - w h i l e u p d a t i n g t h e r e t u r n e i p p a s t
* the i n s t r u c t i o n t h a t w o u l d h a v e d o n e i t f o r s y s e n t e r .
* /
2009-02-09 22:17:40 +09:00
.macro FIX_STACK offset o k l a b e l
2015-06-08 09:49:11 +02:00
cmpw $ _ _ K E R N E L _ C S , 4 ( % e s p )
jne \ o k
2009-02-09 22:17:40 +09:00
\ label :
2015-06-08 09:49:11 +02:00
movl T S S _ s y s e n t e r _ s p0 + \ o f f s e t ( % e s p ) , % e s p
x86/debug: Remove perpetually broken, unmaintainable dwarf annotations
So the dwarf2 annotations in low level assembly code have
become an increasing hindrance: unreadable, messy macros
mixed into some of the most security sensitive code paths
of the Linux kernel.
These debug info annotations don't even buy the upstream
kernel anything: dwarf driven stack unwinding has caused
problems in the past so it's out of tree, and the upstream
kernel only uses the much more robust framepointers based
stack unwinding method.
In addition to that there's a steady, slow bitrot going
on with these annotations, requiring frequent fixups.
There's no tooling and no functionality upstream that
keeps it correct.
So burn down the sick forest, allowing new, healthier growth:
27 files changed, 350 insertions(+), 1101 deletions(-)
Someone who has the willingness and time to do this
properly can attempt to reintroduce dwarf debuginfo in x86
assembly code plus dwarf unwinding from first principles,
with the following conditions:
- it should be maximally readable, and maximally low-key to
'ordinary' code reading and maintenance.
- find a build time method to insert dwarf annotations
automatically in the most common cases, for pop/push
instructions that manipulate the stack pointer. This could
be done for example via a preprocessing step that just
looks for common patterns - plus special annotations for
the few cases where we want to depart from the default.
We have hundreds of CFI annotations, so automating most of
that makes sense.
- it should come with build tooling checks that ensure that
CFI annotations are sensible. We've seen such efforts from
the framepointer side, and there's no reason it couldn't be
done on the dwarf side.
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Frédéric Weisbecker <fweisbec@gmail.com
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jan Beulich <JBeulich@suse.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-05-28 12:21:47 +02:00
pushfl
2015-06-08 09:49:11 +02:00
pushl $ _ _ K E R N E L _ C S
pushl $ s y s e n t e r _ p a s t _ e s p
2009-02-09 22:17:40 +09:00
.endm
2008-11-24 15:38:45 +01:00
ENTRY( d e b u g )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
cmpl $ e n t r y _ S Y S E N T E R _ 3 2 , ( % e s p )
jne d e b u g _ s t a c k _ c o r r e c t
2009-02-09 22:17:40 +09:00
FIX_ S T A C K 1 2 , d e b u g _ s t a c k _ c o r r e c t , d e b u g _ e s p _ f i x _ i n s n
2008-11-24 15:38:45 +01:00
debug_stack_correct :
2015-06-08 09:49:11 +02:00
pushl $ - 1 # m a r k t h i s a s a n i n t
2008-11-24 15:38:45 +01:00
SAVE_ A L L
TRACE_ I R Q S _ O F F
2015-06-08 09:49:11 +02:00
xorl % e d x , % e d x # e r r o r c o d e 0
movl % e s p , % e a x # p t _ r e g s p o i n t e r
call d o _ d e b u g
jmp r e t _ f r o m _ e x c e p t i o n
2008-11-24 15:38:45 +01:00
END( d e b u g )
/ *
* NMI i s d o u b l y n a s t y . I t c a n h a p p e n _ w h i l e _ w e ' r e h a n d l i n g
* a d e b u g f a u l t , a n d t h e d e b u g f a u l t h a s n ' t y e t b e e n a b l e t o
* clear u p t h e s t a c k . S o w e f i r s t c h e c k w h e t h e r w e g o t a n
* NMI o n t h e s y s e n t e r e n t r y p a t h , b u t a f t e r t h a t w e n e e d t o
* check w h e t h e r w e g o t a n N M I o n t h e d e b u g p a t h w h e r e t h e d e b u g
* fault h a p p e n e d o n t h e s y s e n t e r p a t h .
* /
ENTRY( n m i )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2014-05-04 10:36:22 -07:00
# ifdef C O N F I G _ X 8 6 _ E S P F I X 3 2
2015-06-08 09:49:11 +02:00
pushl % e a x
movl % s s , % e a x
cmpw $ _ _ E S P F I X _ S S , % a x
popl % e a x
je n m i _ e s p f i x _ s t a c k
2014-05-04 10:36:22 -07:00
# endif
2015-06-08 09:49:11 +02:00
cmpl $ e n t r y _ S Y S E N T E R _ 3 2 , ( % e s p )
je n m i _ s t a c k _ f i x u p
pushl % e a x
movl % e s p , % e a x
/ *
* Do n o t a c c e s s m e m o r y a b o v e t h e e n d o f o u r s t a c k p a g e ,
2008-11-24 15:38:45 +01:00
* it m i g h t n o t e x i s t .
* /
2015-06-08 09:49:11 +02:00
andl $ ( T H R E A D _ S I Z E - 1 ) , % e a x
cmpl $ ( T H R E A D _ S I Z E - 2 0 ) , % e a x
popl % e a x
jae n m i _ s t a c k _ c o r r e c t
cmpl $ e n t r y _ S Y S E N T E R _ 3 2 , 1 2 ( % e s p )
je n m i _ d e b u g _ s t a c k _ c h e c k
2008-11-24 15:38:45 +01:00
nmi_stack_correct :
2015-06-08 09:49:11 +02:00
pushl % e a x
2008-11-24 15:38:45 +01:00
SAVE_ A L L
2015-06-08 09:49:11 +02:00
xorl % e d x , % e d x # z e r o e r r o r c o d e
movl % e s p , % e a x # p t _ r e g s p o i n t e r
call d o _ n m i
jmp r e s t o r e _ a l l _ n o t r a c e
2008-11-24 15:38:45 +01:00
nmi_stack_fixup :
2009-02-09 22:17:40 +09:00
FIX_ S T A C K 1 2 , n m i _ s t a c k _ c o r r e c t , 1
2015-06-08 09:49:11 +02:00
jmp n m i _ s t a c k _ c o r r e c t
2008-11-24 15:38:45 +01:00
nmi_debug_stack_check :
2015-06-08 09:49:11 +02:00
cmpw $ _ _ K E R N E L _ C S , 1 6 ( % e s p )
jne n m i _ s t a c k _ c o r r e c t
cmpl $ d e b u g , ( % e s p )
jb n m i _ s t a c k _ c o r r e c t
cmpl $ d e b u g _ e s p _ f i x _ i n s n , ( % e s p )
ja n m i _ s t a c k _ c o r r e c t
2009-02-09 22:17:40 +09:00
FIX_ S T A C K 2 4 , n m i _ s t a c k _ c o r r e c t , 1
2015-06-08 09:49:11 +02:00
jmp n m i _ s t a c k _ c o r r e c t
2008-11-24 15:38:45 +01:00
2014-05-04 10:36:22 -07:00
# ifdef C O N F I G _ X 8 6 _ E S P F I X 3 2
2008-11-24 15:38:45 +01:00
nmi_espfix_stack :
x86/debug: Remove perpetually broken, unmaintainable dwarf annotations
So the dwarf2 annotations in low level assembly code have
become an increasing hindrance: unreadable, messy macros
mixed into some of the most security sensitive code paths
of the Linux kernel.
These debug info annotations don't even buy the upstream
kernel anything: dwarf driven stack unwinding has caused
problems in the past so it's out of tree, and the upstream
kernel only uses the much more robust framepointers based
stack unwinding method.
In addition to that there's a steady, slow bitrot going
on with these annotations, requiring frequent fixups.
There's no tooling and no functionality upstream that
keeps it correct.
So burn down the sick forest, allowing new, healthier growth:
27 files changed, 350 insertions(+), 1101 deletions(-)
Someone who has the willingness and time to do this
properly can attempt to reintroduce dwarf debuginfo in x86
assembly code plus dwarf unwinding from first principles,
with the following conditions:
- it should be maximally readable, and maximally low-key to
'ordinary' code reading and maintenance.
- find a build time method to insert dwarf annotations
automatically in the most common cases, for pop/push
instructions that manipulate the stack pointer. This could
be done for example via a preprocessing step that just
looks for common patterns - plus special annotations for
the few cases where we want to depart from the default.
We have hundreds of CFI annotations, so automating most of
that makes sense.
- it should come with build tooling checks that ensure that
CFI annotations are sensible. We've seen such efforts from
the framepointer side, and there's no reason it couldn't be
done on the dwarf side.
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Frédéric Weisbecker <fweisbec@gmail.com
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jan Beulich <JBeulich@suse.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-05-28 12:21:47 +02:00
/ *
2008-11-24 15:38:45 +01:00
* create t h e p o i n t e r t o l s s b a c k
* /
2015-06-08 09:49:11 +02:00
pushl % s s
pushl % e s p
addl $ 4 , ( % e s p )
2008-11-24 15:38:45 +01:00
/* copy the iret frame of 12 bytes */
.rept 3
2015-06-08 09:49:11 +02:00
pushl 1 6 ( % e s p )
2008-11-24 15:38:45 +01:00
.endr
2015-06-08 09:49:11 +02:00
pushl % e a x
2008-11-24 15:38:45 +01:00
SAVE_ A L L
2015-06-08 09:49:11 +02:00
FIXUP_ E S P F I X _ S T A C K # % e a x = = % e s p
xorl % e d x , % e d x # z e r o e r r o r c o d e
call d o _ n m i
2008-11-24 15:38:45 +01:00
RESTORE_ R E G S
2015-06-08 09:49:11 +02:00
lss 1 2 + 4 ( % e s p ) , % e s p # b a c k t o e s p f i x s t a c k
jmp i r q _ r e t u r n
2014-05-04 10:36:22 -07:00
# endif
2008-11-24 15:38:45 +01:00
END( n m i )
ENTRY( i n t 3 )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ - 1 # m a r k t h i s a s a n i n t
2008-11-24 15:38:45 +01:00
SAVE_ A L L
TRACE_ I R Q S _ O F F
2015-06-08 09:49:11 +02:00
xorl % e d x , % e d x # z e r o e r r o r c o d e
movl % e s p , % e a x # p t _ r e g s p o i n t e r
call d o _ i n t 3
jmp r e t _ f r o m _ e x c e p t i o n
2008-11-24 15:38:45 +01:00
END( i n t 3 )
ENTRY( g e n e r a l _ p r o t e c t i o n )
2015-06-08 09:49:11 +02:00
pushl $ d o _ g e n e r a l _ p r o t e c t i o n
jmp e r r o r _ c o d e
2008-11-24 15:38:45 +01:00
END( g e n e r a l _ p r o t e c t i o n )
2010-10-14 11:22:52 +02:00
# ifdef C O N F I G _ K V M _ G U E S T
ENTRY( a s y n c _ p a g e _ f a u l t )
2012-09-21 13:58:10 -07:00
ASM_ C L A C
2015-06-08 09:49:11 +02:00
pushl $ d o _ a s y n c _ p a g e _ f a u l t
jmp e r r o r _ c o d e
2011-03-08 22:39:24 +01:00
END( a s y n c _ p a g e _ f a u l t )
2010-10-14 11:22:52 +02:00
# endif