2-setup_gdt.md (#22)
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <lunaix/boot_generic.h>
2 #include <lunaix/exec.h>
3 #include <lunaix/foptions.h>
4 #include <lunaix/fs.h>
5 #include <lunaix/fs/probe_boot.h>
6 #include <lunaix/fs/twifs.h>
7 #include <lunaix/spike.h>
8 #include <lunaix/syslog.h>
9 #include <lunaix/types.h>
10 #include <lunaix/owloysius.h>
11
12 #include <klibc/string.h>
13
14 LOG_MODULE("PROC0")
15
16 void
17 init_platform();
18
19 int
20 mount_bootmedium()
21 {
22     struct v_dnode* dnode;
23     int errno = 0;
24     struct device* dev = probe_boot_medium();
25     if (!dev) {
26         ERROR("fail to acquire device. (%d)", errno);
27         return 0;
28     }
29
30     if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) {
31         ERROR("fail to mount boot medium. (%d)", errno);
32         return 0;
33     }
34
35     return 1;
36 }
37
38 int
39 exec_initd()
40 {
41     int errno = 0;
42
43     if ((errno = exec_kexecve("/mnt/lunaix-os/usr/bin/init", NULL, NULL))) {
44         goto fail;
45     }
46
47     fail("should not reach");
48
49 fail:
50     ERROR("fail to load initd. (%d)", errno);
51     return 0;
52 }
53
54 /**
55  * @brief LunaixOS的零号进程,该进程永远为可执行。
56  *
57  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
58  *
59  * 同时,该进程也负责fork出我们的init进程。
60  *
61  */
62 void
63 __proc0()
64 {
65     /*
66      * We must defer boot code/data cleaning to here, after we successfully
67      * escape that area
68      */
69     boot_cleanup();
70
71     init_platform();
72
73     init_proc_user_space(__current);
74
75     if (!mount_bootmedium() || !exec_initd()) {
76         FATAL("failed to initd");
77         // should not reach
78     }
79 }
80
81 void
82 init_platform()
83 {
84     device_postboot_load();
85     invoke_init_function(on_postboot);
86
87     twifs_register_plugins();
88
89     // FIXME Re-design needed!!
90     // sdbg_init();
91 }