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>
81 #include <linux/perf_event.h>
84 #include <asm/branch.h>
85 #include <asm/byteorder.h>
88 #include <asm/uaccess.h>
89 #include <asm/system.h>
91 #define STR(x) __STR(x)
95 UNALIGNED_ACTION_QUIET
,
96 UNALIGNED_ACTION_SIGNAL
,
97 UNALIGNED_ACTION_SHOW
,
99 #ifdef CONFIG_DEBUG_FS
100 static u32 unaligned_instructions
;
101 static u32 unaligned_action
;
103 #define unaligned_action UNALIGNED_ACTION_QUIET
105 extern void show_registers(struct pt_regs
*regs
);
107 static void emulate_load_store_insn(struct pt_regs
*regs
,
108 void __user
*addr
, unsigned int __user
*pc
)
110 union mips_instruction insn
;
114 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, 0);
117 * This load never faults.
119 __get_user(insn
.word
, pc
);
121 switch (insn
.i_format
.opcode
) {
123 * These are instructions that a compiler doesn't generate. We
124 * can assume therefore that the code is MIPS-aware and
125 * really buggy. Emulating these instructions would break the
134 * For these instructions the only way to create an address
135 * error is an attempted access to kernel/supervisor address
152 * The remaining opcodes are the ones that are really of interest.
155 if (!access_ok(VERIFY_READ
, addr
, 2))
158 __asm__
__volatile__ (".set\tnoat\n"
160 "1:\tlb\t%0, 0(%2)\n"
161 "2:\tlbu\t$1, 1(%2)\n\t"
163 #ifdef __LITTLE_ENDIAN
164 "1:\tlb\t%0, 1(%2)\n"
165 "2:\tlbu\t$1, 0(%2)\n\t"
171 ".section\t.fixup,\"ax\"\n\t"
175 ".section\t__ex_table,\"a\"\n\t"
176 STR(PTR
)"\t1b, 4b\n\t"
177 STR(PTR
)"\t2b, 4b\n\t"
179 : "=&r" (value
), "=r" (res
)
180 : "r" (addr
), "i" (-EFAULT
));
183 compute_return_epc(regs
);
184 regs
->regs
[insn
.i_format
.rt
] = value
;
188 if (!access_ok(VERIFY_READ
, addr
, 4))
191 __asm__
__volatile__ (
193 "1:\tlwl\t%0, (%2)\n"
194 "2:\tlwr\t%0, 3(%2)\n\t"
196 #ifdef __LITTLE_ENDIAN
197 "1:\tlwl\t%0, 3(%2)\n"
198 "2:\tlwr\t%0, (%2)\n\t"
201 "3:\t.section\t.fixup,\"ax\"\n\t"
205 ".section\t__ex_table,\"a\"\n\t"
206 STR(PTR
)"\t1b, 4b\n\t"
207 STR(PTR
)"\t2b, 4b\n\t"
209 : "=&r" (value
), "=r" (res
)
210 : "r" (addr
), "i" (-EFAULT
));
213 compute_return_epc(regs
);
214 regs
->regs
[insn
.i_format
.rt
] = value
;
218 if (!access_ok(VERIFY_READ
, addr
, 2))
221 __asm__
__volatile__ (
224 "1:\tlbu\t%0, 0(%2)\n"
225 "2:\tlbu\t$1, 1(%2)\n\t"
227 #ifdef __LITTLE_ENDIAN
228 "1:\tlbu\t%0, 1(%2)\n"
229 "2:\tlbu\t$1, 0(%2)\n\t"
235 ".section\t.fixup,\"ax\"\n\t"
239 ".section\t__ex_table,\"a\"\n\t"
240 STR(PTR
)"\t1b, 4b\n\t"
241 STR(PTR
)"\t2b, 4b\n\t"
243 : "=&r" (value
), "=r" (res
)
244 : "r" (addr
), "i" (-EFAULT
));
247 compute_return_epc(regs
);
248 regs
->regs
[insn
.i_format
.rt
] = value
;
254 * A 32-bit kernel might be running on a 64-bit processor. But
255 * if we're on a 32-bit processor and an i-cache incoherency
256 * or race makes us see a 64-bit instruction here the sdl/sdr
257 * would blow up, so for now we don't handle unaligned 64-bit
258 * instructions on 32-bit kernels.
260 if (!access_ok(VERIFY_READ
, addr
, 4))
263 __asm__
__volatile__ (
265 "1:\tlwl\t%0, (%2)\n"
266 "2:\tlwr\t%0, 3(%2)\n\t"
268 #ifdef __LITTLE_ENDIAN
269 "1:\tlwl\t%0, 3(%2)\n"
270 "2:\tlwr\t%0, (%2)\n\t"
272 "dsll\t%0, %0, 32\n\t"
273 "dsrl\t%0, %0, 32\n\t"
275 "3:\t.section\t.fixup,\"ax\"\n\t"
279 ".section\t__ex_table,\"a\"\n\t"
280 STR(PTR
)"\t1b, 4b\n\t"
281 STR(PTR
)"\t2b, 4b\n\t"
283 : "=&r" (value
), "=r" (res
)
284 : "r" (addr
), "i" (-EFAULT
));
287 compute_return_epc(regs
);
288 regs
->regs
[insn
.i_format
.rt
] = value
;
290 #endif /* CONFIG_64BIT */
292 /* Cannot handle 64-bit instructions in 32-bit kernel */
298 * A 32-bit kernel might be running on a 64-bit processor. But
299 * if we're on a 32-bit processor and an i-cache incoherency
300 * or race makes us see a 64-bit instruction here the sdl/sdr
301 * would blow up, so for now we don't handle unaligned 64-bit
302 * instructions on 32-bit kernels.
304 if (!access_ok(VERIFY_READ
, addr
, 8))
307 __asm__
__volatile__ (
309 "1:\tldl\t%0, (%2)\n"
310 "2:\tldr\t%0, 7(%2)\n\t"
312 #ifdef __LITTLE_ENDIAN
313 "1:\tldl\t%0, 7(%2)\n"
314 "2:\tldr\t%0, (%2)\n\t"
317 "3:\t.section\t.fixup,\"ax\"\n\t"
321 ".section\t__ex_table,\"a\"\n\t"
322 STR(PTR
)"\t1b, 4b\n\t"
323 STR(PTR
)"\t2b, 4b\n\t"
325 : "=&r" (value
), "=r" (res
)
326 : "r" (addr
), "i" (-EFAULT
));
329 compute_return_epc(regs
);
330 regs
->regs
[insn
.i_format
.rt
] = value
;
332 #endif /* CONFIG_64BIT */
334 /* Cannot handle 64-bit instructions in 32-bit kernel */
338 if (!access_ok(VERIFY_WRITE
, addr
, 2))
341 value
= regs
->regs
[insn
.i_format
.rt
];
342 __asm__
__volatile__ (
345 "1:\tsb\t%1, 1(%2)\n\t"
347 "2:\tsb\t$1, 0(%2)\n\t"
350 #ifdef __LITTLE_ENDIAN
352 "1:\tsb\t%1, 0(%2)\n\t"
354 "2:\tsb\t$1, 1(%2)\n\t"
359 ".section\t.fixup,\"ax\"\n\t"
363 ".section\t__ex_table,\"a\"\n\t"
364 STR(PTR
)"\t1b, 4b\n\t"
365 STR(PTR
)"\t2b, 4b\n\t"
368 : "r" (value
), "r" (addr
), "i" (-EFAULT
));
371 compute_return_epc(regs
);
375 if (!access_ok(VERIFY_WRITE
, addr
, 4))
378 value
= regs
->regs
[insn
.i_format
.rt
];
379 __asm__
__volatile__ (
382 "2:\tswr\t%1, 3(%2)\n\t"
384 #ifdef __LITTLE_ENDIAN
385 "1:\tswl\t%1, 3(%2)\n"
386 "2:\tswr\t%1, (%2)\n\t"
390 ".section\t.fixup,\"ax\"\n\t"
394 ".section\t__ex_table,\"a\"\n\t"
395 STR(PTR
)"\t1b, 4b\n\t"
396 STR(PTR
)"\t2b, 4b\n\t"
399 : "r" (value
), "r" (addr
), "i" (-EFAULT
));
402 compute_return_epc(regs
);
408 * A 32-bit kernel might be running on a 64-bit processor. But
409 * if we're on a 32-bit processor and an i-cache incoherency
410 * or race makes us see a 64-bit instruction here the sdl/sdr
411 * would blow up, so for now we don't handle unaligned 64-bit
412 * instructions on 32-bit kernels.
414 if (!access_ok(VERIFY_WRITE
, addr
, 8))
417 value
= regs
->regs
[insn
.i_format
.rt
];
418 __asm__
__volatile__ (
421 "2:\tsdr\t%1, 7(%2)\n\t"
423 #ifdef __LITTLE_ENDIAN
424 "1:\tsdl\t%1, 7(%2)\n"
425 "2:\tsdr\t%1, (%2)\n\t"
429 ".section\t.fixup,\"ax\"\n\t"
433 ".section\t__ex_table,\"a\"\n\t"
434 STR(PTR
)"\t1b, 4b\n\t"
435 STR(PTR
)"\t2b, 4b\n\t"
438 : "r" (value
), "r" (addr
), "i" (-EFAULT
));
441 compute_return_epc(regs
);
443 #endif /* CONFIG_64BIT */
445 /* Cannot handle 64-bit instructions in 32-bit kernel */
453 * I herewith declare: this does not happen. So send SIGBUS.
458 * COP2 is available to implementor for application specific use.
459 * It's up to applications to register a notifier chain and do
460 * whatever they have to do, including possible sending of signals.
463 cu2_notifier_call_chain(CU2_LWC2_OP
, regs
);
467 cu2_notifier_call_chain(CU2_LDC2_OP
, regs
);
471 cu2_notifier_call_chain(CU2_SWC2_OP
, regs
);
475 cu2_notifier_call_chain(CU2_SDC2_OP
, regs
);
480 * Pheeee... We encountered an yet unknown instruction or
481 * cache coherence problem. Die sucker, die ...
486 #ifdef CONFIG_DEBUG_FS
487 unaligned_instructions
++;
493 /* Did we have an exception handler installed? */
494 if (fixup_exception(regs
))
497 die_if_kernel("Unhandled kernel unaligned access", regs
);
498 force_sig(SIGSEGV
, current
);
503 die_if_kernel("Unhandled kernel unaligned access", regs
);
504 force_sig(SIGBUS
, current
);
509 die_if_kernel("Unhandled kernel unaligned access or invalid instruction", regs
);
510 force_sig(SIGILL
, current
);
513 asmlinkage
void do_ade(struct pt_regs
*regs
)
515 unsigned int __user
*pc
;
518 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
,
519 1, regs
, regs
->cp0_badvaddr
);
521 * Did we catch a fault trying to load an instruction?
522 * Or are we running in MIPS16 mode?
524 if ((regs
->cp0_badvaddr
== regs
->cp0_epc
) || (regs
->cp0_epc
& 0x1))
527 pc
= (unsigned int __user
*) exception_epc(regs
);
528 if (user_mode(regs
) && !test_thread_flag(TIF_FIXADE
))
530 if (unaligned_action
== UNALIGNED_ACTION_SIGNAL
)
532 else if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
533 show_registers(regs
);
536 * Do branch emulation only if we didn't forward the exception.
537 * This is all so but ugly ...
540 if (!user_mode(regs
))
542 emulate_load_store_insn(regs
, (void __user
*)regs
->cp0_badvaddr
, pc
);
548 die_if_kernel("Kernel unaligned instruction access", regs
);
549 force_sig(SIGBUS
, current
);
552 * XXX On return from the signal handler we should advance the epc
556 #ifdef CONFIG_DEBUG_FS
557 extern struct dentry
*mips_debugfs_dir
;
558 static int __init
debugfs_unaligned(void)
562 if (!mips_debugfs_dir
)
564 d
= debugfs_create_u32("unaligned_instructions", S_IRUGO
,
565 mips_debugfs_dir
, &unaligned_instructions
);
568 d
= debugfs_create_u32("unaligned_action", S_IRUGO
| S_IWUSR
,
569 mips_debugfs_dir
, &unaligned_action
);
574 __initcall(debugfs_unaligned
);