unaligned.c 13 KB

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