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>
86 #include <asm/uaccess.h>
87 #include <asm/system.h>
89 #define STR(x) __STR(x)
93 UNALIGNED_ACTION_QUIET
,
94 UNALIGNED_ACTION_SIGNAL
,
95 UNALIGNED_ACTION_SHOW
,
97 #ifdef CONFIG_DEBUG_FS
98 static u32 unaligned_instructions
;
99 static u32 unaligned_action
;
101 #define unaligned_action UNALIGNED_ACTION_QUIET
103 extern void show_registers(struct pt_regs
*regs
);
105 static void emulate_load_store_insn(struct pt_regs
*regs
,
106 void __user
*addr
, unsigned int __user
*pc
)
108 union mips_instruction insn
;
115 * This load never faults.
117 __get_user(insn
.word
, pc
);
119 switch (insn
.i_format
.opcode
) {
121 * These are instructions that a compiler doesn't generate. We
122 * can assume therefore that the code is MIPS-aware and
123 * really buggy. Emulating these instructions would break the
132 * For these instructions the only way to create an address
133 * error is an attempted access to kernel/supervisor address
150 * The remaining opcodes are the ones that are really of interest.
153 if (!access_ok(VERIFY_READ
, addr
, 2))
156 __asm__
__volatile__ (".set\tnoat\n"
158 "1:\tlb\t%0, 0(%2)\n"
159 "2:\tlbu\t$1, 1(%2)\n\t"
161 #ifdef __LITTLE_ENDIAN
162 "1:\tlb\t%0, 1(%2)\n"
163 "2:\tlbu\t$1, 0(%2)\n\t"
169 ".section\t.fixup,\"ax\"\n\t"
173 ".section\t__ex_table,\"a\"\n\t"
174 STR(PTR
)"\t1b, 4b\n\t"
175 STR(PTR
)"\t2b, 4b\n\t"
177 : "=&r" (value
), "=r" (res
)
178 : "r" (addr
), "i" (-EFAULT
));
181 compute_return_epc(regs
);
182 regs
->regs
[insn
.i_format
.rt
] = value
;
186 if (!access_ok(VERIFY_READ
, addr
, 4))
189 __asm__
__volatile__ (
191 "1:\tlwl\t%0, (%2)\n"
192 "2:\tlwr\t%0, 3(%2)\n\t"
194 #ifdef __LITTLE_ENDIAN
195 "1:\tlwl\t%0, 3(%2)\n"
196 "2:\tlwr\t%0, (%2)\n\t"
199 "3:\t.section\t.fixup,\"ax\"\n\t"
203 ".section\t__ex_table,\"a\"\n\t"
204 STR(PTR
)"\t1b, 4b\n\t"
205 STR(PTR
)"\t2b, 4b\n\t"
207 : "=&r" (value
), "=r" (res
)
208 : "r" (addr
), "i" (-EFAULT
));
211 compute_return_epc(regs
);
212 regs
->regs
[insn
.i_format
.rt
] = value
;
216 if (!access_ok(VERIFY_READ
, addr
, 2))
219 __asm__
__volatile__ (
222 "1:\tlbu\t%0, 0(%2)\n"
223 "2:\tlbu\t$1, 1(%2)\n\t"
225 #ifdef __LITTLE_ENDIAN
226 "1:\tlbu\t%0, 1(%2)\n"
227 "2:\tlbu\t$1, 0(%2)\n\t"
233 ".section\t.fixup,\"ax\"\n\t"
237 ".section\t__ex_table,\"a\"\n\t"
238 STR(PTR
)"\t1b, 4b\n\t"
239 STR(PTR
)"\t2b, 4b\n\t"
241 : "=&r" (value
), "=r" (res
)
242 : "r" (addr
), "i" (-EFAULT
));
245 compute_return_epc(regs
);
246 regs
->regs
[insn
.i_format
.rt
] = value
;
252 * A 32-bit kernel might be running on a 64-bit processor. But
253 * if we're on a 32-bit processor and an i-cache incoherency
254 * or race makes us see a 64-bit instruction here the sdl/sdr
255 * would blow up, so for now we don't handle unaligned 64-bit
256 * instructions on 32-bit kernels.
258 if (!access_ok(VERIFY_READ
, addr
, 4))
261 __asm__
__volatile__ (
263 "1:\tlwl\t%0, (%2)\n"
264 "2:\tlwr\t%0, 3(%2)\n\t"
266 #ifdef __LITTLE_ENDIAN
267 "1:\tlwl\t%0, 3(%2)\n"
268 "2:\tlwr\t%0, (%2)\n\t"
270 "dsll\t%0, %0, 32\n\t"
271 "dsrl\t%0, %0, 32\n\t"
273 "3:\t.section\t.fixup,\"ax\"\n\t"
277 ".section\t__ex_table,\"a\"\n\t"
278 STR(PTR
)"\t1b, 4b\n\t"
279 STR(PTR
)"\t2b, 4b\n\t"
281 : "=&r" (value
), "=r" (res
)
282 : "r" (addr
), "i" (-EFAULT
));
285 compute_return_epc(regs
);
286 regs
->regs
[insn
.i_format
.rt
] = value
;
288 #endif /* CONFIG_64BIT */
290 /* Cannot handle 64-bit instructions in 32-bit kernel */
296 * A 32-bit kernel might be running on a 64-bit processor. But
297 * if we're on a 32-bit processor and an i-cache incoherency
298 * or race makes us see a 64-bit instruction here the sdl/sdr
299 * would blow up, so for now we don't handle unaligned 64-bit
300 * instructions on 32-bit kernels.
302 if (!access_ok(VERIFY_READ
, addr
, 8))
305 __asm__
__volatile__ (
307 "1:\tldl\t%0, (%2)\n"
308 "2:\tldr\t%0, 7(%2)\n\t"
310 #ifdef __LITTLE_ENDIAN
311 "1:\tldl\t%0, 7(%2)\n"
312 "2:\tldr\t%0, (%2)\n\t"
315 "3:\t.section\t.fixup,\"ax\"\n\t"
319 ".section\t__ex_table,\"a\"\n\t"
320 STR(PTR
)"\t1b, 4b\n\t"
321 STR(PTR
)"\t2b, 4b\n\t"
323 : "=&r" (value
), "=r" (res
)
324 : "r" (addr
), "i" (-EFAULT
));
327 compute_return_epc(regs
);
328 regs
->regs
[insn
.i_format
.rt
] = value
;
330 #endif /* CONFIG_64BIT */
332 /* Cannot handle 64-bit instructions in 32-bit kernel */
336 if (!access_ok(VERIFY_WRITE
, addr
, 2))
339 value
= regs
->regs
[insn
.i_format
.rt
];
340 __asm__
__volatile__ (
343 "1:\tsb\t%1, 1(%2)\n\t"
345 "2:\tsb\t$1, 0(%2)\n\t"
348 #ifdef __LITTLE_ENDIAN
350 "1:\tsb\t%1, 0(%2)\n\t"
352 "2:\tsb\t$1, 1(%2)\n\t"
357 ".section\t.fixup,\"ax\"\n\t"
361 ".section\t__ex_table,\"a\"\n\t"
362 STR(PTR
)"\t1b, 4b\n\t"
363 STR(PTR
)"\t2b, 4b\n\t"
366 : "r" (value
), "r" (addr
), "i" (-EFAULT
));
369 compute_return_epc(regs
);
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
));
400 compute_return_epc(regs
);
406 * A 32-bit kernel might be running on a 64-bit processor. But
407 * if we're on a 32-bit processor and an i-cache incoherency
408 * or race makes us see a 64-bit instruction here the sdl/sdr
409 * would blow up, so for now we don't handle unaligned 64-bit
410 * instructions on 32-bit kernels.
412 if (!access_ok(VERIFY_WRITE
, addr
, 8))
415 value
= regs
->regs
[insn
.i_format
.rt
];
416 __asm__
__volatile__ (
419 "2:\tsdr\t%1, 7(%2)\n\t"
421 #ifdef __LITTLE_ENDIAN
422 "1:\tsdl\t%1, 7(%2)\n"
423 "2:\tsdr\t%1, (%2)\n\t"
427 ".section\t.fixup,\"ax\"\n\t"
431 ".section\t__ex_table,\"a\"\n\t"
432 STR(PTR
)"\t1b, 4b\n\t"
433 STR(PTR
)"\t2b, 4b\n\t"
436 : "r" (value
), "r" (addr
), "i" (-EFAULT
));
439 compute_return_epc(regs
);
441 #endif /* CONFIG_64BIT */
443 /* Cannot handle 64-bit instructions in 32-bit kernel */
451 * I herewith declare: this does not happen. So send SIGBUS.
456 * COP2 is available to implementor for application specific use.
457 * It's up to applications to register a notifier chain and do
458 * whatever they have to do, including possible sending of signals.
461 cu2_notifier_call_chain(CU2_LWC2_OP
, regs
);
465 cu2_notifier_call_chain(CU2_LDC2_OP
, regs
);
469 cu2_notifier_call_chain(CU2_SWC2_OP
, regs
);
473 cu2_notifier_call_chain(CU2_SDC2_OP
, regs
);
478 * Pheeee... We encountered an yet unknown instruction or
479 * cache coherence problem. Die sucker, die ...
484 #ifdef CONFIG_DEBUG_FS
485 unaligned_instructions
++;
491 /* Did we have an exception handler installed? */
492 if (fixup_exception(regs
))
495 die_if_kernel("Unhandled kernel unaligned access", regs
);
496 force_sig(SIGSEGV
, current
);
501 die_if_kernel("Unhandled kernel unaligned access", regs
);
502 force_sig(SIGBUS
, current
);
507 die_if_kernel("Unhandled kernel unaligned access or invalid instruction", regs
);
508 force_sig(SIGILL
, current
);
511 asmlinkage
void do_ade(struct pt_regs
*regs
)
513 unsigned int __user
*pc
;
517 * Did we catch a fault trying to load an instruction?
518 * Or are we running in MIPS16 mode?
520 if ((regs
->cp0_badvaddr
== regs
->cp0_epc
) || (regs
->cp0_epc
& 0x1))
523 pc
= (unsigned int __user
*) exception_epc(regs
);
524 if (user_mode(regs
) && !test_thread_flag(TIF_FIXADE
))
526 if (unaligned_action
== UNALIGNED_ACTION_SIGNAL
)
528 else if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
529 show_registers(regs
);
532 * Do branch emulation only if we didn't forward the exception.
533 * This is all so but ugly ...
536 if (!user_mode(regs
))
538 emulate_load_store_insn(regs
, (void __user
*)regs
->cp0_badvaddr
, pc
);
544 die_if_kernel("Kernel unaligned instruction access", regs
);
545 force_sig(SIGBUS
, current
);
548 * XXX On return from the signal handler we should advance the epc
552 #ifdef CONFIG_DEBUG_FS
553 extern struct dentry
*mips_debugfs_dir
;
554 static int __init
debugfs_unaligned(void)
558 if (!mips_debugfs_dir
)
560 d
= debugfs_create_u32("unaligned_instructions", S_IRUGO
,
561 mips_debugfs_dir
, &unaligned_instructions
);
564 d
= debugfs_create_u32("unaligned_action", S_IRUGO
| S_IWUSR
,
565 mips_debugfs_dir
, &unaligned_action
);
570 __initcall(debugfs_unaligned
);