21#include <Library/BaseLib.h>
22#include <Library/BaseMemoryLib.h>
23#include <Library/DebugLib.h>
30#include <Library/PrintLib.h>
31#include <Library/UefiBootServicesTableLib.h>
32#include <Library/UefiLib.h>
33#include <Library/UefiRuntimeServicesTableLib.h>
42 IN EFI_GET_MEMORY_MAP GetMemoryMap,
43 IN EFI_ALLOCATE_PAGES AllocatePages,
44 IN UINTN NumberOfPages,
45 IN OUT EFI_PHYSICAL_ADDRESS *Memory
52 if (EssentialSize == 0) {
53 return EFI_UNSUPPORTED;
63 BootCompat->KernelState.RelocationBlock = BASE_4GB;
67 EFI_SIZE_TO_PAGES (EssentialSize),
68 &BootCompat->KernelState.RelocationBlock,
73 if (EFI_ERROR (Status)) {
76 "OCABC: Relocation block (0x%Lx) allocation failure - %r\n",
83 BootCompat->KernelState.RelocationBlockUsed = 0;
90 if ( (BootCompat->KernelState.RelocationBlock != 0)
91 && BootCompat->KernelState.RelocationBlockLegacy
92 && (*Memory > BootCompat->KernelState.RelocationBlock)
93 && (*Memory < BootCompat->KernelState.RelocationBlock + EssentialSize))
101 if ( (BootCompat->KernelState.RelocationBlock == 0)
105 return EFI_INVALID_PARAMETER;
111 EssentialSize = (UINTN)(*Memory -
KERNEL_BASE_PADDR + EFI_PAGES_TO_SIZE (NumberOfPages));
112 if (EssentialSize > BootCompat->KernelState.RelocationBlockUsed) {
113 BootCompat->KernelState.RelocationBlockUsed = EssentialSize;
133 if (EssentialSize == 0) {
134 return EFI_UNSUPPORTED;
137 if (BootCompat->KernelState.RelocationBlock == 0) {
138 return EFI_INVALID_PARAMETER;
141 Status =
gBS->FreePages (
142 BootCompat->KernelState.RelocationBlock,
143 EFI_SIZE_TO_PAGES (EssentialSize)
146 BootCompat->KernelState.RelocationBlock = 0;
147 BootCompat->KernelState.RelocationBlockUsed = 0;
148 BootCompat->KernelState.RelocationBlockLegacy = FALSE;
161 UINTN DescriptorSize;
162 UINT32 DescriptorVersion;
163 EFI_MEMORY_DESCRIPTOR *MemoryMap;
164 EFI_PHYSICAL_ADDRESS KernelRTAddress;
167 EFI_MEMORY_DESCRIPTOR *Desc;
169 UINT8 *KernelRTBlock;
171 MemoryMapSize = *BA->MemoryMapSize;
172 DescriptorSize = *BA->MemoryMapDescriptorSize;
173 DescriptorVersion = *BA->MemoryMapDescriptorVersion;
174 MemoryMap = (EFI_MEMORY_DESCRIPTOR *)(UINTN)*BA->MemoryMap;
175 KernelRTAddress = EFI_PAGES_TO_SIZE (*BA->RuntimeServicesPG)
182 NumEntries = MemoryMapSize / DescriptorSize;
184 for (Index = 0; Index < NumEntries; ++Index) {
185 BlockSize = EFI_PAGES_TO_SIZE ((UINTN)Desc->NumberOfPages);
187 if (Desc->Type == EfiReservedMemoryType) {
188 Desc->Attribute &= ~EFI_MEMORY_RUNTIME;
189 }
else if ((Desc->Attribute & EFI_MEMORY_RUNTIME) != 0) {
194 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
200 Status =
gRT->SetVirtualAddressMap (
213 for (Index = 0; Index < NumEntries; ++Index) {
214 if ((Desc->Type == EfiRuntimeServicesCode) || (Desc->Type == EfiRuntimeServicesData)) {
218 KernelRTBlock = (UINT8 *)(UINTN)(Desc->VirtualStart & (BASE_1GB - 1));
219 BlockSize = EFI_PAGES_TO_SIZE ((UINTN)Desc->NumberOfPages);
222 (VOID *)(UINTN)Desc->PhysicalStart,
231 if ( (Desc->PhysicalStart <= *BA->SystemTableP)
234 *BA->SystemTableP = (UINT32)((UINTN)KernelRTBlock
235 + (*BA->SystemTableP - Desc->PhysicalStart)
242 Desc->Type = EfiConventionalMemory;
243 Desc->Attribute = Desc->Attribute & (~EFI_MEMORY_RUNTIME);
246 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
266 UINT32 MemoryMapSize;
267 UINT32 DescriptorSize;
268 EFI_MEMORY_DESCRIPTOR *MemoryMap;
271 EFI_MEMORY_DESCRIPTOR *Desc;
272 EFI_PHYSICAL_ADDRESS PrevDescAddress;
275 PropIter = &OPropIter;
277 RelocDiff = (UINT32)(BootCompat->KernelState.RelocationBlock -
KERNEL_BASE_PADDR);
279 DTInit ((
DTEntry)(UINTN)*BA->DeviceTreeP, BA->DeviceTreeLength);
282 if (!EFI_ERROR (Status)) {
284 if (!EFI_ERROR (Status)) {
302 if ( (PropValue->
Address < BootCompat->KernelState.RelocationBlock)
303 || (PropValue->
Address >= BootCompat->KernelState.RelocationBlock + BootCompat->KernelState.RelocationBlockUsed))
315 if (!BootCompat->KernelState.RelocationBlockLegacy) {
330 PropValue->
Address -= RelocDiff;
341 if (!BootCompat->KernelState.RelocationBlockLegacy) {
342 *BA->SystemTableP -= RelocDiff;
344 MemoryMapSize = *BA->MemoryMapSize;
345 DescriptorSize = *BA->MemoryMapDescriptorSize;
346 MemoryMap = (EFI_MEMORY_DESCRIPTOR *)(UINTN)*BA->MemoryMap;
349 PrevDescAddress = Desc->PhysicalStart;
350 NumEntries = MemoryMapSize / DescriptorSize;
356 for (Index = 0; Index < NumEntries; ++Index) {
357 if (Desc->PhysicalStart < PrevDescAddress) {
358 *BA->MemoryMapSize -= (DescriptorSize * (NumEntries - Index));
362 PrevDescAddress = Desc->PhysicalStart;
363 Desc = NEXT_MEMORY_DESCRIPTOR (Desc, DescriptorSize);
367 *BA->MemoryMap -= RelocDiff;
368 *BA->KernelAddrP -= RelocDiff;
369 *BA->RuntimeServicesPG -= EFI_SIZE_TO_PAGES (RelocDiff);
374 *BA->DeviceTreeP -= RelocDiff;
397 Payload = (VOID *)(UINTN)CallGate;
406 BootCompat->KernelState.RelocationBlockUsed / sizeof (UINT64),
408 BootCompat->KernelState.RelocationBlock,
MEMMAP_DEVICE_PATH MemMap
UINTN(EFIAPI * KERNEL_CALL_GATE)(IN UINTN Arg1, IN UINTN Arg2)
#define KERNEL_STATIC_VADDR
UINTN(EFIAPI * RELOCATION_CALL_GATE)(IN UINTN QWordCount, IN UINTN EntryPoint, IN EFI_PHYSICAL_ADDRESS Source, IN UINTN Args)
UINTN AppleSlideGetRelocationSize(IN OUT BOOT_COMPAT_CONTEXT *BootCompat)
#define KERNEL_BASE_PADDR
#define KERNEL_TEXT_PADDR_LEGACY
#define KERNEL_TEXT_PADDR
#define ESTIMATED_CALL_GATE_SIZE
EFI_STATUS DTLookupEntry(IN CONST DTEntry SearchPoint, IN CONST CHAR8 *PathName, IN DTEntry *FoundEntry)
VOID DTInit(IN VOID *Base, IN UINT32 *Length)
EFI_STATUS DTIterateProperties(IN DTPropertyIterator Iterator, IN CHAR8 **FoundProperty)
#define DT_BOOTER_KEXT_PREFIX
EFI_STATUS DTCreatePropertyIterator(IN CONST DTEntry Entry, IN DTPropertyIterator Iterator)
EFI_STATUS OcAllocatePagesFromTop(IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, IN OUT EFI_PHYSICAL_ADDRESS *Memory, IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL, IN EFI_ALLOCATE_PAGES AllocatePages OPTIONAL, IN CHECK_ALLOCATION_RANGE CheckRange OPTIONAL)
#define LAST_DESCRIPTOR_ADDR(Desc)
#define L_STR_LEN(String)
EFI_STATUS AppleRelocationRelease(IN OUT BOOT_COMPAT_CONTEXT *BootCompat)
EFI_STATUS AppleRelocationVirtualize(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT OC_BOOT_ARGUMENTS *BA)
VOID AppleRelocationRebase(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT OC_BOOT_ARGUMENTS *BA)
VOID AppleRelocationCallGate64(IN OUT UINTN *Args, IN BOOT_COMPAT_CONTEXT *BootCompat, IN KERNEL_CALL_GATE CallGate, IN UINTN *KcgArg1, IN UINTN KcgArg2)
EFI_STATUS AppleRelocationAllocatePages(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN EFI_GET_MEMORY_MAP GetMemoryMap, IN EFI_ALLOCATE_PAGES AllocatePages, IN UINTN NumberOfPages, IN OUT EFI_PHYSICAL_ADDRESS *Memory)
STATIC CONST UINT8 mAsmRelocationCallGate[]
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_RUNTIME_SERVICES * gRT
UINT32 BundlePathPhysAddr
UINT32 ExecutablePhysAddr
UINT32 Length
Length (bytes) of folloing prop value.
DTProperty * CurrentProperty