feat: initial booting kernel, copy libc stuff from myke
This commit is contained in:
13
yak-kernel/src/rt/kmain.c
Normal file
13
yak-kernel/src/rt/kmain.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <yak/rt/kmain.h>
|
||||
#include <yak/rt/panic.h>
|
||||
#include "yak/platform/generic/platform.h"
|
||||
|
||||
void kmain() {
|
||||
// kmain is called from one of the bootloader implementations
|
||||
|
||||
// perform platform specific initialisation
|
||||
platform_init();
|
||||
|
||||
// this should (eventually) be unreachable
|
||||
panic("End of kmain");
|
||||
}
|
||||
44
yak-kernel/src/rt/kprint.c
Normal file
44
yak-kernel/src/rt/kprint.c
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by rick on 21-3-23.
|
||||
//
|
||||
|
||||
#include <yak/rt/kprint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define MAX_PRINT_FUNCTIONS 8
|
||||
|
||||
print_function print_functions[MAX_PRINT_FUNCTIONS] = {0};
|
||||
|
||||
void kprint_now(char* msg) {
|
||||
char* m = msg;
|
||||
while (*m != 0) {
|
||||
kprint_putc_now(*m);
|
||||
m++;
|
||||
}
|
||||
}
|
||||
|
||||
void kprint_putc_now(char c) {
|
||||
for (int i = 0; i < MAX_PRINT_FUNCTIONS; ++i) {
|
||||
if (print_functions[i] != NULL) {
|
||||
print_functions[i](c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void kprint_register(print_function print) {
|
||||
for (int i = 0; i < MAX_PRINT_FUNCTIONS; ++i) {
|
||||
if (print_functions[i] == NULL) {
|
||||
print_functions[i] = print;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void kprint_unregister(print_function print) {
|
||||
for (int i = 0; i < MAX_PRINT_FUNCTIONS; ++i) {
|
||||
if (print_functions[i] == print) {
|
||||
print_functions[i] = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
114
yak-kernel/src/rt/limine/limine.c
Normal file
114
yak-kernel/src/rt/limine/limine.c
Normal file
@@ -0,0 +1,114 @@
|
||||
//
|
||||
// Created by rick on 21-3-23.
|
||||
//
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <limine.h>
|
||||
#include <yak/rt/kprint.h>
|
||||
#include <yak/rt/kmain.h>
|
||||
|
||||
static struct limine_bootloader_info_request limine_bootloader_info_request = {
|
||||
.id = LIMINE_BOOTLOADER_INFO_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
static struct limine_framebuffer_request limine_framebuffer_request = {
|
||||
.id = LIMINE_FRAMEBUFFER_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
static struct limine_terminal_request limine_terminal_request = {
|
||||
.id = LIMINE_TERMINAL_REQUEST,
|
||||
.revision = 0
|
||||
};
|
||||
|
||||
// unused: 5 level paging
|
||||
// unused: SMP
|
||||
|
||||
struct limine_memmap_request limine_memmap_request = {
|
||||
.id = LIMINE_MEMMAP_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
void limine_init();
|
||||
struct limine_entry_point_request limine_entry_point_request = {
|
||||
.id = LIMINE_ENTRY_POINT_REQUEST,
|
||||
.revision = 0,
|
||||
.entry = limine_init,
|
||||
};
|
||||
|
||||
struct limine_kernel_file_request limine_kernel_file_request = {
|
||||
.id = LIMINE_KERNEL_FILE_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
struct limine_module_request limine_module_request = {
|
||||
.id = LIMINE_MODULE_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
struct limine_rsdp_request limine_rsdp_request = {
|
||||
.id = LIMINE_RSDP_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
struct limine_smbios_request limine_smbios_request = {
|
||||
.id = LIMINE_SMBIOS_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
struct limine_efi_system_table_request limine_efi_system_table_request = {
|
||||
.id = LIMINE_EFI_SYSTEM_TABLE_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
struct limine_boot_time_request limine_boot_time_request = {
|
||||
.id = LIMINE_BOOT_TIME_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
struct limine_kernel_address_request limine_kernel_address_request = {
|
||||
.id = LIMINE_KERNEL_ADDRESS_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
struct limine_dtb_request limine_dtb_request = {
|
||||
.id = LIMINE_DTB_REQUEST,
|
||||
.revision = 0,
|
||||
};
|
||||
|
||||
static volatile __attribute__((used, section(".limine_reqs"))) void* limine_requests[] = {
|
||||
&limine_bootloader_info_request,
|
||||
&limine_framebuffer_request,
|
||||
&limine_terminal_request,
|
||||
&limine_memmap_request,
|
||||
&limine_entry_point_request,
|
||||
&limine_kernel_file_request,
|
||||
&limine_module_request,
|
||||
&limine_rsdp_request,
|
||||
&limine_smbios_request,
|
||||
&limine_efi_system_table_request,
|
||||
&limine_boot_time_request,
|
||||
&limine_kernel_address_request,
|
||||
&limine_dtb_request,
|
||||
NULL,
|
||||
};
|
||||
|
||||
void limine_terminal_kprint(char c) {
|
||||
limine_terminal_request.response->write(limine_terminal_request.response->terminals[0], &c, 1);
|
||||
}
|
||||
|
||||
void limine_init() {
|
||||
if (limine_terminal_request.response != NULL && limine_terminal_request.response->terminal_count >= 1) {
|
||||
kprint_register(limine_terminal_kprint);
|
||||
}
|
||||
|
||||
if (limine_bootloader_info_request.response != NULL) {
|
||||
printf("Booted using limine from %s %s\n", limine_bootloader_info_request.response->name, limine_bootloader_info_request.response->version);
|
||||
} else {
|
||||
printf("Booted using limine from an unknown bootloader\n");
|
||||
}
|
||||
|
||||
kmain();
|
||||
}
|
||||
15
yak-kernel/src/rt/panic.c
Normal file
15
yak-kernel/src/rt/panic.c
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by rick on 21-3-23.
|
||||
//
|
||||
|
||||
#include <yak/rt/panic.h>
|
||||
#include "yak/rt/kprint.h"
|
||||
#include "yak/platform/generic/platform.h"
|
||||
|
||||
void __attribute__((__noreturn__)) panic(char *reason) {
|
||||
kprint_now(reason);
|
||||
|
||||
// todo stack trace
|
||||
|
||||
halt_forever();
|
||||
}
|
||||
21
yak-kernel/src/rt/stack_protection.c
Normal file
21
yak-kernel/src/rt/stack_protection.c
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by rick on 21-3-23.
|
||||
//
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <yak/rt/panic.h>
|
||||
|
||||
#if UINT32_MAX == UINTPTR_MAX
|
||||
#define STACK_CHK_GUARD 0xe2dee396
|
||||
#else
|
||||
#define STACK_CHK_GUARD 0x595e9fbd94fda766
|
||||
#endif
|
||||
|
||||
// todo this value should be unique every time the kernel starts
|
||||
uintptr_t __stack_chk_guard = STACK_CHK_GUARD;
|
||||
|
||||
void __attribute__((__noreturn__)) __stack_chk_fail() {
|
||||
panic("Stack Smashed");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user