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