Architectural Support: x86_64 (#37)
[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     tty_init(ioremap(0xB8000, PAGE_SIZE));
47     
48     /* Setup kernel memory layout and services */
49     kmem_init(bhctx);
50
51     // FIXME this goes to hal/gfxa
52     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
53
54     boot_parse_cmdline(bhctx);
55
56     /* Prepare stack trace environment */
57     trace_modksyms_init(bhctx);
58
59     device_scan_drivers();
60
61     device_sysconf_load();
62
63     invoke_init_function(on_earlyboot);
64
65     clock_init();
66     timer_init();
67
68     /*
69         TODO autoload these init function that do not have dependency between
70        them
71     */
72
73     /* Let's get fs online as soon as possible, as things rely on them */
74     vfs_init();
75     fsm_init();
76     input_init();
77     block_init();
78     sched_init();
79
80     device_onboot_load();
81
82     /* the bare metal are now happy, let's get software over with */
83
84     must_success(vfs_mount_root("ramfs", NULL));
85     must_success(vfs_mount("/dev", "devfs", NULL, 0));
86     
87     invoke_init_function(on_boot);
88
89     must_success(vfs_unmount("/dev"));
90
91     /* Finish up bootstrapping sequence, we are ready to spawn the root process
92      * and start geting into uspace
93      */
94     boot_end(bhctx);
95
96     spawn_lunad();
97 }
98
99 extern void
100 lunad_main();
101
102 /**
103  * @brief 创建并运行Lunaix守护进程
104  *
105  */
106 void
107 spawn_lunad()
108 {
109     int has_error;
110     struct thread* kthread;
111     
112     has_error = spawn_process(&kthread, (ptr_t)lunad_main, false);
113     assert_msg(!has_error, "failed to spawn lunad");
114
115     run(kthread);
116     
117     fail("Unexpected Return");
118 }
119
120 void
121 kmem_init(struct boot_handoff* bhctx)
122 {
123     pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT);
124
125     ptep = mkl0tep(ptep);
126
127     unsigned int i = ptep_vfn(ptep);
128     do {
129         if (l0tep_impile_vmnts(ptep)) {
130             ptep++;
131             continue;
132         }
133
134 #if   LnT_ENABLED(1)
135         assert(mkl1t(ptep++, 0, KERNEL_DATA));
136 #elif LnT_ENABLED(2)
137         assert(mkl2t(ptep++, 0, KERNEL_DATA));
138 #elif LnT_ENABLED(3)
139         assert(mkl3t(ptep++, 0, KERNEL_DATA));
140 #else
141         assert(mklft(ptep++, 0, KERNEL_DATA));
142 #endif
143     } while (++i < MAX_PTEN);
144
145     // allocators
146     cake_init();
147     valloc_init();
148 }