c74f8b3d5179b8d294f5413cb436b05dc530bbae
[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     boot_cleanup();
96
97     spawn_lunad();
98 }
99
100 extern void
101 lunad_main();
102
103 /**
104  * @brief 创建并运行Lunaix守护进程
105  *
106  */
107 void
108 spawn_lunad()
109 {
110     int has_error;
111     struct thread* kthread;
112     
113     has_error = spawn_process(&kthread, (ptr_t)lunad_main, false);
114     assert_msg(!has_error, "failed to spawn lunad");
115
116     run(kthread);
117     
118     fail("Unexpected Return");
119 }
120
121 void
122 kmem_init(struct boot_handoff* bhctx)
123 {
124     pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT);
125     ptep = mkl0tep(ptep);
126
127     do {
128 #if   LnT_ENABLED(1)
129         assert(mkl1t(ptep++, 0, KERNEL_DATA));
130 #elif LnT_ENABLED(2)
131         assert(mkl2t(ptep++, 0, KERNEL_DATA));
132 #elif LnT_ENABLED(3)
133         assert(mkl3t(ptep++, 0, KERNEL_DATA));
134 #else
135         assert(mklft(ptep++, 0, KERNEL_DATA));
136 #endif
137     } while (ptep_vfn(ptep) < MAX_PTEN - 2);
138
139     // allocators
140     cake_init();
141     valloc_init();
142 }