]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/EbcDxe/EbcInt.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / EbcInt.h
... / ...
CommitLineData
1/** @file\r
2 Main routines for the EBC interpreter. Includes the initialization and\r
3 main interpreter routines.\r
4\r
5Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
6SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#ifndef _EBC_INT_H_\r
11#define _EBC_INT_H_\r
12\r
13#include <Uefi.h>\r
14\r
15#include <Protocol/DebugSupport.h>\r
16#include <Protocol/Ebc.h>\r
17#include <Protocol/EbcVmTest.h>\r
18#include <Protocol/EbcSimpleDebugger.h>\r
19#include <Protocol/PeCoffImageEmulator.h>\r
20\r
21#include <Library/BaseLib.h>\r
22#include <Library/CacheMaintenanceLib.h>\r
23#include <Library/DebugLib.h>\r
24#include <Library/PeCoffLib.h>\r
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
30extern VM_CONTEXT *mVmPtr;\r
31\r
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
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
60EFI_STATUS\r
61EbcCreateThunks (\r
62 IN EFI_HANDLE ImageHandle,\r
63 IN VOID *EbcEntryPoint,\r
64 OUT VOID **Thunk,\r
65 IN UINT32 Flags\r
66 );\r
67\r
68/**\r
69 Add a thunk to our list of thunks for a given image handle.\r
70 Also flush the instruction cache since we've written thunk code\r
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
81EFI_STATUS\r
82EbcAddImageThunk (\r
83 IN EFI_HANDLE ImageHandle,\r
84 IN VOID *ThunkBuffer,\r
85 IN UINT32 ThunkSize\r
86 );\r
87\r
88//\r
89// Define a constant of how often to call the debugger periodic callback\r
90// function.\r
91//\r
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
96\r
97//\r
98// External low level functions that are native-processor dependent\r
99//\r
100\r
101/**\r
102 The VM thunk code stuffs an EBC entry point into a processor\r
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
106\r
107 @return The contents of the register in which the entry point is passed.\r
108\r
109**/\r
110UINTN\r
111EFIAPI\r
112EbcLLGetEbcEntryPoint (\r
113 VOID\r
114 );\r
115\r
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
119 code. For x64, we switch stacks, copy the arguments to the stack\r
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
127\r
128 @return The unmodified value returned by the native code.\r
129\r
130**/\r
131INT64\r
132EFIAPI\r
133EbcLLCALLEXNative (\r
134 IN UINTN CallAddr,\r
135 IN UINTN EbcSp,\r
136 IN VOID *FramePtr\r
137 );\r
138\r
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
154VOID\r
155EbcLLCALLEX (\r
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
161 );\r
162\r
163/**\r
164 Returns the stack index and buffer assosicated with the Handle parameter.\r
165\r
166 @param Handle The EFI handle as the index to the EBC stack.\r
167 @param StackBuffer A pointer to hold the returned stack buffer.\r
168 @param BufferIndex A pointer to hold the returned stack index.\r
169\r
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
176EFI_STATUS\r
177GetEBCStack (\r
178 IN EFI_HANDLE Handle,\r
179 OUT VOID **StackBuffer,\r
180 OUT UINTN *BufferIndex\r
181 );\r
182\r
183/**\r
184 Returns from the EBC stack by stack Index.\r
185\r
186 @param Index Specifies which EBC stack to return from.\r
187\r
188 @retval EFI_SUCCESS The function completed successfully.\r
189\r
190**/\r
191EFI_STATUS\r
192ReturnEBCStack (\r
193 IN UINTN Index\r
194 );\r
195\r
196/**\r
197 Allocates memory to hold all the EBC stacks.\r
198\r
199 @retval EFI_SUCCESS The EBC stacks were allocated successfully.\r
200 @retval EFI_OUT_OF_RESOURCES Not enough memory available for EBC stacks.\r
201\r
202**/\r
203EFI_STATUS\r
204InitEBCStack (\r
205 VOID\r
206 );\r
207\r
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
214EFI_STATUS\r
215FreeEBCStack (\r
216 VOID\r
217 );\r
218\r
219/**\r
220 Returns from the EBC stack associated with the Handle parameter.\r
221\r
222 @param Handle Specifies the EFI handle to find the EBC stack with.\r
223\r
224 @retval EFI_SUCCESS The function completed successfully.\r
225\r
226**/\r
227EFI_STATUS\r
228ReturnEBCStackByHandle (\r
229 IN EFI_HANDLE Handle\r
230 );\r
231\r
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
239#define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('e', 'b', 'c', 'p')\r
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
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
258#endif // #ifndef _EBC_INT_H_\r