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