2 * Handle unaligned accesses by emulation.
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
8 * Copyright (C) 1996, 1998, 1999, 2002 by Ralf Baechle
9 * Copyright (C) 1999 Silicon Graphics, Inc.
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.
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.
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
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:
32 * #include <sys/sysmips.h>
35 * sysmips(MIPS_FIXADE, x);
38 * The argument x is 0 for disabling software emulation, enabled otherwise.
40 * Below a little program to play around with this feature.
43 * #include <sys/sysmips.h>
46 * unsigned char bar[8];
49 * main(int argc, char *argv[])
51 * struct foo x = {0, 1, 2, 3, 4, 5, 6, 7};
52 * unsigned int *p = (unsigned int *) (x.bar + 3);
56 * sysmips(MIPS_FIXADE, atoi(argv[1]));
58 * printf("*p = %08lx\n", *p);
62 * for(i = 0; i <= 7; i++)
63 * printf("%02x ", x.bar[i]);
67 * Coprocessor loads are not supported; I think this case is unimportant
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.
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>
82 #include <asm/branch.h>
83 #include <asm/byteorder.h>
85 #include <asm/uaccess.h>
86 #include <asm/system.h>
88 #define STR(x) __STR(x)
92 UNALIGNED_ACTION_QUIET,
93 UNALIGNED_ACTION_SIGNAL,
94 UNALIGNED_ACTION_SHOW,
96 #ifdef CONFIG_DEBUG_FS
97 static u32 unaligned_instructions;
98 static u32 unaligned_action;
100 #define unaligned_action UNALIGNED_ACTION_QUIET
102 extern void show_registers(struct pt_regs *regs);
104 static inline int emulate_load_store_insn(struct pt_regs *regs,
105 void __user *addr, unsigned int __user *pc,
106 unsigned long **regptr, unsigned long *newvalue)
108 union mips_instruction insn;
116 * This load never faults.
118 __get_user(insn.word, pc);
120 switch (insn.i_format.opcode) {
122 * These are instructions that a compiler doesn't generate. We
123 * can assume therefore that the code is MIPS-aware and
124 * really buggy. Emulating these instructions would break the
133 * For these instructions the only way to create an address
134 * error is an attempted access to kernel/supervisor address
151 * The remaining opcodes are the ones that are really of interest.
154 if (!access_ok(VERIFY_READ, addr, 2))
157 __asm__ __volatile__ (".set\tnoat\n"
159 "1:\tlb\t%0, 0(%2)\n"
160 "2:\tlbu\t$1, 1(%2)\n\t"
162 #ifdef __LITTLE_ENDIAN
163 "1:\tlb\t%0, 1(%2)\n"
164 "2:\tlbu\t$1, 0(%2)\n\t"
170 ".section\t.fixup,\"ax\"\n\t"
174 ".section\t__ex_table,\"a\"\n\t"
175 STR(PTR)"\t1b, 4b\n\t"
176 STR(PTR)"\t2b, 4b\n\t"
178 : "=&r" (value), "=r" (res)
179 : "r" (addr), "i" (-EFAULT));
183 *regptr = ®s->regs[insn.i_format.rt];
187 if (!access_ok(VERIFY_READ, addr, 4))
190 __asm__ __volatile__ (
192 "1:\tlwl\t%0, (%2)\n"
193 "2:\tlwr\t%0, 3(%2)\n\t"
195 #ifdef __LITTLE_ENDIAN
196 "1:\tlwl\t%0, 3(%2)\n"
197 "2:\tlwr\t%0, (%2)\n\t"
200 "3:\t.section\t.fixup,\"ax\"\n\t"
204 ".section\t__ex_table,\"a\"\n\t"
205 STR(PTR)"\t1b, 4b\n\t"
206 STR(PTR)"\t2b, 4b\n\t"
208 : "=&r" (value), "=r" (res)
209 : "r" (addr), "i" (-EFAULT));
213 *regptr = ®s->regs[insn.i_format.rt];
217 if (!access_ok(VERIFY_READ, addr, 2))
220 __asm__ __volatile__ (
223 "1:\tlbu\t%0, 0(%2)\n"
224 "2:\tlbu\t$1, 1(%2)\n\t"
226 #ifdef __LITTLE_ENDIAN
227 "1:\tlbu\t%0, 1(%2)\n"
228 "2:\tlbu\t$1, 0(%2)\n\t"
234 ".section\t.fixup,\"ax\"\n\t"
238 ".section\t__ex_table,\"a\"\n\t"
239 STR(PTR)"\t1b, 4b\n\t"
240 STR(PTR)"\t2b, 4b\n\t"
242 : "=&r" (value), "=r" (res)
243 : "r" (addr), "i" (-EFAULT));
247 *regptr = ®s->regs[insn.i_format.rt];
253 * A 32-bit kernel might be running on a 64-bit processor. But
254 * if we're on a 32-bit processor and an i-cache incoherency
255 * or race makes us see a 64-bit instruction here the sdl/sdr
256 * would blow up, so for now we don't handle unaligned 64-bit
257 * instructions on 32-bit kernels.
259 if (!access_ok(VERIFY_READ, addr, 4))
262 __asm__ __volatile__ (
264 "1:\tlwl\t%0, (%2)\n"
265 "2:\tlwr\t%0, 3(%2)\n\t"
267 #ifdef __LITTLE_ENDIAN
268 "1:\tlwl\t%0, 3(%2)\n"
269 "2:\tlwr\t%0, (%2)\n\t"
271 "dsll\t%0, %0, 32\n\t"
272 "dsrl\t%0, %0, 32\n\t"
274 "3:\t.section\t.fixup,\"ax\"\n\t"
278 ".section\t__ex_table,\"a\"\n\t"
279 STR(PTR)"\t1b, 4b\n\t"
280 STR(PTR)"\t2b, 4b\n\t"
282 : "=&r" (value), "=r" (res)
283 : "r" (addr), "i" (-EFAULT));
287 *regptr = ®s->regs[insn.i_format.rt];
289 #endif /* CONFIG_64BIT */
291 /* Cannot handle 64-bit instructions in 32-bit kernel */
297 * A 32-bit kernel might be running on a 64-bit processor. But
298 * if we're on a 32-bit processor and an i-cache incoherency
299 * or race makes us see a 64-bit instruction here the sdl/sdr
300 * would blow up, so for now we don't handle unaligned 64-bit
301 * instructions on 32-bit kernels.
303 if (!access_ok(VERIFY_READ, addr, 8))
306 __asm__ __volatile__ (
308 "1:\tldl\t%0, (%2)\n"
309 "2:\tldr\t%0, 7(%2)\n\t"
311 #ifdef __LITTLE_ENDIAN
312 "1:\tldl\t%0, 7(%2)\n"
313 "2:\tldr\t%0, (%2)\n\t"
316 "3:\t.section\t.fixup,\"ax\"\n\t"
320 ".section\t__ex_table,\"a\"\n\t"
321 STR(PTR)"\t1b, 4b\n\t"
322 STR(PTR)"\t2b, 4b\n\t"
324 : "=&r" (value), "=r" (res)
325 : "r" (addr), "i" (-EFAULT));
329 *regptr = ®s->regs[insn.i_format.rt];
331 #endif /* CONFIG_64BIT */
333 /* Cannot handle 64-bit instructions in 32-bit kernel */
337 if (!access_ok(VERIFY_WRITE, addr, 2))
340 value = regs->regs[insn.i_format.rt];
341 __asm__ __volatile__ (
344 "1:\tsb\t%1, 1(%2)\n\t"
346 "2:\tsb\t$1, 0(%2)\n\t"
349 #ifdef __LITTLE_ENDIAN
351 "1:\tsb\t%1, 0(%2)\n\t"
353 "2:\tsb\t$1, 1(%2)\n\t"
358 ".section\t.fixup,\"ax\"\n\t"
362 ".section\t__ex_table,\"a\"\n\t"
363 STR(PTR)"\t1b, 4b\n\t"
364 STR(PTR)"\t2b, 4b\n\t"
367 : "r" (value), "r" (addr), "i" (-EFAULT));
373 if (!access_ok(VERIFY_WRITE, addr, 4))
376 value = regs->regs[insn.i_format.rt];
377 __asm__ __volatile__ (
380 "2:\tswr\t%1, 3(%2)\n\t"
382 #ifdef __LITTLE_ENDIAN
383 "1:\tswl\t%1, 3(%2)\n"
384 "2:\tswr\t%1, (%2)\n\t"
388 ".section\t.fixup,\"ax\"\n\t"
392 ".section\t__ex_table,\"a\"\n\t"
393 STR(PTR)"\t1b, 4b\n\t"
394 STR(PTR)"\t2b, 4b\n\t"
397 : "r" (value), "r" (addr), "i" (-EFAULT));
405 * A 32-bit kernel might be running on a 64-bit processor. But
406 * if we're on a 32-bit processor and an i-cache incoherency
407 * or race makes us see a 64-bit instruction here the sdl/sdr
408 * would blow up, so for now we don't handle unaligned 64-bit
409 * instructions on 32-bit kernels.
411 if (!access_ok(VERIFY_WRITE, addr, 8))
414 value = regs->regs[insn.i_format.rt];
415 __asm__ __volatile__ (
418 "2:\tsdr\t%1, 7(%2)\n\t"
420 #ifdef __LITTLE_ENDIAN
421 "1:\tsdl\t%1, 7(%2)\n"
422 "2:\tsdr\t%1, (%2)\n\t"
426 ".section\t.fixup,\"ax\"\n\t"
430 ".section\t__ex_table,\"a\"\n\t"
431 STR(PTR)"\t1b, 4b\n\t"
432 STR(PTR)"\t2b, 4b\n\t"
435 : "r" (value), "r" (addr), "i" (-EFAULT));
439 #endif /* CONFIG_64BIT */
441 /* Cannot handle 64-bit instructions in 32-bit kernel */
449 * I herewith declare: this does not happen. So send SIGBUS.
458 * These are the coprocessor 2 load/stores. The current
459 * implementations don't use cp2 and cp2 should always be
460 * disabled in c0_status. So send SIGILL.
461 * (No longer true: The Sony Praystation uses cp2 for
462 * 3D matrix operations. Dunno if that thingy has a MMU ...)
466 * Pheeee... We encountered an yet unknown instruction or
467 * cache coherence problem. Die sucker, die ...
472 #ifdef CONFIG_DEBUG_FS
473 unaligned_instructions++;
479 /* Did we have an exception handler installed? */
480 if (fixup_exception(regs))
483 die_if_kernel ("Unhandled kernel unaligned access", regs);
484 send_sig(SIGSEGV, current, 1);
489 die_if_kernel("Unhandled kernel unaligned access", regs);
490 send_sig(SIGBUS, current, 1);
495 die_if_kernel("Unhandled kernel unaligned access or invalid instruction", regs);
496 send_sig(SIGILL, current, 1);
501 asmlinkage void do_ade(struct pt_regs *regs)
503 unsigned long *regptr, newval;
504 extern int do_dsemulret(struct pt_regs *);
505 unsigned int __user *pc;
509 * Address errors may be deliberately induced by the FPU emulator to
510 * retake control of the CPU after executing the instruction in the
511 * delay slot of an emulated branch.
513 /* Terminate if exception was recognized as a delay slot return */
514 if (do_dsemulret(regs))
517 /* Otherwise handle as normal */
520 * Did we catch a fault trying to load an instruction?
521 * Or are we running in MIPS16 mode?
523 if ((regs->cp0_badvaddr == regs->cp0_epc) || (regs->cp0_epc & 0x1))
526 pc = (unsigned int __user *) exception_epc(regs);
527 if (user_mode(regs) && (current->thread.mflags & MF_FIXADE) == 0)
529 if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
531 else if (unaligned_action == UNALIGNED_ACTION_SHOW)
532 show_registers(regs);
535 * Do branch emulation only if we didn't forward the exception.
536 * This is all so but ugly ...
539 if (!user_mode(regs))
541 if (!emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc,
543 compute_return_epc(regs);
545 * Now that branch is evaluated, update the dest
546 * register if necessary
556 die_if_kernel("Kernel unaligned instruction access", regs);
557 force_sig(SIGBUS, current);
560 * XXX On return from the signal handler we should advance the epc
564 #ifdef CONFIG_DEBUG_FS
565 extern struct dentry *mips_debugfs_dir;
566 static int __init debugfs_unaligned(void)
570 if (!mips_debugfs_dir)
572 d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
573 mips_debugfs_dir, &unaligned_instructions);
576 d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
577 mips_debugfs_dir, &unaligned_action);
582 __initcall(debugfs_unaligned);