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>
88 #include <asm/debug.h>
90 #include <asm/fpu_emulator.h>
92 #include <asm/uaccess.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, type) \
113 __asm__ __volatile__ (".set\tnoat\n" \
114 "1:\t"type##_lb("%0", "0(%2)")"\n" \
115 "2:\t"type##_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)); \
133 #ifndef CONFIG_CPU_MIPSR6
134 #define _LoadW(addr, value, res, type) \
136 __asm__ __volatile__ ( \
137 "1:\t"type##_lwl("%0", "(%2)")"\n" \
138 "2:\t"type##_lwr("%0", "3(%2)")"\n\t"\
142 ".section\t.fixup,\"ax\"\n\t" \
143 "4:\tli\t%1, %3\n\t" \
146 ".section\t__ex_table,\"a\"\n\t" \
147 STR(PTR)"\t1b, 4b\n\t" \
148 STR(PTR)"\t2b, 4b\n\t" \
150 : "=&r" (value), "=r" (res) \
151 : "r" (addr), "i" (-EFAULT)); \
155 /* MIPSR6 has no lwl instruction */
156 #define _LoadW(addr, value, res, type) \
158 __asm__ __volatile__ ( \
161 "1:"type##_lb("%0", "0(%2)")"\n\t" \
162 "2:"type##_lbu("$1", "1(%2)")"\n\t" \
165 "3:"type##_lbu("$1", "2(%2)")"\n\t" \
168 "4:"type##_lbu("$1", "3(%2)")"\n\t" \
175 ".section\t.fixup,\"ax\"\n\t" \
176 "11:\tli\t%1, %3\n\t" \
179 ".section\t__ex_table,\"a\"\n\t" \
180 STR(PTR)"\t1b, 11b\n\t" \
181 STR(PTR)"\t2b, 11b\n\t" \
182 STR(PTR)"\t3b, 11b\n\t" \
183 STR(PTR)"\t4b, 11b\n\t" \
185 : "=&r" (value), "=r" (res) \
186 : "r" (addr), "i" (-EFAULT)); \
189 #endif /* CONFIG_CPU_MIPSR6 */
191 #define _LoadHWU(addr, value, res, type) \
193 __asm__ __volatile__ ( \
195 "1:\t"type##_lbu("%0", "0(%2)")"\n" \
196 "2:\t"type##_lbu("$1", "1(%2)")"\n\t"\
203 ".section\t.fixup,\"ax\"\n\t" \
204 "4:\tli\t%1, %3\n\t" \
207 ".section\t__ex_table,\"a\"\n\t" \
208 STR(PTR)"\t1b, 4b\n\t" \
209 STR(PTR)"\t2b, 4b\n\t" \
211 : "=&r" (value), "=r" (res) \
212 : "r" (addr), "i" (-EFAULT)); \
215 #ifndef CONFIG_CPU_MIPSR6
216 #define _LoadWU(addr, value, res, type) \
218 __asm__ __volatile__ ( \
219 "1:\t"type##_lwl("%0", "(%2)")"\n" \
220 "2:\t"type##_lwr("%0", "3(%2)")"\n\t"\
221 "dsll\t%0, %0, 32\n\t" \
222 "dsrl\t%0, %0, 32\n\t" \
226 "\t.section\t.fixup,\"ax\"\n\t" \
227 "4:\tli\t%1, %3\n\t" \
230 ".section\t__ex_table,\"a\"\n\t" \
231 STR(PTR)"\t1b, 4b\n\t" \
232 STR(PTR)"\t2b, 4b\n\t" \
234 : "=&r" (value), "=r" (res) \
235 : "r" (addr), "i" (-EFAULT)); \
238 #define _LoadDW(addr, value, res) \
240 __asm__ __volatile__ ( \
241 "1:\tldl\t%0, (%2)\n" \
242 "2:\tldr\t%0, 7(%2)\n\t" \
246 "\t.section\t.fixup,\"ax\"\n\t" \
247 "4:\tli\t%1, %3\n\t" \
250 ".section\t__ex_table,\"a\"\n\t" \
251 STR(PTR)"\t1b, 4b\n\t" \
252 STR(PTR)"\t2b, 4b\n\t" \
254 : "=&r" (value), "=r" (res) \
255 : "r" (addr), "i" (-EFAULT)); \
259 /* MIPSR6 has not lwl and ldl instructions */
260 #define _LoadWU(addr, value, res, type) \
262 __asm__ __volatile__ ( \
265 "1:"type##_lbu("%0", "0(%2)")"\n\t" \
266 "2:"type##_lbu("$1", "1(%2)")"\n\t" \
269 "3:"type##_lbu("$1", "2(%2)")"\n\t" \
272 "4:"type##_lbu("$1", "3(%2)")"\n\t" \
279 ".section\t.fixup,\"ax\"\n\t" \
280 "11:\tli\t%1, %3\n\t" \
283 ".section\t__ex_table,\"a\"\n\t" \
284 STR(PTR)"\t1b, 11b\n\t" \
285 STR(PTR)"\t2b, 11b\n\t" \
286 STR(PTR)"\t3b, 11b\n\t" \
287 STR(PTR)"\t4b, 11b\n\t" \
289 : "=&r" (value), "=r" (res) \
290 : "r" (addr), "i" (-EFAULT)); \
293 #define _LoadDW(addr, value, res) \
295 __asm__ __volatile__ ( \
298 "1:lb\t%0, 0(%2)\n\t" \
299 "2:lbu\t $1, 1(%2)\n\t" \
300 "dsll\t%0, 0x8\n\t" \
302 "3:lbu\t$1, 2(%2)\n\t" \
303 "dsll\t%0, 0x8\n\t" \
305 "4:lbu\t$1, 3(%2)\n\t" \
306 "dsll\t%0, 0x8\n\t" \
308 "5:lbu\t$1, 4(%2)\n\t" \
309 "dsll\t%0, 0x8\n\t" \
311 "6:lbu\t$1, 5(%2)\n\t" \
312 "dsll\t%0, 0x8\n\t" \
314 "7:lbu\t$1, 6(%2)\n\t" \
315 "dsll\t%0, 0x8\n\t" \
317 "8:lbu\t$1, 7(%2)\n\t" \
318 "dsll\t%0, 0x8\n\t" \
324 ".section\t.fixup,\"ax\"\n\t" \
325 "11:\tli\t%1, %3\n\t" \
328 ".section\t__ex_table,\"a\"\n\t" \
329 STR(PTR)"\t1b, 11b\n\t" \
330 STR(PTR)"\t2b, 11b\n\t" \
331 STR(PTR)"\t3b, 11b\n\t" \
332 STR(PTR)"\t4b, 11b\n\t" \
333 STR(PTR)"\t5b, 11b\n\t" \
334 STR(PTR)"\t6b, 11b\n\t" \
335 STR(PTR)"\t7b, 11b\n\t" \
336 STR(PTR)"\t8b, 11b\n\t" \
338 : "=&r" (value), "=r" (res) \
339 : "r" (addr), "i" (-EFAULT)); \
342 #endif /* CONFIG_CPU_MIPSR6 */
345 #define _StoreHW(addr, value, res, type) \
347 __asm__ __volatile__ ( \
349 "1:\t"type##_sb("%1", "1(%2)")"\n" \
350 "srl\t$1, %1, 0x8\n" \
351 "2:\t"type##_sb("$1", "0(%2)")"\n" \
356 ".section\t.fixup,\"ax\"\n\t" \
357 "4:\tli\t%0, %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" \
365 : "r" (value), "r" (addr), "i" (-EFAULT));\
368 #ifndef CONFIG_CPU_MIPSR6
369 #define _StoreW(addr, value, res, type) \
371 __asm__ __volatile__ ( \
372 "1:\t"type##_swl("%1", "(%2)")"\n" \
373 "2:\t"type##_swr("%1", "3(%2)")"\n\t"\
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)); \
389 #define _StoreDW(addr, value, res) \
391 __asm__ __volatile__ ( \
392 "1:\tsdl\t%1,(%2)\n" \
393 "2:\tsdr\t%1, 7(%2)\n\t" \
397 ".section\t.fixup,\"ax\"\n\t" \
398 "4:\tli\t%0, %3\n\t" \
401 ".section\t__ex_table,\"a\"\n\t" \
402 STR(PTR)"\t1b, 4b\n\t" \
403 STR(PTR)"\t2b, 4b\n\t" \
406 : "r" (value), "r" (addr), "i" (-EFAULT)); \
410 /* MIPSR6 has no swl and sdl instructions */
411 #define _StoreW(addr, value, res, type) \
413 __asm__ __volatile__ ( \
416 "1:"type##_sb("%1", "3(%2)")"\n\t" \
417 "srl\t$1, %1, 0x8\n\t" \
418 "2:"type##_sb("$1", "2(%2)")"\n\t" \
419 "srl\t$1, $1, 0x8\n\t" \
420 "3:"type##_sb("$1", "1(%2)")"\n\t" \
421 "srl\t$1, $1, 0x8\n\t" \
422 "4:"type##_sb("$1", "0(%2)")"\n\t" \
427 ".section\t.fixup,\"ax\"\n\t" \
428 "11:\tli\t%0, %3\n\t" \
431 ".section\t__ex_table,\"a\"\n\t" \
432 STR(PTR)"\t1b, 11b\n\t" \
433 STR(PTR)"\t2b, 11b\n\t" \
434 STR(PTR)"\t3b, 11b\n\t" \
435 STR(PTR)"\t4b, 11b\n\t" \
438 : "r" (value), "r" (addr), "i" (-EFAULT) \
442 #define _StoreDW(addr, value, res) \
444 __asm__ __volatile__ ( \
447 "1:sb\t%1, 7(%2)\n\t" \
448 "dsrl\t$1, %1, 0x8\n\t" \
449 "2:sb\t$1, 6(%2)\n\t" \
450 "dsrl\t$1, $1, 0x8\n\t" \
451 "3:sb\t$1, 5(%2)\n\t" \
452 "dsrl\t$1, $1, 0x8\n\t" \
453 "4:sb\t$1, 4(%2)\n\t" \
454 "dsrl\t$1, $1, 0x8\n\t" \
455 "5:sb\t$1, 3(%2)\n\t" \
456 "dsrl\t$1, $1, 0x8\n\t" \
457 "6:sb\t$1, 2(%2)\n\t" \
458 "dsrl\t$1, $1, 0x8\n\t" \
459 "7:sb\t$1, 1(%2)\n\t" \
460 "dsrl\t$1, $1, 0x8\n\t" \
461 "8:sb\t$1, 0(%2)\n\t" \
462 "dsrl\t$1, $1, 0x8\n\t" \
467 ".section\t.fixup,\"ax\"\n\t" \
468 "11:\tli\t%0, %3\n\t" \
471 ".section\t__ex_table,\"a\"\n\t" \
472 STR(PTR)"\t1b, 11b\n\t" \
473 STR(PTR)"\t2b, 11b\n\t" \
474 STR(PTR)"\t3b, 11b\n\t" \
475 STR(PTR)"\t4b, 11b\n\t" \
476 STR(PTR)"\t5b, 11b\n\t" \
477 STR(PTR)"\t6b, 11b\n\t" \
478 STR(PTR)"\t7b, 11b\n\t" \
479 STR(PTR)"\t8b, 11b\n\t" \
482 : "r" (value), "r" (addr), "i" (-EFAULT) \
486 #endif /* CONFIG_CPU_MIPSR6 */
488 #else /* __BIG_ENDIAN */
490 #define _LoadHW(addr, value, res, type) \
492 __asm__ __volatile__ (".set\tnoat\n" \
493 "1:\t"type##_lb("%0", "1(%2)")"\n" \
494 "2:\t"type##_lbu("$1", "0(%2)")"\n\t"\
500 ".section\t.fixup,\"ax\"\n\t" \
501 "4:\tli\t%1, %3\n\t" \
504 ".section\t__ex_table,\"a\"\n\t" \
505 STR(PTR)"\t1b, 4b\n\t" \
506 STR(PTR)"\t2b, 4b\n\t" \
508 : "=&r" (value), "=r" (res) \
509 : "r" (addr), "i" (-EFAULT)); \
512 #ifndef CONFIG_CPU_MIPSR6
513 #define _LoadW(addr, value, res, type) \
515 __asm__ __volatile__ ( \
516 "1:\t"type##_lwl("%0", "3(%2)")"\n" \
517 "2:\t"type##_lwr("%0", "(%2)")"\n\t"\
521 ".section\t.fixup,\"ax\"\n\t" \
522 "4:\tli\t%1, %3\n\t" \
525 ".section\t__ex_table,\"a\"\n\t" \
526 STR(PTR)"\t1b, 4b\n\t" \
527 STR(PTR)"\t2b, 4b\n\t" \
529 : "=&r" (value), "=r" (res) \
530 : "r" (addr), "i" (-EFAULT)); \
534 /* MIPSR6 has no lwl instruction */
535 #define _LoadW(addr, value, res, type) \
537 __asm__ __volatile__ ( \
540 "1:"type##_lb("%0", "3(%2)")"\n\t" \
541 "2:"type##_lbu("$1", "2(%2)")"\n\t" \
544 "3:"type##_lbu("$1", "1(%2)")"\n\t" \
547 "4:"type##_lbu("$1", "0(%2)")"\n\t" \
554 ".section\t.fixup,\"ax\"\n\t" \
555 "11:\tli\t%1, %3\n\t" \
558 ".section\t__ex_table,\"a\"\n\t" \
559 STR(PTR)"\t1b, 11b\n\t" \
560 STR(PTR)"\t2b, 11b\n\t" \
561 STR(PTR)"\t3b, 11b\n\t" \
562 STR(PTR)"\t4b, 11b\n\t" \
564 : "=&r" (value), "=r" (res) \
565 : "r" (addr), "i" (-EFAULT)); \
568 #endif /* CONFIG_CPU_MIPSR6 */
571 #define _LoadHWU(addr, value, res, type) \
573 __asm__ __volatile__ ( \
575 "1:\t"type##_lbu("%0", "1(%2)")"\n" \
576 "2:\t"type##_lbu("$1", "0(%2)")"\n\t"\
583 ".section\t.fixup,\"ax\"\n\t" \
584 "4:\tli\t%1, %3\n\t" \
587 ".section\t__ex_table,\"a\"\n\t" \
588 STR(PTR)"\t1b, 4b\n\t" \
589 STR(PTR)"\t2b, 4b\n\t" \
591 : "=&r" (value), "=r" (res) \
592 : "r" (addr), "i" (-EFAULT)); \
595 #ifndef CONFIG_CPU_MIPSR6
596 #define _LoadWU(addr, value, res, type) \
598 __asm__ __volatile__ ( \
599 "1:\t"type##_lwl("%0", "3(%2)")"\n" \
600 "2:\t"type##_lwr("%0", "(%2)")"\n\t"\
601 "dsll\t%0, %0, 32\n\t" \
602 "dsrl\t%0, %0, 32\n\t" \
606 "\t.section\t.fixup,\"ax\"\n\t" \
607 "4:\tli\t%1, %3\n\t" \
610 ".section\t__ex_table,\"a\"\n\t" \
611 STR(PTR)"\t1b, 4b\n\t" \
612 STR(PTR)"\t2b, 4b\n\t" \
614 : "=&r" (value), "=r" (res) \
615 : "r" (addr), "i" (-EFAULT)); \
618 #define _LoadDW(addr, value, res) \
620 __asm__ __volatile__ ( \
621 "1:\tldl\t%0, 7(%2)\n" \
622 "2:\tldr\t%0, (%2)\n\t" \
626 "\t.section\t.fixup,\"ax\"\n\t" \
627 "4:\tli\t%1, %3\n\t" \
630 ".section\t__ex_table,\"a\"\n\t" \
631 STR(PTR)"\t1b, 4b\n\t" \
632 STR(PTR)"\t2b, 4b\n\t" \
634 : "=&r" (value), "=r" (res) \
635 : "r" (addr), "i" (-EFAULT)); \
639 /* MIPSR6 has not lwl and ldl instructions */
640 #define _LoadWU(addr, value, res, type) \
642 __asm__ __volatile__ ( \
645 "1:"type##_lbu("%0", "3(%2)")"\n\t" \
646 "2:"type##_lbu("$1", "2(%2)")"\n\t" \
649 "3:"type##_lbu("$1", "1(%2)")"\n\t" \
652 "4:"type##_lbu("$1", "0(%2)")"\n\t" \
659 ".section\t.fixup,\"ax\"\n\t" \
660 "11:\tli\t%1, %3\n\t" \
663 ".section\t__ex_table,\"a\"\n\t" \
664 STR(PTR)"\t1b, 11b\n\t" \
665 STR(PTR)"\t2b, 11b\n\t" \
666 STR(PTR)"\t3b, 11b\n\t" \
667 STR(PTR)"\t4b, 11b\n\t" \
669 : "=&r" (value), "=r" (res) \
670 : "r" (addr), "i" (-EFAULT)); \
673 #define _LoadDW(addr, value, res) \
675 __asm__ __volatile__ ( \
678 "1:lb\t%0, 7(%2)\n\t" \
679 "2:lbu\t$1, 6(%2)\n\t" \
680 "dsll\t%0, 0x8\n\t" \
682 "3:lbu\t$1, 5(%2)\n\t" \
683 "dsll\t%0, 0x8\n\t" \
685 "4:lbu\t$1, 4(%2)\n\t" \
686 "dsll\t%0, 0x8\n\t" \
688 "5:lbu\t$1, 3(%2)\n\t" \
689 "dsll\t%0, 0x8\n\t" \
691 "6:lbu\t$1, 2(%2)\n\t" \
692 "dsll\t%0, 0x8\n\t" \
694 "7:lbu\t$1, 1(%2)\n\t" \
695 "dsll\t%0, 0x8\n\t" \
697 "8:lbu\t$1, 0(%2)\n\t" \
698 "dsll\t%0, 0x8\n\t" \
704 ".section\t.fixup,\"ax\"\n\t" \
705 "11:\tli\t%1, %3\n\t" \
708 ".section\t__ex_table,\"a\"\n\t" \
709 STR(PTR)"\t1b, 11b\n\t" \
710 STR(PTR)"\t2b, 11b\n\t" \
711 STR(PTR)"\t3b, 11b\n\t" \
712 STR(PTR)"\t4b, 11b\n\t" \
713 STR(PTR)"\t5b, 11b\n\t" \
714 STR(PTR)"\t6b, 11b\n\t" \
715 STR(PTR)"\t7b, 11b\n\t" \
716 STR(PTR)"\t8b, 11b\n\t" \
718 : "=&r" (value), "=r" (res) \
719 : "r" (addr), "i" (-EFAULT)); \
721 #endif /* CONFIG_CPU_MIPSR6 */
723 #define _StoreHW(addr, value, res, type) \
725 __asm__ __volatile__ ( \
727 "1:\t"type##_sb("%1", "0(%2)")"\n" \
728 "srl\t$1,%1, 0x8\n" \
729 "2:\t"type##_sb("$1", "1(%2)")"\n" \
734 ".section\t.fixup,\"ax\"\n\t" \
735 "4:\tli\t%0, %3\n\t" \
738 ".section\t__ex_table,\"a\"\n\t" \
739 STR(PTR)"\t1b, 4b\n\t" \
740 STR(PTR)"\t2b, 4b\n\t" \
743 : "r" (value), "r" (addr), "i" (-EFAULT));\
746 #ifndef CONFIG_CPU_MIPSR6
747 #define _StoreW(addr, value, res, type) \
749 __asm__ __volatile__ ( \
750 "1:\t"type##_swl("%1", "3(%2)")"\n" \
751 "2:\t"type##_swr("%1", "(%2)")"\n\t"\
755 ".section\t.fixup,\"ax\"\n\t" \
756 "4:\tli\t%0, %3\n\t" \
759 ".section\t__ex_table,\"a\"\n\t" \
760 STR(PTR)"\t1b, 4b\n\t" \
761 STR(PTR)"\t2b, 4b\n\t" \
764 : "r" (value), "r" (addr), "i" (-EFAULT)); \
767 #define _StoreDW(addr, value, res) \
769 __asm__ __volatile__ ( \
770 "1:\tsdl\t%1, 7(%2)\n" \
771 "2:\tsdr\t%1, (%2)\n\t" \
775 ".section\t.fixup,\"ax\"\n\t" \
776 "4:\tli\t%0, %3\n\t" \
779 ".section\t__ex_table,\"a\"\n\t" \
780 STR(PTR)"\t1b, 4b\n\t" \
781 STR(PTR)"\t2b, 4b\n\t" \
784 : "r" (value), "r" (addr), "i" (-EFAULT)); \
788 /* MIPSR6 has no swl and sdl instructions */
789 #define _StoreW(addr, value, res, type) \
791 __asm__ __volatile__ ( \
794 "1:"type##_sb("%1", "0(%2)")"\n\t" \
795 "srl\t$1, %1, 0x8\n\t" \
796 "2:"type##_sb("$1", "1(%2)")"\n\t" \
797 "srl\t$1, $1, 0x8\n\t" \
798 "3:"type##_sb("$1", "2(%2)")"\n\t" \
799 "srl\t$1, $1, 0x8\n\t" \
800 "4:"type##_sb("$1", "3(%2)")"\n\t" \
805 ".section\t.fixup,\"ax\"\n\t" \
806 "11:\tli\t%0, %3\n\t" \
809 ".section\t__ex_table,\"a\"\n\t" \
810 STR(PTR)"\t1b, 11b\n\t" \
811 STR(PTR)"\t2b, 11b\n\t" \
812 STR(PTR)"\t3b, 11b\n\t" \
813 STR(PTR)"\t4b, 11b\n\t" \
816 : "r" (value), "r" (addr), "i" (-EFAULT) \
820 #define _StoreDW(addr, value, res) \
822 __asm__ __volatile__ ( \
825 "1:sb\t%1, 0(%2)\n\t" \
826 "dsrl\t$1, %1, 0x8\n\t" \
827 "2:sb\t$1, 1(%2)\n\t" \
828 "dsrl\t$1, $1, 0x8\n\t" \
829 "3:sb\t$1, 2(%2)\n\t" \
830 "dsrl\t$1, $1, 0x8\n\t" \
831 "4:sb\t$1, 3(%2)\n\t" \
832 "dsrl\t$1, $1, 0x8\n\t" \
833 "5:sb\t$1, 4(%2)\n\t" \
834 "dsrl\t$1, $1, 0x8\n\t" \
835 "6:sb\t$1, 5(%2)\n\t" \
836 "dsrl\t$1, $1, 0x8\n\t" \
837 "7:sb\t$1, 6(%2)\n\t" \
838 "dsrl\t$1, $1, 0x8\n\t" \
839 "8:sb\t$1, 7(%2)\n\t" \
840 "dsrl\t$1, $1, 0x8\n\t" \
845 ".section\t.fixup,\"ax\"\n\t" \
846 "11:\tli\t%0, %3\n\t" \
849 ".section\t__ex_table,\"a\"\n\t" \
850 STR(PTR)"\t1b, 11b\n\t" \
851 STR(PTR)"\t2b, 11b\n\t" \
852 STR(PTR)"\t3b, 11b\n\t" \
853 STR(PTR)"\t4b, 11b\n\t" \
854 STR(PTR)"\t5b, 11b\n\t" \
855 STR(PTR)"\t6b, 11b\n\t" \
856 STR(PTR)"\t7b, 11b\n\t" \
857 STR(PTR)"\t8b, 11b\n\t" \
860 : "r" (value), "r" (addr), "i" (-EFAULT) \
864 #endif /* CONFIG_CPU_MIPSR6 */
867 #define LoadHWU(addr, value, res) _LoadHWU(addr, value, res, kernel)
868 #define LoadHWUE(addr, value, res) _LoadHWU(addr, value, res, user)
869 #define LoadWU(addr, value, res) _LoadWU(addr, value, res, kernel)
870 #define LoadWUE(addr, value, res) _LoadWU(addr, value, res, user)
871 #define LoadHW(addr, value, res) _LoadHW(addr, value, res, kernel)
872 #define LoadHWE(addr, value, res) _LoadHW(addr, value, res, user)
873 #define LoadW(addr, value, res) _LoadW(addr, value, res, kernel)
874 #define LoadWE(addr, value, res) _LoadW(addr, value, res, user)
875 #define LoadDW(addr, value, res) _LoadDW(addr, value, res)
877 #define StoreHW(addr, value, res) _StoreHW(addr, value, res, kernel)
878 #define StoreHWE(addr, value, res) _StoreHW(addr, value, res, user)
879 #define StoreW(addr, value, res) _StoreW(addr, value, res, kernel)
880 #define StoreWE(addr, value, res) _StoreW(addr, value, res, user)
881 #define StoreDW(addr, value, res) _StoreDW(addr, value, res)
883 static void emulate_load_store_insn(struct pt_regs *regs,
884 void __user *addr, unsigned int __user *pc)
886 union mips_instruction insn;
889 unsigned long origpc;
890 unsigned long orig31;
891 void __user *fault_addr = NULL;
898 origpc = (unsigned long)pc;
899 orig31 = regs->regs[31];
901 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
904 * This load never faults.
906 __get_user(insn.word, pc);
908 switch (insn.i_format.opcode) {
910 * These are instructions that a compiler doesn't generate. We
911 * can assume therefore that the code is MIPS-aware and
912 * really buggy. Emulating these instructions would break the
921 * For these instructions the only way to create an address
922 * error is an attempted access to kernel/supervisor address
939 * The remaining opcodes are the ones that are really of
945 * we can land here only from kernel accessing user memory,
946 * so we need to "switch" the address limit to user space, so
947 * address check can work properly.
951 switch (insn.spec3_format.func) {
953 if (!access_ok(VERIFY_READ, addr, 2)) {
957 LoadHWE(addr, value, res);
962 compute_return_epc(regs);
963 regs->regs[insn.spec3_format.rt] = value;
966 if (!access_ok(VERIFY_READ, addr, 4)) {
970 LoadWE(addr, value, res);
975 compute_return_epc(regs);
976 regs->regs[insn.spec3_format.rt] = value;
979 if (!access_ok(VERIFY_READ, addr, 2)) {
983 LoadHWUE(addr, value, res);
988 compute_return_epc(regs);
989 regs->regs[insn.spec3_format.rt] = value;
992 if (!access_ok(VERIFY_WRITE, addr, 2)) {
996 compute_return_epc(regs);
997 value = regs->regs[insn.spec3_format.rt];
998 StoreHWE(addr, value, res);
1005 if (!access_ok(VERIFY_WRITE, addr, 4)) {
1009 compute_return_epc(regs);
1010 value = regs->regs[insn.spec3_format.rt];
1011 StoreWE(addr, value, res);
1025 if (!access_ok(VERIFY_READ, addr, 2))
1028 if (config_enabled(CONFIG_EVA)) {
1029 if (segment_eq(get_fs(), get_ds()))
1030 LoadHW(addr, value, res);
1032 LoadHWE(addr, value, res);
1034 LoadHW(addr, value, res);
1039 compute_return_epc(regs);
1040 regs->regs[insn.i_format.rt] = value;
1044 if (!access_ok(VERIFY_READ, addr, 4))
1047 if (config_enabled(CONFIG_EVA)) {
1048 if (segment_eq(get_fs(), get_ds()))
1049 LoadW(addr, value, res);
1051 LoadWE(addr, value, res);
1053 LoadW(addr, value, res);
1058 compute_return_epc(regs);
1059 regs->regs[insn.i_format.rt] = value;
1063 if (!access_ok(VERIFY_READ, addr, 2))
1066 if (config_enabled(CONFIG_EVA)) {
1067 if (segment_eq(get_fs(), get_ds()))
1068 LoadHWU(addr, value, res);
1070 LoadHWUE(addr, value, res);
1072 LoadHWU(addr, value, res);
1077 compute_return_epc(regs);
1078 regs->regs[insn.i_format.rt] = value;
1084 * A 32-bit kernel might be running on a 64-bit processor. But
1085 * if we're on a 32-bit processor and an i-cache incoherency
1086 * or race makes us see a 64-bit instruction here the sdl/sdr
1087 * would blow up, so for now we don't handle unaligned 64-bit
1088 * instructions on 32-bit kernels.
1090 if (!access_ok(VERIFY_READ, addr, 4))
1093 LoadWU(addr, value, res);
1096 compute_return_epc(regs);
1097 regs->regs[insn.i_format.rt] = value;
1099 #endif /* CONFIG_64BIT */
1101 /* Cannot handle 64-bit instructions in 32-bit kernel */
1107 * A 32-bit kernel might be running on a 64-bit processor. But
1108 * if we're on a 32-bit processor and an i-cache incoherency
1109 * or race makes us see a 64-bit instruction here the sdl/sdr
1110 * would blow up, so for now we don't handle unaligned 64-bit
1111 * instructions on 32-bit kernels.
1113 if (!access_ok(VERIFY_READ, addr, 8))
1116 LoadDW(addr, value, res);
1119 compute_return_epc(regs);
1120 regs->regs[insn.i_format.rt] = value;
1122 #endif /* CONFIG_64BIT */
1124 /* Cannot handle 64-bit instructions in 32-bit kernel */
1128 if (!access_ok(VERIFY_WRITE, addr, 2))
1131 compute_return_epc(regs);
1132 value = regs->regs[insn.i_format.rt];
1134 if (config_enabled(CONFIG_EVA)) {
1135 if (segment_eq(get_fs(), get_ds()))
1136 StoreHW(addr, value, res);
1138 StoreHWE(addr, value, res);
1140 StoreHW(addr, value, res);
1148 if (!access_ok(VERIFY_WRITE, addr, 4))
1151 compute_return_epc(regs);
1152 value = regs->regs[insn.i_format.rt];
1154 if (config_enabled(CONFIG_EVA)) {
1155 if (segment_eq(get_fs(), get_ds()))
1156 StoreW(addr, value, res);
1158 StoreWE(addr, value, res);
1160 StoreW(addr, value, res);
1170 * A 32-bit kernel might be running on a 64-bit processor. But
1171 * if we're on a 32-bit processor and an i-cache incoherency
1172 * or race makes us see a 64-bit instruction here the sdl/sdr
1173 * would blow up, so for now we don't handle unaligned 64-bit
1174 * instructions on 32-bit kernels.
1176 if (!access_ok(VERIFY_WRITE, addr, 8))
1179 compute_return_epc(regs);
1180 value = regs->regs[insn.i_format.rt];
1181 StoreDW(addr, value, res);
1185 #endif /* CONFIG_64BIT */
1187 /* Cannot handle 64-bit instructions in 32-bit kernel */
1194 die_if_kernel("Unaligned FP access in kernel code", regs);
1195 BUG_ON(!used_math());
1197 lose_fpu(1); /* Save FPU state for the emulator. */
1198 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1200 own_fpu(1); /* Restore FPU state. */
1202 /* Signal if something went wrong. */
1203 process_fpemu_return(res, fault_addr, 0);
1214 * If we've reached this point then userland should have taken
1215 * the MSA disabled exception & initialised vector context at
1216 * some point in the past.
1218 BUG_ON(!thread_msa_context_live());
1220 df = insn.msa_mi10_format.df;
1221 wd = insn.msa_mi10_format.wd;
1222 fpr = ¤t->thread.fpu.fpr[wd];
1224 switch (insn.msa_mi10_format.func) {
1226 if (!access_ok(VERIFY_READ, addr, sizeof(*fpr)))
1230 * Disable preemption to avoid a race between copying
1231 * state from userland, migrating to another CPU and
1232 * updating the hardware vector register below.
1236 res = __copy_from_user_inatomic(fpr, addr,
1242 * Update the hardware register if it is in use by the
1243 * task in this quantum, in order to avoid having to
1244 * save & restore the whole vector context.
1246 if (test_thread_flag(TIF_USEDMSA))
1247 write_msa_wr(wd, fpr, df);
1253 if (!access_ok(VERIFY_WRITE, addr, sizeof(*fpr)))
1257 * Update from the hardware register if it is in use by
1258 * the task in this quantum, in order to avoid having to
1259 * save & restore the whole vector context.
1262 if (test_thread_flag(TIF_USEDMSA))
1263 read_msa_wr(wd, fpr, df);
1266 res = __copy_to_user_inatomic(addr, fpr, sizeof(*fpr));
1275 compute_return_epc(regs);
1278 #ifndef CONFIG_CPU_MIPSR6
1280 * COP2 is available to implementor for application specific use.
1281 * It's up to applications to register a notifier chain and do
1282 * whatever they have to do, including possible sending of signals.
1284 * This instruction has been reallocated in Release 6
1287 cu2_notifier_call_chain(CU2_LWC2_OP, regs);
1291 cu2_notifier_call_chain(CU2_LDC2_OP, regs);
1295 cu2_notifier_call_chain(CU2_SWC2_OP, regs);
1299 cu2_notifier_call_chain(CU2_SDC2_OP, regs);
1304 * Pheeee... We encountered an yet unknown instruction or
1305 * cache coherence problem. Die sucker, die ...
1310 #ifdef CONFIG_DEBUG_FS
1311 unaligned_instructions++;
1317 /* roll back jump/branch */
1318 regs->cp0_epc = origpc;
1319 regs->regs[31] = orig31;
1320 /* Did we have an exception handler installed? */
1321 if (fixup_exception(regs))
1324 die_if_kernel("Unhandled kernel unaligned access", regs);
1325 force_sig(SIGSEGV, current);
1330 die_if_kernel("Unhandled kernel unaligned access", regs);
1331 force_sig(SIGBUS, current);
1337 ("Unhandled kernel unaligned access or invalid instruction", regs);
1338 force_sig(SIGILL, current);
1341 /* Recode table from 16-bit register notation to 32-bit GPR. */
1342 const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
1344 /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
1345 const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
1347 static void emulate_load_store_microMIPS(struct pt_regs *regs,
1350 unsigned long value;
1353 unsigned int reg = 0, rvar;
1354 unsigned long orig31;
1358 unsigned long origpc, contpc;
1359 union mips_instruction insn;
1360 struct mm_decoded_insn mminsn;
1361 void __user *fault_addr = NULL;
1363 origpc = regs->cp0_epc;
1364 orig31 = regs->regs[31];
1366 mminsn.micro_mips_mode = 1;
1369 * This load never faults.
1371 pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
1372 __get_user(halfword, pc16);
1374 contpc = regs->cp0_epc + 2;
1375 word = ((unsigned int)halfword << 16);
1378 if (!mm_insn_16bit(halfword)) {
1379 __get_user(halfword, pc16);
1381 contpc = regs->cp0_epc + 4;
1387 if (get_user(halfword, pc16))
1389 mminsn.next_pc_inc = 2;
1390 word = ((unsigned int)halfword << 16);
1392 if (!mm_insn_16bit(halfword)) {
1394 if (get_user(halfword, pc16))
1396 mminsn.next_pc_inc = 4;
1399 mminsn.next_insn = word;
1401 insn = (union mips_instruction)(mminsn.insn);
1402 if (mm_isBranchInstr(regs, mminsn, &contpc))
1403 insn = (union mips_instruction)(mminsn.next_insn);
1405 /* Parse instruction to find what to do */
1407 switch (insn.mm_i_format.opcode) {
1410 switch (insn.mm_x_format.func) {
1412 reg = insn.mm_x_format.rd;
1419 switch (insn.mm_m_format.func) {
1421 reg = insn.mm_m_format.rd;
1425 if (!access_ok(VERIFY_READ, addr, 8))
1428 LoadW(addr, value, res);
1431 regs->regs[reg] = value;
1433 LoadW(addr, value, res);
1436 regs->regs[reg + 1] = value;
1440 reg = insn.mm_m_format.rd;
1444 if (!access_ok(VERIFY_WRITE, addr, 8))
1447 value = regs->regs[reg];
1448 StoreW(addr, value, res);
1452 value = regs->regs[reg + 1];
1453 StoreW(addr, value, res);
1460 reg = insn.mm_m_format.rd;
1464 if (!access_ok(VERIFY_READ, addr, 16))
1467 LoadDW(addr, value, res);
1470 regs->regs[reg] = value;
1472 LoadDW(addr, value, res);
1475 regs->regs[reg + 1] = value;
1477 #endif /* CONFIG_64BIT */
1483 reg = insn.mm_m_format.rd;
1487 if (!access_ok(VERIFY_WRITE, addr, 16))
1490 value = regs->regs[reg];
1491 StoreDW(addr, value, res);
1495 value = regs->regs[reg + 1];
1496 StoreDW(addr, value, res);
1500 #endif /* CONFIG_64BIT */
1505 reg = insn.mm_m_format.rd;
1507 if ((rvar > 9) || !reg)
1511 (VERIFY_READ, addr, 4 * (rvar + 1)))
1514 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1519 for (i = 16; rvar; rvar--, i++) {
1520 LoadW(addr, value, res);
1524 regs->regs[i] = value;
1526 if ((reg & 0xf) == 9) {
1527 LoadW(addr, value, res);
1531 regs->regs[30] = value;
1534 LoadW(addr, value, res);
1537 regs->regs[31] = value;
1542 reg = insn.mm_m_format.rd;
1544 if ((rvar > 9) || !reg)
1548 (VERIFY_WRITE, addr, 4 * (rvar + 1)))
1551 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1556 for (i = 16; rvar; rvar--, i++) {
1557 value = regs->regs[i];
1558 StoreW(addr, value, res);
1563 if ((reg & 0xf) == 9) {
1564 value = regs->regs[30];
1565 StoreW(addr, value, res);
1571 value = regs->regs[31];
1572 StoreW(addr, value, res);
1580 reg = insn.mm_m_format.rd;
1582 if ((rvar > 9) || !reg)
1586 (VERIFY_READ, addr, 8 * (rvar + 1)))
1589 if (!access_ok(VERIFY_READ, addr, 8 * rvar))
1595 for (i = 16; rvar; rvar--, i++) {
1596 LoadDW(addr, value, res);
1600 regs->regs[i] = value;
1602 if ((reg & 0xf) == 9) {
1603 LoadDW(addr, value, res);
1607 regs->regs[30] = value;
1610 LoadDW(addr, value, res);
1613 regs->regs[31] = value;
1616 #endif /* CONFIG_64BIT */
1622 reg = insn.mm_m_format.rd;
1624 if ((rvar > 9) || !reg)
1628 (VERIFY_WRITE, addr, 8 * (rvar + 1)))
1631 if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
1637 for (i = 16; rvar; rvar--, i++) {
1638 value = regs->regs[i];
1639 StoreDW(addr, value, res);
1644 if ((reg & 0xf) == 9) {
1645 value = regs->regs[30];
1646 StoreDW(addr, value, res);
1652 value = regs->regs[31];
1653 StoreDW(addr, value, res);
1658 #endif /* CONFIG_64BIT */
1662 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1668 switch (insn.mm_m_format.func) {
1670 reg = insn.mm_m_format.rd;
1674 /* LL,SC,LLD,SCD are not serviced */
1678 switch (insn.mm_x_format.func) {
1693 /* roll back jump/branch */
1694 regs->cp0_epc = origpc;
1695 regs->regs[31] = orig31;
1697 die_if_kernel("Unaligned FP access in kernel code", regs);
1698 BUG_ON(!used_math());
1699 BUG_ON(!is_fpu_owner());
1701 lose_fpu(1); /* save the FPU state for the emulator */
1702 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1704 own_fpu(1); /* restore FPU state */
1706 /* If something went wrong, signal */
1707 process_fpemu_return(res, fault_addr, 0);
1714 reg = insn.mm_i_format.rt;
1718 reg = insn.mm_i_format.rt;
1722 reg = insn.mm_i_format.rt;
1726 reg = insn.mm_i_format.rt;
1730 reg = insn.mm_i_format.rt;
1734 reg = insn.mm_i_format.rt;
1738 reg = insn.mm_i_format.rt;
1742 switch (insn.mm16_m_format.func) {
1744 reg = insn.mm16_m_format.rlist;
1746 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1749 for (i = 16; rvar; rvar--, i++) {
1750 LoadW(addr, value, res);
1754 regs->regs[i] = value;
1756 LoadW(addr, value, res);
1759 regs->regs[31] = value;
1764 reg = insn.mm16_m_format.rlist;
1766 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1769 for (i = 16; rvar; rvar--, i++) {
1770 value = regs->regs[i];
1771 StoreW(addr, value, res);
1776 value = regs->regs[31];
1777 StoreW(addr, value, res);
1788 reg = reg16to32[insn.mm16_rb_format.rt];
1792 reg = reg16to32[insn.mm16_rb_format.rt];
1796 reg = reg16to32st[insn.mm16_rb_format.rt];
1800 reg = reg16to32st[insn.mm16_rb_format.rt];
1804 reg = insn.mm16_r5_format.rt;
1808 reg = insn.mm16_r5_format.rt;
1812 reg = reg16to32[insn.mm16_r3_format.rt];
1820 if (!access_ok(VERIFY_READ, addr, 2))
1823 LoadHW(addr, value, res);
1826 regs->regs[reg] = value;
1830 if (!access_ok(VERIFY_READ, addr, 2))
1833 LoadHWU(addr, value, res);
1836 regs->regs[reg] = value;
1840 if (!access_ok(VERIFY_READ, addr, 4))
1843 LoadW(addr, value, res);
1846 regs->regs[reg] = value;
1852 * A 32-bit kernel might be running on a 64-bit processor. But
1853 * if we're on a 32-bit processor and an i-cache incoherency
1854 * or race makes us see a 64-bit instruction here the sdl/sdr
1855 * would blow up, so for now we don't handle unaligned 64-bit
1856 * instructions on 32-bit kernels.
1858 if (!access_ok(VERIFY_READ, addr, 4))
1861 LoadWU(addr, value, res);
1864 regs->regs[reg] = value;
1866 #endif /* CONFIG_64BIT */
1868 /* Cannot handle 64-bit instructions in 32-bit kernel */
1874 * A 32-bit kernel might be running on a 64-bit processor. But
1875 * if we're on a 32-bit processor and an i-cache incoherency
1876 * or race makes us see a 64-bit instruction here the sdl/sdr
1877 * would blow up, so for now we don't handle unaligned 64-bit
1878 * instructions on 32-bit kernels.
1880 if (!access_ok(VERIFY_READ, addr, 8))
1883 LoadDW(addr, value, res);
1886 regs->regs[reg] = value;
1888 #endif /* CONFIG_64BIT */
1890 /* Cannot handle 64-bit instructions in 32-bit kernel */
1894 if (!access_ok(VERIFY_WRITE, addr, 2))
1897 value = regs->regs[reg];
1898 StoreHW(addr, value, res);
1904 if (!access_ok(VERIFY_WRITE, addr, 4))
1907 value = regs->regs[reg];
1908 StoreW(addr, value, res);
1916 * A 32-bit kernel might be running on a 64-bit processor. But
1917 * if we're on a 32-bit processor and an i-cache incoherency
1918 * or race makes us see a 64-bit instruction here the sdl/sdr
1919 * would blow up, so for now we don't handle unaligned 64-bit
1920 * instructions on 32-bit kernels.
1922 if (!access_ok(VERIFY_WRITE, addr, 8))
1925 value = regs->regs[reg];
1926 StoreDW(addr, value, res);
1930 #endif /* CONFIG_64BIT */
1932 /* Cannot handle 64-bit instructions in 32-bit kernel */
1936 regs->cp0_epc = contpc; /* advance or branch */
1938 #ifdef CONFIG_DEBUG_FS
1939 unaligned_instructions++;
1944 /* roll back jump/branch */
1945 regs->cp0_epc = origpc;
1946 regs->regs[31] = orig31;
1947 /* Did we have an exception handler installed? */
1948 if (fixup_exception(regs))
1951 die_if_kernel("Unhandled kernel unaligned access", regs);
1952 force_sig(SIGSEGV, current);
1957 die_if_kernel("Unhandled kernel unaligned access", regs);
1958 force_sig(SIGBUS, current);
1964 ("Unhandled kernel unaligned access or invalid instruction", regs);
1965 force_sig(SIGILL, current);
1968 static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
1970 unsigned long value;
1973 unsigned long orig31;
1975 unsigned long origpc;
1976 union mips16e_instruction mips16inst, oldinst;
1978 origpc = regs->cp0_epc;
1979 orig31 = regs->regs[31];
1980 pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
1982 * This load never faults.
1984 __get_user(mips16inst.full, pc16);
1985 oldinst = mips16inst;
1987 /* skip EXTEND instruction */
1988 if (mips16inst.ri.opcode == MIPS16e_extend_op) {
1990 __get_user(mips16inst.full, pc16);
1991 } else if (delay_slot(regs)) {
1992 /* skip jump instructions */
1993 /* JAL/JALX are 32 bits but have OPCODE in first short int */
1994 if (mips16inst.ri.opcode == MIPS16e_jal_op)
1997 if (get_user(mips16inst.full, pc16))
2001 switch (mips16inst.ri.opcode) {
2002 case MIPS16e_i64_op: /* I64 or RI64 instruction */
2003 switch (mips16inst.i64.func) { /* I64/RI64 func field check */
2004 case MIPS16e_ldpc_func:
2005 case MIPS16e_ldsp_func:
2006 reg = reg16to32[mips16inst.ri64.ry];
2009 case MIPS16e_sdsp_func:
2010 reg = reg16to32[mips16inst.ri64.ry];
2013 case MIPS16e_sdrasp_func:
2014 reg = 29; /* GPRSP */
2020 case MIPS16e_swsp_op:
2021 case MIPS16e_lwpc_op:
2022 case MIPS16e_lwsp_op:
2023 reg = reg16to32[mips16inst.ri.rx];
2027 if (mips16inst.i8.func != MIPS16e_swrasp_func)
2029 reg = 29; /* GPRSP */
2033 reg = reg16to32[mips16inst.rri.ry];
2037 switch (mips16inst.ri.opcode) {
2040 case MIPS16e_lbu_op:
2045 if (!access_ok(VERIFY_READ, addr, 2))
2048 LoadHW(addr, value, res);
2051 MIPS16e_compute_return_epc(regs, &oldinst);
2052 regs->regs[reg] = value;
2055 case MIPS16e_lhu_op:
2056 if (!access_ok(VERIFY_READ, addr, 2))
2059 LoadHWU(addr, value, res);
2062 MIPS16e_compute_return_epc(regs, &oldinst);
2063 regs->regs[reg] = value;
2067 case MIPS16e_lwpc_op:
2068 case MIPS16e_lwsp_op:
2069 if (!access_ok(VERIFY_READ, addr, 4))
2072 LoadW(addr, value, res);
2075 MIPS16e_compute_return_epc(regs, &oldinst);
2076 regs->regs[reg] = value;
2079 case MIPS16e_lwu_op:
2082 * A 32-bit kernel might be running on a 64-bit processor. But
2083 * if we're on a 32-bit processor and an i-cache incoherency
2084 * or race makes us see a 64-bit instruction here the sdl/sdr
2085 * would blow up, so for now we don't handle unaligned 64-bit
2086 * instructions on 32-bit kernels.
2088 if (!access_ok(VERIFY_READ, addr, 4))
2091 LoadWU(addr, value, res);
2094 MIPS16e_compute_return_epc(regs, &oldinst);
2095 regs->regs[reg] = value;
2097 #endif /* CONFIG_64BIT */
2099 /* Cannot handle 64-bit instructions in 32-bit kernel */
2106 * A 32-bit kernel might be running on a 64-bit processor. But
2107 * if we're on a 32-bit processor and an i-cache incoherency
2108 * or race makes us see a 64-bit instruction here the sdl/sdr
2109 * would blow up, so for now we don't handle unaligned 64-bit
2110 * instructions on 32-bit kernels.
2112 if (!access_ok(VERIFY_READ, addr, 8))
2115 LoadDW(addr, value, res);
2118 MIPS16e_compute_return_epc(regs, &oldinst);
2119 regs->regs[reg] = value;
2121 #endif /* CONFIG_64BIT */
2123 /* Cannot handle 64-bit instructions in 32-bit kernel */
2127 if (!access_ok(VERIFY_WRITE, addr, 2))
2130 MIPS16e_compute_return_epc(regs, &oldinst);
2131 value = regs->regs[reg];
2132 StoreHW(addr, value, res);
2138 case MIPS16e_swsp_op:
2139 case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
2140 if (!access_ok(VERIFY_WRITE, addr, 4))
2143 MIPS16e_compute_return_epc(regs, &oldinst);
2144 value = regs->regs[reg];
2145 StoreW(addr, value, res);
2154 * A 32-bit kernel might be running on a 64-bit processor. But
2155 * if we're on a 32-bit processor and an i-cache incoherency
2156 * or race makes us see a 64-bit instruction here the sdl/sdr
2157 * would blow up, so for now we don't handle unaligned 64-bit
2158 * instructions on 32-bit kernels.
2160 if (!access_ok(VERIFY_WRITE, addr, 8))
2163 MIPS16e_compute_return_epc(regs, &oldinst);
2164 value = regs->regs[reg];
2165 StoreDW(addr, value, res);
2169 #endif /* CONFIG_64BIT */
2171 /* Cannot handle 64-bit instructions in 32-bit kernel */
2176 * Pheeee... We encountered an yet unknown instruction or
2177 * cache coherence problem. Die sucker, die ...
2182 #ifdef CONFIG_DEBUG_FS
2183 unaligned_instructions++;
2189 /* roll back jump/branch */
2190 regs->cp0_epc = origpc;
2191 regs->regs[31] = orig31;
2192 /* Did we have an exception handler installed? */
2193 if (fixup_exception(regs))
2196 die_if_kernel("Unhandled kernel unaligned access", regs);
2197 force_sig(SIGSEGV, current);
2202 die_if_kernel("Unhandled kernel unaligned access", regs);
2203 force_sig(SIGBUS, current);
2209 ("Unhandled kernel unaligned access or invalid instruction", regs);
2210 force_sig(SIGILL, current);
2213 asmlinkage void do_ade(struct pt_regs *regs)
2215 enum ctx_state prev_state;
2216 unsigned int __user *pc;
2219 prev_state = exception_enter();
2220 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
2221 1, regs, regs->cp0_badvaddr);
2223 * Did we catch a fault trying to load an instruction?
2225 if (regs->cp0_badvaddr == regs->cp0_epc)
2228 if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
2230 if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
2234 * Do branch emulation only if we didn't forward the exception.
2235 * This is all so but ugly ...
2239 * Are we running in microMIPS mode?
2241 if (get_isa16_mode(regs->cp0_epc)) {
2243 * Did we catch a fault trying to load an instruction in
2246 if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
2248 if (unaligned_action == UNALIGNED_ACTION_SHOW)
2249 show_registers(regs);
2251 if (cpu_has_mmips) {
2253 if (!user_mode(regs))
2255 emulate_load_store_microMIPS(regs,
2256 (void __user *)regs->cp0_badvaddr);
2262 if (cpu_has_mips16) {
2264 if (!user_mode(regs))
2266 emulate_load_store_MIPS16e(regs,
2267 (void __user *)regs->cp0_badvaddr);
2276 if (unaligned_action == UNALIGNED_ACTION_SHOW)
2277 show_registers(regs);
2278 pc = (unsigned int __user *)exception_epc(regs);
2281 if (!user_mode(regs))
2283 emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
2289 die_if_kernel("Kernel unaligned instruction access", regs);
2290 force_sig(SIGBUS, current);
2293 * XXX On return from the signal handler we should advance the epc
2295 exception_exit(prev_state);
2298 #ifdef CONFIG_DEBUG_FS
2299 static int __init debugfs_unaligned(void)
2303 if (!mips_debugfs_dir)
2305 d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
2306 mips_debugfs_dir, &unaligned_instructions);
2309 d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
2310 mips_debugfs_dir, &unaligned_action);
2315 arch_initcall(debugfs_unaligned);