feat: device subsystem rework
[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 <klibc/string.h>
22
23 LOG_MODULE("PROC0")
24
25 void
26 init_platform();
27
28 int
29 mount_bootmedium()
30 {
31     struct v_dnode* dnode;
32     int errno = 0;
33     struct device* dev = probe_boot_medium();
34     if (!dev) {
35         kprintf(KERROR "fail to acquire device. (%d)", errno);
36         return 0;
37     }
38
39     if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) {
40         kprintf(KERROR "fail to mount boot medium. (%d)", errno);
41         return 0;
42     }
43
44     return 1;
45 }
46
47 int
48 exec_initd()
49 {
50     int errno = 0;
51
52     if ((errno = exec_kexecve("/mnt/lunaix-os/usr/bin/init", NULL, NULL))) {
53         goto fail;
54     }
55
56     fail("should not reach");
57
58 fail:
59     kprintf(KERROR "fail to load initd. (%d)", errno);
60     return 0;
61 }
62
63 /**
64  * @brief LunaixOS的零号进程,该进程永远为可执行。
65  *
66  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
67  *
68  * 同时,该进程也负责fork出我们的init进程。
69  *
70  */
71 void
72 __proc0()
73 {
74     /*
75      * We must defer boot code/data cleaning to here, after we successfully
76      * escape that area
77      */
78     boot_cleanup();
79
80     init_platform();
81
82     init_proc_user_space(__current);
83
84     if (!mount_bootmedium() || !exec_initd()) {
85         while (1) {
86             asm("hlt");
87         }
88         // should not reach
89     }
90 }
91
92 void
93 init_platform()
94 {
95     kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
96             __VERSION__,
97             __TIME__);
98
99     device_poststage();
100
101     twifs_register_plugins();
102
103     // FIXME This 8025 serial should integrated into device layer
104     serial_init();
105
106     // debugger
107     sdbg_init();
108
109     // console
110     console_start_flushing();
111     console_flush();
112 }