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