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 <linux/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;
888 unsigned int res, preempted;
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
943 if (insn.dsp_format.func == lx_op) {
944 switch (insn.dsp_format.op) {
946 if (!access_ok(VERIFY_READ, addr, 4))
948 LoadW(addr, value, res);
951 compute_return_epc(regs);
952 regs->regs[insn.dsp_format.rd] = value;
955 if (!access_ok(VERIFY_READ, addr, 2))
957 LoadHW(addr, value, res);
960 compute_return_epc(regs);
961 regs->regs[insn.dsp_format.rd] = value;
970 * we can land here only from kernel accessing user
971 * memory, so we need to "switch" the address limit to
972 * user space, so that address check can work properly.
976 switch (insn.spec3_format.func) {
978 if (!access_ok(VERIFY_READ, addr, 2)) {
982 LoadHWE(addr, value, res);
987 compute_return_epc(regs);
988 regs->regs[insn.spec3_format.rt] = value;
991 if (!access_ok(VERIFY_READ, addr, 4)) {
995 LoadWE(addr, value, res);
1000 compute_return_epc(regs);
1001 regs->regs[insn.spec3_format.rt] = value;
1004 if (!access_ok(VERIFY_READ, addr, 2)) {
1008 LoadHWUE(addr, value, res);
1013 compute_return_epc(regs);
1014 regs->regs[insn.spec3_format.rt] = value;
1017 if (!access_ok(VERIFY_WRITE, addr, 2)) {
1021 compute_return_epc(regs);
1022 value = regs->regs[insn.spec3_format.rt];
1023 StoreHWE(addr, value, res);
1030 if (!access_ok(VERIFY_WRITE, addr, 4)) {
1034 compute_return_epc(regs);
1035 value = regs->regs[insn.spec3_format.rt];
1036 StoreWE(addr, value, res);
1051 if (!access_ok(VERIFY_READ, addr, 2))
1054 if (IS_ENABLED(CONFIG_EVA)) {
1055 if (uaccess_kernel())
1056 LoadHW(addr, value, res);
1058 LoadHWE(addr, value, res);
1060 LoadHW(addr, value, res);
1065 compute_return_epc(regs);
1066 regs->regs[insn.i_format.rt] = value;
1070 if (!access_ok(VERIFY_READ, addr, 4))
1073 if (IS_ENABLED(CONFIG_EVA)) {
1074 if (uaccess_kernel())
1075 LoadW(addr, value, res);
1077 LoadWE(addr, value, res);
1079 LoadW(addr, value, res);
1084 compute_return_epc(regs);
1085 regs->regs[insn.i_format.rt] = value;
1089 if (!access_ok(VERIFY_READ, addr, 2))
1092 if (IS_ENABLED(CONFIG_EVA)) {
1093 if (uaccess_kernel())
1094 LoadHWU(addr, value, res);
1096 LoadHWUE(addr, value, res);
1098 LoadHWU(addr, value, res);
1103 compute_return_epc(regs);
1104 regs->regs[insn.i_format.rt] = value;
1110 * A 32-bit kernel might be running on a 64-bit processor. But
1111 * if we're on a 32-bit processor and an i-cache incoherency
1112 * or race makes us see a 64-bit instruction here the sdl/sdr
1113 * would blow up, so for now we don't handle unaligned 64-bit
1114 * instructions on 32-bit kernels.
1116 if (!access_ok(VERIFY_READ, addr, 4))
1119 LoadWU(addr, value, res);
1122 compute_return_epc(regs);
1123 regs->regs[insn.i_format.rt] = value;
1125 #endif /* CONFIG_64BIT */
1127 /* Cannot handle 64-bit instructions in 32-bit kernel */
1133 * A 32-bit kernel might be running on a 64-bit processor. But
1134 * if we're on a 32-bit processor and an i-cache incoherency
1135 * or race makes us see a 64-bit instruction here the sdl/sdr
1136 * would blow up, so for now we don't handle unaligned 64-bit
1137 * instructions on 32-bit kernels.
1139 if (!access_ok(VERIFY_READ, addr, 8))
1142 LoadDW(addr, value, res);
1145 compute_return_epc(regs);
1146 regs->regs[insn.i_format.rt] = value;
1148 #endif /* CONFIG_64BIT */
1150 /* Cannot handle 64-bit instructions in 32-bit kernel */
1154 if (!access_ok(VERIFY_WRITE, addr, 2))
1157 compute_return_epc(regs);
1158 value = regs->regs[insn.i_format.rt];
1160 if (IS_ENABLED(CONFIG_EVA)) {
1161 if (uaccess_kernel())
1162 StoreHW(addr, value, res);
1164 StoreHWE(addr, value, res);
1166 StoreHW(addr, value, res);
1174 if (!access_ok(VERIFY_WRITE, addr, 4))
1177 compute_return_epc(regs);
1178 value = regs->regs[insn.i_format.rt];
1180 if (IS_ENABLED(CONFIG_EVA)) {
1181 if (uaccess_kernel())
1182 StoreW(addr, value, res);
1184 StoreWE(addr, value, res);
1186 StoreW(addr, value, res);
1196 * A 32-bit kernel might be running on a 64-bit processor. But
1197 * if we're on a 32-bit processor and an i-cache incoherency
1198 * or race makes us see a 64-bit instruction here the sdl/sdr
1199 * would blow up, so for now we don't handle unaligned 64-bit
1200 * instructions on 32-bit kernels.
1202 if (!access_ok(VERIFY_WRITE, addr, 8))
1205 compute_return_epc(regs);
1206 value = regs->regs[insn.i_format.rt];
1207 StoreDW(addr, value, res);
1211 #endif /* CONFIG_64BIT */
1213 /* Cannot handle 64-bit instructions in 32-bit kernel */
1221 die_if_kernel("Unaligned FP access in kernel code", regs);
1222 BUG_ON(!used_math());
1224 lose_fpu(1); /* Save FPU state for the emulator. */
1225 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1227 own_fpu(1); /* Restore FPU state. */
1229 /* Signal if something went wrong. */
1230 process_fpemu_return(res, fault_addr, 0);
1241 * If we've reached this point then userland should have taken
1242 * the MSA disabled exception & initialised vector context at
1243 * some point in the past.
1245 BUG_ON(!thread_msa_context_live());
1247 df = insn.msa_mi10_format.df;
1248 wd = insn.msa_mi10_format.wd;
1249 fpr = ¤t->thread.fpu.fpr[wd];
1251 switch (insn.msa_mi10_format.func) {
1253 if (!access_ok(VERIFY_READ, addr, sizeof(*fpr)))
1258 * If we have live MSA context keep track of
1259 * whether we get preempted in order to avoid
1260 * the register context we load being clobbered
1261 * by the live context as it's saved during
1262 * preemption. If we don't have live context
1263 * then it can't be saved to clobber the value
1266 preempted = test_thread_flag(TIF_USEDMSA);
1268 res = __copy_from_user_inatomic(fpr, addr,
1274 * Update the hardware register if it is in use
1275 * by the task in this quantum, in order to
1276 * avoid having to save & restore the whole
1280 if (test_thread_flag(TIF_USEDMSA)) {
1281 write_msa_wr(wd, fpr, df);
1285 } while (preempted);
1289 if (!access_ok(VERIFY_WRITE, addr, sizeof(*fpr)))
1293 * Update from the hardware register if it is in use by
1294 * the task in this quantum, in order to avoid having to
1295 * save & restore the whole vector context.
1298 if (test_thread_flag(TIF_USEDMSA))
1299 read_msa_wr(wd, fpr, df);
1302 res = __copy_to_user_inatomic(addr, fpr, sizeof(*fpr));
1311 compute_return_epc(regs);
1314 #ifndef CONFIG_CPU_MIPSR6
1316 * COP2 is available to implementor for application specific use.
1317 * It's up to applications to register a notifier chain and do
1318 * whatever they have to do, including possible sending of signals.
1320 * This instruction has been reallocated in Release 6
1323 cu2_notifier_call_chain(CU2_LWC2_OP, regs);
1327 cu2_notifier_call_chain(CU2_LDC2_OP, regs);
1331 cu2_notifier_call_chain(CU2_SWC2_OP, regs);
1335 cu2_notifier_call_chain(CU2_SDC2_OP, regs);
1340 * Pheeee... We encountered an yet unknown instruction or
1341 * cache coherence problem. Die sucker, die ...
1346 #ifdef CONFIG_DEBUG_FS
1347 unaligned_instructions++;
1353 /* roll back jump/branch */
1354 regs->cp0_epc = origpc;
1355 regs->regs[31] = orig31;
1356 /* Did we have an exception handler installed? */
1357 if (fixup_exception(regs))
1360 die_if_kernel("Unhandled kernel unaligned access", regs);
1361 force_sig(SIGSEGV, current);
1366 die_if_kernel("Unhandled kernel unaligned access", regs);
1367 force_sig(SIGBUS, current);
1373 ("Unhandled kernel unaligned access or invalid instruction", regs);
1374 force_sig(SIGILL, current);
1377 /* Recode table from 16-bit register notation to 32-bit GPR. */
1378 const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
1380 /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
1381 static const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
1383 static void emulate_load_store_microMIPS(struct pt_regs *regs,
1386 unsigned long value;
1389 unsigned int reg = 0, rvar;
1390 unsigned long orig31;
1394 unsigned long origpc, contpc;
1395 union mips_instruction insn;
1396 struct mm_decoded_insn mminsn;
1397 void __user *fault_addr = NULL;
1399 origpc = regs->cp0_epc;
1400 orig31 = regs->regs[31];
1402 mminsn.micro_mips_mode = 1;
1405 * This load never faults.
1407 pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
1408 __get_user(halfword, pc16);
1410 contpc = regs->cp0_epc + 2;
1411 word = ((unsigned int)halfword << 16);
1414 if (!mm_insn_16bit(halfword)) {
1415 __get_user(halfword, pc16);
1417 contpc = regs->cp0_epc + 4;
1423 if (get_user(halfword, pc16))
1425 mminsn.next_pc_inc = 2;
1426 word = ((unsigned int)halfword << 16);
1428 if (!mm_insn_16bit(halfword)) {
1430 if (get_user(halfword, pc16))
1432 mminsn.next_pc_inc = 4;
1435 mminsn.next_insn = word;
1437 insn = (union mips_instruction)(mminsn.insn);
1438 if (mm_isBranchInstr(regs, mminsn, &contpc))
1439 insn = (union mips_instruction)(mminsn.next_insn);
1441 /* Parse instruction to find what to do */
1443 switch (insn.mm_i_format.opcode) {
1446 switch (insn.mm_x_format.func) {
1448 reg = insn.mm_x_format.rd;
1455 switch (insn.mm_m_format.func) {
1457 reg = insn.mm_m_format.rd;
1461 if (!access_ok(VERIFY_READ, addr, 8))
1464 LoadW(addr, value, res);
1467 regs->regs[reg] = value;
1469 LoadW(addr, value, res);
1472 regs->regs[reg + 1] = value;
1476 reg = insn.mm_m_format.rd;
1480 if (!access_ok(VERIFY_WRITE, addr, 8))
1483 value = regs->regs[reg];
1484 StoreW(addr, value, res);
1488 value = regs->regs[reg + 1];
1489 StoreW(addr, value, res);
1496 reg = insn.mm_m_format.rd;
1500 if (!access_ok(VERIFY_READ, addr, 16))
1503 LoadDW(addr, value, res);
1506 regs->regs[reg] = value;
1508 LoadDW(addr, value, res);
1511 regs->regs[reg + 1] = value;
1513 #endif /* CONFIG_64BIT */
1519 reg = insn.mm_m_format.rd;
1523 if (!access_ok(VERIFY_WRITE, addr, 16))
1526 value = regs->regs[reg];
1527 StoreDW(addr, value, res);
1531 value = regs->regs[reg + 1];
1532 StoreDW(addr, value, res);
1536 #endif /* CONFIG_64BIT */
1541 reg = insn.mm_m_format.rd;
1543 if ((rvar > 9) || !reg)
1547 (VERIFY_READ, addr, 4 * (rvar + 1)))
1550 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1555 for (i = 16; rvar; rvar--, i++) {
1556 LoadW(addr, value, res);
1560 regs->regs[i] = value;
1562 if ((reg & 0xf) == 9) {
1563 LoadW(addr, value, res);
1567 regs->regs[30] = value;
1570 LoadW(addr, value, res);
1573 regs->regs[31] = value;
1578 reg = insn.mm_m_format.rd;
1580 if ((rvar > 9) || !reg)
1584 (VERIFY_WRITE, addr, 4 * (rvar + 1)))
1587 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1592 for (i = 16; rvar; rvar--, i++) {
1593 value = regs->regs[i];
1594 StoreW(addr, value, res);
1599 if ((reg & 0xf) == 9) {
1600 value = regs->regs[30];
1601 StoreW(addr, value, res);
1607 value = regs->regs[31];
1608 StoreW(addr, value, res);
1616 reg = insn.mm_m_format.rd;
1618 if ((rvar > 9) || !reg)
1622 (VERIFY_READ, addr, 8 * (rvar + 1)))
1625 if (!access_ok(VERIFY_READ, addr, 8 * rvar))
1631 for (i = 16; rvar; rvar--, i++) {
1632 LoadDW(addr, value, res);
1636 regs->regs[i] = value;
1638 if ((reg & 0xf) == 9) {
1639 LoadDW(addr, value, res);
1643 regs->regs[30] = value;
1646 LoadDW(addr, value, res);
1649 regs->regs[31] = value;
1652 #endif /* CONFIG_64BIT */
1658 reg = insn.mm_m_format.rd;
1660 if ((rvar > 9) || !reg)
1664 (VERIFY_WRITE, addr, 8 * (rvar + 1)))
1667 if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
1673 for (i = 16; rvar; rvar--, i++) {
1674 value = regs->regs[i];
1675 StoreDW(addr, value, res);
1680 if ((reg & 0xf) == 9) {
1681 value = regs->regs[30];
1682 StoreDW(addr, value, res);
1688 value = regs->regs[31];
1689 StoreDW(addr, value, res);
1694 #endif /* CONFIG_64BIT */
1698 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1704 switch (insn.mm_m_format.func) {
1706 reg = insn.mm_m_format.rd;
1710 /* LL,SC,LLD,SCD are not serviced */
1714 switch (insn.mm_x_format.func) {
1729 /* roll back jump/branch */
1730 regs->cp0_epc = origpc;
1731 regs->regs[31] = orig31;
1733 die_if_kernel("Unaligned FP access in kernel code", regs);
1734 BUG_ON(!used_math());
1735 BUG_ON(!is_fpu_owner());
1737 lose_fpu(1); /* save the FPU state for the emulator */
1738 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1740 own_fpu(1); /* restore FPU state */
1742 /* If something went wrong, signal */
1743 process_fpemu_return(res, fault_addr, 0);
1750 reg = insn.mm_i_format.rt;
1754 reg = insn.mm_i_format.rt;
1758 reg = insn.mm_i_format.rt;
1762 reg = insn.mm_i_format.rt;
1766 reg = insn.mm_i_format.rt;
1770 reg = insn.mm_i_format.rt;
1774 reg = insn.mm_i_format.rt;
1778 switch (insn.mm16_m_format.func) {
1780 reg = insn.mm16_m_format.rlist;
1782 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1785 for (i = 16; rvar; rvar--, i++) {
1786 LoadW(addr, value, res);
1790 regs->regs[i] = value;
1792 LoadW(addr, value, res);
1795 regs->regs[31] = value;
1800 reg = insn.mm16_m_format.rlist;
1802 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1805 for (i = 16; rvar; rvar--, i++) {
1806 value = regs->regs[i];
1807 StoreW(addr, value, res);
1812 value = regs->regs[31];
1813 StoreW(addr, value, res);
1824 reg = reg16to32[insn.mm16_rb_format.rt];
1828 reg = reg16to32[insn.mm16_rb_format.rt];
1832 reg = reg16to32st[insn.mm16_rb_format.rt];
1836 reg = reg16to32st[insn.mm16_rb_format.rt];
1840 reg = insn.mm16_r5_format.rt;
1844 reg = insn.mm16_r5_format.rt;
1848 reg = reg16to32[insn.mm16_r3_format.rt];
1856 if (!access_ok(VERIFY_READ, addr, 2))
1859 LoadHW(addr, value, res);
1862 regs->regs[reg] = value;
1866 if (!access_ok(VERIFY_READ, addr, 2))
1869 LoadHWU(addr, value, res);
1872 regs->regs[reg] = value;
1876 if (!access_ok(VERIFY_READ, addr, 4))
1879 LoadW(addr, value, res);
1882 regs->regs[reg] = value;
1888 * A 32-bit kernel might be running on a 64-bit processor. But
1889 * if we're on a 32-bit processor and an i-cache incoherency
1890 * or race makes us see a 64-bit instruction here the sdl/sdr
1891 * would blow up, so for now we don't handle unaligned 64-bit
1892 * instructions on 32-bit kernels.
1894 if (!access_ok(VERIFY_READ, addr, 4))
1897 LoadWU(addr, value, res);
1900 regs->regs[reg] = value;
1902 #endif /* CONFIG_64BIT */
1904 /* Cannot handle 64-bit instructions in 32-bit kernel */
1910 * A 32-bit kernel might be running on a 64-bit processor. But
1911 * if we're on a 32-bit processor and an i-cache incoherency
1912 * or race makes us see a 64-bit instruction here the sdl/sdr
1913 * would blow up, so for now we don't handle unaligned 64-bit
1914 * instructions on 32-bit kernels.
1916 if (!access_ok(VERIFY_READ, addr, 8))
1919 LoadDW(addr, value, res);
1922 regs->regs[reg] = value;
1924 #endif /* CONFIG_64BIT */
1926 /* Cannot handle 64-bit instructions in 32-bit kernel */
1930 if (!access_ok(VERIFY_WRITE, addr, 2))
1933 value = regs->regs[reg];
1934 StoreHW(addr, value, res);
1940 if (!access_ok(VERIFY_WRITE, addr, 4))
1943 value = regs->regs[reg];
1944 StoreW(addr, value, res);
1952 * A 32-bit kernel might be running on a 64-bit processor. But
1953 * if we're on a 32-bit processor and an i-cache incoherency
1954 * or race makes us see a 64-bit instruction here the sdl/sdr
1955 * would blow up, so for now we don't handle unaligned 64-bit
1956 * instructions on 32-bit kernels.
1958 if (!access_ok(VERIFY_WRITE, addr, 8))
1961 value = regs->regs[reg];
1962 StoreDW(addr, value, res);
1966 #endif /* CONFIG_64BIT */
1968 /* Cannot handle 64-bit instructions in 32-bit kernel */
1972 regs->cp0_epc = contpc; /* advance or branch */
1974 #ifdef CONFIG_DEBUG_FS
1975 unaligned_instructions++;
1980 /* roll back jump/branch */
1981 regs->cp0_epc = origpc;
1982 regs->regs[31] = orig31;
1983 /* Did we have an exception handler installed? */
1984 if (fixup_exception(regs))
1987 die_if_kernel("Unhandled kernel unaligned access", regs);
1988 force_sig(SIGSEGV, current);
1993 die_if_kernel("Unhandled kernel unaligned access", regs);
1994 force_sig(SIGBUS, current);
2000 ("Unhandled kernel unaligned access or invalid instruction", regs);
2001 force_sig(SIGILL, current);
2004 static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
2006 unsigned long value;
2009 unsigned long orig31;
2011 unsigned long origpc;
2012 union mips16e_instruction mips16inst, oldinst;
2013 unsigned int opcode;
2016 origpc = regs->cp0_epc;
2017 orig31 = regs->regs[31];
2018 pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
2020 * This load never faults.
2022 __get_user(mips16inst.full, pc16);
2023 oldinst = mips16inst;
2025 /* skip EXTEND instruction */
2026 if (mips16inst.ri.opcode == MIPS16e_extend_op) {
2029 __get_user(mips16inst.full, pc16);
2030 } else if (delay_slot(regs)) {
2031 /* skip jump instructions */
2032 /* JAL/JALX are 32 bits but have OPCODE in first short int */
2033 if (mips16inst.ri.opcode == MIPS16e_jal_op)
2036 if (get_user(mips16inst.full, pc16))
2040 opcode = mips16inst.ri.opcode;
2042 case MIPS16e_i64_op: /* I64 or RI64 instruction */
2043 switch (mips16inst.i64.func) { /* I64/RI64 func field check */
2044 case MIPS16e_ldpc_func:
2045 case MIPS16e_ldsp_func:
2046 reg = reg16to32[mips16inst.ri64.ry];
2049 case MIPS16e_sdsp_func:
2050 reg = reg16to32[mips16inst.ri64.ry];
2053 case MIPS16e_sdrasp_func:
2054 reg = 29; /* GPRSP */
2060 case MIPS16e_swsp_op:
2061 reg = reg16to32[mips16inst.ri.rx];
2062 if (extended && cpu_has_mips16e2)
2063 switch (mips16inst.ri.imm >> 5) {
2068 opcode = MIPS16e_sh_op;
2075 case MIPS16e_lwpc_op:
2076 reg = reg16to32[mips16inst.ri.rx];
2079 case MIPS16e_lwsp_op:
2080 reg = reg16to32[mips16inst.ri.rx];
2081 if (extended && cpu_has_mips16e2)
2082 switch (mips16inst.ri.imm >> 5) {
2087 opcode = MIPS16e_lh_op;
2090 opcode = MIPS16e_lhu_op;
2098 if (mips16inst.i8.func != MIPS16e_swrasp_func)
2100 reg = 29; /* GPRSP */
2104 reg = reg16to32[mips16inst.rri.ry];
2111 case MIPS16e_lbu_op:
2116 if (!access_ok(VERIFY_READ, addr, 2))
2119 LoadHW(addr, value, res);
2122 MIPS16e_compute_return_epc(regs, &oldinst);
2123 regs->regs[reg] = value;
2126 case MIPS16e_lhu_op:
2127 if (!access_ok(VERIFY_READ, addr, 2))
2130 LoadHWU(addr, value, res);
2133 MIPS16e_compute_return_epc(regs, &oldinst);
2134 regs->regs[reg] = value;
2138 case MIPS16e_lwpc_op:
2139 case MIPS16e_lwsp_op:
2140 if (!access_ok(VERIFY_READ, addr, 4))
2143 LoadW(addr, value, res);
2146 MIPS16e_compute_return_epc(regs, &oldinst);
2147 regs->regs[reg] = value;
2150 case MIPS16e_lwu_op:
2153 * A 32-bit kernel might be running on a 64-bit processor. But
2154 * if we're on a 32-bit processor and an i-cache incoherency
2155 * or race makes us see a 64-bit instruction here the sdl/sdr
2156 * would blow up, so for now we don't handle unaligned 64-bit
2157 * instructions on 32-bit kernels.
2159 if (!access_ok(VERIFY_READ, addr, 4))
2162 LoadWU(addr, value, res);
2165 MIPS16e_compute_return_epc(regs, &oldinst);
2166 regs->regs[reg] = value;
2168 #endif /* CONFIG_64BIT */
2170 /* Cannot handle 64-bit instructions in 32-bit kernel */
2177 * A 32-bit kernel might be running on a 64-bit processor. But
2178 * if we're on a 32-bit processor and an i-cache incoherency
2179 * or race makes us see a 64-bit instruction here the sdl/sdr
2180 * would blow up, so for now we don't handle unaligned 64-bit
2181 * instructions on 32-bit kernels.
2183 if (!access_ok(VERIFY_READ, addr, 8))
2186 LoadDW(addr, value, res);
2189 MIPS16e_compute_return_epc(regs, &oldinst);
2190 regs->regs[reg] = value;
2192 #endif /* CONFIG_64BIT */
2194 /* Cannot handle 64-bit instructions in 32-bit kernel */
2198 if (!access_ok(VERIFY_WRITE, addr, 2))
2201 MIPS16e_compute_return_epc(regs, &oldinst);
2202 value = regs->regs[reg];
2203 StoreHW(addr, value, res);
2209 case MIPS16e_swsp_op:
2210 case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
2211 if (!access_ok(VERIFY_WRITE, addr, 4))
2214 MIPS16e_compute_return_epc(regs, &oldinst);
2215 value = regs->regs[reg];
2216 StoreW(addr, value, res);
2225 * A 32-bit kernel might be running on a 64-bit processor. But
2226 * if we're on a 32-bit processor and an i-cache incoherency
2227 * or race makes us see a 64-bit instruction here the sdl/sdr
2228 * would blow up, so for now we don't handle unaligned 64-bit
2229 * instructions on 32-bit kernels.
2231 if (!access_ok(VERIFY_WRITE, addr, 8))
2234 MIPS16e_compute_return_epc(regs, &oldinst);
2235 value = regs->regs[reg];
2236 StoreDW(addr, value, res);
2240 #endif /* CONFIG_64BIT */
2242 /* Cannot handle 64-bit instructions in 32-bit kernel */
2247 * Pheeee... We encountered an yet unknown instruction or
2248 * cache coherence problem. Die sucker, die ...
2253 #ifdef CONFIG_DEBUG_FS
2254 unaligned_instructions++;
2260 /* roll back jump/branch */
2261 regs->cp0_epc = origpc;
2262 regs->regs[31] = orig31;
2263 /* Did we have an exception handler installed? */
2264 if (fixup_exception(regs))
2267 die_if_kernel("Unhandled kernel unaligned access", regs);
2268 force_sig(SIGSEGV, current);
2273 die_if_kernel("Unhandled kernel unaligned access", regs);
2274 force_sig(SIGBUS, current);
2280 ("Unhandled kernel unaligned access or invalid instruction", regs);
2281 force_sig(SIGILL, current);
2284 asmlinkage void do_ade(struct pt_regs *regs)
2286 enum ctx_state prev_state;
2287 unsigned int __user *pc;
2290 prev_state = exception_enter();
2291 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
2292 1, regs, regs->cp0_badvaddr);
2294 * Did we catch a fault trying to load an instruction?
2296 if (regs->cp0_badvaddr == regs->cp0_epc)
2299 if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
2301 if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
2305 * Do branch emulation only if we didn't forward the exception.
2306 * This is all so but ugly ...
2310 * Are we running in microMIPS mode?
2312 if (get_isa16_mode(regs->cp0_epc)) {
2314 * Did we catch a fault trying to load an instruction in
2317 if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
2319 if (unaligned_action == UNALIGNED_ACTION_SHOW)
2320 show_registers(regs);
2322 if (cpu_has_mmips) {
2324 if (!user_mode(regs))
2326 emulate_load_store_microMIPS(regs,
2327 (void __user *)regs->cp0_badvaddr);
2333 if (cpu_has_mips16) {
2335 if (!user_mode(regs))
2337 emulate_load_store_MIPS16e(regs,
2338 (void __user *)regs->cp0_badvaddr);
2347 if (unaligned_action == UNALIGNED_ACTION_SHOW)
2348 show_registers(regs);
2349 pc = (unsigned int __user *)exception_epc(regs);
2352 if (!user_mode(regs))
2354 emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
2360 die_if_kernel("Kernel unaligned instruction access", regs);
2361 force_sig(SIGBUS, current);
2364 * XXX On return from the signal handler we should advance the epc
2366 exception_exit(prev_state);
2369 #ifdef CONFIG_DEBUG_FS
2370 static int __init debugfs_unaligned(void)
2374 if (!mips_debugfs_dir)
2376 d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
2377 mips_debugfs_dir, &unaligned_instructions);
2380 d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
2381 mips_debugfs_dir, &unaligned_action);
2386 arch_initcall(debugfs_unaligned);