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