mem: fixed memory mapping bugs with kmap
function `kmap` is now capable of reliably mapping physical addresses to virtual addresses Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
ffddd592da
commit
705abf3234
5 changed files with 22 additions and 32 deletions
2
Makefile
2
Makefile
|
|
@ -23,7 +23,7 @@ LDFLAGS ?=
|
|||
|
||||
# Internal C flags that should not be changed by the user.
|
||||
override CFLAGS += \
|
||||
-std=c11 \
|
||||
-std=gnu99 \
|
||||
-ffreestanding \
|
||||
-fno-stack-protector \
|
||||
-fno-stack-check \
|
||||
|
|
|
|||
|
|
@ -44,7 +44,6 @@ struct cr3 {
|
|||
};
|
||||
};
|
||||
|
||||
extern uintptr_t __hhdm_start;
|
||||
extern uintptr_t __data_end;
|
||||
extern uintptr_t __bss_start;
|
||||
extern uintptr_t __bss_end;
|
||||
|
|
|
|||
11
init/main.c
11
init/main.c
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
#include <limine/limine.h>
|
||||
|
||||
struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST, .revision = 0 };
|
||||
|
||||
uintptr_t hhdm_start;
|
||||
uintptr_t data_end;
|
||||
uintptr_t bss_start;
|
||||
|
|
@ -24,24 +26,23 @@ static void done(void)
|
|||
|
||||
void kmain(void)
|
||||
{
|
||||
hhdm_start = (uintptr_t)(&__hhdm_start);
|
||||
hhdm_start = hhdm_req.response->offset;
|
||||
data_end = (uintptr_t)(&__data_end);
|
||||
bss_start = (uintptr_t)(&__bss_start);
|
||||
bss_end = (uintptr_t)(&__bss_end);
|
||||
|
||||
serial_init();
|
||||
|
||||
gdt_init();
|
||||
idt_init();
|
||||
|
||||
acpi_init();
|
||||
|
||||
/* Initialize early kernel memory array */
|
||||
const uintptr_t one_gb = 0x40000000;
|
||||
char kmem[one_gb] ALIGN(0x1000);
|
||||
printf(LOG_WARN "kmem: %Xh\n", kmem);
|
||||
|
||||
pfa_init(kmem, one_gb);
|
||||
|
||||
acpi_init();
|
||||
|
||||
apic_init();
|
||||
|
||||
done();
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ SECTIONS
|
|||
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
|
||||
/* that is the beginning of the region. */
|
||||
. = 0xffffffff80000000;
|
||||
__hhdm_start = 0xffffffff80000000;
|
||||
|
||||
.text : {
|
||||
*(.text .text.*)
|
||||
|
|
|
|||
39
mem/pfa.c
39
mem/pfa.c
|
|
@ -5,18 +5,13 @@
|
|||
#include <dev/apic.h>
|
||||
#include <limine/limine.h>
|
||||
|
||||
struct limine_memmap_request map_req = { .id = LIMINE_MEMMAP_REQUEST,
|
||||
.revision = 0 };
|
||||
struct limine_memmap_request map_req = { .id = LIMINE_MEMMAP_REQUEST, .revision = 0 };
|
||||
struct limine_kernel_address_request kern_req = { .id = LIMINE_KERNEL_ADDRESS_REQUEST, .revision = 0 };
|
||||
|
||||
static char *kmem;
|
||||
static size_t kmem_len;
|
||||
static uintptr_t hwm = 0;
|
||||
static struct page *pml4;
|
||||
|
||||
static const char *map_names[] = { "USABLE ", "RESERVED ",
|
||||
"ACPI_RECLAIM", "ACPI_NVS ",
|
||||
"BAD_MEM ", "BOOT_RECLAIM",
|
||||
"KERNEL ", "FRAMEBUFFER " };
|
||||
static struct page *pml4 ALIGN(0x1000);
|
||||
|
||||
void *alloc_page()
|
||||
{
|
||||
|
|
@ -33,7 +28,7 @@ uint64_t *next_level(uint64_t *this_level, size_t next_num)
|
|||
if (!(this_level[next_num] & 1)) {
|
||||
r = alloc_page();
|
||||
memset(r, 0, 4096);
|
||||
this_level[next_num] = (uintptr_t)r | 3;
|
||||
this_level[next_num] = ((uintptr_t)r | 3) - hhdm_start;
|
||||
} else
|
||||
r = (void *)(this_level[next_num] & -4096ull);
|
||||
return r;
|
||||
|
|
@ -79,19 +74,14 @@ void pfa_init(char *mem, size_t len)
|
|||
kmem = mem;
|
||||
kmem_len = len;
|
||||
|
||||
uint64_t kernel_base = 0;
|
||||
size_t kernel_size = 0;
|
||||
|
||||
/* Print limine early memory map */
|
||||
/* Parse limine physical memory map */
|
||||
struct limine_memmap_response *res = map_req.response;
|
||||
for (unsigned int i = 0; i < res->entry_count; i++) {
|
||||
struct limine_memmap_entry *entry = res->entries[i];
|
||||
printf(LOG_INFO "MEM REGION %s BASE: %X, SIZE: %X\n",
|
||||
map_names[entry->type], entry->base, entry->length);
|
||||
|
||||
switch (entry->type) {
|
||||
case LIMINE_MEMMAP_KERNEL_AND_MODULES:
|
||||
kernel_base = entry->base;
|
||||
kernel_size = entry->length;
|
||||
break;
|
||||
default:
|
||||
|
|
@ -99,21 +89,22 @@ void pfa_init(char *mem, size_t len)
|
|||
}
|
||||
}
|
||||
|
||||
/* Need to make these linker script values usable */
|
||||
|
||||
printf(LOG_WARN "__hhdm_start %X\n", hhdm_start);
|
||||
printf(LOG_WARN "__data_end %X\n", data_end);
|
||||
printf(LOG_WARN "__bss_start %X\n", bss_start);
|
||||
printf(LOG_WARN "__bss_end %X\n", bss_end);
|
||||
printf(LOG_WARN "sizeof size_t %d\n", sizeof(size_t));
|
||||
uint64_t kpaddr = kern_req.response->physical_base;
|
||||
uint64_t kvaddr = kern_req.response->virtual_base;
|
||||
|
||||
struct page attrs;
|
||||
attrs.val = 0;
|
||||
attrs.rw = 1;
|
||||
attrs.xd = 0;
|
||||
kmap(kernel_base, hhdm_start, kernel_size, attrs.val);
|
||||
|
||||
struct cr3 cr3 = { .pml4_addr = ((uintptr_t)pml4 >> 12) };
|
||||
kmap(kpaddr, kvaddr, kernel_size, attrs.val);
|
||||
kmap(0x0000, hhdm_start, 0x100000000, attrs.val);
|
||||
kmap(0x1000, 0x0000, 0x100000000, attrs.val);
|
||||
|
||||
uintptr_t pml4_paddr = (uintptr_t)pml4;
|
||||
pml4_paddr -= hhdm_start;
|
||||
|
||||
struct cr3 cr3 = { .pml4_addr = pml4_paddr >> 12 };
|
||||
cr3_write(cr3.val);
|
||||
|
||||
printf(LOG_SUCCESS "Early memory map initialized\n");
|
||||
|
|
|
|||
Loading…
Reference in a new issue