* Remove the debugging hack in procvm.c
[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     pmm_init(bhctx->mem.size);
40     vmm_init();
41
42     /* Begin kernel bootstrapping sequence */
43     boot_begin(bhctx);
44
45     /* Setup kernel memory layout and services */
46     kmem_init(bhctx);
47
48     boot_parse_cmdline(bhctx);
49
50     /* Prepare stack trace environment */
51     trace_modksyms_init(bhctx);
52
53     device_scan_drivers();
54
55     invoke_init_function(on_earlyboot);
56
57     // FIXME this goes to hal/gfxa
58     tty_init(ioremap(0xB8000, PAGE_SIZE));
59     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
60
61     device_sysconf_load();
62
63     /* Get intc online, this is the cornerstone when initing devices */
64     intc_init();
65
66     clock_init();
67     timer_init();
68
69     /*
70         TODO autoload these init function that do not have dependency between
71        them
72     */
73
74     /* Let's get fs online as soon as possible, as things rely on them */
75     vfs_init();
76     fsm_init();
77     input_init();
78     block_init();
79     sched_init();
80
81     device_onboot_load();
82
83     /* the bare metal are now happy, let's get software over with */
84
85     must_success(vfs_mount_root("ramfs", NULL));
86     must_success(vfs_mount("/dev", "devfs", NULL, 0));
87     
88     invoke_init_function(on_boot);
89
90     must_success(vfs_unmount("/dev"));
91
92     /* Finish up bootstrapping sequence, we are ready to spawn the root process
93      * and start geting into uspace
94      */
95     boot_end(bhctx);
96     boot_cleanup();
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 = leaf_count((ptr_t)&__kexec_end - KERNEL_RESIDENT);
128     pmm_mark_chunk_occupied(0, pg_count, PP_FGLOCKED);
129
130     pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT);
131     ptep = mkl0tep(ptep);
132
133     do {
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 (ptep_vfn(ptep) < MAX_PTEN - 2);
144
145     // allocators
146     cake_init();
147     valloc_init();
148 }