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 #include <asm/fpu_emulator.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
);
108 #define LoadHW(addr, value, res) \
109 __asm__ __volatile__ (".set\tnoat\n" \
110 "1:\tlb\t%0, 0(%2)\n" \
111 "2:\tlbu\t$1, 1(%2)\n\t" \
117 ".section\t.fixup,\"ax\"\n\t" \
118 "4:\tli\t%1, %3\n\t" \
121 ".section\t__ex_table,\"a\"\n\t" \
122 STR(PTR)"\t1b, 4b\n\t" \
123 STR(PTR)"\t2b, 4b\n\t" \
125 : "=&r" (value), "=r" (res) \
126 : "r" (addr), "i" (-EFAULT));
128 #define LoadW(addr, value, res) \
129 __asm__ __volatile__ ( \
130 "1:\tlwl\t%0, (%2)\n" \
131 "2:\tlwr\t%0, 3(%2)\n\t" \
135 ".section\t.fixup,\"ax\"\n\t" \
136 "4:\tli\t%1, %3\n\t" \
139 ".section\t__ex_table,\"a\"\n\t" \
140 STR(PTR)"\t1b, 4b\n\t" \
141 STR(PTR)"\t2b, 4b\n\t" \
143 : "=&r" (value), "=r" (res) \
144 : "r" (addr), "i" (-EFAULT));
146 #define LoadHWU(addr, value, res) \
147 __asm__ __volatile__ ( \
149 "1:\tlbu\t%0, 0(%2)\n" \
150 "2:\tlbu\t$1, 1(%2)\n\t" \
157 ".section\t.fixup,\"ax\"\n\t" \
158 "4:\tli\t%1, %3\n\t" \
161 ".section\t__ex_table,\"a\"\n\t" \
162 STR(PTR)"\t1b, 4b\n\t" \
163 STR(PTR)"\t2b, 4b\n\t" \
165 : "=&r" (value), "=r" (res) \
166 : "r" (addr), "i" (-EFAULT));
168 #define LoadWU(addr, value, res) \
169 __asm__ __volatile__ ( \
170 "1:\tlwl\t%0, (%2)\n" \
171 "2:\tlwr\t%0, 3(%2)\n\t" \
172 "dsll\t%0, %0, 32\n\t" \
173 "dsrl\t%0, %0, 32\n\t" \
177 "\t.section\t.fixup,\"ax\"\n\t" \
178 "4:\tli\t%1, %3\n\t" \
181 ".section\t__ex_table,\"a\"\n\t" \
182 STR(PTR)"\t1b, 4b\n\t" \
183 STR(PTR)"\t2b, 4b\n\t" \
185 : "=&r" (value), "=r" (res) \
186 : "r" (addr), "i" (-EFAULT));
188 #define LoadDW(addr, value, res) \
189 __asm__ __volatile__ ( \
190 "1:\tldl\t%0, (%2)\n" \
191 "2:\tldr\t%0, 7(%2)\n\t" \
195 "\t.section\t.fixup,\"ax\"\n\t" \
196 "4:\tli\t%1, %3\n\t" \
199 ".section\t__ex_table,\"a\"\n\t" \
200 STR(PTR)"\t1b, 4b\n\t" \
201 STR(PTR)"\t2b, 4b\n\t" \
203 : "=&r" (value), "=r" (res) \
204 : "r" (addr), "i" (-EFAULT));
206 #define StoreHW(addr, value, res) \
207 __asm__ __volatile__ ( \
209 "1:\tsb\t%1, 1(%2)\n\t" \
210 "srl\t$1, %1, 0x8\n" \
211 "2:\tsb\t$1, 0(%2)\n\t" \
216 ".section\t.fixup,\"ax\"\n\t" \
217 "4:\tli\t%0, %3\n\t" \
220 ".section\t__ex_table,\"a\"\n\t" \
221 STR(PTR)"\t1b, 4b\n\t" \
222 STR(PTR)"\t2b, 4b\n\t" \
225 : "r" (value), "r" (addr), "i" (-EFAULT));
227 #define StoreW(addr, value, res) \
228 __asm__ __volatile__ ( \
229 "1:\tswl\t%1,(%2)\n" \
230 "2:\tswr\t%1, 3(%2)\n\t" \
234 ".section\t.fixup,\"ax\"\n\t" \
235 "4:\tli\t%0, %3\n\t" \
238 ".section\t__ex_table,\"a\"\n\t" \
239 STR(PTR)"\t1b, 4b\n\t" \
240 STR(PTR)"\t2b, 4b\n\t" \
243 : "r" (value), "r" (addr), "i" (-EFAULT));
245 #define StoreDW(addr, value, res) \
246 __asm__ __volatile__ ( \
247 "1:\tsdl\t%1,(%2)\n" \
248 "2:\tsdr\t%1, 7(%2)\n\t" \
252 ".section\t.fixup,\"ax\"\n\t" \
253 "4:\tli\t%0, %3\n\t" \
256 ".section\t__ex_table,\"a\"\n\t" \
257 STR(PTR)"\t1b, 4b\n\t" \
258 STR(PTR)"\t2b, 4b\n\t" \
261 : "r" (value), "r" (addr), "i" (-EFAULT));
264 #ifdef __LITTLE_ENDIAN
265 #define LoadHW(addr, value, res) \
266 __asm__ __volatile__ (".set\tnoat\n" \
267 "1:\tlb\t%0, 1(%2)\n" \
268 "2:\tlbu\t$1, 0(%2)\n\t" \
274 ".section\t.fixup,\"ax\"\n\t" \
275 "4:\tli\t%1, %3\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));
285 #define LoadW(addr, value, res) \
286 __asm__ __volatile__ ( \
287 "1:\tlwl\t%0, 3(%2)\n" \
288 "2:\tlwr\t%0, (%2)\n\t" \
292 ".section\t.fixup,\"ax\"\n\t" \
293 "4:\tli\t%1, %3\n\t" \
296 ".section\t__ex_table,\"a\"\n\t" \
297 STR(PTR)"\t1b, 4b\n\t" \
298 STR(PTR)"\t2b, 4b\n\t" \
300 : "=&r" (value), "=r" (res) \
301 : "r" (addr), "i" (-EFAULT));
303 #define LoadHWU(addr, value, res) \
304 __asm__ __volatile__ ( \
306 "1:\tlbu\t%0, 1(%2)\n" \
307 "2:\tlbu\t$1, 0(%2)\n\t" \
314 ".section\t.fixup,\"ax\"\n\t" \
315 "4:\tli\t%1, %3\n\t" \
318 ".section\t__ex_table,\"a\"\n\t" \
319 STR(PTR)"\t1b, 4b\n\t" \
320 STR(PTR)"\t2b, 4b\n\t" \
322 : "=&r" (value), "=r" (res) \
323 : "r" (addr), "i" (-EFAULT));
325 #define LoadWU(addr, value, res) \
326 __asm__ __volatile__ ( \
327 "1:\tlwl\t%0, 3(%2)\n" \
328 "2:\tlwr\t%0, (%2)\n\t" \
329 "dsll\t%0, %0, 32\n\t" \
330 "dsrl\t%0, %0, 32\n\t" \
334 "\t.section\t.fixup,\"ax\"\n\t" \
335 "4:\tli\t%1, %3\n\t" \
338 ".section\t__ex_table,\"a\"\n\t" \
339 STR(PTR)"\t1b, 4b\n\t" \
340 STR(PTR)"\t2b, 4b\n\t" \
342 : "=&r" (value), "=r" (res) \
343 : "r" (addr), "i" (-EFAULT));
345 #define LoadDW(addr, value, res) \
346 __asm__ __volatile__ ( \
347 "1:\tldl\t%0, 7(%2)\n" \
348 "2:\tldr\t%0, (%2)\n\t" \
352 "\t.section\t.fixup,\"ax\"\n\t" \
353 "4:\tli\t%1, %3\n\t" \
356 ".section\t__ex_table,\"a\"\n\t" \
357 STR(PTR)"\t1b, 4b\n\t" \
358 STR(PTR)"\t2b, 4b\n\t" \
360 : "=&r" (value), "=r" (res) \
361 : "r" (addr), "i" (-EFAULT));
363 #define StoreHW(addr, value, res) \
364 __asm__ __volatile__ ( \
366 "1:\tsb\t%1, 0(%2)\n\t" \
367 "srl\t$1,%1, 0x8\n" \
368 "2:\tsb\t$1, 1(%2)\n\t" \
373 ".section\t.fixup,\"ax\"\n\t" \
374 "4:\tli\t%0, %3\n\t" \
377 ".section\t__ex_table,\"a\"\n\t" \
378 STR(PTR)"\t1b, 4b\n\t" \
379 STR(PTR)"\t2b, 4b\n\t" \
382 : "r" (value), "r" (addr), "i" (-EFAULT));
384 #define StoreW(addr, value, res) \
385 __asm__ __volatile__ ( \
386 "1:\tswl\t%1, 3(%2)\n" \
387 "2:\tswr\t%1, (%2)\n\t" \
391 ".section\t.fixup,\"ax\"\n\t" \
392 "4:\tli\t%0, %3\n\t" \
395 ".section\t__ex_table,\"a\"\n\t" \
396 STR(PTR)"\t1b, 4b\n\t" \
397 STR(PTR)"\t2b, 4b\n\t" \
400 : "r" (value), "r" (addr), "i" (-EFAULT));
402 #define StoreDW(addr, value, res) \
403 __asm__ __volatile__ ( \
404 "1:\tsdl\t%1, 7(%2)\n" \
405 "2:\tsdr\t%1, (%2)\n\t" \
409 ".section\t.fixup,\"ax\"\n\t" \
410 "4:\tli\t%0, %3\n\t" \
413 ".section\t__ex_table,\"a\"\n\t" \
414 STR(PTR)"\t1b, 4b\n\t" \
415 STR(PTR)"\t2b, 4b\n\t" \
418 : "r" (value), "r" (addr), "i" (-EFAULT));
421 static void emulate_load_store_insn(struct pt_regs
*regs
,
423 unsigned int __user
*pc
)
425 union mips_instruction insn
;
428 unsigned long origpc
;
429 unsigned long orig31
;
430 void __user
*fault_addr
= NULL
;
432 origpc
= (unsigned long)pc
;
433 orig31
= regs
->regs
[31];
436 * This load never faults.
438 __get_user(insn
.word
, pc
);
440 switch (insn
.i_format
.opcode
) {
442 * These are instructions that a compiler doesn't generate. We
443 * can assume therefore that the code is MIPS-aware and
444 * really buggy. Emulating these instructions would break the
453 * For these instructions the only way to create an address
454 * error is an attempted access to kernel/supervisor address
471 * The remaining opcodes are the ones that are really of
475 if (!access_ok(VERIFY_READ
, addr
, 2))
478 LoadHW(addr
, value
, res
);
481 compute_return_epc(regs
);
482 regs
->regs
[insn
.i_format
.rt
] = value
;
486 if (!access_ok(VERIFY_READ
, addr
, 4))
489 LoadW(addr
, value
, res
);
492 compute_return_epc(regs
);
493 regs
->regs
[insn
.i_format
.rt
] = value
;
497 if (!access_ok(VERIFY_READ
, addr
, 2))
500 LoadHWU(addr
, value
, res
);
503 compute_return_epc(regs
);
504 regs
->regs
[insn
.i_format
.rt
] = value
;
510 * A 32-bit kernel might be running on a 64-bit processor. But
511 * if we're on a 32-bit processor and an i-cache incoherency
512 * or race makes us see a 64-bit instruction here the sdl/sdr
513 * would blow up, so for now we don't handle unaligned 64-bit
514 * instructions on 32-bit kernels.
516 if (!access_ok(VERIFY_READ
, addr
, 4))
519 LoadWU(addr
, value
, res
);
522 compute_return_epc(regs
);
523 regs
->regs
[insn
.i_format
.rt
] = value
;
525 #endif /* CONFIG_64BIT */
527 /* Cannot handle 64-bit instructions in 32-bit kernel */
533 * A 32-bit kernel might be running on a 64-bit processor. But
534 * if we're on a 32-bit processor and an i-cache incoherency
535 * or race makes us see a 64-bit instruction here the sdl/sdr
536 * would blow up, so for now we don't handle unaligned 64-bit
537 * instructions on 32-bit kernels.
539 if (!access_ok(VERIFY_READ
, addr
, 8))
542 LoadDW(addr
, value
, res
);
545 compute_return_epc(regs
);
546 regs
->regs
[insn
.i_format
.rt
] = value
;
548 #endif /* CONFIG_64BIT */
550 /* Cannot handle 64-bit instructions in 32-bit kernel */
554 if (!access_ok(VERIFY_WRITE
, addr
, 2))
557 compute_return_epc(regs
);
558 value
= regs
->regs
[insn
.i_format
.rt
];
559 StoreHW(addr
, value
, res
);
565 if (!access_ok(VERIFY_WRITE
, addr
, 4))
568 compute_return_epc(regs
);
569 value
= regs
->regs
[insn
.i_format
.rt
];
570 StoreW(addr
, value
, res
);
578 * A 32-bit kernel might be running on a 64-bit processor. But
579 * if we're on a 32-bit processor and an i-cache incoherency
580 * or race makes us see a 64-bit instruction here the sdl/sdr
581 * would blow up, so for now we don't handle unaligned 64-bit
582 * instructions on 32-bit kernels.
584 if (!access_ok(VERIFY_WRITE
, addr
, 8))
587 compute_return_epc(regs
);
588 value
= regs
->regs
[insn
.i_format
.rt
];
589 StoreDW(addr
, value
, res
);
593 #endif /* CONFIG_64BIT */
595 /* Cannot handle 64-bit instructions in 32-bit kernel */
602 die_if_kernel("Unaligned FP access in kernel code", regs
);
603 BUG_ON(!used_math());
604 BUG_ON(!is_fpu_owner());
606 lose_fpu(1); /* save the FPU state for the emulator */
607 res
= fpu_emulator_cop1Handler(regs
, ¤t
->thread
.fpu
, 1,
609 own_fpu(1); /* restore FPU state */
611 /* If something went wrong, signal */
612 process_fpemu_return(res
, fault_addr
);
619 * COP2 is available to implementor for application specific use.
620 * It's up to applications to register a notifier chain and do
621 * whatever they have to do, including possible sending of signals.
624 cu2_notifier_call_chain(CU2_LWC2_OP
, regs
);
628 cu2_notifier_call_chain(CU2_LDC2_OP
, regs
);
632 cu2_notifier_call_chain(CU2_SWC2_OP
, regs
);
636 cu2_notifier_call_chain(CU2_SDC2_OP
, regs
);
641 * Pheeee... We encountered an yet unknown instruction or
642 * cache coherence problem. Die sucker, die ...
647 #ifdef CONFIG_DEBUG_FS
648 unaligned_instructions
++;
654 /* roll back jump/branch */
655 regs
->cp0_epc
= origpc
;
656 regs
->regs
[31] = orig31
;
657 /* Did we have an exception handler installed? */
658 if (fixup_exception(regs
))
661 die_if_kernel("Unhandled kernel unaligned access", regs
);
662 force_sig(SIGSEGV
, current
);
667 die_if_kernel("Unhandled kernel unaligned access", regs
);
668 force_sig(SIGBUS
, current
);
674 ("Unhandled kernel unaligned access or invalid instruction", regs
);
675 force_sig(SIGILL
, current
);
678 /* recode table from micromips register notation to GPR */
679 static int mmreg16to32
[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
681 /* recode table from micromips STORE register notation to GPR */
682 static int mmreg16to32_st
[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
684 void emulate_load_store_microMIPS(struct pt_regs
*regs
, void __user
* addr
)
689 unsigned int reg
= 0, rvar
;
690 unsigned long orig31
;
694 unsigned long origpc
, contpc
;
695 union mips_instruction insn
;
696 struct decoded_instn mminst
;
697 void __user
*fault_addr
= NULL
;
699 origpc
= regs
->cp0_epc
;
700 orig31
= regs
->regs
[31];
702 mminst
.micro_mips_mode
= 1;
705 * This load never faults.
707 pc16
= (unsigned short __user
*)(regs
->cp0_epc
& ~MIPS_ISA_MODE
);
708 __get_user(halfword
, pc16
);
710 contpc
= regs
->cp0_epc
+ 2;
711 word
= ((unsigned int)halfword
<< 16);
714 if (!mm_is16bit(halfword
)) {
715 __get_user(halfword
, pc16
);
717 contpc
= regs
->cp0_epc
+ 4;
723 if (get_user(halfword
, pc16
))
725 mminst
.next_pc_inc
= 2;
726 word
= ((unsigned int)halfword
<< 16);
728 if (!mm_is16bit(halfword
)) {
730 if (get_user(halfword
, pc16
))
732 mminst
.next_pc_inc
= 4;
735 mminst
.next_insn
= word
;
737 insn
= (union mips_instruction
)(mminst
.insn
);
738 if (mm_isBranchInstr(regs
, mminst
, &contpc
))
739 insn
= (union mips_instruction
)(mminst
.next_insn
);
741 /* Parse instruction to find what to do */
743 switch (insn
.mm_i_format
.opcode
) {
746 switch (insn
.mm_x_format
.func
) {
748 reg
= insn
.mm_x_format
.rd
;
755 switch (insn
.mm_m_format
.func
) {
757 reg
= insn
.mm_m_format
.rd
;
761 if (!access_ok(VERIFY_READ
, addr
, 8))
764 LoadW(addr
, value
, res
);
767 regs
->regs
[reg
] = value
;
769 LoadW(addr
, value
, res
);
772 regs
->regs
[reg
+ 1] = value
;
776 reg
= insn
.mm_m_format
.rd
;
780 if (!access_ok(VERIFY_WRITE
, addr
, 8))
783 value
= regs
->regs
[reg
];
784 StoreW(addr
, value
, res
);
788 value
= regs
->regs
[reg
+ 1];
789 StoreW(addr
, value
, res
);
796 reg
= insn
.mm_m_format
.rd
;
800 if (!access_ok(VERIFY_READ
, addr
, 16))
803 LoadDW(addr
, value
, res
);
806 regs
->regs
[reg
] = value
;
808 LoadDW(addr
, value
, res
);
811 regs
->regs
[reg
+ 1] = value
;
813 #endif /* CONFIG_64BIT */
819 reg
= insn
.mm_m_format
.rd
;
823 if (!access_ok(VERIFY_WRITE
, addr
, 16))
826 value
= regs
->regs
[reg
];
827 StoreDW(addr
, value
, res
);
831 value
= regs
->regs
[reg
+ 1];
832 StoreDW(addr
, value
, res
);
836 #endif /* CONFIG_64BIT */
841 reg
= insn
.mm_m_format
.rd
;
843 if ((rvar
> 9) || !reg
)
847 (VERIFY_READ
, addr
, 4 * (rvar
+ 1)))
850 if (!access_ok(VERIFY_READ
, addr
, 4 * rvar
))
855 for (i
= 16; rvar
; rvar
--, i
++) {
856 LoadW(addr
, value
, res
);
860 regs
->regs
[i
] = value
;
862 if ((reg
& 0xf) == 9) {
863 LoadW(addr
, value
, res
);
867 regs
->regs
[30] = value
;
870 LoadW(addr
, value
, res
);
873 regs
->regs
[31] = value
;
878 reg
= insn
.mm_m_format
.rd
;
880 if ((rvar
> 9) || !reg
)
884 (VERIFY_WRITE
, addr
, 4 * (rvar
+ 1)))
887 if (!access_ok(VERIFY_WRITE
, addr
, 4 * rvar
))
892 for (i
= 16; rvar
; rvar
--, i
++) {
893 value
= regs
->regs
[i
];
894 StoreW(addr
, value
, res
);
899 if ((reg
& 0xf) == 9) {
900 value
= regs
->regs
[30];
901 StoreW(addr
, value
, res
);
907 value
= regs
->regs
[31];
908 StoreW(addr
, value
, res
);
916 reg
= insn
.mm_m_format
.rd
;
918 if ((rvar
> 9) || !reg
)
922 (VERIFY_READ
, addr
, 8 * (rvar
+ 1)))
925 if (!access_ok(VERIFY_READ
, addr
, 8 * rvar
))
931 for (i
= 16; rvar
; rvar
--, i
++) {
932 LoadDW(addr
, value
, res
);
936 regs
->regs
[i
] = value
;
938 if ((reg
& 0xf) == 9) {
939 LoadDW(addr
, value
, res
);
943 regs
->regs
[30] = value
;
946 LoadDW(addr
, value
, res
);
949 regs
->regs
[31] = value
;
952 #endif /* CONFIG_64BIT */
958 reg
= insn
.mm_m_format
.rd
;
960 if ((rvar
> 9) || !reg
)
964 (VERIFY_WRITE
, addr
, 8 * (rvar
+ 1)))
967 if (!access_ok(VERIFY_WRITE
, addr
, 8 * rvar
))
973 for (i
= 16; rvar
; rvar
--, i
++) {
974 value
= regs
->regs
[i
];
975 StoreDW(addr
, value
, res
);
980 if ((reg
& 0xf) == 9) {
981 value
= regs
->regs
[30];
982 StoreDW(addr
, value
, res
);
988 value
= regs
->regs
[31];
989 StoreDW(addr
, value
, res
);
994 #endif /* CONFIG_64BIT */
998 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1004 switch (insn
.mm_m_format
.func
) {
1006 reg
= insn
.mm_m_format
.rd
;
1010 /* LL,SC,LLD,SCD are not serviced */
1014 switch (insn
.mm_x_format
.func
) {
1029 /* roll back jump/branch */
1030 regs
->cp0_epc
= origpc
;
1031 regs
->regs
[31] = orig31
;
1033 die_if_kernel("Unaligned FP access in kernel code", regs
);
1034 BUG_ON(!used_math());
1035 BUG_ON(!is_fpu_owner());
1037 lose_fpu(1); /* save the FPU state for the emulator */
1038 res
= fpu_emulator_cop1Handler(regs
, ¤t
->thread
.fpu
, 1,
1040 own_fpu(1); /* restore FPU state */
1042 /* If something went wrong, signal */
1043 process_fpemu_return(res
, fault_addr
);
1050 reg
= insn
.mm_i_format
.rt
;
1054 reg
= insn
.mm_i_format
.rt
;
1058 reg
= insn
.mm_i_format
.rt
;
1062 reg
= insn
.mm_i_format
.rt
;
1066 reg
= insn
.mm_i_format
.rt
;
1070 reg
= insn
.mm_i_format
.rt
;
1074 reg
= insn
.mm_i_format
.rt
;
1078 switch (insn
.mm16_m_format
.func
) {
1080 reg
= insn
.mm16_m_format
.rlist
;
1082 if (!access_ok(VERIFY_READ
, addr
, 4 * rvar
))
1085 for (i
= 16; rvar
; rvar
--, i
++) {
1086 LoadW(addr
, value
, res
);
1090 regs
->regs
[i
] = value
;
1092 LoadW(addr
, value
, res
);
1095 regs
->regs
[31] = value
;
1100 reg
= insn
.mm16_m_format
.rlist
;
1102 if (!access_ok(VERIFY_WRITE
, addr
, 4 * rvar
))
1105 for (i
= 16; rvar
; rvar
--, i
++) {
1106 value
= regs
->regs
[i
];
1107 StoreW(addr
, value
, res
);
1112 value
= regs
->regs
[31];
1113 StoreW(addr
, value
, res
);
1124 reg
= mmreg16to32
[insn
.mm16_rb_format
.rt
];
1128 reg
= mmreg16to32
[insn
.mm16_rb_format
.rt
];
1132 reg
= mmreg16to32_st
[insn
.mm16_rb_format
.rt
];
1136 reg
= mmreg16to32_st
[insn
.mm16_rb_format
.rt
];
1140 reg
= insn
.mm16_r5_format
.rt
;
1144 reg
= insn
.mm16_r5_format
.rt
;
1148 reg
= mmreg16to32
[insn
.mm16_r3_format
.rt
];
1156 if (!access_ok(VERIFY_READ
, addr
, 2))
1159 LoadHW(addr
, value
, res
);
1162 regs
->regs
[reg
] = value
;
1166 if (!access_ok(VERIFY_READ
, addr
, 2))
1169 LoadHWU(addr
, value
, res
);
1172 regs
->regs
[reg
] = value
;
1176 if (!access_ok(VERIFY_READ
, addr
, 4))
1179 LoadW(addr
, value
, res
);
1182 regs
->regs
[reg
] = value
;
1188 * A 32-bit kernel might be running on a 64-bit processor. But
1189 * if we're on a 32-bit processor and an i-cache incoherency
1190 * or race makes us see a 64-bit instruction here the sdl/sdr
1191 * would blow up, so for now we don't handle unaligned 64-bit
1192 * instructions on 32-bit kernels.
1194 if (!access_ok(VERIFY_READ
, addr
, 4))
1197 LoadWU(addr
, value
, res
);
1200 regs
->regs
[reg
] = value
;
1202 #endif /* CONFIG_64BIT */
1204 /* Cannot handle 64-bit instructions in 32-bit kernel */
1210 * A 32-bit kernel might be running on a 64-bit processor. But
1211 * if we're on a 32-bit processor and an i-cache incoherency
1212 * or race makes us see a 64-bit instruction here the sdl/sdr
1213 * would blow up, so for now we don't handle unaligned 64-bit
1214 * instructions on 32-bit kernels.
1216 if (!access_ok(VERIFY_READ
, addr
, 8))
1219 LoadDW(addr
, value
, res
);
1222 regs
->regs
[reg
] = value
;
1224 #endif /* CONFIG_64BIT */
1226 /* Cannot handle 64-bit instructions in 32-bit kernel */
1230 if (!access_ok(VERIFY_WRITE
, addr
, 2))
1233 value
= regs
->regs
[reg
];
1234 StoreHW(addr
, value
, res
);
1240 if (!access_ok(VERIFY_WRITE
, addr
, 4))
1243 value
= regs
->regs
[reg
];
1244 StoreW(addr
, value
, res
);
1252 * A 32-bit kernel might be running on a 64-bit processor. But
1253 * if we're on a 32-bit processor and an i-cache incoherency
1254 * or race makes us see a 64-bit instruction here the sdl/sdr
1255 * would blow up, so for now we don't handle unaligned 64-bit
1256 * instructions on 32-bit kernels.
1258 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1261 value
= regs
->regs
[reg
];
1262 StoreDW(addr
, value
, res
);
1266 #endif /* CONFIG_64BIT */
1268 /* Cannot handle 64-bit instructions in 32-bit kernel */
1272 regs
->cp0_epc
= contpc
; /* advance or branch */
1274 #ifdef CONFIG_DEBUG_FS
1275 unaligned_instructions
++;
1280 /* roll back jump/branch */
1281 regs
->cp0_epc
= origpc
;
1282 regs
->regs
[31] = orig31
;
1283 /* Did we have an exception handler installed? */
1284 if (fixup_exception(regs
))
1287 die_if_kernel("Unhandled kernel unaligned access", regs
);
1288 force_sig(SIGSEGV
, current
);
1293 die_if_kernel("Unhandled kernel unaligned access", regs
);
1294 force_sig(SIGBUS
, current
);
1300 ("Unhandled kernel unaligned access or invalid instruction", regs
);
1301 force_sig(SIGILL
, current
);
1304 /* recode table from MIPS16e register notation to GPR */
1305 int mips16e_reg2gpr
[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
1307 static void emulate_load_store_MIPS16e(struct pt_regs
*regs
, void __user
* addr
)
1309 unsigned long value
;
1312 unsigned long orig31
;
1314 unsigned long origpc
;
1315 union mips16e_instruction mips16inst
, oldinst
;
1317 origpc
= regs
->cp0_epc
;
1318 orig31
= regs
->regs
[31];
1319 pc16
= (unsigned short __user
*)(origpc
& ~MIPS_ISA_MODE
);
1321 * This load never faults.
1323 __get_user(mips16inst
.full
, pc16
);
1324 oldinst
= mips16inst
;
1326 /* skip EXTEND instruction */
1327 if (mips16inst
.ri
.opcode
== MIPS16e_extend_op
) {
1329 __get_user(mips16inst
.full
, pc16
);
1330 } else if (delay_slot(regs
)) {
1331 /* skip jump instructions */
1332 /* JAL/JALX are 32 bits but have OPCODE in first short int */
1333 if (mips16inst
.ri
.opcode
== MIPS16e_jal_op
)
1336 if (get_user(mips16inst
.full
, pc16
))
1340 switch (mips16inst
.ri
.opcode
) {
1341 case MIPS16e_i64_op
: /* I64 or RI64 instruction */
1342 switch (mips16inst
.i64
.func
) { /* I64/RI64 func field check */
1343 case MIPS16e_ldpc_func
:
1344 case MIPS16e_ldsp_func
:
1345 reg
= mips16e_reg2gpr
[mips16inst
.ri64
.ry
];
1348 case MIPS16e_sdsp_func
:
1349 reg
= mips16e_reg2gpr
[mips16inst
.ri64
.ry
];
1352 case MIPS16e_sdrasp_func
:
1353 reg
= 29; /* GPRSP */
1359 case MIPS16e_swsp_op
:
1360 case MIPS16e_lwpc_op
:
1361 case MIPS16e_lwsp_op
:
1362 reg
= mips16e_reg2gpr
[mips16inst
.ri
.rx
];
1366 if (mips16inst
.i8
.func
!= MIPS16e_swrasp_func
)
1368 reg
= 29; /* GPRSP */
1372 reg
= mips16e_reg2gpr
[mips16inst
.rri
.ry
];
1376 switch (mips16inst
.ri
.opcode
) {
1379 case MIPS16e_lbu_op
:
1384 if (!access_ok(VERIFY_READ
, addr
, 2))
1387 LoadHW(addr
, value
, res
);
1390 MIPS16e_compute_return_epc(regs
, &oldinst
);
1391 regs
->regs
[reg
] = value
;
1394 case MIPS16e_lhu_op
:
1395 if (!access_ok(VERIFY_READ
, addr
, 2))
1398 LoadHWU(addr
, value
, res
);
1401 MIPS16e_compute_return_epc(regs
, &oldinst
);
1402 regs
->regs
[reg
] = value
;
1406 case MIPS16e_lwpc_op
:
1407 case MIPS16e_lwsp_op
:
1408 if (!access_ok(VERIFY_READ
, addr
, 4))
1411 LoadW(addr
, value
, res
);
1414 MIPS16e_compute_return_epc(regs
, &oldinst
);
1415 regs
->regs
[reg
] = value
;
1418 case MIPS16e_lwu_op
:
1421 * A 32-bit kernel might be running on a 64-bit processor. But
1422 * if we're on a 32-bit processor and an i-cache incoherency
1423 * or race makes us see a 64-bit instruction here the sdl/sdr
1424 * would blow up, so for now we don't handle unaligned 64-bit
1425 * instructions on 32-bit kernels.
1427 if (!access_ok(VERIFY_READ
, addr
, 4))
1430 LoadWU(addr
, value
, res
);
1433 MIPS16e_compute_return_epc(regs
, &oldinst
);
1434 regs
->regs
[reg
] = value
;
1436 #endif /* CONFIG_64BIT */
1438 /* Cannot handle 64-bit instructions in 32-bit kernel */
1445 * A 32-bit kernel might be running on a 64-bit processor. But
1446 * if we're on a 32-bit processor and an i-cache incoherency
1447 * or race makes us see a 64-bit instruction here the sdl/sdr
1448 * would blow up, so for now we don't handle unaligned 64-bit
1449 * instructions on 32-bit kernels.
1451 if (!access_ok(VERIFY_READ
, addr
, 8))
1454 LoadDW(addr
, value
, res
);
1457 MIPS16e_compute_return_epc(regs
, &oldinst
);
1458 regs
->regs
[reg
] = value
;
1460 #endif /* CONFIG_64BIT */
1462 /* Cannot handle 64-bit instructions in 32-bit kernel */
1466 if (!access_ok(VERIFY_WRITE
, addr
, 2))
1469 MIPS16e_compute_return_epc(regs
, &oldinst
);
1470 value
= regs
->regs
[reg
];
1471 StoreHW(addr
, value
, res
);
1477 case MIPS16e_swsp_op
:
1478 case MIPS16e_i8_op
: /* actually - MIPS16e_swrasp_func */
1479 if (!access_ok(VERIFY_WRITE
, addr
, 4))
1482 MIPS16e_compute_return_epc(regs
, &oldinst
);
1483 value
= regs
->regs
[reg
];
1484 StoreW(addr
, value
, res
);
1493 * A 32-bit kernel might be running on a 64-bit processor. But
1494 * if we're on a 32-bit processor and an i-cache incoherency
1495 * or race makes us see a 64-bit instruction here the sdl/sdr
1496 * would blow up, so for now we don't handle unaligned 64-bit
1497 * instructions on 32-bit kernels.
1499 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1502 MIPS16e_compute_return_epc(regs
, &oldinst
);
1503 value
= regs
->regs
[reg
];
1504 StoreDW(addr
, value
, res
);
1508 #endif /* CONFIG_64BIT */
1510 /* Cannot handle 64-bit instructions in 32-bit kernel */
1515 * Pheeee... We encountered an yet unknown instruction or
1516 * cache coherence problem. Die sucker, die ...
1521 #ifdef CONFIG_DEBUG_FS
1522 unaligned_instructions
++;
1528 /* roll back jump/branch */
1529 regs
->cp0_epc
= origpc
;
1530 regs
->regs
[31] = orig31
;
1531 /* Did we have an exception handler installed? */
1532 if (fixup_exception(regs
))
1535 die_if_kernel("Unhandled kernel unaligned access", regs
);
1536 force_sig(SIGSEGV
, current
);
1541 die_if_kernel("Unhandled kernel unaligned access", regs
);
1542 force_sig(SIGBUS
, current
);
1548 ("Unhandled kernel unaligned access or invalid instruction", regs
);
1549 force_sig(SIGILL
, current
);
1552 asmlinkage
void do_ade(struct pt_regs
*regs
)
1554 unsigned int __user
*pc
;
1558 * Did we catch a fault trying to load an instruction?
1560 if (regs
->cp0_badvaddr
== regs
->cp0_epc
)
1563 if (user_mode(regs
) && !test_thread_flag(TIF_FIXADE
))
1565 if (unaligned_action
== UNALIGNED_ACTION_SIGNAL
)
1569 * Do branch emulation only if we didn't forward the exception.
1570 * This is all so but ugly ...
1574 * Are we running in MIPS16e/microMIPS mode?
1576 if (is16mode(regs
)) {
1578 * Did we catch a fault trying to load an instruction in
1581 if (regs
->cp0_badvaddr
== (regs
->cp0_epc
& ~MIPS_ISA_MODE
))
1583 if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
1584 show_registers(regs
);
1586 if (cpu_has_mips16
) {
1588 if (!user_mode(regs
))
1590 emulate_load_store_MIPS16e(regs
,
1591 (void __user
*)regs
->
1598 if (cpu_has_mmips
) { /* micromips unaligned access */
1600 if (!user_mode(regs
))
1602 emulate_load_store_microMIPS(regs
,
1603 (void __user
*)regs
->
1613 if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
1614 show_registers(regs
);
1615 pc
= (unsigned int __user
*)exception_epc(regs
);
1618 if (!user_mode(regs
))
1620 emulate_load_store_insn(regs
, (void __user
*)regs
->cp0_badvaddr
, pc
);
1626 die_if_kernel("Kernel unaligned instruction access", regs
);
1627 force_sig(SIGBUS
, current
);
1631 #ifdef CONFIG_DEBUG_FS
1632 extern struct dentry
*mips_debugfs_dir
;
1633 static int __init
debugfs_unaligned(void)
1637 if (!mips_debugfs_dir
)
1639 d
= debugfs_create_u32("unaligned_instructions", S_IRUGO
,
1640 mips_debugfs_dir
, &unaligned_instructions
);
1643 d
= debugfs_create_u32("unaligned_action", S_IRUGO
| S_IWUSR
,
1644 mips_debugfs_dir
, &unaligned_action
);
1649 __initcall(debugfs_unaligned
);