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