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 @param VmPtr Pointer to a VM context.
40 @param FuncAddr Callee's address
41 @param NewStackPointer New stack pointer after the call
42 @param FramePtr New frame pointer after the call
43 @param Size The size of call instruction
50 IN UINTN NewStackPointer
,
62 // Processor specific code to check whether the callee is a thunk to EBC.
64 if (*((UINT8
*)FuncAddr
) != 0xB8) {
68 if (*((UINT8
*)FuncAddr
+ 1) != 0xBC) {
72 if (*((UINT8
*)FuncAddr
+ 2) != 0x2E) {
76 if (*((UINT8
*)FuncAddr
+ 3) != 0x11) {
80 if (*((UINT8
*)FuncAddr
+ 4) != 0xCA) {
84 if (*((UINT8
*)FuncAddr
+ 5) != 0xB8) {
88 if (*((UINT8
*)FuncAddr
+ 10) != 0xB9) {
92 if (*((UINT8
*)FuncAddr
+ 15) != 0xFF) {
96 if (*((UINT8
*)FuncAddr
+ 16) != 0xE1) {
101 TargetEbcAddr
= ((UINTN
)(*((UINT8
*)FuncAddr
+ 9)) << 24) + ((UINTN
)(*((UINT8
*)FuncAddr
+ 8)) << 16) +
102 ((UINTN
)(*((UINT8
*)FuncAddr
+ 7)) << 8) + ((UINTN
)(*((UINT8
*)FuncAddr
+ 6)));
107 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
108 // put our return address and frame pointer on the VM stack.
109 // Then set the VM's IP to new EBC code.
112 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->R
[0], (UINTN
) FramePtr
);
113 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->R
[0];
115 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->R
[0], (UINT64
) (UINTN
) (VmPtr
->Ip
+ Size
));
117 VmPtr
->Ip
= (VMIP
) (UINTN
) TargetEbcAddr
;
120 // The callee is not a thunk to EBC, call native code.
122 EbcLLCALLEXNative (FuncAddr
, NewStackPointer
, FramePtr
);
125 // Get return value and advance the IP.
127 VmPtr
->R
[7] = EbcLLGetReturnValue ();
134 Begin executing an EBC image. The address of the entry point is passed
135 in via a processor register, so we'll need to make a call to get the
138 This is a thunk function. Microsoft x64 compiler only provide fast_call
139 calling convention, so the first four arguments are passed by rcx, rdx,
140 r8, and r9, while other arguments are passed in stack.
142 @param Arg1 The 1st argument.
143 @param Arg2 The 2nd argument.
144 @param Arg3 The 3rd argument.
145 @param Arg4 The 4th argument.
146 @param Arg5 The 5th argument.
147 @param Arg6 The 6th argument.
148 @param Arg7 The 7th argument.
149 @param Arg8 The 8th argument.
150 @param Arg9 The 9th argument.
151 @param Arg10 The 10th argument.
152 @param Arg11 The 11th argument.
153 @param Arg12 The 12th argument.
154 @param Arg13 The 13th argument.
155 @param Arg14 The 14th argument.
156 @param Arg15 The 15th argument.
157 @param Arg16 The 16th argument.
159 @return The value returned by the EBC application we're going to run.
184 // Create a new VM context on the stack
186 VM_CONTEXT VmContext
;
192 // Get the EBC entry point from the processor register.
194 Addr
= EbcLLGetEbcEntryPoint ();
197 // Now clear out our context
199 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
202 // Set the VM instruction pointer to the correct location in memory.
204 VmContext
.Ip
= (VMIP
) Addr
;
206 // Initialize the stack pointer for the EBC. Get the current system stack
207 // pointer and adjust it down by the max needed for the interpreter.
211 // Align the stack on a natural boundary
215 // Allocate stack pool
217 Status
= GetEBCStack((EFI_HANDLE
)-1, &VmContext
.StackPool
, &StackIndex
);
218 if (EFI_ERROR(Status
)) {
221 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
222 VmContext
.R
[0] = (UINT64
)(UINTN
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
223 VmContext
.HighStackBottom
= (UINTN
)VmContext
.R
[0];
224 VmContext
.R
[0] &= ~(sizeof (UINTN
) - 1);
225 VmContext
.R
[0] -= sizeof (UINTN
);
228 // Put a magic value in the stack gap, then adjust down again
230 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
231 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.R
[0];
232 VmContext
.LowStackTop
= (UINTN
) VmContext
.R
[0];
235 // For IA32, this is where we say our return address is
237 VmContext
.R
[0] -= sizeof (UINTN
);
238 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg16
;
239 VmContext
.R
[0] -= sizeof (UINTN
);
240 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg15
;
241 VmContext
.R
[0] -= sizeof (UINTN
);
242 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg14
;
243 VmContext
.R
[0] -= sizeof (UINTN
);
244 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg13
;
245 VmContext
.R
[0] -= sizeof (UINTN
);
246 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg12
;
247 VmContext
.R
[0] -= sizeof (UINTN
);
248 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg11
;
249 VmContext
.R
[0] -= sizeof (UINTN
);
250 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg10
;
251 VmContext
.R
[0] -= sizeof (UINTN
);
252 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg9
;
253 VmContext
.R
[0] -= sizeof (UINTN
);
254 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg8
;
255 VmContext
.R
[0] -= sizeof (UINTN
);
256 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg7
;
257 VmContext
.R
[0] -= sizeof (UINTN
);
258 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg6
;
259 VmContext
.R
[0] -= sizeof (UINTN
);
260 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg5
;
261 VmContext
.R
[0] -= sizeof (UINTN
);
262 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg4
;
263 VmContext
.R
[0] -= sizeof (UINTN
);
264 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg3
;
265 VmContext
.R
[0] -= sizeof (UINTN
);
266 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg2
;
267 VmContext
.R
[0] -= sizeof (UINTN
);
268 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) Arg1
;
269 VmContext
.R
[0] -= 16;
270 VmContext
.StackRetAddr
= (UINT64
) VmContext
.R
[0];
273 // We need to keep track of where the EBC stack starts. This way, if the EBC
274 // accesses any stack variables above its initial stack setting, then we know
275 // it's accessing variables passed into it, which means the data is on the
277 // When we're called, on the stack (high to low) we have the parameters, the
278 // return address, then the saved ebp. Save the pointer to the return address.
279 // EBC code knows that's there, so should look above it for function parameters.
280 // The offset is the size of locals (VMContext + Addr + saved ebp).
281 // Note that the interpreter assumes there is a 16 bytes of return address on
282 // the stack too, so adjust accordingly.
283 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));
287 // Begin executing the EBC code
289 EbcExecute (&VmContext
);
292 // Return the value in R[7] unless there was an error
294 ReturnEBCStack(StackIndex
);
295 return (UINT64
) VmContext
.R
[7];
300 Begin executing an EBC image. The address of the entry point is passed
301 in via a processor register, so we'll need to make a call to get the
304 @param ImageHandle image handle for the EBC application we're executing
305 @param SystemTable standard system table passed into an driver's entry
308 @return The value returned by the EBC application we're going to run.
313 ExecuteEbcImageEntryPoint (
314 IN EFI_HANDLE ImageHandle
,
315 IN EFI_SYSTEM_TABLE
*SystemTable
319 // Create a new VM context on the stack
321 VM_CONTEXT VmContext
;
327 // Get the EBC entry point from the processor register. Make sure you don't
328 // call any functions before this or you could mess up the register the
329 // entry point is passed in.
331 Addr
= EbcLLGetEbcEntryPoint ();
334 // Print(L"*** Thunked into EBC entry point - ImageHandle = 0x%X\n", (UINTN)ImageHandle);
335 // Print(L"EBC entry point is 0x%X\n", (UINT32)(UINTN)Addr);
337 // Now clear out our context
339 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
342 // Save the image handle so we can track the thunks created for this image
344 VmContext
.ImageHandle
= ImageHandle
;
345 VmContext
.SystemTable
= SystemTable
;
348 // Set the VM instruction pointer to the correct location in memory.
350 VmContext
.Ip
= (VMIP
) Addr
;
353 // Initialize the stack pointer for the EBC. Get the current system stack
354 // pointer and adjust it down by the max needed for the interpreter.
358 // Allocate stack pool
360 Status
= GetEBCStack(ImageHandle
, &VmContext
.StackPool
, &StackIndex
);
361 if (EFI_ERROR(Status
)) {
364 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
365 VmContext
.R
[0] = (UINT64
)(UINTN
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
366 VmContext
.HighStackBottom
= (UINTN
)VmContext
.R
[0];
367 VmContext
.R
[0] -= sizeof (UINTN
);
370 // Put a magic value in the stack gap, then adjust down again
372 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
373 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.R
[0];
376 // Align the stack on a natural boundary
377 // VmContext.R[0] &= ~(sizeof(UINTN) - 1);
379 VmContext
.LowStackTop
= (UINTN
) VmContext
.R
[0];
380 VmContext
.R
[0] -= sizeof (UINTN
);
381 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) SystemTable
;
382 VmContext
.R
[0] -= sizeof (UINTN
);
383 *(UINTN
*) (UINTN
) (VmContext
.R
[0]) = (UINTN
) ImageHandle
;
385 VmContext
.R
[0] -= 16;
386 VmContext
.StackRetAddr
= (UINT64
) VmContext
.R
[0];
388 // VM pushes 16-bytes for return address. Simulate that here.
392 // Begin executing the EBC code
394 EbcExecute (&VmContext
);
397 // Return the value in R[7] unless there was an error
399 return (UINT64
) VmContext
.R
[7];
404 Create thunks for an EBC image entry point, or an EBC protocol service.
406 @param ImageHandle Image handle for the EBC image. If not null, then
407 we're creating a thunk for an image entry point.
408 @param EbcEntryPoint Address of the EBC code that the thunk is to call
409 @param Thunk Returned thunk we create here
410 @param Flags Flags indicating options for creating the thunk
412 @retval EFI_SUCCESS The thunk was created successfully.
413 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
415 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
417 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
422 IN EFI_HANDLE ImageHandle
,
423 IN VOID
*EbcEntryPoint
,
436 // Check alignment of pointer to EBC code
438 if ((UINT32
) (UINTN
) EbcEntryPoint
& 0x01) {
439 return EFI_INVALID_PARAMETER
;
442 Size
= EBC_THUNK_SIZE
;
445 Ptr
= AllocatePool (Size
);
448 return EFI_OUT_OF_RESOURCES
;
451 // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);
453 // Save the start address so we can add a pointer to it to a list later.
458 // Give them the address of our buffer we're going to fix up
460 *Thunk
= (VOID
*) Ptr
;
463 // Add a magic code here to help the VM recognize the thunk..
464 // mov eax, 0xca112ebc => B8 BC 2E 11 CA
469 Addr
= (UINT32
) 0xCA112EBC;
470 for (Index
= 0; Index
< sizeof (Addr
); Index
++) {
471 *Ptr
= (UINT8
) (UINTN
) Addr
;
478 // Add code bytes to load up a processor register with the EBC entry point.
479 // mov eax, 0xaa55aa55 => B8 55 AA 55 AA
480 // The first 8 bytes of the thunk entry is the address of the EBC
486 Addr
= (UINT32
) EbcEntryPoint
;
487 for (Index
= 0; Index
< sizeof (Addr
); Index
++) {
488 *Ptr
= (UINT8
) (UINTN
) Addr
;
494 // Stick in a load of ecx with the address of appropriate VM function.
495 // mov ecx 12345678h => 0xB9 0x78 0x56 0x34 0x12
497 if (Flags
& FLAG_THUNK_ENTRY_POINT
) {
498 Addr
= (UINT32
) (UINTN
) ExecuteEbcImageEntryPoint
;
500 Addr
= (UINT32
) (UINTN
) EbcInterpret
;
509 for (Index
= 0; Index
< sizeof (Addr
); Index
++) {
516 // Stick in jump opcode bytes for jmp ecx => 0xFF 0xE1
525 // Double check that our defined size is ok (application error)
529 return EFI_BUFFER_TOO_SMALL
;
532 // Add the thunk to the list for this image. Do this last since the add
533 // function flushes the cache for us.
535 EbcAddImageThunk (ImageHandle
, (VOID
*) ThunkBase
, ThunkSize
);