]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/EbcDxe/Ia32/EbcSupport.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / Ia32 / EbcSupport.c
CommitLineData
fb0b259e 1/** @file\r
53c71d09 2 This module contains EBC support routines that are customized based on\r
21d13c61 3 the target ia32 processor.\r
53c71d09 4\r
d1102dba 5Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
fb0b259e 7\r
8**/\r
53c71d09 9\r
10#include "EbcInt.h"\r
11#include "EbcExecute.h"\r
6f0a3cd2 12#include "EbcDebuggerHook.h"\r
53c71d09 13\r
14//\r
15// NOTE: This is the stack size allocated for the interpreter\r
16// when it executes an EBC image. The requirements can change\r
17// based on whether or not a debugger is present, and other\r
18// platform-specific configurations.\r
19//\r
1436aea4 20#define VM_STACK_SIZE (1024 * 4)\r
53c71d09 21\r
1436aea4 22#define STACK_REMAIN_SIZE (1024 * 4)\r
53c71d09 23\r
21d13c61
JY
24//\r
25// This is instruction buffer used to create EBC thunk\r
26//\r
1436aea4
MK
27#define EBC_ENTRYPOINT_SIGNATURE 0xAFAFAFAF\r
28#define EBC_LL_EBC_ENTRYPOINT_SIGNATURE 0xFAFAFAFA\r
21d13c61
JY
29UINT8 mInstructionBufferTemplate[] = {\r
30 //\r
31 // Add a magic code here to help the VM recognize the thunk..\r
32 // mov eax, 0xca112ebc => B8 BC 2E 11 CA\r
33 //\r
1436aea4 34 0xB8, 0xBC, 0x2E, 0x11, 0xCA,\r
21d13c61
JY
35 //\r
36 // Add code bytes to load up a processor register with the EBC entry point.\r
37 // mov eax, EbcEntryPoint => B8 XX XX XX XX (To be fixed at runtime)\r
38 // These 4 bytes of the thunk entry is the address of the EBC\r
39 // entry point.\r
40 //\r
d1102dba 41 0xB8,\r
1436aea4
MK
42 (UINT8)(EBC_ENTRYPOINT_SIGNATURE & 0xFF),\r
43 (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 8) & 0xFF),\r
44 (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 16) & 0xFF),\r
45 (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 24) & 0xFF),\r
21d13c61
JY
46 //\r
47 // Stick in a load of ecx with the address of appropriate VM function.\r
48 // mov ecx, EbcLLEbcInterpret => B9 XX XX XX XX (To be fixed at runtime)\r
49 //\r
50 0xB9,\r
1436aea4
MK
51 (UINT8)(EBC_LL_EBC_ENTRYPOINT_SIGNATURE & 0xFF),\r
52 (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 8) & 0xFF),\r
53 (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 16) & 0xFF),\r
54 (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 24) & 0xFF),\r
21d13c61
JY
55 //\r
56 // Stick in jump opcode bytes\r
57 // jmp ecx => FF E1\r
58 //\r
1436aea4 59 0xFF, 0xE1,\r
21d13c61
JY
60};\r
61\r
7102b199
JY
62/**\r
63 Begin executing an EBC image.\r
64 This is used for Ebc Thunk call.\r
65\r
66 @return The value returned by the EBC application we're going to run.\r
67\r
68**/\r
69UINT64\r
70EFIAPI\r
71EbcLLEbcInterpret (\r
72 VOID\r
73 );\r
74\r
75/**\r
76 Begin executing an EBC image.\r
77 This is used for Ebc image entrypoint.\r
78\r
79 @return The value returned by the EBC application we're going to run.\r
80\r
81**/\r
82UINT64\r
83EFIAPI\r
84EbcLLExecuteEbcImageEntryPoint (\r
85 VOID\r
86 );\r
53c71d09 87\r
fb0b259e 88/**\r
89 This function is called to execute an EBC CALLEX instruction.\r
53c71d09 90 The function check the callee's content to see whether it is common native\r
91 code or a thunk to another piece of EBC code.\r
92 If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
93 otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
94 be startup which cost time and stack space.\r
53c71d09 95\r
8e3bc754 96 @param VmPtr Pointer to a VM context.\r
97 @param FuncAddr Callee's address\r
98 @param NewStackPointer New stack pointer after the call\r
99 @param FramePtr New frame pointer after the call\r
100 @param Size The size of call instruction\r
53c71d09 101\r
fb0b259e 102**/\r
103VOID\r
104EbcLLCALLEX (\r
1436aea4
MK
105 IN VM_CONTEXT *VmPtr,\r
106 IN UINTN FuncAddr,\r
107 IN UINTN NewStackPointer,\r
108 IN VOID *FramePtr,\r
109 IN UINT8 Size\r
fb0b259e 110 )\r
53c71d09 111{\r
1436aea4
MK
112 UINTN IsThunk;\r
113 UINTN TargetEbcAddr;\r
114 UINT8 InstructionBuffer[sizeof (mInstructionBufferTemplate)];\r
115 UINTN Index;\r
116 UINTN IndexOfEbcEntrypoint;\r
117\r
118 IsThunk = 1;\r
119 TargetEbcAddr = 0;\r
21d13c61 120 IndexOfEbcEntrypoint = 0;\r
53c71d09 121\r
122 //\r
123 // Processor specific code to check whether the callee is a thunk to EBC.\r
124 //\r
1436aea4 125 CopyMem (InstructionBuffer, (VOID *)FuncAddr, sizeof (InstructionBuffer));\r
21d13c61
JY
126 //\r
127 // Fill the signature according to mInstructionBufferTemplate\r
128 //\r
1436aea4 129 for (Index = 0; Index < sizeof (mInstructionBufferTemplate) - sizeof (UINTN); Index++) {\r
21d13c61
JY
130 if (*(UINTN *)&mInstructionBufferTemplate[Index] == EBC_ENTRYPOINT_SIGNATURE) {\r
131 *(UINTN *)&InstructionBuffer[Index] = EBC_ENTRYPOINT_SIGNATURE;\r
1436aea4 132 IndexOfEbcEntrypoint = Index;\r
21d13c61 133 }\r
1436aea4 134\r
21d13c61
JY
135 if (*(UINTN *)&mInstructionBufferTemplate[Index] == EBC_LL_EBC_ENTRYPOINT_SIGNATURE) {\r
136 *(UINTN *)&InstructionBuffer[Index] = EBC_LL_EBC_ENTRYPOINT_SIGNATURE;\r
137 }\r
53c71d09 138 }\r
1436aea4 139\r
21d13c61
JY
140 //\r
141 // Check if we need thunk to native\r
142 //\r
1436aea4 143 if (CompareMem (InstructionBuffer, mInstructionBufferTemplate, sizeof (mInstructionBufferTemplate)) != 0) {\r
53c71d09 144 IsThunk = 0;\r
53c71d09 145 }\r
146\r
1436aea4 147 if (IsThunk == 1) {\r
53c71d09 148 //\r
149 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
150 // put our return address and frame pointer on the VM stack.\r
151 // Then set the VM's IP to new EBC code.\r
152 //\r
1ccdbf2a 153 VmPtr->Gpr[0] -= 8;\r
1436aea4
MK
154 VmWriteMemN (VmPtr, (UINTN)VmPtr->Gpr[0], (UINTN)FramePtr);\r
155 VmPtr->FramePtr = (VOID *)(UINTN)VmPtr->Gpr[0];\r
156 VmPtr->Gpr[0] -= 8;\r
157 VmWriteMem64 (VmPtr, (UINTN)VmPtr->Gpr[0], (UINT64)(UINTN)(VmPtr->Ip + Size));\r
53c71d09 158\r
1436aea4
MK
159 CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + IndexOfEbcEntrypoint, sizeof (UINTN));\r
160 VmPtr->Ip = (VMIP)(UINTN)TargetEbcAddr;\r
53c71d09 161 } else {\r
162 //\r
fa97cbf4 163 // The callee is not a thunk to EBC, call native code,\r
21d13c61 164 // and get return value.\r
53c71d09 165 //\r
fa97cbf4 166 VmPtr->Gpr[7] = EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
fb0b259e 167\r
53c71d09 168 //\r
fa97cbf4 169 // Advance the IP.\r
53c71d09 170 //\r
53c71d09 171 VmPtr->Ip += Size;\r
172 }\r
173}\r
174\r
fb0b259e 175/**\r
7102b199 176 Begin executing an EBC image.\r
fb0b259e 177\r
8e3bc754 178 This is a thunk function. Microsoft x64 compiler only provide fast_call\r
179 calling convention, so the first four arguments are passed by rcx, rdx,\r
180 r8, and r9, while other arguments are passed in stack.\r
181\r
7102b199 182 @param EntryPoint The entrypoint of EBC code.\r
8e3bc754 183 @param Arg1 The 1st argument.\r
184 @param Arg2 The 2nd argument.\r
185 @param Arg3 The 3rd argument.\r
186 @param Arg4 The 4th argument.\r
187 @param Arg5 The 5th argument.\r
188 @param Arg6 The 6th argument.\r
189 @param Arg7 The 7th argument.\r
190 @param Arg8 The 8th argument.\r
191 @param Arg9 The 9th argument.\r
192 @param Arg10 The 10th argument.\r
193 @param Arg11 The 11th argument.\r
194 @param Arg12 The 12th argument.\r
195 @param Arg13 The 13th argument.\r
196 @param Arg14 The 14th argument.\r
197 @param Arg15 The 15th argument.\r
198 @param Arg16 The 16th argument.\r
fb0b259e 199\r
200 @return The value returned by the EBC application we're going to run.\r
201\r
202**/\r
53c71d09 203UINT64\r
fa97cbf4 204EFIAPI\r
53c71d09 205EbcInterpret (\r
1436aea4
MK
206 IN UINTN EntryPoint,\r
207 IN UINTN Arg1,\r
208 IN UINTN Arg2,\r
209 IN UINTN Arg3,\r
210 IN UINTN Arg4,\r
211 IN UINTN Arg5,\r
212 IN UINTN Arg6,\r
213 IN UINTN Arg7,\r
214 IN UINTN Arg8,\r
215 IN UINTN Arg9,\r
216 IN UINTN Arg10,\r
217 IN UINTN Arg11,\r
218 IN UINTN Arg12,\r
219 IN UINTN Arg13,\r
220 IN UINTN Arg14,\r
221 IN UINTN Arg15,\r
222 IN UINTN Arg16\r
53c71d09 223 )\r
53c71d09 224{\r
225 //\r
226 // Create a new VM context on the stack\r
227 //\r
228 VM_CONTEXT VmContext;\r
229 UINTN Addr;\r
230 EFI_STATUS Status;\r
231 UINTN StackIndex;\r
232\r
233 //\r
7102b199 234 // Get the EBC entry point\r
53c71d09 235 //\r
7102b199 236 Addr = EntryPoint;\r
53c71d09 237\r
238 //\r
239 // Now clear out our context\r
240 //\r
1436aea4 241 ZeroMem ((VOID *)&VmContext, sizeof (VM_CONTEXT));\r
53c71d09 242\r
243 //\r
244 // Set the VM instruction pointer to the correct location in memory.\r
245 //\r
1436aea4 246 VmContext.Ip = (VMIP)Addr;\r
53c71d09 247 //\r
248 // Initialize the stack pointer for the EBC. Get the current system stack\r
249 // pointer and adjust it down by the max needed for the interpreter.\r
250 //\r
251\r
252 //\r
253 // Align the stack on a natural boundary\r
254 //\r
255\r
256 //\r
257 // Allocate stack pool\r
258 //\r
1436aea4
MK
259 Status = GetEBCStack ((EFI_HANDLE)-1, &VmContext.StackPool, &StackIndex);\r
260 if (EFI_ERROR (Status)) {\r
53c71d09 261 return Status;\r
262 }\r
1436aea4
MK
263\r
264 VmContext.StackTop = (UINT8 *)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
265 VmContext.Gpr[0] = (UINT64)(UINTN)((UINT8 *)VmContext.StackPool + STACK_POOL_SIZE);\r
1ccdbf2a 266 VmContext.HighStackBottom = (UINTN)VmContext.Gpr[0];\r
1436aea4
MK
267 VmContext.Gpr[0] &= ~((VM_REGISTER)(sizeof (UINTN) - 1));\r
268 VmContext.Gpr[0] -= sizeof (UINTN);\r
53c71d09 269\r
270 //\r
271 // Put a magic value in the stack gap, then adjust down again\r
272 //\r
1436aea4
MK
273 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)VM_STACK_KEY_VALUE;\r
274 VmContext.StackMagicPtr = (UINTN *)(UINTN)VmContext.Gpr[0];\r
275 VmContext.LowStackTop = (UINTN)VmContext.Gpr[0];\r
53c71d09 276\r
277 //\r
278 // For IA32, this is where we say our return address is\r
279 //\r
1436aea4
MK
280 VmContext.Gpr[0] -= sizeof (UINTN);\r
281 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg16;\r
282 VmContext.Gpr[0] -= sizeof (UINTN);\r
283 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg15;\r
284 VmContext.Gpr[0] -= sizeof (UINTN);\r
285 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg14;\r
286 VmContext.Gpr[0] -= sizeof (UINTN);\r
287 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg13;\r
288 VmContext.Gpr[0] -= sizeof (UINTN);\r
289 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg12;\r
290 VmContext.Gpr[0] -= sizeof (UINTN);\r
291 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg11;\r
292 VmContext.Gpr[0] -= sizeof (UINTN);\r
293 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg10;\r
294 VmContext.Gpr[0] -= sizeof (UINTN);\r
295 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg9;\r
296 VmContext.Gpr[0] -= sizeof (UINTN);\r
297 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg8;\r
298 VmContext.Gpr[0] -= sizeof (UINTN);\r
299 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg7;\r
300 VmContext.Gpr[0] -= sizeof (UINTN);\r
301 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg6;\r
302 VmContext.Gpr[0] -= sizeof (UINTN);\r
303 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg5;\r
304 VmContext.Gpr[0] -= sizeof (UINTN);\r
305 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg4;\r
306 VmContext.Gpr[0] -= sizeof (UINTN);\r
307 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg3;\r
308 VmContext.Gpr[0] -= sizeof (UINTN);\r
309 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg2;\r
310 VmContext.Gpr[0] -= sizeof (UINTN);\r
311 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)Arg1;\r
312 VmContext.Gpr[0] -= 16;\r
313 VmContext.StackRetAddr = (UINT64)VmContext.Gpr[0];\r
53c71d09 314\r
315 //\r
316 // We need to keep track of where the EBC stack starts. This way, if the EBC\r
317 // accesses any stack variables above its initial stack setting, then we know\r
318 // it's accessing variables passed into it, which means the data is on the\r
319 // VM's stack.\r
320 // When we're called, on the stack (high to low) we have the parameters, the\r
321 // return address, then the saved ebp. Save the pointer to the return address.\r
322 // EBC code knows that's there, so should look above it for function parameters.\r
323 // The offset is the size of locals (VMContext + Addr + saved ebp).\r
324 // Note that the interpreter assumes there is a 16 bytes of return address on\r
325 // the stack too, so adjust accordingly.\r
326 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
327 //\r
328\r
329 //\r
330 // Begin executing the EBC code\r
331 //\r
6f0a3cd2 332 EbcDebuggerHookEbcInterpret (&VmContext);\r
53c71d09 333 EbcExecute (&VmContext);\r
334\r
335 //\r
6f0a3cd2 336 // Return the value in Gpr[7] unless there was an error\r
53c71d09 337 //\r
1436aea4
MK
338 ReturnEBCStack (StackIndex);\r
339 return (UINT64)VmContext.Gpr[7];\r
53c71d09 340}\r
341\r
fb0b259e 342/**\r
7102b199 343 Begin executing an EBC image.\r
53c71d09 344\r
7102b199 345 @param EntryPoint The entrypoint of EBC code.\r
8e3bc754 346 @param ImageHandle image handle for the EBC application we're executing\r
347 @param SystemTable standard system table passed into an driver's entry\r
348 point\r
53c71d09 349\r
fb0b259e 350 @return The value returned by the EBC application we're going to run.\r
53c71d09 351\r
fb0b259e 352**/\r
fb0b259e 353UINT64\r
fa97cbf4 354EFIAPI\r
fb0b259e 355ExecuteEbcImageEntryPoint (\r
1436aea4
MK
356 IN UINTN EntryPoint,\r
357 IN EFI_HANDLE ImageHandle,\r
358 IN EFI_SYSTEM_TABLE *SystemTable\r
fb0b259e 359 )\r
53c71d09 360{\r
361 //\r
362 // Create a new VM context on the stack\r
363 //\r
364 VM_CONTEXT VmContext;\r
365 UINTN Addr;\r
366 EFI_STATUS Status;\r
367 UINTN StackIndex;\r
368\r
369 //\r
7102b199 370 // Get the EBC entry point\r
53c71d09 371 //\r
7102b199 372 Addr = EntryPoint;\r
53c71d09 373\r
53c71d09 374 //\r
375 // Now clear out our context\r
376 //\r
1436aea4 377 ZeroMem ((VOID *)&VmContext, sizeof (VM_CONTEXT));\r
53c71d09 378\r
379 //\r
380 // Save the image handle so we can track the thunks created for this image\r
381 //\r
382 VmContext.ImageHandle = ImageHandle;\r
383 VmContext.SystemTable = SystemTable;\r
384\r
385 //\r
386 // Set the VM instruction pointer to the correct location in memory.\r
387 //\r
1436aea4 388 VmContext.Ip = (VMIP)Addr;\r
53c71d09 389\r
390 //\r
391 // Initialize the stack pointer for the EBC. Get the current system stack\r
392 // pointer and adjust it down by the max needed for the interpreter.\r
393 //\r
394\r
395 //\r
396 // Allocate stack pool\r
397 //\r
1436aea4
MK
398 Status = GetEBCStack (ImageHandle, &VmContext.StackPool, &StackIndex);\r
399 if (EFI_ERROR (Status)) {\r
53c71d09 400 return Status;\r
401 }\r
1436aea4
MK
402\r
403 VmContext.StackTop = (UINT8 *)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
404 VmContext.Gpr[0] = (UINT64)(UINTN)((UINT8 *)VmContext.StackPool + STACK_POOL_SIZE);\r
1ccdbf2a 405 VmContext.HighStackBottom = (UINTN)VmContext.Gpr[0];\r
1436aea4 406 VmContext.Gpr[0] -= sizeof (UINTN);\r
fb0b259e 407\r
53c71d09 408 //\r
409 // Put a magic value in the stack gap, then adjust down again\r
410 //\r
1436aea4
MK
411 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)VM_STACK_KEY_VALUE;\r
412 VmContext.StackMagicPtr = (UINTN *)(UINTN)VmContext.Gpr[0];\r
53c71d09 413\r
414 //\r
415 // Align the stack on a natural boundary\r
1ccdbf2a 416 // VmContext.Gpr[0] &= ~(sizeof(UINTN) - 1);\r
53c71d09 417 //\r
1436aea4
MK
418 VmContext.LowStackTop = (UINTN)VmContext.Gpr[0];\r
419 VmContext.Gpr[0] -= sizeof (UINTN);\r
420 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)SystemTable;\r
421 VmContext.Gpr[0] -= sizeof (UINTN);\r
422 *(UINTN *)(UINTN)(VmContext.Gpr[0]) = (UINTN)ImageHandle;\r
53c71d09 423\r
1436aea4
MK
424 VmContext.Gpr[0] -= 16;\r
425 VmContext.StackRetAddr = (UINT64)VmContext.Gpr[0];\r
53c71d09 426 //\r
427 // VM pushes 16-bytes for return address. Simulate that here.\r
428 //\r
429\r
430 //\r
431 // Begin executing the EBC code\r
432 //\r
6f0a3cd2 433 EbcDebuggerHookExecuteEbcImageEntryPoint (&VmContext);\r
53c71d09 434 EbcExecute (&VmContext);\r
435\r
436 //\r
6f0a3cd2 437 // Return the value in Gpr[7] unless there was an error\r
53c71d09 438 //\r
1436aea4
MK
439 ReturnEBCStack (StackIndex);\r
440 return (UINT64)VmContext.Gpr[7];\r
53c71d09 441}\r
442\r
fb0b259e 443/**\r
8e3bc754 444 Create thunks for an EBC image entry point, or an EBC protocol service.\r
fb0b259e 445\r
8e3bc754 446 @param ImageHandle Image handle for the EBC image. If not null, then\r
447 we're creating a thunk for an image entry point.\r
448 @param EbcEntryPoint Address of the EBC code that the thunk is to call\r
449 @param Thunk Returned thunk we create here\r
450 @param Flags Flags indicating options for creating the thunk\r
fb0b259e 451\r
8e3bc754 452 @retval EFI_SUCCESS The thunk was created successfully.\r
453 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit\r
454 aligned.\r
455 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC\r
456 Thunk.\r
457 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.\r
fb0b259e 458\r
459**/\r
53c71d09 460EFI_STATUS\r
461EbcCreateThunks (\r
1436aea4
MK
462 IN EFI_HANDLE ImageHandle,\r
463 IN VOID *EbcEntryPoint,\r
464 OUT VOID **Thunk,\r
465 IN UINT32 Flags\r
53c71d09 466 )\r
53c71d09 467{\r
1436aea4
MK
468 UINT8 *Ptr;\r
469 UINT8 *ThunkBase;\r
470 UINT32 Index;\r
471 INT32 ThunkSize;\r
53c71d09 472\r
473 //\r
474 // Check alignment of pointer to EBC code\r
475 //\r
1436aea4 476 if ((UINT32)(UINTN)EbcEntryPoint & 0x01) {\r
53c71d09 477 return EFI_INVALID_PARAMETER;\r
478 }\r
479\r
1436aea4 480 ThunkSize = sizeof (mInstructionBufferTemplate);\r
53c71d09 481\r
1436aea4 482 Ptr = EbcAllocatePoolForThunk (sizeof (mInstructionBufferTemplate));\r
53c71d09 483\r
484 if (Ptr == NULL) {\r
485 return EFI_OUT_OF_RESOURCES;\r
486 }\r
1436aea4 487\r
53c71d09 488 //\r
489 // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
490 //\r
491 // Save the start address so we can add a pointer to it to a list later.\r
492 //\r
493 ThunkBase = Ptr;\r
494\r
495 //\r
496 // Give them the address of our buffer we're going to fix up\r
497 //\r
1436aea4 498 *Thunk = (VOID *)Ptr;\r
53c71d09 499\r
500 //\r
21d13c61 501 // Copy whole thunk instruction buffer template\r
53c71d09 502 //\r
1436aea4 503 CopyMem (Ptr, mInstructionBufferTemplate, sizeof (mInstructionBufferTemplate));\r
53c71d09 504\r
505 //\r
21d13c61 506 // Patch EbcEntryPoint and EbcLLEbcInterpret\r
53c71d09 507 //\r
1436aea4 508 for (Index = 0; Index < sizeof (mInstructionBufferTemplate) - sizeof (UINTN); Index++) {\r
21d13c61
JY
509 if (*(UINTN *)&Ptr[Index] == EBC_ENTRYPOINT_SIGNATURE) {\r
510 *(UINTN *)&Ptr[Index] = (UINTN)EbcEntryPoint;\r
511 }\r
1436aea4 512\r
21d13c61
JY
513 if (*(UINTN *)&Ptr[Index] == EBC_LL_EBC_ENTRYPOINT_SIGNATURE) {\r
514 if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r
515 *(UINTN *)&Ptr[Index] = (UINTN)EbcLLExecuteEbcImageEntryPoint;\r
516 } else {\r
517 *(UINTN *)&Ptr[Index] = (UINTN)EbcLLEbcInterpret;\r
518 }\r
519 }\r
53c71d09 520 }\r
521\r
53c71d09 522 //\r
523 // Add the thunk to the list for this image. Do this last since the add\r
524 // function flushes the cache for us.\r
525 //\r
1436aea4 526 EbcAddImageThunk (ImageHandle, (VOID *)ThunkBase, ThunkSize);\r
53c71d09 527\r
528 return EFI_SUCCESS;\r
529}\r