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 #include <asm/fpu_emulator.h>
95 #define STR(x) __STR(x)
99 UNALIGNED_ACTION_QUIET,
100 UNALIGNED_ACTION_SIGNAL,
101 UNALIGNED_ACTION_SHOW,
103 #ifdef CONFIG_DEBUG_FS
104 static u32 unaligned_instructions;
105 static u32 unaligned_action;
107 #define unaligned_action UNALIGNED_ACTION_QUIET
109 extern void show_registers(struct pt_regs *regs);
112 #define _LoadHW(addr, value, res, type) \
114 __asm__ __volatile__ (".set\tnoat\n" \
115 "1:\t"type##_lb("%0", "0(%2)")"\n" \
116 "2:\t"type##_lbu("$1", "1(%2)")"\n\t"\
122 ".section\t.fixup,\"ax\"\n\t" \
123 "4:\tli\t%1, %3\n\t" \
126 ".section\t__ex_table,\"a\"\n\t" \
127 STR(PTR)"\t1b, 4b\n\t" \
128 STR(PTR)"\t2b, 4b\n\t" \
130 : "=&r" (value), "=r" (res) \
131 : "r" (addr), "i" (-EFAULT)); \
134 #ifndef CONFIG_CPU_MIPSR6
135 #define _LoadW(addr, value, res, type) \
137 __asm__ __volatile__ ( \
138 "1:\t"type##_lwl("%0", "(%2)")"\n" \
139 "2:\t"type##_lwr("%0", "3(%2)")"\n\t"\
143 ".section\t.fixup,\"ax\"\n\t" \
144 "4:\tli\t%1, %3\n\t" \
147 ".section\t__ex_table,\"a\"\n\t" \
148 STR(PTR)"\t1b, 4b\n\t" \
149 STR(PTR)"\t2b, 4b\n\t" \
151 : "=&r" (value), "=r" (res) \
152 : "r" (addr), "i" (-EFAULT)); \
156 /* MIPSR6 has no lwl instruction */
157 #define _LoadW(addr, value, res, type) \
159 __asm__ __volatile__ ( \
162 "1:"type##_lb("%0", "0(%2)")"\n\t" \
163 "2:"type##_lbu("$1", "1(%2)")"\n\t" \
166 "3:"type##_lbu("$1", "2(%2)")"\n\t" \
169 "4:"type##_lbu("$1", "3(%2)")"\n\t" \
176 ".section\t.fixup,\"ax\"\n\t" \
177 "11:\tli\t%1, %3\n\t" \
180 ".section\t__ex_table,\"a\"\n\t" \
181 STR(PTR)"\t1b, 11b\n\t" \
182 STR(PTR)"\t2b, 11b\n\t" \
183 STR(PTR)"\t3b, 11b\n\t" \
184 STR(PTR)"\t4b, 11b\n\t" \
186 : "=&r" (value), "=r" (res) \
187 : "r" (addr), "i" (-EFAULT)); \
190 #endif /* CONFIG_CPU_MIPSR6 */
192 #define _LoadHWU(addr, value, res, type) \
194 __asm__ __volatile__ ( \
196 "1:\t"type##_lbu("%0", "0(%2)")"\n" \
197 "2:\t"type##_lbu("$1", "1(%2)")"\n\t"\
204 ".section\t.fixup,\"ax\"\n\t" \
205 "4:\tli\t%1, %3\n\t" \
208 ".section\t__ex_table,\"a\"\n\t" \
209 STR(PTR)"\t1b, 4b\n\t" \
210 STR(PTR)"\t2b, 4b\n\t" \
212 : "=&r" (value), "=r" (res) \
213 : "r" (addr), "i" (-EFAULT)); \
216 #ifndef CONFIG_CPU_MIPSR6
217 #define _LoadWU(addr, value, res, type) \
219 __asm__ __volatile__ ( \
220 "1:\t"type##_lwl("%0", "(%2)")"\n" \
221 "2:\t"type##_lwr("%0", "3(%2)")"\n\t"\
222 "dsll\t%0, %0, 32\n\t" \
223 "dsrl\t%0, %0, 32\n\t" \
227 "\t.section\t.fixup,\"ax\"\n\t" \
228 "4:\tli\t%1, %3\n\t" \
231 ".section\t__ex_table,\"a\"\n\t" \
232 STR(PTR)"\t1b, 4b\n\t" \
233 STR(PTR)"\t2b, 4b\n\t" \
235 : "=&r" (value), "=r" (res) \
236 : "r" (addr), "i" (-EFAULT)); \
239 #define _LoadDW(addr, value, res) \
241 __asm__ __volatile__ ( \
242 "1:\tldl\t%0, (%2)\n" \
243 "2:\tldr\t%0, 7(%2)\n\t" \
247 "\t.section\t.fixup,\"ax\"\n\t" \
248 "4:\tli\t%1, %3\n\t" \
251 ".section\t__ex_table,\"a\"\n\t" \
252 STR(PTR)"\t1b, 4b\n\t" \
253 STR(PTR)"\t2b, 4b\n\t" \
255 : "=&r" (value), "=r" (res) \
256 : "r" (addr), "i" (-EFAULT)); \
260 /* MIPSR6 has not lwl and ldl instructions */
261 #define _LoadWU(addr, value, res, type) \
263 __asm__ __volatile__ ( \
266 "1:"type##_lbu("%0", "0(%2)")"\n\t" \
267 "2:"type##_lbu("$1", "1(%2)")"\n\t" \
270 "3:"type##_lbu("$1", "2(%2)")"\n\t" \
273 "4:"type##_lbu("$1", "3(%2)")"\n\t" \
280 ".section\t.fixup,\"ax\"\n\t" \
281 "11:\tli\t%1, %3\n\t" \
284 ".section\t__ex_table,\"a\"\n\t" \
285 STR(PTR)"\t1b, 11b\n\t" \
286 STR(PTR)"\t2b, 11b\n\t" \
287 STR(PTR)"\t3b, 11b\n\t" \
288 STR(PTR)"\t4b, 11b\n\t" \
290 : "=&r" (value), "=r" (res) \
291 : "r" (addr), "i" (-EFAULT)); \
294 #define _LoadDW(addr, value, res) \
296 __asm__ __volatile__ ( \
299 "1:lb\t%0, 0(%2)\n\t" \
300 "2:lbu\t $1, 1(%2)\n\t" \
301 "dsll\t%0, 0x8\n\t" \
303 "3:lbu\t$1, 2(%2)\n\t" \
304 "dsll\t%0, 0x8\n\t" \
306 "4:lbu\t$1, 3(%2)\n\t" \
307 "dsll\t%0, 0x8\n\t" \
309 "5:lbu\t$1, 4(%2)\n\t" \
310 "dsll\t%0, 0x8\n\t" \
312 "6:lbu\t$1, 5(%2)\n\t" \
313 "dsll\t%0, 0x8\n\t" \
315 "7:lbu\t$1, 6(%2)\n\t" \
316 "dsll\t%0, 0x8\n\t" \
318 "8:lbu\t$1, 7(%2)\n\t" \
319 "dsll\t%0, 0x8\n\t" \
325 ".section\t.fixup,\"ax\"\n\t" \
326 "11:\tli\t%1, %3\n\t" \
329 ".section\t__ex_table,\"a\"\n\t" \
330 STR(PTR)"\t1b, 11b\n\t" \
331 STR(PTR)"\t2b, 11b\n\t" \
332 STR(PTR)"\t3b, 11b\n\t" \
333 STR(PTR)"\t4b, 11b\n\t" \
334 STR(PTR)"\t5b, 11b\n\t" \
335 STR(PTR)"\t6b, 11b\n\t" \
336 STR(PTR)"\t7b, 11b\n\t" \
337 STR(PTR)"\t8b, 11b\n\t" \
339 : "=&r" (value), "=r" (res) \
340 : "r" (addr), "i" (-EFAULT)); \
343 #endif /* CONFIG_CPU_MIPSR6 */
346 #define _StoreHW(addr, value, res, type) \
348 __asm__ __volatile__ ( \
350 "1:\t"type##_sb("%1", "1(%2)")"\n" \
351 "srl\t$1, %1, 0x8\n" \
352 "2:\t"type##_sb("$1", "0(%2)")"\n" \
357 ".section\t.fixup,\"ax\"\n\t" \
358 "4:\tli\t%0, %3\n\t" \
361 ".section\t__ex_table,\"a\"\n\t" \
362 STR(PTR)"\t1b, 4b\n\t" \
363 STR(PTR)"\t2b, 4b\n\t" \
366 : "r" (value), "r" (addr), "i" (-EFAULT));\
369 #ifndef CONFIG_CPU_MIPSR6
370 #define _StoreW(addr, value, res, type) \
372 __asm__ __volatile__ ( \
373 "1:\t"type##_swl("%1", "(%2)")"\n" \
374 "2:\t"type##_swr("%1", "3(%2)")"\n\t"\
378 ".section\t.fixup,\"ax\"\n\t" \
379 "4:\tli\t%0, %3\n\t" \
382 ".section\t__ex_table,\"a\"\n\t" \
383 STR(PTR)"\t1b, 4b\n\t" \
384 STR(PTR)"\t2b, 4b\n\t" \
387 : "r" (value), "r" (addr), "i" (-EFAULT)); \
390 #define _StoreDW(addr, value, res) \
392 __asm__ __volatile__ ( \
393 "1:\tsdl\t%1,(%2)\n" \
394 "2:\tsdr\t%1, 7(%2)\n\t" \
398 ".section\t.fixup,\"ax\"\n\t" \
399 "4:\tli\t%0, %3\n\t" \
402 ".section\t__ex_table,\"a\"\n\t" \
403 STR(PTR)"\t1b, 4b\n\t" \
404 STR(PTR)"\t2b, 4b\n\t" \
407 : "r" (value), "r" (addr), "i" (-EFAULT)); \
411 /* MIPSR6 has no swl and sdl instructions */
412 #define _StoreW(addr, value, res, type) \
414 __asm__ __volatile__ ( \
417 "1:"type##_sb("%1", "3(%2)")"\n\t" \
418 "srl\t$1, %1, 0x8\n\t" \
419 "2:"type##_sb("$1", "2(%2)")"\n\t" \
420 "srl\t$1, $1, 0x8\n\t" \
421 "3:"type##_sb("$1", "1(%2)")"\n\t" \
422 "srl\t$1, $1, 0x8\n\t" \
423 "4:"type##_sb("$1", "0(%2)")"\n\t" \
428 ".section\t.fixup,\"ax\"\n\t" \
429 "11:\tli\t%0, %3\n\t" \
432 ".section\t__ex_table,\"a\"\n\t" \
433 STR(PTR)"\t1b, 11b\n\t" \
434 STR(PTR)"\t2b, 11b\n\t" \
435 STR(PTR)"\t3b, 11b\n\t" \
436 STR(PTR)"\t4b, 11b\n\t" \
439 : "r" (value), "r" (addr), "i" (-EFAULT) \
443 #define StoreDW(addr, value, res) \
445 __asm__ __volatile__ ( \
448 "1:sb\t%1, 7(%2)\n\t" \
449 "dsrl\t$1, %1, 0x8\n\t" \
450 "2:sb\t$1, 6(%2)\n\t" \
451 "dsrl\t$1, $1, 0x8\n\t" \
452 "3:sb\t$1, 5(%2)\n\t" \
453 "dsrl\t$1, $1, 0x8\n\t" \
454 "4:sb\t$1, 4(%2)\n\t" \
455 "dsrl\t$1, $1, 0x8\n\t" \
456 "5:sb\t$1, 3(%2)\n\t" \
457 "dsrl\t$1, $1, 0x8\n\t" \
458 "6:sb\t$1, 2(%2)\n\t" \
459 "dsrl\t$1, $1, 0x8\n\t" \
460 "7:sb\t$1, 1(%2)\n\t" \
461 "dsrl\t$1, $1, 0x8\n\t" \
462 "8:sb\t$1, 0(%2)\n\t" \
463 "dsrl\t$1, $1, 0x8\n\t" \
468 ".section\t.fixup,\"ax\"\n\t" \
469 "11:\tli\t%0, %3\n\t" \
472 ".section\t__ex_table,\"a\"\n\t" \
473 STR(PTR)"\t1b, 11b\n\t" \
474 STR(PTR)"\t2b, 11b\n\t" \
475 STR(PTR)"\t3b, 11b\n\t" \
476 STR(PTR)"\t4b, 11b\n\t" \
477 STR(PTR)"\t5b, 11b\n\t" \
478 STR(PTR)"\t6b, 11b\n\t" \
479 STR(PTR)"\t7b, 11b\n\t" \
480 STR(PTR)"\t8b, 11b\n\t" \
483 : "r" (value), "r" (addr), "i" (-EFAULT) \
487 #endif /* CONFIG_CPU_MIPSR6 */
489 #else /* __BIG_ENDIAN */
491 #define _LoadHW(addr, value, res, type) \
493 __asm__ __volatile__ (".set\tnoat\n" \
494 "1:\t"type##_lb("%0", "1(%2)")"\n" \
495 "2:\t"type##_lbu("$1", "0(%2)")"\n\t"\
501 ".section\t.fixup,\"ax\"\n\t" \
502 "4:\tli\t%1, %3\n\t" \
505 ".section\t__ex_table,\"a\"\n\t" \
506 STR(PTR)"\t1b, 4b\n\t" \
507 STR(PTR)"\t2b, 4b\n\t" \
509 : "=&r" (value), "=r" (res) \
510 : "r" (addr), "i" (-EFAULT)); \
513 #ifndef CONFIG_CPU_MIPSR6
514 #define _LoadW(addr, value, res, type) \
516 __asm__ __volatile__ ( \
517 "1:\t"type##_lwl("%0", "3(%2)")"\n" \
518 "2:\t"type##_lwr("%0", "(%2)")"\n\t"\
522 ".section\t.fixup,\"ax\"\n\t" \
523 "4:\tli\t%1, %3\n\t" \
526 ".section\t__ex_table,\"a\"\n\t" \
527 STR(PTR)"\t1b, 4b\n\t" \
528 STR(PTR)"\t2b, 4b\n\t" \
530 : "=&r" (value), "=r" (res) \
531 : "r" (addr), "i" (-EFAULT)); \
535 /* MIPSR6 has no lwl instruction */
536 #define _LoadW(addr, value, res, type) \
538 __asm__ __volatile__ ( \
541 "1:"type##_lb("%0", "3(%2)")"\n\t" \
542 "2:"type##_lbu("$1", "2(%2)")"\n\t" \
545 "3:"type##_lbu("$1", "1(%2)")"\n\t" \
548 "4:"type##_lbu("$1", "0(%2)")"\n\t" \
555 ".section\t.fixup,\"ax\"\n\t" \
556 "11:\tli\t%1, %3\n\t" \
559 ".section\t__ex_table,\"a\"\n\t" \
560 STR(PTR)"\t1b, 11b\n\t" \
561 STR(PTR)"\t2b, 11b\n\t" \
562 STR(PTR)"\t3b, 11b\n\t" \
563 STR(PTR)"\t4b, 11b\n\t" \
565 : "=&r" (value), "=r" (res) \
566 : "r" (addr), "i" (-EFAULT)); \
569 #endif /* CONFIG_CPU_MIPSR6 */
572 #define _LoadHWU(addr, value, res, type) \
574 __asm__ __volatile__ ( \
576 "1:\t"type##_lbu("%0", "1(%2)")"\n" \
577 "2:\t"type##_lbu("$1", "0(%2)")"\n\t"\
584 ".section\t.fixup,\"ax\"\n\t" \
585 "4:\tli\t%1, %3\n\t" \
588 ".section\t__ex_table,\"a\"\n\t" \
589 STR(PTR)"\t1b, 4b\n\t" \
590 STR(PTR)"\t2b, 4b\n\t" \
592 : "=&r" (value), "=r" (res) \
593 : "r" (addr), "i" (-EFAULT)); \
596 #ifndef CONFIG_CPU_MIPSR6
597 #define _LoadWU(addr, value, res, type) \
599 __asm__ __volatile__ ( \
600 "1:\t"type##_lwl("%0", "3(%2)")"\n" \
601 "2:\t"type##_lwr("%0", "(%2)")"\n\t"\
602 "dsll\t%0, %0, 32\n\t" \
603 "dsrl\t%0, %0, 32\n\t" \
607 "\t.section\t.fixup,\"ax\"\n\t" \
608 "4:\tli\t%1, %3\n\t" \
611 ".section\t__ex_table,\"a\"\n\t" \
612 STR(PTR)"\t1b, 4b\n\t" \
613 STR(PTR)"\t2b, 4b\n\t" \
615 : "=&r" (value), "=r" (res) \
616 : "r" (addr), "i" (-EFAULT)); \
619 #define _LoadDW(addr, value, res) \
621 __asm__ __volatile__ ( \
622 "1:\tldl\t%0, 7(%2)\n" \
623 "2:\tldr\t%0, (%2)\n\t" \
627 "\t.section\t.fixup,\"ax\"\n\t" \
628 "4:\tli\t%1, %3\n\t" \
631 ".section\t__ex_table,\"a\"\n\t" \
632 STR(PTR)"\t1b, 4b\n\t" \
633 STR(PTR)"\t2b, 4b\n\t" \
635 : "=&r" (value), "=r" (res) \
636 : "r" (addr), "i" (-EFAULT)); \
640 /* MIPSR6 has not lwl and ldl instructions */
641 #define _LoadWU(addr, value, res, type) \
643 __asm__ __volatile__ ( \
646 "1:"type##_lbu("%0", "3(%2)")"\n\t" \
647 "2:"type##_lbu("$1", "2(%2)")"\n\t" \
650 "3:"type##_lbu("$1", "1(%2)")"\n\t" \
653 "4:"type##_lbu("$1", "0(%2)")"\n\t" \
660 ".section\t.fixup,\"ax\"\n\t" \
661 "11:\tli\t%1, %3\n\t" \
664 ".section\t__ex_table,\"a\"\n\t" \
665 STR(PTR)"\t1b, 11b\n\t" \
666 STR(PTR)"\t2b, 11b\n\t" \
667 STR(PTR)"\t3b, 11b\n\t" \
668 STR(PTR)"\t4b, 11b\n\t" \
670 : "=&r" (value), "=r" (res) \
671 : "r" (addr), "i" (-EFAULT)); \
674 #define _LoadDW(addr, value, res) \
676 __asm__ __volatile__ ( \
679 "1:lb\t%0, 7(%2)\n\t" \
680 "2:lbu\t$1, 6(%2)\n\t" \
681 "dsll\t%0, 0x8\n\t" \
683 "3:lbu\t$1, 5(%2)\n\t" \
684 "dsll\t%0, 0x8\n\t" \
686 "4:lbu\t$1, 4(%2)\n\t" \
687 "dsll\t%0, 0x8\n\t" \
689 "5:lbu\t$1, 3(%2)\n\t" \
690 "dsll\t%0, 0x8\n\t" \
692 "6:lbu\t$1, 2(%2)\n\t" \
693 "dsll\t%0, 0x8\n\t" \
695 "7:lbu\t$1, 1(%2)\n\t" \
696 "dsll\t%0, 0x8\n\t" \
698 "8:lbu\t$1, 0(%2)\n\t" \
699 "dsll\t%0, 0x8\n\t" \
705 ".section\t.fixup,\"ax\"\n\t" \
706 "11:\tli\t%1, %3\n\t" \
709 ".section\t__ex_table,\"a\"\n\t" \
710 STR(PTR)"\t1b, 11b\n\t" \
711 STR(PTR)"\t2b, 11b\n\t" \
712 STR(PTR)"\t3b, 11b\n\t" \
713 STR(PTR)"\t4b, 11b\n\t" \
714 STR(PTR)"\t5b, 11b\n\t" \
715 STR(PTR)"\t6b, 11b\n\t" \
716 STR(PTR)"\t7b, 11b\n\t" \
717 STR(PTR)"\t8b, 11b\n\t" \
719 : "=&r" (value), "=r" (res) \
720 : "r" (addr), "i" (-EFAULT)); \
722 #endif /* CONFIG_CPU_MIPSR6 */
724 #define _StoreHW(addr, value, res, type) \
726 __asm__ __volatile__ ( \
728 "1:\t"type##_sb("%1", "0(%2)")"\n" \
729 "srl\t$1,%1, 0x8\n" \
730 "2:\t"type##_sb("$1", "1(%2)")"\n" \
735 ".section\t.fixup,\"ax\"\n\t" \
736 "4:\tli\t%0, %3\n\t" \
739 ".section\t__ex_table,\"a\"\n\t" \
740 STR(PTR)"\t1b, 4b\n\t" \
741 STR(PTR)"\t2b, 4b\n\t" \
744 : "r" (value), "r" (addr), "i" (-EFAULT));\
747 #ifndef CONFIG_CPU_MIPSR6
748 #define _StoreW(addr, value, res, type) \
750 __asm__ __volatile__ ( \
751 "1:\t"type##_swl("%1", "3(%2)")"\n" \
752 "2:\t"type##_swr("%1", "(%2)")"\n\t"\
756 ".section\t.fixup,\"ax\"\n\t" \
757 "4:\tli\t%0, %3\n\t" \
760 ".section\t__ex_table,\"a\"\n\t" \
761 STR(PTR)"\t1b, 4b\n\t" \
762 STR(PTR)"\t2b, 4b\n\t" \
765 : "r" (value), "r" (addr), "i" (-EFAULT)); \
768 #define _StoreDW(addr, value, res) \
770 __asm__ __volatile__ ( \
771 "1:\tsdl\t%1, 7(%2)\n" \
772 "2:\tsdr\t%1, (%2)\n\t" \
776 ".section\t.fixup,\"ax\"\n\t" \
777 "4:\tli\t%0, %3\n\t" \
780 ".section\t__ex_table,\"a\"\n\t" \
781 STR(PTR)"\t1b, 4b\n\t" \
782 STR(PTR)"\t2b, 4b\n\t" \
785 : "r" (value), "r" (addr), "i" (-EFAULT)); \
789 /* MIPSR6 has no swl and sdl instructions */
790 #define _StoreW(addr, value, res, type) \
792 __asm__ __volatile__ ( \
795 "1:"type##_sb("%1", "0(%2)")"\n\t" \
796 "srl\t$1, %1, 0x8\n\t" \
797 "2:"type##_sb("$1", "1(%2)")"\n\t" \
798 "srl\t$1, $1, 0x8\n\t" \
799 "3:"type##_sb("$1", "2(%2)")"\n\t" \
800 "srl\t$1, $1, 0x8\n\t" \
801 "4:"type##_sb("$1", "3(%2)")"\n\t" \
806 ".section\t.fixup,\"ax\"\n\t" \
807 "11:\tli\t%0, %3\n\t" \
810 ".section\t__ex_table,\"a\"\n\t" \
811 STR(PTR)"\t1b, 11b\n\t" \
812 STR(PTR)"\t2b, 11b\n\t" \
813 STR(PTR)"\t3b, 11b\n\t" \
814 STR(PTR)"\t4b, 11b\n\t" \
817 : "r" (value), "r" (addr), "i" (-EFAULT) \
821 #define _StoreDW(addr, value, res) \
823 __asm__ __volatile__ ( \
826 "1:sb\t%1, 0(%2)\n\t" \
827 "dsrl\t$1, %1, 0x8\n\t" \
828 "2:sb\t$1, 1(%2)\n\t" \
829 "dsrl\t$1, $1, 0x8\n\t" \
830 "3:sb\t$1, 2(%2)\n\t" \
831 "dsrl\t$1, $1, 0x8\n\t" \
832 "4:sb\t$1, 3(%2)\n\t" \
833 "dsrl\t$1, $1, 0x8\n\t" \
834 "5:sb\t$1, 4(%2)\n\t" \
835 "dsrl\t$1, $1, 0x8\n\t" \
836 "6:sb\t$1, 5(%2)\n\t" \
837 "dsrl\t$1, $1, 0x8\n\t" \
838 "7:sb\t$1, 6(%2)\n\t" \
839 "dsrl\t$1, $1, 0x8\n\t" \
840 "8:sb\t$1, 7(%2)\n\t" \
841 "dsrl\t$1, $1, 0x8\n\t" \
846 ".section\t.fixup,\"ax\"\n\t" \
847 "11:\tli\t%0, %3\n\t" \
850 ".section\t__ex_table,\"a\"\n\t" \
851 STR(PTR)"\t1b, 11b\n\t" \
852 STR(PTR)"\t2b, 11b\n\t" \
853 STR(PTR)"\t3b, 11b\n\t" \
854 STR(PTR)"\t4b, 11b\n\t" \
855 STR(PTR)"\t5b, 11b\n\t" \
856 STR(PTR)"\t6b, 11b\n\t" \
857 STR(PTR)"\t7b, 11b\n\t" \
858 STR(PTR)"\t8b, 11b\n\t" \
861 : "r" (value), "r" (addr), "i" (-EFAULT) \
865 #endif /* CONFIG_CPU_MIPSR6 */
868 #define LoadHWU(addr, value, res) _LoadHWU(addr, value, res, kernel)
869 #define LoadHWUE(addr, value, res) _LoadHWU(addr, value, res, user)
870 #define LoadWU(addr, value, res) _LoadWU(addr, value, res, kernel)
871 #define LoadWUE(addr, value, res) _LoadWU(addr, value, res, user)
872 #define LoadHW(addr, value, res) _LoadHW(addr, value, res, kernel)
873 #define LoadHWE(addr, value, res) _LoadHW(addr, value, res, user)
874 #define LoadW(addr, value, res) _LoadW(addr, value, res, kernel)
875 #define LoadWE(addr, value, res) _LoadW(addr, value, res, user)
876 #define LoadDW(addr, value, res) _LoadDW(addr, value, res)
878 #define StoreHW(addr, value, res) _StoreHW(addr, value, res, kernel)
879 #define StoreHWE(addr, value, res) _StoreHW(addr, value, res, user)
880 #define StoreW(addr, value, res) _StoreW(addr, value, res, kernel)
881 #define StoreWE(addr, value, res) _StoreW(addr, value, res, user)
882 #define StoreDW(addr, value, res) _StoreDW(addr, value, res)
884 static void emulate_load_store_insn(struct pt_regs *regs,
885 void __user *addr, unsigned int __user *pc)
887 union mips_instruction insn;
890 unsigned long origpc;
891 unsigned long orig31;
892 void __user *fault_addr = NULL;
896 origpc = (unsigned long)pc;
897 orig31 = regs->regs[31];
899 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
902 * This load never faults.
904 __get_user(insn.word, pc);
906 switch (insn.i_format.opcode) {
908 * These are instructions that a compiler doesn't generate. We
909 * can assume therefore that the code is MIPS-aware and
910 * really buggy. Emulating these instructions would break the
919 * For these instructions the only way to create an address
920 * error is an attempted access to kernel/supervisor address
937 * The remaining opcodes are the ones that are really of
943 * we can land here only from kernel accessing user memory,
944 * so we need to "switch" the address limit to user space, so
945 * address check can work properly.
949 switch (insn.spec3_format.func) {
951 if (!access_ok(VERIFY_READ, addr, 2)) {
955 LoadHWE(addr, value, res);
960 compute_return_epc(regs);
961 regs->regs[insn.spec3_format.rt] = value;
964 if (!access_ok(VERIFY_READ, addr, 4)) {
968 LoadWE(addr, value, res);
973 compute_return_epc(regs);
974 regs->regs[insn.spec3_format.rt] = value;
977 if (!access_ok(VERIFY_READ, addr, 2)) {
981 LoadHWUE(addr, value, res);
986 compute_return_epc(regs);
987 regs->regs[insn.spec3_format.rt] = value;
990 if (!access_ok(VERIFY_WRITE, addr, 2)) {
994 compute_return_epc(regs);
995 value = regs->regs[insn.spec3_format.rt];
996 StoreHWE(addr, value, res);
1003 if (!access_ok(VERIFY_WRITE, addr, 4)) {
1007 compute_return_epc(regs);
1008 value = regs->regs[insn.spec3_format.rt];
1009 StoreWE(addr, value, res);
1023 if (!access_ok(VERIFY_READ, addr, 2))
1026 if (config_enabled(CONFIG_EVA)) {
1027 if (segment_eq(get_fs(), get_ds()))
1028 LoadHW(addr, value, res);
1030 LoadHWE(addr, value, res);
1032 LoadHW(addr, value, res);
1037 compute_return_epc(regs);
1038 regs->regs[insn.i_format.rt] = value;
1042 if (!access_ok(VERIFY_READ, addr, 4))
1045 if (config_enabled(CONFIG_EVA)) {
1046 if (segment_eq(get_fs(), get_ds()))
1047 LoadW(addr, value, res);
1049 LoadWE(addr, value, res);
1051 LoadW(addr, value, res);
1056 compute_return_epc(regs);
1057 regs->regs[insn.i_format.rt] = value;
1061 if (!access_ok(VERIFY_READ, addr, 2))
1064 if (config_enabled(CONFIG_EVA)) {
1065 if (segment_eq(get_fs(), get_ds()))
1066 LoadHWU(addr, value, res);
1068 LoadHWUE(addr, value, res);
1070 LoadHWU(addr, value, res);
1075 compute_return_epc(regs);
1076 regs->regs[insn.i_format.rt] = value;
1082 * A 32-bit kernel might be running on a 64-bit processor. But
1083 * if we're on a 32-bit processor and an i-cache incoherency
1084 * or race makes us see a 64-bit instruction here the sdl/sdr
1085 * would blow up, so for now we don't handle unaligned 64-bit
1086 * instructions on 32-bit kernels.
1088 if (!access_ok(VERIFY_READ, addr, 4))
1091 LoadWU(addr, value, res);
1094 compute_return_epc(regs);
1095 regs->regs[insn.i_format.rt] = value;
1097 #endif /* CONFIG_64BIT */
1099 /* Cannot handle 64-bit instructions in 32-bit kernel */
1105 * A 32-bit kernel might be running on a 64-bit processor. But
1106 * if we're on a 32-bit processor and an i-cache incoherency
1107 * or race makes us see a 64-bit instruction here the sdl/sdr
1108 * would blow up, so for now we don't handle unaligned 64-bit
1109 * instructions on 32-bit kernels.
1111 if (!access_ok(VERIFY_READ, addr, 8))
1114 LoadDW(addr, value, res);
1117 compute_return_epc(regs);
1118 regs->regs[insn.i_format.rt] = value;
1120 #endif /* CONFIG_64BIT */
1122 /* Cannot handle 64-bit instructions in 32-bit kernel */
1126 if (!access_ok(VERIFY_WRITE, addr, 2))
1129 compute_return_epc(regs);
1130 value = regs->regs[insn.i_format.rt];
1132 if (config_enabled(CONFIG_EVA)) {
1133 if (segment_eq(get_fs(), get_ds()))
1134 StoreHW(addr, value, res);
1136 StoreHWE(addr, value, res);
1138 StoreHW(addr, value, res);
1146 if (!access_ok(VERIFY_WRITE, addr, 4))
1149 compute_return_epc(regs);
1150 value = regs->regs[insn.i_format.rt];
1152 if (config_enabled(CONFIG_EVA)) {
1153 if (segment_eq(get_fs(), get_ds()))
1154 StoreW(addr, value, res);
1156 StoreWE(addr, value, res);
1158 StoreW(addr, value, res);
1168 * A 32-bit kernel might be running on a 64-bit processor. But
1169 * if we're on a 32-bit processor and an i-cache incoherency
1170 * or race makes us see a 64-bit instruction here the sdl/sdr
1171 * would blow up, so for now we don't handle unaligned 64-bit
1172 * instructions on 32-bit kernels.
1174 if (!access_ok(VERIFY_WRITE, addr, 8))
1177 compute_return_epc(regs);
1178 value = regs->regs[insn.i_format.rt];
1179 StoreDW(addr, value, res);
1183 #endif /* CONFIG_64BIT */
1185 /* Cannot handle 64-bit instructions in 32-bit kernel */
1192 die_if_kernel("Unaligned FP access in kernel code", regs);
1193 BUG_ON(!used_math());
1195 lose_fpu(1); /* Save FPU state for the emulator. */
1196 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1198 own_fpu(1); /* Restore FPU state. */
1200 /* Signal if something went wrong. */
1201 process_fpemu_return(res, fault_addr);
1207 #ifndef CONFIG_CPU_MIPSR6
1209 * COP2 is available to implementor for application specific use.
1210 * It's up to applications to register a notifier chain and do
1211 * whatever they have to do, including possible sending of signals.
1213 * This instruction has been reallocated in Release 6
1216 cu2_notifier_call_chain(CU2_LWC2_OP, regs);
1220 cu2_notifier_call_chain(CU2_LDC2_OP, regs);
1224 cu2_notifier_call_chain(CU2_SWC2_OP, regs);
1228 cu2_notifier_call_chain(CU2_SDC2_OP, regs);
1233 * Pheeee... We encountered an yet unknown instruction or
1234 * cache coherence problem. Die sucker, die ...
1239 #ifdef CONFIG_DEBUG_FS
1240 unaligned_instructions++;
1246 /* roll back jump/branch */
1247 regs->cp0_epc = origpc;
1248 regs->regs[31] = orig31;
1249 /* Did we have an exception handler installed? */
1250 if (fixup_exception(regs))
1253 die_if_kernel("Unhandled kernel unaligned access", regs);
1254 force_sig(SIGSEGV, current);
1259 die_if_kernel("Unhandled kernel unaligned access", regs);
1260 force_sig(SIGBUS, current);
1266 ("Unhandled kernel unaligned access or invalid instruction", regs);
1267 force_sig(SIGILL, current);
1270 /* Recode table from 16-bit register notation to 32-bit GPR. */
1271 const int reg16to32[] = { 16, 17, 2, 3, 4, 5, 6, 7 };
1273 /* Recode table from 16-bit STORE register notation to 32-bit GPR. */
1274 const int reg16to32st[] = { 0, 17, 2, 3, 4, 5, 6, 7 };
1276 static void emulate_load_store_microMIPS(struct pt_regs *regs,
1279 unsigned long value;
1282 unsigned int reg = 0, rvar;
1283 unsigned long orig31;
1287 unsigned long origpc, contpc;
1288 union mips_instruction insn;
1289 struct mm_decoded_insn mminsn;
1290 void __user *fault_addr = NULL;
1292 origpc = regs->cp0_epc;
1293 orig31 = regs->regs[31];
1295 mminsn.micro_mips_mode = 1;
1298 * This load never faults.
1300 pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
1301 __get_user(halfword, pc16);
1303 contpc = regs->cp0_epc + 2;
1304 word = ((unsigned int)halfword << 16);
1307 if (!mm_insn_16bit(halfword)) {
1308 __get_user(halfword, pc16);
1310 contpc = regs->cp0_epc + 4;
1316 if (get_user(halfword, pc16))
1318 mminsn.next_pc_inc = 2;
1319 word = ((unsigned int)halfword << 16);
1321 if (!mm_insn_16bit(halfword)) {
1323 if (get_user(halfword, pc16))
1325 mminsn.next_pc_inc = 4;
1328 mminsn.next_insn = word;
1330 insn = (union mips_instruction)(mminsn.insn);
1331 if (mm_isBranchInstr(regs, mminsn, &contpc))
1332 insn = (union mips_instruction)(mminsn.next_insn);
1334 /* Parse instruction to find what to do */
1336 switch (insn.mm_i_format.opcode) {
1339 switch (insn.mm_x_format.func) {
1341 reg = insn.mm_x_format.rd;
1348 switch (insn.mm_m_format.func) {
1350 reg = insn.mm_m_format.rd;
1354 if (!access_ok(VERIFY_READ, addr, 8))
1357 LoadW(addr, value, res);
1360 regs->regs[reg] = value;
1362 LoadW(addr, value, res);
1365 regs->regs[reg + 1] = value;
1369 reg = insn.mm_m_format.rd;
1373 if (!access_ok(VERIFY_WRITE, addr, 8))
1376 value = regs->regs[reg];
1377 StoreW(addr, value, res);
1381 value = regs->regs[reg + 1];
1382 StoreW(addr, value, res);
1389 reg = insn.mm_m_format.rd;
1393 if (!access_ok(VERIFY_READ, addr, 16))
1396 LoadDW(addr, value, res);
1399 regs->regs[reg] = value;
1401 LoadDW(addr, value, res);
1404 regs->regs[reg + 1] = value;
1406 #endif /* CONFIG_64BIT */
1412 reg = insn.mm_m_format.rd;
1416 if (!access_ok(VERIFY_WRITE, addr, 16))
1419 value = regs->regs[reg];
1420 StoreDW(addr, value, res);
1424 value = regs->regs[reg + 1];
1425 StoreDW(addr, value, res);
1429 #endif /* CONFIG_64BIT */
1434 reg = insn.mm_m_format.rd;
1436 if ((rvar > 9) || !reg)
1440 (VERIFY_READ, addr, 4 * (rvar + 1)))
1443 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1448 for (i = 16; rvar; rvar--, i++) {
1449 LoadW(addr, value, res);
1453 regs->regs[i] = value;
1455 if ((reg & 0xf) == 9) {
1456 LoadW(addr, value, res);
1460 regs->regs[30] = value;
1463 LoadW(addr, value, res);
1466 regs->regs[31] = value;
1471 reg = insn.mm_m_format.rd;
1473 if ((rvar > 9) || !reg)
1477 (VERIFY_WRITE, addr, 4 * (rvar + 1)))
1480 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1485 for (i = 16; rvar; rvar--, i++) {
1486 value = regs->regs[i];
1487 StoreW(addr, value, res);
1492 if ((reg & 0xf) == 9) {
1493 value = regs->regs[30];
1494 StoreW(addr, value, res);
1500 value = regs->regs[31];
1501 StoreW(addr, value, res);
1509 reg = insn.mm_m_format.rd;
1511 if ((rvar > 9) || !reg)
1515 (VERIFY_READ, addr, 8 * (rvar + 1)))
1518 if (!access_ok(VERIFY_READ, addr, 8 * rvar))
1524 for (i = 16; rvar; rvar--, i++) {
1525 LoadDW(addr, value, res);
1529 regs->regs[i] = value;
1531 if ((reg & 0xf) == 9) {
1532 LoadDW(addr, value, res);
1536 regs->regs[30] = value;
1539 LoadDW(addr, value, res);
1542 regs->regs[31] = value;
1545 #endif /* CONFIG_64BIT */
1551 reg = insn.mm_m_format.rd;
1553 if ((rvar > 9) || !reg)
1557 (VERIFY_WRITE, addr, 8 * (rvar + 1)))
1560 if (!access_ok(VERIFY_WRITE, addr, 8 * rvar))
1566 for (i = 16; rvar; rvar--, i++) {
1567 value = regs->regs[i];
1568 StoreDW(addr, value, res);
1573 if ((reg & 0xf) == 9) {
1574 value = regs->regs[30];
1575 StoreDW(addr, value, res);
1581 value = regs->regs[31];
1582 StoreDW(addr, value, res);
1587 #endif /* CONFIG_64BIT */
1591 /* LWC2, SWC2, LDC2, SDC2 are not serviced */
1597 switch (insn.mm_m_format.func) {
1599 reg = insn.mm_m_format.rd;
1603 /* LL,SC,LLD,SCD are not serviced */
1607 switch (insn.mm_x_format.func) {
1622 /* roll back jump/branch */
1623 regs->cp0_epc = origpc;
1624 regs->regs[31] = orig31;
1626 die_if_kernel("Unaligned FP access in kernel code", regs);
1627 BUG_ON(!used_math());
1628 BUG_ON(!is_fpu_owner());
1630 lose_fpu(1); /* save the FPU state for the emulator */
1631 res = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1,
1633 own_fpu(1); /* restore FPU state */
1635 /* If something went wrong, signal */
1636 process_fpemu_return(res, fault_addr);
1643 reg = insn.mm_i_format.rt;
1647 reg = insn.mm_i_format.rt;
1651 reg = insn.mm_i_format.rt;
1655 reg = insn.mm_i_format.rt;
1659 reg = insn.mm_i_format.rt;
1663 reg = insn.mm_i_format.rt;
1667 reg = insn.mm_i_format.rt;
1671 switch (insn.mm16_m_format.func) {
1673 reg = insn.mm16_m_format.rlist;
1675 if (!access_ok(VERIFY_READ, addr, 4 * rvar))
1678 for (i = 16; rvar; rvar--, i++) {
1679 LoadW(addr, value, res);
1683 regs->regs[i] = value;
1685 LoadW(addr, value, res);
1688 regs->regs[31] = value;
1693 reg = insn.mm16_m_format.rlist;
1695 if (!access_ok(VERIFY_WRITE, addr, 4 * rvar))
1698 for (i = 16; rvar; rvar--, i++) {
1699 value = regs->regs[i];
1700 StoreW(addr, value, res);
1705 value = regs->regs[31];
1706 StoreW(addr, value, res);
1717 reg = reg16to32[insn.mm16_rb_format.rt];
1721 reg = reg16to32[insn.mm16_rb_format.rt];
1725 reg = reg16to32st[insn.mm16_rb_format.rt];
1729 reg = reg16to32st[insn.mm16_rb_format.rt];
1733 reg = insn.mm16_r5_format.rt;
1737 reg = insn.mm16_r5_format.rt;
1741 reg = reg16to32[insn.mm16_r3_format.rt];
1749 if (!access_ok(VERIFY_READ, addr, 2))
1752 LoadHW(addr, value, res);
1755 regs->regs[reg] = value;
1759 if (!access_ok(VERIFY_READ, addr, 2))
1762 LoadHWU(addr, value, res);
1765 regs->regs[reg] = value;
1769 if (!access_ok(VERIFY_READ, addr, 4))
1772 LoadW(addr, value, res);
1775 regs->regs[reg] = value;
1781 * A 32-bit kernel might be running on a 64-bit processor. But
1782 * if we're on a 32-bit processor and an i-cache incoherency
1783 * or race makes us see a 64-bit instruction here the sdl/sdr
1784 * would blow up, so for now we don't handle unaligned 64-bit
1785 * instructions on 32-bit kernels.
1787 if (!access_ok(VERIFY_READ, addr, 4))
1790 LoadWU(addr, value, res);
1793 regs->regs[reg] = value;
1795 #endif /* CONFIG_64BIT */
1797 /* Cannot handle 64-bit instructions in 32-bit kernel */
1803 * A 32-bit kernel might be running on a 64-bit processor. But
1804 * if we're on a 32-bit processor and an i-cache incoherency
1805 * or race makes us see a 64-bit instruction here the sdl/sdr
1806 * would blow up, so for now we don't handle unaligned 64-bit
1807 * instructions on 32-bit kernels.
1809 if (!access_ok(VERIFY_READ, addr, 8))
1812 LoadDW(addr, value, res);
1815 regs->regs[reg] = value;
1817 #endif /* CONFIG_64BIT */
1819 /* Cannot handle 64-bit instructions in 32-bit kernel */
1823 if (!access_ok(VERIFY_WRITE, addr, 2))
1826 value = regs->regs[reg];
1827 StoreHW(addr, value, res);
1833 if (!access_ok(VERIFY_WRITE, addr, 4))
1836 value = regs->regs[reg];
1837 StoreW(addr, value, res);
1845 * A 32-bit kernel might be running on a 64-bit processor. But
1846 * if we're on a 32-bit processor and an i-cache incoherency
1847 * or race makes us see a 64-bit instruction here the sdl/sdr
1848 * would blow up, so for now we don't handle unaligned 64-bit
1849 * instructions on 32-bit kernels.
1851 if (!access_ok(VERIFY_WRITE, addr, 8))
1854 value = regs->regs[reg];
1855 StoreDW(addr, value, res);
1859 #endif /* CONFIG_64BIT */
1861 /* Cannot handle 64-bit instructions in 32-bit kernel */
1865 regs->cp0_epc = contpc; /* advance or branch */
1867 #ifdef CONFIG_DEBUG_FS
1868 unaligned_instructions++;
1873 /* roll back jump/branch */
1874 regs->cp0_epc = origpc;
1875 regs->regs[31] = orig31;
1876 /* Did we have an exception handler installed? */
1877 if (fixup_exception(regs))
1880 die_if_kernel("Unhandled kernel unaligned access", regs);
1881 force_sig(SIGSEGV, current);
1886 die_if_kernel("Unhandled kernel unaligned access", regs);
1887 force_sig(SIGBUS, current);
1893 ("Unhandled kernel unaligned access or invalid instruction", regs);
1894 force_sig(SIGILL, current);
1897 static void emulate_load_store_MIPS16e(struct pt_regs *regs, void __user * addr)
1899 unsigned long value;
1902 unsigned long orig31;
1904 unsigned long origpc;
1905 union mips16e_instruction mips16inst, oldinst;
1907 origpc = regs->cp0_epc;
1908 orig31 = regs->regs[31];
1909 pc16 = (unsigned short __user *)msk_isa16_mode(origpc);
1911 * This load never faults.
1913 __get_user(mips16inst.full, pc16);
1914 oldinst = mips16inst;
1916 /* skip EXTEND instruction */
1917 if (mips16inst.ri.opcode == MIPS16e_extend_op) {
1919 __get_user(mips16inst.full, pc16);
1920 } else if (delay_slot(regs)) {
1921 /* skip jump instructions */
1922 /* JAL/JALX are 32 bits but have OPCODE in first short int */
1923 if (mips16inst.ri.opcode == MIPS16e_jal_op)
1926 if (get_user(mips16inst.full, pc16))
1930 switch (mips16inst.ri.opcode) {
1931 case MIPS16e_i64_op: /* I64 or RI64 instruction */
1932 switch (mips16inst.i64.func) { /* I64/RI64 func field check */
1933 case MIPS16e_ldpc_func:
1934 case MIPS16e_ldsp_func:
1935 reg = reg16to32[mips16inst.ri64.ry];
1938 case MIPS16e_sdsp_func:
1939 reg = reg16to32[mips16inst.ri64.ry];
1942 case MIPS16e_sdrasp_func:
1943 reg = 29; /* GPRSP */
1949 case MIPS16e_swsp_op:
1950 case MIPS16e_lwpc_op:
1951 case MIPS16e_lwsp_op:
1952 reg = reg16to32[mips16inst.ri.rx];
1956 if (mips16inst.i8.func != MIPS16e_swrasp_func)
1958 reg = 29; /* GPRSP */
1962 reg = reg16to32[mips16inst.rri.ry];
1966 switch (mips16inst.ri.opcode) {
1969 case MIPS16e_lbu_op:
1974 if (!access_ok(VERIFY_READ, addr, 2))
1977 LoadHW(addr, value, res);
1980 MIPS16e_compute_return_epc(regs, &oldinst);
1981 regs->regs[reg] = value;
1984 case MIPS16e_lhu_op:
1985 if (!access_ok(VERIFY_READ, addr, 2))
1988 LoadHWU(addr, value, res);
1991 MIPS16e_compute_return_epc(regs, &oldinst);
1992 regs->regs[reg] = value;
1996 case MIPS16e_lwpc_op:
1997 case MIPS16e_lwsp_op:
1998 if (!access_ok(VERIFY_READ, addr, 4))
2001 LoadW(addr, value, res);
2004 MIPS16e_compute_return_epc(regs, &oldinst);
2005 regs->regs[reg] = value;
2008 case MIPS16e_lwu_op:
2011 * A 32-bit kernel might be running on a 64-bit processor. But
2012 * if we're on a 32-bit processor and an i-cache incoherency
2013 * or race makes us see a 64-bit instruction here the sdl/sdr
2014 * would blow up, so for now we don't handle unaligned 64-bit
2015 * instructions on 32-bit kernels.
2017 if (!access_ok(VERIFY_READ, addr, 4))
2020 LoadWU(addr, value, res);
2023 MIPS16e_compute_return_epc(regs, &oldinst);
2024 regs->regs[reg] = value;
2026 #endif /* CONFIG_64BIT */
2028 /* Cannot handle 64-bit instructions in 32-bit kernel */
2035 * A 32-bit kernel might be running on a 64-bit processor. But
2036 * if we're on a 32-bit processor and an i-cache incoherency
2037 * or race makes us see a 64-bit instruction here the sdl/sdr
2038 * would blow up, so for now we don't handle unaligned 64-bit
2039 * instructions on 32-bit kernels.
2041 if (!access_ok(VERIFY_READ, addr, 8))
2044 LoadDW(addr, value, res);
2047 MIPS16e_compute_return_epc(regs, &oldinst);
2048 regs->regs[reg] = value;
2050 #endif /* CONFIG_64BIT */
2052 /* Cannot handle 64-bit instructions in 32-bit kernel */
2056 if (!access_ok(VERIFY_WRITE, addr, 2))
2059 MIPS16e_compute_return_epc(regs, &oldinst);
2060 value = regs->regs[reg];
2061 StoreHW(addr, value, res);
2067 case MIPS16e_swsp_op:
2068 case MIPS16e_i8_op: /* actually - MIPS16e_swrasp_func */
2069 if (!access_ok(VERIFY_WRITE, addr, 4))
2072 MIPS16e_compute_return_epc(regs, &oldinst);
2073 value = regs->regs[reg];
2074 StoreW(addr, value, res);
2083 * A 32-bit kernel might be running on a 64-bit processor. But
2084 * if we're on a 32-bit processor and an i-cache incoherency
2085 * or race makes us see a 64-bit instruction here the sdl/sdr
2086 * would blow up, so for now we don't handle unaligned 64-bit
2087 * instructions on 32-bit kernels.
2089 if (!access_ok(VERIFY_WRITE, addr, 8))
2092 MIPS16e_compute_return_epc(regs, &oldinst);
2093 value = regs->regs[reg];
2094 StoreDW(addr, value, res);
2098 #endif /* CONFIG_64BIT */
2100 /* Cannot handle 64-bit instructions in 32-bit kernel */
2105 * Pheeee... We encountered an yet unknown instruction or
2106 * cache coherence problem. Die sucker, die ...
2111 #ifdef CONFIG_DEBUG_FS
2112 unaligned_instructions++;
2118 /* roll back jump/branch */
2119 regs->cp0_epc = origpc;
2120 regs->regs[31] = orig31;
2121 /* Did we have an exception handler installed? */
2122 if (fixup_exception(regs))
2125 die_if_kernel("Unhandled kernel unaligned access", regs);
2126 force_sig(SIGSEGV, current);
2131 die_if_kernel("Unhandled kernel unaligned access", regs);
2132 force_sig(SIGBUS, current);
2138 ("Unhandled kernel unaligned access or invalid instruction", regs);
2139 force_sig(SIGILL, current);
2142 asmlinkage void do_ade(struct pt_regs *regs)
2144 enum ctx_state prev_state;
2145 unsigned int __user *pc;
2148 prev_state = exception_enter();
2149 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS,
2150 1, regs, regs->cp0_badvaddr);
2152 * Did we catch a fault trying to load an instruction?
2154 if (regs->cp0_badvaddr == regs->cp0_epc)
2157 if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
2159 if (unaligned_action == UNALIGNED_ACTION_SIGNAL)
2163 * Do branch emulation only if we didn't forward the exception.
2164 * This is all so but ugly ...
2168 * Are we running in microMIPS mode?
2170 if (get_isa16_mode(regs->cp0_epc)) {
2172 * Did we catch a fault trying to load an instruction in
2175 if (regs->cp0_badvaddr == msk_isa16_mode(regs->cp0_epc))
2177 if (unaligned_action == UNALIGNED_ACTION_SHOW)
2178 show_registers(regs);
2180 if (cpu_has_mmips) {
2182 if (!user_mode(regs))
2184 emulate_load_store_microMIPS(regs,
2185 (void __user *)regs->cp0_badvaddr);
2191 if (cpu_has_mips16) {
2193 if (!user_mode(regs))
2195 emulate_load_store_MIPS16e(regs,
2196 (void __user *)regs->cp0_badvaddr);
2205 if (unaligned_action == UNALIGNED_ACTION_SHOW)
2206 show_registers(regs);
2207 pc = (unsigned int __user *)exception_epc(regs);
2210 if (!user_mode(regs))
2212 emulate_load_store_insn(regs, (void __user *)regs->cp0_badvaddr, pc);
2218 die_if_kernel("Kernel unaligned instruction access", regs);
2219 force_sig(SIGBUS, current);
2222 * XXX On return from the signal handler we should advance the epc
2224 exception_exit(prev_state);
2227 #ifdef CONFIG_DEBUG_FS
2228 extern struct dentry *mips_debugfs_dir;
2229 static int __init debugfs_unaligned(void)
2233 if (!mips_debugfs_dir)
2235 d = debugfs_create_u32("unaligned_instructions", S_IRUGO,
2236 mips_debugfs_dir, &unaligned_instructions);
2239 d = debugfs_create_u32("unaligned_action", S_IRUGO | S_IWUSR,
2240 mips_debugfs_dir, &unaligned_action);
2245 __initcall(debugfs_unaligned);