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>
89 #include <asm/fpu_emulator.h>
91 #include <asm/uaccess.h>
93 #define STR(x) __STR(x)
97 UNALIGNED_ACTION_QUIET,
98 UNALIGNED_ACTION_SIGNAL,
99 UNALIGNED_ACTION_SHOW,
101 #ifdef CONFIG_DEBUG_FS
102 static u32 unaligned_instructions;
103 static u32 unaligned_action;
105 #define unaligned_action UNALIGNED_ACTION_QUIET
107 extern void show_registers(struct pt_regs *regs);
110 #define _LoadHW(addr, value, res, type) \
112 __asm__ __volatile__ (".set\tnoat\n" \
113 "1:\t"type##_lb("%0", "0(%2)")"\n" \
114 "2:\t"type##_lbu("$1", "1(%2)")"\n\t"\
120 ".section\t.fixup,\"ax\"\n\t" \
121 "4:\tli\t%1, %3\n\t" \
124 ".section\t__ex_table,\"a\"\n\t" \
125 STR(PTR)"\t1b, 4b\n\t" \
126 STR(PTR)"\t2b, 4b\n\t" \
128 : "=&r" (value), "=r" (res) \
129 : "r" (addr), "i" (-EFAULT)); \
132 #ifndef CONFIG_CPU_MIPSR6
133 #define _LoadW(addr, value, res, type) \
135 __asm__ __volatile__ ( \
136 "1:\t"type##_lwl("%0", "(%2)")"\n" \
137 "2:\t"type##_lwr("%0", "3(%2)")"\n\t"\
141 ".section\t.fixup,\"ax\"\n\t" \
142 "4:\tli\t%1, %3\n\t" \
145 ".section\t__ex_table,\"a\"\n\t" \
146 STR(PTR)"\t1b, 4b\n\t" \
147 STR(PTR)"\t2b, 4b\n\t" \
149 : "=&r" (value), "=r" (res) \
150 : "r" (addr), "i" (-EFAULT)); \
154 /* MIPSR6 has no lwl instruction */
155 #define _LoadW(addr, value, res, type) \
157 __asm__ __volatile__ ( \
160 "1:"type##_lb("%0", "0(%2)")"\n\t" \
161 "2:"type##_lbu("$1", "1(%2)")"\n\t" \
164 "3:"type##_lbu("$1", "2(%2)")"\n\t" \
167 "4:"type##_lbu("$1", "3(%2)")"\n\t" \
174 ".section\t.fixup,\"ax\"\n\t" \
175 "11:\tli\t%1, %3\n\t" \
178 ".section\t__ex_table,\"a\"\n\t" \
179 STR(PTR)"\t1b, 11b\n\t" \
180 STR(PTR)"\t2b, 11b\n\t" \
181 STR(PTR)"\t3b, 11b\n\t" \
182 STR(PTR)"\t4b, 11b\n\t" \
184 : "=&r" (value), "=r" (res) \
185 : "r" (addr), "i" (-EFAULT)); \
188 #endif /* CONFIG_CPU_MIPSR6 */
190 #define _LoadHWU(addr, value, res, type) \
192 __asm__ __volatile__ ( \
194 "1:\t"type##_lbu("%0", "0(%2)")"\n" \
195 "2:\t"type##_lbu("$1", "1(%2)")"\n\t"\
202 ".section\t.fixup,\"ax\"\n\t" \
203 "4:\tli\t%1, %3\n\t" \
206 ".section\t__ex_table,\"a\"\n\t" \
207 STR(PTR)"\t1b, 4b\n\t" \
208 STR(PTR)"\t2b, 4b\n\t" \
210 : "=&r" (value), "=r" (res) \
211 : "r" (addr), "i" (-EFAULT)); \
214 #ifndef CONFIG_CPU_MIPSR6
215 #define _LoadWU(addr, value, res, type) \
217 __asm__ __volatile__ ( \
218 "1:\t"type##_lwl("%0", "(%2)")"\n" \
219 "2:\t"type##_lwr("%0", "3(%2)")"\n\t"\
220 "dsll\t%0, %0, 32\n\t" \
221 "dsrl\t%0, %0, 32\n\t" \
225 "\t.section\t.fixup,\"ax\"\n\t" \
226 "4:\tli\t%1, %3\n\t" \
229 ".section\t__ex_table,\"a\"\n\t" \
230 STR(PTR)"\t1b, 4b\n\t" \
231 STR(PTR)"\t2b, 4b\n\t" \
233 : "=&r" (value), "=r" (res) \
234 : "r" (addr), "i" (-EFAULT)); \
237 #define _LoadDW(addr, value, res) \
239 __asm__ __volatile__ ( \
240 "1:\tldl\t%0, (%2)\n" \
241 "2:\tldr\t%0, 7(%2)\n\t" \
245 "\t.section\t.fixup,\"ax\"\n\t" \
246 "4:\tli\t%1, %3\n\t" \
249 ".section\t__ex_table,\"a\"\n\t" \
250 STR(PTR)"\t1b, 4b\n\t" \
251 STR(PTR)"\t2b, 4b\n\t" \
253 : "=&r" (value), "=r" (res) \
254 : "r" (addr), "i" (-EFAULT)); \
258 /* MIPSR6 has not lwl and ldl instructions */
259 #define _LoadWU(addr, value, res, type) \
261 __asm__ __volatile__ ( \
264 "1:"type##_lbu("%0", "0(%2)")"\n\t" \
265 "2:"type##_lbu("$1", "1(%2)")"\n\t" \
268 "3:"type##_lbu("$1", "2(%2)")"\n\t" \
271 "4:"type##_lbu("$1", "3(%2)")"\n\t" \
278 ".section\t.fixup,\"ax\"\n\t" \
279 "11:\tli\t%1, %3\n\t" \
282 ".section\t__ex_table,\"a\"\n\t" \
283 STR(PTR)"\t1b, 11b\n\t" \
284 STR(PTR)"\t2b, 11b\n\t" \
285 STR(PTR)"\t3b, 11b\n\t" \
286 STR(PTR)"\t4b, 11b\n\t" \
288 : "=&r" (value), "=r" (res) \
289 : "r" (addr), "i" (-EFAULT)); \
292 #define _LoadDW(addr, value, res) \
294 __asm__ __volatile__ ( \
297 "1:lb\t%0, 0(%2)\n\t" \
298 "2:lbu\t $1, 1(%2)\n\t" \
299 "dsll\t%0, 0x8\n\t" \
301 "3:lbu\t$1, 2(%2)\n\t" \
302 "dsll\t%0, 0x8\n\t" \
304 "4:lbu\t$1, 3(%2)\n\t" \
305 "dsll\t%0, 0x8\n\t" \
307 "5:lbu\t$1, 4(%2)\n\t" \
308 "dsll\t%0, 0x8\n\t" \
310 "6:lbu\t$1, 5(%2)\n\t" \
311 "dsll\t%0, 0x8\n\t" \
313 "7:lbu\t$1, 6(%2)\n\t" \
314 "dsll\t%0, 0x8\n\t" \
316 "8:lbu\t$1, 7(%2)\n\t" \
317 "dsll\t%0, 0x8\n\t" \
323 ".section\t.fixup,\"ax\"\n\t" \
324 "11:\tli\t%1, %3\n\t" \
327 ".section\t__ex_table,\"a\"\n\t" \
328 STR(PTR)"\t1b, 11b\n\t" \
329 STR(PTR)"\t2b, 11b\n\t" \
330 STR(PTR)"\t3b, 11b\n\t" \
331 STR(PTR)"\t4b, 11b\n\t" \
332 STR(PTR)"\t5b, 11b\n\t" \
333 STR(PTR)"\t6b, 11b\n\t" \
334 STR(PTR)"\t7b, 11b\n\t" \
335 STR(PTR)"\t8b, 11b\n\t" \
337 : "=&r" (value), "=r" (res) \
338 : "r" (addr), "i" (-EFAULT)); \
341 #endif /* CONFIG_CPU_MIPSR6 */
344 #define _StoreHW(addr, value, res, type) \
346 __asm__ __volatile__ ( \
348 "1:\t"type##_sb("%1", "1(%2)")"\n" \
349 "srl\t$1, %1, 0x8\n" \
350 "2:\t"type##_sb("$1", "0(%2)")"\n" \
355 ".section\t.fixup,\"ax\"\n\t" \
356 "4:\tli\t%0, %3\n\t" \
359 ".section\t__ex_table,\"a\"\n\t" \
360 STR(PTR)"\t1b, 4b\n\t" \
361 STR(PTR)"\t2b, 4b\n\t" \
364 : "r" (value), "r" (addr), "i" (-EFAULT));\
367 #ifndef CONFIG_CPU_MIPSR6
368 #define _StoreW(addr, value, res, type) \
370 __asm__ __volatile__ ( \
371 "1:\t"type##_swl("%1", "(%2)")"\n" \
372 "2:\t"type##_swr("%1", "3(%2)")"\n\t"\
376 ".section\t.fixup,\"ax\"\n\t" \
377 "4:\tli\t%0, %3\n\t" \
380 ".section\t__ex_table,\"a\"\n\t" \
381 STR(PTR)"\t1b, 4b\n\t" \
382 STR(PTR)"\t2b, 4b\n\t" \
385 : "r" (value), "r" (addr), "i" (-EFAULT)); \
388 #define _StoreDW(addr, value, res) \
390 __asm__ __volatile__ ( \
391 "1:\tsdl\t%1,(%2)\n" \
392 "2:\tsdr\t%1, 7(%2)\n\t" \
396 ".section\t.fixup,\"ax\"\n\t" \
397 "4:\tli\t%0, %3\n\t" \
400 ".section\t__ex_table,\"a\"\n\t" \
401 STR(PTR)"\t1b, 4b\n\t" \
402 STR(PTR)"\t2b, 4b\n\t" \
405 : "r" (value), "r" (addr), "i" (-EFAULT)); \
409 /* MIPSR6 has no swl and sdl instructions */
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_MIPSR6 */
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 #ifndef CONFIG_CPU_MIPSR6
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)); \
533 /* MIPSR6 has no 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_MIPSR6 */
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 #ifndef CONFIG_CPU_MIPSR6
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)); \
638 /* MIPSR6 has not 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_MIPSR6 */
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 #ifndef CONFIG_CPU_MIPSR6
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)); \
787 /* MIPSR6 has no 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_MIPSR6 */
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;
888 unsigned long origpc;
889 unsigned long orig31;
890 void __user *fault_addr = NULL;
894 origpc = (unsigned long)pc;
895 orig31 = regs->regs[31];
897 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
900 * This load never faults.
902 __get_user(insn.word, pc);
904 switch (insn.i_format.opcode) {
906 * These are instructions that a compiler doesn't generate. We
907 * can assume therefore that the code is MIPS-aware and
908 * really buggy. Emulating these instructions would break the
917 * For these instructions the only way to create an address
918 * error is an attempted access to kernel/supervisor address
935 * The remaining opcodes are the ones that are really of
941 * we can land here only from kernel accessing user memory,
942 * so we need to "switch" the address limit to user space, so
943 * address check can work properly.
947 switch (insn.spec3_format.func) {
949 if (!access_ok(VERIFY_READ, addr, 2)) {
953 LoadHWE(addr, value, res);
958 compute_return_epc(regs);
959 regs->regs[insn.spec3_format.rt] = value;
962 if (!access_ok(VERIFY_READ, addr, 4)) {
966 LoadWE(addr, value, res);
971 compute_return_epc(regs);
972 regs->regs[insn.spec3_format.rt] = value;
975 if (!access_ok(VERIFY_READ, addr, 2)) {
979 LoadHWUE(addr, value, res);
984 compute_return_epc(regs);
985 regs->regs[insn.spec3_format.rt] = value;
988 if (!access_ok(VERIFY_WRITE, addr, 2)) {
992 compute_return_epc(regs);
993 value = regs->regs[insn.spec3_format.rt];
994 StoreHWE(addr, value, res);
1001 if (!access_ok(VERIFY_WRITE, addr, 4)) {
1005 compute_return_epc(regs);
1006 value = regs->regs[insn.spec3_format.rt];
1007 StoreWE(addr, value, res);
1021 if (!access_ok(VERIFY_READ, addr, 2))
1024 if (config_enabled(CONFIG_EVA)) {
1025 if (segment_eq(get_fs(), get_ds()))
1026 LoadHW(addr, value, res);
1028 LoadHWE(addr, value, res);
1030 LoadHW(addr, value, res);
1035 compute_return_epc(regs);
1036 regs->regs[insn.i_format.rt] = value;
1040 if (!access_ok(VERIFY_READ, addr, 4))
1043 if (config_enabled(CONFIG_EVA)) {
1044 if (segment_eq(get_fs(), get_ds()))
1045 LoadW(addr, value, res);
1047 LoadWE(addr, value, res);
1049 LoadW(addr, value, res);
1054 compute_return_epc(regs);
1055 regs->regs[insn.i_format.rt] = value;
1059 if (!access_ok(VERIFY_READ, addr, 2))
1062 if (config_enabled(CONFIG_EVA)) {
1063 if (segment_eq(get_fs(), get_ds()))
1064 LoadHWU(addr, value, res);
1066 LoadHWUE(addr, value, res);
1068 LoadHWU(addr, value, res);
1073 compute_return_epc(regs);
1074 regs->regs[insn.i_format.rt] = value;
1080 * A 32-bit kernel might be running on a 64-bit processor. But
1081 * if we're on a 32-bit processor and an i-cache incoherency
1082 * or race makes us see a 64-bit instruction here the sdl/sdr
1083 * would blow up, so for now we don't handle unaligned 64-bit
1084 * instructions on 32-bit kernels.
1086 if (!access_ok(VERIFY_READ, addr, 4))
1089 LoadWU(addr, value, res);
1092 compute_return_epc(regs);
1093 regs->regs[insn.i_format.rt] = value;
1095 #endif /* CONFIG_64BIT */
1097 /* Cannot handle 64-bit instructions in 32-bit kernel */
1103 * A 32-bit kernel might be running on a 64-bit processor. But
1104 * if we're on a 32-bit processor and an i-cache incoherency
1105 * or race makes us see a 64-bit instruction here the sdl/sdr
1106 * would blow up, so for now we don't handle unaligned 64-bit
1107 * instructions on 32-bit kernels.
1109 if (!access_ok(VERIFY_READ, addr, 8))
1112 LoadDW(addr, value, res);
1115 compute_return_epc(regs);
1116 regs->regs[insn.i_format.rt] = value;
1118 #endif /* CONFIG_64BIT */
1120 /* Cannot handle 64-bit instructions in 32-bit kernel */
1124 if (!access_ok(VERIFY_WRITE, addr, 2))
1127 compute_return_epc(regs);
1128 value = regs->regs[insn.i_format.rt];
1130 if (config_enabled(CONFIG_EVA)) {
1131 if (segment_eq(get_fs(), get_ds()))
1132 StoreHW(addr, value, res);
1134 StoreHWE(addr, value, res);
1136 StoreHW(addr, value, res);
1144 if (!access_ok(VERIFY_WRITE, addr, 4))
1147 compute_return_epc(regs);
1148 value = regs->regs[insn.i_format.rt];
1150 if (config_enabled(CONFIG_EVA)) {
1151 if (segment_eq(get_fs(), get_ds()))
1152 StoreW(addr, value, res);
1154 StoreWE(addr, value, res);
1156 StoreW(addr, value, res);
1166 * A 32-bit kernel might be running on a 64-bit processor. But
1167 * if we're on a 32-bit processor and an i-cache incoherency
1168 * or race makes us see a 64-bit instruction here the sdl/sdr
1169 * would blow up, so for now we don't handle unaligned 64-bit
1170 * instructions on 32-bit kernels.
1172 if (!access_ok(VERIFY_WRITE, addr, 8))
1175 compute_return_epc(regs);
1176 value = regs->regs[insn.i_format.rt];
1177 StoreDW(addr, value, res);
1181 #endif /* CONFIG_64BIT */
1183 /* Cannot handle 64-bit instructions in 32-bit kernel */
1190 die_if_kernel("Unaligned FP access in kernel code", regs);
1191 BUG_ON(!used_math());
1193 lose_fpu(1); /* Save FPU state for the emulator. */
1194 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1196 own_fpu(1); /* Restore FPU state. */
1198 /* Signal if something went wrong. */
1199 process_fpemu_return(res, fault_addr, 0);
1205 #ifndef CONFIG_CPU_MIPSR6
1207 * COP2 is available to implementor for application specific use.
1208 * It's up to applications to register a notifier chain and do
1209 * whatever they have to do, including possible sending of signals.
1211 * This instruction has been reallocated in Release 6
1214 cu2_notifier_call_chain(CU2_LWC2_OP, regs);
1218 cu2_notifier_call_chain(CU2_LDC2_OP, regs);
1222 cu2_notifier_call_chain(CU2_SWC2_OP, regs);
1226 cu2_notifier_call_chain(CU2_SDC2_OP, regs);
1231 * Pheeee... We encountered an yet unknown instruction or
1232 * cache coherence problem. Die sucker, die ...
1237 #ifdef CONFIG_DEBUG_FS
1238 unaligned_instructions++;
1244 /* roll back jump/branch */
1245 regs->cp0_epc = origpc;
1246 regs->regs[31] = orig31;
1247 /* Did we have an exception handler installed? */
1248 if (fixup_exception(regs))
1251 die_if_kernel("Unhandled kernel unaligned access", regs);
1252 force_sig(SIGSEGV, current);
1257 die_if_kernel("Unhandled kernel unaligned access", regs);
1258 force_sig(SIGBUS, current);
1264 ("Unhandled kernel unaligned access or invalid instruction", regs);
1265 force_sig(SIGILL, current);
1268 /* Recode table from 16-bit register notation to 32-bit GPR. */
1269 const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
1271 /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
1272 const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
1274 static void emulate_load_store_microMIPS(struct pt_regs *regs,
1277 unsigned long value;
1280 unsigned int reg = 0, rvar;
1281 unsigned long orig31;
1285 unsigned long origpc, contpc;
1286 union mips_instruction insn;
1287 struct mm_decoded_insn mminsn;
1288 void __user *fault_addr = NULL;
1290 origpc = regs->cp0_epc;
1291 orig31 = regs->regs[31];
1293 mminsn.micro_mips_mode = 1;
1296 * This load never faults.
1298 pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
1299 __get_user(halfword, pc16);
1301 contpc = regs->cp0_epc + 2;
1302 word = ((unsigned int)halfword << 16);
1305 if (!mm_insn_16bit(halfword)) {
1306 __get_user(halfword, pc16);
1308 contpc = regs->cp0_epc + 4;
1314 if (get_user(halfword, pc16))
1316 mminsn.next_pc_inc = 2;
1317 word = ((unsigned int)halfword << 16);
1319 if (!mm_insn_16bit(halfword)) {
1321 if (get_user(halfword, pc16))
1323 mminsn.next_pc_inc = 4;
1326 mminsn.next_insn = word;
1328 insn = (union mips_instruction)(mminsn.insn);
1329 if (mm_isBranchInstr(regs, mminsn, &contpc))
1330 insn = (union mips_instruction)(mminsn.next_insn);
1332 /* Parse instruction to find what to do */
1334 switch (insn.mm_i_format.opcode) {
1337 switch (insn.mm_x_format.func) {
1339 reg = insn.mm_x_format.rd;
1346 switch (insn.mm_m_format.func) {
1348 reg = insn.mm_m_format.rd;
1352 if (!access_ok(VERIFY_READ, addr, 8))
1355 LoadW(addr, value, res);
1358 regs->regs[reg] = value;
1360 LoadW(addr, value, res);
1363 regs->regs[reg + 1] = value;
1367 reg = insn.mm_m_format.rd;
1371 if (!access_ok(VERIFY_WRITE, addr, 8))
1374 value = regs->regs[reg];
1375 StoreW(addr, value, res);
1379 value = regs->regs[reg + 1];
1380 StoreW(addr, value, res);
1387 reg = insn.mm_m_format.rd;
1391 if (!access_ok(VERIFY_READ, addr, 16))
1394 LoadDW(addr, value, res);
1397 regs->regs[reg] = value;
1399 LoadDW(addr, value, res);
1402 regs->regs[reg + 1] = value;
1404 #endif /* CONFIG_64BIT */
1410 reg = insn.mm_m_format.rd;
1414 if (!access_ok(VERIFY_WRITE, addr, 16))
1417 value = regs->regs[reg];
1418 StoreDW(addr, value, res);
1422 value = regs->regs[reg + 1];
1423 StoreDW(addr, value, res);
1427 #endif /* CONFIG_64BIT */
1432 reg = insn.mm_m_format.rd;
1434 if ((rvar > 9) || !reg)
1438 (VERIFY_READ, addr, 4 * (rvar + 1)))
1441 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1446 for (i = 16; rvar; rvar--, i++) {
1447 LoadW(addr, value, res);
1451 regs->regs[i] = value;
1453 if ((reg & 0xf) == 9) {
1454 LoadW(addr, value, res);
1458 regs->regs[30] = value;
1461 LoadW(addr, value, res);
1464 regs->regs[31] = value;
1469 reg = insn.mm_m_format.rd;
1471 if ((rvar > 9) || !reg)
1475 (VERIFY_WRITE, addr, 4 * (rvar + 1)))
1478 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1483 for (i = 16; rvar; rvar--, i++) {
1484 value = regs->regs[i];
1485 StoreW(addr, value, res);
1490 if ((reg & 0xf) == 9) {
1491 value = regs->regs[30];
1492 StoreW(addr, value, res);
1498 value = regs->regs[31];
1499 StoreW(addr, value, res);
1507 reg = insn.mm_m_format.rd;
1509 if ((rvar > 9) || !reg)
1513 (VERIFY_READ, addr, 8 * (rvar + 1)))
1516 if (!access_ok(VERIFY_READ, addr, 8 * rvar))
1522 for (i = 16; rvar; rvar--, i++) {
1523 LoadDW(addr, value, res);
1527 regs->regs[i] = value;
1529 if ((reg & 0xf) == 9) {
1530 LoadDW(addr, value, res);
1534 regs->regs[30] = value;
1537 LoadDW(addr, value, res);
1540 regs->regs[31] = value;
1543 #endif /* CONFIG_64BIT */
1549 reg = insn.mm_m_format.rd;
1551 if ((rvar > 9) || !reg)
1555 (VERIFY_WRITE, addr, 8 * (rvar + 1)))
1558 if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
1564 for (i = 16; rvar; rvar--, i++) {
1565 value = regs->regs[i];
1566 StoreDW(addr, value, res);
1571 if ((reg & 0xf) == 9) {
1572 value = regs->regs[30];
1573 StoreDW(addr, value, res);
1579 value = regs->regs[31];
1580 StoreDW(addr, value, res);
1585 #endif /* CONFIG_64BIT */
1589 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1595 switch (insn.mm_m_format.func) {
1597 reg = insn.mm_m_format.rd;
1601 /* LL,SC,LLD,SCD are not serviced */
1605 switch (insn.mm_x_format.func) {
1620 /* roll back jump/branch */
1621 regs->cp0_epc = origpc;
1622 regs->regs[31] = orig31;
1624 die_if_kernel("Unaligned FP access in kernel code", regs);
1625 BUG_ON(!used_math());
1626 BUG_ON(!is_fpu_owner());
1628 lose_fpu(1); /* save the FPU state for the emulator */
1629 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1631 own_fpu(1); /* restore FPU state */
1633 /* If something went wrong, signal */
1634 process_fpemu_return(res, fault_addr, 0);
1641 reg = insn.mm_i_format.rt;
1645 reg = insn.mm_i_format.rt;
1649 reg = insn.mm_i_format.rt;
1653 reg = insn.mm_i_format.rt;
1657 reg = insn.mm_i_format.rt;
1661 reg = insn.mm_i_format.rt;
1665 reg = insn.mm_i_format.rt;
1669 switch (insn.mm16_m_format.func) {
1671 reg = insn.mm16_m_format.rlist;
1673 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1676 for (i = 16; rvar; rvar--, i++) {
1677 LoadW(addr, value, res);
1681 regs->regs[i] = value;
1683 LoadW(addr, value, res);
1686 regs->regs[31] = value;
1691 reg = insn.mm16_m_format.rlist;
1693 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1696 for (i = 16; rvar; rvar--, i++) {
1697 value = regs->regs[i];
1698 StoreW(addr, value, res);
1703 value = regs->regs[31];
1704 StoreW(addr, value, res);
1715 reg = reg16to32[insn.mm16_rb_format.rt];
1719 reg = reg16to32[insn.mm16_rb_format.rt];
1723 reg = reg16to32st[insn.mm16_rb_format.rt];
1727 reg = reg16to32st[insn.mm16_rb_format.rt];
1731 reg = insn.mm16_r5_format.rt;
1735 reg = insn.mm16_r5_format.rt;
1739 reg = reg16to32[insn.mm16_r3_format.rt];
1747 if (!access_ok(VERIFY_READ, addr, 2))
1750 LoadHW(addr, value, res);
1753 regs->regs[reg] = value;
1757 if (!access_ok(VERIFY_READ, addr, 2))
1760 LoadHWU(addr, value, res);
1763 regs->regs[reg] = value;
1767 if (!access_ok(VERIFY_READ, addr, 4))
1770 LoadW(addr, value, res);
1773 regs->regs[reg] = value;
1779 * A 32-bit kernel might be running on a 64-bit processor. But
1780 * if we're on a 32-bit processor and an i-cache incoherency
1781 * or race makes us see a 64-bit instruction here the sdl/sdr
1782 * would blow up, so for now we don't handle unaligned 64-bit
1783 * instructions on 32-bit kernels.
1785 if (!access_ok(VERIFY_READ, addr, 4))
1788 LoadWU(addr, value, res);
1791 regs->regs[reg] = value;
1793 #endif /* CONFIG_64BIT */
1795 /* Cannot handle 64-bit instructions in 32-bit kernel */
1801 * A 32-bit kernel might be running on a 64-bit processor. But
1802 * if we're on a 32-bit processor and an i-cache incoherency
1803 * or race makes us see a 64-bit instruction here the sdl/sdr
1804 * would blow up, so for now we don't handle unaligned 64-bit
1805 * instructions on 32-bit kernels.
1807 if (!access_ok(VERIFY_READ, addr, 8))
1810 LoadDW(addr, value, res);
1813 regs->regs[reg] = value;
1815 #endif /* CONFIG_64BIT */
1817 /* Cannot handle 64-bit instructions in 32-bit kernel */
1821 if (!access_ok(VERIFY_WRITE, addr, 2))
1824 value = regs->regs[reg];
1825 StoreHW(addr, value, res);
1831 if (!access_ok(VERIFY_WRITE, addr, 4))
1834 value = regs->regs[reg];
1835 StoreW(addr, value, res);
1843 * A 32-bit kernel might be running on a 64-bit processor. But
1844 * if we're on a 32-bit processor and an i-cache incoherency
1845 * or race makes us see a 64-bit instruction here the sdl/sdr
1846 * would blow up, so for now we don't handle unaligned 64-bit
1847 * instructions on 32-bit kernels.
1849 if (!access_ok(VERIFY_WRITE, addr, 8))
1852 value = regs->regs[reg];
1853 StoreDW(addr, value, res);
1857 #endif /* CONFIG_64BIT */
1859 /* Cannot handle 64-bit instructions in 32-bit kernel */
1863 regs->cp0_epc = contpc; /* advance or branch */
1865 #ifdef CONFIG_DEBUG_FS
1866 unaligned_instructions++;
1871 /* roll back jump/branch */
1872 regs->cp0_epc = origpc;
1873 regs->regs[31] = orig31;
1874 /* Did we have an exception handler installed? */
1875 if (fixup_exception(regs))
1878 die_if_kernel("Unhandled kernel unaligned access", regs);
1879 force_sig(SIGSEGV, current);
1884 die_if_kernel("Unhandled kernel unaligned access", regs);
1885 force_sig(SIGBUS, current);
1891 ("Unhandled kernel unaligned access or invalid instruction", regs);
1892 force_sig(SIGILL, current);
1895 static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
1897 unsigned long value;
1900 unsigned long orig31;
1902 unsigned long origpc;
1903 union mips16e_instruction mips16inst, oldinst;
1905 origpc = regs->cp0_epc;
1906 orig31 = regs->regs[31];
1907 pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
1909 * This load never faults.
1911 __get_user(mips16inst.full, pc16);
1912 oldinst = mips16inst;
1914 /* skip EXTEND instruction */
1915 if (mips16inst.ri.opcode == MIPS16e_extend_op) {
1917 __get_user(mips16inst.full, pc16);
1918 } else if (delay_slot(regs)) {
1919 /* skip jump instructions */
1920 /* JAL/JALX are 32 bits but have OPCODE in first short int */
1921 if (mips16inst.ri.opcode == MIPS16e_jal_op)
1924 if (get_user(mips16inst.full, pc16))
1928 switch (mips16inst.ri.opcode) {
1929 case MIPS16e_i64_op: /* I64 or RI64 instruction */
1930 switch (mips16inst.i64.func) { /* I64/RI64 func field check */
1931 case MIPS16e_ldpc_func:
1932 case MIPS16e_ldsp_func:
1933 reg = reg16to32[mips16inst.ri64.ry];
1936 case MIPS16e_sdsp_func:
1937 reg = reg16to32[mips16inst.ri64.ry];
1940 case MIPS16e_sdrasp_func:
1941 reg = 29; /* GPRSP */
1947 case MIPS16e_swsp_op:
1948 case MIPS16e_lwpc_op:
1949 case MIPS16e_lwsp_op:
1950 reg = reg16to32[mips16inst.ri.rx];
1954 if (mips16inst.i8.func != MIPS16e_swrasp_func)
1956 reg = 29; /* GPRSP */
1960 reg = reg16to32[mips16inst.rri.ry];
1964 switch (mips16inst.ri.opcode) {
1967 case MIPS16e_lbu_op:
1972 if (!access_ok(VERIFY_READ, addr, 2))
1975 LoadHW(addr, value, res);
1978 MIPS16e_compute_return_epc(regs, &oldinst);
1979 regs->regs[reg] = value;
1982 case MIPS16e_lhu_op:
1983 if (!access_ok(VERIFY_READ, addr, 2))
1986 LoadHWU(addr, value, res);
1989 MIPS16e_compute_return_epc(regs, &oldinst);
1990 regs->regs[reg] = value;
1994 case MIPS16e_lwpc_op:
1995 case MIPS16e_lwsp_op:
1996 if (!access_ok(VERIFY_READ, addr, 4))
1999 LoadW(addr, value, res);
2002 MIPS16e_compute_return_epc(regs, &oldinst);
2003 regs->regs[reg] = value;
2006 case MIPS16e_lwu_op:
2009 * A 32-bit kernel might be running on a 64-bit processor. But
2010 * if we're on a 32-bit processor and an i-cache incoherency
2011 * or race makes us see a 64-bit instruction here the sdl/sdr
2012 * would blow up, so for now we don't handle unaligned 64-bit
2013 * instructions on 32-bit kernels.
2015 if (!access_ok(VERIFY_READ, addr, 4))
2018 LoadWU(addr, value, res);
2021 MIPS16e_compute_return_epc(regs, &oldinst);
2022 regs->regs[reg] = value;
2024 #endif /* CONFIG_64BIT */
2026 /* Cannot handle 64-bit instructions in 32-bit kernel */
2033 * A 32-bit kernel might be running on a 64-bit processor. But
2034 * if we're on a 32-bit processor and an i-cache incoherency
2035 * or race makes us see a 64-bit instruction here the sdl/sdr
2036 * would blow up, so for now we don't handle unaligned 64-bit
2037 * instructions on 32-bit kernels.
2039 if (!access_ok(VERIFY_READ, addr, 8))
2042 LoadDW(addr, value, res);
2045 MIPS16e_compute_return_epc(regs, &oldinst);
2046 regs->regs[reg] = value;
2048 #endif /* CONFIG_64BIT */
2050 /* Cannot handle 64-bit instructions in 32-bit kernel */
2054 if (!access_ok(VERIFY_WRITE, addr, 2))
2057 MIPS16e_compute_return_epc(regs, &oldinst);
2058 value = regs->regs[reg];
2059 StoreHW(addr, value, res);
2065 case MIPS16e_swsp_op:
2066 case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
2067 if (!access_ok(VERIFY_WRITE, addr, 4))
2070 MIPS16e_compute_return_epc(regs, &oldinst);
2071 value = regs->regs[reg];
2072 StoreW(addr, value, res);
2081 * A 32-bit kernel might be running on a 64-bit processor. But
2082 * if we're on a 32-bit processor and an i-cache incoherency
2083 * or race makes us see a 64-bit instruction here the sdl/sdr
2084 * would blow up, so for now we don't handle unaligned 64-bit
2085 * instructions on 32-bit kernels.
2087 if (!access_ok(VERIFY_WRITE, addr, 8))
2090 MIPS16e_compute_return_epc(regs, &oldinst);
2091 value = regs->regs[reg];
2092 StoreDW(addr, value, res);
2096 #endif /* CONFIG_64BIT */
2098 /* Cannot handle 64-bit instructions in 32-bit kernel */
2103 * Pheeee... We encountered an yet unknown instruction or
2104 * cache coherence problem. Die sucker, die ...
2109 #ifdef CONFIG_DEBUG_FS
2110 unaligned_instructions++;
2116 /* roll back jump/branch */
2117 regs->cp0_epc = origpc;
2118 regs->regs[31] = orig31;
2119 /* Did we have an exception handler installed? */
2120 if (fixup_exception(regs))
2123 die_if_kernel("Unhandled kernel unaligned access", regs);
2124 force_sig(SIGSEGV, current);
2129 die_if_kernel("Unhandled kernel unaligned access", regs);
2130 force_sig(SIGBUS, current);
2136 ("Unhandled kernel unaligned access or invalid instruction", regs);
2137 force_sig(SIGILL, current);
2140 asmlinkage void do_ade(struct pt_regs *regs)
2142 enum ctx_state prev_state;
2143 unsigned int __user *pc;
2146 prev_state = exception_enter();
2147 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
2148 1, regs, regs->cp0_badvaddr);
2150 * Did we catch a fault trying to load an instruction?
2152 if (regs->cp0_badvaddr == regs->cp0_epc)
2155 if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
2157 if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
2161 * Do branch emulation only if we didn't forward the exception.
2162 * This is all so but ugly ...
2166 * Are we running in microMIPS mode?
2168 if (get_isa16_mode(regs->cp0_epc)) {
2170 * Did we catch a fault trying to load an instruction in
2173 if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
2175 if (unaligned_action == UNALIGNED_ACTION_SHOW)
2176 show_registers(regs);
2178 if (cpu_has_mmips) {
2180 if (!user_mode(regs))
2182 emulate_load_store_microMIPS(regs,
2183 (void __user *)regs->cp0_badvaddr);
2189 if (cpu_has_mips16) {
2191 if (!user_mode(regs))
2193 emulate_load_store_MIPS16e(regs,
2194 (void __user *)regs->cp0_badvaddr);
2203 if (unaligned_action == UNALIGNED_ACTION_SHOW)
2204 show_registers(regs);
2205 pc = (unsigned int __user *)exception_epc(regs);
2208 if (!user_mode(regs))
2210 emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
2216 die_if_kernel("Kernel unaligned instruction access", regs);
2217 force_sig(SIGBUS, current);
2220 * XXX On return from the signal handler we should advance the epc
2222 exception_exit(prev_state);
2225 #ifdef CONFIG_DEBUG_FS
2226 extern struct dentry *mips_debugfs_dir;
2227 static int __init debugfs_unaligned(void)
2231 if (!mips_debugfs_dir)
2233 d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
2234 mips_debugfs_dir, &unaligned_instructions);
2237 d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
2238 mips_debugfs_dir, &unaligned_action);
2243 __initcall(debugfs_unaligned);