]>
Commit | Line | Data |
---|---|---|
fb0b259e | 1 | /** @file\r |
53c71d09 | 2 | This module contains EBC support routines that are customized based on\r |
3 | the target x64 processor.\r | |
4 | \r | |
6e1e5405 | 5 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r |
e5eed7d3 | 6 | This program and the accompanying materials\r |
fb0b259e | 7 | are licensed and made available under the terms and conditions of the BSD License\r |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
53c71d09 | 15 | \r |
16 | #include "EbcInt.h"\r | |
17 | #include "EbcExecute.h"\r | |
18 | \r | |
19 | //\r | |
20 | // NOTE: This is the stack size allocated for the interpreter\r | |
21 | // when it executes an EBC image. The requirements can change\r | |
22 | // based on whether or not a debugger is present, and other\r | |
23 | // platform-specific configurations.\r | |
24 | //\r | |
25 | #define VM_STACK_SIZE (1024 * 8)\r | |
53c71d09 | 26 | \r |
27 | #define STACK_REMAIN_SIZE (1024 * 4)\r | |
28 | \r | |
21d13c61 JY |
29 | //\r |
30 | // This is instruction buffer used to create EBC thunk\r | |
31 | //\r | |
32 | #define EBC_ENTRYPOINT_SIGNATURE 0xAFAFAFAFAFAFAFAFull\r | |
33 | #define EBC_LL_EBC_ENTRYPOINT_SIGNATURE 0xFAFAFAFAFAFAFAFAull\r | |
34 | UINT8 mInstructionBufferTemplate[] = {\r | |
35 | //\r | |
36 | // Add a magic code here to help the VM recognize the thunk..\r | |
37 | // mov rax, 0xca112ebcca112ebc => 48 B8 BC 2E 11 CA BC 2E 11 CA\r | |
38 | //\r | |
39 | 0x48, 0xB8, 0xBC, 0x2E, 0x11, 0xCA, 0xBC, 0x2E, 0x11, 0xCA,\r | |
40 | //\r | |
41 | // Add code bytes to load up a processor register with the EBC entry point.\r | |
42 | // mov r10, EbcEntryPoint => 49 BA XX XX XX XX XX XX XX XX (To be fixed at runtime)\r | |
43 | // These 8 bytes of the thunk entry is the address of the EBC\r | |
44 | // entry point.\r | |
45 | //\r | |
46 | 0x49, 0xBA, \r | |
47 | (UINT8)(EBC_ENTRYPOINT_SIGNATURE & 0xFF),\r | |
48 | (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 8) & 0xFF),\r | |
49 | (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 16) & 0xFF),\r | |
50 | (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 24) & 0xFF),\r | |
51 | (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 32) & 0xFF),\r | |
52 | (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 40) & 0xFF),\r | |
53 | (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 48) & 0xFF),\r | |
54 | (UINT8)((EBC_ENTRYPOINT_SIGNATURE >> 56) & 0xFF),\r | |
55 | //\r | |
56 | // Stick in a load of r11 with the address of appropriate VM function.\r | |
57 | // mov r11, EbcLLEbcInterpret => 49 BB XX XX XX XX XX XX XX XX (To be fixed at runtime)\r | |
58 | //\r | |
59 | 0x49, 0xBB,\r | |
60 | (UINT8)(EBC_LL_EBC_ENTRYPOINT_SIGNATURE & 0xFF),\r | |
61 | (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 8) & 0xFF),\r | |
62 | (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 16) & 0xFF),\r | |
63 | (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 24) & 0xFF),\r | |
64 | (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 32) & 0xFF),\r | |
65 | (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 40) & 0xFF),\r | |
66 | (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 48) & 0xFF),\r | |
67 | (UINT8)((EBC_LL_EBC_ENTRYPOINT_SIGNATURE >> 56) & 0xFF),\r | |
68 | //\r | |
69 | // Stick in jump opcode bytes\r | |
70 | // jmp r11 => 41 FF E3\r | |
71 | //\r | |
72 | 0x41, 0xFF, 0xE3,\r | |
73 | };\r | |
74 | \r | |
7102b199 JY |
75 | /**\r |
76 | Begin executing an EBC image.\r | |
77 | This is used for Ebc Thunk call.\r | |
78 | \r | |
79 | @return The value returned by the EBC application we're going to run.\r | |
80 | \r | |
81 | **/\r | |
82 | UINT64\r | |
83 | EFIAPI\r | |
84 | EbcLLEbcInterpret (\r | |
85 | VOID\r | |
86 | );\r | |
87 | \r | |
88 | /**\r | |
89 | Begin executing an EBC image.\r | |
90 | This is used for Ebc image entrypoint.\r | |
91 | \r | |
92 | @return The value returned by the EBC application we're going to run.\r | |
93 | \r | |
94 | **/\r | |
95 | UINT64\r | |
96 | EFIAPI\r | |
97 | EbcLLExecuteEbcImageEntryPoint (\r | |
98 | VOID\r | |
99 | );\r | |
8e3bc754 | 100 | \r |
101 | /**\r | |
102 | Pushes a 64 bit unsigned value to the VM stack.\r | |
103 | \r | |
104 | @param VmPtr The pointer to current VM context.\r | |
105 | @param Arg The value to be pushed.\r | |
106 | \r | |
107 | **/\r | |
53c71d09 | 108 | VOID\r |
109 | PushU64 (\r | |
8e3bc754 | 110 | IN VM_CONTEXT *VmPtr,\r |
111 | IN UINT64 Arg\r | |
53c71d09 | 112 | )\r |
53c71d09 | 113 | {\r |
114 | //\r | |
115 | // Advance the VM stack down, and then copy the argument to the stack.\r | |
116 | // Hope it's aligned.\r | |
117 | //\r | |
1ccdbf2a | 118 | VmPtr->Gpr[0] -= sizeof (UINT64);\r |
119 | *(UINT64 *) VmPtr->Gpr[0] = Arg;\r | |
53c71d09 | 120 | return;\r |
121 | }\r | |
122 | \r | |
fb0b259e | 123 | \r |
124 | /**\r | |
7102b199 | 125 | Begin executing an EBC image.\r |
fb0b259e | 126 | \r |
127 | This is a thunk function. Microsoft x64 compiler only provide fast_call\r | |
128 | calling convention, so the first four arguments are passed by rcx, rdx,\r | |
129 | r8, and r9, while other arguments are passed in stack.\r | |
130 | \r | |
7102b199 | 131 | @param EntryPoint The entrypoint of EBC code.\r |
8e3bc754 | 132 | @param Arg1 The 1st argument.\r |
133 | @param Arg2 The 2nd argument.\r | |
134 | @param Arg3 The 3rd argument.\r | |
135 | @param Arg4 The 4th argument.\r | |
136 | @param Arg5 The 5th argument.\r | |
137 | @param Arg6 The 6th argument.\r | |
138 | @param Arg7 The 7th argument.\r | |
139 | @param Arg8 The 8th argument.\r | |
140 | @param Arg9 The 9th argument.\r | |
141 | @param Arg10 The 10th argument.\r | |
142 | @param Arg11 The 11th argument.\r | |
143 | @param Arg12 The 12th argument.\r | |
144 | @param Arg13 The 13th argument.\r | |
145 | @param Arg14 The 14th argument.\r | |
146 | @param Arg15 The 15th argument.\r | |
147 | @param Arg16 The 16th argument.\r | |
148 | \r | |
fb0b259e | 149 | @return The value returned by the EBC application we're going to run.\r |
150 | \r | |
151 | **/\r | |
53c71d09 | 152 | UINT64\r |
fa97cbf4 | 153 | EFIAPI\r |
53c71d09 | 154 | EbcInterpret (\r |
7102b199 JY |
155 | IN UINTN EntryPoint,\r |
156 | IN UINTN Arg1,\r | |
157 | IN UINTN Arg2,\r | |
158 | IN UINTN Arg3,\r | |
159 | IN UINTN Arg4,\r | |
160 | IN UINTN Arg5,\r | |
161 | IN UINTN Arg6,\r | |
162 | IN UINTN Arg7,\r | |
163 | IN UINTN Arg8,\r | |
164 | IN UINTN Arg9,\r | |
165 | IN UINTN Arg10,\r | |
166 | IN UINTN Arg11,\r | |
167 | IN UINTN Arg12,\r | |
168 | IN UINTN Arg13,\r | |
169 | IN UINTN Arg14,\r | |
170 | IN UINTN Arg15,\r | |
171 | IN UINTN Arg16\r | |
53c71d09 | 172 | )\r |
53c71d09 | 173 | {\r |
174 | //\r | |
175 | // Create a new VM context on the stack\r | |
176 | //\r | |
177 | VM_CONTEXT VmContext;\r | |
178 | UINTN Addr;\r | |
179 | EFI_STATUS Status;\r | |
180 | UINTN StackIndex;\r | |
181 | \r | |
182 | //\r | |
7102b199 | 183 | // Get the EBC entry point\r |
53c71d09 | 184 | //\r |
7102b199 | 185 | Addr = EntryPoint;\r |
53c71d09 | 186 | \r |
187 | //\r | |
188 | // Now clear out our context\r | |
189 | //\r | |
190 | ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r | |
191 | \r | |
192 | //\r | |
193 | // Set the VM instruction pointer to the correct location in memory.\r | |
194 | //\r | |
195 | VmContext.Ip = (VMIP) Addr;\r | |
196 | \r | |
197 | //\r | |
198 | // Initialize the stack pointer for the EBC. Get the current system stack\r | |
199 | // pointer and adjust it down by the max needed for the interpreter.\r | |
200 | //\r | |
53c71d09 | 201 | \r |
202 | //\r | |
203 | // Adjust the VM's stack pointer down.\r | |
204 | //\r | |
fb0b259e | 205 | \r |
53c71d09 | 206 | Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r |
207 | if (EFI_ERROR(Status)) {\r | |
208 | return Status;\r | |
209 | }\r | |
210 | VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r | |
1ccdbf2a | 211 | VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r |
212 | VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];\r | |
213 | VmContext.Gpr[0] -= sizeof (UINTN);\r | |
53c71d09 | 214 | \r |
215 | //\r | |
216 | // Align the stack on a natural boundary.\r | |
217 | //\r | |
6e1e5405 | 218 | VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof (UINTN) - 1);\r |
53c71d09 | 219 | \r |
220 | //\r | |
221 | // Put a magic value in the stack gap, then adjust down again.\r | |
222 | //\r | |
1ccdbf2a | 223 | *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE;\r |
224 | VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0];\r | |
53c71d09 | 225 | \r |
226 | //\r | |
227 | // The stack upper to LowStackTop is belong to the VM.\r | |
228 | //\r | |
1ccdbf2a | 229 | VmContext.LowStackTop = (UINTN) VmContext.Gpr[0];\r |
53c71d09 | 230 | \r |
231 | //\r | |
232 | // For the worst case, assume there are 4 arguments passed in registers, store\r | |
233 | // them to VM's stack.\r | |
234 | //\r | |
235 | PushU64 (&VmContext, (UINT64) Arg16);\r | |
236 | PushU64 (&VmContext, (UINT64) Arg15);\r | |
237 | PushU64 (&VmContext, (UINT64) Arg14);\r | |
238 | PushU64 (&VmContext, (UINT64) Arg13);\r | |
239 | PushU64 (&VmContext, (UINT64) Arg12);\r | |
240 | PushU64 (&VmContext, (UINT64) Arg11);\r | |
241 | PushU64 (&VmContext, (UINT64) Arg10);\r | |
242 | PushU64 (&VmContext, (UINT64) Arg9);\r | |
243 | PushU64 (&VmContext, (UINT64) Arg8);\r | |
244 | PushU64 (&VmContext, (UINT64) Arg7);\r | |
245 | PushU64 (&VmContext, (UINT64) Arg6);\r | |
246 | PushU64 (&VmContext, (UINT64) Arg5);\r | |
247 | PushU64 (&VmContext, (UINT64) Arg4);\r | |
248 | PushU64 (&VmContext, (UINT64) Arg3);\r | |
249 | PushU64 (&VmContext, (UINT64) Arg2);\r | |
250 | PushU64 (&VmContext, (UINT64) Arg1);\r | |
251 | \r | |
252 | //\r | |
253 | // Interpreter assumes 64-bit return address is pushed on the stack.\r | |
254 | // The x64 does not do this so pad the stack accordingly.\r | |
255 | //\r | |
256 | PushU64 (&VmContext, (UINT64) 0);\r | |
257 | PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r | |
258 | \r | |
259 | //\r | |
260 | // For x64, this is where we say our return address is\r | |
261 | //\r | |
1ccdbf2a | 262 | VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0];\r |
53c71d09 | 263 | \r |
264 | //\r | |
265 | // We need to keep track of where the EBC stack starts. This way, if the EBC\r | |
266 | // accesses any stack variables above its initial stack setting, then we know\r | |
267 | // it's accessing variables passed into it, which means the data is on the\r | |
268 | // VM's stack.\r | |
269 | // When we're called, on the stack (high to low) we have the parameters, the\r | |
270 | // return address, then the saved ebp. Save the pointer to the return address.\r | |
271 | // EBC code knows that's there, so should look above it for function parameters.\r | |
272 | // The offset is the size of locals (VMContext + Addr + saved ebp).\r | |
273 | // Note that the interpreter assumes there is a 16 bytes of return address on\r | |
274 | // the stack too, so adjust accordingly.\r | |
275 | // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r | |
276 | //\r | |
277 | \r | |
278 | //\r | |
279 | // Begin executing the EBC code\r | |
280 | //\r | |
281 | EbcExecute (&VmContext);\r | |
282 | \r | |
283 | //\r | |
284 | // Return the value in R[7] unless there was an error\r | |
285 | //\r | |
286 | ReturnEBCStack(StackIndex);\r | |
1ccdbf2a | 287 | return (UINT64) VmContext.Gpr[7];\r |
53c71d09 | 288 | }\r |
289 | \r | |
53c71d09 | 290 | \r |
fb0b259e | 291 | /**\r |
7102b199 | 292 | Begin executing an EBC image.\r |
53c71d09 | 293 | \r |
7102b199 | 294 | @param EntryPoint The entrypoint of EBC code.\r |
fb0b259e | 295 | @param ImageHandle image handle for the EBC application we're executing\r |
296 | @param SystemTable standard system table passed into an driver's entry\r | |
297 | point\r | |
53c71d09 | 298 | \r |
fb0b259e | 299 | @return The value returned by the EBC application we're going to run.\r |
53c71d09 | 300 | \r |
fb0b259e | 301 | **/\r |
fb0b259e | 302 | UINT64\r |
fa97cbf4 | 303 | EFIAPI\r |
fb0b259e | 304 | ExecuteEbcImageEntryPoint (\r |
7102b199 | 305 | IN UINTN EntryPoint,\r |
fb0b259e | 306 | IN EFI_HANDLE ImageHandle,\r |
307 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
308 | )\r | |
53c71d09 | 309 | {\r |
310 | //\r | |
311 | // Create a new VM context on the stack\r | |
312 | //\r | |
313 | VM_CONTEXT VmContext;\r | |
314 | UINTN Addr;\r | |
315 | EFI_STATUS Status;\r | |
316 | UINTN StackIndex;\r | |
317 | \r | |
318 | //\r | |
7102b199 | 319 | // Get the EBC entry point\r |
53c71d09 | 320 | //\r |
7102b199 | 321 | Addr = EntryPoint;\r |
53c71d09 | 322 | \r |
323 | //\r | |
324 | // Now clear out our context\r | |
325 | //\r | |
326 | ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r | |
327 | \r | |
328 | //\r | |
329 | // Save the image handle so we can track the thunks created for this image\r | |
330 | //\r | |
331 | VmContext.ImageHandle = ImageHandle;\r | |
332 | VmContext.SystemTable = SystemTable;\r | |
333 | \r | |
334 | //\r | |
335 | // Set the VM instruction pointer to the correct location in memory.\r | |
336 | //\r | |
337 | VmContext.Ip = (VMIP) Addr;\r | |
338 | \r | |
339 | //\r | |
340 | // Initialize the stack pointer for the EBC. Get the current system stack\r | |
341 | // pointer and adjust it down by the max needed for the interpreter.\r | |
342 | //\r | |
53c71d09 | 343 | \r |
344 | Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r | |
345 | if (EFI_ERROR(Status)) {\r | |
346 | return Status;\r | |
347 | }\r | |
348 | VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r | |
1ccdbf2a | 349 | VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r |
350 | VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];\r | |
351 | VmContext.Gpr[0] -= sizeof (UINTN);\r | |
53c71d09 | 352 | \r |
353 | \r | |
354 | //\r | |
355 | // Put a magic value in the stack gap, then adjust down again\r | |
356 | //\r | |
1ccdbf2a | 357 | *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE;\r |
358 | VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0];\r | |
53c71d09 | 359 | \r |
360 | //\r | |
361 | // Align the stack on a natural boundary\r | |
6e1e5405 | 362 | VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof(UINTN) - 1);\r |
53c71d09 | 363 | //\r |
1ccdbf2a | 364 | VmContext.LowStackTop = (UINTN) VmContext.Gpr[0];\r |
53c71d09 | 365 | \r |
366 | //\r | |
367 | // Simply copy the image handle and system table onto the EBC stack.\r | |
368 | // Greatly simplifies things by not having to spill the args.\r | |
369 | //\r | |
370 | PushU64 (&VmContext, (UINT64) SystemTable);\r | |
371 | PushU64 (&VmContext, (UINT64) ImageHandle);\r | |
372 | \r | |
373 | //\r | |
374 | // VM pushes 16-bytes for return address. Simulate that here.\r | |
375 | //\r | |
376 | PushU64 (&VmContext, (UINT64) 0);\r | |
377 | PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r | |
378 | \r | |
379 | //\r | |
380 | // For x64, this is where we say our return address is\r | |
381 | //\r | |
1ccdbf2a | 382 | VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0];\r |
53c71d09 | 383 | \r |
384 | //\r | |
385 | // Entry function needn't access high stack context, simply\r | |
386 | // put the stack pointer here.\r | |
387 | //\r | |
388 | \r | |
389 | //\r | |
390 | // Begin executing the EBC code\r | |
391 | //\r | |
392 | EbcExecute (&VmContext);\r | |
393 | \r | |
394 | //\r | |
395 | // Return the value in R[7] unless there was an error\r | |
396 | //\r | |
397 | ReturnEBCStack(StackIndex);\r | |
1ccdbf2a | 398 | return (UINT64) VmContext.Gpr[7];\r |
53c71d09 | 399 | }\r |
400 | \r | |
fb0b259e | 401 | \r |
402 | /**\r | |
8e3bc754 | 403 | Create thunks for an EBC image entry point, or an EBC protocol service.\r |
fb0b259e | 404 | \r |
8e3bc754 | 405 | @param ImageHandle Image handle for the EBC image. If not null, then\r |
406 | we're creating a thunk for an image entry point.\r | |
407 | @param EbcEntryPoint Address of the EBC code that the thunk is to call\r | |
408 | @param Thunk Returned thunk we create here\r | |
409 | @param Flags Flags indicating options for creating the thunk\r | |
fb0b259e | 410 | \r |
8e3bc754 | 411 | @retval EFI_SUCCESS The thunk was created successfully.\r |
412 | @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit\r | |
413 | aligned.\r | |
414 | @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC\r | |
415 | Thunk.\r | |
416 | @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.\r | |
fb0b259e | 417 | \r |
418 | **/\r | |
53c71d09 | 419 | EFI_STATUS\r |
420 | EbcCreateThunks (\r | |
421 | IN EFI_HANDLE ImageHandle,\r | |
422 | IN VOID *EbcEntryPoint,\r | |
423 | OUT VOID **Thunk,\r | |
424 | IN UINT32 Flags\r | |
425 | )\r | |
53c71d09 | 426 | {\r |
427 | UINT8 *Ptr;\r | |
428 | UINT8 *ThunkBase;\r | |
8e3bc754 | 429 | UINT32 Index;\r |
53c71d09 | 430 | INT32 ThunkSize;\r |
431 | \r | |
432 | //\r | |
433 | // Check alignment of pointer to EBC code\r | |
434 | //\r | |
435 | if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r | |
436 | return EFI_INVALID_PARAMETER;\r | |
437 | }\r | |
438 | \r | |
21d13c61 | 439 | ThunkSize = sizeof(mInstructionBufferTemplate);\r |
53c71d09 | 440 | \r |
21d13c61 | 441 | Ptr = AllocatePool (sizeof(mInstructionBufferTemplate));\r |
53c71d09 | 442 | \r |
443 | if (Ptr == NULL) {\r | |
444 | return EFI_OUT_OF_RESOURCES;\r | |
445 | }\r | |
446 | //\r | |
447 | // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r | |
448 | //\r | |
449 | // Save the start address so we can add a pointer to it to a list later.\r | |
450 | //\r | |
451 | ThunkBase = Ptr;\r | |
452 | \r | |
453 | //\r | |
454 | // Give them the address of our buffer we're going to fix up\r | |
455 | //\r | |
456 | *Thunk = (VOID *) Ptr;\r | |
457 | \r | |
458 | //\r | |
21d13c61 | 459 | // Copy whole thunk instruction buffer template\r |
53c71d09 | 460 | //\r |
21d13c61 | 461 | CopyMem (Ptr, mInstructionBufferTemplate, sizeof(mInstructionBufferTemplate));\r |
53c71d09 | 462 | \r |
463 | //\r | |
21d13c61 | 464 | // Patch EbcEntryPoint and EbcLLEbcInterpret\r |
53c71d09 | 465 | //\r |
21d13c61 JY |
466 | for (Index = 0; Index < sizeof(mInstructionBufferTemplate) - sizeof(UINTN); Index++) {\r |
467 | if (*(UINTN *)&Ptr[Index] == EBC_ENTRYPOINT_SIGNATURE) {\r | |
468 | *(UINTN *)&Ptr[Index] = (UINTN)EbcEntryPoint;\r | |
469 | }\r | |
470 | if (*(UINTN *)&Ptr[Index] == EBC_LL_EBC_ENTRYPOINT_SIGNATURE) {\r | |
471 | if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r | |
472 | *(UINTN *)&Ptr[Index] = (UINTN)EbcLLExecuteEbcImageEntryPoint;\r | |
473 | } else {\r | |
474 | *(UINTN *)&Ptr[Index] = (UINTN)EbcLLEbcInterpret;\r | |
475 | }\r | |
476 | }\r | |
53c71d09 | 477 | }\r |
478 | \r | |
53c71d09 | 479 | //\r |
480 | // Add the thunk to the list for this image. Do this last since the add\r | |
481 | // function flushes the cache for us.\r | |
482 | //\r | |
483 | EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r | |
484 | \r | |
485 | return EFI_SUCCESS;\r | |
486 | }\r | |
487 | \r | |
53c71d09 | 488 | \r |
fb0b259e | 489 | /**\r |
490 | This function is called to execute an EBC CALLEX instruction.\r | |
53c71d09 | 491 | The function check the callee's content to see whether it is common native\r |
492 | code or a thunk to another piece of EBC code.\r | |
493 | If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r | |
494 | otherwise, set the VM->IP to target EBC code directly to avoid another VM\r | |
495 | be startup which cost time and stack space.\r | |
53c71d09 | 496 | \r |
fb0b259e | 497 | @param VmPtr Pointer to a VM context.\r |
498 | @param FuncAddr Callee's address\r | |
499 | @param NewStackPointer New stack pointer after the call\r | |
500 | @param FramePtr New frame pointer after the call\r | |
501 | @param Size The size of call instruction\r | |
53c71d09 | 502 | \r |
fb0b259e | 503 | **/\r |
504 | VOID\r | |
505 | EbcLLCALLEX (\r | |
506 | IN VM_CONTEXT *VmPtr,\r | |
507 | IN UINTN FuncAddr,\r | |
508 | IN UINTN NewStackPointer,\r | |
509 | IN VOID *FramePtr,\r | |
510 | IN UINT8 Size\r | |
511 | )\r | |
53c71d09 | 512 | {\r |
513 | UINTN IsThunk;\r | |
514 | UINTN TargetEbcAddr;\r | |
21d13c61 JY |
515 | UINT8 InstructionBuffer[sizeof(mInstructionBufferTemplate)];\r |
516 | UINTN Index;\r | |
517 | UINTN IndexOfEbcEntrypoint;\r | |
53c71d09 | 518 | \r |
519 | IsThunk = 1;\r | |
520 | TargetEbcAddr = 0;\r | |
21d13c61 | 521 | IndexOfEbcEntrypoint = 0;\r |
53c71d09 | 522 | \r |
523 | //\r | |
524 | // Processor specific code to check whether the callee is a thunk to EBC.\r | |
525 | //\r | |
21d13c61 JY |
526 | CopyMem (InstructionBuffer, (VOID *)FuncAddr, sizeof(InstructionBuffer));\r |
527 | //\r | |
528 | // Fill the signature according to mInstructionBufferTemplate\r | |
529 | //\r | |
530 | for (Index = 0; Index < sizeof(mInstructionBufferTemplate) - sizeof(UINTN); Index++) {\r | |
531 | if (*(UINTN *)&mInstructionBufferTemplate[Index] == EBC_ENTRYPOINT_SIGNATURE) {\r | |
532 | *(UINTN *)&InstructionBuffer[Index] = EBC_ENTRYPOINT_SIGNATURE;\r | |
533 | IndexOfEbcEntrypoint = Index;\r | |
534 | }\r | |
535 | if (*(UINTN *)&mInstructionBufferTemplate[Index] == EBC_LL_EBC_ENTRYPOINT_SIGNATURE) {\r | |
536 | *(UINTN *)&InstructionBuffer[Index] = EBC_LL_EBC_ENTRYPOINT_SIGNATURE;\r | |
537 | }\r | |
53c71d09 | 538 | }\r |
21d13c61 JY |
539 | //\r |
540 | // Check if we need thunk to native\r | |
541 | //\r | |
542 | if (CompareMem (InstructionBuffer, mInstructionBufferTemplate, sizeof(mInstructionBufferTemplate)) != 0) {\r | |
53c71d09 | 543 | IsThunk = 0;\r |
53c71d09 | 544 | }\r |
545 | \r | |
53c71d09 | 546 | if (IsThunk == 1){\r |
547 | //\r | |
548 | // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r | |
549 | // put our return address and frame pointer on the VM stack.\r | |
550 | // Then set the VM's IP to new EBC code.\r | |
551 | //\r | |
1ccdbf2a | 552 | VmPtr->Gpr[0] -= 8;\r |
553 | VmWriteMemN (VmPtr, (UINTN) VmPtr->Gpr[0], (UINTN) FramePtr);\r | |
554 | VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->Gpr[0];\r | |
555 | VmPtr->Gpr[0] -= 8;\r | |
21d13c61 | 556 | VmWriteMem64 (VmPtr, (UINTN) VmPtr->Gpr[0], (UINT64) (UINTN) (VmPtr->Ip + Size));\r |
53c71d09 | 557 | \r |
21d13c61 | 558 | CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + IndexOfEbcEntrypoint, sizeof(UINTN));\r |
53c71d09 | 559 | VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r |
560 | } else {\r | |
561 | //\r | |
fa97cbf4 JY |
562 | // The callee is not a thunk to EBC, call native code,\r |
563 | // and get return value.\r | |
53c71d09 | 564 | //\r |
fa97cbf4 | 565 | VmPtr->Gpr[7] = EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r |
53c71d09 | 566 | \r |
567 | //\r | |
fa97cbf4 | 568 | // Advance the IP.\r |
53c71d09 | 569 | //\r |
53c71d09 | 570 | VmPtr->Ip += Size;\r |
571 | }\r | |
572 | }\r | |
573 | \r |