refactor: simplify the vmm design, single responsibility. But using it should with...
[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         uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
86         vmm_set_mapping(
87           PD_REFERENCED, KSTACK_START + (i << PG_SIZE_BITS), pa, PG_PREM_RW);
88     }
89     kprintf(KINFO "[MM] Allocated %d pages for stack start at %p\n",
90             KSTACK_SIZE >> PG_SIZE_BITS,
91             KSTACK_START);
92
93     sched_init();
94
95     spawn_proc0();
96 }
97
98 /**
99  * @brief 创建并运行proc0进程
100  *
101  */
102 void
103 spawn_proc0()
104 {
105     struct proc_info proc0;
106
107     /**
108      * @brief
109      * 注意:这里和视频中说的不一样,属于我之后的一点微调。
110      * 在视频中,spawn_proc0是在_kernel_post_init的末尾才调用的。并且是直接跳转到_proc0
111      *
112      * 但是我后来发现,上述的方法会产生竞态条件。这是因为spawn_proc0被调用的时候,时钟中断已经开启,
113      * 而中断的产生会打乱栈的布局,从而使得下面的上下文设置代码产生未定义行为(Undefined
114      * Behaviour)。 为了保险起见,有两种办法:
115      *      1. 在创建proc0进程前关闭中断
116      *      2. 将_kernel_post_init搬进proc0进程
117      * (_kernel_post_init已经更名为init_platform)
118      *
119      * 目前的解决方案是2
120      */
121
122     init_proc(&proc0);
123     proc0.intr_ctx = (isr_param){ .registers = { .ds = KDATA_SEG,
124                                                  .es = KDATA_SEG,
125                                                  .fs = KDATA_SEG,
126                                                  .gs = KDATA_SEG },
127                                   .cs = KCODE_SEG,
128                                   .eip = (void*)__proc0,
129                                   .ss = KDATA_SEG,
130                                   .eflags = cpu_reflags() };
131
132     // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
133     // cpu_disable_interrupt();
134
135     setup_proc_mem(&proc0, PD_REFERENCED);
136
137     // Ok... 首先fork进我们的零号进程,而后由那里,我们fork进init进程。
138     /*
139         这里是一些栈的设置,因为我们将切换到一个新的地址空间里,并且使用一个全新的栈。
140         让iret满意!
141     */
142     asm volatile("movl %%cr3, %%eax\n"
143                  "movl %%esp, %%ebx\n"
144                  "movl %1, %%cr3\n"
145                  "movl %2, %%esp\n"
146                  "pushf\n"
147                  "pushl %3\n"
148                  "pushl %4\n"
149                  "pushl $0\n"
150                  "pushl $0\n"
151                  "movl %%esp, %0\n"
152                  "movl %%eax, %%cr3\n"
153                  "movl %%ebx, %%esp\n"
154                  : "=m"(proc0.intr_ctx.registers.esp)
155                  : "r"(proc0.page_table),
156                    "i"(KSTACK_TOP),
157                    "i"(KCODE_SEG),
158                    "r"(proc0.intr_ctx.eip)
159                  : "%eax", "%ebx", "memory");
160
161     // 向调度器注册进程。
162     push_process(&proc0);
163
164     // 由于时钟中断未就绪,我们需要手动通知调度器进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位
165     schedule();
166
167     /* Should not return */
168     assert_msg(0, "Unexpected Return");
169 }
170
171 // 按照 Memory map 标识可用的物理页
172 void
173 setup_memory(multiboot_memory_map_t* map, size_t map_size)
174 {
175
176     // First pass, to mark the physical pages
177     for (unsigned int i = 0; i < map_size; i++) {
178         multiboot_memory_map_t mmap = map[i];
179         kprintf("[MM] Base: 0x%x, len: %u KiB, type: %u\n",
180                 map[i].addr_low,
181                 map[i].len_low >> 10,
182                 map[i].type);
183         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
184             // 整数向上取整除法
185             uintptr_t pg = map[i].addr_low + 0x0fffU;
186             pmm_mark_chunk_free(pg >> PG_SIZE_BITS,
187                                 map[i].len_low >> PG_SIZE_BITS);
188             kprintf(KINFO "[MM] Freed %u pages start from 0x%x\n",
189                     map[i].len_low >> PG_SIZE_BITS,
190                     pg & ~0x0fffU);
191         }
192     }
193
194     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
195     size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS;
196     pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, 0);
197     kprintf(KINFO "[MM] Allocated %d pages for kernel.\n", pg_count);
198
199     size_t vga_buf_pgs = VGA_BUFFER_SIZE >> PG_SIZE_BITS;
200
201     // 首先,标记VGA部分为已占用
202     pmm_mark_chunk_occupied(
203       KERNEL_PID, VGA_BUFFER_PADDR >> PG_SIZE_BITS, vga_buf_pgs, 0);
204
205     // 重映射VGA文本缓冲区(以后会变成显存,i.e., framebuffer)
206     for (size_t i = 0; i < vga_buf_pgs; i++) {
207         vmm_set_mapping(PD_REFERENCED,
208                         VGA_BUFFER_VADDR + (i << PG_SIZE_BITS),
209                         VGA_BUFFER_PADDR + (i << PG_SIZE_BITS),
210                         PG_PREM_URW);
211     }
212
213     // 更新VGA缓冲区位置至虚拟地址
214     tty_set_buffer((void*)VGA_BUFFER_VADDR);
215
216     kprintf(KINFO "[MM] Mapped VGA to %p.\n", VGA_BUFFER_VADDR);
217 }