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