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