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/signal.h>
77 #include <linux/smp.h>
78 #include <linux/sched.h>
79 #include <linux/debugfs.h>
80 #include <linux/perf_event.h>
83 #include <asm/branch.h>
84 #include <asm/byteorder.h>
87 #include <asm/fpu_emulator.h>
89 #include <asm/uaccess.h>
91 #include <asm/fpu_emulator.h>
93 #define STR(x) __STR(x)
97 UNALIGNED_ACTION_QUIET,
98 UNALIGNED_ACTION_SIGNAL,
99 UNALIGNED_ACTION_SHOW,
101 #ifdef CONFIG_DEBUG_FS
102 static u32 unaligned_instructions;
103 static u32 unaligned_action;
105 #define unaligned_action UNALIGNED_ACTION_QUIET
107 extern void show_registers(struct pt_regs *regs);
110 #define LoadHW(addr, value, res) \
111 __asm__ __volatile__ (".set\tnoat\n" \
112 "1:\tlb\t%0, 0(%2)\n" \
113 "2:\tlbu\t$1, 1(%2)\n\t" \
119 ".section\t.fixup,\"ax\"\n\t" \
120 "4:\tli\t%1, %3\n\t" \
123 ".section\t__ex_table,\"a\"\n\t" \
124 STR(PTR)"\t1b, 4b\n\t" \
125 STR(PTR)"\t2b, 4b\n\t" \
127 : "=&r" (value), "=r" (res) \
128 : "r" (addr), "i" (-EFAULT));
130 #define LoadW(addr, value, res) \
131 __asm__ __volatile__ ( \
132 "1:\tlwl\t%0, (%2)\n" \
133 "2:\tlwr\t%0, 3(%2)\n\t" \
137 ".section\t.fixup,\"ax\"\n\t" \
138 "4:\tli\t%1, %3\n\t" \
141 ".section\t__ex_table,\"a\"\n\t" \
142 STR(PTR)"\t1b, 4b\n\t" \
143 STR(PTR)"\t2b, 4b\n\t" \
145 : "=&r" (value), "=r" (res) \
146 : "r" (addr), "i" (-EFAULT));
148 #define LoadHWU(addr, value, res) \
149 __asm__ __volatile__ ( \
151 "1:\tlbu\t%0, 0(%2)\n" \
152 "2:\tlbu\t$1, 1(%2)\n\t" \
159 ".section\t.fixup,\"ax\"\n\t" \
160 "4:\tli\t%1, %3\n\t" \
163 ".section\t__ex_table,\"a\"\n\t" \
164 STR(PTR)"\t1b, 4b\n\t" \
165 STR(PTR)"\t2b, 4b\n\t" \
167 : "=&r" (value), "=r" (res) \
168 : "r" (addr), "i" (-EFAULT));
170 #define LoadWU(addr, value, res) \
171 __asm__ __volatile__ ( \
172 "1:\tlwl\t%0, (%2)\n" \
173 "2:\tlwr\t%0, 3(%2)\n\t" \
174 "dsll\t%0, %0, 32\n\t" \
175 "dsrl\t%0, %0, 32\n\t" \
179 "\t.section\t.fixup,\"ax\"\n\t" \
180 "4:\tli\t%1, %3\n\t" \
183 ".section\t__ex_table,\"a\"\n\t" \
184 STR(PTR)"\t1b, 4b\n\t" \
185 STR(PTR)"\t2b, 4b\n\t" \
187 : "=&r" (value), "=r" (res) \
188 : "r" (addr), "i" (-EFAULT));
190 #define LoadDW(addr, value, res) \
191 __asm__ __volatile__ ( \
192 "1:\tldl\t%0, (%2)\n" \
193 "2:\tldr\t%0, 7(%2)\n\t" \
197 "\t.section\t.fixup,\"ax\"\n\t" \
198 "4:\tli\t%1, %3\n\t" \
201 ".section\t__ex_table,\"a\"\n\t" \
202 STR(PTR)"\t1b, 4b\n\t" \
203 STR(PTR)"\t2b, 4b\n\t" \
205 : "=&r" (value), "=r" (res) \
206 : "r" (addr), "i" (-EFAULT));
208 #define StoreHW(addr, value, res) \
209 __asm__ __volatile__ ( \
211 "1:\tsb\t%1, 1(%2)\n\t" \
212 "srl\t$1, %1, 0x8\n" \
213 "2:\tsb\t$1, 0(%2)\n\t" \
218 ".section\t.fixup,\"ax\"\n\t" \
219 "4:\tli\t%0, %3\n\t" \
222 ".section\t__ex_table,\"a\"\n\t" \
223 STR(PTR)"\t1b, 4b\n\t" \
224 STR(PTR)"\t2b, 4b\n\t" \
227 : "r" (value), "r" (addr), "i" (-EFAULT));
229 #define StoreW(addr, value, res) \
230 __asm__ __volatile__ ( \
231 "1:\tswl\t%1,(%2)\n" \
232 "2:\tswr\t%1, 3(%2)\n\t" \
236 ".section\t.fixup,\"ax\"\n\t" \
237 "4:\tli\t%0, %3\n\t" \
240 ".section\t__ex_table,\"a\"\n\t" \
241 STR(PTR)"\t1b, 4b\n\t" \
242 STR(PTR)"\t2b, 4b\n\t" \
245 : "r" (value), "r" (addr), "i" (-EFAULT));
247 #define StoreDW(addr, value, res) \
248 __asm__ __volatile__ ( \
249 "1:\tsdl\t%1,(%2)\n" \
250 "2:\tsdr\t%1, 7(%2)\n\t" \
254 ".section\t.fixup,\"ax\"\n\t" \
255 "4:\tli\t%0, %3\n\t" \
258 ".section\t__ex_table,\"a\"\n\t" \
259 STR(PTR)"\t1b, 4b\n\t" \
260 STR(PTR)"\t2b, 4b\n\t" \
263 : "r" (value), "r" (addr), "i" (-EFAULT));
266 #ifdef __LITTLE_ENDIAN
267 #define LoadHW(addr, value, res) \
268 __asm__ __volatile__ (".set\tnoat\n" \
269 "1:\tlb\t%0, 1(%2)\n" \
270 "2:\tlbu\t$1, 0(%2)\n\t" \
276 ".section\t.fixup,\"ax\"\n\t" \
277 "4:\tli\t%1, %3\n\t" \
280 ".section\t__ex_table,\"a\"\n\t" \
281 STR(PTR)"\t1b, 4b\n\t" \
282 STR(PTR)"\t2b, 4b\n\t" \
284 : "=&r" (value), "=r" (res) \
285 : "r" (addr), "i" (-EFAULT));
287 #define LoadW(addr, value, res) \
288 __asm__ __volatile__ ( \
289 "1:\tlwl\t%0, 3(%2)\n" \
290 "2:\tlwr\t%0, (%2)\n\t" \
294 ".section\t.fixup,\"ax\"\n\t" \
295 "4:\tli\t%1, %3\n\t" \
298 ".section\t__ex_table,\"a\"\n\t" \
299 STR(PTR)"\t1b, 4b\n\t" \
300 STR(PTR)"\t2b, 4b\n\t" \
302 : "=&r" (value), "=r" (res) \
303 : "r" (addr), "i" (-EFAULT));
305 #define LoadHWU(addr, value, res) \
306 __asm__ __volatile__ ( \
308 "1:\tlbu\t%0, 1(%2)\n" \
309 "2:\tlbu\t$1, 0(%2)\n\t" \
316 ".section\t.fixup,\"ax\"\n\t" \
317 "4:\tli\t%1, %3\n\t" \
320 ".section\t__ex_table,\"a\"\n\t" \
321 STR(PTR)"\t1b, 4b\n\t" \
322 STR(PTR)"\t2b, 4b\n\t" \
324 : "=&r" (value), "=r" (res) \
325 : "r" (addr), "i" (-EFAULT));
327 #define LoadWU(addr, value, res) \
328 __asm__ __volatile__ ( \
329 "1:\tlwl\t%0, 3(%2)\n" \
330 "2:\tlwr\t%0, (%2)\n\t" \
331 "dsll\t%0, %0, 32\n\t" \
332 "dsrl\t%0, %0, 32\n\t" \
336 "\t.section\t.fixup,\"ax\"\n\t" \
337 "4:\tli\t%1, %3\n\t" \
340 ".section\t__ex_table,\"a\"\n\t" \
341 STR(PTR)"\t1b, 4b\n\t" \
342 STR(PTR)"\t2b, 4b\n\t" \
344 : "=&r" (value), "=r" (res) \
345 : "r" (addr), "i" (-EFAULT));
347 #define LoadDW(addr, value, res) \
348 __asm__ __volatile__ ( \
349 "1:\tldl\t%0, 7(%2)\n" \
350 "2:\tldr\t%0, (%2)\n\t" \
354 "\t.section\t.fixup,\"ax\"\n\t" \
355 "4:\tli\t%1, %3\n\t" \
358 ".section\t__ex_table,\"a\"\n\t" \
359 STR(PTR)"\t1b, 4b\n\t" \
360 STR(PTR)"\t2b, 4b\n\t" \
362 : "=&r" (value), "=r" (res) \
363 : "r" (addr), "i" (-EFAULT));
365 #define StoreHW(addr, value, res) \
366 __asm__ __volatile__ ( \
368 "1:\tsb\t%1, 0(%2)\n\t" \
369 "srl\t$1,%1, 0x8\n" \
370 "2:\tsb\t$1, 1(%2)\n\t" \
375 ".section\t.fixup,\"ax\"\n\t" \
376 "4:\tli\t%0, %3\n\t" \
379 ".section\t__ex_table,\"a\"\n\t" \
380 STR(PTR)"\t1b, 4b\n\t" \
381 STR(PTR)"\t2b, 4b\n\t" \
384 : "r" (value), "r" (addr), "i" (-EFAULT));
386 #define StoreW(addr, value, res) \
387 __asm__ __volatile__ ( \
388 "1:\tswl\t%1, 3(%2)\n" \
389 "2:\tswr\t%1, (%2)\n\t" \
393 ".section\t.fixup,\"ax\"\n\t" \
394 "4:\tli\t%0, %3\n\t" \
397 ".section\t__ex_table,\"a\"\n\t" \
398 STR(PTR)"\t1b, 4b\n\t" \
399 STR(PTR)"\t2b, 4b\n\t" \
402 : "r" (value), "r" (addr), "i" (-EFAULT));
404 #define StoreDW(addr, value, res) \
405 __asm__ __volatile__ ( \
406 "1:\tsdl\t%1, 7(%2)\n" \
407 "2:\tsdr\t%1, (%2)\n\t" \
411 ".section\t.fixup,\"ax\"\n\t" \
412 "4:\tli\t%0, %3\n\t" \
415 ".section\t__ex_table,\"a\"\n\t" \
416 STR(PTR)"\t1b, 4b\n\t" \
417 STR(PTR)"\t2b, 4b\n\t" \
420 : "r" (value), "r" (addr), "i" (-EFAULT));
423 static void emulate_load_store_insn(struct pt_regs *regs,
424 void __user *addr, unsigned int __user *pc)
426 union mips_instruction insn;
429 unsigned long origpc;
430 unsigned long orig31;
431 void __user *fault_addr = NULL;
433 origpc = (unsigned long)pc;
434 orig31 = regs->regs[31];
436 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
439 * This load never faults.
441 __get_user(insn.word, pc);
443 switch (insn.i_format.opcode) {
445 * These are instructions that a compiler doesn't generate. We
446 * can assume therefore that the code is MIPS-aware and
447 * really buggy. Emulating these instructions would break the
456 * For these instructions the only way to create an address
457 * error is an attempted access to kernel/supervisor address
474 * The remaining opcodes are the ones that are really of
478 if (!access_ok(VERIFY_READ, addr, 2))
481 LoadHW(addr, value, res);
484 compute_return_epc(regs);
485 regs->regs[insn.i_format.rt] = value;
489 if (!access_ok(VERIFY_READ, addr, 4))
492 LoadW(addr, value, res);
495 compute_return_epc(regs);
496 regs->regs[insn.i_format.rt] = value;
500 if (!access_ok(VERIFY_READ, addr, 2))
503 LoadHWU(addr, value, res);
506 compute_return_epc(regs);
507 regs->regs[insn.i_format.rt] = value;
513 * A 32-bit kernel might be running on a 64-bit processor. But
514 * if we're on a 32-bit processor and an i-cache incoherency
515 * or race makes us see a 64-bit instruction here the sdl/sdr
516 * would blow up, so for now we don't handle unaligned 64-bit
517 * instructions on 32-bit kernels.
519 if (!access_ok(VERIFY_READ, addr, 4))
522 LoadWU(addr, value, res);
525 compute_return_epc(regs);
526 regs->regs[insn.i_format.rt] = value;
528 #endif /* CONFIG_64BIT */
530 /* Cannot handle 64-bit instructions in 32-bit kernel */
536 * A 32-bit kernel might be running on a 64-bit processor. But
537 * if we're on a 32-bit processor and an i-cache incoherency
538 * or race makes us see a 64-bit instruction here the sdl/sdr
539 * would blow up, so for now we don't handle unaligned 64-bit
540 * instructions on 32-bit kernels.
542 if (!access_ok(VERIFY_READ, addr, 8))
545 LoadDW(addr, value, res);
548 compute_return_epc(regs);
549 regs->regs[insn.i_format.rt] = value;
551 #endif /* CONFIG_64BIT */
553 /* Cannot handle 64-bit instructions in 32-bit kernel */
557 if (!access_ok(VERIFY_WRITE, addr, 2))
560 compute_return_epc(regs);
561 value = regs->regs[insn.i_format.rt];
562 StoreHW(addr, value, res);
568 if (!access_ok(VERIFY_WRITE, addr, 4))
571 compute_return_epc(regs);
572 value = regs->regs[insn.i_format.rt];
573 StoreW(addr, value, res);
581 * A 32-bit kernel might be running on a 64-bit processor. But
582 * if we're on a 32-bit processor and an i-cache incoherency
583 * or race makes us see a 64-bit instruction here the sdl/sdr
584 * would blow up, so for now we don't handle unaligned 64-bit
585 * instructions on 32-bit kernels.
587 if (!access_ok(VERIFY_WRITE, addr, 8))
590 compute_return_epc(regs);
591 value = regs->regs[insn.i_format.rt];
592 StoreDW(addr, value, res);
596 #endif /* CONFIG_64BIT */
598 /* Cannot handle 64-bit instructions in 32-bit kernel */
605 die_if_kernel("Unaligned FP access in kernel code", regs);
606 BUG_ON(!used_math());
607 BUG_ON(!is_fpu_owner());
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 void emulate_load_store_microMIPS(struct pt_regs *regs, void __user * addr)
692 unsigned int reg = 0, rvar;
693 unsigned long orig31;
697 unsigned long origpc, contpc;
698 union mips_instruction insn;
699 struct mm_decoded_insn mminsn;
700 void __user *fault_addr = NULL;
702 origpc = regs->cp0_epc;
703 orig31 = regs->regs[31];
705 mminsn.micro_mips_mode = 1;
708 * This load never faults.
710 pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
711 __get_user(halfword, pc16);
713 contpc = regs->cp0_epc + 2;
714 word = ((unsigned int)halfword << 16);
717 if (!mm_insn_16bit(halfword)) {
718 __get_user(halfword, pc16);
720 contpc = regs->cp0_epc + 4;
726 if (get_user(halfword, pc16))
728 mminsn.next_pc_inc = 2;
729 word = ((unsigned int)halfword << 16);
731 if (!mm_insn_16bit(halfword)) {
733 if (get_user(halfword, pc16))
735 mminsn.next_pc_inc = 4;
738 mminsn.next_insn = word;
740 insn = (union mips_instruction)(mminsn.insn);
741 if (mm_isBranchInstr(regs, mminsn, &contpc))
742 insn = (union mips_instruction)(mminsn.next_insn);
744 /* Parse instruction to find what to do */
746 switch (insn.mm_i_format.opcode) {
749 switch (insn.mm_x_format.func) {
751 reg = insn.mm_x_format.rd;
758 switch (insn.mm_m_format.func) {
760 reg = insn.mm_m_format.rd;
764 if (!access_ok(VERIFY_READ, addr, 8))
767 LoadW(addr, value, res);
770 regs->regs[reg] = value;
772 LoadW(addr, value, res);
775 regs->regs[reg + 1] = value;
779 reg = insn.mm_m_format.rd;
783 if (!access_ok(VERIFY_WRITE, addr, 8))
786 value = regs->regs[reg];
787 StoreW(addr, value, res);
791 value = regs->regs[reg + 1];
792 StoreW(addr, value, res);
799 reg = insn.mm_m_format.rd;
803 if (!access_ok(VERIFY_READ, addr, 16))
806 LoadDW(addr, value, res);
809 regs->regs[reg] = value;
811 LoadDW(addr, value, res);
814 regs->regs[reg + 1] = value;
816 #endif /* CONFIG_64BIT */
822 reg = insn.mm_m_format.rd;
826 if (!access_ok(VERIFY_WRITE, addr, 16))
829 value = regs->regs[reg];
830 StoreDW(addr, value, res);
834 value = regs->regs[reg + 1];
835 StoreDW(addr, value, res);
839 #endif /* CONFIG_64BIT */
844 reg = insn.mm_m_format.rd;
846 if ((rvar > 9) || !reg)
850 (VERIFY_READ, addr, 4 * (rvar + 1)))
853 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
858 for (i = 16; rvar; rvar--, i++) {
859 LoadW(addr, value, res);
863 regs->regs[i] = value;
865 if ((reg & 0xf) == 9) {
866 LoadW(addr, value, res);
870 regs->regs[30] = value;
873 LoadW(addr, value, res);
876 regs->regs[31] = value;
881 reg = insn.mm_m_format.rd;
883 if ((rvar > 9) || !reg)
887 (VERIFY_WRITE, addr, 4 * (rvar + 1)))
890 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
895 for (i = 16; rvar; rvar--, i++) {
896 value = regs->regs[i];
897 StoreW(addr, value, res);
902 if ((reg & 0xf) == 9) {
903 value = regs->regs[30];
904 StoreW(addr, value, res);
910 value = regs->regs[31];
911 StoreW(addr, value, res);
919 reg = insn.mm_m_format.rd;
921 if ((rvar > 9) || !reg)
925 (VERIFY_READ, addr, 8 * (rvar + 1)))
928 if (!access_ok(VERIFY_READ, addr, 8 * rvar))
934 for (i = 16; rvar; rvar--, i++) {
935 LoadDW(addr, value, res);
939 regs->regs[i] = value;
941 if ((reg & 0xf) == 9) {
942 LoadDW(addr, value, res);
946 regs->regs[30] = value;
949 LoadDW(addr, value, res);
952 regs->regs[31] = value;
955 #endif /* CONFIG_64BIT */
961 reg = insn.mm_m_format.rd;
963 if ((rvar > 9) || !reg)
967 (VERIFY_WRITE, addr, 8 * (rvar + 1)))
970 if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
976 for (i = 16; rvar; rvar--, i++) {
977 value = regs->regs[i];
978 StoreDW(addr, value, res);
983 if ((reg & 0xf) == 9) {
984 value = regs->regs[30];
985 StoreDW(addr, value, res);
991 value = regs->regs[31];
992 StoreDW(addr, value, res);
997 #endif /* CONFIG_64BIT */
1001 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1007 switch (insn.mm_m_format.func) {
1009 reg = insn.mm_m_format.rd;
1013 /* LL,SC,LLD,SCD are not serviced */
1017 switch (insn.mm_x_format.func) {
1032 /* roll back jump/branch */
1033 regs->cp0_epc = origpc;
1034 regs->regs[31] = orig31;
1036 die_if_kernel("Unaligned FP access in kernel code", regs);
1037 BUG_ON(!used_math());
1038 BUG_ON(!is_fpu_owner());
1040 lose_fpu(1); /* save the FPU state for the emulator */
1041 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1043 own_fpu(1); /* restore FPU state */
1045 /* If something went wrong, signal */
1046 process_fpemu_return(res, fault_addr);
1053 reg = insn.mm_i_format.rt;
1057 reg = insn.mm_i_format.rt;
1061 reg = insn.mm_i_format.rt;
1065 reg = insn.mm_i_format.rt;
1069 reg = insn.mm_i_format.rt;
1073 reg = insn.mm_i_format.rt;
1077 reg = insn.mm_i_format.rt;
1081 switch (insn.mm16_m_format.func) {
1083 reg = insn.mm16_m_format.rlist;
1085 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1088 for (i = 16; rvar; rvar--, i++) {
1089 LoadW(addr, value, res);
1093 regs->regs[i] = value;
1095 LoadW(addr, value, res);
1098 regs->regs[31] = value;
1103 reg = insn.mm16_m_format.rlist;
1105 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1108 for (i = 16; rvar; rvar--, i++) {
1109 value = regs->regs[i];
1110 StoreW(addr, value, res);
1115 value = regs->regs[31];
1116 StoreW(addr, value, res);
1127 reg = reg16to32[insn.mm16_rb_format.rt];
1131 reg = reg16to32[insn.mm16_rb_format.rt];
1135 reg = reg16to32st[insn.mm16_rb_format.rt];
1139 reg = reg16to32st[insn.mm16_rb_format.rt];
1143 reg = insn.mm16_r5_format.rt;
1147 reg = insn.mm16_r5_format.rt;
1151 reg = reg16to32[insn.mm16_r3_format.rt];
1159 if (!access_ok(VERIFY_READ, addr, 2))
1162 LoadHW(addr, value, res);
1165 regs->regs[reg] = value;
1169 if (!access_ok(VERIFY_READ, addr, 2))
1172 LoadHWU(addr, value, res);
1175 regs->regs[reg] = value;
1179 if (!access_ok(VERIFY_READ, addr, 4))
1182 LoadW(addr, value, res);
1185 regs->regs[reg] = value;
1191 * A 32-bit kernel might be running on a 64-bit processor. But
1192 * if we're on a 32-bit processor and an i-cache incoherency
1193 * or race makes us see a 64-bit instruction here the sdl/sdr
1194 * would blow up, so for now we don't handle unaligned 64-bit
1195 * instructions on 32-bit kernels.
1197 if (!access_ok(VERIFY_READ, addr, 4))
1200 LoadWU(addr, value, res);
1203 regs->regs[reg] = value;
1205 #endif /* CONFIG_64BIT */
1207 /* Cannot handle 64-bit instructions in 32-bit kernel */
1213 * A 32-bit kernel might be running on a 64-bit processor. But
1214 * if we're on a 32-bit processor and an i-cache incoherency
1215 * or race makes us see a 64-bit instruction here the sdl/sdr
1216 * would blow up, so for now we don't handle unaligned 64-bit
1217 * instructions on 32-bit kernels.
1219 if (!access_ok(VERIFY_READ, addr, 8))
1222 LoadDW(addr, value, res);
1225 regs->regs[reg] = value;
1227 #endif /* CONFIG_64BIT */
1229 /* Cannot handle 64-bit instructions in 32-bit kernel */
1233 if (!access_ok(VERIFY_WRITE, addr, 2))
1236 value = regs->regs[reg];
1237 StoreHW(addr, value, res);
1243 if (!access_ok(VERIFY_WRITE, addr, 4))
1246 value = regs->regs[reg];
1247 StoreW(addr, value, res);
1255 * A 32-bit kernel might be running on a 64-bit processor. But
1256 * if we're on a 32-bit processor and an i-cache incoherency
1257 * or race makes us see a 64-bit instruction here the sdl/sdr
1258 * would blow up, so for now we don't handle unaligned 64-bit
1259 * instructions on 32-bit kernels.
1261 if (!access_ok(VERIFY_WRITE, addr, 8))
1264 value = regs->regs[reg];
1265 StoreDW(addr, value, res);
1269 #endif /* CONFIG_64BIT */
1271 /* Cannot handle 64-bit instructions in 32-bit kernel */
1275 regs->cp0_epc = contpc; /* advance or branch */
1277 #ifdef CONFIG_DEBUG_FS
1278 unaligned_instructions++;
1283 /* roll back jump/branch */
1284 regs->cp0_epc = origpc;
1285 regs->regs[31] = orig31;
1286 /* Did we have an exception handler installed? */
1287 if (fixup_exception(regs))
1290 die_if_kernel("Unhandled kernel unaligned access", regs);
1291 force_sig(SIGSEGV, current);
1296 die_if_kernel("Unhandled kernel unaligned access", regs);
1297 force_sig(SIGBUS, current);
1303 ("Unhandled kernel unaligned access or invalid instruction", regs);
1304 force_sig(SIGILL, current);
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 *)msk_isa16_mode(origpc);
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 = reg16to32[mips16inst.ri64.ry];
1348 case MIPS16e_sdsp_func:
1349 reg = reg16to32[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 = reg16to32[mips16inst.ri.rx];
1366 if (mips16inst.i8.func != MIPS16e_swrasp_func)
1368 reg = 29; /* GPRSP */
1372 reg = reg16to32[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);
1551 asmlinkage void do_ade(struct pt_regs *regs)
1553 unsigned int __user *pc;
1556 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
1557 1, regs, regs->cp0_badvaddr);
1559 * Did we catch a fault trying to load an instruction?
1561 if (regs->cp0_badvaddr == regs->cp0_epc)
1564 if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
1566 if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
1570 * Do branch emulation only if we didn't forward the exception.
1571 * This is all so but ugly ...
1575 * Are we running in microMIPS mode?
1577 if (get_isa16_mode(regs->cp0_epc)) {
1579 * Did we catch a fault trying to load an instruction in
1582 if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
1584 if (unaligned_action == UNALIGNED_ACTION_SHOW)
1585 show_registers(regs);
1587 if (cpu_has_mmips) {
1589 if (!user_mode(regs))
1591 emulate_load_store_microMIPS(regs,
1592 (void __user *)regs->cp0_badvaddr);
1598 if (cpu_has_mips16) {
1600 if (!user_mode(regs))
1602 emulate_load_store_MIPS16e(regs,
1603 (void __user *)regs->cp0_badvaddr);
1612 if (unaligned_action == UNALIGNED_ACTION_SHOW)
1613 show_registers(regs);
1614 pc = (unsigned int __user *)exception_epc(regs);
1617 if (!user_mode(regs))
1619 emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
1625 die_if_kernel("Kernel unaligned instruction access", regs);
1626 force_sig(SIGBUS, current);
1629 * XXX On return from the signal handler we should advance the epc
1633 #ifdef CONFIG_DEBUG_FS
1634 extern struct dentry *mips_debugfs_dir;
1635 static int __init debugfs_unaligned(void)
1639 if (!mips_debugfs_dir)
1641 d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
1642 mips_debugfs_dir, &unaligned_instructions);
1645 d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
1646 mips_debugfs_dir, &unaligned_action);
1651 __initcall(debugfs_unaligned);