alignment.c 13 KB

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