kernel/kernel/main.c

169 lines
4.7 KiB
C
Raw Normal View History

2017-11-04 15:08:59 +00:00
#include "paging.h"
2017-11-04 02:32:23 +00:00
2017-11-09 16:00:36 +00:00
#include "info.h"
2020-11-26 18:50:25 +00:00
#include "panic.h"
#include "protected.h"
#include "tasks.h"
#include "elf.h"
2020-12-06 02:49:59 +00:00
#include <kernaux/console.h>
2021-12-15 02:10:49 +00:00
#include <kernaux/libc.h>
2020-11-28 00:19:36 +00:00
#include <kernaux/multiboot2.h>
2020-12-01 00:01:47 +00:00
#include <kernaux/pfa.h>
2020-11-30 01:03:45 +00:00
#include <kernaux/stdlib.h>
2020-11-28 00:19:36 +00:00
// Defined in linker script
extern char _kernel_offset;
extern char _kernel_size;
extern char _kernel_phys_base;
extern char _kernel_virt_base;
extern char _kernel_stack_top;
2017-11-04 10:38:28 +00:00
2021-12-12 14:00:17 +00:00
static struct Kernel_Info kinfo;
2020-12-01 00:01:47 +00:00
static struct KernAux_PFA pfa;
2020-11-29 16:33:09 +00:00
void main(
const unsigned long multiboot2_magic,
const struct KernAux_Multiboot2 *const multiboot2_info
) {
2021-12-14 23:53:05 +00:00
kernaux_assert_cb = kernaux_assert_fn;
2020-11-29 16:33:09 +00:00
if (multiboot2_magic != KERNAUX_MULTIBOOT2_MAGIC) {
2020-11-26 18:52:35 +00:00
panic("Multiboot 2 magic number is invalid.");
}
2017-11-04 11:45:48 +00:00
2020-12-06 02:49:59 +00:00
KernAux_Multiboot2_print(multiboot2_info, kernaux_console_printf);
2020-11-28 01:38:32 +00:00
2020-11-29 16:33:09 +00:00
if (!KernAux_Multiboot2_is_valid(multiboot2_info)) {
2020-11-28 03:24:03 +00:00
panic("Multiboot 2 info is invalid.");
}
2021-12-17 23:26:56 +00:00
kernel_info_init_start(
2021-12-17 22:45:22 +00:00
&kinfo,
(size_t)&_kernel_offset,
(size_t)&_kernel_size,
(size_t)&_kernel_phys_base,
(size_t)&_kernel_virt_base,
(size_t)&_kernel_stack_top
);
2021-12-17 22:35:56 +00:00
2021-12-17 22:45:22 +00:00
KernAux_PFA_initialize(&pfa);
2021-12-17 22:35:56 +00:00
2021-12-18 02:29:25 +00:00
kernel_info_init_cmdline(&kinfo,
KernAux_Multiboot2_boot_cmd_line(multiboot2_info));
{
const struct KernAux_Multiboot2_Tag_MemoryMap *const tag =
(struct KernAux_Multiboot2_Tag_MemoryMap*)
KernAux_Multiboot2_first_tag_with_type(
multiboot2_info,
KERNAUX_MULTIBOOT2_TAGTYPE_MEMORY_MAP
);
if (!tag) {
panic("No memory map provided in Multiboot 2 info.");
}
for (
const struct KernAux_Multiboot2_Tag_MemoryMap_EntryBase *entry =
(struct KernAux_Multiboot2_Tag_MemoryMap_EntryBase*)tag->data;
(unsigned char*)entry < (unsigned char*)tag + tag->base.size;
entry =
(struct KernAux_Multiboot2_Tag_MemoryMap_EntryBase*)
((unsigned char*)entry + tag->entry_size)
) {
2020-12-01 00:01:47 +00:00
if (entry->type == 1) {
KernAux_PFA_mark_available(
&pfa,
entry->base_addr,
entry->base_addr + entry->length - 1
);
}
2021-12-12 14:00:17 +00:00
if (kinfo.areas_count >= KERNEL_INFO_AREAS_MAX) {
panic("Too many memory map entries in Multiboot 2 info.");
}
2021-12-12 14:00:17 +00:00
struct Kernel_Info_Area *const area =
&kinfo.areas[kinfo.areas_count];
area->base = entry->base_addr;
area->size = entry->length;
area->limit = area->base + area->size - 1;
area->is_available = entry->type == 1;
++kinfo.areas_count;
}
}
for (
const struct KernAux_Multiboot2_Tag_Module *tag =
(struct KernAux_Multiboot2_Tag_Module*)
KernAux_Multiboot2_first_tag_with_type(
multiboot2_info,
KERNAUX_MULTIBOOT2_TAGTYPE_MODULE
);
tag;
tag = (struct KernAux_Multiboot2_Tag_Module*)
KernAux_Multiboot2_tag_with_type_after(
multiboot2_info,
KERNAUX_MULTIBOOT2_TAGTYPE_MODULE,
(struct KernAux_Multiboot2_TagBase*)tag
)
) {
2021-12-12 14:00:17 +00:00
if (kinfo.modules_count >= KERNEL_INFO_MODULES_MAX) {
panic("Too many modules in Multiboot 2 info.");
}
2021-12-15 02:10:49 +00:00
unsigned int slen = strlen(tag->cmdline);
2021-12-12 14:00:17 +00:00
if (slen > KERNEL_INFO_CMDLINE_SLEN_MAX) {
panic("Multiboot 2 module cmd line is too long.");
}
2021-12-12 14:00:17 +00:00
struct Kernel_Info_Module *const module =
&kinfo.modules[kinfo.modules_count];
2021-12-15 02:10:49 +00:00
strcpy(module->cmdline, tag->cmdline);
2020-11-29 21:31:30 +00:00
module->base = tag->mod_start;
module->limit = tag->mod_end;
module->size = module->limit - module->base + 1;
++kinfo.modules_count;
kinfo.modules_total_size += module->size;
}
2021-12-18 02:42:03 +00:00
kernel_info_init_finish(&kinfo);
2021-12-17 23:05:01 +00:00
kernel_info_print(&kinfo);
2021-12-18 02:48:27 +00:00
assert(kernel_info_is_valid(&kinfo), "Invalid kernel information.");
2021-12-17 23:05:01 +00:00
2017-11-04 15:08:59 +00:00
paging_clear();
paging_identity();
2021-12-18 02:49:17 +00:00
paging_mapkernel(&kinfo);
2017-11-04 15:08:59 +00:00
paging_load();
paging_enable();
2020-11-26 18:50:25 +00:00
protected_initialize(&kinfo);
if (kinfo.modules_count > 0) {
2021-12-12 14:00:17 +00:00
const struct Kernel_ELF_Header *const elf_header =
2020-11-26 18:50:25 +00:00
(void*)kinfo.modules[0].base;
2021-12-12 14:00:17 +00:00
if (Kernel_ELF_Header_is_valid(elf_header)) {
2020-11-26 18:50:25 +00:00
const unsigned long real_entrypoint =
kinfo.modules[0].base + elf_header->entrypoint;
tasks_switch_to_user(real_entrypoint);
}
else {
2020-12-06 03:04:34 +00:00
kernaux_console_print("[WARN] init: Invalid ELF header.\n");
2020-11-26 18:50:25 +00:00
}
}
2017-11-04 02:32:23 +00:00
}