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.
75 #include <linux/context_tracking.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/fpu_emulator.h>
90 #include <asm/uaccess.h>
92 #include <asm/fpu_emulator.h>
94 #define STR(x) __STR(x)
98 UNALIGNED_ACTION_QUIET
,
99 UNALIGNED_ACTION_SIGNAL
,
100 UNALIGNED_ACTION_SHOW
,
102 #ifdef CONFIG_DEBUG_FS
103 static u32 unaligned_instructions
;
104 static u32 unaligned_action
;
106 #define unaligned_action UNALIGNED_ACTION_QUIET
108 extern void show_registers(struct pt_regs
*regs
);
111 #define LoadHW(addr, value, res) \
112 __asm__ __volatile__ (".set\tnoat\n" \
113 "1:\tlb\t%0, 0(%2)\n" \
114 "2:\tlbu\t$1, 1(%2)\n\t" \
120 ".section\t.fixup,\"ax\"\n\t" \
121 "4:\tli\t%1, %3\n\t" \
124 ".section\t__ex_table,\"a\"\n\t" \
125 STR(PTR)"\t1b, 4b\n\t" \
126 STR(PTR)"\t2b, 4b\n\t" \
128 : "=&r" (value), "=r" (res) \
129 : "r" (addr), "i" (-EFAULT));
131 #define LoadW(addr, value, res) \
132 __asm__ __volatile__ ( \
133 "1:\tlwl\t%0, (%2)\n" \
134 "2:\tlwr\t%0, 3(%2)\n\t" \
138 ".section\t.fixup,\"ax\"\n\t" \
139 "4:\tli\t%1, %3\n\t" \
142 ".section\t__ex_table,\"a\"\n\t" \
143 STR(PTR)"\t1b, 4b\n\t" \
144 STR(PTR)"\t2b, 4b\n\t" \
146 : "=&r" (value), "=r" (res) \
147 : "r" (addr), "i" (-EFAULT));
149 #define LoadHWU(addr, value, res) \
150 __asm__ __volatile__ ( \
152 "1:\tlbu\t%0, 0(%2)\n" \
153 "2:\tlbu\t$1, 1(%2)\n\t" \
160 ".section\t.fixup,\"ax\"\n\t" \
161 "4:\tli\t%1, %3\n\t" \
164 ".section\t__ex_table,\"a\"\n\t" \
165 STR(PTR)"\t1b, 4b\n\t" \
166 STR(PTR)"\t2b, 4b\n\t" \
168 : "=&r" (value), "=r" (res) \
169 : "r" (addr), "i" (-EFAULT));
171 #define LoadWU(addr, value, res) \
172 __asm__ __volatile__ ( \
173 "1:\tlwl\t%0, (%2)\n" \
174 "2:\tlwr\t%0, 3(%2)\n\t" \
175 "dsll\t%0, %0, 32\n\t" \
176 "dsrl\t%0, %0, 32\n\t" \
180 "\t.section\t.fixup,\"ax\"\n\t" \
181 "4:\tli\t%1, %3\n\t" \
184 ".section\t__ex_table,\"a\"\n\t" \
185 STR(PTR)"\t1b, 4b\n\t" \
186 STR(PTR)"\t2b, 4b\n\t" \
188 : "=&r" (value), "=r" (res) \
189 : "r" (addr), "i" (-EFAULT));
191 #define LoadDW(addr, value, res) \
192 __asm__ __volatile__ ( \
193 "1:\tldl\t%0, (%2)\n" \
194 "2:\tldr\t%0, 7(%2)\n\t" \
198 "\t.section\t.fixup,\"ax\"\n\t" \
199 "4:\tli\t%1, %3\n\t" \
202 ".section\t__ex_table,\"a\"\n\t" \
203 STR(PTR)"\t1b, 4b\n\t" \
204 STR(PTR)"\t2b, 4b\n\t" \
206 : "=&r" (value), "=r" (res) \
207 : "r" (addr), "i" (-EFAULT));
209 #define StoreHW(addr, value, res) \
210 __asm__ __volatile__ ( \
212 "1:\tsb\t%1, 1(%2)\n\t" \
213 "srl\t$1, %1, 0x8\n" \
214 "2:\tsb\t$1, 0(%2)\n\t" \
219 ".section\t.fixup,\"ax\"\n\t" \
220 "4:\tli\t%0, %3\n\t" \
223 ".section\t__ex_table,\"a\"\n\t" \
224 STR(PTR)"\t1b, 4b\n\t" \
225 STR(PTR)"\t2b, 4b\n\t" \
228 : "r" (value), "r" (addr), "i" (-EFAULT));
230 #define StoreW(addr, value, res) \
231 __asm__ __volatile__ ( \
232 "1:\tswl\t%1,(%2)\n" \
233 "2:\tswr\t%1, 3(%2)\n\t" \
237 ".section\t.fixup,\"ax\"\n\t" \
238 "4:\tli\t%0, %3\n\t" \
241 ".section\t__ex_table,\"a\"\n\t" \
242 STR(PTR)"\t1b, 4b\n\t" \
243 STR(PTR)"\t2b, 4b\n\t" \
246 : "r" (value), "r" (addr), "i" (-EFAULT));
248 #define StoreDW(addr, value, res) \
249 __asm__ __volatile__ ( \
250 "1:\tsdl\t%1,(%2)\n" \
251 "2:\tsdr\t%1, 7(%2)\n\t" \
255 ".section\t.fixup,\"ax\"\n\t" \
256 "4:\tli\t%0, %3\n\t" \
259 ".section\t__ex_table,\"a\"\n\t" \
260 STR(PTR)"\t1b, 4b\n\t" \
261 STR(PTR)"\t2b, 4b\n\t" \
264 : "r" (value), "r" (addr), "i" (-EFAULT));
267 #ifdef __LITTLE_ENDIAN
268 #define LoadHW(addr, value, res) \
269 __asm__ __volatile__ (".set\tnoat\n" \
270 "1:\tlb\t%0, 1(%2)\n" \
271 "2:\tlbu\t$1, 0(%2)\n\t" \
277 ".section\t.fixup,\"ax\"\n\t" \
278 "4:\tli\t%1, %3\n\t" \
281 ".section\t__ex_table,\"a\"\n\t" \
282 STR(PTR)"\t1b, 4b\n\t" \
283 STR(PTR)"\t2b, 4b\n\t" \
285 : "=&r" (value), "=r" (res) \
286 : "r" (addr), "i" (-EFAULT));
288 #define LoadW(addr, value, res) \
289 __asm__ __volatile__ ( \
290 "1:\tlwl\t%0, 3(%2)\n" \
291 "2:\tlwr\t%0, (%2)\n\t" \
295 ".section\t.fixup,\"ax\"\n\t" \
296 "4:\tli\t%1, %3\n\t" \
299 ".section\t__ex_table,\"a\"\n\t" \
300 STR(PTR)"\t1b, 4b\n\t" \
301 STR(PTR)"\t2b, 4b\n\t" \
303 : "=&r" (value), "=r" (res) \
304 : "r" (addr), "i" (-EFAULT));
306 #define LoadHWU(addr, value, res) \
307 __asm__ __volatile__ ( \
309 "1:\tlbu\t%0, 1(%2)\n" \
310 "2:\tlbu\t$1, 0(%2)\n\t" \
317 ".section\t.fixup,\"ax\"\n\t" \
318 "4:\tli\t%1, %3\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));
328 #define LoadWU(addr, value, res) \
329 __asm__ __volatile__ ( \
330 "1:\tlwl\t%0, 3(%2)\n" \
331 "2:\tlwr\t%0, (%2)\n\t" \
332 "dsll\t%0, %0, 32\n\t" \
333 "dsrl\t%0, %0, 32\n\t" \
337 "\t.section\t.fixup,\"ax\"\n\t" \
338 "4:\tli\t%1, %3\n\t" \
341 ".section\t__ex_table,\"a\"\n\t" \
342 STR(PTR)"\t1b, 4b\n\t" \
343 STR(PTR)"\t2b, 4b\n\t" \
345 : "=&r" (value), "=r" (res) \
346 : "r" (addr), "i" (-EFAULT));
348 #define LoadDW(addr, value, res) \
349 __asm__ __volatile__ ( \
350 "1:\tldl\t%0, 7(%2)\n" \
351 "2:\tldr\t%0, (%2)\n\t" \
355 "\t.section\t.fixup,\"ax\"\n\t" \
356 "4:\tli\t%1, %3\n\t" \
359 ".section\t__ex_table,\"a\"\n\t" \
360 STR(PTR)"\t1b, 4b\n\t" \
361 STR(PTR)"\t2b, 4b\n\t" \
363 : "=&r" (value), "=r" (res) \
364 : "r" (addr), "i" (-EFAULT));
366 #define StoreHW(addr, value, res) \
367 __asm__ __volatile__ ( \
369 "1:\tsb\t%1, 0(%2)\n\t" \
370 "srl\t$1,%1, 0x8\n" \
371 "2:\tsb\t$1, 1(%2)\n\t" \
376 ".section\t.fixup,\"ax\"\n\t" \
377 "4:\tli\t%0, %3\n\t" \
380 ".section\t__ex_table,\"a\"\n\t" \
381 STR(PTR)"\t1b, 4b\n\t" \
382 STR(PTR)"\t2b, 4b\n\t" \
385 : "r" (value), "r" (addr), "i" (-EFAULT));
387 #define StoreW(addr, value, res) \
388 __asm__ __volatile__ ( \
389 "1:\tswl\t%1, 3(%2)\n" \
390 "2:\tswr\t%1, (%2)\n\t" \
394 ".section\t.fixup,\"ax\"\n\t" \
395 "4:\tli\t%0, %3\n\t" \
398 ".section\t__ex_table,\"a\"\n\t" \
399 STR(PTR)"\t1b, 4b\n\t" \
400 STR(PTR)"\t2b, 4b\n\t" \
403 : "r" (value), "r" (addr), "i" (-EFAULT));
405 #define StoreDW(addr, value, res) \
406 __asm__ __volatile__ ( \
407 "1:\tsdl\t%1, 7(%2)\n" \
408 "2:\tsdr\t%1, (%2)\n\t" \
412 ".section\t.fixup,\"ax\"\n\t" \
413 "4:\tli\t%0, %3\n\t" \
416 ".section\t__ex_table,\"a\"\n\t" \
417 STR(PTR)"\t1b, 4b\n\t" \
418 STR(PTR)"\t2b, 4b\n\t" \
421 : "r" (value), "r" (addr), "i" (-EFAULT));
424 static void emulate_load_store_insn(struct pt_regs
*regs
,
425 void __user
*addr
, unsigned int __user
*pc
)
427 union mips_instruction insn
;
430 unsigned long origpc
;
431 unsigned long orig31
;
432 void __user
*fault_addr
= NULL
;
434 origpc
= (unsigned long)pc
;
435 orig31
= regs
->regs
[31];
437 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, 0);
440 * This load never faults.
442 __get_user(insn
.word
, pc
);
444 switch (insn
.i_format
.opcode
) {
446 * These are instructions that a compiler doesn't generate. We
447 * can assume therefore that the code is MIPS-aware and
448 * really buggy. Emulating these instructions would break the
457 * For these instructions the only way to create an address
458 * error is an attempted access to kernel/supervisor address
475 * The remaining opcodes are the ones that are really of
479 if (!access_ok(VERIFY_READ
, addr
, 2))
482 LoadHW(addr
, value
, res
);
485 compute_return_epc(regs
);
486 regs
->regs
[insn
.i_format
.rt
] = value
;
490 if (!access_ok(VERIFY_READ
, addr
, 4))
493 LoadW(addr
, value
, res
);
496 compute_return_epc(regs
);
497 regs
->regs
[insn
.i_format
.rt
] = value
;
501 if (!access_ok(VERIFY_READ
, addr
, 2))
504 LoadHWU(addr
, value
, res
);
507 compute_return_epc(regs
);
508 regs
->regs
[insn
.i_format
.rt
] = value
;
514 * A 32-bit kernel might be running on a 64-bit processor. But
515 * if we're on a 32-bit processor and an i-cache incoherency
516 * or race makes us see a 64-bit instruction here the sdl/sdr
517 * would blow up, so for now we don't handle unaligned 64-bit
518 * instructions on 32-bit kernels.
520 if (!access_ok(VERIFY_READ
, addr
, 4))
523 LoadWU(addr
, value
, res
);
526 compute_return_epc(regs
);
527 regs
->regs
[insn
.i_format
.rt
] = value
;
529 #endif /* CONFIG_64BIT */
531 /* Cannot handle 64-bit instructions in 32-bit kernel */
537 * A 32-bit kernel might be running on a 64-bit processor. But
538 * if we're on a 32-bit processor and an i-cache incoherency
539 * or race makes us see a 64-bit instruction here the sdl/sdr
540 * would blow up, so for now we don't handle unaligned 64-bit
541 * instructions on 32-bit kernels.
543 if (!access_ok(VERIFY_READ
, addr
, 8))
546 LoadDW(addr
, value
, res
);
549 compute_return_epc(regs
);
550 regs
->regs
[insn
.i_format
.rt
] = value
;
552 #endif /* CONFIG_64BIT */
554 /* Cannot handle 64-bit instructions in 32-bit kernel */
558 if (!access_ok(VERIFY_WRITE
, addr
, 2))
561 compute_return_epc(regs
);
562 value
= regs
->regs
[insn
.i_format
.rt
];
563 StoreHW(addr
, value
, res
);
569 if (!access_ok(VERIFY_WRITE
, addr
, 4))
572 compute_return_epc(regs
);
573 value
= regs
->regs
[insn
.i_format
.rt
];
574 StoreW(addr
, value
, res
);
582 * A 32-bit kernel might be running on a 64-bit processor. But
583 * if we're on a 32-bit processor and an i-cache incoherency
584 * or race makes us see a 64-bit instruction here the sdl/sdr
585 * would blow up, so for now we don't handle unaligned 64-bit
586 * instructions on 32-bit kernels.
588 if (!access_ok(VERIFY_WRITE
, addr
, 8))
591 compute_return_epc(regs
);
592 value
= regs
->regs
[insn
.i_format
.rt
];
593 StoreDW(addr
, value
, res
);
597 #endif /* CONFIG_64BIT */
599 /* Cannot handle 64-bit instructions in 32-bit kernel */
606 die_if_kernel("Unaligned FP access in kernel code", regs
);
607 BUG_ON(!used_math());
609 lose_fpu(1); /* Save FPU state for the emulator. */
610 res
= fpu_emulator_cop1Handler(regs
, ¤t
->thread
.fpu
, 1,
612 own_fpu(1); /* Restore FPU state. */
614 /* Signal if something went wrong. */
615 process_fpemu_return(res
, fault_addr
);
622 * COP2 is available to implementor for application specific use.
623 * It's up to applications to register a notifier chain and do
624 * whatever they have to do, including possible sending of signals.
627 cu2_notifier_call_chain(CU2_LWC2_OP
, regs
);
631 cu2_notifier_call_chain(CU2_LDC2_OP
, regs
);
635 cu2_notifier_call_chain(CU2_SWC2_OP
, regs
);
639 cu2_notifier_call_chain(CU2_SDC2_OP
, regs
);
644 * Pheeee... We encountered an yet unknown instruction or
645 * cache coherence problem. Die sucker, die ...
650 #ifdef CONFIG_DEBUG_FS
651 unaligned_instructions
++;
657 /* roll back jump/branch */
658 regs
->cp0_epc
= origpc
;
659 regs
->regs
[31] = orig31
;
660 /* Did we have an exception handler installed? */
661 if (fixup_exception(regs
))
664 die_if_kernel("Unhandled kernel unaligned access", regs
);
665 force_sig(SIGSEGV
, current
);
670 die_if_kernel("Unhandled kernel unaligned access", regs
);
671 force_sig(SIGBUS
, current
);
677 ("Unhandled kernel unaligned access or invalid instruction", regs
);
678 force_sig(SIGILL
, current
);
681 /* Recode table from 16-bit register notation to 32-bit GPR. */
682 const int reg16to32
[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
684 /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
685 const int reg16to32st
[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
687 static void emulate_load_store_microMIPS(struct pt_regs
*regs
,
693 unsigned int reg
= 0, rvar
;
694 unsigned long orig31
;
698 unsigned long origpc
, contpc
;
699 union mips_instruction insn
;
700 struct mm_decoded_insn mminsn
;
701 void __user
*fault_addr
= NULL
;
703 origpc
= regs
->cp0_epc
;
704 orig31
= regs
->regs
[31];
706 mminsn
.micro_mips_mode
= 1;
709 * This load never faults.
711 pc16
= (unsigned short __user
*)msk_isa16_mode(regs
->cp0_epc
);
712 __get_user(halfword
, pc16
);
714 contpc
= regs
->cp0_epc
+ 2;
715 word
= ((unsigned int)halfword
<< 16);
718 if (!mm_insn_16bit(halfword
)) {
719 __get_user(halfword
, pc16
);
721 contpc
= regs
->cp0_epc
+ 4;
727 if (get_user(halfword
, pc16
))
729 mminsn
.next_pc_inc
= 2;
730 word
= ((unsigned int)halfword
<< 16);
732 if (!mm_insn_16bit(halfword
)) {
734 if (get_user(halfword
, pc16
))
736 mminsn
.next_pc_inc
= 4;
739 mminsn
.next_insn
= word
;
741 insn
= (union mips_instruction
)(mminsn
.insn
);
742 if (mm_isBranchInstr(regs
, mminsn
, &contpc
))
743 insn
= (union mips_instruction
)(mminsn
.next_insn
);
745 /* Parse instruction to find what to do */
747 switch (insn
.mm_i_format
.opcode
) {
750 switch (insn
.mm_x_format
.func
) {
752 reg
= insn
.mm_x_format
.rd
;
759 switch (insn
.mm_m_format
.func
) {
761 reg
= insn
.mm_m_format
.rd
;
765 if (!access_ok(VERIFY_READ
, addr
, 8))
768 LoadW(addr
, value
, res
);
771 regs
->regs
[reg
] = value
;
773 LoadW(addr
, value
, res
);
776 regs
->regs
[reg
+ 1] = value
;
780 reg
= insn
.mm_m_format
.rd
;
784 if (!access_ok(VERIFY_WRITE
, addr
, 8))
787 value
= regs
->regs
[reg
];
788 StoreW(addr
, value
, res
);
792 value
= regs
->regs
[reg
+ 1];
793 StoreW(addr
, value
, res
);
800 reg
= insn
.mm_m_format
.rd
;
804 if (!access_ok(VERIFY_READ
, addr
, 16))
807 LoadDW(addr
, value
, res
);
810 regs
->regs
[reg
] = value
;
812 LoadDW(addr
, value
, res
);
815 regs
->regs
[reg
+ 1] = value
;
817 #endif /* CONFIG_64BIT */
823 reg
= insn
.mm_m_format
.rd
;
827 if (!access_ok(VERIFY_WRITE
, addr
, 16))
830 value
= regs
->regs
[reg
];
831 StoreDW(addr
, value
, res
);
835 value
= regs
->regs
[reg
+ 1];
836 StoreDW(addr
, value
, res
);
840 #endif /* CONFIG_64BIT */
845 reg
= insn
.mm_m_format
.rd
;
847 if ((rvar
> 9) || !reg
)
851 (VERIFY_READ
, addr
, 4 * (rvar
+ 1)))
854 if (!access_ok(VERIFY_READ
, addr
, 4 * rvar
))
859 for (i
= 16; rvar
; rvar
--, i
++) {
860 LoadW(addr
, value
, res
);
864 regs
->regs
[i
] = value
;
866 if ((reg
& 0xf) == 9) {
867 LoadW(addr
, value
, res
);
871 regs
->regs
[30] = value
;
874 LoadW(addr
, value
, res
);
877 regs
->regs
[31] = value
;
882 reg
= insn
.mm_m_format
.rd
;
884 if ((rvar
> 9) || !reg
)
888 (VERIFY_WRITE
, addr
, 4 * (rvar
+ 1)))
891 if (!access_ok(VERIFY_WRITE
, addr
, 4 * rvar
))
896 for (i
= 16; rvar
; rvar
--, i
++) {
897 value
= regs
->regs
[i
];
898 StoreW(addr
, value
, res
);
903 if ((reg
& 0xf) == 9) {
904 value
= regs
->regs
[30];
905 StoreW(addr
, value
, res
);
911 value
= regs
->regs
[31];
912 StoreW(addr
, value
, res
);
920 reg
= insn
.mm_m_format
.rd
;
922 if ((rvar
> 9) || !reg
)
926 (VERIFY_READ
, addr
, 8 * (rvar
+ 1)))
929 if (!access_ok(VERIFY_READ
, addr
, 8 * rvar
))
935 for (i
= 16; rvar
; rvar
--, i
++) {
936 LoadDW(addr
, value
, res
);
940 regs
->regs
[i
] = value
;
942 if ((reg
& 0xf) == 9) {
943 LoadDW(addr
, value
, res
);
947 regs
->regs
[30] = value
;
950 LoadDW(addr
, value
, res
);
953 regs
->regs
[31] = value
;
956 #endif /* CONFIG_64BIT */
962 reg
= insn
.mm_m_format
.rd
;
964 if ((rvar
> 9) || !reg
)
968 (VERIFY_WRITE
, addr
, 8 * (rvar
+ 1)))
971 if (!access_ok(VERIFY_WRITE
, addr
, 8 * rvar
))
977 for (i
= 16; rvar
; rvar
--, i
++) {
978 value
= regs
->regs
[i
];
979 StoreDW(addr
, value
, res
);
984 if ((reg
& 0xf) == 9) {
985 value
= regs
->regs
[30];
986 StoreDW(addr
, value
, res
);
992 value
= regs
->regs
[31];
993 StoreDW(addr
, value
, res
);
998 #endif /* CONFIG_64BIT */
1002 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1008 switch (insn
.mm_m_format
.func
) {
1010 reg
= insn
.mm_m_format
.rd
;
1014 /* LL,SC,LLD,SCD are not serviced */
1018 switch (insn
.mm_x_format
.func
) {
1033 /* roll back jump/branch */
1034 regs
->cp0_epc
= origpc
;
1035 regs
->regs
[31] = orig31
;
1037 die_if_kernel("Unaligned FP access in kernel code", regs
);
1038 BUG_ON(!used_math());
1039 BUG_ON(!is_fpu_owner());
1041 lose_fpu(1); /* save the FPU state for the emulator */
1042 res
= fpu_emulator_cop1Handler(regs
, ¤t
->thread
.fpu
, 1,
1044 own_fpu(1); /* restore FPU state */
1046 /* If something went wrong, signal */
1047 process_fpemu_return(res
, fault_addr
);
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 reg
= insn
.mm_i_format
.rt
;
1082 switch (insn
.mm16_m_format
.func
) {
1084 reg
= insn
.mm16_m_format
.rlist
;
1086 if (!access_ok(VERIFY_READ
, addr
, 4 * rvar
))
1089 for (i
= 16; rvar
; rvar
--, i
++) {
1090 LoadW(addr
, value
, res
);
1094 regs
->regs
[i
] = value
;
1096 LoadW(addr
, value
, res
);
1099 regs
->regs
[31] = value
;
1104 reg
= insn
.mm16_m_format
.rlist
;
1106 if (!access_ok(VERIFY_WRITE
, addr
, 4 * rvar
))
1109 for (i
= 16; rvar
; rvar
--, i
++) {
1110 value
= regs
->regs
[i
];
1111 StoreW(addr
, value
, res
);
1116 value
= regs
->regs
[31];
1117 StoreW(addr
, value
, res
);
1128 reg
= reg16to32
[insn
.mm16_rb_format
.rt
];
1132 reg
= reg16to32
[insn
.mm16_rb_format
.rt
];
1136 reg
= reg16to32st
[insn
.mm16_rb_format
.rt
];
1140 reg
= reg16to32st
[insn
.mm16_rb_format
.rt
];
1144 reg
= insn
.mm16_r5_format
.rt
;
1148 reg
= insn
.mm16_r5_format
.rt
;
1152 reg
= reg16to32
[insn
.mm16_r3_format
.rt
];
1160 if (!access_ok(VERIFY_READ
, addr
, 2))
1163 LoadHW(addr
, value
, res
);
1166 regs
->regs
[reg
] = value
;
1170 if (!access_ok(VERIFY_READ
, addr
, 2))
1173 LoadHWU(addr
, value
, res
);
1176 regs
->regs
[reg
] = value
;
1180 if (!access_ok(VERIFY_READ
, addr
, 4))
1183 LoadW(addr
, value
, res
);
1186 regs
->regs
[reg
] = value
;
1192 * A 32-bit kernel might be running on a 64-bit processor. But
1193 * if we're on a 32-bit processor and an i-cache incoherency
1194 * or race makes us see a 64-bit instruction here the sdl/sdr
1195 * would blow up, so for now we don't handle unaligned 64-bit
1196 * instructions on 32-bit kernels.
1198 if (!access_ok(VERIFY_READ
, addr
, 4))
1201 LoadWU(addr
, value
, res
);
1204 regs
->regs
[reg
] = value
;
1206 #endif /* CONFIG_64BIT */
1208 /* Cannot handle 64-bit instructions in 32-bit kernel */
1214 * A 32-bit kernel might be running on a 64-bit processor. But
1215 * if we're on a 32-bit processor and an i-cache incoherency
1216 * or race makes us see a 64-bit instruction here the sdl/sdr
1217 * would blow up, so for now we don't handle unaligned 64-bit
1218 * instructions on 32-bit kernels.
1220 if (!access_ok(VERIFY_READ
, addr
, 8))
1223 LoadDW(addr
, value
, res
);
1226 regs
->regs
[reg
] = value
;
1228 #endif /* CONFIG_64BIT */
1230 /* Cannot handle 64-bit instructions in 32-bit kernel */
1234 if (!access_ok(VERIFY_WRITE
, addr
, 2))
1237 value
= regs
->regs
[reg
];
1238 StoreHW(addr
, value
, res
);
1244 if (!access_ok(VERIFY_WRITE
, addr
, 4))
1247 value
= regs
->regs
[reg
];
1248 StoreW(addr
, value
, res
);
1256 * A 32-bit kernel might be running on a 64-bit processor. But
1257 * if we're on a 32-bit processor and an i-cache incoherency
1258 * or race makes us see a 64-bit instruction here the sdl/sdr
1259 * would blow up, so for now we don't handle unaligned 64-bit
1260 * instructions on 32-bit kernels.
1262 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1265 value
= regs
->regs
[reg
];
1266 StoreDW(addr
, value
, res
);
1270 #endif /* CONFIG_64BIT */
1272 /* Cannot handle 64-bit instructions in 32-bit kernel */
1276 regs
->cp0_epc
= contpc
; /* advance or branch */
1278 #ifdef CONFIG_DEBUG_FS
1279 unaligned_instructions
++;
1284 /* roll back jump/branch */
1285 regs
->cp0_epc
= origpc
;
1286 regs
->regs
[31] = orig31
;
1287 /* Did we have an exception handler installed? */
1288 if (fixup_exception(regs
))
1291 die_if_kernel("Unhandled kernel unaligned access", regs
);
1292 force_sig(SIGSEGV
, current
);
1297 die_if_kernel("Unhandled kernel unaligned access", regs
);
1298 force_sig(SIGBUS
, current
);
1304 ("Unhandled kernel unaligned access or invalid instruction", regs
);
1305 force_sig(SIGILL
, current
);
1308 static void emulate_load_store_MIPS16e(struct pt_regs
*regs
, void __user
* addr
)
1310 unsigned long value
;
1313 unsigned long orig31
;
1315 unsigned long origpc
;
1316 union mips16e_instruction mips16inst
, oldinst
;
1318 origpc
= regs
->cp0_epc
;
1319 orig31
= regs
->regs
[31];
1320 pc16
= (unsigned short __user
*)msk_isa16_mode(origpc
);
1322 * This load never faults.
1324 __get_user(mips16inst
.full
, pc16
);
1325 oldinst
= mips16inst
;
1327 /* skip EXTEND instruction */
1328 if (mips16inst
.ri
.opcode
== MIPS16e_extend_op
) {
1330 __get_user(mips16inst
.full
, pc16
);
1331 } else if (delay_slot(regs
)) {
1332 /* skip jump instructions */
1333 /* JAL/JALX are 32 bits but have OPCODE in first short int */
1334 if (mips16inst
.ri
.opcode
== MIPS16e_jal_op
)
1337 if (get_user(mips16inst
.full
, pc16
))
1341 switch (mips16inst
.ri
.opcode
) {
1342 case MIPS16e_i64_op
: /* I64 or RI64 instruction */
1343 switch (mips16inst
.i64
.func
) { /* I64/RI64 func field check */
1344 case MIPS16e_ldpc_func
:
1345 case MIPS16e_ldsp_func
:
1346 reg
= reg16to32
[mips16inst
.ri64
.ry
];
1349 case MIPS16e_sdsp_func
:
1350 reg
= reg16to32
[mips16inst
.ri64
.ry
];
1353 case MIPS16e_sdrasp_func
:
1354 reg
= 29; /* GPRSP */
1360 case MIPS16e_swsp_op
:
1361 case MIPS16e_lwpc_op
:
1362 case MIPS16e_lwsp_op
:
1363 reg
= reg16to32
[mips16inst
.ri
.rx
];
1367 if (mips16inst
.i8
.func
!= MIPS16e_swrasp_func
)
1369 reg
= 29; /* GPRSP */
1373 reg
= reg16to32
[mips16inst
.rri
.ry
];
1377 switch (mips16inst
.ri
.opcode
) {
1380 case MIPS16e_lbu_op
:
1385 if (!access_ok(VERIFY_READ
, addr
, 2))
1388 LoadHW(addr
, value
, res
);
1391 MIPS16e_compute_return_epc(regs
, &oldinst
);
1392 regs
->regs
[reg
] = value
;
1395 case MIPS16e_lhu_op
:
1396 if (!access_ok(VERIFY_READ
, addr
, 2))
1399 LoadHWU(addr
, value
, res
);
1402 MIPS16e_compute_return_epc(regs
, &oldinst
);
1403 regs
->regs
[reg
] = value
;
1407 case MIPS16e_lwpc_op
:
1408 case MIPS16e_lwsp_op
:
1409 if (!access_ok(VERIFY_READ
, addr
, 4))
1412 LoadW(addr
, value
, res
);
1415 MIPS16e_compute_return_epc(regs
, &oldinst
);
1416 regs
->regs
[reg
] = value
;
1419 case MIPS16e_lwu_op
:
1422 * A 32-bit kernel might be running on a 64-bit processor. But
1423 * if we're on a 32-bit processor and an i-cache incoherency
1424 * or race makes us see a 64-bit instruction here the sdl/sdr
1425 * would blow up, so for now we don't handle unaligned 64-bit
1426 * instructions on 32-bit kernels.
1428 if (!access_ok(VERIFY_READ
, addr
, 4))
1431 LoadWU(addr
, value
, res
);
1434 MIPS16e_compute_return_epc(regs
, &oldinst
);
1435 regs
->regs
[reg
] = value
;
1437 #endif /* CONFIG_64BIT */
1439 /* Cannot handle 64-bit instructions in 32-bit kernel */
1446 * A 32-bit kernel might be running on a 64-bit processor. But
1447 * if we're on a 32-bit processor and an i-cache incoherency
1448 * or race makes us see a 64-bit instruction here the sdl/sdr
1449 * would blow up, so for now we don't handle unaligned 64-bit
1450 * instructions on 32-bit kernels.
1452 if (!access_ok(VERIFY_READ
, addr
, 8))
1455 LoadDW(addr
, value
, res
);
1458 MIPS16e_compute_return_epc(regs
, &oldinst
);
1459 regs
->regs
[reg
] = value
;
1461 #endif /* CONFIG_64BIT */
1463 /* Cannot handle 64-bit instructions in 32-bit kernel */
1467 if (!access_ok(VERIFY_WRITE
, addr
, 2))
1470 MIPS16e_compute_return_epc(regs
, &oldinst
);
1471 value
= regs
->regs
[reg
];
1472 StoreHW(addr
, value
, res
);
1478 case MIPS16e_swsp_op
:
1479 case MIPS16e_i8_op
: /* actually - MIPS16e_swrasp_func */
1480 if (!access_ok(VERIFY_WRITE
, addr
, 4))
1483 MIPS16e_compute_return_epc(regs
, &oldinst
);
1484 value
= regs
->regs
[reg
];
1485 StoreW(addr
, value
, res
);
1494 * A 32-bit kernel might be running on a 64-bit processor. But
1495 * if we're on a 32-bit processor and an i-cache incoherency
1496 * or race makes us see a 64-bit instruction here the sdl/sdr
1497 * would blow up, so for now we don't handle unaligned 64-bit
1498 * instructions on 32-bit kernels.
1500 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1503 MIPS16e_compute_return_epc(regs
, &oldinst
);
1504 value
= regs
->regs
[reg
];
1505 StoreDW(addr
, value
, res
);
1509 #endif /* CONFIG_64BIT */
1511 /* Cannot handle 64-bit instructions in 32-bit kernel */
1516 * Pheeee... We encountered an yet unknown instruction or
1517 * cache coherence problem. Die sucker, die ...
1522 #ifdef CONFIG_DEBUG_FS
1523 unaligned_instructions
++;
1529 /* roll back jump/branch */
1530 regs
->cp0_epc
= origpc
;
1531 regs
->regs
[31] = orig31
;
1532 /* Did we have an exception handler installed? */
1533 if (fixup_exception(regs
))
1536 die_if_kernel("Unhandled kernel unaligned access", regs
);
1537 force_sig(SIGSEGV
, current
);
1542 die_if_kernel("Unhandled kernel unaligned access", regs
);
1543 force_sig(SIGBUS
, current
);
1549 ("Unhandled kernel unaligned access or invalid instruction", regs
);
1550 force_sig(SIGILL
, current
);
1553 asmlinkage
void do_ade(struct pt_regs
*regs
)
1555 enum ctx_state prev_state
;
1556 unsigned int __user
*pc
;
1559 prev_state
= exception_enter();
1560 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
,
1561 1, regs
, regs
->cp0_badvaddr
);
1563 * Did we catch a fault trying to load an instruction?
1565 if (regs
->cp0_badvaddr
== regs
->cp0_epc
)
1568 if (user_mode(regs
) && !test_thread_flag(TIF_FIXADE
))
1570 if (unaligned_action
== UNALIGNED_ACTION_SIGNAL
)
1574 * Do branch emulation only if we didn't forward the exception.
1575 * This is all so but ugly ...
1579 * Are we running in microMIPS mode?
1581 if (get_isa16_mode(regs
->cp0_epc
)) {
1583 * Did we catch a fault trying to load an instruction in
1586 if (regs
->cp0_badvaddr
== msk_isa16_mode(regs
->cp0_epc
))
1588 if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
1589 show_registers(regs
);
1591 if (cpu_has_mmips
) {
1593 if (!user_mode(regs
))
1595 emulate_load_store_microMIPS(regs
,
1596 (void __user
*)regs
->cp0_badvaddr
);
1602 if (cpu_has_mips16
) {
1604 if (!user_mode(regs
))
1606 emulate_load_store_MIPS16e(regs
,
1607 (void __user
*)regs
->cp0_badvaddr
);
1616 if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
1617 show_registers(regs
);
1618 pc
= (unsigned int __user
*)exception_epc(regs
);
1621 if (!user_mode(regs
))
1623 emulate_load_store_insn(regs
, (void __user
*)regs
->cp0_badvaddr
, pc
);
1629 die_if_kernel("Kernel unaligned instruction access", regs
);
1630 force_sig(SIGBUS
, current
);
1633 * XXX On return from the signal handler we should advance the epc
1635 exception_exit(prev_state
);
1638 #ifdef CONFIG_DEBUG_FS
1639 extern struct dentry
*mips_debugfs_dir
;
1640 static int __init
debugfs_unaligned(void)
1644 if (!mips_debugfs_dir
)
1646 d
= debugfs_create_u32("unaligned_instructions", S_IRUGO
,
1647 mips_debugfs_dir
, &unaligned_instructions
);
1650 d
= debugfs_create_u32("unaligned_action", S_IRUGO
| S_IWUSR
,
1651 mips_debugfs_dir
, &unaligned_action
);
1656 __initcall(debugfs_unaligned
);