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