align.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * align.c - address exception handler for M32R
  3. *
  4. * Copyright (c) 2003 Hitoshi Yamamoto
  5. */
  6. #include <asm/ptrace.h>
  7. #include <asm/uaccess.h>
  8. static int get_reg(struct pt_regs *regs, int nr)
  9. {
  10. int val;
  11. if (nr < 4)
  12. val = *(unsigned long *)(&regs->r0 + nr);
  13. else if (nr < 7)
  14. val = *(unsigned long *)(&regs->r4 + (nr - 4));
  15. else if (nr < 13)
  16. val = *(unsigned long *)(&regs->r7 + (nr - 7));
  17. else
  18. val = *(unsigned long *)(&regs->fp + (nr - 13));
  19. return val;
  20. }
  21. static void set_reg(struct pt_regs *regs, int nr, int val)
  22. {
  23. if (nr < 4)
  24. *(unsigned long *)(&regs->r0 + nr) = val;
  25. else if (nr < 7)
  26. *(unsigned long *)(&regs->r4 + (nr - 4)) = val;
  27. else if (nr < 13)
  28. *(unsigned long *)(&regs->r7 + (nr - 7)) = val;
  29. else
  30. *(unsigned long *)(&regs->fp + (nr - 13)) = val;
  31. }
  32. #define REG1(insn) (((insn) & 0x0f00) >> 8)
  33. #define REG2(insn) ((insn) & 0x000f)
  34. #define PSW_BC 0x100
  35. /* O- instruction */
  36. #define ISA_LD1 0x20c0 /* ld Rdest, @Rsrc */
  37. #define ISA_LD2 0x20e0 /* ld Rdest, @Rsrc+ */
  38. #define ISA_LDH 0x20a0 /* ldh Rdest, @Rsrc */
  39. #define ISA_LDUH 0x20b0 /* lduh Rdest, @Rsrc */
  40. #define ISA_ST1 0x2040 /* st Rsrc1, @Rsrc2 */
  41. #define ISA_ST2 0x2060 /* st Rsrc1, @+Rsrc2 */
  42. #define ISA_ST3 0x2070 /* st Rsrc1, @-Rsrc2 */
  43. #define ISA_STH1 0x2020 /* sth Rsrc1, @Rsrc2 */
  44. #define ISA_STH2 0x2030 /* sth Rsrc1, @Rsrc2+ */
  45. #ifdef CONFIG_ISA_DUAL_ISSUE
  46. /* OS instruction */
  47. #define ISA_ADD 0x00a0 /* add Rdest, Rsrc */
  48. #define ISA_ADDI 0x4000 /* addi Rdest, #imm8 */
  49. #define ISA_ADDX 0x0090 /* addx Rdest, Rsrc */
  50. #define ISA_AND 0x00c0 /* and Rdest, Rsrc */
  51. #define ISA_CMP 0x0040 /* cmp Rsrc1, Rsrc2 */
  52. #define ISA_CMPEQ 0x0060 /* cmpeq Rsrc1, Rsrc2 */
  53. #define ISA_CMPU 0x0050 /* cmpu Rsrc1, Rsrc2 */
  54. #define ISA_CMPZ 0x0070 /* cmpz Rsrc */
  55. #define ISA_LDI 0x6000 /* ldi Rdest, #imm8 */
  56. #define ISA_MV 0x1080 /* mv Rdest, Rsrc */
  57. #define ISA_NEG 0x0030 /* neg Rdest, Rsrc */
  58. #define ISA_NOP 0x7000 /* nop */
  59. #define ISA_NOT 0x00b0 /* not Rdest, Rsrc */
  60. #define ISA_OR 0x00e0 /* or Rdest, Rsrc */
  61. #define ISA_SUB 0x0020 /* sub Rdest, Rsrc */
  62. #define ISA_SUBX 0x0010 /* subx Rdest, Rsrc */
  63. #define ISA_XOR 0x00d0 /* xor Rdest, Rsrc */
  64. /* -S instruction */
  65. #define ISA_MUL 0x1060 /* mul Rdest, Rsrc */
  66. #define ISA_MULLO_A0 0x3010 /* mullo Rsrc1, Rsrc2, A0 */
  67. #define ISA_MULLO_A1 0x3090 /* mullo Rsrc1, Rsrc2, A1 */
  68. #define ISA_MVFACMI_A0 0x50f2 /* mvfacmi Rdest, A0 */
  69. #define ISA_MVFACMI_A1 0x50f6 /* mvfacmi Rdest, A1 */
  70. static int emu_addi(unsigned short insn, struct pt_regs *regs)
  71. {
  72. char imm = (char)(insn & 0xff);
  73. int dest = REG1(insn);
  74. int val;
  75. val = get_reg(regs, dest);
  76. val += imm;
  77. set_reg(regs, dest, val);
  78. return 0;
  79. }
  80. static int emu_ldi(unsigned short insn, struct pt_regs *regs)
  81. {
  82. char imm = (char)(insn & 0xff);
  83. set_reg(regs, REG1(insn), (int)imm);
  84. return 0;
  85. }
  86. static int emu_add(unsigned short insn, struct pt_regs *regs)
  87. {
  88. int dest = REG1(insn);
  89. int src = REG2(insn);
  90. int val;
  91. val = get_reg(regs, dest);
  92. val += get_reg(regs, src);
  93. set_reg(regs, dest, val);
  94. return 0;
  95. }
  96. static int emu_addx(unsigned short insn, struct pt_regs *regs)
  97. {
  98. int dest = REG1(insn);
  99. unsigned int val, tmp;
  100. val = regs->psw & PSW_BC ? 1 : 0;
  101. tmp = get_reg(regs, dest);
  102. val += tmp;
  103. val += (unsigned int)get_reg(regs, REG2(insn));
  104. set_reg(regs, dest, val);
  105. /* C bit set */
  106. if (val < tmp)
  107. regs->psw |= PSW_BC;
  108. else
  109. regs->psw &= ~(PSW_BC);
  110. return 0;
  111. }
  112. static int emu_and(unsigned short insn, struct pt_regs *regs)
  113. {
  114. int dest = REG1(insn);
  115. int val;
  116. val = get_reg(regs, dest);
  117. val &= get_reg(regs, REG2(insn));
  118. set_reg(regs, dest, val);
  119. return 0;
  120. }
  121. static int emu_cmp(unsigned short insn, struct pt_regs *regs)
  122. {
  123. if (get_reg(regs, REG1(insn)) < get_reg(regs, REG2(insn)))
  124. regs->psw |= PSW_BC;
  125. else
  126. regs->psw &= ~(PSW_BC);
  127. return 0;
  128. }
  129. static int emu_cmpeq(unsigned short insn, struct pt_regs *regs)
  130. {
  131. if (get_reg(regs, REG1(insn)) == get_reg(regs, REG2(insn)))
  132. regs->psw |= PSW_BC;
  133. else
  134. regs->psw &= ~(PSW_BC);
  135. return 0;
  136. }
  137. static int emu_cmpu(unsigned short insn, struct pt_regs *regs)
  138. {
  139. if ((unsigned int)get_reg(regs, REG1(insn))
  140. < (unsigned int)get_reg(regs, REG2(insn)))
  141. regs->psw |= PSW_BC;
  142. else
  143. regs->psw &= ~(PSW_BC);
  144. return 0;
  145. }
  146. static int emu_cmpz(unsigned short insn, struct pt_regs *regs)
  147. {
  148. if (!get_reg(regs, REG2(insn)))
  149. regs->psw |= PSW_BC;
  150. else
  151. regs->psw &= ~(PSW_BC);
  152. return 0;
  153. }
  154. static int emu_mv(unsigned short insn, struct pt_regs *regs)
  155. {
  156. int val;
  157. val = get_reg(regs, REG2(insn));
  158. set_reg(regs, REG1(insn), val);
  159. return 0;
  160. }
  161. static int emu_neg(unsigned short insn, struct pt_regs *regs)
  162. {
  163. int val;
  164. val = get_reg(regs, REG2(insn));
  165. set_reg(regs, REG1(insn), 0 - val);
  166. return 0;
  167. }
  168. static int emu_not(unsigned short insn, struct pt_regs *regs)
  169. {
  170. int val;
  171. val = get_reg(regs, REG2(insn));
  172. set_reg(regs, REG1(insn), ~val);
  173. return 0;
  174. }
  175. static int emu_or(unsigned short insn, struct pt_regs *regs)
  176. {
  177. int dest = REG1(insn);
  178. int val;
  179. val = get_reg(regs, dest);
  180. val |= get_reg(regs, REG2(insn));
  181. set_reg(regs, dest, val);
  182. return 0;
  183. }
  184. static int emu_sub(unsigned short insn, struct pt_regs *regs)
  185. {
  186. int dest = REG1(insn);
  187. int val;
  188. val = get_reg(regs, dest);
  189. val -= get_reg(regs, REG2(insn));
  190. set_reg(regs, dest, val);
  191. return 0;
  192. }
  193. static int emu_subx(unsigned short insn, struct pt_regs *regs)
  194. {
  195. int dest = REG1(insn);
  196. unsigned int val, tmp;
  197. val = tmp = get_reg(regs, dest);
  198. val -= (unsigned int)get_reg(regs, REG2(insn));
  199. val -= regs->psw & PSW_BC ? 1 : 0;
  200. set_reg(regs, dest, val);
  201. /* C bit set */
  202. if (val > tmp)
  203. regs->psw |= PSW_BC;
  204. else
  205. regs->psw &= ~(PSW_BC);
  206. return 0;
  207. }
  208. static int emu_xor(unsigned short insn, struct pt_regs *regs)
  209. {
  210. int dest = REG1(insn);
  211. unsigned int val;
  212. val = (unsigned int)get_reg(regs, dest);
  213. val ^= (unsigned int)get_reg(regs, REG2(insn));
  214. set_reg(regs, dest, val);
  215. return 0;
  216. }
  217. static int emu_mul(unsigned short insn, struct pt_regs *regs)
  218. {
  219. int dest = REG1(insn);
  220. int reg1, reg2;
  221. reg1 = get_reg(regs, dest);
  222. reg2 = get_reg(regs, REG2(insn));
  223. __asm__ __volatile__ (
  224. "mul %0, %1; \n\t"
  225. : "+r" (reg1) : "r" (reg2)
  226. );
  227. set_reg(regs, dest, reg1);
  228. return 0;
  229. }
  230. static int emu_mullo_a0(unsigned short insn, struct pt_regs *regs)
  231. {
  232. int reg1, reg2;
  233. reg1 = get_reg(regs, REG1(insn));
  234. reg2 = get_reg(regs, REG2(insn));
  235. __asm__ __volatile__ (
  236. "mullo %0, %1, a0; \n\t"
  237. "mvfachi %0, a0; \n\t"
  238. "mvfaclo %1, a0; \n\t"
  239. : "+r" (reg1), "+r" (reg2)
  240. );
  241. regs->acc0h = reg1;
  242. regs->acc0l = reg2;
  243. return 0;
  244. }
  245. static int emu_mullo_a1(unsigned short insn, struct pt_regs *regs)
  246. {
  247. int reg1, reg2;
  248. reg1 = get_reg(regs, REG1(insn));
  249. reg2 = get_reg(regs, REG2(insn));
  250. __asm__ __volatile__ (
  251. "mullo %0, %1, a0; \n\t"
  252. "mvfachi %0, a0; \n\t"
  253. "mvfaclo %1, a0; \n\t"
  254. : "+r" (reg1), "+r" (reg2)
  255. );
  256. regs->acc1h = reg1;
  257. regs->acc1l = reg2;
  258. return 0;
  259. }
  260. static int emu_mvfacmi_a0(unsigned short insn, struct pt_regs *regs)
  261. {
  262. unsigned long val;
  263. val = (regs->acc0h << 16) | (regs->acc0l >> 16);
  264. set_reg(regs, REG1(insn), (int)val);
  265. return 0;
  266. }
  267. static int emu_mvfacmi_a1(unsigned short insn, struct pt_regs *regs)
  268. {
  269. unsigned long val;
  270. val = (regs->acc1h << 16) | (regs->acc1l >> 16);
  271. set_reg(regs, REG1(insn), (int)val);
  272. return 0;
  273. }
  274. static int emu_m32r2(unsigned short insn, struct pt_regs *regs)
  275. {
  276. int res = -1;
  277. if ((insn & 0x7fff) == ISA_NOP) /* nop */
  278. return 0;
  279. switch(insn & 0x7000) {
  280. case ISA_ADDI: /* addi Rdest, #imm8 */
  281. res = emu_addi(insn, regs);
  282. break;
  283. case ISA_LDI: /* ldi Rdest, #imm8 */
  284. res = emu_ldi(insn, regs);
  285. break;
  286. default:
  287. break;
  288. }
  289. if (!res)
  290. return 0;
  291. switch(insn & 0x70f0) {
  292. case ISA_ADD: /* add Rdest, Rsrc */
  293. res = emu_add(insn, regs);
  294. break;
  295. case ISA_ADDX: /* addx Rdest, Rsrc */
  296. res = emu_addx(insn, regs);
  297. break;
  298. case ISA_AND: /* and Rdest, Rsrc */
  299. res = emu_and(insn, regs);
  300. break;
  301. case ISA_CMP: /* cmp Rsrc1, Rsrc2 */
  302. res = emu_cmp(insn, regs);
  303. break;
  304. case ISA_CMPEQ: /* cmpeq Rsrc1, Rsrc2 */
  305. res = emu_cmpeq(insn, regs);
  306. break;
  307. case ISA_CMPU: /* cmpu Rsrc1, Rsrc2 */
  308. res = emu_cmpu(insn, regs);
  309. break;
  310. case ISA_CMPZ: /* cmpz Rsrc */
  311. res = emu_cmpz(insn, regs);
  312. break;
  313. case ISA_MV: /* mv Rdest, Rsrc */
  314. res = emu_mv(insn, regs);
  315. break;
  316. case ISA_NEG: /* neg Rdest, Rsrc */
  317. res = emu_neg(insn, regs);
  318. break;
  319. case ISA_NOT: /* not Rdest, Rsrc */
  320. res = emu_not(insn, regs);
  321. break;
  322. case ISA_OR: /* or Rdest, Rsrc */
  323. res = emu_or(insn, regs);
  324. break;
  325. case ISA_SUB: /* sub Rdest, Rsrc */
  326. res = emu_sub(insn, regs);
  327. break;
  328. case ISA_SUBX: /* subx Rdest, Rsrc */
  329. res = emu_subx(insn, regs);
  330. break;
  331. case ISA_XOR: /* xor Rdest, Rsrc */
  332. res = emu_xor(insn, regs);
  333. break;
  334. case ISA_MUL: /* mul Rdest, Rsrc */
  335. res = emu_mul(insn, regs);
  336. break;
  337. case ISA_MULLO_A0: /* mullo Rsrc1, Rsrc2 */
  338. res = emu_mullo_a0(insn, regs);
  339. break;
  340. case ISA_MULLO_A1: /* mullo Rsrc1, Rsrc2 */
  341. res = emu_mullo_a1(insn, regs);
  342. break;
  343. default:
  344. break;
  345. }
  346. if (!res)
  347. return 0;
  348. switch(insn & 0x70ff) {
  349. case ISA_MVFACMI_A0: /* mvfacmi Rdest */
  350. res = emu_mvfacmi_a0(insn, regs);
  351. break;
  352. case ISA_MVFACMI_A1: /* mvfacmi Rdest */
  353. res = emu_mvfacmi_a1(insn, regs);
  354. break;
  355. default:
  356. break;
  357. }
  358. return res;
  359. }
  360. #endif /* CONFIG_ISA_DUAL_ISSUE */
  361. /*
  362. * ld : ?010 dest 1100 src
  363. * 0010 dest 1110 src : ld Rdest, @Rsrc+
  364. * ldh : ?010 dest 1010 src
  365. * lduh : ?010 dest 1011 src
  366. * st : ?010 src1 0100 src2
  367. * 0010 src1 0110 src2 : st Rsrc1, @+Rsrc2
  368. * 0010 src1 0111 src2 : st Rsrc1, @-Rsrc2
  369. * sth : ?010 src1 0010 src2
  370. */
  371. static int insn_check(unsigned long insn, struct pt_regs *regs,
  372. unsigned char **ucp)
  373. {
  374. int res = 0;
  375. /*
  376. * 32bit insn
  377. * ld Rdest, @(disp16, Rsrc)
  378. * st Rdest, @(disp16, Rsrc)
  379. */
  380. if (insn & 0x80000000) { /* 32bit insn */
  381. *ucp += (short)(insn & 0x0000ffff);
  382. regs->bpc += 4;
  383. } else { /* 16bit insn */
  384. #ifdef CONFIG_ISA_DUAL_ISSUE
  385. /* parallel exec check */
  386. if (!(regs->bpc & 0x2) && insn & 0x8000) {
  387. res = emu_m32r2((unsigned short)insn, regs);
  388. regs->bpc += 4;
  389. } else
  390. #endif /* CONFIG_ISA_DUAL_ISSUE */
  391. regs->bpc += 2;
  392. }
  393. return res;
  394. }
  395. static int emu_ld(unsigned long insn32, struct pt_regs *regs)
  396. {
  397. unsigned char *ucp;
  398. unsigned long val;
  399. unsigned short insn16;
  400. int size, src;
  401. insn16 = insn32 >> 16;
  402. src = REG2(insn16);
  403. ucp = (unsigned char *)get_reg(regs, src);
  404. if (insn_check(insn32, regs, &ucp))
  405. return -1;
  406. size = insn16 & 0x0040 ? 4 : 2;
  407. if (copy_from_user(&val, ucp, size))
  408. return -1;
  409. if (size == 2)
  410. val >>= 16;
  411. /* ldh sign check */
  412. if ((insn16 & 0x00f0) == 0x00a0 && (val & 0x8000))
  413. val |= 0xffff0000;
  414. set_reg(regs, REG1(insn16), val);
  415. /* ld increment check */
  416. if ((insn16 & 0xf0f0) == ISA_LD2) /* ld Rdest, @Rsrc+ */
  417. set_reg(regs, src, (unsigned long)(ucp + 4));
  418. return 0;
  419. }
  420. static int emu_st(unsigned long insn32, struct pt_regs *regs)
  421. {
  422. unsigned char *ucp;
  423. unsigned long val;
  424. unsigned short insn16;
  425. int size, src2;
  426. insn16 = insn32 >> 16;
  427. src2 = REG2(insn16);
  428. ucp = (unsigned char *)get_reg(regs, src2);
  429. if (insn_check(insn32, regs, &ucp))
  430. return -1;
  431. size = insn16 & 0x0040 ? 4 : 2;
  432. val = get_reg(regs, REG1(insn16));
  433. if (size == 2)
  434. val <<= 16;
  435. /* st inc/dec check */
  436. if ((insn16 & 0xf0e0) == 0x2060) {
  437. if (insn16 & 0x0010)
  438. ucp -= 4;
  439. else
  440. ucp += 4;
  441. set_reg(regs, src2, (unsigned long)ucp);
  442. }
  443. if (copy_to_user(ucp, &val, size))
  444. return -1;
  445. /* sth inc check */
  446. if ((insn16 & 0xf0f0) == ISA_STH2) {
  447. ucp += 2;
  448. set_reg(regs, src2, (unsigned long)ucp);
  449. }
  450. return 0;
  451. }
  452. int handle_unaligned_access(unsigned long insn32, struct pt_regs *regs)
  453. {
  454. unsigned short insn16;
  455. int res;
  456. insn16 = insn32 >> 16;
  457. /* ld or st check */
  458. if ((insn16 & 0x7000) != 0x2000)
  459. return -1;
  460. /* insn alignment check */
  461. if ((insn16 & 0x8000) && (regs->bpc & 3))
  462. return -1;
  463. if (insn16 & 0x0080) /* ld */
  464. res = emu_ld(insn32, regs);
  465. else /* st */
  466. res = emu_st(insn32, regs);
  467. return res;
  468. }