2005-04-17 02:20:36 +04:00
/ *
*
* Copyright ( C ) 1 9 9 1 , 1 9 9 2 L i n u s T o r v a l d s
* /
/ *
* entry. 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 f a u l t l o w - l e v e l h a n d l i n g r o u t i n e s .
* This a l s o c o n t a i n s t h e t i m e r - i n t e r r u p t h a n d l e r , a s w e l l a s a l l i n t e r r u p t s
* and f a u l t s t h a t c a n r e s u l t i n a t a s k - s w i t c h .
*
* NOTE : This c o d e h a n d l e s s i g n a l - r e c o g n i t i o n , w h i c h h a p p e n s e v e r y t i m e
* after a t i m e r - i n t e r r u p t a n d a f t e r e a c h s y s t e m c a l l .
*
* I c h a n g e d a l l t h e . a l i g n ' s t o 4 ( 1 6 b y t e a l i g n m e n t ) , a s t h a t ' s f a s t e r
* on a 4 8 6 .
*
2007-05-02 21:27:19 +04:00
* Stack l a y o u t i n ' s y s c a l l _ e x i t ' :
2005-04-17 02:20:36 +04:00
* ptrace n e e d s t o h a v e a l l r e g 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 ,
* 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
* C( % e s p ) - % e s i
* 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 15:26:20 +03:00
* 2 4 ( % esp) - % f s
2009-02-09 16:17:40 +03: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-17 02:20:36 +04:00
*
* " current" i s i n r e g i s t e r % e b x d u r i n g a n y s l o w e n t r i e s .
* /
# include < l i n u x / l i n k a g e . h >
# include < a s m / t h r e a d _ i n f o . h >
2006-07-03 11:24:43 +04:00
# include < a s m / i r q f l a g s . h >
2005-04-17 02:20:36 +04: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 22:14:01 +03:00
# include < a s m / p a g e _ t y p e s . h >
2006-12-07 04:14:01 +03:00
# include < a s m / p e r c p u . h >
2006-06-26 15:57:44 +04:00
# include < a s m / d w a r f2 . 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 22:17:27 +04:00
# include < a s m / f t r a c e . h >
2008-05-02 22:10:09 +04:00
# include < a s m / i r q _ v e c t o r s . h >
2005-04-17 02:20:36 +04:00
2008-06-24 15:16:52 +04:00
/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
# include < l i n u x / e l f - e m . h >
# define A U D I T _ A R C H _ I 3 8 6 ( E M _ 3 8 6 | _ _ A U D I T _ A R C H _ L E )
# define _ _ A U D I T _ A R C H _ L E 0 x40 0 0 0 0 0 0
# ifndef C O N F I G _ A U D I T S Y S C A L L
# define s y s e n t e r _ a u d i t s y s c a l l _ t r a c e _ e n t r y
# define s y s e x i t _ a u d i t s y s c a l l _ e x i t _ w o r k
# endif
2006-12-07 04:14:08 +03: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 08: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 04:14:08 +03: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-17 02:20:36 +04:00
# define n r _ s y s c a l l s ( ( s y s c a l l _ t a b l e _ s i z e ) / 4 )
# ifdef C O N F I G _ P R E E M P T
2006-12-07 04:14:08 +03: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-17 02:20:36 +04:00
# else
2006-12-07 04:14:08 +03:00
# define p r e e m p t _ s t o p ( c l o b b e r s )
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 02:35:57 +04:00
# 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-17 02:20:36 +04:00
# endif
2006-07-03 11:24:43 +04: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
2008-03-25 22:16:32 +03: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 ?
2006-07-03 11:24:43 +04:00
jz 1 f
TRACE_ I R Q S _ O N
1 :
# endif
.endm
2006-06-27 13:53:48 +04:00
# ifdef C O N F I G _ V M 8 6
# define r e s u m e _ u s e r s p a c e _ s i g c h e c k _ u s e r s p a c e
# else
# define r e s u m e _ u s e r s p a c e _ s i g r e s u m e _ u s e r s p a c e
# endif
2009-02-09 16:17:40 +03: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
pushl $ 0
CFI_ A D J U S T _ C F A _ O F F S E T 4
.endm
.macro POP_GS pop=0
addl $ ( 4 + \ p o p ) , % e s p
CFI_ A D J U S T _ C F A _ O F F S E T - ( 4 + \ p o p )
.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
pushl % g s
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET gs, 0*/
.endm
.macro POP_GS pop=0
98 : popl % g s
CFI_ A D J U S T _ C F A _ O F F S E T - 4
/*CFI_RESTORE gs*/
.if \ pop < > 0
add $ \ p o p , % e s p
CFI_ A D J U S T _ C F A _ O F F S E T - \ p o p
.endif
.endm
.macro POP_GS_EX
.pushsection .fixup , " ax"
99 : movl $ 0 , ( % e s p )
jmp 9 8 b
.section _ _ ex_ t a b l e , " a "
.align 4
.long 9 8 b, 9 9 b
.popsection
.endm
.macro PTGS_TO_GS
98 : mov P T _ G S ( % e s p ) , % g s
.endm
.macro PTGS_TO_GS_EX
.pushsection .fixup , " ax"
99 : movl $ 0 , P T _ G S ( % e s p )
jmp 9 8 b
.section _ _ ex_ t a b l e , " a "
.align 4
.long 9 8 b, 9 9 b
.popsection
.endm
.macro GS_TO_REG reg
movl % g s , \ r e g
/*CFI_REGISTER gs, \reg*/
.endm
.macro REG_TO_PTGS reg
movl \ r e g , P T _ G S ( % e s p )
/*CFI_REL_OFFSET gs, PT_GS*/
.endm
.macro SET_KERNEL_GS reg
2009-02-09 16:17:40 +03:00
movl $ ( _ _ K E R N E L _ S T A C K _ C A N A R Y ) , \ r e g
2009-02-09 16:17:40 +03:00
movl \ r e g , % g s
.endm
# endif / * C O N F I G _ X 8 6 _ 3 2 _ L A Z Y _ G S * /
2009-02-09 16:17:40 +03:00
.macro SAVE_ALL
cld
2009-02-09 16:17:40 +03:00
PUSH_ G S
2009-02-09 16:17:40 +03:00
pushl % f s
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET fs, 0;*/
pushl % e s
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET es, 0;*/
pushl % d s
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET ds, 0;*/
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e a x , 0
pushl % e b p
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e b p , 0
pushl % e d i
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e d i , 0
pushl % e s i
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e s i , 0
pushl % e d x
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e d x , 0
pushl % e c x
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e c x , 0
pushl % e b x
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e b x , 0
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
2007-02-13 15:26:20 +03:00
movl % e d x , % f s
2009-02-09 16:17:40 +03:00
SET_ K E R N E L _ G S % e d x
2009-02-09 16:17:40 +03:00
.endm
2005-04-17 02:20:36 +04:00
2009-02-09 16:17:40 +03:00
.macro RESTORE_INT_REGS
popl % e b x
CFI_ A D J U S T _ C F A _ O F F S E T - 4
CFI_ R E S T O R E e b x
popl % e c x
CFI_ A D J U S T _ C F A _ O F F S E T - 4
CFI_ R E S T O R E e c x
popl % e d x
CFI_ A D J U S T _ C F A _ O F F S E T - 4
CFI_ R E S T O R E e d x
popl % e s i
CFI_ A D J U S T _ C F A _ O F F S E T - 4
CFI_ R E S T O R E e s i
popl % e d i
CFI_ A D J U S T _ C F A _ O F F S E T - 4
CFI_ R E S T O R E e d i
popl % e b p
CFI_ A D J U S T _ C F A _ O F F S E T - 4
CFI_ R E S T O R E e b p
popl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T - 4
2006-06-26 15:57:44 +04:00
CFI_ R E S T O R E e a x
2009-02-09 16:17:40 +03:00
.endm
2005-04-17 02:20:36 +04:00
2009-02-09 16:17:40 +03:00
.macro RESTORE_REGS pop=0
2009-02-09 16:17:40 +03:00
RESTORE_ I N T _ R E G S
1 : popl % d s
CFI_ A D J U S T _ C F A _ O F F S E T - 4
/*CFI_RESTORE ds;*/
2 : popl % e s
CFI_ A D J U S T _ C F A _ O F F S E T - 4
/*CFI_RESTORE es;*/
3 : popl % f s
CFI_ A D J U S T _ C F A _ O F F S E T - 4
/*CFI_RESTORE fs;*/
2009-02-09 16:17:40 +03:00
POP_ G S \ p o p
2009-02-09 16:17:40 +03:00
.pushsection .fixup , " ax"
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
.section _ _ ex_ t a b l e , " a "
.align 4
.long 1 b, 4 b
.long 2 b, 5 b
.long 3 b, 6 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 04:14:02 +03:00
.popsection
2009-02-09 16:17:40 +03:00
POP_ G S _ E X
2009-02-09 16:17:40 +03:00
.endm
2005-04-17 02:20:36 +04:00
2009-02-09 16:17:40 +03:00
.macro RING0_INT_FRAME
CFI_ S T A R T P R O C s i m p l e
CFI_ S I G N A L _ F R A M E
CFI_ D E F _ C F A e s p , 3 * 4
/*CFI_OFFSET cs, -2*4;*/
2006-06-26 15:57:44 +04:00
CFI_ O F F S E T e i p , - 3 * 4
2009-02-09 16:17:40 +03:00
.endm
2006-06-26 15:57:44 +04:00
2009-02-09 16:17:40 +03:00
.macro RING0_EC_FRAME
CFI_ S T A R T P R O C s i m p l e
CFI_ S I G N A L _ F R A M E
CFI_ D E F _ C F A e s p , 4 * 4
/*CFI_OFFSET cs, -2*4;*/
2006-06-26 15:57:44 +04:00
CFI_ O F F S E T e i p , - 3 * 4
2009-02-09 16:17:40 +03:00
.endm
2006-06-26 15:57:44 +04:00
2009-02-09 16:17:40 +03:00
.macro RING0_PTREGS_FRAME
CFI_ S T A R T P R O C s i m p l e
CFI_ S I G N A L _ F R A M E
CFI_ D E F _ C F A e s p , P T _ O L D E S P - P T _ E B X
/*CFI_OFFSET cs, PT_CS-PT_OLDESP;*/
CFI_ O F F S E T e i p , P T _ E I P - P T _ O L D E S P
/*CFI_OFFSET es, PT_ES-PT_OLDESP;*/
/*CFI_OFFSET ds, PT_DS-PT_OLDESP;*/
CFI_ O F F S E T e a x , P T _ E A X - P T _ O L D E S P
CFI_ O F F S E T e b p , P T _ E B P - P T _ O L D E S P
CFI_ O F F S E T e d i , P T _ E D I - P T _ O L D E S P
CFI_ O F F S E T e s i , P T _ E S I - P T _ O L D E S P
CFI_ O F F S E T e d x , P T _ E D X - P T _ O L D E S P
CFI_ O F F S E T e c x , P T _ E C X - P T _ O L D E S P
2006-12-07 04:14:02 +03:00
CFI_ O F F S E T e b x , P T _ E B X - P T _ O L D E S P
2009-02-09 16:17:40 +03:00
.endm
2005-04-17 02:20:36 +04:00
ENTRY( r e t _ f r o m _ f o r k )
2006-06-26 15:57:44 +04:00
CFI_ S T A R T P R O C
2005-04-17 02:20:36 +04:00
pushl % e a x
2006-07-30 14:04:08 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
call s c h e d u l e _ t a i l
GET_ T H R E A D _ I N F O ( % e b p )
popl % e a x
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T - 4
2006-09-19 03:20:40 +04:00
pushl $ 0 x02 0 2 # R e s e t k e r n e l e f l a g s
CFI_ A D J U S T _ C F A _ O F F S E T 4
popfl
CFI_ A D J U S T _ C F A _ O F F S E T - 4
2005-04-17 02:20:36 +04:00
jmp s y s c a l l _ e x i t
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( r e t _ f r o m _ f o r k )
2005-04-17 02:20:36 +04:00
2009-09-08 20:47:55 +04:00
/ *
* Interrupt e x i t f u n c t i o n s s h o u l d b e p r o t e c t e d a g a i n s t k p r o b e s
* /
.pushsection .kprobes .text , " ax"
2005-04-17 02:20:36 +04: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
2006-06-26 15:57:44 +04:00
RING0 _ P T R E G S _ F R A M E
2005-04-17 02:20:36 +04:00
ret_from_exception :
2006-12-07 04:14:08 +03:00
preempt_ s t o p ( C L B R _ A N Y )
2005-04-17 02:20:36 +04:00
ret_from_intr :
GET_ T H R E A D _ I N F O ( % e b p )
2006-06-27 13:53:48 +04:00
check_userspace :
2006-12-07 04:14:02 +03: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
2008-03-25 22:16:32 +03:00
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
2006-09-26 12:52:39 +04: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 04:14:02 +03:00
2005-04-17 02:20:36 +04:00
ENTRY( r e s u m e _ u s e r s p a c e )
2007-10-12 00:11:12 +04:00
LOCKDEP_ S Y S _ E X I T
2006-12-07 04:14:08 +03:00
DISABLE_ I N T E R R U P T S ( C L B R _ A N Y ) # m a k e s u r e w e d o n ' t m i s s a n i n t e r r u p t
2005-04-17 02:20:36 +04:00
# setting n e e d _ r e s c h e d o r s i g p e n d i n g
# between s a m p l i n g a n d t h e i r e t
2008-06-06 12:14:08 +04:00
TRACE_ I R Q S _ O F F
2005-04-17 02:20:36 +04:00
movl T I _ f l a g s ( % e b p ) , % e c x
andl $ _ T I F _ W O R K _ M A S K , % e c x # i s t h e r e a n y w o r k t o b e d o n e o n
# int/ e x c e p t i o n r e t u r n ?
jne w o r k _ p e n d i n g
jmp r e s t o r e _ a l l
2007-02-13 15:26:24 +03:00
END( r e t _ f r o m _ e x c e p t i o n )
2005-04-17 02:20:36 +04: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 04:14:08 +03:00
DISABLE_ I N T E R R U P T S ( C L B R _ A N Y )
2005-04-17 02:20:36 +04:00
cmpl $ 0 ,T I _ p r e e m p t _ c o u n t ( % e b p ) # n o n - z e r o p r e e m p t _ c o u n t ?
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 02:35:57 +04:00
jnz r e s t o r e _ a l l
2005-04-17 02:20:36 +04:00
need_resched :
movl T I _ f l a g s ( % e b p ) , % e c x # n e e d _ r e s c h e d s e t ?
testb $ _ T I F _ N E E D _ R E S C H E D , % c l
jz r e s t o r e _ a l l
2008-03-25 22:16:32 +03: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 ( e x c e p t i o n p a t h ) ?
2005-04-17 02:20:36 +04:00
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 15:26:24 +03:00
END( r e s u m e _ k e r n e l )
2005-04-17 02:20:36 +04:00
# endif
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2009-09-08 20:47:55 +04:00
/ *
* End o f k p r o b e s s e c t i o n
* /
.popsection
2005-04-17 02:20:36 +04:00
/ * SYSENTER_ R E T U R N p o i n t s t o a f t e r t h e " s y s e n t e r " i n s t r u c t i o n i n
the v s y s c a l l p a g e . S e e v s y s c a l l - s y s e n t r y . S , w h i c h d e f i n e s t h e s y m b o l . * /
# sysenter c a l l h a n d l e r s t u b
2008-01-30 15:30:43 +03:00
ENTRY( i a32 _ s y s e n t e r _ t a r g e t )
2006-06-26 15:57:44 +04:00
CFI_ S T A R T P R O C s i m p l e
2006-09-26 12:52:41 +04:00
CFI_ S I G N A L _ F R A M E
2006-06-26 15:57:44 +04:00
CFI_ D E F _ C F A e s p , 0
CFI_ R E G I S T E R e s p , e b p
2008-01-30 15:31:02 +03:00
movl T S S _ s y s e n t e r _ s p0 ( % e s p ) ,% e s p
2005-04-17 02:20:36 +04:00
sysenter_past_esp :
2006-07-03 11:24:43 +04:00
/ *
2008-03-25 02:43:21 +03:00
* Interrupts a r e d i s a b l e d h e r e , b u t w e c a n ' t t r a c e i t u n t i l
* enough k e r n e l s t a t e t o c a l l T R A C E _ I R Q S _ O F F c a n b e c a l l e d - b u t
* we i m m e d i a t e l y e n a b l e i n t e r r u p t s a t t h a t p o i n t a n y w a y .
2006-07-03 11:24:43 +04:00
* /
2005-04-17 02:20:36 +04:00
pushl $ ( _ _ U S E R _ D S )
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET ss, 0*/
2005-04-17 02:20:36 +04:00
pushl % e b p
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e s p , 0
2005-04-17 02:20:36 +04:00
pushfl
2008-03-25 02:43:21 +03:00
orl $ X 8 6 _ E F L A G S _ I F , ( % e s p )
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
pushl $ ( _ _ U S E R _ C S )
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET cs, 0*/
[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 13:53:50 +04:00
/ *
* Push c u r r e n t _ t h r e a d _ i n f o ( ) - > s y s e n t e r _ r e t u r n t o t h e s t a c k .
* A t i n y b i t o f o f f s e t f i x u p i s n e c e s s a r y - 4 * 4 m e a n s t h e 4 w o r d s
* pushed a b o v e ; +8 corresponds to copy_thread's esp0 setting.
* /
pushl ( T I _ s y s e n t e r _ r e t u r n - T H R E A D _ S I Z E + 8 + 4 * 4 ) ( % e s p )
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e i p , 0
2005-04-17 02:20:36 +04:00
2008-03-25 02:43:21 +03:00
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
SAVE_ A L L
ENABLE_ I N T E R R U P T S ( C L B R _ N O N E )
2005-04-17 02:20:36 +04:00
/ *
* Load t h e p o t e n t i a l s i x t h a r g u m e n t f r o m u s e r s t a c k .
* Careful a b o u t s e c u r i t y .
* /
cmpl $ _ _ P A G E _ O F F S E T - 3 ,% e b p
jae s y s c a l l _ f a u l t
1 : movl ( % e b p ) ,% e b p
2008-03-25 02:43:21 +03:00
movl % e b p ,P T _ E B P ( % e s p )
2005-04-17 02:20:36 +04:00
.section _ _ ex_ t a b l e ," a "
.align 4
.long 1 b,s y s c a l l _ f a u l t
.previous
GET_ T H R E A D _ I N F O ( % e b p )
2009-03-14 09:38:13 +03:00
testl $ _ T I F _ W O R K _ S Y S C A L L _ E N T R Y ,T I _ f l a g s ( % e b p )
2008-06-24 15:16:52 +04:00
jnz s y s e n t e r _ a u d i t
sysenter_do_call :
2005-04-17 02:20:36 +04:00
cmpl $ ( n r _ s y s c a l l s ) , % e a x
jae s y s c a l l _ b a d s y s
call * s y s _ c a l l _ t a b l e ( ,% e a x ,4 )
2006-12-07 04:14:02 +03:00
movl % e a x ,P T _ E A X ( % e s p )
2007-10-12 00:11:12 +04:00
LOCKDEP_ S Y S _ E X I T
2007-05-02 21:27:14 +04:00
DISABLE_ I N T E R R U P T S ( C L B R _ A N Y )
2006-07-03 11:24:43 +04:00
TRACE_ I R Q S _ O F F
2005-04-17 02:20:36 +04:00
movl T I _ f l a g s ( % e b p ) , % e c x
2009-03-14 09:38:13 +03:00
testl $ _ T I F _ A L L W O R K _ M A S K , % e c x
2008-06-24 15:16:52 +04:00
jne s y s e x i t _ a u d i t
sysenter_exit :
2005-04-17 02:20:36 +04:00
/* if something modifies registers it must also disable sysexit */
2006-12-07 04:14:02 +03:00
movl P T _ E I P ( % e s p ) , % e d x
movl P T _ O L D E S P ( % e s p ) , % e c x
2005-04-17 02:20:36 +04:00
xorl % e b p ,% e b p
2006-07-03 11:24:43 +04:00
TRACE_ I R Q S _ O N
2007-02-13 15:26:20 +03:00
1 : mov P T _ F S ( % e s p ) , % f s
2009-02-09 16:17:40 +03:00
PTGS_ T O _ G S
2008-06-25 08:19:26 +04:00
ENABLE_ I N T E R R U P T S _ S Y S E X I T
2008-06-24 15:16:52 +04:00
# ifdef C O N F I G _ A U D I T S Y S C A L L
sysenter_audit :
2009-03-14 09:38:13 +03:00
testl $ ( _ T I F _ W O R K _ S Y S C A L L _ E N T R Y & ~ _ T I F _ S Y S C A L L _ A U D I T ) ,T I _ f l a g s ( % e b p )
2008-06-24 15:16:52 +04:00
jnz s y s c a l l _ t r a c e _ e n t r y
addl $ 4 ,% e s p
CFI_ A D J U S T _ C F A _ O F F S E T - 4
/* %esi already in 8(%esp) 6th arg: 4th syscall arg */
/* %edx already in 4(%esp) 5th arg: 3rd syscall arg */
/* %ecx already in 0(%esp) 4th arg: 2nd syscall arg */
movl % e b x ,% e c x / * 3 r d a r g : 1 s t s y s c a l l a r g * /
movl % e a x ,% e d x / * 2 n d a r g : s y s c a l l n u m b e r * /
movl $ A U D I T _ A R C H _ I 3 8 6 ,% e a x / * 1 s t a r g : a u d i t a r c h * /
call a u d i t _ s y s c a l l _ e n t r y
pushl % e b x
CFI_ A D J U S T _ C F A _ O F F S E T 4
movl P T _ E A X ( % e s p ) ,% e a x / * r e l o a d s y s c a l l n u m b e r * /
jmp s y s e n t e r _ d o _ c a l l
sysexit_audit :
2009-03-14 09:38:13 +03:00
testl $ ( _ T I F _ A L L W O R K _ M A S K & ~ _ T I F _ S Y S C A L L _ A U D I T ) , % e c x
2008-06-24 15:16:52 +04:00
jne s y s c a l l _ e x i t _ w o r k
TRACE_ I R Q S _ O N
ENABLE_ I N T E R R U P T S ( C L B R _ A N Y )
movl % e a x ,% e d x / * s e c o n d a r g , s y s c a l l r e t u r n v a l u e * /
cmpl $ 0 ,% e a x / * i s i t < 0 ? * /
setl % a l / * 1 i f s o , 0 i f n o t * /
movzbl % a l ,% e a x / * z e r o - e x t e n d t h a t * /
inc % e a x / * f i r s t a r g , 0 - > 1 ( A U D I T S C _ S U C C E S S ) , 1 - > 2 ( A U D I T S C _ F A I L U R E ) * /
call a u d i t _ s y s c a l l _ e x i t
DISABLE_ I N T E R R U P T S ( C L B R _ A N Y )
TRACE_ I R Q S _ O F F
movl T I _ f l a g s ( % e b p ) , % e c x
2009-03-14 09:38:13 +03:00
testl $ ( _ T I F _ A L L W O R K _ M A S K & ~ _ T I F _ S Y S C A L L _ A U D I T ) , % e c x
2008-06-24 15:16:52 +04:00
jne s y s c a l l _ e x i t _ w o r k
movl P T _ E A X ( % e s p ) ,% e a x / * r e l o a d s y s c a l l r e t u r n v a l u e * /
jmp s y s e n t e r _ e x i t
# endif
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
[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 04:14:02 +03:00
.pushsection .fixup , " ax"
2007-02-13 15:26:20 +03:00
2 : movl $ 0 ,P T _ F S ( % e s p )
[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 04:14:02 +03:00
jmp 1 b
.section _ _ ex_ t a b l e ," a "
.align 4
.long 1 b,2 b
.popsection
2009-02-09 16:17:40 +03:00
PTGS_ T O _ G S _ E X
2008-01-30 15:30:43 +03:00
ENDPROC( i a32 _ s y s e n t e r _ t a r g e t )
2005-04-17 02:20:36 +04:00
2009-09-08 20:47:55 +04:00
/ *
* syscall s t u b i n c l u d i n g i r q e x i t s h o u l d b e p r o t e c t e d a g a i n s t k p r o b e s
* /
.pushsection .kprobes .text , " ax"
2005-04-17 02:20:36 +04:00
# system c a l l h a n d l e r s t u b
ENTRY( s y s t e m _ c a l l )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E # c a n ' t u n w i n d i n t o u s e r s p a c e a n y w a y
2005-04-17 02:20:36 +04:00
pushl % e a x # s a v e o r i g _ e a x
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
SAVE_ A L L
GET_ T H R E A D _ I N F O ( % e b p )
[PATCH] UML Support - Ptrace: adds the host SYSEMU support, for UML and general usage
Jeff Dike <jdike@addtoit.com>,
Paolo 'Blaisorblade' Giarrusso <blaisorblade_spam@yahoo.it>,
Bodo Stroesser <bstroesser@fujitsu-siemens.com>
Adds a new ptrace(2) mode, called PTRACE_SYSEMU, resembling PTRACE_SYSCALL
except that the kernel does not execute the requested syscall; this is useful
to improve performance for virtual environments, like UML, which want to run
the syscall on their own.
In fact, using PTRACE_SYSCALL means stopping child execution twice, on entry
and on exit, and each time you also have two context switches; with SYSEMU you
avoid the 2nd stop and so save two context switches per syscall.
Also, some architectures don't have support in the host for changing the
syscall number via ptrace(), which is currently needed to skip syscall
execution (UML turns any syscall into getpid() to avoid it being executed on
the host). Fixing that is hard, while SYSEMU is easier to implement.
* This version of the patch includes some suggestions of Jeff Dike to avoid
adding any instructions to the syscall fast path, plus some other little
changes, by myself, to make it work even when the syscall is executed with
SYSENTER (but I'm unsure about them). It has been widely tested for quite a
lot of time.
* Various fixed were included to handle the various switches between
various states, i.e. when for instance a syscall entry is traced with one of
PT_SYSCALL / _SYSEMU / _SINGLESTEP and another one is used on exit.
Basically, this is done by remembering which one of them was used even after
the call to ptrace_notify().
* We're combining TIF_SYSCALL_EMU with TIF_SYSCALL_TRACE or TIF_SINGLESTEP
to make do_syscall_trace() notice that the current syscall was started with
SYSEMU on entry, so that no notification ought to be done in the exit path;
this is a bit of a hack, so this problem is solved in another way in next
patches.
* Also, the effects of the patch:
"Ptrace - i386: fix Syscall Audit interaction with singlestep"
are cancelled; they are restored back in the last patch of this series.
Detailed descriptions of the patches doing this kind of processing follow (but
I've already summed everything up).
* Fix behaviour when changing interception kind #1.
In do_syscall_trace(), we check the status of the TIF_SYSCALL_EMU flag
only after doing the debugger notification; but the debugger might have
changed the status of this flag because he continued execution with
PTRACE_SYSCALL, so this is wrong. This patch fixes it by saving the flag
status before calling ptrace_notify().
* Fix behaviour when changing interception kind #2:
avoid intercepting syscall on return when using SYSCALL again.
A guest process switching from using PTRACE_SYSEMU to PTRACE_SYSCALL
crashes.
The problem is in arch/i386/kernel/entry.S. The current SYSEMU patch
inhibits the syscall-handler to be called, but does not prevent
do_syscall_trace() to be called after this for syscall completion
interception.
The appended patch fixes this. It reuses the flag TIF_SYSCALL_EMU to
remember "we come from PTRACE_SYSEMU and now are in PTRACE_SYSCALL", since
the flag is unused in the depicted situation.
* Fix behaviour when changing interception kind #3:
avoid intercepting syscall on return when using SINGLESTEP.
When testing 2.6.9 and the skas3.v6 patch, with my latest patch and had
problems with singlestepping on UML in SKAS with SYSEMU. It looped
receiving SIGTRAPs without moving forward. EIP of the traced process was
the same for all SIGTRAPs.
What's missing is to handle switching from PTRACE_SYSCALL_EMU to
PTRACE_SINGLESTEP in a way very similar to what is done for the change from
PTRACE_SYSCALL_EMU to PTRACE_SYSCALL_TRACE.
I.e., after calling ptrace(PTRACE_SYSEMU), on the return path, the debugger is
notified and then wake ups the process; the syscall is executed (or skipped,
when do_syscall_trace() returns 0, i.e. when using PTRACE_SYSEMU), and
do_syscall_trace() is called again. Since we are on the return path of a
SYSEMU'd syscall, if the wake up is performed through ptrace(PTRACE_SYSCALL),
we must still avoid notifying the parent of the syscall exit. Now, this
behaviour is extended even to resuming with PTRACE_SINGLESTEP.
Signed-off-by: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Cc: Jeff Dike <jdike@addtoit.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-04 02:57:18 +04:00
# system c a l l t r a c i n g i n o p e r a t i o n / e m u l a t i o n
2009-03-14 09:38:13 +03:00
testl $ _ T I F _ W O R K _ S Y S C A L L _ E N T R Y ,T I _ f l a g s ( % e b p )
2005-04-17 02:20:36 +04:00
jnz s y s c a l l _ t r a c e _ e n t r y
cmpl $ ( n r _ s y s c a l l s ) , % e a x
jae s y s c a l l _ b a d s y s
syscall_call :
call * s y s _ c a l l _ t a b l e ( ,% e a x ,4 )
2006-12-07 04:14:02 +03:00
movl % e a x ,P T _ E A X ( % e s p ) # s t o r e t h e r e t u r n v a l u e
2005-04-17 02:20:36 +04:00
syscall_exit :
2007-10-12 00:11:12 +04:00
LOCKDEP_ S Y S _ E X I T
2006-12-07 04:14:08 +03:00
DISABLE_ I N T E R R U P T S ( C L B R _ A N Y ) # m a k e s u r e w e d o n ' t m i s s a n i n t e r r u p t
2005-04-17 02:20:36 +04:00
# setting n e e d _ r e s c h e d o r s i g p e n d i n g
# between s a m p l i n g a n d t h e i r e t
2006-07-03 11:24:43 +04:00
TRACE_ I R Q S _ O F F
2005-04-17 02:20:36 +04:00
movl T I _ f l a g s ( % e b p ) , % e c x
2009-03-14 09:38:13 +03:00
testl $ _ T I F _ A L L W O R K _ M A S K , % e c x # c u r r e n t - > w o r k
2005-04-17 02:20:36 +04:00
jne s y s c a l l _ e x i t _ w o r k
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 02:35:57 +04:00
TRACE_ I R Q S _ I R E T
restore_all_notrace :
2006-12-07 04:14:02 +03: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
2005-04-17 02:24:01 +04:00
# 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 .
2006-12-07 04:14:02 +03:00
movb P T _ O L D S S ( % e s p ) , % a h
movb P T _ C S ( % e s p ) , % a l
2008-03-25 22:16:32 +03:00
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
2006-09-26 12:52:39 +04:00
cmpl $ ( ( S E G M E N T _ L D T < < 8 ) | U S E R _ R P L ) , % e a x
2006-06-26 15:57:44 +04:00
CFI_ R E M E M B E R _ S T A T E
2005-04-17 02:20:36 +04:00
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
restore_nocheck :
2009-02-09 16:17:40 +03: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
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T - 4
2008-02-14 00:29:53 +03:00
irq_return :
2008-02-10 01:24:08 +03:00
INTERRUPT_ R E T U R N
2005-04-17 02:20:36 +04:00
.section .fixup , " ax"
2008-03-18 02:37:12 +03:00
ENTRY( i r e t _ e x c )
2005-04-29 20:38:44 +04:00
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-17 02:20:36 +04:00
.previous
.section _ _ ex_ t a b l e ," a "
.align 4
2008-02-10 01:24:08 +03:00
.long irq_ r e t u r n ,i r e t _ e x c
2005-04-17 02:20:36 +04:00
.previous
2006-06-26 15:57:44 +04:00
CFI_ R E S T O R E _ S T A T E
2005-04-17 02:20:36 +04:00
ldt_ss :
2006-12-07 04:14:02 +03:00
larl P T _ O L D S S ( % e s p ) , % e a x
2005-04-17 02:20:36 +04:00
jnz r e s t o r e _ n o c h e c k
testl $ 0 x00 4 0 0 0 0 0 , % e a x # r e t u r n i n g t o 32 b i t s t a c k ?
jnz r e s t o r e _ n o c h e c k # a l l r i g h t , n o r m a l r e t u r n
2006-12-07 04:14:07 +03:00
# ifdef C O N F I G _ P A R A V I R T
/ *
* The k e r n e l c a n ' t r u n o n a n o n - f l a t s t a c k i f p a r a v i r t m o d e
* is a c t i v e . R a t h e r t h a n t r y t o f i x u p t h e h i g h b i t s o f
* ESP, b y p a s s t h i s c o d e e n t i r e l y . T h i s m a y b r e a k D O S e m u
* and/ o r W i n e s u p p o r t i n a p a r a v i r t V M , a l t h o u g h t h e o p t i o n
* is s t i l l a v a i l a b l e t o i m p l e m e n t t h e s e t t i n g o f t h e h i g h
* 1 6 - bits i n t h e I N T E R R U P T _ R E T U R N p a r a v i r t - o p .
* /
2007-10-16 22:51:29 +04:00
cmpl $ 0 , p v _ i n f o + P A R A V I R T _ e n a b l e d
2006-12-07 04:14:07 +03:00
jne r e s t o r e _ n o c h e c k
# endif
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 02:35:58 +04: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 .
* /
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 * /
sub % e a x , % e d x / * o f f s e t ( l o w w o r d i s 0 ) * /
PER_ C P U ( g d t _ p a g e , % e b x )
shr $ 1 6 , % e d x
mov % d l , G D T _ E N T R Y _ E S P F I X _ S S * 8 + 4 ( % e b x ) / * b i t s 1 6 . . 2 3 * /
mov % d h , G D T _ E N T R Y _ E S P F I X _ S S * 8 + 7 ( % e b x ) / * b i t s 2 4 . . 3 1 * /
2006-12-07 04:14:01 +03:00
pushl $ _ _ E S P F I X _ S S
CFI_ A D J U S T _ C F A _ O F F S E T 4
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 02:35:58 +04:00
push % e a x / * n e w k e r n e l e s p * /
2006-12-07 04:14:01 +03:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
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 02:35:57 +04:00
/ * 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
* 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
* the i r q s t a t e a f t e r t h e i r e t * /
2006-12-07 04:14:08 +03:00
DISABLE_ I N T E R R U P T S ( C L B R _ E A X )
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 02:35:58 +04: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 * /
2006-12-07 04:14:01 +03:00
CFI_ A D J U S T _ C F A _ O F F S E T - 8
jmp r e s t o r e _ n o c h e c k
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
ENDPROC( s y s t e m _ c a l l )
2005-04-17 02:20:36 +04:00
# perform w o r k t h a t n e e d s t o b e d o n e i m m e d i a t e l y b e f o r e r e s u m p t i o n
ALIGN
2006-06-26 15:57:44 +04:00
RING0 _ P T R E G S _ F R A M E # c a n ' t u n w i n d i n t o u s e r s p a c e a n y w a y
2005-04-17 02:20:36 +04:00
work_pending :
testb $ _ T I F _ N E E D _ R E S C H E D , % c l
jz w o r k _ n o t i f y s i g
work_resched :
call s c h e d u l e
2007-10-12 00:11:12 +04:00
LOCKDEP_ S Y S _ E X I T
2006-12-07 04:14:08 +03:00
DISABLE_ I N T E R R U P T S ( C L B R _ A N Y ) # m a k e s u r e w e d o n ' t m i s s a n i n t e r r u p t
2005-04-17 02:20:36 +04:00
# setting n e e d _ r e s c h e d o r s i g p e n d i n g
# between s a m p l i n g a n d t h e i r e t
2006-07-03 11:24:43 +04:00
TRACE_ I R Q S _ O F F
2005-04-17 02:20:36 +04:00
movl T I _ f l a g s ( % e b p ) , % e c x
andl $ _ T I F _ W O R K _ M A S K , % e c x # i s t h e r e a n y w o r k t o b e d o n e o t h e r
# than s y s c a l l t r a c i n g ?
jz r e s t o r e _ a l l
testb $ _ T I F _ N E E D _ R E S C H E D , % c l
jnz w o r k _ r e s c h e d
work_notifysig : # deal w i t h p e n d i n g s i g n a l s a n d
# notify- r e s u m e r e q u e s t s
2006-12-07 04:14:04 +03:00
# ifdef C O N F I G _ V M 8 6
2008-03-25 22:16:32 +03:00
testl $ X 8 6 _ E F L A G S _ V M , P T _ E F L A G S ( % e s p )
2005-04-17 02:20:36 +04:00
movl % e s p , % e a x
jne w o r k _ n o t i f y s i g _ v86 # r e t u r n i n g t o k e r n e l - s p a c e o r
# vm8 6 - s p a c e
xorl % e d x , % e d x
call d o _ n o t i f y _ r e s u m e
2006-06-27 13:53:48 +04:00
jmp r e s u m e _ u s e r s p a c e _ s i g
2005-04-17 02:20:36 +04:00
ALIGN
work_notifysig_v86 :
pushl % e c x # s a v e t i _ f l a g s f o r d o _ n o t i f y _ r e s u m e
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
call s a v e _ v86 _ s t a t e # % e a x c o n t a i n s p t _ r e g s p o i n t e r
popl % e c x
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T - 4
2005-04-17 02:20:36 +04:00
movl % e a x , % e s p
2006-12-07 04:14:04 +03:00
# else
movl % e s p , % e a x
# endif
2005-04-17 02:20:36 +04:00
xorl % e d x , % e d x
call d o _ n o t i f y _ r e s u m e
2006-06-27 13:53:48 +04:00
jmp r e s u m e _ u s e r s p a c e _ s i g
2007-02-13 15:26:24 +03:00
END( w o r k _ p e n d i n g )
2005-04-17 02:20:36 +04:00
# perform s y s c a l l e x i t t r a c i n g
ALIGN
syscall_trace_entry :
2006-12-07 04:14:02 +03:00
movl $ - E N O S Y S ,P T _ E A X ( % e s p )
2005-04-17 02:20:36 +04:00
movl % e s p , % e a x
2008-07-09 13:38:07 +04:00
call s y s c a l l _ t r a c e _ e n t e r
/* What it returned is what we'll actually use. */
2005-04-17 02:20:36 +04:00
cmpl $ ( n r _ s y s c a l l s ) , % e a x
jnae s y s c a l l _ c a l l
jmp s y s c a l l _ e x i t
2007-02-13 15:26:24 +03:00
END( s y s c a l l _ t r a c e _ e n t r y )
2005-04-17 02:20:36 +04:00
# perform s y s c a l l e x i t t r a c i n g
ALIGN
syscall_exit_work :
2009-03-14 09:38:13 +03:00
testl $ _ T I F _ W O R K _ S Y S C A L L _ E X I T , % e c x
2005-04-17 02:20:36 +04:00
jz w o r k _ p e n d i n g
2006-07-03 11:24:43 +04:00
TRACE_ I R Q S _ O N
2008-07-09 13:38:07 +04:00
ENABLE_ I N T E R R U P T S ( C L B R _ A N Y ) # c o u l d l e t s y s c a l l _ t r a c e _ l e a v e ( ) c a l l
2005-04-17 02:20:36 +04:00
# schedule( ) i n s t e a d
movl % e s p , % e a x
2008-07-09 13:38:07 +04:00
call s y s c a l l _ t r a c e _ l e a v e
2005-04-17 02:20:36 +04:00
jmp r e s u m e _ u s e r s p a c e
2007-02-13 15:26:24 +03:00
END( s y s c a l l _ e x i t _ w o r k )
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2005-04-17 02:20:36 +04:00
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E # c a n ' t u n w i n d i n t o u s e r s p a c e a n y w a y
2005-04-17 02:20:36 +04:00
syscall_fault :
GET_ T H R E A D _ I N F O ( % e b p )
2006-12-07 04:14:02 +03:00
movl $ - E F A U L T ,P T _ E A X ( % e s p )
2005-04-17 02:20:36 +04:00
jmp r e s u m e _ u s e r s p a c e
2007-02-13 15:26:24 +03:00
END( s y s c a l l _ f a u l t )
2005-04-17 02:20:36 +04:00
syscall_badsys :
2006-12-07 04:14:02 +03:00
movl $ - E N O S Y S ,P T _ E A X ( % e s p )
2005-04-17 02:20:36 +04:00
jmp r e s u m e _ u s e r s p a c e
2007-02-13 15:26:24 +03:00
END( s y s c a l l _ b a d s y s )
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2009-09-08 20:47:55 +04:00
/ *
* End o f k p r o b e s s e c t i o n
* /
.popsection
2005-04-17 02:20:36 +04:00
2009-02-10 17:51:46 +03:00
/ *
* System c a l l s t h a t n e e d a p t _ r e g s p o i n t e r .
* /
2009-12-10 03:01:51 +03:00
# define P T R E G S C A L L 0 ( n a m e ) \
2009-02-10 17:51:46 +03:00
ALIGN; \
ptregs_ ## n a m e : \
leal 4 ( % e s p ) ,% e a x ; \
jmp s y s _ ## n a m e ;
2009-12-10 03:01:51 +03:00
# define P T R E G S C A L L 1 ( n a m e ) \
ALIGN; \
ptregs_ ## n a m e : \
leal 4 ( % e s p ) ,% e d x ; \
movl P T _ E B X ( % e d x ) ,% e a x ; \
jmp s y s _ ## n a m e ;
# define P T R E G S C A L L 2 ( n a m e ) \
ALIGN; \
ptregs_ ## n a m e : \
leal 4 ( % e s p ) ,% e c x ; \
movl P T _ E C X ( % e c x ) ,% e d x ; \
movl P T _ E B X ( % e c x ) ,% e a x ; \
jmp s y s _ ## n a m e ;
# define P T R E G S C A L L 3 ( n a m e ) \
ALIGN; \
ptregs_ ## n a m e : \
leal 4 ( % e s p ) ,% e a x ; \
pushl % e a x ; \
movl P T _ E D X ( % e a x ) ,% e c x ; \
movl P T _ E C X ( % e a x ) ,% e d x ; \
movl P T _ E B X ( % e a x ) ,% e a x ; \
call s y s _ ## n a m e ; \
addl $ 4 ,% e s p ; \
ret
2009-12-10 03:01:52 +03:00
PTREGSCALL1 ( i o p l )
2009-12-10 03:01:51 +03:00
PTREGSCALL0 ( f o r k )
PTREGSCALL0 ( c l o n e )
PTREGSCALL0 ( v f o r k )
2009-12-10 03:01:53 +03:00
PTREGSCALL3 ( e x e c v e )
2009-12-10 03:01:54 +03:00
PTREGSCALL2 ( s i g a l t s t a c k )
2009-12-10 03:01:51 +03:00
PTREGSCALL0 ( s i g r e t u r n )
PTREGSCALL0 ( r t _ s i g r e t u r n )
2009-12-10 03:01:55 +03:00
PTREGSCALL2 ( v m 8 6 )
PTREGSCALL1 ( v m 8 6 o l d )
2009-02-10 17:51:46 +03:00
2009-02-09 16:17:40 +03: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 02:35:58 +04: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 .
* /
/* fixup the stack */
2009-02-09 16:17:40 +03:00
PER_ C P U ( g d t _ p a g e , % e b x )
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 02:35:58 +04:00
mov G D T _ E N T R Y _ E S P F I X _ S S * 8 + 4 ( % e b x ) , % a l / * b i t s 1 6 . . 2 3 * /
mov G D T _ E N T R Y _ E S P F I X _ S S * 8 + 7 ( % e b x ) , % a h / * b i t s 2 4 . . 3 1 * /
shl $ 1 6 , % e a x
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 * /
2009-02-09 16:17:40 +03:00
pushl $ _ _ K E R N E L _ D S
CFI_ A D J U S T _ C F A _ O F F S E T 4
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
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 02:35:58 +04:00
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 * /
2009-02-09 16:17:40 +03:00
CFI_ A D J U S T _ C F A _ O F F S E T - 8
.endm
.macro UNWIND_ESPFIX_STACK
movl % s s , % e a x
/* see if on espfix stack */
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
/* switch to normal stack */
FIXUP_ E S P F I X _ S T A C K
27 :
.endm
2005-04-17 02:20:36 +04:00
/ *
2008-11-12 00:24:58 +03:00
* Build t h e e n t r y s t u b s a n d p o i n t e r t a b l e 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 7 s t u b s i n t o a s i n g l e 3 2 - b y t e c h u n k , w h i c h w i l l f i t i n a
* single c a c h e l i n e o n a l l m o d e r n x86 i m p l e m e n t a t i o n s .
2005-04-17 02:20:36 +04:00
* /
2008-11-12 00:03:07 +03:00
.section .init .rodata , " a"
2005-04-17 02:20:36 +04:00
ENTRY( i n t e r r u p t )
.text
2008-11-12 00:24:58 +03:00
.p2align 5
.p2align CONFIG_X86_L1_CACHE_SHIFT
2005-04-17 02:20:36 +04:00
ENTRY( i r q _ e n t r i e s _ s t a r t )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2008-11-12 00:03:07 +03:00
vector=FIRST_EXTERNAL_VECTOR
2008-11-12 00:24:58 +03:00
.rept ( NR_ V E C T O R S - F I R S T _ E X T E R N A L _ V E C T O R + 6 ) / 7
.balign 32
.rept 7
.if vector < NR_ V E C T O R S
2008-11-12 21:27:35 +03:00
.if vector < > FIRST_ E X T E R N A L _ V E C T O R
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T - 4
2008-11-12 00:24:58 +03:00
.endif
1 : 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 * /
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2008-11-12 21:27:35 +03:00
.if ( ( vector- F I R S T _ E X T E R N A L _ V E C T O R ) % 7 ) < > 6
2008-11-12 00:24:58 +03:00
jmp 2 f
.endif
.previous
2005-04-17 02:20:36 +04:00
.long 1b
2008-11-12 00:24:58 +03:00
.text
2005-04-17 02:20:36 +04:00
vector=vector + 1
2008-11-12 00:24:58 +03:00
.endif
.endr
2 : jmp c o m m o n _ i n t e r r u p t
2005-04-17 02:20:36 +04:00
.endr
2007-02-13 15:26:24 +03:00
END( i r q _ e n t r i e s _ s t a r t )
.previous
END( i n t e r r u p t )
.previous
2005-04-17 02:20:36 +04:00
2006-07-03 11:24:43 +04: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-12 00:24:58 +03:00
.p2align CONFIG_X86_L1_CACHE_SHIFT
2005-04-17 02:20:36 +04:00
common_interrupt :
2008-11-12 00:24:58 +03: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-17 02:20:36 +04:00
SAVE_ A L L
2006-07-03 11:24:43 +04:00
TRACE_ I R Q S _ O F F
2005-04-17 02:20:36 +04: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 15:26:24 +03:00
ENDPROC( c o m m o n _ i n t e r r u p t )
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2005-04-17 02:20:36 +04:00
2009-09-08 20:47:55 +04:00
/ *
* Irq e n t r i e s s h o u l d b e p r o t e c t e d a g a i n s t k p r o b e s
* /
.pushsection .kprobes .text , " ax"
2009-01-21 11:26:06 +03: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-17 02:20:36 +04:00
ENTRY( n a m e ) \
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E ; \
2006-06-27 13:53:44 +04:00
pushl $ ~ ( n r ) ; \
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4 ; \
SAVE_ A L L ; \
2006-07-03 11:24:43 +04:00
TRACE_ I R Q S _ O F F \
2005-04-17 02:20:36 +04:00
movl % e s p ,% e a x ; \
2009-01-21 11:26:06 +03:00
call f n ; \
2006-07-03 11:24:43 +04:00
jmp r e t _ f r o m _ i n t r ; \
2007-02-13 15:26:24 +03:00
CFI_ E N D P R O C ; \
ENDPROC( n a m e )
2005-04-17 02:20:36 +04:00
2009-01-21 11:26:06 +03:00
# define 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 ( n a m e , n r , s m p _ ## n a m e )
2005-04-17 02:20:36 +04:00
/* The include is where all of the SMP etc. interrupts come from */
2009-01-28 21:34:09 +03:00
# include < a s m / e n t r y _ a r c h . h >
2005-04-17 02:20:36 +04:00
ENTRY( c o p r o c e s s o r _ e r r o r )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ 0
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
pushl $ d o _ c o p r o c e s s o r _ e r r o r
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( c o p r o c e s s o r _ e r r o r )
2005-04-17 02:20:36 +04:00
ENTRY( s i m d _ c o p r o c e s s o r _ e r r o r )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ 0
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
pushl $ d o _ s i m d _ c o p r o c e s s o r _ e r r o r
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( s i m d _ c o p r o c e s s o r _ e r r o r )
2005-04-17 02:20:36 +04:00
ENTRY( d e v i c e _ n o t _ a v a i l a b l e )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ - 1 # m a r k t h i s a s a n i n t
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2008-09-09 23:56:02 +04:00
pushl $ d o _ d e v i c e _ n o t _ a v a i l a b l e
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2008-09-09 23:56:02 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( d e v i c e _ n o t _ a v a i l a b l e )
2005-04-17 02:20:36 +04:00
2006-12-07 04:14:07 +03: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-10 01:24:08 +03:00
iret
2006-12-07 04:14:07 +03:00
.section _ _ ex_ t a b l e ," a "
.align 4
2008-02-10 01:24:08 +03:00
.long native_ i r e t , i r e t _ e x c
2006-12-07 04:14:07 +03:00
.previous
2007-02-13 15:26:24 +03:00
END( n a t i v e _ i r e t )
2006-12-07 04:14:07 +03:00
2008-06-25 08:19:26 +04:00
ENTRY( n a t i v e _ i r q _ e n a b l e _ s y s e x i t )
2006-12-07 04:14:07 +03:00
sti
sysexit
2008-06-25 08:19:26 +04:00
END( n a t i v e _ i r q _ e n a b l e _ s y s e x i t )
2006-12-07 04:14:07 +03:00
# endif
2005-04-17 02:20:36 +04:00
ENTRY( o v e r f l o w )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ 0
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
pushl $ d o _ o v e r f l o w
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( o v e r f l o w )
2005-04-17 02:20:36 +04:00
ENTRY( b o u n d s )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ 0
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
pushl $ d o _ b o u n d s
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( b o u n d s )
2005-04-17 02:20:36 +04:00
ENTRY( i n v a l i d _ o p )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ 0
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
pushl $ d o _ i n v a l i d _ o p
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( i n v a l i d _ o p )
2005-04-17 02:20:36 +04: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 )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ 0
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
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
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03: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-17 02:20:36 +04:00
ENTRY( i n v a l i d _ T S S )
2006-06-26 15:57:44 +04:00
RING0 _ E C _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ d o _ i n v a l i d _ T S S
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( i n v a l i d _ T S S )
2005-04-17 02:20:36 +04:00
ENTRY( s e g m e n t _ n o t _ p r e s e n t )
2006-06-26 15:57:44 +04:00
RING0 _ E C _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ d o _ s e g m e n t _ n o t _ p r e s e n t
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( s e g m e n t _ n o t _ p r e s e n t )
2005-04-17 02:20:36 +04:00
ENTRY( s t a c k _ s e g m e n t )
2006-06-26 15:57:44 +04:00
RING0 _ E C _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ d o _ s t a c k _ s e g m e n t
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( s t a c k _ s e g m e n t )
2005-04-17 02:20:36 +04:00
ENTRY( a l i g n m e n t _ c h e c k )
2006-06-26 15:57:44 +04:00
RING0 _ E C _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ d o _ a l i g n m e n t _ c h e c k
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( a l i g n m e n t _ c h e c k )
2005-04-17 02:20:36 +04: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 12:52:34 +04:00
ENTRY( d i v i d e _ e r r o r )
RING0 _ I N T _ F R A M E
pushl $ 0 # n o e r r o r c o d e
CFI_ A D J U S T _ C F A _ O F F S E T 4
pushl $ d o _ d i v i d e _ e r r o r
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( d i v i d e _ e r r o r )
2005-04-17 02:20:36 +04: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 )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ 0
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2008-10-22 00:45:22 +04:00
pushl m a c h i n e _ c h e c k _ v e c t o r
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( m a c h i n e _ c h e c k )
2005-04-17 02:20:36 +04:00
# endif
ENTRY( s p u r i o u s _ i n t e r r u p t _ b u g )
2006-06-26 15:57:44 +04:00
RING0 _ I N T _ F R A M E
2005-04-17 02:20:36 +04:00
pushl $ 0
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
pushl $ d o _ s p u r i o u s _ i n t e r r u p t _ b u g
2006-06-26 15:57:44 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T 4
2005-04-17 02:20:36 +04:00
jmp e r r o r _ c o d e
2006-06-26 15:57:44 +04:00
CFI_ E N D P R O C
2007-02-13 15:26:24 +03:00
END( s p u r i o u s _ i n t e r r u p t _ b u g )
2009-09-08 20:47:55 +04:00
/ *
* End o f k p r o b e s s e c t i o n
* /
.popsection
2005-04-17 02:20:36 +04:00
2006-09-26 12:52:35 +04:00
ENTRY( k e r n e l _ t h r e a d _ h e l p e r )
pushl $ 0 # f a k e r e t u r n a d d r e s s f o r u n w i n d e r
CFI_ S T A R T P R O C
movl % e d x ,% e a x
push % e d x
CFI_ A D J U S T _ C F A _ O F F S E T 4
call * % e b x
push % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
call d o _ e x i t
2008-11-23 17:47:10 +03:00
ud2 # p a d d i n g f o r c a l l t r a c e
2006-09-26 12:52:35 +04:00
CFI_ E N D P R O C
ENDPROC( k e r n e l _ t h r e a d _ h e l p e 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-18 05:37:04 +04:00
# ifdef C O N F I G _ X E N
2008-03-18 02:37:17 +03: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
entrypoint 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 . * /
ENTRY( x e n _ s y s e n t e r _ t a r g e t )
RING0 _ I N T _ F R A M E
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 * /
2008-07-18 16:32:23 +04:00
CFI_ A D J U S T _ C F A _ O F F S E T - 5 * 4
2008-03-18 02:37:17 +03:00
jmp s y s e n t e r _ p a s t _ e s p
2008-07-10 22:09:20 +04:00
CFI_ E N D P R O C
2008-03-18 02:37:17 +03: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-18 05:37:04 +04:00
ENTRY( x e n _ h y p e r v i s o r _ c a l l b a c k )
CFI_ S T A R T P R O C
pushl $ 0
CFI_ A D J U S T _ C F A _ O F F S E T 4
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-18 05:37:07 +04: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
2008-03-18 02:37:22 +03:00
jmp x e n _ i r e t _ c r i t _ f i x u p
2008-03-18 02:37:17 +03:00
ENTRY( x e n _ d o _ u p c a l l )
2008-04-02 21:54:11 +04:00
1 : mov % e s p , % 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-18 05:37:04 +04:00
call x e n _ e v t c h n _ d o _ u p c a l l
jmp r e t _ f r o m _ i n t r
CFI_ E N D P R O C
ENDPROC( x e n _ h y p e r v i s o r _ c a l l b a c k )
# 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 .
ENTRY( x e n _ f a i l s a f e _ c a l l b a c k )
CFI_ S T A R T P R O C
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
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
testl % e a x ,% e a x
popl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T - 4
lea 1 6 ( % e s p ) ,% e s p
CFI_ A D J U S T _ C F A _ O F F S E T - 1 6
jz 5 f
addl $ 1 6 ,% e s p
jmp i r e t _ e x c # E A X ! = 0 = > C a t e g o r y 2 ( B a d I R E T )
5 : pushl $ 0 # E A X = = 0 = > C a t e g o r y 1 ( B a d s e g m e n t )
CFI_ A D J U S T _ C F A _ O F F S E T 4
SAVE_ A L L
jmp r e t _ f r o m _ e x c e p t i o n
CFI_ E N D P R O C
.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
.previous
.section _ _ ex_ t a b l e ," a "
.align 4
.long 1 b,6 b
.long 2 b,7 b
.long 3 b,8 b
.long 4 b,9 b
.previous
ENDPROC( x e n _ f a i l s a f e _ c a l l b a c k )
# endif / * C O N F I G _ X E N * /
2008-10-07 03: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 23:20:43 +04: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 )
2008-11-06 00:05:44 +03:00
cmpl $ 0 , f u n c t i o n _ t r a c e _ s t o p
jne f t r a c e _ s t u b
2008-05-12 23:20:43 +04: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
2008-06-21 22:17:27 +04:00
subl $ M C O U N T _ I N S N _ S I Z E , % e a x
2008-05-12 23:20:43 +04:00
.globl ftrace_call
ftrace_call :
call f t r a c e _ s t u b
popl % e d x
popl % e c x
popl % e a x
2008-11-26 08:16:24 +03: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 :
jmp f t r a c e _ s t u b
# endif
2008-05-12 23:20:43 +04:00
.globl ftrace_stub
ftrace_stub :
ret
END( f t r a c e _ c a l l e r )
# else / * ! C O N F I G _ D Y N A M I C _ F T R A C E * /
2008-05-12 23:20:42 +04:00
ENTRY( m c o u n t )
2008-11-06 00:05:44 +03:00
cmpl $ 0 , f u n c t i o n _ t r a c e _ s t o p
jne f t r a c e _ s t u b
2008-05-12 23:20:42 +04: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 23:07:04 +03: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
2008-11-26 05:10:01 +03: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
2008-11-25 23:07:04 +03:00
jnz f t r a c e _ g r a p h _ c a l l e r
2008-12-03 07:50:05 +03: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 09:03:45 +03:00
# endif
2008-05-12 23:20:42 +04:00
.globl ftrace_stub
ftrace_stub :
ret
/* taken from glibc */
trace :
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
2008-06-21 22:17:27 +04:00
subl $ M C O U N T _ I N S N _ S I Z E , % e a x
2008-05-12 23:20:42 +04:00
2008-05-12 23:20:43 +04:00
call * f t r a c e _ t r a c e _ f u n c t i o n
2008-05-12 23:20:42 +04:00
popl % e d x
popl % e c x
popl % e a x
jmp f t r a c e _ s t u b
END( m c o u n t )
2008-05-12 23:20:43 +04:00
# endif / * C O N F I G _ D Y N A M I C _ F T R A C E * /
2008-10-07 03: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 23:20:42 +04:00
2008-11-25 23:07:04 +03: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 )
2008-11-16 08:02:06 +03:00
cmpl $ 0 , f u n c t i o n _ t r a c e _ s t o p
jne f t r a c e _ s t u b
2008-11-11 09:03:45 +03:00
pushl % e a x
pushl % e c x
pushl % e d x
2008-11-13 00:49:23 +03:00
movl 0 x c ( % e s p ) , % e d x
2008-11-11 09:03:45 +03:00
lea 0 x4 ( % e b p ) , % e a x
function-graph: add stack frame test
In case gcc does something funny with the stack frames, or the return
from function code, we would like to detect that.
An arch may implement passing of a variable that is unique to the
function and can be saved on entering a function and can be tested
when exiting the function. Usually the frame pointer can be used for
this purpose.
This patch also implements this for x86. Where it passes in the stack
frame of the parent function, and will test that frame on exit.
There was a case in x86_32 with optimize for size (-Os) where, for a
few functions, gcc would align the stack frame and place a copy of the
return address into it. The function graph tracer modified the copy and
not the actual return address. On return from the funtion, it did not go
to the tracer hook, but returned to the parent. This broke the function
graph tracer, because the return of the parent (where gcc did not do
this funky manipulation) returned to the location that the child function
was suppose to. This caused strange kernel crashes.
This test detected the problem and pointed out where the issue was.
This modifies the parameters of one of the functions that the arch
specific code calls, so it includes changes to arch code to accommodate
the new prototype.
Note, I notice that the parsic arch implements its own push_return_trace.
This is now a generic function and the ftrace_push_return_trace should be
used instead. This patch does not touch that code.
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
2009-06-18 20:45:08 +04:00
movl ( % e b p ) , % e c x
2008-12-02 23:34:09 +03:00
subl $ M C O U N T _ I N S N _ S I Z E , % e d x
2008-11-11 09:03:45 +03:00
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 08:02:06 +03:00
ret
2008-11-25 23:07:04 +03:00
END( f t r a c e _ g r a p h _ c a l l e r )
2008-11-11 09:03:45 +03:00
.globl return_to_handler
return_to_handler :
pushl % e a x
pushl % e d x
function-graph: add stack frame test
In case gcc does something funny with the stack frames, or the return
from function code, we would like to detect that.
An arch may implement passing of a variable that is unique to the
function and can be saved on entering a function and can be tested
when exiting the function. Usually the frame pointer can be used for
this purpose.
This patch also implements this for x86. Where it passes in the stack
frame of the parent function, and will test that frame on exit.
There was a case in x86_32 with optimize for size (-Os) where, for a
few functions, gcc would align the stack frame and place a copy of the
return address into it. The function graph tracer modified the copy and
not the actual return address. On return from the funtion, it did not go
to the tracer hook, but returned to the parent. This broke the function
graph tracer, because the return of the parent (where gcc did not do
this funky manipulation) returned to the location that the child function
was suppose to. This caused strange kernel crashes.
This test detected the problem and pointed out where the issue was.
This modifies the parameters of one of the functions that the arch
specific code calls, so it includes changes to arch code to accommodate
the new prototype.
Note, I notice that the parsic arch implements its own push_return_trace.
This is now a generic function and the ftrace_push_return_trace should be
used instead. This patch does not touch that code.
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
2009-06-18 20:45:08 +04:00
movl % e b p , % e a x
2008-11-11 09:03:45 +03:00
call f t r a c e _ r e t u r n _ t o _ h a n d l e r
2009-10-14 00:33:50 +04:00
movl % e a x , % e c x
2008-11-11 09:03:45 +03:00
popl % e d x
popl % e a x
2009-10-14 00:33:50 +04:00
jmp * % e c x
2008-11-16 08:02:06 +03:00
# endif
2008-05-12 23:20:42 +04:00
2006-01-06 11:12:05 +03:00
.section .rodata , " a"
2007-10-11 13:12:00 +04:00
# include " s y s c a l l _ t a b l e _ 3 2 . S "
2005-04-17 02:20:36 +04:00
syscall_ t a b l e _ s i z e = ( . - s y s _ c a l l _ t a b l e )
2008-11-24 17:38:45 +03:00
/ *
* Some f u n c t i o n s s h o u l d b e p r o t e c t e d a g a i n s t k p r o b e s
* /
.pushsection .kprobes .text , " ax"
ENTRY( p a g e _ f a u l t )
RING0 _ E C _ F R A M E
pushl $ d o _ p a g e _ f a u l t
CFI_ A D J U S T _ C F A _ O F F S E T 4
ALIGN
error_code :
2009-02-09 16:17:40 +03:00
/* the function address is in %gs's slot on the stack */
pushl % f s
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET fs, 0*/
2008-11-24 17:38:45 +03:00
pushl % e s
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET es, 0*/
pushl % d s
CFI_ A D J U S T _ C F A _ O F F S E T 4
/*CFI_REL_OFFSET ds, 0*/
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e a x , 0
pushl % e b p
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e b p , 0
pushl % e d i
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e d i , 0
pushl % e s i
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e s i , 0
pushl % e d x
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e d x , 0
pushl % e c x
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e c x , 0
pushl % e b x
CFI_ A D J U S T _ C F A _ O F F S E T 4
CFI_ R E L _ O F F S E T e b x , 0
cld
movl $ ( _ _ K E R N E L _ P E R C P U ) , % e c x
movl % e c x , % f s
UNWIND_ E S P F I X _ S T A C K
2009-02-09 16:17:40 +03:00
GS_ T O _ R E G % e c x
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
2008-11-24 17:38:45 +03:00
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 16:17:40 +03:00
REG_ T O _ P T G S % e c x
SET_ K E R N E L _ G S % e c x
2008-11-24 17:38:45 +03:00
movl $ ( _ _ U S E R _ D S ) , % e c x
movl % e c x , % d s
movl % e c x , % e s
TRACE_ I R Q S _ O F F
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
CFI_ E N D P R O C
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 16:17:40 +03:00
.macro FIX_STACK offset o k l a b e l
cmpw $ _ _ K E R N E L _ C S , 4 ( % e s p )
jne \ o k
\ label :
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
CFI_ D E F _ C F A e s p , 0
CFI_ U N D E F I N E D e i p
pushfl
CFI_ A D J U S T _ C F A _ O F F S E T 4
pushl $ _ _ K E R N E L _ C S
CFI_ A D J U S T _ C F A _ O F F S E T 4
pushl $ s y s e n t e r _ p a s t _ e s p
CFI_ A D J U S T _ C F A _ O F F S E T 4
2008-11-24 17:38:45 +03:00
CFI_ R E L _ O F F S E T e i p , 0
2009-02-09 16:17:40 +03:00
.endm
2008-11-24 17:38:45 +03:00
ENTRY( d e b u g )
RING0 _ I N T _ F R A M E
cmpl $ i a32 _ s y s e n t e r _ t a r g e t ,( % e s p )
jne d e b u g _ s t a c k _ c o r r e c t
2009-02-09 16:17:40 +03: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 17:38:45 +03:00
debug_stack_correct :
pushl $ - 1 # m a r k t h i s a s a n i n t
CFI_ A D J U S T _ C F A _ O F F S E T 4
SAVE_ A L L
TRACE_ I R Q S _ O F F
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
CFI_ E N D P R O C
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 )
RING0 _ I N T _ F R A M E
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
movl % s s , % e a x
cmpw $ _ _ E S P F I X _ S S , % a x
popl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T - 4
je n m i _ e s p f i x _ s t a c k
cmpl $ i a32 _ s y s e n t e r _ t a r g e t ,( % e s p )
je n m i _ s t a c k _ f i x u p
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
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 ,
* it m i g h t n o t e x i s t .
* /
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
CFI_ A D J U S T _ C F A _ O F F S E T - 4
jae n m i _ s t a c k _ c o r r e c t
cmpl $ i a32 _ s y s e n t e r _ t a r g e t ,1 2 ( % e s p )
je n m i _ d e b u g _ s t a c k _ c h e c k
nmi_stack_correct :
/* We have a RING0_INT_FRAME here */
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
SAVE_ A L L
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
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 02:35:57 +04:00
jmp r e s t o r e _ a l l _ n o t r a c e
2008-11-24 17:38:45 +03:00
CFI_ E N D P R O C
nmi_stack_fixup :
RING0 _ I N T _ F R A M E
2009-02-09 16:17:40 +03: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
2008-11-24 17:38:45 +03:00
jmp n m i _ s t a c k _ c o r r e c t
nmi_debug_stack_check :
/* We have a RING0_INT_FRAME here */
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 16:17:40 +03: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
2008-11-24 17:38:45 +03:00
jmp n m i _ s t a c k _ c o r r e c t
nmi_espfix_stack :
/ * We h a v e a R I N G 0 _ I N T _ F R A M E h e r e .
*
* create t h e p o i n t e r t o l s s b a c k
* /
pushl % s s
CFI_ A D J U S T _ C F A _ O F F S E T 4
pushl % e s p
CFI_ A D J U S T _ C F A _ O F F S E T 4
2009-02-23 19:13:07 +03:00
addl $ 4 , ( % e s p )
2008-11-24 17:38:45 +03:00
/* copy the iret frame of 12 bytes */
.rept 3
pushl 1 6 ( % e s p )
CFI_ A D J U S T _ C F A _ O F F S E T 4
.endr
pushl % e a x
CFI_ A D J U S T _ C F A _ O F F S E T 4
SAVE_ A L L
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
RESTORE_ R E G S
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
CFI_ A D J U S T _ C F A _ O F F S E T - 2 4
jmp i r q _ r e t u r n
CFI_ E N D P R O C
END( n m i )
ENTRY( i n t 3 )
RING0 _ I N T _ F R A M E
pushl $ - 1 # m a r k t h i s a s a n i n t
CFI_ A D J U S T _ C F A _ O F F S E T 4
SAVE_ A L L
TRACE_ I R Q S _ O F F
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
CFI_ E N D P R O C
END( i n t 3 )
ENTRY( g e n e r a l _ p r o t e c t i o n )
RING0 _ E C _ F R A M E
pushl $ d o _ g e n e r a l _ p r o t e c t i o n
CFI_ A D J U S T _ C F A _ O F F S E T 4
jmp e r r o r _ c o d e
CFI_ E N D P R O C
END( g e n e r a l _ p r o t e c t i o n )
/ *
* End o f k p r o b e s s e c t i o n
* /
.popsection