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