optimize the menuconfig redrawing
[lunaix-os.git] / lunaix-os / kernel / kinit.c
1 #include <lunaix/types.h>
2 #include <lunaix/block.h>
3 #include <lunaix/boot_generic.h>
4 #include <lunaix/device.h>
5 #include <lunaix/foptions.h>
6 #include <lunaix/fs/twifs.h>
7 #include <lunaix/input.h>
8 #include <lunaix/mm/cake.h>
9 #include <lunaix/mm/mmio.h>
10 #include <lunaix/mm/pmm.h>
11 #include <lunaix/mm/valloc.h>
12 #include <lunaix/mm/vmm.h>
13 #include <lunaix/process.h>
14 #include <lunaix/sched.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/trace.h>
17 #include <lunaix/tty/tty.h>
18 #include <lunaix/owloysius.h>
19 #include <lunaix/hart_state.h>
20
21 #include <hal/acpi/acpi.h>
22
23 #include <sys/abi.h>
24 #include <sys/mm/mm_defs.h>
25
26 #include <klibc/strfmt.h>
27 #include <klibc/string.h>
28
29 void
30 spawn_lunad();
31
32 void
33 kmem_init(struct boot_handoff* bhctx);
34
35 void
36 kernel_bootstrap(struct boot_handoff* bhctx)
37 {
38     vmm_init();
39
40     pmm_init(bhctx);
41     // now we can start reserving physical space
42
43     /* Begin kernel bootstrapping sequence */
44     boot_begin(bhctx);
45
46     /* Setup kernel memory layout and services */
47     kmem_init(bhctx);
48
49     boot_parse_cmdline(bhctx);
50
51     /* Prepare stack trace environment */
52     trace_modksyms_init(bhctx);
53
54     device_scan_drivers();
55
56     device_sysconf_load();
57
58     invoke_init_function(on_earlyboot);
59
60     clock_init();
61     timer_init();
62
63     /*
64         TODO autoload these init function that do not have dependency between
65        them
66     */
67
68     /* Let's get fs online as soon as possible, as things rely on them */
69     vfs_init();
70     fsm_init();
71     input_init();
72     block_init();
73     sched_init();
74
75     device_onboot_load();
76
77     /* the bare metal are now happy, let's get software over with */
78
79     must_success(vfs_mount_root("ramfs", NULL));
80     must_success(vfs_mount("/dev", "devfs", NULL, 0));
81     
82     invoke_init_function(on_boot);
83
84     must_success(vfs_unmount("/dev"));
85
86     /* Finish up bootstrapping sequence, we are ready to spawn the root process
87      * and start geting into uspace
88      */
89     boot_end(bhctx);
90
91     spawn_lunad();
92 }
93
94 extern void
95 lunad_main();
96
97 /**
98  * @brief 创建并运行Lunaix守护进程
99  *
100  */
101 void
102 spawn_lunad()
103 {
104     int has_error;
105     struct thread* kthread;
106     
107     has_error = spawn_process(&kthread, (ptr_t)lunad_main, false);
108     assert_msg(!has_error, "failed to spawn lunad");
109
110     run(kthread);
111     
112     fail("Unexpected Return");
113 }
114
115 void
116 kmem_init(struct boot_handoff* bhctx)
117 {
118     pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT);
119
120     ptep = mkl0tep(ptep);
121
122     unsigned int i = ptep_vfn(ptep);
123     do {
124         if (l0tep_impile_vmnts(ptep)) {
125             ptep++;
126             continue;
127         }
128
129 #if   LnT_ENABLED(1)
130         assert(mkl1t(ptep++, 0, KERNEL_DATA));
131 #elif LnT_ENABLED(2)
132         assert(mkl2t(ptep++, 0, KERNEL_DATA));
133 #elif LnT_ENABLED(3)
134         assert(mkl3t(ptep++, 0, KERNEL_DATA));
135 #else
136         assert(mklft(ptep++, 0, KERNEL_DATA));
137 #endif
138     } while (++i < MAX_PTEN);
139
140     // allocators
141     cake_init();
142     valloc_init();
143 }