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