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