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