/
main.c
376 lines (287 loc) · 9.28 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* SPDX-License-Identifier: BSD-2-Clause */
#include <tilck_gen_headers/config_debug.h>
#include <tilck_gen_headers/mod_console.h>
#include <tilck_gen_headers/mod_fb.h>
#include <tilck_gen_headers/mod_acpi.h>
#include <tilck/common/basic_defs.h>
#include <tilck/common/string_util.h>
#include <tilck/common/printk.h>
#include <tilck/common/utils.h>
#include <multiboot.h>
#include <tilck/kernel/modules.h>
#include <tilck/kernel/hal.h>
#include <tilck/kernel/irq.h>
#include <tilck/kernel/kmalloc.h>
#include <tilck/kernel/debug_utils.h>
#include <tilck/kernel/sched.h>
#include <tilck/kernel/elf_loader.h>
#include <tilck/kernel/worker_thread.h>
#include <tilck/kernel/fs/fat32.h>
#include <tilck/kernel/fs/devfs.h>
#include <tilck/kernel/timer.h>
#include <tilck/kernel/syscalls.h>
#include <tilck/kernel/system_mmap.h>
#include <tilck/kernel/elf_utils.h>
#include <tilck/kernel/cmdline.h>
#include <tilck/kernel/self_tests.h>
#include <tilck/kernel/term.h>
#include <tilck/kernel/process.h>
#include <tilck/kernel/fs/kernelfs.h>
#include <tilck/kernel/fs/vfs.h>
#include <tilck/mods/console.h>
#include <tilck/mods/fb_console.h>
#include <tilck/mods/serial.h>
#include <tilck/mods/acpi.h>
#include <3rd_party/acpi/acpi.h>
#include <3rd_party/acpi/acexcep.h>
static bool read_multiboot_info_passed;
static u32 saved_multiboot_magic;
static multiboot_info_t *saved_multiboot_mbi;
static void read_multiboot_info(void);
static void
save_multiboot_info(u32 magic, u32 mbi)
{
saved_multiboot_magic = magic;
saved_multiboot_mbi = TO_PTR(mbi);
}
static void
init_video_console(void)
{
if (in_panic()) {
/*
* We're in panic and there's no video console yet. Well, we can
* initialize it, but we have to check if we at least read the multiboot
* info first.
*/
if (!read_multiboot_info_passed)
read_multiboot_info();
}
if (!use_framebuffer()) {
if (MOD_console) {
/*
* NOTE: since we got here, we're sure that we have the console module
* built-in, but we still have to put the call under an if, in order
* to remove the link dependency on that symbol in case the console
* module is _not_ built-in.
*/
if (MOD_acpi &&
get_acpi_init_status() >= ais_tables_initialized &&
acpi_is_vga_text_mode_avail() == tri_no)
{
/* no text mode on this machine */
init_first_term_null();
} else {
/*
* No ACPI or ACPI tables are not initialized (panic?) or
* vga text mode is available or ACPI's FADT is too old to tell
* whether text mode is available or not so we assume it is.
*/
init_textmode_console();
}
}
return;
}
/* framebuffer console */
if (!MOD_fb) {
/*
* This compile-time check (MOD_fb will evalute as 0 or 1) instead, is
* strictly necessary, as the kernel could be loaded in graphical mode
* without having the `fb` module built-in. In this case, we just won't
* display anything on the screen.
*/
init_first_term_null();
return;
}
init_fb_console();
}
void
init_console(void)
{
printk("*** Init the kernel console\n");
if (kopt_serial_console) {
if (!serial_term_intf) {
/*
* We have to use the serial console, but the module is not built-in:
* disable it and display an error on video, if we can. Otherwise,
* we're simply doomed.
*/
if (!video_term_intf)
goto we_are_doomed;
kopt_serial_console = false;
panic("Unable to init the serial console without the serial module!");
}
init_first_serial_term(COM1);
printk_flush_ringbuf();
return;
}
if (!video_term_intf) {
/*
* We have to use the video console, but the module is not built-in:
* disable it and send an error on the serial port, if we can. Otherwise,
* we're simply doomed.
*/
if (!serial_term_intf)
goto we_are_doomed;
kopt_serial_console = true;
panic("Unable to init the video console without the console module!");
}
init_video_console();
printk_flush_ringbuf();
return;
we_are_doomed:
disable_interrupts_forced();
while (1)
halt();
}
static void
read_multiboot_info(void)
{
multiboot_info_t *mbi = saved_multiboot_mbi;
if (read_multiboot_info_passed)
return; /* We're probably in panic: just do nothing */
if (saved_multiboot_magic != MULTIBOOT_BOOTLOADER_MAGIC) {
if (in_panic())
goto out; /* We're already in panic: just fail silently */
panic("The Tilck kernel requires a multiboot-compatible bootloader");
}
if (~mbi->flags & MULTIBOOT_INFO_MEM_MAP) {
if (in_panic())
goto out; /* We're already in panic: just fail silently */
panic("No memory map in the multiboot info struct");
}
if (mbi->flags & MULTIBOOT_INFO_BOOT_LOADER_NAME) {
const char *name = TO_PTR(mbi->boot_loader_name);
if (!strcmp(name, "TILCK_EFI")) {
printk("Multiboot: detected the TILCK_EFI bootloader\n");
printk("Multiboot: ACPI root ptr: %p\n", TO_PTR(mbi->apm_table));
acpi_set_root_pointer(mbi->apm_table);
}
}
/* Loading ramdisk(s) is not even worth considering if we're in panic */
if (!in_panic()) {
if (mbi->flags & MULTIBOOT_INFO_MODS) {
multiboot_module_t *mods = TO_PTR(mbi->mods_addr);
for (u32 i = 0; i < mbi->mods_count; i++)
system_mmap_add_ramdisk(mods[i].mod_start, mods[i].mod_end);
}
}
/* Framebuffer info is essential, even if we're in panic */
if (MOD_fb) {
if (mbi->flags & MULTIBOOT_INFO_FRAMEBUFFER_INFO)
if (mbi->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_RGB)
set_framebuffer_info_from_mbi(mbi);
}
/* Set system's memory map based on what was provided to us via multiboot */
system_mmap_set(mbi);
/* It's safer to stick to the default settings, when we're in panic */
if (!in_panic()) {
if (mbi->flags & MULTIBOOT_INFO_CMDLINE)
parse_kernel_cmdline(TO_PTR(mbi->cmdline));
}
out:
read_multiboot_info_passed = true;
/* Reset the temporary multiboot variables as they won't be used again */
saved_multiboot_magic = 0;
saved_multiboot_mbi = NULL;
}
static void
mount_initrd(void)
{
/* declare the ramfs_create() function */
struct fs *ramfs_create(void);
struct fs *initrd, *ramfs;
void *ramdisk;
size_t ramdisk_size;
int rc;
if (system_mmap_get_ramdisk(0, &ramdisk, &ramdisk_size) < 0)
panic("system_mmap_get_ramdisk_vaddr(0) failed");
if (!(ramfs = ramfs_create()))
panic("Unable to create ramfs");
if ((rc = mp_init(ramfs)))
panic("mp_init() failed with error: %d", rc);
if ((rc = vfs_mkdir("/tmp", 0777)))
panic("vfs_mkdir(\"/tmp\") failed with error: %d", rc);
/* Set kernel's process `cwd` to the root folder */
{
struct vfs_path tp;
struct process *pi = kernel_process_pi;
ASSERT(pi == get_curr_proc());
tp.fs = mp_get_root();
vfs_get_root_entry(tp.fs, &tp.fs_path);
process_set_cwd2_nolock_raw(pi, &tp);
}
if (LIKELY(ramdisk != NULL)) {
if (!(initrd = fat_mount_ramdisk(ramdisk, ramdisk_size, 0)))
panic("Unable to mount the initrd fat32 RAMDISK");
if ((rc = vfs_mkdir("/initrd", 0777)))
panic("vfs_mkdir(\"/initrd\") failed with error: %d", rc);
if ((rc = mp_add(initrd, "/initrd")))
panic("mp_add() failed with error: %d", rc);
} else {
printk("[WARNING] No RAMDISK found.\n");
}
}
static void
run_init_or_selftest(void)
{
if (self_test_to_run) {
if (KERNEL_SELFTESTS) {
self_test_to_run();
return;
}
panic("The kernel was not compiled with self-tests");
} else {
if (system_mmap_get_ramdisk(0, NULL, NULL) < 0)
panic("No ramdisk and no selftest requested: nothing to do.");
/* Run init or whatever program was passed in the cmdline */
long rc = first_execve(cmd_args[0], cmd_args);
if (rc != 0)
panic("execve('%s') failed with %i\n", cmd_args[0], rc);
}
}
static void do_async_init()
{
/* declare the show_hello_message() function */
void show_hello_message(void);
mount_initrd();
init_devfs();
init_modules();
init_extra_debug_features();
show_hello_message();
run_init_or_selftest();
}
static void
async_init(void)
{
if (kthread_create(&do_async_init, KTH_ALLOC_BUFS, NULL) < 0)
panic("Unable to create a kthread for do_async_init()");
}
void
kmain(u32 multiboot_magic, u32 mbi_addr)
{
call_kernel_global_ctors();
save_multiboot_info(multiboot_magic, mbi_addr);
early_init_serial_ports();
init_cpu_exception_handling();
early_init_paging();
early_init_kmalloc();
read_multiboot_info();
enable_cpu_features();
kmain_early_checks();
init_segmentation();
init_fpu_memcpy();
init_kmalloc();
init_paging();
acpi_mod_init_tables();
init_console();
init_self_tests();
init_irq_handling();
init_sched();
init_syscall_interfaces();
init_worker_threads();
init_timer();
init_system_time();
init_kernelfs();
async_init();
schedule();
}