X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/64e5fa9a495e388c922157b9a616204c299f5e05..6c506d8916fb114675e93d0e2cb20831d4022294:/lunaix-os/kernel/boot_helper.c diff --git a/lunaix-os/kernel/boot_helper.c b/lunaix-os/kernel/boot_helper.c index d50e471..7f324e4 100644 --- a/lunaix-os/kernel/boot_helper.c +++ b/lunaix-os/kernel/boot_helper.c @@ -1,11 +1,12 @@ #include #include -#include #include #include #include #include -#include +#include + +extern unsigned char __kexec_end[], __kexec_start[]; /** * @brief Reserve memory for kernel bootstrapping initialization @@ -16,35 +17,39 @@ void boot_begin(struct boot_handoff* bhctx) { bhctx->prepare(bhctx); + + // Identity-map the first 3GiB address spaces + pte_t* ptep = mkl0tep(mkptep_va(VMS_SELF, 0)); + pte_t pte = mkpte_prot(KERNEL_DATA); + size_t count = page_count(KERNEL_RESIDENT, L0T_SIZE); - struct boot_mmapent *mmap = bhctx->mem.mmap, *mmapent; - for (size_t i = 0; i < bhctx->mem.mmap_len; i++) { - mmapent = &mmap[i]; - size_t size_pg = PN(ROUNDUP(mmapent->size, PG_SIZE)); + vmm_set_ptes_contig(ptep, pte_mkhuge(pte), L0T_SIZE, count); - if (mmapent->type == BOOT_MMAP_FREE) { - pmm_mark_chunk_free(PN(mmapent->start), size_pg); - continue; - } + // 将内核占据的页,包括前1MB,hhk_init 设为已占用 + size_t pg_count = leaf_count(to_kphysical(__kexec_end)); + pmm_onhold_range(0, pg_count); - ptr_t pa = PG_ALIGN(mmapent->start); - for (size_t j = 0; j < size_pg && pa < KERNEL_EXEC; - j++, pa += PM_PAGE_SIZE) { - vmm_set_mapping(VMS_SELF, pa, pa, PG_PREM_RW, VMAP_IGNORE); + size_t i; + struct boot_mmapent* ent; + for (i = 0; i < bhctx->mem.mmap_len; i++) { + ent = &bhctx->mem.mmap[i]; + + if (reserved_memregion(ent) || reclaimable_memregion(ent)) { + unsigned int counts = leaf_count(ent->size); + pmm_onhold_range(pfn(ent->start), counts); } } /* Reserve region for all loaded modules */ - for (size_t i = 0; i < bhctx->mods.mods_num; i++) { + for (i = 0; i < bhctx->mods.mods_num; i++) { struct boot_modent* mod = &bhctx->mods.entries[i]; - pmm_mark_chunk_occupied(KERNEL_PID, - PN(mod->start), - CEIL(mod->end - mod->start, PG_SIZE_BITS), - PP_FGLOCKED); + unsigned int counts = leaf_count(mod->end - mod->start); + + pmm_onhold_range(pfn(mod->start), counts); } } -extern u8_t __kexec_boot_end; /* link/linker.ld */ +extern u8_t __kboot_end; /* link/linker.ld */ /** * @brief Release memory for kernel bootstrapping initialization @@ -54,23 +59,13 @@ extern u8_t __kexec_boot_end; /* link/linker.ld */ void boot_end(struct boot_handoff* bhctx) { - struct boot_mmapent *mmap = bhctx->mem.mmap, *mmapent; + struct boot_mmapent* ent; for (size_t i = 0; i < bhctx->mem.mmap_len; i++) { - mmapent = &mmap[i]; - size_t size_pg = PN(ROUNDUP(mmapent->size, PG_SIZE)); + ent = &bhctx->mem.mmap[i]; - if (mmapent->start >= KERNEL_EXEC || mmapent->type == BOOT_MMAP_FREE) { - continue; - } - - if (mmapent->type == BOOT_MMAP_RCLM) { - pmm_mark_chunk_free(PN(mmapent->start), size_pg); - } - - ptr_t pa = PG_ALIGN(mmapent->start); - for (size_t j = 0; j < size_pg && pa < KERNEL_EXEC; - j++, pa += PM_PAGE_SIZE) { - vmm_del_mapping(VMS_SELF, pa); + if (reclaimable_memregion(ent)) { + unsigned int counts = leaf_count(ent->size); + pmm_unhold_range(pfn(ent->start), counts); } } @@ -84,11 +79,9 @@ boot_end(struct boot_handoff* bhctx) void boot_cleanup() { - // clean up - for (size_t i = 0; i < (ptr_t)(&__kexec_boot_end); i += PG_SIZE) { - vmm_del_mapping(VMS_SELF, (ptr_t)i); - pmm_free_page(KERNEL_PID, (ptr_t)i); - } + pte_t* ptep = mkl0tep(mkptep_va(VMS_SELF, 0)); + size_t count = page_count(KERNEL_RESIDENT, L0T_SIZE); + vmm_unset_ptes(ptep, count); } void