2d911bd1318de4d885bf7d506baf7abfad3530bc
[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/page.h>
11 #include <lunaix/mm/pmm.h>
12 #include <lunaix/mm/valloc.h>
13 #include <lunaix/mm/vmm.h>
14 #include <lunaix/process.h>
15 #include <lunaix/sched.h>
16 #include <lunaix/spike.h>
17 #include <lunaix/trace.h>
18 #include <lunaix/tty/tty.h>
19 #include <lunaix/owloysius.h>
20 #include <lunaix/pcontext.h>
21
22 #include <hal/acpi/acpi.h>
23 #include <hal/intc.h>
24
25 #include <sys/abi.h>
26 #include <sys/mm/mempart.h>
27
28 #include <klibc/strfmt.h>
29 #include <klibc/string.h>
30
31 void
32 spawn_lunad();
33
34 void
35 kmem_init(struct boot_handoff* bhctx);
36
37 void
38 kernel_bootstrap(struct boot_handoff* bhctx)
39 {
40     pmm_init(bhctx->mem.size);
41     vmm_init();
42
43     /* Begin kernel bootstrapping sequence */
44     boot_begin(bhctx);
45
46     /* Setup kernel memory layout and services */
47     kmem_init(bhctx);
48
49     boot_parse_cmdline(bhctx);
50
51     /* Prepare stack trace environment */
52     trace_modksyms_init(bhctx);
53
54     device_scan_drivers();
55
56     invoke_init_function(on_earlyboot);
57
58     // FIXME this goes to hal/gfxa
59     tty_init(ioremap(0xB8000, PG_SIZE));
60     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
61
62     device_sysconf_load();
63
64     /* Get intc online, this is the cornerstone when initing devices */
65     intc_init();
66
67     clock_init();
68     timer_init();
69
70     /*
71         TODO autoload these init function that do not have dependency between
72        them
73     */
74
75     /* Let's get fs online as soon as possible, as things rely on them */
76     vfs_init();
77     fsm_init();
78     input_init();
79     block_init();
80     sched_init();
81
82     device_onboot_load();
83
84     /* the bare metal are now happy, let's get software over with */
85
86     must_success(vfs_mount_root("ramfs", NULL));
87     must_success(vfs_mount("/dev", "devfs", NULL, 0));
88     
89     invoke_init_function(on_boot);
90
91     must_success(vfs_unmount("/dev"));
92
93     /* Finish up bootstrapping sequence, we are ready to spawn the root process
94      * and start geting into uspace
95      */
96     boot_end(bhctx);
97
98     spawn_lunad();
99 }
100
101 extern void
102 lunad_main();
103
104 /**
105  * @brief 创建并运行Lunaix守护进程
106  *
107  */
108 void
109 spawn_lunad()
110 {
111     int has_error;
112     struct thread* kthread;
113     
114     has_error = spawn_process(&kthread, (ptr_t)lunad_main, false);
115     assert_msg(!has_error, "failed to spawn lunad");
116
117     run(kthread);
118     
119     fail("Unexpected Return");
120 }
121
122 void
123 kmem_init(struct boot_handoff* bhctx)
124 {
125     extern u8_t __kexec_end;
126     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
127     size_t pg_count = ((ptr_t)&__kexec_end - KERNEL_EXEC) >> PG_SIZE_BITS;
128     pmm_mark_chunk_occupied(0, pg_count, PP_FGLOCKED);
129
130     // reserve higher half
131     for (size_t i = L1_INDEX(KERNEL_EXEC); i < 1023; i++) {
132         assert(vmm_set_mapping(VMS_SELF, i << 22, 0, 0, VMAP_NOMAP));
133     }
134
135     // allocators
136     cake_init();
137     valloc_init();
138 }