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.
10 * Copyright (C) 2014 Imagination Technologies Ltd.
12 * This file contains exception handler for address error exception with the
13 * special capability to execute faulting instructions in software. The
14 * handler does not try to handle the case when the program counter points
15 * to an address not aligned to a word boundary.
17 * Putting data to unaligned addresses is a bad practice even on Intel where
18 * only the performance is affected. Much worse is that such code is non-
19 * portable. Due to several programs that die on MIPS due to alignment
20 * problems I decided to implement this handler anyway though I originally
21 * didn't intend to do this at all for user code.
23 * For now I enable fixing of address errors by default to make life easier.
24 * I however intend to disable this somewhen in the future when the alignment
25 * problems with user programs have been fixed. For programmers this is the
28 * Fixing address errors is a per process option. The option is inherited
29 * across fork(2) and execve(2) calls. If you really want to use the
30 * option in your user programs - I discourage the use of the software
31 * emulation strongly - use the following code in your userland stuff:
33 * #include <sys/sysmips.h>
36 * sysmips(MIPS_FIXADE, x);
39 * The argument x is 0 for disabling software emulation, enabled otherwise.
41 * Below a little program to play around with this feature.
44 * #include <sys/sysmips.h>
47 * unsigned char bar[8];
50 * main(int argc, char *argv[])
52 * struct foo x = {0, 1, 2, 3, 4, 5, 6, 7};
53 * unsigned int *p = (unsigned int *) (x.bar + 3);
57 * sysmips(MIPS_FIXADE, atoi(argv[1]));
59 * printf("*p = %08lx\n", *p);
63 * for(i = 0; i <= 7; i++)
64 * printf("%02x ", x.bar[i]);
68 * Coprocessor loads are not supported; I think this case is unimportant
71 * TODO: Handle ndc (attempted store to doubleword in uncached memory)
72 * exception for the R6000.
73 * A store crossing a page boundary might be executed only partially.
74 * Undo the partial store in this case.
76 #include <linux/context_tracking.h>
78 #include <linux/signal.h>
79 #include <linux/smp.h>
80 #include <linux/sched.h>
81 #include <linux/debugfs.h>
82 #include <linux/perf_event.h>
85 #include <asm/branch.h>
86 #include <asm/byteorder.h>
89 #include <asm/fpu_emulator.h>
91 #include <asm/uaccess.h>
93 #include <asm/fpu_emulator.h>
95 #define STR(x) __STR(x)
99 UNALIGNED_ACTION_QUIET,
100 UNALIGNED_ACTION_SIGNAL,
101 UNALIGNED_ACTION_SHOW,
103 #ifdef CONFIG_DEBUG_FS
104 static u32 unaligned_instructions;
105 static u32 unaligned_action;
107 #define unaligned_action UNALIGNED_ACTION_QUIET
109 extern void show_registers(struct pt_regs *regs);
112 #define LoadHW(addr, value, res) \
113 __asm__ __volatile__ (".set\tnoat\n" \
114 "1:\t"user_lb("%0", "0(%2)")"\n" \
115 "2:\t"user_lbu("$1", "1(%2)")"\n\t" \
121 ".section\t.fixup,\"ax\"\n\t" \
122 "4:\tli\t%1, %3\n\t" \
125 ".section\t__ex_table,\"a\"\n\t" \
126 STR(PTR)"\t1b, 4b\n\t" \
127 STR(PTR)"\t2b, 4b\n\t" \
129 : "=&r" (value), "=r" (res) \
130 : "r" (addr), "i" (-EFAULT));
132 #define LoadW(addr, value, res) \
133 __asm__ __volatile__ ( \
134 "1:\t"user_lwl("%0", "(%2)")"\n" \
135 "2:\t"user_lwr("%0", "3(%2)")"\n\t" \
139 ".section\t.fixup,\"ax\"\n\t" \
140 "4:\tli\t%1, %3\n\t" \
143 ".section\t__ex_table,\"a\"\n\t" \
144 STR(PTR)"\t1b, 4b\n\t" \
145 STR(PTR)"\t2b, 4b\n\t" \
147 : "=&r" (value), "=r" (res) \
148 : "r" (addr), "i" (-EFAULT));
150 #define LoadHWU(addr, value, res) \
151 __asm__ __volatile__ ( \
153 "1:\t"user_lbu("%0", "0(%2)")"\n" \
154 "2:\t"user_lbu("$1", "1(%2)")"\n\t" \
161 ".section\t.fixup,\"ax\"\n\t" \
162 "4:\tli\t%1, %3\n\t" \
165 ".section\t__ex_table,\"a\"\n\t" \
166 STR(PTR)"\t1b, 4b\n\t" \
167 STR(PTR)"\t2b, 4b\n\t" \
169 : "=&r" (value), "=r" (res) \
170 : "r" (addr), "i" (-EFAULT));
172 #define LoadWU(addr, value, res) \
173 __asm__ __volatile__ ( \
174 "1:\t"user_lwl("%0", "(%2)")"\n" \
175 "2:\t"user_lwr("%0", "3(%2)")"\n\t" \
176 "dsll\t%0, %0, 32\n\t" \
177 "dsrl\t%0, %0, 32\n\t" \
181 "\t.section\t.fixup,\"ax\"\n\t" \
182 "4:\tli\t%1, %3\n\t" \
185 ".section\t__ex_table,\"a\"\n\t" \
186 STR(PTR)"\t1b, 4b\n\t" \
187 STR(PTR)"\t2b, 4b\n\t" \
189 : "=&r" (value), "=r" (res) \
190 : "r" (addr), "i" (-EFAULT));
192 #define LoadDW(addr, value, res) \
193 __asm__ __volatile__ ( \
194 "1:\tldl\t%0, (%2)\n" \
195 "2:\tldr\t%0, 7(%2)\n\t" \
199 "\t.section\t.fixup,\"ax\"\n\t" \
200 "4:\tli\t%1, %3\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));
210 #define StoreHW(addr, value, res) \
211 __asm__ __volatile__ ( \
213 "1:\t"user_sb("%1", "1(%2)")"\n" \
214 "srl\t$1, %1, 0x8\n" \
215 "2:\t"user_sb("$1", "0(%2)")"\n" \
220 ".section\t.fixup,\"ax\"\n\t" \
221 "4:\tli\t%0, %3\n\t" \
224 ".section\t__ex_table,\"a\"\n\t" \
225 STR(PTR)"\t1b, 4b\n\t" \
226 STR(PTR)"\t2b, 4b\n\t" \
229 : "r" (value), "r" (addr), "i" (-EFAULT));
231 #define StoreW(addr, value, res) \
232 __asm__ __volatile__ ( \
233 "1:\t"user_swl("%1", "(%2)")"\n" \
234 "2:\t"user_swr("%1", "3(%2)")"\n\t" \
238 ".section\t.fixup,\"ax\"\n\t" \
239 "4:\tli\t%0, %3\n\t" \
242 ".section\t__ex_table,\"a\"\n\t" \
243 STR(PTR)"\t1b, 4b\n\t" \
244 STR(PTR)"\t2b, 4b\n\t" \
247 : "r" (value), "r" (addr), "i" (-EFAULT));
249 #define StoreDW(addr, value, res) \
250 __asm__ __volatile__ ( \
251 "1:\tsdl\t%1,(%2)\n" \
252 "2:\tsdr\t%1, 7(%2)\n\t" \
256 ".section\t.fixup,\"ax\"\n\t" \
257 "4:\tli\t%0, %3\n\t" \
260 ".section\t__ex_table,\"a\"\n\t" \
261 STR(PTR)"\t1b, 4b\n\t" \
262 STR(PTR)"\t2b, 4b\n\t" \
265 : "r" (value), "r" (addr), "i" (-EFAULT));
268 #ifdef __LITTLE_ENDIAN
269 #define LoadHW(addr, value, res) \
270 __asm__ __volatile__ (".set\tnoat\n" \
271 "1:\t"user_lb("%0", "1(%2)")"\n" \
272 "2:\t"user_lbu("$1", "0(%2)")"\n\t" \
278 ".section\t.fixup,\"ax\"\n\t" \
279 "4:\tli\t%1, %3\n\t" \
282 ".section\t__ex_table,\"a\"\n\t" \
283 STR(PTR)"\t1b, 4b\n\t" \
284 STR(PTR)"\t2b, 4b\n\t" \
286 : "=&r" (value), "=r" (res) \
287 : "r" (addr), "i" (-EFAULT));
289 #define LoadW(addr, value, res) \
290 __asm__ __volatile__ ( \
291 "1:\t"user_lwl("%0", "3(%2)")"\n" \
292 "2:\t"user_lwr("%0", "(%2)")"\n\t" \
296 ".section\t.fixup,\"ax\"\n\t" \
297 "4:\tli\t%1, %3\n\t" \
300 ".section\t__ex_table,\"a\"\n\t" \
301 STR(PTR)"\t1b, 4b\n\t" \
302 STR(PTR)"\t2b, 4b\n\t" \
304 : "=&r" (value), "=r" (res) \
305 : "r" (addr), "i" (-EFAULT));
307 #define LoadHWU(addr, value, res) \
308 __asm__ __volatile__ ( \
310 "1:\t"user_lbu("%0", "1(%2)")"\n" \
311 "2:\t"user_lbu("$1", "0(%2)")"\n\t" \
318 ".section\t.fixup,\"ax\"\n\t" \
319 "4:\tli\t%1, %3\n\t" \
322 ".section\t__ex_table,\"a\"\n\t" \
323 STR(PTR)"\t1b, 4b\n\t" \
324 STR(PTR)"\t2b, 4b\n\t" \
326 : "=&r" (value), "=r" (res) \
327 : "r" (addr), "i" (-EFAULT));
329 #define LoadWU(addr, value, res) \
330 __asm__ __volatile__ ( \
331 "1:\t"user_lwl("%0", "3(%2)")"\n" \
332 "2:\t"user_lwr("%0", "(%2)")"\n\t" \
333 "dsll\t%0, %0, 32\n\t" \
334 "dsrl\t%0, %0, 32\n\t" \
338 "\t.section\t.fixup,\"ax\"\n\t" \
339 "4:\tli\t%1, %3\n\t" \
342 ".section\t__ex_table,\"a\"\n\t" \
343 STR(PTR)"\t1b, 4b\n\t" \
344 STR(PTR)"\t2b, 4b\n\t" \
346 : "=&r" (value), "=r" (res) \
347 : "r" (addr), "i" (-EFAULT));
349 #define LoadDW(addr, value, res) \
350 __asm__ __volatile__ ( \
351 "1:\tldl\t%0, 7(%2)\n" \
352 "2:\tldr\t%0, (%2)\n\t" \
356 "\t.section\t.fixup,\"ax\"\n\t" \
357 "4:\tli\t%1, %3\n\t" \
360 ".section\t__ex_table,\"a\"\n\t" \
361 STR(PTR)"\t1b, 4b\n\t" \
362 STR(PTR)"\t2b, 4b\n\t" \
364 : "=&r" (value), "=r" (res) \
365 : "r" (addr), "i" (-EFAULT));
367 #define StoreHW(addr, value, res) \
368 __asm__ __volatile__ ( \
370 "1:\t"user_sb("%1", "0(%2)")"\n" \
371 "srl\t$1,%1, 0x8\n" \
372 "2:\t"user_sb("$1", "1(%2)")"\n" \
377 ".section\t.fixup,\"ax\"\n\t" \
378 "4:\tli\t%0, %3\n\t" \
381 ".section\t__ex_table,\"a\"\n\t" \
382 STR(PTR)"\t1b, 4b\n\t" \
383 STR(PTR)"\t2b, 4b\n\t" \
386 : "r" (value), "r" (addr), "i" (-EFAULT));
388 #define StoreW(addr, value, res) \
389 __asm__ __volatile__ ( \
390 "1:\t"user_swl("%1", "3(%2)")"\n" \
391 "2:\t"user_swr("%1", "(%2)")"\n\t" \
395 ".section\t.fixup,\"ax\"\n\t" \
396 "4:\tli\t%0, %3\n\t" \
399 ".section\t__ex_table,\"a\"\n\t" \
400 STR(PTR)"\t1b, 4b\n\t" \
401 STR(PTR)"\t2b, 4b\n\t" \
404 : "r" (value), "r" (addr), "i" (-EFAULT));
406 #define StoreDW(addr, value, res) \
407 __asm__ __volatile__ ( \
408 "1:\tsdl\t%1, 7(%2)\n" \
409 "2:\tsdr\t%1, (%2)\n\t" \
413 ".section\t.fixup,\"ax\"\n\t" \
414 "4:\tli\t%0, %3\n\t" \
417 ".section\t__ex_table,\"a\"\n\t" \
418 STR(PTR)"\t1b, 4b\n\t" \
419 STR(PTR)"\t2b, 4b\n\t" \
422 : "r" (value), "r" (addr), "i" (-EFAULT));
425 static void emulate_load_store_insn(struct pt_regs *regs,
426 void __user *addr, unsigned int __user *pc)
428 union mips_instruction insn;
431 unsigned long origpc;
432 unsigned long orig31;
433 void __user *fault_addr = NULL;
437 origpc = (unsigned long)pc;
438 orig31 = regs->regs[31];
440 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
443 * This load never faults.
445 __get_user(insn.word, pc);
447 switch (insn.i_format.opcode) {
449 * These are instructions that a compiler doesn't generate. We
450 * can assume therefore that the code is MIPS-aware and
451 * really buggy. Emulating these instructions would break the
460 * For these instructions the only way to create an address
461 * error is an attempted access to kernel/supervisor address
478 * The remaining opcodes are the ones that are really of
484 * we can land here only from kernel accessing user memory,
485 * so we need to "switch" the address limit to user space, so
486 * address check can work properly.
490 switch (insn.spec3_format.func) {
492 if (!access_ok(VERIFY_READ, addr, 2)) {
496 LoadHW(addr, value, res);
501 compute_return_epc(regs);
502 regs->regs[insn.spec3_format.rt] = value;
505 if (!access_ok(VERIFY_READ, addr, 4)) {
509 LoadW(addr, value, res);
514 compute_return_epc(regs);
515 regs->regs[insn.spec3_format.rt] = value;
518 if (!access_ok(VERIFY_READ, addr, 2)) {
522 LoadHWU(addr, value, res);
527 compute_return_epc(regs);
528 regs->regs[insn.spec3_format.rt] = value;
531 if (!access_ok(VERIFY_WRITE, addr, 2)) {
535 compute_return_epc(regs);
536 value = regs->regs[insn.spec3_format.rt];
537 StoreHW(addr, value, res);
544 if (!access_ok(VERIFY_WRITE, addr, 4)) {
548 compute_return_epc(regs);
549 value = regs->regs[insn.spec3_format.rt];
550 StoreW(addr, value, res);
564 if (!access_ok(VERIFY_READ, addr, 2))
567 LoadHW(addr, value, res);
570 compute_return_epc(regs);
571 regs->regs[insn.i_format.rt] = value;
575 if (!access_ok(VERIFY_READ, addr, 4))
578 LoadW(addr, value, res);
581 compute_return_epc(regs);
582 regs->regs[insn.i_format.rt] = value;
586 if (!access_ok(VERIFY_READ, addr, 2))
589 LoadHWU(addr, value, res);
592 compute_return_epc(regs);
593 regs->regs[insn.i_format.rt] = value;
599 * A 32-bit kernel might be running on a 64-bit processor. But
600 * if we're on a 32-bit processor and an i-cache incoherency
601 * or race makes us see a 64-bit instruction here the sdl/sdr
602 * would blow up, so for now we don't handle unaligned 64-bit
603 * instructions on 32-bit kernels.
605 if (!access_ok(VERIFY_READ, addr, 4))
608 LoadWU(addr, value, res);
611 compute_return_epc(regs);
612 regs->regs[insn.i_format.rt] = value;
614 #endif /* CONFIG_64BIT */
616 /* Cannot handle 64-bit instructions in 32-bit kernel */
622 * A 32-bit kernel might be running on a 64-bit processor. But
623 * if we're on a 32-bit processor and an i-cache incoherency
624 * or race makes us see a 64-bit instruction here the sdl/sdr
625 * would blow up, so for now we don't handle unaligned 64-bit
626 * instructions on 32-bit kernels.
628 if (!access_ok(VERIFY_READ, addr, 8))
631 LoadDW(addr, value, res);
634 compute_return_epc(regs);
635 regs->regs[insn.i_format.rt] = value;
637 #endif /* CONFIG_64BIT */
639 /* Cannot handle 64-bit instructions in 32-bit kernel */
643 if (!access_ok(VERIFY_WRITE, addr, 2))
646 compute_return_epc(regs);
647 value = regs->regs[insn.i_format.rt];
648 StoreHW(addr, value, res);
654 if (!access_ok(VERIFY_WRITE, addr, 4))
657 compute_return_epc(regs);
658 value = regs->regs[insn.i_format.rt];
659 StoreW(addr, value, res);
667 * A 32-bit kernel might be running on a 64-bit processor. But
668 * if we're on a 32-bit processor and an i-cache incoherency
669 * or race makes us see a 64-bit instruction here the sdl/sdr
670 * would blow up, so for now we don't handle unaligned 64-bit
671 * instructions on 32-bit kernels.
673 if (!access_ok(VERIFY_WRITE, addr, 8))
676 compute_return_epc(regs);
677 value = regs->regs[insn.i_format.rt];
678 StoreDW(addr, value, res);
682 #endif /* CONFIG_64BIT */
684 /* Cannot handle 64-bit instructions in 32-bit kernel */
691 die_if_kernel("Unaligned FP access in kernel code", regs);
692 BUG_ON(!used_math());
694 lose_fpu(1); /* Save FPU state for the emulator. */
695 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
697 own_fpu(1); /* Restore FPU state. */
699 /* Signal if something went wrong. */
700 process_fpemu_return(res, fault_addr);
707 * COP2 is available to implementor for application specific use.
708 * It's up to applications to register a notifier chain and do
709 * whatever they have to do, including possible sending of signals.
712 cu2_notifier_call_chain(CU2_LWC2_OP, regs);
716 cu2_notifier_call_chain(CU2_LDC2_OP, regs);
720 cu2_notifier_call_chain(CU2_SWC2_OP, regs);
724 cu2_notifier_call_chain(CU2_SDC2_OP, regs);
729 * Pheeee... We encountered an yet unknown instruction or
730 * cache coherence problem. Die sucker, die ...
735 #ifdef CONFIG_DEBUG_FS
736 unaligned_instructions++;
742 /* roll back jump/branch */
743 regs->cp0_epc = origpc;
744 regs->regs[31] = orig31;
745 /* Did we have an exception handler installed? */
746 if (fixup_exception(regs))
749 die_if_kernel("Unhandled kernel unaligned access", regs);
750 force_sig(SIGSEGV, current);
755 die_if_kernel("Unhandled kernel unaligned access", regs);
756 force_sig(SIGBUS, current);
762 ("Unhandled kernel unaligned access or invalid instruction", regs);
763 force_sig(SIGILL, current);
766 /* Recode table from 16-bit register notation to 32-bit GPR. */
767 const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
769 /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
770 const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
772 static void emulate_load_store_microMIPS(struct pt_regs *regs,
778 unsigned int reg = 0, rvar;
779 unsigned long orig31;
783 unsigned long origpc, contpc;
784 union mips_instruction insn;
785 struct mm_decoded_insn mminsn;
786 void __user *fault_addr = NULL;
788 origpc = regs->cp0_epc;
789 orig31 = regs->regs[31];
791 mminsn.micro_mips_mode = 1;
794 * This load never faults.
796 pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
797 __get_user(halfword, pc16);
799 contpc = regs->cp0_epc + 2;
800 word = ((unsigned int)halfword << 16);
803 if (!mm_insn_16bit(halfword)) {
804 __get_user(halfword, pc16);
806 contpc = regs->cp0_epc + 4;
812 if (get_user(halfword, pc16))
814 mminsn.next_pc_inc = 2;
815 word = ((unsigned int)halfword << 16);
817 if (!mm_insn_16bit(halfword)) {
819 if (get_user(halfword, pc16))
821 mminsn.next_pc_inc = 4;
824 mminsn.next_insn = word;
826 insn = (union mips_instruction)(mminsn.insn);
827 if (mm_isBranchInstr(regs, mminsn, &contpc))
828 insn = (union mips_instruction)(mminsn.next_insn);
830 /* Parse instruction to find what to do */
832 switch (insn.mm_i_format.opcode) {
835 switch (insn.mm_x_format.func) {
837 reg = insn.mm_x_format.rd;
844 switch (insn.mm_m_format.func) {
846 reg = insn.mm_m_format.rd;
850 if (!access_ok(VERIFY_READ, addr, 8))
853 LoadW(addr, value, res);
856 regs->regs[reg] = value;
858 LoadW(addr, value, res);
861 regs->regs[reg + 1] = value;
865 reg = insn.mm_m_format.rd;
869 if (!access_ok(VERIFY_WRITE, addr, 8))
872 value = regs->regs[reg];
873 StoreW(addr, value, res);
877 value = regs->regs[reg + 1];
878 StoreW(addr, value, res);
885 reg = insn.mm_m_format.rd;
889 if (!access_ok(VERIFY_READ, addr, 16))
892 LoadDW(addr, value, res);
895 regs->regs[reg] = value;
897 LoadDW(addr, value, res);
900 regs->regs[reg + 1] = value;
902 #endif /* CONFIG_64BIT */
908 reg = insn.mm_m_format.rd;
912 if (!access_ok(VERIFY_WRITE, addr, 16))
915 value = regs->regs[reg];
916 StoreDW(addr, value, res);
920 value = regs->regs[reg + 1];
921 StoreDW(addr, value, res);
925 #endif /* CONFIG_64BIT */
930 reg = insn.mm_m_format.rd;
932 if ((rvar > 9) || !reg)
936 (VERIFY_READ, addr, 4 * (rvar + 1)))
939 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
944 for (i = 16; rvar; rvar--, i++) {
945 LoadW(addr, value, res);
949 regs->regs[i] = value;
951 if ((reg & 0xf) == 9) {
952 LoadW(addr, value, res);
956 regs->regs[30] = value;
959 LoadW(addr, value, res);
962 regs->regs[31] = value;
967 reg = insn.mm_m_format.rd;
969 if ((rvar > 9) || !reg)
973 (VERIFY_WRITE, addr, 4 * (rvar + 1)))
976 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
981 for (i = 16; rvar; rvar--, i++) {
982 value = regs->regs[i];
983 StoreW(addr, value, res);
988 if ((reg & 0xf) == 9) {
989 value = regs->regs[30];
990 StoreW(addr, value, res);
996 value = regs->regs[31];
997 StoreW(addr, value, res);
1005 reg = insn.mm_m_format.rd;
1007 if ((rvar > 9) || !reg)
1011 (VERIFY_READ, addr, 8 * (rvar + 1)))
1014 if (!access_ok(VERIFY_READ, addr, 8 * rvar))
1020 for (i = 16; rvar; rvar--, i++) {
1021 LoadDW(addr, value, res);
1025 regs->regs[i] = value;
1027 if ((reg & 0xf) == 9) {
1028 LoadDW(addr, value, res);
1032 regs->regs[30] = value;
1035 LoadDW(addr, value, res);
1038 regs->regs[31] = value;
1041 #endif /* CONFIG_64BIT */
1047 reg = insn.mm_m_format.rd;
1049 if ((rvar > 9) || !reg)
1053 (VERIFY_WRITE, addr, 8 * (rvar + 1)))
1056 if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
1062 for (i = 16; rvar; rvar--, i++) {
1063 value = regs->regs[i];
1064 StoreDW(addr, value, res);
1069 if ((reg & 0xf) == 9) {
1070 value = regs->regs[30];
1071 StoreDW(addr, value, res);
1077 value = regs->regs[31];
1078 StoreDW(addr, value, res);
1083 #endif /* CONFIG_64BIT */
1087 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1093 switch (insn.mm_m_format.func) {
1095 reg = insn.mm_m_format.rd;
1099 /* LL,SC,LLD,SCD are not serviced */
1103 switch (insn.mm_x_format.func) {
1118 /* roll back jump/branch */
1119 regs->cp0_epc = origpc;
1120 regs->regs[31] = orig31;
1122 die_if_kernel("Unaligned FP access in kernel code", regs);
1123 BUG_ON(!used_math());
1124 BUG_ON(!is_fpu_owner());
1126 lose_fpu(1); /* save the FPU state for the emulator */
1127 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1129 own_fpu(1); /* restore FPU state */
1131 /* If something went wrong, signal */
1132 process_fpemu_return(res, fault_addr);
1139 reg = insn.mm_i_format.rt;
1143 reg = insn.mm_i_format.rt;
1147 reg = insn.mm_i_format.rt;
1151 reg = insn.mm_i_format.rt;
1155 reg = insn.mm_i_format.rt;
1159 reg = insn.mm_i_format.rt;
1163 reg = insn.mm_i_format.rt;
1167 switch (insn.mm16_m_format.func) {
1169 reg = insn.mm16_m_format.rlist;
1171 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1174 for (i = 16; rvar; rvar--, i++) {
1175 LoadW(addr, value, res);
1179 regs->regs[i] = value;
1181 LoadW(addr, value, res);
1184 regs->regs[31] = value;
1189 reg = insn.mm16_m_format.rlist;
1191 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1194 for (i = 16; rvar; rvar--, i++) {
1195 value = regs->regs[i];
1196 StoreW(addr, value, res);
1201 value = regs->regs[31];
1202 StoreW(addr, value, res);
1213 reg = reg16to32[insn.mm16_rb_format.rt];
1217 reg = reg16to32[insn.mm16_rb_format.rt];
1221 reg = reg16to32st[insn.mm16_rb_format.rt];
1225 reg = reg16to32st[insn.mm16_rb_format.rt];
1229 reg = insn.mm16_r5_format.rt;
1233 reg = insn.mm16_r5_format.rt;
1237 reg = reg16to32[insn.mm16_r3_format.rt];
1245 if (!access_ok(VERIFY_READ, addr, 2))
1248 LoadHW(addr, value, res);
1251 regs->regs[reg] = value;
1255 if (!access_ok(VERIFY_READ, addr, 2))
1258 LoadHWU(addr, value, res);
1261 regs->regs[reg] = value;
1265 if (!access_ok(VERIFY_READ, addr, 4))
1268 LoadW(addr, value, res);
1271 regs->regs[reg] = value;
1277 * A 32-bit kernel might be running on a 64-bit processor. But
1278 * if we're on a 32-bit processor and an i-cache incoherency
1279 * or race makes us see a 64-bit instruction here the sdl/sdr
1280 * would blow up, so for now we don't handle unaligned 64-bit
1281 * instructions on 32-bit kernels.
1283 if (!access_ok(VERIFY_READ, addr, 4))
1286 LoadWU(addr, value, res);
1289 regs->regs[reg] = value;
1291 #endif /* CONFIG_64BIT */
1293 /* Cannot handle 64-bit instructions in 32-bit kernel */
1299 * A 32-bit kernel might be running on a 64-bit processor. But
1300 * if we're on a 32-bit processor and an i-cache incoherency
1301 * or race makes us see a 64-bit instruction here the sdl/sdr
1302 * would blow up, so for now we don't handle unaligned 64-bit
1303 * instructions on 32-bit kernels.
1305 if (!access_ok(VERIFY_READ, addr, 8))
1308 LoadDW(addr, value, res);
1311 regs->regs[reg] = value;
1313 #endif /* CONFIG_64BIT */
1315 /* Cannot handle 64-bit instructions in 32-bit kernel */
1319 if (!access_ok(VERIFY_WRITE, addr, 2))
1322 value = regs->regs[reg];
1323 StoreHW(addr, value, res);
1329 if (!access_ok(VERIFY_WRITE, addr, 4))
1332 value = regs->regs[reg];
1333 StoreW(addr, value, res);
1341 * A 32-bit kernel might be running on a 64-bit processor. But
1342 * if we're on a 32-bit processor and an i-cache incoherency
1343 * or race makes us see a 64-bit instruction here the sdl/sdr
1344 * would blow up, so for now we don't handle unaligned 64-bit
1345 * instructions on 32-bit kernels.
1347 if (!access_ok(VERIFY_WRITE, addr, 8))
1350 value = regs->regs[reg];
1351 StoreDW(addr, value, res);
1355 #endif /* CONFIG_64BIT */
1357 /* Cannot handle 64-bit instructions in 32-bit kernel */
1361 regs->cp0_epc = contpc; /* advance or branch */
1363 #ifdef CONFIG_DEBUG_FS
1364 unaligned_instructions++;
1369 /* roll back jump/branch */
1370 regs->cp0_epc = origpc;
1371 regs->regs[31] = orig31;
1372 /* Did we have an exception handler installed? */
1373 if (fixup_exception(regs))
1376 die_if_kernel("Unhandled kernel unaligned access", regs);
1377 force_sig(SIGSEGV, current);
1382 die_if_kernel("Unhandled kernel unaligned access", regs);
1383 force_sig(SIGBUS, current);
1389 ("Unhandled kernel unaligned access or invalid instruction", regs);
1390 force_sig(SIGILL, current);
1393 static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
1395 unsigned long value;
1398 unsigned long orig31;
1400 unsigned long origpc;
1401 union mips16e_instruction mips16inst, oldinst;
1403 origpc = regs->cp0_epc;
1404 orig31 = regs->regs[31];
1405 pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
1407 * This load never faults.
1409 __get_user(mips16inst.full, pc16);
1410 oldinst = mips16inst;
1412 /* skip EXTEND instruction */
1413 if (mips16inst.ri.opcode == MIPS16e_extend_op) {
1415 __get_user(mips16inst.full, pc16);
1416 } else if (delay_slot(regs)) {
1417 /* skip jump instructions */
1418 /* JAL/JALX are 32 bits but have OPCODE in first short int */
1419 if (mips16inst.ri.opcode == MIPS16e_jal_op)
1422 if (get_user(mips16inst.full, pc16))
1426 switch (mips16inst.ri.opcode) {
1427 case MIPS16e_i64_op: /* I64 or RI64 instruction */
1428 switch (mips16inst.i64.func) { /* I64/RI64 func field check */
1429 case MIPS16e_ldpc_func:
1430 case MIPS16e_ldsp_func:
1431 reg = reg16to32[mips16inst.ri64.ry];
1434 case MIPS16e_sdsp_func:
1435 reg = reg16to32[mips16inst.ri64.ry];
1438 case MIPS16e_sdrasp_func:
1439 reg = 29; /* GPRSP */
1445 case MIPS16e_swsp_op:
1446 case MIPS16e_lwpc_op:
1447 case MIPS16e_lwsp_op:
1448 reg = reg16to32[mips16inst.ri.rx];
1452 if (mips16inst.i8.func != MIPS16e_swrasp_func)
1454 reg = 29; /* GPRSP */
1458 reg = reg16to32[mips16inst.rri.ry];
1462 switch (mips16inst.ri.opcode) {
1465 case MIPS16e_lbu_op:
1470 if (!access_ok(VERIFY_READ, addr, 2))
1473 LoadHW(addr, value, res);
1476 MIPS16e_compute_return_epc(regs, &oldinst);
1477 regs->regs[reg] = value;
1480 case MIPS16e_lhu_op:
1481 if (!access_ok(VERIFY_READ, addr, 2))
1484 LoadHWU(addr, value, res);
1487 MIPS16e_compute_return_epc(regs, &oldinst);
1488 regs->regs[reg] = value;
1492 case MIPS16e_lwpc_op:
1493 case MIPS16e_lwsp_op:
1494 if (!access_ok(VERIFY_READ, addr, 4))
1497 LoadW(addr, value, res);
1500 MIPS16e_compute_return_epc(regs, &oldinst);
1501 regs->regs[reg] = value;
1504 case MIPS16e_lwu_op:
1507 * A 32-bit kernel might be running on a 64-bit processor. But
1508 * if we're on a 32-bit processor and an i-cache incoherency
1509 * or race makes us see a 64-bit instruction here the sdl/sdr
1510 * would blow up, so for now we don't handle unaligned 64-bit
1511 * instructions on 32-bit kernels.
1513 if (!access_ok(VERIFY_READ, addr, 4))
1516 LoadWU(addr, value, res);
1519 MIPS16e_compute_return_epc(regs, &oldinst);
1520 regs->regs[reg] = value;
1522 #endif /* CONFIG_64BIT */
1524 /* Cannot handle 64-bit instructions in 32-bit kernel */
1531 * A 32-bit kernel might be running on a 64-bit processor. But
1532 * if we're on a 32-bit processor and an i-cache incoherency
1533 * or race makes us see a 64-bit instruction here the sdl/sdr
1534 * would blow up, so for now we don't handle unaligned 64-bit
1535 * instructions on 32-bit kernels.
1537 if (!access_ok(VERIFY_READ, addr, 8))
1540 LoadDW(addr, value, res);
1543 MIPS16e_compute_return_epc(regs, &oldinst);
1544 regs->regs[reg] = value;
1546 #endif /* CONFIG_64BIT */
1548 /* Cannot handle 64-bit instructions in 32-bit kernel */
1552 if (!access_ok(VERIFY_WRITE, addr, 2))
1555 MIPS16e_compute_return_epc(regs, &oldinst);
1556 value = regs->regs[reg];
1557 StoreHW(addr, value, res);
1563 case MIPS16e_swsp_op:
1564 case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
1565 if (!access_ok(VERIFY_WRITE, addr, 4))
1568 MIPS16e_compute_return_epc(regs, &oldinst);
1569 value = regs->regs[reg];
1570 StoreW(addr, value, res);
1579 * A 32-bit kernel might be running on a 64-bit processor. But
1580 * if we're on a 32-bit processor and an i-cache incoherency
1581 * or race makes us see a 64-bit instruction here the sdl/sdr
1582 * would blow up, so for now we don't handle unaligned 64-bit
1583 * instructions on 32-bit kernels.
1585 if (!access_ok(VERIFY_WRITE, addr, 8))
1588 MIPS16e_compute_return_epc(regs, &oldinst);
1589 value = regs->regs[reg];
1590 StoreDW(addr, value, res);
1594 #endif /* CONFIG_64BIT */
1596 /* Cannot handle 64-bit instructions in 32-bit kernel */
1601 * Pheeee... We encountered an yet unknown instruction or
1602 * cache coherence problem. Die sucker, die ...
1607 #ifdef CONFIG_DEBUG_FS
1608 unaligned_instructions++;
1614 /* roll back jump/branch */
1615 regs->cp0_epc = origpc;
1616 regs->regs[31] = orig31;
1617 /* Did we have an exception handler installed? */
1618 if (fixup_exception(regs))
1621 die_if_kernel("Unhandled kernel unaligned access", regs);
1622 force_sig(SIGSEGV, current);
1627 die_if_kernel("Unhandled kernel unaligned access", regs);
1628 force_sig(SIGBUS, current);
1634 ("Unhandled kernel unaligned access or invalid instruction", regs);
1635 force_sig(SIGILL, current);
1638 asmlinkage void do_ade(struct pt_regs *regs)
1640 enum ctx_state prev_state;
1641 unsigned int __user *pc;
1644 prev_state = exception_enter();
1645 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
1646 1, regs, regs->cp0_badvaddr);
1648 * Did we catch a fault trying to load an instruction?
1650 if (regs->cp0_badvaddr == regs->cp0_epc)
1653 if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
1655 if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
1659 * Do branch emulation only if we didn't forward the exception.
1660 * This is all so but ugly ...
1664 * Are we running in microMIPS mode?
1666 if (get_isa16_mode(regs->cp0_epc)) {
1668 * Did we catch a fault trying to load an instruction in
1671 if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
1673 if (unaligned_action == UNALIGNED_ACTION_SHOW)
1674 show_registers(regs);
1676 if (cpu_has_mmips) {
1678 if (!user_mode(regs))
1680 emulate_load_store_microMIPS(regs,
1681 (void __user *)regs->cp0_badvaddr);
1687 if (cpu_has_mips16) {
1689 if (!user_mode(regs))
1691 emulate_load_store_MIPS16e(regs,
1692 (void __user *)regs->cp0_badvaddr);
1701 if (unaligned_action == UNALIGNED_ACTION_SHOW)
1702 show_registers(regs);
1703 pc = (unsigned int __user *)exception_epc(regs);
1706 if (!user_mode(regs))
1708 emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
1714 die_if_kernel("Kernel unaligned instruction access", regs);
1715 force_sig(SIGBUS, current);
1718 * XXX On return from the signal handler we should advance the epc
1720 exception_exit(prev_state);
1723 #ifdef CONFIG_DEBUG_FS
1724 extern struct dentry *mips_debugfs_dir;
1725 static int __init debugfs_unaligned(void)
1729 if (!mips_debugfs_dir)
1731 d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
1732 mips_debugfs_dir, &unaligned_instructions);
1735 d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
1736 mips_debugfs_dir, &unaligned_action);
1741 __initcall(debugfs_unaligned);