refactor: Move the kernel post init stage into proc0
[lunaix-os.git] / lunaix-os / kernel / k_init.c
1 #include <lunaix/common.h>
2 #include <lunaix/tty/tty.h>
3
4 #include <lunaix/clock.h>
5 #include <lunaix/mm/kalloc.h>
6 #include <lunaix/mm/page.h>
7 #include <lunaix/mm/pmm.h>
8 #include <lunaix/mm/vmm.h>
9 #include <lunaix/process.h>
10 #include <lunaix/sched.h>
11 #include <lunaix/spike.h>
12 #include <lunaix/syslog.h>
13 #include <lunaix/timer.h>
14
15 #include <hal/rtc.h>
16
17 #include <arch/x86/boot/multiboot.h>
18 #include <arch/x86/idt.h>
19 #include <arch/x86/interrupts.h>
20
21 #include <klibc/stdio.h>
22 #include <klibc/string.h>
23
24 #include <stddef.h>
25 #include <stdint.h>
26
27 extern uint8_t __kernel_start;
28 extern uint8_t __kernel_end;
29 extern uint8_t __init_hhk_end;
30
31 #define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN)
32
33 // Set remotely by kernel/asm/x86/prologue.S
34 multiboot_info_t* _k_init_mb_info;
35
36 x86_page_table* __kernel_ptd;
37
38 struct proc_info tmp;
39
40 LOG_MODULE("BOOT");
41
42 extern void
43 __proc0(); /* proc0.c */
44
45 void
46 spawn_proc0();
47
48 void
49 setup_memory(multiboot_memory_map_t* map, size_t map_size);
50
51 void
52 _kernel_pre_init()
53 {
54     _init_idt();
55     intr_routine_init();
56
57     pmm_init(MEM_1MB + (_k_init_mb_info->mem_upper << 10));
58     vmm_init();
59     rtc_init();
60
61     tty_init((void*)VGA_BUFFER_PADDR);
62     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
63
64     __kernel_ptd = cpu_rcr3();
65
66     tmp = (struct proc_info){ .page_table = __kernel_ptd };
67
68     __current = &tmp;
69 }
70
71 void
72 _kernel_init()
73 {
74     kprintf("[MM] Mem: %d KiB, Extended Mem: %d KiB\n",
75             _k_init_mb_info->mem_lower,
76             _k_init_mb_info->mem_upper);
77
78     unsigned int map_size =
79       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
80
81     setup_memory((multiboot_memory_map_t*)_k_init_mb_info->mmap_addr, map_size);
82
83     // 为内核创建一个专属栈空间。
84     for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) {
85         vmm_alloc_page(KERNEL_PID,
86                        (void*)(KSTACK_START + (i << PG_SIZE_BITS)),
87                        NULL,
88                        PG_PREM_RW,
89                        0);
90     }
91     kprintf(KINFO "[MM] Allocated %d pages for stack start at %p\n",
92             KSTACK_SIZE >> PG_SIZE_BITS,
93             KSTACK_START);
94
95     sched_init();
96
97     spawn_proc0();
98 }
99
100 /**
101  * @brief 创建并运行proc0进程
102  *
103  */
104 void
105 spawn_proc0()
106 {
107     struct proc_info proc0;
108
109     /**
110      * @brief
111      * 注意:这里和视频中说的不一样,属于我之后的一点微调。
112      * 在视频中,spawn_proc0是在_kernel_post_init的末尾才调用的。并且是直接跳转到_proc0
113      *
114      * 但是我后来发现,上述的方法会产生竞态条件。这是因为spawn_proc0被调用的时候,时钟中断已经开启,
115      * 而中断的产生会打乱栈的布局,从而使得下面的上下文设置代码产生未定义行为(Undefined
116      * Behaviour)。 为了保险起见,有两种办法:
117      *      1. 在创建proc0进程前关闭中断
118      *      2. 将_kernel_post_init搬进proc0进程
119      * (_kernel_post_init已经更名为init_platform)
120      *
121      * 目前的解决方案是两者都使用
122      */
123
124     init_proc(&proc0);
125     proc0.intr_ctx = (isr_param){ .registers.esp = KSTACK_TOP - 20,
126                                   .cs = KCODE_SEG,
127                                   .eip = (void*)__proc0,
128                                   .ss = KDATA_SEG,
129                                   .eflags = cpu_reflags() };
130
131     // 必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
132     cpu_disable_interrupt();
133     setup_proc_mem(&proc0, PD_REFERENCED);
134
135     // Ok... 首先fork进我们的零号进程,而后由那里,我们fork进init进程。
136     /*
137         这里是一些栈的设置,因为我们将切换到一个新的地址空间里,并且使用一个全新的栈。
138         让iret满意!
139     */
140     asm volatile("movl %%cr3, %%eax\n"
141                  "movl %%esp, %%ebx\n"
142                  "movl %0, %%cr3\n"
143                  "movl %1, %%esp\n"
144                  "pushf\n"
145                  "pushl %2\n"
146                  "pushl %3\n"
147                  "pushl $0\n"
148                  "pushl $0\n"
149                  "movl %%eax, %%cr3\n"
150                  "movl %%ebx, %%esp\n" ::"r"(proc0.page_table),
151                  "i"(KSTACK_TOP),
152                  "i"(KCODE_SEG),
153                  "r"(proc0.intr_ctx.eip)
154                  : "%eax", "%ebx", "memory");
155
156     // 向调度器注册进程。
157     push_process(&proc0);
158
159     // 由于时钟中断未就绪,我们需要手动通知调度器进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位
160     schedule();
161
162     /* Should not return */
163     assert_msg(0, "Unexpected Return");
164 }
165
166 // 按照 Memory map 标识可用的物理页
167 void
168 setup_memory(multiboot_memory_map_t* map, size_t map_size)
169 {
170
171     // First pass, to mark the physical pages
172     for (unsigned int i = 0; i < map_size; i++) {
173         multiboot_memory_map_t mmap = map[i];
174         kprintf("[MM] Base: 0x%x, len: %u KiB, type: %u\n",
175                 map[i].addr_low,
176                 map[i].len_low >> 10,
177                 map[i].type);
178         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
179             // 整数向上取整除法
180             uintptr_t pg = map[i].addr_low + 0x0fffU;
181             pmm_mark_chunk_free(pg >> PG_SIZE_BITS,
182                                 map[i].len_low >> PG_SIZE_BITS);
183             kprintf(KINFO "[MM] Freed %u pages start from 0x%x\n",
184                     map[i].len_low >> PG_SIZE_BITS,
185                     pg & ~0x0fffU);
186         }
187     }
188
189     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
190     size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS;
191     pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, 0);
192     kprintf(KINFO "[MM] Allocated %d pages for kernel.\n", pg_count);
193
194     size_t vga_buf_pgs = VGA_BUFFER_SIZE >> PG_SIZE_BITS;
195
196     // 首先,标记VGA部分为已占用
197     pmm_mark_chunk_occupied(
198       KERNEL_PID, VGA_BUFFER_PADDR >> PG_SIZE_BITS, vga_buf_pgs, 0);
199
200     // 重映射VGA文本缓冲区(以后会变成显存,i.e., framebuffer)
201     for (size_t i = 0; i < vga_buf_pgs; i++) {
202         vmm_map_page(KERNEL_PID,
203                      (void*)(VGA_BUFFER_VADDR + (i << PG_SIZE_BITS)),
204                      (void*)(VGA_BUFFER_PADDR + (i << PG_SIZE_BITS)),
205                      PG_PREM_URW);
206     }
207
208     // 更新VGA缓冲区位置至虚拟地址
209     tty_set_buffer((void*)VGA_BUFFER_VADDR);
210
211     kprintf(KINFO "[MM] Mapped VGA to %p.\n", VGA_BUFFER_VADDR);
212 }