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