Module Name:    src
Committed By:   maxv
Date:           Sun Mar 24 15:58:32 UTC 2019

Modified Files:
        src/sys/arch/amd64/amd64: machdep.c netbsd32_machdep.c
        src/sys/compat/linux32/arch/amd64: linux32_machdep.c

Log Message:
Disable preemption when setting PCB_COMPAT32, to prevent a context switch
before cpu_fsgs_reload() finishes, otherwise we write garbage in the GDT.
On NetBSD-current it is harmless, however in NetBSD-8 it might cause
panics, because NetBSD-8 uses the old SegRegs model and under this model
we reload %fs and %gs during switches.


To generate a diff of this commit:
cvs rdiff -u -r1.328 -r1.329 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.119 -r1.120 src/sys/arch/amd64/amd64/netbsd32_machdep.c
cvs rdiff -u -r1.43 -r1.44 \
    src/sys/compat/linux32/arch/amd64/linux32_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.328 src/sys/arch/amd64/amd64/machdep.c:1.329
--- src/sys/arch/amd64/amd64/machdep.c:1.328	Sun Mar 24 13:15:42 2019
+++ src/sys/arch/amd64/amd64/machdep.c	Sun Mar 24 15:58:32 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.328 2019/03/24 13:15:42 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.329 2019/03/24 15:58:32 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.328 2019/03/24 13:15:42 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.329 2019/03/24 15:58:32 maxv Exp $");
 
 #include "opt_modular.h"
 #include "opt_user_ldt.h"
@@ -2237,12 +2237,12 @@ cpu_segregs32_zero(struct lwp *l)
 	struct pcb *pcb;
 	uint64_t zero = 0;
 
+	KASSERT(kpreempt_disabled());
 	KASSERT(l->l_proc->p_flag & PK_32);
 	KASSERT(l == curlwp);
 
 	pcb = lwp_getpcb(l);
 
-	kpreempt_disable();
 	tf->tf_fs = 0;
 	tf->tf_gs = 0;
 	setds(GSEL(GUDATA32_SEL, SEL_UPL));
@@ -2253,7 +2253,6 @@ cpu_segregs32_zero(struct lwp *l)
 	pcb->pcb_gs = 0;
 	update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &zero);
 	update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &zero);
-	kpreempt_enable();
 }
 
 /*

Index: src/sys/arch/amd64/amd64/netbsd32_machdep.c
diff -u src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.119 src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.120
--- src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.119	Fri Mar  1 11:06:55 2019
+++ src/sys/arch/amd64/amd64/netbsd32_machdep.c	Sun Mar 24 15:58:32 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.119 2019/03/01 11:06:55 pgoyette Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.120 2019/03/24 15:58:32 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.119 2019/03/01 11:06:55 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.120 2019/03/24 15:58:32 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -138,21 +138,22 @@ netbsd32_setregs(struct lwp *l, struct e
 
 	netbsd32_adjust_limits(p);
 
-	l->l_md.md_flags = MDL_COMPAT32;	/* Force iret not sysret */
-	pcb->pcb_flags = PCB_COMPAT32;
-
 	fpu_save_area_clear(l, pack->ep_osversion >= 699002600
 	    ?  __NetBSD_NPXCW__ : __NetBSD_COMPAT_NPXCW__);
-
 	x86_dbregs_clear(l);
 
+	kpreempt_disable();
+	pcb->pcb_flags = PCB_COMPAT32;
 	p->p_flag |= PK_32;
+	l->l_md.md_flags = MDL_COMPAT32;	/* force iret not sysret */
+	cpu_segregs32_zero(l);
+	cpu_fsgs_reload(l, LSEL(LUDATA32_SEL, SEL_UPL),
+	    LSEL(LUDATA32_SEL, SEL_UPL));
+	kpreempt_enable();
 
 	tf = l->l_md.md_regs;
 	tf->tf_ds = LSEL(LUDATA32_SEL, SEL_UPL);
 	tf->tf_es = LSEL(LUDATA32_SEL, SEL_UPL);
-	cpu_segregs32_zero(l);
-	cpu_fsgs_reload(l, tf->tf_ds, tf->tf_es);
 	tf->tf_rdi = 0;
 	tf->tf_rsi = 0;
 	tf->tf_rbp = 0;

Index: src/sys/compat/linux32/arch/amd64/linux32_machdep.c
diff -u src/sys/compat/linux32/arch/amd64/linux32_machdep.c:1.43 src/sys/compat/linux32/arch/amd64/linux32_machdep.c:1.44
--- src/sys/compat/linux32/arch/amd64/linux32_machdep.c:1.43	Sat Oct 21 07:24:26 2017
+++ src/sys/compat/linux32/arch/amd64/linux32_machdep.c	Sun Mar 24 15:58:32 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux32_machdep.c,v 1.43 2017/10/21 07:24:26 maxv Exp $ */
+/*	$NetBSD: linux32_machdep.c,v 1.44 2019/03/24 15:58:32 maxv Exp $ */
 
 /*-
  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
@@ -31,7 +31,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.43 2017/10/21 07:24:26 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.44 2019/03/24 15:58:32 maxv Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_user_ldt.h"
@@ -287,10 +287,14 @@ linux32_setregs(struct lwp *l, struct ex
 
 	fpu_save_area_clear(l, __Linux_NPXCW__);
 
-	l->l_md.md_flags = MDL_COMPAT32;	/* Forces iret not sysret */
+	kpreempt_disable();
 	pcb->pcb_flags = PCB_COMPAT32;
-
 	p->p_flag |= PK_32;
+	l->l_md.md_flags = MDL_COMPAT32;	/* force iret not sysret */
+	cpu_segregs32_zero(l);
+	cpu_fsgs_reload(l, GSEL(GUDATA32_SEL, SEL_UPL),
+	    GSEL(GUDATA32_SEL, SEL_UPL));
+	kpreempt_enable();
 
 	tf = l->l_md.md_regs;
 	tf->tf_rax = 0;
@@ -315,8 +319,6 @@ linux32_setregs(struct lwp *l, struct ex
 	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL);
 	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL);
 	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL);
-	cpu_segregs32_zero(l);
-	cpu_fsgs_reload(l, GSEL(GUDATA32_SEL, SEL_UPL), GSEL(GUDATA32_SEL, SEL_UPL));
 }
 
 static void

Reply via email to