refactor: one more step towards arch-agnostic design
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <lunaix/block.h>
2 #include <lunaix/boot_generic.h>
3 #include <lunaix/common.h>
4 #include <lunaix/exec.h>
5 #include <lunaix/foptions.h>
6 #include <lunaix/fs.h>
7 #include <lunaix/fs/probe_boot.h>
8 #include <lunaix/fs/twifs.h>
9 #include <lunaix/lxconsole.h>
10 #include <lunaix/mm/cake.h>
11 #include <lunaix/mm/pmm.h>
12 #include <lunaix/mm/valloc.h>
13 #include <lunaix/mm/vmm.h>
14 #include <lunaix/peripheral/ps2kbd.h>
15 #include <lunaix/peripheral/serial.h>
16 #include <lunaix/spike.h>
17 #include <lunaix/syscall.h>
18 #include <lunaix/syslog.h>
19 #include <lunaix/types.h>
20
21 #include <sdbg/protocol.h>
22
23 #include <hal/acpi/acpi.h>
24 #include <hal/ahci/ahci.h>
25 #include <hal/pci.h>
26
27 #include <klibc/string.h>
28
29 LOG_MODULE("PROC0")
30
31 void
32 init_platform();
33
34 int
35 mount_bootmedium()
36 {
37     struct v_dnode* dnode;
38     int errno = 0;
39     struct device* dev = probe_boot_medium();
40     if (!dev) {
41         kprintf(KERROR "fail to acquire device. (%d)", errno);
42         return 0;
43     }
44
45     if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) {
46         kprintf(KERROR "fail to mount boot medium. (%d)", errno);
47         return 0;
48     }
49
50     return 1;
51 }
52
53 int
54 exec_initd()
55 {
56     int errno = 0;
57
58     if ((errno = exec_kexecve("/mnt/lunaix-os/usr/bin/init", NULL, NULL))) {
59         goto fail;
60     }
61
62     fail("should not reach");
63
64 fail:
65     kprintf(KERROR "fail to load initd. (%d)", errno);
66     return 0;
67 }
68
69 /**
70  * @brief LunaixOS的零号进程,该进程永远为可执行。
71  *
72  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
73  *
74  * 同时,该进程也负责fork出我们的init进程。
75  *
76  */
77 void
78 __proc0()
79 {
80     /*
81      * We must defer boot code/data cleaning after we successfully escape that
82      * area
83      */
84     boot_cleanup();
85
86     init_platform();
87
88     init_proc_user_space(__current);
89
90     if (!mount_bootmedium() || !exec_initd()) {
91         while (1) {
92             asm("hlt");
93         }
94         // should not reach
95     }
96 }
97
98 void
99 init_platform()
100 {
101     kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
102             __VERSION__,
103             __TIME__);
104
105     twifs_register_plugins();
106
107     /* we must start probing pci after all drivers are registered! */
108     pci_load_devices();
109
110     // debugger
111     serial_init();
112     sdbg_init();
113
114     // FIXME ps2 kbd is x86 PC specific, not here.
115     // peripherals & chipset features
116     ps2_kbd_init();
117
118     // console
119     console_start_flushing();
120     console_flush();
121 }