2020-03-25 23:05:14 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2020-05-03 23:37:24 +03:00
|
|
|
#include <stdbool.h>
|
2020-03-25 23:05:14 +03:00
|
|
|
#include <protos/stivale.h>
|
2020-11-27 21:33:34 +03:00
|
|
|
#include <lib/libc.h>
|
2020-03-25 23:05:14 +03:00
|
|
|
#include <lib/elf.h>
|
2020-03-26 02:46:35 +03:00
|
|
|
#include <lib/blib.h>
|
2020-03-26 03:37:56 +03:00
|
|
|
#include <lib/acpi.h>
|
2020-03-30 23:27:15 +03:00
|
|
|
#include <lib/config.h>
|
2020-04-30 22:19:12 +03:00
|
|
|
#include <lib/time.h>
|
2020-05-10 01:38:27 +03:00
|
|
|
#include <lib/print.h>
|
2020-06-01 11:20:34 +03:00
|
|
|
#include <lib/rand.h>
|
2020-06-01 05:47:55 +03:00
|
|
|
#include <lib/real.h>
|
2020-11-02 11:20:34 +03:00
|
|
|
#include <lib/uri.h>
|
2021-03-02 08:21:05 +03:00
|
|
|
#include <lib/fb.h>
|
2020-09-02 10:55:56 +03:00
|
|
|
#include <lib/term.h>
|
2020-09-18 21:02:10 +03:00
|
|
|
#include <sys/pic.h>
|
2020-10-12 22:49:17 +03:00
|
|
|
#include <sys/cpu.h>
|
2021-03-07 02:52:25 +03:00
|
|
|
#include <sys/gdt.h>
|
2020-04-14 06:20:55 +03:00
|
|
|
#include <fs/file.h>
|
2020-09-21 13:15:55 +03:00
|
|
|
#include <mm/vmm.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-09-27 18:39:30 +03:00
|
|
|
#include <mm/mtrr.h>
|
2020-09-18 13:51:26 +03:00
|
|
|
#include <stivale/stivale.h>
|
2020-03-26 02:46:35 +03:00
|
|
|
|
2020-12-28 01:11:11 +03:00
|
|
|
#define KASLR_SLIDE_BITMASK 0x000FFF000u
|
2020-05-29 12:05:50 +03:00
|
|
|
|
2020-03-26 06:06:23 +03:00
|
|
|
struct stivale_struct stivale_struct = {0};
|
2020-03-25 23:05:14 +03:00
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
void stivale_load(char *config, char *cmdline) {
|
2021-03-12 02:04:37 +03:00
|
|
|
// BIOS or UEFI?
|
|
|
|
#if defined (bios)
|
|
|
|
stivale_struct.flags |= (1 << 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
stivale_struct.flags |= (1 << 1); // we give colour information
|
2020-05-15 06:47:38 +03:00
|
|
|
|
2020-12-31 05:26:19 +03:00
|
|
|
struct file_handle *kernel = ext_mem_alloc(sizeof(struct file_handle));
|
2020-10-17 07:23:11 +03:00
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *kernel_path = config_get_value(config, 0, "KERNEL_PATH");
|
|
|
|
if (kernel_path == NULL)
|
2020-10-17 07:23:11 +03:00
|
|
|
panic("KERNEL_PATH not specified");
|
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
if (!uri_open(kernel, kernel_path))
|
2020-10-17 07:23:11 +03:00
|
|
|
panic("Could not open kernel resource");
|
2020-05-06 17:38:45 +03:00
|
|
|
|
2020-03-26 02:46:35 +03:00
|
|
|
struct stivale_header stivale_hdr;
|
2020-04-18 19:01:29 +03:00
|
|
|
|
2020-10-17 07:23:11 +03:00
|
|
|
int bits = elf_bits(kernel);
|
2020-04-18 19:01:29 +03:00
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
2020-05-29 12:05:50 +03:00
|
|
|
uint64_t slide = 0;
|
|
|
|
|
2020-05-03 23:37:24 +03:00
|
|
|
bool level5pg = false;
|
2020-04-18 19:01:29 +03:00
|
|
|
switch (bits) {
|
2020-05-03 23:37:24 +03:00
|
|
|
case 64: {
|
2020-04-30 14:03:04 +03:00
|
|
|
// Check if 64 bit CPU
|
2020-05-03 23:37:24 +03:00
|
|
|
uint32_t eax, ebx, ecx, edx;
|
2020-11-16 23:23:11 +03:00
|
|
|
if (!cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx) || !(edx & (1 << 29))) {
|
2020-05-03 23:37:24 +03:00
|
|
|
panic("stivale: This CPU does not support 64-bit mode.");
|
|
|
|
}
|
|
|
|
// Check if 5-level paging is available
|
2020-11-16 23:23:11 +03:00
|
|
|
if (cpuid(0x00000007, 0, &eax, &ebx, &ecx, &edx) && (ecx & (1 << 16))) {
|
2020-05-03 23:37:24 +03:00
|
|
|
print("stivale: CPU has 5-level paging support\n");
|
|
|
|
level5pg = true;
|
2020-04-30 14:03:04 +03:00
|
|
|
}
|
2020-05-29 12:05:50 +03:00
|
|
|
|
2020-12-28 01:11:11 +03:00
|
|
|
char *s_kaslr = config_get_value(config, 0, "KASLR");
|
|
|
|
if (s_kaslr != NULL && !strcmp(s_kaslr, "yes")) {
|
2020-05-29 12:05:50 +03:00
|
|
|
// KASLR is enabled, set the slide
|
2020-06-01 11:20:34 +03:00
|
|
|
slide = rand64() & KASLR_SLIDE_BITMASK;
|
2020-05-29 12:05:50 +03:00
|
|
|
}
|
|
|
|
|
2020-12-28 01:11:11 +03:00
|
|
|
ret = elf64_load_section(kernel, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
|
|
|
|
|
2020-04-18 19:01:29 +03:00
|
|
|
break;
|
2020-05-03 23:37:24 +03:00
|
|
|
}
|
2020-04-18 19:01:29 +03:00
|
|
|
case 32:
|
2020-10-17 07:23:11 +03:00
|
|
|
ret = elf32_load_section(kernel, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header));
|
2020-04-18 19:01:29 +03:00
|
|
|
break;
|
2020-04-29 17:53:05 +03:00
|
|
|
default:
|
2020-04-30 14:03:04 +03:00
|
|
|
panic("stivale: Not 32 nor 64 bit x86 ELF file.");
|
2020-04-18 19:01:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 17:53:05 +03:00
|
|
|
print("stivale: %u-bit ELF file detected\n", bits);
|
|
|
|
|
2020-03-26 02:46:35 +03:00
|
|
|
switch (ret) {
|
|
|
|
case 1:
|
2020-05-10 01:48:58 +03:00
|
|
|
panic("stivale: File is not a valid ELF.");
|
2020-03-26 02:46:35 +03:00
|
|
|
case 2:
|
2020-05-10 01:48:58 +03:00
|
|
|
panic("stivale: Section .stivalehdr not found.");
|
2020-03-26 02:46:35 +03:00
|
|
|
case 3:
|
2020-05-10 01:48:58 +03:00
|
|
|
panic("stivale: Section .stivalehdr exceeds the size of the struct.");
|
2020-05-30 16:44:14 +03:00
|
|
|
case 4:
|
|
|
|
panic("stivale: Section .stivalehdr is smaller than size of the struct.");
|
2020-03-26 02:46:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
print("stivale: Requested stack at %X\n", stivale_hdr.stack);
|
|
|
|
|
2020-04-18 19:01:29 +03:00
|
|
|
uint64_t entry_point = 0;
|
|
|
|
uint64_t top_used_addr = 0;
|
2020-05-29 12:05:50 +03:00
|
|
|
|
2020-04-18 19:01:29 +03:00
|
|
|
switch (bits) {
|
|
|
|
case 64:
|
2020-10-17 07:23:11 +03:00
|
|
|
elf64_load(kernel, &entry_point, &top_used_addr, slide, 10);
|
2020-04-18 19:01:29 +03:00
|
|
|
break;
|
|
|
|
case 32:
|
2020-10-17 07:23:11 +03:00
|
|
|
elf32_load(kernel, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 10);
|
2020-04-18 19:01:29 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-03-30 23:27:15 +03:00
|
|
|
|
2020-05-30 16:44:14 +03:00
|
|
|
if (stivale_hdr.entry_point != 0)
|
|
|
|
entry_point = stivale_hdr.entry_point;
|
|
|
|
|
2020-05-29 12:05:50 +03:00
|
|
|
print("stivale: Kernel slide: %X\n", slide);
|
|
|
|
|
2020-03-30 23:27:15 +03:00
|
|
|
print("stivale: Top used address in ELF: %X\n", top_used_addr);
|
2020-03-25 23:05:14 +03:00
|
|
|
|
2020-03-30 23:27:15 +03:00
|
|
|
stivale_struct.module_count = 0;
|
|
|
|
uint64_t *prev_mod_ptr = &stivale_struct.modules;
|
|
|
|
for (int i = 0; ; i++) {
|
2020-11-27 21:33:34 +03:00
|
|
|
char *module_path = config_get_value(config, i, "MODULE_PATH");
|
|
|
|
if (module_path == NULL)
|
2020-03-30 23:27:15 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
stivale_struct.module_count++;
|
|
|
|
|
2020-12-31 05:26:19 +03:00
|
|
|
struct stivale_module *m = ext_mem_alloc(sizeof(struct stivale_module));
|
2020-03-30 23:27:15 +03:00
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *module_string = config_get_value(config, i, "MODULE_STRING");
|
|
|
|
if (module_string == NULL) {
|
2020-06-05 18:51:33 +03:00
|
|
|
m->string[0] = '\0';
|
2020-11-27 21:33:34 +03:00
|
|
|
} else {
|
|
|
|
// TODO perhaps change this to be a pointer
|
|
|
|
size_t str_len = strlen(module_string);
|
|
|
|
if (str_len > 127)
|
|
|
|
str_len = 127;
|
|
|
|
memcpy(m->string, module_string, str_len);
|
2020-06-05 18:51:33 +03:00
|
|
|
}
|
2020-03-30 23:27:15 +03:00
|
|
|
|
2021-01-02 23:44:27 +03:00
|
|
|
print("stivale: Loading module `%s`...\n", module_path);
|
|
|
|
|
2020-04-14 06:20:55 +03:00
|
|
|
struct file_handle f;
|
2020-11-27 21:33:34 +03:00
|
|
|
if (!uri_open(&f, module_path))
|
|
|
|
panic("Requested module with path \"%s\" not found!", module_path);
|
2020-03-30 23:27:15 +03:00
|
|
|
|
2021-01-02 23:44:27 +03:00
|
|
|
m->begin = (uint64_t)(size_t)freadall(&f, STIVALE_MMAP_KERNEL_AND_MODULES);
|
2020-04-14 06:20:55 +03:00
|
|
|
m->end = m->begin + f.size;
|
2020-03-30 23:27:15 +03:00
|
|
|
m->next = 0;
|
|
|
|
|
|
|
|
*prev_mod_ptr = (uint64_t)(size_t)m;
|
|
|
|
prev_mod_ptr = &m->next;
|
|
|
|
|
|
|
|
print("stivale: Requested module %u:\n", i);
|
2020-11-27 21:33:34 +03:00
|
|
|
print(" Path: %s\n", module_path);
|
2020-03-30 23:27:15 +03:00
|
|
|
print(" String: %s\n", m->string);
|
|
|
|
print(" Begin: %X\n", m->begin);
|
|
|
|
print(" End: %X\n", m->end);
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
stivale_struct.rsdp = (uint64_t)(size_t)acpi_get_rsdp();
|
2020-03-26 03:37:56 +03:00
|
|
|
|
2020-03-31 11:48:24 +03:00
|
|
|
stivale_struct.cmdline = (uint64_t)(size_t)cmdline;
|
|
|
|
|
2020-04-30 22:19:12 +03:00
|
|
|
stivale_struct.epoch = time();
|
2020-06-25 19:51:54 +03:00
|
|
|
print("stivale: Current epoch: %U\n", stivale_struct.epoch);
|
2020-04-30 22:19:12 +03:00
|
|
|
|
2020-09-02 10:55:56 +03:00
|
|
|
term_deinit();
|
|
|
|
|
2020-05-03 23:37:24 +03:00
|
|
|
if (stivale_hdr.flags & (1 << 0)) {
|
2020-11-09 14:31:47 +03:00
|
|
|
int req_width = stivale_hdr.framebuffer_width;
|
|
|
|
int req_height = stivale_hdr.framebuffer_height;
|
|
|
|
int req_bpp = stivale_hdr.framebuffer_bpp;
|
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *resolution = config_get_value(config, 0, "RESOLUTION");
|
|
|
|
if (resolution != NULL)
|
|
|
|
parse_resolution(&req_width, &req_height, &req_bpp, resolution);
|
2020-11-09 14:31:47 +03:00
|
|
|
|
2021-03-02 08:21:05 +03:00
|
|
|
struct fb_info fbinfo;
|
|
|
|
if (!fb_init(&fbinfo, req_width, req_height, req_bpp))
|
2020-12-05 04:10:02 +03:00
|
|
|
panic("stivale: Unable to set video mode");
|
|
|
|
|
|
|
|
stivale_struct.framebuffer_addr = (uint64_t)fbinfo.framebuffer_addr;
|
|
|
|
stivale_struct.framebuffer_width = fbinfo.framebuffer_width;
|
|
|
|
stivale_struct.framebuffer_height = fbinfo.framebuffer_height;
|
|
|
|
stivale_struct.framebuffer_bpp = fbinfo.framebuffer_bpp;
|
|
|
|
stivale_struct.framebuffer_pitch = fbinfo.framebuffer_pitch;
|
|
|
|
stivale_struct.fb_memory_model = STIVALE_FBUF_MMODEL_RGB;
|
|
|
|
stivale_struct.fb_red_mask_size = fbinfo.red_mask_size;
|
|
|
|
stivale_struct.fb_red_mask_shift = fbinfo.red_mask_shift;
|
|
|
|
stivale_struct.fb_green_mask_size = fbinfo.green_mask_size;
|
|
|
|
stivale_struct.fb_green_mask_shift = fbinfo.green_mask_shift;
|
|
|
|
stivale_struct.fb_blue_mask_size = fbinfo.blue_mask_size;
|
|
|
|
stivale_struct.fb_blue_mask_shift = fbinfo.blue_mask_shift;
|
2020-03-26 05:13:19 +03:00
|
|
|
}
|
|
|
|
|
2021-03-10 05:09:42 +03:00
|
|
|
#if defined (uefi)
|
|
|
|
efi_exit_boot_services();
|
|
|
|
#endif
|
|
|
|
|
2020-11-08 01:50:01 +03:00
|
|
|
bool want_5lv = level5pg && (stivale_hdr.flags & (1 << 1));
|
|
|
|
pagemap_t pagemap = stivale_build_pagemap(want_5lv);
|
|
|
|
|
2020-06-05 18:51:33 +03:00
|
|
|
size_t memmap_entries;
|
|
|
|
struct e820_entry_t *memmap = get_memmap(&memmap_entries);
|
|
|
|
|
2020-09-26 16:06:59 +03:00
|
|
|
stivale_struct.memory_map_entries = (uint64_t)memmap_entries;
|
|
|
|
stivale_struct.memory_map_addr = (uint64_t)(size_t)memmap;
|
|
|
|
|
2021-03-07 02:52:25 +03:00
|
|
|
stivale_spinup(bits, want_5lv, &pagemap,
|
2020-09-18 15:39:29 +03:00
|
|
|
entry_point, &stivale_struct, stivale_hdr.stack);
|
2020-09-02 10:55:56 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 01:50:01 +03:00
|
|
|
pagemap_t stivale_build_pagemap(bool level5pg) {
|
2020-09-18 15:39:29 +03:00
|
|
|
pagemap_t pagemap = new_pagemap(level5pg ? 5 : 4);
|
|
|
|
uint64_t higher_half_base = level5pg ? 0xff00000000000000 : 0xffff800000000000;
|
|
|
|
|
|
|
|
// Map 0 to 2GiB at 0xffffffff80000000
|
|
|
|
for (uint64_t i = 0; i < 0x80000000; i += PAGE_SIZE) {
|
|
|
|
map_page(pagemap, 0xffffffff80000000 + i, i, 0x03);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map 0 to 4GiB at higher half base and 0
|
|
|
|
for (uint64_t i = 0; i < 0x100000000; i += PAGE_SIZE) {
|
|
|
|
map_page(pagemap, i, i, 0x03);
|
|
|
|
map_page(pagemap, higher_half_base + i, i, 0x03);
|
|
|
|
}
|
|
|
|
|
2020-12-10 10:47:37 +03:00
|
|
|
size_t _memmap_entries = memmap_entries;
|
|
|
|
struct e820_entry_t *_memmap =
|
2020-12-31 05:26:19 +03:00
|
|
|
ext_mem_alloc(_memmap_entries * sizeof(struct e820_entry_t));
|
2020-12-10 10:47:37 +03:00
|
|
|
for (size_t i = 0; i < _memmap_entries; i++)
|
|
|
|
_memmap[i] = memmap[i];
|
2020-11-08 01:50:01 +03:00
|
|
|
|
2020-09-18 15:39:29 +03:00
|
|
|
// Map any other region of memory from the memmap
|
2020-12-10 10:47:37 +03:00
|
|
|
for (size_t i = 0; i < _memmap_entries; i++) {
|
|
|
|
uint64_t base = _memmap[i].base;
|
|
|
|
uint64_t length = _memmap[i].length;
|
2020-09-18 15:39:29 +03:00
|
|
|
uint64_t top = base + length;
|
|
|
|
|
|
|
|
uint64_t aligned_base = ALIGN_DOWN(base, PAGE_SIZE);
|
|
|
|
uint64_t aligned_top = ALIGN_UP(top, PAGE_SIZE);
|
|
|
|
uint64_t aligned_length = aligned_top - aligned_base;
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < aligned_length; i += PAGE_SIZE) {
|
|
|
|
uint64_t page = aligned_base + i;
|
|
|
|
map_page(pagemap, page, page, 0x03);
|
|
|
|
map_page(pagemap, higher_half_base + page, page, 0x03);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pagemap;
|
|
|
|
}
|
|
|
|
|
2021-03-07 02:52:25 +03:00
|
|
|
#if defined (uefi)
|
|
|
|
extern symbol ImageBase;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
__attribute__((noreturn)) void stivale_spinup_32(
|
|
|
|
int bits, bool level5pg, uint32_t pagemap_top_lv,
|
|
|
|
uint32_t entry_point_lo, uint32_t entry_point_hi,
|
|
|
|
void *stivale_struct, uint32_t stack_lo, uint32_t stack_hi);
|
|
|
|
|
2020-09-18 15:39:29 +03:00
|
|
|
__attribute__((noreturn)) void stivale_spinup(
|
2021-03-07 02:52:25 +03:00
|
|
|
int bits, bool level5pg, pagemap_t *pagemap,
|
2020-09-18 15:39:29 +03:00
|
|
|
uint64_t entry_point, void *stivale_struct, uint64_t stack) {
|
2020-09-27 18:39:30 +03:00
|
|
|
mtrr_restore();
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
#if defined (bios)
|
2020-06-03 14:54:54 +03:00
|
|
|
if (bits == 64) {
|
|
|
|
// If we're going 64, we might as well call this BIOS interrupt
|
|
|
|
// to tell the BIOS that we are entering Long Mode, since it is in
|
|
|
|
// the specification.
|
|
|
|
struct rm_regs r = {0};
|
|
|
|
r.eax = 0xec00;
|
|
|
|
r.ebx = 0x02; // Long mode only
|
|
|
|
rm_int(0x15, &r, &r);
|
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
2020-06-03 14:54:54 +03:00
|
|
|
|
2020-09-02 03:32:04 +03:00
|
|
|
pic_mask_all();
|
|
|
|
pic_flush();
|
2020-06-01 05:47:55 +03:00
|
|
|
|
2021-03-07 02:52:25 +03:00
|
|
|
#if defined (uefi)
|
|
|
|
do_32(stivale_spinup_32, 8,
|
|
|
|
bits, level5pg, (uint32_t)(uintptr_t)pagemap->top_level,
|
|
|
|
(uint32_t)entry_point, (uint32_t)(entry_point >> 32),
|
|
|
|
stivale_struct,
|
|
|
|
(uint32_t)stack, (uint32_t)(stack >> 32));
|
|
|
|
#endif
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-03-07 02:52:25 +03:00
|
|
|
#if defined (bios)
|
|
|
|
stivale_spinup_32(bits, level5pg, (uint32_t)(uintptr_t)pagemap->top_level,
|
|
|
|
(uint32_t)entry_point, (uint32_t)(entry_point >> 32),
|
|
|
|
stivale_struct,
|
|
|
|
(uint32_t)stack, (uint32_t)(stack >> 32));
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
|
|
|
|
2021-03-07 02:52:25 +03:00
|
|
|
__builtin_unreachable();
|
2020-03-25 23:05:14 +03:00
|
|
|
}
|