1 #include <klibc/string.h>
2 #include <lunaix/boot_generic.h>
3 #include <lunaix/mm/page.h>
4 #include <lunaix/mm/pmm.h>
5 #include <lunaix/mm/vmm.h>
6 #include <lunaix/spike.h>
7 #include <lunaix/kcmd.h>
8 #include <sys/mm/mempart.h>
11 * @brief Reserve memory for kernel bootstrapping initialization
16 boot_begin(struct boot_handoff* bhctx)
18 bhctx->prepare(bhctx);
20 struct boot_mmapent *mmap = bhctx->mem.mmap, *mmapent;
21 for (size_t i = 0; i < bhctx->mem.mmap_len; i++) {
23 size_t size_pg = PN(ROUNDUP(mmapent->size, PG_SIZE));
25 if (mmapent->type == BOOT_MMAP_FREE) {
26 pmm_mark_chunk_free(PN(mmapent->start), size_pg);
30 ptr_t pa = PG_ALIGN(mmapent->start);
31 for (size_t j = 0; j < size_pg && pa < KERNEL_EXEC;
32 j++, pa += PM_PAGE_SIZE) {
33 vmm_set_mapping(VMS_SELF, pa, pa, PG_PREM_RW, VMAP_IGNORE);
37 /* Reserve region for all loaded modules */
38 for (size_t i = 0; i < bhctx->mods.mods_num; i++) {
39 struct boot_modent* mod = &bhctx->mods.entries[i];
40 pmm_mark_chunk_occupied(PN(mod->start),
41 CEIL(mod->end - mod->start, PG_SIZE_BITS),
46 extern u8_t __kexec_boot_end; /* link/linker.ld */
49 * @brief Release memory for kernel bootstrapping initialization
54 boot_end(struct boot_handoff* bhctx)
56 struct boot_mmapent *mmap = bhctx->mem.mmap, *mmapent;
57 for (size_t i = 0; i < bhctx->mem.mmap_len; i++) {
59 size_t size_pg = PN(ROUNDUP(mmapent->size, PG_SIZE));
61 if (mmapent->start >= KERNEL_EXEC || mmapent->type == BOOT_MMAP_FREE) {
65 if (mmapent->type == BOOT_MMAP_RCLM) {
66 pmm_mark_chunk_free(PN(mmapent->start), size_pg);
69 ptr_t pa = PG_ALIGN(mmapent->start);
70 for (size_t j = 0; j < size_pg && pa < KERNEL_EXEC;
71 j++, pa += PM_PAGE_SIZE) {
72 vmm_del_mapping(VMS_SELF, pa);
76 bhctx->release(bhctx);
80 * @brief Clean up the boot stage code and data
87 for (size_t i = 0; i < (ptr_t)(&__kexec_boot_end); i += PG_SIZE) {
88 vmm_del_mapping(VMS_SELF, (ptr_t)i);
89 pmm_free_page((ptr_t)i);
94 boot_parse_cmdline(struct boot_handoff* bhctx) {
95 if (!bhctx->kexec.len) {
99 kcmd_parse_cmdline(bhctx->kexec.cmdline);