unaligned.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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/inst.h>
  85. #include <asm/uaccess.h>
  86. #include <asm/system.h>
  87. #define STR(x) __STR(x)
  88. #define __STR(x) #x
  89. enum {
  90. UNALIGNED_ACTION_QUIET,
  91. UNALIGNED_ACTION_SIGNAL,
  92. UNALIGNED_ACTION_SHOW,
  93. };
  94. #ifdef CONFIG_DEBUG_FS
  95. static u32 unaligned_instructions;
  96. static u32 unaligned_action;
  97. #else
  98. #define unaligned_action UNALIGNED_ACTION_QUIET
  99. #endif
  100. extern void show_registers(struct pt_regs *regs);
  101. static inline int emulate_load_store_insn(struct pt_regs *regs,
  102. void __user *addr, unsigned int __user *pc,
  103. unsigned long **regptr, unsigned long *newvalue)
  104. {
  105. union mips_instruction insn;
  106. unsigned long value;
  107. unsigned int res;
  108. regs->regs[0] = 0;
  109. *regptr=NULL;
  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. *newvalue = value;
  174. *regptr = &regs->regs[insn.i_format.rt];
  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. *newvalue = value;
  202. *regptr = &regs->regs[insn.i_format.rt];
  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. *newvalue = value;
  234. *regptr = &regs->regs[insn.i_format.rt];
  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. *newvalue = value;
  272. *regptr = &regs->regs[insn.i_format.rt];
  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. *newvalue = value;
  311. *regptr = &regs->regs[insn.i_format.rt];
  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. 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. break;
  378. case sd_op:
  379. #ifdef CONFIG_64BIT
  380. /*
  381. * A 32-bit kernel might be running on a 64-bit processor. But
  382. * if we're on a 32-bit processor and an i-cache incoherency
  383. * or race makes us see a 64-bit instruction here the sdl/sdr
  384. * would blow up, so for now we don't handle unaligned 64-bit
  385. * instructions on 32-bit kernels.
  386. */
  387. if (!access_ok(VERIFY_WRITE, addr, 8))
  388. goto sigbus;
  389. value = regs->regs[insn.i_format.rt];
  390. __asm__ __volatile__ (
  391. #ifdef __BIG_ENDIAN
  392. "1:\tsdl\t%1,(%2)\n"
  393. "2:\tsdr\t%1, 7(%2)\n\t"
  394. #endif
  395. #ifdef __LITTLE_ENDIAN
  396. "1:\tsdl\t%1, 7(%2)\n"
  397. "2:\tsdr\t%1, (%2)\n\t"
  398. #endif
  399. "li\t%0, 0\n"
  400. "3:\n\t"
  401. ".section\t.fixup,\"ax\"\n\t"
  402. "4:\tli\t%0, %3\n\t"
  403. "j\t3b\n\t"
  404. ".previous\n\t"
  405. ".section\t__ex_table,\"a\"\n\t"
  406. STR(PTR)"\t1b, 4b\n\t"
  407. STR(PTR)"\t2b, 4b\n\t"
  408. ".previous"
  409. : "=r" (res)
  410. : "r" (value), "r" (addr), "i" (-EFAULT));
  411. if (res)
  412. goto fault;
  413. break;
  414. #endif /* CONFIG_64BIT */
  415. /* Cannot handle 64-bit instructions in 32-bit kernel */
  416. goto sigill;
  417. case lwc1_op:
  418. case ldc1_op:
  419. case swc1_op:
  420. case sdc1_op:
  421. /*
  422. * I herewith declare: this does not happen. So send SIGBUS.
  423. */
  424. goto sigbus;
  425. case lwc2_op:
  426. case ldc2_op:
  427. case swc2_op:
  428. case sdc2_op:
  429. /*
  430. * These are the coprocessor 2 load/stores. The current
  431. * implementations don't use cp2 and cp2 should always be
  432. * disabled in c0_status. So send SIGILL.
  433. * (No longer true: The Sony Praystation uses cp2 for
  434. * 3D matrix operations. Dunno if that thingy has a MMU ...)
  435. */
  436. default:
  437. /*
  438. * Pheeee... We encountered an yet unknown instruction or
  439. * cache coherence problem. Die sucker, die ...
  440. */
  441. goto sigill;
  442. }
  443. #ifdef CONFIG_DEBUG_FS
  444. unaligned_instructions++;
  445. #endif
  446. return 0;
  447. fault:
  448. /* Did we have an exception handler installed? */
  449. if (fixup_exception(regs))
  450. return 1;
  451. die_if_kernel ("Unhandled kernel unaligned access", regs);
  452. send_sig(SIGSEGV, current, 1);
  453. return 0;
  454. sigbus:
  455. die_if_kernel("Unhandled kernel unaligned access", regs);
  456. send_sig(SIGBUS, current, 1);
  457. return 0;
  458. sigill:
  459. die_if_kernel("Unhandled kernel unaligned access or invalid instruction", regs);
  460. send_sig(SIGILL, current, 1);
  461. return 0;
  462. }
  463. asmlinkage void do_ade(struct pt_regs *regs)
  464. {
  465. unsigned long *regptr, newval;
  466. extern int do_dsemulret(struct pt_regs *);
  467. unsigned int __user *pc;
  468. mm_segment_t seg;
  469. /*
  470. * Address errors may be deliberately induced by the FPU emulator to
  471. * retake control of the CPU after executing the instruction in the
  472. * delay slot of an emulated branch.
  473. */
  474. /* Terminate if exception was recognized as a delay slot return */
  475. if (do_dsemulret(regs))
  476. return;
  477. /* Otherwise handle as normal */
  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) && (current->thread.mflags & MF_FIXADE) == 0)
  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. if (!emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc,
  499. &regptr, &newval)) {
  500. compute_return_epc(regs);
  501. /*
  502. * Now that branch is evaluated, update the dest
  503. * register if necessary
  504. */
  505. if (regptr)
  506. *regptr = newval;
  507. }
  508. set_fs(seg);
  509. return;
  510. sigbus:
  511. die_if_kernel("Kernel unaligned instruction access", regs);
  512. force_sig(SIGBUS, current);
  513. /*
  514. * XXX On return from the signal handler we should advance the epc
  515. */
  516. }
  517. #ifdef CONFIG_DEBUG_FS
  518. extern struct dentry *mips_debugfs_dir;
  519. static int __init debugfs_unaligned(void)
  520. {
  521. struct dentry *d;
  522. if (!mips_debugfs_dir)
  523. return -ENODEV;
  524. d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
  525. mips_debugfs_dir, &unaligned_instructions);
  526. if (IS_ERR(d))
  527. return PTR_ERR(d);
  528. d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
  529. mips_debugfs_dir, &unaligned_action);
  530. if (IS_ERR(d))
  531. return PTR_ERR(d);
  532. return 0;
  533. }
  534. __initcall(debugfs_unaligned);
  535. #endif