feat: dynamic boot medium probing and mounting
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <lunaix/block.h>
2 #include <lunaix/common.h>
3 #include <lunaix/foptions.h>
4 #include <lunaix/fs.h>
5 #include <lunaix/fs/probe_boot.h>
6 #include <lunaix/fs/twifs.h>
7 #include <lunaix/ld.h>
8 #include <lunaix/lxconsole.h>
9 #include <lunaix/mm/cake.h>
10 #include <lunaix/mm/pmm.h>
11 #include <lunaix/mm/valloc.h>
12 #include <lunaix/mm/vmm.h>
13 #include <lunaix/peripheral/ps2kbd.h>
14 #include <lunaix/peripheral/serial.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/syscall.h>
17 #include <lunaix/syslog.h>
18 #include <lunaix/types.h>
19
20 #include <sdbg/protocol.h>
21
22 #include <hal/acpi/acpi.h>
23 #include <hal/ahci/ahci.h>
24 #include <hal/apic.h>
25 #include <hal/ioapic.h>
26 #include <hal/pci.h>
27 #include <hal/rtc.h>
28
29 #include <arch/x86/boot/multiboot.h>
30
31 #include <klibc/string.h>
32
33 LOG_MODULE("PROC0")
34
35 void
36 init_platform();
37
38 void
39 lock_reserved_memory();
40
41 void
42 unlock_reserved_memory();
43
44 void
45 __do_reserved_memory(int unlock);
46
47 int
48 mount_bootmedium()
49 {
50     struct v_dnode* dnode;
51     int errno = 0;
52     struct device* dev = probe_boot_medium();
53     if (!dev) {
54         kprintf(KERROR "fail to acquire device. (%d)", errno);
55         return 0;
56     }
57
58     if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) {
59         kprintf(KERROR "fail to mount boot medium. (%d)", errno);
60         return 0;
61     }
62
63     return 1;
64 }
65
66 int
67 exec_initd()
68 {
69     int errno = 0;
70     struct ld_param param;
71     char filename[] = "/mnt/lunaix-os/usr/init";
72
73     ld_create_param(&param, __current, VMS_SELF);
74
75     if ((errno = exec_load_byname(&param, filename, NULL, NULL))) {
76         goto fail;
77     }
78
79     // user space
80     asm volatile("movw %0, %%ax\n"
81                  "movw %%ax, %%es\n"
82                  "movw %%ax, %%ds\n"
83                  "movw %%ax, %%fs\n"
84                  "movw %%ax, %%gs\n"
85                  "pushl %0\n"
86                  "pushl %1\n"
87                  "pushl %2\n"
88                  "pushl %3\n"
89                  "retf" ::"i"(UDATA_SEG),
90                  "r"(param.info.stack_top),
91                  "i"(UCODE_SEG),
92                  "r"(param.info.entry)
93                  : "eax", "memory");
94
95     fail("should not reach");
96
97 fail:
98     kprintf(KERROR "fail to load initd. (%d)", errno);
99     return 0;
100 }
101
102 /**
103  * @brief LunaixOS的零号进程,该进程永远为可执行。
104  *
105  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
106  *
107  * 同时,该进程也负责fork出我们的init进程。
108  *
109  */
110 void
111 __proc0()
112 {
113     init_platform();
114
115     init_proc_user_space(__current);
116
117     if (!mount_bootmedium() || !exec_initd()) {
118         while (1) {
119             asm("hlt");
120         }
121         // should not reach
122     }
123 }
124
125 extern uint8_t __kernel_start;            /* link/linker.ld */
126 extern uint8_t __kernel_end;              /* link/linker.ld */
127 extern uint8_t __init_hhk_end;            /* link/linker.ld */
128 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
129
130 void
131 init_platform()
132 {
133     kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
134             __VERSION__,
135             __TIME__);
136
137     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
138     lock_reserved_memory();
139
140     // firmware
141     acpi_init(_k_init_mb_info);
142
143     // die
144     apic_init();
145     ioapic_init();
146
147     // debugger
148     serial_init();
149     sdbg_init();
150
151     // timers & clock
152     rtc_init();
153     timer_init(SYS_TIMER_FREQUENCY_HZ);
154     clock_init();
155
156     // peripherals & chipset features
157     ps2_kbd_init();
158     block_init();
159     ahci_init();
160
161     pci_init();
162
163     // console
164     console_start_flushing();
165     console_flush();
166
167     // expose cake allocator states to vfs
168     cake_export();
169
170     unlock_reserved_memory();
171
172     // clean up
173     for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
174         vmm_del_mapping(VMS_SELF, (void*)i);
175         pmm_free_page(KERNEL_PID, (void*)i);
176     }
177 }
178
179 void
180 lock_reserved_memory()
181 {
182     __do_reserved_memory(0);
183 }
184
185 void
186 unlock_reserved_memory()
187 {
188     __do_reserved_memory(1);
189 }
190
191 void
192 __do_reserved_memory(int unlock)
193 {
194     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
195     size_t map_size =
196       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
197     // v_mapping mapping;
198     for (unsigned int i = 0; i < map_size; i++) {
199         multiboot_memory_map_t mmap = mmaps[i];
200         uint8_t* pa = PG_ALIGN(mmap.addr_low);
201         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
202             // Don't fuck up our kernel code or any free area!
203             continue;
204         }
205         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
206         size_t j = 0;
207         if (!unlock) {
208             kprintf("mem: freeze: %p..%p type=%x\n",
209                     pa,
210                     pa + pg_num * PG_SIZE,
211                     mmap.type);
212             for (; j < pg_num; j++) {
213                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
214                 if (_pa >= KERNEL_MM_BASE) {
215                     // Don't fuck up our kernel space!
216                     break;
217                 }
218                 vmm_set_mapping(VMS_SELF, _pa, _pa, PG_PREM_R, VMAP_NULL);
219                 pmm_mark_page_occupied(
220                   KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
221             }
222             // Save the progress for later unmapping.
223             mmaps[i].len_low = j * PG_SIZE;
224         } else {
225             kprintf("mem: reclaim: %p..%p type=%x\n",
226                     pa,
227                     pa + pg_num * PG_SIZE,
228                     mmap.type);
229             for (; j < pg_num; j++) {
230                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
231                 vmm_del_mapping(VMS_SELF, _pa);
232                 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
233                     pmm_mark_page_free(_pa >> PG_SIZE_BITS);
234                 }
235             }
236         }
237     }
238 }