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