a54b68a7adaf0e76ecc9e9410d0376ea9796e54a
[lunaix-os.git] / lunaix-os / kernel / kinit.c
1 #include <lunaix/types.h>
2 #include <lunaix/block.h>
3 #include <lunaix/boot_generic.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/mm/cake.h>
9 #include <lunaix/mm/mmio.h>
10 #include <lunaix/mm/page.h>
11 #include <lunaix/mm/pmm.h>
12 #include <lunaix/mm/valloc.h>
13 #include <lunaix/mm/vmm.h>
14 #include <lunaix/process.h>
15 #include <lunaix/sched.h>
16 #include <lunaix/spike.h>
17 #include <lunaix/trace.h>
18 #include <lunaix/tty/tty.h>
19 #include <lunaix/owloysius.h>
20
21 #include <hal/acpi/acpi.h>
22 #include <hal/intc.h>
23
24 #include <sys/abi.h>
25 #include <sys/interrupts.h>
26 #include <sys/mm/mempart.h>
27
28 #include <klibc/strfmt.h>
29 #include <klibc/string.h>
30
31 extern void
32 __proc0(); /* proc0.c */
33
34 void
35 spawn_proc0();
36
37 void
38 kmem_init(struct boot_handoff* bhctx);
39
40 void
41 kernel_bootstrap(struct boot_handoff* bhctx)
42 {
43     pmm_init(bhctx->mem.size);
44     vmm_init();
45
46     /* Begin kernel bootstrapping sequence */
47     boot_begin(bhctx);
48
49     /* Setup kernel memory layout and services */
50     kmem_init(bhctx);
51
52     boot_parse_cmdline(bhctx);
53
54     /* Prepare stack trace environment */
55     trace_modksyms_init(bhctx);
56
57     device_scan_drivers();
58
59     invoke_init_function(on_earlyboot);
60
61     // FIXME this goes to hal/gfxa
62     tty_init(ioremap(0xB8000, PG_SIZE));
63     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
64
65     device_sysconf_load();
66
67     /* Get intc online, this is the cornerstone when initing devices */
68     intc_init();
69
70     clock_init();
71     timer_init();
72
73     /*
74         TODO autoload these init function that do not have dependency between
75        them
76     */
77
78     /* Let's get fs online as soon as possible, as things rely on them */
79     vfs_init();
80     fsm_init();
81     input_init();
82     block_init();
83     sched_init();
84
85     device_onboot_load();
86
87     /* the bare metal are now happy, let's get software over with */
88
89     must_success(vfs_mount_root("ramfs", NULL));
90     must_success(vfs_mount("/dev", "devfs", NULL, 0));
91     
92     invoke_init_function(on_boot);
93
94     must_success(vfs_unmount("/dev"));
95
96     /* Finish up bootstrapping sequence, we are ready to spawn the root process
97      * and start geting into uspace
98      */
99     boot_end(bhctx);
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 < KERNEL_STACK_SIZE; i += PG_SIZE) {
143         ptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
144         vmm_set_mapping(VMS_SELF, KERNEL_STACK + i, pa, PG_PREM_RW, VMAP_NULL);
145     }
146
147     proc_init_transfer(proc0, KERNEL_STACK_END, (ptr_t)__proc0, 0);
148
149     // 向调度器注册进程。
150     commit_process(proc0);
151
152     // 由于时钟中断与APIC未就绪,我们需要手动进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位
153     proc0->state = PS_RUNNING;
154     switch_context(proc0);
155
156     /* Should not return */
157     assert_msg(0, "Unexpected Return");
158 }
159
160 void
161 kmem_init(struct boot_handoff* bhctx)
162 {
163     extern u8_t __kexec_end;
164     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
165     size_t pg_count = ((ptr_t)&__kexec_end - KERNEL_EXEC) >> PG_SIZE_BITS;
166     pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
167
168     // reserve higher half
169     for (size_t i = L1_INDEX(KERNEL_EXEC); i < 1023; i++) {
170         assert(vmm_set_mapping(VMS_SELF, i << 22, 0, 0, VMAP_NOMAP));
171     }
172
173     // allocators
174     cake_init();
175     valloc_init();
176 }