alignment.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * linux/arch/unicore32/mm/alignment.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * TODO:
  14. * FPU ldm/stm not handling
  15. */
  16. #include <linux/compiler.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/init.h>
  21. #include <linux/sched.h>
  22. #include <linux/uaccess.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/unaligned.h>
  25. #define CODING_BITS(i) (i & 0xe0000120)
  26. #define LDST_P_BIT(i) (i & (1 << 28)) /* Preindex */
  27. #define LDST_U_BIT(i) (i & (1 << 27)) /* Add offset */
  28. #define LDST_W_BIT(i) (i & (1 << 25)) /* Writeback */
  29. #define LDST_L_BIT(i) (i & (1 << 24)) /* Load */
  30. #define LDST_P_EQ_U(i) ((((i) ^ ((i) >> 1)) & (1 << 27)) == 0)
  31. #define LDSTH_I_BIT(i) (i & (1 << 26)) /* half-word immed */
  32. #define LDM_S_BIT(i) (i & (1 << 26)) /* write ASR from BSR */
  33. #define LDM_H_BIT(i) (i & (1 << 6)) /* select r0-r15 or r16-r31 */
  34. #define RN_BITS(i) ((i >> 19) & 31) /* Rn */
  35. #define RD_BITS(i) ((i >> 14) & 31) /* Rd */
  36. #define RM_BITS(i) (i & 31) /* Rm */
  37. #define REGMASK_BITS(i) (((i & 0x7fe00) >> 3) | (i & 0x3f))
  38. #define OFFSET_BITS(i) (i & 0x03fff)
  39. #define SHIFT_BITS(i) ((i >> 9) & 0x1f)
  40. #define SHIFT_TYPE(i) (i & 0xc0)
  41. #define SHIFT_LSL 0x00
  42. #define SHIFT_LSR 0x40
  43. #define SHIFT_ASR 0x80
  44. #define SHIFT_RORRRX 0xc0
  45. union offset_union {
  46. unsigned long un;
  47. signed long sn;
  48. };
  49. #define TYPE_ERROR 0
  50. #define TYPE_FAULT 1
  51. #define TYPE_LDST 2
  52. #define TYPE_DONE 3
  53. #define TYPE_SWAP 4
  54. #define TYPE_COLS 5 /* Coprocessor load/store */
  55. #define get8_unaligned_check(val, addr, err) \
  56. __asm__( \
  57. "1: ldb.u %1, [%2], #1\n" \
  58. "2:\n" \
  59. " .pushsection .fixup,\"ax\"\n" \
  60. " .align 2\n" \
  61. "3: mov %0, #1\n" \
  62. " b 2b\n" \
  63. " .popsection\n" \
  64. " .pushsection __ex_table,\"a\"\n" \
  65. " .align 3\n" \
  66. " .long 1b, 3b\n" \
  67. " .popsection\n" \
  68. : "=r" (err), "=&r" (val), "=r" (addr) \
  69. : "0" (err), "2" (addr))
  70. #define get8t_unaligned_check(val, addr, err) \
  71. __asm__( \
  72. "1: ldb.u %1, [%2], #1\n" \
  73. "2:\n" \
  74. " .pushsection .fixup,\"ax\"\n" \
  75. " .align 2\n" \
  76. "3: mov %0, #1\n" \
  77. " b 2b\n" \
  78. " .popsection\n" \
  79. " .pushsection __ex_table,\"a\"\n" \
  80. " .align 3\n" \
  81. " .long 1b, 3b\n" \
  82. " .popsection\n" \
  83. : "=r" (err), "=&r" (val), "=r" (addr) \
  84. : "0" (err), "2" (addr))
  85. #define get16_unaligned_check(val, addr) \
  86. do { \
  87. unsigned int err = 0, v, a = addr; \
  88. get8_unaligned_check(val, a, err); \
  89. get8_unaligned_check(v, a, err); \
  90. val |= v << 8; \
  91. if (err) \
  92. goto fault; \
  93. } while (0)
  94. #define put16_unaligned_check(val, addr) \
  95. do { \
  96. unsigned int err = 0, v = val, a = addr; \
  97. __asm__( \
  98. "1: stb.u %1, [%2], #1\n" \
  99. " mov %1, %1 >> #8\n" \
  100. "2: stb.u %1, [%2]\n" \
  101. "3:\n" \
  102. " .pushsection .fixup,\"ax\"\n" \
  103. " .align 2\n" \
  104. "4: mov %0, #1\n" \
  105. " b 3b\n" \
  106. " .popsection\n" \
  107. " .pushsection __ex_table,\"a\"\n" \
  108. " .align 3\n" \
  109. " .long 1b, 4b\n" \
  110. " .long 2b, 4b\n" \
  111. " .popsection\n" \
  112. : "=r" (err), "=&r" (v), "=&r" (a) \
  113. : "0" (err), "1" (v), "2" (a)); \
  114. if (err) \
  115. goto fault; \
  116. } while (0)
  117. #define __put32_unaligned_check(ins, val, addr) \
  118. do { \
  119. unsigned int err = 0, v = val, a = addr; \
  120. __asm__( \
  121. "1: "ins" %1, [%2], #1\n" \
  122. " mov %1, %1 >> #8\n" \
  123. "2: "ins" %1, [%2], #1\n" \
  124. " mov %1, %1 >> #8\n" \
  125. "3: "ins" %1, [%2], #1\n" \
  126. " mov %1, %1 >> #8\n" \
  127. "4: "ins" %1, [%2]\n" \
  128. "5:\n" \
  129. " .pushsection .fixup,\"ax\"\n" \
  130. " .align 2\n" \
  131. "6: mov %0, #1\n" \
  132. " b 5b\n" \
  133. " .popsection\n" \
  134. " .pushsection __ex_table,\"a\"\n" \
  135. " .align 3\n" \
  136. " .long 1b, 6b\n" \
  137. " .long 2b, 6b\n" \
  138. " .long 3b, 6b\n" \
  139. " .long 4b, 6b\n" \
  140. " .popsection\n" \
  141. : "=r" (err), "=&r" (v), "=&r" (a) \
  142. : "0" (err), "1" (v), "2" (a)); \
  143. if (err) \
  144. goto fault; \
  145. } while (0)
  146. #define get32_unaligned_check(val, addr) \
  147. do { \
  148. unsigned int err = 0, v, a = addr; \
  149. get8_unaligned_check(val, a, err); \
  150. get8_unaligned_check(v, a, err); \
  151. val |= v << 8; \
  152. get8_unaligned_check(v, a, err); \
  153. val |= v << 16; \
  154. get8_unaligned_check(v, a, err); \
  155. val |= v << 24; \
  156. if (err) \
  157. goto fault; \
  158. } while (0)
  159. #define put32_unaligned_check(val, addr) \
  160. __put32_unaligned_check("stb.u", val, addr)
  161. #define get32t_unaligned_check(val, addr) \
  162. do { \
  163. unsigned int err = 0, v, a = addr; \
  164. get8t_unaligned_check(val, a, err); \
  165. get8t_unaligned_check(v, a, err); \
  166. val |= v << 8; \
  167. get8t_unaligned_check(v, a, err); \
  168. val |= v << 16; \
  169. get8t_unaligned_check(v, a, err); \
  170. val |= v << 24; \
  171. if (err) \
  172. goto fault; \
  173. } while (0)
  174. #define put32t_unaligned_check(val, addr) \
  175. __put32_unaligned_check("stb.u", val, addr)
  176. static void
  177. do_alignment_finish_ldst(unsigned long addr, unsigned long instr,
  178. struct pt_regs *regs, union offset_union offset)
  179. {
  180. if (!LDST_U_BIT(instr))
  181. offset.un = -offset.un;
  182. if (!LDST_P_BIT(instr))
  183. addr += offset.un;
  184. if (!LDST_P_BIT(instr) || LDST_W_BIT(instr))
  185. regs->uregs[RN_BITS(instr)] = addr;
  186. }
  187. static int
  188. do_alignment_ldrhstrh(unsigned long addr, unsigned long instr,
  189. struct pt_regs *regs)
  190. {
  191. unsigned int rd = RD_BITS(instr);
  192. /* old value 0x40002120, can't judge swap instr correctly */
  193. if ((instr & 0x4b003fe0) == 0x40000120)
  194. goto swp;
  195. if (LDST_L_BIT(instr)) {
  196. unsigned long val;
  197. get16_unaligned_check(val, addr);
  198. /* signed half-word? */
  199. if (instr & 0x80)
  200. val = (signed long)((signed short)val);
  201. regs->uregs[rd] = val;
  202. } else
  203. put16_unaligned_check(regs->uregs[rd], addr);
  204. return TYPE_LDST;
  205. swp:
  206. /* only handle swap word
  207. * for swap byte should not active this alignment exception */
  208. get32_unaligned_check(regs->uregs[RD_BITS(instr)], addr);
  209. put32_unaligned_check(regs->uregs[RM_BITS(instr)], addr);
  210. return TYPE_SWAP;
  211. fault:
  212. return TYPE_FAULT;
  213. }
  214. static int
  215. do_alignment_ldrstr(unsigned long addr, unsigned long instr,
  216. struct pt_regs *regs)
  217. {
  218. unsigned int rd = RD_BITS(instr);
  219. if (!LDST_P_BIT(instr) && LDST_W_BIT(instr))
  220. goto trans;
  221. if (LDST_L_BIT(instr))
  222. get32_unaligned_check(regs->uregs[rd], addr);
  223. else
  224. put32_unaligned_check(regs->uregs[rd], addr);
  225. return TYPE_LDST;
  226. trans:
  227. if (LDST_L_BIT(instr))
  228. get32t_unaligned_check(regs->uregs[rd], addr);
  229. else
  230. put32t_unaligned_check(regs->uregs[rd], addr);
  231. return TYPE_LDST;
  232. fault:
  233. return TYPE_FAULT;
  234. }
  235. /*
  236. * LDM/STM alignment handler.
  237. *
  238. * There are 4 variants of this instruction:
  239. *
  240. * B = rn pointer before instruction, A = rn pointer after instruction
  241. * ------ increasing address ----->
  242. * | | r0 | r1 | ... | rx | |
  243. * PU = 01 B A
  244. * PU = 11 B A
  245. * PU = 00 A B
  246. * PU = 10 A B
  247. */
  248. static int
  249. do_alignment_ldmstm(unsigned long addr, unsigned long instr,
  250. struct pt_regs *regs)
  251. {
  252. unsigned int rd, rn, pc_correction, reg_correction, nr_regs, regbits;
  253. unsigned long eaddr, newaddr;
  254. if (LDM_S_BIT(instr))
  255. goto bad;
  256. pc_correction = 4; /* processor implementation defined */
  257. /* count the number of registers in the mask to be transferred */
  258. nr_regs = hweight16(REGMASK_BITS(instr)) * 4;
  259. rn = RN_BITS(instr);
  260. newaddr = eaddr = regs->uregs[rn];
  261. if (!LDST_U_BIT(instr))
  262. nr_regs = -nr_regs;
  263. newaddr += nr_regs;
  264. if (!LDST_U_BIT(instr))
  265. eaddr = newaddr;
  266. if (LDST_P_EQ_U(instr)) /* U = P */
  267. eaddr += 4;
  268. /*
  269. * This is a "hint" - we already have eaddr worked out by the
  270. * processor for us.
  271. */
  272. if (addr != eaddr) {
  273. printk(KERN_ERR "LDMSTM: PC = %08lx, instr = %08lx, "
  274. "addr = %08lx, eaddr = %08lx\n",
  275. instruction_pointer(regs), instr, addr, eaddr);
  276. show_regs(regs);
  277. }
  278. if (LDM_H_BIT(instr))
  279. reg_correction = 0x10;
  280. else
  281. reg_correction = 0x00;
  282. for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
  283. regbits >>= 1, rd += 1)
  284. if (regbits & 1) {
  285. if (LDST_L_BIT(instr))
  286. get32_unaligned_check(regs->
  287. uregs[rd + reg_correction], eaddr);
  288. else
  289. put32_unaligned_check(regs->
  290. uregs[rd + reg_correction], eaddr);
  291. eaddr += 4;
  292. }
  293. if (LDST_W_BIT(instr))
  294. regs->uregs[rn] = newaddr;
  295. return TYPE_DONE;
  296. fault:
  297. regs->UCreg_pc -= pc_correction;
  298. return TYPE_FAULT;
  299. bad:
  300. printk(KERN_ERR "Alignment trap: not handling ldm with s-bit set\n");
  301. return TYPE_ERROR;
  302. }
  303. static int
  304. do_alignment(unsigned long addr, unsigned int error_code, struct pt_regs *regs)
  305. {
  306. union offset_union offset;
  307. unsigned long instr, instrptr;
  308. int (*handler) (unsigned long addr, unsigned long instr,
  309. struct pt_regs *regs);
  310. unsigned int type;
  311. instrptr = instruction_pointer(regs);
  312. if (instrptr >= PAGE_OFFSET)
  313. instr = *(unsigned long *)instrptr;
  314. else {
  315. __asm__ __volatile__(
  316. "ldw.u %0, [%1]\n"
  317. : "=&r"(instr)
  318. : "r"(instrptr));
  319. }
  320. regs->UCreg_pc += 4;
  321. switch (CODING_BITS(instr)) {
  322. case 0x40000120: /* ldrh or strh */
  323. if (LDSTH_I_BIT(instr))
  324. offset.un = (instr & 0x3e00) >> 4 | (instr & 31);
  325. else
  326. offset.un = regs->uregs[RM_BITS(instr)];
  327. handler = do_alignment_ldrhstrh;
  328. break;
  329. case 0x60000000: /* ldr or str immediate */
  330. case 0x60000100: /* ldr or str immediate */
  331. case 0x60000020: /* ldr or str immediate */
  332. case 0x60000120: /* ldr or str immediate */
  333. offset.un = OFFSET_BITS(instr);
  334. handler = do_alignment_ldrstr;
  335. break;
  336. case 0x40000000: /* ldr or str register */
  337. offset.un = regs->uregs[RM_BITS(instr)];
  338. {
  339. unsigned int shiftval = SHIFT_BITS(instr);
  340. switch (SHIFT_TYPE(instr)) {
  341. case SHIFT_LSL:
  342. offset.un <<= shiftval;
  343. break;
  344. case SHIFT_LSR:
  345. offset.un >>= shiftval;
  346. break;
  347. case SHIFT_ASR:
  348. offset.sn >>= shiftval;
  349. break;
  350. case SHIFT_RORRRX:
  351. if (shiftval == 0) {
  352. offset.un >>= 1;
  353. if (regs->UCreg_asr & PSR_C_BIT)
  354. offset.un |= 1 << 31;
  355. } else
  356. offset.un = offset.un >> shiftval |
  357. offset.un << (32 - shiftval);
  358. break;
  359. }
  360. }
  361. handler = do_alignment_ldrstr;
  362. break;
  363. case 0x80000000: /* ldm or stm */
  364. case 0x80000020: /* ldm or stm */
  365. handler = do_alignment_ldmstm;
  366. break;
  367. default:
  368. goto bad;
  369. }
  370. type = handler(addr, instr, regs);
  371. if (type == TYPE_ERROR || type == TYPE_FAULT)
  372. goto bad_or_fault;
  373. if (type == TYPE_LDST)
  374. do_alignment_finish_ldst(addr, instr, regs, offset);
  375. return 0;
  376. bad_or_fault:
  377. if (type == TYPE_ERROR)
  378. goto bad;
  379. regs->UCreg_pc -= 4;
  380. /*
  381. * We got a fault - fix it up, or die.
  382. */
  383. do_bad_area(addr, error_code, regs);
  384. return 0;
  385. bad:
  386. /*
  387. * Oops, we didn't handle the instruction.
  388. * However, we must handle fpu instr firstly.
  389. */
  390. #ifdef CONFIG_UNICORE_FPU_F64
  391. /* handle co.load/store */
  392. #define CODING_COLS 0xc0000000
  393. #define COLS_OFFSET_BITS(i) (i & 0x1FF)
  394. #define COLS_L_BITS(i) (i & (1<<24))
  395. #define COLS_FN_BITS(i) ((i>>14) & 31)
  396. if ((instr & 0xe0000000) == CODING_COLS) {
  397. unsigned int fn = COLS_FN_BITS(instr);
  398. unsigned long val = 0;
  399. if (COLS_L_BITS(instr)) {
  400. get32t_unaligned_check(val, addr);
  401. switch (fn) {
  402. #define ASM_MTF(n) case n: \
  403. __asm__ __volatile__("MTF %0, F" __stringify(n) \
  404. : : "r"(val)); \
  405. break;
  406. ASM_MTF(0); ASM_MTF(1); ASM_MTF(2); ASM_MTF(3);
  407. ASM_MTF(4); ASM_MTF(5); ASM_MTF(6); ASM_MTF(7);
  408. ASM_MTF(8); ASM_MTF(9); ASM_MTF(10); ASM_MTF(11);
  409. ASM_MTF(12); ASM_MTF(13); ASM_MTF(14); ASM_MTF(15);
  410. ASM_MTF(16); ASM_MTF(17); ASM_MTF(18); ASM_MTF(19);
  411. ASM_MTF(20); ASM_MTF(21); ASM_MTF(22); ASM_MTF(23);
  412. ASM_MTF(24); ASM_MTF(25); ASM_MTF(26); ASM_MTF(27);
  413. ASM_MTF(28); ASM_MTF(29); ASM_MTF(30); ASM_MTF(31);
  414. #undef ASM_MTF
  415. }
  416. } else {
  417. switch (fn) {
  418. #define ASM_MFF(n) case n: \
  419. __asm__ __volatile__("MFF %0, F" __stringify(n) \
  420. : : "r"(val)); \
  421. break;
  422. ASM_MFF(0); ASM_MFF(1); ASM_MFF(2); ASM_MFF(3);
  423. ASM_MFF(4); ASM_MFF(5); ASM_MFF(6); ASM_MFF(7);
  424. ASM_MFF(8); ASM_MFF(9); ASM_MFF(10); ASM_MFF(11);
  425. ASM_MFF(12); ASM_MFF(13); ASM_MFF(14); ASM_MFF(15);
  426. ASM_MFF(16); ASM_MFF(17); ASM_MFF(18); ASM_MFF(19);
  427. ASM_MFF(20); ASM_MFF(21); ASM_MFF(22); ASM_MFF(23);
  428. ASM_MFF(24); ASM_MFF(25); ASM_MFF(26); ASM_MFF(27);
  429. ASM_MFF(28); ASM_MFF(29); ASM_MFF(30); ASM_MFF(31);
  430. #undef ASM_MFF
  431. }
  432. put32t_unaligned_check(val, addr);
  433. }
  434. return TYPE_COLS;
  435. }
  436. fault:
  437. return TYPE_FAULT;
  438. #endif
  439. printk(KERN_ERR "Alignment trap: not handling instruction "
  440. "%08lx at [<%08lx>]\n", instr, instrptr);
  441. return 1;
  442. }
  443. /*
  444. * This needs to be done after sysctl_init, otherwise sys/ will be
  445. * overwritten. Actually, this shouldn't be in sys/ at all since
  446. * it isn't a sysctl, and it doesn't contain sysctl information.
  447. */
  448. static int __init alignment_init(void)
  449. {
  450. hook_fault_code(1, do_alignment, SIGBUS, BUS_ADRALN,
  451. "alignment exception");
  452. return 0;
  453. }
  454. fs_initcall(alignment_init);