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