3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 This module contains EBC support routines that are customized based on
24 #include "EbcExecute.h"
27 // NOTE: This is the stack size allocated for the interpreter
28 // when it executes an EBC image. The requirements can change
29 // based on whether or not a debugger is present, and other
30 // platform-specific configurations.
32 #define VM_STACK_SIZE (1024 * 4)
33 #define EBC_THUNK_SIZE 32
39 IN UINTN NewStackPointer
,
47 This function is called to execute an EBC CALLEX instruction.
48 The function check the callee's content to see whether it is common native
49 code or a thunk to another piece of EBC code.
50 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
51 otherwise, set the VM->IP to target EBC code directly to avoid another VM
52 be startup which cost time and stack space.
56 VmPtr - Pointer to a VM context.
57 FuncAddr - Callee's address
58 NewStackPointer - New stack pointer after the call
59 FramePtr - New frame pointer after the call
60 Size - The size of call instruction
75 // Processor specific code to check whether the callee is a thunk to EBC.
77 if (*((UINT8
*)FuncAddr
) != 0xB8) {
81 if (*((UINT8
*)FuncAddr
+ 1) != 0xBC) {
85 if (*((UINT8
*)FuncAddr
+ 2) != 0x2E) {
89 if (*((UINT8
*)FuncAddr
+ 3) != 0x11) {
93 if (*((UINT8
*)FuncAddr
+ 4) != 0xCA) {
97 if (*((UINT8
*)FuncAddr
+ 5) != 0xB8) {
101 if (*((UINT8
*)FuncAddr
+ 10) != 0xB9) {
105 if (*((UINT8
*)FuncAddr
+ 15) != 0xFF) {
109 if (*((UINT8
*)FuncAddr
+ 16) != 0xE1) {
114 TargetEbcAddr
= ((UINTN
)(*((UINT8
*)FuncAddr
+ 9)) << 24) + ((UINTN
)(*((UINT8
*)FuncAddr
+ 8)) << 16) +
115 ((UINTN
)(*((UINT8
*)FuncAddr
+ 7)) << 8) + ((UINTN
)(*((UINT8
*)FuncAddr
+ 6)));
120 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
121 // put our return address and frame pointer on the VM stack.
122 // Then set the VM's IP to new EBC code.
125 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->R
[0], (UINTN
) FramePtr
);
126 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->R
[0];
128 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->R
[0], (UINT64
) (UINTN
) (VmPtr
->Ip
+ Size
));
130 VmPtr
->Ip
= (VMIP
) (UINTN
) TargetEbcAddr
;
133 // The callee is not a thunk to EBC, call native code.
135 EbcLLCALLEXNative (FuncAddr
, NewStackPointer
, FramePtr
);
138 // Get return value and advance the IP.
140 VmPtr
->R
[7] = EbcLLGetReturnValue ();
161 Begin executing an EBC image. The address of the entry point is passed
162 in via a processor register, so we'll need to make a call to get the
167 None. Since we're called from a fixed up thunk (which we want to keep
168 small), our only so-called argument is the EBC entry point passed in
169 to us in a processor register.
173 The value returned by the EBC application we're going to run.
178 // Create a new VM context on the stack
180 VM_CONTEXT VmContext
;
184 // Get the EBC entry point from the processor register.
186 Addr
= EbcLLGetEbcEntryPoint ();
189 // Now clear out our context
191 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
194 // Set the VM instruction pointer to the correct location in memory.
196 VmContext
.Ip
= (VMIP
) Addr
;
199 // Initialize the stack pointer for the EBC. Get the current system stack
200 // pointer and adjust it down by the max needed for the interpreter.
202 Addr
= EbcLLGetStackPointer ();
204 VmContext
.R
[0] = (UINT64
) Addr
;
205 VmContext
.R
[0] -= VM_STACK_SIZE
;
208 // Align the stack on a natural boundary
210 VmContext
.R
[0] &= ~(sizeof (UINTN
) - 1);
213 // Put a magic value in the stack gap, then adjust down again
215 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
216 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.R
[0];
217 VmContext
.R
[0] -= sizeof (UINTN
);
220 // For IA32, this is where we say our return address is
222 VmContext
.StackRetAddr
= (UINT64
) VmContext
.R
[0];
223 VmContext
.LowStackTop
= (UINTN
) VmContext
.R
[0];
226 // We need to keep track of where the EBC stack starts. This way, if the EBC
227 // accesses any stack variables above its initial stack setting, then we know
228 // it's accessing variables passed into it, which means the data is on the
230 // When we're called, on the stack (high to low) we have the parameters, the
231 // return address, then the saved ebp. Save the pointer to the return address.
232 // EBC code knows that's there, so should look above it for function parameters.
233 // The offset is the size of locals (VMContext + Addr + saved ebp).
234 // Note that the interpreter assumes there is a 16 bytes of return address on
235 // the stack too, so adjust accordingly.
236 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));
238 VmContext
.HighStackBottom
= (UINTN
) &Arg1
- 16;
240 // Begin executing the EBC code
242 EbcExecute (&VmContext
);
245 // Return the value in R[7] unless there was an error
247 return (UINT64
) VmContext
.R
[7];
252 ExecuteEbcImageEntryPoint (
253 IN EFI_HANDLE ImageHandle
,
254 IN EFI_SYSTEM_TABLE
*SystemTable
260 Begin executing an EBC image. The address of the entry point is passed
261 in via a processor register, so we'll need to make a call to get the
266 ImageHandle - image handle for the EBC application we're executing
267 SystemTable - standard system table passed into an driver's entry point
271 The value returned by the EBC application we're going to run.
276 // Create a new VM context on the stack
278 VM_CONTEXT VmContext
;
282 // Get the EBC entry point from the processor register. Make sure you don't
283 // call any functions before this or you could mess up the register the
284 // entry point is passed in.
286 Addr
= EbcLLGetEbcEntryPoint ();
289 // Print(L"*** Thunked into EBC entry point - ImageHandle = 0x%X\n", (UINTN)ImageHandle);
290 // Print(L"EBC entry point is 0x%X\n", (UINT32)(UINTN)Addr);
292 // Now clear out our context
294 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
297 // Save the image handle so we can track the thunks created for this image
299 VmContext
.ImageHandle
= ImageHandle
;
300 VmContext
.SystemTable
= SystemTable
;
303 // Set the VM instruction pointer to the correct location in memory.
305 VmContext
.Ip
= (VMIP
) Addr
;
308 // Initialize the stack pointer for the EBC. Get the current system stack
309 // pointer and adjust it down by the max needed for the interpreter.
311 Addr
= EbcLLGetStackPointer ();
312 VmContext
.R
[0] = (UINT64
) Addr
;
313 VmContext
.R
[0] -= VM_STACK_SIZE
;
315 // Put a magic value in the stack gap, then adjust down again
317 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
318 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.R
[0];
319 VmContext
.R
[0] -= sizeof (UINTN
);
322 // Align the stack on a natural boundary
323 // VmContext.R[0] &= ~(sizeof(UINTN) - 1);
325 VmContext
.StackRetAddr
= (UINT64
) VmContext
.R
[0];
326 VmContext
.LowStackTop
= (UINTN
) VmContext
.R
[0];
328 // VM pushes 16-bytes for return address. Simulate that here.
330 VmContext
.HighStackBottom
= (UINTN
) &ImageHandle
- 16;
333 // Begin executing the EBC code
335 EbcExecute (&VmContext
);
338 // Return the value in R[7] unless there was an error
340 return (UINT64
) VmContext
.R
[7];
345 IN EFI_HANDLE ImageHandle
,
346 IN VOID
*EbcEntryPoint
,
354 Create an IA32 thunk for the given EBC entry point.
358 ImageHandle - Handle of image for which this thunk is being created
359 EbcEntryPoint - Address of the EBC code that the thunk is to call
360 Thunk - Returned thunk we create here
377 // Check alignment of pointer to EBC code
379 if ((UINT32
) (UINTN
) EbcEntryPoint
& 0x01) {
380 return EFI_INVALID_PARAMETER
;
383 Size
= EBC_THUNK_SIZE
;
386 Status
= gBS
->AllocatePool (
391 if (Status
!= EFI_SUCCESS
) {
392 return EFI_OUT_OF_RESOURCES
;
395 // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);
397 // Save the start address so we can add a pointer to it to a list later.
402 // Give them the address of our buffer we're going to fix up
404 *Thunk
= (VOID
*) Ptr
;
407 // Add a magic code here to help the VM recognize the thunk..
408 // mov eax, 0xca112ebc => B8 BC 2E 11 CA
413 Addr
= (UINT32
) 0xCA112EBC;
414 for (I
= 0; I
< sizeof (Addr
); I
++) {
415 *Ptr
= (UINT8
) (UINTN
) Addr
;
422 // Add code bytes to load up a processor register with the EBC entry point.
423 // mov eax, 0xaa55aa55 => B8 55 AA 55 AA
424 // The first 8 bytes of the thunk entry is the address of the EBC
430 Addr
= (UINT32
) EbcEntryPoint
;
431 for (I
= 0; I
< sizeof (Addr
); I
++) {
432 *Ptr
= (UINT8
) (UINTN
) Addr
;
438 // Stick in a load of ecx with the address of appropriate VM function.
439 // mov ecx 12345678h => 0xB9 0x78 0x56 0x34 0x12
441 if (Flags
& FLAG_THUNK_ENTRY_POINT
) {
442 Addr
= (UINT32
) (UINTN
) ExecuteEbcImageEntryPoint
;
444 Addr
= (UINT32
) (UINTN
) EbcInterpret
;
453 for (I
= 0; I
< sizeof (Addr
); I
++) {
460 // Stick in jump opcode bytes for jmp ecx => 0xFF 0xE1
469 // Double check that our defined size is ok (application error)
473 return EFI_BUFFER_TOO_SMALL
;
476 // Add the thunk to the list for this image. Do this last since the add
477 // function flushes the cache for us.
479 EbcAddImageThunk (ImageHandle
, (VOID
*) ThunkBase
, ThunkSize
);