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
;
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
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
)))
1231 * If we have live MSA context keep track of
1232 * whether we get preempted in order to avoid
1233 * the register context we load being clobbered
1234 * by the live context as it's saved during
1235 * preemption. If we don't have live context
1236 * then it can't be saved to clobber the value
1239 preempted
= test_thread_flag(TIF_USEDMSA
);
1241 res
= __copy_from_user_inatomic(fpr
, addr
,
1247 * Update the hardware register if it is in use
1248 * by the task in this quantum, in order to
1249 * avoid having to save & restore the whole
1253 if (test_thread_flag(TIF_USEDMSA
)) {
1254 write_msa_wr(wd
, fpr
, df
);
1258 } while (preempted
);
1262 if (!access_ok(VERIFY_WRITE
, addr
, sizeof(*fpr
)))
1266 * Update from the hardware register if it is in use by
1267 * the task in this quantum, in order to avoid having to
1268 * save & restore the whole vector context.
1271 if (test_thread_flag(TIF_USEDMSA
))
1272 read_msa_wr(wd
, fpr
, df
);
1275 res
= __copy_to_user_inatomic(addr
, fpr
, sizeof(*fpr
));
1284 compute_return_epc(regs
);
1287 #ifndef CONFIG_CPU_MIPSR6
1289 * COP2 is available to implementor for application specific use.
1290 * It's up to applications to register a notifier chain and do
1291 * whatever they have to do, including possible sending of signals.
1293 * This instruction has been reallocated in Release 6
1296 cu2_notifier_call_chain(CU2_LWC2_OP
, regs
);
1300 cu2_notifier_call_chain(CU2_LDC2_OP
, regs
);
1304 cu2_notifier_call_chain(CU2_SWC2_OP
, regs
);
1308 cu2_notifier_call_chain(CU2_SDC2_OP
, regs
);
1313 * Pheeee... We encountered an yet unknown instruction or
1314 * cache coherence problem. Die sucker, die ...
1319 #ifdef CONFIG_DEBUG_FS
1320 unaligned_instructions
++;
1326 /* roll back jump/branch */
1327 regs
->cp0_epc
= origpc
;
1328 regs
->regs
[31] = orig31
;
1329 /* Did we have an exception handler installed? */
1330 if (fixup_exception(regs
))
1333 die_if_kernel("Unhandled kernel unaligned access", regs
);
1334 force_sig(SIGSEGV
, current
);
1339 die_if_kernel("Unhandled kernel unaligned access", regs
);
1340 force_sig(SIGBUS
, current
);
1346 ("Unhandled kernel unaligned access or invalid instruction", regs
);
1347 force_sig(SIGILL
, current
);
1350 /* Recode table from 16-bit register notation to 32-bit GPR. */
1351 const int reg16to32
[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
1353 /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
1354 const int reg16to32st
[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
1356 static void emulate_load_store_microMIPS(struct pt_regs
*regs
,
1359 unsigned long value
;
1362 unsigned int reg
= 0, rvar
;
1363 unsigned long orig31
;
1367 unsigned long origpc
, contpc
;
1368 union mips_instruction insn
;
1369 struct mm_decoded_insn mminsn
;
1370 void __user
*fault_addr
= NULL
;
1372 origpc
= regs
->cp0_epc
;
1373 orig31
= regs
->regs
[31];
1375 mminsn
.micro_mips_mode
= 1;
1378 * This load never faults.
1380 pc16
= (unsigned short __user
*)msk_isa16_mode(regs
->cp0_epc
);
1381 __get_user(halfword
, pc16
);
1383 contpc
= regs
->cp0_epc
+ 2;
1384 word
= ((unsigned int)halfword
<< 16);
1387 if (!mm_insn_16bit(halfword
)) {
1388 __get_user(halfword
, pc16
);
1390 contpc
= regs
->cp0_epc
+ 4;
1396 if (get_user(halfword
, pc16
))
1398 mminsn
.next_pc_inc
= 2;
1399 word
= ((unsigned int)halfword
<< 16);
1401 if (!mm_insn_16bit(halfword
)) {
1403 if (get_user(halfword
, pc16
))
1405 mminsn
.next_pc_inc
= 4;
1408 mminsn
.next_insn
= word
;
1410 insn
= (union mips_instruction
)(mminsn
.insn
);
1411 if (mm_isBranchInstr(regs
, mminsn
, &contpc
))
1412 insn
= (union mips_instruction
)(mminsn
.next_insn
);
1414 /* Parse instruction to find what to do */
1416 switch (insn
.mm_i_format
.opcode
) {
1419 switch (insn
.mm_x_format
.func
) {
1421 reg
= insn
.mm_x_format
.rd
;
1428 switch (insn
.mm_m_format
.func
) {
1430 reg
= insn
.mm_m_format
.rd
;
1434 if (!access_ok(VERIFY_READ
, addr
, 8))
1437 LoadW(addr
, value
, res
);
1440 regs
->regs
[reg
] = value
;
1442 LoadW(addr
, value
, res
);
1445 regs
->regs
[reg
+ 1] = value
;
1449 reg
= insn
.mm_m_format
.rd
;
1453 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1456 value
= regs
->regs
[reg
];
1457 StoreW(addr
, value
, res
);
1461 value
= regs
->regs
[reg
+ 1];
1462 StoreW(addr
, value
, res
);
1469 reg
= insn
.mm_m_format
.rd
;
1473 if (!access_ok(VERIFY_READ
, addr
, 16))
1476 LoadDW(addr
, value
, res
);
1479 regs
->regs
[reg
] = value
;
1481 LoadDW(addr
, value
, res
);
1484 regs
->regs
[reg
+ 1] = value
;
1486 #endif /* CONFIG_64BIT */
1492 reg
= insn
.mm_m_format
.rd
;
1496 if (!access_ok(VERIFY_WRITE
, addr
, 16))
1499 value
= regs
->regs
[reg
];
1500 StoreDW(addr
, value
, res
);
1504 value
= regs
->regs
[reg
+ 1];
1505 StoreDW(addr
, value
, res
);
1509 #endif /* CONFIG_64BIT */
1514 reg
= insn
.mm_m_format
.rd
;
1516 if ((rvar
> 9) || !reg
)
1520 (VERIFY_READ
, addr
, 4 * (rvar
+ 1)))
1523 if (!access_ok(VERIFY_READ
, addr
, 4 * rvar
))
1528 for (i
= 16; rvar
; rvar
--, i
++) {
1529 LoadW(addr
, value
, res
);
1533 regs
->regs
[i
] = value
;
1535 if ((reg
& 0xf) == 9) {
1536 LoadW(addr
, value
, res
);
1540 regs
->regs
[30] = value
;
1543 LoadW(addr
, value
, res
);
1546 regs
->regs
[31] = value
;
1551 reg
= insn
.mm_m_format
.rd
;
1553 if ((rvar
> 9) || !reg
)
1557 (VERIFY_WRITE
, addr
, 4 * (rvar
+ 1)))
1560 if (!access_ok(VERIFY_WRITE
, addr
, 4 * rvar
))
1565 for (i
= 16; rvar
; rvar
--, i
++) {
1566 value
= regs
->regs
[i
];
1567 StoreW(addr
, value
, res
);
1572 if ((reg
& 0xf) == 9) {
1573 value
= regs
->regs
[30];
1574 StoreW(addr
, value
, res
);
1580 value
= regs
->regs
[31];
1581 StoreW(addr
, value
, res
);
1589 reg
= insn
.mm_m_format
.rd
;
1591 if ((rvar
> 9) || !reg
)
1595 (VERIFY_READ
, addr
, 8 * (rvar
+ 1)))
1598 if (!access_ok(VERIFY_READ
, addr
, 8 * rvar
))
1604 for (i
= 16; rvar
; rvar
--, i
++) {
1605 LoadDW(addr
, value
, res
);
1609 regs
->regs
[i
] = value
;
1611 if ((reg
& 0xf) == 9) {
1612 LoadDW(addr
, value
, res
);
1616 regs
->regs
[30] = value
;
1619 LoadDW(addr
, value
, res
);
1622 regs
->regs
[31] = value
;
1625 #endif /* CONFIG_64BIT */
1631 reg
= insn
.mm_m_format
.rd
;
1633 if ((rvar
> 9) || !reg
)
1637 (VERIFY_WRITE
, addr
, 8 * (rvar
+ 1)))
1640 if (!access_ok(VERIFY_WRITE
, addr
, 8 * rvar
))
1646 for (i
= 16; rvar
; rvar
--, i
++) {
1647 value
= regs
->regs
[i
];
1648 StoreDW(addr
, value
, res
);
1653 if ((reg
& 0xf) == 9) {
1654 value
= regs
->regs
[30];
1655 StoreDW(addr
, value
, res
);
1661 value
= regs
->regs
[31];
1662 StoreDW(addr
, value
, res
);
1667 #endif /* CONFIG_64BIT */
1671 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1677 switch (insn
.mm_m_format
.func
) {
1679 reg
= insn
.mm_m_format
.rd
;
1683 /* LL,SC,LLD,SCD are not serviced */
1687 switch (insn
.mm_x_format
.func
) {
1702 /* roll back jump/branch */
1703 regs
->cp0_epc
= origpc
;
1704 regs
->regs
[31] = orig31
;
1706 die_if_kernel("Unaligned FP access in kernel code", regs
);
1707 BUG_ON(!used_math());
1708 BUG_ON(!is_fpu_owner());
1710 lose_fpu(1); /* save the FPU state for the emulator */
1711 res
= fpu_emulator_cop1Handler(regs
, ¤t
->thread
.fpu
, 1,
1713 own_fpu(1); /* restore FPU state */
1715 /* If something went wrong, signal */
1716 process_fpemu_return(res
, fault_addr
, 0);
1723 reg
= insn
.mm_i_format
.rt
;
1727 reg
= insn
.mm_i_format
.rt
;
1731 reg
= insn
.mm_i_format
.rt
;
1735 reg
= insn
.mm_i_format
.rt
;
1739 reg
= insn
.mm_i_format
.rt
;
1743 reg
= insn
.mm_i_format
.rt
;
1747 reg
= insn
.mm_i_format
.rt
;
1751 switch (insn
.mm16_m_format
.func
) {
1753 reg
= insn
.mm16_m_format
.rlist
;
1755 if (!access_ok(VERIFY_READ
, addr
, 4 * rvar
))
1758 for (i
= 16; rvar
; rvar
--, i
++) {
1759 LoadW(addr
, value
, res
);
1763 regs
->regs
[i
] = value
;
1765 LoadW(addr
, value
, res
);
1768 regs
->regs
[31] = value
;
1773 reg
= insn
.mm16_m_format
.rlist
;
1775 if (!access_ok(VERIFY_WRITE
, addr
, 4 * rvar
))
1778 for (i
= 16; rvar
; rvar
--, i
++) {
1779 value
= regs
->regs
[i
];
1780 StoreW(addr
, value
, res
);
1785 value
= regs
->regs
[31];
1786 StoreW(addr
, value
, res
);
1797 reg
= reg16to32
[insn
.mm16_rb_format
.rt
];
1801 reg
= reg16to32
[insn
.mm16_rb_format
.rt
];
1805 reg
= reg16to32st
[insn
.mm16_rb_format
.rt
];
1809 reg
= reg16to32st
[insn
.mm16_rb_format
.rt
];
1813 reg
= insn
.mm16_r5_format
.rt
;
1817 reg
= insn
.mm16_r5_format
.rt
;
1821 reg
= reg16to32
[insn
.mm16_r3_format
.rt
];
1829 if (!access_ok(VERIFY_READ
, addr
, 2))
1832 LoadHW(addr
, value
, res
);
1835 regs
->regs
[reg
] = value
;
1839 if (!access_ok(VERIFY_READ
, addr
, 2))
1842 LoadHWU(addr
, value
, res
);
1845 regs
->regs
[reg
] = value
;
1849 if (!access_ok(VERIFY_READ
, addr
, 4))
1852 LoadW(addr
, value
, res
);
1855 regs
->regs
[reg
] = value
;
1861 * A 32-bit kernel might be running on a 64-bit processor. But
1862 * if we're on a 32-bit processor and an i-cache incoherency
1863 * or race makes us see a 64-bit instruction here the sdl/sdr
1864 * would blow up, so for now we don't handle unaligned 64-bit
1865 * instructions on 32-bit kernels.
1867 if (!access_ok(VERIFY_READ
, addr
, 4))
1870 LoadWU(addr
, value
, res
);
1873 regs
->regs
[reg
] = value
;
1875 #endif /* CONFIG_64BIT */
1877 /* Cannot handle 64-bit instructions in 32-bit kernel */
1883 * A 32-bit kernel might be running on a 64-bit processor. But
1884 * if we're on a 32-bit processor and an i-cache incoherency
1885 * or race makes us see a 64-bit instruction here the sdl/sdr
1886 * would blow up, so for now we don't handle unaligned 64-bit
1887 * instructions on 32-bit kernels.
1889 if (!access_ok(VERIFY_READ
, addr
, 8))
1892 LoadDW(addr
, value
, res
);
1895 regs
->regs
[reg
] = value
;
1897 #endif /* CONFIG_64BIT */
1899 /* Cannot handle 64-bit instructions in 32-bit kernel */
1903 if (!access_ok(VERIFY_WRITE
, addr
, 2))
1906 value
= regs
->regs
[reg
];
1907 StoreHW(addr
, value
, res
);
1913 if (!access_ok(VERIFY_WRITE
, addr
, 4))
1916 value
= regs
->regs
[reg
];
1917 StoreW(addr
, value
, res
);
1925 * A 32-bit kernel might be running on a 64-bit processor. But
1926 * if we're on a 32-bit processor and an i-cache incoherency
1927 * or race makes us see a 64-bit instruction here the sdl/sdr
1928 * would blow up, so for now we don't handle unaligned 64-bit
1929 * instructions on 32-bit kernels.
1931 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1934 value
= regs
->regs
[reg
];
1935 StoreDW(addr
, value
, res
);
1939 #endif /* CONFIG_64BIT */
1941 /* Cannot handle 64-bit instructions in 32-bit kernel */
1945 regs
->cp0_epc
= contpc
; /* advance or branch */
1947 #ifdef CONFIG_DEBUG_FS
1948 unaligned_instructions
++;
1953 /* roll back jump/branch */
1954 regs
->cp0_epc
= origpc
;
1955 regs
->regs
[31] = orig31
;
1956 /* Did we have an exception handler installed? */
1957 if (fixup_exception(regs
))
1960 die_if_kernel("Unhandled kernel unaligned access", regs
);
1961 force_sig(SIGSEGV
, current
);
1966 die_if_kernel("Unhandled kernel unaligned access", regs
);
1967 force_sig(SIGBUS
, current
);
1973 ("Unhandled kernel unaligned access or invalid instruction", regs
);
1974 force_sig(SIGILL
, current
);
1977 static void emulate_load_store_MIPS16e(struct pt_regs
*regs
, void __user
* addr
)
1979 unsigned long value
;
1982 unsigned long orig31
;
1984 unsigned long origpc
;
1985 union mips16e_instruction mips16inst
, oldinst
;
1987 origpc
= regs
->cp0_epc
;
1988 orig31
= regs
->regs
[31];
1989 pc16
= (unsigned short __user
*)msk_isa16_mode(origpc
);
1991 * This load never faults.
1993 __get_user(mips16inst
.full
, pc16
);
1994 oldinst
= mips16inst
;
1996 /* skip EXTEND instruction */
1997 if (mips16inst
.ri
.opcode
== MIPS16e_extend_op
) {
1999 __get_user(mips16inst
.full
, pc16
);
2000 } else if (delay_slot(regs
)) {
2001 /* skip jump instructions */
2002 /* JAL/JALX are 32 bits but have OPCODE in first short int */
2003 if (mips16inst
.ri
.opcode
== MIPS16e_jal_op
)
2006 if (get_user(mips16inst
.full
, pc16
))
2010 switch (mips16inst
.ri
.opcode
) {
2011 case MIPS16e_i64_op
: /* I64 or RI64 instruction */
2012 switch (mips16inst
.i64
.func
) { /* I64/RI64 func field check */
2013 case MIPS16e_ldpc_func
:
2014 case MIPS16e_ldsp_func
:
2015 reg
= reg16to32
[mips16inst
.ri64
.ry
];
2018 case MIPS16e_sdsp_func
:
2019 reg
= reg16to32
[mips16inst
.ri64
.ry
];
2022 case MIPS16e_sdrasp_func
:
2023 reg
= 29; /* GPRSP */
2029 case MIPS16e_swsp_op
:
2030 case MIPS16e_lwpc_op
:
2031 case MIPS16e_lwsp_op
:
2032 reg
= reg16to32
[mips16inst
.ri
.rx
];
2036 if (mips16inst
.i8
.func
!= MIPS16e_swrasp_func
)
2038 reg
= 29; /* GPRSP */
2042 reg
= reg16to32
[mips16inst
.rri
.ry
];
2046 switch (mips16inst
.ri
.opcode
) {
2049 case MIPS16e_lbu_op
:
2054 if (!access_ok(VERIFY_READ
, addr
, 2))
2057 LoadHW(addr
, value
, res
);
2060 MIPS16e_compute_return_epc(regs
, &oldinst
);
2061 regs
->regs
[reg
] = value
;
2064 case MIPS16e_lhu_op
:
2065 if (!access_ok(VERIFY_READ
, addr
, 2))
2068 LoadHWU(addr
, value
, res
);
2071 MIPS16e_compute_return_epc(regs
, &oldinst
);
2072 regs
->regs
[reg
] = value
;
2076 case MIPS16e_lwpc_op
:
2077 case MIPS16e_lwsp_op
:
2078 if (!access_ok(VERIFY_READ
, addr
, 4))
2081 LoadW(addr
, value
, res
);
2084 MIPS16e_compute_return_epc(regs
, &oldinst
);
2085 regs
->regs
[reg
] = value
;
2088 case MIPS16e_lwu_op
:
2091 * A 32-bit kernel might be running on a 64-bit processor. But
2092 * if we're on a 32-bit processor and an i-cache incoherency
2093 * or race makes us see a 64-bit instruction here the sdl/sdr
2094 * would blow up, so for now we don't handle unaligned 64-bit
2095 * instructions on 32-bit kernels.
2097 if (!access_ok(VERIFY_READ
, addr
, 4))
2100 LoadWU(addr
, value
, res
);
2103 MIPS16e_compute_return_epc(regs
, &oldinst
);
2104 regs
->regs
[reg
] = value
;
2106 #endif /* CONFIG_64BIT */
2108 /* Cannot handle 64-bit instructions in 32-bit kernel */
2115 * A 32-bit kernel might be running on a 64-bit processor. But
2116 * if we're on a 32-bit processor and an i-cache incoherency
2117 * or race makes us see a 64-bit instruction here the sdl/sdr
2118 * would blow up, so for now we don't handle unaligned 64-bit
2119 * instructions on 32-bit kernels.
2121 if (!access_ok(VERIFY_READ
, addr
, 8))
2124 LoadDW(addr
, value
, res
);
2127 MIPS16e_compute_return_epc(regs
, &oldinst
);
2128 regs
->regs
[reg
] = value
;
2130 #endif /* CONFIG_64BIT */
2132 /* Cannot handle 64-bit instructions in 32-bit kernel */
2136 if (!access_ok(VERIFY_WRITE
, addr
, 2))
2139 MIPS16e_compute_return_epc(regs
, &oldinst
);
2140 value
= regs
->regs
[reg
];
2141 StoreHW(addr
, value
, res
);
2147 case MIPS16e_swsp_op
:
2148 case MIPS16e_i8_op
: /* actually - MIPS16e_swrasp_func */
2149 if (!access_ok(VERIFY_WRITE
, addr
, 4))
2152 MIPS16e_compute_return_epc(regs
, &oldinst
);
2153 value
= regs
->regs
[reg
];
2154 StoreW(addr
, value
, res
);
2163 * A 32-bit kernel might be running on a 64-bit processor. But
2164 * if we're on a 32-bit processor and an i-cache incoherency
2165 * or race makes us see a 64-bit instruction here the sdl/sdr
2166 * would blow up, so for now we don't handle unaligned 64-bit
2167 * instructions on 32-bit kernels.
2169 if (!access_ok(VERIFY_WRITE
, addr
, 8))
2172 MIPS16e_compute_return_epc(regs
, &oldinst
);
2173 value
= regs
->regs
[reg
];
2174 StoreDW(addr
, value
, res
);
2178 #endif /* CONFIG_64BIT */
2180 /* Cannot handle 64-bit instructions in 32-bit kernel */
2185 * Pheeee... We encountered an yet unknown instruction or
2186 * cache coherence problem. Die sucker, die ...
2191 #ifdef CONFIG_DEBUG_FS
2192 unaligned_instructions
++;
2198 /* roll back jump/branch */
2199 regs
->cp0_epc
= origpc
;
2200 regs
->regs
[31] = orig31
;
2201 /* Did we have an exception handler installed? */
2202 if (fixup_exception(regs
))
2205 die_if_kernel("Unhandled kernel unaligned access", regs
);
2206 force_sig(SIGSEGV
, current
);
2211 die_if_kernel("Unhandled kernel unaligned access", regs
);
2212 force_sig(SIGBUS
, current
);
2218 ("Unhandled kernel unaligned access or invalid instruction", regs
);
2219 force_sig(SIGILL
, current
);
2222 asmlinkage
void do_ade(struct pt_regs
*regs
)
2224 enum ctx_state prev_state
;
2225 unsigned int __user
*pc
;
2228 prev_state
= exception_enter();
2229 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
,
2230 1, regs
, regs
->cp0_badvaddr
);
2232 * Did we catch a fault trying to load an instruction?
2234 if (regs
->cp0_badvaddr
== regs
->cp0_epc
)
2237 if (user_mode(regs
) && !test_thread_flag(TIF_FIXADE
))
2239 if (unaligned_action
== UNALIGNED_ACTION_SIGNAL
)
2243 * Do branch emulation only if we didn't forward the exception.
2244 * This is all so but ugly ...
2248 * Are we running in microMIPS mode?
2250 if (get_isa16_mode(regs
->cp0_epc
)) {
2252 * Did we catch a fault trying to load an instruction in
2255 if (regs
->cp0_badvaddr
== msk_isa16_mode(regs
->cp0_epc
))
2257 if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
2258 show_registers(regs
);
2260 if (cpu_has_mmips
) {
2262 if (!user_mode(regs
))
2264 emulate_load_store_microMIPS(regs
,
2265 (void __user
*)regs
->cp0_badvaddr
);
2271 if (cpu_has_mips16
) {
2273 if (!user_mode(regs
))
2275 emulate_load_store_MIPS16e(regs
,
2276 (void __user
*)regs
->cp0_badvaddr
);
2285 if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
2286 show_registers(regs
);
2287 pc
= (unsigned int __user
*)exception_epc(regs
);
2290 if (!user_mode(regs
))
2292 emulate_load_store_insn(regs
, (void __user
*)regs
->cp0_badvaddr
, pc
);
2298 die_if_kernel("Kernel unaligned instruction access", regs
);
2299 force_sig(SIGBUS
, current
);
2302 * XXX On return from the signal handler we should advance the epc
2304 exception_exit(prev_state
);
2307 #ifdef CONFIG_DEBUG_FS
2308 static int __init
debugfs_unaligned(void)
2312 if (!mips_debugfs_dir
)
2314 d
= debugfs_create_u32("unaligned_instructions", S_IRUGO
,
2315 mips_debugfs_dir
, &unaligned_instructions
);
2318 d
= debugfs_create_u32("unaligned_action", S_IRUGO
| S_IWUSR
,
2319 mips_debugfs_dir
, &unaligned_action
);
2324 arch_initcall(debugfs_unaligned
);