2 This module contains EBC support routines that are customized based on
3 the target ia32 processor.
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 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 "EbcDebuggerHook.h"
21 // NOTE: This is the stack size allocated for the interpreter
22 // when it executes an EBC image. The requirements can change
23 // based on whether or not a debugger is present, and other
24 // platform-specific configurations.
26 #define VM_STACK_SIZE (1024 * 4)
28 #define STACK_REMAIN_SIZE (1024 * 4)
31 // This is instruction buffer used to create EBC thunk
33 #define EBC_ENTRYPOINT_SIGNATURE 0xAFAFAFAF
34 #define EBC_LL_EBC_ENTRYPOINT_SIGNATURE 0xFAFAFAFA
35 UINT8 mInstructionBufferTemplate
[] = {
37 // Add a magic code here to help the VM recognize the thunk..
38 // mov eax, 0xca112ebc => B8 BC 2E 11 CA
40 0xB8, 0xBC, 0x2E, 0x11, 0xCA,
42 // Add code bytes to load up a processor register with the EBC entry point.
43 // mov eax, EbcEntryPoint => B8 XX XX XX XX (To be fixed at runtime)
44 // These 4 bytes of the thunk entry is the address of the EBC
48 (UINT8
)(EBC_ENTRYPOINT_SIGNATURE
& 0xFF),
49 (UINT8
)((EBC_ENTRYPOINT_SIGNATURE
>> 8) & 0xFF),
50 (UINT8
)((EBC_ENTRYPOINT_SIGNATURE
>> 16) & 0xFF),
51 (UINT8
)((EBC_ENTRYPOINT_SIGNATURE
>> 24) & 0xFF),
53 // Stick in a load of ecx with the address of appropriate VM function.
54 // mov ecx, EbcLLEbcInterpret => B9 XX XX XX XX (To be fixed at runtime)
57 (UINT8
)(EBC_LL_EBC_ENTRYPOINT_SIGNATURE
& 0xFF),
58 (UINT8
)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE
>> 8) & 0xFF),
59 (UINT8
)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE
>> 16) & 0xFF),
60 (UINT8
)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE
>> 24) & 0xFF),
62 // Stick in jump opcode bytes
69 Begin executing an EBC image.
70 This is used for Ebc Thunk call.
72 @return The value returned by the EBC application we're going to run.
82 Begin executing an EBC image.
83 This is used for Ebc image entrypoint.
85 @return The value returned by the EBC application we're going to run.
90 EbcLLExecuteEbcImageEntryPoint (
95 This function is called to execute an EBC CALLEX instruction.
96 The function check the callee's content to see whether it is common native
97 code or a thunk to another piece of EBC code.
98 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
99 otherwise, set the VM->IP to target EBC code directly to avoid another VM
100 be startup which cost time and stack space.
102 @param VmPtr Pointer to a VM context.
103 @param FuncAddr Callee's address
104 @param NewStackPointer New stack pointer after the call
105 @param FramePtr New frame pointer after the call
106 @param Size The size of call instruction
111 IN VM_CONTEXT
*VmPtr
,
113 IN UINTN NewStackPointer
,
120 UINT8 InstructionBuffer
[sizeof(mInstructionBufferTemplate
)];
122 UINTN IndexOfEbcEntrypoint
;
126 IndexOfEbcEntrypoint
= 0;
129 // Processor specific code to check whether the callee is a thunk to EBC.
131 CopyMem (InstructionBuffer
, (VOID
*)FuncAddr
, sizeof(InstructionBuffer
));
133 // Fill the signature according to mInstructionBufferTemplate
135 for (Index
= 0; Index
< sizeof(mInstructionBufferTemplate
) - sizeof(UINTN
); Index
++) {
136 if (*(UINTN
*)&mInstructionBufferTemplate
[Index
] == EBC_ENTRYPOINT_SIGNATURE
) {
137 *(UINTN
*)&InstructionBuffer
[Index
] = EBC_ENTRYPOINT_SIGNATURE
;
138 IndexOfEbcEntrypoint
= Index
;
140 if (*(UINTN
*)&mInstructionBufferTemplate
[Index
] == EBC_LL_EBC_ENTRYPOINT_SIGNATURE
) {
141 *(UINTN
*)&InstructionBuffer
[Index
] = EBC_LL_EBC_ENTRYPOINT_SIGNATURE
;
145 // Check if we need thunk to native
147 if (CompareMem (InstructionBuffer
, mInstructionBufferTemplate
, sizeof(mInstructionBufferTemplate
)) != 0) {
153 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
154 // put our return address and frame pointer on the VM stack.
155 // Then set the VM's IP to new EBC code.
158 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINTN
) FramePtr
);
159 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->Gpr
[0];
161 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINT64
) (UINTN
) (VmPtr
->Ip
+ Size
));
163 CopyMem (&TargetEbcAddr
, (UINT8
*)FuncAddr
+ IndexOfEbcEntrypoint
, sizeof(UINTN
));
164 VmPtr
->Ip
= (VMIP
) (UINTN
) TargetEbcAddr
;
167 // The callee is not a thunk to EBC, call native code,
168 // and get return value.
170 VmPtr
->Gpr
[7] = EbcLLCALLEXNative (FuncAddr
, NewStackPointer
, FramePtr
);
181 Begin executing an EBC image.
183 This is a thunk function. Microsoft x64 compiler only provide fast_call
184 calling convention, so the first four arguments are passed by rcx, rdx,
185 r8, and r9, while other arguments are passed in stack.
187 @param EntryPoint The entrypoint of EBC code.
188 @param Arg1 The 1st argument.
189 @param Arg2 The 2nd argument.
190 @param Arg3 The 3rd argument.
191 @param Arg4 The 4th argument.
192 @param Arg5 The 5th argument.
193 @param Arg6 The 6th argument.
194 @param Arg7 The 7th argument.
195 @param Arg8 The 8th argument.
196 @param Arg9 The 9th argument.
197 @param Arg10 The 10th argument.
198 @param Arg11 The 11th argument.
199 @param Arg12 The 12th argument.
200 @param Arg13 The 13th argument.
201 @param Arg14 The 14th argument.
202 @param Arg15 The 15th argument.
203 @param Arg16 The 16th argument.
205 @return The value returned by the EBC application we're going to run.
231 // Create a new VM context on the stack
233 VM_CONTEXT VmContext
;
239 // Get the EBC entry point
244 // Now clear out our context
246 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
249 // Set the VM instruction pointer to the correct location in memory.
251 VmContext
.Ip
= (VMIP
) Addr
;
253 // Initialize the stack pointer for the EBC. Get the current system stack
254 // pointer and adjust it down by the max needed for the interpreter.
258 // Align the stack on a natural boundary
262 // Allocate stack pool
264 Status
= GetEBCStack((EFI_HANDLE
)-1, &VmContext
.StackPool
, &StackIndex
);
265 if (EFI_ERROR(Status
)) {
268 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
269 VmContext
.Gpr
[0] = (UINT64
)(UINTN
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
270 VmContext
.HighStackBottom
= (UINTN
)VmContext
.Gpr
[0];
271 VmContext
.Gpr
[0] &= ~((VM_REGISTER
)(sizeof (UINTN
) - 1));
272 VmContext
.Gpr
[0] -= sizeof (UINTN
);
275 // Put a magic value in the stack gap, then adjust down again
277 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
278 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.Gpr
[0];
279 VmContext
.LowStackTop
= (UINTN
) VmContext
.Gpr
[0];
282 // For IA32, this is where we say our return address is
284 VmContext
.Gpr
[0] -= sizeof (UINTN
);
285 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg16
;
286 VmContext
.Gpr
[0] -= sizeof (UINTN
);
287 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg15
;
288 VmContext
.Gpr
[0] -= sizeof (UINTN
);
289 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg14
;
290 VmContext
.Gpr
[0] -= sizeof (UINTN
);
291 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg13
;
292 VmContext
.Gpr
[0] -= sizeof (UINTN
);
293 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg12
;
294 VmContext
.Gpr
[0] -= sizeof (UINTN
);
295 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg11
;
296 VmContext
.Gpr
[0] -= sizeof (UINTN
);
297 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg10
;
298 VmContext
.Gpr
[0] -= sizeof (UINTN
);
299 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg9
;
300 VmContext
.Gpr
[0] -= sizeof (UINTN
);
301 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg8
;
302 VmContext
.Gpr
[0] -= sizeof (UINTN
);
303 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg7
;
304 VmContext
.Gpr
[0] -= sizeof (UINTN
);
305 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg6
;
306 VmContext
.Gpr
[0] -= sizeof (UINTN
);
307 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg5
;
308 VmContext
.Gpr
[0] -= sizeof (UINTN
);
309 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg4
;
310 VmContext
.Gpr
[0] -= sizeof (UINTN
);
311 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg3
;
312 VmContext
.Gpr
[0] -= sizeof (UINTN
);
313 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg2
;
314 VmContext
.Gpr
[0] -= sizeof (UINTN
);
315 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) Arg1
;
316 VmContext
.Gpr
[0] -= 16;
317 VmContext
.StackRetAddr
= (UINT64
) VmContext
.Gpr
[0];
320 // We need to keep track of where the EBC stack starts. This way, if the EBC
321 // accesses any stack variables above its initial stack setting, then we know
322 // it's accessing variables passed into it, which means the data is on the
324 // When we're called, on the stack (high to low) we have the parameters, the
325 // return address, then the saved ebp. Save the pointer to the return address.
326 // EBC code knows that's there, so should look above it for function parameters.
327 // The offset is the size of locals (VMContext + Addr + saved ebp).
328 // Note that the interpreter assumes there is a 16 bytes of return address on
329 // the stack too, so adjust accordingly.
330 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));
334 // Begin executing the EBC code
336 EbcDebuggerHookEbcInterpret (&VmContext
);
337 EbcExecute (&VmContext
);
340 // Return the value in Gpr[7] unless there was an error
342 ReturnEBCStack(StackIndex
);
343 return (UINT64
) VmContext
.Gpr
[7];
348 Begin executing an EBC image.
350 @param EntryPoint The entrypoint of EBC code.
351 @param ImageHandle image handle for the EBC application we're executing
352 @param SystemTable standard system table passed into an driver's entry
355 @return The value returned by the EBC application we're going to run.
360 ExecuteEbcImageEntryPoint (
362 IN EFI_HANDLE ImageHandle
,
363 IN EFI_SYSTEM_TABLE
*SystemTable
367 // Create a new VM context on the stack
369 VM_CONTEXT VmContext
;
375 // Get the EBC entry point
380 // Now clear out our context
382 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
385 // Save the image handle so we can track the thunks created for this image
387 VmContext
.ImageHandle
= ImageHandle
;
388 VmContext
.SystemTable
= SystemTable
;
391 // Set the VM instruction pointer to the correct location in memory.
393 VmContext
.Ip
= (VMIP
) Addr
;
396 // Initialize the stack pointer for the EBC. Get the current system stack
397 // pointer and adjust it down by the max needed for the interpreter.
401 // Allocate stack pool
403 Status
= GetEBCStack(ImageHandle
, &VmContext
.StackPool
, &StackIndex
);
404 if (EFI_ERROR(Status
)) {
407 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
408 VmContext
.Gpr
[0] = (UINT64
)(UINTN
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
409 VmContext
.HighStackBottom
= (UINTN
)VmContext
.Gpr
[0];
410 VmContext
.Gpr
[0] -= sizeof (UINTN
);
413 // Put a magic value in the stack gap, then adjust down again
415 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
416 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.Gpr
[0];
419 // Align the stack on a natural boundary
420 // VmContext.Gpr[0] &= ~(sizeof(UINTN) - 1);
422 VmContext
.LowStackTop
= (UINTN
) VmContext
.Gpr
[0];
423 VmContext
.Gpr
[0] -= sizeof (UINTN
);
424 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) SystemTable
;
425 VmContext
.Gpr
[0] -= sizeof (UINTN
);
426 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) ImageHandle
;
428 VmContext
.Gpr
[0] -= 16;
429 VmContext
.StackRetAddr
= (UINT64
) VmContext
.Gpr
[0];
431 // VM pushes 16-bytes for return address. Simulate that here.
435 // Begin executing the EBC code
437 EbcDebuggerHookExecuteEbcImageEntryPoint (&VmContext
);
438 EbcExecute (&VmContext
);
441 // Return the value in Gpr[7] unless there was an error
443 ReturnEBCStack(StackIndex
);
444 return (UINT64
) VmContext
.Gpr
[7];
449 Create thunks for an EBC image entry point, or an EBC protocol service.
451 @param ImageHandle Image handle for the EBC image. If not null, then
452 we're creating a thunk for an image entry point.
453 @param EbcEntryPoint Address of the EBC code that the thunk is to call
454 @param Thunk Returned thunk we create here
455 @param Flags Flags indicating options for creating the thunk
457 @retval EFI_SUCCESS The thunk was created successfully.
458 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
460 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
462 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
467 IN EFI_HANDLE ImageHandle
,
468 IN VOID
*EbcEntryPoint
,
479 // Check alignment of pointer to EBC code
481 if ((UINT32
) (UINTN
) EbcEntryPoint
& 0x01) {
482 return EFI_INVALID_PARAMETER
;
485 ThunkSize
= sizeof(mInstructionBufferTemplate
);
487 Ptr
= EbcAllocatePoolForThunk (sizeof(mInstructionBufferTemplate
));
490 return EFI_OUT_OF_RESOURCES
;
493 // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);
495 // Save the start address so we can add a pointer to it to a list later.
500 // Give them the address of our buffer we're going to fix up
502 *Thunk
= (VOID
*) Ptr
;
505 // Copy whole thunk instruction buffer template
507 CopyMem (Ptr
, mInstructionBufferTemplate
, sizeof(mInstructionBufferTemplate
));
510 // Patch EbcEntryPoint and EbcLLEbcInterpret
512 for (Index
= 0; Index
< sizeof(mInstructionBufferTemplate
) - sizeof(UINTN
); Index
++) {
513 if (*(UINTN
*)&Ptr
[Index
] == EBC_ENTRYPOINT_SIGNATURE
) {
514 *(UINTN
*)&Ptr
[Index
] = (UINTN
)EbcEntryPoint
;
516 if (*(UINTN
*)&Ptr
[Index
] == EBC_LL_EBC_ENTRYPOINT_SIGNATURE
) {
517 if ((Flags
& FLAG_THUNK_ENTRY_POINT
) != 0) {
518 *(UINTN
*)&Ptr
[Index
] = (UINTN
)EbcLLExecuteEbcImageEntryPoint
;
520 *(UINTN
*)&Ptr
[Index
] = (UINTN
)EbcLLEbcInterpret
;
526 // Add the thunk to the list for this image. Do this last since the add
527 // function flushes the cache for us.
529 EbcAddImageThunk (ImageHandle
, (VOID
*) ThunkBase
, ThunkSize
);