2 This module contains EBC support routines that are customized based on
3 the target AArch64 processor.
5 Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>
6 Copyright (c) 2015, The Linux Foundation. All rights reserved.<BR>
7 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include "EbcExecute.h"
15 #include "EbcDebuggerHook.h"
18 // Amount of space that is not used in the stack
20 #define STACK_REMAIN_SIZE (1024 * 4)
27 UINT64 EbcLlEntryPoint
;
28 } EBC_INSTRUCTION_BUFFER
;
31 extern CONST EBC_INSTRUCTION_BUFFER mEbcInstructionBufferTemplate
;
34 Begin executing an EBC image.
35 This is used for Ebc Thunk call.
37 @return The value returned by the EBC application we're going to run.
47 Begin executing an EBC image.
48 This is used for Ebc image entrypoint.
50 @return The value returned by the EBC application we're going to run.
55 EbcLLExecuteEbcImageEntryPoint (
60 Pushes a 64 bit unsigned value to the VM stack.
62 @param VmPtr The pointer to current VM context.
63 @param Arg The value to be pushed.
73 // Advance the VM stack down, and then copy the argument to the stack.
76 VmPtr
->Gpr
[0] -= sizeof (UINT64
);
77 *(UINT64
*) VmPtr
->Gpr
[0] = Arg
;
83 Begin executing an EBC image.
85 This is a thunk function.
87 @param Arg1 The 1st argument.
88 @param Arg2 The 2nd argument.
89 @param Arg3 The 3rd argument.
90 @param Arg4 The 4th argument.
91 @param Arg5 The 5th argument.
92 @param Arg6 The 6th argument.
93 @param Arg7 The 7th argument.
94 @param Arg8 The 8th argument.
95 @param EntryPoint The entrypoint of EBC code.
96 @param Args9_16[] Array containing arguments #9 to #16.
98 @return The value returned by the EBC application we're going to run.
113 IN CONST UINTN Args9_16
[]
117 // Create a new VM context on the stack
119 VM_CONTEXT VmContext
;
125 // Get the EBC entry point
130 // Now clear out our context
132 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
135 // Set the VM instruction pointer to the correct location in memory.
137 VmContext
.Ip
= (VMIP
) Addr
;
140 // Initialize the stack pointer for the EBC. Get the current system stack
141 // pointer and adjust it down by the max needed for the interpreter.
145 // Adjust the VM's stack pointer down.
148 Status
= GetEBCStack((EFI_HANDLE
)(UINTN
)-1, &VmContext
.StackPool
, &StackIndex
);
149 if (EFI_ERROR(Status
)) {
152 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
153 VmContext
.Gpr
[0] = (UINT64
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
154 VmContext
.HighStackBottom
= (UINTN
) VmContext
.Gpr
[0];
155 VmContext
.Gpr
[0] -= sizeof (UINTN
);
158 // Align the stack on a natural boundary.
160 VmContext
.Gpr
[0] &= ~(VM_REGISTER
)(sizeof (UINTN
) - 1);
163 // Put a magic value in the stack gap, then adjust down again.
165 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
166 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.Gpr
[0];
169 // The stack upper to LowStackTop is belong to the VM.
171 VmContext
.LowStackTop
= (UINTN
) VmContext
.Gpr
[0];
174 // For the worst case, assume there are 4 arguments passed in registers, store
175 // them to VM's stack.
177 PushU64 (&VmContext
, (UINT64
) Args9_16
[7]);
178 PushU64 (&VmContext
, (UINT64
) Args9_16
[6]);
179 PushU64 (&VmContext
, (UINT64
) Args9_16
[5]);
180 PushU64 (&VmContext
, (UINT64
) Args9_16
[4]);
181 PushU64 (&VmContext
, (UINT64
) Args9_16
[3]);
182 PushU64 (&VmContext
, (UINT64
) Args9_16
[2]);
183 PushU64 (&VmContext
, (UINT64
) Args9_16
[1]);
184 PushU64 (&VmContext
, (UINT64
) Args9_16
[0]);
185 PushU64 (&VmContext
, (UINT64
) Arg8
);
186 PushU64 (&VmContext
, (UINT64
) Arg7
);
187 PushU64 (&VmContext
, (UINT64
) Arg6
);
188 PushU64 (&VmContext
, (UINT64
) Arg5
);
189 PushU64 (&VmContext
, (UINT64
) Arg4
);
190 PushU64 (&VmContext
, (UINT64
) Arg3
);
191 PushU64 (&VmContext
, (UINT64
) Arg2
);
192 PushU64 (&VmContext
, (UINT64
) Arg1
);
195 // Interpreter assumes 64-bit return address is pushed on the stack.
196 // AArch64 does not do this so pad the stack accordingly.
198 PushU64 (&VmContext
, (UINT64
) 0);
199 PushU64 (&VmContext
, (UINT64
) 0x1234567887654321ULL
);
202 // For AArch64, this is where we say our return address is
204 VmContext
.StackRetAddr
= (UINT64
) VmContext
.Gpr
[0];
207 // We need to keep track of where the EBC stack starts. This way, if the EBC
208 // accesses any stack variables above its initial stack setting, then we know
209 // it's accessing variables passed into it, which means the data is on the
211 // When we're called, on the stack (high to low) we have the parameters, the
212 // return address, then the saved ebp. Save the pointer to the return address.
213 // EBC code knows that's there, so should look above it for function parameters.
214 // The offset is the size of locals (VMContext + Addr + saved ebp).
215 // Note that the interpreter assumes there is a 16 bytes of return address on
216 // the stack too, so adjust accordingly.
217 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));
221 // Begin executing the EBC code
223 EbcDebuggerHookEbcInterpret (&VmContext
);
224 EbcExecute (&VmContext
);
227 // Return the value in R[7] unless there was an error
229 ReturnEBCStack(StackIndex
);
230 return (UINT64
) VmContext
.Gpr
[7];
235 Begin executing an EBC image.
237 @param ImageHandle image handle for the EBC application we're executing
238 @param SystemTable standard system table passed into an driver's entry
240 @param EntryPoint The entrypoint of EBC code.
242 @return The value returned by the EBC application we're going to run.
247 ExecuteEbcImageEntryPoint (
248 IN EFI_HANDLE ImageHandle
,
249 IN EFI_SYSTEM_TABLE
*SystemTable
,
254 // Create a new VM context on the stack
256 VM_CONTEXT VmContext
;
262 // Get the EBC entry point
267 // Now clear out our context
269 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
272 // Save the image handle so we can track the thunks created for this image
274 VmContext
.ImageHandle
= ImageHandle
;
275 VmContext
.SystemTable
= SystemTable
;
278 // Set the VM instruction pointer to the correct location in memory.
280 VmContext
.Ip
= (VMIP
) Addr
;
283 // Initialize the stack pointer for the EBC. Get the current system stack
284 // pointer and adjust it down by the max needed for the interpreter.
287 Status
= GetEBCStack(ImageHandle
, &VmContext
.StackPool
, &StackIndex
);
288 if (EFI_ERROR(Status
)) {
291 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
292 VmContext
.Gpr
[0] = (UINT64
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
293 VmContext
.HighStackBottom
= (UINTN
) VmContext
.Gpr
[0];
294 VmContext
.Gpr
[0] -= sizeof (UINTN
);
298 // Put a magic value in the stack gap, then adjust down again
300 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
301 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.Gpr
[0];
304 // Align the stack on a natural boundary
305 VmContext
.Gpr
[0] &= ~(VM_REGISTER
)(sizeof(UINTN
) - 1);
307 VmContext
.LowStackTop
= (UINTN
) VmContext
.Gpr
[0];
310 // Simply copy the image handle and system table onto the EBC stack.
311 // Greatly simplifies things by not having to spill the args.
313 PushU64 (&VmContext
, (UINT64
) SystemTable
);
314 PushU64 (&VmContext
, (UINT64
) ImageHandle
);
317 // VM pushes 16-bytes for return address. Simulate that here.
319 PushU64 (&VmContext
, (UINT64
) 0);
320 PushU64 (&VmContext
, (UINT64
) 0x1234567887654321ULL
);
323 // For AArch64, this is where we say our return address is
325 VmContext
.StackRetAddr
= (UINT64
) VmContext
.Gpr
[0];
328 // Entry function needn't access high stack context, simply
329 // put the stack pointer here.
333 // Begin executing the EBC code
335 EbcDebuggerHookExecuteEbcImageEntryPoint (&VmContext
);
336 EbcExecute (&VmContext
);
339 // Return the value in R[7] unless there was an error
341 ReturnEBCStack(StackIndex
);
342 return (UINT64
) VmContext
.Gpr
[7];
347 Create thunks for an EBC image entry point, or an EBC protocol service.
349 @param ImageHandle Image handle for the EBC image. If not null, then
350 we're creating a thunk for an image entry point.
351 @param EbcEntryPoint Address of the EBC code that the thunk is to call
352 @param Thunk Returned thunk we create here
353 @param Flags Flags indicating options for creating the thunk
355 @retval EFI_SUCCESS The thunk was created successfully.
356 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
358 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
360 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
365 IN EFI_HANDLE ImageHandle
,
366 IN VOID
*EbcEntryPoint
,
371 EBC_INSTRUCTION_BUFFER
*InstructionBuffer
;
374 // Check alignment of pointer to EBC code
376 if ((UINT32
) (UINTN
) EbcEntryPoint
& 0x01) {
377 return EFI_INVALID_PARAMETER
;
380 InstructionBuffer
= EbcAllocatePoolForThunk (sizeof (EBC_INSTRUCTION_BUFFER
));
381 if (InstructionBuffer
== NULL
) {
382 return EFI_OUT_OF_RESOURCES
;
386 // Give them the address of our buffer we're going to fix up
388 *Thunk
= InstructionBuffer
;
391 // Copy whole thunk instruction buffer template
393 CopyMem (InstructionBuffer
, &mEbcInstructionBufferTemplate
,
394 sizeof (EBC_INSTRUCTION_BUFFER
));
397 // Patch EbcEntryPoint and EbcLLEbcInterpret
399 InstructionBuffer
->EbcEntryPoint
= (UINT64
)EbcEntryPoint
;
400 if ((Flags
& FLAG_THUNK_ENTRY_POINT
) != 0) {
401 InstructionBuffer
->EbcLlEntryPoint
= (UINT64
)EbcLLExecuteEbcImageEntryPoint
;
403 InstructionBuffer
->EbcLlEntryPoint
= (UINT64
)EbcLLEbcInterpret
;
407 // Add the thunk to the list for this image. Do this last since the add
408 // function flushes the cache for us.
410 EbcAddImageThunk (ImageHandle
, InstructionBuffer
,
411 sizeof (EBC_INSTRUCTION_BUFFER
));
418 This function is called to execute an EBC CALLEX instruction.
419 The function check the callee's content to see whether it is common native
420 code or a thunk to another piece of EBC code.
421 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
422 otherwise, set the VM->IP to target EBC code directly to avoid another VM
423 be startup which cost time and stack space.
425 @param VmPtr Pointer to a VM context.
426 @param FuncAddr Callee's address
427 @param NewStackPointer New stack pointer after the call
428 @param FramePtr New frame pointer after the call
429 @param Size The size of call instruction
434 IN VM_CONTEXT
*VmPtr
,
436 IN UINTN NewStackPointer
,
441 CONST EBC_INSTRUCTION_BUFFER
*InstructionBuffer
;
444 // Processor specific code to check whether the callee is a thunk to EBC.
446 InstructionBuffer
= (EBC_INSTRUCTION_BUFFER
*)FuncAddr
;
448 if (CompareMem (InstructionBuffer
, &mEbcInstructionBufferTemplate
,
449 sizeof(EBC_INSTRUCTION_BUFFER
) - 2 * sizeof (UINT64
)) == 0) {
451 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
452 // put our return address and frame pointer on the VM stack.
453 // Then set the VM's IP to new EBC code.
456 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINTN
) FramePtr
);
457 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->Gpr
[0];
459 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINT64
) (UINTN
) (VmPtr
->Ip
+ Size
));
461 VmPtr
->Ip
= (VMIP
) InstructionBuffer
->EbcEntryPoint
;
464 // The callee is not a thunk to EBC, call native code,
465 // and get return value.
467 VmPtr
->Gpr
[7] = EbcLLCALLEXNative (FuncAddr
, NewStackPointer
, FramePtr
);