disasm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * several functions that help interpret ARC instructions
  3. * used for unaligned accesses, kprobes and kgdb
  4. *
  5. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/kprobes.h>
  13. #include <linux/slab.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/disasm.h>
  16. #if defined(CONFIG_KGDB) || defined(CONFIG_ARC_EMUL_UNALIGNED) || \
  17. defined(CONFIG_KPROBES)
  18. /* disasm_instr: Analyses instruction at addr, stores
  19. * findings in *state
  20. */
  21. void __kprobes disasm_instr(unsigned long addr, struct disasm_state *state,
  22. int userspace, struct pt_regs *regs, struct callee_regs *cregs)
  23. {
  24. int fieldA = 0;
  25. int fieldC = 0, fieldCisReg = 0;
  26. uint16_t word1 = 0, word0 = 0;
  27. int subopcode, is_linked, op_format;
  28. uint16_t *ins_ptr;
  29. uint16_t ins_buf[4];
  30. int bytes_not_copied = 0;
  31. memset(state, 0, sizeof(struct disasm_state));
  32. /* This fetches the upper part of the 32 bit instruction
  33. * in both the cases of Little Endian or Big Endian configurations. */
  34. if (userspace) {
  35. bytes_not_copied = copy_from_user(ins_buf,
  36. (const void __user *) addr, 8);
  37. if (bytes_not_copied > 6)
  38. goto fault;
  39. ins_ptr = ins_buf;
  40. } else {
  41. ins_ptr = (uint16_t *) addr;
  42. }
  43. word1 = *((uint16_t *)addr);
  44. state->major_opcode = (word1 >> 11) & 0x1F;
  45. /* Check if the instruction is 32 bit or 16 bit instruction */
  46. if (state->major_opcode < 0x0B) {
  47. if (bytes_not_copied > 4)
  48. goto fault;
  49. state->instr_len = 4;
  50. word0 = *((uint16_t *)(addr+2));
  51. state->words[0] = (word1 << 16) | word0;
  52. } else {
  53. state->instr_len = 2;
  54. state->words[0] = word1;
  55. }
  56. /* Read the second word in case of limm */
  57. word1 = *((uint16_t *)(addr + state->instr_len));
  58. word0 = *((uint16_t *)(addr + state->instr_len + 2));
  59. state->words[1] = (word1 << 16) | word0;
  60. switch (state->major_opcode) {
  61. case op_Bcc:
  62. state->is_branch = 1;
  63. /* unconditional branch s25, conditional branch s21 */
  64. fieldA = (IS_BIT(state->words[0], 16)) ?
  65. FIELD_s25(state->words[0]) :
  66. FIELD_s21(state->words[0]);
  67. state->delay_slot = IS_BIT(state->words[0], 5);
  68. state->target = fieldA + (addr & ~0x3);
  69. state->flow = direct_jump;
  70. break;
  71. case op_BLcc:
  72. if (IS_BIT(state->words[0], 16)) {
  73. /* Branch and Link*/
  74. /* unconditional branch s25, conditional branch s21 */
  75. fieldA = (IS_BIT(state->words[0], 17)) ?
  76. (FIELD_s25(state->words[0]) & ~0x3) :
  77. FIELD_s21(state->words[0]);
  78. state->flow = direct_call;
  79. } else {
  80. /*Branch On Compare */
  81. fieldA = FIELD_s9(state->words[0]) & ~0x3;
  82. state->flow = direct_jump;
  83. }
  84. state->delay_slot = IS_BIT(state->words[0], 5);
  85. state->target = fieldA + (addr & ~0x3);
  86. state->is_branch = 1;
  87. break;
  88. case op_LD: /* LD<zz> a,[b,s9] */
  89. state->write = 0;
  90. state->di = BITS(state->words[0], 11, 11);
  91. if (state->di)
  92. break;
  93. state->x = BITS(state->words[0], 6, 6);
  94. state->zz = BITS(state->words[0], 7, 8);
  95. state->aa = BITS(state->words[0], 9, 10);
  96. state->wb_reg = FIELD_B(state->words[0]);
  97. if (state->wb_reg == REG_LIMM) {
  98. state->instr_len += 4;
  99. state->aa = 0;
  100. state->src1 = state->words[1];
  101. } else {
  102. state->src1 = get_reg(state->wb_reg, regs, cregs);
  103. }
  104. state->src2 = FIELD_s9(state->words[0]);
  105. state->dest = FIELD_A(state->words[0]);
  106. state->pref = (state->dest == REG_LIMM);
  107. break;
  108. case op_ST:
  109. state->write = 1;
  110. state->di = BITS(state->words[0], 5, 5);
  111. if (state->di)
  112. break;
  113. state->aa = BITS(state->words[0], 3, 4);
  114. state->zz = BITS(state->words[0], 1, 2);
  115. state->src1 = FIELD_C(state->words[0]);
  116. if (state->src1 == REG_LIMM) {
  117. state->instr_len += 4;
  118. state->src1 = state->words[1];
  119. } else {
  120. state->src1 = get_reg(state->src1, regs, cregs);
  121. }
  122. state->wb_reg = FIELD_B(state->words[0]);
  123. if (state->wb_reg == REG_LIMM) {
  124. state->aa = 0;
  125. state->instr_len += 4;
  126. state->src2 = state->words[1];
  127. } else {
  128. state->src2 = get_reg(state->wb_reg, regs, cregs);
  129. }
  130. state->src3 = FIELD_s9(state->words[0]);
  131. break;
  132. case op_MAJOR_4:
  133. subopcode = MINOR_OPCODE(state->words[0]);
  134. switch (subopcode) {
  135. case 32: /* Jcc */
  136. case 33: /* Jcc.D */
  137. case 34: /* JLcc */
  138. case 35: /* JLcc.D */
  139. is_linked = 0;
  140. if (subopcode == 33 || subopcode == 35)
  141. state->delay_slot = 1;
  142. if (subopcode == 34 || subopcode == 35)
  143. is_linked = 1;
  144. fieldCisReg = 0;
  145. op_format = BITS(state->words[0], 22, 23);
  146. if (op_format == 0 || ((op_format == 3) &&
  147. (!IS_BIT(state->words[0], 5)))) {
  148. fieldC = FIELD_C(state->words[0]);
  149. if (fieldC == REG_LIMM) {
  150. fieldC = state->words[1];
  151. state->instr_len += 4;
  152. } else {
  153. fieldCisReg = 1;
  154. }
  155. } else if (op_format == 1 || ((op_format == 3)
  156. && (IS_BIT(state->words[0], 5)))) {
  157. fieldC = FIELD_C(state->words[0]);
  158. } else {
  159. /* op_format == 2 */
  160. fieldC = FIELD_s12(state->words[0]);
  161. }
  162. if (!fieldCisReg) {
  163. state->target = fieldC;
  164. state->flow = is_linked ?
  165. direct_call : direct_jump;
  166. } else {
  167. state->target = get_reg(fieldC, regs, cregs);
  168. state->flow = is_linked ?
  169. indirect_call : indirect_jump;
  170. }
  171. state->is_branch = 1;
  172. break;
  173. case 40: /* LPcc */
  174. if (BITS(state->words[0], 22, 23) == 3) {
  175. /* Conditional LPcc u7 */
  176. fieldC = FIELD_C(state->words[0]);
  177. fieldC = fieldC << 1;
  178. fieldC += (addr & ~0x03);
  179. state->is_branch = 1;
  180. state->flow = direct_jump;
  181. state->target = fieldC;
  182. }
  183. /* For Unconditional lp, next pc is the fall through
  184. * which is updated */
  185. break;
  186. case 48 ... 55: /* LD a,[b,c] */
  187. state->di = BITS(state->words[0], 15, 15);
  188. if (state->di)
  189. break;
  190. state->x = BITS(state->words[0], 16, 16);
  191. state->zz = BITS(state->words[0], 17, 18);
  192. state->aa = BITS(state->words[0], 22, 23);
  193. state->wb_reg = FIELD_B(state->words[0]);
  194. if (state->wb_reg == REG_LIMM) {
  195. state->instr_len += 4;
  196. state->src1 = state->words[1];
  197. } else {
  198. state->src1 = get_reg(state->wb_reg, regs,
  199. cregs);
  200. }
  201. state->src2 = FIELD_C(state->words[0]);
  202. if (state->src2 == REG_LIMM) {
  203. state->instr_len += 4;
  204. state->src2 = state->words[1];
  205. } else {
  206. state->src2 = get_reg(state->src2, regs,
  207. cregs);
  208. }
  209. state->dest = FIELD_A(state->words[0]);
  210. if (state->dest == REG_LIMM)
  211. state->pref = 1;
  212. break;
  213. case 10: /* MOV */
  214. /* still need to check for limm to extract instr len */
  215. /* MOV is special case because it only takes 2 args */
  216. switch (BITS(state->words[0], 22, 23)) {
  217. case 0: /* OP a,b,c */
  218. if (FIELD_C(state->words[0]) == REG_LIMM)
  219. state->instr_len += 4;
  220. break;
  221. case 1: /* OP a,b,u6 */
  222. break;
  223. case 2: /* OP b,b,s12 */
  224. break;
  225. case 3: /* OP.cc b,b,c/u6 */
  226. if ((!IS_BIT(state->words[0], 5)) &&
  227. (FIELD_C(state->words[0]) == REG_LIMM))
  228. state->instr_len += 4;
  229. break;
  230. }
  231. break;
  232. default:
  233. /* Not a Load, Jump or Loop instruction */
  234. /* still need to check for limm to extract instr len */
  235. switch (BITS(state->words[0], 22, 23)) {
  236. case 0: /* OP a,b,c */
  237. if ((FIELD_B(state->words[0]) == REG_LIMM) ||
  238. (FIELD_C(state->words[0]) == REG_LIMM))
  239. state->instr_len += 4;
  240. break;
  241. case 1: /* OP a,b,u6 */
  242. break;
  243. case 2: /* OP b,b,s12 */
  244. break;
  245. case 3: /* OP.cc b,b,c/u6 */
  246. if ((!IS_BIT(state->words[0], 5)) &&
  247. ((FIELD_B(state->words[0]) == REG_LIMM) ||
  248. (FIELD_C(state->words[0]) == REG_LIMM)))
  249. state->instr_len += 4;
  250. break;
  251. }
  252. break;
  253. }
  254. break;
  255. /* 16 Bit Instructions */
  256. case op_LD_ADD: /* LD_S|LDB_S|LDW_S a,[b,c] */
  257. state->zz = BITS(state->words[0], 3, 4);
  258. state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
  259. state->src2 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
  260. state->dest = FIELD_S_A(state->words[0]);
  261. break;
  262. case op_ADD_MOV_CMP:
  263. /* check for limm, ignore mov_s h,b (== mov_s 0,b) */
  264. if ((BITS(state->words[0], 3, 4) < 3) &&
  265. (FIELD_S_H(state->words[0]) == REG_LIMM))
  266. state->instr_len += 4;
  267. break;
  268. case op_S:
  269. subopcode = BITS(state->words[0], 5, 7);
  270. switch (subopcode) {
  271. case 0: /* j_s */
  272. case 1: /* j_s.d */
  273. case 2: /* jl_s */
  274. case 3: /* jl_s.d */
  275. state->target = get_reg(FIELD_S_B(state->words[0]),
  276. regs, cregs);
  277. state->delay_slot = subopcode & 1;
  278. state->flow = (subopcode >= 2) ?
  279. direct_call : indirect_jump;
  280. break;
  281. case 7:
  282. switch (BITS(state->words[0], 8, 10)) {
  283. case 4: /* jeq_s [blink] */
  284. case 5: /* jne_s [blink] */
  285. case 6: /* j_s [blink] */
  286. case 7: /* j_s.d [blink] */
  287. state->delay_slot = (subopcode == 7);
  288. state->flow = indirect_jump;
  289. state->target = get_reg(31, regs, cregs);
  290. default:
  291. break;
  292. }
  293. default:
  294. break;
  295. }
  296. break;
  297. case op_LD_S: /* LD_S c, [b, u7] */
  298. state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
  299. state->src2 = FIELD_S_u7(state->words[0]);
  300. state->dest = FIELD_S_C(state->words[0]);
  301. break;
  302. case op_LDB_S:
  303. case op_STB_S:
  304. /* no further handling required as byte accesses should not
  305. * cause an unaligned access exception */
  306. state->zz = 1;
  307. break;
  308. case op_LDWX_S: /* LDWX_S c, [b, u6] */
  309. state->x = 1;
  310. /* intentional fall-through */
  311. case op_LDW_S: /* LDW_S c, [b, u6] */
  312. state->zz = 2;
  313. state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
  314. state->src2 = FIELD_S_u6(state->words[0]);
  315. state->dest = FIELD_S_C(state->words[0]);
  316. break;
  317. case op_ST_S: /* ST_S c, [b, u7] */
  318. state->write = 1;
  319. state->src1 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
  320. state->src2 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
  321. state->src3 = FIELD_S_u7(state->words[0]);
  322. break;
  323. case op_STW_S: /* STW_S c,[b,u6] */
  324. state->write = 1;
  325. state->zz = 2;
  326. state->src1 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
  327. state->src2 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
  328. state->src3 = FIELD_S_u6(state->words[0]);
  329. break;
  330. case op_SP: /* LD_S|LDB_S b,[sp,u7], ST_S|STB_S b,[sp,u7] */
  331. /* note: we are ignoring possibility of:
  332. * ADD_S, SUB_S, PUSH_S, POP_S as these should not
  333. * cause unaliged exception anyway */
  334. state->write = BITS(state->words[0], 6, 6);
  335. state->zz = BITS(state->words[0], 5, 5);
  336. if (state->zz)
  337. break; /* byte accesses should not come here */
  338. if (!state->write) {
  339. state->src1 = get_reg(28, regs, cregs);
  340. state->src2 = FIELD_S_u7(state->words[0]);
  341. state->dest = FIELD_S_B(state->words[0]);
  342. } else {
  343. state->src1 = get_reg(FIELD_S_B(state->words[0]), regs,
  344. cregs);
  345. state->src2 = get_reg(28, regs, cregs);
  346. state->src3 = FIELD_S_u7(state->words[0]);
  347. }
  348. break;
  349. case op_GP: /* LD_S|LDB_S|LDW_S r0,[gp,s11/s9/s10] */
  350. /* note: ADD_S r0, gp, s11 is ignored */
  351. state->zz = BITS(state->words[0], 9, 10);
  352. state->src1 = get_reg(26, regs, cregs);
  353. state->src2 = state->zz ? FIELD_S_s10(state->words[0]) :
  354. FIELD_S_s11(state->words[0]);
  355. state->dest = 0;
  356. break;
  357. case op_Pcl: /* LD_S b,[pcl,u10] */
  358. state->src1 = regs->ret & ~3;
  359. state->src2 = FIELD_S_u10(state->words[0]);
  360. state->dest = FIELD_S_B(state->words[0]);
  361. break;
  362. case op_BR_S:
  363. state->target = FIELD_S_s8(state->words[0]) + (addr & ~0x03);
  364. state->flow = direct_jump;
  365. state->is_branch = 1;
  366. break;
  367. case op_B_S:
  368. fieldA = (BITS(state->words[0], 9, 10) == 3) ?
  369. FIELD_S_s7(state->words[0]) :
  370. FIELD_S_s10(state->words[0]);
  371. state->target = fieldA + (addr & ~0x03);
  372. state->flow = direct_jump;
  373. state->is_branch = 1;
  374. break;
  375. case op_BL_S:
  376. state->target = FIELD_S_s13(state->words[0]) + (addr & ~0x03);
  377. state->flow = direct_call;
  378. state->is_branch = 1;
  379. break;
  380. default:
  381. break;
  382. }
  383. if (bytes_not_copied <= (8 - state->instr_len))
  384. return;
  385. fault: state->fault = 1;
  386. }
  387. long __kprobes get_reg(int reg, struct pt_regs *regs,
  388. struct callee_regs *cregs)
  389. {
  390. long *p;
  391. if (reg <= 12) {
  392. p = &regs->r0;
  393. return p[-reg];
  394. }
  395. if (cregs && (reg <= 25)) {
  396. p = &cregs->r13;
  397. return p[13-reg];
  398. }
  399. if (reg == 26)
  400. return regs->r26;
  401. if (reg == 27)
  402. return regs->fp;
  403. if (reg == 28)
  404. return regs->sp;
  405. if (reg == 31)
  406. return regs->blink;
  407. return 0;
  408. }
  409. void __kprobes set_reg(int reg, long val, struct pt_regs *regs,
  410. struct callee_regs *cregs)
  411. {
  412. long *p;
  413. switch (reg) {
  414. case 0 ... 12:
  415. p = &regs->r0;
  416. p[-reg] = val;
  417. break;
  418. case 13 ... 25:
  419. if (cregs) {
  420. p = &cregs->r13;
  421. p[13-reg] = val;
  422. }
  423. break;
  424. case 26:
  425. regs->r26 = val;
  426. break;
  427. case 27:
  428. regs->fp = val;
  429. break;
  430. case 28:
  431. regs->sp = val;
  432. break;
  433. case 31:
  434. regs->blink = val;
  435. break;
  436. default:
  437. break;
  438. }
  439. }
  440. /*
  441. * Disassembles the insn at @pc and sets @next_pc to next PC (which could be
  442. * @pc +2/4/6 (ARCompact ISA allows free intermixing of 16/32 bit insns).
  443. *
  444. * If @pc is a branch
  445. * -@tgt_if_br is set to branch target.
  446. * -If branch has delay slot, @next_pc updated with actual next PC.
  447. */
  448. int __kprobes disasm_next_pc(unsigned long pc, struct pt_regs *regs,
  449. struct callee_regs *cregs,
  450. unsigned long *next_pc, unsigned long *tgt_if_br)
  451. {
  452. struct disasm_state instr;
  453. memset(&instr, 0, sizeof(struct disasm_state));
  454. disasm_instr(pc, &instr, 0, regs, cregs);
  455. *next_pc = pc + instr.instr_len;
  456. /* Instruction with possible two targets branch, jump and loop */
  457. if (instr.is_branch)
  458. *tgt_if_br = instr.target;
  459. /* For the instructions with delay slots, the fall through is the
  460. * instruction following the instruction in delay slot.
  461. */
  462. if (instr.delay_slot) {
  463. struct disasm_state instr_d;
  464. disasm_instr(*next_pc, &instr_d, 0, regs, cregs);
  465. *next_pc += instr_d.instr_len;
  466. }
  467. /* Zero Overhead Loop - end of the loop */
  468. if (!(regs->status32 & STATUS32_L) && (*next_pc == regs->lp_end)
  469. && (regs->lp_count > 1)) {
  470. *next_pc = regs->lp_start;
  471. }
  472. return instr.is_branch;
  473. }
  474. #endif /* CONFIG_KGDB || CONFIG_ARC_EMUL_UNALIGNED || CONFIG_KPROBES */