x86_64: initialise memblock using e820

This commit is contained in:
2023-02-05 10:50:13 +00:00
parent 21907010bd
commit b9b3794aee
5 changed files with 111 additions and 9 deletions

56
arch/x86_64/e820.c Normal file
View File

@@ -0,0 +1,56 @@
#include "socks/types.h"
#include <socks/memblock.h>
#include <socks/printk.h>
#include <socks/util.h>
#include <arch/e820.h>
void e820_scan(multiboot_memory_map_t *mmap, size_t len)
{
size_t mem_total = 0, mem_reserved = 0;
multiboot_memory_map_t *entry = NULL;
for (size_t i = 0; i < len; i += sizeof *entry) {
entry = (void *)((char *)mmap + i);
int reserved = 1;
const char *type = "unknown";
switch (entry->type) {
case MULTIBOOT_MEMORY_AVAILABLE:
type = "available";
reserved = 0;
break;
case MULTIBOOT_MEMORY_RESERVED:
type = "reserved";
break;
case MULTIBOOT_MEMORY_ACPI_RECLAIMABLE:
type = "acpi";
break;
case MULTIBOOT_MEMORY_NVS:
type = "nvs";
break;
case MULTIBOOT_MEMORY_BADRAM:
type = "bad";
break;
default:
break;
}
printk("e820: [mem 0x%016llx-0x%016llx] %s",
entry->addr, entry->addr + entry->len - 1, type);
memblock_add(entry->addr, entry->len);
if (reserved) {
memblock_reserve(entry->addr, entry->len);
mem_reserved += entry->len;
}
mem_total += entry->len;
}
char str_mem_total[64], str_mem_reserved[64];
data_size_to_string(mem_total, str_mem_total, sizeof str_mem_total);
data_size_to_string(mem_reserved, str_mem_reserved, sizeof str_mem_reserved);
printk("e820: total memory: %s, hw reserved: %s", str_mem_total, str_mem_reserved);
}

View File

@@ -0,0 +1,9 @@
#ifndef ARCH_E820_H_
#define ARCH_E820_H_
#include <stddef.h>
#include <arch/multiboot.h>
extern void e820_scan(multiboot_memory_map_t *mmap, size_t len);
#endif

View File

@@ -1,21 +1,51 @@
#include "socks/types.h"
#include <arch/e820.h>
#include <socks/init.h>
#include <socks/memblock.h>
#include <socks/vm.h>
#include <socks/printk.h>
#include <socks/machine/cpu.h>
#include <arch/vgacon.h>
#define PTR32(x) ((void *)((uintptr_t)(x)))
static ml_cpu_block g_bootstrap_cpu = {0};
/* start and end of kernel image (physical addresses) */
extern char __pstart[], __pend[];
static void bootstrap_cpu_init(void)
{
ml_cpu_block_init(&g_bootstrap_cpu);
ml_cpu_block_use(&g_bootstrap_cpu);
}
static void early_vm_init(void)
{
uintptr_t alloc_start = VM_KERNEL_VOFFSET;
/* boot code mapped 2 GiB of memory from
VM_KERNEL_VOFFSET */
uintptr_t alloc_end = VM_KERNEL_VOFFSET + 0x7fffffff;
memblock_init(alloc_start, alloc_end, VM_KERNEL_VOFFSET);
printk("memblock: allocating from [0x%llx-0x%llx]", alloc_start, alloc_end);
memblock_reserve(0x00, (uintptr_t)__pend);
printk("memblock: reserved bios+kernel at [0x%016llx-0x%016llx]", 0, (uintptr_t)__pend);
}
int ml_init(uintptr_t arg)
{
multiboot_info_t *mb = (multiboot_info_t *)arg;
bootstrap_cpu_init();
vgacon_init();
print_kernel_banner();
early_vm_init();
e820_scan(PTR32(mb->mmap_addr), mb->mmap_length);
return 0;
}

View File

@@ -17,6 +17,9 @@ SECTIONS {
. += KERNEL_VMA;
__start = .;
__pstart = . - KERNEL_VMA;
.text ALIGN(4K) : AT(ADDR(.text) - KERNEL_VMA)
{
*(.text)
@@ -47,7 +50,8 @@ SECTIONS {
*(COMMON)
}
_end = .;
__end = .;
__pend = . - KERNEL_VMA;
/DISCARD/ :
{

View File

@@ -6,6 +6,8 @@
extern unsigned long get_rflags(void);
extern char __pstart[], __pend[];
void print_kernel_banner(void)
{
printk("Socks kernel version " BUILD_ID);
@@ -14,5 +16,6 @@ void print_kernel_banner(void)
void kernel_init(uintptr_t arg)
{
ml_init(arg);
ml_halt_cpu();
}