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
35 #define STACK_REMAIN_SIZE (1024 * 4)
40 IN UINTN NewStackPointer
,
48 This function is called to execute an EBC CALLEX instruction.
49 The function check the callee's content to see whether it is common native
50 code or a thunk to another piece of EBC code.
51 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
52 otherwise, set the VM->IP to target EBC code directly to avoid another VM
53 be startup which cost time and stack space.
57 VmPtr - Pointer to a VM context.
58 FuncAddr - Callee's address
59 NewStackPointer - New stack pointer after the call
60 FramePtr - New frame pointer after the call
61 Size - The size of call instruction
76 // Processor specific code to check whether the callee is a thunk to EBC.
78 if (*((UINT8
*)FuncAddr
) != 0xB8) {
82 if (*((UINT8
*)FuncAddr
+ 1) != 0xBC) {
86 if (*((UINT8
*)FuncAddr
+ 2) != 0x2E) {
90 if (*((UINT8
*)FuncAddr
+ 3) != 0x11) {
94 if (*((UINT8
*)FuncAddr
+ 4) != 0xCA) {
98 if (*((UINT8
*)FuncAddr
+ 5) != 0xB8) {
102 if (*((UINT8
*)FuncAddr
+ 10) != 0xB9) {
106 if (*((UINT8
*)FuncAddr
+ 15) != 0xFF) {
110 if (*((UINT8
*)FuncAddr
+ 16) != 0xE1) {
115 TargetEbcAddr
= ((UINTN
)(*((UINT8
*)FuncAddr
+ 9)) << 24) + ((UINTN
)(*((UINT8
*)FuncAddr
+ 8)) << 16) +
116 ((UINTN
)(*((UINT8
*)FuncAddr
+ 7)) << 8) + ((UINTN
)(*((UINT8
*)FuncAddr
+ 6)));
121 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
122 // put our return address and frame pointer on the VM stack.
123 // Then set the VM's IP to new EBC code.
126 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->R
[0], (UINTN
) FramePtr
);
127 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->R
[0];
129 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->R
[0], (UINT64
) (UINTN
) (VmPtr
->Ip
+ Size
));
131 VmPtr
->Ip
= (VMIP
) (UINTN
) TargetEbcAddr
;
134 // The callee is not a thunk to EBC, call native code.
136 EbcLLCALLEXNative (FuncAddr
, NewStackPointer
, FramePtr
);
139 // Get return value and advance the IP.
141 VmPtr
->R
[7] = EbcLLGetReturnValue ();
170 Begin executing an EBC image. The address of the entry point is passed
171 in via a processor register, so we'll need to make a call to get the
176 None. Since we're called from a fixed up thunk (which we want to keep
177 small), our only so-called argument is the EBC entry point passed in
178 to us in a processor register.
182 The value returned by the EBC application we're going to run.
187 // Create a new VM context on the stack
189 VM_CONTEXT VmContext
;
195 // Get the EBC entry point from the processor register.
197 Addr
= EbcLLGetEbcEntryPoint ();
200 // Now clear out our context
202 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
205 // Set the VM instruction pointer to the correct location in memory.
207 VmContext
.Ip
= (VMIP
) Addr
;
209 // Initialize the stack pointer for the EBC. Get the current system stack
210 // pointer and adjust it down by the max needed for the interpreter.
214 // Align the stack on a natural boundary
218 // Allocate stack pool
220 Status
= GetEBCStack((EFI_HANDLE
)-1, &VmContext
.StackPool
, &StackIndex
);
221 if (EFI_ERROR(Status
)) {
224 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
225 VmContext
.R
[0] = (UINT64
)(UINTN
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
226 VmContext
.HighStackBottom
= (UINTN
)VmContext
.R
[0];
227 VmContext
.R
[0] &= ~(sizeof (UINTN
) - 1);
228 VmContext
.R
[0] -= sizeof (UINTN
);
231 // Put a magic value in the stack gap, then adjust down again
233 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
234 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.R
[0];
235 VmContext
.LowStackTop
= (UINTN
) VmContext
.R
[0];
238 // For IA32, this is where we say our return address is
240 VmContext
.R
[0] -= sizeof (UINTN
);
241 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg16
;
242 VmContext
.R
[0] -= sizeof (UINTN
);
243 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg15
;
244 VmContext
.R
[0] -= sizeof (UINTN
);
245 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg14
;
246 VmContext
.R
[0] -= sizeof (UINTN
);
247 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg13
;
248 VmContext
.R
[0] -= sizeof (UINTN
);
249 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg12
;
250 VmContext
.R
[0] -= sizeof (UINTN
);
251 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg11
;
252 VmContext
.R
[0] -= sizeof (UINTN
);
253 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg10
;
254 VmContext
.R
[0] -= sizeof (UINTN
);
255 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg9
;
256 VmContext
.R
[0] -= sizeof (UINTN
);
257 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg8
;
258 VmContext
.R
[0] -= sizeof (UINTN
);
259 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg7
;
260 VmContext
.R
[0] -= sizeof (UINTN
);
261 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg6
;
262 VmContext
.R
[0] -= sizeof (UINTN
);
263 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg5
;
264 VmContext
.R
[0] -= sizeof (UINTN
);
265 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg4
;
266 VmContext
.R
[0] -= sizeof (UINTN
);
267 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg3
;
268 VmContext
.R
[0] -= sizeof (UINTN
);
269 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg2
;
270 VmContext
.R
[0] -= sizeof (UINTN
);
271 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg1
;
272 VmContext
.R
[0] -= 16;
273 VmContext
.StackRetAddr
= (UINT64
) VmContext
.R
[0];
276 // We need to keep track of where the EBC stack starts. This way, if the EBC
277 // accesses any stack variables above its initial stack setting, then we know
278 // it's accessing variables passed into it, which means the data is on the
280 // When we're called, on the stack (high to low) we have the parameters, the
281 // return address, then the saved ebp. Save the pointer to the return address.
282 // EBC code knows that's there, so should look above it for function parameters.
283 // The offset is the size of locals (VMContext + Addr + saved ebp).
284 // Note that the interpreter assumes there is a 16 bytes of return address on
285 // the stack too, so adjust accordingly.
286 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));
290 // Begin executing the EBC code
292 EbcExecute (&VmContext
);
295 // Return the value in R[7] unless there was an error
297 ReturnEBCStack(StackIndex
);
298 return (UINT64
) VmContext
.R
[7];
303 ExecuteEbcImageEntryPoint (
304 IN EFI_HANDLE ImageHandle
,
305 IN EFI_SYSTEM_TABLE
*SystemTable
311 Begin executing an EBC image. The address of the entry point is passed
312 in via a processor register, so we'll need to make a call to get the
317 ImageHandle - image handle for the EBC application we're executing
318 SystemTable - standard system table passed into an driver's entry point
322 The value returned by the EBC application we're going to run.
327 // Create a new VM context on the stack
329 VM_CONTEXT VmContext
;
335 // Get the EBC entry point from the processor register. Make sure you don't
336 // call any functions before this or you could mess up the register the
337 // entry point is passed in.
339 Addr
= EbcLLGetEbcEntryPoint ();
342 // Print(L"*** Thunked into EBC entry point - ImageHandle = 0x%X\n", (UINTN)ImageHandle);
343 // Print(L"EBC entry point is 0x%X\n", (UINT32)(UINTN)Addr);
345 // Now clear out our context
347 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
350 // Save the image handle so we can track the thunks created for this image
352 VmContext
.ImageHandle
= ImageHandle
;
353 VmContext
.SystemTable
= SystemTable
;
356 // Set the VM instruction pointer to the correct location in memory.
358 VmContext
.Ip
= (VMIP
) Addr
;
361 // Initialize the stack pointer for the EBC. Get the current system stack
362 // pointer and adjust it down by the max needed for the interpreter.
366 // Allocate stack pool
368 Status
= GetEBCStack(ImageHandle
, &VmContext
.StackPool
, &StackIndex
);
369 if (EFI_ERROR(Status
)) {
372 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
373 VmContext
.R
[0] = (UINT64
)(UINTN
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
374 VmContext
.HighStackBottom
= (UINTN
)VmContext
.R
[0];
375 VmContext
.R
[0] -= sizeof (UINTN
);
378 // Put a magic value in the stack gap, then adjust down again
380 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
381 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.R
[0];
384 // Align the stack on a natural boundary
385 // VmContext.R[0] &= ~(sizeof(UINTN) - 1);
387 VmContext
.LowStackTop
= (UINTN
) VmContext
.R
[0];
388 VmContext
.R
[0] -= sizeof (UINTN
);
389 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) SystemTable
;
390 VmContext
.R
[0] -= sizeof (UINTN
);
391 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) ImageHandle
;
393 VmContext
.R
[0] -= 16;
394 VmContext
.StackRetAddr
= (UINT64
) VmContext
.R
[0];
396 // VM pushes 16-bytes for return address. Simulate that here.
400 // Begin executing the EBC code
402 EbcExecute (&VmContext
);
405 // Return the value in R[7] unless there was an error
407 return (UINT64
) VmContext
.R
[7];
412 IN EFI_HANDLE ImageHandle
,
413 IN VOID
*EbcEntryPoint
,
421 Create an IA32 thunk for the given EBC entry point.
425 ImageHandle - Handle of image for which this thunk is being created
426 EbcEntryPoint - Address of the EBC code that the thunk is to call
427 Thunk - Returned thunk we create here
443 // Check alignment of pointer to EBC code
445 if ((UINT32
) (UINTN
) EbcEntryPoint
& 0x01) {
446 return EFI_INVALID_PARAMETER
;
449 Size
= EBC_THUNK_SIZE
;
452 Ptr
= AllocatePool (Size
);
455 return EFI_OUT_OF_RESOURCES
;
458 // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);
460 // Save the start address so we can add a pointer to it to a list later.
465 // Give them the address of our buffer we're going to fix up
467 *Thunk
= (VOID
*) Ptr
;
470 // Add a magic code here to help the VM recognize the thunk..
471 // mov eax, 0xca112ebc => B8 BC 2E 11 CA
476 Addr
= (UINT32
) 0xCA112EBC;
477 for (I
= 0; I
< sizeof (Addr
); I
++) {
478 *Ptr
= (UINT8
) (UINTN
) Addr
;
485 // Add code bytes to load up a processor register with the EBC entry point.
486 // mov eax, 0xaa55aa55 => B8 55 AA 55 AA
487 // The first 8 bytes of the thunk entry is the address of the EBC
493 Addr
= (UINT32
) EbcEntryPoint
;
494 for (I
= 0; I
< sizeof (Addr
); I
++) {
495 *Ptr
= (UINT8
) (UINTN
) Addr
;
501 // Stick in a load of ecx with the address of appropriate VM function.
502 // mov ecx 12345678h => 0xB9 0x78 0x56 0x34 0x12
504 if (Flags
& FLAG_THUNK_ENTRY_POINT
) {
505 Addr
= (UINT32
) (UINTN
) ExecuteEbcImageEntryPoint
;
507 Addr
= (UINT32
) (UINTN
) EbcInterpret
;
516 for (I
= 0; I
< sizeof (Addr
); I
++) {
523 // Stick in jump opcode bytes for jmp ecx => 0xFF 0xE1
532 // Double check that our defined size is ok (application error)
536 return EFI_BUFFER_TOO_SMALL
;
539 // Add the thunk to the list for this image. Do this last since the add
540 // function flushes the cache for us.
542 EbcAddImageThunk (ImageHandle
, (VOID
*) ThunkBase
, ThunkSize
);