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