main: refactor to keep the EFI headers from polluting the main namespace

This commit is contained in:
2021-11-06 23:40:50 +08:00
parent e2a2679f6d
commit 4460661bfd
13 changed files with 89 additions and 85 deletions

View File

@ -1,5 +1,6 @@
#include "../main.h"
#include "../efimain.h"
#include "memory.h"
#include "../runtime/stdio.h"
#include "../extlib/dlmalloc/malloc-2.8.6.h"

View File

@ -2,6 +2,7 @@
#include "../main.h"
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
@ -43,9 +44,6 @@ static inline uint64_t paging_MapFunction(void *func) {
}
// efiMallocTyped allocates from EFI_BOOT_SERVICES.AllocatePool.
void *efiMallocTyped(size_t size, EFI_MEMORY_TYPE type);
// efiMallocTyped allocates from EFI_BOOT_SERVICES.AllocatePool
// with a memory type of EfiLoaderData.
void *efiMalloc(size_t size);
@ -61,12 +59,6 @@ void *kMalloc(size_t size);
void kFree(void *data);
extern EFI_MEMORY_DESCRIPTOR *efiMemoryMap;
extern UINTN efiMemoryMapSize;
extern UINTN efiMemoryMapKey;
extern UINTN efiDescriptorSize;
extern UINT32 efiDescriptorVertion;
// runtime_InitPaging initializes paging and kMalloc/kFree allocator.
// This function calls ExitBootServices()!!! which is great
// Furthermore, it sets up a new stack, calls kMain() and does not return.

11
memory/memory_internal.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "memory.h"
#include "../efimain.h"
extern EFI_MEMORY_DESCRIPTOR *efiMemoryMap;
extern UINTN efiMemoryMapSize;
extern UINTN efiMemoryMapKey;
extern UINTN efiDescriptorSize;
extern UINT32 efiDescriptorVertion;

View File

@ -1,5 +1,6 @@
#include "../main.h"
#include "../efimain.h"
#include "memory.h"
#include "../runtime/panic_assert.h"
#include "../runtime/stdio.h"
@ -13,6 +14,7 @@ void execformat_pe_ReadSystemHeader(execformat_pe_PortableExecutable *pe);
#include <string.h>
#include "paging_internal.h"
#include "paging_internal_efi.h"
EFI_MEMORY_DESCRIPTOR *efiMemoryMap;
@ -111,7 +113,6 @@ void runtime_InitPaging() {
paging_UsableBytes / 1024.0 / 1024.0,
paging_UsableBytes / 1024.0 / 1024.0 / 1024.0);
io_PauseForKeystroke();
assert(paging_LoaderCodeAddress && "EfiLoaderCode mapping not found");

View File

@ -9,13 +9,6 @@ extern "C" {
#endif
// defined in paging_init.c
extern EFI_MEMORY_DESCRIPTOR *efiMemoryMap;
extern UINTN efiMemoryMapSize;
extern UINTN efiMemoryMapKey;
extern UINTN efiDescriptorSize;
extern UINT32 efiDescriptorVertion;
// defined in paging_init.c
extern uint64_t paging_TotalBytes, paging_UsableBytes;
extern bool paging_SupportExecuteDisable;
@ -67,37 +60,6 @@ FASTCALL_ABI void paging_modeswitch_4LevelPaging(void *pml4, int pcid);
FASTCALL_ABI void paging_modeswitch_4LevelPagingNX(void *pml4, int pcid); // with setting the Execute-Disalbe bit
FASTCALL_ABI void paging_modeswitch_Table(void *pml, int pcid);
static inline const char *
memoryTypeName(EFI_MEMORY_TYPE type) {
#define CASE(c) \
case c: \
return #c;
switch (type) {
CASE(EfiReservedMemoryType)
CASE(EfiLoaderCode)
CASE(EfiLoaderData)
CASE(EfiBootServicesCode)
CASE(EfiBootServicesData)
CASE(EfiRuntimeServicesCode)
CASE(EfiRuntimeServicesData)
CASE(EfiConventionalMemory)
CASE(EfiUnusableMemory)
CASE(EfiACPIReclaimMemory)
CASE(EfiACPIMemoryNVS)
CASE(EfiMemoryMappedIO)
CASE(EfiMemoryMappedIOPortSpace)
CASE(EfiPalCode)
case EfiMaxMemoryType:
return "EfiPersistentMemory";
}
return "(unknown)";
#undef CASE
}
#ifndef NEXT_MEMORY_DESCRITOR
#define NEXT_MEMORY_DESCRITOR(desc, size) ((EFI_MEMORY_DESCRIPTOR *)((char *)desc + size)))
#endif
inline static uint64_t roundUpTo2Exponent(uint64_t v) {
uint64_t s = 1;

View File

@ -0,0 +1,42 @@
#pragma once
#include "../efimain.h"
// defined in paging_init.c
extern EFI_MEMORY_DESCRIPTOR *efiMemoryMap;
extern UINTN efiMemoryMapSize;
extern UINTN efiMemoryMapKey;
extern UINTN efiDescriptorSize;
extern UINT32 efiDescriptorVertion;
static inline const char *
memoryTypeName(EFI_MEMORY_TYPE type) {
#define CASE(c) \
case c: \
return #c;
switch (type) {
CASE(EfiReservedMemoryType)
CASE(EfiLoaderCode)
CASE(EfiLoaderData)
CASE(EfiBootServicesCode)
CASE(EfiBootServicesData)
CASE(EfiRuntimeServicesCode)
CASE(EfiRuntimeServicesData)
CASE(EfiConventionalMemory)
CASE(EfiUnusableMemory)
CASE(EfiACPIReclaimMemory)
CASE(EfiACPIMemoryNVS)
CASE(EfiMemoryMappedIO)
CASE(EfiMemoryMappedIOPortSpace)
CASE(EfiPalCode)
case EfiMaxMemoryType:
return "EfiPersistentMemory";
}
return "(unknown)";
#undef CASE
}
#ifndef NEXT_MEMORY_DESCRITOR
#define NEXT_MEMORY_DESCRITOR(desc, size) ((EFI_MEMORY_DESCRIPTOR *)((char *)desc + size)))
#endif