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