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