2 Main routines for the EBC interpreter. Includes the initialization and
3 main interpreter routines.
5 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
6 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 Gpr
[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
,
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.
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.
116 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
117 @retval EFI_SUCCESS The function completed successfully.
122 IN EFI_HANDLE ImageHandle
,
123 IN VOID
*ThunkBuffer
,
128 // The interpreter calls these when an exception is detected,
129 // or as a periodic callback.
132 The VM interpreter calls this function when an exception is detected.
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
139 @retval EFI_SUCCESS This function completed successfully.
143 EbcDebugSignalException (
144 IN EFI_EXCEPTION_TYPE ExceptionType
,
145 IN EXCEPTION_FLAGS ExceptionFlags
,
150 // Define a constant of how often to call the debugger periodic callback
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
159 // External low level functions that are native-processor dependent
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
167 @return The contents of the register in which the entry point is passed.
172 EbcLLGetEbcEntryPoint (
177 Returns the caller's value of the stack pointer.
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.
182 @return The current value of the stack pointer for the caller.
187 EbcLLGetStackPointer (
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.
199 @param CallAddr The function address.
200 @param EbcSp The new EBC stack pointer.
201 @param FramePtr The frame pointer.
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.
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
229 IN VM_CONTEXT
*VmPtr
,
231 IN UINTN NewStackPointer
,
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.
242 @return The unmodified value returned by the native code.
247 EbcLLGetReturnValue (
252 Returns the stack index and buffer assosicated with the Handle parameter.
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.
258 @retval EFI_OUT_OF_RESOURCES The Handle parameter does not correspond to any
260 @retval EFI_SUCCESS The stack index and buffer were found and
261 returned to the caller.
266 IN EFI_HANDLE Handle
,
267 OUT VOID
**StackBuffer
,
268 OUT UINTN
*BufferIndex
272 Returns from the EBC stack by stack Index.
274 @param Index Specifies which EBC stack to return from.
276 @retval EFI_SUCCESS The function completed successfully.
285 Allocates memory to hold all the EBC stacks.
287 @retval EFI_SUCCESS The EBC stacks were allocated successfully.
288 @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.
297 Free all EBC stacks allocated before.
299 @retval EFI_SUCCESS All the EBC stacks were freed.
308 Returns from the EBC stack associated with the Handle parameter.
310 @param Handle Specifies the EFI handle to find the EBC stack with.
312 @retval EFI_SUCCESS The function completed successfully.
316 ReturnEBCStackByHandle(
322 // Defines for a simple EBC debugger interface
324 typedef struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
;
326 #define EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID \
328 0x2a72d11e, 0x7376, 0x40f6, { 0x9c, 0x68, 0x23, 0xfa, 0x2f, 0xe3, 0x63, 0xf1 } \
333 (*EBC_DEBUGGER_SIGNAL_EXCEPTION
) (
334 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
*This
,
335 IN VM_CONTEXT
*VmPtr
,
336 IN EFI_EXCEPTION_TYPE ExceptionType
341 (*EBC_DEBUGGER_DEBUG
) (
342 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
*This
,
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
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.
363 (*EBC_DEBUGGER_CONFIGURE
) (
364 IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL
*This
,
370 // Prototype for the actual EBC debug support protocol interface
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
;
380 EFI_EBC_PROTOCOL
*This
;
382 EFI_HANDLE ImageHandle
;
383 VM_CONTEXT VmContext
;
384 } EFI_EBC_THUNK_DATA
;
386 #define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('e', 'b', 'c', 'p')
389 #define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \
390 CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)
393 #endif // #ifndef _EBC_INT_H_