]>
Commit | Line | Data |
---|---|---|
fb0b259e | 1 | /** @file\r |
2 | Main routines for the EBC interpreter. Includes the initialization and\r | |
3 | main interpreter routines.\r | |
53c71d09 | 4 | \r |
fb0b259e | 5 | Copyright (c) 2006, Intel Corporation\r |
6 | All rights reserved. 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 | |
53c71d09 | 10 | \r |
fb0b259e | 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 | |
53c71d09 | 13 | \r |
fb0b259e | 14 | **/\r |
53c71d09 | 15 | \r |
16 | #ifndef _EBC_INT_H_\r | |
17 | #define _EBC_INT_H_\r | |
18 | \r | |
ed7748fe | 19 | \r |
53c71d09 | 20 | #include <PiDxe.h>\r |
ed7748fe | 21 | \r |
53c71d09 | 22 | #include <Protocol/DebugSupport.h>\r |
23 | #include <Protocol/Ebc.h>\r | |
ed7748fe | 24 | \r |
53c71d09 | 25 | #include <Library/BaseLib.h>\r |
26 | #include <Library/DebugLib.h>\r | |
27 | #include <Library/UefiDriverEntryPoint.h>\r | |
28 | #include <Library/BaseMemoryLib.h>\r | |
29 | #include <Library/UefiBootServicesTableLib.h>\r | |
30 | #include <Library/MemoryAllocationLib.h>\r | |
31 | \r | |
32 | typedef INT64 VM_REGISTER;\r | |
33 | typedef UINT8 *VMIP; // instruction pointer for the VM\r | |
34 | typedef UINT32 EXCEPTION_FLAGS;\r | |
35 | \r | |
36 | typedef struct {\r | |
37 | VM_REGISTER R[8]; // General purpose registers.\r | |
38 | UINT64 Flags; // Flags register:\r | |
39 | // 0 Set to 1 if the result of the last compare was true\r | |
40 | // 1 Set to 1 if stepping\r | |
41 | // 2..63 Reserved.\r | |
42 | VMIP Ip; // Instruction pointer.\r | |
43 | UINTN LastException; //\r | |
44 | EXCEPTION_FLAGS ExceptionFlags; // to keep track of exceptions\r | |
45 | UINT32 StopFlags;\r | |
46 | UINT32 CompilerVersion; // via break(6)\r | |
47 | UINTN HighStackBottom; // bottom of the upper stack\r | |
48 | UINTN LowStackTop; // top of the lower stack\r | |
49 | UINT64 StackRetAddr; // location of final return address on stack\r | |
50 | UINTN *StackMagicPtr; // pointer to magic value on stack to detect corruption\r | |
51 | EFI_HANDLE ImageHandle; // for this EBC driver\r | |
52 | EFI_SYSTEM_TABLE *SystemTable; // for debugging only\r | |
53 | UINTN LastAddrConverted; // for debug\r | |
54 | UINTN LastAddrConvertedValue; // for debug\r | |
55 | VOID *FramePtr;\r | |
56 | VOID *EntryPoint; // entry point of EBC image\r | |
57 | UINTN ImageBase;\r | |
58 | VOID *StackPool;\r | |
59 | VOID *StackTop;\r | |
60 | } VM_CONTEXT;\r | |
61 | \r | |
62 | extern VM_CONTEXT *mVmPtr;\r | |
63 | \r | |
64 | //\r | |
65 | // Bits of exception flags field of VM context\r | |
66 | //\r | |
67 | #define EXCEPTION_FLAG_FATAL 0x80000000 // can't continue\r | |
68 | #define EXCEPTION_FLAG_ERROR 0x40000000 // bad, but try to continue\r | |
69 | #define EXCEPTION_FLAG_WARNING 0x20000000 // harmless problem\r | |
70 | #define EXCEPTION_FLAG_NONE 0x00000000 // for normal return\r | |
71 | //\r | |
72 | // Flags passed to the internal create-thunks function.\r | |
73 | //\r | |
74 | #define FLAG_THUNK_ENTRY_POINT 0x01 // thunk for an image entry point\r | |
75 | #define FLAG_THUNK_PROTOCOL 0x00 // thunk for an EBC protocol service\r | |
76 | //\r | |
77 | // Put this value at the bottom of the VM's stack gap so we can check it on\r | |
78 | // occasion to make sure the stack has not been corrupted.\r | |
79 | //\r | |
80 | #define VM_STACK_KEY_VALUE 0xDEADBEEF\r | |
81 | \r | |
8e3bc754 | 82 | /**\r |
83 | Create thunks for an EBC image entry point, or an EBC protocol service.\r | |
84 | \r | |
85 | @param ImageHandle Image handle for the EBC image. If not null, then\r | |
86 | we're creating a thunk for an image entry point.\r | |
87 | @param EbcEntryPoint Address of the EBC code that the thunk is to call\r | |
88 | @param Thunk Returned thunk we create here\r | |
89 | @param Flags Flags indicating options for creating the thunk\r | |
90 | \r | |
91 | @retval EFI_SUCCESS The thunk was created successfully.\r | |
92 | @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit\r | |
93 | aligned.\r | |
94 | @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC\r | |
95 | Thunk.\r | |
96 | @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.\r | |
97 | \r | |
98 | **/\r | |
53c71d09 | 99 | EFI_STATUS\r |
100 | EbcCreateThunks (\r | |
101 | IN EFI_HANDLE ImageHandle,\r | |
102 | IN VOID *EbcEntryPoint,\r | |
103 | OUT VOID **Thunk,\r | |
104 | IN UINT32 Flags\r | |
105 | )\r | |
106 | ;\r | |
107 | \r | |
8e3bc754 | 108 | /**\r |
109 | Add a thunk to our list of thunks for a given image handle.\r | |
110 | Also flush the instruction cache since we've written thunk code\r | |
111 | to memory that will be executed eventually.\r | |
112 | \r | |
113 | @param ImageHandle The image handle to which the thunk is tied.\r | |
114 | @param ThunkBuffer The buffer that has been created/allocated.\r | |
115 | @param ThunkSize The size of the thunk memory allocated.\r | |
116 | \r | |
117 | @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r | |
118 | @retval EFI_SUCCESS The function completed successfully.\r | |
119 | \r | |
120 | **/\r | |
53c71d09 | 121 | EFI_STATUS\r |
122 | EbcAddImageThunk (\r | |
123 | IN EFI_HANDLE ImageHandle,\r | |
124 | IN VOID *ThunkBuffer,\r | |
125 | IN UINT32 ThunkSize\r | |
126 | )\r | |
127 | ;\r | |
128 | \r | |
129 | //\r | |
130 | // The interpreter calls these when an exception is detected,\r | |
131 | // or as a periodic callback.\r | |
132 | //\r | |
8e3bc754 | 133 | /**\r |
134 | The VM interpreter calls this function when an exception is detected.\r | |
135 | \r | |
136 | @param ExceptionType Specifies the processor exception detected.\r | |
137 | @param ExceptionFlags Specifies the exception context. \r | |
138 | @param VmPtr Pointer to a VM context for passing info to the\r | |
139 | EFI debugger.\r | |
140 | \r | |
141 | @retval EFI_SUCCESS This function completed successfully.\r | |
142 | \r | |
143 | **/\r | |
53c71d09 | 144 | EFI_STATUS\r |
145 | EbcDebugSignalException (\r | |
146 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
147 | IN EXCEPTION_FLAGS ExceptionFlags,\r | |
148 | IN VM_CONTEXT *VmPtr\r | |
149 | )\r | |
150 | ;\r | |
151 | \r | |
152 | //\r | |
153 | // Define a constant of how often to call the debugger periodic callback\r | |
154 | // function.\r | |
155 | //\r | |
156 | #define EFI_TIMER_UNIT_1MS (1000 * 10)\r | |
157 | #define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)\r | |
158 | #define STACK_POOL_SIZE (1024 * 1020)\r | |
159 | #define MAX_STACK_NUM 4\r | |
160 | \r | |
53c71d09 | 161 | //\r |
162 | // External low level functions that are native-processor dependent\r | |
8e3bc754 | 163 | // \r |
164 | /** \r | |
165 | The VM thunk code stuffs an EBC entry point into a processor \r | |
166 | register. Since we can't use inline assembly to get it from\r | |
167 | the interpreter C code, stuff it into the return value\r | |
168 | register and return.\r | |
169 | \r | |
170 | @return The contents of the register in which the entry point is passed.\r | |
171 | \r | |
172 | **/ \r | |
53c71d09 | 173 | UINTN\r |
8e3bc754 | 174 | EFIAPI\r |
53c71d09 | 175 | EbcLLGetEbcEntryPoint (\r |
176 | VOID\r | |
177 | )\r | |
178 | ;\r | |
179 | \r | |
8e3bc754 | 180 | /**\r |
181 | Returns the caller's value of the stack pointer.\r | |
182 | \r | |
183 | We adjust it by 4 here because when they called us, the return address\r | |
184 | is put on the stack, thereby lowering it by 4 bytes.\r | |
185 | \r | |
186 | @return The current value of the stack pointer for the caller. \r | |
187 | \r | |
188 | **/\r | |
53c71d09 | 189 | UINTN\r |
8e3bc754 | 190 | EFIAPI\r |
53c71d09 | 191 | EbcLLGetStackPointer (\r |
192 | VOID\r | |
193 | )\r | |
194 | ;\r | |
195 | \r | |
8e3bc754 | 196 | /**\r |
197 | This function is called to execute an EBC CALLEX instruction.\r | |
198 | This instruction requires that we thunk out to external native\r | |
199 | code. For x64, we switch stacks, copy the arguments to the stack \r | |
200 | and jump to the specified function.\r | |
201 | On return, we restore the stack pointer to its original location.\r | |
202 | Destroys no working registers.\r | |
203 | \r | |
204 | @param CallAddr The function address.\r | |
205 | @param EbcSp The new EBC stack pointer.\r | |
206 | @param FramePtr The frame pointer.\r | |
207 | \r | |
208 | **/\r | |
53c71d09 | 209 | VOID\r |
8e3bc754 | 210 | EFIAPI\r |
53c71d09 | 211 | EbcLLCALLEXNative (\r |
212 | IN UINTN CallAddr,\r | |
213 | IN UINTN EbcSp,\r | |
214 | IN VOID *FramePtr\r | |
215 | )\r | |
216 | ;\r | |
217 | \r | |
8e3bc754 | 218 | /**\r |
219 | This function is called to execute an EBC CALLEX instruction.\r | |
220 | The function check the callee's content to see whether it is common native\r | |
221 | code or a thunk to another piece of EBC code.\r | |
222 | If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r | |
223 | otherwise, set the VM->IP to target EBC code directly to avoid another VM\r | |
224 | be startup which cost time and stack space.\r | |
225 | \r | |
226 | @param VmPtr Pointer to a VM context.\r | |
227 | @param FuncAddr Callee's address\r | |
228 | @param NewStackPointer New stack pointer after the call\r | |
229 | @param FramePtr New frame pointer after the call\r | |
230 | @param Size The size of call instruction\r | |
231 | \r | |
232 | **/\r | |
53c71d09 | 233 | VOID\r |
234 | EbcLLCALLEX (\r | |
235 | IN VM_CONTEXT *VmPtr,\r | |
8e3bc754 | 236 | IN UINTN FuncAddr,\r |
237 | IN UINTN NewStackPointer,\r | |
53c71d09 | 238 | IN VOID *FramePtr,\r |
239 | IN UINT8 Size\r | |
240 | )\r | |
241 | ;\r | |
242 | \r | |
8e3bc754 | 243 | /**\r |
244 | When EBC calls native, on return the VM has to stuff the return\r | |
245 | value into a VM register. It's assumed here that the value is still\r | |
246 | in the register, so simply return and the caller should get the\r | |
247 | return result properly.\r | |
248 | \r | |
249 | @return The unmodified value returned by the native code. \r | |
250 | \r | |
251 | **/\r | |
53c71d09 | 252 | INT64\r |
8e3bc754 | 253 | EFIAPI\r |
53c71d09 | 254 | EbcLLGetReturnValue (\r |
255 | VOID\r | |
256 | )\r | |
257 | ;\r | |
258 | \r | |
8e3bc754 | 259 | /**\r |
260 | Returns the stack index and buffer assosicated with the Handle parameter.\r | |
261 | \r | |
262 | @param Handle The EFI handle as the index to the EBC stack. \r | |
263 | @param StackBuffer A pointer to hold the returned stack buffer.\r | |
264 | @param BufferIndex A pointer to hold the returned stack index.\r | |
265 | \r | |
266 | @retval EFI_OUT_OF_RESOURCES The Handle parameter does not correspond to any\r | |
267 | existing EBC stack.\r | |
268 | @retval EFI_SUCCESS The stack index and buffer were found and\r | |
269 | returned to the caller.\r | |
270 | \r | |
271 | **/\r | |
53c71d09 | 272 | EFI_STATUS\r |
273 | GetEBCStack(\r | |
8e3bc754 | 274 | IN EFI_HANDLE Handle,\r |
275 | OUT VOID **StackBuffer,\r | |
276 | OUT UINTN *BufferIndex\r | |
53c71d09 | 277 | );\r |
278 | \r | |
8e3bc754 | 279 | /**\r |
280 | Returns from the EBC stack by stack Index. \r | |
281 | \r | |
282 | @param Index Specifies which EBC stack to return from.\r | |
283 | \r | |
284 | @retval EFI_SUCCESS The function completed successfully.\r | |
285 | \r | |
286 | **/\r | |
53c71d09 | 287 | EFI_STATUS\r |
288 | ReturnEBCStack(\r | |
8e3bc754 | 289 | IN UINTN Index\r |
53c71d09 | 290 | );\r |
291 | \r | |
8e3bc754 | 292 | /**\r |
293 | Allocates memory to hold all the EBC stacks.\r | |
294 | \r | |
295 | @retval EFI_SUCCESS The EBC stacks were allocated successfully. \r | |
296 | @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.\r | |
297 | \r | |
298 | **/\r | |
53c71d09 | 299 | EFI_STATUS\r |
300 | InitEBCStack (\r | |
301 | VOID\r | |
302 | );\r | |
303 | \r | |
8e3bc754 | 304 | /**\r |
305 | Free all EBC stacks allocated before.\r | |
306 | \r | |
307 | @retval EFI_SUCCESS All the EBC stacks were freed.\r | |
308 | \r | |
309 | **/\r | |
53c71d09 | 310 | EFI_STATUS\r |
311 | FreeEBCStack(\r | |
312 | VOID\r | |
313 | );\r | |
314 | \r | |
8e3bc754 | 315 | /**\r |
316 | Returns from the EBC stack associated with the Handle parameter. \r | |
317 | \r | |
318 | @param Handle Specifies the EFI handle to find the EBC stack with.\r | |
319 | \r | |
320 | @retval EFI_SUCCESS The function completed successfully.\r | |
321 | \r | |
322 | **/\r | |
53c71d09 | 323 | EFI_STATUS\r |
324 | ReturnEBCStackByHandle(\r | |
8e3bc754 | 325 | IN EFI_HANDLE Handle\r |
53c71d09 | 326 | );\r |
327 | //\r | |
328 | // Defines for a simple EBC debugger interface\r | |
329 | //\r | |
330 | typedef struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL;\r | |
331 | \r | |
332 | #define EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID \\r | |
333 | { \\r | |
334 | 0x2a72d11e, 0x7376, 0x40f6, { 0x9c, 0x68, 0x23, 0xfa, 0x2f, 0xe3, 0x63, 0xf1 } \\r | |
335 | }\r | |
336 | \r | |
337 | typedef\r | |
338 | EFI_STATUS\r | |
339 | (*EBC_DEBUGGER_SIGNAL_EXCEPTION) (\r | |
340 | IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL * This,\r | |
341 | IN VM_CONTEXT * VmPtr,\r | |
342 | IN EFI_EXCEPTION_TYPE ExceptionType\r | |
343 | );\r | |
344 | \r | |
345 | typedef\r | |
346 | VOID\r | |
347 | (*EBC_DEBUGGER_DEBUG) (\r | |
348 | IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL * This,\r | |
349 | IN VM_CONTEXT * VmPtr\r | |
350 | );\r | |
351 | \r | |
352 | typedef\r | |
353 | UINT32\r | |
354 | (*EBC_DEBUGGER_DASM) (\r | |
355 | IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL * This,\r | |
356 | IN VM_CONTEXT * VmPtr,\r | |
357 | IN UINT16 *DasmString OPTIONAL,\r | |
358 | IN UINT32 DasmStringSize\r | |
359 | );\r | |
360 | \r | |
361 | //\r | |
362 | // This interface allows you to configure the EBC debug support\r | |
363 | // driver. For example, turn on or off saving and printing of\r | |
364 | // delta VM even if called. Or to even disable the entire interface,\r | |
365 | // in which case all functions become no-ops.\r | |
366 | //\r | |
367 | typedef\r | |
368 | EFI_STATUS\r | |
369 | (*EBC_DEBUGGER_CONFIGURE) (\r | |
370 | IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL * This,\r | |
371 | IN UINT32 ConfigId,\r | |
372 | IN UINTN ConfigValue\r | |
373 | );\r | |
374 | \r | |
375 | //\r | |
376 | // Prototype for the actual EBC debug support protocol interface\r | |
377 | //\r | |
378 | struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL {\r | |
379 | EBC_DEBUGGER_DEBUG Debugger;\r | |
380 | EBC_DEBUGGER_SIGNAL_EXCEPTION SignalException;\r | |
381 | EBC_DEBUGGER_DASM Dasm;\r | |
382 | EBC_DEBUGGER_CONFIGURE Configure;\r | |
383 | };\r | |
384 | \r | |
385 | typedef struct {\r | |
386 | EFI_EBC_PROTOCOL *This;\r | |
387 | VOID *EntryPoint;\r | |
388 | EFI_HANDLE ImageHandle;\r | |
389 | VM_CONTEXT VmContext;\r | |
390 | } EFI_EBC_THUNK_DATA;\r | |
391 | \r | |
392 | #define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE EFI_SIGNATURE_32 ('e', 'b', 'c', 'p')\r | |
393 | \r | |
53c71d09 | 394 | \r |
395 | #define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \\r | |
396 | CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)\r | |
397 | \r | |
398 | \r | |
399 | #endif // #ifndef _EBC_INT_H_\r |