]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/EbcDxe/EbcInt.h
MdeModulePkg/EbcDxe: implement the PE/COFF emulator protocol
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / EbcInt.h
1 /** @file
2 Main routines for the EBC interpreter. Includes the initialization and
3 main interpreter routines.
4
5 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #ifndef _EBC_INT_H_
11 #define _EBC_INT_H_
12
13
14 #include <Uefi.h>
15
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>
21
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>
30
31 extern VM_CONTEXT *mVmPtr;
32
33 //
34 // Flags passed to the internal create-thunks function.
35 //
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
38 //
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.
41 //
42 #define VM_STACK_KEY_VALUE 0xDEADBEEF
43
44 /**
45 Create thunks for an EBC image entry point, or an EBC protocol service.
46
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
52
53 @retval EFI_SUCCESS The thunk was created successfully.
54 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
55 aligned.
56 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
57 Thunk.
58 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
59
60 **/
61 EFI_STATUS
62 EbcCreateThunks (
63 IN EFI_HANDLE ImageHandle,
64 IN VOID *EbcEntryPoint,
65 OUT VOID **Thunk,
66 IN UINT32 Flags
67 );
68
69 /**
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.
73
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.
77
78 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
79 @retval EFI_SUCCESS The function completed successfully.
80
81 **/
82 EFI_STATUS
83 EbcAddImageThunk (
84 IN EFI_HANDLE ImageHandle,
85 IN VOID *ThunkBuffer,
86 IN UINT32 ThunkSize
87 );
88
89 //
90 // Define a constant of how often to call the debugger periodic callback
91 // function.
92 //
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
97
98 //
99 // External low level functions that are native-processor dependent
100 //
101 /**
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
105 register and return.
106
107 @return The contents of the register in which the entry point is passed.
108
109 **/
110 UINTN
111 EFIAPI
112 EbcLLGetEbcEntryPoint (
113 VOID
114 );
115
116 /**
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.
123
124 @param CallAddr The function address.
125 @param EbcSp The new EBC stack pointer.
126 @param FramePtr The frame pointer.
127
128 @return The unmodified value returned by the native code.
129
130 **/
131 INT64
132 EFIAPI
133 EbcLLCALLEXNative (
134 IN UINTN CallAddr,
135 IN UINTN EbcSp,
136 IN VOID *FramePtr
137 );
138
139 /**
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.
146
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
152
153 **/
154 VOID
155 EbcLLCALLEX (
156 IN VM_CONTEXT *VmPtr,
157 IN UINTN FuncAddr,
158 IN UINTN NewStackPointer,
159 IN VOID *FramePtr,
160 IN UINT8 Size
161 );
162
163 /**
164 Returns the stack index and buffer assosicated with the Handle parameter.
165
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.
169
170 @retval EFI_OUT_OF_RESOURCES The Handle parameter does not correspond to any
171 existing EBC stack.
172 @retval EFI_SUCCESS The stack index and buffer were found and
173 returned to the caller.
174
175 **/
176 EFI_STATUS
177 GetEBCStack(
178 IN EFI_HANDLE Handle,
179 OUT VOID **StackBuffer,
180 OUT UINTN *BufferIndex
181 );
182
183 /**
184 Returns from the EBC stack by stack Index.
185
186 @param Index Specifies which EBC stack to return from.
187
188 @retval EFI_SUCCESS The function completed successfully.
189
190 **/
191 EFI_STATUS
192 ReturnEBCStack(
193 IN UINTN Index
194 );
195
196 /**
197 Allocates memory to hold all the EBC stacks.
198
199 @retval EFI_SUCCESS The EBC stacks were allocated successfully.
200 @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.
201
202 **/
203 EFI_STATUS
204 InitEBCStack (
205 VOID
206 );
207
208 /**
209 Free all EBC stacks allocated before.
210
211 @retval EFI_SUCCESS All the EBC stacks were freed.
212
213 **/
214 EFI_STATUS
215 FreeEBCStack(
216 VOID
217 );
218
219 /**
220 Returns from the EBC stack associated with the Handle parameter.
221
222 @param Handle Specifies the EFI handle to find the EBC stack with.
223
224 @retval EFI_SUCCESS The function completed successfully.
225
226 **/
227 EFI_STATUS
228 ReturnEBCStackByHandle(
229 IN EFI_HANDLE Handle
230 );
231
232 typedef struct {
233 EFI_EBC_PROTOCOL *This;
234 VOID *EntryPoint;
235 EFI_HANDLE ImageHandle;
236 VM_CONTEXT VmContext;
237 } EFI_EBC_THUNK_DATA;
238
239 #define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('e', 'b', 'c', 'p')
240
241
242 #define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \
243 CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)
244
245
246 /**
247 Allocates a buffer of type EfiBootServicesCode.
248
249 @param AllocationSize The number of bytes to allocate.
250
251 @return A pointer to the allocated buffer or NULL if allocation fails.
252
253 **/
254 VOID *
255 EFIAPI
256 EbcAllocatePoolForThunk (
257 IN UINTN AllocationSize
258 );
259
260 #endif // #ifndef _EBC_INT_H_