Added improved memmap

This commit is contained in:
mintsuki 2020-06-05 17:51:33 +02:00
parent 3d75644e2c
commit e91cb82222
10 changed files with 229 additions and 43 deletions

Binary file not shown.

View File

@ -10,27 +10,6 @@
#include <lib/e820.h>
#include <lib/print.h>
// Checks if a given memory range is entirely within a usable e820 entry.
// TODO: Consider the possibility of adjacent usable entries.
// TODO: Consider the possibility of usable entry being overlapped by non-usable
// ones.
void is_valid_memory_range(size_t base, size_t size) {
// We don't allow loads below 1MB
if (base < 0x100000)
panic("Trying to overwrite bootloader.");
for (size_t i = 0; i < e820_entries; i++) {
if (e820_map[i].type != 1)
continue;
size_t entry_base = e820_map[i].base;
size_t entry_top = e820_map[i].base + e820_map[i].length;
size_t limit = base + size;
if (base >= entry_base && base < entry_top &&
limit >= entry_base && limit < entry_top)
return;
}
panic("Out of memory");
}
uint8_t bcd_to_int(uint8_t val) {
return (val & 0x0f) + ((val & 0xf0) >> 4) * 10;
}

View File

@ -4,7 +4,24 @@
#include <stddef.h>
#include <stdint.h>
void is_valid_memory_range(size_t base, size_t size);
#define ALIGN_UP(x, a) ({ \
typeof(x) value = x; \
typeof(a) align = a; \
if ((value & (align - 1)) != 0) { \
value &= ~(align - 1); \
value += align; \
} \
value; \
})
#define ALIGN_DOWN(x, a) ({ \
typeof(x) value = x; \
typeof(a) align = a; \
if ((value & (align - 1)) != 0) { \
value &= ~(align - 1); \
} \
value; \
})
uint8_t bcd_to_int(uint8_t val);

View File

@ -4,6 +4,7 @@
#include <lib/libc.h>
#include <lib/elf.h>
#include <lib/print.h>
#include <lib/memmap.h>
#include <fs/file.h>
#define PT_LOAD 0x00000001
@ -313,7 +314,7 @@ int elf64_load(struct file_handle *fd, uint64_t *entry_point, uint64_t *top, uin
if (this_top > *top)
*top = this_top;
is_valid_memory_range((size_t)load_vaddr, (size_t)phdr.p_memsz);
memmap_alloc_range((size_t)load_vaddr, (size_t)phdr.p_memsz);
fread(fd, (void *)(uint32_t)load_vaddr, phdr.p_offset, phdr.p_filesz);
@ -369,7 +370,7 @@ int elf32_load(struct file_handle *fd, uint32_t *entry_point, uint32_t *top) {
if (this_top > *top)
*top = this_top;
is_valid_memory_range((size_t)phdr.p_paddr, (size_t)phdr.p_memsz);
memmap_alloc_range((size_t)phdr.p_paddr, (size_t)phdr.p_memsz);
fread(fd, (void *)phdr.p_paddr, phdr.p_offset, phdr.p_filesz);

166
src/lib/memmap.c Normal file
View File

@ -0,0 +1,166 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <lib/memmap.h>
#include <lib/e820.h>
#include <lib/blib.h>
#define PAGE_SIZE 4096
#define MEMMAP_BASE ((size_t)0x100000)
#define MEMMAP_MAX_ENTRIES 256
static struct e820_entry_t memmap[MEMMAP_MAX_ENTRIES];
static size_t memmap_entries = 0;
static void align_entry_down(uint64_t *base, uint64_t *length) {
uint64_t orig_base = *base;
*base = ALIGN_UP(*base, PAGE_SIZE);
*length = *length - (*base - orig_base);
*length = ALIGN_DOWN(*length, PAGE_SIZE);
}
static int align_entry_with_base(uint64_t *base, uint64_t *length) {
align_entry_down(base, length);
if (!length)
return -1;
uint64_t top = *base + *length;
if (*base < MEMMAP_BASE) {
if (top > MEMMAP_BASE) {
*length -= MEMMAP_BASE - *base;
*base = MEMMAP_BASE;
} else {
return -1;
}
}
return 0;
}
static void memmap_align_free_entries(void) {
for (size_t i = 0; i < memmap_entries; i++) {
if (memmap[i].type != 1)
continue;
align_entry_down(&memmap[i].base, &memmap[i].length);
if (!memmap[i].length) {
// Eradicate from memmap
for (size_t j = i; j < memmap_entries - 1; j++) {
memmap[j] = memmap[j+1];
}
memmap_entries--;
}
}
}
struct e820_entry_t *get_memmap(size_t *entries) {
memmap_align_free_entries();
*entries = memmap_entries;
return memmap;
}
void init_memmap(void) {
for (size_t i = 0; i < e820_entries; i++) {
if (memmap_entries == MEMMAP_MAX_ENTRIES) {
panic("Memory map exhausted.");
}
if (e820_map[i].type != 1) {
memmap[memmap_entries++] = e820_map[i];
continue;
}
uint64_t base = e820_map[i].base;
uint64_t length = e820_map[i].length;
uint64_t top = base + length;
// Check if the entry overlaps non-usable entries
for (size_t j = 0; j < e820_entries; j++) {
if (e820_map[j].type == 1)
continue;
size_t res_base = e820_map[j].base;
size_t res_length = e820_map[j].length;
size_t res_top = res_base + res_length;
// TODO actually handle splitting off usable chunks
if ( (res_base >= base && res_base < top)
&& (res_top >= base && res_top < top) ) {
panic("A non-usable e820 entry is inside a usable section.");
}
if (res_base >= base && res_base < top) {
top = res_base;
}
if (res_top >= base && res_top < top) {
base = res_top;
}
}
if (align_entry_with_base(&base, &length))
continue;
memmap[memmap_entries].type = 1;
memmap[memmap_entries].base = base;
memmap[memmap_entries].length = length;
memmap_entries++;
}
}
void memmap_alloc_range(uint64_t base, uint64_t length) {
uint64_t top = base + length;
for (size_t i = 0; i < memmap_entries; i++) {
if (memmap[i].type != 1)
continue;
uint64_t entry_base = memmap[i].base;
uint64_t entry_top = memmap[i].base + memmap[i].length;
if (base >= entry_base && base < entry_top &&
top >= entry_base && top < entry_top) {
memmap[i].length = base - entry_base;
if (memmap[i].length == 0) {
// Eradicate from memmap
for (size_t j = i; j < memmap_entries - 1; j++) {
memmap[j] = memmap[j+1];
}
memmap_entries--;
}
if (memmap_entries >= MEMMAP_MAX_ENTRIES) {
panic("Memory map exhausted.");
}
struct e820_entry_t *target = &memmap[memmap_entries];
target->length = entry_top - top;
if (target->length != 0) {
target->base = top;
target->type = 1;
memmap_entries++;
}
if (memmap_entries >= MEMMAP_MAX_ENTRIES) {
panic("Memory map exhausted.");
}
target = &memmap[memmap_entries++];
target->type = 10;
target->base = base;
target->length = length;
return;
}
}
panic("Out of memory");
}

11
src/lib/memmap.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef __LIB__MEMMAP_H__
#define __LIB__MEMMAP_H__
#include <stdint.h>
#include <lib/e820.h>
void init_memmap(void);
void memmap_alloc_range(uint64_t base, uint64_t length);
struct e820_entry_t *get_memmap(size_t *entries);
#endif

View File

@ -41,6 +41,15 @@
static bool rand_initialised = false;
#define n ((int)624)
#define m ((int)397)
#define matrix_a ((uint32_t)0x9908b0df)
#define msb ((uint32_t)0x80000000)
#define lsbs ((uint32_t)0x7fffffff)
static uint32_t *status;
static int ctr;
static void init_rand(void) {
uint32_t seed = ((uint32_t)0xc597060c * rdtsc(uint32_t))
* ((uint32_t)0xce86d624)
@ -55,20 +64,13 @@ static void init_rand(void) {
seed *= (seed ^ rdrand(uint32_t));
}
status = balloc(n);
srand(seed);
rand_initialised = true;
}
#define n ((int)624)
#define m ((int)397)
#define matrix_a ((uint32_t)0x9908b0df)
#define msb ((uint32_t)0x80000000)
#define lsbs ((uint32_t)0x7fffffff)
static uint32_t status[n];
static int ctr;
void srand(uint32_t s) {
status[0] = s;
for (ctr = 1; ctr < n; ctr++)

View File

@ -21,6 +21,7 @@ asm (
#include <lib/part.h>
#include <lib/config.h>
#include <lib/e820.h>
#include <lib/memmap.h>
#include <lib/print.h>
#include <fs/file.h>
#include <lib/elf.h>
@ -154,6 +155,7 @@ void main(int boot_drive) {
got_entry:
init_e820();
init_memmap();
char proto[32];
if (!config_get_value(proto, 0, 32, "KERNEL_PROTO")) {

View File

@ -7,6 +7,7 @@
#include <drivers/vga_textmode.h>
#include <lib/config.h>
#include <lib/print.h>
#include <lib/memmap.h>
#define KERNEL_LOAD_ADDR ((size_t)0x100000)
#define INITRD_LOAD_ADDR ((size_t)0x1000000)
@ -102,7 +103,7 @@ void linux_load(char *cmdline, int boot_drive) {
// load kernel
print("Loading kernel...\n");
is_valid_memory_range(KERNEL_LOAD_ADDR, fd->size - real_mode_code_size);
memmap_alloc_range(KERNEL_LOAD_ADDR, fd->size - real_mode_code_size);
fread(fd, (void *)KERNEL_LOAD_ADDR, real_mode_code_size, fd->size - real_mode_code_size);
char initrd_path[64];
@ -124,7 +125,7 @@ void linux_load(char *cmdline, int boot_drive) {
}
print("Loading initrd...\n");
is_valid_memory_range(INITRD_LOAD_ADDR, initrd.size);
memmap_alloc_range(INITRD_LOAD_ADDR, initrd.size);
fread(&initrd, (void *)INITRD_LOAD_ADDR, 0, initrd.size);
*((uint32_t *)(real_mode_code + 0x218)) = (uint32_t)INITRD_LOAD_ADDR;

View File

@ -5,7 +5,7 @@
#include <lib/elf.h>
#include <lib/blib.h>
#include <lib/acpi.h>
#include <lib/e820.h>
#include <lib/memmap.h>
#include <lib/config.h>
#include <lib/time.h>
#include <lib/print.h>
@ -159,9 +159,6 @@ void stivale_load(char *cmdline, int boot_drive) {
print("stivale: Top used address in ELF: %X\n", top_used_addr);
stivale_struct.memory_map_entries = (uint64_t)e820_entries;
stivale_struct.memory_map_addr = (uint64_t)(size_t)e820_map;
stivale_struct.module_count = 0;
uint64_t *prev_mod_ptr = &stivale_struct.modules;
for (int i = 0; ; i++) {
@ -173,12 +170,17 @@ void stivale_load(char *cmdline, int boot_drive) {
struct stivale_module *m = balloc(sizeof(struct stivale_module));
config_get_value(m->string, i, 128, "MODULE_STRING");
if (!config_get_value(m->string, i, 128, "MODULE_STRING")) {
m->string[0] = '\0';
}
int part; {
char buf[32];
config_get_value(buf, i, 32, "MODULE_PARTITION");
part = (int)strtoui(buf);
if (!config_get_value(buf, i, 32, "MODULE_PARTITION")) {
part = kernel_part;
} else {
part = (int)strtoui(buf);
}
}
struct file_handle f;
@ -188,7 +190,7 @@ void stivale_load(char *cmdline, int boot_drive) {
((uint32_t)top_used_addr & ~((uint32_t)0xfff)) + 0x1000 :
(uint32_t)top_used_addr);
is_valid_memory_range((size_t)module_addr, f.size);
memmap_alloc_range((size_t)module_addr, f.size);
fread(&f, module_addr, 0, f.size);
m->begin = (uint64_t)(size_t)module_addr;
@ -227,6 +229,11 @@ void stivale_load(char *cmdline, int boot_drive) {
deinit_vga_textmode();
}
size_t memmap_entries;
struct e820_entry_t *memmap = get_memmap(&memmap_entries);
stivale_struct.memory_map_entries = (uint64_t)memmap_entries;
stivale_struct.memory_map_addr = (uint64_t)(size_t)memmap;
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