Truly enable virtual memory.

Map is still identity-mapped. I think we need to sort and/or clean up
the map before using a higher half address. In-kernel vga output not
working yet, but do_the_set_registers() is getting called.
This commit is contained in:
Justin C. Miller
2018-03-25 13:51:32 -07:00
parent 417f080993
commit 64a6d88e5c
5 changed files with 52 additions and 35 deletions

View File

@@ -2,15 +2,15 @@
#include <efi.h> #include <efi.h>
#ifndef KERNEL_PHYS_ADDRESS #ifndef KERNEL_PHYS_ADDRESS
#define KERNEL_PHYS_ADDRESS 0x0000000000100000 #define KERNEL_PHYS_ADDRESS 0x100000
#endif #endif
#ifndef KERNEL_VIRT_ADDRESS #ifndef KERNEL_VIRT_ADDRESS
#define KERNEL_VIRT_ADDRESS 0x7fff000000000000 #define KERNEL_VIRT_ADDRESS 0xf00000000
#endif #endif
#ifndef KERNEL_MEMTYPE #ifndef KERNEL_MEMTYPE
#define KERNEL_MEMTYPE 0xffffffff #define KERNEL_MEMTYPE 0x80000000
#endif #endif
#ifndef KERNEL_FILENAME #ifndef KERNEL_FILENAME

View File

@@ -38,16 +38,6 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
// From here on out, use CHECK_EFI_STATUS_OR_FAIL instead // From here on out, use CHECK_EFI_STATUS_OR_FAIL instead
// because the console is now set up // because the console is now set up
// Get info about the image
/*
con_status_begin(L"Gathering image information...");
EFI_LOADED_IMAGE *info = 0;
EFI_GUID image_proto = EFI_LOADED_IMAGE_PROTOCOL_GUID;
status = ST->BootServices->HandleProtocol(ImageHandle, &image_proto, (void **)&info);
CHECK_EFI_STATUS_OR_FAIL(status);
con_status_ok();
*/
con_status_begin(L"Loading kernel into memory..."); con_status_begin(L"Loading kernel into memory...");
void *kernel_image = NULL; void *kernel_image = NULL;
uint64_t kernel_length = 0; uint64_t kernel_length = 0;
@@ -68,7 +58,6 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
version->gitsha & 0x0fffffff, version->gitsha & 0x0fffffff,
version->gitsha & 0xf0000000 ? "*" : "" version->gitsha & 0xf0000000 ? "*" : ""
); );
Print(L"\n Entrypoint %d", version->entrypoint); Print(L"\n Entrypoint %d", version->entrypoint);
void (*kernel_main)() = version->entrypoint; void (*kernel_main)() = version->entrypoint;
@@ -97,14 +86,12 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
CHECK_EFI_STATUS_OR_FAIL(status); CHECK_EFI_STATUS_OR_FAIL(status);
status = ST->BootServices->ExitBootServices(ImageHandle, memmap_key); status = ST->BootServices->ExitBootServices(ImageHandle, memmap_key);
CHECK_EFI_STATUS_OR_FAIL(status); CHECK_EFI_STATUS_OR_ASSERT(status, 0);
status = memory_virtualize( status = memory_virtualize(
&kernel_image,
memory_map, memmap_size, memory_map, memmap_size,
desc_size, desc_version); desc_size, desc_version);
CHECK_EFI_STATUS_OR_ASSERT(status, 0);
CHECK_EFI_STATUS_OR_FAIL(status);
kernel_main(); kernel_main();
return EFI_LOAD_ERROR; return EFI_LOAD_ERROR;

View File

@@ -26,13 +26,19 @@ const CHAR16 *memory_type_names[] = {
}; };
static const CHAR16 *memory_type_name(UINT32 value) { static const CHAR16 *memory_type_name(UINT32 value) {
if (value >= (sizeof(memory_type_names)/sizeof(CHAR16*))) if (value >= (sizeof(memory_type_names)/sizeof(CHAR16*))) {
if (value == KERNEL_MEMTYPE)
return L"Kernel Image";
return L"Bad Type Value"; return L"Bad Type Value";
}
return memory_type_names[value]; return memory_type_names[value];
} }
void EFIAPI memory_update_addresses(EFI_EVENT UNUSED *event, void *context) { void EFIAPI memory_update_addresses(EFI_EVENT UNUSED *event, void *context) {
ST->RuntimeServices->ConvertPointer(0, (void **)context); EFI_STATUS status;
status = ST->RuntimeServices->ConvertPointer(0, (void **)context);
CHECK_EFI_STATUS_OR_ASSERT(status, *((void**)context));
} }
EFI_STATUS memory_mark_address_for_update(void **pointer) { EFI_STATUS memory_mark_address_for_update(void **pointer) {
@@ -46,17 +52,29 @@ EFI_STATUS memory_mark_address_for_update(void **pointer) {
(void*)pointer, (void*)pointer,
&event); &event);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to create memory update event"); CHECK_EFI_STATUS_OR_ASSERT(status, pointer);
} }
EFI_STATUS memory_virtualize(void **kernel_addr, EFI_MEMORY_DESCRIPTOR *memory_map, EFI_STATUS
UINTN memmap_size, UINTN desc_size, UINT32 desc_version) { memory_virtualize(
unsigned count = memmap_size / desc_size; EFI_MEMORY_DESCRIPTOR *memory_map,
for (unsigned i=0; i<count; ++i) { UINTN memmap_size,
if (memory_map[i].Type == KERNEL_MEMTYPE) UINTN desc_size,
memory_map[i].VirtualStart = (EFI_VIRTUAL_ADDRESS)KERNEL_VIRT_ADDRESS; UINT32 desc_version) {
else
memory_map[i].VirtualStart = (EFI_VIRTUAL_ADDRESS)memory_map[i].PhysicalStart; EFI_MEMORY_DESCRIPTOR *end = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)memory_map + memmap_size);
EFI_MEMORY_DESCRIPTOR *d = memory_map;
while (d < end) {
if (d->Type == KERNEL_MEMTYPE) {
//d->VirtualStart = (EFI_VIRTUAL_ADDRESS)KERNEL_VIRT_ADDRESS;
d->VirtualStart = (EFI_VIRTUAL_ADDRESS)d->PhysicalStart;
d->Attribute |= EFI_MEMORY_RUNTIME;
}
else /*if (d->Attribute & EFI_MEMORY_RUNTIME)*/ {
d->VirtualStart = (EFI_VIRTUAL_ADDRESS)d->PhysicalStart;
}
d = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)d + desc_size);
} }
return ST->RuntimeServices->SetVirtualAddressMap(memmap_size, desc_size, desc_version, memory_map); return ST->RuntimeServices->SetVirtualAddressMap(memmap_size, desc_size, desc_version, memory_map);
@@ -101,10 +119,9 @@ EFI_STATUS memory_dump_map() {
EFI_MEMORY_DESCRIPTOR *end = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)buffer + buffer_size); EFI_MEMORY_DESCRIPTOR *end = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)buffer + buffer_size);
EFI_MEMORY_DESCRIPTOR *d = buffer; EFI_MEMORY_DESCRIPTOR *d = buffer;
while (d < end) { while (d < end) {
UINTN size_bytes = d->NumberOfPages * PAGE_SIZE; int runtime = (d->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME;
Print(L"%23s%s ", memory_type_name(d->Type), runtime ? L"*" : L" ");
Print(L"%23s ", memory_type_name(d->Type)); Print(L"%016llx (%3d pages)\n", d->PhysicalStart, d->NumberOfPages);
Print(L"%016llx (%3d pages)\n", d->PhysicalStart, size_bytes / 0x1000);
d = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)d + desc_size); d = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)d + desc_size);
} }

View File

@@ -4,7 +4,6 @@
EFI_STATUS memory_mark_address_for_update(void **pointer); EFI_STATUS memory_mark_address_for_update(void **pointer);
EFI_STATUS memory_virtualize( EFI_STATUS memory_virtualize(
void **kernel_image,
EFI_MEMORY_DESCRIPTOR *memory_map, EFI_MEMORY_DESCRIPTOR *memory_map,
UINTN memmap_size, UINTN memmap_size,
UINTN desc_size, UINTN desc_size,

View File

@@ -18,3 +18,17 @@ const CHAR16 *util_error_message(EFI_STATUS status);
while (1) __asm__("hlt"); \ while (1) __asm__("hlt"); \
} }
#define CHECK_EFI_STATUS_OR_ASSERT(s, d) \
if (EFI_ERROR((s))) { \
__asm__ __volatile__ ( \
"movq %0, %%r8;" \
"movq %1, %%r9;" \
"movq %2, %%r10;" \
"movq $0, %%rdx;" \
"divq %%rdx;" \
: \
:"r"((uint64_t)s), "r"((uint64_t)d), "r"((uint64_t)__LINE__) \
:"rax", "rdx", "r8", "r9", "r10" \
); \
}