1 #include <arch/x86/boot/multiboot.h>
2 #include <lunaix/common.h>
4 #include <lunaix/fs/twifs.h>
5 #include <lunaix/lunistd.h>
6 #include <lunaix/lxconsole.h>
7 #include <lunaix/mm/cake.h>
8 #include <lunaix/mm/pmm.h>
9 #include <lunaix/mm/valloc.h>
10 #include <lunaix/mm/vmm.h>
11 #include <lunaix/peripheral/ps2kbd.h>
12 #include <lunaix/proc.h>
13 #include <lunaix/spike.h>
14 #include <lunaix/syscall.h>
15 #include <lunaix/syslog.h>
18 #include <hal/acpi/acpi.h>
19 #include <hal/ahci/ahci.h>
21 #include <hal/ioapic.h>
24 #include <klibc/string.h>
29 _lxinit_main(); /* lxinit.c */
35 lock_reserved_memory();
38 unlock_reserved_memory();
41 __do_reserved_memory(int unlock);
66 #elif defined DEMO_SIGNAL
81 * @brief LunaixOS的零号进程,该进程永远为可执行。
83 * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
85 * 同时,该进程也负责fork出我们的init进程。
93 init_proc_user_space(__current);
95 asm volatile("movw %0, %%ax\n"
104 "retf" ::"i"(UDATA_SEG),
105 "i"(USTACK_TOP & ~0xf),
111 extern uint8_t __kernel_start; /* link/linker.ld */
112 extern uint8_t __kernel_end; /* link/linker.ld */
113 extern uint8_t __init_hhk_end; /* link/linker.ld */
114 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
116 char test_sequence[] = "Once upon a time, in a magical land of Equestria. "
117 "There were two regal sisters who ruled together "
118 "and created harmony for all the land.";
123 struct hba_port* port = ahci_get_port(0);
124 struct hba_device* dev = port->device;
125 char* buffer = vzalloc_dma(port->device->block_size);
126 strcpy(buffer, test_sequence);
127 kprintf("WRITE: %s\n", buffer);
131 result = dev->ops.write_buffer(dev, 0, buffer, dev->block_size);
133 kprintf(KWARN "fail to write: %x\n", dev->last_error);
136 memset(buffer, 0, dev->block_size);
139 result = dev->ops.read_buffer(dev, 0, buffer, dev->block_size);
140 kprintf(KDEBUG "%x, %x\n", port->regs[HBA_RPxIS], port->regs[HBA_RPxTFD]);
142 kprintf(KWARN "fail to read: %x\n", dev->last_error);
144 kprint_hex(buffer, 256);
153 // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
154 lock_reserved_memory();
156 assert_msg(kalloc_init(), "Fail to initialize heap");
158 acpi_init(_k_init_mb_info);
161 timer_init(SYS_TIMER_FREQUENCY_HZ);
166 // ahci_list_device();
172 vfs_mount("/", "twifs", -1);
180 console_start_flushing();
183 unlock_reserved_memory();
185 for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
186 vmm_del_mapping(PD_REFERENCED, (void*)i);
187 pmm_free_page(KERNEL_PID, (void*)i);
192 lock_reserved_memory()
194 __do_reserved_memory(0);
198 unlock_reserved_memory()
200 __do_reserved_memory(1);
204 __do_reserved_memory(int unlock)
206 multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
208 _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
209 // v_mapping mapping;
210 for (unsigned int i = 0; i < map_size; i++) {
211 multiboot_memory_map_t mmap = mmaps[i];
212 uint8_t* pa = PG_ALIGN(mmap.addr_low);
213 if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
214 // Don't fuck up our kernel code or any free area!
217 size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
220 for (; j < pg_num; j++) {
221 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
222 if (_pa >= KERNEL_MM_BASE) {
223 // Don't fuck up our kernel space!
226 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
227 pmm_mark_page_occupied(
228 KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
230 // Save the progress for later unmapping.
231 mmaps[i].len_low = j * PG_SIZE;
233 for (; j < pg_num; j++) {
234 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
235 vmm_del_mapping(PD_REFERENCED, _pa);
236 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
237 pmm_mark_page_free(_pa >> PG_SIZE_BITS);