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