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.
This commit is contained in:
maxv 2019-03-24 15:58:32 +00:00
parent 2ee402b3d0
commit 6300e1b8c4
3 changed files with 20 additions and 18 deletions

View File

@ -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();
}
/*

View File

@ -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 exec_package *pack, vaddr_t stack)
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;

View File

@ -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 exec_package *pack, u_long stack)
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 exec_package *pack, u_long stack)
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