qdb.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <mach.h>
  5. /*
  6. * PowerPC-specific debugger interface,
  7. * including 64-bit modes
  8. * forsyth@terzarima.net
  9. */
  10. static char *powerexcep(Map*, Rgetter);
  11. static int powerfoll(Map*, uvlong, Rgetter, uvlong*);
  12. static int powerinst(Map*, uvlong, char, char*, int);
  13. static int powerinstlen(Map*, uvlong);
  14. static int powerdas(Map*, uvlong, char*, int);
  15. /*
  16. * Machine description
  17. */
  18. Machdata powermach =
  19. {
  20. {0x02, 0x8f, 0xff, 0xff}, /* break point */ /* BUG */
  21. 4, /* break point size */
  22. beswab, /* short to local byte order */
  23. beswal, /* long to local byte order */
  24. beswav, /* vlong to local byte order */
  25. risctrace, /* print C traceback */
  26. riscframe, /* frame finder */
  27. powerexcep, /* print exception */
  28. 0, /* breakpoint fixup */
  29. beieeesftos, /* single precision float printer */
  30. beieeedftos, /* double precisioin float printer */
  31. powerfoll, /* following addresses */
  32. powerinst, /* print instruction */
  33. powerdas, /* dissembler */
  34. powerinstlen, /* instruction size */
  35. };
  36. static char *excname[] =
  37. {
  38. "reserved 0",
  39. "system reset",
  40. "machine check",
  41. "data access",
  42. "instruction access",
  43. "external interrupt",
  44. "alignment",
  45. "program exception",
  46. "floating-point unavailable",
  47. "decrementer",
  48. "i/o controller interface error",
  49. "reserved B",
  50. "system call",
  51. "trace trap",
  52. "floating point assist",
  53. "reserved",
  54. "ITLB miss",
  55. "DTLB load miss",
  56. "DTLB store miss",
  57. "instruction address breakpoint"
  58. "SMI interrupt"
  59. "reserved 15",
  60. "reserved 16",
  61. "reserved 17",
  62. "reserved 18",
  63. "reserved 19",
  64. "reserved 1A",
  65. /* the following are made up on a program exception */
  66. "floating point exception", /* FPEXC */
  67. "illegal instruction",
  68. "privileged instruction",
  69. "trap",
  70. "illegal operation",
  71. };
  72. static char*
  73. powerexcep(Map *map, Rgetter rget)
  74. {
  75. long c;
  76. static char buf[32];
  77. c = (*rget)(map, "CAUSE") >> 8;
  78. if(c < nelem(excname))
  79. return excname[c];
  80. sprint(buf, "unknown trap #%lx", c);
  81. return buf;
  82. }
  83. /*
  84. * disassemble PowerPC opcodes
  85. */
  86. #define REGSP 1 /* should come from q.out.h, but there's a clash */
  87. #define REGSB 2
  88. static char FRAMENAME[] = ".frame";
  89. static Map *mymap;
  90. /*
  91. * ibm conventions for these: bit 0 is top bit
  92. * from table 10-1
  93. */
  94. typedef struct {
  95. uchar aa; /* bit 30 */
  96. uchar crba; /* bits 11-15 */
  97. uchar crbb; /* bits 16-20 */
  98. long bd; /* bits 16-29 */
  99. uchar crfd; /* bits 6-8 */
  100. uchar crfs; /* bits 11-13 */
  101. uchar bi; /* bits 11-15 */
  102. uchar bo; /* bits 6-10 */
  103. uchar crbd; /* bits 6-10 */
  104. union {
  105. short d; /* bits 16-31 */
  106. short simm;
  107. ushort uimm;
  108. };
  109. uchar fm; /* bits 7-14 */
  110. uchar fra; /* bits 11-15 */
  111. uchar frb; /* bits 16-20 */
  112. uchar frc; /* bits 21-25 */
  113. uchar frs; /* bits 6-10 */
  114. uchar frd; /* bits 6-10 */
  115. uchar crm; /* bits 12-19 */
  116. long li; /* bits 6-29 || b'00' */
  117. uchar lk; /* bit 31 */
  118. uchar mb; /* bits 21-25 */
  119. uchar me; /* bits 26-30 */
  120. uchar xmbe; /* bits 26,21-25: mb[5] || mb[0:4], also xme */
  121. uchar xsh; /* bits 30,16-20: sh[5] || sh[0:4] */
  122. uchar nb; /* bits 16-20 */
  123. uchar op; /* bits 0-5 */
  124. uchar oe; /* bit 21 */
  125. uchar ra; /* bits 11-15 */
  126. uchar rb; /* bits 16-20 */
  127. uchar rc; /* bit 31 */
  128. union {
  129. uchar rs; /* bits 6-10 */
  130. uchar rd;
  131. };
  132. uchar sh; /* bits 16-20 */
  133. ushort spr; /* bits 11-20 */
  134. uchar to; /* bits 6-10 */
  135. uchar imm; /* bits 16-19 */
  136. ushort xo; /* bits 21-30, 22-30, 26-30, or 30 (beware) */
  137. uvlong imm64;
  138. long w0;
  139. long w1;
  140. uvlong addr; /* pc of instruction */
  141. short target;
  142. short m64; /* 64-bit mode */
  143. char *curr; /* current fill level in output buffer */
  144. char *end; /* end of buffer */
  145. int size; /* number of longs in instr */
  146. char *err; /* errmsg */
  147. } Instr;
  148. #define IBF(v,a,b) (((ulong)(v)>>(32-(b)-1)) & ~(~0L<<(((b)-(a)+1))))
  149. #define IB(v,b) IBF((v),(b),(b))
  150. #pragma varargck argpos bprint 2
  151. static void
  152. bprint(Instr *i, char *fmt, ...)
  153. {
  154. va_list arg;
  155. va_start(arg, fmt);
  156. i->curr = vseprint(i->curr, i->end, fmt, arg);
  157. va_end(arg);
  158. }
  159. static int
  160. decode(uvlong pc, Instr *i)
  161. {
  162. ulong w;
  163. if (get4(mymap, pc, &w) < 0) {
  164. werrstr("can't read instruction: %r");
  165. return -1;
  166. }
  167. i->m64 = asstype == APOWER64;
  168. i->aa = IB(w, 30);
  169. i->crba = IBF(w, 11, 15);
  170. i->crbb = IBF(w, 16, 20);
  171. i->bd = IBF(w, 16, 29)<<2;
  172. if(i->bd & 0x8000)
  173. i->bd |= ~0L<<16;
  174. i->crfd = IBF(w, 6, 8);
  175. i->crfs = IBF(w, 11, 13);
  176. i->bi = IBF(w, 11, 15);
  177. i->bo = IBF(w, 6, 10);
  178. i->crbd = IBF(w, 6, 10);
  179. i->uimm = IBF(w, 16, 31); /* also d, simm */
  180. i->fm = IBF(w, 7, 14);
  181. i->fra = IBF(w, 11, 15);
  182. i->frb = IBF(w, 16, 20);
  183. i->frc = IBF(w, 21, 25);
  184. i->frs = IBF(w, 6, 10);
  185. i->frd = IBF(w, 6, 10);
  186. i->crm = IBF(w, 12, 19);
  187. i->li = IBF(w, 6, 29)<<2;
  188. if(IB(w, 6))
  189. i->li |= ~0<<25;
  190. i->lk = IB(w, 31);
  191. i->mb = IBF(w, 21, 25);
  192. i->me = IBF(w, 26, 30);
  193. i->xmbe = (IB(w,26)<<5) | i->mb;
  194. i->nb = IBF(w, 16, 20);
  195. i->op = IBF(w, 0, 5);
  196. i->oe = IB(w, 21);
  197. i->ra = IBF(w, 11, 15);
  198. i->rb = IBF(w, 16, 20);
  199. i->rc = IB(w, 31);
  200. i->rs = IBF(w, 6, 10); /* also rd */
  201. i->sh = IBF(w, 16, 20);
  202. i->xsh = (IB(w, 30)<<5) | i->sh;
  203. i->spr = IBF(w, 11, 20);
  204. i->to = IBF(w, 6, 10);
  205. i->imm = IBF(w, 16, 19);
  206. i->xo = IBF(w, 21, 30); /* bits 21-30, 22-30, 26-30, or 30 (beware) */
  207. if(i->op == 58){ /* class of 64-bit loads */
  208. i->xo = i->simm & 3;
  209. i->simm &= ~3;
  210. }
  211. i->imm64 = i->simm;
  212. if(i->op == 15)
  213. i->imm64 <<= 16;
  214. else if(i->op == 25 || i->op == 27 || i->op == 29)
  215. i->imm64 = (uvlong)(i->uimm<<16);
  216. i->w0 = w;
  217. i->target = -1;
  218. i->addr = pc;
  219. i->size = 1;
  220. return 1;
  221. }
  222. static int
  223. mkinstr(uvlong pc, Instr *i)
  224. {
  225. Instr x;
  226. if(decode(pc, i) < 0)
  227. return -1;
  228. /*
  229. * combine ADDIS/ORI (CAU/ORIL) into MOVW
  230. * also ORIS/ORIL for unsigned in 64-bit mode
  231. */
  232. if ((i->op == 15 || i->op == 25) && i->ra==0) {
  233. if(decode(pc+4, &x) < 0)
  234. return -1;
  235. if (x.op == 24 && x.rs == x.ra && x.ra == i->rd) {
  236. i->imm64 |= (x.imm64 & 0xFFFF);
  237. if(i->op != 15)
  238. i->imm64 &= 0xFFFFFFFFUL;
  239. i->w1 = x.w0;
  240. i->target = x.rd;
  241. i->size++;
  242. return 1;
  243. }
  244. }
  245. return 1;
  246. }
  247. static int
  248. plocal(Instr *i)
  249. {
  250. long offset;
  251. Symbol s;
  252. if (!findsym(i->addr, CTEXT, &s) || !findlocal(&s, FRAMENAME, &s))
  253. return -1;
  254. offset = s.value - i->imm64;
  255. if (offset > 0) {
  256. if(getauto(&s, offset, CAUTO, &s)) {
  257. bprint(i, "%s+%lld(SP)", s.name, s.value);
  258. return 1;
  259. }
  260. } else {
  261. if (getauto(&s, -offset-4, CPARAM, &s)) {
  262. bprint(i, "%s+%ld(FP)", s.name, -offset);
  263. return 1;
  264. }
  265. }
  266. return -1;
  267. }
  268. static int
  269. pglobal(Instr *i, uvlong off, int anyoff, char *reg)
  270. {
  271. Symbol s, s2;
  272. uvlong off1;
  273. if(findsym(off, CANY, &s) &&
  274. s.value-off < 4096 &&
  275. (s.class == CDATA || s.class == CTEXT)) {
  276. if(off==s.value && s.name[0]=='$'){
  277. off1 = 0;
  278. geta(mymap, s.value, &off1);
  279. if(off1 && findsym(off1, CANY, &s2) && s2.value == off1){
  280. bprint(i, "$%s%s", s2.name, reg);
  281. return 1;
  282. }
  283. }
  284. bprint(i, "%s", s.name);
  285. if (s.value != off)
  286. bprint(i, "+%llux", off-s.value);
  287. bprint(i, reg);
  288. return 1;
  289. }
  290. if(!anyoff)
  291. return 0;
  292. bprint(i, "%llux%s", off, reg);
  293. return 1;
  294. }
  295. static void
  296. address(Instr *i)
  297. {
  298. if (i->ra == REGSP && plocal(i) >= 0)
  299. return;
  300. if (i->ra == REGSB && mach->sb && pglobal(i, mach->sb+i->imm64, 0, "(SB)") >= 0)
  301. return;
  302. if(i->simm < 0)
  303. bprint(i, "-%x(R%d)", -i->simm, i->ra);
  304. else
  305. bprint(i, "%llux(R%d)", i->imm64, i->ra);
  306. }
  307. static char *tcrbits[] = {"LT", "GT", "EQ", "VS"};
  308. static char *fcrbits[] = {"GE", "LE", "NE", "VC"};
  309. typedef struct Opcode Opcode;
  310. struct Opcode {
  311. uchar op;
  312. ushort xo;
  313. ushort xomask;
  314. char *mnemonic;
  315. void (*f)(Opcode *, Instr *);
  316. char *ken;
  317. int flags;
  318. };
  319. static void format(char *, Instr *, char *);
  320. static void
  321. branch(Opcode *o, Instr *i)
  322. {
  323. char buf[8];
  324. int bo, bi;
  325. bo = i->bo & ~1; /* ignore prediction bit */
  326. if(bo==4 || bo==12 || bo==20) { /* simple forms */
  327. if(bo != 20) {
  328. bi = i->bi&3;
  329. sprint(buf, "B%s%%L", bo==12? tcrbits[bi]: fcrbits[bi]);
  330. format(buf, i, 0);
  331. bprint(i, "\t");
  332. if(i->bi > 4)
  333. bprint(i, "CR(%d),", i->bi/4);
  334. } else
  335. format("BR%L\t", i, 0);
  336. if(i->op == 16)
  337. format(0, i, "%J");
  338. else if(i->op == 19 && i->xo == 528)
  339. format(0, i, "(CTR)");
  340. else if(i->op == 19 && i->xo == 16)
  341. format(0, i, "(LR)");
  342. } else
  343. format(o->mnemonic, i, o->ken);
  344. }
  345. static void
  346. addi(Opcode *o, Instr *i)
  347. {
  348. if (i->op==14 && i->ra == 0)
  349. format("MOVW", i, "%i,R%d");
  350. else if (i->ra == REGSB) {
  351. bprint(i, "MOVW\t$");
  352. address(i);
  353. bprint(i, ",R%d", i->rd);
  354. } else if(i->op==14 && i->simm < 0) {
  355. bprint(i, "SUB\t$%d,R%d", -i->simm, i->ra);
  356. if(i->rd != i->ra)
  357. bprint(i, ",R%d", i->rd);
  358. } else if(i->ra == i->rd) {
  359. format(o->mnemonic, i, "%i");
  360. bprint(i, ",R%d", i->rd);
  361. } else
  362. format(o->mnemonic, i, o->ken);
  363. }
  364. static void
  365. addis(Opcode *o, Instr *i)
  366. {
  367. long v;
  368. v = i->imm64;
  369. if (i->op==15 && i->ra == 0)
  370. bprint(i, "MOVW\t$%lux,R%d", v, i->rd);
  371. else if (i->op==15 && i->ra == REGSB) {
  372. bprint(i, "MOVW\t$");
  373. address(i);
  374. bprint(i, ",R%d", i->rd);
  375. } else if(i->op==15 && v < 0) {
  376. bprint(i, "SUB\t$%ld,R%d", -v, i->ra);
  377. if(i->rd != i->ra)
  378. bprint(i, ",R%d", i->rd);
  379. } else {
  380. format(o->mnemonic, i, 0);
  381. bprint(i, "\t$%ld,R%d", v, i->ra);
  382. if(i->rd != i->ra)
  383. bprint(i, ",R%d", i->rd);
  384. }
  385. }
  386. static void
  387. andi(Opcode *o, Instr *i)
  388. {
  389. if (i->ra == i->rs)
  390. format(o->mnemonic, i, "%I,R%d");
  391. else
  392. format(o->mnemonic, i, o->ken);
  393. }
  394. static void
  395. gencc(Opcode *o, Instr *i)
  396. {
  397. format(o->mnemonic, i, o->ken);
  398. }
  399. static void
  400. gen(Opcode *o, Instr *i)
  401. {
  402. format(o->mnemonic, i, o->ken);
  403. if (i->rc)
  404. bprint(i, " [illegal Rc]");
  405. }
  406. static void
  407. ldx(Opcode *o, Instr *i)
  408. {
  409. if(i->ra == 0)
  410. format(o->mnemonic, i, "(R%b),R%d");
  411. else
  412. format(o->mnemonic, i, "(R%b+R%a),R%d");
  413. if(i->rc)
  414. bprint(i, " [illegal Rc]");
  415. }
  416. static void
  417. stx(Opcode *o, Instr *i)
  418. {
  419. if(i->ra == 0)
  420. format(o->mnemonic, i, "R%d,(R%b)");
  421. else
  422. format(o->mnemonic, i, "R%d,(R%b+R%a)");
  423. if(i->rc && i->xo != 150)
  424. bprint(i, " [illegal Rc]");
  425. }
  426. static void
  427. fldx(Opcode *o, Instr *i)
  428. {
  429. if(i->ra == 0)
  430. format(o->mnemonic, i, "(R%b),F%d");
  431. else
  432. format(o->mnemonic, i, "(R%b+R%a),F%d");
  433. if(i->rc)
  434. bprint(i, " [illegal Rc]");
  435. }
  436. static void
  437. fstx(Opcode *o, Instr *i)
  438. {
  439. if(i->ra == 0)
  440. format(o->mnemonic, i, "F%d,(R%b)");
  441. else
  442. format(o->mnemonic, i, "F%d,(R%b+R%a)");
  443. if(i->rc)
  444. bprint(i, " [illegal Rc]");
  445. }
  446. static void
  447. dcb(Opcode *o, Instr *i)
  448. {
  449. if(i->ra == 0)
  450. format(o->mnemonic, i, "(R%b)");
  451. else
  452. format(o->mnemonic, i, "(R%b+R%a)");
  453. if(i->rd)
  454. bprint(i, " [illegal Rd]");
  455. if(i->rc)
  456. bprint(i, " [illegal Rc]");
  457. }
  458. static void
  459. lw(Opcode *o, Instr *i, char r)
  460. {
  461. format(o->mnemonic, i, 0);
  462. bprint(i, "\t");
  463. address(i);
  464. bprint(i, ",%c%d", r, i->rd);
  465. }
  466. static void
  467. load(Opcode *o, Instr *i)
  468. {
  469. lw(o, i, 'R');
  470. }
  471. static void
  472. fload(Opcode *o, Instr *i)
  473. {
  474. lw(o, i, 'F');
  475. }
  476. static void
  477. sw(Opcode *o, Instr *i, char r)
  478. {
  479. int offset;
  480. char *m;
  481. Symbol s;
  482. m = o->mnemonic;
  483. if (i->rs == REGSP) {
  484. if (findsym(i->addr, CTEXT, &s) && findlocal(&s, FRAMENAME, &s)) {
  485. offset = s.value-i->imm64;
  486. if (offset > 0 && getauto(&s, offset, CAUTO, &s)) {
  487. bprint(i, "%s\t%c%d,%s-%d(SP)", m, r, i->rd,
  488. s.name, offset);
  489. return;
  490. }
  491. }
  492. }
  493. if (i->rs == REGSB && mach->sb) {
  494. bprint(i, "%s\t%c%d,", m, r, i->rd);
  495. address(i);
  496. return;
  497. }
  498. if (r == 'F')
  499. format(m, i, "F%d,%l");
  500. else
  501. format(m, i, o->ken);
  502. }
  503. static void
  504. store(Opcode *o, Instr *i)
  505. {
  506. sw(o, i, 'R');
  507. }
  508. static void
  509. fstore(Opcode *o, Instr *i)
  510. {
  511. sw(o, i, 'F');
  512. }
  513. static void
  514. shifti(Opcode *o, Instr *i)
  515. {
  516. if (i->ra == i->rs)
  517. format(o->mnemonic, i, "$%k,R%a");
  518. else
  519. format(o->mnemonic, i, o->ken);
  520. }
  521. static void
  522. shift(Opcode *o, Instr *i)
  523. {
  524. if (i->ra == i->rs)
  525. format(o->mnemonic, i, "R%b,R%a");
  526. else
  527. format(o->mnemonic, i, o->ken);
  528. }
  529. static void
  530. add(Opcode *o, Instr *i)
  531. {
  532. if (i->rd == i->ra)
  533. format(o->mnemonic, i, "R%b,R%d");
  534. else if (i->rd == i->rb)
  535. format(o->mnemonic, i, "R%a,R%d");
  536. else
  537. format(o->mnemonic, i, o->ken);
  538. }
  539. static void
  540. sub(Opcode *o, Instr *i)
  541. {
  542. format(o->mnemonic, i, 0);
  543. bprint(i, "\t");
  544. if(i->op == 31) {
  545. bprint(i, "\tR%d,R%d", i->ra, i->rb); /* subtract Ra from Rb */
  546. if(i->rd != i->rb)
  547. bprint(i, ",R%d", i->rd);
  548. } else
  549. bprint(i, "\tR%d,$%d,R%d", i->ra, i->simm, i->rd);
  550. }
  551. static void
  552. qdiv(Opcode *o, Instr *i)
  553. {
  554. format(o->mnemonic, i, 0);
  555. if(i->op == 31)
  556. bprint(i, "\tR%d,R%d", i->rb, i->ra);
  557. else
  558. bprint(i, "\t$%d,R%d", i->simm, i->ra);
  559. if(i->ra != i->rd)
  560. bprint(i, ",R%d", i->rd);
  561. }
  562. static void
  563. and(Opcode *o, Instr *i)
  564. {
  565. if (i->op == 31) {
  566. /* Rb,Rs,Ra */
  567. if (i->ra == i->rs)
  568. format(o->mnemonic, i, "R%b,R%a");
  569. else if (i->ra == i->rb)
  570. format(o->mnemonic, i, "R%s,R%a");
  571. else
  572. format(o->mnemonic, i, o->ken);
  573. } else {
  574. /* imm,Rs,Ra */
  575. if (i->ra == i->rs)
  576. format(o->mnemonic, i, "%I,R%a");
  577. else
  578. format(o->mnemonic, i, o->ken);
  579. }
  580. }
  581. static void
  582. or(Opcode *o, Instr *i)
  583. {
  584. if (i->op == 31) {
  585. /* Rb,Rs,Ra */
  586. if (i->rs == 0 && i->ra == 0 && i->rb == 0)
  587. format("NOP", i, 0);
  588. else if (i->rs == i->rb)
  589. format("MOVW", i, "R%b,R%a");
  590. else
  591. and(o, i);
  592. } else
  593. and(o, i);
  594. }
  595. static void
  596. shifted(Opcode *o, Instr *i)
  597. {
  598. format(o->mnemonic, i, 0);
  599. bprint(i, "\t$%lux,", (ulong)i->uimm<<16);
  600. if (i->rs == i->ra)
  601. bprint(i, "R%d", i->ra);
  602. else
  603. bprint(i, "R%d,R%d", i->rs, i->ra);
  604. }
  605. static void
  606. neg(Opcode *o, Instr *i)
  607. {
  608. if (i->rd == i->ra)
  609. format(o->mnemonic, i, "R%d");
  610. else
  611. format(o->mnemonic, i, o->ken);
  612. }
  613. static char ir2[] = "R%a,R%d"; /* reverse of IBM order */
  614. static char ir3[] = "R%b,R%a,R%d";
  615. static char ir3r[] = "R%a,R%b,R%d";
  616. static char il3[] = "R%b,R%s,R%a";
  617. static char il2u[] = "%I,R%a,R%d";
  618. static char il3s[] = "$%k,R%s,R%a";
  619. static char il2[] = "R%s,R%a";
  620. static char icmp3[] = "R%a,R%b,%D";
  621. static char cr3op[] = "%b,%a,%d";
  622. static char ir2i[] = "%i,R%a,R%d";
  623. static char fp2[] = "F%b,F%d";
  624. static char fp3[] = "F%b,F%a,F%d";
  625. static char fp3c[] = "F%c,F%a,F%d";
  626. static char fp4[] = "F%a,F%c,F%b,F%d";
  627. static char fpcmp[] = "F%a,F%b,%D";
  628. static char ldop[] = "%l,R%d";
  629. static char stop[] = "R%d,%l";
  630. static char fldop[] = "%l,F%d";
  631. static char fstop[] = "F%d,%l";
  632. static char rldc[] = "R%b,R%s,$%E,R%a";
  633. static char rlim[] = "R%b,R%s,$%z,R%a";
  634. static char rlimi[] = "$%k,R%s,$%z,R%a";
  635. static char rldi[] = "$%e,R%s,$%E,R%a";
  636. #define OEM IBF(~0,22,30)
  637. #define FP4 IBF(~0,26,30)
  638. #define ALL (~0)
  639. #define RLDC 0xF
  640. #define RLDI 0xE
  641. /*
  642. notes:
  643. 10-26: crfD = rD>>2; rD&3 mbz
  644. also, L bit (bit 10) mbz or selects 64-bit operands
  645. */
  646. static Opcode opcodes[] = {
  647. {31, 266, OEM, "ADD%V%C", add, ir3},
  648. {31, 10, OEM, "ADDC%V%C", add, ir3},
  649. {31, 138, OEM, "ADDE%V%C", add, ir3},
  650. {14, 0, 0, "ADD", addi, ir2i},
  651. {12, 0, 0, "ADDC", addi, ir2i},
  652. {13, 0, 0, "ADDCCC", addi, ir2i},
  653. {15, 0, 0, "ADD", addis, 0},
  654. {31, 234, OEM, "ADDME%V%C", gencc, ir2},
  655. {31, 202, OEM, "ADDZE%V%C", gencc, ir2},
  656. {31, 28, ALL, "AND%C", and, il3},
  657. {31, 60, ALL, "ANDN%C", and, il3},
  658. {28, 0, 0, "ANDCC", andi, il2u},
  659. {29, 0, 0, "ANDCC", shifted, 0},
  660. {18, 0, 0, "B%L", gencc, "%j"},
  661. {16, 0, 0, "BC%L", branch, "%d,%a,%J"},
  662. {19, 528, ALL, "BC%L", branch, "%d,%a,(CTR)"},
  663. {19, 16, ALL, "BC%L", branch, "%d,%a,(LR)"},
  664. {31, 0, ALL, "CMP", 0, icmp3},
  665. {11, 0, 0, "CMP", 0, "R%a,%i,%D"},
  666. {31, 32, ALL, "CMPU", 0, icmp3},
  667. {10, 0, 0, "CMPU", 0, "R%a,%I,%D"},
  668. {31, 58, ALL, "CNTLZD%C", gencc, ir2}, /* 64 */
  669. {31, 26, ALL, "CNTLZ%W%C", gencc, ir2},
  670. {19, 257, ALL, "CRAND", gen, cr3op},
  671. {19, 129, ALL, "CRANDN", gen, cr3op},
  672. {19, 289, ALL, "CREQV", gen, cr3op},
  673. {19, 225, ALL, "CRNAND", gen, cr3op},
  674. {19, 33, ALL, "CRNOR", gen, cr3op},
  675. {19, 449, ALL, "CROR", gen, cr3op},
  676. {19, 417, ALL, "CRORN", gen, cr3op},
  677. {19, 193, ALL, "CRXOR", gen, cr3op},
  678. {31, 86, ALL, "DCBF", dcb, 0},
  679. {31, 470, ALL, "DCBI", dcb, 0},
  680. {31, 54, ALL, "DCBST", dcb, 0},
  681. {31, 278, ALL, "DCBT", dcb, 0},
  682. {31, 246, ALL, "DCBTST", dcb, 0},
  683. {31, 1014, ALL, "DCBZ", dcb, 0},
  684. {31, 454, ALL, "DCCCI", dcb, 0},
  685. {31, 966, ALL, "ICCCI", dcb, 0},
  686. {31, 489, OEM, "DIVD%V%C", qdiv, ir3}, /* 64 */
  687. {31, 457, OEM, "DIVDU%V%C", qdiv, ir3}, /* 64 */
  688. {31, 491, OEM, "DIVW%V%C", qdiv, ir3},
  689. {31, 459, OEM, "DIVWU%V%C", qdiv, ir3},
  690. {31, 310, ALL, "ECIWX", ldx, 0},
  691. {31, 438, ALL, "ECOWX", stx, 0},
  692. {31, 854, ALL, "EIEIO", gen, 0},
  693. {31, 284, ALL, "EQV%C", gencc, il3},
  694. {31, 954, ALL, "EXTSB%C", gencc, il2},
  695. {31, 922, ALL, "EXTSH%C", gencc, il2},
  696. {31, 986, ALL, "EXTSW%C", gencc, il2}, /* 64 */
  697. {63, 264, ALL, "FABS%C", gencc, fp2},
  698. {63, 21, ALL, "FADD%C", gencc, fp3},
  699. {59, 21, ALL, "FADDS%C", gencc, fp3},
  700. {63, 32, ALL, "FCMPO", gen, fpcmp},
  701. {63, 0, ALL, "FCMPU", gen, fpcmp},
  702. {63, 846, ALL, "FCFID%C", gencc, fp2}, /* 64 */
  703. {63, 814, ALL, "FCTID%C", gencc, fp2}, /* 64 */
  704. {63, 815, ALL, "FCTIDZ%C", gencc, fp2}, /* 64 */
  705. {63, 14, ALL, "FCTIW%C", gencc, fp2},
  706. {63, 15, ALL, "FCTIWZ%C", gencc, fp2},
  707. {63, 18, ALL, "FDIV%C", gencc, fp3},
  708. {59, 18, ALL, "FDIVS%C", gencc, fp3},
  709. {63, 29, FP4, "FMADD%C", gencc, fp4},
  710. {59, 29, FP4, "FMADDS%C", gencc, fp4},
  711. {63, 72, ALL, "FMOVD%C", gencc, fp2},
  712. {63, 28, FP4, "FMSUB%C", gencc, fp4},
  713. {59, 28, FP4, "FMSUBS%C", gencc, fp4},
  714. {63, 25, FP4, "FMUL%C", gencc, fp3c},
  715. {59, 25, FP4, "FMULS%C", gencc, fp3c},
  716. {63, 136, ALL, "FNABS%C", gencc, fp2},
  717. {63, 40, ALL, "FNEG%C", gencc, fp2},
  718. {63, 31, FP4, "FNMADD%C", gencc, fp4},
  719. {59, 31, FP4, "FNMADDS%C", gencc, fp4},
  720. {63, 30, FP4, "FNMSUB%C", gencc, fp4},
  721. {59, 30, FP4, "FNMSUBS%C", gencc, fp4},
  722. {59, 24, ALL, "FRES%C", gencc, fp2}, /* optional */
  723. {63, 12, ALL, "FRSP%C", gencc, fp2},
  724. {63, 26, ALL, "FRSQRTE%C", gencc, fp2}, /* optional */
  725. {63, 23, FP4, "FSEL%CC", gencc, fp4}, /* optional */
  726. {63, 22, ALL, "FSQRT%C", gencc, fp2}, /* optional */
  727. {59, 22, ALL, "FSQRTS%C", gencc, fp2}, /* optional */
  728. {63, 20, FP4, "FSUB%C", gencc, fp3},
  729. {59, 20, FP4, "FSUBS%C", gencc, fp3},
  730. {31, 982, ALL, "ICBI", dcb, 0}, /* optional */
  731. {19, 150, ALL, "ISYNC", gen, 0},
  732. {34, 0, 0, "MOVBZ", load, ldop},
  733. {35, 0, 0, "MOVBZU", load, ldop},
  734. {31, 119, ALL, "MOVBZU", ldx, 0},
  735. {31, 87, ALL, "MOVBZ", ldx, 0},
  736. {50, 0, 0, "FMOVD", fload, fldop},
  737. {51, 0, 0, "FMOVDU", fload, fldop},
  738. {31, 631, ALL, "FMOVDU", fldx, 0},
  739. {31, 599, ALL, "FMOVD", fldx, 0},
  740. {48, 0, 0, "FMOVS", load, fldop},
  741. {49, 0, 0, "FMOVSU", load, fldop},
  742. {31, 567, ALL, "FMOVSU", fldx, 0},
  743. {31, 535, ALL, "FMOVS", fldx, 0},
  744. {42, 0, 0, "MOVH", load, ldop},
  745. {43, 0, 0, "MOVHU", load, ldop},
  746. {31, 375, ALL, "MOVHU", ldx, 0},
  747. {31, 343, ALL, "MOVH", ldx, 0},
  748. {31, 790, ALL, "MOVHBR", ldx, 0},
  749. {40, 0, 0, "MOVHZ", load, ldop},
  750. {41, 0, 0, "MOVHZU", load, ldop},
  751. {31, 311, ALL, "MOVHZU", ldx, 0},
  752. {31, 279, ALL, "MOVHZ", ldx, 0},
  753. {46, 0, 0, "MOVMW", load, ldop},
  754. {31, 597, ALL, "LSW", gen, "(R%a),$%n,R%d"},
  755. {31, 533, ALL, "LSW", ldx, 0},
  756. {31, 20, ALL, "LWAR", ldx, 0},
  757. {31, 84, ALL, "LWARD", ldx, 0}, /* 64 */
  758. {58, 0, ALL, "MOVD", load, ldop}, /* 64 */
  759. {58, 1, ALL, "MOVDU", load, ldop}, /* 64 */
  760. {31, 53, ALL, "MOVDU", ldx, 0}, /* 64 */
  761. {31, 21, ALL, "MOVD", ldx, 0}, /* 64 */
  762. {31, 534, ALL, "MOVWBR", ldx, 0},
  763. {58, 2, ALL, "MOVW", load, ldop}, /* 64 (lwa) */
  764. {31, 373, ALL, "MOVWU", ldx, 0}, /* 64 */
  765. {31, 341, ALL, "MOVW", ldx, 0}, /* 64 */
  766. {32, 0, 0, "MOVW%Z", load, ldop},
  767. {33, 0, 0, "MOVW%ZU", load, ldop},
  768. {31, 55, ALL, "MOVW%ZU", ldx, 0},
  769. {31, 23, ALL, "MOVW%Z", ldx, 0},
  770. {19, 0, ALL, "MOVFL", gen, "%S,%D"},
  771. {63, 64, ALL, "MOVCRFS", gen, "%S,%D"},
  772. {31, 512, ALL, "MOVW", gen, "XER,%D"},
  773. {31, 19, ALL, "MOVW", gen, "CR,R%d"},
  774. {63, 583, ALL, "MOVW%C", gen, "FPSCR, F%d"}, /* mffs */
  775. {31, 83, ALL, "MOVW", gen, "MSR,R%d"},
  776. {31, 339, ALL, "MOVW", gen, "%P,R%d"},
  777. {31, 595, ALL, "MOVW", gen, "SEG(%a),R%d"},
  778. {31, 659, ALL, "MOVW", gen, "SEG(R%b),R%d"},
  779. {31, 323, ALL, "MOVW", gen, "DCR(%Q),R%d"},
  780. {31, 451, ALL, "MOVW", gen, "R%s,DCR(%Q)"},
  781. {31, 144, ALL, "MOVFL", gen, "R%s,%m,CR"},
  782. {63, 70, ALL, "MTFSB0%C", gencc, "%D"},
  783. {63, 38, ALL, "MTFSB1%C", gencc, "%D"},
  784. {63, 711, ALL, "MOVFL%C", gencc, "F%b,%M,FPSCR"}, /* mtfsf */
  785. {63, 134, ALL, "MOVFL%C", gencc, "%K,%D"},
  786. {31, 146, ALL, "MOVW", gen, "R%s,MSR"},
  787. {31, 178, ALL, "MOVD", gen, "R%s,MSR"},
  788. {31, 467, ALL, "MOVW", gen, "R%s,%P"},
  789. {31, 210, ALL, "MOVW", gen, "R%s,SEG(%a)"},
  790. {31, 242, ALL, "MOVW", gen, "R%s,SEG(R%b)"},
  791. {31, 73, ALL, "MULHD%C", gencc, ir3},
  792. {31, 9, ALL, "MULHDU%C", gencc, ir3},
  793. {31, 233, OEM, "MULLD%V%C", gencc, ir3},
  794. {31, 75, ALL, "MULHW%C", gencc, ir3},
  795. {31, 11, ALL, "MULHWU%C", gencc, ir3},
  796. {31, 235, OEM, "MULLW%V%C", gencc, ir3},
  797. {7, 0, 0, "MULLW", qdiv, "%i,R%a,R%d"},
  798. {31, 476, ALL, "NAND%C", gencc, il3},
  799. {31, 104, OEM, "NEG%V%C", neg, ir2},
  800. {31, 124, ALL, "NOR%C", gencc, il3},
  801. {31, 444, ALL, "OR%C", or, il3},
  802. {31, 412, ALL, "ORN%C", or, il3},
  803. {24, 0, 0, "OR", and, "%I,R%d,R%a"},
  804. {25, 0, 0, "OR", shifted, 0},
  805. {19, 50, ALL, "RFI", gen, 0},
  806. {19, 51, ALL, "RFCI", gen, 0},
  807. {30, 8, RLDC, "RLDCL%C", gencc, rldc}, /* 64 */
  808. {30, 9, RLDC, "RLDCR%C", gencc, rldc}, /* 64 */
  809. {30, 0, RLDI, "RLDCL%C", gencc, rldi}, /* 64 */
  810. {30, 1<<1, RLDI, "RLDCR%C", gencc, rldi}, /* 64 */
  811. {30, 2<<1, RLDI, "RLDC%C", gencc, rldi}, /* 64 */
  812. {30, 3<<1, RLDI, "RLDMI%C", gencc, rldi}, /* 64 */
  813. {20, 0, 0, "RLWMI%C", gencc, rlimi},
  814. {21, 0, 0, "RLWNM%C", gencc, rlimi},
  815. {23, 0, 0, "RLWNM%C", gencc, rlim},
  816. {17, 1, ALL, "SYSCALL", gen, 0},
  817. {31, 27, ALL, "SLD%C", shift, il3}, /* 64 */
  818. {31, 24, ALL, "SLW%C", shift, il3},
  819. {31, 794, ALL, "SRAD%C", shift, il3}, /* 64 */
  820. {31, 413, ALL, "SRAD%C", shifti, il3s}, /* 64 */
  821. {31, 792, ALL, "SRAW%C", shift, il3},
  822. {31, 824, ALL, "SRAW%C", shifti, il3s},
  823. {31, 539, ALL, "SRD%C", shift, il3}, /* 64 */
  824. {31, 536, ALL, "SRW%C", shift, il3},
  825. {38, 0, 0, "MOVB", store, stop},
  826. {39, 0, 0, "MOVBU", store, stop},
  827. {31, 247, ALL, "MOVBU", stx, 0},
  828. {31, 215, ALL, "MOVB", stx, 0},
  829. {54, 0, 0, "FMOVD", fstore, fstop},
  830. {55, 0, 0, "FMOVDU", fstore, fstop},
  831. {31, 759, ALL, "FMOVDU", fstx, 0},
  832. {31, 727, ALL, "FMOVD", fstx, 0},
  833. {52, 0, 0, "FMOVS", fstore, fstop},
  834. {53, 0, 0, "FMOVSU", fstore, fstop},
  835. {31, 695, ALL, "FMOVSU", fstx, 0},
  836. {31, 663, ALL, "FMOVS", fstx, 0},
  837. {44, 0, 0, "MOVH", store, stop},
  838. {31, 918, ALL, "MOVHBR", stx, 0},
  839. {45, 0, 0, "MOVHU", store, stop},
  840. {31, 439, ALL, "MOVHU", stx, 0},
  841. {31, 407, ALL, "MOVH", stx, 0},
  842. {47, 0, 0, "MOVMW", store, stop},
  843. {31, 725, ALL, "STSW", gen, "R%d,$%n,(R%a)"},
  844. {31, 661, ALL, "STSW", stx, 0},
  845. {36, 0, 0, "MOVW", store, stop},
  846. {31, 662, ALL, "MOVWBR", stx, 0},
  847. {31, 150, ALL, "STWCCC", stx, 0},
  848. {31, 214, ALL, "STDCCC", stx, 0}, /* 64 */
  849. {37, 0, 0, "MOVWU", store, stop},
  850. {31, 183, ALL, "MOVWU", stx, 0},
  851. {31, 151, ALL, "MOVW", stx, 0},
  852. {62, 0, 0, "MOVD%U", store, stop}, /* 64 */
  853. {31, 149, ALL, "MOVD", stx, 0,}, /* 64 */
  854. {31, 181, ALL, "MOVDU", stx, 0}, /* 64 */
  855. {31, 498, ALL, "SLBIA", gen, 0}, /* 64 */
  856. {31, 434, ALL, "SLBIE", gen, "R%b"}, /* 64 */
  857. {31, 466, ALL, "SLBIEX", gen, "R%b"}, /* 64 */
  858. {31, 915, ALL, "SLBMFEE", gen, "R%b,R%d"}, /* 64 */
  859. {31, 851, ALL, "SLBMFEV", gen, "R%b,R%d"}, /* 64 */
  860. {31, 402, ALL, "SLBMTE", gen, "R%s,R%b"}, /* 64 */
  861. {31, 40, OEM, "SUB%V%C", sub, ir3},
  862. {31, 8, OEM, "SUBC%V%C", sub, ir3},
  863. {31, 136, OEM, "SUBE%V%C", sub, ir3},
  864. {8, 0, 0, "SUBC", gen, "R%a,%i,R%d"},
  865. {31, 232, OEM, "SUBME%V%C", sub, ir2},
  866. {31, 200, OEM, "SUBZE%V%C", sub, ir2},
  867. {31, 598, ALL, "SYNC", gen, 0}, /* TO DO: there's a parameter buried in there */
  868. {2, 0, 0, "TD", gen, "%d,R%a,%i"}, /* 64 */
  869. {31, 370, ALL, "TLBIA", gen, 0}, /* optional */
  870. {31, 306, ALL, "TLBIE", gen, "R%b"}, /* optional */
  871. {31, 274, ALL, "TLBIEL", gen, "R%b"}, /* optional */
  872. {31, 1010, ALL, "TLBLI", gen, "R%b"}, /* optional */
  873. {31, 978, ALL, "TLBLD", gen, "R%b"}, /* optional */
  874. {31, 566, ALL, "TLBSYNC", gen, 0}, /* optional */
  875. {31, 68, ALL, "TD", gen, "%d,R%a,R%b"}, /* 64 */
  876. {31, 4, ALL, "TW", gen, "%d,R%a,R%b"},
  877. {3, 0, 0, "TW", gen, "%d,R%a,%i"},
  878. {31, 316, ALL, "XOR", and, il3},
  879. {26, 0, 0, "XOR", and, il2u},
  880. {27, 0, 0, "XOR", shifted, 0},
  881. {0},
  882. };
  883. typedef struct Spr Spr;
  884. struct Spr {
  885. int n;
  886. char *name;
  887. };
  888. static Spr sprname[] = {
  889. {0, "MQ"},
  890. {1, "XER"},
  891. {268, "TBL"},
  892. {269, "TBU"},
  893. {8, "LR"},
  894. {9, "CTR"},
  895. {528, "IBAT0U"},
  896. {529, "IBAT0L"},
  897. {530, "IBAT1U"},
  898. {531, "IBAT1L"},
  899. {532, "IBAT2U"},
  900. {533, "IBAT2L"},
  901. {534, "IBAT3U"},
  902. {535, "IBAT3L"},
  903. {536, "DBAT0U"},
  904. {537, "DBAT0L"},
  905. {538, "DBAT1U"},
  906. {539, "DBAT1L"},
  907. {540, "DBAT2U"},
  908. {541, "DBAT2L"},
  909. {542, "DBAT3U"},
  910. {543, "DBAT3L"},
  911. {25, "SDR1"},
  912. {19, "DAR"},
  913. {272, "SPRG0"},
  914. {273, "SPRG1"},
  915. {274, "SPRG2"},
  916. {275, "SPRG3"},
  917. {18, "DSISR"},
  918. {26, "SRR0"},
  919. {27, "SRR1"},
  920. {284, "TBLW"},
  921. {285, "TBUW"},
  922. {22, "DEC"},
  923. {282, "EAR"},
  924. {1008, "HID0"},
  925. {1009, "HID1"},
  926. {976, "DMISS"},
  927. {977, "DCMP"},
  928. {978, "HASH1"},
  929. {979, "HASH2"},
  930. {980, "IMISS"},
  931. {981, "ICMP"},
  932. {982, "RPA"},
  933. {1010, "IABR"},
  934. {1013, "DABR"},
  935. {0,0},
  936. };
  937. static int
  938. shmask(uvlong *m)
  939. {
  940. int i;
  941. for(i=0; i<63; i++)
  942. if(*m & ((uvlong)1<<i))
  943. break;
  944. if(i > 63)
  945. return 0;
  946. if(*m & ~((uvlong)1<<i)){ /* more than one bit: do multiples of bytes */
  947. i = (i/8)*8;
  948. if(i == 0)
  949. return 0;
  950. }
  951. *m >>= i;
  952. return i;
  953. }
  954. static void
  955. format(char *mnemonic, Instr *i, char *f)
  956. {
  957. int n, s;
  958. ulong mask;
  959. uvlong vmask;
  960. if (mnemonic)
  961. format(0, i, mnemonic);
  962. if (f == 0)
  963. return;
  964. if (mnemonic)
  965. bprint(i, "\t");
  966. for ( ; *f; f++) {
  967. if (*f != '%') {
  968. bprint(i, "%c", *f);
  969. continue;
  970. }
  971. switch (*++f) {
  972. case 'a':
  973. bprint(i, "%d", i->ra);
  974. break;
  975. case 'b':
  976. bprint(i, "%d", i->rb);
  977. break;
  978. case 'c':
  979. bprint(i, "%d", i->frc);
  980. break;
  981. case 'd':
  982. case 's':
  983. bprint(i, "%d", i->rd);
  984. break;
  985. case 'C':
  986. if(i->rc)
  987. bprint(i, "CC");
  988. break;
  989. case 'D':
  990. if(i->rd & 3)
  991. bprint(i, "CR(INVAL:%d)", i->rd);
  992. else if(i->op == 63)
  993. bprint(i, "FPSCR(%d)", i->crfd);
  994. else
  995. bprint(i, "CR(%d)", i->crfd);
  996. break;
  997. case 'e':
  998. bprint(i, "%d", i->xsh);
  999. break;
  1000. case 'E':
  1001. switch(IBF(i->w0,27,30)){ /* low bit is top bit of shift in rldiX cases */
  1002. case 8: i->mb = i->xmbe; i->me = 63; break; /* rldcl */
  1003. case 9: i->mb = 0; i->me = i->xmbe; break; /* rldcr */
  1004. case 4: case 5:
  1005. i->mb = i->xmbe; i->me = 63-i->xsh; break; /* rldic */
  1006. case 0: case 1:
  1007. i->mb = i->xmbe; i->me = 63; break; /* rldicl */
  1008. case 2: case 3:
  1009. i->mb = 0; i->me = i->xmbe; break; /* rldicr */
  1010. case 6: case 7:
  1011. i->mb = i->xmbe; i->me = 63-i->xsh; break; /* rldimi */
  1012. }
  1013. vmask = (~(uvlong)0>>i->mb) & (~(uvlong)0<<(63-i->me));
  1014. s = shmask(&vmask);
  1015. if(s)
  1016. bprint(i, "(%llux<<%d)", vmask, s);
  1017. else
  1018. bprint(i, "%llux", vmask);
  1019. break;
  1020. case 'i':
  1021. bprint(i, "$%d", i->simm);
  1022. break;
  1023. case 'I':
  1024. bprint(i, "$%ux", i->uimm);
  1025. break;
  1026. case 'j':
  1027. if(i->aa)
  1028. pglobal(i, i->li, 1, "(SB)");
  1029. else
  1030. pglobal(i, i->addr+i->li, 1, "");
  1031. break;
  1032. case 'J':
  1033. if(i->aa)
  1034. pglobal(i, i->bd, 1, "(SB)");
  1035. else
  1036. pglobal(i, i->addr+i->bd, 1, "");
  1037. break;
  1038. case 'k':
  1039. bprint(i, "%d", i->sh);
  1040. break;
  1041. case 'K':
  1042. bprint(i, "$%x", i->imm);
  1043. break;
  1044. case 'L':
  1045. if(i->lk)
  1046. bprint(i, "L");
  1047. break;
  1048. case 'l':
  1049. if(i->simm < 0)
  1050. bprint(i, "-%x(R%d)", -i->simm, i->ra);
  1051. else
  1052. bprint(i, "%x(R%d)", i->simm, i->ra);
  1053. break;
  1054. case 'm':
  1055. bprint(i, "%ux", i->crm);
  1056. break;
  1057. case 'M':
  1058. bprint(i, "%ux", i->fm);
  1059. break;
  1060. case 'n':
  1061. bprint(i, "%d", i->nb==0? 32: i->nb); /* eg, pg 10-103 */
  1062. break;
  1063. case 'P':
  1064. n = ((i->spr&0x1f)<<5)|((i->spr>>5)&0x1f);
  1065. for(s=0; sprname[s].name; s++)
  1066. if(sprname[s].n == n)
  1067. break;
  1068. if(sprname[s].name) {
  1069. if(s < 10)
  1070. bprint(i, sprname[s].name);
  1071. else
  1072. bprint(i, "SPR(%s)", sprname[s].name);
  1073. } else
  1074. bprint(i, "SPR(%d)", n);
  1075. break;
  1076. case 'Q':
  1077. n = ((i->spr&0x1f)<<5)|((i->spr>>5)&0x1f);
  1078. bprint(i, "%d", n);
  1079. break;
  1080. case 'S':
  1081. if(i->ra & 3)
  1082. bprint(i, "CR(INVAL:%d)", i->ra);
  1083. else if(i->op == 63)
  1084. bprint(i, "FPSCR(%d)", i->crfs);
  1085. else
  1086. bprint(i, "CR(%d)", i->crfs);
  1087. break;
  1088. case 'U':
  1089. if(i->rc)
  1090. bprint(i, "U");
  1091. break;
  1092. case 'V':
  1093. if(i->oe)
  1094. bprint(i, "V");
  1095. break;
  1096. case 'w':
  1097. bprint(i, "[%lux]", i->w0);
  1098. break;
  1099. case 'W':
  1100. if(i->m64)
  1101. bprint(i, "W");
  1102. break;
  1103. case 'Z':
  1104. if(i->m64)
  1105. bprint(i, "Z");
  1106. break;
  1107. case 'z':
  1108. if(i->mb <= i->me)
  1109. mask = ((ulong)~0L>>i->mb) & (~0L<<(31-i->me));
  1110. else
  1111. mask = ~(((ulong)~0L>>(i->me+1)) & (~0L<<(31-(i->mb-1))));
  1112. bprint(i, "%lux", mask);
  1113. break;
  1114. case '\0':
  1115. bprint(i, "%%");
  1116. return;
  1117. default:
  1118. bprint(i, "%%%c", *f);
  1119. break;
  1120. }
  1121. }
  1122. }
  1123. static int
  1124. printins(Map *map, uvlong pc, char *buf, int n)
  1125. {
  1126. Instr i;
  1127. Opcode *o;
  1128. mymap = map;
  1129. memset(&i, 0, sizeof(i));
  1130. i.curr = buf;
  1131. i.end = buf+n-1;
  1132. if(mkinstr(pc, &i) < 0)
  1133. return -1;
  1134. for(o = opcodes; o->mnemonic != 0; o++)
  1135. if(i.op == o->op && (i.xo & o->xomask) == o->xo) {
  1136. if (o->f)
  1137. (*o->f)(o, &i);
  1138. else
  1139. format(o->mnemonic, &i, o->ken);
  1140. return i.size*4;
  1141. }
  1142. bprint(&i, "unknown %lux", i.w0);
  1143. return i.size*4;
  1144. }
  1145. static int
  1146. powerinst(Map *map, uvlong pc, char modifier, char *buf, int n)
  1147. {
  1148. USED(modifier);
  1149. return printins(map, pc, buf, n);
  1150. }
  1151. static int
  1152. powerdas(Map *map, uvlong pc, char *buf, int n)
  1153. {
  1154. Instr instr;
  1155. mymap = map;
  1156. memset(&instr, 0, sizeof(instr));
  1157. instr.curr = buf;
  1158. instr.end = buf+n-1;
  1159. if (mkinstr(pc, &instr) < 0)
  1160. return -1;
  1161. if (instr.end-instr.curr > 8)
  1162. instr.curr = _hexify(instr.curr, instr.w0, 7);
  1163. if (instr.end-instr.curr > 9 && instr.size == 2) {
  1164. *instr.curr++ = ' ';
  1165. instr.curr = _hexify(instr.curr, instr.w1, 7);
  1166. }
  1167. *instr.curr = 0;
  1168. return instr.size*4;
  1169. }
  1170. static int
  1171. powerinstlen(Map *map, uvlong pc)
  1172. {
  1173. Instr i;
  1174. mymap = map;
  1175. if (mkinstr(pc, &i) < 0)
  1176. return -1;
  1177. return i.size*4;
  1178. }
  1179. static int
  1180. powerfoll(Map *map, uvlong pc, Rgetter rget, uvlong *foll)
  1181. {
  1182. char *reg;
  1183. Instr i;
  1184. mymap = map;
  1185. if (mkinstr(pc, &i) < 0)
  1186. return -1;
  1187. foll[0] = pc+4;
  1188. foll[1] = pc+4;
  1189. switch(i.op) {
  1190. default:
  1191. return 1;
  1192. case 18: /* branch */
  1193. foll[0] = i.li;
  1194. if(!i.aa)
  1195. foll[0] += pc;
  1196. break;
  1197. case 16: /* conditional branch */
  1198. foll[0] = i.bd;
  1199. if(!i.aa)
  1200. foll[0] += pc;
  1201. break;
  1202. case 19: /* conditional branch to register */
  1203. if(i.xo == 528)
  1204. reg = "CTR";
  1205. else if(i.xo == 16)
  1206. reg = "LR";
  1207. else
  1208. return 1; /* not a branch */
  1209. foll[0] = (*rget)(map, reg);
  1210. break;
  1211. }
  1212. if(i.lk)
  1213. return 2;
  1214. return 1;
  1215. }