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