refactor: one more step towards arch-agnostic design
[lunaix-os.git] / lunaix-os / kernel / kinit.c
1 #include <lunaix/block.h>
2 #include <lunaix/boot_generic.h>
3 #include <lunaix/common.h>
4 #include <lunaix/device.h>
5 #include <lunaix/foptions.h>
6 #include <lunaix/fs/twifs.h>
7 #include <lunaix/input.h>
8 #include <lunaix/lxconsole.h>
9 #include <lunaix/mm/cake.h>
10 #include <lunaix/mm/mmio.h>
11 #include <lunaix/mm/page.h>
12 #include <lunaix/mm/pmm.h>
13 #include <lunaix/mm/valloc.h>
14 #include <lunaix/mm/vmm.h>
15 #include <lunaix/process.h>
16 #include <lunaix/sched.h>
17 #include <lunaix/spike.h>
18 #include <lunaix/trace.h>
19 #include <lunaix/tty/tty.h>
20 #include <lunaix/types.h>
21
22 #include <hal/acpi/acpi.h>
23 #include <hal/intc.h>
24 #include <hal/pci.h>
25
26 #include <sys/abi.h>
27 #include <sys/interrupts.h>
28 #include <sys/mm/mempart.h>
29
30 #include <klibc/stdio.h>
31 #include <klibc/string.h>
32
33 extern void
34 __proc0(); /* proc0.c */
35
36 void
37 spawn_proc0();
38
39 void
40 kmem_init(struct boot_handoff* bhctx);
41
42 void
43 kernel_bootstrap(struct boot_handoff* bhctx)
44 {
45     pmm_init(bhctx->mem.size);
46     vmm_init();
47
48     /* Begin kernel bootstrapping sequence */
49     boot_begin(bhctx);
50
51     /* Setup kernel memory layout and services */
52     kmem_init(bhctx);
53
54     /* Prepare stack trace environment */
55     trace_modksyms_init(bhctx);
56
57     // crt
58     tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE));
59     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
60     lxconsole_init();
61
62     /* Get platform configuration */
63     acpi_init();
64
65     /* Let's get fs online as soon as possible, as things rely on them */
66     vfs_init();
67     fsm_init();
68     input_init();
69
70     /* Get intc online, this is the cornerstone when initing devices */
71     intc_init();
72
73     /* System timing and clock support */
74     clock_init();
75     timer_init();
76
77     block_init();
78
79     /* the bare metal are now happy, let's get software over with */
80     sched_init();
81
82     int errno = 0;
83     if ((errno = vfs_mount_root("ramfs", NULL))) {
84         panickf("Fail to mount root. (errno=%d)", errno);
85     }
86
87     /* Mount these system-wide pseudo-fs */
88     vfs_mount("/dev", "devfs", NULL, 0);
89     vfs_mount("/sys", "twifs", NULL, MNT_RO);
90     vfs_mount("/task", "taskfs", NULL, MNT_RO);
91
92     lxconsole_spawn_ttydev();
93     device_init_builtin();
94
95     /* Finish up bootstrapping sequence, we are ready to spawn the root process
96      * and start geting into uspace
97      */
98     boot_end(bhctx);
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     switch_context(proc0);
158
159     /* Should not return */
160     assert_msg(0, "Unexpected Return");
161 }
162
163 void
164 kmem_init(struct boot_handoff* bhctx)
165 {
166     extern u8_t __kexec_end;
167     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
168     size_t pg_count = ((ptr_t)&__kexec_end - KERNEL_EXEC) >> PG_SIZE_BITS;
169     pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
170
171     // reserve higher half
172     for (size_t i = L1_INDEX(KERNEL_EXEC); i < 1023; i++) {
173         assert(vmm_set_mapping(VMS_SELF, i << 22, 0, 0, VMAP_NOMAP));
174     }
175
176     // allocators
177     cake_init();
178     valloc_init();
179 }