From ec94e2341c124a89d97c2f78128d51900215b50d Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Sun, 11 Jan 2026 13:15:28 +0100 Subject: [PATCH] Relocate kernel and modules to KSEG0 memory space --- boot/xtldr/modules/pecoff/pecoff.cc | 2 +- boot/xtldr/modules/xtos_o/includes/xtos.hh | 21 ++----- boot/xtldr/modules/xtos_o/xtos.cc | 70 ++++++++++++++++++++-- 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/boot/xtldr/modules/pecoff/pecoff.cc b/boot/xtldr/modules/pecoff/pecoff.cc index 00f105a..7d9692c 100644 --- a/boot/xtldr/modules/pecoff/pecoff.cc +++ b/boot/xtldr/modules/pecoff/pecoff.cc @@ -729,11 +729,11 @@ PeCoff::RelocateLoadedImage(IN PPECOFF_IMAGE_CONTEXT Image) } else { - /* Check if loaded 32-bit PE32 image should be relocated */ /* Set relocation data directory and image base address */ DataDirectory = &Image->PeHeader->OptionalHeader32.DataDirectory[PECOFF_IMAGE_DIRECTORY_ENTRY_BASERELOC]; ImageBase = Image->PeHeader->OptionalHeader32.ImageBase; + /* Check if loaded 32-bit PE32 image should be relocated */ if(Image->PeHeader->OptionalHeader32.NumberOfRvaAndSizes <= PECOFF_IMAGE_DIRECTORY_ENTRY_BASERELOC || DataDirectory->VirtualAddress == 0 || DataDirectory->Size < sizeof(PECOFF_IMAGE_BASE_RELOCATION)) { diff --git a/boot/xtldr/modules/xtos_o/includes/xtos.hh b/boot/xtldr/modules/xtos_o/includes/xtos.hh index 4d70606..ea37111 100644 --- a/boot/xtldr/modules/xtos_o/includes/xtos.hh +++ b/boot/xtldr/modules/xtos_o/includes/xtos.hh @@ -33,12 +33,6 @@ class Xtos IN PEFI_SYSTEM_TABLE SystemTable); private: - STATIC XTCDECL EFI_STATUS AddVirtualMemoryMapping(IN PLIST_ENTRY MemoryMappings, - IN PVOID VirtualAddress, - IN PVOID PhysicalAddress, - IN UINT NumberOfPages, - IN LOADER_MEMORY_TYPE MemoryType); - STATIC XTCDECL LOADER_MEMORY_TYPE ConvertEfiMemoryType(IN EFI_MEMORY_TYPE EfiMemoryType); STATIC XTCDECL ULONG DeterminePagingLevel(IN CONST PWCHAR Parameters); STATIC XTCDECL EFI_STATUS EnablePaging(IN PXTBL_PAGE_MAPPING PageMap); STATIC XTCDECL VOID GetDisplayInformation(OUT PSYSTEM_RESOURCE_FRAMEBUFFER FrameBufferResource, @@ -51,26 +45,21 @@ class Xtos STATIC XTCDECL EFI_STATUS GetSystemResourcesList(IN PXTBL_PAGE_MAPPING PageMap, IN PVOID *VirtualAddress, OUT PLIST_ENTRY SystemResourcesList); - STATIC XTCDECL EFI_STATUS GetVirtualAddress(IN PLIST_ENTRY MemoryMappings, - IN PVOID PhysicalAddress, - OUT PVOID *VirtualAddress); STATIC XTCDECL EFI_STATUS InitializeApicBase(IN PXTBL_PAGE_MAPPING PageMap); STATIC XTCDECL EFI_STATUS InitializeLoaderBlock(IN PXTBL_PAGE_MAPPING PageMap, IN PVOID *VirtualAddress, IN PXTBL_BOOT_PARAMETERS Parameters); - STATIC XTCDECL EFI_STATUS InitializeVirtualMemory(IN OUT PLIST_ENTRY MemoryMappings, - IN OUT PVOID *MemoryMapAddress); STATIC XTCDECL EFI_STATUS LoadModule(IN PEFI_FILE_HANDLE BootDir, IN PWCHAR FileName, IN PVOID VirtualAddress, IN LOADER_MEMORY_TYPE MemoryType, OUT PPECOFF_IMAGE_CONTEXT *ImageContext); STATIC XTCDECL EFI_STATUS MapHardwareMemoryPool(IN PXTBL_PAGE_MAPPING PageMap); - STATIC XTCDECL EFI_STATUS MapVirtualMemory(IN PLIST_ENTRY MemoryMappings, - IN UINT_PTR VirtualAddress, - IN UINT_PTR PhysicalAddress, - IN UINT NumberOfPages, - IN OUT PVOID *PtePointer); + STATIC XTCDECL EFI_STATUS MapMemory(IN OUT PXTBL_PAGE_MAPPING PageMap, + IN ULONGLONG Address, + IN ULONG NumberOfPages, + IN LOADER_MEMORY_TYPE MemoryType, + IN BOOLEAN KernelMapping); STATIC XTCDECL EFI_STATUS RunBootSequence(IN PEFI_FILE_HANDLE BootDir, IN PXTBL_BOOT_PARAMETERS Parameters); }; diff --git a/boot/xtldr/modules/xtos_o/xtos.cc b/boot/xtldr/modules/xtos_o/xtos.cc index 3482003..e35331e 100644 --- a/boot/xtldr/modules/xtos_o/xtos.cc +++ b/boot/xtldr/modules/xtos_o/xtos.cc @@ -266,6 +266,7 @@ Xtos::GetSystemResourcesList(IN PXTBL_PAGE_MAPPING PageMap, { return Status; } + Status = XtLdrProtocol->Memory.MapVirtualMemory(PageMap, (ULONGLONG)*VirtualAddress, Address, Pages, LoaderFirmwarePermanent); if(Status != STATUS_EFI_SUCCESS) { @@ -545,8 +546,8 @@ Xtos::LoadModule(IN PEFI_FILE_HANDLE SystemDir, return Status; } - /* Load the PE/COFF image file */ - Status = PeCoffProtocol->LoadImage(ModuleHandle, MemoryType, VirtualAddress, (PVOID*)ImageContext); + /* Load the PE/COFF image file into memory */ + Status = PeCoffProtocol->LoadImage(ModuleHandle, MemoryType, NULLPTR, (PVOID*)ImageContext); if(Status != STATUS_EFI_SUCCESS) { /* Unable to load the file */ @@ -554,6 +555,15 @@ Xtos::LoadModule(IN PEFI_FILE_HANDLE SystemDir, return Status; } + /* Relocate the PE/COFF image file */ + Status = PeCoffProtocol->RelocateImage(*ImageContext, KSEG0_BASE + (ULONGLONG)(*ImageContext)->PhysicalAddress); + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to relocate the file */ + XtLdrProtocol->Debug.Print(L"ERROR: Failed to relocate '%S'\n", FileName); + return Status; + } + /* Close image file */ ModuleHandle->Close(ModuleHandle); @@ -577,12 +587,60 @@ Xtos::LoadModule(IN PEFI_FILE_HANDLE SystemDir, /* Print debug message */ XtLdrProtocol->Debug.Print(L"Loaded %S at PA: %P, VA: %P\n", FileName, - (*ImageContext)->PhysicalAddress, (*ImageContext)->VirtualAddress); + (*ImageContext)->PhysicalAddress, KSEG0_BASE + (ULONGLONG)(*ImageContext)->VirtualAddress); /* Return success */ return STATUS_EFI_SUCCESS; } +/** + * Adds a physical to virtual address mapping. + * + * @param PageMap + * Supplies a pointer to the page mapping structure. + * + * @param PhysicalAddress + * Supplies a physical address which will be mapped. + * + * @param NumberOfPages + * Supplies a number of pages that will be mapped. + * + * @param MemoryType + * Supplies the type of mapped memory that will be assigned to the memory descriptor. + * + * @param KernelMapping + * Supplies a flag that indicates if mapping should be done in kernel mode or if identity mapping should be used. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +Xtos::MapMemory(IN OUT PXTBL_PAGE_MAPPING PageMap, + IN ULONGLONG Address, + IN ULONG NumberOfPages, + IN LOADER_MEMORY_TYPE MemoryType, + IN BOOLEAN KernelMapping) +{ + ULONGLONG BaseAddress; + + /* Check if kernel mode mapping */ + if(KernelMapping) + { + /* Map memory based on kernel base address */ + BaseAddress = KSEG0_BASE; + } + else + { + /* Use identity mapping */ + BaseAddress = (ULONGLONG)NULLPTR; + } + + /* Map memory and return status code */ + return XtLdrProtocol->Memory.MapVirtualMemory(PageMap, BaseAddress + Address, Address, NumberOfPages, MemoryType); +} + /** * This routine initiates an XTOS boot sequence. * @@ -650,11 +708,11 @@ Xtos::RunBootSequence(IN PEFI_FILE_HANDLE BootDir, } /* Add kernel image memory mapping */ - Status = XtLdrProtocol->Memory.MapVirtualMemory(&PageMap, (ULONGLONG)ImageContext->VirtualAddress, - (ULONGLONG)ImageContext->PhysicalAddress, ImageContext->ImagePages, - LoaderSystemCode); + Status = MapMemory(&PageMap, (ULONGLONG)ImageContext->PhysicalAddress, + ImageContext->ImagePages, LoaderSystemCode, TRUE); if(Status != STATUS_EFI_SUCCESS) { + /* Failed to map kernel image memory */ return Status; }