Commit | Line | Data |
---|---|---|
fb0b259e | 1 | /** @file\r |
53c71d09 | 2 | This module contains EBC support routines that are customized based on\r |
3 | the target processor.\r | |
4 | \r | |
3bbe68a3 | 5 | Copyright (c) 2006 - 2012, 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 | #include "EbcSupport.h"\r | |
6f0a3cd2 | 19 | #include "EbcDebuggerHook.h"\r |
53c71d09 | 20 | \r |
8e3bc754 | 21 | /**\r |
22 | Given raw bytes of Itanium based code, format them into a bundle and\r | |
23 | write them out.\r | |
24 | \r | |
25 | @param MemPtr pointer to memory location to write the bundles\r | |
26 | to.\r | |
27 | @param Template 5-bit template.\r | |
28 | @param Slot0 Instruction slot 0 data for the bundle.\r | |
29 | @param Slot1 Instruction slot 1 data for the bundle.\r | |
30 | @param Slot2 Instruction slot 2 data for the bundle.\r | |
31 | \r | |
32 | @retval EFI_INVALID_PARAMETER Pointer is not aligned\r | |
33 | @retval EFI_INVALID_PARAMETER No more than 5 bits in template\r | |
34 | @retval EFI_INVALID_PARAMETER More than 41 bits used in code\r | |
35 | @retval EFI_SUCCESS All data is written.\r | |
36 | \r | |
37 | **/\r | |
53c71d09 | 38 | EFI_STATUS\r |
39 | WriteBundle (\r | |
40 | IN VOID *MemPtr,\r | |
41 | IN UINT8 Template,\r | |
42 | IN UINT64 Slot0,\r | |
43 | IN UINT64 Slot1,\r | |
44 | IN UINT64 Slot2\r | |
45 | );\r | |
46 | \r | |
8e3bc754 | 47 | /**\r |
48 | Pushes a 64 bit unsigned value to the VM stack.\r | |
49 | \r | |
50 | @param VmPtr The pointer to current VM context.\r | |
51 | @param Arg The value to be pushed.\r | |
52 | \r | |
53 | **/\r | |
53c71d09 | 54 | VOID\r |
55 | PushU64 (\r | |
8e3bc754 | 56 | IN VM_CONTEXT *VmPtr,\r |
57 | IN UINT64 Arg\r | |
53c71d09 | 58 | )\r |
59 | {\r | |
60 | //\r | |
61 | // Advance the VM stack down, and then copy the argument to the stack.\r | |
62 | // Hope it's aligned.\r | |
63 | //\r | |
1ccdbf2a | 64 | VmPtr->Gpr[0] -= sizeof (UINT64);\r |
65 | *(UINT64 *) VmPtr->Gpr[0] = Arg;\r | |
53c71d09 | 66 | }\r |
67 | \r | |
8e3bc754 | 68 | /**\r |
69 | Begin executing an EBC image. The address of the entry point is passed\r | |
70 | in via a processor register, so we'll need to make a call to get the\r | |
71 | value.\r | |
72 | \r | |
73 | This is a thunk function. Microsoft x64 compiler only provide fast_call\r | |
74 | calling convention, so the first four arguments are passed by rcx, rdx,\r | |
75 | r8, and r9, while other arguments are passed in stack.\r | |
76 | \r | |
77 | @param Arg1 The 1st argument.\r | |
78 | @param ... The variable arguments list.\r | |
79 | \r | |
80 | @return The value returned by the EBC application we're going to run.\r | |
81 | \r | |
82 | **/\r | |
53c71d09 | 83 | UINT64\r |
8091267c | 84 | EFIAPI\r |
53c71d09 | 85 | EbcInterpret (\r |
86 | UINT64 Arg1,\r | |
87 | ...\r | |
88 | )\r | |
89 | {\r | |
90 | //\r | |
91 | // Create a new VM context on the stack\r | |
92 | //\r | |
93 | VM_CONTEXT VmContext;\r | |
94 | UINTN Addr;\r | |
95 | EFI_STATUS Status;\r | |
96 | UINTN StackIndex;\r | |
97 | VA_LIST List;\r | |
98 | UINT64 Arg2;\r | |
99 | UINT64 Arg3;\r | |
100 | UINT64 Arg4;\r | |
101 | UINT64 Arg5;\r | |
102 | UINT64 Arg6;\r | |
103 | UINT64 Arg7;\r | |
104 | UINT64 Arg8;\r | |
105 | UINT64 Arg9;\r | |
106 | UINT64 Arg10;\r | |
107 | UINT64 Arg11;\r | |
108 | UINT64 Arg12;\r | |
109 | UINT64 Arg13;\r | |
110 | UINT64 Arg14;\r | |
111 | UINT64 Arg15;\r | |
112 | UINT64 Arg16;\r | |
113 | //\r | |
114 | // Get the EBC entry point from the processor register. Make sure you don't\r | |
115 | // call any functions before this or you could mess up the register the\r | |
116 | // entry point is passed in.\r | |
117 | //\r | |
118 | Addr = EbcLLGetEbcEntryPoint ();\r | |
119 | //\r | |
120 | // Need the args off the stack.\r | |
121 | //\r | |
122 | VA_START (List, Arg1);\r | |
123 | Arg2 = VA_ARG (List, UINT64);\r | |
124 | Arg3 = VA_ARG (List, UINT64);\r | |
125 | Arg4 = VA_ARG (List, UINT64);\r | |
126 | Arg5 = VA_ARG (List, UINT64);\r | |
127 | Arg6 = VA_ARG (List, UINT64);\r | |
128 | Arg7 = VA_ARG (List, UINT64);\r | |
129 | Arg8 = VA_ARG (List, UINT64);\r | |
130 | Arg9 = VA_ARG (List, UINT64);\r | |
131 | Arg10 = VA_ARG (List, UINT64);\r | |
132 | Arg11 = VA_ARG (List, UINT64);\r | |
133 | Arg12 = VA_ARG (List, UINT64);\r | |
134 | Arg13 = VA_ARG (List, UINT64);\r | |
135 | Arg14 = VA_ARG (List, UINT64);\r | |
136 | Arg15 = VA_ARG (List, UINT64);\r | |
137 | Arg16 = VA_ARG (List, UINT64);\r | |
3bbe68a3 | 138 | VA_END (List);\r |
53c71d09 | 139 | //\r |
140 | // Now clear out our context\r | |
141 | //\r | |
142 | ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r | |
143 | //\r | |
144 | // Set the VM instruction pointer to the correct location in memory.\r | |
145 | //\r | |
146 | VmContext.Ip = (VMIP) Addr;\r | |
147 | //\r | |
148 | // Initialize the stack pointer for the EBC. Get the current system stack\r | |
149 | // pointer and adjust it down by the max needed for the interpreter.\r | |
150 | //\r | |
151 | //\r | |
152 | // NOTE: Eventually we should have the interpreter allocate memory\r | |
153 | // for stack space which it will use during its execution. This\r | |
154 | // would likely improve performance because the interpreter would\r | |
155 | // no longer be required to test each memory access and adjust\r | |
156 | // those reading from the stack gap.\r | |
157 | //\r | |
158 | // For IPF, the stack looks like (assuming 10 args passed)\r | |
159 | // arg10\r | |
160 | // arg9 (Bottom of high stack)\r | |
161 | // [ stack gap for interpreter execution ]\r | |
162 | // [ magic value for detection of stack corruption ]\r | |
163 | // arg8 (Top of low stack)\r | |
164 | // arg7....\r | |
165 | // arg1\r | |
166 | // [ 64-bit return address ]\r | |
167 | // [ ebc stack ]\r | |
168 | // If the EBC accesses memory in the stack gap, then we assume that it's\r | |
169 | // actually trying to access args9 and greater. Therefore we need to\r | |
170 | // adjust memory accesses in this region to point above the stack gap.\r | |
171 | //\r | |
172 | //\r | |
173 | // Now adjust the EBC stack pointer down to leave a gap for interpreter\r | |
174 | // execution. Then stuff a magic value there.\r | |
175 | //\r | |
fb0b259e | 176 | \r |
53c71d09 | 177 | Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r |
178 | if (EFI_ERROR(Status)) {\r | |
179 | return Status;\r | |
180 | }\r | |
181 | VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r | |
1ccdbf2a | 182 | VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r |
183 | VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];\r | |
184 | VmContext.Gpr[0] -= sizeof (UINTN);\r | |
53c71d09 | 185 | \r |
fb0b259e | 186 | \r |
53c71d09 | 187 | PushU64 (&VmContext, (UINT64) VM_STACK_KEY_VALUE);\r |
1ccdbf2a | 188 | VmContext.StackMagicPtr = (UINTN *) VmContext.Gpr[0];\r |
189 | VmContext.LowStackTop = (UINTN) VmContext.Gpr[0];\r | |
53c71d09 | 190 | //\r |
191 | // Push the EBC arguments on the stack. Does not matter that they may not\r | |
192 | // all be valid.\r | |
193 | //\r | |
194 | PushU64 (&VmContext, Arg16);\r | |
195 | PushU64 (&VmContext, Arg15);\r | |
196 | PushU64 (&VmContext, Arg14);\r | |
197 | PushU64 (&VmContext, Arg13);\r | |
198 | PushU64 (&VmContext, Arg12);\r | |
199 | PushU64 (&VmContext, Arg11);\r | |
200 | PushU64 (&VmContext, Arg10);\r | |
201 | PushU64 (&VmContext, Arg9);\r | |
202 | PushU64 (&VmContext, Arg8);\r | |
203 | PushU64 (&VmContext, Arg7);\r | |
204 | PushU64 (&VmContext, Arg6);\r | |
205 | PushU64 (&VmContext, Arg5);\r | |
206 | PushU64 (&VmContext, Arg4);\r | |
207 | PushU64 (&VmContext, Arg3);\r | |
208 | PushU64 (&VmContext, Arg2);\r | |
209 | PushU64 (&VmContext, Arg1);\r | |
210 | //\r | |
211 | // Push a bogus return address on the EBC stack because the\r | |
212 | // interpreter expects one there. For stack alignment purposes on IPF,\r | |
213 | // EBC return addresses are always 16 bytes. Push a bogus value as well.\r | |
214 | //\r | |
215 | PushU64 (&VmContext, 0);\r | |
216 | PushU64 (&VmContext, 0xDEADBEEFDEADBEEF);\r | |
1ccdbf2a | 217 | VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0];\r |
6f0a3cd2 | 218 | \r |
53c71d09 | 219 | //\r |
220 | // Begin executing the EBC code\r | |
221 | //\r | |
6f0a3cd2 | 222 | EbcDebuggerHookEbcInterpret (&VmContext);\r |
53c71d09 | 223 | EbcExecute (&VmContext);\r |
6f0a3cd2 | 224 | \r |
53c71d09 | 225 | //\r |
6f0a3cd2 | 226 | // Return the value in Gpr[7] unless there was an error\r |
53c71d09 | 227 | //\r |
228 | ReturnEBCStack(StackIndex);\r | |
1ccdbf2a | 229 | return (UINT64) VmContext.Gpr[7];\r |
53c71d09 | 230 | }\r |
231 | \r | |
53c71d09 | 232 | \r |
fb0b259e | 233 | /**\r |
53c71d09 | 234 | Begin executing an EBC image. The address of the entry point is passed\r |
235 | in via a processor register, so we'll need to make a call to get the\r | |
236 | value.\r | |
53c71d09 | 237 | \r |
8e3bc754 | 238 | @param ImageHandle image handle for the EBC application we're executing\r |
239 | @param SystemTable standard system table passed into an driver's entry\r | |
240 | point\r | |
53c71d09 | 241 | \r |
fb0b259e | 242 | @return The value returned by the EBC application we're going to run.\r |
53c71d09 | 243 | \r |
fb0b259e | 244 | **/\r |
fb0b259e | 245 | UINT64\r |
fa97cbf4 | 246 | EFIAPI\r |
fb0b259e | 247 | ExecuteEbcImageEntryPoint (\r |
248 | IN EFI_HANDLE ImageHandle,\r | |
249 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
250 | )\r | |
53c71d09 | 251 | {\r |
252 | //\r | |
253 | // Create a new VM context on the stack\r | |
254 | //\r | |
255 | VM_CONTEXT VmContext;\r | |
256 | UINTN Addr;\r | |
257 | EFI_STATUS Status;\r | |
258 | UINTN StackIndex;\r | |
259 | \r | |
260 | //\r | |
261 | // Get the EBC entry point from the processor register. Make sure you don't\r | |
262 | // call any functions before this or you could mess up the register the\r | |
263 | // entry point is passed in.\r | |
264 | //\r | |
265 | Addr = EbcLLGetEbcEntryPoint ();\r | |
266 | \r | |
267 | //\r | |
268 | // Now clear out our context\r | |
269 | //\r | |
270 | ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r | |
271 | \r | |
272 | //\r | |
273 | // Save the image handle so we can track the thunks created for this image\r | |
274 | //\r | |
275 | VmContext.ImageHandle = ImageHandle;\r | |
276 | VmContext.SystemTable = SystemTable;\r | |
277 | \r | |
278 | //\r | |
279 | // Set the VM instruction pointer to the correct location in memory.\r | |
280 | //\r | |
281 | VmContext.Ip = (VMIP) Addr;\r | |
282 | \r | |
283 | //\r | |
284 | // Get the stack pointer. This is the bottom of the upper stack.\r | |
285 | //\r | |
fb0b259e | 286 | \r |
53c71d09 | 287 | Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r |
288 | if (EFI_ERROR(Status)) {\r | |
289 | return Status;\r | |
290 | }\r | |
291 | VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r | |
1ccdbf2a | 292 | VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r |
293 | VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];\r | |
294 | VmContext.Gpr[0] -= sizeof (UINTN);\r | |
53c71d09 | 295 | \r |
fb0b259e | 296 | \r |
53c71d09 | 297 | //\r |
298 | // Allocate stack space for the interpreter. Then put a magic value\r | |
299 | // at the bottom so we can detect stack corruption.\r | |
300 | //\r | |
301 | PushU64 (&VmContext, (UINT64) VM_STACK_KEY_VALUE);\r | |
1ccdbf2a | 302 | VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0];\r |
53c71d09 | 303 | \r |
304 | //\r | |
305 | // When we thunk to external native code, we copy the last 8 qwords from\r | |
306 | // the EBC stack into the processor registers, and adjust the stack pointer\r | |
307 | // up. If the caller is not passing 8 parameters, then we've moved the\r | |
308 | // stack pointer up into the stack gap. If this happens, then the caller\r | |
309 | // can mess up the stack gap contents (in particular our magic value).\r | |
310 | // Therefore, leave another gap below the magic value. Pick 10 qwords down,\r | |
311 | // just as a starting point.\r | |
312 | //\r | |
1ccdbf2a | 313 | VmContext.Gpr[0] -= 10 * sizeof (UINT64);\r |
53c71d09 | 314 | \r |
315 | //\r | |
316 | // Align the stack pointer such that after pushing the system table,\r | |
317 | // image handle, and return address on the stack, it's aligned on a 16-byte\r | |
318 | // boundary as required for IPF.\r | |
319 | //\r | |
1ccdbf2a | 320 | VmContext.Gpr[0] &= (INT64)~0x0f;\r |
321 | VmContext.LowStackTop = (UINTN) VmContext.Gpr[0];\r | |
53c71d09 | 322 | //\r |
323 | // Simply copy the image handle and system table onto the EBC stack.\r | |
324 | // Greatly simplifies things by not having to spill the args\r | |
325 | //\r | |
326 | PushU64 (&VmContext, (UINT64) SystemTable);\r | |
327 | PushU64 (&VmContext, (UINT64) ImageHandle);\r | |
328 | \r | |
329 | //\r | |
330 | // Interpreter assumes 64-bit return address is pushed on the stack.\r | |
331 | // IPF does not do this so pad the stack accordingly. Also, a\r | |
332 | // "return address" is 16 bytes as required for IPF stack alignments.\r | |
333 | //\r | |
334 | PushU64 (&VmContext, (UINT64) 0);\r | |
335 | PushU64 (&VmContext, (UINT64) 0x1234567887654321);\r | |
1ccdbf2a | 336 | VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0];\r |
53c71d09 | 337 | \r |
338 | //\r | |
339 | // Begin executing the EBC code\r | |
340 | //\r | |
6f0a3cd2 | 341 | EbcDebuggerHookExecuteEbcImageEntryPoint (&VmContext);\r |
53c71d09 | 342 | EbcExecute (&VmContext);\r |
343 | \r | |
344 | //\r | |
6f0a3cd2 | 345 | // Return the value in Gpr[7] unless there was an error\r |
53c71d09 | 346 | //\r |
347 | ReturnEBCStack(StackIndex);\r | |
1ccdbf2a | 348 | return (UINT64) VmContext.Gpr[7];\r |
53c71d09 | 349 | }\r |
350 | \r | |
fb0b259e | 351 | \r |
352 | /**\r | |
353 | Create thunks for an EBC image entry point, or an EBC protocol service.\r | |
354 | \r | |
8e3bc754 | 355 | @param ImageHandle Image handle for the EBC image. If not null, then\r |
356 | we're creating a thunk for an image entry point.\r | |
357 | @param EbcEntryPoint Address of the EBC code that the thunk is to call\r | |
358 | @param Thunk Returned thunk we create here\r | |
359 | @param Flags Flags indicating options for creating the thunk\r | |
fb0b259e | 360 | \r |
8e3bc754 | 361 | @retval EFI_SUCCESS The thunk was created successfully.\r |
362 | @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit\r | |
363 | aligned.\r | |
364 | @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC\r | |
365 | Thunk.\r | |
366 | @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.\r | |
fb0b259e | 367 | \r |
368 | **/\r | |
53c71d09 | 369 | EFI_STATUS\r |
370 | EbcCreateThunks (\r | |
f45af90b | 371 | IN EFI_HANDLE ImageHandle,\r |
372 | IN VOID *EbcEntryPoint,\r | |
373 | OUT VOID **Thunk,\r | |
374 | IN UINT32 Flags\r | |
53c71d09 | 375 | )\r |
53c71d09 | 376 | {\r |
377 | UINT8 *Ptr;\r | |
378 | UINT8 *ThunkBase;\r | |
379 | UINT64 Addr;\r | |
380 | UINT64 Code[3]; // Code in a bundle\r | |
381 | UINT64 RegNum; // register number for MOVL\r | |
8e3bc754 | 382 | UINT64 BitI; // bits of MOVL immediate data\r |
383 | UINT64 BitIc; // bits of MOVL immediate data\r | |
384 | UINT64 BitImm5c; // bits of MOVL immediate data\r | |
385 | UINT64 BitImm9d; // bits of MOVL immediate data\r | |
386 | UINT64 BitImm7b; // bits of MOVL immediate data\r | |
53c71d09 | 387 | UINT64 Br; // branch register for loading and jumping\r |
388 | UINT64 *Data64Ptr;\r | |
389 | UINT32 ThunkSize;\r | |
390 | UINT32 Size;\r | |
391 | \r | |
392 | //\r | |
393 | // Check alignment of pointer to EBC code, which must always be aligned\r | |
394 | // on a 2-byte boundary.\r | |
395 | //\r | |
396 | if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r | |
397 | return EFI_INVALID_PARAMETER;\r | |
398 | }\r | |
399 | //\r | |
400 | // Allocate memory for the thunk. Make the (most likely incorrect) assumption\r | |
401 | // that the returned buffer is not aligned, so round up to the next\r | |
402 | // alignment size.\r | |
403 | //\r | |
404 | Size = EBC_THUNK_SIZE + EBC_THUNK_ALIGNMENT - 1;\r | |
405 | ThunkSize = Size;\r | |
406 | Ptr = AllocatePool (Size);\r | |
407 | \r | |
408 | if (Ptr == NULL) {\r | |
409 | return EFI_OUT_OF_RESOURCES;\r | |
410 | }\r | |
411 | //\r | |
412 | // Save the start address of the buffer.\r | |
413 | //\r | |
414 | ThunkBase = Ptr;\r | |
415 | \r | |
416 | //\r | |
417 | // Make sure it's aligned for code execution. If not, then\r | |
418 | // round up.\r | |
419 | //\r | |
420 | if ((UINT32) (UINTN) Ptr & (EBC_THUNK_ALIGNMENT - 1)) {\r | |
421 | Ptr = (UINT8 *) (((UINTN) Ptr + (EBC_THUNK_ALIGNMENT - 1)) &~ (UINT64) (EBC_THUNK_ALIGNMENT - 1));\r | |
422 | }\r | |
423 | //\r | |
424 | // Return the pointer to the thunk to the caller to user as the\r | |
425 | // image entry point.\r | |
426 | //\r | |
427 | *Thunk = (VOID *) Ptr;\r | |
428 | \r | |
429 | //\r | |
430 | // Clear out the thunk entry\r | |
431 | // ZeroMem(Ptr, Size);\r | |
432 | //\r | |
433 | // For IPF, when you do a call via a function pointer, the function pointer\r | |
434 | // actually points to a function descriptor which consists of a 64-bit\r | |
435 | // address of the function, followed by a 64-bit gp for the function being\r | |
436 | // called. See the the Software Conventions and Runtime Architecture Guide\r | |
437 | // for details.\r | |
438 | // So first off in our thunk, create a descriptor for our actual thunk code.\r | |
439 | // This means we need to create a pointer to the thunk code (which follows\r | |
440 | // the descriptor we're going to create), followed by the gp of the Vm\r | |
441 | // interpret function we're going to eventually execute.\r | |
442 | //\r | |
443 | Data64Ptr = (UINT64 *) Ptr;\r | |
444 | \r | |
445 | //\r | |
446 | // Write the function's entry point (which is our thunk code that follows\r | |
447 | // this descriptor we're creating).\r | |
448 | //\r | |
449 | *Data64Ptr = (UINT64) (Data64Ptr + 2);\r | |
450 | //\r | |
451 | // Get the gp from the descriptor for EbcInterpret and stuff it in our thunk\r | |
452 | // descriptor.\r | |
453 | //\r | |
454 | *(Data64Ptr + 1) = *(UINT64 *) ((UINT64 *) (UINTN) EbcInterpret + 1);\r | |
455 | //\r | |
456 | // Advance our thunk data pointer past the descriptor. Since the\r | |
457 | // descriptor consists of 16 bytes, the pointer is still aligned for\r | |
458 | // IPF code execution (on 16-byte boundary).\r | |
459 | //\r | |
460 | Ptr += sizeof (UINT64) * 2;\r | |
461 | \r | |
462 | //\r | |
463 | // *************************** MAGIC BUNDLE ********************************\r | |
464 | //\r | |
465 | // Write magic code bundle for: movl r8 = 0xca112ebcca112ebc to help the VM\r | |
466 | // to recognize it is a thunk.\r | |
467 | //\r | |
468 | Addr = (UINT64) 0xCA112EBCCA112EBC;\r | |
469 | \r | |
470 | //\r | |
471 | // Now generate the code bytes. First is nop.m 0x0\r | |
472 | //\r | |
473 | Code[0] = OPCODE_NOP;\r | |
474 | \r | |
475 | //\r | |
476 | // Next is simply Addr[62:22] (41 bits) of the address\r | |
477 | //\r | |
478 | Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r | |
479 | \r | |
480 | //\r | |
481 | // Extract bits from the address for insertion into the instruction\r | |
482 | // i = Addr[63:63]\r | |
483 | //\r | |
8e3bc754 | 484 | BitI = RShiftU64 (Addr, 63) & 0x01;\r |
53c71d09 | 485 | //\r |
486 | // ic = Addr[21:21]\r | |
487 | //\r | |
8e3bc754 | 488 | BitIc = RShiftU64 (Addr, 21) & 0x01;\r |
53c71d09 | 489 | //\r |
490 | // imm5c = Addr[20:16] for 5 bits\r | |
491 | //\r | |
8e3bc754 | 492 | BitImm5c = RShiftU64 (Addr, 16) & 0x1F;\r |
53c71d09 | 493 | //\r |
494 | // imm9d = Addr[15:7] for 9 bits\r | |
495 | //\r | |
8e3bc754 | 496 | BitImm9d = RShiftU64 (Addr, 7) & 0x1FF;\r |
53c71d09 | 497 | //\r |
498 | // imm7b = Addr[6:0] for 7 bits\r | |
499 | //\r | |
8e3bc754 | 500 | BitImm7b = Addr & 0x7F;\r |
53c71d09 | 501 | \r |
502 | //\r | |
503 | // The EBC entry point will be put into r8, so r8 can be used here\r | |
504 | // temporary. R8 is general register and is auto-serialized.\r | |
505 | //\r | |
506 | RegNum = 8;\r | |
507 | \r | |
508 | //\r | |
509 | // Next is jumbled data, including opcode and rest of address\r | |
510 | //\r | |
8e3bc754 | 511 | Code[2] = LShiftU64 (BitImm7b, 13);\r |
53c71d09 | 512 | Code[2] = Code[2] | LShiftU64 (0x00, 20); // vc\r |
8e3bc754 | 513 | Code[2] = Code[2] | LShiftU64 (BitIc, 21);\r |
514 | Code[2] = Code[2] | LShiftU64 (BitImm5c, 22);\r | |
515 | Code[2] = Code[2] | LShiftU64 (BitImm9d, 27);\r | |
516 | Code[2] = Code[2] | LShiftU64 (BitI, 36);\r | |
53c71d09 | 517 | Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r |
518 | Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r | |
519 | \r | |
520 | WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r | |
521 | \r | |
522 | //\r | |
523 | // *************************** FIRST BUNDLE ********************************\r | |
524 | //\r | |
525 | // Write code bundle for: movl r8 = EBC_ENTRY_POINT so we pass\r | |
526 | // the ebc entry point in to the interpreter function via a processor\r | |
527 | // register.\r | |
528 | // Note -- we could easily change this to pass in a pointer to a structure\r | |
529 | // that contained, among other things, the EBC image's entry point. But\r | |
530 | // for now pass it directly.\r | |
531 | //\r | |
532 | Ptr += 16;\r | |
533 | Addr = (UINT64) EbcEntryPoint;\r | |
534 | \r | |
535 | //\r | |
536 | // Now generate the code bytes. First is nop.m 0x0\r | |
537 | //\r | |
538 | Code[0] = OPCODE_NOP;\r | |
539 | \r | |
540 | //\r | |
541 | // Next is simply Addr[62:22] (41 bits) of the address\r | |
542 | //\r | |
543 | Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r | |
544 | \r | |
545 | //\r | |
546 | // Extract bits from the address for insertion into the instruction\r | |
547 | // i = Addr[63:63]\r | |
548 | //\r | |
8e3bc754 | 549 | BitI = RShiftU64 (Addr, 63) & 0x01;\r |
53c71d09 | 550 | //\r |
551 | // ic = Addr[21:21]\r | |
552 | //\r | |
8e3bc754 | 553 | BitIc = RShiftU64 (Addr, 21) & 0x01;\r |
53c71d09 | 554 | //\r |
555 | // imm5c = Addr[20:16] for 5 bits\r | |
556 | //\r | |
8e3bc754 | 557 | BitImm5c = RShiftU64 (Addr, 16) & 0x1F;\r |
53c71d09 | 558 | //\r |
559 | // imm9d = Addr[15:7] for 9 bits\r | |
560 | //\r | |
8e3bc754 | 561 | BitImm9d = RShiftU64 (Addr, 7) & 0x1FF;\r |
53c71d09 | 562 | //\r |
563 | // imm7b = Addr[6:0] for 7 bits\r | |
564 | //\r | |
8e3bc754 | 565 | BitImm7b = Addr & 0x7F;\r |
53c71d09 | 566 | \r |
567 | //\r | |
568 | // Put the EBC entry point in r8, which is the location of the return value\r | |
569 | // for functions.\r | |
570 | //\r | |
571 | RegNum = 8;\r | |
572 | \r | |
573 | //\r | |
574 | // Next is jumbled data, including opcode and rest of address\r | |
575 | //\r | |
8e3bc754 | 576 | Code[2] = LShiftU64 (BitImm7b, 13);\r |
53c71d09 | 577 | Code[2] = Code[2] | LShiftU64 (0x00, 20); // vc\r |
8e3bc754 | 578 | Code[2] = Code[2] | LShiftU64 (BitIc, 21);\r |
579 | Code[2] = Code[2] | LShiftU64 (BitImm5c, 22);\r | |
580 | Code[2] = Code[2] | LShiftU64 (BitImm9d, 27);\r | |
581 | Code[2] = Code[2] | LShiftU64 (BitI, 36);\r | |
53c71d09 | 582 | Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r |
583 | Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r | |
584 | \r | |
585 | WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r | |
586 | \r | |
587 | //\r | |
588 | // *************************** NEXT BUNDLE *********************************\r | |
589 | //\r | |
590 | // Write code bundle for:\r | |
591 | // movl rx = offset_of(EbcInterpret|ExecuteEbcImageEntryPoint)\r | |
592 | //\r | |
593 | // Advance pointer to next bundle, then compute the offset from this bundle\r | |
594 | // to the address of the entry point of the interpreter.\r | |
595 | //\r | |
596 | Ptr += 16;\r | |
366219ab | 597 | if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r |
53c71d09 | 598 | Addr = (UINT64) ExecuteEbcImageEntryPoint;\r |
599 | } else {\r | |
600 | Addr = (UINT64) EbcInterpret;\r | |
601 | }\r | |
602 | //\r | |
603 | // Indirection on Itanium-based systems\r | |
604 | //\r | |
605 | Addr = *(UINT64 *) Addr;\r | |
606 | \r | |
607 | //\r | |
608 | // Now write the code to load the offset into a register\r | |
609 | //\r | |
610 | Code[0] = OPCODE_NOP;\r | |
611 | \r | |
612 | //\r | |
613 | // Next is simply Addr[62:22] (41 bits) of the address\r | |
614 | //\r | |
615 | Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r | |
616 | \r | |
617 | //\r | |
618 | // Extract bits from the address for insertion into the instruction\r | |
619 | // i = Addr[63:63]\r | |
620 | //\r | |
8e3bc754 | 621 | BitI = RShiftU64 (Addr, 63) & 0x01;\r |
53c71d09 | 622 | //\r |
623 | // ic = Addr[21:21]\r | |
624 | //\r | |
8e3bc754 | 625 | BitIc = RShiftU64 (Addr, 21) & 0x01;\r |
53c71d09 | 626 | //\r |
627 | // imm5c = Addr[20:16] for 5 bits\r | |
628 | //\r | |
8e3bc754 | 629 | BitImm5c = RShiftU64 (Addr, 16) & 0x1F;\r |
53c71d09 | 630 | //\r |
631 | // imm9d = Addr[15:7] for 9 bits\r | |
632 | //\r | |
8e3bc754 | 633 | BitImm9d = RShiftU64 (Addr, 7) & 0x1FF;\r |
53c71d09 | 634 | //\r |
635 | // imm7b = Addr[6:0] for 7 bits\r | |
636 | //\r | |
8e3bc754 | 637 | BitImm7b = Addr & 0x7F;\r |
53c71d09 | 638 | \r |
639 | //\r | |
640 | // Put it in r31, a scratch register\r | |
641 | //\r | |
642 | RegNum = 31;\r | |
643 | \r | |
644 | //\r | |
645 | // Next is jumbled data, including opcode and rest of address\r | |
646 | //\r | |
8e3bc754 | 647 | Code[2] = LShiftU64(BitImm7b, 13);\r |
53c71d09 | 648 | Code[2] = Code[2] | LShiftU64 (0x00, 20); // vc\r |
8e3bc754 | 649 | Code[2] = Code[2] | LShiftU64 (BitIc, 21);\r |
650 | Code[2] = Code[2] | LShiftU64 (BitImm5c, 22);\r | |
651 | Code[2] = Code[2] | LShiftU64 (BitImm9d, 27);\r | |
652 | Code[2] = Code[2] | LShiftU64 (BitI, 36);\r | |
53c71d09 | 653 | Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r |
654 | Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r | |
655 | \r | |
656 | WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r | |
657 | \r | |
658 | //\r | |
659 | // *************************** NEXT BUNDLE *********************************\r | |
660 | //\r | |
661 | // Load branch register with EbcInterpret() function offset from the bundle\r | |
662 | // address: mov b6 = RegNum\r | |
663 | //\r | |
664 | // See volume 3 page 4-29 of the Arch. Software Developer's Manual.\r | |
665 | //\r | |
666 | // Advance pointer to next bundle\r | |
667 | //\r | |
668 | Ptr += 16;\r | |
669 | Code[0] = OPCODE_NOP;\r | |
670 | Code[1] = OPCODE_NOP;\r | |
671 | Code[2] = OPCODE_MOV_BX_RX;\r | |
672 | \r | |
673 | //\r | |
674 | // Pick a branch register to use. Then fill in the bits for the branch\r | |
675 | // register and user register (same user register as previous bundle).\r | |
676 | //\r | |
677 | Br = 6;\r | |
678 | Code[2] |= LShiftU64 (Br, 6);\r | |
679 | Code[2] |= LShiftU64 (RegNum, 13);\r | |
680 | WriteBundle ((VOID *) Ptr, 0x0d, Code[0], Code[1], Code[2]);\r | |
681 | \r | |
682 | //\r | |
683 | // *************************** NEXT BUNDLE *********************************\r | |
684 | //\r | |
685 | // Now do the branch: (p0) br.cond.sptk.few b6\r | |
686 | //\r | |
687 | // Advance pointer to next bundle.\r | |
688 | // Fill in the bits for the branch register (same reg as previous bundle)\r | |
689 | //\r | |
690 | Ptr += 16;\r | |
691 | Code[0] = OPCODE_NOP;\r | |
692 | Code[1] = OPCODE_NOP;\r | |
693 | Code[2] = OPCODE_BR_COND_SPTK_FEW;\r | |
694 | Code[2] |= LShiftU64 (Br, 13);\r | |
695 | WriteBundle ((VOID *) Ptr, 0x1d, Code[0], Code[1], Code[2]);\r | |
696 | \r | |
697 | //\r | |
698 | // Add the thunk to our list of allocated thunks so we can do some cleanup\r | |
699 | // when the image is unloaded. Do this last since the Add function flushes\r | |
700 | // the instruction cache for us.\r | |
701 | //\r | |
702 | EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r | |
703 | \r | |
704 | //\r | |
705 | // Done\r | |
706 | //\r | |
707 | return EFI_SUCCESS;\r | |
708 | }\r | |
709 | \r | |
fb0b259e | 710 | \r |
711 | /**\r | |
712 | Given raw bytes of Itanium based code, format them into a bundle and\r | |
713 | write them out.\r | |
714 | \r | |
8e3bc754 | 715 | @param MemPtr pointer to memory location to write the bundles\r |
716 | to.\r | |
717 | @param Template 5-bit template.\r | |
718 | @param Slot0 Instruction slot 0 data for the bundle.\r | |
719 | @param Slot1 Instruction slot 1 data for the bundle.\r | |
720 | @param Slot2 Instruction slot 2 data for the bundle.\r | |
fb0b259e | 721 | \r |
722 | @retval EFI_INVALID_PARAMETER Pointer is not aligned\r | |
8e3bc754 | 723 | @retval EFI_INVALID_PARAMETER No more than 5 bits in template\r |
724 | @retval EFI_INVALID_PARAMETER More than 41 bits used in code\r | |
fb0b259e | 725 | @retval EFI_SUCCESS All data is written.\r |
726 | \r | |
727 | **/\r | |
53c71d09 | 728 | EFI_STATUS\r |
729 | WriteBundle (\r | |
730 | IN VOID *MemPtr,\r | |
731 | IN UINT8 Template,\r | |
732 | IN UINT64 Slot0,\r | |
733 | IN UINT64 Slot1,\r | |
734 | IN UINT64 Slot2\r | |
735 | )\r | |
53c71d09 | 736 | {\r |
737 | UINT8 *BPtr;\r | |
738 | UINT32 Index;\r | |
739 | UINT64 Low64;\r | |
740 | UINT64 High64;\r | |
741 | \r | |
742 | //\r | |
743 | // Verify pointer is aligned\r | |
744 | //\r | |
745 | if ((UINT64) MemPtr & 0xF) {\r | |
746 | return EFI_INVALID_PARAMETER;\r | |
747 | }\r | |
748 | //\r | |
749 | // Verify no more than 5 bits in template\r | |
750 | //\r | |
366219ab | 751 | if ((Template &~0x1F) != 0) {\r |
53c71d09 | 752 | return EFI_INVALID_PARAMETER;\r |
753 | }\r | |
754 | //\r | |
755 | // Verify max of 41 bits used in code\r | |
756 | //\r | |
366219ab | 757 | if (((Slot0 | Slot1 | Slot2) &~0x1ffffffffff) != 0) {\r |
53c71d09 | 758 | return EFI_INVALID_PARAMETER;\r |
759 | }\r | |
760 | \r | |
761 | Low64 = LShiftU64 (Slot1, 46);\r | |
762 | Low64 = Low64 | LShiftU64 (Slot0, 5) | Template;\r | |
763 | \r | |
764 | High64 = RShiftU64 (Slot1, 18);\r | |
765 | High64 = High64 | LShiftU64 (Slot2, 23);\r | |
766 | \r | |
767 | //\r | |
768 | // Now write it all out\r | |
769 | //\r | |
770 | BPtr = (UINT8 *) MemPtr;\r | |
771 | for (Index = 0; Index < 8; Index++) {\r | |
772 | *BPtr = (UINT8) Low64;\r | |
773 | Low64 = RShiftU64 (Low64, 8);\r | |
774 | BPtr++;\r | |
775 | }\r | |
776 | \r | |
777 | for (Index = 0; Index < 8; Index++) {\r | |
778 | *BPtr = (UINT8) High64;\r | |
779 | High64 = RShiftU64 (High64, 8);\r | |
780 | BPtr++;\r | |
781 | }\r | |
782 | \r | |
783 | return EFI_SUCCESS;\r | |
784 | }\r | |
785 | \r | |
53c71d09 | 786 | \r |
fb0b259e | 787 | /**\r |
788 | This function is called to execute an EBC CALLEX instruction.\r | |
53c71d09 | 789 | The function check the callee's content to see whether it is common native\r |
790 | code or a thunk to another piece of EBC code.\r | |
791 | If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r | |
792 | otherwise, set the VM->IP to target EBC code directly to avoid another VM\r | |
793 | be startup which cost time and stack space.\r | |
53c71d09 | 794 | \r |
8e3bc754 | 795 | @param VmPtr Pointer to a VM context.\r |
796 | @param FuncAddr Callee's address\r | |
797 | @param NewStackPointer New stack pointer after the call\r | |
798 | @param FramePtr New frame pointer after the call\r | |
799 | @param Size The size of call instruction\r | |
53c71d09 | 800 | \r |
fb0b259e | 801 | **/\r |
802 | VOID\r | |
803 | EbcLLCALLEX (\r | |
804 | IN VM_CONTEXT *VmPtr,\r | |
805 | IN UINTN FuncAddr,\r | |
806 | IN UINTN NewStackPointer,\r | |
807 | IN VOID *FramePtr,\r | |
808 | IN UINT8 Size\r | |
809 | )\r | |
53c71d09 | 810 | {\r |
811 | UINTN IsThunk;\r | |
812 | UINTN TargetEbcAddr;\r | |
813 | UINTN CodeOne18;\r | |
814 | UINTN CodeOne23;\r | |
815 | UINTN CodeTwoI;\r | |
816 | UINTN CodeTwoIc;\r | |
817 | UINTN CodeTwo7b;\r | |
818 | UINTN CodeTwo5c;\r | |
819 | UINTN CodeTwo9d;\r | |
820 | UINTN CalleeAddr;\r | |
821 | \r | |
822 | IsThunk = 1;\r | |
823 | TargetEbcAddr = 0;\r | |
824 | \r | |
825 | //\r | |
826 | // FuncAddr points to the descriptor of the target instructions.\r | |
827 | //\r | |
828 | CalleeAddr = *((UINT64 *)FuncAddr);\r | |
829 | \r | |
830 | //\r | |
831 | // Processor specific code to check whether the callee is a thunk to EBC.\r | |
832 | //\r | |
833 | if (*((UINT64 *)CalleeAddr) != 0xBCCA000100000005) {\r | |
834 | IsThunk = 0;\r | |
835 | goto Action;\r | |
836 | }\r | |
837 | if (*((UINT64 *)CalleeAddr + 1) != 0x697623C1004A112E) {\r | |
838 | IsThunk = 0;\r | |
839 | goto Action;\r | |
840 | }\r | |
841 | \r | |
842 | CodeOne18 = RShiftU64 (*((UINT64 *)CalleeAddr + 2), 46) & 0x3FFFF;\r | |
843 | CodeOne23 = (*((UINT64 *)CalleeAddr + 3)) & 0x7FFFFF;\r | |
844 | CodeTwoI = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 59) & 0x1;\r | |
845 | CodeTwoIc = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 44) & 0x1;\r | |
846 | CodeTwo7b = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 36) & 0x7F;\r | |
847 | CodeTwo5c = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 45) & 0x1F;\r | |
848 | CodeTwo9d = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 50) & 0x1FF;\r | |
849 | \r | |
850 | TargetEbcAddr = CodeTwo7b;\r | |
851 | TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwo9d, 7);\r | |
852 | TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwo5c, 16);\r | |
853 | TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwoIc, 21);\r | |
854 | TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeOne18, 22);\r | |
855 | TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeOne23, 40);\r | |
856 | TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwoI, 63);\r | |
857 | \r | |
858 | Action:\r | |
859 | if (IsThunk == 1){\r | |
860 | //\r | |
861 | // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r | |
862 | // put our return address and frame pointer on the VM stack.\r | |
863 | // Then set the VM's IP to new EBC code.\r | |
864 | //\r | |
1ccdbf2a | 865 | VmPtr->Gpr[0] -= 8;\r |
866 | VmWriteMemN (VmPtr, (UINTN) VmPtr->Gpr[0], (UINTN) FramePtr);\r | |
867 | VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->Gpr[0];\r | |
868 | VmPtr->Gpr[0] -= 8;\r | |
869 | VmWriteMem64 (VmPtr, (UINTN) VmPtr->Gpr[0], (UINT64) (VmPtr->Ip + Size));\r | |
53c71d09 | 870 | \r |
871 | VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r | |
872 | } else {\r | |
873 | //\r | |
fa97cbf4 JY |
874 | // The callee is not a thunk to EBC, call native code,\r |
875 | // and get return value.\r | |
53c71d09 | 876 | //\r |
fa97cbf4 | 877 | VmPtr->Gpr[7] = EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r |
53c71d09 | 878 | \r |
879 | //\r | |
fa97cbf4 | 880 | // Advance the IP.\r |
53c71d09 | 881 | //\r |
53c71d09 | 882 | VmPtr->Ip += Size;\r |
883 | }\r | |
884 | }\r |