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