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