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