Source-Changes-HG archive

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

[src/trunk]: src/sys/arch/amd64/stand/prekern Clean up and add some ASSERTs.



details:   https://anonhg.NetBSD.org/src/rev/21264db14279
branches:  trunk
changeset: 827985:21264db14279
user:      maxv <maxv%NetBSD.org@localhost>
date:      Tue Nov 21 07:56:05 2017 +0000

description:
Clean up and add some ASSERTs.

diffstat:

 sys/arch/amd64/stand/prekern/elf.c     |  19 +++++-
 sys/arch/amd64/stand/prekern/mm.c      |  92 ++++++++++++++-------------------
 sys/arch/amd64/stand/prekern/prekern.h |   3 +-
 3 files changed, 55 insertions(+), 59 deletions(-)

diffs (235 lines):

diff -r 8698e643b3d9 -r 21264db14279 sys/arch/amd64/stand/prekern/elf.c
--- a/sys/arch/amd64/stand/prekern/elf.c        Tue Nov 21 07:48:07 2017 +0000
+++ b/sys/arch/amd64/stand/prekern/elf.c        Tue Nov 21 07:56:05 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: elf.c,v 1.16 2017/11/17 07:07:52 maxv Exp $    */
+/*     $NetBSD: elf.c,v 1.17 2017/11/21 07:56:05 maxv Exp $    */
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -347,6 +347,9 @@
        if (i == eif.ehdr->e_shnum) {
                fatal("elf_build_boot: symtab not found");
        }
+       if (eif.shdr[i].sh_offset == 0) {
+               fatal("elf_build_boot: symtab not loaded");
+       }
        eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
        eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
 
@@ -358,6 +361,9 @@
        if (eif.shdr[j].sh_type != SHT_STRTAB) {
                fatal("elf_build_boot: wrong strtab type");
        }
+       if (eif.shdr[j].sh_offset == 0) {
+               fatal("elf_build_boot: strtab not loaded");
+       }
        eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
        eif.strsz = eif.shdr[j].sh_size;
 }
@@ -380,6 +386,7 @@
                    eif.shdr[i].sh_type != SHT_PROGBITS) {
                        continue;
                }
+               ASSERT(eif.shdr[i].sh_offset != 0);
                secva = baseva + eif.shdr[i].sh_offset;
                for (j = 0; j < eif.symcnt; j++) {
                        sym = &eif.symtab[j];
@@ -400,9 +407,10 @@
                size_t secidx, nrel;
                uintptr_t base;
 
-               if (eif.shdr[i].sh_type != SHT_REL)
+               if (eif.shdr[i].sh_type != SHT_REL) {
                        continue;
-
+               }
+               ASSERT(eif.shdr[i].sh_offset != 0);
                reltab = (Elf_Rel *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
                nrel = eif.shdr[i].sh_size / sizeof(Elf_Rel);
 
@@ -428,9 +436,10 @@
                size_t secidx, nrela;
                uintptr_t base;
 
-               if (eif.shdr[i].sh_type != SHT_RELA)
+               if (eif.shdr[i].sh_type != SHT_RELA) {
                        continue;
-
+               }
+               ASSERT(eif.shdr[i].sh_offset != 0);
                relatab = (Elf_Rela *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
                nrela = eif.shdr[i].sh_size / sizeof(Elf_Rela);
 
diff -r 8698e643b3d9 -r 21264db14279 sys/arch/amd64/stand/prekern/mm.c
--- a/sys/arch/amd64/stand/prekern/mm.c Tue Nov 21 07:48:07 2017 +0000
+++ b/sys/arch/amd64/stand/prekern/mm.c Tue Nov 21 07:56:05 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mm.c,v 1.17 2017/11/15 20:45:16 maxv Exp $     */
+/*     $NetBSD: mm.c,v 1.18 2017/11/21 07:56:05 maxv Exp $     */
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -36,7 +36,7 @@
 
 #define ELFROUND       64
 
-static const int pads[4] = {
+static const uint8_t pads[4] = {
        [BTSEG_NONE] = 0x00,
        [BTSEG_TEXT] = 0xCC,
        [BTSEG_RODATA] = 0x00,
@@ -107,12 +107,6 @@
        return ((pte & PG_V) != 0);
 }
 
-paddr_t
-mm_vatopa(vaddr_t va)
-{
-       return (PTE_BASE[pl1_i(va)] & PG_FRAME);
-}
-
 static void
 mm_mprotect(vaddr_t startva, size_t size, pte_prot_t prot)
 {
@@ -169,9 +163,7 @@
        size_t L4e_idx, L3e_idx, L2e_idx;
        paddr_t pa;
 
-       /*
-        * Build L4.
-        */
+       /* Build L4. */
        L4e_idx = pl4_i(startva);
        nL4e = mm_nentries_range(startva, endva, NBPD_L4);
        ASSERT(L4e_idx == 511);
@@ -181,9 +173,7 @@
                L4_BASE[L4e_idx] = pa | PG_V | PG_RW;
        }
 
-       /*
-        * Build L3.
-        */
+       /* Build L3. */
        L3e_idx = pl3_i(startva);
        nL3e = mm_nentries_range(startva, endva, NBPD_L3);
        for (i = 0; i < nL3e; i++) {
@@ -194,9 +184,7 @@
                L3_BASE[L3e_idx+i] = pa | PG_V | PG_RW;
        }
 
-       /*
-        * Build L2.
-        */
+       /* Build L2. */
        L2e_idx = pl2_i(startva);
        nL2e = mm_nentries_range(startva, endva, NBPD_L2);
        for (i = 0; i < nL2e; i++) {
@@ -215,39 +203,6 @@
        return rdtsc();
 }
 
-static void
-mm_map_head(void)
-{
-       size_t i, npages, size;
-       uint64_t rnd;
-       vaddr_t randva;
-
-       /*
-        * To get the size of the head, we give a look at the read-only
-        * mapping of the kernel we created in locore. We're identity mapped,
-        * so kernpa = kernva.
-        */
-       size = elf_get_head_size((vaddr_t)kernpa_start);
-       npages = size / PAGE_SIZE;
-
-       rnd = mm_rand_num64();
-       randva = rounddown(HEAD_WINDOW_BASE + rnd % (HEAD_WINDOW_SIZE - size),
-           PAGE_SIZE);
-       mm_map_tree(randva, randva + size);
-
-       /* Enter the area and build the ELF info */
-       for (i = 0; i < npages; i++) {
-               mm_enter_pa(kernpa_start + i * PAGE_SIZE,
-                   randva + i * PAGE_SIZE, MM_PROT_READ|MM_PROT_WRITE);
-       }
-       elf_build_head(randva);
-
-       /* Register the values in bootspace */
-       bootspace.head.va = randva;
-       bootspace.head.pa = kernpa_start;
-       bootspace.head.sz = size;
-}
-
 static vaddr_t
 mm_randva_kregion(size_t size, size_t pagesz)
 {
@@ -352,6 +307,39 @@
        return offset;
 }
 
+static void
+mm_map_head(void)
+{
+       size_t i, npages, size;
+       uint64_t rnd;
+       vaddr_t randva;
+
+       /*
+        * To get the size of the head, we give a look at the read-only
+        * mapping of the kernel we created in locore. We're identity mapped,
+        * so kernpa = kernva.
+        */
+       size = elf_get_head_size((vaddr_t)kernpa_start);
+       npages = size / PAGE_SIZE;
+
+       rnd = mm_rand_num64();
+       randva = rounddown(HEAD_WINDOW_BASE + rnd % (HEAD_WINDOW_SIZE - size),
+           PAGE_SIZE);
+       mm_map_tree(randva, randva + size);
+
+       /* Enter the area and build the ELF info */
+       for (i = 0; i < npages; i++) {
+               mm_enter_pa(kernpa_start + i * PAGE_SIZE,
+                   randva + i * PAGE_SIZE, MM_PROT_READ|MM_PROT_WRITE);
+       }
+       elf_build_head(randva);
+
+       /* Register the values in bootspace */
+       bootspace.head.va = randva;
+       bootspace.head.pa = kernpa_start;
+       bootspace.head.sz = size;
+}
+
 vaddr_t
 mm_map_segment(int segtype, paddr_t pa, size_t elfsz, size_t elfalign)
 {
@@ -434,8 +422,8 @@
 }
 
 /*
- * There are five independent regions: head, text, rodata, data, boot. They are
- * all mapped at random VAs.
+ * There is a variable number of independent regions: one head, several kernel
+ * segments, one boot. They are all mapped at random VAs.
  *
  * Head contains the ELF Header and ELF Section Headers, and we use them to
  * map the rest of the regions. Head must be placed in memory *before* the
diff -r 8698e643b3d9 -r 21264db14279 sys/arch/amd64/stand/prekern/prekern.h
--- a/sys/arch/amd64/stand/prekern/prekern.h    Tue Nov 21 07:48:07 2017 +0000
+++ b/sys/arch/amd64/stand/prekern/prekern.h    Tue Nov 21 07:56:05 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: prekern.h,v 1.15 2017/11/15 20:45:16 maxv Exp $        */
+/*     $NetBSD: prekern.h,v 1.16 2017/11/21 07:56:05 maxv Exp $        */
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -104,7 +104,6 @@
 
 /* mm.c */
 void mm_init(paddr_t);
-paddr_t mm_vatopa(vaddr_t);
 void mm_bootspace_mprotect(void);
 vaddr_t mm_map_segment(int, paddr_t, size_t, size_t);
 void mm_map_kernel(void);



Home | Main Index | Thread Index | Old Index