]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/EbcDxe/EbcInt.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
60c93673 13#include <Uefi.h>\r
ed7748fe 14\r
53c71d09 15#include <Protocol/DebugSupport.h>\r
16#include <Protocol/Ebc.h>\r
c8ad2d7a
LG
17#include <Protocol/EbcVmTest.h>\r
18#include <Protocol/EbcSimpleDebugger.h>\r
095fcfc6 19#include <Protocol/PeCoffImageEmulator.h>\r
ed7748fe 20\r
53c71d09 21#include <Library/BaseLib.h>\r
095fcfc6 22#include <Library/CacheMaintenanceLib.h>\r
53c71d09 23#include <Library/DebugLib.h>\r
095fcfc6 24#include <Library/PeCoffLib.h>\r
53c71d09 25#include <Library/UefiDriverEntryPoint.h>\r
26#include <Library/BaseMemoryLib.h>\r
27#include <Library/UefiBootServicesTableLib.h>\r
28#include <Library/MemoryAllocationLib.h>\r
29\r
1436aea4 30extern VM_CONTEXT *mVmPtr;\r
53c71d09 31\r
53c71d09 32//\r
33// Flags passed to the internal create-thunks function.\r
34//\r
35#define FLAG_THUNK_ENTRY_POINT 0x01 // thunk for an image entry point\r
36#define FLAG_THUNK_PROTOCOL 0x00 // thunk for an EBC protocol service\r
37//\r
38// Put this value at the bottom of the VM's stack gap so we can check it on\r
39// occasion to make sure the stack has not been corrupted.\r
40//\r
41#define VM_STACK_KEY_VALUE 0xDEADBEEF\r
42\r
8e3bc754 43/**\r
44 Create thunks for an EBC image entry point, or an EBC protocol service.\r
45\r
46 @param ImageHandle Image handle for the EBC image. If not null, then\r
47 we're creating a thunk for an image entry point.\r
48 @param EbcEntryPoint Address of the EBC code that the thunk is to call\r
49 @param Thunk Returned thunk we create here\r
50 @param Flags Flags indicating options for creating the thunk\r
51\r
52 @retval EFI_SUCCESS The thunk was created successfully.\r
53 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit\r
54 aligned.\r
55 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC\r
56 Thunk.\r
57 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.\r
58\r
59**/\r
53c71d09 60EFI_STATUS\r
61EbcCreateThunks (\r
1436aea4
MK
62 IN EFI_HANDLE ImageHandle,\r
63 IN VOID *EbcEntryPoint,\r
64 OUT VOID **Thunk,\r
65 IN UINT32 Flags\r
ea7cb08c 66 );\r
53c71d09 67\r
8e3bc754 68/**\r
69 Add a thunk to our list of thunks for a given image handle.\r
ead7e7dc 70 Also flush the instruction cache since we've written thunk code\r
8e3bc754 71 to memory that will be executed eventually.\r
72\r
73 @param ImageHandle The image handle to which the thunk is tied.\r
74 @param ThunkBuffer The buffer that has been created/allocated.\r
75 @param ThunkSize The size of the thunk memory allocated.\r
76\r
77 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
78 @retval EFI_SUCCESS The function completed successfully.\r
79\r
80**/\r
53c71d09 81EFI_STATUS\r
82EbcAddImageThunk (\r
1436aea4
MK
83 IN EFI_HANDLE ImageHandle,\r
84 IN VOID *ThunkBuffer,\r
85 IN UINT32 ThunkSize\r
ea7cb08c 86 );\r
53c71d09 87\r
53c71d09 88//\r
89// Define a constant of how often to call the debugger periodic callback\r
90// function.\r
91//\r
1436aea4
MK
92#define EFI_TIMER_UNIT_1MS (1000 * 10)\r
93#define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)\r
94#define STACK_POOL_SIZE (1024 * 1020)\r
95#define MAX_STACK_NUM 4\r
53c71d09 96\r
53c71d09 97//\r
98// External low level functions that are native-processor dependent\r
34e4e297 99//\r
1436aea4 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
1436aea4
MK
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
1436aea4
MK
156 IN VM_CONTEXT *VmPtr,\r
157 IN UINTN FuncAddr,\r
158 IN UINTN NewStackPointer,\r
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
1436aea4
MK
177GetEBCStack (\r
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
1436aea4
MK
192ReturnEBCStack (\r
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
1436aea4 215FreeEBCStack (\r
53c71d09 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
1436aea4
MK
228ReturnEBCStackByHandle (\r
229 IN EFI_HANDLE Handle\r
53c71d09 230 );\r
34e4e297 231\r
53c71d09 232typedef struct {\r
1436aea4
MK
233 EFI_EBC_PROTOCOL *This;\r
234 VOID *EntryPoint;\r
235 EFI_HANDLE ImageHandle;\r
236 VM_CONTEXT VmContext;\r
53c71d09 237} EFI_EBC_THUNK_DATA;\r
238\r
1436aea4 239#define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('e', 'b', 'c', 'p')\r
53c71d09 240\r
241#define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \\r
242 CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)\r
243\r
16dc5b68
AB
244/**\r
245 Allocates a buffer of type EfiBootServicesCode.\r
246\r
247 @param AllocationSize The number of bytes to allocate.\r
248\r
249 @return A pointer to the allocated buffer or NULL if allocation fails.\r
250\r
251**/\r
252VOID *\r
253EFIAPI\r
254EbcAllocatePoolForThunk (\r
255 IN UINTN AllocationSize\r
256 );\r
257\r
53c71d09 258#endif // #ifndef _EBC_INT_H_\r