1 #include <lunaix/common.h>
2 #include <lunaix/tty/tty.h>
4 #include <lunaix/device.h>
5 #include <lunaix/foptions.h>
6 #include <lunaix/input.h>
7 #include <lunaix/isrm.h>
8 #include <lunaix/lxconsole.h>
9 #include <lunaix/mm/mmio.h>
10 #include <lunaix/mm/page.h>
11 #include <lunaix/mm/pmm.h>
12 #include <lunaix/mm/vmm.h>
13 #include <lunaix/process.h>
14 #include <lunaix/sched.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/types.h>
18 #include <arch/x86/boot/multiboot.h>
19 #include <arch/x86/idt.h>
20 #include <arch/x86/interrupts.h>
22 #include <klibc/stdio.h>
23 #include <klibc/string.h>
25 extern uint8_t __kernel_start;
26 extern uint8_t __kernel_end;
27 extern uint8_t __init_hhk_end;
29 #define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN)
31 // Set remotely by kernel/asm/x86/prologue.S
32 multiboot_info_t* _k_init_mb_info;
34 x86_page_table* __kernel_ptd;
37 __proc0(); /* proc0.c */
43 setup_memory(multiboot_memory_map_t* map, size_t map_size);
54 pmm_init(MEM_1MB + (_k_init_mb_info->mem_upper << 10));
57 unsigned int map_size =
58 _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
60 setup_memory((multiboot_memory_map_t*)_k_init_mb_info->mmap_addr, map_size);
75 tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE));
76 tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
78 // file system & device subsys
83 vfs_export_attributes();
85 if ((errno = vfs_mount_root("ramfs", NULL))) {
86 panickf("Fail to mount root. (errno=%d)", errno);
89 vfs_mount("/dev", "devfs", NULL, 0);
90 vfs_mount("/sys", "twifs", NULL, MNT_RO);
91 vfs_mount("/task", "taskfs", NULL, MNT_RO);
101 * @brief 创建并运行proc0进程
107 struct proc_info* proc0 = alloc_process();
111 * 注意:这里和视频中说的不一样,属于我之后的一点微调。
112 * 在视频中,spawn_proc0是在_kernel_post_init的末尾才调用的。并且是直接跳转到_proc0
114 * 但是我后来发现,上述的方法会产生竞态条件。这是因为spawn_proc0被调用的时候,时钟中断已经开启,
115 * 而中断的产生会打乱栈的布局,从而使得下面的上下文设置代码产生未定义行为(Undefined
116 * Behaviour)。 为了保险起见,有两种办法:
118 * 2. 将_kernel_post_init搬进proc0进程
119 * (_kernel_post_init已经更名为init_platform)
124 proc0->intr_ctx = (isr_param){ .registers = { .ds = KDATA_SEG,
129 .eip = (void*)__proc0,
131 .eflags = cpu_reflags() };
132 proc0->parent = proc0;
134 // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
135 // cpu_disable_interrupt();
137 /* Ok... 首先fork进我们的零号进程,而后由那里,我们fork进init进程。 */
139 // 把当前虚拟地址空间(内核)复制一份。
140 proc0->page_table = vmm_dup_vmspace(proc0->pid);
143 cpu_lcr3(proc0->page_table);
146 for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) {
147 uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
148 vmm_set_mapping(PD_REFERENCED,
149 KSTACK_START + (i << PG_SIZE_BITS),
156 asm volatile("movl %%esp, %%ebx\n"
164 "movl %%ebx, %%esp\n"
165 : "=m"(proc0->intr_ctx.registers.esp)
166 : "i"(KSTACK_TOP), "i"(KCODE_SEG), "r"(proc0->intr_ctx.eip)
170 asm volatile("fninit\n"
171 "fxsave (%%eax)" ::"a"(proc0->fxstate)
175 commit_process(proc0);
177 // 由于时钟中断与APIC未就绪,我们需要手动进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位
178 proc0->state = PS_RUNNING;
179 asm volatile("pushl %0\n"
180 "jmp switch_to\n" ::"r"(proc0));
182 /* Should not return */
183 assert_msg(0, "Unexpected Return");
186 extern void __usrtext_start;
187 extern void __usrtext_end;
189 // 按照 Memory map 标识可用的物理页
191 setup_memory(multiboot_memory_map_t* map, size_t map_size)
194 // First pass, to mark the physical pages
195 for (unsigned int i = 0; i < map_size; i++) {
196 multiboot_memory_map_t mmap = map[i];
197 if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
199 uintptr_t pg = map[i].addr_low + 0x0fffU;
200 pmm_mark_chunk_free(pg >> PG_SIZE_BITS,
201 map[i].len_low >> PG_SIZE_BITS);
205 // 将内核占据的页,包括前1MB,hhk_init 设为已占用
206 size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS;
207 pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
209 for (uintptr_t i = &__usrtext_start; i < &__usrtext_end; i += PG_SIZE) {
210 vmm_set_mapping(PD_REFERENCED, i, V2P(i), PG_PREM_UR, VMAP_NULL);