add generic kremap for kernel remapping
authorLunaixsky <lunaixsky@qq.com>
Sun, 29 Sep 2024 15:55:55 +0000 (16:55 +0100)
committerLunaixsky <lunaixsky@qq.com>
Sun, 29 Sep 2024 20:00:25 +0000 (21:00 +0100)
* add generic utility to allocate pagetable from static pool for initial
  mmu setup
* fix incorrect pte advancement in set_ptes_contig
* always dump the configured headers when performing `make config`
* add custom qemu trace event for logging pagefaults in x86
* refactor the generic parts of bstage.h to asm-generic/boot_stage.h
* make the rootfs accessible if `make root` is invoked directly from
  sudo

19 files changed:
lunaix-os/arch/generic/LBuild
lunaix-os/arch/generic/includes/asm-generic/boot_stage.h [moved from lunaix-os/arch/x86/includes/sys/boot/bstage.h with 76% similarity]
lunaix-os/arch/generic/includes/asm-generic/init_pagetable.h [new file with mode: 0644]
lunaix-os/arch/generic/includes/asm-generic/muldiv64.h
lunaix-os/arch/generic/kremap.c [new file with mode: 0644]
lunaix-os/arch/x86/boot/boot_helper.c
lunaix-os/arch/x86/boot/i386/boot32.S
lunaix-os/arch/x86/boot/i386/kremap32.c
lunaix-os/arch/x86/boot/kpt_setup.c
lunaix-os/arch/x86/boot/mb_parser.c
lunaix-os/arch/x86/boot/x86_64/kremap64.c
lunaix-os/arch/x86/includes/asm/boot_stage.h [new file with mode: 0644]
lunaix-os/arch/x86/includes/asm/variants/mempart32.h
lunaix-os/arch/x86/includes/sys/boot/archinit.h
lunaix-os/includes/lunaix/mm/pagetable.h
lunaix-os/includes/lunaix/mm/vmm.h
lunaix-os/scripts/build-tools/luna_build.py
lunaix-os/scripts/mkrootfs
lunaix-os/scripts/qemus/qemu_x86_dev.json

index a8cbeadca1d87fd95ce93125070eb7a11af83a82..b57fdfa00fadea0cf8115d2c1583659a7ef0814f 100644 (file)
@@ -6,4 +6,5 @@ sources([
     "vmutils.c",
     "procvm.c",
     "arch.c",
+    "kremap.c"
 ])
\ No newline at end of file
similarity index 76%
rename from lunaix-os/arch/x86/includes/sys/boot/bstage.h
rename to lunaix-os/arch/generic/includes/asm-generic/boot_stage.h
index 45275f9e458b7eb260c9efac3d54071544a70293..3f74a481319ed6a5f9a70ba71a8fa13633ba12d7 100644 (file)
@@ -1,16 +1,8 @@
-#ifndef __LUNAIX_BSTAGE_H
-#define __LUNAIX_BSTAGE_H
-#include <lunaix/types.h>
-#include <lunaix/boot_generic.h>
-
-extern ptr_t __multiboot_addr;
-
-extern u8_t __kboot_start[];
-extern u8_t __kboot_end[];
+#ifndef __LUNAIX_ARCH_GENERIC_BOOT_STAGE_H
+#define __LUNAIX_ARCH_GENERIC_BOOT_STAGE_H
 
 #define boot_text __attribute__((section(".boot.text")))
 #define boot_data __attribute__((section(".boot.data")))
-#define boot_bss  __attribute__((section(".boot.bss")))
 
 /*
     Bridge the far symbol to the vicinity.
@@ -19,7 +11,7 @@ extern u8_t __kboot_end[];
     issue where symbol define in kernel 
     code is too far away from the boot code.
 */
-#ifdef CONFIG_ARCH_X86_64
+#ifdef CONFIG_ARCH_BITS_64
 #define __bridge_farsym(far_sym)        \
     asm(                                \
         ".section .boot.data\n"         \
@@ -43,7 +35,4 @@ extern u8_t __kboot_end[];
 
 #endif
 
-ptr_t 
-remap_kernel();
-
-#endif /* __LUNAIX_BSTAGE_H */
+#endif /* __LUNAIX_ARCH_GENERIC_BOOT_STAGE_H */
diff --git a/lunaix-os/arch/generic/includes/asm-generic/init_pagetable.h b/lunaix-os/arch/generic/includes/asm-generic/init_pagetable.h
new file mode 100644 (file)
index 0000000..ae58501
--- /dev/null
@@ -0,0 +1,84 @@
+#ifndef __LUNAIX_ARCH_GENERIC_INIT_PAGETABLE_H
+#define __LUNAIX_ARCH_GENERIC_INIT_PAGETABLE_H
+
+#include <lunaix/types.h>
+#include <lunaix/mm/pagetable.h>
+
+struct pt_alloc
+{
+    ptr_t base;
+    int index;
+    int total;
+};
+
+struct ptw_state
+{
+    struct pt_alloc* alloc;
+    pte_t* root;
+    pte_t* lntp;
+    int tab_index;
+    int level;
+};
+
+/**
+ * Allocate a page table from the page-table pool
+ */
+ptr_t 
+kpt_alloc_table(struct pt_alloc* alloc);
+
+/**
+ * Set contiguous number of ptes starting from `addr`
+ * Using flattened apporach (i.e., unfold recursive)
+ */
+unsigned int 
+kpt_set_ptes_flatten(struct ptw_state* state, ptr_t addr, 
+                pte_t pte, unsigned long lsize, unsigned int nr);
+
+/**
+ * Remap the kernel to high-memory
+ */
+void 
+kpt_migrate_highmem(struct ptw_state* state);
+
+static inline void must_inline
+init_pt_alloc(struct pt_alloc* alloc, ptr_t pool, unsigned int size)
+{
+    *alloc = (struct pt_alloc) {
+        .base = pool,
+        .index = 0,
+        .total = size / PAGE_SIZE
+    };
+}
+
+static inline void must_inline
+init_ptw_state(struct ptw_state* state, struct pt_alloc* alloc, ptr_t root)
+{
+    *state = (struct ptw_state) {
+        .alloc = alloc,
+        .root  = (pte_t*) root,
+        .level = 0
+    };
+}
+
+/**
+ * set_ptes that is ensured to success
+ */
+static inline void must_inline
+kpt_set_ptes(struct ptw_state* state, ptr_t addr, 
+                pte_t pte, unsigned long lsize, unsigned int nr)
+{
+    if (kpt_set_ptes_flatten(state, addr, pte, lsize, nr) != nr) {
+        spin();
+    }
+}
+
+/**
+ *  prepare a editable page table covering va range [addr, addr + lsize)
+ */
+static inline void must_inline
+kpt_mktable_at(struct ptw_state* state, ptr_t addr, unsigned long lsize)
+{
+    kpt_set_ptes(state, addr, null_pte, lsize >> _PAGE_BASE_SHIFT, 1);
+}
+
+#endif /* __LUNAIX_ARCH_GENERIC_INIT_PAGETABLE_H */
index fefc202d33c0f5561346e9a7d4e610ed733f95c8..fbfab19709d5ce0ef90febabe8deda85924c26e2 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef __LUNAIX_ARCH_MULDIV64_H
-#define __LUNAIX_ARCH_MULDIV64_H
+#ifndef __LUNAIX_ARCH_GENERIC_MULDIV64_H
+#define __LUNAIX_ARCH_GENERIC_MULDIV64_H
 
 #include <lunaix/types.h>
 
diff --git a/lunaix-os/arch/generic/kremap.c b/lunaix-os/arch/generic/kremap.c
new file mode 100644 (file)
index 0000000..3cd6fa7
--- /dev/null
@@ -0,0 +1,147 @@
+#include <lunaix/mm/pagetable.h>
+#include <lunaix/sections.h>
+
+#include <asm/boot_stage.h>
+#include <asm/mm_defs.h>
+#include <asm-generic/init_pagetable.h>
+
+ptr_t boot_text
+kpt_alloc_table(struct pt_alloc* alloc)
+{
+    if (alloc->index >= alloc->total) {
+        return 0;
+    }
+
+    ptr_t next;
+
+    next = alloc->base + alloc->index * PAGE_SIZE;
+    alloc->index++;
+
+    return next;
+}
+
+static bool boot_text
+__walk(struct ptw_state* state, ptr_t addr, 
+       unsigned long level_size, bool create)
+{
+    pte_t pte, *pt;
+    int level, pt_index;
+    unsigned long lsize;
+    ptr_t  next_level;
+
+    pt = state->root;
+    level = 0;
+
+    do {
+        lsize    = lnt_page_size(level);
+        pt_index = va_level_index(addr, lsize);
+
+        pte = pt[pt_index];
+        if (!pte_isnull(pte)) {
+            next_level = pte_paddr(pte);
+            goto cont;
+        }
+
+        if (pt_last_level(level) || lsize == level_size) {
+            break;
+        }
+
+        if (!create) {
+            goto fail;
+        }
+
+        next_level = kpt_alloc_table(state->alloc);
+        if (!next_level) {
+            state->lntp = NULL;
+            goto fail;
+        }
+
+        pte = mkpte(next_level, KERNEL_PGTAB);
+        pt[pt_index] = pte;
+
+    cont:
+        pt = (pte_t*) next_level;
+        level++;
+    }
+    while (lsize > level_size);
+
+    state->lntp  = pt;
+    state->level = level;
+    state->tab_index = pt_index;
+
+    return true;
+
+fail:
+    state->lntp = NULL;
+    return false;
+}
+
+unsigned int boot_text
+kpt_set_ptes_flatten(struct ptw_state* state, ptr_t addr, 
+                pte_t pte, unsigned long lsize, unsigned int nr)
+{
+    unsigned int tab_i, _n;
+    pte_t *lntp;
+
+    _n = 0;
+    addr = addr & ~(lsize - 1);
+
+    do {
+        if (!__walk(state, addr, lsize, true)) {
+            break;
+        }
+
+        lntp  = state->lntp;
+        tab_i = state->tab_index;
+        while (_n < nr && tab_i < LEVEL_SIZE) {
+            lntp[tab_i++] = pte;
+            pte = pte_advance(pte, lsize);
+
+            addr += lsize;
+            _n++;
+        }
+    }
+    while (_n < nr);
+
+    return _n;
+}
+
+#define ksection_maps autogen_name(ksecmap)
+#define PF_X 0x1
+#define PF_W 0x2
+
+extern_autogen(ksecmap);
+
+bridge_farsym(__kexec_text_start);
+bridge_farsym(ksection_maps);
+
+void boot_text
+kpt_migrate_highmem(struct ptw_state* state)
+{
+    pte_t pte;
+    struct ksecmap* maps;
+    struct ksection* section;
+    pfn_t pgs;
+
+    maps = (struct ksecmap*)to_kphysical(__far(ksection_maps));
+
+    for (unsigned int i = 0; i < maps->num; i++)
+    {
+        section = &maps->secs[i];
+        
+        if (section->va < KERNEL_RESIDENT) {
+            continue;
+        }
+
+        pte = mkpte(section->pa, KERNEL_RDONLY);
+        if ((section->flags & PF_X)) {
+            pte = pte_mkexec(pte);
+        }
+        if ((section->flags & PF_W)) {
+            pte = pte_mkwritable(pte);
+        }
+
+        pgs = leaf_count(section->size);
+        kpt_set_ptes(state, section->va, pte, LFT_SIZE, pgs);
+    }
+}
\ No newline at end of file
index 88ab02592a6ffedc898be07319b788e07ef72208..2bc35a485640fd9fbd6299fad24d8ab2b47e04da 100644 (file)
@@ -5,7 +5,7 @@
 #include <lunaix/sections.h>
 
 #include <asm/mm_defs.h>
-#include <sys/boot/bstage.h>
+#include <asm/boot_stage.h>
 #include <sys-generic/bootmem.h>
 
 #ifdef CONFIG_ARCH_X86_64
index b969104abdd5f2b2d37537b0d4a0550915a9df70..e585a1beb926e492195088a0fa19a21b6ba5af8e 100644 (file)
@@ -4,7 +4,7 @@
 #include "sys/boot/multiboot.S.inc"
 #endif
 
-.section .boot.bss
+.section .boot.data
     /* 根据System V ABI,栈地址必须16字节对齐 */
     /* 这里只是一个临时栈,在_hhk_init里面我们会初始化内核专用栈 */
     .align 16
index eac579315acd89981dfb4fdbf23427240c5c32e1..5a8949ea45301a346abd3b9a0507ae0b12f9654c 100644 (file)
 #define __BOOT_CODE__
 
-#include <lunaix/mm/pagetable.h>
-#include <lunaix/compiler.h>
-#include <lunaix/sections.h>
-
-#include <sys/boot/bstage.h>
+#include <asm/boot_stage.h>
 #include <asm/mm_defs.h>
-
-#define PF_X 0x1
-#define PF_W 0x2
-#define ksection_maps autogen_name(ksecmap)
-
-extern_autogen(ksecmap);
-
-bridge_farsym(__kexec_text_start);
-bridge_farsym(ksection_maps);
+#include <asm-generic/init_pagetable.h>
 
 // define the initial page table layout
-struct kernel_map;
-
-static struct kernel_map kernel_pt __section(".kpg");
-export_symbol(debug, boot, kernel_pt);
-
 struct kernel_map 
 {
-    pte_t l0t[_PAGE_LEVEL_SIZE];
-    pte_t pg_mnt[_PAGE_LEVEL_SIZE];
-
     struct {
         pte_t _lft[_PAGE_LEVEL_SIZE];
     } kernel_lfts[16];
 } align(4);
 
-static void boot_text
+static struct kernel_map kernel_pt __section(".kpg");
+export_symbol(debug, boot, kernel_pt);
+
+static ptr_t boot_text
 do_remap()
 {
-    struct kernel_map* kpt_pa = (struct kernel_map*)to_kphysical(&kernel_pt);
-    
-    size_t mia_casa_i   = pfn_at(KERNEL_RESIDENT, L0T_SIZE);
-    pte_t* klptep       = (pte_t*) &kpt_pa->l0t[mia_casa_i];
-    pte_t* ktep         = (pte_t*)  kpt_pa->kernel_lfts;    
-    pte_t* boot_l0tep   = (pte_t*)  kpt_pa;
+    struct pt_alloc alloc;
+    struct ptw_state ptw;
+    pte_t pte;
 
-    set_pte(boot_l0tep, pte_mkhuge(mkpte_prot(KERNEL_PGTAB)));
+    init_pt_alloc(&alloc, to_kphysical(&kernel_pt), sizeof(kernel_pt));
+    init_ptw_state(&ptw, &alloc, kpt_alloc_table(&alloc));
 
-    // --- 将内核重映射至高半区 ---
+    pte = pte_mkhuge(mkpte_prot(KERNEL_DATA));
+    kpt_set_ptes(&ptw, 0, pte, L0T_SIZE, 1);
 
-    // Hook the kernel reserved LFTs onto L0T
-    pte_t pte = mkpte((ptr_t)ktep, KERNEL_PGTAB);
-    
-    for (u32_t i = 0; i < KEXEC_RSVD; i++) {
-        pte = pte_setpaddr(pte, (ptr_t)&kpt_pa->kernel_lfts[i]);
-        set_pte(klptep, pte);
+    kpt_mktable_at(&ptw, KMAP, L0T_SIZE);
+    kpt_mktable_at(&ptw, VMAP, L0T_SIZE);
 
-        klptep++;
-    }
+    kpt_migrate_highmem(&ptw);
 
-    struct ksecmap* maps;
-    struct ksection* section;
-    pfn_t pgs;
-    pte_t *kmntep;
+    pte = mkpte(__ptr(ptw.root), KERNEL_PGTAB);
+    kpt_set_ptes(&ptw, VMS_SELF, pte, L0T_SIZE, 1);
 
-    maps = (struct ksecmap*)to_kphysical(__far(ksection_maps));
-    ktep += pfn(to_kphysical(__far(__kexec_text_start)));
-
-    // Ensure the size of kernel is within the reservation
-    if (leaf_count(maps->ksize) > KEXEC_RSVD * _PAGE_LEVEL_SIZE) 
-    {
-        // ERROR: require more pages
-        //  here should do something else other than head into blocking
-        asm("ud2");
-    }
-
-    // Now, map the sections
-
-    for (unsigned int i = 0; i < maps->num; i++)
-    {
-        section = &maps->secs[i];
-
-        if (section->va < KERNEL_RESIDENT) {
-            continue;
-        }
-
-        pte = mkpte_prot(KERNEL_RDONLY);
-        if ((section->flags & PF_X)) {
-            pte = pte_mkexec(pte);
-        }
-        if ((section->flags & PF_W)) {
-            pte = pte_mkwritable(pte);
-        }
-
-        pgs = leaf_count(section->size);
-        for (pfn_t j = 0; j < pgs; j++)
-        {
-            pte = pte_setpaddr(pte, section->pa + page_addr(j));
-            set_pte(ktep, pte);
-
-            ktep++;
-        }
-    }
-
-    // set mount point
-    kmntep = (pte_t*) &kpt_pa->l0t[pfn_at(PG_MOUNT_1, L0T_SIZE)];
-    set_pte(kmntep, mkpte((ptr_t)kpt_pa->pg_mnt, KERNEL_PGTAB));
-
-    // Build up self-reference
-    int level = (VMS_SELF / L0T_SIZE) & _PAGE_LEVEL_MASK;
-    
-    pte = mkpte_root((ptr_t)kpt_pa, KERNEL_PGTAB);
-    set_pte(&boot_l0tep[level], pte);
+    return __ptr(ptw.root);
 }
 
 ptr_t boot_text
@@ -119,7 +47,5 @@ remap_kernel()
         ((u8_t*)kmap_pa)[i] = 0;
     }
 
-    do_remap();
-
-    return kmap_pa;
+    return do_remap();
 }
\ No newline at end of file
index 535ad83ca42bf3520bcbff57c97a99e013301f30..7155a15c7f64bb55e2f65c6a01e71e00c33ffc01 100644 (file)
@@ -3,7 +3,7 @@
 #include <lunaix/mm/pagetable.h>
 #include <lunaix/compiler.h>
 
-#include <sys/boot/bstage.h>
+#include <asm/boot_stage.h>
 #include <asm/mm_defs.h>
 
 
index 595b5192ffd4a4d19a81c504b1e66cff205a6a1a..14d420cb191128788925047a9b26b35f8d49bb32 100644 (file)
@@ -2,7 +2,7 @@
 
 #include <lunaix/boot_generic.h>
 
-#include <sys/boot/bstage.h>
+#include <asm/boot_stage.h>
 #include <sys/boot/multiboot.h>
 #include <sys-generic/bootmem.h>
 
index 4da66fc4f2243e211562b6cf7ed942652f3cbe37..8d32345a3aef27908d49ff55937cecce30032b57 100644 (file)
 #define __BOOT_CODE__
 
-#include <lunaix/mm/pagetable.h>
-#include <lunaix/compiler.h>
-#include <lunaix/sections.h>
-
-#include <sys/boot/bstage.h>
+#include <asm/boot_stage.h>
 #include <asm/mm_defs.h>
+#include <asm-generic/init_pagetable.h>
 
 #define RSVD_PAGES 32
 
-#define ksection_maps autogen_name(ksecmap)
-#define PF_X 0x1
-#define PF_W 0x2
-
-extern_autogen(ksecmap);
-
-bridge_farsym(__kexec_text_start);
-bridge_farsym(ksection_maps);
-
-// define the initial page table layout
-struct kernel_map;
-
-static struct kernel_map kpt __section(".kpg");
-export_symbol(debug, boot, kpt);
-
 struct kernel_map 
 {
-    pte_t l0t[_PAGE_LEVEL_SIZE];        // root table
-    pte_t l1t_rsvd[_PAGE_LEVEL_SIZE];   // 0~4G reservation
-
     struct {
         pte_t _lft[_PAGE_LEVEL_SIZE];
     } krsvd[RSVD_PAGES];
 } align(8);
 
-struct allocator
-{
-    struct kernel_map* kpt_pa;
-    int pt_usage;
-};
-
-static inline ptr_t
-alloc_rsvd_page(struct allocator* _alloc)
-{
-    if (_alloc->pt_usage >= KEXEC_RSVD) {
-        asm ("ud2");
-    }
-
-    return __ptr(&_alloc->kpt_pa->krsvd[_alloc->pt_usage++]);
-}
-
-static pte_t* boot_text
-prealloc_pt(struct allocator* _allc, ptr_t va, 
-            pte_attr_t prot, size_t to_gran) 
-{
-    int lvl_i;
-    pte_t *ptep, pte;
-    size_t gran = L0T_SIZE;
-
-    ptep = (pte_t*)&_allc->kpt_pa->l0t[0];
-
-    for (int i = 0; i < _PTW_LEVEL && gran > to_gran; i++)
-    {
-        lvl_i = va_level_index(va, gran);
-        ptep = &ptep[lvl_i];
-        pte  = pte_at(ptep);
-
-        gran = gran >> _PAGE_LEVEL_SHIFT;
-
-        if (pte_isnull(pte)) {
-            pte = mkpte(alloc_rsvd_page(_allc), KERNEL_PGTAB);
-            if (to_gran == gran) {
-                pte = pte_setprot(pte, prot);
-            }
-
-            set_pte(ptep, pte);
-        }
-        ptep = (pte_t*) pte_paddr(pte);
-    }
-
-    return ptep;
-}
+static struct kernel_map kpt __section(".kpg");
+export_symbol(debug, boot, kpt);
 
-static void boot_text
+static ptr_t boot_text
 do_remap()
 {
-    struct kernel_map* kpt_pa;
-    pte_t *boot_l0tep, *klptep, *l1_rsvd;
-    pte_t id_map, pte;
-    ptr_t kstart;
+    struct pt_alloc alloc;
+    struct ptw_state ptw;
+    pte_t pte;
 
-    unsigned int lvl_i = 0;
+    init_pt_alloc(&alloc, to_kphysical(&kpt), sizeof(kpt));
+    init_ptw_state(&ptw, &alloc, kpt_alloc_table(&alloc));
 
-    // identity map the first 4G for legacy compatibility
-    kpt_pa     = (struct kernel_map*)to_kphysical(&kpt);
-    boot_l0tep = (pte_t*) kpt_pa;
-    l1_rsvd    = (pte_t*) kpt_pa->l1t_rsvd;
-    id_map     = pte_mkhuge(mkpte_prot(KERNEL_PGTAB));
-    
-    pte = mkpte((ptr_t)l1_rsvd, KERNEL_PGTAB);
-    set_pte(boot_l0tep, pte);
-    
-    for (int i = 0; i < 4; i++, l1_rsvd++)
-    {
-        id_map = pte_setpaddr(id_map, (ptr_t)i << 30);
-        set_pte(l1_rsvd, id_map);
-    }
+    pte = pte_mkhuge(mkpte_prot(KERNEL_PGTAB));
+    kpt_set_ptes(&ptw, 0, pte, L1T_SIZE, 4);
 
-    // Remap the kernel to -2GiB
-
-    struct allocator alloc = {
-        .kpt_pa = kpt_pa,
-        .pt_usage = 0
-    };
-
-    prealloc_pt(&alloc, VMAP, KERNEL_PGTAB, L1T_SIZE);
-    prealloc_pt(&alloc, PG_MOUNT_1, KERNEL_PGTAB, LFT_SIZE);
-
-    kstart = page_aligned(__far(__kexec_text_start));
+    kpt_mktable_at(&ptw, VMAP, L0T_SIZE);
 
 #if LnT_ENABLED(3)
     size_t gran  = L3T_SIZE;
@@ -125,81 +37,25 @@ do_remap()
     size_t gran  = L2T_SIZE;
 #endif
 
-    prealloc_pt(&alloc, PMAP, KERNEL_PGTAB, gran);
-    klptep = prealloc_pt(&alloc, kstart, KERNEL_PGTAB, gran);
-    klptep += va_level_index(kstart, gran);
-
-    pte = mkpte(0, KERNEL_PGTAB);
-    for (int i = alloc.pt_usage; i < KEXEC_RSVD; i++)
-    {
-        pte = pte_setpaddr(pte, (ptr_t)&kpt_pa->krsvd[i]);
-        set_pte(klptep++, pte);
-    }
-
-    struct ksecmap* maps;
-    struct ksection* section;
-    pfn_t pgs;
+    kpt_mktable_at(&ptw, KMAP, gran);
+    kpt_mktable_at(&ptw, PMAP, gran);
 
-    maps = (struct ksecmap*)to_kphysical(__far(ksection_maps));
+    kpt_migrate_highmem(&ptw);
 
-    // this is the first LFT we hooked on.
-    // all these LFT are contig in physical address 
-    klptep = (pte_t*) &kpt_pa->krsvd[alloc.pt_usage];
-    klptep += pfn(to_kphysical(kstart));
+    pte = mkpte(__ptr(ptw.root), KERNEL_PGTAB);
+    kpt_set_ptes(&ptw, VMS_SELF, pte, L0T_SIZE, 1);
 
-    // Ensure the size of kernel is within the reservation
-    int remain = KEXEC_RSVD - alloc.pt_usage;
-    if (leaf_count(maps->ksize) > remain * _PAGE_LEVEL_SIZE) 
-    {
-        asm("ud2");
-    }
-
-    // assume contig kernel vaddrs
-    for (unsigned int i = 0; i < maps->num; i++)
-    {
-        section = &maps->secs[i];
-
-        if (section->va < KERNEL_RESIDENT) {
-            continue;
-        }
-
-        pte = mkpte_prot(KERNEL_RDONLY);
-        if ((section->flags & PF_X)) {
-            pte = pte_mkexec(pte);
-        }
-        if ((section->flags & PF_W)) {
-            pte = pte_mkwritable(pte);
-        }
-
-        pgs = leaf_count(section->size);
-        for (pfn_t j = 0; j < pgs; j++)
-        {
-            pte = pte_setpaddr(pte, section->pa + page_addr(j));
-            set_pte(klptep, pte);
-
-            klptep++;
-        }
-    }
-
-    // Build up self-reference
-    lvl_i = va_level_index(VMS_SELF, L0T_SIZE);
-    pte   = mkpte_root(__ptr(kpt_pa), KERNEL_PGTAB);
-    set_pte(boot_l0tep + lvl_i, pte);
+    return __ptr(ptw.root);
 }
 
-
 ptr_t boot_text
 remap_kernel()
-{
-    ptr_t kmap_pa = to_kphysical(&kpt);
-    
+{    
     asm volatile("movq %1, %%rdi\n"
                  "rep stosb\n" ::"c"(sizeof(kpt)),
-                 "r"(kmap_pa),
+                 "r"(to_kphysical(&kpt)),
                  "a"(0)
                  : "rdi", "memory");
 
-    do_remap();
-
-    return kmap_pa;
+    return do_remap();
 }
\ No newline at end of file
diff --git a/lunaix-os/arch/x86/includes/asm/boot_stage.h b/lunaix-os/arch/x86/includes/asm/boot_stage.h
new file mode 100644 (file)
index 0000000..03a1bc5
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef __LUNAIX_ARCH_BOOT_STAGE_H
+#define __LUNAIX_ARCH_BOOT_STAGE_H
+#include <lunaix/types.h>
+#include <lunaix/boot_generic.h>
+
+extern ptr_t __multiboot_addr;
+
+extern u8_t __kboot_start[];
+extern u8_t __kboot_end[];
+
+#include <asm-generic/boot_stage.h>
+
+ptr_t 
+remap_kernel();
+
+#endif /* __LUNAIX_ARCH_BOOT_STAGE_H */
index c741b69497ec9895de4e615ff3ee608a6a32454e..cc34086ed40d3db22fe311a4fcf604daec4d0820 100644 (file)
@@ -32,7 +32,8 @@
 #define KERNEL_IMG_SIZE             __ulong(0x4000000)
 #define KERNEL_IMG_END END_POINT(KERNEL_IMG)
 
-#define PG_MOUNT_1                  __ulong(0xc8000000)
+#define KMAP                        __ulong(0xc8000000)
+#define PG_MOUNT_1                  KMAP
 #define PG_MOUNT_1_SIZE             __ulong(0x1000)
 #define PG_MOUNT_1_END END_POINT(PG_MOUNT_1)
 
index 5ac58ca7cc242337dbbf438ec67e8be4495e684e..86fa1516db8ec4160358a02172be7e249dbba3e6 100644 (file)
@@ -2,7 +2,7 @@
 #define __LUNAIX_ARCHINIT_H
 
 #include <lunaix/types.h>
-#include "bstage.h"
+#include <asm/boot_stage.h>
 #include "multiboot.h"
 
 ptr_t boot_text
index 79244eb38cd2b9d519b5aeb7d68a115a1804f4e2..7093d669ec0a6dc5d5a19a165c36f378b7072930 100644 (file)
@@ -646,4 +646,10 @@ ptep_count_level(pte_t* ptep)
     return 0;
 }
 
+static inline pte_t must_inline
+pte_advance(pte_t pte, unsigned long lvl_size)
+{
+    return pte_setpaddr(pte, pte_paddr(pte) + lvl_size);
+}
+
 #endif /* __LUNAIX_PAGETABLE_H */
index a5bde364761c53c1d5441ead8805a1b05fa156b7..1d19d118f7f32c4e8b02c83bc998eea2645b40c3 100644 (file)
@@ -11,7 +11,7 @@ vmm_set_ptes_contig(pte_t* ptep, pte_t pte, size_t lvl_size, size_t n)
 {
     do {
         set_pte(ptep, pte);
-        pte_val(pte) += lvl_size;
+        pte = pte_advance(pte, lvl_size);
         ptep++;
     } while (--n > 0);
 }
index ce3be825f5b9779cf1a188d62d51634d0dbb93cc..6b8e8979eaad764bb8a13cc10124dc6ec57c4f4f 100755 (executable)
@@ -114,8 +114,8 @@ def main():
         lcfg_env.update()
         lcfg_env.save(opts.config_save)
         lcfg_env.export()
-    else:
-        do_buildfile_gen(opts, lcfg_env)
+
+    do_buildfile_gen(opts, lcfg_env)
 
 if __name__ == "__main__":
     main()
\ No newline at end of file
index 4178839469ada3181a72711a29216941cccb1d0f..5f05f012360f7d6b11b0f53129577adbcfa2bf74 100755 (executable)
@@ -78,6 +78,8 @@ ${prefix} umount "${tmp_mnt}" || cleanup
 
 ${prefix} rm -d "${tmp_mnt}" || cleanup
 
+${prefix} chmod o+rw ${rootfs} || cleanup
+
 if [ ! "${has_err:-0}" -eq 0  ]; then
     echo "done, but with error."
 else
index 8f39682ce1904392767c7fd199d195679eae3562..f52ed94e69afaa7323241f581bdc32085bc1b55e 100644 (file)
@@ -27,6 +27,7 @@
         "gdb_port": "$GDB_PORT",
         "traced": [
             "x86_recv_fault",
+            "x86_log_pagefault",
             "ide_dma_cb"
         ]
     },