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