db_disasm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /* $NetBSD: db_disasm.c,v 1.28 2013/07/04 23:00:23 joerg Exp $ */
  2. /* $OpenBSD: db_disasm.c,v 1.2 1996/12/28 06:21:48 rahnds Exp $ */
  3. #include <sys/cdefs.h>
  4. __FBSDID("$FreeBSD$");
  5. #include <sys/param.h>
  6. #include <sys/proc.h>
  7. #include <sys/systm.h>
  8. #include <machine/db_machdep.h>
  9. #include <ddb/ddb.h>
  10. #include <ddb/db_access.h>
  11. #include <ddb/db_sym.h>
  12. #include <ddb/db_variables.h>
  13. #include <ddb/db_output.h>
  14. enum function_mask {
  15. Op_A = 0x00000001,
  16. Op_B = 0x00000002,
  17. Op_BI = 0x00000004,
  18. Op_BO = 0x00000008,
  19. Op_BC = Op_BI | Op_BO,
  20. Op_CRM = 0x00000010,
  21. Op_D = 0x00000020,
  22. Op_ST = 0x00000020, /* Op_S for store-operations, same as D */
  23. Op_S = 0x00000040, /* S-field is swapped with A-field */
  24. Op_FM = Op_D | Op_S, /* kludge (reduce Op_s) */
  25. Op_dA = 0x00000080,
  26. Op_LK = 0x00000100,
  27. Op_Rc = 0x00000200,
  28. Op_AA = Op_LK | Op_Rc, /* kludge (reduce Op_s) */
  29. Op_LKM = Op_AA,
  30. Op_RcM = Op_AA,
  31. Op_OE = 0x00000400,
  32. Op_SR = 0x00000800,
  33. Op_TO = 0x00001000,
  34. Op_sign = 0x00002000,
  35. Op_const = 0x00004000,
  36. Op_SIMM = Op_const | Op_sign,
  37. Op_UIMM = Op_const,
  38. Op_crbA = 0x00008000,
  39. Op_crbB = 0x00010000,
  40. Op_WS = Op_crbB, /* kludge, same field as crbB */
  41. Op_rSH = Op_crbB, /* kludge, same field as crbB */
  42. Op_crbD = 0x00020000,
  43. Op_crfD = 0x00040000,
  44. Op_crfS = 0x00080000,
  45. Op_ds = 0x00100000,
  46. Op_me = 0x00200000,
  47. Op_spr = 0x00400000,
  48. Op_dcr = Op_spr, /* out of bits - cheat with Op_spr */
  49. Op_tbr = 0x00800000,
  50. Op_BP = 0x01000000,
  51. Op_BD = 0x02000000,
  52. Op_LI = 0x04000000,
  53. Op_C = 0x08000000,
  54. Op_NB = 0x10000000,
  55. Op_sh_mb_sh = 0x20000000,
  56. Op_sh = 0x40000000,
  57. Op_SH = Op_sh | Op_sh_mb_sh,
  58. Op_mb = 0x80000000,
  59. Op_MB = Op_mb | Op_sh_mb_sh,
  60. Op_ME = Op_MB,
  61. };
  62. struct opcode {
  63. const char *name;
  64. u_int32_t mask;
  65. u_int32_t code;
  66. enum function_mask func;
  67. };
  68. typedef u_int32_t instr_t;
  69. typedef void (op_class_func) (instr_t, vm_offset_t);
  70. u_int32_t extract_field(u_int32_t value, u_int32_t base, u_int32_t width);
  71. void disasm_fields(const struct opcode *popcode, instr_t instr, vm_offset_t loc,
  72. char *disasm_str, size_t slen);
  73. void dis_ppc(const struct opcode *opcodeset, instr_t instr, vm_offset_t loc);
  74. op_class_func op_ill, op_base;
  75. op_class_func op_cl_x13, op_cl_x1e, op_cl_x1f;
  76. op_class_func op_cl_x3a, op_cl_x3b;
  77. op_class_func op_cl_x3e, op_cl_x3f;
  78. op_class_func *opcodes_base[] = {
  79. /*x00*/ op_ill, op_ill, op_base, op_ill,
  80. /*x04*/ op_ill, op_ill, op_ill, op_base,
  81. /*x08*/ op_base, op_base, op_base, op_base,
  82. /*x0C*/ op_base, op_base, op_base/*XXX*/, op_base/*XXX*/,
  83. /*x10*/ op_base, op_base, op_base, op_cl_x13,
  84. /*x14*/ op_base, op_base, op_ill, op_base,
  85. /*x18*/ op_base, op_base, op_base, op_base,
  86. /*x1C*/ op_base, op_base, op_cl_x1e, op_cl_x1f,
  87. /*x20*/ op_base, op_base, op_base, op_base,
  88. /*x24*/ op_base, op_base, op_base, op_base,
  89. /*x28*/ op_base, op_base, op_base, op_base,
  90. /*x2C*/ op_base, op_base, op_base, op_base,
  91. /*x30*/ op_base, op_base, op_base, op_base,
  92. /*x34*/ op_base, op_base, op_base, op_base,
  93. /*x38*/ op_ill, op_ill, op_cl_x3a, op_cl_x3b,
  94. /*x3C*/ op_ill, op_ill, op_cl_x3e, op_cl_x3f
  95. };
  96. /* This table could be modified to make significant the "reserved" fields
  97. * of the opcodes, But I didn't feel like it when typing in the table,
  98. * I would recommend that this table be looked over for errors,
  99. * This was derived from the table in Appendix A.2 of (Mot part # MPCFPE/AD)
  100. * PowerPC Microprocessor Family: The Programming Environments
  101. */
  102. const struct opcode opcodes[] = {
  103. { "tdi", 0xfc000000, 0x08000000, Op_TO | Op_A | Op_SIMM },
  104. { "twi", 0xfc000000, 0x0c000000, Op_TO | Op_A | Op_SIMM },
  105. { "mulli", 0xfc000000, 0x1c000000, Op_D | Op_A | Op_SIMM },
  106. { "subfic", 0xfc000000, 0x20000000, Op_D | Op_A | Op_SIMM },
  107. { "cmplwi", 0xfc200000, 0x28000000, Op_crfD | Op_A | Op_SIMM },
  108. { "cmpldi", 0xfc200000, 0x28200000, Op_crfD | Op_A | Op_SIMM },
  109. { "cmpwi", 0xfc200000, 0x2c000000, Op_crfD | Op_A | Op_SIMM },
  110. { "cmpdi", 0xfc200000, 0x2c200000, Op_crfD | Op_A | Op_SIMM },
  111. { "addic", 0xfc000000, 0x30000000, Op_D | Op_A | Op_SIMM },
  112. { "addic.", 0xfc000000, 0x34000000, Op_D | Op_A | Op_SIMM },
  113. { "addi", 0xfc000000, 0x38000000, Op_D | Op_A | Op_SIMM },
  114. { "addis", 0xfc000000, 0x3c000000, Op_D | Op_A | Op_SIMM },
  115. { "b", 0xfc000000, 0x40000000, Op_BC | Op_BD | Op_AA | Op_LK }, /* bc */
  116. { "sc", 0xffffffff, 0x44000002, 0 },
  117. { "b", 0xfc000000, 0x48000000, Op_LI | Op_AA | Op_LK },
  118. { "rlwimi", 0xfc000000, 0x50000000, Op_S | Op_A | Op_SH | Op_MB | Op_ME | Op_Rc },
  119. { "rlwinm", 0xfc000000, 0x54000000, Op_S | Op_A | Op_SH | Op_MB | Op_ME | Op_Rc },
  120. { "rlwnm", 0xfc000000, 0x5c000000, Op_S | Op_A | Op_SH | Op_MB | Op_ME | Op_Rc },
  121. { "ori", 0xfc000000, 0x60000000, Op_S | Op_A | Op_UIMM },
  122. { "oris", 0xfc000000, 0x64000000, Op_S | Op_A | Op_UIMM },
  123. { "xori", 0xfc000000, 0x68000000, Op_S | Op_A | Op_UIMM },
  124. { "xoris", 0xfc000000, 0x6c000000, Op_S | Op_A | Op_UIMM },
  125. { "andi.", 0xfc000000, 0x70000000, Op_S | Op_A | Op_UIMM },
  126. { "andis.", 0xfc000000, 0x74000000, Op_S | Op_A | Op_UIMM },
  127. { "lwz", 0xfc000000, 0x80000000, Op_D | Op_dA },
  128. { "lwzu", 0xfc000000, 0x84000000, Op_D | Op_dA },
  129. { "lbz", 0xfc000000, 0x88000000, Op_D | Op_dA },
  130. { "lbzu", 0xfc000000, 0x8c000000, Op_D | Op_dA },
  131. { "stw", 0xfc000000, 0x90000000, Op_ST | Op_dA },
  132. { "stwu", 0xfc000000, 0x94000000, Op_ST | Op_dA },
  133. { "stb", 0xfc000000, 0x98000000, Op_ST | Op_dA },
  134. { "stbu", 0xfc000000, 0x9c000000, Op_ST | Op_dA },
  135. { "lhz", 0xfc000000, 0xa0000000, Op_D | Op_dA },
  136. { "lhzu", 0xfc000000, 0xa4000000, Op_D | Op_dA },
  137. { "lha", 0xfc000000, 0xa8000000, Op_D | Op_dA },
  138. { "lhau", 0xfc000000, 0xac000000, Op_D | Op_dA },
  139. { "sth", 0xfc000000, 0xb0000000, Op_ST | Op_dA },
  140. { "sthu", 0xfc000000, 0xb4000000, Op_ST | Op_dA },
  141. { "lmw", 0xfc000000, 0xb8000000, Op_D | Op_dA },
  142. { "stmw", 0xfc000000, 0xbc000000, Op_ST | Op_dA },
  143. { "lfs", 0xfc000000, 0xc0000000, Op_D | Op_dA },
  144. { "lfsu", 0xfc000000, 0xc4000000, Op_D | Op_dA },
  145. { "lfd", 0xfc000000, 0xc8000000, Op_D | Op_dA },
  146. { "lfdu", 0xfc000000, 0xcc000000, Op_D | Op_dA },
  147. { "stfs", 0xfc000000, 0xd0000000, Op_ST | Op_dA },
  148. { "stfsu", 0xfc000000, 0xd4000000, Op_ST | Op_dA },
  149. { "stfd", 0xfc000000, 0xd8000000, Op_ST | Op_dA },
  150. { "stfdu", 0xfc000000, 0xdc000000, Op_ST | Op_dA },
  151. { "", 0x0, 0x0, 0 }
  152. };
  153. /* 13 * 4 = 4c */
  154. const struct opcode opcodes_13[] = {
  155. /* 0x13 << 2 */
  156. { "mcrf", 0xfc0007fe, 0x4c000000, Op_crfD | Op_crfS },
  157. { "b", 0xfc0007fe, 0x4c000020, Op_BC | Op_LK }, /* bclr */
  158. { "crnor", 0xfc0007fe, 0x4c000042, Op_crbD | Op_crbA | Op_crbB },
  159. { "rfi", 0xfc0007fe, 0x4c000064, 0 },
  160. { "crandc", 0xfc0007fe, 0x4c000102, Op_crbD | Op_crbA | Op_crbB },
  161. { "isync", 0xfc0007fe, 0x4c00012c, 0 },
  162. { "crxor", 0xfc0007fe, 0x4c000182, Op_crbD | Op_crbA | Op_crbB },
  163. { "crnand", 0xfc0007fe, 0x4c0001c2, Op_crbD | Op_crbA | Op_crbB },
  164. { "crand", 0xfc0007fe, 0x4c000202, Op_crbD | Op_crbA | Op_crbB },
  165. { "creqv", 0xfc0007fe, 0x4c000242, Op_crbD | Op_crbA | Op_crbB },
  166. { "crorc", 0xfc0007fe, 0x4c000342, Op_crbD | Op_crbA | Op_crbB },
  167. { "cror", 0xfc0007fe, 0x4c000382, Op_crbD | Op_crbA | Op_crbB },
  168. { "b", 0xfc0007fe, 0x4c000420, Op_BC | Op_LK }, /* bcctr */
  169. { "", 0x0, 0x0, 0 }
  170. };
  171. /* 1e * 4 = 78 */
  172. const struct opcode opcodes_1e[] = {
  173. { "rldicl", 0xfc00001c, 0x78000000, Op_S | Op_A | Op_sh | Op_mb | Op_Rc },
  174. { "rldicr", 0xfc00001c, 0x78000004, Op_S | Op_A | Op_sh | Op_me | Op_Rc },
  175. { "rldic", 0xfc00001c, 0x78000008, Op_S | Op_A | Op_sh | Op_mb | Op_Rc },
  176. { "rldimi", 0xfc00001c, 0x7800000c, Op_S | Op_A | Op_sh | Op_mb | Op_Rc },
  177. { "rldcl", 0xfc00003e, 0x78000010, Op_S | Op_A | Op_B | Op_mb | Op_Rc },
  178. { "rldcr", 0xfc00003e, 0x78000012, Op_S | Op_A | Op_B | Op_me | Op_Rc },
  179. { "", 0x0, 0x0, 0 }
  180. };
  181. /* 1f * 4 = 7c */
  182. const struct opcode opcodes_1f[] = {
  183. /* 1f << 2 */
  184. { "cmpw", 0xfc2007fe, 0x7c000000, Op_crfD | Op_A | Op_B },
  185. { "cmpd", 0xfc2007fe, 0x7c200000, Op_crfD | Op_A | Op_B },
  186. { "tw", 0xfc0007fe, 0x7c000008, Op_TO | Op_A | Op_B },
  187. { "subfc", 0xfc0003fe, 0x7c000010, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  188. { "mulhdu", 0xfc0007fe, 0x7c000012, Op_D | Op_A | Op_B | Op_Rc },
  189. { "addc", 0xfc0003fe, 0x7c000014, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  190. { "mulhwu", 0xfc0007fe, 0x7c000016, Op_D | Op_A | Op_B | Op_Rc },
  191. { "isellt", 0xfc0007ff, 0x7c00001e, Op_D | Op_A | Op_B },
  192. { "iselgt", 0xfc0007ff, 0x7c00005e, Op_D | Op_A | Op_B },
  193. { "iseleq", 0xfc0007ff, 0x7c00009e, Op_D | Op_A | Op_B },
  194. { "mfcr", 0xfc0007fe, 0x7c000026, Op_D },
  195. { "lwarx", 0xfc0007fe, 0x7c000028, Op_D | Op_A | Op_B },
  196. { "ldx", 0xfc0007fe, 0x7c00002a, Op_D | Op_A | Op_B },
  197. { "lwzx", 0xfc0007fe, 0x7c00002e, Op_D | Op_A | Op_B },
  198. { "slw", 0xfc0007fe, 0x7c000030, Op_D | Op_A | Op_B | Op_Rc },
  199. { "cntlzw", 0xfc0007fe, 0x7c000034, Op_S | Op_A | Op_Rc },
  200. { "sld", 0xfc0007fe, 0x7c000036, Op_D | Op_A | Op_B | Op_Rc },
  201. { "and", 0xfc0007fe, 0x7c000038, Op_D | Op_A | Op_B | Op_Rc },
  202. { "cmplw", 0xfc2007fe, 0x7c000040, Op_crfD | Op_A | Op_B },
  203. { "cmpld", 0xfc2007fe, 0x7c200040, Op_crfD | Op_A | Op_B },
  204. { "subf", 0xfc0003fe, 0x7c000050, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  205. { "ldux", 0xfc0007fe, 0x7c00006a, Op_D | Op_A | Op_B },
  206. { "dcbst", 0xfc0007fe, 0x7c00006c, Op_A | Op_B },
  207. { "lwzux", 0xfc0007fe, 0x7c00006e, Op_D | Op_A | Op_B },
  208. { "cntlzd", 0xfc0007fe, 0x7c000074, Op_S | Op_A | Op_Rc },
  209. { "andc", 0xfc0007fe, 0x7c000078, Op_S | Op_A | Op_B | Op_Rc },
  210. { "td", 0xfc0007fe, 0x7c000088, Op_TO | Op_A | Op_B },
  211. { "mulhd", 0xfc0007fe, 0x7c000092, Op_D | Op_A | Op_B | Op_Rc },
  212. { "mulhw", 0xfc0007fe, 0x7c000096, Op_D | Op_A | Op_B | Op_Rc },
  213. { "mfmsr", 0xfc0007fe, 0x7c0000a6, Op_D },
  214. { "ldarx", 0xfc0007fe, 0x7c0000a8, Op_D | Op_A | Op_B },
  215. { "dcbf", 0xfc0007fe, 0x7c0000ac, Op_A | Op_B },
  216. { "lbzx", 0xfc0007fe, 0x7c0000ae, Op_D | Op_A | Op_B },
  217. { "neg", 0xfc0003fe, 0x7c0000d0, Op_D | Op_A | Op_OE | Op_Rc },
  218. { "lbzux", 0xfc0007fe, 0x7c0000ee, Op_D | Op_A | Op_B },
  219. { "nor", 0xfc0007fe, 0x7c0000f8, Op_S | Op_A | Op_B | Op_Rc },
  220. { "wrtee", 0xfc0003ff, 0x7c000106, Op_S },
  221. { "subfe", 0xfc0003fe, 0x7c000110, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  222. { "adde", 0xfc0003fe, 0x7c000114, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  223. { "mtcrf", 0xfc0007fe, 0x7c000120, Op_S | Op_CRM },
  224. { "mtmsr", 0xfc0007fe, 0x7c000124, Op_S },
  225. { "stdx", 0xfc0007fe, 0x7c00012a, Op_ST | Op_A | Op_B },
  226. { "stwcx.", 0xfc0007ff, 0x7c00012d, Op_ST | Op_A | Op_B },
  227. { "stwx", 0xfc0007fe, 0x7c00012e, Op_ST | Op_A | Op_B },
  228. { "wrteei", 0xfc0003fe, 0x7c000146, 0 }, /* XXX: out of flags! */
  229. { "stdux", 0xfc0007fe, 0x7c00016a, Op_ST | Op_A | Op_B },
  230. { "stwux", 0xfc0007fe, 0x7c00016e, Op_ST | Op_A | Op_B },
  231. { "subfze", 0xfc0003fe, 0x7c000190, Op_D | Op_A | Op_OE | Op_Rc },
  232. { "addze", 0xfc0003fe, 0x7c000194, Op_D | Op_A | Op_OE | Op_Rc },
  233. { "mtsr", 0xfc0007fe, 0x7c0001a4, Op_S | Op_SR },
  234. { "stdcx.", 0xfc0007ff, 0x7c0001ad, Op_ST | Op_A | Op_B },
  235. { "stbx", 0xfc0007fe, 0x7c0001ae, Op_ST | Op_A | Op_B },
  236. { "subfme", 0xfc0003fe, 0x7c0001d0, Op_D | Op_A | Op_OE | Op_Rc },
  237. { "mulld", 0xfc0003fe, 0x7c0001d2, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  238. { "addme", 0xfc0003fe, 0x7c0001d4, Op_D | Op_A | Op_OE | Op_Rc },
  239. { "mullw", 0xfc0003fe, 0x7c0001d6, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  240. { "mtsrin", 0xfc0007fe, 0x7c0001e4, Op_S | Op_B },
  241. { "dcbtst", 0xfc0007fe, 0x7c0001ec, Op_A | Op_B },
  242. { "stbux", 0xfc0007fe, 0x7c0001ee, Op_ST | Op_A | Op_B },
  243. { "add", 0xfc0003fe, 0x7c000214, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  244. { "dcbt", 0xfc0007fe, 0x7c00022c, Op_A | Op_B },
  245. { "lhzx", 0xfc0007ff, 0x7c00022e, Op_D | Op_A | Op_B },
  246. { "eqv", 0xfc0007fe, 0x7c000238, Op_S | Op_A | Op_B | Op_Rc },
  247. { "tlbie", 0xfc0007fe, 0x7c000264, Op_B },
  248. { "eciwx", 0xfc0007fe, 0x7c00026c, Op_D | Op_A | Op_B },
  249. { "lhzux", 0xfc0007fe, 0x7c00026e, Op_D | Op_A | Op_B },
  250. { "xor", 0xfc0007fe, 0x7c000278, Op_S | Op_A | Op_B | Op_Rc },
  251. { "mfdcr", 0xfc0007fe, 0x7c000286, Op_D | Op_dcr },
  252. { "mfspr", 0xfc0007fe, 0x7c0002a6, Op_D | Op_spr },
  253. { "lwax", 0xfc0007fe, 0x7c0002aa, Op_D | Op_A | Op_B },
  254. { "lhax", 0xfc0007fe, 0x7c0002ae, Op_D | Op_A | Op_B },
  255. { "tlbia", 0xfc0007fe, 0x7c0002e4, 0 },
  256. { "mftb", 0xfc0007fe, 0x7c0002e6, Op_D | Op_tbr },
  257. { "lwaux", 0xfc0007fe, 0x7c0002ea, Op_D | Op_A | Op_B },
  258. { "lhaux", 0xfc0007fe, 0x7c0002ee, Op_D | Op_A | Op_B },
  259. { "sthx", 0xfc0007fe, 0x7c00032e, Op_ST | Op_A | Op_B },
  260. { "orc", 0xfc0007fe, 0x7c000338, Op_S | Op_A | Op_B | Op_Rc },
  261. { "ecowx", 0xfc0007fe, 0x7c00036c, Op_ST | Op_A | Op_B | Op_Rc },
  262. { "slbie", 0xfc0007fc, 0x7c000364, Op_B },
  263. { "sthux", 0xfc0007fe, 0x7c00036e, Op_ST | Op_A | Op_B },
  264. { "or", 0xfc0007fe, 0x7c000378, Op_S | Op_A | Op_B | Op_Rc },
  265. { "mtdcr", 0xfc0007fe, 0x7c000386, Op_S | Op_dcr },
  266. { "divdu", 0xfc0003fe, 0x7c000392, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  267. { "divwu", 0xfc0003fe, 0x7c000396, Op_D | Op_A | Op_B | Op_OE | Op_Rc },
  268. { "mtspr", 0xfc0007fe, 0x7c0003a6, Op_S | Op_spr },
  269. { "dcbi", 0xfc0007fe, 0x7c0003ac, Op_A | Op_B },
  270. { "nand", 0xfc0007fe, 0x7c0003b8, Op_S | Op_A | Op_B | Op_Rc },
  271. { "dcread", 0xfc0007fe, 0x7c0003cc, Op_D | Op_A | Op_B },
  272. { "divd", 0xfc0003fe, 0x7c0003d2, Op_S | Op_A | Op_B | Op_OE | Op_Rc },
  273. { "divw", 0xfc0003fe, 0x7c0003d6, Op_S | Op_A | Op_B | Op_OE | Op_Rc },
  274. { "slbia", 0xfc0003fe, 0x7c0003e4, Op_S | Op_A | Op_B | Op_OE | Op_Rc },
  275. { "mcrxr", 0xfc0007fe, 0x7c000400, Op_crfD },
  276. { "lswx", 0xfc0007fe, 0x7c00042a, Op_D | Op_A | Op_B },
  277. { "lwbrx", 0xfc0007fe, 0x7c00042c, Op_D | Op_A | Op_B },
  278. { "lfsx", 0xfc0007fe, 0x7c00042e, Op_D | Op_A | Op_B },
  279. { "srw", 0xfc0007fe, 0x7c000430, Op_S | Op_A | Op_B | Op_Rc },
  280. { "srd", 0xfc0007fe, 0x7c000436, Op_S | Op_A | Op_B | Op_Rc },
  281. { "tlbsync", 0xfc0007fe, 0x7c00046c, 0 },
  282. { "lfsux", 0xfc0007fe, 0x7c00046e, Op_D | Op_A | Op_B },
  283. { "mfsr", 0xfc0007fe, 0x7c0004a6, Op_D | Op_SR },
  284. { "lswi", 0xfc0007fe, 0x7c0004aa, Op_D | Op_A | Op_NB },
  285. { "sync", 0xfc6007fe, 0x7c0004ac, 0 },
  286. { "lwsync", 0xfc6007fe, 0x7c2004ac, 0 },
  287. { "ptesync", 0xfc6007fe, 0x7c4004ac, 0 },
  288. { "lfdx", 0xfc0007fe, 0x7c0004ae, Op_D | Op_A | Op_B },
  289. { "lfdux", 0xfc0007fe, 0x7c0004ee, Op_D | Op_A | Op_B },
  290. { "mfsrin", 0xfc0007fe, 0x7c000526, Op_D | Op_B },
  291. { "stswx", 0xfc0007fe, 0x7c00052a, Op_ST | Op_A | Op_B },
  292. { "stwbrx", 0xfc0007fe, 0x7c00052c, Op_ST | Op_A | Op_B },
  293. { "stfsx", 0xfc0007fe, 0x7c00052e, Op_ST | Op_A | Op_B },
  294. { "stfsux", 0xfc0007fe, 0x7c00056e, Op_ST | Op_A | Op_B },
  295. { "stswi", 0xfc0007fe, 0x7c0005aa, Op_ST | Op_A | Op_NB },
  296. { "stfdx", 0xfc0007fe, 0x7c0005ae, Op_ST | Op_A | Op_B },
  297. { "stfdux", 0xfc0007fe, 0x7c0005ee, Op_ST | Op_A | Op_B },
  298. { "lhbrx", 0xfc0007fe, 0x7c00062c, Op_D | Op_A | Op_B },
  299. { "sraw", 0xfc0007fe, 0x7c000630, Op_S | Op_A | Op_B },
  300. { "srad", 0xfc0007fe, 0x7c000634, Op_S | Op_A | Op_B | Op_Rc },
  301. { "srawi", 0xfc0007fe, 0x7c000670, Op_S | Op_A | Op_rSH | Op_Rc },
  302. { "sradi", 0xfc0007fc, 0x7c000674, Op_S | Op_A | Op_sh },
  303. { "eieio", 0xfc0007fe, 0x7c0006ac, 0 },
  304. { "tlbsx", 0xfc0007fe, 0x7c000724, Op_S | Op_A | Op_B | Op_Rc },
  305. { "sthbrx", 0xfc0007fe, 0x7c00072c, Op_ST | Op_A | Op_B },
  306. { "extsh", 0xfc0007fe, 0x7c000734, Op_S | Op_A | Op_Rc },
  307. { "tlbre", 0xfc0007fe, 0x7c000764, Op_D | Op_A | Op_WS },
  308. { "extsb", 0xfc0007fe, 0x7c000774, Op_S | Op_A | Op_Rc },
  309. { "icbi", 0xfc0007fe, 0x7c0007ac, Op_A | Op_B },
  310. { "tlbwe", 0xfc0007fe, 0x7c0007a4, Op_S | Op_A | Op_WS },
  311. { "stfiwx", 0xfc0007fe, 0x7c0007ae, Op_ST | Op_A | Op_B },
  312. { "extsw", 0xfc0007fe, 0x7c0007b4, Op_S | Op_A | Op_Rc },
  313. { "dcbz", 0xfc0007fe, 0x7c0007ec, Op_A | Op_B },
  314. { "", 0x0, 0x0, 0 }
  315. };
  316. /* 3a * 4 = e8 */
  317. const struct opcode opcodes_3a[] = {
  318. { "ld", 0xfc000003, 0xe8000000, Op_D | Op_A | Op_ds },
  319. { "ldu", 0xfc000003, 0xe8000001, Op_D | Op_A | Op_ds },
  320. { "lwa", 0xfc000003, 0xe8000002, Op_D | Op_A | Op_ds },
  321. { "", 0x0, 0x0, 0 }
  322. };
  323. /* 3b * 4 = ec */
  324. const struct opcode opcodes_3b[] = {
  325. { "fdivs", 0xfc00003e, 0xec000024, Op_D | Op_A | Op_B | Op_Rc },
  326. { "fsubs", 0xfc00003e, 0xec000028, Op_D | Op_A | Op_B | Op_Rc },
  327. { "fadds", 0xfc00003e, 0xec00002a, Op_D | Op_A | Op_B | Op_Rc },
  328. { "fsqrts", 0xfc00003e, 0xec00002c, Op_D | Op_B | Op_Rc },
  329. { "fres", 0xfc00003e, 0xec000030, Op_D | Op_B | Op_Rc },
  330. { "fmuls", 0xfc00003e, 0xec000032, Op_D | Op_A | Op_C | Op_Rc },
  331. { "fmsubs", 0xfc00003e, 0xec000038, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  332. { "fmadds", 0xfc00003e, 0xec00003a, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  333. { "fnmsubs", 0xfc00003e, 0xec00003c, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  334. { "fnmadds", 0xfc00003e, 0xec00003e, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  335. { "", 0x0, 0x0, 0 }
  336. };
  337. /* 3e * 4 = f8 */
  338. const struct opcode opcodes_3e[] = {
  339. { "std", 0xfc000003, 0xf8000000, Op_ST | Op_A | Op_ds },
  340. { "stdu", 0xfc000003, 0xf8000001, Op_ST | Op_A | Op_ds },
  341. { "", 0x0, 0x0, 0 }
  342. };
  343. /* 3f * 4 = fc */
  344. const struct opcode opcodes_3f[] = {
  345. { "fcmpu", 0xfc0007fe, 0xfc000000, Op_crfD | Op_A | Op_B },
  346. { "frsp", 0xfc0007fe, 0xfc000018, Op_D | Op_B | Op_Rc },
  347. { "fctiw", 0xfc0007fe, 0xfc00001c, Op_D | Op_B | Op_Rc },
  348. { "fctiwz", 0xfc0007fe, 0xfc00001e, Op_D | Op_B | Op_Rc },
  349. { "fdiv", 0xfc00003e, 0xfc000024, Op_D | Op_A | Op_B | Op_Rc },
  350. { "fsub", 0xfc00003e, 0xfc000028, Op_D | Op_A | Op_B | Op_Rc },
  351. { "fadd", 0xfc00003e, 0xfc00002a, Op_D | Op_A | Op_B | Op_Rc },
  352. { "fsqrt", 0xfc00003e, 0xfc00002c, Op_D | Op_B | Op_Rc },
  353. { "fsel", 0xfc00003e, 0xfc00002e, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  354. { "fmul", 0xfc00003e, 0xfc000032, Op_D | Op_A | Op_C | Op_Rc },
  355. { "frsqrte", 0xfc00003e, 0xfc000034, Op_D | Op_B | Op_Rc },
  356. { "fmsub", 0xfc00003e, 0xfc000038, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  357. { "fmadd", 0xfc00003e, 0xfc00003a, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  358. { "fnmsub", 0xfc00003e, 0xfc00003c, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  359. { "fnmadd", 0xfc00003e, 0xfc00003e, Op_D | Op_A | Op_B | Op_C | Op_Rc },
  360. { "fcmpo", 0xfc0007fe, 0xfc000040, Op_crfD | Op_A | Op_B },
  361. { "mtfsb1", 0xfc0007fe, 0xfc00004c, Op_crfD | Op_Rc },
  362. { "fneg", 0xfc0007fe, 0xfc000050, Op_D | Op_B | Op_Rc },
  363. { "mcrfs", 0xfc0007fe, 0xfc000080, Op_D | Op_B | Op_Rc },
  364. { "mtfsb0", 0xfc0007fe, 0xfc00008c, Op_crfD | Op_Rc },
  365. { "fmr", 0xfc0007fe, 0xfc000090, Op_D | Op_B | Op_Rc },
  366. { "mtfsfi", 0xfc0007fe, 0xfc00010c, 0 }, /* XXX: out of flags! */
  367. { "fnabs", 0xfc0007fe, 0xfc000110, Op_D | Op_B | Op_Rc },
  368. { "fabs", 0xfc0007fe, 0xfc000210, Op_D | Op_B | Op_Rc },
  369. { "mffs", 0xfc0007fe, 0xfc00048e, Op_D | Op_B | Op_Rc },
  370. { "mtfsf", 0xfc0007fe, 0xfc00058e, Op_FM | Op_B | Op_Rc },
  371. { "fctid", 0xfc0007fe, 0xfc00065c, Op_D | Op_B | Op_Rc },
  372. { "fctidz", 0xfc0007fe, 0xfc00065e, Op_D | Op_B | Op_Rc },
  373. { "fcfid", 0xfc0007fe, 0xfc00069c, Op_D | Op_B | Op_Rc },
  374. { "", 0x0, 0x0, 0 }
  375. };
  376. struct specialreg {
  377. int reg;
  378. const char *name;
  379. };
  380. const struct specialreg sprregs[] = {
  381. { 0x000, "mq" },
  382. { 0x001, "xer" },
  383. { 0x008, "lr" },
  384. { 0x009, "ctr" },
  385. { 0x012, "dsisr" },
  386. { 0x013, "dar" },
  387. { 0x016, "dec" },
  388. { 0x019, "sdr1" },
  389. { 0x01a, "srr0" },
  390. { 0x01b, "srr1" },
  391. { 0x100, "vrsave" },
  392. { 0x110, "sprg0" },
  393. { 0x111, "sprg1" },
  394. { 0x112, "sprg2" },
  395. { 0x113, "sprg3" },
  396. { 0x114, "sprg4" },
  397. { 0x115, "sprg5" },
  398. { 0x116, "sprg6" },
  399. { 0x117, "sprg7" },
  400. { 0x118, "asr" },
  401. { 0x11a, "aer" },
  402. { 0x11c, "tbl" },
  403. { 0x11d, "tbu" },
  404. { 0x11f, "pvr" },
  405. { 0x210, "ibat0u" },
  406. { 0x211, "ibat0l" },
  407. { 0x212, "ibat1u" },
  408. { 0x213, "ibat1l" },
  409. { 0x214, "ibat2u" },
  410. { 0x215, "ibat2l" },
  411. { 0x216, "ibat3u" },
  412. { 0x217, "ibat3l" },
  413. { 0x218, "dbat0u" },
  414. { 0x219, "dbat0l" },
  415. { 0x21a, "dbat1u" },
  416. { 0x21b, "dbat1l" },
  417. { 0x21c, "dbat2u" },
  418. { 0x21d, "dbat2l" },
  419. { 0x21e, "dbat3u" },
  420. { 0x21f, "dbat3l" },
  421. { 0x230, "ibat4u" },
  422. { 0x231, "ibat4l" },
  423. { 0x232, "ibat5u" },
  424. { 0x233, "ibat5l" },
  425. { 0x234, "ibat6u" },
  426. { 0x235, "ibat6l" },
  427. { 0x236, "ibat7u" },
  428. { 0x237, "ibat7l" },
  429. { 0x238, "dbat4u" },
  430. { 0x239, "dbat4l" },
  431. { 0x23a, "dbat5u" },
  432. { 0x23b, "dbat5l" },
  433. { 0x23c, "dbat6u" },
  434. { 0x23d, "dbat6l" },
  435. { 0x23e, "dbat7u" },
  436. { 0x23f, "dbat7l" },
  437. { 0x3b0, "zpr" },
  438. { 0x3b1, "pid" },
  439. { 0x3b3, "ccr0" },
  440. { 0x3b4, "iac3" },
  441. { 0x3b5, "iac4" },
  442. { 0x3b6, "dvc1" },
  443. { 0x3b7, "dvc2" },
  444. { 0x3b9, "sgr" },
  445. { 0x3ba, "dcwr" },
  446. { 0x3bb, "sler" },
  447. { 0x3bc, "su0r" },
  448. { 0x3bd, "dbcr1" },
  449. { 0x3d3, "icdbdr" },
  450. { 0x3d4, "esr" },
  451. { 0x3d5, "dear" },
  452. { 0x3d6, "evpr" },
  453. { 0x3d8, "tsr" },
  454. { 0x3da, "tcr" },
  455. { 0x3db, "pit" },
  456. { 0x3de, "srr2" },
  457. { 0x3df, "srr3" },
  458. { 0x3f0, "hid0" },
  459. { 0x3f1, "hid1" },
  460. { 0x3f2, "iabr" },
  461. { 0x3f3, "hid2" },
  462. { 0x3f5, "dabr" },
  463. { 0x3f6, "msscr0" },
  464. { 0x3f7, "msscr1" },
  465. { 0x3f9, "l2cr" },
  466. { 0x3fa, "dccr" },
  467. { 0x3fb, "iccr" },
  468. { 0x3ff, "pir" },
  469. { 0, NULL }
  470. };
  471. const struct specialreg dcrregs[] = {
  472. { 0x010, "sdram0_cfgaddr" },
  473. { 0x011, "sdram0_cfgdata" },
  474. { 0x012, "ebc0_cfgaddr" },
  475. { 0x013, "ebc0_cfgdata" },
  476. { 0x014, "dcp0_cfgaddr" },
  477. { 0x015, "dcp0_cfgdata" },
  478. { 0x018, "ocm0_isarc" },
  479. { 0x019, "ocm0_iscntl" },
  480. { 0x01a, "ocm0_dsarc" },
  481. { 0x01b, "ocm0_dscntl" },
  482. { 0x084, "plb0_besr" },
  483. { 0x086, "plb0_bear" },
  484. { 0x087, "plb0_acr" },
  485. { 0x0a0, "pob0_besr0" },
  486. { 0x0a2, "pob0_bear" },
  487. { 0x0a4, "pob0_besr1" },
  488. { 0x0b0, "cpc0_pllmr" },
  489. { 0x0b1, "cpc0_cr0" },
  490. { 0x0b2, "cpc0_cr1" },
  491. { 0x0b4, "cpc0_psr" },
  492. { 0x0b5, "cpc0_jtagid" },
  493. { 0x0b8, "cpc0_sr" },
  494. { 0x0b9, "cpc0_er" },
  495. { 0x0ba, "cpc0_fr" },
  496. { 0x0c0, "uic0_sr" },
  497. { 0x0c2, "uic0_er" },
  498. { 0x0c3, "uic0_cr" },
  499. { 0x0c4, "uic0_pr" },
  500. { 0x0c5, "uic0_tr" },
  501. { 0x0c6, "uic0_msr" },
  502. { 0x0c7, "uic0_vr" },
  503. { 0x0c8, "uic0_vcr" },
  504. { 0x100, "dma0_cr0" },
  505. { 0x101, "dma0_ct0" },
  506. { 0x102, "dma0_da0" },
  507. { 0x103, "dma0_sa0" },
  508. { 0x104, "dma0_sg0" },
  509. { 0x108, "dma0_cr1" },
  510. { 0x109, "dma0_ct1" },
  511. { 0x10a, "dma0_da1" },
  512. { 0x10b, "dma0_sa1" },
  513. { 0x10c, "dma0_sg1" },
  514. { 0x110, "dma0_cr2" },
  515. { 0x111, "dma0_ct2" },
  516. { 0x112, "dma0_da2" },
  517. { 0x113, "dma0_sa2" },
  518. { 0x114, "dma0_sg2" },
  519. { 0x118, "dma0_cr3" },
  520. { 0x119, "dma0_ct3" },
  521. { 0x11a, "dma0_da3" },
  522. { 0x11b, "dma0_sa3" },
  523. { 0x11c, "dma0_sg3" },
  524. { 0x120, "dma0_sr" },
  525. { 0x123, "dma0_sgc" },
  526. { 0x125, "dma0_slp" },
  527. { 0x126, "dma0_pol" },
  528. { 0x180, "mal0_cfg" },
  529. { 0x181, "mal0_esr" },
  530. { 0x182, "mal0_ier" },
  531. { 0x184, "mal0_txcasr" },
  532. { 0x185, "mal0_txcarr" },
  533. { 0x186, "mal0_txeobisr" },
  534. { 0x187, "mal0_txdeir" },
  535. { 0x190, "mal0_rxcasr" },
  536. { 0x191, "mal0_rxcarr" },
  537. { 0x192, "mal0_rxeobisr" },
  538. { 0x193, "mal0_rxdeir" },
  539. { 0x1a0, "mal0_txctp0r" },
  540. { 0x1a1, "mal0_txctp1r" },
  541. { 0x1a2, "mal0_txctp2r" },
  542. { 0x1a3, "mal0_txctp3r" },
  543. { 0x1c0, "mal0_rxctp0r" },
  544. { 0x1e0, "mal0_rcbs0" },
  545. { 0, NULL }
  546. };
  547. static const char *condstr[8] = {
  548. "ge", "le", "ne", "ns", "lt", "gt", "eq", "so"
  549. };
  550. void
  551. op_ill(instr_t instr, vm_offset_t loc)
  552. {
  553. db_printf("illegal instruction %x\n", instr);
  554. }
  555. u_int32_t
  556. extract_field(u_int32_t value, u_int32_t base, u_int32_t width)
  557. {
  558. u_int32_t mask = (1 << width) - 1;
  559. return ((value >> base) & mask);
  560. }
  561. const struct opcode * search_op(const struct opcode *);
  562. void
  563. disasm_fields(const struct opcode *popcode, instr_t instr, vm_offset_t loc,
  564. char *disasm_str, size_t slen)
  565. {
  566. char * pstr;
  567. enum function_mask func;
  568. int len;
  569. #define ADD_LEN(s) do { \
  570. len = (s); \
  571. slen -= len; \
  572. pstr += len; \
  573. } while(0)
  574. #define APP_PSTR(fmt, arg) ADD_LEN(snprintf(pstr, slen, (fmt), (arg)))
  575. #define APP_PSTRS(fmt) ADD_LEN(snprintf(pstr, slen, "%s", (fmt)))
  576. pstr = disasm_str;
  577. func = popcode->func;
  578. if (func & Op_BC) {
  579. u_int BO, BI;
  580. BO = extract_field(instr, 31 - 10, 5);
  581. BI = extract_field(instr, 31 - 15, 5);
  582. func &= ~Op_BC;
  583. if (BO & 4) {
  584. /* standard, no decrement */
  585. if (BO & 16) {
  586. if (popcode->code == 0x40000000) {
  587. APP_PSTRS("c");
  588. func |= Op_BO | Op_BI;
  589. }
  590. }
  591. else {
  592. APP_PSTRS(condstr[((BO & 8) >> 1) + (BI & 3)]);
  593. if (BI >= 4)
  594. func |= Op_crfS;
  595. }
  596. }
  597. else {
  598. /* decrement and branch */
  599. if (BO & 2)
  600. APP_PSTRS("dz");
  601. else
  602. APP_PSTRS("dnz");
  603. if ((BO & 24) == 0)
  604. APP_PSTRS("f");
  605. else if ((BO & 24) == 8)
  606. APP_PSTRS("t");
  607. else
  608. func |= Op_BI;
  609. }
  610. if (popcode->code == 0x4c000020)
  611. APP_PSTRS("lr");
  612. else if (popcode->code == 0x4c000420)
  613. APP_PSTRS("ctr");
  614. if ((BO & 20) != 20 && (func & Op_BO) == 0)
  615. func |= Op_BP; /* branch prediction hint */
  616. }
  617. if (func & Op_OE) {
  618. u_int OE;
  619. OE = extract_field(instr, 31 - 21, 1);
  620. if (OE) {
  621. APP_PSTRS("o");
  622. }
  623. func &= ~Op_OE;
  624. }
  625. switch (func & Op_LKM) {
  626. case Op_Rc:
  627. if (instr & 0x1)
  628. APP_PSTRS(".");
  629. break;
  630. case Op_AA:
  631. if (instr & 0x1)
  632. APP_PSTRS("l");
  633. if (instr & 0x2) {
  634. APP_PSTRS("a");
  635. loc = 0; /* Absolute address */
  636. }
  637. break;
  638. case Op_LK:
  639. if (instr & 0x1)
  640. APP_PSTRS("l");
  641. break;
  642. default:
  643. func &= ~Op_LKM;
  644. }
  645. if (func & Op_BP) {
  646. int y;
  647. y = (instr & 0x200000) != 0;
  648. if (popcode->code == 0x40000000) {
  649. int BD;
  650. BD = extract_field(instr, 31 - 29, 14);
  651. BD = BD << 18;
  652. BD = BD >> 16;
  653. BD += loc;
  654. if ((vm_offset_t)BD < loc)
  655. y ^= 1;
  656. }
  657. APP_PSTR("%c", y ? '+' : '-');
  658. func &= ~Op_BP;
  659. }
  660. APP_PSTRS("\t");
  661. /* XXX: special cases here, out of flags in a 32bit word. */
  662. if (strcmp(popcode->name, "wrteei") == 0) {
  663. int E;
  664. E = extract_field(instr, 31 - 16, 5);
  665. APP_PSTR("%d", E);
  666. return;
  667. }
  668. else if (strcmp(popcode->name, "mtfsfi") == 0) {
  669. u_int UI;
  670. UI = extract_field(instr, 31 - 8, 3);
  671. APP_PSTR("crf%u, ", UI);
  672. UI = extract_field(instr, 31 - 19, 4);
  673. APP_PSTR("0x%x", UI);
  674. }
  675. /* XXX: end of special cases here. */
  676. if ((func & Op_FM) == Op_FM) {
  677. u_int FM;
  678. FM = extract_field(instr, 31 - 14, 8);
  679. APP_PSTR("0x%x, ", FM);
  680. func &= ~Op_FM;
  681. }
  682. if (func & Op_D) { /* Op_ST is the same */
  683. u_int D;
  684. D = extract_field(instr, 31 - 10, 5);
  685. APP_PSTR("r%d, ", D);
  686. func &= ~Op_D;
  687. }
  688. if (func & Op_crbD) {
  689. u_int crbD;
  690. crbD = extract_field(instr, 31 - 10, 5);
  691. APP_PSTR("crb%d, ", crbD);
  692. func &= ~Op_crbD;
  693. }
  694. if (func & Op_crfD) {
  695. u_int crfD;
  696. crfD = extract_field(instr, 31 - 8, 3);
  697. APP_PSTR("crf%d, ", crfD);
  698. func &= ~Op_crfD;
  699. }
  700. if (func & Op_TO) {
  701. u_int TO;
  702. TO = extract_field(instr, 31 - 10, 1);
  703. APP_PSTR("%d, ", TO);
  704. func &= ~Op_TO;
  705. }
  706. if (func & Op_crfS) {
  707. u_int crfS;
  708. crfS = extract_field(instr, 31 - 13, 3);
  709. APP_PSTR("crf%d, ", crfS);
  710. func &= ~Op_crfS;
  711. }
  712. if (func & Op_CRM) {
  713. u_int CRM;
  714. CRM = extract_field(instr, 31 - 19, 8);
  715. APP_PSTR("0x%x, ", CRM);
  716. func &= ~Op_CRM;
  717. }
  718. if (func & Op_BO) {
  719. u_int BO;
  720. BO = extract_field(instr, 31 - 10, 5);
  721. APP_PSTR("%d, ", BO);
  722. func &= ~Op_BO;
  723. }
  724. if (func & Op_BI) {
  725. u_int BI;
  726. BI = extract_field(instr, 31 - 15, 5);
  727. APP_PSTR("%d, ", BI);
  728. func &= ~Op_BI;
  729. }
  730. if (func & Op_dA) { /* register A indirect with displacement */
  731. u_int A;
  732. A = extract_field(instr, 31 - 31, 16);
  733. if (A & 0x8000) {
  734. APP_PSTRS("-");
  735. A = 0x10000-A;
  736. }
  737. APP_PSTR("0x%x", A);
  738. A = extract_field(instr, 31 - 15, 5);
  739. APP_PSTR("(r%d)", A);
  740. func &= ~Op_dA;
  741. }
  742. if (func & Op_spr) {
  743. u_int spr;
  744. u_int sprl;
  745. u_int sprh;
  746. const struct specialreg *regs;
  747. int i;
  748. sprl = extract_field(instr, 31 - 15, 5);
  749. sprh = extract_field(instr, 31 - 20, 5);
  750. spr = sprh << 5 | sprl;
  751. /* ugly hack - out of bitfields in the function mask */
  752. if (popcode->name[2] == 'd') /* m.Dcr */
  753. regs = dcrregs;
  754. else
  755. regs = sprregs;
  756. for (i = 0; regs[i].name != NULL; i++)
  757. if (spr == regs[i].reg)
  758. break;
  759. if (regs[i].name == NULL)
  760. APP_PSTR("[unknown special reg (%d)]", spr);
  761. else
  762. APP_PSTR("%s", regs[i].name);
  763. if (popcode->name[1] == 't') /* spr is destination */
  764. APP_PSTRS(", ");
  765. func &= ~Op_spr;
  766. }
  767. if (func & Op_SR) {
  768. u_int SR;
  769. SR = extract_field(instr, 31 - 15, 3);
  770. APP_PSTR("sr%d", SR);
  771. if (popcode->name[1] == 't') /* SR is destination */
  772. APP_PSTRS(", ");
  773. func &= ~Op_SR;
  774. }
  775. if (func & Op_A) {
  776. u_int A;
  777. A = extract_field(instr, 31 - 15, 5);
  778. APP_PSTR("r%d, ", A);
  779. func &= ~Op_A;
  780. }
  781. if (func & Op_S) {
  782. u_int D;
  783. D = extract_field(instr, 31 - 10, 5);
  784. APP_PSTR("r%d, ", D);
  785. func &= ~Op_S;
  786. }
  787. if (func & Op_C) {
  788. u_int C;
  789. C = extract_field(instr, 31 - 25, 5);
  790. APP_PSTR("r%d, ", C);
  791. func &= ~Op_C;
  792. }
  793. if (func & Op_B) {
  794. u_int B;
  795. B = extract_field(instr, 31 - 20, 5);
  796. APP_PSTR("r%d", B);
  797. func &= ~Op_B;
  798. }
  799. if (func & Op_crbA) {
  800. u_int crbA;
  801. crbA = extract_field(instr, 31 - 15, 5);
  802. APP_PSTR("%d, ", crbA);
  803. func &= ~Op_crbA;
  804. }
  805. if (func & Op_crbB) {
  806. u_int crbB;
  807. crbB = extract_field(instr, 31 - 20, 5);
  808. APP_PSTR("%d, ", crbB);
  809. func &= ~Op_crbB;
  810. }
  811. if (func & Op_LI) {
  812. int LI;
  813. LI = extract_field(instr, 31 - 29, 24);
  814. LI = LI << 8;
  815. LI = LI >> 6;
  816. LI += loc;
  817. APP_PSTR("0x%x", LI);
  818. func &= ~Op_LI;
  819. }
  820. switch (func & Op_SIMM) {
  821. u_int IMM;
  822. case Op_SIMM: /* same as Op_d */
  823. IMM = extract_field(instr, 31 - 31, 16);
  824. if (IMM & 0x8000) {
  825. APP_PSTRS("-");
  826. IMM = 0x10000-IMM;
  827. }
  828. func &= ~Op_SIMM;
  829. goto common;
  830. case Op_UIMM:
  831. IMM = extract_field(instr, 31 - 31, 16);
  832. func &= ~Op_UIMM;
  833. goto common;
  834. common:
  835. APP_PSTR("0x%x", IMM);
  836. break;
  837. default:
  838. ;
  839. }
  840. if (func & Op_BD) {
  841. int BD;
  842. BD = extract_field(instr, 31 - 29, 14);
  843. BD = BD << 18;
  844. BD = BD >> 16;
  845. BD += loc;
  846. /* Need to sign extend and shift up 2, then add addr */
  847. APP_PSTR("0x%x", BD);
  848. func &= ~Op_BD;
  849. }
  850. if (func & Op_ds) {
  851. u_int ds;
  852. ds = extract_field(instr, 31 - 29, 14) << 2;
  853. APP_PSTR("0x%x", ds);
  854. func &= ~Op_ds;
  855. }
  856. if (func & Op_me) {
  857. u_int me, mel, meh;
  858. mel = extract_field(instr, 31 - 25, 4);
  859. meh = extract_field(instr, 31 - 26, 1);
  860. me = meh << 4 | mel;
  861. APP_PSTR(", 0x%x", me);
  862. func &= ~Op_me;
  863. }
  864. if ((func & Op_SH) && (func & Op_sh_mb_sh)) {
  865. u_int SH;
  866. SH = extract_field(instr, 31 - 20, 5);
  867. APP_PSTR("%d", SH);
  868. }
  869. if ((func & Op_MB) && (func & Op_sh_mb_sh)) {
  870. u_int MB;
  871. u_int ME;
  872. MB = extract_field(instr, 31 - 25, 5);
  873. APP_PSTR(", %d", MB);
  874. ME = extract_field(instr, 31 - 30, 5);
  875. APP_PSTR(", %d", ME);
  876. }
  877. if ((func & Op_sh) && ! (func & Op_sh_mb_sh)) {
  878. u_int sh, shl, shh;
  879. shl = extract_field(instr, 31 - 19, 4);
  880. shh = extract_field(instr, 31 - 20, 1);
  881. sh = shh << 4 | shl;
  882. APP_PSTR(", %d", sh);
  883. }
  884. if ((func & Op_mb) && ! (func & Op_sh_mb_sh)) {
  885. u_int mb, mbl, mbh;
  886. mbl = extract_field(instr, 31 - 25, 4);
  887. mbh = extract_field(instr, 31 - 26, 1);
  888. mb = mbh << 4 | mbl;
  889. APP_PSTR(", %d", mb);
  890. }
  891. if ((func & Op_me) && ! (func & Op_sh_mb_sh)) {
  892. u_int me, mel, meh;
  893. mel = extract_field(instr, 31 - 25, 4);
  894. meh = extract_field(instr, 31 - 26, 1);
  895. me = meh << 4 | mel;
  896. APP_PSTR(", %d", me);
  897. }
  898. if (func & Op_tbr) {
  899. u_int tbr;
  900. u_int tbrl;
  901. u_int tbrh;
  902. const char *reg;
  903. tbrl = extract_field(instr, 31 - 15, 5);
  904. tbrh = extract_field(instr, 31 - 20, 5);
  905. tbr = tbrh << 5 | tbrl;
  906. switch (tbr) {
  907. case 268:
  908. reg = "tbl";
  909. break;
  910. case 269:
  911. reg = "tbu";
  912. break;
  913. default:
  914. reg = NULL;
  915. }
  916. if (reg == NULL)
  917. APP_PSTR(", [unknown tbr %d ]", tbr);
  918. else
  919. APP_PSTR(", %s", reg);
  920. func &= ~Op_tbr;
  921. }
  922. if (func & Op_NB) {
  923. u_int NB;
  924. NB = extract_field(instr, 31 - 20, 5);
  925. if (NB == 0)
  926. NB = 32;
  927. APP_PSTR(", %d", NB);
  928. func &= ~Op_SR;
  929. }
  930. #undef ADD_LEN
  931. #undef APP_PSTR
  932. #undef APP_PSTRS
  933. }
  934. void
  935. op_base(instr_t instr, vm_offset_t loc)
  936. {
  937. dis_ppc(opcodes, instr, loc);
  938. }
  939. void
  940. op_cl_x13(instr_t instr, vm_offset_t loc)
  941. {
  942. dis_ppc(opcodes_13, instr, loc);
  943. }
  944. void
  945. op_cl_x1e(instr_t instr, vm_offset_t loc)
  946. {
  947. dis_ppc(opcodes_1e, instr, loc);
  948. }
  949. void
  950. op_cl_x1f(instr_t instr, vm_offset_t loc)
  951. {
  952. dis_ppc(opcodes_1f, instr, loc);
  953. }
  954. void
  955. op_cl_x3a(instr_t instr, vm_offset_t loc)
  956. {
  957. dis_ppc(opcodes_3a, instr, loc);
  958. }
  959. void
  960. op_cl_x3b(instr_t instr, vm_offset_t loc)
  961. {
  962. dis_ppc(opcodes_3b, instr, loc);
  963. }
  964. void
  965. op_cl_x3e(instr_t instr, vm_offset_t loc)
  966. {
  967. dis_ppc(opcodes_3e, instr, loc);
  968. }
  969. void
  970. op_cl_x3f(instr_t instr, vm_offset_t loc)
  971. {
  972. dis_ppc(opcodes_3f, instr, loc);
  973. }
  974. void
  975. dis_ppc(const struct opcode *opcodeset, instr_t instr, vm_offset_t loc)
  976. {
  977. const struct opcode *op;
  978. int found = 0;
  979. int i;
  980. char disasm_str[80];
  981. for (i = 0, op = &opcodeset[0];
  982. found == 0 && op->mask != 0;
  983. i++, op = &opcodeset[i]) {
  984. if ((instr & op->mask) == op->code) {
  985. found = 1;
  986. disasm_fields(op, instr, loc, disasm_str,
  987. sizeof disasm_str);
  988. db_printf("%s%s\n", op->name, disasm_str);
  989. return;
  990. }
  991. }
  992. op_ill(instr, loc);
  993. }
  994. db_addr_t
  995. db_disasm(db_addr_t loc, bool extended)
  996. {
  997. int class;
  998. instr_t opcode;
  999. opcode = *(instr_t *)(loc);
  1000. if (extended)
  1001. db_printf("|%08x| ", opcode);
  1002. class = opcode >> 26;
  1003. (opcodes_base[class])(opcode, loc);
  1004. return (loc + 4);
  1005. }
  1006. vm_offset_t opc_disasm(vm_offset_t loc, int);
  1007. vm_offset_t
  1008. opc_disasm(vm_offset_t loc, int xin)
  1009. {
  1010. int class;
  1011. instr_t opcode;
  1012. opcode = xin;
  1013. class = opcode >> 26;
  1014. (opcodes_base[class])(opcode, loc);
  1015. return (loc + 4);
  1016. }