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