unaligned.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * Handle unaligned accesses by emulation.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1996, 1998, 1999, 2002 by Ralf Baechle
  9. * Copyright (C) 1999 Silicon Graphics, Inc.
  10. *
  11. * This file contains exception handler for address error exception with the
  12. * special capability to execute faulting instructions in software. The
  13. * handler does not try to handle the case when the program counter points
  14. * to an address not aligned to a word boundary.
  15. *
  16. * Putting data to unaligned addresses is a bad practice even on Intel where
  17. * only the performance is affected. Much worse is that such code is non-
  18. * portable. Due to several programs that die on MIPS due to alignment
  19. * problems I decided to implement this handler anyway though I originally
  20. * didn't intend to do this at all for user code.
  21. *
  22. * For now I enable fixing of address errors by default to make life easier.
  23. * I however intend to disable this somewhen in the future when the alignment
  24. * problems with user programs have been fixed. For programmers this is the
  25. * right way to go.
  26. *
  27. * Fixing address errors is a per process option. The option is inherited
  28. * across fork(2) and execve(2) calls. If you really want to use the
  29. * option in your user programs - I discourage the use of the software
  30. * emulation strongly - use the following code in your userland stuff:
  31. *
  32. * #include <sys/sysmips.h>
  33. *
  34. * ...
  35. * sysmips(MIPS_FIXADE, x);
  36. * ...
  37. *
  38. * The argument x is 0 for disabling software emulation, enabled otherwise.
  39. *
  40. * Below a little program to play around with this feature.
  41. *
  42. * #include <stdio.h>
  43. * #include <sys/sysmips.h>
  44. *
  45. * struct foo {
  46. * unsigned char bar[8];
  47. * };
  48. *
  49. * main(int argc, char *argv[])
  50. * {
  51. * struct foo x = {0, 1, 2, 3, 4, 5, 6, 7};
  52. * unsigned int *p = (unsigned int *) (x.bar + 3);
  53. * int i;
  54. *
  55. * if (argc > 1)
  56. * sysmips(MIPS_FIXADE, atoi(argv[1]));
  57. *
  58. * printf("*p = %08lx\n", *p);
  59. *
  60. * *p = 0xdeadface;
  61. *
  62. * for(i = 0; i <= 7; i++)
  63. * printf("%02x ", x.bar[i]);
  64. * printf("\n");
  65. * }
  66. *
  67. * Coprocessor loads are not supported; I think this case is unimportant
  68. * in the practice.
  69. *
  70. * TODO: Handle ndc (attempted store to doubleword in uncached memory)
  71. * exception for the R6000.
  72. * A store crossing a page boundary might be executed only partially.
  73. * Undo the partial store in this case.
  74. */
  75. #include <linux/mm.h>
  76. #include <linux/module.h>
  77. #include <linux/signal.h>
  78. #include <linux/smp.h>
  79. #include <linux/sched.h>
  80. #include <linux/debugfs.h>
  81. #include <asm/asm.h>
  82. #include <asm/branch.h>
  83. #include <asm/byteorder.h>
  84. #include <asm/cop2.h>
  85. #include <asm/inst.h>
  86. #include <asm/uaccess.h>
  87. #include <asm/system.h>
  88. #define STR(x) __STR(x)
  89. #define __STR(x) #x
  90. enum {
  91. UNALIGNED_ACTION_QUIET,
  92. UNALIGNED_ACTION_SIGNAL,
  93. UNALIGNED_ACTION_SHOW,
  94. };
  95. #ifdef CONFIG_DEBUG_FS
  96. static u32 unaligned_instructions;
  97. static u32 unaligned_action;
  98. #else
  99. #define unaligned_action UNALIGNED_ACTION_QUIET
  100. #endif
  101. extern void show_registers(struct pt_regs *regs);
  102. static void emulate_load_store_insn(struct pt_regs *regs,
  103. void __user *addr, unsigned int __user *pc)
  104. {
  105. union mips_instruction insn;
  106. unsigned long value;
  107. unsigned int res;
  108. regs->regs[0] = 0;
  109. /*
  110. * This load never faults.
  111. */
  112. __get_user(insn.word, pc);
  113. switch (insn.i_format.opcode) {
  114. /*
  115. * These are instructions that a compiler doesn't generate. We
  116. * can assume therefore that the code is MIPS-aware and
  117. * really buggy. Emulating these instructions would break the
  118. * semantics anyway.
  119. */
  120. case ll_op:
  121. case lld_op:
  122. case sc_op:
  123. case scd_op:
  124. /*
  125. * For these instructions the only way to create an address
  126. * error is an attempted access to kernel/supervisor address
  127. * space.
  128. */
  129. case ldl_op:
  130. case ldr_op:
  131. case lwl_op:
  132. case lwr_op:
  133. case sdl_op:
  134. case sdr_op:
  135. case swl_op:
  136. case swr_op:
  137. case lb_op:
  138. case lbu_op:
  139. case sb_op:
  140. goto sigbus;
  141. /*
  142. * The remaining opcodes are the ones that are really of interest.
  143. */
  144. case lh_op:
  145. if (!access_ok(VERIFY_READ, addr, 2))
  146. goto sigbus;
  147. __asm__ __volatile__ (".set\tnoat\n"
  148. #ifdef __BIG_ENDIAN
  149. "1:\tlb\t%0, 0(%2)\n"
  150. "2:\tlbu\t$1, 1(%2)\n\t"
  151. #endif
  152. #ifdef __LITTLE_ENDIAN
  153. "1:\tlb\t%0, 1(%2)\n"
  154. "2:\tlbu\t$1, 0(%2)\n\t"
  155. #endif
  156. "sll\t%0, 0x8\n\t"
  157. "or\t%0, $1\n\t"
  158. "li\t%1, 0\n"
  159. "3:\t.set\tat\n\t"
  160. ".section\t.fixup,\"ax\"\n\t"
  161. "4:\tli\t%1, %3\n\t"
  162. "j\t3b\n\t"
  163. ".previous\n\t"
  164. ".section\t__ex_table,\"a\"\n\t"
  165. STR(PTR)"\t1b, 4b\n\t"
  166. STR(PTR)"\t2b, 4b\n\t"
  167. ".previous"
  168. : "=&r" (value), "=r" (res)
  169. : "r" (addr), "i" (-EFAULT));
  170. if (res)
  171. goto fault;
  172. compute_return_epc(regs);
  173. regs->regs[insn.i_format.rt] = value;
  174. break;
  175. case lw_op:
  176. if (!access_ok(VERIFY_READ, addr, 4))
  177. goto sigbus;
  178. __asm__ __volatile__ (
  179. #ifdef __BIG_ENDIAN
  180. "1:\tlwl\t%0, (%2)\n"
  181. "2:\tlwr\t%0, 3(%2)\n\t"
  182. #endif
  183. #ifdef __LITTLE_ENDIAN
  184. "1:\tlwl\t%0, 3(%2)\n"
  185. "2:\tlwr\t%0, (%2)\n\t"
  186. #endif
  187. "li\t%1, 0\n"
  188. "3:\t.section\t.fixup,\"ax\"\n\t"
  189. "4:\tli\t%1, %3\n\t"
  190. "j\t3b\n\t"
  191. ".previous\n\t"
  192. ".section\t__ex_table,\"a\"\n\t"
  193. STR(PTR)"\t1b, 4b\n\t"
  194. STR(PTR)"\t2b, 4b\n\t"
  195. ".previous"
  196. : "=&r" (value), "=r" (res)
  197. : "r" (addr), "i" (-EFAULT));
  198. if (res)
  199. goto fault;
  200. compute_return_epc(regs);
  201. regs->regs[insn.i_format.rt] = value;
  202. break;
  203. case lhu_op:
  204. if (!access_ok(VERIFY_READ, addr, 2))
  205. goto sigbus;
  206. __asm__ __volatile__ (
  207. ".set\tnoat\n"
  208. #ifdef __BIG_ENDIAN
  209. "1:\tlbu\t%0, 0(%2)\n"
  210. "2:\tlbu\t$1, 1(%2)\n\t"
  211. #endif
  212. #ifdef __LITTLE_ENDIAN
  213. "1:\tlbu\t%0, 1(%2)\n"
  214. "2:\tlbu\t$1, 0(%2)\n\t"
  215. #endif
  216. "sll\t%0, 0x8\n\t"
  217. "or\t%0, $1\n\t"
  218. "li\t%1, 0\n"
  219. "3:\t.set\tat\n\t"
  220. ".section\t.fixup,\"ax\"\n\t"
  221. "4:\tli\t%1, %3\n\t"
  222. "j\t3b\n\t"
  223. ".previous\n\t"
  224. ".section\t__ex_table,\"a\"\n\t"
  225. STR(PTR)"\t1b, 4b\n\t"
  226. STR(PTR)"\t2b, 4b\n\t"
  227. ".previous"
  228. : "=&r" (value), "=r" (res)
  229. : "r" (addr), "i" (-EFAULT));
  230. if (res)
  231. goto fault;
  232. compute_return_epc(regs);
  233. regs->regs[insn.i_format.rt] = value;
  234. break;
  235. case lwu_op:
  236. #ifdef CONFIG_64BIT
  237. /*
  238. * A 32-bit kernel might be running on a 64-bit processor. But
  239. * if we're on a 32-bit processor and an i-cache incoherency
  240. * or race makes us see a 64-bit instruction here the sdl/sdr
  241. * would blow up, so for now we don't handle unaligned 64-bit
  242. * instructions on 32-bit kernels.
  243. */
  244. if (!access_ok(VERIFY_READ, addr, 4))
  245. goto sigbus;
  246. __asm__ __volatile__ (
  247. #ifdef __BIG_ENDIAN
  248. "1:\tlwl\t%0, (%2)\n"
  249. "2:\tlwr\t%0, 3(%2)\n\t"
  250. #endif
  251. #ifdef __LITTLE_ENDIAN
  252. "1:\tlwl\t%0, 3(%2)\n"
  253. "2:\tlwr\t%0, (%2)\n\t"
  254. #endif
  255. "dsll\t%0, %0, 32\n\t"
  256. "dsrl\t%0, %0, 32\n\t"
  257. "li\t%1, 0\n"
  258. "3:\t.section\t.fixup,\"ax\"\n\t"
  259. "4:\tli\t%1, %3\n\t"
  260. "j\t3b\n\t"
  261. ".previous\n\t"
  262. ".section\t__ex_table,\"a\"\n\t"
  263. STR(PTR)"\t1b, 4b\n\t"
  264. STR(PTR)"\t2b, 4b\n\t"
  265. ".previous"
  266. : "=&r" (value), "=r" (res)
  267. : "r" (addr), "i" (-EFAULT));
  268. if (res)
  269. goto fault;
  270. compute_return_epc(regs);
  271. regs->regs[insn.i_format.rt] = value;
  272. break;
  273. #endif /* CONFIG_64BIT */
  274. /* Cannot handle 64-bit instructions in 32-bit kernel */
  275. goto sigill;
  276. case ld_op:
  277. #ifdef CONFIG_64BIT
  278. /*
  279. * A 32-bit kernel might be running on a 64-bit processor. But
  280. * if we're on a 32-bit processor and an i-cache incoherency
  281. * or race makes us see a 64-bit instruction here the sdl/sdr
  282. * would blow up, so for now we don't handle unaligned 64-bit
  283. * instructions on 32-bit kernels.
  284. */
  285. if (!access_ok(VERIFY_READ, addr, 8))
  286. goto sigbus;
  287. __asm__ __volatile__ (
  288. #ifdef __BIG_ENDIAN
  289. "1:\tldl\t%0, (%2)\n"
  290. "2:\tldr\t%0, 7(%2)\n\t"
  291. #endif
  292. #ifdef __LITTLE_ENDIAN
  293. "1:\tldl\t%0, 7(%2)\n"
  294. "2:\tldr\t%0, (%2)\n\t"
  295. #endif
  296. "li\t%1, 0\n"
  297. "3:\t.section\t.fixup,\"ax\"\n\t"
  298. "4:\tli\t%1, %3\n\t"
  299. "j\t3b\n\t"
  300. ".previous\n\t"
  301. ".section\t__ex_table,\"a\"\n\t"
  302. STR(PTR)"\t1b, 4b\n\t"
  303. STR(PTR)"\t2b, 4b\n\t"
  304. ".previous"
  305. : "=&r" (value), "=r" (res)
  306. : "r" (addr), "i" (-EFAULT));
  307. if (res)
  308. goto fault;
  309. compute_return_epc(regs);
  310. regs->regs[insn.i_format.rt] = value;
  311. break;
  312. #endif /* CONFIG_64BIT */
  313. /* Cannot handle 64-bit instructions in 32-bit kernel */
  314. goto sigill;
  315. case sh_op:
  316. if (!access_ok(VERIFY_WRITE, addr, 2))
  317. goto sigbus;
  318. value = regs->regs[insn.i_format.rt];
  319. __asm__ __volatile__ (
  320. #ifdef __BIG_ENDIAN
  321. ".set\tnoat\n"
  322. "1:\tsb\t%1, 1(%2)\n\t"
  323. "srl\t$1, %1, 0x8\n"
  324. "2:\tsb\t$1, 0(%2)\n\t"
  325. ".set\tat\n\t"
  326. #endif
  327. #ifdef __LITTLE_ENDIAN
  328. ".set\tnoat\n"
  329. "1:\tsb\t%1, 0(%2)\n\t"
  330. "srl\t$1,%1, 0x8\n"
  331. "2:\tsb\t$1, 1(%2)\n\t"
  332. ".set\tat\n\t"
  333. #endif
  334. "li\t%0, 0\n"
  335. "3:\n\t"
  336. ".section\t.fixup,\"ax\"\n\t"
  337. "4:\tli\t%0, %3\n\t"
  338. "j\t3b\n\t"
  339. ".previous\n\t"
  340. ".section\t__ex_table,\"a\"\n\t"
  341. STR(PTR)"\t1b, 4b\n\t"
  342. STR(PTR)"\t2b, 4b\n\t"
  343. ".previous"
  344. : "=r" (res)
  345. : "r" (value), "r" (addr), "i" (-EFAULT));
  346. if (res)
  347. goto fault;
  348. compute_return_epc(regs);
  349. break;
  350. case sw_op:
  351. if (!access_ok(VERIFY_WRITE, addr, 4))
  352. goto sigbus;
  353. value = regs->regs[insn.i_format.rt];
  354. __asm__ __volatile__ (
  355. #ifdef __BIG_ENDIAN
  356. "1:\tswl\t%1,(%2)\n"
  357. "2:\tswr\t%1, 3(%2)\n\t"
  358. #endif
  359. #ifdef __LITTLE_ENDIAN
  360. "1:\tswl\t%1, 3(%2)\n"
  361. "2:\tswr\t%1, (%2)\n\t"
  362. #endif
  363. "li\t%0, 0\n"
  364. "3:\n\t"
  365. ".section\t.fixup,\"ax\"\n\t"
  366. "4:\tli\t%0, %3\n\t"
  367. "j\t3b\n\t"
  368. ".previous\n\t"
  369. ".section\t__ex_table,\"a\"\n\t"
  370. STR(PTR)"\t1b, 4b\n\t"
  371. STR(PTR)"\t2b, 4b\n\t"
  372. ".previous"
  373. : "=r" (res)
  374. : "r" (value), "r" (addr), "i" (-EFAULT));
  375. if (res)
  376. goto fault;
  377. compute_return_epc(regs);
  378. break;
  379. case sd_op:
  380. #ifdef CONFIG_64BIT
  381. /*
  382. * A 32-bit kernel might be running on a 64-bit processor. But
  383. * if we're on a 32-bit processor and an i-cache incoherency
  384. * or race makes us see a 64-bit instruction here the sdl/sdr
  385. * would blow up, so for now we don't handle unaligned 64-bit
  386. * instructions on 32-bit kernels.
  387. */
  388. if (!access_ok(VERIFY_WRITE, addr, 8))
  389. goto sigbus;
  390. value = regs->regs[insn.i_format.rt];
  391. __asm__ __volatile__ (
  392. #ifdef __BIG_ENDIAN
  393. "1:\tsdl\t%1,(%2)\n"
  394. "2:\tsdr\t%1, 7(%2)\n\t"
  395. #endif
  396. #ifdef __LITTLE_ENDIAN
  397. "1:\tsdl\t%1, 7(%2)\n"
  398. "2:\tsdr\t%1, (%2)\n\t"
  399. #endif
  400. "li\t%0, 0\n"
  401. "3:\n\t"
  402. ".section\t.fixup,\"ax\"\n\t"
  403. "4:\tli\t%0, %3\n\t"
  404. "j\t3b\n\t"
  405. ".previous\n\t"
  406. ".section\t__ex_table,\"a\"\n\t"
  407. STR(PTR)"\t1b, 4b\n\t"
  408. STR(PTR)"\t2b, 4b\n\t"
  409. ".previous"
  410. : "=r" (res)
  411. : "r" (value), "r" (addr), "i" (-EFAULT));
  412. if (res)
  413. goto fault;
  414. compute_return_epc(regs);
  415. break;
  416. #endif /* CONFIG_64BIT */
  417. /* Cannot handle 64-bit instructions in 32-bit kernel */
  418. goto sigill;
  419. case lwc1_op:
  420. case ldc1_op:
  421. case swc1_op:
  422. case sdc1_op:
  423. /*
  424. * I herewith declare: this does not happen. So send SIGBUS.
  425. */
  426. goto sigbus;
  427. /*
  428. * COP2 is available to implementor for application specific use.
  429. * It's up to applications to register a notifier chain and do
  430. * whatever they have to do, including possible sending of signals.
  431. */
  432. case lwc2_op:
  433. cu2_notifier_call_chain(CU2_LWC2_OP, regs);
  434. break;
  435. case ldc2_op:
  436. cu2_notifier_call_chain(CU2_LDC2_OP, regs);
  437. break;
  438. case swc2_op:
  439. cu2_notifier_call_chain(CU2_SWC2_OP, regs);
  440. break;
  441. case sdc2_op:
  442. cu2_notifier_call_chain(CU2_SDC2_OP, regs);
  443. break;
  444. default:
  445. /*
  446. * Pheeee... We encountered an yet unknown instruction or
  447. * cache coherence problem. Die sucker, die ...
  448. */
  449. goto sigill;
  450. }
  451. #ifdef CONFIG_DEBUG_FS
  452. unaligned_instructions++;
  453. #endif
  454. return;
  455. fault:
  456. /* Did we have an exception handler installed? */
  457. if (fixup_exception(regs))
  458. return;
  459. die_if_kernel("Unhandled kernel unaligned access", regs);
  460. force_sig(SIGSEGV, current);
  461. return;
  462. sigbus:
  463. die_if_kernel("Unhandled kernel unaligned access", regs);
  464. force_sig(SIGBUS, current);
  465. return;
  466. sigill:
  467. die_if_kernel("Unhandled kernel unaligned access or invalid instruction", regs);
  468. force_sig(SIGILL, current);
  469. }
  470. asmlinkage void do_ade(struct pt_regs *regs)
  471. {
  472. unsigned int __user *pc;
  473. mm_segment_t seg;
  474. /*
  475. * Did we catch a fault trying to load an instruction?
  476. * Or are we running in MIPS16 mode?
  477. */
  478. if ((regs->cp0_badvaddr == regs->cp0_epc) || (regs->cp0_epc & 0x1))
  479. goto sigbus;
  480. pc = (unsigned int __user *) exception_epc(regs);
  481. if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
  482. goto sigbus;
  483. if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
  484. goto sigbus;
  485. else if (unaligned_action == UNALIGNED_ACTION_SHOW)
  486. show_registers(regs);
  487. /*
  488. * Do branch emulation only if we didn't forward the exception.
  489. * This is all so but ugly ...
  490. */
  491. seg = get_fs();
  492. if (!user_mode(regs))
  493. set_fs(KERNEL_DS);
  494. emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
  495. set_fs(seg);
  496. return;
  497. sigbus:
  498. die_if_kernel("Kernel unaligned instruction access", regs);
  499. force_sig(SIGBUS, current);
  500. /*
  501. * XXX On return from the signal handler we should advance the epc
  502. */
  503. }
  504. #ifdef CONFIG_DEBUG_FS
  505. extern struct dentry *mips_debugfs_dir;
  506. static int __init debugfs_unaligned(void)
  507. {
  508. struct dentry *d;
  509. if (!mips_debugfs_dir)
  510. return -ENODEV;
  511. d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
  512. mips_debugfs_dir, &unaligned_instructions);
  513. if (!d)
  514. return -ENOMEM;
  515. d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  516. mips_debugfs_dir, &unaligned_action);
  517. if (!d)
  518. return -ENOMEM;
  519. return 0;
  520. }
  521. __initcall(debugfs_unaligned);
  522. #endif