fix: use wait queue for blocking process
[lunaix-os.git] / lunaix-os / kernel / k_init.c
1 #include <lunaix/common.h>
2 #include <lunaix/tty/tty.h>
3
4 #include <lunaix/device.h>
5 #include <lunaix/foptions.h>
6 #include <lunaix/input.h>
7 #include <lunaix/isrm.h>
8 #include <lunaix/lxconsole.h>
9 #include <lunaix/mm/mmio.h>
10 #include <lunaix/mm/page.h>
11 #include <lunaix/mm/pmm.h>
12 #include <lunaix/mm/vmm.h>
13 #include <lunaix/process.h>
14 #include <lunaix/sched.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/types.h>
17
18 #include <arch/x86/boot/multiboot.h>
19 #include <arch/x86/idt.h>
20 #include <arch/x86/interrupts.h>
21
22 #include <klibc/stdio.h>
23 #include <klibc/string.h>
24
25 extern uint8_t __kernel_start;
26 extern uint8_t __kernel_end;
27 extern uint8_t __init_hhk_end;
28
29 #define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN)
30
31 // Set remotely by kernel/asm/x86/prologue.S
32 multiboot_info_t* _k_init_mb_info;
33
34 x86_page_table* __kernel_ptd;
35
36 struct proc_info tmp;
37
38 extern void
39 __proc0(); /* proc0.c */
40
41 void
42 spawn_proc0();
43
44 void
45 setup_memory(multiboot_memory_map_t* map, size_t map_size);
46
47 void
48 _kernel_pre_init()
49 {
50     // interrupts
51     _init_idt();
52     isrm_init();
53     intr_routine_init();
54
55     // memory
56     pmm_init(MEM_1MB + (_k_init_mb_info->mem_upper << 10));
57     vmm_init();
58
59     unsigned int map_size =
60       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
61
62     setup_memory((multiboot_memory_map_t*)_k_init_mb_info->mmap_addr, map_size);
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     int errno = 0;
75
76     // allocators
77     cake_init();
78     valloc_init();
79
80     // crt
81     tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE));
82     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
83
84     // file system & device subsys
85     vfs_init();
86     fsm_init();
87     input_init();
88
89     vfs_export_attributes();
90
91     if ((errno = vfs_mount_root("ramfs", NULL))) {
92         panickf("Fail to mount root. (errno=%d)", errno);
93     }
94
95     vfs_mount("/dev", "devfs", NULL, 0);
96     vfs_mount("/sys", "twifs", NULL, MNT_RO);
97     vfs_mount("/task", "taskfs", NULL, MNT_RO);
98
99     lxconsole_init();
100
101     sched_init();
102
103     syscall_install();
104
105     spawn_proc0();
106 }
107
108 /**
109  * @brief 创建并运行proc0进程
110  *
111  */
112 void
113 spawn_proc0()
114 {
115     struct proc_info* proc0 = alloc_process();
116
117     /**
118      * @brief
119      * 注意:这里和视频中说的不一样,属于我之后的一点微调。
120      * 在视频中,spawn_proc0是在_kernel_post_init的末尾才调用的。并且是直接跳转到_proc0
121      *
122      * 但是我后来发现,上述的方法会产生竞态条件。这是因为spawn_proc0被调用的时候,时钟中断已经开启,
123      * 而中断的产生会打乱栈的布局,从而使得下面的上下文设置代码产生未定义行为(Undefined
124      * Behaviour)。 为了保险起见,有两种办法:
125      *      1. 在创建proc0进程前关闭中断
126      *      2. 将_kernel_post_init搬进proc0进程
127      * (_kernel_post_init已经更名为init_platform)
128      *
129      * 目前的解决方案是2
130      */
131
132     proc0->intr_ctx = (isr_param){ .registers = { .ds = KDATA_SEG,
133                                                   .es = KDATA_SEG,
134                                                   .fs = KDATA_SEG,
135                                                   .gs = KDATA_SEG },
136                                    .cs = KCODE_SEG,
137                                    .eip = (void*)__proc0,
138                                    .ss = KDATA_SEG,
139                                    .eflags = cpu_reflags() };
140     proc0->parent = proc0;
141
142     // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
143     // cpu_disable_interrupt();
144
145     /* Ok... 首先fork进我们的零号进程,而后由那里,我们fork进init进程。 */
146
147     // 把当前虚拟地址空间(内核)复制一份。
148     proc0->page_table = vmm_dup_vmspace(proc0->pid);
149
150     // 直接切换到新的拷贝,进行配置。
151     cpu_lcr3(proc0->page_table);
152
153     // 为内核创建一个专属栈空间。
154     for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) {
155         uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
156         vmm_set_mapping(PD_REFERENCED,
157                         KSTACK_START + (i << PG_SIZE_BITS),
158                         pa,
159                         PG_PREM_RW,
160                         VMAP_NULL);
161     }
162
163     // 手动设置进程上下文:用于第一次调度
164     asm volatile("movl %%esp, %%ebx\n"
165                  "movl %1, %%esp\n"
166                  "pushf\n"
167                  "pushl %2\n"
168                  "pushl %3\n"
169                  "pushl $0\n"
170                  "pushl $0\n"
171                  "movl %%esp, %0\n"
172                  "movl %%ebx, %%esp\n"
173                  : "=m"(proc0->intr_ctx.registers.esp)
174                  : "i"(KSTACK_TOP), "i"(KCODE_SEG), "r"(proc0->intr_ctx.eip)
175                  : "%ebx", "memory");
176
177     // 加载x87默认配置
178     asm volatile("fninit\n"
179                  "fxsave (%%eax)" ::"a"(proc0->fxstate)
180                  : "memory");
181
182     // 向调度器注册进程。
183     commit_process(proc0);
184
185     // 由于时钟中断与APIC未就绪,我们需要手动进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位
186     proc0->state = PS_RUNNING;
187     asm volatile("pushl %0\n"
188                  "jmp switch_to\n" ::"r"(proc0));
189
190     /* Should not return */
191     assert_msg(0, "Unexpected Return");
192 }
193
194 extern void __usrtext_start;
195 extern void __usrtext_end;
196
197 // 按照 Memory map 标识可用的物理页
198 void
199 setup_memory(multiboot_memory_map_t* map, size_t map_size)
200 {
201
202     // First pass, to mark the physical pages
203     for (unsigned int i = 0; i < map_size; i++) {
204         multiboot_memory_map_t mmap = map[i];
205         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
206             // 整数向上取整除法
207             uintptr_t pg = map[i].addr_low + 0x0fffU;
208             pmm_mark_chunk_free(pg >> PG_SIZE_BITS,
209                                 map[i].len_low >> PG_SIZE_BITS);
210         }
211     }
212
213     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
214     size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS;
215     pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
216
217     for (uintptr_t i = &__usrtext_start; i < &__usrtext_end; i += PG_SIZE) {
218         vmm_set_mapping(PD_REFERENCED, i, V2P(i), PG_PREM_UR, VMAP_NULL);
219     }
220 }