1 #include <lunaix/block.h>
2 #include <lunaix/common.h>
3 #include <lunaix/fctrl.h>
4 #include <lunaix/foptions.h>
6 #include <lunaix/fs/twifs.h>
7 #include <lunaix/lunaix.h>
8 #include <lunaix/lunistd.h>
9 #include <lunaix/lxconsole.h>
10 #include <lunaix/mm/cake.h>
11 #include <lunaix/mm/pmm.h>
12 #include <lunaix/mm/valloc.h>
13 #include <lunaix/mm/vmm.h>
14 #include <lunaix/peripheral/ps2kbd.h>
15 #include <lunaix/peripheral/serial.h>
16 #include <lunaix/spike.h>
17 #include <lunaix/syscall.h>
18 #include <lunaix/syslog.h>
19 #include <lunaix/types.h>
21 #include <sdbg/protocol.h>
23 #include <hal/acpi/acpi.h>
24 #include <hal/ahci/ahci.h>
26 #include <hal/ioapic.h>
30 #include <arch/x86/boot/multiboot.h>
32 #include <klibc/string.h>
34 #include <ulibc/stdio.h>
39 _lxinit_main(); /* lxinit.c */
45 lock_reserved_memory();
48 unlock_reserved_memory();
51 __do_reserved_memory(int unlock);
54 // #define DEMO_SIGNAL
55 // #define DEMO_READDIR
56 // #define DEMO_IOTEST
57 // #define DEMO_INPUT_TEST
58 #define DEMO_SIMPLE_SH
86 mkdir("/mnt/lunaix-os");
88 if ((errno = mount("/dev/sdb", "/mnt/lunaix-os", "iso9660", 0))) {
89 syslog(2, "fail mounting boot medium. (%d)\n", errno);
96 // 打开tty设备(控制台),作为标准输入输出。
97 // tty设备属于序列设备(Sequential Device),该类型设备的上层读写
98 // 无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
99 int fdstdout = open("/dev/tty", 0);
100 int fdstdin = dup2(stdout, 1);
109 #elif defined DEMO_SIGNAL
111 #elif defined DEMO_READDIR
113 #elif defined DEMO_IOTEST
115 #elif defined DEMO_INPUT_TEST
117 #elif defined DEMO_SIMPLE_SH
122 printf("==== test end ====\n");
134 * @brief LunaixOS的零号进程,该进程永远为可执行。
136 * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
138 * 同时,该进程也负责fork出我们的init进程。
146 init_proc_user_space(__current);
149 asm volatile("movw %0, %%ax\n"
158 "retf" ::"i"(UDATA_SEG),
159 "i"(USTACK_TOP & ~0xf),
165 extern uint8_t __kernel_start; /* link/linker.ld */
166 extern uint8_t __kernel_end; /* link/linker.ld */
167 extern uint8_t __init_hhk_end; /* link/linker.ld */
168 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
173 kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
177 // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
178 lock_reserved_memory();
181 acpi_init(_k_init_mb_info);
193 timer_init(SYS_TIMER_FREQUENCY_HZ);
196 // peripherals & chipset features
204 console_start_flushing();
207 // expose cake allocator states to vfs
210 unlock_reserved_memory();
213 for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
214 vmm_del_mapping(PD_REFERENCED, (void*)i);
215 pmm_free_page(KERNEL_PID, (void*)i);
220 lock_reserved_memory()
222 __do_reserved_memory(0);
226 unlock_reserved_memory()
228 __do_reserved_memory(1);
232 __do_reserved_memory(int unlock)
234 multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
236 _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
237 // v_mapping mapping;
238 for (unsigned int i = 0; i < map_size; i++) {
239 multiboot_memory_map_t mmap = mmaps[i];
240 uint8_t* pa = PG_ALIGN(mmap.addr_low);
241 if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
242 // Don't fuck up our kernel code or any free area!
245 size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
248 kprintf("mem: freeze: %p..%p type=%x\n",
250 pa + pg_num * PG_SIZE,
252 for (; j < pg_num; j++) {
253 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
254 if (_pa >= KERNEL_MM_BASE) {
255 // Don't fuck up our kernel space!
258 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
259 pmm_mark_page_occupied(
260 KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
262 // Save the progress for later unmapping.
263 mmaps[i].len_low = j * PG_SIZE;
265 kprintf("mem: reclaim: %p..%p type=%x\n",
267 pa + pg_num * PG_SIZE,
269 for (; j < pg_num; j++) {
270 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
271 vmm_del_mapping(PD_REFERENCED, _pa);
272 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
273 pmm_mark_page_free(_pa >> PG_SIZE_BITS);