]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/EbcDxe/EbcDebugger/EdbDisasmSupport.c
MdeModulePkg/EbcDxe: add EBC Debugger
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / EbcDebugger / EdbDisasmSupport.c
1 /*++
2
3 Copyright (c) 2007, 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 EdbDisasmSupport.c
15
16 Abstract:
17
18
19 --*/
20
21 #include "Edb.h"
22
23 extern EDB_DISASM_INSTRUCTION mEdbDisasmInstructionTable[];
24
25 typedef struct {
26 CHAR16 Name[EDB_INSTRUCTION_NAME_MAX_LENGTH];
27 CHAR16 Content[EDB_INSTRUCTION_CONTENT_MAX_LENGTH];
28 CHAR16 Tail;
29 } EDB_INSTRUCTION_STRING;
30
31 EDB_INSTRUCTION_STRING mInstructionString;
32 UINTN mInstructionNameOffset;
33 UINTN mInstructionContentOffset;
34
35 VOID
36 EdbSetOffset (
37 IN UINTN InstructionNameOffset,
38 IN UINTN InstructionContentOffset
39 )
40 /*++
41
42 Routine Description:
43
44 Set offset for Instruction name and content
45
46 Arguments:
47
48 InstructionNameOffset - Instruction name offset
49 InstructionContentOffset - Instruction content offset
50
51 Returns:
52
53 None
54
55 --*/
56 {
57 mInstructionNameOffset = InstructionNameOffset;
58 mInstructionContentOffset = InstructionContentOffset;
59
60 return ;
61 }
62
63 CHAR16 *
64 EdbPreInstructionString (
65 VOID
66 )
67 /*++
68
69 Routine Description:
70
71 Pre instruction string construction
72
73 Arguments:
74
75 None
76
77 Returns:
78
79 Instruction string
80
81 --*/
82 {
83 ZeroMem (&mInstructionString, sizeof(mInstructionString));
84 mInstructionNameOffset = 0;
85 mInstructionContentOffset = 0;
86
87 return (CHAR16 *)&mInstructionString;
88 }
89
90 CHAR16 *
91 EdbPostInstructionString (
92 VOID
93 )
94 /*++
95
96 Routine Description:
97
98 Post instruction string construction
99
100 Arguments:
101
102 None
103
104 Returns:
105
106 Instruction string
107
108 --*/
109 {
110 CHAR16 *Char;
111
112 for (Char = (CHAR16 *)&mInstructionString; Char < &mInstructionString.Tail; Char++) {
113 if (*Char == 0) {
114 *Char = L' ';
115 }
116 }
117 mInstructionString.Tail = 0;
118
119 mInstructionNameOffset = 0;
120 mInstructionContentOffset = 0;
121
122 return (CHAR16 *)&mInstructionString;
123 }
124
125 BOOLEAN
126 EdbGetNaturalIndex16 (
127 IN UINT16 Data16,
128 OUT UINTN *NaturalUnits,
129 OUT UINTN *ConstantUnits
130 )
131 /*++
132
133 Routine Description:
134
135 Get Sign, NaturalUnits, and ConstantUnits of the WORD data
136
137 Arguments:
138
139 Data16 - WORD data
140 NaturalUnits - Natural Units of the WORD
141 ConstantUnits - Constant Units of the WORD
142
143 Returns:
144
145 Sign value of WORD
146
147 --*/
148 {
149 BOOLEAN Sign;
150 UINTN NaturalUnitBit;
151
152 Sign = (BOOLEAN)(Data16 >> 15);
153 NaturalUnitBit = (UINTN)((Data16 >> 12) & 0x7);
154 NaturalUnitBit *= 2;
155 Data16 = Data16 & 0xFFF;
156 *NaturalUnits = (UINTN)(Data16 & ((1 << NaturalUnitBit) - 1));
157 *ConstantUnits = (UINTN)((Data16 >> NaturalUnitBit) & ((1 << (12 - NaturalUnitBit)) - 1));
158
159 return Sign;
160 }
161
162 BOOLEAN
163 EdbGetNaturalIndex32 (
164 IN UINT32 Data32,
165 OUT UINTN *NaturalUnits,
166 OUT UINTN *ConstantUnits
167 )
168 /*++
169
170 Routine Description:
171
172 Get Sign, NaturalUnits, and ConstantUnits of the DWORD data
173
174 Arguments:
175
176 Data32 - DWORD data
177 NaturalUnits - Natural Units of the DWORD
178 ConstantUnits - Constant Units of the DWORD
179
180 Returns:
181
182 Sign value of DWORD
183
184 --*/
185 {
186 BOOLEAN Sign;
187 UINTN NaturalUnitBit;
188
189 Sign = (BOOLEAN)(Data32 >> 31);
190 NaturalUnitBit = (UINTN)((Data32 >> 28) & 0x7);
191 NaturalUnitBit *= 4;
192 Data32 = Data32 & 0xFFFFFFF;
193 *NaturalUnits = (UINTN)(Data32 & ((1 << NaturalUnitBit) - 1));
194 *ConstantUnits = (UINTN)((Data32 >> NaturalUnitBit) & ((1 << (28 - NaturalUnitBit)) - 1));
195
196 return Sign;
197 }
198
199 BOOLEAN
200 EdbGetNaturalIndex64 (
201 IN UINT64 Data64,
202 OUT UINT64 *NaturalUnits,
203 OUT UINT64 *ConstantUnits
204 )
205 /*++
206
207 Routine Description:
208
209 Get Sign, NaturalUnits, and ConstantUnits of the QWORD data
210
211 Arguments:
212
213 Data64 - QWORD data
214 NaturalUnits - Natural Units of the QWORD
215 ConstantUnits - Constant Units of the QWORD
216
217 Returns:
218
219 Sign value of QWORD
220
221 --*/
222 {
223 BOOLEAN Sign;
224 UINTN NaturalUnitBit;
225
226 Sign = (BOOLEAN)RShiftU64 (Data64, 63);
227 NaturalUnitBit = (UINTN)(RShiftU64 (Data64, 60) & 0x7);
228 NaturalUnitBit *= 8;
229 Data64 = RShiftU64 (LShiftU64 (Data64, 4), 4);
230 *NaturalUnits = (UINT64)(Data64 & (LShiftU64 (1, NaturalUnitBit) - 1));
231 *ConstantUnits = (UINT64)(RShiftU64 (Data64, NaturalUnitBit) & (LShiftU64 (1, (60 - NaturalUnitBit)) - 1));
232
233 return Sign;
234 }
235
236 UINT8
237 EdbGetBitWidth (
238 IN UINT64 Value
239 )
240 /*++
241
242 Routine Description:
243
244 Get Bit Width of the value
245
246 Arguments:
247
248 Value - data
249
250 Returns:
251
252 Bit width
253
254 --*/
255 {
256 if (Value >= 10000000000000) {
257 return 14;
258 } else if (Value >= 1000000000000) {
259 return 13;
260 } else if (Value >= 100000000000) {
261 return 12;
262 } else if (Value >= 10000000000) {
263 return 11;
264 } else if (Value >= 1000000000) {
265 return 10;
266 } else if (Value >= 100000000) {
267 return 9;
268 } else if (Value >= 10000000) {
269 return 8;
270 } else if (Value >= 1000000) {
271 return 7;
272 } else if (Value >= 100000) {
273 return 6;
274 } else if (Value >= 10000) {
275 return 5;
276 } else if (Value >= 1000) {
277 return 4;
278 } else if (Value >= 100) {
279 return 3;
280 } else if (Value >= 10) {
281 return 2;
282 } else {
283 return 1;
284 }
285 }
286
287 UINTN
288 EdbPrintInstructionName (
289 IN CHAR16 *Name
290 )
291 /*++
292
293 Routine Description:
294
295 Print the instruction name
296
297 Arguments:
298
299 Name - instruction name
300
301 Returns:
302
303 Instruction name offset
304
305 --*/
306 {
307 EDBSPrintWithOffset (
308 mInstructionString.Name,
309 EDB_INSTRUCTION_NAME_MAX_SIZE,
310 mInstructionNameOffset,
311 L"%s",
312 Name
313 );
314 mInstructionNameOffset += StrLen (Name);
315
316 return mInstructionNameOffset;
317 }
318
319 UINTN
320 EdbPrintRegister1 (
321 IN UINT8 Operands
322 )
323 /*++
324
325 Routine Description:
326
327 Print register 1 in operands
328
329 Arguments:
330
331 Operands - instruction operands
332
333 Returns:
334
335 Instruction content offset
336
337 --*/
338 {
339 if (Operands & OPERAND_M_INDIRECT1) {
340 EDBSPrintWithOffset (
341 mInstructionString.Content,
342 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
343 mInstructionContentOffset,
344 L"@"
345 );
346 mInstructionContentOffset += 1;
347 }
348 EDBSPrintWithOffset (
349 mInstructionString.Content,
350 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
351 mInstructionContentOffset,
352 L"R%d",
353 (UINTN)(Operands & OPERAND_M_OP1)
354 );
355 mInstructionContentOffset += 2;
356
357 return mInstructionContentOffset;
358 }
359
360 UINTN
361 EdbPrintRegister2 (
362 IN UINT8 Operands
363 )
364 /*++
365
366 Routine Description:
367
368 Print register 2 in operands
369
370 Arguments:
371
372 Operands - instruction operands
373
374 Returns:
375
376 Instruction content offset
377
378 --*/
379 {
380 if (Operands & OPERAND_M_INDIRECT2) {
381 EDBSPrintWithOffset (
382 mInstructionString.Content,
383 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
384 mInstructionContentOffset,
385 L"@"
386 );
387 mInstructionContentOffset += 1;
388 }
389 EDBSPrintWithOffset (
390 mInstructionString.Content,
391 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
392 mInstructionContentOffset,
393 L"R%d",
394 (UINTN)((Operands & OPERAND_M_OP2) >> 4)
395 );
396 mInstructionContentOffset += 2;
397
398 return mInstructionContentOffset;
399 }
400
401 UINTN
402 EdbPrintDedicatedRegister1 (
403 IN UINT8 Operands
404 )
405 /*++
406
407 Routine Description:
408
409 Print dedicated register 1 in operands
410
411 Arguments:
412
413 Operands - instruction operands
414
415 Returns:
416
417 Instruction content offset
418
419 --*/
420 {
421 switch (Operands & OPERAND_M_OP1) {
422 case 0:
423 EDBSPrintWithOffset (
424 mInstructionString.Content,
425 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
426 mInstructionContentOffset,
427 L"[FLAGS]"
428 );
429 mInstructionContentOffset += 7;
430 break;
431 case 1:
432 EDBSPrintWithOffset (
433 mInstructionString.Content,
434 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
435 mInstructionContentOffset,
436 L"[IP]"
437 );
438 mInstructionContentOffset += 4;
439 break;
440 }
441
442 return mInstructionContentOffset;
443 }
444
445 UINTN
446 EdbPrintDedicatedRegister2 (
447 IN UINT8 Operands
448 )
449 /*++
450
451 Routine Description:
452
453 Print dedicated register 2 in operands
454
455 Arguments:
456
457 Operands - instruction operands
458
459 Returns:
460
461 Instruction content offset
462
463 --*/
464 {
465 switch ((Operands & OPERAND_M_OP2) >> 4) {
466 case 0:
467 EDBSPrintWithOffset (
468 mInstructionString.Content,
469 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
470 mInstructionContentOffset,
471 L"[FLAGS]"
472 );
473 mInstructionContentOffset += 7;
474 break;
475 case 1:
476 EDBSPrintWithOffset (
477 mInstructionString.Content,
478 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
479 mInstructionContentOffset,
480 L"[IP]"
481 );
482 mInstructionContentOffset += 4;
483 break;
484 }
485
486 return mInstructionContentOffset;
487 }
488
489 UINTN
490 EdbPrintIndexData (
491 IN BOOLEAN Sign,
492 IN UINTN NaturalUnits,
493 IN UINTN ConstantUnits
494 )
495 /*++
496
497 Routine Description:
498
499 Print the hexical UINTN index data to instruction content
500
501 Arguments:
502
503 Sign - Signed bit of UINTN data
504 NaturalUnits - natural units of UINTN data
505 ConstantUnits - natural units of UINTN data
506
507 Returns:
508
509 Instruction content offset
510
511 --*/
512 {
513 EDBSPrintWithOffset (
514 mInstructionString.Content,
515 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
516 mInstructionContentOffset,
517 L"(%s%d,%s%d)",
518 Sign ? L"-" : L"+",
519 NaturalUnits,
520 Sign ? L"-" : L"+",
521 ConstantUnits
522 );
523 mInstructionContentOffset = mInstructionContentOffset + 5 + EdbGetBitWidth (NaturalUnits) + EdbGetBitWidth (ConstantUnits);
524
525 return mInstructionContentOffset;
526 }
527
528 UINTN
529 EdbPrintIndexData64 (
530 IN BOOLEAN Sign,
531 IN UINT64 NaturalUnits,
532 IN UINT64 ConstantUnits
533 )
534 /*++
535
536 Routine Description:
537
538 Print the hexical QWORD index data to instruction content
539
540 Arguments:
541
542 Sign - Signed bit of QWORD data
543 NaturalUnits - natural units of QWORD data
544 ConstantUnits - natural units of QWORD data
545
546 Returns:
547
548 Instruction content offset
549
550 --*/
551 {
552 EDBSPrintWithOffset (
553 mInstructionString.Content,
554 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
555 mInstructionContentOffset,
556 L"(%s%ld,%s%ld)",
557 Sign ? L"-" : L"+",
558 NaturalUnits,
559 Sign ? L"-" : L"+",
560 ConstantUnits
561 );
562 mInstructionContentOffset = mInstructionContentOffset + 5 + EdbGetBitWidth (NaturalUnits) + EdbGetBitWidth (ConstantUnits);
563
564 return mInstructionContentOffset;
565 }
566
567 UINTN
568 EdbPrintRawIndexData16 (
569 IN UINT16 Data16
570 )
571 /*++
572
573 Routine Description:
574
575 Print the hexical WORD raw index data to instruction content
576
577 Arguments:
578
579 Data16 - WORD data
580
581 Returns:
582
583 Instruction content offset
584
585 --*/
586 {
587 BOOLEAN Sign;
588 UINTN NaturalUnits;
589 UINTN ConstantUnits;
590 UINTN Offset;
591
592 Sign = EdbGetNaturalIndex16 (Data16, &NaturalUnits, &ConstantUnits);
593 Offset = EdbPrintIndexData (Sign, NaturalUnits, ConstantUnits);
594
595 return Offset;
596 }
597
598 UINTN
599 EdbPrintRawIndexData32 (
600 IN UINT32 Data32
601 )
602 /*++
603
604 Routine Description:
605
606 Print the hexical DWORD raw index data to instruction content
607
608 Arguments:
609
610 Data32 - DWORD data
611
612 Returns:
613
614 Instruction content offset
615
616 --*/
617 {
618 BOOLEAN Sign;
619 UINTN NaturalUnits;
620 UINTN ConstantUnits;
621 UINTN Offset;
622
623 Sign = EdbGetNaturalIndex32 (Data32, &NaturalUnits, &ConstantUnits);
624 Offset = EdbPrintIndexData (Sign, NaturalUnits, ConstantUnits);
625
626 return Offset;
627 }
628
629 UINTN
630 EdbPrintRawIndexData64 (
631 IN UINT64 Data64
632 )
633 /*++
634
635 Routine Description:
636
637 Print the hexical QWORD raw index data to instruction content
638
639 Arguments:
640
641 Data64 - QWORD data
642
643 Returns:
644
645 Instruction content offset
646
647 --*/
648 {
649 BOOLEAN Sign;
650 UINT64 NaturalUnits;
651 UINT64 ConstantUnits;
652 UINTN Offset;
653
654 Sign = EdbGetNaturalIndex64 (Data64, &NaturalUnits, &ConstantUnits);
655 Offset = EdbPrintIndexData64 (Sign, NaturalUnits, ConstantUnits);
656
657 return Offset;
658 }
659
660 UINTN
661 EdbPrintImmData8 (
662 IN UINT8 Data
663 )
664 /*++
665
666 Routine Description:
667
668 Print the hexical BYTE immediate data to instruction content
669
670 Arguments:
671
672 Data - BYTE data
673
674 Returns:
675
676 Instruction content offset
677
678 --*/
679 {
680 EDBSPrintWithOffset (
681 mInstructionString.Content,
682 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
683 mInstructionContentOffset,
684 L"(0x%02x)",
685 (UINTN)Data
686 );
687 mInstructionContentOffset += 6;
688
689 return mInstructionContentOffset;
690 }
691
692 UINTN
693 EdbPrintImmData16 (
694 IN UINT16 Data
695 )
696 /*++
697
698 Routine Description:
699
700 Print the hexical WORD immediate data to instruction content
701
702 Arguments:
703
704 Data - WORD data
705
706 Returns:
707
708 Instruction content offset
709
710 --*/
711 {
712 EDBSPrintWithOffset (
713 mInstructionString.Content,
714 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
715 mInstructionContentOffset,
716 L"(0x%04x)",
717 (UINTN)Data
718 );
719 mInstructionContentOffset += 8;
720
721 return mInstructionContentOffset;
722 }
723
724 UINTN
725 EdbPrintImmData32 (
726 IN UINT32 Data
727 )
728 /*++
729
730 Routine Description:
731
732 Print the hexical DWORD immediate data to instruction content
733
734 Arguments:
735
736 Data - DWORD data
737
738 Returns:
739
740 Instruction content offset
741
742 --*/
743 {
744 EDBSPrintWithOffset (
745 mInstructionString.Content,
746 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
747 mInstructionContentOffset,
748 L"(0x%08x)",
749 (UINTN)Data
750 );
751 mInstructionContentOffset += 12;
752
753 return mInstructionContentOffset;
754 }
755
756 UINTN
757 EdbPrintImmData64 (
758 IN UINT64 Data
759 )
760 /*++
761
762 Routine Description:
763
764 Print the hexical QWORD immediate data to instruction content
765
766 Arguments:
767
768 Data - QWORD data
769
770 Returns:
771
772 Instruction content offset
773
774 --*/
775 {
776 EDBSPrintWithOffset (
777 mInstructionString.Content,
778 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
779 mInstructionContentOffset,
780 L"(0x%016lx)",
781 Data
782 );
783 mInstructionContentOffset += 20;
784
785 return mInstructionContentOffset;
786 }
787
788 UINTN
789 EdbPrintImmDatan (
790 IN UINTN Data
791 )
792 /*++
793
794 Routine Description:
795
796 Print the decimal UINTN immediate data to instruction content
797
798 Arguments:
799
800 Data - UINTN data
801
802 Returns:
803
804 Instruction content offset
805
806 --*/
807 {
808 EDBSPrintWithOffset (
809 mInstructionString.Content,
810 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
811 mInstructionContentOffset,
812 L"(%d)",
813 (UINTN)Data
814 );
815 mInstructionContentOffset = mInstructionContentOffset + 2 + EdbGetBitWidth (Data);
816
817 return mInstructionContentOffset;
818 }
819
820 UINTN
821 EdbPrintImmData64n (
822 IN UINT64 Data64
823 )
824 /*++
825
826 Routine Description:
827
828 Print the decimal QWORD immediate data to instruction content
829
830 Arguments:
831
832 Data64 - QWORD data
833
834 Returns:
835
836 Instruction content offset
837
838 --*/
839 {
840 EDBSPrintWithOffset (
841 mInstructionString.Content,
842 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
843 mInstructionContentOffset,
844 L"(%ld)",
845 Data64
846 );
847 mInstructionContentOffset = mInstructionContentOffset + 2 + EdbGetBitWidth (Data64);
848
849 return mInstructionContentOffset;
850 }
851
852 UINTN
853 EdbPrintData8 (
854 IN UINT8 Data8
855 )
856 /*++
857
858 Routine Description:
859
860 Print the hexical BYTE to instruction content
861
862 Arguments:
863
864 Data8 - BYTE data
865
866 Returns:
867
868 Instruction content offset
869
870 --*/
871 {
872 EDBSPrintWithOffset (
873 mInstructionString.Content,
874 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
875 mInstructionContentOffset,
876 L"0x%02x",
877 (UINTN)Data8
878 );
879 mInstructionContentOffset += 4;
880
881 return mInstructionContentOffset;
882 }
883
884 UINTN
885 EdbPrintData16 (
886 IN UINT16 Data16
887 )
888 /*++
889
890 Routine Description:
891
892 Print the hexical WORD to instruction content
893
894 Arguments:
895
896 Data16 - WORD data
897
898 Returns:
899
900 Instruction content offset
901
902 --*/
903 {
904 EDBSPrintWithOffset (
905 mInstructionString.Content,
906 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
907 mInstructionContentOffset,
908 L"0x%04x",
909 (UINTN)Data16
910 );
911 mInstructionContentOffset += 6;
912
913 return mInstructionContentOffset;
914 }
915
916 UINTN
917 EdbPrintData32 (
918 IN UINT32 Data32
919 )
920 /*++
921
922 Routine Description:
923
924 Print the hexical DWORD to instruction content
925
926 Arguments:
927
928 Data32 - DWORD data
929
930 Returns:
931
932 Instruction content offset
933
934 --*/
935 {
936 EDBSPrintWithOffset (
937 mInstructionString.Content,
938 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
939 mInstructionContentOffset,
940 L"0x%08x",
941 (UINTN)Data32
942 );
943 mInstructionContentOffset += 10;
944
945 return mInstructionContentOffset;
946 }
947
948 UINTN
949 EdbPrintData64 (
950 IN UINT64 Data64
951 )
952 /*++
953
954 Routine Description:
955
956 Print the hexical QWORD to instruction content
957
958 Arguments:
959
960 Data64 - QWORD data
961
962 Returns:
963
964 Instruction content offset
965
966 --*/
967 {
968 EDBSPrintWithOffset (
969 mInstructionString.Content,
970 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
971 mInstructionContentOffset,
972 L"0x%016lx",
973 (UINT64)Data64
974 );
975 mInstructionContentOffset += 18;
976
977 return mInstructionContentOffset;
978 }
979
980 UINTN
981 EdbPrintDatan (
982 IN UINTN Data
983 )
984 /*++
985
986 Routine Description:
987
988 Print the decimal unsigned UINTN to instruction content
989
990 Arguments:
991
992 Data - unsigned UINTN data
993
994 Returns:
995
996 Instruction content offset
997
998 --*/
999 {
1000 EDBSPrintWithOffset (
1001 mInstructionString.Content,
1002 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
1003 mInstructionContentOffset,
1004 L"%d",
1005 (UINTN)Data
1006 );
1007 mInstructionContentOffset = mInstructionContentOffset + EdbGetBitWidth (Data);
1008
1009 return mInstructionContentOffset;
1010 }
1011
1012 UINTN
1013 EdbPrintData64n (
1014 IN UINT64 Data64
1015 )
1016 /*++
1017
1018 Routine Description:
1019
1020 Print the decimal unsigned QWORD to instruction content
1021
1022 Arguments:
1023
1024 Data64 - unsigned QWORD data
1025
1026 Returns:
1027
1028 Instruction content offset
1029
1030 --*/
1031 {
1032 EDBSPrintWithOffset (
1033 mInstructionString.Content,
1034 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
1035 mInstructionContentOffset,
1036 L"%ld",
1037 Data64
1038 );
1039 mInstructionContentOffset = mInstructionContentOffset + EdbGetBitWidth (Data64);
1040
1041 return mInstructionContentOffset;
1042 }
1043
1044 UINTN
1045 EdbPrintData8s (
1046 IN UINT8 Data8
1047 )
1048 /*++
1049
1050 Routine Description:
1051
1052 Print the decimal signed BYTE to instruction content
1053
1054 Arguments:
1055
1056 Data8 - signed BYTE data
1057
1058 Returns:
1059
1060 Instruction content offset
1061
1062 --*/
1063 {
1064 BOOLEAN Sign;
1065
1066 Sign = (BOOLEAN)(Data8 >> 7);
1067
1068 EDBSPrintWithOffset (
1069 mInstructionString.Content,
1070 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
1071 mInstructionContentOffset,
1072 L"%s%d",
1073 Sign ? L"-" : L"+",
1074 (UINTN)(Data8 & 0x7F)
1075 );
1076 mInstructionContentOffset = mInstructionContentOffset + 1 + EdbGetBitWidth (Data8 & 0x7F);
1077
1078 return mInstructionContentOffset;
1079 }
1080
1081 UINTN
1082 EdbPrintData16s (
1083 IN UINT16 Data16
1084 )
1085 /*++
1086
1087 Routine Description:
1088
1089 Print the decimal signed WORD to instruction content
1090
1091 Arguments:
1092
1093 Data16 - signed WORD data
1094
1095 Returns:
1096
1097 Instruction content offset
1098
1099 --*/
1100 {
1101 BOOLEAN Sign;
1102
1103 Sign = (BOOLEAN)(Data16 >> 15);
1104
1105 EDBSPrintWithOffset (
1106 mInstructionString.Content,
1107 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
1108 mInstructionContentOffset,
1109 L"%s%d",
1110 Sign ? L"-" : L"+",
1111 (UINTN)(Data16 & 0x7FFF)
1112 );
1113 mInstructionContentOffset = mInstructionContentOffset + 1 + EdbGetBitWidth (Data16 & 0x7FFF);
1114
1115 return mInstructionContentOffset;
1116 }
1117
1118 UINTN
1119 EdbPrintData32s (
1120 IN UINT32 Data32
1121 )
1122 /*++
1123
1124 Routine Description:
1125
1126 Print the decimal signed DWORD to instruction content
1127
1128 Arguments:
1129
1130 Data32 - signed DWORD data
1131
1132 Returns:
1133
1134 Instruction content offset
1135
1136 --*/
1137 {
1138 BOOLEAN Sign;
1139
1140 Sign = (BOOLEAN)(Data32 >> 31);
1141
1142 EDBSPrintWithOffset (
1143 mInstructionString.Content,
1144 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
1145 mInstructionContentOffset,
1146 L"%s%d",
1147 Sign ? L"-" : L"+",
1148 (UINTN)(Data32 & 0x7FFFFFFF)
1149 );
1150 mInstructionContentOffset = mInstructionContentOffset + 1 + EdbGetBitWidth (Data32 & 0x7FFFFFFF);
1151
1152 return mInstructionContentOffset;
1153 }
1154
1155 UINTN
1156 EdbPrintData64s (
1157 IN UINT64 Data64
1158 )
1159 /*++
1160
1161 Routine Description:
1162
1163 Print the decimal signed QWORD to instruction content
1164
1165 Arguments:
1166
1167 Data64 - signed QWORD data
1168
1169 Returns:
1170
1171 Instruction content offset
1172
1173 --*/
1174 {
1175 BOOLEAN Sign;
1176 INT64 Data64s;
1177
1178 Sign = (BOOLEAN)RShiftU64 (Data64, 63);
1179 Data64s = (INT64)RShiftU64 (LShiftU64 (Data64, 1), 1);
1180
1181 EDBSPrintWithOffset (
1182 mInstructionString.Content,
1183 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
1184 mInstructionContentOffset,
1185 L"%s%ld",
1186 Sign ? L"-" : L"+",
1187 (UINT64)Data64s
1188 );
1189 mInstructionContentOffset = mInstructionContentOffset + 1 + EdbGetBitWidth (Data64s);
1190
1191 return mInstructionContentOffset;
1192 }
1193
1194 UINTN
1195 EdbPrintComma (
1196 VOID
1197 )
1198 /*++
1199
1200 Routine Description:
1201
1202 Print the comma to instruction content
1203
1204 Arguments:
1205
1206 None
1207
1208 Returns:
1209
1210 Instruction content offset
1211
1212 --*/
1213 {
1214 EDBSPrintWithOffset (
1215 mInstructionString.Content,
1216 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
1217 mInstructionContentOffset,
1218 L", "
1219 );
1220 mInstructionContentOffset += 2;
1221
1222 return mInstructionContentOffset;
1223 }
1224
1225 UINTN
1226 EdbFindAndPrintSymbol (
1227 IN UINTN Address
1228 )
1229 /*++
1230
1231 Routine Description:
1232
1233 Find the symbol string according to address, then print it
1234
1235 Arguments:
1236
1237 Address - instruction address
1238
1239 Returns:
1240
1241 1 - symbol string is found and printed
1242 0 - symbol string not found
1243
1244 --*/
1245 {
1246 CHAR8 *SymbolStr;
1247
1248 SymbolStr = FindSymbolStr (Address);
1249 if (SymbolStr != NULL) {
1250 EDBSPrintWithOffset (
1251 mInstructionString.Content,
1252 EDB_INSTRUCTION_CONTENT_MAX_SIZE,
1253 mInstructionContentOffset,
1254 L"[%a]",
1255 SymbolStr
1256 );
1257 return 1;
1258 }
1259
1260 return 0;
1261 }
1262
1263 VOID
1264 EdbPrintRaw (
1265 IN EFI_PHYSICAL_ADDRESS InstructionAddress,
1266 IN UINTN InstructionNumber
1267 )
1268 /*++
1269
1270 Routine Description:
1271
1272 Print the EBC byte code
1273
1274 Arguments:
1275
1276 InstructionAddress - instruction address
1277 InstructionNumber - instruction number
1278
1279 Returns:
1280
1281 None
1282
1283 --*/
1284 {
1285 UINTN LineNumber;
1286 UINTN ByteNumber;
1287 UINTN LineIndex;
1288 UINTN ByteIndex;
1289 CHAR8 *SymbolStr;
1290
1291 if (InstructionNumber == 0) {
1292 return ;
1293 }
1294
1295 LineNumber = InstructionNumber / EDB_BYTECODE_NUMBER_IN_LINE;
1296 ByteNumber = InstructionNumber % EDB_BYTECODE_NUMBER_IN_LINE;
1297 if (ByteNumber == 0) {
1298 LineNumber -= 1;
1299 ByteNumber = EDB_BYTECODE_NUMBER_IN_LINE;
1300 }
1301
1302 //
1303 // Print Symbol
1304 //
1305 SymbolStr = FindSymbolStr ((UINTN)InstructionAddress);
1306 if (SymbolStr != NULL) {
1307 EDBPrint (L"[%a]:\n", SymbolStr);
1308 }
1309
1310 for (LineIndex = 0; LineIndex < LineNumber; LineIndex++) {
1311 EDBPrint (EDB_PRINT_ADDRESS_FORMAT, (UINTN)InstructionAddress);
1312 for (ByteIndex = 0; ByteIndex < EDB_BYTECODE_NUMBER_IN_LINE; ByteIndex++) {
1313 EDBPrint (L"%02x ", *(UINT8 *)(UINTN)InstructionAddress);
1314 InstructionAddress += 1;
1315 }
1316 EDBPrint (L"\n");
1317 }
1318
1319 EDBPrint (EDB_PRINT_ADDRESS_FORMAT, (UINTN)InstructionAddress);
1320 for (ByteIndex = 0; ByteIndex < ByteNumber; ByteIndex++) {
1321 EDBPrint (L"%02x ", *(UINT8 *)(UINTN)InstructionAddress);
1322 InstructionAddress += 1;
1323 }
1324 for (ByteIndex = 0; ByteIndex < EDB_BYTECODE_NUMBER_IN_LINE - ByteNumber; ByteIndex++) {
1325 EDBPrint (L" ");
1326 }
1327
1328 return ;
1329 }
1330
1331 EFI_STATUS
1332 EdbShowDisasm (
1333 IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
1334 IN EFI_SYSTEM_CONTEXT SystemContext
1335 )
1336 /*++
1337
1338 Routine Description:
1339
1340 Print the EBC asm code
1341
1342 Arguments:
1343
1344 DebuggerPrivate - EBC Debugger private data structure
1345 SystemContext - EBC system context.
1346
1347 Returns:
1348
1349 EFI_SUCCESS - show disasm successfully
1350
1351 --*/
1352 {
1353 EFI_PHYSICAL_ADDRESS InstructionAddress;
1354 UINTN InstructionNumber;
1355 UINTN InstructionLength;
1356 UINT8 Opcode;
1357 CHAR16 *InstructionString;
1358 // UINTN Result;
1359
1360 InstructionAddress = DebuggerPrivate->InstructionScope;
1361 for (InstructionNumber = 0; InstructionNumber < DebuggerPrivate->InstructionNumber; InstructionNumber++) {
1362
1363 //
1364 // Break each 0x10 instruction
1365 //
1366 if (((InstructionNumber % EFI_DEBUGGER_LINE_NUMBER_IN_PAGE) == 0) &&
1367 (InstructionNumber != 0)) {
1368 if (SetPageBreak ()) {
1369 break;
1370 }
1371 }
1372
1373 Opcode = GET_OPCODE(InstructionAddress);
1374 if ((Opcode < OPCODE_MAX) && (mEdbDisasmInstructionTable[Opcode] != NULL)) {
1375 InstructionLength = mEdbDisasmInstructionTable [Opcode] (InstructionAddress, SystemContext, &InstructionString);
1376 if (InstructionLength != 0) {
1377
1378 //
1379 // Print Source
1380 //
1381 // Result = EdbPrintSource ((UINTN)InstructionAddress, FALSE);
1382
1383 if (!DebuggerPrivate->DebuggerSymbolContext.DisplayCodeOnly) {
1384
1385 EdbPrintRaw (InstructionAddress, InstructionLength);
1386 if (InstructionString != NULL) {
1387 EDBPrint (L"%s\n", InstructionString);
1388 } else {
1389 EDBPrint (L"%s\n", L"<Unknown Instruction>");
1390 }
1391 }
1392
1393 EdbPrintSource ((UINTN)InstructionAddress, TRUE);
1394
1395 InstructionAddress += InstructionLength;
1396 } else {
1397 //
1398 // Something wrong with OPCODE
1399 //
1400 EdbPrintRaw (InstructionAddress, EDB_BYTECODE_NUMBER_IN_LINE);
1401 EDBPrint (L"%s\n", L"<Bad Instruction>");
1402 break;
1403 }
1404 } else {
1405 //
1406 // Something wrong with OPCODE
1407 //
1408 EdbPrintRaw (InstructionAddress, EDB_BYTECODE_NUMBER_IN_LINE);
1409 EDBPrint (L"%s\n", L"<Bad Instruction>");
1410 break;
1411 }
1412 }
1413
1414 return EFI_SUCCESS;
1415 }
1416
1417 UINT64
1418 GetRegisterValue (
1419 IN EFI_SYSTEM_CONTEXT SystemContext,
1420 IN UINT8 Index
1421 )
1422 /*++
1423
1424 Routine Description:
1425
1426 Get register value accroding to the system context, and register index
1427
1428 Arguments:
1429
1430 SystemContext - EBC system context.
1431 Index - EBC register index
1432
1433 Returns:
1434
1435 register value
1436
1437 --*/
1438 {
1439 switch (Index) {
1440 case 0:
1441 return SystemContext.SystemContextEbc->R0;
1442 case 1:
1443 return SystemContext.SystemContextEbc->R1;
1444 case 2:
1445 return SystemContext.SystemContextEbc->R2;
1446 case 3:
1447 return SystemContext.SystemContextEbc->R3;
1448 case 4:
1449 return SystemContext.SystemContextEbc->R4;
1450 case 5:
1451 return SystemContext.SystemContextEbc->R5;
1452 case 6:
1453 return SystemContext.SystemContextEbc->R6;
1454 case 7:
1455 return SystemContext.SystemContextEbc->R7;
1456 default:
1457 ASSERT (FALSE);
1458 break;
1459 }
1460 return 0;
1461 }