2 Main routines for the EBC interpreter. Includes the initialization and
3 main interpreter routines.
5 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include <Protocol/DebugSupport.h>
17 #include <Protocol/Ebc.h>
18 #include <Protocol/EbcVmTest.h>
19 #include <Protocol/EbcSimpleDebugger.h>
20 #include <Protocol/PeCoffImageEmulator.h>
22 #include <Library/BaseLib.h>
23 #include <Library/CacheMaintenanceLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/PeCoffLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/MemoryAllocationLib.h>
31 extern VM_CONTEXT
*mVmPtr
;
34 // Flags passed to the internal create-thunks function.
36 #define FLAG_THUNK_ENTRY_POINT 0x01 // thunk for an image entry point
37 #define FLAG_THUNK_PROTOCOL 0x00 // thunk for an EBC protocol service
39 // Put this value at the bottom of the VM's stack gap so we can check it on
40 // occasion to make sure the stack has not been corrupted.
42 #define VM_STACK_KEY_VALUE 0xDEADBEEF
45 Create thunks for an EBC image entry point, or an EBC protocol service.
47 @param ImageHandle Image handle for the EBC image. If not null, then
48 we're creating a thunk for an image entry point.
49 @param EbcEntryPoint Address of the EBC code that the thunk is to call
50 @param Thunk Returned thunk we create here
51 @param Flags Flags indicating options for creating the thunk
53 @retval EFI_SUCCESS The thunk was created successfully.
54 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
56 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
58 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
63 IN EFI_HANDLE ImageHandle
,
64 IN VOID
*EbcEntryPoint
,
70 Add a thunk to our list of thunks for a given image handle.
71 Also flush the instruction cache since we've written thunk code
72 to memory that will be executed eventually.
74 @param ImageHandle The image handle to which the thunk is tied.
75 @param ThunkBuffer The buffer that has been created/allocated.
76 @param ThunkSize The size of the thunk memory allocated.
78 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
79 @retval EFI_SUCCESS The function completed successfully.
84 IN EFI_HANDLE ImageHandle
,
90 // Define a constant of how often to call the debugger periodic callback
93 #define EFI_TIMER_UNIT_1MS (1000 * 10)
94 #define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)
95 #define STACK_POOL_SIZE (1024 * 1020)
96 #define MAX_STACK_NUM 4
99 // External low level functions that are native-processor dependent
102 The VM thunk code stuffs an EBC entry point into a processor
103 register. Since we can't use inline assembly to get it from
104 the interpreter C code, stuff it into the return value
107 @return The contents of the register in which the entry point is passed.
112 EbcLLGetEbcEntryPoint (
117 This function is called to execute an EBC CALLEX instruction.
118 This instruction requires that we thunk out to external native
119 code. For x64, we switch stacks, copy the arguments to the stack
120 and jump to the specified function.
121 On return, we restore the stack pointer to its original location.
122 Destroys no working registers.
124 @param CallAddr The function address.
125 @param EbcSp The new EBC stack pointer.
126 @param FramePtr The frame pointer.
128 @return The unmodified value returned by the native code.
140 This function is called to execute an EBC CALLEX instruction.
141 The function check the callee's content to see whether it is common native
142 code or a thunk to another piece of EBC code.
143 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
144 otherwise, set the VM->IP to target EBC code directly to avoid another VM
145 be startup which cost time and stack space.
147 @param VmPtr Pointer to a VM context.
148 @param FuncAddr Callee's address
149 @param NewStackPointer New stack pointer after the call
150 @param FramePtr New frame pointer after the call
151 @param Size The size of call instruction
156 IN VM_CONTEXT
*VmPtr
,
158 IN UINTN NewStackPointer
,
164 Returns the stack index and buffer assosicated with the Handle parameter.
166 @param Handle The EFI handle as the index to the EBC stack.
167 @param StackBuffer A pointer to hold the returned stack buffer.
168 @param BufferIndex A pointer to hold the returned stack index.
170 @retval EFI_OUT_OF_RESOURCES The Handle parameter does not correspond to any
172 @retval EFI_SUCCESS The stack index and buffer were found and
173 returned to the caller.
178 IN EFI_HANDLE Handle
,
179 OUT VOID
**StackBuffer
,
180 OUT UINTN
*BufferIndex
184 Returns from the EBC stack by stack Index.
186 @param Index Specifies which EBC stack to return from.
188 @retval EFI_SUCCESS The function completed successfully.
197 Allocates memory to hold all the EBC stacks.
199 @retval EFI_SUCCESS The EBC stacks were allocated successfully.
200 @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.
209 Free all EBC stacks allocated before.
211 @retval EFI_SUCCESS All the EBC stacks were freed.
220 Returns from the EBC stack associated with the Handle parameter.
222 @param Handle Specifies the EFI handle to find the EBC stack with.
224 @retval EFI_SUCCESS The function completed successfully.
228 ReturnEBCStackByHandle(
233 EFI_EBC_PROTOCOL
*This
;
235 EFI_HANDLE ImageHandle
;
236 VM_CONTEXT VmContext
;
237 } EFI_EBC_THUNK_DATA
;
239 #define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('e', 'b', 'c', 'p')
242 #define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \
243 CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)
247 Allocates a buffer of type EfiBootServicesCode.
249 @param AllocationSize The number of bytes to allocate.
251 @return A pointer to the allocated buffer or NULL if allocation fails.
256 EbcAllocatePoolForThunk (
257 IN UINTN AllocationSize
260 #endif // #ifndef _EBC_INT_H_