Fixed paging

This commit is contained in:
Jozef Nagy 2025-04-17 16:28:43 +02:00
parent 7c18d7e7b1
commit aa3f734406
Signed by untrusted user who does not match committer: crz
GPG key ID: 459A4811CEAC7068
4 changed files with 212 additions and 8 deletions

View file

@ -59,7 +59,7 @@ static void _map(pagetable *pm, uintptr_t virt, uintptr_t phys, uint64_t flags)
pagetable *pml1_table = (pagetable *)(pml2_table->entries[pml2_idx] & 0x000FFFFFFFFFF000); pagetable *pml1_table = (pagetable *)(pml2_table->entries[pml2_idx] & 0x000FFFFFFFFFF000);
if ((pml1_table->entries[pml1_idx] & 1)) { if ((pml1_table->entries[pml1_idx] & 1)) {
debug("_map(): Remapping present page\n"); // debug("_map(): Remapping present page\n");
} }
pml1_table->entries[pml1_idx] = (phys & 0x000FFFFFFFFFF000) | flags; pml1_table->entries[pml1_idx] = (phys & 0x000FFFFFFFFFF000) | flags;

View file

@ -20,6 +20,7 @@
#include <proto/aurix.h> #include <proto/aurix.h>
#include <loader/elf.h> #include <loader/elf.h>
#include <mm/mman.h> #include <mm/mman.h>
#include <mm/memmap.h>
#include <mm/vmm.h> #include <mm/vmm.h>
#include <vfs/vfs.h> #include <vfs/vfs.h>
#include <print.h> #include <print.h>
@ -30,21 +31,22 @@
extern __attribute__((noreturn)) void aurix_handoff(void *pagemap, void *stack, uint64_t entry, void *params); extern __attribute__((noreturn)) void aurix_handoff(void *pagemap, void *stack, uint64_t entry, void *params);
extern char _aurix_handoff_start[], _aurix_handoff_end[]; extern char _aurix_handoff_start[], _aurix_handoff_end[];
void aurix_load(char *kernel) void aurix_load(char *kernel_path)
{ {
// read kernel -> test read // read kernel -> test read
char *kbuf = NULL; char *kbuf = NULL;
vfs_read(kernel, &kbuf); vfs_read(kernel_path, &kbuf);
// TODO: Do something with the kernel :p // TODO: Do something with the kernel :p
pagetable *pm = create_pagemap(); pagetable *pm = create_pagemap();
// __asm__ volatile("mov %%cr3, %0" : "=r"(pm));
if (!pm) { if (!pm) {
debug("aurix_load(): Failed to create kernel pagemap! Halting...\n"); debug("aurix_load(): Failed to create kernel pagemap! Halting...\n");
// TODO: Halt // TODO: Halt
while (1); while (1);
} }
axboot_memmap *memmap = get_memmap(pm);
map_pages(pm, (uintptr_t)pm, (uintptr_t)pm, PAGE_SIZE, VMM_WRITABLE); map_pages(pm, (uintptr_t)pm, (uintptr_t)pm, PAGE_SIZE, VMM_WRITABLE);
map_pages(pm, (uintptr_t)_aurix_handoff_start, (uintptr_t)_aurix_handoff_start, (uint64_t)_aurix_handoff_end - (uint64_t)_aurix_handoff_start, 0); map_pages(pm, (uintptr_t)_aurix_handoff_start, (uintptr_t)_aurix_handoff_start, (uint64_t)_aurix_handoff_end - (uint64_t)_aurix_handoff_start, 0);
@ -58,7 +60,7 @@ void aurix_load(char *kernel)
void *kernel_entry = (void *)elf_load(kbuf, pm); void *kernel_entry = (void *)elf_load(kbuf, pm);
if (!kernel_entry) { if (!kernel_entry) {
debug("aurix_load(): Failed to load '%s'! Halting...\n", kernel); debug("aurix_load(): Failed to load '%s'! Halting...\n", kernel_path);
mem_free(kbuf); mem_free(kbuf);
while (1); while (1);
} }
@ -75,6 +77,5 @@ void aurix_load(char *kernel)
"movq %[stack], %%rsp\n" "movq %[stack], %%rsp\n"
"callq *%[entry]\n" "callq *%[entry]\n"
:: [pml4]"r"(pm), [stack]"r"(stack), [entry]"r"(kernel_entry) : "memory"); :: [pml4]"r"(pm), [stack]"r"(stack), [entry]"r"(kernel_entry) : "memory");
// __asm__ volatile("callq *%[entry]\n" __builtin_unreachable();
// :: [entry]"r"(kernel_entry));
} }

50
boot/include/mm/memmap.h Normal file
View file

@ -0,0 +1,50 @@
/*********************************************************************************/
/* Module Name: memmap.h */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#ifndef _MEM_MEMMAP_H
#define _MEM_MEMMAP_H
#include <stdint.h>
enum AxBootMemMapType {
MemMapReserved = 0,
MemMapFaulty = 1,
MemMapACPIReclaimable = 2,
MemMapACPIMappedIO = 3,
MemMapACPIMappedIOPortSpace = 4,
MemMapACPINVS = 5,
MemMapFirmware = 6,
// stuff we can consider usable after loading the kernel
MemMapFreeOnLoad = 7,
MemMapUsable = 10,
};
typedef struct _axboot_memmap {
uintptr_t base;
uintptr_t size;
int type;
} axboot_memmap;
axboot_memmap *get_memmap();
#endif /* _MEM_MEMMAP_H */

View file

@ -0,0 +1,153 @@
/*********************************************************************************/
/* Module Name: memmap.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <mm/memmap.h>
#include <mm/mman.h>
#include <mm/vmm.h>
#include <lib/string.h>
#include <print.h>
#include <axboot.h>
#include <stddef.h>
#include <efi.h>
#include <efilib.h>
static int efi_type_to_axboot(uint32_t efi_type)
{
switch (efi_type) {
case EfiReservedMemoryType:
return MemMapReserved;
case EfiLoaderCode:
return MemMapFirmware;
case EfiLoaderData:
return MemMapFirmware;
case EfiBootServicesCode:
case EfiBootServicesData:
return MemMapFreeOnLoad;
case EfiRuntimeServicesCode:
case EfiRuntimeServicesData:
return MemMapFirmware;
case EfiConventionalMemory:
return MemMapUsable;
case EfiUnusableMemory:
return MemMapFaulty;
case EfiACPIReclaimMemory:
return MemMapACPIReclaimable;
case EfiACPIMemoryNVS:
return MemMapACPINVS;
case EfiMemoryMappedIO:
return MemMapACPIMappedIO;
case EfiMemoryMappedIOPortSpace:
return MemMapACPIMappedIOPortSpace;
case EfiPalCode:
return MemMapUsable;
case EfiPersistentMemory:
return MemMapUsable;
case EfiUnacceptedMemoryType:
return MemMapReserved;
default:
return MemMapReserved;
}
}
axboot_memmap *get_memmap(pagetable *pm)
{
EFI_MEMORY_DESCRIPTOR *efi_map;
EFI_UINTN efi_map_key;
EFI_UINTN size = 0;
EFI_UINTN desc_size;
EFI_UINT32 desc_ver;
EFI_STATUS status;
status = gBootServices->GetMemoryMap(&size, efi_map, &efi_map_key, &desc_size, &desc_ver);
if (EFI_ERROR(status) && status != EFI_BUFFER_TOO_SMALL) {
debug("get_memmap(): GetMemoryMap() returned an error: %s (0x%llx)\n", efi_status_to_str(status), status);
return NULL;
}
efi_map = (EFI_MEMORY_DESCRIPTOR *)mem_alloc(size);
do {
status = gBootServices->GetMemoryMap(&size, efi_map, &efi_map_key, &desc_size, &desc_ver);
if (!EFI_ERROR(status)) {
break;
} else if (status == EFI_BUFFER_TOO_SMALL) {
size += 2 * desc_size;
efi_map = (EFI_MEMORY_DESCRIPTOR *)mem_realloc(efi_map, size);
} else {
debug("get_memmap(): GetMemoryMap() returned an error: %s (0x%llx)\n", efi_status_to_str(status), status);
return NULL;
}
} while (status != EFI_SUCCESS);
EFI_MEMORY_DESCRIPTOR *cur_entry = efi_map;
uint32_t entry_count = size / desc_size;
// map all the memory
for (int i = 0; i < entry_count; i++) {
uint64_t flags;
switch (cur_entry->Type) {
case EfiConventionalMemory:
case EfiBootServicesCode:
case EfiBootServicesData:
flags = VMM_WRITABLE;
break;
case EfiRuntimeServicesCode:
flags = 0;
break;
case EfiRuntimeServicesData:
case EfiACPIReclaimMemory:
case EfiACPIMemoryNVS:
case EfiMemoryMappedIO:
case EfiMemoryMappedIOPortSpace:
flags = VMM_WRITABLE | VMM_NX;
break;
case EfiUnusableMemory:
case EfiReservedMemoryType:
case EfiLoaderData:
case EfiLoaderCode:
case EfiPalCode:
flags = 0;
break;
default:
flags = VMM_WRITABLE | VMM_NX;
break;
}
map_pages(pm, cur_entry->PhysicalStart, cur_entry->PhysicalStart, cur_entry->NumberOfPages * PAGE_SIZE, flags);
cur_entry = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)cur_entry + desc_size);
}
axboot_memmap *new_map = (axboot_memmap *)mem_alloc(sizeof(axboot_memmap) * entry_count);
memset(new_map, 0, sizeof(axboot_memmap) * entry_count);
cur_entry = efi_map;
// translate efi memmap to axboot memmap
for (int i = 0; i < entry_count; i++) {
new_map[i].base = cur_entry->PhysicalStart;
new_map[i].size = cur_entry->NumberOfPages * PAGE_SIZE;
new_map[i].type = efi_type_to_axboot(efi_map[i].Type);
cur_entry = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)cur_entry + desc_size);
}
mem_free(efi_map);
return new_map;
}