1 #include <lunaix/block.h>
2 #include <lunaix/boot_generic.h>
3 #include <lunaix/device.h>
4 #include <lunaix/foptions.h>
5 #include <lunaix/fs/twifs.h>
6 #include <lunaix/input.h>
7 #include <lunaix/lxconsole.h>
8 #include <lunaix/mm/cake.h>
9 #include <lunaix/mm/mmio.h>
10 #include <lunaix/mm/page.h>
11 #include <lunaix/mm/pmm.h>
12 #include <lunaix/mm/valloc.h>
13 #include <lunaix/mm/vmm.h>
14 #include <lunaix/process.h>
15 #include <lunaix/sched.h>
16 #include <lunaix/spike.h>
17 #include <lunaix/trace.h>
18 #include <lunaix/tty/tty.h>
19 #include <lunaix/types.h>
21 #include <hal/acpi/acpi.h>
26 #include <sys/interrupts.h>
27 #include <sys/mm/mempart.h>
29 #include <klibc/stdio.h>
30 #include <klibc/string.h>
33 __proc0(); /* proc0.c */
39 kmem_init(struct boot_handoff* bhctx);
42 kernel_bootstrap(struct boot_handoff* bhctx)
44 pmm_init(bhctx->mem.size);
47 /* Begin kernel bootstrapping sequence */
50 /* Setup kernel memory layout and services */
53 /* Prepare stack trace environment */
54 trace_modksyms_init(bhctx);
57 tty_init(ioremap(0xB8000, PG_SIZE));
58 tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
61 /* Get platform configuration */
64 /* Let's get fs online as soon as possible, as things rely on them */
68 /* Get intc online, this is the cornerstone when initing devices */
71 /* System timing and clock support */
78 /* the bare metal are now happy, let's get software over with */
82 if ((errno = vfs_mount_root("ramfs", NULL))) {
83 panickf("Fail to mount root. (errno=%d)", errno);
86 /* Mount these system-wide pseudo-fs */
87 vfs_mount("/dev", "devfs", NULL, 0);
88 vfs_mount("/sys", "twifs", NULL, MNT_RO);
89 vfs_mount("/task", "taskfs", NULL, MNT_RO);
91 lxconsole_spawn_ttydev();
92 device_install_pseudo();
94 /* Finish up bootstrapping sequence, we are ready to spawn the root process
95 * and start geting into uspace
103 * @brief 创建并运行proc0进程
109 struct proc_info* proc0 = alloc_process();
113 * 注意:这里和视频中说的不一样,属于我之后的一点微调。
114 * 在视频中,spawn_proc0是在_kernel_post_init的末尾才调用的。并且是直接跳转到_proc0
116 * 但是我后来发现,上述的方法会产生竞态条件。这是因为spawn_proc0被调用的时候,时钟中断已经开启,
117 * 而中断的产生会打乱栈的布局,从而使得下面的上下文设置代码产生未定义行为(Undefined
118 * Behaviour)。 为了保险起见,有两种办法:
120 * 2. 将_kernel_post_init搬进proc0进程
121 * (_kernel_post_init已经更名为init_platform)
126 proc0->parent = proc0;
128 // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
129 // cpu_disable_interrupt();
131 /* Ok... 首先fork进我们的零号进程,而后由那里,我们fork进init进程。 */
133 // 把当前虚拟地址空间(内核)复制一份。
134 proc0->page_table = vmm_dup_vmspace(proc0->pid);
137 cpu_chvmspace(proc0->page_table);
140 for (size_t i = 0; i < KERNEL_STACK_SIZE; i += PG_SIZE) {
141 ptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
142 vmm_set_mapping(VMS_SELF, KERNEL_STACK + i, pa, PG_PREM_RW, VMAP_NULL);
145 proc_init_transfer(proc0, KERNEL_STACK_END, (ptr_t)__proc0, 0);
148 commit_process(proc0);
150 // 由于时钟中断与APIC未就绪,我们需要手动进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位
151 proc0->state = PS_RUNNING;
152 switch_context(proc0);
154 /* Should not return */
155 assert_msg(0, "Unexpected Return");
159 kmem_init(struct boot_handoff* bhctx)
161 extern u8_t __kexec_end;
162 // 将内核占据的页,包括前1MB,hhk_init 设为已占用
163 size_t pg_count = ((ptr_t)&__kexec_end - KERNEL_EXEC) >> PG_SIZE_BITS;
164 pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
166 // reserve higher half
167 for (size_t i = L1_INDEX(KERNEL_EXEC); i < 1023; i++) {
168 assert(vmm_set_mapping(VMS_SELF, i << 22, 0, 0, VMAP_NOMAP));