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