Skip to contentSkip to conversation

linux/x86: use sysenter/syscall for 32-bit apps on 64-bit Xen

PATCH New Jan Beulich 1 participant 1 message Aug 8, 2007 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux/x86: use sysenter/syscall for 32-bit apps on 64-bit Xen

.. for both 32-bit apps on 64-bit kernels and 32bit kernels.

Also removing the needless re-enabling of events on x86-64's 64-bit syscall path as well as it's 32-bit int80 path (the latter accompanied by telling Xen not to disable them in the first place).

This was tested on 2.6.22.1, and only made apply to 2.6.18 without further testing.

arch/i386/kernel/entry-xen.S +20 -0
Index: head-2007-08-07/arch/i386/kernel/entry-xen.S
===================================================================
@@ -382,6 +382,26 @@ sysenter_past_esp:
 	CFI_ENDPROC
 
 
+	# pv sysenter call handler stub
+ENTRY(sysenter_entry_pv)
+	RING0_INT_FRAME
+	movl $__USER_DS,16(%esp)
+	movl %ebp,12(%esp)
+/*
+ * Load the potential sixth argument from user stack.
+ * Careful about security.
+ */
+	cmpl $__PAGE_OFFSET-3,%ebp
+	jae syscall_fault
+1:	movl (%ebp),%ebp
+.section __ex_table,"a"
+	.align 4
+	.long 1b,syscall_fault
+.previous
+	/* fall through */
+	CFI_ENDPROC
+ENDPROC(sysenter_entry_pv)
+
 	# system call handler stub
 ENTRY(system_call)
 	RING0_INT_FRAME			# can't unwind into user space anyway
arch/i386/kernel/process-xen.c +20 -1
Index: head-2007-08-07/arch/i386/kernel/process-xen.c
===================================================================
@@ -51,6 +51,7 @@
 #include <asm/math_emu.h>
 #endif
 
+#include <xen/interface/callback.h>
 #include <xen/interface/physdev.h>
 #include <xen/interface/vcpu.h>
 #include <xen/cpu_hotplug.h>
@@ -581,6 +582,22 @@ struct task_struct fastcall * __switch_t
 	mcl->args[1] = next->esp0;
 	mcl++;
 
+	if (boot_cpu_has(X86_FEATURE_SEP) && next_p->mm) {
+		/* Specify sysexit address. */
+		struct callback_register sysexit = {
+			.type = CALLBACKTYPE_sysexit,
+			.address = {
+				__USER_CS,
+				(unsigned long)task_thread_info(next_p)->sysenter_return
+			}
+		};
+
+		mcl->op      = __HYPERVISOR_callback_op;
+		mcl->args[0] = CALLBACKOP_register;
+		mcl->args[1] = (unsigned long)&sysexit;
+		mcl++;
+	}
+
 	/*
 	 * Load the per-thread Thread-Local Storage descriptor.
 	 * This is load_TLS(next, cpu) with multicalls.
@@ -616,7 +633,9 @@ struct task_struct fastcall * __switch_t
 		mcl++;
 	}
 
-	(void)HYPERVISOR_multicall(_mcl, mcl - _mcl);
+	BUG_ON(mcl > _mcl + ARRAY_SIZE(_mcl));
+	if (unlikely(HYPERVISOR_multicall_check(_mcl, mcl - _mcl, NULL)))
+		BUG();
 
 	/*
 	 * Restore %fs and %gs if needed.
arch/i386/kernel/sysenter.c +43 -14

82 lines not on this page · raw message

arch/x86_64/ia32/Makefile +6 -11
Index: head-2007-08-07/arch/x86_64/ia32/Makefile
===================================================================
@@ -14,11 +14,14 @@ obj-$(CONFIG_IA32_AOUT) += ia32_aout.o
 audit-class-$(CONFIG_AUDIT) := audit.o
 obj-$(CONFIG_IA32_EMULATION) += $(audit-class-y)
 
+syscall32-types-y := sysenter syscall
+syscall32-types-$(subst 1,$(CONFIG_XEN),$(shell expr $(CONFIG_XEN_COMPAT)0 '<' 0x0302000)) += int80
+
 $(obj)/syscall32_syscall.o: \
-	$(foreach F,sysenter syscall,$(obj)/vsyscall-$F.so)
+	$(foreach F,$(syscall32-types-y),$(obj)/vsyscall-$F.so)
 
 # Teach kbuild about targets
-targets := $(foreach F,sysenter syscall,vsyscall-$F.o vsyscall-$F.so)
+targets := $(foreach F,$(syscall32-types-y),vsyscall-$F.o vsyscall-$F.so)
 
 # The DSO images are built using a special linker script
 quiet_cmd_syscall = SYSCALL $@
@@ -27,18 +30,10 @@ quiet_cmd_syscall = SYSCALL $@
 			   -Wl,-soname=linux-gate.so.1 -o $@ \
 			   -Wl,-T,$(filter-out FORCE,$^)
 
-$(obj)/vsyscall-int80.so \
-$(obj)/vsyscall-sysenter.so $(obj)/vsyscall-syscall.so: \
+$(foreach F,$(syscall32-types-y),$(obj)/vsyscall-$F.so): \
 $(obj)/vsyscall-%.so: $(src)/vsyscall.lds $(obj)/vsyscall-%.o FORCE
 	$(call if_changed,syscall)
 
 AFLAGS_vsyscall-sysenter.o = -m32 -Wa,-32 -Iarch/i386/kernel
 AFLAGS_vsyscall-syscall.o = -m32 -Wa,-32 -Iarch/i386/kernel
-
-ifdef CONFIG_XEN
 AFLAGS_vsyscall-int80.o = -m32 -Wa,-32 -Iarch/i386/kernel
-CFLAGS_syscall32.o += -DUSE_INT80
-AFLAGS_syscall32_syscall.o += -DUSE_INT80
-
-$(obj)/syscall32_syscall.o: $(obj)/vsyscall-int80.so
-endif
arch/x86_64/ia32/ia32entry-xen.S +46 -119

235 lines not on this page · raw message

arch/x86_64/ia32/syscall32_syscall-xen.S +1 -1
Index: head-2007-08-07/arch/x86_64/ia32/syscall32_syscall-xen.S
===================================================================
@@ -2,7 +2,7 @@
 
 	.section ".init.data","aw"
 
-#ifdef USE_INT80
+#if CONFIG_XEN_COMPAT < 0x030200
 
 	.globl syscall32_int80
 	.globl syscall32_int80_end
arch/x86_64/ia32/syscall32-xen.c +36 -18

89 lines not on this page · raw message

arch/x86_64/kernel/entry-xen.S +1 -6
Index: head-2007-08-07/arch/x86_64/kernel/entry-xen.S
===================================================================
@@ -244,7 +244,7 @@ END(ret_from_fork)
  * r11  eflags for syscall/sysret, temporary for C
  * r12-r15,rbp,rbx saved by C code, not touched. 		
  * 
- * Interrupts are off on entry.
+ * Interrupts are enabled on entry.
  * Only called from user space.
  *
  * XXX	if we had a free scratch register we could save the RSP into the stack frame
@@ -259,11 +259,6 @@ ENTRY(system_call)
 	_frame (RIP-0x10)
 	SAVE_ARGS -8,0
 	movq  %rax,ORIG_RAX-ARGOFFSET(%rsp) 
-	/*
-	 * No need to follow this irqs off/on section - it's straight
-	 * and short:
-	 */
-        XEN_UNBLOCK_EVENTS(%r11)        
 	GET_THREAD_INFO(%rcx)
 	testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SECCOMP),threadinfo_flags(%rcx)
 	CFI_REMEMBER_STATE
arch/x86_64/kernel/process-xen.c +4 -1
Index: head-2007-08-07/arch/x86_64/kernel/process-xen.c
===================================================================
@@ -584,7 +584,10 @@ __switch_to(struct task_struct *prev_p, 
 		mcl++;
 	}
 
-	(void)HYPERVISOR_multicall(_mcl, mcl - _mcl);
+	BUG_ON(mcl > _mcl + ARRAY_SIZE(_mcl));
+	if (unlikely(HYPERVISOR_multicall_check(_mcl, mcl - _mcl, NULL)))
+		BUG();
+
 	/* 
 	 * Switch DS and ES.
 	 * This won't pick up thread selector changes, but I guess that is ok.
arch/x86_64/kernel/setup64-xen.c +23 -10
Index: head-2007-08-07/arch/x86_64/kernel/setup64-xen.c
===================================================================
@@ -29,6 +29,7 @@
 #include <asm/sections.h>
 #ifdef CONFIG_XEN
 #include <asm/hypervisor.h>
+#include <xen/interface/callback.h>
 #endif
 
 char x86_boot_params[BOOT_PARAM_SIZE] __initdata = {0,};
@@ -211,21 +212,33 @@
 /* May not be marked __init: used by software suspend */
 void syscall_init(void)
 {
-#ifndef CONFIG_XEN
-	/* 
-	 * LSTAR and STAR live in a bit strange symbiosis.
-	 * They both write to the same internal register. STAR allows to set CS/DS
-	 * but only a 32bit target. LSTAR sets the 64bit rip. 	 
-	 */ 
-	wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32); 
-	wrmsrl(MSR_LSTAR, system_call); 
+	static struct callback_register lstar = {
+		.type = CALLBACKTYPE_syscall,
+		.address = (unsigned long)system_call
+	};
+	static struct callback_register sfmask = {
+		.type = CALLBACKTYPE_sfmask,
+		.address = EF_TF|EF_DF
+	};
 
-	/* Flags to clear on syscall */
-	wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE|0x3000); 
+#if CONFIG_XEN_COMPAT < 0x030200
+	/*
+	 * Can't check SYSCALL flag here, as it is always on; SEP indicates
+	 * availability of the new callback types.
+	 */
+	if (!boot_cpu_has(X86_FEATURE_SEP))
+		return;
 #endif
+	if (HYPERVISOR_callback_op(CALLBACKOP_register, &lstar) < 0)
+		BUG();
+
 #ifdef CONFIG_IA32_EMULATION   		
 	syscall32_cpu_init ();
 #endif
+
+	/* Flags to clear on syscall */
+	if (HYPERVISOR_callback_op(CALLBACKOP_register, &sfmask) < 0)
+		BUG();
 }
 
 void __cpuinit check_efer(void)
arch/x86_64/kernel/traps-xen.c +1 -1
Index: head-2007-08-07/arch/x86_64/kernel/traps-xen.c
===================================================================
@@ -1107,7 +1107,7 @@ static trap_info_t trap_table[] = {
 #endif
         { 19, 0|4, __KERNEL_CS, (unsigned long)simd_coprocessor_error     },
 #ifdef CONFIG_IA32_EMULATION
-	{ IA32_SYSCALL_VECTOR, 3|4, __KERNEL_CS, (unsigned long)ia32_syscall},
+	{ IA32_SYSCALL_VECTOR, 3, __KERNEL_CS, (unsigned long)ia32_syscall},
 #endif
         {  0, 0,           0, 0                                              }
 };
drivers/xen/Kconfig +5 -1
Index: head-2007-08-07/drivers/xen/Kconfig
===================================================================
@@ -13,7 +13,7 @@ config XEN
 if XEN
 config XEN_INTERFACE_VERSION
 	hex
-	default 0x00030205
+	default 0x00030206
 
 menu "XEN"
 
@@ -237,6 +237,9 @@ choice
 	config XEN_COMPAT_030004_AND_LATER
 		bool "3.0.4 and later"
 
+	config XEN_COMPAT_030100_AND_LATER
+		bool "3.1.0 and later"
+
 	config XEN_COMPAT_LATEST_ONLY
 		bool "no compatibility code"
 
@@ -245,6 +248,7 @@ endchoice
 config XEN_COMPAT
 	hex
 	default 0xffffff if XEN_COMPAT_LATEST_ONLY
+	default 0x030100 if XEN_COMPAT_030100_AND_LATER
 	default 0x030004 if XEN_COMPAT_030004_AND_LATER
 	default 0x030002 if XEN_COMPAT_030002_AND_LATER
 	default 0
drivers/xen/core/smpboot.c +1 -0
Index: head-2007-08-07/drivers/xen/core/smpboot.c
===================================================================
@@ -172,6 +172,7 @@ static void xen_smp_intr_exit(unsigned i
 void cpu_bringup(void)
 {
 	cpu_init();
+	identify_cpu(cpu_data + smp_processor_id());
 	touch_softlockup_watchdog();
 	preempt_disable();
 	local_irq_enable();
include/asm-x86_64/mach-xen/setup_arch_post.h +4 -0
Index: head-2007-08-07/include/asm-x86_64/mach-xen/setup_arch_post.h
===================================================================
@@ -23,10 +23,12 @@ static void __init machine_specific_arch
 		.type = CALLBACKTYPE_failsafe,
 		.address = (unsigned long)failsafe_callback,
 	};
+#if CONFIG_XEN_COMPAT < 0x030200
 	static struct callback_register __initdata syscall = {
 		.type = CALLBACKTYPE_syscall,
 		.address = (unsigned long)system_call,
 	};
+#endif
 #ifdef CONFIG_X86_LOCAL_APIC
 	static struct callback_register __initdata nmi_cb = {
 		.type = CALLBACKTYPE_nmi,
@@ -35,8 +37,10 @@ static void __init machine_specific_arch
 	ret = HYPERVISOR_callback_op(CALLBACKOP_register, &event);
 	if (ret == 0)
 		ret = HYPERVISOR_callback_op(CALLBACKOP_register, &failsafe);
+#if CONFIG_XEN_COMPAT < 0x030200
 	if (ret == 0)
 		ret = HYPERVISOR_callback_op(CALLBACKOP_register, &syscall);
+#endif
 #if CONFIG_XEN_COMPAT <= 0x030002
 	if (ret == -ENOSYS)
 		ret = HYPERVISOR_set_callbacks(
include/xen/interface/callback.h +25 -4
Index: head-2007-08-07/include/xen/interface/callback.h
===================================================================
@@ -38,13 +38,34 @@
 
 #define CALLBACKTYPE_event                 0
 #define CALLBACKTYPE_failsafe              1
-#define CALLBACKTYPE_syscall               2 /* x86_64 only */
+#define CALLBACKTYPE_syscall               2 /* x86_64 hv only */
 /*
- * sysenter is only available on x86_32 with the
- * supervisor_mode_kernel option enabled.
+ * sysenter_deprecated is only available on x86_32 with the
+ * supervisor_mode_kernel option enabled, and should not be used in new code.
  */
-#define CALLBACKTYPE_sysenter              3
+#define CALLBACKTYPE_sysenter_deprecated   3
 #define CALLBACKTYPE_nmi                   4
+#if __XEN_INTERFACE_VERSION__ < 0x00030206
+#define CALLBACKTYPE_sysenter              CALLBACKTYPE_sysenter_deprecated
+#else
+/*
+ * sysenter is only available
+ * - on x86_32 with the supervisor_mode_kernel option enabled,
+ * - on x86_64 hv for x86_32 pv or 32-bit guest support in x86_64 pv.
+ */
+#define CALLBACKTYPE_sysenter              5
+/*
+ * sysexit is only available on x86_64 hv, and is only used to fill a
+ * sysenter frame's return address (if the guest desires to have a non-NULL
+ * value there). Additionally, since CALLBACKF_mask_events is meaningless
+ * here, it is being (mis-)used for 64-bits guests to distinguish sysenter
+ * callers expected to be in 64-bit mode (flag set) from 32-bit ones (flag
+ * clear).
+ */
+#define CALLBACKTYPE_sysexit               6
+#define CALLBACKTYPE_syscall32             7 /* x86_64 only */
+#define CALLBACKTYPE_sfmask                8 /* x86_64 only */
+#endif
 
 /*
  * Disable event deliver during callback? This flag is ignored for event and
include/xen/interface/xen-compat.h +1 -1
Index: head-2007-08-07/include/xen/interface/xen-compat.h
===================================================================
@@ -27,7 +27,7 @@
 #ifndef __XEN_PUBLIC_XEN_COMPAT_H__
 #define __XEN_PUBLIC_XEN_COMPAT_H__
 
-#define __XEN_LATEST_INTERFACE_VERSION__ 0x00030205
+#define __XEN_LATEST_INTERFACE_VERSION__ 0x00030206
 
 #if defined(__XEN__) || defined(__XEN_TOOLS__)
 /* Xen is built with matching headers and implements the latest interface. */