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