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