]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/EbcDxe/EbcInt.h
MdeModulePkg: Replace BSD License with BSD+Patent License
[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
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/UefiDriverEntryPoint.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/MemoryAllocationLib.h>
27
28 extern VM_CONTEXT *mVmPtr;
29
30 //
31 // Flags passed to the internal create-thunks function.
32 //
33 #define FLAG_THUNK_ENTRY_POINT 0x01 // thunk for an image entry point
34 #define FLAG_THUNK_PROTOCOL 0x00 // thunk for an EBC protocol service
35 //
36 // Put this value at the bottom of the VM's stack gap so we can check it on
37 // occasion to make sure the stack has not been corrupted.
38 //
39 #define VM_STACK_KEY_VALUE 0xDEADBEEF
40
41 /**
42 Create thunks for an EBC image entry point, or an EBC protocol service.
43
44 @param ImageHandle Image handle for the EBC image. If not null, then
45 we're creating a thunk for an image entry point.
46 @param EbcEntryPoint Address of the EBC code that the thunk is to call
47 @param Thunk Returned thunk we create here
48 @param Flags Flags indicating options for creating the thunk
49
50 @retval EFI_SUCCESS The thunk was created successfully.
51 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
52 aligned.
53 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
54 Thunk.
55 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
56
57 **/
58 EFI_STATUS
59 EbcCreateThunks (
60 IN EFI_HANDLE ImageHandle,
61 IN VOID *EbcEntryPoint,
62 OUT VOID **Thunk,
63 IN UINT32 Flags
64 );
65
66 /**
67 Add a thunk to our list of thunks for a given image handle.
68 Also flush the instruction cache since we've written thunk code
69 to memory that will be executed eventually.
70
71 @param ImageHandle The image handle to which the thunk is tied.
72 @param ThunkBuffer The buffer that has been created/allocated.
73 @param ThunkSize The size of the thunk memory allocated.
74
75 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
76 @retval EFI_SUCCESS The function completed successfully.
77
78 **/
79 EFI_STATUS
80 EbcAddImageThunk (
81 IN EFI_HANDLE ImageHandle,
82 IN VOID *ThunkBuffer,
83 IN UINT32 ThunkSize
84 );
85
86 //
87 // Define a constant of how often to call the debugger periodic callback
88 // function.
89 //
90 #define EFI_TIMER_UNIT_1MS (1000 * 10)
91 #define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)
92 #define STACK_POOL_SIZE (1024 * 1020)
93 #define MAX_STACK_NUM 4
94
95 //
96 // External low level functions that are native-processor dependent
97 //
98 /**
99 The VM thunk code stuffs an EBC entry point into a processor
100 register. Since we can't use inline assembly to get it from
101 the interpreter C code, stuff it into the return value
102 register and return.
103
104 @return The contents of the register in which the entry point is passed.
105
106 **/
107 UINTN
108 EFIAPI
109 EbcLLGetEbcEntryPoint (
110 VOID
111 );
112
113 /**
114 This function is called to execute an EBC CALLEX instruction.
115 This instruction requires that we thunk out to external native
116 code. For x64, we switch stacks, copy the arguments to the stack
117 and jump to the specified function.
118 On return, we restore the stack pointer to its original location.
119 Destroys no working registers.
120
121 @param CallAddr The function address.
122 @param EbcSp The new EBC stack pointer.
123 @param FramePtr The frame pointer.
124
125 @return The unmodified value returned by the native code.
126
127 **/
128 INT64
129 EFIAPI
130 EbcLLCALLEXNative (
131 IN UINTN CallAddr,
132 IN UINTN EbcSp,
133 IN VOID *FramePtr
134 );
135
136 /**
137 This function is called to execute an EBC CALLEX instruction.
138 The function check the callee's content to see whether it is common native
139 code or a thunk to another piece of EBC code.
140 If the callee is common native code, use EbcLLCAllEXASM to manipulate,
141 otherwise, set the VM->IP to target EBC code directly to avoid another VM
142 be startup which cost time and stack space.
143
144 @param VmPtr Pointer to a VM context.
145 @param FuncAddr Callee's address
146 @param NewStackPointer New stack pointer after the call
147 @param FramePtr New frame pointer after the call
148 @param Size The size of call instruction
149
150 **/
151 VOID
152 EbcLLCALLEX (
153 IN VM_CONTEXT *VmPtr,
154 IN UINTN FuncAddr,
155 IN UINTN NewStackPointer,
156 IN VOID *FramePtr,
157 IN UINT8 Size
158 );
159
160 /**
161 Returns the stack index and buffer assosicated with the Handle parameter.
162
163 @param Handle The EFI handle as the index to the EBC stack.
164 @param StackBuffer A pointer to hold the returned stack buffer.
165 @param BufferIndex A pointer to hold the returned stack index.
166
167 @retval EFI_OUT_OF_RESOURCES The Handle parameter does not correspond to any
168 existing EBC stack.
169 @retval EFI_SUCCESS The stack index and buffer were found and
170 returned to the caller.
171
172 **/
173 EFI_STATUS
174 GetEBCStack(
175 IN EFI_HANDLE Handle,
176 OUT VOID **StackBuffer,
177 OUT UINTN *BufferIndex
178 );
179
180 /**
181 Returns from the EBC stack by stack Index.
182
183 @param Index Specifies which EBC stack to return from.
184
185 @retval EFI_SUCCESS The function completed successfully.
186
187 **/
188 EFI_STATUS
189 ReturnEBCStack(
190 IN UINTN Index
191 );
192
193 /**
194 Allocates memory to hold all the EBC stacks.
195
196 @retval EFI_SUCCESS The EBC stacks were allocated successfully.
197 @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.
198
199 **/
200 EFI_STATUS
201 InitEBCStack (
202 VOID
203 );
204
205 /**
206 Free all EBC stacks allocated before.
207
208 @retval EFI_SUCCESS All the EBC stacks were freed.
209
210 **/
211 EFI_STATUS
212 FreeEBCStack(
213 VOID
214 );
215
216 /**
217 Returns from the EBC stack associated with the Handle parameter.
218
219 @param Handle Specifies the EFI handle to find the EBC stack with.
220
221 @retval EFI_SUCCESS The function completed successfully.
222
223 **/
224 EFI_STATUS
225 ReturnEBCStackByHandle(
226 IN EFI_HANDLE Handle
227 );
228
229 typedef struct {
230 EFI_EBC_PROTOCOL *This;
231 VOID *EntryPoint;
232 EFI_HANDLE ImageHandle;
233 VM_CONTEXT VmContext;
234 } EFI_EBC_THUNK_DATA;
235
236 #define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('e', 'b', 'c', 'p')
237
238
239 #define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \
240 CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)
241
242
243 /**
244 Allocates a buffer of type EfiBootServicesCode.
245
246 @param AllocationSize The number of bytes to allocate.
247
248 @return A pointer to the allocated buffer or NULL if allocation fails.
249
250 **/
251 VOID *
252 EFIAPI
253 EbcAllocatePoolForThunk (
254 IN UINTN AllocationSize
255 );
256
257 #endif // #ifndef _EBC_INT_H_