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 | #include <Uefi.h>
|
---|
14 |
|
---|
15 | #include <Protocol/DebugSupport.h>
|
---|
16 | #include <Protocol/Ebc.h>
|
---|
17 | #include <Protocol/EbcVmTest.h>
|
---|
18 | #include <Protocol/EbcSimpleDebugger.h>
|
---|
19 | #include <Protocol/PeCoffImageEmulator.h>
|
---|
20 |
|
---|
21 | #include <Library/BaseLib.h>
|
---|
22 | #include <Library/CacheMaintenanceLib.h>
|
---|
23 | #include <Library/DebugLib.h>
|
---|
24 | #include <Library/PeCoffLib.h>
|
---|
25 | #include <Library/UefiDriverEntryPoint.h>
|
---|
26 | #include <Library/BaseMemoryLib.h>
|
---|
27 | #include <Library/UefiBootServicesTableLib.h>
|
---|
28 | #include <Library/MemoryAllocationLib.h>
|
---|
29 |
|
---|
30 | extern VM_CONTEXT *mVmPtr;
|
---|
31 |
|
---|
32 | //
|
---|
33 | // Flags passed to the internal create-thunks function.
|
---|
34 | //
|
---|
35 | #define FLAG_THUNK_ENTRY_POINT 0x01 // thunk for an image entry point
|
---|
36 | #define FLAG_THUNK_PROTOCOL 0x00 // thunk for an EBC protocol service
|
---|
37 | //
|
---|
38 | // Put this value at the bottom of the VM's stack gap so we can check it on
|
---|
39 | // occasion to make sure the stack has not been corrupted.
|
---|
40 | //
|
---|
41 | #define VM_STACK_KEY_VALUE 0xDEADBEEF
|
---|
42 |
|
---|
43 | /**
|
---|
44 | Create thunks for an EBC image entry point, or an EBC protocol service.
|
---|
45 |
|
---|
46 | @param ImageHandle Image handle for the EBC image. If not null, then
|
---|
47 | we're creating a thunk for an image entry point.
|
---|
48 | @param EbcEntryPoint Address of the EBC code that the thunk is to call
|
---|
49 | @param Thunk Returned thunk we create here
|
---|
50 | @param Flags Flags indicating options for creating the thunk
|
---|
51 |
|
---|
52 | @retval EFI_SUCCESS The thunk was created successfully.
|
---|
53 | @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
|
---|
54 | aligned.
|
---|
55 | @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
|
---|
56 | Thunk.
|
---|
57 | @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
|
---|
58 |
|
---|
59 | **/
|
---|
60 | EFI_STATUS
|
---|
61 | EbcCreateThunks (
|
---|
62 | IN EFI_HANDLE ImageHandle,
|
---|
63 | IN VOID *EbcEntryPoint,
|
---|
64 | OUT VOID **Thunk,
|
---|
65 | IN UINT32 Flags
|
---|
66 | );
|
---|
67 |
|
---|
68 | /**
|
---|
69 | Add a thunk to our list of thunks for a given image handle.
|
---|
70 | Also flush the instruction cache since we've written thunk code
|
---|
71 | to memory that will be executed eventually.
|
---|
72 |
|
---|
73 | @param ImageHandle The image handle to which the thunk is tied.
|
---|
74 | @param ThunkBuffer The buffer that has been created/allocated.
|
---|
75 | @param ThunkSize The size of the thunk memory allocated.
|
---|
76 |
|
---|
77 | @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
|
---|
78 | @retval EFI_SUCCESS The function completed successfully.
|
---|
79 |
|
---|
80 | **/
|
---|
81 | EFI_STATUS
|
---|
82 | EbcAddImageThunk (
|
---|
83 | IN EFI_HANDLE ImageHandle,
|
---|
84 | IN VOID *ThunkBuffer,
|
---|
85 | IN UINT32 ThunkSize
|
---|
86 | );
|
---|
87 |
|
---|
88 | //
|
---|
89 | // Define a constant of how often to call the debugger periodic callback
|
---|
90 | // function.
|
---|
91 | //
|
---|
92 | #define EFI_TIMER_UNIT_1MS (1000 * 10)
|
---|
93 | #define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)
|
---|
94 | #define STACK_POOL_SIZE (1024 * 1020)
|
---|
95 | #define MAX_STACK_NUM 4
|
---|
96 |
|
---|
97 | //
|
---|
98 | // External low level functions that are native-processor dependent
|
---|
99 | //
|
---|
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 | #define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \
|
---|
242 | CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)
|
---|
243 |
|
---|
244 | /**
|
---|
245 | Allocates a buffer of type EfiBootServicesCode.
|
---|
246 |
|
---|
247 | @param AllocationSize The number of bytes to allocate.
|
---|
248 |
|
---|
249 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
250 |
|
---|
251 | **/
|
---|
252 | VOID *
|
---|
253 | EFIAPI
|
---|
254 | EbcAllocatePoolForThunk (
|
---|
255 | IN UINTN AllocationSize
|
---|
256 | );
|
---|
257 |
|
---|
258 | #endif // #ifndef _EBC_INT_H_
|
---|