]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/EbcDxe/EbcInt.h
Code scrub DxeIpl, Runtime, DevicePath, FvbServicesLib, DiskIo, Partition, English...
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / EbcInt.h
... / ...
CommitLineData
1/** @file\r
2 Main routines for the EBC interpreter. Includes the initialization and\r
3 main interpreter routines.\r
4\r
5Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
6All rights reserved. This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT 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\r
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
32typedef INT64 VM_REGISTER;\r
33typedef UINT8 *VMIP; // instruction pointer for the VM\r
34typedef UINT32 EXCEPTION_FLAGS;\r
35\r
36typedef 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
62extern 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
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
99EFI_STATUS\r
100EbcCreateThunks (\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
108 Add a thunk to our list of thunks for a given image handle.\r
109 Also flush the instruction cache since we have written thunk code\r
110 to memory that will be executed eventually.\r
111\r
112 @param ImageHandle The image handle to which the thunk is tied.\r
113 @param ThunkBuffer The buffer that has been created/allocated.\r
114 @param ThunkSize The size of the thunk memory allocated.\r
115\r
116 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
117 @retval EFI_SUCCESS The function completed successfully.\r
118\r
119**/\r
120EFI_STATUS\r
121EbcAddImageThunk (\r
122 IN EFI_HANDLE ImageHandle,\r
123 IN VOID *ThunkBuffer,\r
124 IN UINT32 ThunkSize\r
125 );\r
126\r
127//\r
128// The interpreter calls these when an exception is detected,\r
129// or as a periodic callback.\r
130//\r
131/**\r
132 The VM interpreter calls this function when an exception is detected.\r
133\r
134 @param ExceptionType Specifies the processor exception detected.\r
135 @param ExceptionFlags Specifies the exception context.\r
136 @param VmPtr Pointer to a VM context for passing info to the\r
137 EFI debugger.\r
138\r
139 @retval EFI_SUCCESS This function completed successfully.\r
140\r
141**/\r
142EFI_STATUS\r
143EbcDebugSignalException (\r
144 IN EFI_EXCEPTION_TYPE ExceptionType,\r
145 IN EXCEPTION_FLAGS ExceptionFlags,\r
146 IN VM_CONTEXT *VmPtr\r
147 );\r
148\r
149//\r
150// Define a constant of how often to call the debugger periodic callback\r
151// function.\r
152//\r
153#define EFI_TIMER_UNIT_1MS (1000 * 10)\r
154#define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)\r
155#define STACK_POOL_SIZE (1024 * 1020)\r
156#define MAX_STACK_NUM 4\r
157\r
158//\r
159// External low level functions that are native-processor dependent\r
160//\r
161/**\r
162 The VM thunk code stuffs an EBC entry point into a processor\r
163 register. Since we can't use inline assembly to get it from\r
164 the interpreter C code, stuff it into the return value\r
165 register and return.\r
166\r
167 @return The contents of the register in which the entry point is passed.\r
168\r
169**/\r
170UINTN\r
171EFIAPI\r
172EbcLLGetEbcEntryPoint (\r
173 VOID\r
174 );\r
175\r
176/**\r
177 Returns the caller's value of the stack pointer.\r
178\r
179 We adjust it by 4 here because when they called us, the return address\r
180 is put on the stack, thereby lowering it by 4 bytes.\r
181\r
182 @return The current value of the stack pointer for the caller.\r
183\r
184**/\r
185UINTN\r
186EFIAPI\r
187EbcLLGetStackPointer (\r
188 VOID\r
189 );\r
190\r
191/**\r
192 This function is called to execute an EBC CALLEX instruction.\r
193 This instruction requires that we thunk out to external native\r
194 code. For x64, we switch stacks, copy the arguments to the stack\r
195 and jump to the specified function.\r
196 On return, we restore the stack pointer to its original location.\r
197 Destroys no working registers.\r
198\r
199 @param CallAddr The function address.\r
200 @param EbcSp The new EBC stack pointer.\r
201 @param FramePtr The frame pointer.\r
202\r
203**/\r
204VOID\r
205EFIAPI\r
206EbcLLCALLEXNative (\r
207 IN UINTN CallAddr,\r
208 IN UINTN EbcSp,\r
209 IN VOID *FramePtr\r
210 );\r
211\r
212/**\r
213 This function is called to execute an EBC CALLEX instruction.\r
214 The function check the callee's content to see whether it is common native\r
215 code or a thunk to another piece of EBC code.\r
216 If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
217 otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
218 be startup which cost time and stack space.\r
219\r
220 @param VmPtr Pointer to a VM context.\r
221 @param FuncAddr Callee's address\r
222 @param NewStackPointer New stack pointer after the call\r
223 @param FramePtr New frame pointer after the call\r
224 @param Size The size of call instruction\r
225\r
226**/\r
227VOID\r
228EbcLLCALLEX (\r
229 IN VM_CONTEXT *VmPtr,\r
230 IN UINTN FuncAddr,\r
231 IN UINTN NewStackPointer,\r
232 IN VOID *FramePtr,\r
233 IN UINT8 Size\r
234 );\r
235\r
236/**\r
237 When EBC calls native, on return the VM has to stuff the return\r
238 value into a VM register. It's assumed here that the value is still\r
239 in the register, so simply return and the caller should get the\r
240 return result properly.\r
241\r
242 @return The unmodified value returned by the native code.\r
243\r
244**/\r
245INT64\r
246EFIAPI\r
247EbcLLGetReturnValue (\r
248 VOID\r
249 );\r
250\r
251/**\r
252 Returns the stack index and buffer associated with the Handle parameter.\r
253\r
254 @param Handle The EFI handle as the index to the EBC stack.\r
255 @param StackBuffer A pointer to hold the returned stack buffer.\r
256 @param BufferIndex A pointer to hold the returned stack index.\r
257\r
258 @retval EFI_OUT_OF_RESOURCES The Handle parameter does not correspond to any\r
259 existing EBC stack.\r
260 @retval EFI_SUCCESS The stack index and buffer were found and\r
261 returned to the caller.\r
262\r
263**/\r
264EFI_STATUS\r
265GetEBCStack(\r
266 IN EFI_HANDLE Handle,\r
267 OUT VOID **StackBuffer,\r
268 OUT UINTN *BufferIndex\r
269 );\r
270\r
271/**\r
272 Returns from the EBC stack by stack Index.\r
273\r
274 @param Index Specifies which EBC stack to return from.\r
275\r
276 @retval EFI_SUCCESS The function completed successfully.\r
277\r
278**/\r
279EFI_STATUS\r
280ReturnEBCStack(\r
281 IN UINTN Index\r
282 );\r
283\r
284/**\r
285 Allocates memory to hold all the EBC stacks.\r
286\r
287 @retval EFI_SUCCESS The EBC stacks were allocated successfully.\r
288 @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.\r
289\r
290**/\r
291EFI_STATUS\r
292InitEBCStack (\r
293 VOID\r
294 );\r
295\r
296/**\r
297 Free all EBC stacks allocated before.\r
298\r
299 @retval EFI_SUCCESS All the EBC stacks were freed.\r
300\r
301**/\r
302EFI_STATUS\r
303FreeEBCStack(\r
304 VOID\r
305 );\r
306\r
307/**\r
308 Returns from the EBC stack associated with the Handle parameter.\r
309\r
310 @param Handle Specifies the EFI handle to find the EBC stack with.\r
311\r
312 @retval EFI_SUCCESS The function completed successfully.\r
313\r
314**/\r
315EFI_STATUS\r
316ReturnEBCStackByHandle(\r
317 IN EFI_HANDLE Handle\r
318 );\r
319\r
320\r
321//\r
322// Defines for a simple EBC debugger interface\r
323//\r
324typedef struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL;\r
325\r
326#define EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID \\r
327 { \\r
328 0x2a72d11e, 0x7376, 0x40f6, { 0x9c, 0x68, 0x23, 0xfa, 0x2f, 0xe3, 0x63, 0xf1 } \\r
329 }\r
330\r
331typedef\r
332EFI_STATUS\r
333(*EBC_DEBUGGER_SIGNAL_EXCEPTION) (\r
334 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL *This,\r
335 IN VM_CONTEXT *VmPtr,\r
336 IN EFI_EXCEPTION_TYPE ExceptionType\r
337 );\r
338\r
339typedef\r
340VOID\r
341(*EBC_DEBUGGER_DEBUG) (\r
342 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL *This,\r
343 IN VM_CONTEXT *VmPtr\r
344 );\r
345\r
346typedef\r
347UINT32\r
348(*EBC_DEBUGGER_DASM) (\r
349 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL *This,\r
350 IN VM_CONTEXT *VmPtr,\r
351 IN UINT16 *DasmString OPTIONAL,\r
352 IN UINT32 DasmStringSize\r
353 );\r
354\r
355//\r
356// This interface allows you to configure the EBC debug support\r
357// driver. For example, turn on or off saving and printing of\r
358// delta VM even if called. Or to even disable the entire interface,\r
359// in which case all functions become no-ops.\r
360//\r
361typedef\r
362EFI_STATUS\r
363(*EBC_DEBUGGER_CONFIGURE) (\r
364 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL *This,\r
365 IN UINT32 ConfigId,\r
366 IN UINTN ConfigValue\r
367 );\r
368\r
369//\r
370// Prototype for the actual EBC debug support protocol interface\r
371//\r
372struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL {\r
373 EBC_DEBUGGER_DEBUG Debugger;\r
374 EBC_DEBUGGER_SIGNAL_EXCEPTION SignalException;\r
375 EBC_DEBUGGER_DASM Dasm;\r
376 EBC_DEBUGGER_CONFIGURE Configure;\r
377};\r
378\r
379typedef struct {\r
380 EFI_EBC_PROTOCOL *This;\r
381 VOID *EntryPoint;\r
382 EFI_HANDLE ImageHandle;\r
383 VM_CONTEXT VmContext;\r
384} EFI_EBC_THUNK_DATA;\r
385\r
386#define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('e', 'b', 'c', 'p')\r
387\r
388\r
389#define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \\r
390 CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)\r
391\r
392\r
393#endif // #ifndef _EBC_INT_H_\r