1 #include <arch/x86/boot/multiboot.h>
2 #include <lunaix/common.h>
3 #include <lunaix/lunistd.h>
4 #include <lunaix/lxconsole.h>
5 #include <lunaix/mm/cake.h>
6 #include <lunaix/mm/pmm.h>
7 #include <lunaix/mm/valloc.h>
8 #include <lunaix/mm/vmm.h>
9 #include <lunaix/peripheral/ps2kbd.h>
10 #include <lunaix/proc.h>
11 #include <lunaix/spike.h>
12 #include <lunaix/syscall.h>
13 #include <lunaix/syslog.h>
16 #include <hal/acpi/acpi.h>
19 #include <hal/ioapic.h>
25 _lxinit_main(); /* lxinit.c */
31 lock_reserved_memory();
34 unlock_reserved_memory();
37 __do_reserved_memory(int unlock);
62 #elif defined DEMO_SIGNAL
77 * @brief LunaixOS的零号进程,该进程永远为可执行。
79 * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
81 * 同时,该进程也负责fork出我们的init进程。
89 init_proc_user_space(__current);
91 asm volatile("movw %0, %%ax\n"
100 "retf" ::"i"(UDATA_SEG),
101 "i"(USTACK_TOP & ~0xf),
107 extern uint8_t __kernel_start; /* link/linker.ld */
108 extern uint8_t __kernel_end; /* link/linker.ld */
109 extern uint8_t __init_hhk_end; /* link/linker.ld */
110 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
115 // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
116 lock_reserved_memory();
120 assert_msg(kalloc_init(), "Fail to initialize heap");
123 acpi_init(_k_init_mb_info);
126 timer_init(SYS_TIMER_FREQUENCY_HZ);
137 console_start_flushing();
140 unlock_reserved_memory();
142 for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
143 vmm_del_mapping(PD_REFERENCED, (void*)i);
144 pmm_free_page(KERNEL_PID, (void*)i);
149 lock_reserved_memory()
151 __do_reserved_memory(0);
155 unlock_reserved_memory()
157 __do_reserved_memory(1);
161 __do_reserved_memory(int unlock)
163 multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
165 _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
166 // v_mapping mapping;
167 for (unsigned int i = 0; i < map_size; i++) {
168 multiboot_memory_map_t mmap = mmaps[i];
169 uint8_t* pa = PG_ALIGN(mmap.addr_low);
170 if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
171 // Don't fuck up our kernel code or any free area!
174 size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
177 for (; j < pg_num; j++) {
178 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
179 if (_pa >= KERNEL_MM_BASE) {
180 // Don't fuck up our kernel space!
183 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
184 pmm_mark_page_occupied(
185 KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
187 // Save the progress for later unmapping.
188 mmaps[i].len_low = j * PG_SIZE;
190 for (; j < pg_num; j++) {
191 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
192 vmm_del_mapping(PD_REFERENCED, _pa);
193 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
194 pmm_mark_page_free(_pa >> PG_SIZE_BITS);