fix dependency check logic cause config always disabled
[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/input.h>
6
7 #include <lunaix/mm/cake.h>
8 #include <lunaix/mm/pmm.h>
9 #include <lunaix/mm/page.h>
10 #include <lunaix/mm/valloc.h>
11 #include <lunaix/mm/vmm.h>
12
13 #include <lunaix/process.h>
14 #include <lunaix/sched.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/trace.h>
17 #include <lunaix/owloysius.h>
18 #include <lunaix/hart_state.h>
19 #include <lunaix/syslog.h>
20 #include <lunaix/sections.h>
21
22 #include <hal/devtree.h>
23
24 #include <asm/mm_defs.h>
25
26 LOG_MODULE("kinit")
27
28 extern void
29 lunad_main();
30
31 /**
32  * @brief 创建并运行Lunaix守护进程
33  *
34  */
35 static void
36 spawn_lunad()
37 {
38     int has_error;
39     struct thread* kthread;
40     
41     has_error = spawn_process(&kthread, (ptr_t)lunad_main, false);
42     assert_msg(!has_error, "failed to spawn lunad");
43
44     run(kthread);
45     
46     fail("Unexpected Return");
47 }
48
49 static void
50 kmem_init(struct boot_handoff* bhctx)
51 {
52     pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT);
53
54     ptep = mkl0tep(ptep);
55
56     unsigned int i = ptep_vfn(ptep);
57     do {
58         if (lntep_implie_vmnts(ptep, L0T_SIZE)) {
59             ptep++;
60             continue;
61         }
62
63 #if   LnT_ENABLED(1)
64         assert(mkl1t(ptep++, 0, KERNEL_PGTAB));
65 #elif LnT_ENABLED(2)
66         assert(mkl2t(ptep++, 0, KERNEL_PGTAB));
67 #elif LnT_ENABLED(3)
68         assert(mkl3t(ptep++, 0, KERNEL_PGTAB));
69 #else
70         assert(mklft(ptep++, 0, KERNEL_PGTAB));
71 #endif
72     } while (++i < MAX_PTEN);
73
74     // allocators
75     cake_init();
76     valloc_init();
77 }
78
79 static void
80 __remap_and_load_dtb(struct boot_handoff* bhctx)
81 {
82 #ifdef CONFIG_USE_DEVICETREE
83     ptr_t dtb = bhctx->kexec.dtb_pa;
84
85     if (!dtb) {
86         return;
87     }
88
89     if (va_offset(dtb)) {
90         WARN("bad-aligned dtb location, expect page aligned");
91         return;
92     }
93
94     pte_t *ptep, pte;
95     size_t nr_pages;
96     bool loaded;
97     
98     pte  = mkpte(dtb, KERNEL_DATA);
99     ptep = mkptep_va(VMS_SELF, dtb_start);
100     nr_pages = leaf_count(CONFIG_DTB_MAXSIZE);
101
102     pmm_onhold_range(dtb, nr_pages);
103     vmm_set_ptes_contig(ptep, pte, PAGE_SIZE, nr_pages);
104
105     loaded = dt_load(dtb_start);
106     if (!loaded) {
107         ERROR("dtb load failed");
108     }
109 #endif
110
111     return;
112 }
113
114 static void
115 log_bootup_time()
116 {
117     datetime_t dt;
118
119     clock_walltime(&dt);
120     INFO("kernel boot at: %d/%d/%d %d:%d:%d", 
121             dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
122 }
123
124 void
125 kernel_bootstrap(struct boot_handoff* bhctx)
126 {
127     pmm_init(bhctx);
128     // now we can start reserving physical space
129
130     /* Begin kernel bootstrapping sequence */
131     boot_begin(bhctx);
132
133     /* Setup kernel memory layout and services */
134     kmem_init(bhctx);
135
136     INFO();
137     INFO("Lunaix " CONFIG_LUNAIX_VER " (c) Lunaixsky 2022-2025");
138
139     boot_parse_cmdline(bhctx);
140
141     /* Prepare stack trace environment */
142     trace_modksyms_init(bhctx);
143
144     device_scan_drivers();
145
146     initfn_invoke_sysconf();
147     
148     __remap_and_load_dtb(bhctx);
149     device_sysconf_load();
150
151     // TODO register devtree hooks
152     // TODO re-scan devtree to bind devices.
153
154     clock_init();
155     timer_init();
156     log_bootup_time();
157
158     initfn_invoke_earlyboot();
159
160     vfs_init();
161     fsm_init();
162     input_init();
163     block_init();
164     sched_init();
165
166     device_onboot_load();
167
168     /* the bare metal are now happy, let's get software over with */
169
170     must_success(vfs_mount_root("ramfs", NULL));
171     must_success(vfs_mount("/dev", "devfs", NULL, 0));
172     
173     initfn_invoke_boot();
174
175     /* Finish up bootstrapping sequence, we are ready to spawn the root process
176      * and start geting into uspace
177      */
178     boot_end(bhctx);
179
180     spawn_lunad();
181 }
182