Source-Changes-HG archive

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

[src/trunk]: src/sys Use two separate functions: cpu_segregs32_zero and cpu_s...



details:   https://anonhg.NetBSD.org/src/rev/90e46a7b69c7
branches:  trunk
changeset: 827123:90e46a7b69c7
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sun Oct 15 12:49:53 2017 +0000

description:
Use two separate functions: cpu_segregs32_zero and cpu_segregs64_zero. The
way segment registers work on amd64 will diverge between 32bit and 64bit
LWPs.

diffstat:

 sys/arch/amd64/amd64/machdep.c                  |  55 ++++++++++++++++++------
 sys/arch/amd64/amd64/netbsd32_machdep.c         |   6 +-
 sys/arch/amd64/include/segments.h               |   5 +-
 sys/compat/linux/arch/amd64/linux_machdep.c     |   6 +-
 sys/compat/linux32/arch/amd64/linux32_machdep.c |   6 +-
 5 files changed, 53 insertions(+), 25 deletions(-)

diffs (207 lines):

diff -r 2a9df8951fb5 -r 90e46a7b69c7 sys/arch/amd64/amd64/machdep.c
--- a/sys/arch/amd64/amd64/machdep.c    Sun Oct 15 12:01:43 2017 +0000
+++ b/sys/arch/amd64/amd64/machdep.c    Sun Oct 15 12:49:53 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: machdep.c,v 1.265 2017/10/15 10:58:32 maxv Exp $       */
+/*     $NetBSD: machdep.c,v 1.266 2017/10/15 12:49:53 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.265 2017/10/15 10:58:32 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.266 2017/10/15 12:49:53 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1330,7 +1330,7 @@
        tf = l->l_md.md_regs;
        tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
        tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
-       cpu_fsgs_zero(l);
+       cpu_segregs64_zero(l);
        tf->tf_rdi = 0;
        tf->tf_rsi = 0;
        tf->tf_rbp = 0;
@@ -2042,17 +2042,17 @@
 }
 
 /*
- * Zero out an LWP's TLS context (%fs and %gs and associated stuff).
- * Used when exec'ing a new program.
+ * Zero out a 64bit LWP's segments registers. Used when exec'ing a new
+ * 64bit program.
  */
-
 void
-cpu_fsgs_zero(struct lwp *l)
+cpu_segregs64_zero(struct lwp *l)
 {
        struct trapframe * const tf = l->l_md.md_regs;
        struct pcb *pcb;
        uint64_t zero = 0;
 
+       KASSERT((l->l_proc->p_flag & PK_32) == 0);
        KASSERT(l == curlwp);
 
        pcb = lwp_getpcb(l);
@@ -2062,15 +2062,43 @@
        tf->tf_gs = 0;
        setfs(0);
        setusergs(0);
-       if ((l->l_proc->p_flag & PK_32) == 0) {
+
 #ifndef XEN
-               wrmsr(MSR_FSBASE, 0);
-               wrmsr(MSR_KERNELGSBASE, 0);
+       wrmsr(MSR_FSBASE, 0);
+       wrmsr(MSR_KERNELGSBASE, 0);
 #else
-               HYPERVISOR_set_segment_base(SEGBASE_FS, 0);
-               HYPERVISOR_set_segment_base(SEGBASE_GS_USER, 0);
+       HYPERVISOR_set_segment_base(SEGBASE_FS, 0);
+       HYPERVISOR_set_segment_base(SEGBASE_GS_USER, 0);
 #endif
-       }
+
+       pcb->pcb_fs = 0;
+       pcb->pcb_gs = 0;
+       update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &zero);
+       update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &zero);
+       kpreempt_enable();
+}
+
+/*
+ * Zero out a 32bit LWP's segments registers. Used when exec'ing a new
+ * 32bit program.
+ */
+void
+cpu_segregs32_zero(struct lwp *l)
+{
+       struct trapframe * const tf = l->l_md.md_regs;
+       struct pcb *pcb;
+       uint64_t zero = 0;
+
+       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;
+       setfs(0);
+       setusergs(0);
        pcb->pcb_fs = 0;
        pcb->pcb_gs = 0;
        update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &zero);
@@ -2082,7 +2110,6 @@
  * Load an LWP's TLS context, possibly changing the %fs and %gs selectors.
  * Used only for 32-bit processes.
  */
-
 void
 cpu_fsgs_reload(struct lwp *l, int fssel, int gssel)
 {
diff -r 2a9df8951fb5 -r 90e46a7b69c7 sys/arch/amd64/amd64/netbsd32_machdep.c
--- a/sys/arch/amd64/amd64/netbsd32_machdep.c   Sun Oct 15 12:01:43 2017 +0000
+++ b/sys/arch/amd64/amd64/netbsd32_machdep.c   Sun Oct 15 12:49:53 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: netbsd32_machdep.c,v 1.110 2017/10/15 11:36:15 maxv Exp $      */
+/*     $NetBSD: netbsd32_machdep.c,v 1.111 2017/10/15 12:49:53 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.110 2017/10/15 11:36:15 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.111 2017/10/15 12:49:53 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -148,7 +148,7 @@
        tf = l->l_md.md_regs;
        tf->tf_ds = LSEL(LUDATA32_SEL, SEL_UPL);
        tf->tf_es = LSEL(LUDATA32_SEL, SEL_UPL);
-       cpu_fsgs_zero(l);
+       cpu_segregs32_zero(l);
        cpu_fsgs_reload(l, tf->tf_ds, tf->tf_es);
        tf->tf_rdi = 0;
        tf->tf_rsi = 0;
diff -r 2a9df8951fb5 -r 90e46a7b69c7 sys/arch/amd64/include/segments.h
--- a/sys/arch/amd64/include/segments.h Sun Oct 15 12:01:43 2017 +0000
+++ b/sys/arch/amd64/include/segments.h Sun Oct 15 12:49:53 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: segments.h,v 1.30 2017/09/17 09:41:35 maxv Exp $       */
+/*     $NetBSD: segments.h,v 1.31 2017/10/15 12:49:53 maxv Exp $       */
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -253,7 +253,8 @@
 #endif
 
 struct lwp;
-void cpu_fsgs_zero(struct lwp *);
+void cpu_segregs64_zero(struct lwp *);
+void cpu_segregs32_zero(struct lwp *);
 void cpu_fsgs_reload(struct lwp *, int, int);
 
 #endif /* _KERNEL */
diff -r 2a9df8951fb5 -r 90e46a7b69c7 sys/compat/linux/arch/amd64/linux_machdep.c
--- a/sys/compat/linux/arch/amd64/linux_machdep.c       Sun Oct 15 12:01:43 2017 +0000
+++ b/sys/compat/linux/arch/amd64/linux_machdep.c       Sun Oct 15 12:49:53 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: linux_machdep.c,v 1.52 2017/07/14 13:21:29 maxv Exp $ */
+/*     $NetBSD: linux_machdep.c,v 1.53 2017/10/15 12:49:53 maxv Exp $ */
 
 /*-
  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
@@ -33,7 +33,7 @@
 
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.52 2017/07/14 13:21:29 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.53 2017/10/15 12:49:53 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -118,7 +118,7 @@
        tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
        tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
        tf->tf_es = 0;
-       cpu_fsgs_zero(l);
+       cpu_segregs64_zero(l);
 
        return;
 }
diff -r 2a9df8951fb5 -r 90e46a7b69c7 sys/compat/linux32/arch/amd64/linux32_machdep.c
--- a/sys/compat/linux32/arch/amd64/linux32_machdep.c   Sun Oct 15 12:01:43 2017 +0000
+++ b/sys/compat/linux32/arch/amd64/linux32_machdep.c   Sun Oct 15 12:49:53 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: linux32_machdep.c,v 1.40 2017/10/15 11:36:15 maxv Exp $ */
+/*     $NetBSD: linux32_machdep.c,v 1.41 2017/10/15 12:49:53 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.40 2017/10/15 11:36:15 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.41 2017/10/15 12:49:53 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
@@ -311,7 +311,7 @@
        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_fsgs_zero(l);
+       cpu_segregs32_zero(l);
        cpu_fsgs_reload(l, GSEL(GUDATA32_SEL, SEL_UPL), GSEL(GUDATA32_SEL, SEL_UPL));
 }
 



Home | Main Index | Thread Index | Old Index