Skip to contentSkip to conversation

linux-2.6.18/x86: improve CR0 read/write handling

PATCH New Jan Beulich 1 participant 1 message Jul 2, 2012

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 linux-2.6.18/x86: improve CR0 read/write handling

With the only bit in CR0 permitted to be changed by PV guests being TS, optimize the handling towards that: Keep a cached value in a per-CPU variable, and issue HYPERVISOR_fpu_taskswitch hypercalls for updates in all but the unusual case should something in the system still try to modify another bit (the attempt of which would then be logged by the hypervisor).

This removes the need to have the hypervisor emulate MOV to/from CR0 instructions in all halfway frequently executed code paths.

---
v2: Add safety measure for nested updates (those will now always access
non-cached state).

arch/i386/kernel/cpu/common-xen.c +7 -0
@@ -32,6 +32,11 @@ EXPORT_PER_CPU_SYMBOL(cpu_gdt_descr);
 #ifndef CONFIG_XEN
 DEFINE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]);
 EXPORT_PER_CPU_SYMBOL(cpu_16bit_stack);
+#else
+DEFINE_PER_CPU(unsigned int, xen_x86_cr0);
+DEFINE_PER_CPU(unsigned int, xen_x86_cr0_upd) = ~0;
+EXPORT_PER_CPU_SYMBOL(xen_x86_cr0);
+EXPORT_PER_CPU_SYMBOL(xen_x86_cr0_upd);
 #endif
 
 static int cachesize_override __cpuinitdata = -1;
@@ -681,6 +686,8 @@ old_gdt:
 	cpu_gdt_descr->size = GDT_SIZE - 1;
  	cpu_gdt_descr->address = (unsigned long)gdt;
 #else
+	__get_cpu_var(xen_x86_cr0) = raw_read_cr0();
+	xen_clear_cr0_upd();
 	if (cpu == 0 && cpu_gdt_descr->address == 0) {
 		gdt = (struct desc_struct *)alloc_bootmem_pages(PAGE_SIZE);
 		/* alloc_bootmem_pages panics on failure, so no check */
arch/i386/kernel/process-xen.c +6 -0
@@ -639,8 +639,14 @@ struct task_struct fastcall * __switch_t
 	BUG_ON(pdo > _pdo + ARRAY_SIZE(_pdo));
 #endif
 	BUG_ON(mcl > _mcl + ARRAY_SIZE(_mcl));
+	if (_mcl->op == __HYPERVISOR_fpu_taskswitch)
+		__get_cpu_var(xen_x86_cr0_upd) = X86_CR0_TS;
 	if (unlikely(HYPERVISOR_multicall_check(_mcl, mcl - _mcl, NULL)))
 		BUG();
+	if (_mcl->op == __HYPERVISOR_fpu_taskswitch) {
+		__get_cpu_var(xen_x86_cr0) |= X86_CR0_TS;
+		xen_clear_cr0_upd();
+	}
 
 	/*
 	 * Restore %fs and %gs if needed.
arch/i386/kernel/traps-xen.c +1 -0
@@ -1057,6 +1057,7 @@ asmlinkage void math_state_restore(struc
 	struct task_struct *tsk = thread->task;
 
 	/* NB. 'clts' is done for us by Xen during virtual trap. */
+	__get_cpu_var(xen_x86_cr0) &= ~X86_CR0_TS;
 	if (!tsk_used_math(tsk))
 		init_fpu(tsk);
 	restore_fpu(tsk);
arch/x86_64/kernel/process-xen.c +6 -0
@@ -572,8 +572,14 @@ __switch_to(struct task_struct *prev_p, 
 	BUG_ON(pdo > _pdo + ARRAY_SIZE(_pdo));
 #endif
 	BUG_ON(mcl > _mcl + ARRAY_SIZE(_mcl));
+	if (_mcl->op == __HYPERVISOR_fpu_taskswitch)
+		__get_cpu_var(xen_x86_cr0_upd) = X86_CR0_TS;
 	if (unlikely(HYPERVISOR_multicall_check(_mcl, mcl - _mcl, NULL)))
 		BUG();
+	if (_mcl->op == __HYPERVISOR_fpu_taskswitch) {
+		__get_cpu_var(xen_x86_cr0) |= X86_CR0_TS;
+		xen_clear_cr0_upd();
+	}
 
 	/* 
 	 * Switch DS and ES.
arch/x86_64/kernel/setup64-xen.c +7 -0
@@ -126,6 +126,11 @@ void __init setup_per_cpu_areas(void)
 } 
 
 #ifdef CONFIG_XEN
+DEFINE_PER_CPU(unsigned long, xen_x86_cr0);
+DEFINE_PER_CPU(unsigned long, xen_x86_cr0_upd) = ~0;
+EXPORT_PER_CPU_SYMBOL(xen_x86_cr0);
+EXPORT_PER_CPU_SYMBOL(xen_x86_cr0_upd);
+
 static void switch_pt(void)
 {
 	xen_pt_switch(__pa_symbol(init_level4_pgt));
@@ -174,6 +179,8 @@ void pda_init(int cpu)
 	if (HYPERVISOR_set_segment_base(SEGBASE_GS_KERNEL,
 					(unsigned long)pda))
 		BUG();
+	__get_cpu_var(xen_x86_cr0) = raw_read_cr0();
+	xen_clear_cr0_upd();
 #endif
 	pda->cpunumber = cpu; 
 	pda->irqcount = -1;
arch/x86_64/kernel/traps-xen.c +2 -1
@@ -1075,8 +1075,9 @@ asmlinkage void __attribute__((weak)) mc
 asmlinkage void math_state_restore(void)
 {
 	struct task_struct *me = current;
-        /* clts(); */ /* 'clts' is done for us by Xen during virtual trap. */
 
+	/* NB. 'clts' is done for us by Xen during virtual trap. */
+	__get_cpu_var(xen_x86_cr0) &= ~X86_CR0_TS;
 	if (!used_math())
 		init_fpu(me);
 	restore_fpu_checking(&me->thread.i387.fxsave);
include/asm-i386/mach-xen/asm/system.h +60 -4

91 lines not on this page · raw message

include/asm-x86_64/mach-xen/asm/system.h +59 -5

92 lines not on this page · raw message

x86: improve CR0 read/write handling

With the only bit in CR0 permitted to be changed by PV guests being TS, optimize the handling towards that: Keep a cached value in a per-CPU variable, and issue HYPERVISOR_fpu_taskswitch hypercalls for updates in all but the unusual case should something in the system still try to modify another bit (the attempt of which would then be logged by the hypervisor).

This removes the need to have the hypervisor emulate MOV to/from CR0 instructions in all halfway frequently executed code paths.

---
v2: Add safety measure for nested updates (those will now always access
non-cached state).

arch/i386/kernel/cpu/common-xen.c +7 -0
@@ -32,6 +32,11 @@ EXPORT_PER_CPU_SYMBOL(cpu_gdt_descr);
 #ifndef CONFIG_XEN
 DEFINE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]);
 EXPORT_PER_CPU_SYMBOL(cpu_16bit_stack);
+#else
+DEFINE_PER_CPU(unsigned int, xen_x86_cr0);
+DEFINE_PER_CPU(unsigned int, xen_x86_cr0_upd) = ~0;
+EXPORT_PER_CPU_SYMBOL(xen_x86_cr0);
+EXPORT_PER_CPU_SYMBOL(xen_x86_cr0_upd);
 #endif
 
 static int cachesize_override __cpuinitdata = -1;
@@ -681,6 +686,8 @@ old_gdt:
 	cpu_gdt_descr->size = GDT_SIZE - 1;
  	cpu_gdt_descr->address = (unsigned long)gdt;
 #else
+	__get_cpu_var(xen_x86_cr0) = raw_read_cr0();
+	xen_clear_cr0_upd();
 	if (cpu == 0 && cpu_gdt_descr->address == 0) {
 		gdt = (struct desc_struct *)alloc_bootmem_pages(PAGE_SIZE);
 		/* alloc_bootmem_pages panics on failure, so no check */
arch/i386/kernel/process-xen.c +6 -0
@@ -639,8 +639,14 @@ struct task_struct fastcall * __switch_t
 	BUG_ON(pdo > _pdo + ARRAY_SIZE(_pdo));
 #endif
 	BUG_ON(mcl > _mcl + ARRAY_SIZE(_mcl));
+	if (_mcl->op == __HYPERVISOR_fpu_taskswitch)
+		__get_cpu_var(xen_x86_cr0_upd) = X86_CR0_TS;
 	if (unlikely(HYPERVISOR_multicall_check(_mcl, mcl - _mcl, NULL)))
 		BUG();
+	if (_mcl->op == __HYPERVISOR_fpu_taskswitch) {
+		__get_cpu_var(xen_x86_cr0) |= X86_CR0_TS;
+		xen_clear_cr0_upd();
+	}
 
 	/*
 	 * Restore %fs and %gs if needed.
arch/i386/kernel/traps-xen.c +1 -0
@@ -1057,6 +1057,7 @@ asmlinkage void math_state_restore(struc
 	struct task_struct *tsk = thread->task;
 
 	/* NB. 'clts' is done for us by Xen during virtual trap. */
+	__get_cpu_var(xen_x86_cr0) &= ~X86_CR0_TS;
 	if (!tsk_used_math(tsk))
 		init_fpu(tsk);
 	restore_fpu(tsk);
arch/x86_64/kernel/process-xen.c +6 -0
@@ -572,8 +572,14 @@ __switch_to(struct task_struct *prev_p, 
 	BUG_ON(pdo > _pdo + ARRAY_SIZE(_pdo));
 #endif
 	BUG_ON(mcl > _mcl + ARRAY_SIZE(_mcl));
+	if (_mcl->op == __HYPERVISOR_fpu_taskswitch)
+		__get_cpu_var(xen_x86_cr0_upd) = X86_CR0_TS;
 	if (unlikely(HYPERVISOR_multicall_check(_mcl, mcl - _mcl, NULL)))
 		BUG();
+	if (_mcl->op == __HYPERVISOR_fpu_taskswitch) {
+		__get_cpu_var(xen_x86_cr0) |= X86_CR0_TS;
+		xen_clear_cr0_upd();
+	}
 
 	/* 
 	 * Switch DS and ES.
arch/x86_64/kernel/setup64-xen.c +7 -0
@@ -126,6 +126,11 @@ void __init setup_per_cpu_areas(void)
 } 
 
 #ifdef CONFIG_XEN
+DEFINE_PER_CPU(unsigned long, xen_x86_cr0);
+DEFINE_PER_CPU(unsigned long, xen_x86_cr0_upd) = ~0;
+EXPORT_PER_CPU_SYMBOL(xen_x86_cr0);
+EXPORT_PER_CPU_SYMBOL(xen_x86_cr0_upd);
+
 static void switch_pt(void)
 {
 	xen_pt_switch(__pa_symbol(init_level4_pgt));
@@ -174,6 +179,8 @@ void pda_init(int cpu)
 	if (HYPERVISOR_set_segment_base(SEGBASE_GS_KERNEL,
 					(unsigned long)pda))
 		BUG();
+	__get_cpu_var(xen_x86_cr0) = raw_read_cr0();
+	xen_clear_cr0_upd();
 #endif
 	pda->cpunumber = cpu; 
 	pda->irqcount = -1;
arch/x86_64/kernel/traps-xen.c +2 -1
@@ -1075,8 +1075,9 @@ asmlinkage void __attribute__((weak)) mc
 asmlinkage void math_state_restore(void)
 {
 	struct task_struct *me = current;
-        /* clts(); */ /* 'clts' is done for us by Xen during virtual trap. */
 
+	/* NB. 'clts' is done for us by Xen during virtual trap. */
+	__get_cpu_var(xen_x86_cr0) &= ~X86_CR0_TS;
 	if (!used_math())
 		init_fpu(me);
 	restore_fpu_checking(&me->thread.i387.fxsave);
include/asm-i386/mach-xen/asm/system.h +60 -4

91 lines not on this page · raw message

include/asm-x86_64/mach-xen/asm/system.h +59 -5

92 lines not on this page · raw message

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