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