sweep through entire page table to free up intermediate tables
[lunaix-os.git] / lunaix-os / kernel / boot_helper.c
1 #include <klibc/string.h>
2 #include <lunaix/boot_generic.h>
3 #include <lunaix/mm/pmm.h>
4 #include <lunaix/mm/vmm.h>
5 #include <lunaix/spike.h>
6 #include <lunaix/kcmd.h>
7 #include <lunaix/sections.h>
8 #include <asm/mm_defs.h>
9
10 /**
11  * @brief Reserve memory for kernel bootstrapping initialization
12  *
13  * @param bhctx
14  */
15 void
16 boot_begin(struct boot_handoff* bhctx)
17 {
18     bhctx->prepare(bhctx);
19
20     boot_begin_arch_reserve(bhctx);
21     
22     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
23     size_t pg_count = leaf_count(to_kphysical(kernel_load_end));
24     pmm_onhold_range(0, pg_count);
25
26     size_t i;
27     struct boot_mmapent* ent;
28     for (i = 0; i < bhctx->mem.mmap_len; i++) {
29         ent = &bhctx->mem.mmap[i];
30
31         if (reserved_memregion(ent) || reclaimable_memregion(ent)) {
32             unsigned int counts = leaf_count(ent->size);
33             pmm_onhold_range(pfn(ent->start), counts);
34         }
35     }
36
37     /* Reserve region for all loaded modules */
38     for (i = 0; i < bhctx->mods.mods_num; i++) {
39         struct boot_modent* mod = &bhctx->mods.entries[i];
40         unsigned int counts = leaf_count(mod->end - mod->start);
41
42         pmm_onhold_range(pfn(mod->start), counts);
43     }
44 }
45
46 static void
47 __free_reclaimable()
48 {
49     ptr_t start;
50     pfn_t pgs;
51     pte_t* ptep;
52
53     start = reclaimable_start;
54     pgs   = leaf_count(reclaimable_end - start);
55     ptep  = mkptep_va(VMS_SELF, start);
56
57     pmm_unhold_range(pfn(to_kphysical(start)), pgs);
58     vmm_unset_ptes(ptep, pgs);
59 }
60
61 /**
62  * @brief Release memory for kernel bootstrapping initialization
63  *
64  * @param bhctx
65  */
66 void
67 boot_end(struct boot_handoff* bhctx)
68 {
69     struct boot_mmapent* ent;
70     for (size_t i = 0; i < bhctx->mem.mmap_len; i++) {
71         ent = &bhctx->mem.mmap[i];
72
73         if (reclaimable_memregion(ent)) {
74             unsigned int counts = leaf_count(ent->size);
75             pmm_unhold_range(pfn(ent->start), counts);
76         }
77     }
78
79     bhctx->release(bhctx);
80
81     boot_clean_arch_reserve(bhctx);
82
83     __free_reclaimable();
84 }
85
86 void
87 boot_parse_cmdline(struct boot_handoff* bhctx) {
88     if (!bhctx->kexec.len) {
89         return;
90     }
91
92     kcmd_parse_cmdline(bhctx->kexec.cmdline);
93 }