2 Main routines for the EBC interpreter. Includes the initialization and
3 main interpreter routines.
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
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.
22 #include <Protocol/DebugSupport.h>
23 #include <Protocol/Ebc.h>
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>
32 typedef INT64 VM_REGISTER
;
33 typedef UINT8
*VMIP
; // instruction pointer for the VM
34 typedef UINT32 EXCEPTION_FLAGS
;
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
42 VMIP Ip
; // Instruction pointer.
43 UINTN LastException
; //
44 EXCEPTION_FLAGS ExceptionFlags
; // to keep track of exceptions
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
56 VOID
*EntryPoint
; // entry point of EBC image
62 extern VM_CONTEXT
*mVmPtr
;
65 // Bits of exception flags field of VM context
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
72 // Flags passed to the internal create-thunks function.
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
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.
80 #define VM_STACK_KEY_VALUE 0xDEADBEEF
83 Create thunks for an EBC image entry point, or an EBC protocol service.
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
91 @retval EFI_SUCCESS The thunk was created successfully.
92 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
94 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC
96 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.
101 IN EFI_HANDLE ImageHandle
,
102 IN VOID
*EbcEntryPoint
,
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.
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.
117 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
118 @retval EFI_SUCCESS The function completed successfully.
123 IN EFI_HANDLE ImageHandle
,
124 IN VOID
*ThunkBuffer
,
130 // The interpreter calls these when an exception is detected,
131 // or as a periodic callback.
134 The VM interpreter calls this function when an exception is detected.
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
141 @retval EFI_SUCCESS This function completed successfully.
145 EbcDebugSignalException (
146 IN EFI_EXCEPTION_TYPE ExceptionType
,
147 IN EXCEPTION_FLAGS ExceptionFlags
,
153 // Define a constant of how often to call the debugger periodic callback
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
162 // External low level functions that are native-processor dependent
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
170 @return The contents of the register in which the entry point is passed.
175 EbcLLGetEbcEntryPoint (
181 Returns the caller's value of the stack pointer.
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.
186 @return The current value of the stack pointer for the caller.
191 EbcLLGetStackPointer (
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.
204 @param CallAddr The function address.
205 @param EbcSp The new EBC stack pointer.
206 @param FramePtr The frame pointer.
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.
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
235 IN VM_CONTEXT
*VmPtr
,
237 IN UINTN NewStackPointer
,
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.
249 @return The unmodified value returned by the native code.
254 EbcLLGetReturnValue (
260 Returns the stack index and buffer assosicated with the Handle parameter.
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.
266 @retval EFI_OUT_OF_RESOURCES The Handle parameter does not correspond to any
268 @retval EFI_SUCCESS The stack index and buffer were found and
269 returned to the caller.
274 IN EFI_HANDLE Handle
,
275 OUT VOID
**StackBuffer
,
276 OUT UINTN
*BufferIndex
280 Returns from the EBC stack by stack Index.
282 @param Index Specifies which EBC stack to return from.
284 @retval EFI_SUCCESS The function completed successfully.
293 Allocates memory to hold all the EBC stacks.
295 @retval EFI_SUCCESS The EBC stacks were allocated successfully.
296 @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.
305 Free all EBC stacks allocated before.
307 @retval EFI_SUCCESS All the EBC stacks were freed.
316 Returns from the EBC stack associated with the Handle parameter.
318 @param Handle Specifies the EFI handle to find the EBC stack with.
320 @retval EFI_SUCCESS The function completed successfully.
324 ReturnEBCStackByHandle(
328 // Defines for a simple EBC debugger interface
330 typedef struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
;
332 #define EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID \
334 0x2a72d11e, 0x7376, 0x40f6, { 0x9c, 0x68, 0x23, 0xfa, 0x2f, 0xe3, 0x63, 0xf1 } \
339 (*EBC_DEBUGGER_SIGNAL_EXCEPTION
) (
340 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
* This
,
341 IN VM_CONTEXT
* VmPtr
,
342 IN EFI_EXCEPTION_TYPE ExceptionType
347 (*EBC_DEBUGGER_DEBUG
) (
348 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
* This
,
349 IN VM_CONTEXT
* VmPtr
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
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.
369 (*EBC_DEBUGGER_CONFIGURE
) (
370 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
* This
,
376 // Prototype for the actual EBC debug support protocol interface
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
;
386 EFI_EBC_PROTOCOL
*This
;
388 EFI_HANDLE ImageHandle
;
389 VM_CONTEXT VmContext
;
390 } EFI_EBC_THUNK_DATA
;
392 #define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE EFI_SIGNATURE_32 ('e', 'b', 'c', 'p')
395 #define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \
396 CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)
399 #endif // #ifndef _EBC_INT_H_