alignment.c 13 KB

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