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 #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
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)); \
154 #else /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
155 /* For CPUs without 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_HAS_LOAD_STORE_LR */
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 #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
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)); \
258 #else /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
259 /* For CPUs without 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_HAS_LOAD_STORE_LR */
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 #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
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)); \
409 #else /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
410 #define _StoreW(addr, value, res, type) \
412 __asm__ __volatile__ ( \
415 "1:"type##_sb("%1", "3(%2)")"\n\t" \
416 "srl\t$1, %1, 0x8\n\t" \
417 "2:"type##_sb("$1", "2(%2)")"\n\t" \
418 "srl\t$1, $1, 0x8\n\t" \
419 "3:"type##_sb("$1", "1(%2)")"\n\t" \
420 "srl\t$1, $1, 0x8\n\t" \
421 "4:"type##_sb("$1", "0(%2)")"\n\t" \
426 ".section\t.fixup,\"ax\"\n\t" \
427 "11:\tli\t%0, %3\n\t" \
430 ".section\t__ex_table,\"a\"\n\t" \
431 STR(PTR)"\t1b, 11b\n\t" \
432 STR(PTR)"\t2b, 11b\n\t" \
433 STR(PTR)"\t3b, 11b\n\t" \
434 STR(PTR)"\t4b, 11b\n\t" \
437 : "r" (value), "r" (addr), "i" (-EFAULT) \
441 #define _StoreDW(addr, value, res) \
443 __asm__ __volatile__ ( \
446 "1:sb\t%1, 7(%2)\n\t" \
447 "dsrl\t$1, %1, 0x8\n\t" \
448 "2:sb\t$1, 6(%2)\n\t" \
449 "dsrl\t$1, $1, 0x8\n\t" \
450 "3:sb\t$1, 5(%2)\n\t" \
451 "dsrl\t$1, $1, 0x8\n\t" \
452 "4:sb\t$1, 4(%2)\n\t" \
453 "dsrl\t$1, $1, 0x8\n\t" \
454 "5:sb\t$1, 3(%2)\n\t" \
455 "dsrl\t$1, $1, 0x8\n\t" \
456 "6:sb\t$1, 2(%2)\n\t" \
457 "dsrl\t$1, $1, 0x8\n\t" \
458 "7:sb\t$1, 1(%2)\n\t" \
459 "dsrl\t$1, $1, 0x8\n\t" \
460 "8:sb\t$1, 0(%2)\n\t" \
461 "dsrl\t$1, $1, 0x8\n\t" \
466 ".section\t.fixup,\"ax\"\n\t" \
467 "11:\tli\t%0, %3\n\t" \
470 ".section\t__ex_table,\"a\"\n\t" \
471 STR(PTR)"\t1b, 11b\n\t" \
472 STR(PTR)"\t2b, 11b\n\t" \
473 STR(PTR)"\t3b, 11b\n\t" \
474 STR(PTR)"\t4b, 11b\n\t" \
475 STR(PTR)"\t5b, 11b\n\t" \
476 STR(PTR)"\t6b, 11b\n\t" \
477 STR(PTR)"\t7b, 11b\n\t" \
478 STR(PTR)"\t8b, 11b\n\t" \
481 : "r" (value), "r" (addr), "i" (-EFAULT) \
485 #endif /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
487 #else /* __BIG_ENDIAN */
489 #define _LoadHW(addr, value, res, type) \
491 __asm__ __volatile__ (".set\tnoat\n" \
492 "1:\t"type##_lb("%0", "1(%2)")"\n" \
493 "2:\t"type##_lbu("$1", "0(%2)")"\n\t"\
499 ".section\t.fixup,\"ax\"\n\t" \
500 "4:\tli\t%1, %3\n\t" \
503 ".section\t__ex_table,\"a\"\n\t" \
504 STR(PTR)"\t1b, 4b\n\t" \
505 STR(PTR)"\t2b, 4b\n\t" \
507 : "=&r" (value), "=r" (res) \
508 : "r" (addr), "i" (-EFAULT)); \
511 #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
512 #define _LoadW(addr, value, res, type) \
514 __asm__ __volatile__ ( \
515 "1:\t"type##_lwl("%0", "3(%2)")"\n" \
516 "2:\t"type##_lwr("%0", "(%2)")"\n\t"\
520 ".section\t.fixup,\"ax\"\n\t" \
521 "4:\tli\t%1, %3\n\t" \
524 ".section\t__ex_table,\"a\"\n\t" \
525 STR(PTR)"\t1b, 4b\n\t" \
526 STR(PTR)"\t2b, 4b\n\t" \
528 : "=&r" (value), "=r" (res) \
529 : "r" (addr), "i" (-EFAULT)); \
532 #else /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
533 /* For CPUs without lwl instruction */
534 #define _LoadW(addr, value, res, type) \
536 __asm__ __volatile__ ( \
539 "1:"type##_lb("%0", "3(%2)")"\n\t" \
540 "2:"type##_lbu("$1", "2(%2)")"\n\t" \
543 "3:"type##_lbu("$1", "1(%2)")"\n\t" \
546 "4:"type##_lbu("$1", "0(%2)")"\n\t" \
553 ".section\t.fixup,\"ax\"\n\t" \
554 "11:\tli\t%1, %3\n\t" \
557 ".section\t__ex_table,\"a\"\n\t" \
558 STR(PTR)"\t1b, 11b\n\t" \
559 STR(PTR)"\t2b, 11b\n\t" \
560 STR(PTR)"\t3b, 11b\n\t" \
561 STR(PTR)"\t4b, 11b\n\t" \
563 : "=&r" (value), "=r" (res) \
564 : "r" (addr), "i" (-EFAULT)); \
567 #endif /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
570 #define _LoadHWU(addr, value, res, type) \
572 __asm__ __volatile__ ( \
574 "1:\t"type##_lbu("%0", "1(%2)")"\n" \
575 "2:\t"type##_lbu("$1", "0(%2)")"\n\t"\
582 ".section\t.fixup,\"ax\"\n\t" \
583 "4:\tli\t%1, %3\n\t" \
586 ".section\t__ex_table,\"a\"\n\t" \
587 STR(PTR)"\t1b, 4b\n\t" \
588 STR(PTR)"\t2b, 4b\n\t" \
590 : "=&r" (value), "=r" (res) \
591 : "r" (addr), "i" (-EFAULT)); \
594 #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
595 #define _LoadWU(addr, value, res, type) \
597 __asm__ __volatile__ ( \
598 "1:\t"type##_lwl("%0", "3(%2)")"\n" \
599 "2:\t"type##_lwr("%0", "(%2)")"\n\t"\
600 "dsll\t%0, %0, 32\n\t" \
601 "dsrl\t%0, %0, 32\n\t" \
605 "\t.section\t.fixup,\"ax\"\n\t" \
606 "4:\tli\t%1, %3\n\t" \
609 ".section\t__ex_table,\"a\"\n\t" \
610 STR(PTR)"\t1b, 4b\n\t" \
611 STR(PTR)"\t2b, 4b\n\t" \
613 : "=&r" (value), "=r" (res) \
614 : "r" (addr), "i" (-EFAULT)); \
617 #define _LoadDW(addr, value, res) \
619 __asm__ __volatile__ ( \
620 "1:\tldl\t%0, 7(%2)\n" \
621 "2:\tldr\t%0, (%2)\n\t" \
625 "\t.section\t.fixup,\"ax\"\n\t" \
626 "4:\tli\t%1, %3\n\t" \
629 ".section\t__ex_table,\"a\"\n\t" \
630 STR(PTR)"\t1b, 4b\n\t" \
631 STR(PTR)"\t2b, 4b\n\t" \
633 : "=&r" (value), "=r" (res) \
634 : "r" (addr), "i" (-EFAULT)); \
637 #else /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
638 /* For CPUs without lwl and ldl instructions */
639 #define _LoadWU(addr, value, res, type) \
641 __asm__ __volatile__ ( \
644 "1:"type##_lbu("%0", "3(%2)")"\n\t" \
645 "2:"type##_lbu("$1", "2(%2)")"\n\t" \
648 "3:"type##_lbu("$1", "1(%2)")"\n\t" \
651 "4:"type##_lbu("$1", "0(%2)")"\n\t" \
658 ".section\t.fixup,\"ax\"\n\t" \
659 "11:\tli\t%1, %3\n\t" \
662 ".section\t__ex_table,\"a\"\n\t" \
663 STR(PTR)"\t1b, 11b\n\t" \
664 STR(PTR)"\t2b, 11b\n\t" \
665 STR(PTR)"\t3b, 11b\n\t" \
666 STR(PTR)"\t4b, 11b\n\t" \
668 : "=&r" (value), "=r" (res) \
669 : "r" (addr), "i" (-EFAULT)); \
672 #define _LoadDW(addr, value, res) \
674 __asm__ __volatile__ ( \
677 "1:lb\t%0, 7(%2)\n\t" \
678 "2:lbu\t$1, 6(%2)\n\t" \
679 "dsll\t%0, 0x8\n\t" \
681 "3:lbu\t$1, 5(%2)\n\t" \
682 "dsll\t%0, 0x8\n\t" \
684 "4:lbu\t$1, 4(%2)\n\t" \
685 "dsll\t%0, 0x8\n\t" \
687 "5:lbu\t$1, 3(%2)\n\t" \
688 "dsll\t%0, 0x8\n\t" \
690 "6:lbu\t$1, 2(%2)\n\t" \
691 "dsll\t%0, 0x8\n\t" \
693 "7:lbu\t$1, 1(%2)\n\t" \
694 "dsll\t%0, 0x8\n\t" \
696 "8:lbu\t$1, 0(%2)\n\t" \
697 "dsll\t%0, 0x8\n\t" \
703 ".section\t.fixup,\"ax\"\n\t" \
704 "11:\tli\t%1, %3\n\t" \
707 ".section\t__ex_table,\"a\"\n\t" \
708 STR(PTR)"\t1b, 11b\n\t" \
709 STR(PTR)"\t2b, 11b\n\t" \
710 STR(PTR)"\t3b, 11b\n\t" \
711 STR(PTR)"\t4b, 11b\n\t" \
712 STR(PTR)"\t5b, 11b\n\t" \
713 STR(PTR)"\t6b, 11b\n\t" \
714 STR(PTR)"\t7b, 11b\n\t" \
715 STR(PTR)"\t8b, 11b\n\t" \
717 : "=&r" (value), "=r" (res) \
718 : "r" (addr), "i" (-EFAULT)); \
720 #endif /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
722 #define _StoreHW(addr, value, res, type) \
724 __asm__ __volatile__ ( \
726 "1:\t"type##_sb("%1", "0(%2)")"\n" \
727 "srl\t$1,%1, 0x8\n" \
728 "2:\t"type##_sb("$1", "1(%2)")"\n" \
733 ".section\t.fixup,\"ax\"\n\t" \
734 "4:\tli\t%0, %3\n\t" \
737 ".section\t__ex_table,\"a\"\n\t" \
738 STR(PTR)"\t1b, 4b\n\t" \
739 STR(PTR)"\t2b, 4b\n\t" \
742 : "r" (value), "r" (addr), "i" (-EFAULT));\
745 #ifdef CONFIG_CPU_HAS_LOAD_STORE_LR
746 #define _StoreW(addr, value, res, type) \
748 __asm__ __volatile__ ( \
749 "1:\t"type##_swl("%1", "3(%2)")"\n" \
750 "2:\t"type##_swr("%1", "(%2)")"\n\t"\
754 ".section\t.fixup,\"ax\"\n\t" \
755 "4:\tli\t%0, %3\n\t" \
758 ".section\t__ex_table,\"a\"\n\t" \
759 STR(PTR)"\t1b, 4b\n\t" \
760 STR(PTR)"\t2b, 4b\n\t" \
763 : "r" (value), "r" (addr), "i" (-EFAULT)); \
766 #define _StoreDW(addr, value, res) \
768 __asm__ __volatile__ ( \
769 "1:\tsdl\t%1, 7(%2)\n" \
770 "2:\tsdr\t%1, (%2)\n\t" \
774 ".section\t.fixup,\"ax\"\n\t" \
775 "4:\tli\t%0, %3\n\t" \
778 ".section\t__ex_table,\"a\"\n\t" \
779 STR(PTR)"\t1b, 4b\n\t" \
780 STR(PTR)"\t2b, 4b\n\t" \
783 : "r" (value), "r" (addr), "i" (-EFAULT)); \
786 #else /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
787 /* For CPUs without swl and sdl instructions */
788 #define _StoreW(addr, value, res, type) \
790 __asm__ __volatile__ ( \
793 "1:"type##_sb("%1", "0(%2)")"\n\t" \
794 "srl\t$1, %1, 0x8\n\t" \
795 "2:"type##_sb("$1", "1(%2)")"\n\t" \
796 "srl\t$1, $1, 0x8\n\t" \
797 "3:"type##_sb("$1", "2(%2)")"\n\t" \
798 "srl\t$1, $1, 0x8\n\t" \
799 "4:"type##_sb("$1", "3(%2)")"\n\t" \
804 ".section\t.fixup,\"ax\"\n\t" \
805 "11:\tli\t%0, %3\n\t" \
808 ".section\t__ex_table,\"a\"\n\t" \
809 STR(PTR)"\t1b, 11b\n\t" \
810 STR(PTR)"\t2b, 11b\n\t" \
811 STR(PTR)"\t3b, 11b\n\t" \
812 STR(PTR)"\t4b, 11b\n\t" \
815 : "r" (value), "r" (addr), "i" (-EFAULT) \
819 #define _StoreDW(addr, value, res) \
821 __asm__ __volatile__ ( \
824 "1:sb\t%1, 0(%2)\n\t" \
825 "dsrl\t$1, %1, 0x8\n\t" \
826 "2:sb\t$1, 1(%2)\n\t" \
827 "dsrl\t$1, $1, 0x8\n\t" \
828 "3:sb\t$1, 2(%2)\n\t" \
829 "dsrl\t$1, $1, 0x8\n\t" \
830 "4:sb\t$1, 3(%2)\n\t" \
831 "dsrl\t$1, $1, 0x8\n\t" \
832 "5:sb\t$1, 4(%2)\n\t" \
833 "dsrl\t$1, $1, 0x8\n\t" \
834 "6:sb\t$1, 5(%2)\n\t" \
835 "dsrl\t$1, $1, 0x8\n\t" \
836 "7:sb\t$1, 6(%2)\n\t" \
837 "dsrl\t$1, $1, 0x8\n\t" \
838 "8:sb\t$1, 7(%2)\n\t" \
839 "dsrl\t$1, $1, 0x8\n\t" \
844 ".section\t.fixup,\"ax\"\n\t" \
845 "11:\tli\t%0, %3\n\t" \
848 ".section\t__ex_table,\"a\"\n\t" \
849 STR(PTR)"\t1b, 11b\n\t" \
850 STR(PTR)"\t2b, 11b\n\t" \
851 STR(PTR)"\t3b, 11b\n\t" \
852 STR(PTR)"\t4b, 11b\n\t" \
853 STR(PTR)"\t5b, 11b\n\t" \
854 STR(PTR)"\t6b, 11b\n\t" \
855 STR(PTR)"\t7b, 11b\n\t" \
856 STR(PTR)"\t8b, 11b\n\t" \
859 : "r" (value), "r" (addr), "i" (-EFAULT) \
863 #endif /* !CONFIG_CPU_HAS_LOAD_STORE_LR */
866 #define LoadHWU(addr, value, res) _LoadHWU(addr, value, res, kernel)
867 #define LoadHWUE(addr, value, res) _LoadHWU(addr, value, res, user)
868 #define LoadWU(addr, value, res) _LoadWU(addr, value, res, kernel)
869 #define LoadWUE(addr, value, res) _LoadWU(addr, value, res, user)
870 #define LoadHW(addr, value, res) _LoadHW(addr, value, res, kernel)
871 #define LoadHWE(addr, value, res) _LoadHW(addr, value, res, user)
872 #define LoadW(addr, value, res) _LoadW(addr, value, res, kernel)
873 #define LoadWE(addr, value, res) _LoadW(addr, value, res, user)
874 #define LoadDW(addr, value, res) _LoadDW(addr, value, res)
876 #define StoreHW(addr, value, res) _StoreHW(addr, value, res, kernel)
877 #define StoreHWE(addr, value, res) _StoreHW(addr, value, res, user)
878 #define StoreW(addr, value, res) _StoreW(addr, value, res, kernel)
879 #define StoreWE(addr, value, res) _StoreW(addr, value, res, user)
880 #define StoreDW(addr, value, res) _StoreDW(addr, value, res)
882 static void emulate_load_store_insn(struct pt_regs
*regs
,
883 void __user
*addr
, unsigned int __user
*pc
)
885 union mips_instruction insn
;
887 unsigned int res
, preempted
;
888 unsigned long origpc
;
889 unsigned long orig31
;
890 void __user
*fault_addr
= NULL
;
897 origpc
= (unsigned long)pc
;
898 orig31
= regs
->regs
[31];
900 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, 0);
903 * This load never faults.
905 __get_user(insn
.word
, pc
);
907 switch (insn
.i_format
.opcode
) {
909 * These are instructions that a compiler doesn't generate. We
910 * can assume therefore that the code is MIPS-aware and
911 * really buggy. Emulating these instructions would break the
920 * For these instructions the only way to create an address
921 * error is an attempted access to kernel/supervisor address
938 * The remaining opcodes are the ones that are really of
942 if (insn
.dsp_format
.func
== lx_op
) {
943 switch (insn
.dsp_format
.op
) {
945 if (!access_ok(VERIFY_READ
, addr
, 4))
947 LoadW(addr
, value
, res
);
950 compute_return_epc(regs
);
951 regs
->regs
[insn
.dsp_format
.rd
] = value
;
954 if (!access_ok(VERIFY_READ
, addr
, 2))
956 LoadHW(addr
, value
, res
);
959 compute_return_epc(regs
);
960 regs
->regs
[insn
.dsp_format
.rd
] = value
;
969 * we can land here only from kernel accessing user
970 * memory, so we need to "switch" the address limit to
971 * user space, so that address check can work properly.
975 switch (insn
.spec3_format
.func
) {
977 if (!access_ok(VERIFY_READ
, addr
, 2)) {
981 LoadHWE(addr
, value
, res
);
986 compute_return_epc(regs
);
987 regs
->regs
[insn
.spec3_format
.rt
] = value
;
990 if (!access_ok(VERIFY_READ
, addr
, 4)) {
994 LoadWE(addr
, value
, res
);
999 compute_return_epc(regs
);
1000 regs
->regs
[insn
.spec3_format
.rt
] = value
;
1003 if (!access_ok(VERIFY_READ
, addr
, 2)) {
1007 LoadHWUE(addr
, value
, res
);
1012 compute_return_epc(regs
);
1013 regs
->regs
[insn
.spec3_format
.rt
] = value
;
1016 if (!access_ok(VERIFY_WRITE
, addr
, 2)) {
1020 compute_return_epc(regs
);
1021 value
= regs
->regs
[insn
.spec3_format
.rt
];
1022 StoreHWE(addr
, value
, res
);
1029 if (!access_ok(VERIFY_WRITE
, addr
, 4)) {
1033 compute_return_epc(regs
);
1034 value
= regs
->regs
[insn
.spec3_format
.rt
];
1035 StoreWE(addr
, value
, res
);
1050 if (!access_ok(VERIFY_READ
, addr
, 2))
1053 if (IS_ENABLED(CONFIG_EVA
)) {
1054 if (uaccess_kernel())
1055 LoadHW(addr
, value
, res
);
1057 LoadHWE(addr
, value
, res
);
1059 LoadHW(addr
, value
, res
);
1064 compute_return_epc(regs
);
1065 regs
->regs
[insn
.i_format
.rt
] = value
;
1069 if (!access_ok(VERIFY_READ
, addr
, 4))
1072 if (IS_ENABLED(CONFIG_EVA
)) {
1073 if (uaccess_kernel())
1074 LoadW(addr
, value
, res
);
1076 LoadWE(addr
, value
, res
);
1078 LoadW(addr
, value
, res
);
1083 compute_return_epc(regs
);
1084 regs
->regs
[insn
.i_format
.rt
] = value
;
1088 if (!access_ok(VERIFY_READ
, addr
, 2))
1091 if (IS_ENABLED(CONFIG_EVA
)) {
1092 if (uaccess_kernel())
1093 LoadHWU(addr
, value
, res
);
1095 LoadHWUE(addr
, value
, res
);
1097 LoadHWU(addr
, value
, res
);
1102 compute_return_epc(regs
);
1103 regs
->regs
[insn
.i_format
.rt
] = value
;
1109 * A 32-bit kernel might be running on a 64-bit processor. But
1110 * if we're on a 32-bit processor and an i-cache incoherency
1111 * or race makes us see a 64-bit instruction here the sdl/sdr
1112 * would blow up, so for now we don't handle unaligned 64-bit
1113 * instructions on 32-bit kernels.
1115 if (!access_ok(VERIFY_READ
, addr
, 4))
1118 LoadWU(addr
, value
, res
);
1121 compute_return_epc(regs
);
1122 regs
->regs
[insn
.i_format
.rt
] = value
;
1124 #endif /* CONFIG_64BIT */
1126 /* Cannot handle 64-bit instructions in 32-bit kernel */
1132 * A 32-bit kernel might be running on a 64-bit processor. But
1133 * if we're on a 32-bit processor and an i-cache incoherency
1134 * or race makes us see a 64-bit instruction here the sdl/sdr
1135 * would blow up, so for now we don't handle unaligned 64-bit
1136 * instructions on 32-bit kernels.
1138 if (!access_ok(VERIFY_READ
, addr
, 8))
1141 LoadDW(addr
, value
, res
);
1144 compute_return_epc(regs
);
1145 regs
->regs
[insn
.i_format
.rt
] = value
;
1147 #endif /* CONFIG_64BIT */
1149 /* Cannot handle 64-bit instructions in 32-bit kernel */
1153 if (!access_ok(VERIFY_WRITE
, addr
, 2))
1156 compute_return_epc(regs
);
1157 value
= regs
->regs
[insn
.i_format
.rt
];
1159 if (IS_ENABLED(CONFIG_EVA
)) {
1160 if (uaccess_kernel())
1161 StoreHW(addr
, value
, res
);
1163 StoreHWE(addr
, value
, res
);
1165 StoreHW(addr
, value
, res
);
1173 if (!access_ok(VERIFY_WRITE
, addr
, 4))
1176 compute_return_epc(regs
);
1177 value
= regs
->regs
[insn
.i_format
.rt
];
1179 if (IS_ENABLED(CONFIG_EVA
)) {
1180 if (uaccess_kernel())
1181 StoreW(addr
, value
, res
);
1183 StoreWE(addr
, value
, res
);
1185 StoreW(addr
, value
, res
);
1195 * A 32-bit kernel might be running on a 64-bit processor. But
1196 * if we're on a 32-bit processor and an i-cache incoherency
1197 * or race makes us see a 64-bit instruction here the sdl/sdr
1198 * would blow up, so for now we don't handle unaligned 64-bit
1199 * instructions on 32-bit kernels.
1201 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1204 compute_return_epc(regs
);
1205 value
= regs
->regs
[insn
.i_format
.rt
];
1206 StoreDW(addr
, value
, res
);
1210 #endif /* CONFIG_64BIT */
1212 /* Cannot handle 64-bit instructions in 32-bit kernel */
1220 die_if_kernel("Unaligned FP access in kernel code", regs
);
1221 BUG_ON(!used_math());
1223 lose_fpu(1); /* Save FPU state for the emulator. */
1224 res
= fpu_emulator_cop1Handler(regs
, ¤t
->thread
.fpu
, 1,
1226 own_fpu(1); /* Restore FPU state. */
1228 /* Signal if something went wrong. */
1229 process_fpemu_return(res
, fault_addr
, 0);
1240 * If we've reached this point then userland should have taken
1241 * the MSA disabled exception & initialised vector context at
1242 * some point in the past.
1244 BUG_ON(!thread_msa_context_live());
1246 df
= insn
.msa_mi10_format
.df
;
1247 wd
= insn
.msa_mi10_format
.wd
;
1248 fpr
= ¤t
->thread
.fpu
.fpr
[wd
];
1250 switch (insn
.msa_mi10_format
.func
) {
1252 if (!access_ok(VERIFY_READ
, addr
, sizeof(*fpr
)))
1257 * If we have live MSA context keep track of
1258 * whether we get preempted in order to avoid
1259 * the register context we load being clobbered
1260 * by the live context as it's saved during
1261 * preemption. If we don't have live context
1262 * then it can't be saved to clobber the value
1265 preempted
= test_thread_flag(TIF_USEDMSA
);
1267 res
= __copy_from_user_inatomic(fpr
, addr
,
1273 * Update the hardware register if it is in use
1274 * by the task in this quantum, in order to
1275 * avoid having to save & restore the whole
1279 if (test_thread_flag(TIF_USEDMSA
)) {
1280 write_msa_wr(wd
, fpr
, df
);
1284 } while (preempted
);
1288 if (!access_ok(VERIFY_WRITE
, addr
, sizeof(*fpr
)))
1292 * Update from the hardware register if it is in use by
1293 * the task in this quantum, in order to avoid having to
1294 * save & restore the whole vector context.
1297 if (test_thread_flag(TIF_USEDMSA
))
1298 read_msa_wr(wd
, fpr
, df
);
1301 res
= __copy_to_user_inatomic(addr
, fpr
, sizeof(*fpr
));
1310 compute_return_epc(regs
);
1313 #ifndef CONFIG_CPU_MIPSR6
1315 * COP2 is available to implementor for application specific use.
1316 * It's up to applications to register a notifier chain and do
1317 * whatever they have to do, including possible sending of signals.
1319 * This instruction has been reallocated in Release 6
1322 cu2_notifier_call_chain(CU2_LWC2_OP
, regs
);
1326 cu2_notifier_call_chain(CU2_LDC2_OP
, regs
);
1330 cu2_notifier_call_chain(CU2_SWC2_OP
, regs
);
1334 cu2_notifier_call_chain(CU2_SDC2_OP
, regs
);
1339 * Pheeee... We encountered an yet unknown instruction or
1340 * cache coherence problem. Die sucker, die ...
1345 #ifdef CONFIG_DEBUG_FS
1346 unaligned_instructions
++;
1352 /* roll back jump/branch */
1353 regs
->cp0_epc
= origpc
;
1354 regs
->regs
[31] = orig31
;
1355 /* Did we have an exception handler installed? */
1356 if (fixup_exception(regs
))
1359 die_if_kernel("Unhandled kernel unaligned access", regs
);
1360 force_sig(SIGSEGV
, current
);
1365 die_if_kernel("Unhandled kernel unaligned access", regs
);
1366 force_sig(SIGBUS
, current
);
1372 ("Unhandled kernel unaligned access or invalid instruction", regs
);
1373 force_sig(SIGILL
, current
);
1376 /* Recode table from 16-bit register notation to 32-bit GPR. */
1377 const int reg16to32
[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
1379 /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
1380 static const int reg16to32st
[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
1382 static void emulate_load_store_microMIPS(struct pt_regs
*regs
,
1385 unsigned long value
;
1388 unsigned int reg
= 0, rvar
;
1389 unsigned long orig31
;
1393 unsigned long origpc
, contpc
;
1394 union mips_instruction insn
;
1395 struct mm_decoded_insn mminsn
;
1396 void __user
*fault_addr
= NULL
;
1398 origpc
= regs
->cp0_epc
;
1399 orig31
= regs
->regs
[31];
1401 mminsn
.micro_mips_mode
= 1;
1404 * This load never faults.
1406 pc16
= (unsigned short __user
*)msk_isa16_mode(regs
->cp0_epc
);
1407 __get_user(halfword
, pc16
);
1409 contpc
= regs
->cp0_epc
+ 2;
1410 word
= ((unsigned int)halfword
<< 16);
1413 if (!mm_insn_16bit(halfword
)) {
1414 __get_user(halfword
, pc16
);
1416 contpc
= regs
->cp0_epc
+ 4;
1422 if (get_user(halfword
, pc16
))
1424 mminsn
.next_pc_inc
= 2;
1425 word
= ((unsigned int)halfword
<< 16);
1427 if (!mm_insn_16bit(halfword
)) {
1429 if (get_user(halfword
, pc16
))
1431 mminsn
.next_pc_inc
= 4;
1434 mminsn
.next_insn
= word
;
1436 insn
= (union mips_instruction
)(mminsn
.insn
);
1437 if (mm_isBranchInstr(regs
, mminsn
, &contpc
))
1438 insn
= (union mips_instruction
)(mminsn
.next_insn
);
1440 /* Parse instruction to find what to do */
1442 switch (insn
.mm_i_format
.opcode
) {
1445 switch (insn
.mm_x_format
.func
) {
1447 reg
= insn
.mm_x_format
.rd
;
1454 switch (insn
.mm_m_format
.func
) {
1456 reg
= insn
.mm_m_format
.rd
;
1460 if (!access_ok(VERIFY_READ
, addr
, 8))
1463 LoadW(addr
, value
, res
);
1466 regs
->regs
[reg
] = value
;
1468 LoadW(addr
, value
, res
);
1471 regs
->regs
[reg
+ 1] = value
;
1475 reg
= insn
.mm_m_format
.rd
;
1479 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1482 value
= regs
->regs
[reg
];
1483 StoreW(addr
, value
, res
);
1487 value
= regs
->regs
[reg
+ 1];
1488 StoreW(addr
, value
, res
);
1495 reg
= insn
.mm_m_format
.rd
;
1499 if (!access_ok(VERIFY_READ
, addr
, 16))
1502 LoadDW(addr
, value
, res
);
1505 regs
->regs
[reg
] = value
;
1507 LoadDW(addr
, value
, res
);
1510 regs
->regs
[reg
+ 1] = value
;
1512 #endif /* CONFIG_64BIT */
1518 reg
= insn
.mm_m_format
.rd
;
1522 if (!access_ok(VERIFY_WRITE
, addr
, 16))
1525 value
= regs
->regs
[reg
];
1526 StoreDW(addr
, value
, res
);
1530 value
= regs
->regs
[reg
+ 1];
1531 StoreDW(addr
, value
, res
);
1535 #endif /* CONFIG_64BIT */
1540 reg
= insn
.mm_m_format
.rd
;
1542 if ((rvar
> 9) || !reg
)
1546 (VERIFY_READ
, addr
, 4 * (rvar
+ 1)))
1549 if (!access_ok(VERIFY_READ
, addr
, 4 * rvar
))
1554 for (i
= 16; rvar
; rvar
--, i
++) {
1555 LoadW(addr
, value
, res
);
1559 regs
->regs
[i
] = value
;
1561 if ((reg
& 0xf) == 9) {
1562 LoadW(addr
, value
, res
);
1566 regs
->regs
[30] = value
;
1569 LoadW(addr
, value
, res
);
1572 regs
->regs
[31] = value
;
1577 reg
= insn
.mm_m_format
.rd
;
1579 if ((rvar
> 9) || !reg
)
1583 (VERIFY_WRITE
, addr
, 4 * (rvar
+ 1)))
1586 if (!access_ok(VERIFY_WRITE
, addr
, 4 * rvar
))
1591 for (i
= 16; rvar
; rvar
--, i
++) {
1592 value
= regs
->regs
[i
];
1593 StoreW(addr
, value
, res
);
1598 if ((reg
& 0xf) == 9) {
1599 value
= regs
->regs
[30];
1600 StoreW(addr
, value
, res
);
1606 value
= regs
->regs
[31];
1607 StoreW(addr
, value
, res
);
1615 reg
= insn
.mm_m_format
.rd
;
1617 if ((rvar
> 9) || !reg
)
1621 (VERIFY_READ
, addr
, 8 * (rvar
+ 1)))
1624 if (!access_ok(VERIFY_READ
, addr
, 8 * rvar
))
1630 for (i
= 16; rvar
; rvar
--, i
++) {
1631 LoadDW(addr
, value
, res
);
1635 regs
->regs
[i
] = value
;
1637 if ((reg
& 0xf) == 9) {
1638 LoadDW(addr
, value
, res
);
1642 regs
->regs
[30] = value
;
1645 LoadDW(addr
, value
, res
);
1648 regs
->regs
[31] = value
;
1651 #endif /* CONFIG_64BIT */
1657 reg
= insn
.mm_m_format
.rd
;
1659 if ((rvar
> 9) || !reg
)
1663 (VERIFY_WRITE
, addr
, 8 * (rvar
+ 1)))
1666 if (!access_ok(VERIFY_WRITE
, addr
, 8 * rvar
))
1672 for (i
= 16; rvar
; rvar
--, i
++) {
1673 value
= regs
->regs
[i
];
1674 StoreDW(addr
, value
, res
);
1679 if ((reg
& 0xf) == 9) {
1680 value
= regs
->regs
[30];
1681 StoreDW(addr
, value
, res
);
1687 value
= regs
->regs
[31];
1688 StoreDW(addr
, value
, res
);
1693 #endif /* CONFIG_64BIT */
1697 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1703 switch (insn
.mm_m_format
.func
) {
1705 reg
= insn
.mm_m_format
.rd
;
1709 /* LL,SC,LLD,SCD are not serviced */
1713 switch (insn
.mm_x_format
.func
) {
1728 /* roll back jump/branch */
1729 regs
->cp0_epc
= origpc
;
1730 regs
->regs
[31] = orig31
;
1732 die_if_kernel("Unaligned FP access in kernel code", regs
);
1733 BUG_ON(!used_math());
1734 BUG_ON(!is_fpu_owner());
1736 lose_fpu(1); /* save the FPU state for the emulator */
1737 res
= fpu_emulator_cop1Handler(regs
, ¤t
->thread
.fpu
, 1,
1739 own_fpu(1); /* restore FPU state */
1741 /* If something went wrong, signal */
1742 process_fpemu_return(res
, fault_addr
, 0);
1749 reg
= insn
.mm_i_format
.rt
;
1753 reg
= insn
.mm_i_format
.rt
;
1757 reg
= insn
.mm_i_format
.rt
;
1761 reg
= insn
.mm_i_format
.rt
;
1765 reg
= insn
.mm_i_format
.rt
;
1769 reg
= insn
.mm_i_format
.rt
;
1773 reg
= insn
.mm_i_format
.rt
;
1777 switch (insn
.mm16_m_format
.func
) {
1779 reg
= insn
.mm16_m_format
.rlist
;
1781 if (!access_ok(VERIFY_READ
, addr
, 4 * rvar
))
1784 for (i
= 16; rvar
; rvar
--, i
++) {
1785 LoadW(addr
, value
, res
);
1789 regs
->regs
[i
] = value
;
1791 LoadW(addr
, value
, res
);
1794 regs
->regs
[31] = value
;
1799 reg
= insn
.mm16_m_format
.rlist
;
1801 if (!access_ok(VERIFY_WRITE
, addr
, 4 * rvar
))
1804 for (i
= 16; rvar
; rvar
--, i
++) {
1805 value
= regs
->regs
[i
];
1806 StoreW(addr
, value
, res
);
1811 value
= regs
->regs
[31];
1812 StoreW(addr
, value
, res
);
1823 reg
= reg16to32
[insn
.mm16_rb_format
.rt
];
1827 reg
= reg16to32
[insn
.mm16_rb_format
.rt
];
1831 reg
= reg16to32st
[insn
.mm16_rb_format
.rt
];
1835 reg
= reg16to32st
[insn
.mm16_rb_format
.rt
];
1839 reg
= insn
.mm16_r5_format
.rt
;
1843 reg
= insn
.mm16_r5_format
.rt
;
1847 reg
= reg16to32
[insn
.mm16_r3_format
.rt
];
1855 if (!access_ok(VERIFY_READ
, addr
, 2))
1858 LoadHW(addr
, value
, res
);
1861 regs
->regs
[reg
] = value
;
1865 if (!access_ok(VERIFY_READ
, addr
, 2))
1868 LoadHWU(addr
, value
, res
);
1871 regs
->regs
[reg
] = value
;
1875 if (!access_ok(VERIFY_READ
, addr
, 4))
1878 LoadW(addr
, value
, res
);
1881 regs
->regs
[reg
] = value
;
1887 * A 32-bit kernel might be running on a 64-bit processor. But
1888 * if we're on a 32-bit processor and an i-cache incoherency
1889 * or race makes us see a 64-bit instruction here the sdl/sdr
1890 * would blow up, so for now we don't handle unaligned 64-bit
1891 * instructions on 32-bit kernels.
1893 if (!access_ok(VERIFY_READ
, addr
, 4))
1896 LoadWU(addr
, value
, res
);
1899 regs
->regs
[reg
] = value
;
1901 #endif /* CONFIG_64BIT */
1903 /* Cannot handle 64-bit instructions in 32-bit kernel */
1909 * A 32-bit kernel might be running on a 64-bit processor. But
1910 * if we're on a 32-bit processor and an i-cache incoherency
1911 * or race makes us see a 64-bit instruction here the sdl/sdr
1912 * would blow up, so for now we don't handle unaligned 64-bit
1913 * instructions on 32-bit kernels.
1915 if (!access_ok(VERIFY_READ
, addr
, 8))
1918 LoadDW(addr
, value
, res
);
1921 regs
->regs
[reg
] = value
;
1923 #endif /* CONFIG_64BIT */
1925 /* Cannot handle 64-bit instructions in 32-bit kernel */
1929 if (!access_ok(VERIFY_WRITE
, addr
, 2))
1932 value
= regs
->regs
[reg
];
1933 StoreHW(addr
, value
, res
);
1939 if (!access_ok(VERIFY_WRITE
, addr
, 4))
1942 value
= regs
->regs
[reg
];
1943 StoreW(addr
, value
, res
);
1951 * A 32-bit kernel might be running on a 64-bit processor. But
1952 * if we're on a 32-bit processor and an i-cache incoherency
1953 * or race makes us see a 64-bit instruction here the sdl/sdr
1954 * would blow up, so for now we don't handle unaligned 64-bit
1955 * instructions on 32-bit kernels.
1957 if (!access_ok(VERIFY_WRITE
, addr
, 8))
1960 value
= regs
->regs
[reg
];
1961 StoreDW(addr
, value
, res
);
1965 #endif /* CONFIG_64BIT */
1967 /* Cannot handle 64-bit instructions in 32-bit kernel */
1971 regs
->cp0_epc
= contpc
; /* advance or branch */
1973 #ifdef CONFIG_DEBUG_FS
1974 unaligned_instructions
++;
1979 /* roll back jump/branch */
1980 regs
->cp0_epc
= origpc
;
1981 regs
->regs
[31] = orig31
;
1982 /* Did we have an exception handler installed? */
1983 if (fixup_exception(regs
))
1986 die_if_kernel("Unhandled kernel unaligned access", regs
);
1987 force_sig(SIGSEGV
, current
);
1992 die_if_kernel("Unhandled kernel unaligned access", regs
);
1993 force_sig(SIGBUS
, current
);
1999 ("Unhandled kernel unaligned access or invalid instruction", regs
);
2000 force_sig(SIGILL
, current
);
2003 static void emulate_load_store_MIPS16e(struct pt_regs
*regs
, void __user
* addr
)
2005 unsigned long value
;
2008 unsigned long orig31
;
2010 unsigned long origpc
;
2011 union mips16e_instruction mips16inst
, oldinst
;
2012 unsigned int opcode
;
2015 origpc
= regs
->cp0_epc
;
2016 orig31
= regs
->regs
[31];
2017 pc16
= (unsigned short __user
*)msk_isa16_mode(origpc
);
2019 * This load never faults.
2021 __get_user(mips16inst
.full
, pc16
);
2022 oldinst
= mips16inst
;
2024 /* skip EXTEND instruction */
2025 if (mips16inst
.ri
.opcode
== MIPS16e_extend_op
) {
2028 __get_user(mips16inst
.full
, pc16
);
2029 } else if (delay_slot(regs
)) {
2030 /* skip jump instructions */
2031 /* JAL/JALX are 32 bits but have OPCODE in first short int */
2032 if (mips16inst
.ri
.opcode
== MIPS16e_jal_op
)
2035 if (get_user(mips16inst
.full
, pc16
))
2039 opcode
= mips16inst
.ri
.opcode
;
2041 case MIPS16e_i64_op
: /* I64 or RI64 instruction */
2042 switch (mips16inst
.i64
.func
) { /* I64/RI64 func field check */
2043 case MIPS16e_ldpc_func
:
2044 case MIPS16e_ldsp_func
:
2045 reg
= reg16to32
[mips16inst
.ri64
.ry
];
2048 case MIPS16e_sdsp_func
:
2049 reg
= reg16to32
[mips16inst
.ri64
.ry
];
2052 case MIPS16e_sdrasp_func
:
2053 reg
= 29; /* GPRSP */
2059 case MIPS16e_swsp_op
:
2060 reg
= reg16to32
[mips16inst
.ri
.rx
];
2061 if (extended
&& cpu_has_mips16e2
)
2062 switch (mips16inst
.ri
.imm
>> 5) {
2067 opcode
= MIPS16e_sh_op
;
2074 case MIPS16e_lwpc_op
:
2075 reg
= reg16to32
[mips16inst
.ri
.rx
];
2078 case MIPS16e_lwsp_op
:
2079 reg
= reg16to32
[mips16inst
.ri
.rx
];
2080 if (extended
&& cpu_has_mips16e2
)
2081 switch (mips16inst
.ri
.imm
>> 5) {
2086 opcode
= MIPS16e_lh_op
;
2089 opcode
= MIPS16e_lhu_op
;
2097 if (mips16inst
.i8
.func
!= MIPS16e_swrasp_func
)
2099 reg
= 29; /* GPRSP */
2103 reg
= reg16to32
[mips16inst
.rri
.ry
];
2110 case MIPS16e_lbu_op
:
2115 if (!access_ok(VERIFY_READ
, addr
, 2))
2118 LoadHW(addr
, value
, res
);
2121 MIPS16e_compute_return_epc(regs
, &oldinst
);
2122 regs
->regs
[reg
] = value
;
2125 case MIPS16e_lhu_op
:
2126 if (!access_ok(VERIFY_READ
, addr
, 2))
2129 LoadHWU(addr
, value
, res
);
2132 MIPS16e_compute_return_epc(regs
, &oldinst
);
2133 regs
->regs
[reg
] = value
;
2137 case MIPS16e_lwpc_op
:
2138 case MIPS16e_lwsp_op
:
2139 if (!access_ok(VERIFY_READ
, addr
, 4))
2142 LoadW(addr
, value
, res
);
2145 MIPS16e_compute_return_epc(regs
, &oldinst
);
2146 regs
->regs
[reg
] = value
;
2149 case MIPS16e_lwu_op
:
2152 * A 32-bit kernel might be running on a 64-bit processor. But
2153 * if we're on a 32-bit processor and an i-cache incoherency
2154 * or race makes us see a 64-bit instruction here the sdl/sdr
2155 * would blow up, so for now we don't handle unaligned 64-bit
2156 * instructions on 32-bit kernels.
2158 if (!access_ok(VERIFY_READ
, addr
, 4))
2161 LoadWU(addr
, value
, res
);
2164 MIPS16e_compute_return_epc(regs
, &oldinst
);
2165 regs
->regs
[reg
] = value
;
2167 #endif /* CONFIG_64BIT */
2169 /* Cannot handle 64-bit instructions in 32-bit kernel */
2176 * A 32-bit kernel might be running on a 64-bit processor. But
2177 * if we're on a 32-bit processor and an i-cache incoherency
2178 * or race makes us see a 64-bit instruction here the sdl/sdr
2179 * would blow up, so for now we don't handle unaligned 64-bit
2180 * instructions on 32-bit kernels.
2182 if (!access_ok(VERIFY_READ
, addr
, 8))
2185 LoadDW(addr
, value
, res
);
2188 MIPS16e_compute_return_epc(regs
, &oldinst
);
2189 regs
->regs
[reg
] = value
;
2191 #endif /* CONFIG_64BIT */
2193 /* Cannot handle 64-bit instructions in 32-bit kernel */
2197 if (!access_ok(VERIFY_WRITE
, addr
, 2))
2200 MIPS16e_compute_return_epc(regs
, &oldinst
);
2201 value
= regs
->regs
[reg
];
2202 StoreHW(addr
, value
, res
);
2208 case MIPS16e_swsp_op
:
2209 case MIPS16e_i8_op
: /* actually - MIPS16e_swrasp_func */
2210 if (!access_ok(VERIFY_WRITE
, addr
, 4))
2213 MIPS16e_compute_return_epc(regs
, &oldinst
);
2214 value
= regs
->regs
[reg
];
2215 StoreW(addr
, value
, res
);
2224 * A 32-bit kernel might be running on a 64-bit processor. But
2225 * if we're on a 32-bit processor and an i-cache incoherency
2226 * or race makes us see a 64-bit instruction here the sdl/sdr
2227 * would blow up, so for now we don't handle unaligned 64-bit
2228 * instructions on 32-bit kernels.
2230 if (!access_ok(VERIFY_WRITE
, addr
, 8))
2233 MIPS16e_compute_return_epc(regs
, &oldinst
);
2234 value
= regs
->regs
[reg
];
2235 StoreDW(addr
, value
, res
);
2239 #endif /* CONFIG_64BIT */
2241 /* Cannot handle 64-bit instructions in 32-bit kernel */
2246 * Pheeee... We encountered an yet unknown instruction or
2247 * cache coherence problem. Die sucker, die ...
2252 #ifdef CONFIG_DEBUG_FS
2253 unaligned_instructions
++;
2259 /* roll back jump/branch */
2260 regs
->cp0_epc
= origpc
;
2261 regs
->regs
[31] = orig31
;
2262 /* Did we have an exception handler installed? */
2263 if (fixup_exception(regs
))
2266 die_if_kernel("Unhandled kernel unaligned access", regs
);
2267 force_sig(SIGSEGV
, current
);
2272 die_if_kernel("Unhandled kernel unaligned access", regs
);
2273 force_sig(SIGBUS
, current
);
2279 ("Unhandled kernel unaligned access or invalid instruction", regs
);
2280 force_sig(SIGILL
, current
);
2283 asmlinkage
void do_ade(struct pt_regs
*regs
)
2285 enum ctx_state prev_state
;
2286 unsigned int __user
*pc
;
2289 prev_state
= exception_enter();
2290 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
,
2291 1, regs
, regs
->cp0_badvaddr
);
2293 * Did we catch a fault trying to load an instruction?
2295 if (regs
->cp0_badvaddr
== regs
->cp0_epc
)
2298 if (user_mode(regs
) && !test_thread_flag(TIF_FIXADE
))
2300 if (unaligned_action
== UNALIGNED_ACTION_SIGNAL
)
2304 * Do branch emulation only if we didn't forward the exception.
2305 * This is all so but ugly ...
2309 * Are we running in microMIPS mode?
2311 if (get_isa16_mode(regs
->cp0_epc
)) {
2313 * Did we catch a fault trying to load an instruction in
2316 if (regs
->cp0_badvaddr
== msk_isa16_mode(regs
->cp0_epc
))
2318 if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
2319 show_registers(regs
);
2321 if (cpu_has_mmips
) {
2323 if (!user_mode(regs
))
2325 emulate_load_store_microMIPS(regs
,
2326 (void __user
*)regs
->cp0_badvaddr
);
2332 if (cpu_has_mips16
) {
2334 if (!user_mode(regs
))
2336 emulate_load_store_MIPS16e(regs
,
2337 (void __user
*)regs
->cp0_badvaddr
);
2346 if (unaligned_action
== UNALIGNED_ACTION_SHOW
)
2347 show_registers(regs
);
2348 pc
= (unsigned int __user
*)exception_epc(regs
);
2351 if (!user_mode(regs
))
2353 emulate_load_store_insn(regs
, (void __user
*)regs
->cp0_badvaddr
, pc
);
2359 die_if_kernel("Kernel unaligned instruction access", regs
);
2360 force_sig(SIGBUS
, current
);
2363 * XXX On return from the signal handler we should advance the epc
2365 exception_exit(prev_state
);
2368 #ifdef CONFIG_DEBUG_FS
2369 static int __init
debugfs_unaligned(void)
2373 if (!mips_debugfs_dir
)
2375 d
= debugfs_create_u32("unaligned_instructions", S_IRUGO
,
2376 mips_debugfs_dir
, &unaligned_instructions
);
2379 d
= debugfs_create_u32("unaligned_action", S_IRUGO
| S_IWUSR
,
2380 mips_debugfs_dir
, &unaligned_action
);
2385 arch_initcall(debugfs_unaligned
);