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 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include "EbcExecute.h"
21 #include "EbcDebuggerHook.h"
24 // Amount of space that is not used in the stack
26 #define STACK_REMAIN_SIZE (1024 * 4)
33 UINT64 EbcLlEntryPoint
;
34 } EBC_INSTRUCTION_BUFFER
;
37 extern CONST EBC_INSTRUCTION_BUFFER mEbcInstructionBufferTemplate
;
40 Begin executing an EBC image.
41 This is used for Ebc Thunk call.
43 @return The value returned by the EBC application we're going to run.
53 Begin executing an EBC image.
54 This is used for Ebc image entrypoint.
56 @return The value returned by the EBC application we're going to run.
61 EbcLLExecuteEbcImageEntryPoint (
66 Pushes a 64 bit unsigned value to the VM stack.
68 @param VmPtr The pointer to current VM context.
69 @param Arg The value to be pushed.
79 // Advance the VM stack down, and then copy the argument to the stack.
82 VmPtr
->Gpr
[0] -= sizeof (UINT64
);
83 *(UINT64
*) VmPtr
->Gpr
[0] = Arg
;
89 Begin executing an EBC image.
91 This is a thunk function.
93 @param Arg1 The 1st argument.
94 @param Arg2 The 2nd argument.
95 @param Arg3 The 3rd argument.
96 @param Arg4 The 4th argument.
97 @param Arg5 The 5th argument.
98 @param Arg6 The 6th argument.
99 @param Arg7 The 7th argument.
100 @param Arg8 The 8th argument.
101 @param EntryPoint The entrypoint of EBC code.
102 @param Args9_16[] Array containing arguments #9 to #16.
104 @return The value returned by the EBC application we're going to run.
119 IN CONST UINTN Args9_16
[]
123 // Create a new VM context on the stack
125 VM_CONTEXT VmContext
;
131 // Get the EBC entry point
136 // Now clear out our context
138 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
141 // Set the VM instruction pointer to the correct location in memory.
143 VmContext
.Ip
= (VMIP
) Addr
;
146 // Initialize the stack pointer for the EBC. Get the current system stack
147 // pointer and adjust it down by the max needed for the interpreter.
151 // Adjust the VM's stack pointer down.
154 Status
= GetEBCStack((EFI_HANDLE
)(UINTN
)-1, &VmContext
.StackPool
, &StackIndex
);
155 if (EFI_ERROR(Status
)) {
158 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
159 VmContext
.Gpr
[0] = (UINT64
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
160 VmContext
.HighStackBottom
= (UINTN
) VmContext
.Gpr
[0];
161 VmContext
.Gpr
[0] -= sizeof (UINTN
);
164 // Align the stack on a natural boundary.
166 VmContext
.Gpr
[0] &= ~(VM_REGISTER
)(sizeof (UINTN
) - 1);
169 // Put a magic value in the stack gap, then adjust down again.
171 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
172 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.Gpr
[0];
175 // The stack upper to LowStackTop is belong to the VM.
177 VmContext
.LowStackTop
= (UINTN
) VmContext
.Gpr
[0];
180 // For the worst case, assume there are 4 arguments passed in registers, store
181 // them to VM's stack.
183 PushU64 (&VmContext
, (UINT64
) Args9_16
[7]);
184 PushU64 (&VmContext
, (UINT64
) Args9_16
[6]);
185 PushU64 (&VmContext
, (UINT64
) Args9_16
[5]);
186 PushU64 (&VmContext
, (UINT64
) Args9_16
[4]);
187 PushU64 (&VmContext
, (UINT64
) Args9_16
[3]);
188 PushU64 (&VmContext
, (UINT64
) Args9_16
[2]);
189 PushU64 (&VmContext
, (UINT64
) Args9_16
[1]);
190 PushU64 (&VmContext
, (UINT64
) Args9_16
[0]);
191 PushU64 (&VmContext
, (UINT64
) Arg8
);
192 PushU64 (&VmContext
, (UINT64
) Arg7
);
193 PushU64 (&VmContext
, (UINT64
) Arg6
);
194 PushU64 (&VmContext
, (UINT64
) Arg5
);
195 PushU64 (&VmContext
, (UINT64
) Arg4
);
196 PushU64 (&VmContext
, (UINT64
) Arg3
);
197 PushU64 (&VmContext
, (UINT64
) Arg2
);
198 PushU64 (&VmContext
, (UINT64
) Arg1
);
201 // Interpreter assumes 64-bit return address is pushed on the stack.
202 // AArch64 does not do this so pad the stack accordingly.
204 PushU64 (&VmContext
, (UINT64
) 0);
205 PushU64 (&VmContext
, (UINT64
) 0x1234567887654321ULL
);
208 // For AArch64, this is where we say our return address is
210 VmContext
.StackRetAddr
= (UINT64
) VmContext
.Gpr
[0];
213 // We need to keep track of where the EBC stack starts. This way, if the EBC
214 // accesses any stack variables above its initial stack setting, then we know
215 // it's accessing variables passed into it, which means the data is on the
217 // When we're called, on the stack (high to low) we have the parameters, the
218 // return address, then the saved ebp. Save the pointer to the return address.
219 // EBC code knows that's there, so should look above it for function parameters.
220 // The offset is the size of locals (VMContext + Addr + saved ebp).
221 // Note that the interpreter assumes there is a 16 bytes of return address on
222 // the stack too, so adjust accordingly.
223 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));
227 // Begin executing the EBC code
229 EbcDebuggerHookEbcInterpret (&VmContext
);
230 EbcExecute (&VmContext
);
233 // Return the value in R[7] unless there was an error
235 ReturnEBCStack(StackIndex
);
236 return (UINT64
) VmContext
.Gpr
[7];
241 Begin executing an EBC image.
243 @param ImageHandle image handle for the EBC application we're executing
244 @param SystemTable standard system table passed into an driver's entry
246 @param EntryPoint The entrypoint of EBC code.
248 @return The value returned by the EBC application we're going to run.
253 ExecuteEbcImageEntryPoint (
254 IN EFI_HANDLE ImageHandle
,
255 IN EFI_SYSTEM_TABLE
*SystemTable
,
260 // Create a new VM context on the stack
262 VM_CONTEXT VmContext
;
268 // Get the EBC entry point
273 // Now clear out our context
275 ZeroMem ((VOID
*) &VmContext
, sizeof (VM_CONTEXT
));
278 // Save the image handle so we can track the thunks created for this image
280 VmContext
.ImageHandle
= ImageHandle
;
281 VmContext
.SystemTable
= SystemTable
;
284 // Set the VM instruction pointer to the correct location in memory.
286 VmContext
.Ip
= (VMIP
) Addr
;
289 // Initialize the stack pointer for the EBC. Get the current system stack
290 // pointer and adjust it down by the max needed for the interpreter.
293 Status
= GetEBCStack(ImageHandle
, &VmContext
.StackPool
, &StackIndex
);
294 if (EFI_ERROR(Status
)) {
297 VmContext
.StackTop
= (UINT8
*)VmContext
.StackPool
+ (STACK_REMAIN_SIZE
);
298 VmContext
.Gpr
[0] = (UINT64
) ((UINT8
*)VmContext
.StackPool
+ STACK_POOL_SIZE
);
299 VmContext
.HighStackBottom
= (UINTN
) VmContext
.Gpr
[0];
300 VmContext
.Gpr
[0] -= sizeof (UINTN
);
304 // Put a magic value in the stack gap, then adjust down again
306 *(UINTN
*) (UINTN
) (VmContext
.Gpr
[0]) = (UINTN
) VM_STACK_KEY_VALUE
;
307 VmContext
.StackMagicPtr
= (UINTN
*) (UINTN
) VmContext
.Gpr
[0];
310 // Align the stack on a natural boundary
311 VmContext
.Gpr
[0] &= ~(VM_REGISTER
)(sizeof(UINTN
) - 1);
313 VmContext
.LowStackTop
= (UINTN
) VmContext
.Gpr
[0];
316 // Simply copy the image handle and system table onto the EBC stack.
317 // Greatly simplifies things by not having to spill the args.
319 PushU64 (&VmContext
, (UINT64
) SystemTable
);
320 PushU64 (&VmContext
, (UINT64
) ImageHandle
);
323 // VM pushes 16-bytes for return address. Simulate that here.
325 PushU64 (&VmContext
, (UINT64
) 0);
326 PushU64 (&VmContext
, (UINT64
) 0x1234567887654321ULL
);
329 // For AArch64, this is where we say our return address is
331 VmContext
.StackRetAddr
= (UINT64
) VmContext
.Gpr
[0];
334 // Entry function needn't access high stack context, simply
335 // put the stack pointer here.
339 // Begin executing the EBC code
341 EbcDebuggerHookExecuteEbcImageEntryPoint (&VmContext
);
342 EbcExecute (&VmContext
);
345 // Return the value in R[7] unless there was an error
347 ReturnEBCStack(StackIndex
);
348 return (UINT64
) VmContext
.Gpr
[7];
353 Create thunks for an EBC image entry point, or an EBC protocol service.
355 @param ImageHandle Image handle for the EBC image. If not null, then
356 we're creating a thunk for an image entry point.
357 @param EbcEntryPoint Address of the EBC code that the thunk is to call
358 @param Thunk Returned thunk we create here
359 @param Flags Flags indicating options for creating the thunk
361 @retval EFI_SUCCESS The thunk was created successfully.
362 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
364 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
366 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
371 IN EFI_HANDLE ImageHandle
,
372 IN VOID
*EbcEntryPoint
,
377 EBC_INSTRUCTION_BUFFER
*InstructionBuffer
;
380 // Check alignment of pointer to EBC code
382 if ((UINT32
) (UINTN
) EbcEntryPoint
& 0x01) {
383 return EFI_INVALID_PARAMETER
;
386 InstructionBuffer
= AllocatePool (sizeof (EBC_INSTRUCTION_BUFFER
));
387 if (InstructionBuffer
== NULL
) {
388 return EFI_OUT_OF_RESOURCES
;
392 // Give them the address of our buffer we're going to fix up
394 *Thunk
= InstructionBuffer
;
397 // Copy whole thunk instruction buffer template
399 CopyMem (InstructionBuffer
, &mEbcInstructionBufferTemplate
,
400 sizeof (EBC_INSTRUCTION_BUFFER
));
403 // Patch EbcEntryPoint and EbcLLEbcInterpret
405 InstructionBuffer
->EbcEntryPoint
= (UINT64
)EbcEntryPoint
;
406 if ((Flags
& FLAG_THUNK_ENTRY_POINT
) != 0) {
407 InstructionBuffer
->EbcLlEntryPoint
= (UINT64
)EbcLLExecuteEbcImageEntryPoint
;
409 InstructionBuffer
->EbcLlEntryPoint
= (UINT64
)EbcLLEbcInterpret
;
413 // Add the thunk to the list for this image. Do this last since the add
414 // function flushes the cache for us.
416 EbcAddImageThunk (ImageHandle
, InstructionBuffer
,
417 sizeof (EBC_INSTRUCTION_BUFFER
));
424 This function is called to execute an EBC CALLEX instruction.
425 The function check the callee's content to see whether it is common native
426 code or a thunk to another piece of EBC code.
427 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
428 otherwise, set the VM->IP to target EBC code directly to avoid another VM
429 be startup which cost time and stack space.
431 @param VmPtr Pointer to a VM context.
432 @param FuncAddr Callee's address
433 @param NewStackPointer New stack pointer after the call
434 @param FramePtr New frame pointer after the call
435 @param Size The size of call instruction
440 IN VM_CONTEXT
*VmPtr
,
442 IN UINTN NewStackPointer
,
447 CONST EBC_INSTRUCTION_BUFFER
*InstructionBuffer
;
450 // Processor specific code to check whether the callee is a thunk to EBC.
452 InstructionBuffer
= (EBC_INSTRUCTION_BUFFER
*)FuncAddr
;
454 if (CompareMem (InstructionBuffer
, &mEbcInstructionBufferTemplate
,
455 sizeof(EBC_INSTRUCTION_BUFFER
) - 2 * sizeof (UINT64
)) == 0) {
457 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and
458 // put our return address and frame pointer on the VM stack.
459 // Then set the VM's IP to new EBC code.
462 VmWriteMemN (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINTN
) FramePtr
);
463 VmPtr
->FramePtr
= (VOID
*) (UINTN
) VmPtr
->Gpr
[0];
465 VmWriteMem64 (VmPtr
, (UINTN
) VmPtr
->Gpr
[0], (UINT64
) (UINTN
) (VmPtr
->Ip
+ Size
));
467 VmPtr
->Ip
= (VMIP
) InstructionBuffer
->EbcEntryPoint
;
470 // The callee is not a thunk to EBC, call native code,
471 // and get return value.
473 VmPtr
->Gpr
[7] = EbcLLCALLEXNative (FuncAddr
, NewStackPointer
, FramePtr
);