2 This module contains EBC support routines that are customized based on
5 Copyright (c) 2006, Intel Corporation
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"
20 // NOTE: This is the stack size allocated for the interpreter
21 // when it executes an EBC image. The requirements can change
22 // based on whether or not a debugger is present, and other
23 // platform-specific configurations.
25 #define VM_STACK_SIZE (1024 * 4)
26 #define EBC_THUNK_SIZE 32
28 #define STACK_REMAIN_SIZE (1024 * 4)
32 This function is called to execute an EBC CALLEX instruction.
33 The function check the callee's content to see whether it is common native
34 code or a thunk to another piece of EBC code.
35 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
36 otherwise, set the VM->IP to target EBC code directly to avoid another VM
37 be startup which cost time and stack space.
39 @parm VmPtr Pointer to a VM context.
40 @parm FuncAddr Callee's address
41 @parm NewStackPointer New stack pointer after the call
42 @parm FramePtr New frame pointer after the call
43 @parm Size The size of call instruction
52 IN UINTN NewStackPointer
,
64 // Processor specific code to check whether the callee is a thunk to EBC.
66 if (*((UINT8
*)FuncAddr
) != 0xB8) {
70 if (*((UINT8
*)FuncAddr
+ 1) != 0xBC) {
74 if (*((UINT8
*)FuncAddr
+ 2) != 0x2E) {
78 if (*((UINT8
*)FuncAddr
+ 3) != 0x11) {
82 if (*((UINT8
*)FuncAddr
+ 4) != 0xCA) {
86 if (*((UINT8
*)FuncAddr
+ 5) != 0xB8) {
90 if (*((UINT8
*)FuncAddr
+ 10) != 0xB9) {
94 if (*((UINT8
*)FuncAddr
+ 15) != 0xFF) {
98 if (*((UINT8
*)FuncAddr
+ 16) != 0xE1) {
103 TargetEbcAddr
= ((UINTN
)(*((UINT8
*)FuncAddr
+ 9)) << 24) + ((UINTN
)(*((UINT8
*)FuncAddr
+ 8)) << 16) +
104 ((UINTN
)(*((UINT8
*)FuncAddr
+ 7)) << 8) + ((UINTN
)(*((UINT8
*)FuncAddr
+ 6)));
109 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
110 // put our return address and frame pointer on the VM stack.
111 // Then set the VM's IP to new EBC code.
114 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->R
[0], (UINTN
) FramePtr
);
115 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->R
[0];
117 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->R
[0], (UINT64
) (UINTN
) (VmPtr
->Ip
+ Size
));
119 VmPtr
->Ip
= (VMIP
) (UINTN
) TargetEbcAddr
;
122 // The callee is not a thunk to EBC, call native code.
124 EbcLLCALLEXNative (FuncAddr
, NewStackPointer
, FramePtr
);
127 // Get return value and advance the IP.
129 VmPtr
->R
[7] = EbcLLGetReturnValue ();
136 Begin executing an EBC image. The address of the entry point is passed
137 in via a processor register, so we'll need to make a call to get the
140 None. Since we're called from a fixed up thunk (which we want to keep
141 small), our only so-called argument is the EBC entry point passed in
142 to us in a processor register.
144 @return The value returned by the EBC application we're going to run.
169 // Create a new VM context on the stack
171 VM_CONTEXT VmContext
;
177 // Get the EBC entry point from the processor register.
179 Addr
= EbcLLGetEbcEntryPoint ();
182 // Now clear out our context
184 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
187 // Set the VM instruction pointer to the correct location in memory.
189 VmContext
.Ip
= (VMIP
) Addr
;
191 // Initialize the stack pointer for the EBC. Get the current system stack
192 // pointer and adjust it down by the max needed for the interpreter.
196 // Align the stack on a natural boundary
200 // Allocate stack pool
202 Status
= GetEBCStack((EFI_HANDLE
)-1, &VmContext
.StackPool
, &StackIndex
);
203 if (EFI_ERROR(Status
)) {
206 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
207 VmContext
.R
[0] = (UINT64
)(UINTN
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
208 VmContext
.HighStackBottom
= (UINTN
)VmContext
.R
[0];
209 VmContext
.R
[0] &= ~(sizeof (UINTN
) - 1);
210 VmContext
.R
[0] -= sizeof (UINTN
);
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
.LowStackTop
= (UINTN
) VmContext
.R
[0];
220 // For IA32, this is where we say our return address is
222 VmContext
.R
[0] -= sizeof (UINTN
);
223 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg16
;
224 VmContext
.R
[0] -= sizeof (UINTN
);
225 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg15
;
226 VmContext
.R
[0] -= sizeof (UINTN
);
227 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg14
;
228 VmContext
.R
[0] -= sizeof (UINTN
);
229 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg13
;
230 VmContext
.R
[0] -= sizeof (UINTN
);
231 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg12
;
232 VmContext
.R
[0] -= sizeof (UINTN
);
233 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg11
;
234 VmContext
.R
[0] -= sizeof (UINTN
);
235 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg10
;
236 VmContext
.R
[0] -= sizeof (UINTN
);
237 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg9
;
238 VmContext
.R
[0] -= sizeof (UINTN
);
239 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg8
;
240 VmContext
.R
[0] -= sizeof (UINTN
);
241 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg7
;
242 VmContext
.R
[0] -= sizeof (UINTN
);
243 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg6
;
244 VmContext
.R
[0] -= sizeof (UINTN
);
245 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg5
;
246 VmContext
.R
[0] -= sizeof (UINTN
);
247 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg4
;
248 VmContext
.R
[0] -= sizeof (UINTN
);
249 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg3
;
250 VmContext
.R
[0] -= sizeof (UINTN
);
251 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg2
;
252 VmContext
.R
[0] -= sizeof (UINTN
);
253 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg1
;
254 VmContext
.R
[0] -= 16;
255 VmContext
.StackRetAddr
= (UINT64
) VmContext
.R
[0];
258 // We need to keep track of where the EBC stack starts. This way, if the EBC
259 // accesses any stack variables above its initial stack setting, then we know
260 // it's accessing variables passed into it, which means the data is on the
262 // When we're called, on the stack (high to low) we have the parameters, the
263 // return address, then the saved ebp. Save the pointer to the return address.
264 // EBC code knows that's there, so should look above it for function parameters.
265 // The offset is the size of locals (VMContext + Addr + saved ebp).
266 // Note that the interpreter assumes there is a 16 bytes of return address on
267 // the stack too, so adjust accordingly.
268 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));
272 // Begin executing the EBC code
274 EbcExecute (&VmContext
);
277 // Return the value in R[7] unless there was an error
279 ReturnEBCStack(StackIndex
);
280 return (UINT64
) VmContext
.R
[7];
285 Begin executing an EBC image. The address of the entry point is passed
286 in via a processor register, so we'll need to make a call to get the
289 @param ImageHandle image handle for the EBC application we're executing
290 @param SystemTable standard system table passed into an driver's entry point
292 @return The value returned by the EBC application we're going to run.
297 ExecuteEbcImageEntryPoint (
298 IN EFI_HANDLE ImageHandle
,
299 IN EFI_SYSTEM_TABLE
*SystemTable
303 // Create a new VM context on the stack
305 VM_CONTEXT VmContext
;
311 // Get the EBC entry point from the processor register. Make sure you don't
312 // call any functions before this or you could mess up the register the
313 // entry point is passed in.
315 Addr
= EbcLLGetEbcEntryPoint ();
318 // Print(L"*** Thunked into EBC entry point - ImageHandle = 0x%X\n", (UINTN)ImageHandle);
319 // Print(L"EBC entry point is 0x%X\n", (UINT32)(UINTN)Addr);
321 // Now clear out our context
323 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
326 // Save the image handle so we can track the thunks created for this image
328 VmContext
.ImageHandle
= ImageHandle
;
329 VmContext
.SystemTable
= SystemTable
;
332 // Set the VM instruction pointer to the correct location in memory.
334 VmContext
.Ip
= (VMIP
) Addr
;
337 // Initialize the stack pointer for the EBC. Get the current system stack
338 // pointer and adjust it down by the max needed for the interpreter.
342 // Allocate stack pool
344 Status
= GetEBCStack(ImageHandle
, &VmContext
.StackPool
, &StackIndex
);
345 if (EFI_ERROR(Status
)) {
348 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
349 VmContext
.R
[0] = (UINT64
)(UINTN
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
350 VmContext
.HighStackBottom
= (UINTN
)VmContext
.R
[0];
351 VmContext
.R
[0] -= sizeof (UINTN
);
354 // Put a magic value in the stack gap, then adjust down again
356 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
357 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.R
[0];
360 // Align the stack on a natural boundary
361 // VmContext.R[0] &= ~(sizeof(UINTN) - 1);
363 VmContext
.LowStackTop
= (UINTN
) VmContext
.R
[0];
364 VmContext
.R
[0] -= sizeof (UINTN
);
365 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) SystemTable
;
366 VmContext
.R
[0] -= sizeof (UINTN
);
367 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) ImageHandle
;
369 VmContext
.R
[0] -= 16;
370 VmContext
.StackRetAddr
= (UINT64
) VmContext
.R
[0];
372 // VM pushes 16-bytes for return address. Simulate that here.
376 // Begin executing the EBC code
378 EbcExecute (&VmContext
);
381 // Return the value in R[7] unless there was an error
383 return (UINT64
) VmContext
.R
[7];
388 Create an IA32 thunk for the given EBC entry point.
390 @param ImageHandle Handle of image for which this thunk is being created
391 @param EbcEntryPoint Address of the EBC code that the thunk is to call
392 @param Thunk Returned thunk we create here
394 @return Standard EFI status.
399 IN EFI_HANDLE ImageHandle
,
400 IN VOID
*EbcEntryPoint
,
413 // Check alignment of pointer to EBC code
415 if ((UINT32
) (UINTN
) EbcEntryPoint
& 0x01) {
416 return EFI_INVALID_PARAMETER
;
419 Size
= EBC_THUNK_SIZE
;
422 Ptr
= AllocatePool (Size
);
425 return EFI_OUT_OF_RESOURCES
;
428 // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);
430 // Save the start address so we can add a pointer to it to a list later.
435 // Give them the address of our buffer we're going to fix up
437 *Thunk
= (VOID
*) Ptr
;
440 // Add a magic code here to help the VM recognize the thunk..
441 // mov eax, 0xca112ebc => B8 BC 2E 11 CA
446 Addr
= (UINT32
) 0xCA112EBC;
447 for (I
= 0; I
< sizeof (Addr
); I
++) {
448 *Ptr
= (UINT8
) (UINTN
) Addr
;
455 // Add code bytes to load up a processor register with the EBC entry point.
456 // mov eax, 0xaa55aa55 => B8 55 AA 55 AA
457 // The first 8 bytes of the thunk entry is the address of the EBC
463 Addr
= (UINT32
) EbcEntryPoint
;
464 for (I
= 0; I
< sizeof (Addr
); I
++) {
465 *Ptr
= (UINT8
) (UINTN
) Addr
;
471 // Stick in a load of ecx with the address of appropriate VM function.
472 // mov ecx 12345678h => 0xB9 0x78 0x56 0x34 0x12
474 if (Flags
& FLAG_THUNK_ENTRY_POINT
) {
475 Addr
= (UINT32
) (UINTN
) ExecuteEbcImageEntryPoint
;
477 Addr
= (UINT32
) (UINTN
) EbcInterpret
;
486 for (I
= 0; I
< sizeof (Addr
); I
++) {
493 // Stick in jump opcode bytes for jmp ecx => 0xFF 0xE1
502 // Double check that our defined size is ok (application error)
506 return EFI_BUFFER_TOO_SMALL
;
509 // Add the thunk to the list for this image. Do this last since the add
510 // function flushes the cache for us.
512 EbcAddImageThunk (ImageHandle
, (VOID
*) ThunkBase
, ThunkSize
);