Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Disable preemption when setting PCB_COMPAT32, to prevent...



details:   https://anonhg.NetBSD.org/src/rev/e5c3182bc416
branches:  trunk
changeset: 449804:e5c3182bc416
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sun Mar 24 15:58:32 2019 +0000

description:
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.

diffstat:

 sys/arch/amd64/amd64/machdep.c                  |   7 +++----
 sys/arch/amd64/amd64/netbsd32_machdep.c         |  17 +++++++++--------
 sys/compat/linux32/arch/amd64/linux32_machdep.c |  14 ++++++++------
 3 files changed, 20 insertions(+), 18 deletions(-)

diffs (131 lines):

diff -r a67244e0abaa -r e5c3182bc416 sys/arch/amd64/amd64/machdep.c
--- a/sys/arch/amd64/amd64/machdep.c    Sun Mar 24 13:45:35 2019 +0000
+++ b/sys/arch/amd64/amd64/machdep.c    Sun Mar 24 15:58:32 2019 +0000
@@ -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 @@
        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 @@
        pcb->pcb_gs = 0;
        update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &zero);
        update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &zero);
-       kpreempt_enable();
 }
 
 /*
diff -r a67244e0abaa -r e5c3182bc416 sys/arch/amd64/amd64/netbsd32_machdep.c
--- a/sys/arch/amd64/amd64/netbsd32_machdep.c   Sun Mar 24 13:45:35 2019 +0000
+++ b/sys/arch/amd64/amd64/netbsd32_machdep.c   Sun Mar 24 15:58:32 2019 +0000
@@ -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_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;
diff -r a67244e0abaa -r e5c3182bc416 sys/compat/linux32/arch/amd64/linux32_machdep.c
--- a/sys/compat/linux32/arch/amd64/linux32_machdep.c   Sun Mar 24 13:45:35 2019 +0000
+++ b/sys/compat/linux32/arch/amd64/linux32_machdep.c   Sun Mar 24 15:58:32 2019 +0000
@@ -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 @@
 
        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 @@
        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



Home | Main Index | Thread Index | Old Index