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