move msi-related functionality to generic isrm
[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 void
115 kernel_bootstrap(struct boot_handoff* bhctx)
116 {
117     pmm_init(bhctx);
118     // now we can start reserving physical space
119
120     /* Begin kernel bootstrapping sequence */
121     boot_begin(bhctx);
122
123     /* Setup kernel memory layout and services */
124     kmem_init(bhctx);
125
126     __remap_and_load_dtb(bhctx);
127
128     boot_parse_cmdline(bhctx);
129
130     /* Prepare stack trace environment */
131     trace_modksyms_init(bhctx);
132
133     device_scan_drivers();
134
135     device_sysconf_load();
136
137     invoke_init_function(on_earlyboot);
138
139     clock_init();
140     timer_init();
141
142     /*
143         TODO autoload these init function that do not have dependency between
144        them
145     */
146
147     /* Let's get fs online as soon as possible, as things rely on them */
148     vfs_init();
149     fsm_init();
150     input_init();
151     block_init();
152     sched_init();
153
154     device_onboot_load();
155
156     /* the bare metal are now happy, let's get software over with */
157
158     must_success(vfs_mount_root("ramfs", NULL));
159     must_success(vfs_mount("/dev", "devfs", NULL, 0));
160     
161     invoke_init_function(on_boot);
162
163     /* Finish up bootstrapping sequence, we are ready to spawn the root process
164      * and start geting into uspace
165      */
166     boot_end(bhctx);
167
168     spawn_lunad();
169 }
170