Skip to contentSkip to conversation

32-on-64 sysenter for pvops

PATCH Jeremy Fitzhardinge 3 participants 4 messages Mar 4, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

I implemented sysenter for 32-on-64, since it seemed straightforward enough. It mostly works, but every now and again I get vcpus just hanging in blocked state, as if events are being lost or ignored. Its very similar to the symptoms that other people have reported against the pvops kernel, which I have not managed to reproduce. Perhaps using sysenter is exacerbating an existing bug...

Anyway, a couple of questions. It seems that the stack frame that Xen's sysenter generates is not exactly the same as the one the kernel expects, so the direct access to the threadinfo structure doesn't work properly. What's the difference in the frames?

I guess the other reason for the separate PV Xen sysenter entrypoint is to deal with sysexit not working. I addressed this by implementing a sysexit pvop using iret, though I think I could just set the TIF_IRET flag in threadinfo.

Anyway, could you look at these changes and see if anything problematic leaps out.

I'm also having debugging it, since xenctx and gdbserver-xen don't work on 32-bit compat domains, and the console itself seems to locked up. I'm not sure how I can get any state out of the vcpus; even an eip would help.

Thanks,
J

Subject: xen: add support for callbackops hypercall

---
 include/asm-x86/xen/hypercall.h  |    6 ++
 include/asm-x86/xen/interface.h  |    4 +
 include/xen/interface/callback.h |  102 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)

===================================================================

include/asm-x86/xen/hypercall.h +6 -0
@@ -161,6 +161,12 @@
 	return _hypercall4(int, set_callbacks,
 			   event_selector, event_address,
 			   failsafe_selector, failsafe_address);
+}
+
+static inline int
+HYPERVISOR_callback_op(int cmd, void *arg)
+{
+	return _hypercall2(int, callback_op, cmd, arg);
 }
 
 static inline int

===================================================================

include/asm-x86/xen/interface.h +4 -0
@@ -171,6 +171,10 @@
     unsigned long pad[5]; /* sizeof(struct vcpu_info) == 64 */
 };
 
+struct xen_callback {
+	unsigned long cs;
+	unsigned long eip;
+};
 #endif /* !__ASSEMBLY__ */
 
 /*

===================================================================

include/xen/interface/callback.h new file +102 -0

102 lines not on this page · raw message

Subject: x86: only enable interrupts when kernel state has been set up

The sysenter path tries to enable interrupts immediately. Unfortunately this doesn't work in a paravirt environment, because not enough kernel state has been set up at that point (namely, pointing %fs to the kernel percpu data segment). To fix this, defer ENABLE_INTERRUPTS until after the kernel state has been set up.

Unfortunately this means that we're running with interrupts disabled for a while without calling the IRQ tracing code, but that can't be called without setting up %fs either.

---
 arch/x86/kernel/entry_32.S |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

===================================================================

arch/x86/kernel/entry_32.S +5 -3
@@ -291,10 +291,10 @@ ENTRY(ia32_sysenter_target)
 	movl TSS_sysenter_sp0(%esp),%esp
 ENTRY(sysenter_past_esp)
 	/*
-	 * No need to follow this irqs on/off section: the syscall
-	 * disabled irqs and here we enable it straight after entry:
+	 * Interrupts are disabled here, but we can't trace it until
+	 * enough kernel state to call TRACE_IRQS_OFF can be called - but
+	 * we immediately enable interrupts at that point anyway.
 	 */
-	ENABLE_INTERRUPTS(CLBR_NONE)
 	pushl $(__USER_DS)
 	CFI_ADJUST_CFA_OFFSET 4
 	/*CFI_REL_OFFSET ss, 0*/
@@ -330,6 +330,7 @@ 1:	movl (%ebp),%ebp
 	pushl %eax
 	CFI_ADJUST_CFA_OFFSET 4
 	SAVE_ALL
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	GET_THREAD_INFO(%ebp)
 
 	/* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
@@ -546,6 +547,7 @@ syscall_fault:
 	pushl %eax			# save orig_eax
 	CFI_ADJUST_CFA_OFFSET 4
 	SAVE_ALL
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	GET_THREAD_INFO(%ebp)
 	movl $-EFAULT,PT_EAX(%esp)
 	jmp resume_userspace

Subject: xen: support sysenter/sysexit if hypervisor does

64-bit Xen supports sysenter for 32-bit guests, so support its use. (sysenter is faster than int $0x80 in 32-on-64.)

sysexit is still not supported, so we fake it up using iret.

---
 arch/x86/kernel/entry_32.S |   22 +++++++++++++++++-
 arch/x86/xen/enlighten.c   |    3 --
 arch/x86/xen/setup.c       |   21 +++++++++++++++++
 arch/x86/xen/smp.c         |    1 
 arch/x86/xen/xen-asm.S     |   52 ++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/xen/xen-ops.h     |    3 ++
 6 files changed, 99 insertions(+), 3 deletions(-)

===================================================================

arch/x86/kernel/entry_32.S +21 -1
@@ -311,6 +311,7 @@ ENTRY(sysenter_past_esp)
 	 * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
 	 * pushed above; +8 corresponds to copy_thread's esp0 setting.
 	 */
+sysenter_stack_setup:
 	pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
 	CFI_ADJUST_CFA_OFFSET 4
 	CFI_REL_OFFSET eip, 0
@@ -1025,6 +1026,16 @@ ENDPROC(kernel_thread_helper)
 ENDPROC(kernel_thread_helper)
 
 #ifdef CONFIG_XEN
+/* Xen doesn't set %esp to be precisely what the normal sysenter
+   entrypoint expects, so fix it up before using the normal path. */
+ENTRY(xen_sysenter_target)
+	RING0_INT_FRAME
+	movl $__USER_DS,16(%esp)
+	movl %ebp,12(%esp)
+	movl $__USER_CS,4(%esp)
+	addl $4,%esp
+	jmp sysenter_stack_setup
+
 ENTRY(xen_hypervisor_callback)
 	CFI_STARTPROC
 	pushl $0
@@ -1044,8 +1055,17 @@ ENTRY(xen_hypervisor_callback)
 	jae  1f
 
 	call xen_iret_crit_fixup
+	jmp  2f
 
-1:	mov %esp, %eax
+1:	cmpl $xen_sysexit_start_crit,%eax
+	jb   2f
+	cmpl $xen_sysexit_end_crit,%eax
+	jae  2f
+
+	jmp xen_sysexit_crit_fixup
+
+ENTRY(xen_do_upcall)
+2:	mov %esp, %eax
 	call xen_evtchn_do_upcall
 	jmp  ret_from_intr
 	CFI_ENDPROC

===================================================================

arch/x86/xen/enlighten.c +1 -2
@@ -153,7 +153,6 @@ static void xen_cpuid(unsigned int *ax, 
 	if (*ax == 1)
 		maskedx = ~((1 << X86_FEATURE_APIC) |  /* disable APIC */
 			    (1 << X86_FEATURE_ACPI) |  /* disable ACPI */
-			    (1 << X86_FEATURE_SEP)  |  /* disable SEP */
 			    (1 << X86_FEATURE_ACC));   /* thermal monitoring */
 
 	asm(XEN_EMULATE_PREFIX "cpuid"
@@ -969,7 +968,7 @@ static const struct pv_cpu_ops xen_cpu_o
 	.read_pmc = native_read_pmc,
 
 	.iret = xen_iret,
-	.irq_enable_syscall_ret = NULL,  /* never called */
+	.irq_enable_syscall_ret = xen_sysexit,
 
 	.load_tr_desc = paravirt_nop,
 	.set_ldt = xen_set_ldt,

===================================================================

arch/x86/xen/setup.c +21 -0
@@ -16,6 +16,7 @@
 #include <asm/xen/hypervisor.h>
 #include <asm/xen/hypercall.h>
 
+#include <xen/interface/callback.h>
 #include <xen/interface/physdev.h>
 #include <xen/features.h>
 
@@ -67,6 +68,24 @@ static void __init fiddle_vdso(void)
 	*mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
 }
 
+void xen_enable_sysenter(void)
+{
+	int cpu = smp_processor_id();
+	extern void xen_sysenter_target(void);
+	/* Mask events on entry, even though they get enabled immediately */
+	static struct callback_register sysenter = {
+		.type = CALLBACKTYPE_sysenter,
+		.address = { __KERNEL_CS, (unsigned long)xen_sysenter_target },
+		.flags = CALLBACKF_mask_events,
+	};
+
+	if (!boot_cpu_has(X86_FEATURE_SEP) ||
+	    HYPERVISOR_callback_op(CALLBACKOP_register, &sysenter) != 0) {
+		clear_cpu_cap(&cpu_data(cpu), X86_FEATURE_SEP);
+		clear_cpu_cap(&boot_cpu_data, X86_FEATURE_SEP);
+	}
+}
+
 void __init xen_arch_setup(void)
 {
 	struct physdev_set_iopl set_iopl;
@@ -80,6 +99,8 @@ void __init xen_arch_setup(void)
 
 	HYPERVISOR_set_callbacks(__KERNEL_CS, (unsigned long)xen_hypervisor_callback,
 				 __KERNEL_CS, (unsigned long)xen_failsafe_callback);
+
+	xen_enable_sysenter();
 
 	set_iopl.iopl = 1;
 	rc = HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);

===================================================================

arch/x86/xen/smp.c +1 -0
@@ -72,6 +72,7 @@ static __cpuinit void cpu_bringup_and_id
 	int cpu = smp_processor_id();
 
 	cpu_init();
+	xen_enable_sysenter();
 
 	preempt_disable();
 	per_cpu(cpu_state, cpu) = CPU_ONLINE;

===================================================================

arch/x86/xen/xen-asm.S +52 -0

58 lines not on this page · raw message

===================================================================

arch/x86/xen/xen-ops.h +3 -0
@@ -19,6 +19,7 @@ char * __init xen_memory_setup(void);
 char * __init xen_memory_setup(void);
 void __init xen_arch_setup(void);
 void __init xen_init_IRQ(void);
+void xen_enable_sysenter(void);
 
 void xen_setup_timer(int cpu);
 void xen_setup_cpu_clockevents(void);
@@ -64,4 +65,6 @@ DECL_ASM(void, xen_restore_fl_direct, un
 DECL_ASM(void, xen_restore_fl_direct, unsigned long);
 
 void xen_iret(void);
+void xen_sysexit(void);
+
 #endif /* XEN_OPS_H */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 4 lines

Anyway, a couple of questions. It seems that the stack frame that Xen's sysenter generates is not exactly the same as the one the kernel expects, so the direct access to the threadinfo structure doesn't work properly. What's the difference in the frames?

The frame is a normal interrupt frame (but not completely/properly filled in - the implication of course is that the stack has been switched, other than native sysenter would do), which is why the code in our kernels just is a special preamble to system_call:

...
ENDPROC(ia32_sysenter_target)

	# pv sysenter call handler stub
ENTRY(ia32pv_sysenter_target)
	RING0_INT_FRAME
	movl $__USER_DS,16(%esp)
	movl %ebp,12(%esp)
	movl $__USER_CS,4(%esp)
	addl $4,%esp
	CFI_ADJUST_CFA_OFFSET -4
	/* +5*4 is SS:ESP,EFLAGS,CS:EIP. +8 is esp0 setting. */
	pushl (TI_sysenter_return-THREAD_SIZE+8+4*4)(%esp)
	CFI_ADJUST_CFA_OFFSET 4
/*
 * 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(ia32pv_sysenter_target)
	# system call handler stub
ENTRY(system_call)
...
Quoted 4 lines

I guess the other reason for the separate PV Xen sysenter entrypoint is to deal with sysexit not working. I addressed this by implementing a sysexit pvop using iret, though I think I could just set the TIF_IRET flag in threadinfo.

Either should work, but as pointed out above letting it just fall through to system_call seems even easier.

Quoted 2 lines

Anyway, could you look at these changes and see if anything problematic leaps out.

This description

Quoted 5 lines

The sysenter path tries to enable interrupts immediately. Unfortunately this doesn't work in a paravirt environment, because not enough kernel state has been set up at that point (namely, pointing %fs to the kernel percpu data segment). To fix this, defer ENABLE_INTERRUPTS until after the kernel state has been set up.

seems bogus: The sysenter handler in our kernels gets called with interrupts enabled, which is as safe as int $80 going through a trap gate (i.e. the rest of the kernel needs to be prepared to deal with interrupts being enabled here anyway).

Jan

#3 lore raw
In reply to Jeremy Fitzhardinge
Quoted 2 lines · Jeremy Fitzhardinge

Anyway, could you look at these changes and see if anything problematic leaps out.

Looks okay to me. The probe for SYSENTER looks good since we promise not do advertise CPUID.SEP to compat guests on AMD systems.

-- Keir

#2 lore raw
In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 2 lines

Anyway, a couple of questions. It seems that the stack frame that Xen's sysenter generates is not exactly the same as the one the kernel expects, so the direct access to the threadinfo structure doesn't work properly. What's the difference in the frames?

The frame is a normal interrupt frame (but not completely/properly filled in - the implication of course is that the stack has been switched, other than native sysenter would do), which is why the code in our kernels just is a special preamble to system_call:

Yes, I copied that code more or less unchanged.

Quoted 11 lines

...
ENDPROC(ia32_sysenter_target)

	# pv sysenter call handler stub
ENTRY(ia32pv_sysenter_target)
	RING0_INT_FRAME
	movl $__USER_DS,16(%esp)
	movl %ebp,12(%esp)
	movl $__USER_CS,4(%esp)
	addl $4,%esp
Quoted 6 lines
Quoted 2 lines

I guess the other reason for the separate PV Xen sysenter entrypoint is to deal with sysexit not working. I addressed this by implementing a sysexit pvop using iret, though I think I could just set the TIF_IRET flag in threadinfo.

Either should work, but as pointed out above letting it just fall through to system_call seems even easier.

It means you need to duplicate more code. My variant just has the Xen-specific stack setup on entry, but then it can just fall back to the normal path.

Quoted 11 lines
Quoted 6 lines

The sysenter path tries to enable interrupts immediately. Unfortunately this doesn't work in a paravirt environment, because not enough kernel state has been set up at that point (namely, pointing %fs to the kernel percpu data segment). To fix this, defer ENABLE_INTERRUPTS until after the kernel state has been set up.

seems bogus: The sysenter handler in our kernels gets called with interrupts enabled, which is as safe as int $80 going through a trap gate (i.e. the rest of the kernel needs to be prepared to deal with interrupts being enabled here anyway).

It's a principled fix. It's true that there's only a visible problem when making the Xen sysenter address point to the normal sysenter target - which doesn't work because of the different calling convention. But if it did work (ie, Xen - or another hypervisor - produced the same frame as the normal sysenter instruction), then ENABLE_INTERRUPTS would fail because it's being called before the kernel's percpu segment has been set up.

So given that ENABLE_INTERRUPTS needs to happen later, I set up xen_sysenter_target to enter with events masked, so that it's as similar to the hardware instruction as possible, and interrupts enabled are in the same place in both cases.

J