2 This module contains EBC support routines that are customized based on
5 Copyright (c) 2006 - 2010, Intel Corporation. <BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "EbcExecute.h"
18 #include "EbcSupport.h"
21 Given raw bytes of Itanium based code, format them into a bundle and
24 @param MemPtr pointer to memory location to write the bundles
26 @param Template 5-bit template.
27 @param Slot0 Instruction slot 0 data for the bundle.
28 @param Slot1 Instruction slot 1 data for the bundle.
29 @param Slot2 Instruction slot 2 data for the bundle.
31 @retval EFI_INVALID_PARAMETER Pointer is not aligned
32 @retval EFI_INVALID_PARAMETER No more than 5 bits in template
33 @retval EFI_INVALID_PARAMETER More than 41 bits used in code
34 @retval EFI_SUCCESS All data is written.
47 Pushes a 64 bit unsigned value to the VM stack.
49 @param VmPtr The pointer to current VM context.
50 @param Arg The value to be pushed.
60 // Advance the VM stack down, and then copy the argument to the stack.
63 VmPtr
->Gpr
[0] -= sizeof (UINT64
);
64 *(UINT64
*) VmPtr
->Gpr
[0] = Arg
;
68 Begin executing an EBC image. The address of the entry point is passed
69 in via a processor register, so we'll need to make a call to get the
72 This is a thunk function. Microsoft x64 compiler only provide fast_call
73 calling convention, so the first four arguments are passed by rcx, rdx,
74 r8, and r9, while other arguments are passed in stack.
76 @param Arg1 The 1st argument.
77 @param ... The variable arguments list.
79 @return The value returned by the EBC application we're going to run.
89 // Create a new VM context on the stack
112 // Get the EBC entry point from the processor register. Make sure you don't
113 // call any functions before this or you could mess up the register the
114 // entry point is passed in.
116 Addr
= EbcLLGetEbcEntryPoint ();
118 // Need the args off the stack.
120 VA_START (List
, Arg1
);
121 Arg2
= VA_ARG (List
, UINT64
);
122 Arg3
= VA_ARG (List
, UINT64
);
123 Arg4
= VA_ARG (List
, UINT64
);
124 Arg5
= VA_ARG (List
, UINT64
);
125 Arg6
= VA_ARG (List
, UINT64
);
126 Arg7
= VA_ARG (List
, UINT64
);
127 Arg8
= VA_ARG (List
, UINT64
);
128 Arg9
= VA_ARG (List
, UINT64
);
129 Arg10
= VA_ARG (List
, UINT64
);
130 Arg11
= VA_ARG (List
, UINT64
);
131 Arg12
= VA_ARG (List
, UINT64
);
132 Arg13
= VA_ARG (List
, UINT64
);
133 Arg14
= VA_ARG (List
, UINT64
);
134 Arg15
= VA_ARG (List
, UINT64
);
135 Arg16
= VA_ARG (List
, UINT64
);
137 // Now clear out our context
139 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
141 // Set the VM instruction pointer to the correct location in memory.
143 VmContext
.Ip
= (VMIP
) Addr
;
145 // Initialize the stack pointer for the EBC. Get the current system stack
146 // pointer and adjust it down by the max needed for the interpreter.
149 // NOTE: Eventually we should have the interpreter allocate memory
150 // for stack space which it will use during its execution. This
151 // would likely improve performance because the interpreter would
152 // no longer be required to test each memory access and adjust
153 // those reading from the stack gap.
155 // For IPF, the stack looks like (assuming 10 args passed)
157 // arg9 (Bottom of high stack)
158 // [ stack gap for interpreter execution ]
159 // [ magic value for detection of stack corruption ]
160 // arg8 (Top of low stack)
163 // [ 64-bit return address ]
165 // If the EBC accesses memory in the stack gap, then we assume that it's
166 // actually trying to access args9 and greater. Therefore we need to
167 // adjust memory accesses in this region to point above the stack gap.
170 // Now adjust the EBC stack pointer down to leave a gap for interpreter
171 // execution. Then stuff a magic value there.
174 Status
= GetEBCStack((EFI_HANDLE
)(UINTN
)-1, &VmContext
.StackPool
, &StackIndex
);
175 if (EFI_ERROR(Status
)) {
178 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
179 VmContext
.Gpr
[0] = (UINT64
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
180 VmContext
.HighStackBottom
= (UINTN
) VmContext
.Gpr
[0];
181 VmContext
.Gpr
[0] -= sizeof (UINTN
);
184 PushU64 (&VmContext
, (UINT64
) VM_STACK_KEY_VALUE
);
185 VmContext
.StackMagicPtr
= (UINTN
*) VmContext
.Gpr
[0];
186 VmContext
.LowStackTop
= (UINTN
) VmContext
.Gpr
[0];
188 // Push the EBC arguments on the stack. Does not matter that they may not
191 PushU64 (&VmContext
, Arg16
);
192 PushU64 (&VmContext
, Arg15
);
193 PushU64 (&VmContext
, Arg14
);
194 PushU64 (&VmContext
, Arg13
);
195 PushU64 (&VmContext
, Arg12
);
196 PushU64 (&VmContext
, Arg11
);
197 PushU64 (&VmContext
, Arg10
);
198 PushU64 (&VmContext
, Arg9
);
199 PushU64 (&VmContext
, Arg8
);
200 PushU64 (&VmContext
, Arg7
);
201 PushU64 (&VmContext
, Arg6
);
202 PushU64 (&VmContext
, Arg5
);
203 PushU64 (&VmContext
, Arg4
);
204 PushU64 (&VmContext
, Arg3
);
205 PushU64 (&VmContext
, Arg2
);
206 PushU64 (&VmContext
, Arg1
);
208 // Push a bogus return address on the EBC stack because the
209 // interpreter expects one there. For stack alignment purposes on IPF,
210 // EBC return addresses are always 16 bytes. Push a bogus value as well.
212 PushU64 (&VmContext
, 0);
213 PushU64 (&VmContext
, 0xDEADBEEFDEADBEEF);
214 VmContext
.StackRetAddr
= (UINT64
) VmContext
.Gpr
[0];
216 // Begin executing the EBC code
218 EbcExecute (&VmContext
);
220 // Return the value in R[7] unless there was an error
222 ReturnEBCStack(StackIndex
);
223 return (UINT64
) VmContext
.Gpr
[7];
228 Begin executing an EBC image. The address of the entry point is passed
229 in via a processor register, so we'll need to make a call to get the
232 @param ImageHandle image handle for the EBC application we're executing
233 @param SystemTable standard system table passed into an driver's entry
236 @return The value returned by the EBC application we're going to run.
240 ExecuteEbcImageEntryPoint (
241 IN EFI_HANDLE ImageHandle
,
242 IN EFI_SYSTEM_TABLE
*SystemTable
246 // Create a new VM context on the stack
248 VM_CONTEXT VmContext
;
254 // Get the EBC entry point from the processor register. Make sure you don't
255 // call any functions before this or you could mess up the register the
256 // entry point is passed in.
258 Addr
= EbcLLGetEbcEntryPoint ();
261 // Now clear out our context
263 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
266 // Save the image handle so we can track the thunks created for this image
268 VmContext
.ImageHandle
= ImageHandle
;
269 VmContext
.SystemTable
= SystemTable
;
272 // Set the VM instruction pointer to the correct location in memory.
274 VmContext
.Ip
= (VMIP
) Addr
;
277 // Get the stack pointer. This is the bottom of the upper stack.
279 Addr
= EbcLLGetStackPointer ();
281 Status
= GetEBCStack(ImageHandle
, &VmContext
.StackPool
, &StackIndex
);
282 if (EFI_ERROR(Status
)) {
285 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
286 VmContext
.Gpr
[0] = (UINT64
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
287 VmContext
.HighStackBottom
= (UINTN
) VmContext
.Gpr
[0];
288 VmContext
.Gpr
[0] -= sizeof (UINTN
);
292 // Allocate stack space for the interpreter. Then put a magic value
293 // at the bottom so we can detect stack corruption.
295 PushU64 (&VmContext
, (UINT64
) VM_STACK_KEY_VALUE
);
296 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.Gpr
[0];
299 // When we thunk to external native code, we copy the last 8 qwords from
300 // the EBC stack into the processor registers, and adjust the stack pointer
301 // up. If the caller is not passing 8 parameters, then we've moved the
302 // stack pointer up into the stack gap. If this happens, then the caller
303 // can mess up the stack gap contents (in particular our magic value).
304 // Therefore, leave another gap below the magic value. Pick 10 qwords down,
305 // just as a starting point.
307 VmContext
.Gpr
[0] -= 10 * sizeof (UINT64
);
310 // Align the stack pointer such that after pushing the system table,
311 // image handle, and return address on the stack, it's aligned on a 16-byte
312 // boundary as required for IPF.
314 VmContext
.Gpr
[0] &= (INT64
)~0x0f;
315 VmContext
.LowStackTop
= (UINTN
) VmContext
.Gpr
[0];
317 // Simply copy the image handle and system table onto the EBC stack.
318 // Greatly simplifies things by not having to spill the args
320 PushU64 (&VmContext
, (UINT64
) SystemTable
);
321 PushU64 (&VmContext
, (UINT64
) ImageHandle
);
324 // Interpreter assumes 64-bit return address is pushed on the stack.
325 // IPF does not do this so pad the stack accordingly. Also, a
326 // "return address" is 16 bytes as required for IPF stack alignments.
328 PushU64 (&VmContext
, (UINT64
) 0);
329 PushU64 (&VmContext
, (UINT64
) 0x1234567887654321);
330 VmContext
.StackRetAddr
= (UINT64
) VmContext
.Gpr
[0];
333 // Begin executing the EBC code
335 EbcExecute (&VmContext
);
338 // Return the value in R[7] unless there was an error
340 ReturnEBCStack(StackIndex
);
341 return (UINT64
) VmContext
.Gpr
[7];
346 Create thunks for an EBC image entry point, or an EBC protocol service.
348 @param ImageHandle Image handle for the EBC image. If not null, then
349 we're creating a thunk for an image entry point.
350 @param EbcEntryPoint Address of the EBC code that the thunk is to call
351 @param Thunk Returned thunk we create here
352 @param Flags Flags indicating options for creating the thunk
354 @retval EFI_SUCCESS The thunk was created successfully.
355 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
357 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
359 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
364 IN EFI_HANDLE ImageHandle
,
365 IN VOID
*EbcEntryPoint
,
373 UINT64 Code
[3]; // Code in a bundle
374 UINT64 RegNum
; // register number for MOVL
375 UINT64 BitI
; // bits of MOVL immediate data
376 UINT64 BitIc
; // bits of MOVL immediate data
377 UINT64 BitImm5c
; // bits of MOVL immediate data
378 UINT64 BitImm9d
; // bits of MOVL immediate data
379 UINT64 BitImm7b
; // bits of MOVL immediate data
380 UINT64 Br
; // branch register for loading and jumping
386 // Check alignment of pointer to EBC code, which must always be aligned
387 // on a 2-byte boundary.
389 if ((UINT32
) (UINTN
) EbcEntryPoint
& 0x01) {
390 return EFI_INVALID_PARAMETER
;
393 // Allocate memory for the thunk. Make the (most likely incorrect) assumption
394 // that the returned buffer is not aligned, so round up to the next
397 Size
= EBC_THUNK_SIZE
+ EBC_THUNK_ALIGNMENT
- 1;
399 Ptr
= AllocatePool (Size
);
402 return EFI_OUT_OF_RESOURCES
;
405 // Save the start address of the buffer.
410 // Make sure it's aligned for code execution. If not, then
413 if ((UINT32
) (UINTN
) Ptr
& (EBC_THUNK_ALIGNMENT
- 1)) {
414 Ptr
= (UINT8
*) (((UINTN
) Ptr
+ (EBC_THUNK_ALIGNMENT
- 1)) &~ (UINT64
) (EBC_THUNK_ALIGNMENT
- 1));
417 // Return the pointer to the thunk to the caller to user as the
418 // image entry point.
420 *Thunk
= (VOID
*) Ptr
;
423 // Clear out the thunk entry
424 // ZeroMem(Ptr, Size);
426 // For IPF, when you do a call via a function pointer, the function pointer
427 // actually points to a function descriptor which consists of a 64-bit
428 // address of the function, followed by a 64-bit gp for the function being
429 // called. See the the Software Conventions and Runtime Architecture Guide
431 // So first off in our thunk, create a descriptor for our actual thunk code.
432 // This means we need to create a pointer to the thunk code (which follows
433 // the descriptor we're going to create), followed by the gp of the Vm
434 // interpret function we're going to eventually execute.
436 Data64Ptr
= (UINT64
*) Ptr
;
439 // Write the function's entry point (which is our thunk code that follows
440 // this descriptor we're creating).
442 *Data64Ptr
= (UINT64
) (Data64Ptr
+ 2);
444 // Get the gp from the descriptor for EbcInterpret and stuff it in our thunk
447 *(Data64Ptr
+ 1) = *(UINT64
*) ((UINT64
*) (UINTN
) EbcInterpret
+ 1);
449 // Advance our thunk data pointer past the descriptor. Since the
450 // descriptor consists of 16 bytes, the pointer is still aligned for
451 // IPF code execution (on 16-byte boundary).
453 Ptr
+= sizeof (UINT64
) * 2;
456 // *************************** MAGIC BUNDLE ********************************
458 // Write magic code bundle for: movl r8 = 0xca112ebcca112ebc to help the VM
459 // to recognize it is a thunk.
461 Addr
= (UINT64
) 0xCA112EBCCA112EBC;
464 // Now generate the code bytes. First is nop.m 0x0
466 Code
[0] = OPCODE_NOP
;
469 // Next is simply Addr[62:22] (41 bits) of the address
471 Code
[1] = RShiftU64 (Addr
, 22) & 0x1ffffffffff;
474 // Extract bits from the address for insertion into the instruction
477 BitI
= RShiftU64 (Addr
, 63) & 0x01;
481 BitIc
= RShiftU64 (Addr
, 21) & 0x01;
483 // imm5c = Addr[20:16] for 5 bits
485 BitImm5c
= RShiftU64 (Addr
, 16) & 0x1F;
487 // imm9d = Addr[15:7] for 9 bits
489 BitImm9d
= RShiftU64 (Addr
, 7) & 0x1FF;
491 // imm7b = Addr[6:0] for 7 bits
493 BitImm7b
= Addr
& 0x7F;
496 // The EBC entry point will be put into r8, so r8 can be used here
497 // temporary. R8 is general register and is auto-serialized.
502 // Next is jumbled data, including opcode and rest of address
504 Code
[2] = LShiftU64 (BitImm7b
, 13);
505 Code
[2] = Code
[2] | LShiftU64 (0x00, 20); // vc
506 Code
[2] = Code
[2] | LShiftU64 (BitIc
, 21);
507 Code
[2] = Code
[2] | LShiftU64 (BitImm5c
, 22);
508 Code
[2] = Code
[2] | LShiftU64 (BitImm9d
, 27);
509 Code
[2] = Code
[2] | LShiftU64 (BitI
, 36);
510 Code
[2] = Code
[2] | LShiftU64 ((UINT64
)MOVL_OPCODE
, 37);
511 Code
[2] = Code
[2] | LShiftU64 ((RegNum
& 0x7F), 6);
513 WriteBundle ((VOID
*) Ptr
, 0x05, Code
[0], Code
[1], Code
[2]);
516 // *************************** FIRST BUNDLE ********************************
518 // Write code bundle for: movl r8 = EBC_ENTRY_POINT so we pass
519 // the ebc entry point in to the interpreter function via a processor
521 // Note -- we could easily change this to pass in a pointer to a structure
522 // that contained, among other things, the EBC image's entry point. But
523 // for now pass it directly.
526 Addr
= (UINT64
) EbcEntryPoint
;
529 // Now generate the code bytes. First is nop.m 0x0
531 Code
[0] = OPCODE_NOP
;
534 // Next is simply Addr[62:22] (41 bits) of the address
536 Code
[1] = RShiftU64 (Addr
, 22) & 0x1ffffffffff;
539 // Extract bits from the address for insertion into the instruction
542 BitI
= RShiftU64 (Addr
, 63) & 0x01;
546 BitIc
= RShiftU64 (Addr
, 21) & 0x01;
548 // imm5c = Addr[20:16] for 5 bits
550 BitImm5c
= RShiftU64 (Addr
, 16) & 0x1F;
552 // imm9d = Addr[15:7] for 9 bits
554 BitImm9d
= RShiftU64 (Addr
, 7) & 0x1FF;
556 // imm7b = Addr[6:0] for 7 bits
558 BitImm7b
= Addr
& 0x7F;
561 // Put the EBC entry point in r8, which is the location of the return value
567 // Next is jumbled data, including opcode and rest of address
569 Code
[2] = LShiftU64 (BitImm7b
, 13);
570 Code
[2] = Code
[2] | LShiftU64 (0x00, 20); // vc
571 Code
[2] = Code
[2] | LShiftU64 (BitIc
, 21);
572 Code
[2] = Code
[2] | LShiftU64 (BitImm5c
, 22);
573 Code
[2] = Code
[2] | LShiftU64 (BitImm9d
, 27);
574 Code
[2] = Code
[2] | LShiftU64 (BitI
, 36);
575 Code
[2] = Code
[2] | LShiftU64 ((UINT64
)MOVL_OPCODE
, 37);
576 Code
[2] = Code
[2] | LShiftU64 ((RegNum
& 0x7F), 6);
578 WriteBundle ((VOID
*) Ptr
, 0x05, Code
[0], Code
[1], Code
[2]);
581 // *************************** NEXT BUNDLE *********************************
583 // Write code bundle for:
584 // movl rx = offset_of(EbcInterpret|ExecuteEbcImageEntryPoint)
586 // Advance pointer to next bundle, then compute the offset from this bundle
587 // to the address of the entry point of the interpreter.
590 if ((Flags
& FLAG_THUNK_ENTRY_POINT
) != 0) {
591 Addr
= (UINT64
) ExecuteEbcImageEntryPoint
;
593 Addr
= (UINT64
) EbcInterpret
;
596 // Indirection on Itanium-based systems
598 Addr
= *(UINT64
*) Addr
;
601 // Now write the code to load the offset into a register
603 Code
[0] = OPCODE_NOP
;
606 // Next is simply Addr[62:22] (41 bits) of the address
608 Code
[1] = RShiftU64 (Addr
, 22) & 0x1ffffffffff;
611 // Extract bits from the address for insertion into the instruction
614 BitI
= RShiftU64 (Addr
, 63) & 0x01;
618 BitIc
= RShiftU64 (Addr
, 21) & 0x01;
620 // imm5c = Addr[20:16] for 5 bits
622 BitImm5c
= RShiftU64 (Addr
, 16) & 0x1F;
624 // imm9d = Addr[15:7] for 9 bits
626 BitImm9d
= RShiftU64 (Addr
, 7) & 0x1FF;
628 // imm7b = Addr[6:0] for 7 bits
630 BitImm7b
= Addr
& 0x7F;
633 // Put it in r31, a scratch register
638 // Next is jumbled data, including opcode and rest of address
640 Code
[2] = LShiftU64(BitImm7b
, 13);
641 Code
[2] = Code
[2] | LShiftU64 (0x00, 20); // vc
642 Code
[2] = Code
[2] | LShiftU64 (BitIc
, 21);
643 Code
[2] = Code
[2] | LShiftU64 (BitImm5c
, 22);
644 Code
[2] = Code
[2] | LShiftU64 (BitImm9d
, 27);
645 Code
[2] = Code
[2] | LShiftU64 (BitI
, 36);
646 Code
[2] = Code
[2] | LShiftU64 ((UINT64
)MOVL_OPCODE
, 37);
647 Code
[2] = Code
[2] | LShiftU64 ((RegNum
& 0x7F), 6);
649 WriteBundle ((VOID
*) Ptr
, 0x05, Code
[0], Code
[1], Code
[2]);
652 // *************************** NEXT BUNDLE *********************************
654 // Load branch register with EbcInterpret() function offset from the bundle
655 // address: mov b6 = RegNum
657 // See volume 3 page 4-29 of the Arch. Software Developer's Manual.
659 // Advance pointer to next bundle
662 Code
[0] = OPCODE_NOP
;
663 Code
[1] = OPCODE_NOP
;
664 Code
[2] = OPCODE_MOV_BX_RX
;
667 // Pick a branch register to use. Then fill in the bits for the branch
668 // register and user register (same user register as previous bundle).
671 Code
[2] |= LShiftU64 (Br
, 6);
672 Code
[2] |= LShiftU64 (RegNum
, 13);
673 WriteBundle ((VOID
*) Ptr
, 0x0d, Code
[0], Code
[1], Code
[2]);
676 // *************************** NEXT BUNDLE *********************************
678 // Now do the branch: (p0) br.cond.sptk.few b6
680 // Advance pointer to next bundle.
681 // Fill in the bits for the branch register (same reg as previous bundle)
684 Code
[0] = OPCODE_NOP
;
685 Code
[1] = OPCODE_NOP
;
686 Code
[2] = OPCODE_BR_COND_SPTK_FEW
;
687 Code
[2] |= LShiftU64 (Br
, 13);
688 WriteBundle ((VOID
*) Ptr
, 0x1d, Code
[0], Code
[1], Code
[2]);
691 // Add the thunk to our list of allocated thunks so we can do some cleanup
692 // when the image is unloaded. Do this last since the Add function flushes
693 // the instruction cache for us.
695 EbcAddImageThunk (ImageHandle
, (VOID
*) ThunkBase
, ThunkSize
);
705 Given raw bytes of Itanium based code, format them into a bundle and
708 @param MemPtr pointer to memory location to write the bundles
710 @param Template 5-bit template.
711 @param Slot0 Instruction slot 0 data for the bundle.
712 @param Slot1 Instruction slot 1 data for the bundle.
713 @param Slot2 Instruction slot 2 data for the bundle.
715 @retval EFI_INVALID_PARAMETER Pointer is not aligned
716 @retval EFI_INVALID_PARAMETER No more than 5 bits in template
717 @retval EFI_INVALID_PARAMETER More than 41 bits used in code
718 @retval EFI_SUCCESS All data is written.
736 // Verify pointer is aligned
738 if ((UINT64
) MemPtr
& 0xF) {
739 return EFI_INVALID_PARAMETER
;
742 // Verify no more than 5 bits in template
744 if ((Template
&~0x1F) != 0) {
745 return EFI_INVALID_PARAMETER
;
748 // Verify max of 41 bits used in code
750 if (((Slot0
| Slot1
| Slot2
) &~0x1ffffffffff) != 0) {
751 return EFI_INVALID_PARAMETER
;
754 Low64
= LShiftU64 (Slot1
, 46);
755 Low64
= Low64
| LShiftU64 (Slot0
, 5) | Template
;
757 High64
= RShiftU64 (Slot1
, 18);
758 High64
= High64
| LShiftU64 (Slot2
, 23);
761 // Now write it all out
763 BPtr
= (UINT8
*) MemPtr
;
764 for (Index
= 0; Index
< 8; Index
++) {
765 *BPtr
= (UINT8
) Low64
;
766 Low64
= RShiftU64 (Low64
, 8);
770 for (Index
= 0; Index
< 8; Index
++) {
771 *BPtr
= (UINT8
) High64
;
772 High64
= RShiftU64 (High64
, 8);
781 This function is called to execute an EBC CALLEX instruction.
782 The function check the callee's content to see whether it is common native
783 code or a thunk to another piece of EBC code.
784 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
785 otherwise, set the VM->IP to target EBC code directly to avoid another VM
786 be startup which cost time and stack space.
788 @param VmPtr Pointer to a VM context.
789 @param FuncAddr Callee's address
790 @param NewStackPointer New stack pointer after the call
791 @param FramePtr New frame pointer after the call
792 @param Size The size of call instruction
797 IN VM_CONTEXT
*VmPtr
,
799 IN UINTN NewStackPointer
,
819 // FuncAddr points to the descriptor of the target instructions.
821 CalleeAddr
= *((UINT64
*)FuncAddr
);
824 // Processor specific code to check whether the callee is a thunk to EBC.
826 if (*((UINT64
*)CalleeAddr
) != 0xBCCA000100000005) {
830 if (*((UINT64
*)CalleeAddr
+ 1) != 0x697623C1004A112E) {
835 CodeOne18
= RShiftU64 (*((UINT64
*)CalleeAddr
+ 2), 46) & 0x3FFFF;
836 CodeOne23
= (*((UINT64
*)CalleeAddr
+ 3)) & 0x7FFFFF;
837 CodeTwoI
= RShiftU64 (*((UINT64
*)CalleeAddr
+ 3), 59) & 0x1;
838 CodeTwoIc
= RShiftU64 (*((UINT64
*)CalleeAddr
+ 3), 44) & 0x1;
839 CodeTwo7b
= RShiftU64 (*((UINT64
*)CalleeAddr
+ 3), 36) & 0x7F;
840 CodeTwo5c
= RShiftU64 (*((UINT64
*)CalleeAddr
+ 3), 45) & 0x1F;
841 CodeTwo9d
= RShiftU64 (*((UINT64
*)CalleeAddr
+ 3), 50) & 0x1FF;
843 TargetEbcAddr
= CodeTwo7b
;
844 TargetEbcAddr
= TargetEbcAddr
| LShiftU64 (CodeTwo9d
, 7);
845 TargetEbcAddr
= TargetEbcAddr
| LShiftU64 (CodeTwo5c
, 16);
846 TargetEbcAddr
= TargetEbcAddr
| LShiftU64 (CodeTwoIc
, 21);
847 TargetEbcAddr
= TargetEbcAddr
| LShiftU64 (CodeOne18
, 22);
848 TargetEbcAddr
= TargetEbcAddr
| LShiftU64 (CodeOne23
, 40);
849 TargetEbcAddr
= TargetEbcAddr
| LShiftU64 (CodeTwoI
, 63);
854 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
855 // put our return address and frame pointer on the VM stack.
856 // Then set the VM's IP to new EBC code.
859 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINTN
) FramePtr
);
860 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->Gpr
[0];
862 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINT64
) (VmPtr
->Ip
+ Size
));
864 VmPtr
->Ip
= (VMIP
) (UINTN
) TargetEbcAddr
;
867 // The callee is not a thunk to EBC, call native code.
869 EbcLLCALLEXNative (FuncAddr
, NewStackPointer
, FramePtr
);
872 // Get return value and advance the IP.
874 VmPtr
->Gpr
[7] = EbcLLGetReturnValue ();