unaligned.c 14 KB

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