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