[PATCH 32 of 36] Add sysret/sysexit pvops for returning to 32-bit compatibility userspace

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Ingo Molnar <mingo@...>
Cc: LKML <linux-kernel@...>, <x86@...>, xen-devel <xen-devel@...>, Stephen Tweedie <sct@...>, Eduardo Habkost <ehabkost@...>, Mark McLoughlin <markmc@...>
Date: Wednesday, June 25, 2008 - 12:19 am

In a 64-bit system, we need separate sysret/sysexit operations to
return to a 32-bit userspace.

Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citirx.com>
---
 arch/x86/ia32/ia32entry.S           |   21 +++++++++---
 arch/x86/kernel/asm-offsets_64.c    |    4 +-
 arch/x86/kernel/entry_64.S          |    4 +-
 arch/x86/kernel/paravirt.c          |   12 ++++---
 arch/x86/kernel/paravirt_patch_64.c |    9 +++--
 include/asm-x86/irqflags.h          |   14 ++++++--
 include/asm-x86/paravirt.h          |   58 ++++++++++++++++++++++++++++-------
 7 files changed, 91 insertions(+), 31 deletions(-)

diff --git a/arch/x86/ia32/ia32entry.S b/arch/x86/ia32/ia32entry.S
--- a/arch/x86/ia32/ia32entry.S
+++ b/arch/x86/ia32/ia32entry.S
@@ -60,6 +60,19 @@
 	CFI_UNDEFINED	r14
 	CFI_UNDEFINED	r15
 	.endm
+
+#ifdef CONFIG_PARAVIRT
+ENTRY(native_usergs_sysret32)
+	swapgs
+	sysretl
+ENDPROC(native_usergs_sysret32)
+
+ENTRY(native_irq_enable_sysexit)
+	swapgs
+	sti
+	sysexit
+ENDPROC(native_irq_enable_sysexit)
+#endif
 
 /*
  * 32bit SYSENTER instruction entry.
@@ -151,10 +164,7 @@
 	CFI_ADJUST_CFA_OFFSET -8
 	CFI_REGISTER rsp,rcx
 	TRACE_IRQS_ON
-	swapgs
-	sti		/* sti only takes effect after the next instruction */
-	/* sysexit */
-	.byte	0xf, 0x35
+	ENABLE_INTERRUPTS_SYSEXIT32
 
 sysenter_tracesys:
 	CFI_RESTORE_STATE
@@ -254,8 +264,7 @@
 	TRACE_IRQS_ON
 	movl RSP-ARGOFFSET(%rsp),%esp
 	CFI_RESTORE rsp
-	swapgs
-	sysretl
+	USERGS_SYSRET32
 	
 cstar_tracesys:	
 	CFI_RESTORE_STATE
diff --git a/arch/x86/kernel/asm-offsets_64.c b/arch/x86/kernel/asm-offsets_64.c
--- a/arch/x86/kernel/asm-offsets_64.c
+++ b/arch/x86/kernel/asm-offsets_64.c
@@ -63,7 +63,9 @@
 	OFFSET(PV_IRQ_irq_enable, pv_irq_ops, irq_enable);
 	OFFSET(PV_CPU_iret, pv_cpu_ops, iret);
 	OFFSET(PV_CPU_nmi_return, pv_cpu_ops, nmi_return);
-	OFFSET(PV_CPU_usergs_sysret, pv_cpu_ops, usergs_sysret);
+	OFFSET(PV_CPU_usergs_sysret32, pv_cpu_ops, usergs_sysret32);
+	OFFSET(PV_CPU_usergs_sysret64, pv_cpu_ops, usergs_sysret64);
+	OFFSET(PV_CPU_irq_enable_sysexit, pv_cpu_ops, irq_enable_sysexit);
 	OFFSET(PV_CPU_swapgs, pv_cpu_ops, swapgs);
 	OFFSET(PV_MMU_read_cr2, pv_mmu_ops, read_cr2);
 #endif
diff --git a/arch/x86/kernel/entry_64.S b/arch/x86/kernel/entry_64.S
--- a/arch/x86/kernel/entry_64.S
+++ b/arch/x86/kernel/entry_64.S
@@ -167,7 +167,7 @@
 #endif	
 
 #ifdef CONFIG_PARAVIRT
-ENTRY(native_usergs_sysret)
+ENTRY(native_usergs_sysret64)
 	swapgs
 	sysretq
 #endif /* CONFIG_PARAVIRT */
@@ -383,7 +383,7 @@
 	RESTORE_ARGS 0,-ARG_SKIP,1
 	/*CFI_REGISTER	rflags,r11*/
 	movq	%gs:pda_oldrsp, %rsp
-	USERGS_SYSRET
+	USERGS_SYSRET64
 
 	CFI_RESTORE_STATE
 	/* Handle reschedules */
diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
--- a/arch/x86/kernel/paravirt.c
+++ b/arch/x86/kernel/paravirt.c
@@ -142,7 +142,8 @@
 	else if (type == PARAVIRT_PATCH(pv_cpu_ops.iret) ||
 		 type == PARAVIRT_PATCH(pv_cpu_ops.nmi_return) ||
 		 type == PARAVIRT_PATCH(pv_cpu_ops.irq_enable_sysexit) ||
-		 type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret))
+		 type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret32) ||
+		 type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret64))
 		/* If operation requires a jmp, then jmp */
 		ret = paravirt_patch_jmp(insnbuf, opfunc, addr, len);
 	else
@@ -195,7 +196,8 @@
 extern void native_iret(void);
 extern void native_nmi_return(void);
 extern void native_irq_enable_sysexit(void);
-extern void native_usergs_sysret(void);
+extern void native_usergs_sysret32(void);
+extern void native_usergs_sysret64(void);
 
 static int __init print_banner(void)
 {
@@ -331,10 +333,10 @@
 	.write_idt_entry = native_write_idt_entry,
 	.load_sp0 = native_load_sp0,
 
-#ifdef CONFIG_X86_32
 	.irq_enable_sysexit = native_irq_enable_sysexit,
-#else
-	.usergs_sysret = native_usergs_sysret,
+#ifdef CONFIG_X86_64
+	.usergs_sysret32 = native_usergs_sysret32,
+	.usergs_sysret64 = native_usergs_sysret64,
 #endif
 	.iret = native_iret,
 	.nmi_return = native_nmi_return,
diff --git a/arch/x86/kernel/paravirt_patch_64.c b/arch/x86/kernel/paravirt_patch_64.c
--- a/arch/x86/kernel/paravirt_patch_64.c
+++ b/arch/x86/kernel/paravirt_patch_64.c
@@ -17,8 +17,9 @@
 DEF_NATIVE(pv_cpu_ops, clts, "clts");
 DEF_NATIVE(pv_cpu_ops, wbinvd, "wbinvd");
 
-/* the three commands give us more control to how to return from a syscall */
-DEF_NATIVE(pv_cpu_ops, usergs_sysret, "swapgs; sysretq;");
+DEF_NATIVE(pv_cpu_ops, irq_enable_sysexit, "swapgs; sti; sysexit");
+DEF_NATIVE(pv_cpu_ops, usergs_sysret64, "swapgs; sysretq");
+DEF_NATIVE(pv_cpu_ops, usergs_sysret32, "swapgs; sysretl");
 DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs");
 
 unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
@@ -39,7 +40,9 @@
 		PATCH_SITE(pv_irq_ops, irq_disable);
 		PATCH_SITE(pv_cpu_ops, iret);
 		PATCH_SITE(pv_cpu_ops, nmi_return);
-		PATCH_SITE(pv_cpu_ops, usergs_sysret);
+		PATCH_SITE(pv_cpu_ops, irq_enable_sysexit);
+		PATCH_SITE(pv_cpu_ops, usergs_sysret32);
+		PATCH_SITE(pv_cpu_ops, usergs_sysret64);
 		PATCH_SITE(pv_cpu_ops, swapgs);
 		PATCH_SITE(pv_mmu_ops, read_cr2);
 		PATCH_SITE(pv_mmu_ops, read_cr3);
diff --git a/include/asm-x86/irqflags.h b/include/asm-x86/irqflags.h
--- a/include/asm-x86/irqflags.h
+++ b/include/asm-x86/irqflags.h
@@ -168,9 +168,17 @@
 
 #ifdef CONFIG_X86_64
 #define INTERRUPT_RETURN	iretq
-#define USERGS_SYSRET					\
-			swapgs;				\
-			sysretq;
+#define USERGS_SYSRET64				\
+	swapgs;					\
+	sysretq;
+#define USERGS_SYSRET32				\
+	swapgs;					\
+	sysretl
+#define ENABLE_INTERRUPTS_SYSEXIT32		\
+	swapgs;					\
+	sti;					\
+	sysexit
+
 #else
 #define INTERRUPT_RETURN		iret
 #define ENABLE_INTERRUPTS_SYSEXIT	sti; sysexit
diff --git a/include/asm-x86/paravirt.h b/include/asm-x86/paravirt.h
--- a/include/asm-x86/paravirt.h
+++ b/include/asm-x86/paravirt.h
@@ -141,10 +141,35 @@
 	u64 (*read_pmc)(int counter);
 	unsigned long long (*read_tscp)(unsigned int *aux);
 
-	/* These ones are jmp'ed to, not actually called. */
+	/*
+	 * Atomically enable interrupts and return to userspace.  This
+	 * is only ever used to return to 32-bit processes; in a
+	 * 64-bit kernel, it's used for 32-on-64 compat processes, but
+	 * never native 64-bit processes.  (Jump, not call.)
+	 */
 	void (*irq_enable_sysexit)(void);
-	void (*usergs_sysret)(void);
+
+	/*
+	 * Switch to usermode gs and return to 64-bit usermode using
+	 * sysret.  Only used in 64-bit kernels to return to 64-bit
+	 * processes.  Usermode register state, including %rsp, must
+	 * already be restored.
+	 */
+	void (*usergs_sysret64)(void);
+
+	/*
+	 * Switch to usermode gs and return to 32-bit usermode using
+	 * sysret.  Used to return to 32-on-64 compat processes.
+	 * Other usermode register state, including %esp, must already
+	 * be restored.
+	 */
+	void (*usergs_sysret32)(void);
+
+	/* Normal iret.  Jump to this with the standard iret stack
+	   frame set up. */
 	void (*iret)(void);
+
+	/* Return from NMI. (?) */
 	void (*nmi_return)(void);
 
 	void (*swapgs)(void);
@@ -1486,18 +1511,24 @@
 		  call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_enable);	\
 		  PV_RESTORE_REGS;)
 
-#define ENABLE_INTERRUPTS_SYSEXIT					\
-	PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit),	\
+#define USERGS_SYSRET32							\
+	PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret32),	\
 		  CLBR_NONE,						\
-		  jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
-
+		  jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret32))
 
 #ifdef CONFIG_X86_32
 #define GET_CR0_INTO_EAX				\
 	push %ecx; push %edx;				\
 	call PARA_INDIRECT(pv_cpu_ops+PV_CPU_read_cr0);	\
 	pop %edx; pop %ecx
-#else
+
+#define ENABLE_INTERRUPTS_SYSEXIT					\
+	PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit),	\
+		  CLBR_NONE,						\
+		  jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
+
+
+#else	/* !CONFIG_X86_32 */
 #define SWAPGS								\
 	PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE,	\
 		  PV_SAVE_REGS;						\
@@ -1510,11 +1541,16 @@
 	movq %rax, %rcx;				\
 	xorq %rax, %rax;
 
-#define USERGS_SYSRET							\
-	PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret),		\
+#define USERGS_SYSRET64							\
+	PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret64),	\
 		  CLBR_NONE,						\
-		  jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret))
-#endif
+		  jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret64))
+
+#define ENABLE_INTERRUPTS_SYSEXIT32					\
+	PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit),	\
+		  CLBR_NONE,						\
+		  jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
+#endif	/* CONFIG_X86_32 */
 
 #endif /* __ASSEMBLY__ */
 #endif /* CONFIG_PARAVIRT */


--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 00 of 36] x86/paravirt: groundwork for 64-bit Xen sup..., Jeremy Fitzhardinge, (Wed Jun 25, 12:18 am)
Re: [PATCH 00 of 36] x86/paravirt: groundwork for 64-bit Xen..., Jeremy Fitzhardinge, (Wed Jun 25, 4:03 pm)
Re: [PATCH 00 of 36] x86/paravirt: groundwork for 64-bit Xen..., Jeremy Fitzhardinge, (Wed Jun 25, 4:12 pm)
Re: [PATCH 00 of 36] x86/paravirt: groundwork for 64-bit Xen..., Jeremy Fitzhardinge, (Thu Jun 26, 3:02 pm)
Re: [PATCH 00 of 36] x86/paravirt: groundwork for 64-bit Xen..., Jeremy Fitzhardinge, (Thu Jun 26, 2:25 pm)
Re: [PATCH 00 of 36] x86/paravirt: groundwork for 64-bit Xen..., Jeremy Fitzhardinge, (Thu Jun 26, 10:28 am)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Thu Jun 26, 10:34 am)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Fri Jun 27, 3:04 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Sun Jun 29, 11:02 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Mon Jun 30, 7:04 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Tue Jul 1, 12:14 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Thu Jul 3, 2:25 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Thu Jul 3, 2:41 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Thu Jul 3, 11:47 am)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Tue Jul 1, 12:10 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Mon Jun 30, 1:57 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Mon Jun 30, 1:17 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Mon Jun 30, 2:36 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Mon Jun 30, 1:32 am)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Fri Jun 27, 12:02 pm)
Re: [Xen-devel] Re: [PATCH 00 of 36] x86/paravirt: groundwor..., Jeremy Fitzhardinge, (Fri Jun 27, 12:25 pm)
Re: [PATCH 00 of 36] x86/paravirt: groundwork for 64-bit Xen..., Jeremy Fitzhardinge, (Wed Jun 25, 7:46 am)
[PATCH 36 of 36] x86_64/paravirt: Make load_gs_index() a par..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
Re: [PATCH 36 of 36] x86_64/paravirt: Make load_gs_index() a..., Jeremy Fitzhardinge, (Wed Jun 25, 7:48 am)
[PATCH 26 of 36] x86_64: Split set_pte_vaddr(), Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 35 of 36] x86_64/paravirt: add adjust_exception_frame, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 04 of 36] x86: remove open-coded save/load segment op..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 25 of 36] x86_64: PSE no longer a hard requirement, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 18 of 36] x86/paravirt: add debugging for missing ope..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 01 of 36] x86: asm-x86/pgtable.h: fix compiler warning, Jeremy Fitzhardinge, (Wed Jun 25, 12:18 am)
[PATCH 32 of 36] Add sysret/sysexit pvops for returning to 3..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 31 of 36] x86_64 pvops: don't restore user rsp within..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 06 of 36] x86_64: use p??_populate() to attach pages ..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 07 of 36] x86_64: unify early_ioremap, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 19 of 36] paravirt_ops: define PARA_INDIRECT for indi..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 28 of 36] Save %fs and %gs before load_TLS() and arch..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 30 of 36] x86/paravirt_ops: split sysret and sysexit, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 21 of 36] x86-64: add FIX_PARAVIRT_BOOTMAP fixmap slot, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 20 of 36] paravirt/x86_64: move __PAGE_OFFSET to leav..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 34 of 36] x86_64: swapgs pvop with a user-stack can n..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 05 of 36] x86_64: use write_gdt_entry in vsyscall_set..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 33 of 36] x86_64: ia32entry: replace privileged instr..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 27 of 36] x86_64: __switch_to(): Move arch_leave_lazy..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 29 of 36] Use __KERNEL_DS as SS when returning to a k..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 24 of 36] x86_64: create small vmemmap mappings if PS..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 10 of 36] x86: unify pgd_index, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 11 of 36] x86: unify mmu_context.h, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 08 of 36] x86_64: Add gate_offset() and gate_segment(..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 09 of 36] x86_64: Use __pgd() on mk_kernel_pgd(), Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 23 of 36] x86_64: adjust mapping of physical pagetabl..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 14 of 36] x86_64: add sync_cmpxchg, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 03 of 36] x86: add memory barriers to wrmsr, Jeremy Fitzhardinge, (Wed Jun 25, 12:18 am)
Re: [PATCH 03 of 36] x86: add memory barriers to wrmsr, Arjan van de Ven, (Wed Jun 25, 12:44 am)
Re: [PATCH 03 of 36] x86: add memory barriers to wrmsr, Jeremy Fitzhardinge, (Wed Jun 25, 5:08 pm)
Re: [PATCH 03 of 36] x86: add memory barriers to wrmsr, Arjan van de Ven, (Wed Jun 25, 6:31 pm)
Re: [PATCH 03 of 36] x86: add memory barriers to wrmsr, H. Peter Anvin, (Wed Jun 25, 7:18 pm)
Re: [PATCH 03 of 36] x86: add memory barriers to wrmsr, Jeremy Fitzhardinge, (Wed Jun 25, 7:37 pm)
Re: [PATCH 03 of 36] x86: add memory barriers to wrmsr, H. Peter Anvin, (Wed Jun 25, 7:42 pm)
Re: [PATCH 03 of 36] x86: add memory barriers to wrmsr, Jeremy Fitzhardinge, (Wed Jun 25, 7:05 pm)
[PATCH 13 of 36] x86_64: add prototype for x86_64_start_kern..., Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 12 of 36] x86_64: replace end_pfn with num_physpages, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 17 of 36] x86: preallocate and prepopulate separately, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 15 of 36] x86: simplify vmalloc_sync_all, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 22 of 36] x86_64: split x86_64_start_kernel, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 16 of 36] x86/paravirt: add a pgd_alloc/free hooks, Jeremy Fitzhardinge, (Wed Jun 25, 12:19 am)
[PATCH 02 of 36] x86: add memory clobber to save/loadsegment, Jeremy Fitzhardinge, (Wed Jun 25, 12:18 am)