1 #include <lunaix/block.h>
2 #include <lunaix/common.h>
3 #include <lunaix/exec.h>
4 #include <lunaix/foptions.h>
6 #include <lunaix/fs/probe_boot.h>
7 #include <lunaix/fs/twifs.h>
8 #include <lunaix/lxconsole.h>
9 #include <lunaix/mm/cake.h>
10 #include <lunaix/mm/pmm.h>
11 #include <lunaix/mm/valloc.h>
12 #include <lunaix/mm/vmm.h>
13 #include <lunaix/peripheral/ps2kbd.h>
14 #include <lunaix/peripheral/serial.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/syscall.h>
17 #include <lunaix/syslog.h>
18 #include <lunaix/types.h>
20 #include <sdbg/protocol.h>
22 #include <hal/acpi/acpi.h>
23 #include <hal/ahci/ahci.h>
25 #include <hal/ioapic.h>
29 #include <arch/x86/boot/multiboot.h>
31 #include <klibc/string.h>
39 lock_reserved_memory();
42 unlock_reserved_memory();
45 __do_reserved_memory(int unlock);
50 struct v_dnode* dnode;
52 struct device* dev = probe_boot_medium();
54 kprintf(KERROR "fail to acquire device. (%d)", errno);
58 if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) {
59 kprintf(KERROR "fail to mount boot medium. (%d)", errno);
71 if (exec_kexecve("/mnt/lunaix-os/usr/init", NULL, NULL)) {
75 fail("should not reach");
78 kprintf(KERROR "fail to load initd. (%d)", errno);
83 * @brief LunaixOS的零号进程,该进程永远为可执行。
85 * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
87 * 同时,该进程也负责fork出我们的init进程。
95 init_proc_user_space(__current);
97 if (!mount_bootmedium() || !exec_initd()) {
105 extern uint8_t __kernel_start; /* link/linker.ld */
106 extern uint8_t __kernel_end; /* link/linker.ld */
107 extern uint8_t __init_hhk_end; /* link/linker.ld */
108 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
113 kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
117 // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
118 lock_reserved_memory();
121 acpi_init(_k_init_mb_info);
133 timer_init(SYS_TIMER_FREQUENCY_HZ);
136 // peripherals & chipset features
144 console_start_flushing();
147 // expose cake allocator states to vfs
150 unlock_reserved_memory();
153 for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
154 vmm_del_mapping(VMS_SELF, (void*)i);
155 pmm_free_page(KERNEL_PID, (void*)i);
160 lock_reserved_memory()
162 __do_reserved_memory(0);
166 unlock_reserved_memory()
168 __do_reserved_memory(1);
172 __do_reserved_memory(int unlock)
174 multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
176 _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
177 // v_mapping mapping;
178 for (unsigned int i = 0; i < map_size; i++) {
179 multiboot_memory_map_t mmap = mmaps[i];
180 uint8_t* pa = PG_ALIGN(mmap.addr_low);
181 if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
182 // Don't fuck up our kernel code or any free area!
185 size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
188 kprintf("mem: freeze: %p..%p type=%x\n",
190 pa + pg_num * PG_SIZE,
192 for (; j < pg_num; j++) {
193 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
194 if (_pa >= KERNEL_MM_BASE) {
195 // Don't fuck up our kernel space!
198 vmm_set_mapping(VMS_SELF, _pa, _pa, PG_PREM_R, VMAP_NULL);
199 pmm_mark_page_occupied(
200 KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
202 // Save the progress for later unmapping.
203 mmaps[i].len_low = j * PG_SIZE;
205 kprintf("mem: reclaim: %p..%p type=%x\n",
207 pa + pg_num * PG_SIZE,
209 for (; j < pg_num; j++) {
210 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
211 vmm_del_mapping(VMS_SELF, _pa);
212 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
213 pmm_mark_page_free(_pa >> PG_SIZE_BITS);