]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/Ebc/Dxe/EbcExecute.c
13e2d7c08f3aefed8c71bd36a51912dd4d777e42
[mirror_edk2.git] / EdkModulePkg / Universal / Ebc / Dxe / EbcExecute.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 EbcExecute.c
15
16 Abstract:
17
18 Contains code that implements the virtual machine.
19
20 --*/
21
22 #include "EbcInt.h"
23 #include "EbcExecute.h"
24
25 //
26 // VM major/minor version
27 //
28 #define VM_MAJOR_VERSION 1
29 #define VM_MINOR_VERSION 0
30
31 //
32 // Define some useful data size constants to allow switch statements based on
33 // size of operands or data.
34 //
35 #define DATA_SIZE_INVALID 0
36 #define DATA_SIZE_8 1
37 #define DATA_SIZE_16 2
38 #define DATA_SIZE_32 4
39 #define DATA_SIZE_64 8
40 #define DATA_SIZE_N 48 // 4 or 8
41 //
42 // Structure we'll use to dispatch opcodes to execute functions.
43 //
44 typedef struct {
45 EFI_STATUS (*ExecuteFunction) (IN VM_CONTEXT * VmPtr);
46 }
47 VM_TABLE_ENTRY;
48
49 typedef
50 UINT64
51 (*DATA_MANIP_EXEC_FUNCTION) (
52 IN VM_CONTEXT * VmPtr,
53 IN UINT64 Op1,
54 IN UINT64 Op2
55 );
56
57 STATIC
58 INT16
59 VmReadIndex16 (
60 IN VM_CONTEXT *VmPtr,
61 IN UINT32 CodeOffset
62 );
63
64 STATIC
65 INT32
66 VmReadIndex32 (
67 IN VM_CONTEXT *VmPtr,
68 IN UINT32 CodeOffset
69 );
70
71 STATIC
72 INT64
73 VmReadIndex64 (
74 IN VM_CONTEXT *VmPtr,
75 IN UINT32 CodeOffset
76 );
77
78 STATIC
79 UINT8
80 VmReadMem8 (
81 IN VM_CONTEXT *VmPtr,
82 IN UINTN Addr
83 );
84
85 STATIC
86 UINT16
87 VmReadMem16 (
88 IN VM_CONTEXT *VmPtr,
89 IN UINTN Addr
90 );
91
92 STATIC
93 UINT32
94 VmReadMem32 (
95 IN VM_CONTEXT *VmPtr,
96 IN UINTN Addr
97 );
98
99 STATIC
100 UINT64
101 VmReadMem64 (
102 IN VM_CONTEXT *VmPtr,
103 IN UINTN Addr
104 );
105
106 STATIC
107 UINTN
108 VmReadMemN (
109 IN VM_CONTEXT *VmPtr,
110 IN UINTN Addr
111 );
112
113 STATIC
114 EFI_STATUS
115 VmWriteMem8 (
116 IN VM_CONTEXT *VmPtr,
117 UINTN Addr,
118 IN UINT8 Data
119 );
120
121 STATIC
122 EFI_STATUS
123 VmWriteMem16 (
124 IN VM_CONTEXT *VmPtr,
125 UINTN Addr,
126 IN UINT16 Data
127 );
128
129 STATIC
130 EFI_STATUS
131 VmWriteMem32 (
132 IN VM_CONTEXT *VmPtr,
133 UINTN Addr,
134 IN UINT32 Data
135 );
136
137 EFI_STATUS
138 VmWriteMemN (
139 IN VM_CONTEXT *VmPtr,
140 UINTN Addr,
141 IN UINTN Data
142 );
143
144 EFI_STATUS
145 VmWriteMem64 (
146 IN VM_CONTEXT *VmPtr,
147 UINTN Addr,
148 IN UINT64 Data
149 );
150
151 STATIC
152 UINT16
153 VmReadCode16 (
154 IN VM_CONTEXT *VmPtr,
155 IN UINT32 Offset
156 );
157
158 STATIC
159 UINT32
160 VmReadCode32 (
161 IN VM_CONTEXT *VmPtr,
162 IN UINT32 Offset
163 );
164
165 STATIC
166 UINT64
167 VmReadCode64 (
168 IN VM_CONTEXT *VmPtr,
169 IN UINT32 Offset
170 );
171
172 STATIC
173 INT8
174 VmReadImmed8 (
175 IN VM_CONTEXT *VmPtr,
176 IN UINT32 Offset
177 );
178
179 STATIC
180 INT16
181 VmReadImmed16 (
182 IN VM_CONTEXT *VmPtr,
183 IN UINT32 Offset
184 );
185
186 STATIC
187 INT32
188 VmReadImmed32 (
189 IN VM_CONTEXT *VmPtr,
190 IN UINT32 Offset
191 );
192
193 STATIC
194 INT64
195 VmReadImmed64 (
196 IN VM_CONTEXT *VmPtr,
197 IN UINT32 Offset
198 );
199
200 STATIC
201 UINTN
202 ConvertStackAddr (
203 IN VM_CONTEXT *VmPtr,
204 IN UINTN Addr
205 );
206
207 STATIC
208 EFI_STATUS
209 ExecuteDataManip (
210 IN VM_CONTEXT *VmPtr,
211 IN BOOLEAN IsSignedOperation
212 );
213
214 //
215 // Functions that execute VM opcodes
216 //
217 STATIC
218 EFI_STATUS
219 ExecuteBREAK (
220 IN VM_CONTEXT *VmPtr
221 );
222
223 STATIC
224 EFI_STATUS
225 ExecuteJMP (
226 IN VM_CONTEXT *VmPtr
227 );
228
229 STATIC
230 EFI_STATUS
231 ExecuteJMP8 (
232 IN VM_CONTEXT *VmPtr
233 );
234
235 STATIC
236 EFI_STATUS
237 ExecuteCALL (
238 IN VM_CONTEXT *VmPtr
239 );
240
241 STATIC
242 EFI_STATUS
243 ExecuteRET (
244 IN VM_CONTEXT *VmPtr
245 );
246
247 STATIC
248 EFI_STATUS
249 ExecuteCMP (
250 IN VM_CONTEXT *VmPtr
251 );
252
253 STATIC
254 EFI_STATUS
255 ExecuteCMPI (
256 IN VM_CONTEXT *VmPtr
257 );
258
259 STATIC
260 EFI_STATUS
261 ExecuteMOVxx (
262 IN VM_CONTEXT *VmPtr
263 );
264
265 STATIC
266 EFI_STATUS
267 ExecuteMOVI (
268 IN VM_CONTEXT *VmPtr
269 );
270
271 STATIC
272 EFI_STATUS
273 ExecuteMOVIn (
274 IN VM_CONTEXT *VmPtr
275 );
276
277 STATIC
278 EFI_STATUS
279 ExecuteMOVREL (
280 IN VM_CONTEXT *VmPtr
281 );
282
283 STATIC
284 EFI_STATUS
285 ExecutePUSHn (
286 IN VM_CONTEXT *VmPtr
287 );
288
289 STATIC
290 EFI_STATUS
291 ExecutePUSH (
292 IN VM_CONTEXT *VmPtr
293 );
294
295 STATIC
296 EFI_STATUS
297 ExecutePOPn (
298 IN VM_CONTEXT *VmPtr
299 );
300
301 STATIC
302 EFI_STATUS
303 ExecutePOP (
304 IN VM_CONTEXT *VmPtr
305 );
306
307 STATIC
308 EFI_STATUS
309 ExecuteSignedDataManip (
310 IN VM_CONTEXT *VmPtr
311 );
312
313 STATIC
314 EFI_STATUS
315 ExecuteUnsignedDataManip (
316 IN VM_CONTEXT *VmPtr
317 );
318
319 STATIC
320 EFI_STATUS
321 ExecuteLOADSP (
322 IN VM_CONTEXT *VmPtr
323 );
324
325 STATIC
326 EFI_STATUS
327 ExecuteSTORESP (
328 IN VM_CONTEXT *VmPtr
329 );
330
331 STATIC
332 EFI_STATUS
333 ExecuteMOVsnd (
334 IN VM_CONTEXT *VmPtr
335 );
336
337 STATIC
338 EFI_STATUS
339 ExecuteMOVsnw (
340 IN VM_CONTEXT *VmPtr
341 );
342
343 //
344 // Data manipulation subfunctions
345 //
346 STATIC
347 UINT64
348 ExecuteNOT (
349 IN VM_CONTEXT *VmPtr,
350 IN UINT64 Op1,
351 IN UINT64 Op2
352 );
353
354 STATIC
355 UINT64
356 ExecuteNEG (
357 IN VM_CONTEXT *VmPtr,
358 IN UINT64 Op1,
359 IN UINT64 Op2
360 );
361
362 STATIC
363 UINT64
364 ExecuteADD (
365 IN VM_CONTEXT *VmPtr,
366 IN UINT64 Op1,
367 IN UINT64 Op2
368 );
369
370 STATIC
371 UINT64
372 ExecuteSUB (
373 IN VM_CONTEXT *VmPtr,
374 IN UINT64 Op1,
375 IN UINT64 Op2
376 );
377
378 STATIC
379 UINT64
380 ExecuteMUL (
381 IN VM_CONTEXT *VmPtr,
382 IN UINT64 Op1,
383 IN UINT64 Op2
384 );
385
386 STATIC
387 UINT64
388 ExecuteMULU (
389 IN VM_CONTEXT *VmPtr,
390 IN UINT64 Op1,
391 IN UINT64 Op2
392 );
393
394 STATIC
395 UINT64
396 ExecuteDIV (
397 IN VM_CONTEXT *VmPtr,
398 IN UINT64 Op1,
399 IN UINT64 Op2
400 );
401
402 STATIC
403 UINT64
404 ExecuteDIVU (
405 IN VM_CONTEXT *VmPtr,
406 IN UINT64 Op1,
407 IN UINT64 Op2
408 );
409
410 STATIC
411 UINT64
412 ExecuteMOD (
413 IN VM_CONTEXT *VmPtr,
414 IN UINT64 Op1,
415 IN UINT64 Op2
416 );
417
418 STATIC
419 UINT64
420 ExecuteMODU (
421 IN VM_CONTEXT *VmPtr,
422 IN UINT64 Op1,
423 IN UINT64 Op2
424 );
425
426 STATIC
427 UINT64
428 ExecuteAND (
429 IN VM_CONTEXT *VmPtr,
430 IN UINT64 Op1,
431 IN UINT64 Op2
432 );
433
434 STATIC
435 UINT64
436 ExecuteOR (
437 IN VM_CONTEXT *VmPtr,
438 IN UINT64 Op1,
439 IN UINT64 Op2
440 );
441
442 STATIC
443 UINT64
444 ExecuteXOR (
445 IN VM_CONTEXT *VmPtr,
446 IN UINT64 Op1,
447 IN UINT64 Op2
448 );
449
450 STATIC
451 UINT64
452 ExecuteSHL (
453 IN VM_CONTEXT *VmPtr,
454 IN UINT64 Op1,
455 IN UINT64 Op2
456 );
457
458 STATIC
459 UINT64
460 ExecuteSHR (
461 IN VM_CONTEXT *VmPtr,
462 IN UINT64 Op1,
463 IN UINT64 Op2
464 );
465
466 STATIC
467 UINT64
468 ExecuteASHR (
469 IN VM_CONTEXT *VmPtr,
470 IN UINT64 Op1,
471 IN UINT64 Op2
472 );
473
474 STATIC
475 UINT64
476 ExecuteEXTNDB (
477 IN VM_CONTEXT *VmPtr,
478 IN UINT64 Op1,
479 IN UINT64 Op2
480 );
481
482 STATIC
483 UINT64
484 ExecuteEXTNDW (
485 IN VM_CONTEXT *VmPtr,
486 IN UINT64 Op1,
487 IN UINT64 Op2
488 );
489
490 STATIC
491 UINT64
492 ExecuteEXTNDD (
493 IN VM_CONTEXT *VmPtr,
494 IN UINT64 Op1,
495 IN UINT64 Op2
496 );
497
498 //
499 // Once we retrieve the operands for the data manipulation instructions,
500 // call these functions to perform the operation.
501 //
502 static CONST DATA_MANIP_EXEC_FUNCTION mDataManipDispatchTable[] = {
503 ExecuteNOT,
504 ExecuteNEG,
505 ExecuteADD,
506 ExecuteSUB,
507 ExecuteMUL,
508 ExecuteMULU,
509 ExecuteDIV,
510 ExecuteDIVU,
511 ExecuteMOD,
512 ExecuteMODU,
513 ExecuteAND,
514 ExecuteOR,
515 ExecuteXOR,
516 ExecuteSHL,
517 ExecuteSHR,
518 ExecuteASHR,
519 ExecuteEXTNDB,
520 ExecuteEXTNDW,
521 ExecuteEXTNDD,
522 };
523
524 static CONST VM_TABLE_ENTRY mVmOpcodeTable[] = {
525 { ExecuteBREAK }, // opcode 0x00
526 { ExecuteJMP }, // opcode 0x01
527 { ExecuteJMP8 }, // opcode 0x02
528 { ExecuteCALL }, // opcode 0x03
529 { ExecuteRET }, // opcode 0x04
530 { ExecuteCMP }, // opcode 0x05 CMPeq
531 { ExecuteCMP }, // opcode 0x06 CMPlte
532 { ExecuteCMP }, // opcode 0x07 CMPgte
533 { ExecuteCMP }, // opcode 0x08 CMPulte
534 { ExecuteCMP }, // opcode 0x09 CMPugte
535 { ExecuteUnsignedDataManip }, // opcode 0x0A NOT
536 { ExecuteSignedDataManip }, // opcode 0x0B NEG
537 { ExecuteSignedDataManip }, // opcode 0x0C ADD
538 { ExecuteSignedDataManip }, // opcode 0x0D SUB
539 { ExecuteSignedDataManip }, // opcode 0x0E MUL
540 { ExecuteUnsignedDataManip }, // opcode 0x0F MULU
541 { ExecuteSignedDataManip }, // opcode 0x10 DIV
542 { ExecuteUnsignedDataManip }, // opcode 0x11 DIVU
543 { ExecuteSignedDataManip }, // opcode 0x12 MOD
544 { ExecuteUnsignedDataManip }, // opcode 0x13 MODU
545 { ExecuteUnsignedDataManip }, // opcode 0x14 AND
546 { ExecuteUnsignedDataManip }, // opcode 0x15 OR
547 { ExecuteUnsignedDataManip }, // opcode 0x16 XOR
548 { ExecuteUnsignedDataManip }, // opcode 0x17 SHL
549 { ExecuteUnsignedDataManip }, // opcode 0x18 SHR
550 { ExecuteSignedDataManip }, // opcode 0x19 ASHR
551 { ExecuteUnsignedDataManip }, // opcode 0x1A EXTNDB
552 { ExecuteUnsignedDataManip }, // opcode 0x1B EXTNDW
553 { ExecuteUnsignedDataManip }, // opcode 0x1C EXTNDD
554 { ExecuteMOVxx }, // opcode 0x1D MOVBW
555 { ExecuteMOVxx }, // opcode 0x1E MOVWW
556 { ExecuteMOVxx }, // opcode 0x1F MOVDW
557 { ExecuteMOVxx }, // opcode 0x20 MOVQW
558 { ExecuteMOVxx }, // opcode 0x21 MOVBD
559 { ExecuteMOVxx }, // opcode 0x22 MOVWD
560 { ExecuteMOVxx }, // opcode 0x23 MOVDD
561 { ExecuteMOVxx }, // opcode 0x24 MOVQD
562 { ExecuteMOVsnw }, // opcode 0x25 MOVsnw
563 { ExecuteMOVsnd }, // opcode 0x26 MOVsnd
564 { NULL }, // opcode 0x27
565 { ExecuteMOVxx }, // opcode 0x28 MOVqq
566 { ExecuteLOADSP }, // opcode 0x29 LOADSP SP1, R2
567 { ExecuteSTORESP }, // opcode 0x2A STORESP R1, SP2
568 { ExecutePUSH }, // opcode 0x2B PUSH {@}R1 [imm16]
569 { ExecutePOP }, // opcode 0x2C POP {@}R1 [imm16]
570 { ExecuteCMPI }, // opcode 0x2D CMPIEQ
571 { ExecuteCMPI }, // opcode 0x2E CMPILTE
572 { ExecuteCMPI }, // opcode 0x2F CMPIGTE
573 { ExecuteCMPI }, // opcode 0x30 CMPIULTE
574 { ExecuteCMPI }, // opcode 0x31 CMPIUGTE
575 { ExecuteMOVxx }, // opcode 0x32 MOVN
576 { ExecuteMOVxx }, // opcode 0x33 MOVND
577 { NULL }, // opcode 0x34
578 { ExecutePUSHn }, // opcode 0x35
579 { ExecutePOPn }, // opcode 0x36
580 { ExecuteMOVI }, // opcode 0x37 - mov immediate data
581 { ExecuteMOVIn }, // opcode 0x38 - mov immediate natural
582 { ExecuteMOVREL } // opcode 0x39 - move data relative to PC
583 };
584
585 //
586 // Length of JMP instructions, depending on upper two bits of opcode.
587 //
588 static CONST UINT8 mJMPLen[] = { 2, 2, 6, 10 };
589
590 //
591 // Simple Debugger Protocol GUID
592 //
593 EFI_GUID mEbcSimpleDebuggerProtocolGuid = EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID;
594
595 EFI_STATUS
596 EbcExecuteInstructions (
597 IN EFI_EBC_VM_TEST_PROTOCOL *This,
598 IN VM_CONTEXT *VmPtr,
599 IN OUT UINTN *InstructionCount
600 )
601 /*++
602
603 Routine Description:
604
605 Given a pointer to a new VM context, execute one or more instructions. This
606 function is only used for test purposes via the EBC VM test protocol.
607
608 Arguments:
609
610 This - pointer to protocol interface
611 VmPtr - pointer to a VM context
612 InstructionCount - how many instructions to execute. 0 if don't count.
613
614 Returns:
615
616 EFI_UNSUPPORTED
617 EFI_SUCCESS
618
619 --*/
620 {
621 UINTN ExecFunc;
622 EFI_STATUS Status;
623 UINTN InstructionsLeft;
624 UINTN SavedInstructionCount;
625
626 Status = EFI_SUCCESS;
627
628 if (*InstructionCount == 0) {
629 InstructionsLeft = 1;
630 } else {
631 InstructionsLeft = *InstructionCount;
632 }
633
634 SavedInstructionCount = *InstructionCount;
635 *InstructionCount = 0;
636
637 //
638 // Index into the opcode table using the opcode byte for this instruction.
639 // This gives you the execute function, which we first test for null, then
640 // call it if it's not null.
641 //
642 while (InstructionsLeft != 0) {
643 ExecFunc = (UINTN) mVmOpcodeTable[(*VmPtr->Ip & 0x3F)].ExecuteFunction;
644 if (ExecFunc == (UINTN) NULL) {
645 EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);
646 return EFI_UNSUPPORTED;
647 } else {
648 mVmOpcodeTable[(*VmPtr->Ip & 0x3F)].ExecuteFunction (VmPtr);
649 *InstructionCount = *InstructionCount + 1;
650 }
651
652 //
653 // Decrement counter if applicable
654 //
655 if (SavedInstructionCount != 0) {
656 InstructionsLeft--;
657 }
658 }
659
660 return Status;
661 }
662
663 EFI_STATUS
664 EbcExecute (
665 IN VM_CONTEXT *VmPtr
666 )
667 /*++
668
669 Routine Description:
670
671 Execute an EBC image from an entry point or from a published protocol.
672
673 Arguments:
674
675 VmPtr - pointer to prepared VM context.
676
677 Returns:
678
679 Standard EBC status.
680
681 --*/
682 {
683 UINTN ExecFunc;
684 UINT8 StackCorrupted;
685 EFI_STATUS Status;
686 EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL *EbcSimpleDebugger;
687
688 EbcSimpleDebugger = NULL;
689 Status = EFI_SUCCESS;
690 StackCorrupted = 0;
691
692 //
693 // Make sure the magic value has been put on the stack before we got here.
694 //
695 if (*VmPtr->StackMagicPtr != (UINTN) VM_STACK_KEY_VALUE) {
696 StackCorrupted = 1;
697 }
698
699 VmPtr->FramePtr = (VOID *) ((UINT8 *) (UINTN) VmPtr->R[0] + 8);
700
701 //
702 // Try to get the debug support for EBC
703 //
704 DEBUG_CODE_BEGIN ();
705 Status = gBS->LocateProtocol (
706 &mEbcSimpleDebuggerProtocolGuid,
707 NULL,
708 (VOID **) &EbcSimpleDebugger
709 );
710 if (EFI_ERROR (Status)) {
711 EbcSimpleDebugger = NULL;
712 }
713 DEBUG_CODE_END ();
714
715 //
716 // Save the start IP for debug. For example, if we take an exception we
717 // can print out the location of the exception relative to the entry point,
718 // which could then be used in a disassembly listing to find the problem.
719 //
720 VmPtr->EntryPoint = (VOID *) VmPtr->Ip;
721
722 //
723 // We'll wait for this flag to know when we're done. The RET
724 // instruction sets it if it runs out of stack.
725 //
726 VmPtr->StopFlags = 0;
727 while (!(VmPtr->StopFlags & STOPFLAG_APP_DONE)) {
728 //
729 // If we've found a simple debugger protocol, call it
730 //
731 DEBUG_CODE_BEGIN ();
732 if (EbcSimpleDebugger != NULL) {
733 EbcSimpleDebugger->Debugger (EbcSimpleDebugger, VmPtr);
734 }
735 DEBUG_CODE_END ();
736
737 //
738 // Verify the opcode is in range. Otherwise generate an exception.
739 //
740 if ((*VmPtr->Ip & OPCODE_M_OPCODE) >= (sizeof (mVmOpcodeTable) / sizeof (mVmOpcodeTable[0]))) {
741 EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);
742 Status = EFI_UNSUPPORTED;
743 goto Done;
744 }
745 //
746 // Use the opcode bits to index into the opcode dispatch table. If the
747 // function pointer is null then generate an exception.
748 //
749 ExecFunc = (UINTN) mVmOpcodeTable[(*VmPtr->Ip & OPCODE_M_OPCODE)].ExecuteFunction;
750 if (ExecFunc == (UINTN) NULL) {
751 EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);
752 Status = EFI_UNSUPPORTED;
753 goto Done;
754 }
755 //
756 // The EBC VM is a strongly ordered processor, so perform a fence operation before
757 // and after each instruction is executed.
758 //
759 MemoryFence ();
760
761 mVmOpcodeTable[(*VmPtr->Ip & OPCODE_M_OPCODE)].ExecuteFunction (VmPtr);
762
763 MemoryFence ();
764
765 //
766 // If the step flag is set, signal an exception and continue. We don't
767 // clear it here. Assuming the debugger is responsible for clearing it.
768 //
769 if (VMFLAG_ISSET (VmPtr, VMFLAGS_STEP)) {
770 EbcDebugSignalException (EXCEPT_EBC_STEP, EXCEPTION_FLAG_NONE, VmPtr);
771 }
772 //
773 // Make sure stack has not been corrupted. Only report it once though.
774 //
775 if (!StackCorrupted && (*VmPtr->StackMagicPtr != (UINTN) VM_STACK_KEY_VALUE)) {
776 EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT, EXCEPTION_FLAG_FATAL, VmPtr);
777 StackCorrupted = 1;
778 }
779 }
780
781 Done:
782 return Status;
783 }
784
785 STATIC
786 EFI_STATUS
787 ExecuteMOVxx (
788 IN VM_CONTEXT *VmPtr
789 )
790 /*++
791
792 Routine Description:
793
794 Execute the MOVxx instructions.
795
796 Arguments:
797
798 VmPtr - pointer to a VM context.
799
800 Returns:
801
802 EFI_UNSUPPORTED
803 EFI_SUCCESS
804
805 Instruction format:
806
807 MOV[b|w|d|q|n]{w|d} {@}R1 {Index16|32}, {@}R2 {Index16|32}
808 MOVqq {@}R1 {Index64}, {@}R2 {Index64}
809
810 Copies contents of [R2] -> [R1], zero extending where required.
811
812 First character indicates the size of the move.
813 Second character indicates the size of the index(s).
814
815 Invalid to have R1 direct with index.
816
817 --*/
818 {
819 UINT8 Opcode;
820 UINT8 OpcMasked;
821 UINT8 Operands;
822 UINT8 Size;
823 UINT8 MoveSize;
824 INT16 Index16;
825 INT32 Index32;
826 INT64 Index64Op1;
827 INT64 Index64Op2;
828 UINT64 Data64;
829 UINT64 DataMask;
830 UINTN Source;
831
832 Opcode = GETOPCODE (VmPtr);
833 OpcMasked = (UINT8) (Opcode & OPCODE_M_OPCODE);
834
835 //
836 // Get the operands byte so we can get R1 and R2
837 //
838 Operands = GETOPERANDS (VmPtr);
839
840 //
841 // Assume no indexes
842 //
843 Index64Op1 = 0;
844 Index64Op2 = 0;
845 Data64 = 0;
846
847 //
848 // Determine if we have an index/immediate data. Base instruction size
849 // is 2 (opcode + operands). Add to this size each index specified.
850 //
851 Size = 2;
852 if (Opcode & (OPCODE_M_IMMED_OP1 | OPCODE_M_IMMED_OP2)) {
853 //
854 // Determine size of the index from the opcode. Then get it.
855 //
856 if ((OpcMasked <= OPCODE_MOVQW) || (OpcMasked == OPCODE_MOVNW)) {
857 //
858 // MOVBW, MOVWW, MOVDW, MOVQW, and MOVNW have 16-bit immediate index.
859 // Get one or both index values.
860 //
861 if (Opcode & OPCODE_M_IMMED_OP1) {
862 Index16 = VmReadIndex16 (VmPtr, 2);
863 Index64Op1 = (INT64) Index16;
864 Size += sizeof (UINT16);
865 }
866
867 if (Opcode & OPCODE_M_IMMED_OP2) {
868 Index16 = VmReadIndex16 (VmPtr, Size);
869 Index64Op2 = (INT64) Index16;
870 Size += sizeof (UINT16);
871 }
872 } else if ((OpcMasked <= OPCODE_MOVQD) || (OpcMasked == OPCODE_MOVND)) {
873 //
874 // MOVBD, MOVWD, MOVDD, MOVQD, and MOVND have 32-bit immediate index
875 //
876 if (Opcode & OPCODE_M_IMMED_OP1) {
877 Index32 = VmReadIndex32 (VmPtr, 2);
878 Index64Op1 = (INT64) Index32;
879 Size += sizeof (UINT32);
880 }
881
882 if (Opcode & OPCODE_M_IMMED_OP2) {
883 Index32 = VmReadIndex32 (VmPtr, Size);
884 Index64Op2 = (INT64) Index32;
885 Size += sizeof (UINT32);
886 }
887 } else if (OpcMasked == OPCODE_MOVQQ) {
888 //
889 // MOVqq -- only form with a 64-bit index
890 //
891 if (Opcode & OPCODE_M_IMMED_OP1) {
892 Index64Op1 = VmReadIndex64 (VmPtr, 2);
893 Size += sizeof (UINT64);
894 }
895
896 if (Opcode & OPCODE_M_IMMED_OP2) {
897 Index64Op2 = VmReadIndex64 (VmPtr, Size);
898 Size += sizeof (UINT64);
899 }
900 } else {
901 //
902 // Obsolete MOVBQ, MOVWQ, MOVDQ, and MOVNQ have 64-bit immediate index
903 //
904 EbcDebugSignalException (
905 EXCEPT_EBC_INSTRUCTION_ENCODING,
906 EXCEPTION_FLAG_FATAL,
907 VmPtr
908 );
909 return EFI_UNSUPPORTED;
910 }
911 }
912 //
913 // Determine the size of the move, and create a mask for it so we can
914 // clear unused bits.
915 //
916 if ((OpcMasked == OPCODE_MOVBW) || (OpcMasked == OPCODE_MOVBD)) {
917 MoveSize = DATA_SIZE_8;
918 DataMask = 0xFF;
919 } else if ((OpcMasked == OPCODE_MOVWW) || (OpcMasked == OPCODE_MOVWD)) {
920 MoveSize = DATA_SIZE_16;
921 DataMask = 0xFFFF;
922 } else if ((OpcMasked == OPCODE_MOVDW) || (OpcMasked == OPCODE_MOVDD)) {
923 MoveSize = DATA_SIZE_32;
924 DataMask = 0xFFFFFFFF;
925 } else if ((OpcMasked == OPCODE_MOVQW) || (OpcMasked == OPCODE_MOVQD) || (OpcMasked == OPCODE_MOVQQ)) {
926 MoveSize = DATA_SIZE_64;
927 DataMask = (UINT64)~0;
928 } else if ((OpcMasked == OPCODE_MOVNW) || (OpcMasked == OPCODE_MOVND)) {
929 MoveSize = DATA_SIZE_N;
930 DataMask = (UINT64)~0 >> (64 - 8 * sizeof (UINTN));
931 } else {
932 //
933 // We were dispatched to this function and we don't recognize the opcode
934 //
935 EbcDebugSignalException (EXCEPT_EBC_UNDEFINED, EXCEPTION_FLAG_FATAL, VmPtr);
936 return EFI_UNSUPPORTED;
937 }
938 //
939 // Now get the source address
940 //
941 if (OPERAND2_INDIRECT (Operands)) {
942 //
943 // Indirect form @R2. Compute address of operand2
944 //
945 Source = (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index64Op2);
946 //
947 // Now get the data from the source. Always 0-extend and let the compiler
948 // sign-extend where required.
949 //
950 switch (MoveSize) {
951 case DATA_SIZE_8:
952 Data64 = (UINT64) (UINT8) VmReadMem8 (VmPtr, Source);
953 break;
954
955 case DATA_SIZE_16:
956 Data64 = (UINT64) (UINT16) VmReadMem16 (VmPtr, Source);
957 break;
958
959 case DATA_SIZE_32:
960 Data64 = (UINT64) (UINT32) VmReadMem32 (VmPtr, Source);
961 break;
962
963 case DATA_SIZE_64:
964 Data64 = (UINT64) VmReadMem64 (VmPtr, Source);
965 break;
966
967 case DATA_SIZE_N:
968 Data64 = (UINT64) (UINTN) VmReadMemN (VmPtr, Source);
969 break;
970
971 default:
972 //
973 // not reached
974 //
975 break;
976 }
977 } else {
978 //
979 // Not indirect source: MOVxx {@}Rx, Ry [Index]
980 //
981 Data64 = VmPtr->R[OPERAND2_REGNUM (Operands)] + Index64Op2;
982 //
983 // Did Operand2 have an index? If so, treat as two signed values since
984 // indexes are signed values.
985 //
986 if (Opcode & OPCODE_M_IMMED_OP2) {
987 //
988 // NOTE: need to find a way to fix this, most likely by changing the VM
989 // implementation to remove the stack gap. To do that, we'd need to
990 // allocate stack space for the VM and actually set the system
991 // stack pointer to the allocated buffer when the VM starts.
992 //
993 // Special case -- if someone took the address of a function parameter
994 // then we need to make sure it's not in the stack gap. We can identify
995 // this situation if (Operand2 register == 0) && (Operand2 is direct)
996 // && (Index applies to Operand2) && (Index > 0) && (Operand1 register != 0)
997 // Situations that to be aware of:
998 // * stack adjustments at beginning and end of functions R0 = R0 += stacksize
999 //
1000 if ((OPERAND2_REGNUM (Operands) == 0) &&
1001 (!OPERAND2_INDIRECT (Operands)) &&
1002 (Index64Op2 > 0) &&
1003 (OPERAND1_REGNUM (Operands) == 0) &&
1004 (OPERAND1_INDIRECT (Operands))
1005 ) {
1006 Data64 = (UINT64) ConvertStackAddr (VmPtr, (UINTN) (INT64) Data64);
1007 }
1008 }
1009 }
1010 //
1011 // Now write it back
1012 //
1013 if (OPERAND1_INDIRECT (Operands)) {
1014 //
1015 // Reuse the Source variable to now be dest.
1016 //
1017 Source = (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index64Op1);
1018 //
1019 // Do the write based on the size
1020 //
1021 switch (MoveSize) {
1022 case DATA_SIZE_8:
1023 VmWriteMem8 (VmPtr, Source, (UINT8) Data64);
1024 break;
1025
1026 case DATA_SIZE_16:
1027 VmWriteMem16 (VmPtr, Source, (UINT16) Data64);
1028 break;
1029
1030 case DATA_SIZE_32:
1031 VmWriteMem32 (VmPtr, Source, (UINT32) Data64);
1032 break;
1033
1034 case DATA_SIZE_64:
1035 VmWriteMem64 (VmPtr, Source, Data64);
1036 break;
1037
1038 case DATA_SIZE_N:
1039 VmWriteMemN (VmPtr, Source, (UINTN) Data64);
1040 break;
1041
1042 default:
1043 //
1044 // not reached
1045 //
1046 break;
1047 }
1048 } else {
1049 //
1050 // Operand1 direct.
1051 // Make sure we didn't have an index on operand1.
1052 //
1053 if (Opcode & OPCODE_M_IMMED_OP1) {
1054 EbcDebugSignalException (
1055 EXCEPT_EBC_INSTRUCTION_ENCODING,
1056 EXCEPTION_FLAG_FATAL,
1057 VmPtr
1058 );
1059 return EFI_UNSUPPORTED;
1060 }
1061 //
1062 // Direct storage in register. Clear unused bits and store back to
1063 // register.
1064 //
1065 VmPtr->R[OPERAND1_REGNUM (Operands)] = Data64 & DataMask;
1066 }
1067 //
1068 // Advance the instruction pointer
1069 //
1070 VmPtr->Ip += Size;
1071 return EFI_SUCCESS;
1072 }
1073
1074 STATIC
1075 EFI_STATUS
1076 ExecuteBREAK (
1077 IN VM_CONTEXT *VmPtr
1078 )
1079 /*++
1080
1081 Routine Description:
1082
1083 Execute the EBC BREAK instruction
1084
1085 Arguments:
1086
1087 VmPtr - pointer to current VM context
1088
1089 Returns:
1090
1091 EFI_UNSUPPORTED
1092 EFI_SUCCESS
1093
1094 --*/
1095 {
1096 UINT8 Operands;
1097 VOID *EbcEntryPoint;
1098 VOID *Thunk;
1099 EFI_STATUS Status;
1100 UINT64 U64EbcEntryPoint;
1101 INT32 Offset;
1102
1103 Operands = GETOPERANDS (VmPtr);
1104 switch (Operands) {
1105 //
1106 // Runaway program break. Generate an exception and terminate
1107 //
1108 case 0:
1109 EbcDebugSignalException (EXCEPT_EBC_BAD_BREAK, EXCEPTION_FLAG_FATAL, VmPtr);
1110 break;
1111
1112 //
1113 // Get VM version -- return VM revision number in R7
1114 //
1115 case 1:
1116 //
1117 // Bits:
1118 // 63-17 = 0
1119 // 16-8 = Major version
1120 // 7-0 = Minor version
1121 //
1122 VmPtr->R[7] = GetVmVersion ();
1123 break;
1124
1125 //
1126 // Debugger breakpoint
1127 //
1128 case 3:
1129 VmPtr->StopFlags |= STOPFLAG_BREAKPOINT;
1130 //
1131 // See if someone has registered a handler
1132 //
1133 EbcDebugSignalException (
1134 EXCEPT_EBC_BREAKPOINT,
1135 EXCEPTION_FLAG_NONE,
1136 VmPtr
1137 );
1138 //
1139 // Don't advance the IP
1140 //
1141 return EFI_UNSUPPORTED;
1142 break;
1143
1144 //
1145 // System call, which there are none, so NOP it.
1146 //
1147 case 4:
1148 break;
1149
1150 //
1151 // Create a thunk for EBC code. R7 points to a 32-bit (in a 64-bit slot)
1152 // "offset from self" pointer to the EBC entry point.
1153 // After we're done, *(UINT64 *)R7 will be the address of the new thunk.
1154 //
1155 case 5:
1156 Offset = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->R[7]);
1157 U64EbcEntryPoint = (UINT64) (VmPtr->R[7] + Offset + 4);
1158 EbcEntryPoint = (VOID *) (UINTN) U64EbcEntryPoint;
1159
1160 //
1161 // Now create a new thunk
1162 //
1163 Status = EbcCreateThunks (VmPtr->ImageHandle, EbcEntryPoint, &Thunk, 0);
1164
1165 //
1166 // Finally replace the EBC entry point memory with the thunk address
1167 //
1168 VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[7], (UINT64) (UINTN) Thunk);
1169 break;
1170
1171 //
1172 // Compiler setting version per value in R7
1173 //
1174 case 6:
1175 VmPtr->CompilerVersion = (UINT32) VmPtr->R[7];
1176 //
1177 // Check compiler version against VM version?
1178 //
1179 break;
1180
1181 //
1182 // Unhandled break code. Signal exception.
1183 //
1184 default:
1185 EbcDebugSignalException (EXCEPT_EBC_BAD_BREAK, EXCEPTION_FLAG_FATAL, VmPtr);
1186 break;
1187 }
1188 //
1189 // Advance IP
1190 //
1191 VmPtr->Ip += 2;
1192 return EFI_SUCCESS;
1193 }
1194
1195 STATIC
1196 EFI_STATUS
1197 ExecuteJMP (
1198 IN VM_CONTEXT *VmPtr
1199 )
1200 /*++
1201
1202 Routine Description:
1203 Execute the JMP instruction
1204
1205 Arguments:
1206 VmPtr - pointer to VM context
1207
1208 Returns:
1209 Standard EFI_STATUS
1210
1211 Instruction syntax:
1212 JMP64{cs|cc} Immed64
1213 JMP32{cs|cc} {@}R1 {Immed32|Index32}
1214
1215 Encoding:
1216 b0.7 - immediate data present
1217 b0.6 - 1 = 64 bit immediate data
1218 0 = 32 bit immediate data
1219 b1.7 - 1 = conditional
1220 b1.6 1 = CS (condition set)
1221 0 = CC (condition clear)
1222 b1.4 1 = relative address
1223 0 = absolute address
1224 b1.3 1 = operand1 indirect
1225 b1.2-0 operand 1
1226
1227 --*/
1228 {
1229 UINT8 Opcode;
1230 UINT8 CompareSet;
1231 UINT8 ConditionFlag;
1232 UINT8 Size;
1233 UINT8 Operand;
1234 UINT64 Data64;
1235 INT32 Index32;
1236 UINTN Addr;
1237
1238 Operand = GETOPERANDS (VmPtr);
1239 Opcode = GETOPCODE (VmPtr);
1240
1241 //
1242 // Get instruction length from the opcode. The upper two bits are used here
1243 // to index into the length array.
1244 //
1245 Size = mJMPLen[(Opcode >> 6) & 0x03];
1246
1247 //
1248 // Decode instruction conditions
1249 // If we haven't met the condition, then simply advance the IP and return.
1250 //
1251 CompareSet = (UINT8) ((Operand & JMP_M_CS) ? 1 : 0);
1252 ConditionFlag = (UINT8) VMFLAG_ISSET (VmPtr, VMFLAGS_CC);
1253 if (Operand & CONDITION_M_CONDITIONAL) {
1254 if (CompareSet != ConditionFlag) {
1255 VmPtr->Ip += Size;
1256 return EFI_SUCCESS;
1257 }
1258 }
1259 //
1260 // Check for 64-bit form and do it right away since it's the most
1261 // straight-forward form.
1262 //
1263 if (Opcode & OPCODE_M_IMMDATA64) {
1264 //
1265 // Double check for immediate-data, which is required. If not there,
1266 // then signal an exception
1267 //
1268 if (!(Opcode & OPCODE_M_IMMDATA)) {
1269 EbcDebugSignalException (
1270 EXCEPT_EBC_INSTRUCTION_ENCODING,
1271 EXCEPTION_FLAG_ERROR,
1272 VmPtr
1273 );
1274 return EFI_UNSUPPORTED;
1275 }
1276 //
1277 // 64-bit immediate data is full address. Read the immediate data,
1278 // check for alignment, and jump absolute.
1279 //
1280 Data64 = VmReadImmed64 (VmPtr, 2);
1281 if (!IS_ALIGNED ((UINTN) Data64, sizeof (UINT16))) {
1282 EbcDebugSignalException (
1283 EXCEPT_EBC_ALIGNMENT_CHECK,
1284 EXCEPTION_FLAG_FATAL,
1285 VmPtr
1286 );
1287
1288 return EFI_UNSUPPORTED;
1289 }
1290
1291 //
1292 // Take jump -- relative or absolute
1293 //
1294 if (Operand & JMP_M_RELATIVE) {
1295 VmPtr->Ip += (UINTN) Data64 + Size;
1296 } else {
1297 VmPtr->Ip = (VMIP) (UINTN) Data64;
1298 }
1299
1300 return EFI_SUCCESS;
1301 }
1302 //
1303 // 32-bit forms:
1304 // Get the index if there is one. May be either an index, or an immediate
1305 // offset depending on indirect operand.
1306 // JMP32 @R1 Index32 -- immediate data is an index
1307 // JMP32 R1 Immed32 -- immedate data is an offset
1308 //
1309 if (Opcode & OPCODE_M_IMMDATA) {
1310 if (OPERAND1_INDIRECT (Operand)) {
1311 Index32 = VmReadIndex32 (VmPtr, 2);
1312 } else {
1313 Index32 = VmReadImmed32 (VmPtr, 2);
1314 }
1315 } else {
1316 Index32 = 0;
1317 }
1318 //
1319 // Get the register data. If R == 0, then special case where it's ignored.
1320 //
1321 if (OPERAND1_REGNUM (Operand) == 0) {
1322 Data64 = 0;
1323 } else {
1324 Data64 = OPERAND1_REGDATA (VmPtr, Operand);
1325 }
1326 //
1327 // Decode the forms
1328 //
1329 if (OPERAND1_INDIRECT (Operand)) {
1330 //
1331 // Form: JMP32 @Rx {Index32}
1332 //
1333 Addr = VmReadMemN (VmPtr, (UINTN) Data64 + Index32);
1334 if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {
1335 EbcDebugSignalException (
1336 EXCEPT_EBC_ALIGNMENT_CHECK,
1337 EXCEPTION_FLAG_FATAL,
1338 VmPtr
1339 );
1340
1341 return EFI_UNSUPPORTED;
1342 }
1343
1344 if (Operand & JMP_M_RELATIVE) {
1345 VmPtr->Ip += (UINTN) Addr + Size;
1346 } else {
1347 VmPtr->Ip = (VMIP) Addr;
1348 }
1349 } else {
1350 //
1351 // Form: JMP32 Rx {Immed32}
1352 //
1353 Addr = (UINTN) (Data64 + Index32);
1354 if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {
1355 EbcDebugSignalException (
1356 EXCEPT_EBC_ALIGNMENT_CHECK,
1357 EXCEPTION_FLAG_FATAL,
1358 VmPtr
1359 );
1360
1361 return EFI_UNSUPPORTED;
1362 }
1363
1364 if (Operand & JMP_M_RELATIVE) {
1365 VmPtr->Ip += (UINTN) Addr + Size;
1366 } else {
1367 VmPtr->Ip = (VMIP) Addr;
1368 }
1369 }
1370
1371 return EFI_SUCCESS;
1372 }
1373
1374 STATIC
1375 EFI_STATUS
1376 ExecuteJMP8 (
1377 IN VM_CONTEXT *VmPtr
1378 )
1379 /*++
1380
1381 Routine Description:
1382 Execute the EBC JMP8 instruction
1383
1384 Arguments:
1385 VmPtr - pointer to a VM context
1386
1387 Returns:
1388 Standard EFI_STATUS
1389
1390 Instruction syntax:
1391 JMP8{cs|cc} Offset/2
1392
1393 --*/
1394 {
1395 UINT8 Opcode;
1396 UINT8 ConditionFlag;
1397 UINT8 CompareSet;
1398 INT8 Offset;
1399
1400 //
1401 // Decode instruction.
1402 //
1403 Opcode = GETOPCODE (VmPtr);
1404 CompareSet = (UINT8) ((Opcode & JMP_M_CS) ? 1 : 0);
1405 ConditionFlag = (UINT8) VMFLAG_ISSET (VmPtr, VMFLAGS_CC);
1406
1407 //
1408 // If we haven't met the condition, then simply advance the IP and return
1409 //
1410 if (Opcode & CONDITION_M_CONDITIONAL) {
1411 if (CompareSet != ConditionFlag) {
1412 VmPtr->Ip += 2;
1413 return EFI_SUCCESS;
1414 }
1415 }
1416 //
1417 // Get the offset from the instruction stream. It's relative to the
1418 // following instruction, and divided by 2.
1419 //
1420 Offset = VmReadImmed8 (VmPtr, 1);
1421 //
1422 // Want to check for offset == -2 and then raise an exception?
1423 //
1424 VmPtr->Ip += (Offset * 2) + 2;
1425 return EFI_SUCCESS;
1426 }
1427
1428 STATIC
1429 EFI_STATUS
1430 ExecuteMOVI (
1431 IN VM_CONTEXT *VmPtr
1432 )
1433 /*++
1434
1435 Routine Description:
1436
1437 Execute the EBC MOVI
1438
1439 Arguments:
1440
1441 VmPtr - pointer to a VM context
1442
1443 Returns:
1444
1445 Standard EFI_STATUS
1446
1447 Instruction syntax:
1448
1449 MOVI[b|w|d|q][w|d|q] {@}R1 {Index16}, ImmData16|32|64
1450
1451 First variable character specifies the move size
1452 Second variable character specifies size of the immediate data
1453
1454 Sign-extend the immediate data to the size of the operation, and zero-extend
1455 if storing to a register.
1456
1457 Operand1 direct with index/immed is invalid.
1458
1459 --*/
1460 {
1461 UINT8 Opcode;
1462 UINT8 Operands;
1463 UINT8 Size;
1464 INT16 Index16;
1465 INT64 ImmData64;
1466 UINT64 Op1;
1467 UINT64 Mask64;
1468
1469 //
1470 // Get the opcode and operands byte so we can get R1 and R2
1471 //
1472 Opcode = GETOPCODE (VmPtr);
1473 Operands = GETOPERANDS (VmPtr);
1474
1475 //
1476 // Get the index (16-bit) if present
1477 //
1478 if (Operands & MOVI_M_IMMDATA) {
1479 Index16 = VmReadIndex16 (VmPtr, 2);
1480 Size = 4;
1481 } else {
1482 Index16 = 0;
1483 Size = 2;
1484 }
1485 //
1486 // Extract the immediate data. Sign-extend always.
1487 //
1488 if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {
1489 ImmData64 = (INT64) (INT16) VmReadImmed16 (VmPtr, Size);
1490 Size += 2;
1491 } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {
1492 ImmData64 = (INT64) (INT32) VmReadImmed32 (VmPtr, Size);
1493 Size += 4;
1494 } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {
1495 ImmData64 = (INT64) VmReadImmed64 (VmPtr, Size);
1496 Size += 8;
1497 } else {
1498 //
1499 // Invalid encoding
1500 //
1501 EbcDebugSignalException (
1502 EXCEPT_EBC_INSTRUCTION_ENCODING,
1503 EXCEPTION_FLAG_FATAL,
1504 VmPtr
1505 );
1506 return EFI_UNSUPPORTED;
1507 }
1508 //
1509 // Now write back the result
1510 //
1511 if (!OPERAND1_INDIRECT (Operands)) {
1512 //
1513 // Operand1 direct. Make sure it didn't have an index.
1514 //
1515 if (Operands & MOVI_M_IMMDATA) {
1516 EbcDebugSignalException (
1517 EXCEPT_EBC_INSTRUCTION_ENCODING,
1518 EXCEPTION_FLAG_FATAL,
1519 VmPtr
1520 );
1521 return EFI_UNSUPPORTED;
1522 }
1523 //
1524 // Writing directly to a register. Clear unused bits.
1525 //
1526 if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH8) {
1527 Mask64 = 0x000000FF;
1528 } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH16) {
1529 Mask64 = 0x0000FFFF;
1530 } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH32) {
1531 Mask64 = 0x00000000FFFFFFFF;
1532 } else {
1533 Mask64 = (UINT64)~0;
1534 }
1535
1536 VmPtr->R[OPERAND1_REGNUM (Operands)] = ImmData64 & Mask64;
1537 } else {
1538 //
1539 // Get the address then write back based on size of the move
1540 //
1541 Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;
1542 if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH8) {
1543 VmWriteMem8 (VmPtr, (UINTN) Op1, (UINT8) ImmData64);
1544 } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH16) {
1545 VmWriteMem16 (VmPtr, (UINTN) Op1, (UINT16) ImmData64);
1546 } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH32) {
1547 VmWriteMem32 (VmPtr, (UINTN) Op1, (UINT32) ImmData64);
1548 } else {
1549 VmWriteMem64 (VmPtr, (UINTN) Op1, ImmData64);
1550 }
1551 }
1552 //
1553 // Advance the instruction pointer
1554 //
1555 VmPtr->Ip += Size;
1556 return EFI_SUCCESS;
1557 }
1558
1559 STATIC
1560 EFI_STATUS
1561 ExecuteMOVIn (
1562 IN VM_CONTEXT *VmPtr
1563 )
1564 /*++
1565
1566 Routine Description:
1567
1568 Execute the EBC MOV immediate natural. This instruction moves an immediate
1569 index value into a register or memory location.
1570
1571 Arguments:
1572
1573 VmPtr - pointer to a VM context
1574
1575 Returns:
1576
1577 Standard EFI_STATUS
1578
1579 Instruction syntax:
1580
1581 MOVIn[w|d|q] {@}R1 {Index16}, Index16|32|64
1582
1583 --*/
1584 {
1585 UINT8 Opcode;
1586 UINT8 Operands;
1587 UINT8 Size;
1588 INT16 Index16;
1589 INT16 ImmedIndex16;
1590 INT32 ImmedIndex32;
1591 INT64 ImmedIndex64;
1592 UINT64 Op1;
1593
1594 //
1595 // Get the opcode and operands byte so we can get R1 and R2
1596 //
1597 Opcode = GETOPCODE (VmPtr);
1598 Operands = GETOPERANDS (VmPtr);
1599
1600 //
1601 // Get the operand1 index (16-bit) if present
1602 //
1603 if (Operands & MOVI_M_IMMDATA) {
1604 Index16 = VmReadIndex16 (VmPtr, 2);
1605 Size = 4;
1606 } else {
1607 Index16 = 0;
1608 Size = 2;
1609 }
1610 //
1611 // Extract the immediate data and convert to a 64-bit index.
1612 //
1613 if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {
1614 ImmedIndex16 = VmReadIndex16 (VmPtr, Size);
1615 ImmedIndex64 = (INT64) ImmedIndex16;
1616 Size += 2;
1617 } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {
1618 ImmedIndex32 = VmReadIndex32 (VmPtr, Size);
1619 ImmedIndex64 = (INT64) ImmedIndex32;
1620 Size += 4;
1621 } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {
1622 ImmedIndex64 = VmReadIndex64 (VmPtr, Size);
1623 Size += 8;
1624 } else {
1625 //
1626 // Invalid encoding
1627 //
1628 EbcDebugSignalException (
1629 EXCEPT_EBC_INSTRUCTION_ENCODING,
1630 EXCEPTION_FLAG_FATAL,
1631 VmPtr
1632 );
1633 return EFI_UNSUPPORTED;
1634 }
1635 //
1636 // Now write back the result
1637 //
1638 if (!OPERAND1_INDIRECT (Operands)) {
1639 //
1640 // Check for MOVIn R1 Index16, Immed (not indirect, with index), which
1641 // is illegal
1642 //
1643 if (Operands & MOVI_M_IMMDATA) {
1644 EbcDebugSignalException (
1645 EXCEPT_EBC_INSTRUCTION_ENCODING,
1646 EXCEPTION_FLAG_FATAL,
1647 VmPtr
1648 );
1649 return EFI_UNSUPPORTED;
1650 }
1651
1652 VmPtr->R[OPERAND1_REGNUM (Operands)] = ImmedIndex64;
1653 } else {
1654 //
1655 // Get the address
1656 //
1657 Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;
1658 VmWriteMemN (VmPtr, (UINTN) Op1, (INTN) ImmedIndex64);
1659 }
1660 //
1661 // Advance the instruction pointer
1662 //
1663 VmPtr->Ip += Size;
1664 return EFI_SUCCESS;
1665 }
1666
1667 STATIC
1668 EFI_STATUS
1669 ExecuteMOVREL (
1670 IN VM_CONTEXT *VmPtr
1671 )
1672 /*++
1673
1674 Routine Description:
1675
1676 Execute the EBC MOVREL instruction.
1677 Dest <- Ip + ImmData
1678
1679 Arguments:
1680
1681 VmPtr - pointer to a VM context
1682
1683 Returns:
1684
1685 Standard EFI_STATUS
1686
1687 Instruction syntax:
1688
1689 MOVREL[w|d|q] {@}R1 {Index16}, ImmData16|32|64
1690
1691 --*/
1692 {
1693 UINT8 Opcode;
1694 UINT8 Operands;
1695 UINT8 Size;
1696 INT16 Index16;
1697 INT64 ImmData64;
1698 UINT64 Op1;
1699 UINT64 Op2;
1700
1701 //
1702 // Get the opcode and operands byte so we can get R1 and R2
1703 //
1704 Opcode = GETOPCODE (VmPtr);
1705 Operands = GETOPERANDS (VmPtr);
1706
1707 //
1708 // Get the Operand 1 index (16-bit) if present
1709 //
1710 if (Operands & MOVI_M_IMMDATA) {
1711 Index16 = VmReadIndex16 (VmPtr, 2);
1712 Size = 4;
1713 } else {
1714 Index16 = 0;
1715 Size = 2;
1716 }
1717 //
1718 // Get the immediate data.
1719 //
1720 if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {
1721 ImmData64 = (INT64) VmReadImmed16 (VmPtr, Size);
1722 Size += 2;
1723 } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {
1724 ImmData64 = (INT64) VmReadImmed32 (VmPtr, Size);
1725 Size += 4;
1726 } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {
1727 ImmData64 = VmReadImmed64 (VmPtr, Size);
1728 Size += 8;
1729 } else {
1730 //
1731 // Invalid encoding
1732 //
1733 EbcDebugSignalException (
1734 EXCEPT_EBC_INSTRUCTION_ENCODING,
1735 EXCEPTION_FLAG_FATAL,
1736 VmPtr
1737 );
1738 return EFI_UNSUPPORTED;
1739 }
1740 //
1741 // Compute the value and write back the result
1742 //
1743 Op2 = (UINT64) ((INT64) ((UINT64) (UINTN) VmPtr->Ip) + (INT64) ImmData64 + Size);
1744 if (!OPERAND1_INDIRECT (Operands)) {
1745 //
1746 // Check for illegal combination of operand1 direct with immediate data
1747 //
1748 if (Operands & MOVI_M_IMMDATA) {
1749 EbcDebugSignalException (
1750 EXCEPT_EBC_INSTRUCTION_ENCODING,
1751 EXCEPTION_FLAG_FATAL,
1752 VmPtr
1753 );
1754 return EFI_UNSUPPORTED;
1755 }
1756
1757 VmPtr->R[OPERAND1_REGNUM (Operands)] = (VM_REGISTER) Op2;
1758 } else {
1759 //
1760 // Get the address = [Rx] + Index16
1761 // Write back the result. Always a natural size write, since
1762 // we're talking addresses here.
1763 //
1764 Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;
1765 VmWriteMemN (VmPtr, (UINTN) Op1, (UINTN) Op2);
1766 }
1767 //
1768 // Advance the instruction pointer
1769 //
1770 VmPtr->Ip += Size;
1771 return EFI_SUCCESS;
1772 }
1773
1774 STATIC
1775 EFI_STATUS
1776 ExecuteMOVsnw (
1777 IN VM_CONTEXT *VmPtr
1778 )
1779 /*++
1780
1781 Routine Description:
1782
1783 Execute the EBC MOVsnw instruction. This instruction loads a signed
1784 natural value from memory or register to another memory or register. On
1785 32-bit machines, the value gets sign-extended to 64 bits if the destination
1786 is a register.
1787
1788 Arguments:
1789
1790 VmPtr - pointer to a VM context
1791
1792 Returns:
1793
1794 Standard EFI_STATUS
1795
1796 Instruction syntax:
1797
1798 MOVsnw {@}R1 {Index16}, {@}R2 {Index16|Immed16}
1799
1800 0:7 1=>operand1 index present
1801 0:6 1=>operand2 index present
1802
1803 --*/
1804 {
1805 UINT8 Opcode;
1806 UINT8 Operands;
1807 UINT8 Size;
1808 INT16 Op1Index;
1809 INT16 Op2Index;
1810 UINT64 Op2;
1811
1812 //
1813 // Get the opcode and operand bytes
1814 //
1815 Opcode = GETOPCODE (VmPtr);
1816 Operands = GETOPERANDS (VmPtr);
1817
1818 Op1Index = Op2Index = 0;
1819
1820 //
1821 // Get the indexes if present.
1822 //
1823 Size = 2;
1824 if (Opcode & OPCODE_M_IMMED_OP1) {
1825 if (OPERAND1_INDIRECT (Operands)) {
1826 Op1Index = VmReadIndex16 (VmPtr, 2);
1827 } else {
1828 //
1829 // Illegal form operand1 direct with index: MOVsnw R1 Index16, {@}R2
1830 //
1831 EbcDebugSignalException (
1832 EXCEPT_EBC_INSTRUCTION_ENCODING,
1833 EXCEPTION_FLAG_FATAL,
1834 VmPtr
1835 );
1836 return EFI_UNSUPPORTED;
1837 }
1838
1839 Size += sizeof (UINT16);
1840 }
1841
1842 if (Opcode & OPCODE_M_IMMED_OP2) {
1843 if (OPERAND2_INDIRECT (Operands)) {
1844 Op2Index = VmReadIndex16 (VmPtr, Size);
1845 } else {
1846 Op2Index = VmReadImmed16 (VmPtr, Size);
1847 }
1848
1849 Size += sizeof (UINT16);
1850 }
1851 //
1852 // Get the data from the source.
1853 //
1854 Op2 = (INT64) ((INTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Op2Index));
1855 if (OPERAND2_INDIRECT (Operands)) {
1856 Op2 = (INT64) (INTN) VmReadMemN (VmPtr, (UINTN) Op2);
1857 }
1858 //
1859 // Now write back the result.
1860 //
1861 if (!OPERAND1_INDIRECT (Operands)) {
1862 VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;
1863 } else {
1864 VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2);
1865 }
1866 //
1867 // Advance the instruction pointer
1868 //
1869 VmPtr->Ip += Size;
1870 return EFI_SUCCESS;
1871 }
1872
1873 STATIC
1874 EFI_STATUS
1875 ExecuteMOVsnd (
1876 IN VM_CONTEXT *VmPtr
1877 )
1878 /*++
1879
1880 Routine Description:
1881
1882 Execute the EBC MOVsnw instruction. This instruction loads a signed
1883 natural value from memory or register to another memory or register. On
1884 32-bit machines, the value gets sign-extended to 64 bits if the destination
1885 is a register.
1886
1887 Arguments:
1888
1889 VmPtr - pointer to a VM context
1890
1891 Returns:
1892
1893 Standard EFI_STATUS
1894
1895 Instruction syntax:
1896
1897 MOVsnd {@}R1 {Indx32}, {@}R2 {Index32|Immed32}
1898
1899 0:7 1=>operand1 index present
1900 0:6 1=>operand2 index present
1901
1902 --*/
1903 {
1904 UINT8 Opcode;
1905 UINT8 Operands;
1906 UINT8 Size;
1907 INT32 Op1Index;
1908 INT32 Op2Index;
1909 UINT64 Op2;
1910
1911 //
1912 // Get the opcode and operand bytes
1913 //
1914 Opcode = GETOPCODE (VmPtr);
1915 Operands = GETOPERANDS (VmPtr);
1916
1917 Op1Index = Op2Index = 0;
1918
1919 //
1920 // Get the indexes if present.
1921 //
1922 Size = 2;
1923 if (Opcode & OPCODE_M_IMMED_OP1) {
1924 if (OPERAND1_INDIRECT (Operands)) {
1925 Op1Index = VmReadIndex32 (VmPtr, 2);
1926 } else {
1927 //
1928 // Illegal form operand1 direct with index: MOVsnd R1 Index16,..
1929 //
1930 EbcDebugSignalException (
1931 EXCEPT_EBC_INSTRUCTION_ENCODING,
1932 EXCEPTION_FLAG_FATAL,
1933 VmPtr
1934 );
1935 return EFI_UNSUPPORTED;
1936 }
1937
1938 Size += sizeof (UINT32);
1939 }
1940
1941 if (Opcode & OPCODE_M_IMMED_OP2) {
1942 if (OPERAND2_INDIRECT (Operands)) {
1943 Op2Index = VmReadIndex32 (VmPtr, Size);
1944 } else {
1945 Op2Index = VmReadImmed32 (VmPtr, Size);
1946 }
1947
1948 Size += sizeof (UINT32);
1949 }
1950 //
1951 // Get the data from the source.
1952 //
1953 Op2 = (INT64) ((INTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Op2Index));
1954 if (OPERAND2_INDIRECT (Operands)) {
1955 Op2 = (INT64) (INTN) VmReadMemN (VmPtr, (UINTN) Op2);
1956 }
1957 //
1958 // Now write back the result.
1959 //
1960 if (!OPERAND1_INDIRECT (Operands)) {
1961 VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;
1962 } else {
1963 VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2);
1964 }
1965 //
1966 // Advance the instruction pointer
1967 //
1968 VmPtr->Ip += Size;
1969 return EFI_SUCCESS;
1970 }
1971
1972 STATIC
1973 EFI_STATUS
1974 ExecutePUSHn (
1975 IN VM_CONTEXT *VmPtr
1976 )
1977 /*++
1978
1979 Routine Description:
1980 Execute the EBC PUSHn instruction
1981
1982 Arguments:
1983 VmPtr - pointer to a VM context
1984
1985 Returns:
1986 Standard EFI_STATUS
1987
1988 Instruction syntax:
1989 PUSHn {@}R1 {Index16|Immed16}
1990
1991 --*/
1992 {
1993 UINT8 Opcode;
1994 UINT8 Operands;
1995 INT16 Index16;
1996 UINTN DataN;
1997
1998 //
1999 // Get opcode and operands
2000 //
2001 Opcode = GETOPCODE (VmPtr);
2002 Operands = GETOPERANDS (VmPtr);
2003
2004 //
2005 // Get index if present
2006 //
2007 if (Opcode & PUSHPOP_M_IMMDATA) {
2008 if (OPERAND1_INDIRECT (Operands)) {
2009 Index16 = VmReadIndex16 (VmPtr, 2);
2010 } else {
2011 Index16 = VmReadImmed16 (VmPtr, 2);
2012 }
2013
2014 VmPtr->Ip += 4;
2015 } else {
2016 Index16 = 0;
2017 VmPtr->Ip += 2;
2018 }
2019 //
2020 // Get the data to push
2021 //
2022 if (OPERAND1_INDIRECT (Operands)) {
2023 DataN = VmReadMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));
2024 } else {
2025 DataN = (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16);
2026 }
2027 //
2028 // Adjust the stack down.
2029 //
2030 VmPtr->R[0] -= sizeof (UINTN);
2031 VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], DataN);
2032 return EFI_SUCCESS;
2033 }
2034
2035 STATIC
2036 EFI_STATUS
2037 ExecutePUSH (
2038 IN VM_CONTEXT *VmPtr
2039 )
2040 /*++
2041
2042 Routine Description:
2043 Execute the EBC PUSH instruction
2044
2045 Arguments:
2046 VmPtr - pointer to a VM context
2047
2048 Returns:
2049 Standard EFI_STATUS
2050
2051 Instruction syntax:
2052 PUSH[32|64] {@}R1 {Index16|Immed16}
2053
2054 --*/
2055 {
2056 UINT8 Opcode;
2057 UINT8 Operands;
2058 UINT32 Data32;
2059 UINT64 Data64;
2060 INT16 Index16;
2061
2062 //
2063 // Get opcode and operands
2064 //
2065 Opcode = GETOPCODE (VmPtr);
2066 Operands = GETOPERANDS (VmPtr);
2067 //
2068 // Get immediate index if present, then advance the IP.
2069 //
2070 if (Opcode & PUSHPOP_M_IMMDATA) {
2071 if (OPERAND1_INDIRECT (Operands)) {
2072 Index16 = VmReadIndex16 (VmPtr, 2);
2073 } else {
2074 Index16 = VmReadImmed16 (VmPtr, 2);
2075 }
2076
2077 VmPtr->Ip += 4;
2078 } else {
2079 Index16 = 0;
2080 VmPtr->Ip += 2;
2081 }
2082 //
2083 // Get the data to push
2084 //
2085 if (Opcode & PUSHPOP_M_64) {
2086 if (OPERAND1_INDIRECT (Operands)) {
2087 Data64 = VmReadMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));
2088 } else {
2089 Data64 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;
2090 }
2091 //
2092 // Adjust the stack down, then write back the data
2093 //
2094 VmPtr->R[0] -= sizeof (UINT64);
2095 VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], Data64);
2096 } else {
2097 //
2098 // 32-bit data
2099 //
2100 if (OPERAND1_INDIRECT (Operands)) {
2101 Data32 = VmReadMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));
2102 } else {
2103 Data32 = (UINT32) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;
2104 }
2105 //
2106 // Adjust the stack down and write the data
2107 //
2108 VmPtr->R[0] -= sizeof (UINT32);
2109 VmWriteMem32 (VmPtr, (UINTN) VmPtr->R[0], Data32);
2110 }
2111
2112 return EFI_SUCCESS;
2113 }
2114
2115 STATIC
2116 EFI_STATUS
2117 ExecutePOPn (
2118 IN VM_CONTEXT *VmPtr
2119 )
2120 /*++
2121
2122 Routine Description:
2123 Execute the EBC POPn instruction
2124
2125 Arguments:
2126 VmPtr - pointer to a VM context
2127
2128 Returns:
2129 Standard EFI_STATUS
2130
2131 Instruction syntax:
2132 POPn {@}R1 {Index16|Immed16}
2133
2134 --*/
2135 {
2136 UINT8 Opcode;
2137 UINT8 Operands;
2138 INT16 Index16;
2139 UINTN DataN;
2140
2141 //
2142 // Get opcode and operands
2143 //
2144 Opcode = GETOPCODE (VmPtr);
2145 Operands = GETOPERANDS (VmPtr);
2146 //
2147 // Get immediate data if present, and advance the IP
2148 //
2149 if (Opcode & PUSHPOP_M_IMMDATA) {
2150 if (OPERAND1_INDIRECT (Operands)) {
2151 Index16 = VmReadIndex16 (VmPtr, 2);
2152 } else {
2153 Index16 = VmReadImmed16 (VmPtr, 2);
2154 }
2155
2156 VmPtr->Ip += 4;
2157 } else {
2158 Index16 = 0;
2159 VmPtr->Ip += 2;
2160 }
2161 //
2162 // Read the data off the stack, then adjust the stack pointer
2163 //
2164 DataN = VmReadMemN (VmPtr, (UINTN) VmPtr->R[0]);
2165 VmPtr->R[0] += sizeof (UINTN);
2166 //
2167 // Do the write-back
2168 //
2169 if (OPERAND1_INDIRECT (Operands)) {
2170 VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), DataN);
2171 } else {
2172 VmPtr->R[OPERAND1_REGNUM (Operands)] = (INT64) (UINT64) ((UINTN) DataN + Index16);
2173 }
2174
2175 return EFI_SUCCESS;
2176 }
2177
2178 STATIC
2179 EFI_STATUS
2180 ExecutePOP (
2181 IN VM_CONTEXT *VmPtr
2182 )
2183 /*++
2184
2185 Routine Description:
2186 Execute the EBC POP instruction
2187
2188 Arguments:
2189 VmPtr - pointer to a VM context
2190
2191 Returns:
2192 Standard EFI_STATUS
2193
2194 Instruction syntax:
2195 POP {@}R1 {Index16|Immed16}
2196
2197 --*/
2198 {
2199 UINT8 Opcode;
2200 UINT8 Operands;
2201 INT16 Index16;
2202 INT32 Data32;
2203 UINT64 Data64;
2204
2205 //
2206 // Get opcode and operands
2207 //
2208 Opcode = GETOPCODE (VmPtr);
2209 Operands = GETOPERANDS (VmPtr);
2210 //
2211 // Get immediate data if present, and advance the IP.
2212 //
2213 if (Opcode & PUSHPOP_M_IMMDATA) {
2214 if (OPERAND1_INDIRECT (Operands)) {
2215 Index16 = VmReadIndex16 (VmPtr, 2);
2216 } else {
2217 Index16 = VmReadImmed16 (VmPtr, 2);
2218 }
2219
2220 VmPtr->Ip += 4;
2221 } else {
2222 Index16 = 0;
2223 VmPtr->Ip += 2;
2224 }
2225 //
2226 // Get the data off the stack, then write it to the appropriate location
2227 //
2228 if (Opcode & PUSHPOP_M_64) {
2229 //
2230 // Read the data off the stack, then adjust the stack pointer
2231 //
2232 Data64 = VmReadMem64 (VmPtr, (UINTN) VmPtr->R[0]);
2233 VmPtr->R[0] += sizeof (UINT64);
2234 //
2235 // Do the write-back
2236 //
2237 if (OPERAND1_INDIRECT (Operands)) {
2238 VmWriteMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), Data64);
2239 } else {
2240 VmPtr->R[OPERAND1_REGNUM (Operands)] = Data64 + Index16;
2241 }
2242 } else {
2243 //
2244 // 32-bit pop. Read it off the stack and adjust the stack pointer
2245 //
2246 Data32 = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->R[0]);
2247 VmPtr->R[0] += sizeof (UINT32);
2248 //
2249 // Do the write-back
2250 //
2251 if (OPERAND1_INDIRECT (Operands)) {
2252 VmWriteMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), Data32);
2253 } else {
2254 VmPtr->R[OPERAND1_REGNUM (Operands)] = (INT64) Data32 + Index16;
2255 }
2256 }
2257
2258 return EFI_SUCCESS;
2259 }
2260
2261 STATIC
2262 EFI_STATUS
2263 ExecuteCALL (
2264 IN VM_CONTEXT *VmPtr
2265 )
2266 /*++
2267
2268 Routine Description:
2269 Implements the EBC CALL instruction.
2270
2271 Instruction format:
2272
2273 CALL64 Immed64
2274 CALL32 {@}R1 {Immed32|Index32}
2275 CALLEX64 Immed64
2276 CALLEX16 {@}R1 {Immed32}
2277
2278 If Rx == R0, then it's a PC relative call to PC = PC + imm32.
2279
2280 Arguments:
2281 VmPtr - pointer to a VM context.
2282
2283 Returns:
2284 Standard EFI_STATUS
2285
2286 --*/
2287 {
2288 UINT8 Opcode;
2289 UINT8 Operands;
2290 INT32 Immed32;
2291 UINT8 Size;
2292 INT64 Immed64;
2293 VOID *FramePtr;
2294
2295 //
2296 // Get opcode and operands
2297 //
2298 Opcode = GETOPCODE (VmPtr);
2299 Operands = GETOPERANDS (VmPtr);
2300 //
2301 // Assign these as well to avoid compiler warnings
2302 //
2303 Immed64 = 0;
2304 Immed32 = 0;
2305
2306 FramePtr = VmPtr->FramePtr;
2307 //
2308 // Determine the instruction size, and get immediate data if present
2309 //
2310 if (Opcode & OPCODE_M_IMMDATA) {
2311 if (Opcode & OPCODE_M_IMMDATA64) {
2312 Immed64 = VmReadImmed64 (VmPtr, 2);
2313 Size = 10;
2314 } else {
2315 //
2316 // If register operand is indirect, then the immediate data is an index
2317 //
2318 if (OPERAND1_INDIRECT (Operands)) {
2319 Immed32 = VmReadIndex32 (VmPtr, 2);
2320 } else {
2321 Immed32 = VmReadImmed32 (VmPtr, 2);
2322 }
2323
2324 Size = 6;
2325 }
2326 } else {
2327 Size = 2;
2328 }
2329 //
2330 // If it's a call to EBC, adjust the stack pointer down 16 bytes and
2331 // put our return address and frame pointer on the VM stack.
2332 //
2333 if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {
2334 VmPtr->R[0] -= 8;
2335 VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);
2336 VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];
2337 VmPtr->R[0] -= 8;
2338 VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (UINTN) (VmPtr->Ip + Size));
2339 }
2340 //
2341 // If 64-bit data, then absolute jump only
2342 //
2343 if (Opcode & OPCODE_M_IMMDATA64) {
2344 //
2345 // Native or EBC call?
2346 //
2347 if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {
2348 VmPtr->Ip = (VMIP) (UINTN) Immed64;
2349 } else {
2350 //
2351 // Call external function, get the return value, and advance the IP
2352 //
2353 EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->R[0], FramePtr, Size);
2354 }
2355 } else {
2356 //
2357 // Get the register data. If operand1 == 0, then ignore register and
2358 // take immediate data as relative or absolute address.
2359 // Compiler should take care of upper bits if 32-bit machine.
2360 //
2361 if (OPERAND1_REGNUM (Operands) != 0) {
2362 Immed64 = (UINT64) (UINTN) VmPtr->R[OPERAND1_REGNUM (Operands)];
2363 }
2364 //
2365 // Get final address
2366 //
2367 if (OPERAND1_INDIRECT (Operands)) {
2368 Immed64 = (INT64) (UINT64) (UINTN) VmReadMemN (VmPtr, (UINTN) (Immed64 + Immed32));
2369 } else {
2370 Immed64 += Immed32;
2371 }
2372 //
2373 // Now determine if external call, and then if relative or absolute
2374 //
2375 if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {
2376 //
2377 // EBC call. Relative or absolute? If relative, then it's relative to the
2378 // start of the next instruction.
2379 //
2380 if (Operands & OPERAND_M_RELATIVE_ADDR) {
2381 VmPtr->Ip += Immed64 + Size;
2382 } else {
2383 VmPtr->Ip = (VMIP) (UINTN) Immed64;
2384 }
2385 } else {
2386 //
2387 // Native call. Relative or absolute?
2388 //
2389 if (Operands & OPERAND_M_RELATIVE_ADDR) {
2390 EbcLLCALLEX (VmPtr, (UINTN) (Immed64 + VmPtr->Ip + Size), (UINTN) VmPtr->R[0], FramePtr, Size);
2391 } else {
2392 if (VmPtr->StopFlags & STOPFLAG_BREAK_ON_CALLEX) {
2393 CpuBreakpoint ();
2394 }
2395
2396 EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->R[0], FramePtr, Size);
2397 }
2398 }
2399 }
2400
2401 return EFI_SUCCESS;
2402 }
2403
2404 STATIC
2405 EFI_STATUS
2406 ExecuteRET (
2407 IN VM_CONTEXT *VmPtr
2408 )
2409 /*++
2410
2411 Routine Description:
2412 Execute the EBC RET instruction
2413
2414 Arguments:
2415 VmPtr - pointer to a VM context
2416
2417 Returns:
2418 Standard EFI_STATUS
2419
2420 Instruction syntax:
2421 RET
2422
2423 --*/
2424 {
2425 //
2426 // If we're at the top of the stack, then simply set the done
2427 // flag and return
2428 //
2429 if (VmPtr->StackRetAddr == (UINT64) VmPtr->R[0]) {
2430 VmPtr->StopFlags |= STOPFLAG_APP_DONE;
2431 } else {
2432 //
2433 // Pull the return address off the VM app's stack and set the IP
2434 // to it
2435 //
2436 if (!IS_ALIGNED ((UINTN) VmPtr->R[0], sizeof (UINT16))) {
2437 EbcDebugSignalException (
2438 EXCEPT_EBC_ALIGNMENT_CHECK,
2439 EXCEPTION_FLAG_FATAL,
2440 VmPtr
2441 );
2442 }
2443 //
2444 // Restore the IP and frame pointer from the stack
2445 //
2446 VmPtr->Ip = (VMIP) (UINTN) VmReadMem64 (VmPtr, (UINTN) VmPtr->R[0]);
2447 VmPtr->R[0] += 8;
2448 VmPtr->FramePtr = (VOID *) VmReadMemN (VmPtr, (UINTN) VmPtr->R[0]);
2449 VmPtr->R[0] += 8;
2450 }
2451
2452 return EFI_SUCCESS;
2453 }
2454
2455 STATIC
2456 EFI_STATUS
2457 ExecuteCMP (
2458 IN VM_CONTEXT *VmPtr
2459 )
2460 /*++
2461
2462 Routine Description:
2463 Execute the EBC CMP instruction
2464
2465 Arguments:
2466 VmPtr - pointer to a VM context
2467
2468 Returns:
2469 Standard EFI_STATUS
2470
2471 Instruction syntax:
2472 CMP[32|64][eq|lte|gte|ulte|ugte] R1, {@}R2 {Index16|Immed16}
2473
2474 --*/
2475 {
2476 UINT8 Opcode;
2477 UINT8 Operands;
2478 UINT8 Size;
2479 INT16 Index16;
2480 UINT32 Flag;
2481 INT64 Op2;
2482 INT64 Op1;
2483
2484 //
2485 // Get opcode and operands
2486 //
2487 Opcode = GETOPCODE (VmPtr);
2488 Operands = GETOPERANDS (VmPtr);
2489 //
2490 // Get the register data we're going to compare to
2491 //
2492 Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];
2493 //
2494 // Get immediate data
2495 //
2496 if (Opcode & OPCODE_M_IMMDATA) {
2497 if (OPERAND2_INDIRECT (Operands)) {
2498 Index16 = VmReadIndex16 (VmPtr, 2);
2499 } else {
2500 Index16 = VmReadImmed16 (VmPtr, 2);
2501 }
2502
2503 Size = 4;
2504 } else {
2505 Index16 = 0;
2506 Size = 2;
2507 }
2508 //
2509 // Now get Op2
2510 //
2511 if (OPERAND2_INDIRECT (Operands)) {
2512 if (Opcode & OPCODE_M_64BIT) {
2513 Op2 = (INT64) VmReadMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16));
2514 } else {
2515 //
2516 // 32-bit operations. 0-extend the values for all cases.
2517 //
2518 Op2 = (INT64) (UINT64) ((UINT32) VmReadMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16)));
2519 }
2520 } else {
2521 Op2 = VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16;
2522 }
2523 //
2524 // Now do the compare
2525 //
2526 Flag = 0;
2527 if (Opcode & OPCODE_M_64BIT) {
2528 //
2529 // 64-bit compares
2530 //
2531 switch (Opcode & OPCODE_M_OPCODE) {
2532 case OPCODE_CMPEQ:
2533 if (Op1 == Op2) {
2534 Flag = 1;
2535 }
2536 break;
2537
2538 case OPCODE_CMPLTE:
2539 if (Op1 <= Op2) {
2540 Flag = 1;
2541 }
2542 break;
2543
2544 case OPCODE_CMPGTE:
2545 if (Op1 >= Op2) {
2546 Flag = 1;
2547 }
2548 break;
2549
2550 case OPCODE_CMPULTE:
2551 if ((UINT64) Op1 <= (UINT64) Op2) {
2552 Flag = 1;
2553 }
2554 break;
2555
2556 case OPCODE_CMPUGTE:
2557 if ((UINT64) Op1 >= (UINT64) Op2) {
2558 Flag = 1;
2559 }
2560 break;
2561
2562 default:
2563 ASSERT (0);
2564 }
2565 } else {
2566 //
2567 // 32-bit compares
2568 //
2569 switch (Opcode & OPCODE_M_OPCODE) {
2570 case OPCODE_CMPEQ:
2571 if ((INT32) Op1 == (INT32) Op2) {
2572 Flag = 1;
2573 }
2574 break;
2575
2576 case OPCODE_CMPLTE:
2577 if ((INT32) Op1 <= (INT32) Op2) {
2578 Flag = 1;
2579 }
2580 break;
2581
2582 case OPCODE_CMPGTE:
2583 if ((INT32) Op1 >= (INT32) Op2) {
2584 Flag = 1;
2585 }
2586 break;
2587
2588 case OPCODE_CMPULTE:
2589 if ((UINT32) Op1 <= (UINT32) Op2) {
2590 Flag = 1;
2591 }
2592 break;
2593
2594 case OPCODE_CMPUGTE:
2595 if ((UINT32) Op1 >= (UINT32) Op2) {
2596 Flag = 1;
2597 }
2598 break;
2599
2600 default:
2601 ASSERT (0);
2602 }
2603 }
2604 //
2605 // Now set the flag accordingly for the comparison
2606 //
2607 if (Flag) {
2608 VMFLAG_SET (VmPtr, VMFLAGS_CC);
2609 } else {
2610 VMFLAG_CLEAR (VmPtr, VMFLAGS_CC);
2611 }
2612 //
2613 // Advance the IP
2614 //
2615 VmPtr->Ip += Size;
2616 return EFI_SUCCESS;
2617 }
2618
2619 STATIC
2620 EFI_STATUS
2621 ExecuteCMPI (
2622 IN VM_CONTEXT *VmPtr
2623 )
2624 /*++
2625
2626 Routine Description:
2627 Execute the EBC CMPI instruction
2628
2629 Arguments:
2630 VmPtr - pointer to a VM context
2631
2632 Returns:
2633 Standard EFI_STATUS
2634
2635 Instruction syntax:
2636 CMPI[32|64]{w|d}[eq|lte|gte|ulte|ugte] {@}Rx {Index16}, Immed16|Immed32
2637
2638 --*/
2639 {
2640 UINT8 Opcode;
2641 UINT8 Operands;
2642 UINT8 Size;
2643 INT64 Op1;
2644 INT64 Op2;
2645 INT16 Index16;
2646 UINT32 Flag;
2647
2648 //
2649 // Get opcode and operands
2650 //
2651 Opcode = GETOPCODE (VmPtr);
2652 Operands = GETOPERANDS (VmPtr);
2653
2654 //
2655 // Get operand1 index if present
2656 //
2657 Size = 2;
2658 if (Operands & OPERAND_M_CMPI_INDEX) {
2659 Index16 = VmReadIndex16 (VmPtr, 2);
2660 Size += 2;
2661 } else {
2662 Index16 = 0;
2663 }
2664 //
2665 // Get operand1 data we're going to compare to
2666 //
2667 Op1 = (INT64) VmPtr->R[OPERAND1_REGNUM (Operands)];
2668 if (OPERAND1_INDIRECT (Operands)) {
2669 //
2670 // Indirect operand1. Fetch 32 or 64-bit value based on compare size.
2671 //
2672 if (Opcode & OPCODE_M_CMPI64) {
2673 Op1 = (INT64) VmReadMem64 (VmPtr, (UINTN) Op1 + Index16);
2674 } else {
2675 Op1 = (INT64) VmReadMem32 (VmPtr, (UINTN) Op1 + Index16);
2676 }
2677 } else {
2678 //
2679 // Better not have been an index with direct. That is, CMPI R1 Index,...
2680 // is illegal.
2681 //
2682 if (Operands & OPERAND_M_CMPI_INDEX) {
2683 EbcDebugSignalException (
2684 EXCEPT_EBC_INSTRUCTION_ENCODING,
2685 EXCEPTION_FLAG_ERROR,
2686 VmPtr
2687 );
2688 VmPtr->Ip += Size;
2689 return EFI_UNSUPPORTED;
2690 }
2691 }
2692 //
2693 // Get immediate data -- 16- or 32-bit sign extended
2694 //
2695 if (Opcode & OPCODE_M_CMPI32_DATA) {
2696 Op2 = (INT64) VmReadImmed32 (VmPtr, Size);
2697 Size += 4;
2698 } else {
2699 //
2700 // 16-bit immediate data. Sign extend always.
2701 //
2702 Op2 = (INT64) ((INT16) VmReadImmed16 (VmPtr, Size));
2703 Size += 2;
2704 }
2705 //
2706 // Now do the compare
2707 //
2708 Flag = 0;
2709 if (Opcode & OPCODE_M_CMPI64) {
2710 //
2711 // 64 bit comparison
2712 //
2713 switch (Opcode & OPCODE_M_OPCODE) {
2714 case OPCODE_CMPIEQ:
2715 if (Op1 == (INT64) Op2) {
2716 Flag = 1;
2717 }
2718 break;
2719
2720 case OPCODE_CMPILTE:
2721 if (Op1 <= (INT64) Op2) {
2722 Flag = 1;
2723 }
2724 break;
2725
2726 case OPCODE_CMPIGTE:
2727 if (Op1 >= (INT64) Op2) {
2728 Flag = 1;
2729 }
2730 break;
2731
2732 case OPCODE_CMPIULTE:
2733 if ((UINT64) Op1 <= (UINT64) ((UINT32) Op2)) {
2734 Flag = 1;
2735 }
2736 break;
2737
2738 case OPCODE_CMPIUGTE:
2739 if ((UINT64) Op1 >= (UINT64) ((UINT32) Op2)) {
2740 Flag = 1;
2741 }
2742 break;
2743
2744 default:
2745 ASSERT (0);
2746 }
2747 } else {
2748 //
2749 // 32-bit comparisons
2750 //
2751 switch (Opcode & OPCODE_M_OPCODE) {
2752 case OPCODE_CMPIEQ:
2753 if ((INT32) Op1 == Op2) {
2754 Flag = 1;
2755 }
2756 break;
2757
2758 case OPCODE_CMPILTE:
2759 if ((INT32) Op1 <= Op2) {
2760 Flag = 1;
2761 }
2762 break;
2763
2764 case OPCODE_CMPIGTE:
2765 if ((INT32) Op1 >= Op2) {
2766 Flag = 1;
2767 }
2768 break;
2769
2770 case OPCODE_CMPIULTE:
2771 if ((UINT32) Op1 <= (UINT32) Op2) {
2772 Flag = 1;
2773 }
2774 break;
2775
2776 case OPCODE_CMPIUGTE:
2777 if ((UINT32) Op1 >= (UINT32) Op2) {
2778 Flag = 1;
2779 }
2780 break;
2781
2782 default:
2783 ASSERT (0);
2784 }
2785 }
2786 //
2787 // Now set the flag accordingly for the comparison
2788 //
2789 if (Flag) {
2790 VMFLAG_SET (VmPtr, VMFLAGS_CC);
2791 } else {
2792 VMFLAG_CLEAR (VmPtr, VMFLAGS_CC);
2793 }
2794 //
2795 // Advance the IP
2796 //
2797 VmPtr->Ip += Size;
2798 return EFI_SUCCESS;
2799 }
2800
2801 STATIC
2802 UINT64
2803 ExecuteNOT (
2804 IN VM_CONTEXT *VmPtr,
2805 IN UINT64 Op1,
2806 IN UINT64 Op2
2807 )
2808 /*++
2809
2810 Routine Description:
2811 Execute the EBC NOT instruction
2812
2813 Arguments:
2814 VmPtr - pointer to a VM context
2815 Op1 - Operand 1 from the instruction
2816 Op2 - Operand 2 from the instruction
2817
2818 Returns:
2819 ~Op2
2820
2821 Instruction syntax:
2822 NOT[32|64] {@}R1, {@}R2 {Index16|Immed16}
2823
2824 --*/
2825 {
2826 return ~Op2;
2827 }
2828
2829 STATIC
2830 UINT64
2831 ExecuteNEG (
2832 IN VM_CONTEXT *VmPtr,
2833 IN UINT64 Op1,
2834 IN UINT64 Op2
2835 )
2836 /*++
2837
2838 Routine Description:
2839 Execute the EBC NEG instruction
2840
2841 Arguments:
2842 VmPtr - pointer to a VM context
2843 Op1 - Operand 1 from the instruction
2844 Op2 - Operand 2 from the instruction
2845
2846 Returns:
2847 Op2 * -1
2848
2849 Instruction syntax:
2850 NEG[32|64] {@}R1, {@}R2 {Index16|Immed16}
2851
2852 --*/
2853 {
2854 return ~Op2 + 1;
2855 }
2856
2857 STATIC
2858 UINT64
2859 ExecuteADD (
2860 IN VM_CONTEXT *VmPtr,
2861 IN UINT64 Op1,
2862 IN UINT64 Op2
2863 )
2864 /*++
2865
2866 Routine Description:
2867
2868 Execute the EBC ADD instruction
2869
2870 Arguments:
2871 VmPtr - pointer to a VM context
2872 Op1 - Operand 1 from the instruction
2873 Op2 - Operand 2 from the instruction
2874
2875 Returns:
2876 Op1 + Op2
2877
2878 Instruction syntax:
2879 ADD[32|64] {@}R1, {@}R2 {Index16}
2880
2881 --*/
2882 {
2883 return Op1 + Op2;
2884 }
2885
2886 STATIC
2887 UINT64
2888 ExecuteSUB (
2889 IN VM_CONTEXT *VmPtr,
2890 IN UINT64 Op1,
2891 IN UINT64 Op2
2892 )
2893 /*++
2894
2895 Routine Description:
2896 Execute the EBC SUB instruction
2897
2898 Arguments:
2899 VmPtr - pointer to a VM context
2900 Op1 - Operand 1 from the instruction
2901 Op2 - Operand 2 from the instruction
2902
2903 Returns:
2904 Op1 - Op2
2905 Standard EFI_STATUS
2906
2907 Instruction syntax:
2908 SUB[32|64] {@}R1, {@}R2 {Index16|Immed16}
2909
2910 --*/
2911 {
2912 if (*VmPtr->Ip & DATAMANIP_M_64) {
2913 return (UINT64) ((INT64) ((INT64) Op1 - (INT64) Op2));
2914 } else {
2915 return (UINT64) ((INT64) ((INT32) Op1 - (INT32) Op2));
2916 }
2917 }
2918
2919 STATIC
2920 UINT64
2921 ExecuteMUL (
2922 IN VM_CONTEXT *VmPtr,
2923 IN UINT64 Op1,
2924 IN UINT64 Op2
2925 )
2926 /*++
2927
2928 Routine Description:
2929
2930 Execute the EBC MUL instruction
2931
2932 Arguments:
2933 VmPtr - pointer to a VM context
2934 Op1 - Operand 1 from the instruction
2935 Op2 - Operand 2 from the instruction
2936
2937 Returns:
2938 Op1 * Op2
2939
2940 Instruction syntax:
2941 MUL[32|64] {@}R1, {@}R2 {Index16|Immed16}
2942
2943 --*/
2944 {
2945 if (*VmPtr->Ip & DATAMANIP_M_64) {
2946 return MultS64x64 ((INT64)Op1, (INT64)Op2);
2947 } else {
2948 return (UINT64) ((INT64) ((INT32) Op1 * (INT32) Op2));
2949 }
2950 }
2951
2952 STATIC
2953 UINT64
2954 ExecuteMULU (
2955 IN VM_CONTEXT *VmPtr,
2956 IN UINT64 Op1,
2957 IN UINT64 Op2
2958 )
2959 /*++
2960
2961 Routine Description:
2962 Execute the EBC MULU instruction
2963
2964 Arguments:
2965 VmPtr - pointer to a VM context
2966 Op1 - Operand 1 from the instruction
2967 Op2 - Operand 2 from the instruction
2968
2969 Returns:
2970 (unsigned)Op1 * (unsigned)Op2
2971
2972 Instruction syntax:
2973 MULU[32|64] {@}R1, {@}R2 {Index16|Immed16}
2974
2975 --*/
2976 {
2977 if (*VmPtr->Ip & DATAMANIP_M_64) {
2978 return MultU64x64 (Op1, Op2);
2979 } else {
2980 return (UINT64) ((UINT32) Op1 * (UINT32) Op2);
2981 }
2982 }
2983
2984 STATIC
2985 UINT64
2986 ExecuteDIV (
2987 IN VM_CONTEXT *VmPtr,
2988 IN UINT64 Op1,
2989 IN UINT64 Op2
2990 )
2991 /*++
2992
2993 Routine Description:
2994
2995 Execute the EBC DIV instruction
2996
2997 Arguments:
2998 VmPtr - pointer to a VM context
2999 Op1 - Operand 1 from the instruction
3000 Op2 - Operand 2 from the instruction
3001
3002 Returns:
3003 Op1/Op2
3004
3005 Instruction syntax:
3006 DIV[32|64] {@}R1, {@}R2 {Index16|Immed16}
3007
3008 --*/
3009 {
3010 INT64 Remainder;
3011
3012 //
3013 // Check for divide-by-0
3014 //
3015 if (Op2 == 0) {
3016 EbcDebugSignalException (
3017 EXCEPT_EBC_DIVIDE_ERROR,
3018 EXCEPTION_FLAG_FATAL,
3019 VmPtr
3020 );
3021
3022 return 0;
3023 } else {
3024 if (*VmPtr->Ip & DATAMANIP_M_64) {
3025 return (UINT64) (DivS64x64Remainder (Op1, Op2, &Remainder));
3026 } else {
3027 return (UINT64) ((INT64) ((INT32) Op1 / (INT32) Op2));
3028 }
3029 }
3030 }
3031
3032 STATIC
3033 UINT64
3034 ExecuteDIVU (
3035 IN VM_CONTEXT *VmPtr,
3036 IN UINT64 Op1,
3037 IN UINT64 Op2
3038 )
3039 /*++
3040
3041 Routine Description:
3042 Execute the EBC DIVU instruction
3043
3044 Arguments:
3045 VmPtr - pointer to a VM context
3046 Op1 - Operand 1 from the instruction
3047 Op2 - Operand 2 from the instruction
3048
3049 Returns:
3050 (unsigned)Op1 / (unsigned)Op2
3051
3052 Instruction syntax:
3053 DIVU[32|64] {@}R1, {@}R2 {Index16|Immed16}
3054
3055 --*/
3056 {
3057 UINT64 Remainder;
3058
3059 //
3060 // Check for divide-by-0
3061 //
3062 if (Op2 == 0) {
3063 EbcDebugSignalException (
3064 EXCEPT_EBC_DIVIDE_ERROR,
3065 EXCEPTION_FLAG_FATAL,
3066 VmPtr
3067 );
3068 return 0;
3069 } else {
3070 //
3071 // Get the destination register
3072 //
3073 if (*VmPtr->Ip & DATAMANIP_M_64) {
3074 return (UINT64) (DivU64x64Remainder ((INT64)Op1, (INT64)Op2, &Remainder));
3075 } else {
3076 return (UINT64) ((UINT32) Op1 / (UINT32) Op2);
3077 }
3078 }
3079 }
3080
3081 STATIC
3082 UINT64
3083 ExecuteMOD (
3084 IN VM_CONTEXT *VmPtr,
3085 IN UINT64 Op1,
3086 IN UINT64 Op2
3087 )
3088 /*++
3089
3090 Routine Description:
3091 Execute the EBC MOD instruction
3092
3093 Arguments:
3094 VmPtr - pointer to a VM context
3095 Op1 - Operand 1 from the instruction
3096 Op2 - Operand 2 from the instruction
3097
3098 Returns:
3099 Op1 MODULUS Op2
3100
3101 Instruction syntax:
3102 MOD[32|64] {@}R1, {@}R2 {Index16|Immed16}
3103
3104 --*/
3105 {
3106 INT64 Remainder;
3107
3108 //
3109 // Check for divide-by-0
3110 //
3111 if (Op2 == 0) {
3112 EbcDebugSignalException (
3113 EXCEPT_EBC_DIVIDE_ERROR,
3114 EXCEPTION_FLAG_FATAL,
3115 VmPtr
3116 );
3117 return 0;
3118 } else {
3119 DivS64x64Remainder ((INT64)Op1, (INT64)Op2, &Remainder);
3120 return Remainder;
3121 }
3122 }
3123
3124 STATIC
3125 UINT64
3126 ExecuteMODU (
3127 IN VM_CONTEXT *VmPtr,
3128 IN UINT64 Op1,
3129 IN UINT64 Op2
3130 )
3131 /*++
3132
3133 Routine Description:
3134 Execute the EBC MODU instruction
3135
3136 Arguments:
3137 VmPtr - pointer to a VM context
3138 Op1 - Operand 1 from the instruction
3139 Op2 - Operand 2 from the instruction
3140
3141 Returns:
3142 Op1 UNSIGNED_MODULUS Op2
3143
3144 Instruction syntax:
3145 MODU[32|64] {@}R1, {@}R2 {Index16|Immed16}
3146
3147 --*/
3148 {
3149 UINT64 Remainder;
3150
3151 //
3152 // Check for divide-by-0
3153 //
3154 if (Op2 == 0) {
3155 EbcDebugSignalException (
3156 EXCEPT_EBC_DIVIDE_ERROR,
3157 EXCEPTION_FLAG_FATAL,
3158 VmPtr
3159 );
3160 return 0;
3161 } else {
3162 DivU64x64Remainder (Op1, Op2, &Remainder);
3163 return Remainder;
3164 }
3165 }
3166
3167 STATIC
3168 UINT64
3169 ExecuteAND (
3170 IN VM_CONTEXT *VmPtr,
3171 IN UINT64 Op1,
3172 IN UINT64 Op2
3173 )
3174 /*++
3175
3176 Routine Description:
3177 Execute the EBC AND instruction
3178
3179 Arguments:
3180 VmPtr - pointer to a VM context
3181 Op1 - Operand 1 from the instruction
3182 Op2 - Operand 2 from the instruction
3183
3184 Returns:
3185 Op1 AND Op2
3186
3187 Instruction syntax:
3188 AND[32|64] {@}R1, {@}R2 {Index16|Immed16}
3189
3190 --*/
3191 {
3192 return Op1 & Op2;
3193 }
3194
3195 STATIC
3196 UINT64
3197 ExecuteOR (
3198 IN VM_CONTEXT *VmPtr,
3199 IN UINT64 Op1,
3200 IN UINT64 Op2
3201 )
3202 /*++
3203
3204 Routine Description:
3205 Execute the EBC OR instruction
3206
3207 Arguments:
3208 VmPtr - pointer to a VM context
3209 Op1 - Operand 1 from the instruction
3210 Op2 - Operand 2 from the instruction
3211
3212 Returns:
3213 Op1 OR Op2
3214
3215 Instruction syntax:
3216 OR[32|64] {@}R1, {@}R2 {Index16|Immed16}
3217
3218 --*/
3219 {
3220 return Op1 | Op2;
3221 }
3222
3223 STATIC
3224 UINT64
3225 ExecuteXOR (
3226 IN VM_CONTEXT *VmPtr,
3227 IN UINT64 Op1,
3228 IN UINT64 Op2
3229 )
3230 /*++
3231
3232 Routine Description:
3233 Execute the EBC XOR instruction
3234
3235 Arguments:
3236 VmPtr - pointer to a VM context
3237 Op1 - Operand 1 from the instruction
3238 Op2 - Operand 2 from the instruction
3239
3240 Returns:
3241 Op1 XOR Op2
3242
3243 Instruction syntax:
3244 XOR[32|64] {@}R1, {@}R2 {Index16|Immed16}
3245
3246 --*/
3247 {
3248 return Op1 ^ Op2;
3249 }
3250
3251 STATIC
3252 UINT64
3253 ExecuteSHL (
3254 IN VM_CONTEXT *VmPtr,
3255 IN UINT64 Op1,
3256 IN UINT64 Op2
3257 )
3258 /*++
3259
3260 Routine Description:
3261
3262 Execute the EBC SHL shift left instruction
3263
3264 Arguments:
3265 VmPtr - pointer to a VM context
3266 Op1 - Operand 1 from the instruction
3267 Op2 - Operand 2 from the instruction
3268
3269 Returns:
3270 Op1 << Op2
3271
3272 Instruction syntax:
3273 SHL[32|64] {@}R1, {@}R2 {Index16|Immed16}
3274
3275 --*/
3276 {
3277 if (*VmPtr->Ip & DATAMANIP_M_64) {
3278 return LShiftU64 (Op1, (UINTN)Op2);
3279 } else {
3280 return (UINT64) ((UINT32) ((UINT32) Op1 << (UINT32) Op2));
3281 }
3282 }
3283
3284 STATIC
3285 UINT64
3286 ExecuteSHR (
3287 IN VM_CONTEXT *VmPtr,
3288 IN UINT64 Op1,
3289 IN UINT64 Op2
3290 )
3291 /*++
3292
3293 Routine Description:
3294 Execute the EBC SHR instruction
3295
3296 Arguments:
3297 VmPtr - pointer to a VM context
3298 Op1 - Operand 1 from the instruction
3299 Op2 - Operand 2 from the instruction
3300
3301 Returns:
3302 Op1 >> Op2 (unsigned operands)
3303
3304 Instruction syntax:
3305 SHR[32|64] {@}R1, {@}R2 {Index16|Immed16}
3306
3307 --*/
3308 {
3309 if (*VmPtr->Ip & DATAMANIP_M_64) {
3310 return RShiftU64 (Op1, (UINTN)Op2);
3311 } else {
3312 return (UINT64) ((UINT32) Op1 >> (UINT32) Op2);
3313 }
3314 }
3315
3316 STATIC
3317 UINT64
3318 ExecuteASHR (
3319 IN VM_CONTEXT *VmPtr,
3320 IN UINT64 Op1,
3321 IN UINT64 Op2
3322 )
3323 /*++
3324
3325 Routine Description:
3326 Execute the EBC ASHR instruction
3327
3328 Arguments:
3329 VmPtr - pointer to a VM context
3330 Op1 - Operand 1 from the instruction
3331 Op2 - Operand 2 from the instruction
3332
3333 Returns:
3334 Op1 >> Op2 (signed)
3335
3336 Instruction syntax:
3337 ASHR[32|64] {@}R1, {@}R2 {Index16|Immed16}
3338
3339 --*/
3340 {
3341 if (*VmPtr->Ip & DATAMANIP_M_64) {
3342 return ARShiftU64 (Op1, (UINTN)Op2);
3343 } else {
3344 return (UINT64) ((INT64) ((INT32) Op1 >> (UINT32) Op2));
3345 }
3346 }
3347
3348 STATIC
3349 UINT64
3350 ExecuteEXTNDB (
3351 IN VM_CONTEXT *VmPtr,
3352 IN UINT64 Op1,
3353 IN UINT64 Op2
3354 )
3355 /*++
3356
3357 Routine Description:
3358 Execute the EBC EXTNDB instruction to sign-extend a byte value.
3359
3360 Arguments:
3361 VmPtr - pointer to a VM context
3362 Op1 - Operand 1 from the instruction
3363 Op2 - Operand 2 from the instruction
3364
3365 Returns:
3366 (INT64)(INT8)Op2
3367
3368 Instruction syntax:
3369 EXTNDB[32|64] {@}R1, {@}R2 {Index16|Immed16}
3370
3371
3372 --*/
3373 {
3374 INT8 Data8;
3375 INT64 Data64;
3376 //
3377 // Convert to byte, then return as 64-bit signed value to let compiler
3378 // sign-extend the value
3379 //
3380 Data8 = (INT8) Op2;
3381 Data64 = (INT64) Data8;
3382
3383 return (UINT64) Data64;
3384 }
3385
3386 STATIC
3387 UINT64
3388 ExecuteEXTNDW (
3389 IN VM_CONTEXT *VmPtr,
3390 IN UINT64 Op1,
3391 IN UINT64 Op2
3392 )
3393 /*++
3394
3395 Routine Description:
3396 Execute the EBC EXTNDW instruction to sign-extend a 16-bit value.
3397
3398 Arguments:
3399 VmPtr - pointer to a VM context
3400 Op1 - Operand 1 from the instruction
3401 Op2 - Operand 2 from the instruction
3402
3403 Returns:
3404 (INT64)(INT16)Op2
3405
3406 Instruction syntax:
3407 EXTNDW[32|64] {@}R1, {@}R2 {Index16|Immed16}
3408
3409
3410 --*/
3411 {
3412 INT16 Data16;
3413 INT64 Data64;
3414 //
3415 // Convert to word, then return as 64-bit signed value to let compiler
3416 // sign-extend the value
3417 //
3418 Data16 = (INT16) Op2;
3419 Data64 = (INT64) Data16;
3420
3421 return (UINT64) Data64;
3422 }
3423 //
3424 // Execute the EBC EXTNDD instruction.
3425 //
3426 // Format: EXTNDD {@}Rx, {@}Ry [Index16|Immed16]
3427 // EXTNDD Dest, Source
3428 //
3429 // Operation: Dest <- SignExtended((DWORD)Source))
3430 //
3431 STATIC
3432 UINT64
3433 ExecuteEXTNDD (
3434 IN VM_CONTEXT *VmPtr,
3435 IN UINT64 Op1,
3436 IN UINT64 Op2
3437 )
3438 /*++
3439
3440 Routine Description:
3441 Execute the EBC EXTNDD instruction to sign-extend a 32-bit value.
3442
3443 Arguments:
3444 VmPtr - pointer to a VM context
3445 Op1 - Operand 1 from the instruction
3446 Op2 - Operand 2 from the instruction
3447
3448 Returns:
3449 (INT64)(INT32)Op2
3450
3451 Instruction syntax:
3452 EXTNDD[32|64] {@}R1, {@}R2 {Index16|Immed16}
3453
3454
3455 --*/
3456 {
3457 INT32 Data32;
3458 INT64 Data64;
3459 //
3460 // Convert to 32-bit value, then return as 64-bit signed value to let compiler
3461 // sign-extend the value
3462 //
3463 Data32 = (INT32) Op2;
3464 Data64 = (INT64) Data32;
3465
3466 return (UINT64) Data64;
3467 }
3468
3469 STATIC
3470 EFI_STATUS
3471 ExecuteSignedDataManip (
3472 IN VM_CONTEXT *VmPtr
3473 )
3474 {
3475 //
3476 // Just call the data manipulation function with a flag indicating this
3477 // is a signed operation.
3478 //
3479 return ExecuteDataManip (VmPtr, TRUE);
3480 }
3481
3482 STATIC
3483 EFI_STATUS
3484 ExecuteUnsignedDataManip (
3485 IN VM_CONTEXT *VmPtr
3486 )
3487 {
3488 //
3489 // Just call the data manipulation function with a flag indicating this
3490 // is not a signed operation.
3491 //
3492 return ExecuteDataManip (VmPtr, FALSE);
3493 }
3494
3495 STATIC
3496 EFI_STATUS
3497 ExecuteDataManip (
3498 IN VM_CONTEXT *VmPtr,
3499 IN BOOLEAN IsSignedOp
3500 )
3501 /*++
3502
3503 Routine Description:
3504 Execute all the EBC data manipulation instructions.
3505 Since the EBC data manipulation instructions all have the same basic form,
3506 they can share the code that does the fetch of operands and the write-back
3507 of the result. This function performs the fetch of the operands (even if
3508 both are not needed to be fetched, like NOT instruction), dispatches to the
3509 appropriate subfunction, then writes back the returned result.
3510
3511 Arguments:
3512 VmPtr - pointer to VM context
3513
3514 Returns:
3515 Standard EBC status
3516
3517 Format:
3518 INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}
3519
3520 --*/
3521 {
3522 UINT8 Opcode;
3523 INT16 Index16;
3524 UINT8 Operands;
3525 UINT8 Size;
3526 UINT64 Op1;
3527 UINT64 Op2;
3528
3529 //
3530 // Get opcode and operands
3531 //
3532 Opcode = GETOPCODE (VmPtr);
3533 Operands = GETOPERANDS (VmPtr);
3534
3535 //
3536 // Determine if we have immediate data by the opcode
3537 //
3538 if (Opcode & DATAMANIP_M_IMMDATA) {
3539 //
3540 // Index16 if Ry is indirect, or Immed16 if Ry direct.
3541 //
3542 if (OPERAND2_INDIRECT (Operands)) {
3543 Index16 = VmReadIndex16 (VmPtr, 2);
3544 } else {
3545 Index16 = VmReadImmed16 (VmPtr, 2);
3546 }
3547
3548 Size = 4;
3549 } else {
3550 Index16 = 0;
3551 Size = 2;
3552 }
3553 //
3554 // Now get operand2 (source). It's of format {@}R2 {Index16|Immed16}
3555 //
3556 Op2 = (UINT64) VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16;
3557 if (OPERAND2_INDIRECT (Operands)) {
3558 //
3559 // Indirect form: @R2 Index16. Fetch as 32- or 64-bit data
3560 //
3561 if (Opcode & DATAMANIP_M_64) {
3562 Op2 = VmReadMem64 (VmPtr, (UINTN) Op2);
3563 } else {
3564 //
3565 // Read as signed value where appropriate.
3566 //
3567 if (IsSignedOp) {
3568 Op2 = (UINT64) (INT64) ((INT32) VmReadMem32 (VmPtr, (UINTN) Op2));
3569 } else {
3570 Op2 = (UINT64) VmReadMem32 (VmPtr, (UINTN) Op2);
3571 }
3572 }
3573 } else {
3574 if ((Opcode & DATAMANIP_M_64) == 0) {
3575 if (IsSignedOp) {
3576 Op2 = (UINT64) (INT64) ((INT32) Op2);
3577 } else {
3578 Op2 = (UINT64) ((UINT32) Op2);
3579 }
3580 }
3581 }
3582 //
3583 // Get operand1 (destination and sometimes also an actual operand)
3584 // of form {@}R1
3585 //
3586 Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];
3587 if (OPERAND1_INDIRECT (Operands)) {
3588 if (Opcode & DATAMANIP_M_64) {
3589 Op1 = VmReadMem64 (VmPtr, (UINTN) Op1);
3590 } else {
3591 if (IsSignedOp) {
3592 Op1 = (UINT64) (INT64) ((INT32) VmReadMem32 (VmPtr, (UINTN) Op1));
3593 } else {
3594 Op1 = (UINT64) VmReadMem32 (VmPtr, (UINTN) Op1);
3595 }
3596 }
3597 } else {
3598 if ((Opcode & DATAMANIP_M_64) == 0) {
3599 if (IsSignedOp) {
3600 Op1 = (UINT64) (INT64) ((INT32) Op1);
3601 } else {
3602 Op1 = (UINT64) ((UINT32) Op1);
3603 }
3604 }
3605 }
3606 //
3607 // Dispatch to the computation function
3608 //
3609 if (((Opcode & OPCODE_M_OPCODE) - OPCODE_NOT) >=
3610 (sizeof (mDataManipDispatchTable) / sizeof (mDataManipDispatchTable[0]))
3611 ) {
3612 EbcDebugSignalException (
3613 EXCEPT_EBC_INVALID_OPCODE,
3614 EXCEPTION_FLAG_ERROR,
3615 VmPtr
3616 );
3617 //
3618 // Advance and return
3619 //
3620 VmPtr->Ip += Size;
3621 return EFI_UNSUPPORTED;
3622 } else {
3623 Op2 = mDataManipDispatchTable[(Opcode & OPCODE_M_OPCODE) - OPCODE_NOT](VmPtr, Op1, Op2);
3624 }
3625 //
3626 // Write back the result.
3627 //
3628 if (OPERAND1_INDIRECT (Operands)) {
3629 Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];
3630 if (Opcode & DATAMANIP_M_64) {
3631 VmWriteMem64 (VmPtr, (UINTN) Op1, Op2);
3632 } else {
3633 VmWriteMem32 (VmPtr, (UINTN) Op1, (UINT32) Op2);
3634 }
3635 } else {
3636 //
3637 // Storage back to a register. Write back, clearing upper bits (as per
3638 // the specification) if 32-bit operation.
3639 //
3640 VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;
3641 if ((Opcode & DATAMANIP_M_64) == 0) {
3642 VmPtr->R[OPERAND1_REGNUM (Operands)] &= 0xFFFFFFFF;
3643 }
3644 }
3645 //
3646 // Advance the instruction pointer
3647 //
3648 VmPtr->Ip += Size;
3649 return EFI_SUCCESS;
3650 }
3651
3652 STATIC
3653 EFI_STATUS
3654 ExecuteLOADSP (
3655 IN VM_CONTEXT *VmPtr
3656 )
3657 /*++
3658
3659 Routine Description:
3660 Execute the EBC LOADSP instruction
3661
3662 Arguments:
3663 VmPtr - pointer to a VM context
3664
3665 Returns:
3666 Standard EFI_STATUS
3667
3668 Instruction syntax:
3669 LOADSP SP1, R2
3670
3671 --*/
3672 {
3673 UINT8 Operands;
3674
3675 //
3676 // Get the operands
3677 //
3678 Operands = GETOPERANDS (VmPtr);
3679
3680 //
3681 // Do the operation
3682 //
3683 switch (OPERAND1_REGNUM (Operands)) {
3684 //
3685 // Set flags
3686 //
3687 case 0:
3688 //
3689 // Spec states that this instruction will not modify reserved bits in
3690 // the flags register.
3691 //
3692 VmPtr->Flags = (VmPtr->Flags &~VMFLAGS_ALL_VALID) | (VmPtr->R[OPERAND2_REGNUM (Operands)] & VMFLAGS_ALL_VALID);
3693 break;
3694
3695 default:
3696 EbcDebugSignalException (
3697 EXCEPT_EBC_INSTRUCTION_ENCODING,
3698 EXCEPTION_FLAG_WARNING,
3699 VmPtr
3700 );
3701 VmPtr->Ip += 2;
3702 return EFI_UNSUPPORTED;
3703 }
3704
3705 VmPtr->Ip += 2;
3706 return EFI_SUCCESS;
3707 }
3708
3709 STATIC
3710 EFI_STATUS
3711 ExecuteSTORESP (
3712 IN VM_CONTEXT *VmPtr
3713 )
3714 /*++
3715
3716 Routine Description:
3717 Execute the EBC STORESP instruction
3718
3719 Arguments:
3720 VmPtr - pointer to a VM context
3721
3722 Returns:
3723 Standard EFI_STATUS
3724
3725 Instruction syntax:
3726 STORESP Rx, FLAGS|IP
3727
3728 --*/
3729 {
3730 UINT8 Operands;
3731
3732 //
3733 // Get the operands
3734 //
3735 Operands = GETOPERANDS (VmPtr);
3736
3737 //
3738 // Do the operation
3739 //
3740 switch (OPERAND2_REGNUM (Operands)) {
3741 //
3742 // Get flags
3743 //
3744 case 0:
3745 //
3746 // Retrieve the value in the flags register, then clear reserved bits
3747 //
3748 VmPtr->R[OPERAND1_REGNUM (Operands)] = (UINT64) (VmPtr->Flags & VMFLAGS_ALL_VALID);
3749 break;
3750
3751 //
3752 // Get IP -- address of following instruction
3753 //
3754 case 1:
3755 VmPtr->R[OPERAND1_REGNUM (Operands)] = (UINT64) (UINTN) VmPtr->Ip + 2;
3756 break;
3757
3758 default:
3759 EbcDebugSignalException (
3760 EXCEPT_EBC_INSTRUCTION_ENCODING,
3761 EXCEPTION_FLAG_WARNING,
3762 VmPtr
3763 );
3764 VmPtr->Ip += 2;
3765 return EFI_UNSUPPORTED;
3766 break;
3767 }
3768
3769 VmPtr->Ip += 2;
3770 return EFI_SUCCESS;
3771 }
3772
3773 STATIC
3774 INT16
3775 VmReadIndex16 (
3776 IN VM_CONTEXT *VmPtr,
3777 IN UINT32 CodeOffset
3778 )
3779 /*++
3780
3781 Routine Description:
3782 Decode a 16-bit index to determine the offset. Given an index value:
3783
3784 b15 - sign bit
3785 b14:12 - number of bits in this index assigned to natural units (=a)
3786 ba:11 - constant units = C
3787 b0:a - natural units = N
3788
3789 Given this info, the offset can be computed by:
3790 offset = sign_bit * (C + N * sizeof(UINTN))
3791
3792 Max offset is achieved with index = 0x7FFF giving an offset of
3793 0x27B (32-bit machine) or 0x477 (64-bit machine).
3794 Min offset is achieved with index =
3795
3796 Arguments:
3797 VmPtr - pointer to VM context
3798 CodeOffset - offset from IP of the location of the 16-bit index to decode
3799
3800 Returns:
3801 The decoded offset.
3802
3803 --*/
3804 {
3805 UINT16 Index;
3806 INT16 Offset;
3807 INT16 C;
3808 INT16 N;
3809 INT16 NBits;
3810 INT16 Mask;
3811
3812 //
3813 // First read the index from the code stream
3814 //
3815 Index = VmReadCode16 (VmPtr, CodeOffset);
3816
3817 //
3818 // Get the mask for N. First get the number of bits from the index.
3819 //
3820 NBits = (INT16) ((Index & 0x7000) >> 12);
3821
3822 //
3823 // Scale it for 16-bit indexes
3824 //
3825 NBits *= 2;
3826
3827 //
3828 // Now using the number of bits, create a mask.
3829 //
3830 Mask = (INT16) ((INT16)~0 << NBits);
3831
3832 //
3833 // Now using the mask, extract N from the lower bits of the index.
3834 //
3835 N = (INT16) (Index &~Mask);
3836
3837 //
3838 // Now compute C
3839 //
3840 C = (INT16) (((Index &~0xF000) & Mask) >> NBits);
3841
3842 Offset = (INT16) (N * sizeof (UINTN) + C);
3843
3844 //
3845 // Now set the sign
3846 //
3847 if (Index & 0x8000) {
3848 //
3849 // Do it the hard way to work around a bogus compiler warning
3850 //
3851 // Offset = -1 * Offset;
3852 //
3853 Offset = (INT16) ((INT32) Offset * -1);
3854 }
3855
3856 return Offset;
3857 }
3858
3859 STATIC
3860 INT32
3861 VmReadIndex32 (
3862 IN VM_CONTEXT *VmPtr,
3863 IN UINT32 CodeOffset
3864 )
3865 /*++
3866
3867 Routine Description:
3868 Decode a 32-bit index to determine the offset.
3869
3870 Arguments:
3871 VmPtr - pointer to VM context
3872 CodeOffset - offset from IP of the location of the 32-bit index to decode
3873
3874 Returns:
3875 Converted index per EBC VM specification
3876
3877 --*/
3878 {
3879 UINT32 Index;
3880 INT32 Offset;
3881 INT32 C;
3882 INT32 N;
3883 INT32 NBits;
3884 INT32 Mask;
3885
3886 Index = VmReadImmed32 (VmPtr, CodeOffset);
3887
3888 //
3889 // Get the mask for N. First get the number of bits from the index.
3890 //
3891 NBits = (Index & 0x70000000) >> 28;
3892
3893 //
3894 // Scale it for 32-bit indexes
3895 //
3896 NBits *= 4;
3897
3898 //
3899 // Now using the number of bits, create a mask.
3900 //
3901 Mask = (INT32)~0 << NBits;
3902
3903 //
3904 // Now using the mask, extract N from the lower bits of the index.
3905 //
3906 N = Index &~Mask;
3907
3908 //
3909 // Now compute C
3910 //
3911 C = ((Index &~0xF0000000) & Mask) >> NBits;
3912
3913 Offset = N * sizeof (UINTN) + C;
3914
3915 //
3916 // Now set the sign
3917 //
3918 if (Index & 0x80000000) {
3919 Offset = Offset * -1;
3920 }
3921
3922 return Offset;
3923 }
3924
3925 STATIC
3926 INT64
3927 VmReadIndex64 (
3928 IN VM_CONTEXT *VmPtr,
3929 IN UINT32 CodeOffset
3930 )
3931 /*++
3932
3933 Routine Description:
3934 Decode a 64-bit index to determine the offset.
3935
3936 Arguments:
3937 VmPtr - pointer to VM context
3938 CodeOffset - offset from IP of the location of the 64-bit index to decode
3939
3940 Returns:
3941 Converted index per EBC VM specification
3942
3943 --*/
3944 {
3945 UINT64 Index;
3946 INT64 Offset;
3947 INT64 C;
3948 INT64 N;
3949 INT64 NBits;
3950 INT64 Mask;
3951
3952 Index = VmReadCode64 (VmPtr, CodeOffset);
3953
3954 //
3955 // Get the mask for N. First get the number of bits from the index.
3956 //
3957 NBits = RShiftU64 ((Index & 0x7000000000000000ULL), 60);
3958
3959 //
3960 // Scale it for 64-bit indexes (multiply by 8 by shifting left 3)
3961 //
3962 NBits = LShiftU64 ((UINT64)NBits, 3);
3963
3964 //
3965 // Now using the number of bits, create a mask.
3966 //
3967 Mask = (LShiftU64 ((UINT64)~0, (UINTN)NBits));
3968
3969 //
3970 // Now using the mask, extract N from the lower bits of the index.
3971 //
3972 N = Index &~Mask;
3973
3974 //
3975 // Now compute C
3976 //
3977 C = ARShiftU64 (((Index &~0xF000000000000000ULL) & Mask), (UINTN)NBits);
3978
3979 Offset = MultU64x64 (N, sizeof (UINTN)) + C;
3980
3981 //
3982 // Now set the sign
3983 //
3984 if (Index & 0x8000000000000000ULL) {
3985 Offset = MultS64x64 (Offset, -1);
3986 }
3987
3988 return Offset;
3989 }
3990
3991 STATIC
3992 EFI_STATUS
3993 VmWriteMem8 (
3994 IN VM_CONTEXT *VmPtr,
3995 IN UINTN Addr,
3996 IN UINT8 Data
3997 )
3998 /*++
3999
4000 Routine Description:
4001 The following VmWriteMem? routines are called by the EBC data
4002 movement instructions that write to memory. Since these writes
4003 may be to the stack, which looks like (high address on top) this,
4004
4005 [EBC entry point arguments]
4006 [VM stack]
4007 [EBC stack]
4008
4009 we need to detect all attempts to write to the EBC entry point argument
4010 stack area and adjust the address (which will initially point into the
4011 VM stack) to point into the EBC entry point arguments.
4012
4013 Arguments:
4014 VmPtr - pointer to a VM context
4015 Addr - adddress to write to
4016 Data - value to write to Addr
4017
4018 Returns:
4019 Standard EFI_STATUS
4020
4021 --*/
4022 {
4023 //
4024 // Convert the address if it's in the stack gap
4025 //
4026 Addr = ConvertStackAddr (VmPtr, Addr);
4027 *(UINT8 *) Addr = Data;
4028 return EFI_SUCCESS;
4029 }
4030
4031 STATIC
4032 EFI_STATUS
4033 VmWriteMem16 (
4034 IN VM_CONTEXT *VmPtr,
4035 IN UINTN Addr,
4036 IN UINT16 Data
4037 )
4038 {
4039 EFI_STATUS Status;
4040
4041 //
4042 // Convert the address if it's in the stack gap
4043 //
4044 Addr = ConvertStackAddr (VmPtr, Addr);
4045
4046 //
4047 // Do a simple write if aligned
4048 //
4049 if (IS_ALIGNED (Addr, sizeof (UINT16))) {
4050 *(UINT16 *) Addr = Data;
4051 } else {
4052 //
4053 // Write as two bytes
4054 //
4055 MemoryFence ();
4056 if ((Status = VmWriteMem8 (VmPtr, Addr, (UINT8) Data)) != EFI_SUCCESS) {
4057 return Status;
4058 }
4059
4060 MemoryFence ();
4061 if ((Status = VmWriteMem8 (VmPtr, Addr + 1, (UINT8) (Data >> 8))) != EFI_SUCCESS) {
4062 return Status;
4063 }
4064
4065 MemoryFence ();
4066 }
4067
4068 return EFI_SUCCESS;
4069 }
4070
4071 STATIC
4072 EFI_STATUS
4073 VmWriteMem32 (
4074 IN VM_CONTEXT *VmPtr,
4075 IN UINTN Addr,
4076 IN UINT32 Data
4077 )
4078 {
4079 EFI_STATUS Status;
4080
4081 //
4082 // Convert the address if it's in the stack gap
4083 //
4084 Addr = ConvertStackAddr (VmPtr, Addr);
4085
4086 //
4087 // Do a simple write if aligned
4088 //
4089 if (IS_ALIGNED (Addr, sizeof (UINT32))) {
4090 *(UINT32 *) Addr = Data;
4091 } else {
4092 //
4093 // Write as two words
4094 //
4095 MemoryFence ();
4096 if ((Status = VmWriteMem16 (VmPtr, Addr, (UINT16) Data)) != EFI_SUCCESS) {
4097 return Status;
4098 }
4099
4100 MemoryFence ();
4101 if ((Status = VmWriteMem16 (VmPtr, Addr + sizeof (UINT16), (UINT16) (Data >> 16))) != EFI_SUCCESS) {
4102 return Status;
4103 }
4104
4105 MemoryFence ();
4106 }
4107
4108 return EFI_SUCCESS;
4109 }
4110
4111 EFI_STATUS
4112 VmWriteMem64 (
4113 IN VM_CONTEXT *VmPtr,
4114 IN UINTN Addr,
4115 IN UINT64 Data
4116 )
4117 {
4118 EFI_STATUS Status;
4119 UINT32 Data32;
4120
4121 //
4122 // Convert the address if it's in the stack gap
4123 //
4124 Addr = ConvertStackAddr (VmPtr, Addr);
4125
4126 //
4127 // Do a simple write if aligned
4128 //
4129 if (IS_ALIGNED (Addr, sizeof (UINT64))) {
4130 *(UINT64 *) Addr = Data;
4131 } else {
4132 //
4133 // Write as two 32-bit words
4134 //
4135 MemoryFence ();
4136 if ((Status = VmWriteMem32 (VmPtr, Addr, (UINT32) Data)) != EFI_SUCCESS) {
4137 return Status;
4138 }
4139
4140 MemoryFence ();
4141 Data32 = (UINT32) (((UINT32 *) &Data)[1]);
4142 if ((Status = VmWriteMem32 (VmPtr, Addr + sizeof (UINT32), Data32)) != EFI_SUCCESS) {
4143 return Status;
4144 }
4145
4146 MemoryFence ();
4147 }
4148
4149 return EFI_SUCCESS;
4150 }
4151
4152 EFI_STATUS
4153 VmWriteMemN (
4154 IN VM_CONTEXT *VmPtr,
4155 IN UINTN Addr,
4156 IN UINTN Data
4157 )
4158 {
4159 EFI_STATUS Status;
4160 UINTN Index;
4161
4162 Status = EFI_SUCCESS;
4163
4164 //
4165 // Convert the address if it's in the stack gap
4166 //
4167 Addr = ConvertStackAddr (VmPtr, Addr);
4168
4169 //
4170 // Do a simple write if aligned
4171 //
4172 if (IS_ALIGNED (Addr, sizeof (UINTN))) {
4173 *(UINTN *) Addr = Data;
4174 } else {
4175 for (Index = 0; Index < sizeof (UINTN) / sizeof (UINT32); Index++) {
4176 MemoryFence ();
4177 Status = VmWriteMem32 (VmPtr, Addr + Index * sizeof (UINT32), (UINT32) Data);
4178 MemoryFence ();
4179 Data = (UINTN)RShiftU64 ((UINT64)Data, 32);
4180 }
4181 }
4182
4183 return Status;
4184 }
4185
4186 STATIC
4187 INT8
4188 VmReadImmed8 (
4189 IN VM_CONTEXT *VmPtr,
4190 IN UINT32 Offset
4191 )
4192 /*++
4193
4194 Routine Description:
4195
4196 The following VmReadImmed routines are called by the EBC execute
4197 functions to read EBC immediate values from the code stream.
4198 Since we can't assume alignment, each tries to read in the biggest
4199 chunks size available, but will revert to smaller reads if necessary.
4200
4201 Arguments:
4202 VmPtr - pointer to a VM context
4203 Offset - offset from IP of the code bytes to read.
4204
4205 Returns:
4206 Signed data of the requested size from the specified address.
4207
4208 --*/
4209 {
4210 //
4211 // Simply return the data in flat memory space
4212 //
4213 return * (INT8 *) (VmPtr->Ip + Offset);
4214 }
4215
4216 STATIC
4217 INT16
4218 VmReadImmed16 (
4219 IN VM_CONTEXT *VmPtr,
4220 IN UINT32 Offset
4221 )
4222 {
4223 //
4224 // Read direct if aligned
4225 //
4226 if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (INT16))) {
4227 return * (INT16 *) (VmPtr->Ip + Offset);
4228 } else {
4229 //
4230 // All code word reads should be aligned
4231 //
4232 EbcDebugSignalException (
4233 EXCEPT_EBC_ALIGNMENT_CHECK,
4234 EXCEPTION_FLAG_WARNING,
4235 VmPtr
4236 );
4237 }
4238 //
4239 // Return unaligned data
4240 //
4241 return (INT16) (*(UINT8 *) (VmPtr->Ip + Offset) + (*(UINT8 *) (VmPtr->Ip + Offset + 1) << 8));
4242 }
4243
4244 STATIC
4245 INT32
4246 VmReadImmed32 (
4247 IN VM_CONTEXT *VmPtr,
4248 IN UINT32 Offset
4249 )
4250 {
4251 UINT32 Data;
4252
4253 //
4254 // Read direct if aligned
4255 //
4256 if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {
4257 return * (INT32 *) (VmPtr->Ip + Offset);
4258 }
4259 //
4260 // Return unaligned data
4261 //
4262 Data = (UINT32) VmReadCode16 (VmPtr, Offset);
4263 Data |= (UINT32) (VmReadCode16 (VmPtr, Offset + 2) << 16);
4264 return Data;
4265 }
4266
4267 STATIC
4268 INT64
4269 VmReadImmed64 (
4270 IN VM_CONTEXT *VmPtr,
4271 IN UINT32 Offset
4272 )
4273 {
4274 UINT64 Data64;
4275 UINT32 Data32;
4276 UINT8 *Ptr;
4277
4278 //
4279 // Read direct if aligned
4280 //
4281 if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {
4282 return * (UINT64 *) (VmPtr->Ip + Offset);
4283 }
4284 //
4285 // Return unaligned data.
4286 //
4287 Ptr = (UINT8 *) &Data64;
4288 Data32 = VmReadCode32 (VmPtr, Offset);
4289 *(UINT32 *) Ptr = Data32;
4290 Ptr += sizeof (Data32);
4291 Data32 = VmReadCode32 (VmPtr, Offset + sizeof (UINT32));
4292 *(UINT32 *) Ptr = Data32;
4293 return Data64;
4294 }
4295
4296 STATIC
4297 UINT16
4298 VmReadCode16 (
4299 IN VM_CONTEXT *VmPtr,
4300 IN UINT32 Offset
4301 )
4302 /*++
4303
4304 Routine Description:
4305 The following VmReadCode() routines provide the ability to read raw
4306 unsigned data from the code stream.
4307
4308 Arguments:
4309 VmPtr - pointer to VM context
4310 Offset - offset from current IP to the raw data to read.
4311
4312 Returns:
4313 The raw unsigned 16-bit value from the code stream.
4314
4315 --*/
4316 {
4317 //
4318 // Read direct if aligned
4319 //
4320 if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT16))) {
4321 return * (UINT16 *) (VmPtr->Ip + Offset);
4322 } else {
4323 //
4324 // All code word reads should be aligned
4325 //
4326 EbcDebugSignalException (
4327 EXCEPT_EBC_ALIGNMENT_CHECK,
4328 EXCEPTION_FLAG_WARNING,
4329 VmPtr
4330 );
4331 }
4332 //
4333 // Return unaligned data
4334 //
4335 return (UINT16) (*(UINT8 *) (VmPtr->Ip + Offset) + (*(UINT8 *) (VmPtr->Ip + Offset + 1) << 8));
4336 }
4337
4338 STATIC
4339 UINT32
4340 VmReadCode32 (
4341 IN VM_CONTEXT *VmPtr,
4342 IN UINT32 Offset
4343 )
4344 {
4345 UINT32 Data;
4346 //
4347 // Read direct if aligned
4348 //
4349 if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {
4350 return * (UINT32 *) (VmPtr->Ip + Offset);
4351 }
4352 //
4353 // Return unaligned data
4354 //
4355 Data = (UINT32) VmReadCode16 (VmPtr, Offset);
4356 Data |= (VmReadCode16 (VmPtr, Offset + 2) << 16);
4357 return Data;
4358 }
4359
4360 STATIC
4361 UINT64
4362 VmReadCode64 (
4363 IN VM_CONTEXT *VmPtr,
4364 IN UINT32 Offset
4365 )
4366 {
4367 UINT64 Data64;
4368 UINT32 Data32;
4369 UINT8 *Ptr;
4370
4371 //
4372 // Read direct if aligned
4373 //
4374 if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {
4375 return * (UINT64 *) (VmPtr->Ip + Offset);
4376 }
4377 //
4378 // Return unaligned data.
4379 //
4380 Ptr = (UINT8 *) &Data64;
4381 Data32 = VmReadCode32 (VmPtr, Offset);
4382 *(UINT32 *) Ptr = Data32;
4383 Ptr += sizeof (Data32);
4384 Data32 = VmReadCode32 (VmPtr, Offset + sizeof (UINT32));
4385 *(UINT32 *) Ptr = Data32;
4386 return Data64;
4387 }
4388
4389 STATIC
4390 UINT8
4391 VmReadMem8 (
4392 IN VM_CONTEXT *VmPtr,
4393 IN UINTN Addr
4394 )
4395 {
4396 //
4397 // Convert the address if it's in the stack gap
4398 //
4399 Addr = ConvertStackAddr (VmPtr, Addr);
4400 //
4401 // Simply return the data in flat memory space
4402 //
4403 return * (UINT8 *) Addr;
4404 }
4405
4406 STATIC
4407 UINT16
4408 VmReadMem16 (
4409 IN VM_CONTEXT *VmPtr,
4410 IN UINTN Addr
4411 )
4412 {
4413 //
4414 // Convert the address if it's in the stack gap
4415 //
4416 Addr = ConvertStackAddr (VmPtr, Addr);
4417 //
4418 // Read direct if aligned
4419 //
4420 if (IS_ALIGNED (Addr, sizeof (UINT16))) {
4421 return * (UINT16 *) Addr;
4422 }
4423 //
4424 // Return unaligned data
4425 //
4426 return (UINT16) (*(UINT8 *) Addr + (*(UINT8 *) (Addr + 1) << 8));
4427 }
4428
4429 STATIC
4430 UINT32
4431 VmReadMem32 (
4432 IN VM_CONTEXT *VmPtr,
4433 IN UINTN Addr
4434 )
4435 {
4436 UINT32 Data;
4437
4438 //
4439 // Convert the address if it's in the stack gap
4440 //
4441 Addr = ConvertStackAddr (VmPtr, Addr);
4442 //
4443 // Read direct if aligned
4444 //
4445 if (IS_ALIGNED (Addr, sizeof (UINT32))) {
4446 return * (UINT32 *) Addr;
4447 }
4448 //
4449 // Return unaligned data
4450 //
4451 Data = (UINT32) VmReadMem16 (VmPtr, Addr);
4452 Data |= (VmReadMem16 (VmPtr, Addr + 2) << 16);
4453 return Data;
4454 }
4455
4456 STATIC
4457 UINT64
4458 VmReadMem64 (
4459 IN VM_CONTEXT *VmPtr,
4460 IN UINTN Addr
4461 )
4462 {
4463 UINT64 Data;
4464 UINT32 Data32;
4465
4466 //
4467 // Convert the address if it's in the stack gap
4468 //
4469 Addr = ConvertStackAddr (VmPtr, Addr);
4470
4471 //
4472 // Read direct if aligned
4473 //
4474 if (IS_ALIGNED (Addr, sizeof (UINT64))) {
4475 return * (UINT64 *) Addr;
4476 }
4477 //
4478 // Return unaligned data. Assume little endian.
4479 //
4480 Data = (UINT64) VmReadMem32 (VmPtr, Addr);
4481 Data32 = VmReadMem32 (VmPtr, Addr + sizeof (UINT32));
4482 *(UINT32 *) ((UINT32 *) &Data + 1) = Data32;
4483 return Data;
4484 }
4485
4486 STATIC
4487 UINTN
4488 ConvertStackAddr (
4489 IN VM_CONTEXT *VmPtr,
4490 IN UINTN Addr
4491 )
4492 /*++
4493
4494 Routine Description:
4495
4496 Given an address that EBC is going to read from or write to, return
4497 an appropriate address that accounts for a gap in the stack.
4498
4499 The stack for this application looks like this (high addr on top)
4500 [EBC entry point arguments]
4501 [VM stack]
4502 [EBC stack]
4503
4504 The EBC assumes that its arguments are at the top of its stack, which
4505 is where the VM stack is really. Therefore if the EBC does memory
4506 accesses into the VM stack area, then we need to convert the address
4507 to point to the EBC entry point arguments area. Do this here.
4508
4509 Arguments:
4510
4511 VmPtr - pointer to VM context
4512 Addr - address of interest
4513
4514 Returns:
4515
4516 The unchanged address if it's not in the VM stack region. Otherwise,
4517 adjust for the stack gap and return the modified address.
4518
4519 --*/
4520 {
4521 if ((Addr >= VmPtr->LowStackTop) && (Addr < VmPtr->HighStackBottom)) {
4522 //
4523 // In the stack gap -- now make sure it's not in the VM itself, which
4524 // would be the case if it's accessing VM register contents.
4525 //
4526 if ((Addr < (UINTN) VmPtr) || (Addr > (UINTN) VmPtr + sizeof (VM_CONTEXT))) {
4527 VmPtr->LastAddrConverted = Addr;
4528 VmPtr->LastAddrConvertedValue = Addr - VmPtr->LowStackTop + VmPtr->HighStackBottom;
4529 return Addr - VmPtr->LowStackTop + VmPtr->HighStackBottom;
4530 }
4531 }
4532
4533 return Addr;
4534 }
4535
4536 STATIC
4537 UINTN
4538 VmReadMemN (
4539 IN VM_CONTEXT *VmPtr,
4540 IN UINTN Addr
4541 )
4542 /*++
4543
4544 Routine Description:
4545 Read a natural value from memory. May or may not be aligned.
4546
4547 Arguments:
4548 VmPtr - current VM context
4549 Addr - the address to read from
4550
4551 Returns:
4552 The natural value at address Addr.
4553
4554 --*/
4555 {
4556 UINTN Data;
4557 UINT32 Size;
4558 UINT8 *FromPtr;
4559 UINT8 *ToPtr;
4560 //
4561 // Convert the address if it's in the stack gap
4562 //
4563 Addr = ConvertStackAddr (VmPtr, Addr);
4564 //
4565 // Read direct if aligned
4566 //
4567 if (IS_ALIGNED (Addr, sizeof (UINTN))) {
4568 return * (UINTN *) Addr;
4569 }
4570 //
4571 // Return unaligned data
4572 //
4573 Data = 0;
4574 FromPtr = (UINT8 *) Addr;
4575 ToPtr = (UINT8 *) &Data;
4576
4577 for (Size = 0; Size < sizeof (Data); Size++) {
4578 *ToPtr = *FromPtr;
4579 ToPtr++;
4580 FromPtr++;
4581 }
4582
4583 return Data;
4584 }
4585
4586 UINT64
4587 GetVmVersion (
4588 VOID
4589 )
4590 {
4591 return (UINT64) (((VM_MAJOR_VERSION & 0xFFFF) << 16) | ((VM_MINOR_VERSION & 0xFFFF)));
4592 }