unaligned.c 13 KB

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