optimize the menuconfig redrawing
[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 <sys/mm/mm_defs.h>
8
9 extern unsigned char __kexec_end[], __kexec_start[];
10
11 /**
12  * @brief Reserve memory for kernel bootstrapping initialization
13  *
14  * @param bhctx
15  */
16 void
17 boot_begin(struct boot_handoff* bhctx)
18 {
19     bhctx->prepare(bhctx);
20
21     boot_begin_arch_reserve(bhctx);
22     
23     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
24     size_t pg_count = leaf_count(to_kphysical(__kexec_end));
25     pmm_onhold_range(0, pg_count);
26
27     size_t i;
28     struct boot_mmapent* ent;
29     for (i = 0; i < bhctx->mem.mmap_len; i++) {
30         ent = &bhctx->mem.mmap[i];
31
32         if (reserved_memregion(ent) || reclaimable_memregion(ent)) {
33             unsigned int counts = leaf_count(ent->size);
34             pmm_onhold_range(pfn(ent->start), counts);
35         }
36     }
37
38     /* Reserve region for all loaded modules */
39     for (i = 0; i < bhctx->mods.mods_num; i++) {
40         struct boot_modent* mod = &bhctx->mods.entries[i];
41         unsigned int counts = leaf_count(mod->end - mod->start);
42
43         pmm_onhold_range(pfn(mod->start), counts);
44     }
45 }
46
47 extern u8_t __kboot_end; /* link/linker.ld */
48
49 /**
50  * @brief Release memory for kernel bootstrapping initialization
51  *
52  * @param bhctx
53  */
54 void
55 boot_end(struct boot_handoff* bhctx)
56 {
57     struct boot_mmapent* ent;
58     for (size_t i = 0; i < bhctx->mem.mmap_len; i++) {
59         ent = &bhctx->mem.mmap[i];
60
61         if (reclaimable_memregion(ent)) {
62             unsigned int counts = leaf_count(ent->size);
63             pmm_unhold_range(pfn(ent->start), counts);
64         }
65     }
66
67     bhctx->release(bhctx);
68
69     boot_clean_arch_reserve(bhctx);
70 }
71
72 void
73 boot_parse_cmdline(struct boot_handoff* bhctx) {
74     if (!bhctx->kexec.len) {
75         return;
76     }
77
78     kcmd_parse_cmdline(bhctx->kexec.cmdline);
79 }