feat: kprintf now goes into dedicated pseudo-dev rather than flooding the framebuffer
[lunaix-os.git] / lunaix-os / kernel / kinit.c
1 #include <lunaix/block.h>
2 #include <lunaix/boot_generic.h>
3 #include <lunaix/device.h>
4 #include <lunaix/foptions.h>
5 #include <lunaix/fs/twifs.h>
6 #include <lunaix/input.h>
7 #include <lunaix/lxconsole.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/types.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     /* Prepare stack trace environment */
53     trace_modksyms_init(bhctx);
54
55     device_scan_drivers();
56
57     // crt
58     tty_init(ioremap(0xB8000, 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     /* Get intc online, this is the cornerstone when initing devices */
66     intc_init();
67
68     /*
69         TODO autoload these init function that do not have dependency between
70        them
71     */
72
73     /* Let's get fs online as soon as possible, as things rely on them */
74     vfs_init();
75     fsm_init();
76     input_init();
77     block_init();
78     sched_init();
79
80     device_earlystage();
81
82     /* System timing and clock support */
83     /*
84         FIXME we must get timer as earlier as possible
85
86         A decoupling between rtc and general device sub-sys is needed.
87         Otherwise we timer can only be loaded after device_earlystage.
88
89         We need a dedicated timer&clock subsystem
90     */
91     timer_init();
92
93     /* the bare metal are now happy, let's get software over with */
94
95     int errno = 0;
96     if ((errno = vfs_mount_root("ramfs", NULL))) {
97         panickf("Fail to mount root. (errno=%d)", errno);
98     }
99
100     /* Mount these system-wide pseudo-fs */
101     vfs_mount("/dev", "devfs", NULL, 0);
102     vfs_mount("/sys", "twifs", NULL, MNT_RO);
103     vfs_mount("/task", "taskfs", NULL, MNT_RO);
104
105     /* Finish up bootstrapping sequence, we are ready to spawn the root process
106      * and start geting into uspace
107      */
108     boot_end(bhctx);
109
110     spawn_proc0();
111 }
112
113 /**
114  * @brief 创建并运行proc0进程
115  *
116  */
117 void
118 spawn_proc0()
119 {
120     struct proc_info* proc0 = alloc_process();
121
122     /**
123      * @brief
124      * 注意:这里和视频中说的不一样,属于我之后的一点微调。
125      * 在视频中,spawn_proc0是在_kernel_post_init的末尾才调用的。并且是直接跳转到_proc0
126      *
127      * 但是我后来发现,上述的方法会产生竞态条件。这是因为spawn_proc0被调用的时候,时钟中断已经开启,
128      * 而中断的产生会打乱栈的布局,从而使得下面的上下文设置代码产生未定义行为(Undefined
129      * Behaviour)。 为了保险起见,有两种办法:
130      *      1. 在创建proc0进程前关闭中断
131      *      2. 将_kernel_post_init搬进proc0进程
132      * (_kernel_post_init已经更名为init_platform)
133      *
134      * 目前的解决方案是2
135      */
136
137     proc0->parent = proc0;
138
139     // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
140     // cpu_disable_interrupt();
141
142     /* Ok... 首先fork进我们的零号进程,而后由那里,我们fork进init进程。 */
143
144     // 把当前虚拟地址空间(内核)复制一份。
145     proc0->page_table = vmm_dup_vmspace(proc0->pid);
146
147     // 直接切换到新的拷贝,进行配置。
148     cpu_chvmspace(proc0->page_table);
149
150     // 为内核创建一个专属栈空间。
151     for (size_t i = 0; i < KERNEL_STACK_SIZE; i += PG_SIZE) {
152         ptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
153         vmm_set_mapping(VMS_SELF, KERNEL_STACK + i, pa, PG_PREM_RW, VMAP_NULL);
154     }
155
156     proc_init_transfer(proc0, KERNEL_STACK_END, (ptr_t)__proc0, 0);
157
158     // 向调度器注册进程。
159     commit_process(proc0);
160
161     // 由于时钟中断与APIC未就绪,我们需要手动进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位
162     proc0->state = PS_RUNNING;
163     switch_context(proc0);
164
165     /* Should not return */
166     assert_msg(0, "Unexpected Return");
167 }
168
169 void
170 kmem_init(struct boot_handoff* bhctx)
171 {
172     extern u8_t __kexec_end;
173     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
174     size_t pg_count = ((ptr_t)&__kexec_end - KERNEL_EXEC) >> PG_SIZE_BITS;
175     pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
176
177     // reserve higher half
178     for (size_t i = L1_INDEX(KERNEL_EXEC); i < 1023; i++) {
179         assert(vmm_set_mapping(VMS_SELF, i << 22, 0, 0, VMAP_NOMAP));
180     }
181
182     // allocators
183     cake_init();
184     valloc_init();
185 }