Home | History | Annotate | Download | only in EbcDxe
      1 /** @file
      2   Main routines for the EBC interpreter.  Includes the initialization and
      3   main interpreter routines.
      4 
      5 Copyright (c) 2006 - 2011, 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
     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 #include <Protocol/EbcVmTest.h>
     25 #include <Protocol/EbcSimpleDebugger.h>
     26 
     27 #include <Library/BaseLib.h>
     28 #include <Library/DebugLib.h>
     29 #include <Library/UefiDriverEntryPoint.h>
     30 #include <Library/BaseMemoryLib.h>
     31 #include <Library/UefiBootServicesTableLib.h>
     32 #include <Library/MemoryAllocationLib.h>
     33 
     34 extern VM_CONTEXT                    *mVmPtr;
     35 
     36 //
     37 // Flags passed to the internal create-thunks function.
     38 //
     39 #define FLAG_THUNK_ENTRY_POINT  0x01  // thunk for an image entry point
     40 #define FLAG_THUNK_PROTOCOL     0x00  // thunk for an EBC protocol service
     41 //
     42 // Put this value at the bottom of the VM's stack gap so we can check it on
     43 // occasion to make sure the stack has not been corrupted.
     44 //
     45 #define VM_STACK_KEY_VALUE  0xDEADBEEF
     46 
     47 /**
     48   Create thunks for an EBC image entry point, or an EBC protocol service.
     49 
     50   @param  ImageHandle           Image handle for the EBC image. If not null, then
     51                                 we're creating a thunk for an image entry point.
     52   @param  EbcEntryPoint         Address of the EBC code that the thunk is to call
     53   @param  Thunk                 Returned thunk we create here
     54   @param  Flags                 Flags indicating options for creating the thunk
     55 
     56   @retval EFI_SUCCESS           The thunk was created successfully.
     57   @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit
     58                                 aligned.
     59   @retval EFI_OUT_OF_RESOURCES  There is not enough memory to created the EBC
     60                                 Thunk.
     61   @retval EFI_BUFFER_TOO_SMALL  EBC_THUNK_SIZE is not larger enough.
     62 
     63 **/
     64 EFI_STATUS
     65 EbcCreateThunks (
     66   IN EFI_HANDLE           ImageHandle,
     67   IN VOID                 *EbcEntryPoint,
     68   OUT VOID                **Thunk,
     69   IN  UINT32              Flags
     70   );
     71 
     72 /**
     73   Add a thunk to our list of thunks for a given image handle.
     74   Also flush the instruction cache since we've written thunk code
     75   to memory that will be executed eventually.
     76 
     77   @param  ImageHandle            The image handle to which the thunk is tied.
     78   @param  ThunkBuffer            The buffer that has been created/allocated.
     79   @param  ThunkSize              The size of the thunk memory allocated.
     80 
     81   @retval EFI_OUT_OF_RESOURCES   Memory allocation failed.
     82   @retval EFI_SUCCESS            The function completed successfully.
     83 
     84 **/
     85 EFI_STATUS
     86 EbcAddImageThunk (
     87   IN EFI_HANDLE      ImageHandle,
     88   IN VOID            *ThunkBuffer,
     89   IN UINT32          ThunkSize
     90   );
     91 
     92 //
     93 // Define a constant of how often to call the debugger periodic callback
     94 // function.
     95 //
     96 #define EFI_TIMER_UNIT_1MS            (1000 * 10)
     97 #define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)
     98 #define STACK_POOL_SIZE               (1024 * 1020)
     99 #define MAX_STACK_NUM                 4
    100 
    101 //
    102 // External low level functions that are native-processor dependent
    103 //
    104 /**
    105   The VM thunk code stuffs an EBC entry point into a processor
    106   register. Since we can't use inline assembly to get it from
    107   the interpreter C code, stuff it into the return value
    108   register and return.
    109 
    110   @return  The contents of the register in which the entry point is passed.
    111 
    112 **/
    113 UINTN
    114 EFIAPI
    115 EbcLLGetEbcEntryPoint (
    116   VOID
    117   );
    118 
    119 /**
    120   This function is called to execute an EBC CALLEX instruction.
    121   This instruction requires that we thunk out to external native
    122   code. For x64, we switch stacks, copy the arguments to the stack
    123   and jump to the specified function.
    124   On return, we restore the stack pointer to its original location.
    125   Destroys no working registers.
    126 
    127   @param  CallAddr     The function address.
    128   @param  EbcSp        The new EBC stack pointer.
    129   @param  FramePtr     The frame pointer.
    130 
    131   @return The unmodified value returned by the native code.
    132 
    133 **/
    134 INT64
    135 EFIAPI
    136 EbcLLCALLEXNative (
    137   IN UINTN        CallAddr,
    138   IN UINTN        EbcSp,
    139   IN VOID         *FramePtr
    140   );
    141 
    142 /**
    143   This function is called to execute an EBC CALLEX instruction.
    144   The function check the callee's content to see whether it is common native
    145   code or a thunk to another piece of EBC code.
    146   If the callee is common native code, use EbcLLCAllEXASM to manipulate,
    147   otherwise, set the VM->IP to target EBC code directly to avoid another VM
    148   be startup which cost time and stack space.
    149 
    150   @param  VmPtr            Pointer to a VM context.
    151   @param  FuncAddr         Callee's address
    152   @param  NewStackPointer  New stack pointer after the call
    153   @param  FramePtr         New frame pointer after the call
    154   @param  Size             The size of call instruction
    155 
    156 **/
    157 VOID
    158 EbcLLCALLEX (
    159   IN VM_CONTEXT   *VmPtr,
    160   IN UINTN        FuncAddr,
    161   IN UINTN        NewStackPointer,
    162   IN VOID         *FramePtr,
    163   IN UINT8        Size
    164   );
    165 
    166 /**
    167   Returns the stack index and buffer assosicated with the Handle parameter.
    168 
    169   @param  Handle                The EFI handle as the index to the EBC stack.
    170   @param  StackBuffer           A pointer to hold the returned stack buffer.
    171   @param  BufferIndex           A pointer to hold the returned stack index.
    172 
    173   @retval EFI_OUT_OF_RESOURCES  The Handle parameter does not correspond to any
    174                                 existing EBC stack.
    175   @retval EFI_SUCCESS           The stack index and buffer were found and
    176                                 returned to the caller.
    177 
    178 **/
    179 EFI_STATUS
    180 GetEBCStack(
    181   IN  EFI_HANDLE Handle,
    182   OUT VOID       **StackBuffer,
    183   OUT UINTN      *BufferIndex
    184   );
    185 
    186 /**
    187   Returns from the EBC stack by stack Index.
    188 
    189   @param  Index        Specifies which EBC stack to return from.
    190 
    191   @retval EFI_SUCCESS  The function completed successfully.
    192 
    193 **/
    194 EFI_STATUS
    195 ReturnEBCStack(
    196   IN UINTN Index
    197   );
    198 
    199 /**
    200   Allocates memory to hold all the EBC stacks.
    201 
    202   @retval EFI_SUCCESS          The EBC stacks were allocated successfully.
    203   @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.
    204 
    205 **/
    206 EFI_STATUS
    207 InitEBCStack (
    208   VOID
    209   );
    210 
    211 /**
    212   Free all EBC stacks allocated before.
    213 
    214   @retval EFI_SUCCESS   All the EBC stacks were freed.
    215 
    216 **/
    217 EFI_STATUS
    218 FreeEBCStack(
    219   VOID
    220   );
    221 
    222 /**
    223   Returns from the EBC stack associated with the Handle parameter.
    224 
    225   @param  Handle      Specifies the EFI handle to find the EBC stack with.
    226 
    227   @retval EFI_SUCCESS The function completed successfully.
    228 
    229 **/
    230 EFI_STATUS
    231 ReturnEBCStackByHandle(
    232   IN EFI_HANDLE Handle
    233   );
    234 
    235 typedef struct {
    236   EFI_EBC_PROTOCOL  *This;
    237   VOID              *EntryPoint;
    238   EFI_HANDLE        ImageHandle;
    239   VM_CONTEXT        VmContext;
    240 } EFI_EBC_THUNK_DATA;
    241 
    242 #define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('e', 'b', 'c', 'p')
    243 
    244 
    245 #define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \
    246       CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)
    247 
    248 
    249 #endif // #ifndef _EBC_INT_H_
    250