OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
KernelSupport64.c
Go to the documentation of this file.
1
16
17#include <Guid/OcVariable.h>
19
20#include <Library/BaseLib.h>
21#include <Library/BaseMemoryLib.h>
22#include <Library/DebugLib.h>
26#include <Library/OcMachoLib.h>
27#include <Library/OcMemoryLib.h>
28#include <Library/OcMiscLib.h>
29#include <Library/OcStringLib.h>
30#include <Library/PrintLib.h>
31#include <Library/UefiBootServicesTableLib.h>
32#include <Library/UefiLib.h>
33#include <Library/UefiRuntimeServicesTableLib.h>
34
35VOID
37 IN OUT BOOT_COMPAT_CONTEXT *BootCompat,
38 IN EFI_PHYSICAL_ADDRESS CallGate,
39 IN UINTN HookAddress
40 )
41{
42 CALL_GATE_JUMP *CallGateJump;
43
44 //
45 // There is no reason to patch the kernel when we do not need it.
46 //
47 if ( !BootCompat->Settings.AvoidRuntimeDefrag
48 && !BootCompat->Settings.DiscardHibernateMap
49 && !BootCompat->Settings.AllowRelocationBlock
50 && !BootCompat->Settings.DisableSingleUser
51 && !BootCompat->Settings.ForceBooterSignature)
52 {
53 return;
54 }
55
56 //
57 // Check whether we have address and abort if not.
58 //
59 if (CallGate == 0) {
60 RUNTIME_DEBUG ((DEBUG_ERROR, "OCABC: Failed to find call gate address\n"));
61 return;
62 }
63
64 CallGateJump = (VOID *)(UINTN)CallGate;
65
66 //
67 // Move call gate jump bytes front.
68 // Performing this on the EfiBootRt KCG may bork the binary, but right now
69 // only corrupts an unused string.
70 //
71 CopyMem (
72 CallGateJump + 1,
73 CallGateJump,
75 );
76 //
77 // lea r8, [rip+XXX]
78 // Passes KCG as third argument to be relocatable. macOS 13 Developer Beta 1
79 // copies the KCG into a separately allocated buffer.
80 //
81 CallGateJump->LeaRip.Command[0] = 0x4C;
82 CallGateJump->LeaRip.Command[1] = 0x8D;
83 CallGateJump->LeaRip.Command[2] = 0x05;
84 CallGateJump->LeaRip.Argument = sizeof (*CallGateJump) - sizeof (CallGateJump->LeaRip);
85 CallGateJump->Jmp.Command = 0x25FF;
86 CallGateJump->Jmp.Argument = 0x0;
87 CallGateJump->Jmp.Address = HookAddress;
88}
89
90STATIC
91UINTN
92EFIAPI
94 IN UINTN *Args,
95 IN UINTN EntryPoint,
96 IN KERNEL_CALL_GATE CallGate,
97 IN UINTN *Arg1,
98 IN UINTN Arg2
99 )
100{
101 BOOT_COMPAT_CONTEXT *BootCompatContext;
102
103 BootCompatContext = GetBootCompatContext ();
104
105 if (BootCompatContext->ServiceState.AppleHibernateWake) {
107 BootCompatContext,
108 *Args
109 );
110 } else {
112 BootCompatContext,
113 (VOID *)*Args
114 );
115 }
116
117 if (BootCompatContext->KernelState.RelocationBlock != 0) {
118 //
119 // Does not return.
120 //
122 Args,
123 BootCompatContext,
124 CallGate,
125 Arg1,
126 Arg2
127 );
128 }
129
130 return CallGate (*Arg1, Arg2);
131}
132
133EFI_STATUS
134EFIAPI
136 IN UINTN SystemTable,
137 IN OUT APPLE_EFI_BOOT_RT_KCG_ARGS *KcgArguments,
138 IN KERNEL_CALL_GATE CallGate
139 )
140{
142 &KcgArguments->Args,
143 KcgArguments->EntryPoint,
144 CallGate,
145 &SystemTable,
146 (UINTN)KcgArguments
147 );
148}
149
150UINTN
151EFIAPI
153 IN UINTN Args,
154 IN UINTN EntryPoint,
155 IN KERNEL_CALL_GATE CallGate
156 )
157{
159 &Args,
160 EntryPoint,
161 CallGate,
162 &Args,
163 EntryPoint
164 );
165}
UINTN(EFIAPI * KERNEL_CALL_GATE)(IN UINTN Arg1, IN UINTN Arg2)
BOOT_COMPAT_CONTEXT * GetBootCompatContext(VOID)
VOID AppleMapPrepareForHibernateWake(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN UINTN ImageHeaderPage)
VOID AppleRelocationCallGate64(IN OUT UINTN *Args, IN BOOT_COMPAT_CONTEXT *BootCompat, IN KERNEL_CALL_GATE CallGate, IN UINTN *KcgArg1, IN UINTN KcgArg2)
VOID AppleMapPrepareForBooting(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN OUT VOID *BootArgs)
#define ESTIMATED_CALL_GATE_SIZE
EFI_STATUS EFIAPI AppleMapPrepareKernelStateNew64(IN UINTN SystemTable, IN OUT APPLE_EFI_BOOT_RT_KCG_ARGS *KcgArguments, IN KERNEL_CALL_GATE CallGate)
STATIC UINTN EFIAPI AppleMapPrepareKernelStateWorker64(IN UINTN *Args, IN UINTN EntryPoint, IN KERNEL_CALL_GATE CallGate, IN UINTN *Arg1, IN UINTN Arg2)
UINTN EFIAPI AppleMapPrepareKernelStateOld64(IN UINTN Args, IN UINTN EntryPoint, IN KERNEL_CALL_GATE CallGate)
VOID AppleMapPrepareKernelJump64(IN OUT BOOT_COMPAT_CONTEXT *BootCompat, IN EFI_PHYSICAL_ADDRESS CallGate, IN UINTN HookAddress)
#define RUNTIME_DEBUG(x)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
SERVICES_OVERRIDE_STATE ServiceState
KERNEL_SUPPORT_STATE KernelState
struct CALL_GATE_JUMP_::@79 Jmp
struct CALL_GATE_JUMP_::@78 LeaRip
EFI_PHYSICAL_ADDRESS RelocationBlock