Source-Changes-HG archive

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

[src/trunk]: src/sys/arch/amd64/amd64 Move proc0's stack out of the BOOTSTRAP...



details:   https://anonhg.NetBSD.org/src/rev/533b0883bab1
branches:  trunk
changeset: 815581:533b0883bab1
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sat May 28 08:43:16 2016 +0000

description:
Move proc0's stack out of the BOOTSTRAP TABLES, and map it independently
with RW permissions. Reduces the impact of a stack overflow.

diffstat:

 sys/arch/amd64/amd64/locore.S |  54 +++++++++++++++++++++++++++---------------
 1 files changed, 34 insertions(+), 20 deletions(-)

diffs (130 lines):

diff -r d069587fbd41 -r 533b0883bab1 sys/arch/amd64/amd64/locore.S
--- a/sys/arch/amd64/amd64/locore.S     Fri May 27 20:01:49 2016 +0000
+++ b/sys/arch/amd64/amd64/locore.S     Sat May 28 08:43:16 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: locore.S,v 1.94 2016/05/26 07:24:55 maxv Exp $ */
+/*     $NetBSD: locore.S,v 1.95 2016/05/28 08:43:16 maxv Exp $ */
 
 /*
  * Copyright-o-rama!
@@ -203,12 +203,11 @@
 #endif
 
 #define PROC0_PML4_OFF 0
-#define PROC0_STK_OFF  (PROC0_PML4_OFF + 1 * PAGE_SIZE)
-#define PROC0_PTP3_OFF (PROC0_STK_OFF + UPAGES * PAGE_SIZE)
+#define PROC0_PTP3_OFF (PROC0_PML4_OFF + 1 * PAGE_SIZE)
 #define PROC0_PTP2_OFF (PROC0_PTP3_OFF + NKL4_KIMG_ENTRIES * PAGE_SIZE)
 #define PROC0_PTP1_OFF (PROC0_PTP2_OFF + TABLE_L3_ENTRIES * PAGE_SIZE)
 #define TABLESIZE \
-  ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1 + UPAGES) \
+  ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1) \
     * PAGE_SIZE)
 
 /*
@@ -308,6 +307,7 @@
 END(lapic_isr)
 #endif /* NLAPIC > 0 */
 
+       .globl  _C_LABEL(proc0stack)
        .globl  _C_LABEL(nox_flag)
        .globl  _C_LABEL(cpuid_level)
        .globl  _C_LABEL(esym)
@@ -321,6 +321,9 @@
        .globl  _C_LABEL(gdtstore)
        .globl  _C_LABEL(cputype)
 
+       .type   _C_LABEL(proc0stack), @object
+LABEL(proc0stack)      .quad   0       /* physical address of proc0's stack */
+END(proc0stack)
        .type   _C_LABEL(nox_flag), @object
 LABEL(nox_flag)                .long   0       /* 32bit NOX flag, set if supported */
 END(nox_flag)
@@ -568,20 +571,17 @@
  * be referred to as: L4 -> L3 -> L2 -> L1.
  *
  * Virtual address space of the kernel:
- * +------+--------+------+-----+--------+---------------------+----------
- * | TEXT | RODATA | DATA | BSS | [SYMS] | [PRELOADED MODULES] | L4 ->
- * +------+--------+------+-----+--------+---------------------+----------
- *                             (1)      (2)                   (3)
+ * +------+--------+------+-----+--------+---------------------+-----------+-
+ * | TEXT | RODATA | DATA | BSS | [SYMS] | [PRELOADED MODULES] | PROC0 STK |
+ * +------+--------+------+-----+--------+---------------------+-----------+-
+ *                             (1)      (2)                   (3)         (4)
  *
- * --------------+-----+-----+----+------------+
- * -> PROC0 STK -> L3 -> L2 -> L1 | ISA IO MEM |
- * --------------+-----+-----+----+------------+
- *                               (4)
+ * -----+-----+-----+----+------------+
+ *  L4 -> L3 -> L2 -> L1 | ISA IO MEM |
+ * -----+-----+-----+----+------------+
+ *                      (5)
  *
- * PROC0 STK is obviously not linked as a page level. It just happens to be
- * caught between L4 and L3.
- *
- * (PROC0 STK + L4 + L3 + L2 + L1) is later referred to as BOOTSTRAP TABLES.
+ * (L4 + L3 + L2 + L1) is later referred to as BOOTSTRAP TABLES.
  *
  * Important note: the kernel segments are properly 4k-aligned
  * (see kern.ldscript), so there's no need to enforce alignment.
@@ -607,11 +607,17 @@
        movl    %eax,%edi
 1:
 
-       /* We are on (3). Align up for BOOTSTRAP TABLES. */
+       /* We are on (3). Align up for PROC0 STK. */
        movl    %edi,%esi
        addl    $PGOFSET,%esi
        andl    $~PGOFSET,%esi
 
+       /* Save proc0's stack pa, and allocate it. Brings us on (4). */
+       movl    $RELOC(proc0stack),%ebp
+       movl    %esi,(%ebp)
+       movl    $0,4(%ebp)
+       addl    $USPACE,%esi
+
        /* We are on the BOOTSTRAP TABLES. Save L4's physical address. */
        movl    $RELOC(PDPpaddr),%ebp
        movl    %esi,(%ebp)
@@ -667,11 +673,19 @@
 
        /* Map [SYMS]+[PRELOADED MODULES] RWX. */
        movl    $RELOC(__kernel_end),%eax
+       movl    RELOC(proc0stack),%ecx  /* start of PROC0 STK */
+       subl    %eax,%ecx
+       shrl    $PGSHIFT,%ecx
+       orl     $(PG_V|PG_KW),%eax
+       fillkpt
+
+       /* Map PROC0 STK RW. */
+       movl    RELOC(proc0stack),%eax  /* start of PROC0 STK */
        movl    %esi,%ecx               /* start of BOOTSTRAP TABLES */
        subl    %eax,%ecx
        shrl    $PGSHIFT,%ecx
        orl     $(PG_V|PG_KW),%eax
-       fillkpt
+       fillkpt_nox
 
        /* Map the BOOTSTRAP TABLES RW. */
        movl    %esi,%eax               /* start of BOOTSTRAP TABLES */
@@ -680,7 +694,7 @@
        orl     $(PG_V|PG_KW),%eax
        fillkpt_nox
 
-       /* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
+       /* We are on (5). Map ISA I/O mem (later atdevbase) RWX. */
        movl    $(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
        movl    $(IOM_SIZE>>PGSHIFT),%ecx
        fillkpt
@@ -842,7 +856,7 @@
        movq    %rdx,_C_LABEL(atdevbase)(%rip)
 
        /* Set up bootstrap stack. */
-       leaq    (PROC0_STK_OFF)(%rsi),%rax
+       movq    _C_LABEL(proc0stack),%rax
        addq    %r8,%rax
        movq    %rax,(_C_LABEL(lwp0)+L_PCB)(%rip) /* XXX L_PCB != uarea */
        leaq    (USPACE-FRAMESIZE)(%rax),%rsp



Home | Main Index | Thread Index | Old Index