Skip to contentSkip to conversation

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

PATCH Superseded Juergen Gross 3 participants 9 messages Feb 1, 2018 – Feb 2, 2018

Subsystems (3)
lore
Share & tools
lore

Juergen Gross

author maintainer
#0 lore raw

PATCH v2 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 | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/x86/xen/xen-head.S +16 -0
index 497cc55a0c16..96f26e026783 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,20 @@ ENTRY(startup_xen)
 	mov %_ASM_SI, xen_start_info
 	mov $init_thread_union+THREAD_SIZE, %_ASM_SP
 
+#ifdef CONFIG_X86_64
+	/* 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
+#endif
+
 	jmp xen_start_kernel
 END(startup_xen)
 	__FINIT
Signature · 1 line
2.13.6
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 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.

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

#3 lore raw

Works great, tested it and it fixes booting Linux v4.15 kernel for me :)

Cheers!

Quoted 18 lines · Boris Ostrovsky
Quoted 8 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.

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

author maintainer
#4 lore raw
Quoted 1 line · Chris Patterson

Works great, tested it and it fixes booting Linux v4.15 kernel for me :)

Can I add your "Tested-by:" to the patch when committing it?

Juergen

Quoted 24 lines

Cheers!

Quoted 18 lines · Boris Ostrovsky
Quoted 8 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.

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

#5 lore raw
Quoted 6 lines · Juergen Gross
Quoted 1 line · Chris Patterson

Works great, tested it and it fixes booting Linux v4.15 kernel for me :)

Can I add your "Tested-by:" to the patch when committing it?

Sure thing, have a great day.

author maintainer
#7 lore raw
In reply to Chris Patterson
Quoted 1 line · Chris Patterson

Works great, tested it and it fixes booting Linux v4.15 kernel for me :)

Can I add your "Tested-by:" to the patch when committing it?

Juergen

Quoted 24 lines

Cheers!

Quoted 18 lines · Boris Ostrovsky
Quoted 8 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.

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

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

#8 lore raw
In reply to Boris Ostrovsky

Works great, tested it and it fixes booting Linux v4.15 kernel for me :)

Cheers!

Quoted 10 lines · Boris Ostrovsky
Quoted 8 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.

Quoted 8 lines

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

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