kernel/kernel/main.c

183 lines
5.3 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>
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;
2017-11-04 10:38:28 +00:00
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
) {
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-12 14:00:17 +00:00
kernaux_memset(&kinfo, 0, sizeof(struct Kernel_Info));
2017-11-04 10:38:28 +00:00
2020-12-01 00:01:47 +00:00
KernAux_PFA_initialize(&pfa);
{
const char *const cmdline =
KernAux_Multiboot2_boot_cmd_line(multiboot2_info);
if (cmdline) {
2020-11-30 01:03:45 +00:00
unsigned int slen = kernaux_strlen(cmdline);
2021-12-12 14:00:17 +00:00
if (slen > KERNEL_INFO_CMDLINE_SLEN_MAX) {
panic("Multiboot 2 boot cmd line is too long.");
}
2020-11-30 01:03:45 +00:00
kernaux_strncpy(kinfo.cmdline, cmdline, slen);
}
}
{
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.");
}
2020-11-30 01:03:45 +00:00
unsigned int slen = kernaux_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];
2020-11-30 01:03:45 +00:00
kernaux_strncpy(module->cmdline, tag->cmdline, slen);
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;
}
kinfo.kernel_offset = (unsigned long)&_kernel_offset;
kinfo.kernel_size = (unsigned long)&_kernel_size;
kinfo.kernel_phys_base = (unsigned long)&_kernel_phys_base;
kinfo.kernel_virt_base = (unsigned long)&_kernel_virt_base;
2017-11-04 10:38:28 +00:00
kinfo.kernel_phys_limit = kinfo.kernel_phys_base + kinfo.kernel_size - 1;
kinfo.kernel_virt_limit = kinfo.kernel_virt_base + kinfo.kernel_size - 1;
2017-11-05 07:13:54 +00:00
kinfo.kernel_and_modules_total_size = kinfo.kernel_size + kinfo.modules_total_size;
kinfo.kernel_stack_top = (unsigned long)&_kernel_stack_top;
2017-11-04 02:32:23 +00:00
2017-11-04 15:08:59 +00:00
paging_clear();
paging_identity();
/* kinfo.freepde_start = */ paging_mapkernel(&kinfo);
2017-11-04 15:08:59 +00:00
paging_load();
paging_enable();
2021-12-12 14:00:17 +00:00
assert(kernel_info_validate(&kinfo), "Invalid kernel information.");
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
}