1 #include <lunaix/block.h>
2 #include <lunaix/common.h>
3 #include <lunaix/fctrl.h>
5 #include <lunaix/fs/twifs.h>
6 #include <lunaix/lunistd.h>
7 #include <lunaix/lxconsole.h>
8 #include <lunaix/mm/cake.h>
9 #include <lunaix/mm/pmm.h>
10 #include <lunaix/mm/valloc.h>
11 #include <lunaix/mm/vmm.h>
12 #include <lunaix/peripheral/ps2kbd.h>
13 #include <lunaix/peripheral/serial.h>
14 #include <lunaix/proc.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/syscall.h>
17 #include <lunaix/syslog.h>
18 #include <lunaix/types.h>
19 #include <sdbg/protocol.h>
21 #include <hal/acpi/acpi.h>
22 #include <hal/ahci/ahci.h>
24 #include <hal/ioapic.h>
28 #include <arch/x86/boot/multiboot.h>
30 #include <klibc/string.h>
32 #include <ulibc/stdio.h>
37 _lxinit_main(); /* lxinit.c */
43 lock_reserved_memory();
46 unlock_reserved_memory();
49 __do_reserved_memory(int unlock);
52 // #define DEMO_SIGNAL
53 // #define DEMO_READDIR
54 // #define DEMO_IOTEST
55 // #define DEMO_INPUT_TEST
56 #define DEMO_SIMPLE_SH
82 // 打开tty设备(控制台),作为标准输入输出。
83 // tty设备属于序列设备(Sequential Device),该类型设备的上层读写
84 // 无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
85 int fdstdout = open("/dev/tty", 0);
86 int fdstdin = dup2(stdout, 1);
93 #elif defined DEMO_SIGNAL
95 #elif defined DEMO_READDIR
97 #elif defined DEMO_IOTEST
99 #elif defined DEMO_INPUT_TEST
101 #elif defined DEMO_SIMPLE_SH
106 printf("==== test end ====\n");
118 * @brief LunaixOS的零号进程,该进程永远为可执行。
120 * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
122 * 同时,该进程也负责fork出我们的init进程。
130 init_proc_user_space(__current);
133 asm volatile("movw %0, %%ax\n"
142 "retf" ::"i"(UDATA_SEG),
143 "i"(USTACK_TOP & ~0xf),
149 extern uint8_t __kernel_start; /* link/linker.ld */
150 extern uint8_t __kernel_end; /* link/linker.ld */
151 extern uint8_t __init_hhk_end; /* link/linker.ld */
152 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
157 kprintf(KINFO "\033[11;0mLunaixOS \033[39;49m\n");
159 // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
160 lock_reserved_memory();
163 acpi_init(_k_init_mb_info);
175 timer_init(SYS_TIMER_FREQUENCY_HZ);
178 // peripherals & chipset features
185 console_start_flushing();
188 // expose cake allocator states to vfs
191 unlock_reserved_memory();
194 for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
195 vmm_del_mapping(PD_REFERENCED, (void*)i);
196 pmm_free_page(KERNEL_PID, (void*)i);
199 // reserve higher half
200 for (size_t i = L1_INDEX(KERNEL_MM_BASE); i < 1023; i++) {
201 vmm_set_mapping(PD_REFERENCED, i << 22, 0, 0, VMAP_NOMAP);
206 lock_reserved_memory()
208 __do_reserved_memory(0);
212 unlock_reserved_memory()
214 __do_reserved_memory(1);
218 __do_reserved_memory(int unlock)
220 multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
222 _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
223 // v_mapping mapping;
224 for (unsigned int i = 0; i < map_size; i++) {
225 multiboot_memory_map_t mmap = mmaps[i];
226 uint8_t* pa = PG_ALIGN(mmap.addr_low);
227 if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
228 // Don't fuck up our kernel code or any free area!
231 size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
234 kprintf("mem: freeze: %p..%p type=%x\n",
236 pa + pg_num * PG_SIZE,
238 for (; j < pg_num; j++) {
239 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
240 if (_pa >= KERNEL_MM_BASE) {
241 // Don't fuck up our kernel space!
244 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
245 pmm_mark_page_occupied(
246 KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
248 // Save the progress for later unmapping.
249 mmaps[i].len_low = j * PG_SIZE;
251 kprintf("mem: reclaim: %p..%p type=%x\n",
253 pa + pg_num * PG_SIZE,
255 for (; j < pg_num; j++) {
256 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
257 vmm_del_mapping(PD_REFERENCED, _pa);
258 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
259 pmm_mark_page_free(_pa >> PG_SIZE_BITS);