unaligned.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 <linux/perf_event.h>
  82. #include <asm/asm.h>
  83. #include <asm/branch.h>
  84. #include <asm/byteorder.h>
  85. #include <asm/cop2.h>
  86. #include <asm/inst.h>
  87. #include <asm/uaccess.h>
  88. #include <asm/system.h>
  89. #define STR(x) __STR(x)
  90. #define __STR(x) #x
  91. enum {
  92. UNALIGNED_ACTION_QUIET,
  93. UNALIGNED_ACTION_SIGNAL,
  94. UNALIGNED_ACTION_SHOW,
  95. };
  96. #ifdef CONFIG_DEBUG_FS
  97. static u32 unaligned_instructions;
  98. static u32 unaligned_action;
  99. #else
  100. #define unaligned_action UNALIGNED_ACTION_QUIET
  101. #endif
  102. extern void show_registers(struct pt_regs *regs);
  103. static void emulate_load_store_insn(struct pt_regs *regs,
  104. void __user *addr, unsigned int __user *pc)
  105. {
  106. union mips_instruction insn;
  107. unsigned long value;
  108. unsigned int res;
  109. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  110. 1, 0, regs, 0);
  111. /*
  112. * This load never faults.
  113. */
  114. __get_user(insn.word, pc);
  115. switch (insn.i_format.opcode) {
  116. /*
  117. * These are instructions that a compiler doesn't generate. We
  118. * can assume therefore that the code is MIPS-aware and
  119. * really buggy. Emulating these instructions would break the
  120. * semantics anyway.
  121. */
  122. case ll_op:
  123. case lld_op:
  124. case sc_op:
  125. case scd_op:
  126. /*
  127. * For these instructions the only way to create an address
  128. * error is an attempted access to kernel/supervisor address
  129. * space.
  130. */
  131. case ldl_op:
  132. case ldr_op:
  133. case lwl_op:
  134. case lwr_op:
  135. case sdl_op:
  136. case sdr_op:
  137. case swl_op:
  138. case swr_op:
  139. case lb_op:
  140. case lbu_op:
  141. case sb_op:
  142. goto sigbus;
  143. /*
  144. * The remaining opcodes are the ones that are really of interest.
  145. */
  146. case lh_op:
  147. if (!access_ok(VERIFY_READ, addr, 2))
  148. goto sigbus;
  149. __asm__ __volatile__ (".set\tnoat\n"
  150. #ifdef __BIG_ENDIAN
  151. "1:\tlb\t%0, 0(%2)\n"
  152. "2:\tlbu\t$1, 1(%2)\n\t"
  153. #endif
  154. #ifdef __LITTLE_ENDIAN
  155. "1:\tlb\t%0, 1(%2)\n"
  156. "2:\tlbu\t$1, 0(%2)\n\t"
  157. #endif
  158. "sll\t%0, 0x8\n\t"
  159. "or\t%0, $1\n\t"
  160. "li\t%1, 0\n"
  161. "3:\t.set\tat\n\t"
  162. ".section\t.fixup,\"ax\"\n\t"
  163. "4:\tli\t%1, %3\n\t"
  164. "j\t3b\n\t"
  165. ".previous\n\t"
  166. ".section\t__ex_table,\"a\"\n\t"
  167. STR(PTR)"\t1b, 4b\n\t"
  168. STR(PTR)"\t2b, 4b\n\t"
  169. ".previous"
  170. : "=&r" (value), "=r" (res)
  171. : "r" (addr), "i" (-EFAULT));
  172. if (res)
  173. goto fault;
  174. compute_return_epc(regs);
  175. regs->regs[insn.i_format.rt] = value;
  176. break;
  177. case lw_op:
  178. if (!access_ok(VERIFY_READ, addr, 4))
  179. goto sigbus;
  180. __asm__ __volatile__ (
  181. #ifdef __BIG_ENDIAN
  182. "1:\tlwl\t%0, (%2)\n"
  183. "2:\tlwr\t%0, 3(%2)\n\t"
  184. #endif
  185. #ifdef __LITTLE_ENDIAN
  186. "1:\tlwl\t%0, 3(%2)\n"
  187. "2:\tlwr\t%0, (%2)\n\t"
  188. #endif
  189. "li\t%1, 0\n"
  190. "3:\t.section\t.fixup,\"ax\"\n\t"
  191. "4:\tli\t%1, %3\n\t"
  192. "j\t3b\n\t"
  193. ".previous\n\t"
  194. ".section\t__ex_table,\"a\"\n\t"
  195. STR(PTR)"\t1b, 4b\n\t"
  196. STR(PTR)"\t2b, 4b\n\t"
  197. ".previous"
  198. : "=&r" (value), "=r" (res)
  199. : "r" (addr), "i" (-EFAULT));
  200. if (res)
  201. goto fault;
  202. compute_return_epc(regs);
  203. regs->regs[insn.i_format.rt] = value;
  204. break;
  205. case lhu_op:
  206. if (!access_ok(VERIFY_READ, addr, 2))
  207. goto sigbus;
  208. __asm__ __volatile__ (
  209. ".set\tnoat\n"
  210. #ifdef __BIG_ENDIAN
  211. "1:\tlbu\t%0, 0(%2)\n"
  212. "2:\tlbu\t$1, 1(%2)\n\t"
  213. #endif
  214. #ifdef __LITTLE_ENDIAN
  215. "1:\tlbu\t%0, 1(%2)\n"
  216. "2:\tlbu\t$1, 0(%2)\n\t"
  217. #endif
  218. "sll\t%0, 0x8\n\t"
  219. "or\t%0, $1\n\t"
  220. "li\t%1, 0\n"
  221. "3:\t.set\tat\n\t"
  222. ".section\t.fixup,\"ax\"\n\t"
  223. "4:\tli\t%1, %3\n\t"
  224. "j\t3b\n\t"
  225. ".previous\n\t"
  226. ".section\t__ex_table,\"a\"\n\t"
  227. STR(PTR)"\t1b, 4b\n\t"
  228. STR(PTR)"\t2b, 4b\n\t"
  229. ".previous"
  230. : "=&r" (value), "=r" (res)
  231. : "r" (addr), "i" (-EFAULT));
  232. if (res)
  233. goto fault;
  234. compute_return_epc(regs);
  235. regs->regs[insn.i_format.rt] = value;
  236. break;
  237. case lwu_op:
  238. #ifdef CONFIG_64BIT
  239. /*
  240. * A 32-bit kernel might be running on a 64-bit processor. But
  241. * if we're on a 32-bit processor and an i-cache incoherency
  242. * or race makes us see a 64-bit instruction here the sdl/sdr
  243. * would blow up, so for now we don't handle unaligned 64-bit
  244. * instructions on 32-bit kernels.
  245. */
  246. if (!access_ok(VERIFY_READ, addr, 4))
  247. goto sigbus;
  248. __asm__ __volatile__ (
  249. #ifdef __BIG_ENDIAN
  250. "1:\tlwl\t%0, (%2)\n"
  251. "2:\tlwr\t%0, 3(%2)\n\t"
  252. #endif
  253. #ifdef __LITTLE_ENDIAN
  254. "1:\tlwl\t%0, 3(%2)\n"
  255. "2:\tlwr\t%0, (%2)\n\t"
  256. #endif
  257. "dsll\t%0, %0, 32\n\t"
  258. "dsrl\t%0, %0, 32\n\t"
  259. "li\t%1, 0\n"
  260. "3:\t.section\t.fixup,\"ax\"\n\t"
  261. "4:\tli\t%1, %3\n\t"
  262. "j\t3b\n\t"
  263. ".previous\n\t"
  264. ".section\t__ex_table,\"a\"\n\t"
  265. STR(PTR)"\t1b, 4b\n\t"
  266. STR(PTR)"\t2b, 4b\n\t"
  267. ".previous"
  268. : "=&r" (value), "=r" (res)
  269. : "r" (addr), "i" (-EFAULT));
  270. if (res)
  271. goto fault;
  272. compute_return_epc(regs);
  273. regs->regs[insn.i_format.rt] = value;
  274. break;
  275. #endif /* CONFIG_64BIT */
  276. /* Cannot handle 64-bit instructions in 32-bit kernel */
  277. goto sigill;
  278. case ld_op:
  279. #ifdef CONFIG_64BIT
  280. /*
  281. * A 32-bit kernel might be running on a 64-bit processor. But
  282. * if we're on a 32-bit processor and an i-cache incoherency
  283. * or race makes us see a 64-bit instruction here the sdl/sdr
  284. * would blow up, so for now we don't handle unaligned 64-bit
  285. * instructions on 32-bit kernels.
  286. */
  287. if (!access_ok(VERIFY_READ, addr, 8))
  288. goto sigbus;
  289. __asm__ __volatile__ (
  290. #ifdef __BIG_ENDIAN
  291. "1:\tldl\t%0, (%2)\n"
  292. "2:\tldr\t%0, 7(%2)\n\t"
  293. #endif
  294. #ifdef __LITTLE_ENDIAN
  295. "1:\tldl\t%0, 7(%2)\n"
  296. "2:\tldr\t%0, (%2)\n\t"
  297. #endif
  298. "li\t%1, 0\n"
  299. "3:\t.section\t.fixup,\"ax\"\n\t"
  300. "4:\tli\t%1, %3\n\t"
  301. "j\t3b\n\t"
  302. ".previous\n\t"
  303. ".section\t__ex_table,\"a\"\n\t"
  304. STR(PTR)"\t1b, 4b\n\t"
  305. STR(PTR)"\t2b, 4b\n\t"
  306. ".previous"
  307. : "=&r" (value), "=r" (res)
  308. : "r" (addr), "i" (-EFAULT));
  309. if (res)
  310. goto fault;
  311. compute_return_epc(regs);
  312. regs->regs[insn.i_format.rt] = value;
  313. break;
  314. #endif /* CONFIG_64BIT */
  315. /* Cannot handle 64-bit instructions in 32-bit kernel */
  316. goto sigill;
  317. case sh_op:
  318. if (!access_ok(VERIFY_WRITE, addr, 2))
  319. goto sigbus;
  320. value = regs->regs[insn.i_format.rt];
  321. __asm__ __volatile__ (
  322. #ifdef __BIG_ENDIAN
  323. ".set\tnoat\n"
  324. "1:\tsb\t%1, 1(%2)\n\t"
  325. "srl\t$1, %1, 0x8\n"
  326. "2:\tsb\t$1, 0(%2)\n\t"
  327. ".set\tat\n\t"
  328. #endif
  329. #ifdef __LITTLE_ENDIAN
  330. ".set\tnoat\n"
  331. "1:\tsb\t%1, 0(%2)\n\t"
  332. "srl\t$1,%1, 0x8\n"
  333. "2:\tsb\t$1, 1(%2)\n\t"
  334. ".set\tat\n\t"
  335. #endif
  336. "li\t%0, 0\n"
  337. "3:\n\t"
  338. ".section\t.fixup,\"ax\"\n\t"
  339. "4:\tli\t%0, %3\n\t"
  340. "j\t3b\n\t"
  341. ".previous\n\t"
  342. ".section\t__ex_table,\"a\"\n\t"
  343. STR(PTR)"\t1b, 4b\n\t"
  344. STR(PTR)"\t2b, 4b\n\t"
  345. ".previous"
  346. : "=r" (res)
  347. : "r" (value), "r" (addr), "i" (-EFAULT));
  348. if (res)
  349. goto fault;
  350. compute_return_epc(regs);
  351. break;
  352. case sw_op:
  353. if (!access_ok(VERIFY_WRITE, addr, 4))
  354. goto sigbus;
  355. value = regs->regs[insn.i_format.rt];
  356. __asm__ __volatile__ (
  357. #ifdef __BIG_ENDIAN
  358. "1:\tswl\t%1,(%2)\n"
  359. "2:\tswr\t%1, 3(%2)\n\t"
  360. #endif
  361. #ifdef __LITTLE_ENDIAN
  362. "1:\tswl\t%1, 3(%2)\n"
  363. "2:\tswr\t%1, (%2)\n\t"
  364. #endif
  365. "li\t%0, 0\n"
  366. "3:\n\t"
  367. ".section\t.fixup,\"ax\"\n\t"
  368. "4:\tli\t%0, %3\n\t"
  369. "j\t3b\n\t"
  370. ".previous\n\t"
  371. ".section\t__ex_table,\"a\"\n\t"
  372. STR(PTR)"\t1b, 4b\n\t"
  373. STR(PTR)"\t2b, 4b\n\t"
  374. ".previous"
  375. : "=r" (res)
  376. : "r" (value), "r" (addr), "i" (-EFAULT));
  377. if (res)
  378. goto fault;
  379. compute_return_epc(regs);
  380. break;
  381. case sd_op:
  382. #ifdef CONFIG_64BIT
  383. /*
  384. * A 32-bit kernel might be running on a 64-bit processor. But
  385. * if we're on a 32-bit processor and an i-cache incoherency
  386. * or race makes us see a 64-bit instruction here the sdl/sdr
  387. * would blow up, so for now we don't handle unaligned 64-bit
  388. * instructions on 32-bit kernels.
  389. */
  390. if (!access_ok(VERIFY_WRITE, addr, 8))
  391. goto sigbus;
  392. value = regs->regs[insn.i_format.rt];
  393. __asm__ __volatile__ (
  394. #ifdef __BIG_ENDIAN
  395. "1:\tsdl\t%1,(%2)\n"
  396. "2:\tsdr\t%1, 7(%2)\n\t"
  397. #endif
  398. #ifdef __LITTLE_ENDIAN
  399. "1:\tsdl\t%1, 7(%2)\n"
  400. "2:\tsdr\t%1, (%2)\n\t"
  401. #endif
  402. "li\t%0, 0\n"
  403. "3:\n\t"
  404. ".section\t.fixup,\"ax\"\n\t"
  405. "4:\tli\t%0, %3\n\t"
  406. "j\t3b\n\t"
  407. ".previous\n\t"
  408. ".section\t__ex_table,\"a\"\n\t"
  409. STR(PTR)"\t1b, 4b\n\t"
  410. STR(PTR)"\t2b, 4b\n\t"
  411. ".previous"
  412. : "=r" (res)
  413. : "r" (value), "r" (addr), "i" (-EFAULT));
  414. if (res)
  415. goto fault;
  416. compute_return_epc(regs);
  417. break;
  418. #endif /* CONFIG_64BIT */
  419. /* Cannot handle 64-bit instructions in 32-bit kernel */
  420. goto sigill;
  421. case lwc1_op:
  422. case ldc1_op:
  423. case swc1_op:
  424. case sdc1_op:
  425. /*
  426. * I herewith declare: this does not happen. So send SIGBUS.
  427. */
  428. goto sigbus;
  429. /*
  430. * COP2 is available to implementor for application specific use.
  431. * It's up to applications to register a notifier chain and do
  432. * whatever they have to do, including possible sending of signals.
  433. */
  434. case lwc2_op:
  435. cu2_notifier_call_chain(CU2_LWC2_OP, regs);
  436. break;
  437. case ldc2_op:
  438. cu2_notifier_call_chain(CU2_LDC2_OP, regs);
  439. break;
  440. case swc2_op:
  441. cu2_notifier_call_chain(CU2_SWC2_OP, regs);
  442. break;
  443. case sdc2_op:
  444. cu2_notifier_call_chain(CU2_SDC2_OP, regs);
  445. break;
  446. default:
  447. /*
  448. * Pheeee... We encountered an yet unknown instruction or
  449. * cache coherence problem. Die sucker, die ...
  450. */
  451. goto sigill;
  452. }
  453. #ifdef CONFIG_DEBUG_FS
  454. unaligned_instructions++;
  455. #endif
  456. return;
  457. fault:
  458. /* Did we have an exception handler installed? */
  459. if (fixup_exception(regs))
  460. return;
  461. die_if_kernel("Unhandled kernel unaligned access", regs);
  462. force_sig(SIGSEGV, current);
  463. return;
  464. sigbus:
  465. die_if_kernel("Unhandled kernel unaligned access", regs);
  466. force_sig(SIGBUS, current);
  467. return;
  468. sigill:
  469. die_if_kernel("Unhandled kernel unaligned access or invalid instruction", regs);
  470. force_sig(SIGILL, current);
  471. }
  472. asmlinkage void do_ade(struct pt_regs *regs)
  473. {
  474. unsigned int __user *pc;
  475. mm_segment_t seg;
  476. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
  477. 1, 0, regs, regs->cp0_badvaddr);
  478. /*
  479. * Did we catch a fault trying to load an instruction?
  480. * Or are we running in MIPS16 mode?
  481. */
  482. if ((regs->cp0_badvaddr == regs->cp0_epc) || (regs->cp0_epc & 0x1))
  483. goto sigbus;
  484. pc = (unsigned int __user *) exception_epc(regs);
  485. if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
  486. goto sigbus;
  487. if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
  488. goto sigbus;
  489. else if (unaligned_action == UNALIGNED_ACTION_SHOW)
  490. show_registers(regs);
  491. /*
  492. * Do branch emulation only if we didn't forward the exception.
  493. * This is all so but ugly ...
  494. */
  495. seg = get_fs();
  496. if (!user_mode(regs))
  497. set_fs(KERNEL_DS);
  498. emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
  499. set_fs(seg);
  500. return;
  501. sigbus:
  502. die_if_kernel("Kernel unaligned instruction access", regs);
  503. force_sig(SIGBUS, current);
  504. /*
  505. * XXX On return from the signal handler we should advance the epc
  506. */
  507. }
  508. #ifdef CONFIG_DEBUG_FS
  509. extern struct dentry *mips_debugfs_dir;
  510. static int __init debugfs_unaligned(void)
  511. {
  512. struct dentry *d;
  513. if (!mips_debugfs_dir)
  514. return -ENODEV;
  515. d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
  516. mips_debugfs_dir, &unaligned_instructions);
  517. if (!d)
  518. return -ENOMEM;
  519. d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  520. mips_debugfs_dir, &unaligned_action);
  521. if (!d)
  522. return -ENOMEM;
  523. return 0;
  524. }
  525. __initcall(debugfs_unaligned);
  526. #endif