Skip to contentSkip to conversation

x86/xen: init %gs very early to avoid page faults with stack protector

PATCH In review Juergen Gross 2 participants 5 messages Feb 1, 2018

Subsystems (3)
lore
Share & tools
lore

Juergen Gross

author maintainer
#0 lore raw

PATCH 1/1 x86/xen: init %gs very early to avoid page faults with stack protector

When running as Xen pv guest %gs is initialized some time after C code is started. Depending on stack protector usage this might be too late, resulting in page faults.

So setup %gs and MSR_GS_BASE in assembly code already.

---
 arch/x86/xen/xen-head.S | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/xen/xen-head.S +14 -0
index 497cc55a0c16..b47d87076efb 100644
@@ -9,7 +9,9 @@
 
 #include <asm/boot.h>
 #include <asm/asm.h>
+#include <asm/msr.h>
 #include <asm/page_types.h>
+#include <asm/percpu.h>
 #include <asm/unwind_hints.h>
 
 #include <xen/interface/elfnote.h>
@@ -35,6 +37,18 @@ ENTRY(startup_xen)
 	mov %_ASM_SI, xen_start_info
 	mov $init_thread_union+THREAD_SIZE, %_ASM_SP
 
+	/* Set up %gs.
+	 *
+	 * The base of %gs always points to the bottom of the irqstack
+	 * union.  If the stack protector canary is enabled, it is
+	 * located at %gs:40.  Note that, on SMP, the boot cpu uses
+	 * init data section till per cpu areas are set up.
+	 */
+	movl	$MSR_GS_BASE,%ecx
+	movq	$INIT_PER_CPU_VAR(irq_stack_union),%rax
+	cdq
+	wrmsr
+
 	jmp xen_start_kernel
 END(startup_xen)
 	__FINIT
Signature · 1 line
2.13.6
#1 lore raw
Quoted 31 lines · Juergen Gross

When running as Xen pv guest %gs is initialized some time after C code is started. Depending on stack protector usage this might be too late, resulting in page faults.

So setup %gs and MSR_GS_BASE in assembly code already.

---
 arch/x86/xen/xen-head.S | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/xen/xen-head.S +3 -0
index 497cc55a0c16..b47d87076efb 100644
@@ -9,7 +9,9 @@
 
 #include <asm/boot.h>
 #include <asm/asm.h>
+#include <asm/msr.h>
 #include <asm/page_types.h>
+#include <asm/percpu.h>
 #include <asm/unwind_hints.h>
 
 #include <xen/interface/elfnote.h>
@@ -35,6 +37,18 @@ ENTRY(startup_xen)
 	mov %_ASM_SI, xen_start_info
 	mov $init_thread_union+THREAD_SIZE, %_ASM_SP
 
+	/* Set up %gs.

arch/x86/xen/xen-head.S ENTRY(startup_xen) · line 50

Quoted 10 lines
arch/x86/xen/xen-head.S +10 -0
+	 *
+	 * The base of %gs always points to the bottom of the irqstack
+	 * union.  If the stack protector canary is enabled, it is
+	 * located at %gs:40.  Note that, on SMP, the boot cpu uses
+	 * init data section till per cpu areas are set up.
+	 */
+	movl	$MSR_GS_BASE,%ecx
+	movq	$INIT_PER_CPU_VAR(irq_stack_union),%rax
+	cdq
+	wrmsr

You surely want a #ifdef __x86_64__ ?  This path is common to the 32bit entry as well?

~Andrew

Quoted 4 lines
arch/x86/xen/xen-head.S +1 -0
+
 	jmp xen_start_kernel
 END(startup_xen)
 	__FINIT
author maintainer
#2 lore raw
Quoted 31 lines · Andrew Cooper
Quoted 30 lines · Juergen Gross

When running as Xen pv guest %gs is initialized some time after C code is started. Depending on stack protector usage this might be too late, resulting in page faults.

So setup %gs and MSR_GS_BASE in assembly code already.

---
 arch/x86/xen/xen-head.S | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/xen/xen-head.S +2 -0
index 497cc55a0c16..b47d87076efb 100644
@@ -9,7 +9,9 @@
 
 #include <asm/boot.h>
 #include <asm/asm.h>
+#include <asm/msr.h>
 #include <asm/page_types.h>
+#include <asm/percpu.h>
 #include <asm/unwind_hints.h>
 
 #include <xen/interface/elfnote.h>
@@ -35,6 +37,18 @@ ENTRY(startup_xen)
 	mov %_ASM_SI, xen_start_info
 	mov $init_thread_union+THREAD_SIZE, %_ASM_SP
 
Quoted 14 lines

arch/x86/xen/xen-head.S ENTRY(startup_xen) · line 50

Quoted 11 lines
arch/x86/xen/xen-head.S +11 -0
+	/* Set up %gs.
+	 *
+	 * The base of %gs always points to the bottom of the irqstack
+	 * union.  If the stack protector canary is enabled, it is
+	 * located at %gs:40.  Note that, on SMP, the boot cpu uses
+	 * init data section till per cpu areas are set up.
+	 */
+	movl	$MSR_GS_BASE,%ecx
+	movq	$INIT_PER_CPU_VAR(irq_stack_union),%rax
+	cdq
+	wrmsr

You surely want a #ifdef __x86_64__ ?  This path is common to the 32bit entry as well?

Oh, indeed! Thanks for noticing.

V2 coming soon...

Juergen

author maintainer
#3 lore raw
In reply to Andrew Cooper
Quoted 31 lines · Andrew Cooper
Quoted 30 lines · Juergen Gross

When running as Xen pv guest %gs is initialized some time after C code is started. Depending on stack protector usage this might be too late, resulting in page faults.

So setup %gs and MSR_GS_BASE in assembly code already.

---
 arch/x86/xen/xen-head.S | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/xen/xen-head.S +2 -0
index 497cc55a0c16..b47d87076efb 100644
@@ -9,7 +9,9 @@
 
 #include <asm/boot.h>
 #include <asm/asm.h>
+#include <asm/msr.h>
 #include <asm/page_types.h>
+#include <asm/percpu.h>
 #include <asm/unwind_hints.h>
 
 #include <xen/interface/elfnote.h>
@@ -35,6 +37,18 @@ ENTRY(startup_xen)
 	mov %_ASM_SI, xen_start_info
 	mov $init_thread_union+THREAD_SIZE, %_ASM_SP
 
Quoted 14 lines

arch/x86/xen/xen-head.S ENTRY(startup_xen) · line 50

Quoted 11 lines
arch/x86/xen/xen-head.S +11 -0
+	/* Set up %gs.
+	 *
+	 * The base of %gs always points to the bottom of the irqstack
+	 * union.  If the stack protector canary is enabled, it is
+	 * located at %gs:40.  Note that, on SMP, the boot cpu uses
+	 * init data section till per cpu areas are set up.
+	 */
+	movl	$MSR_GS_BASE,%ecx
+	movq	$INIT_PER_CPU_VAR(irq_stack_union),%rax
+	cdq
+	wrmsr

You surely want a #ifdef __x86_64__ ?  This path is common to the 32bit entry as well?

Oh, indeed! Thanks for noticing.

V2 coming soon...

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Juergen Gross
Quoted 31 lines · Juergen Gross

When running as Xen pv guest %gs is initialized some time after C code is started. Depending on stack protector usage this might be too late, resulting in page faults.

So setup %gs and MSR_GS_BASE in assembly code already.

---
 arch/x86/xen/xen-head.S | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/xen/xen-head.S +3 -0
index 497cc55a0c16..b47d87076efb 100644
@@ -9,7 +9,9 @@
 
 #include <asm/boot.h>
 #include <asm/asm.h>
+#include <asm/msr.h>
 #include <asm/page_types.h>
+#include <asm/percpu.h>
 #include <asm/unwind_hints.h>
 
 #include <xen/interface/elfnote.h>
@@ -35,6 +37,18 @@ ENTRY(startup_xen)
 	mov %_ASM_SI, xen_start_info
 	mov $init_thread_union+THREAD_SIZE, %_ASM_SP
 
+	/* Set up %gs.

arch/x86/xen/xen-head.S ENTRY(startup_xen) · line 50

Quoted 10 lines
arch/x86/xen/xen-head.S +10 -0
+	 *
+	 * The base of %gs always points to the bottom of the irqstack
+	 * union.  If the stack protector canary is enabled, it is
+	 * located at %gs:40.  Note that, on SMP, the boot cpu uses
+	 * init data section till per cpu areas are set up.
+	 */
+	movl	$MSR_GS_BASE,%ecx
+	movq	$INIT_PER_CPU_VAR(irq_stack_union),%rax
+	cdq
+	wrmsr

You surely want a #ifdef __x86_64__ ?  This path is common to the 32bit entry as well?

~Andrew

arch/x86/xen/xen-head.S ENTRY(startup_xen) · line 54

Quoted 4 lines
arch/x86/xen/xen-head.S +1 -0
+
 	jmp xen_start_kernel
 END(startup_xen)
 	__FINIT

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel