2 * arch/sh/kernel/traps_64.c
4 * Copyright (C) 2000, 2001 Paolo Alberelli
5 * Copyright (C) 2003, 2004 Paul Mundt
6 * Copyright (C) 2003, 2004 Richard Curnow
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
12 #include <linux/sched.h>
13 #include <linux/sched/debug.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/ptrace.h>
18 #include <linux/timer.h>
20 #include <linux/smp.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
24 #include <linux/kallsyms.h>
25 #include <linux/interrupt.h>
26 #include <linux/sysctl.h>
27 #include <linux/module.h>
28 #include <linux/perf_event.h>
29 #include <linux/uaccess.h>
31 #include <asm/alignment.h>
32 #include <asm/processor.h>
33 #include <asm/pgtable.h>
36 static int read_opcode(reg_size_t pc
, insn_size_t
*result_opcode
, int from_user_mode
)
39 unsigned long aligned_pc
;
46 if (!access_ok(VERIFY_READ
, aligned_pc
, sizeof(insn_size_t
))) {
47 get_user_error
= -EFAULT
;
49 get_user_error
= __get_user(opcode
, (insn_size_t
*)aligned_pc
);
50 *result_opcode
= opcode
;
52 return get_user_error
;
54 /* If the fault was in the kernel, we can either read
55 * this directly, or if not, we fault.
57 *result_opcode
= *(insn_size_t
*)aligned_pc
;
60 } else if ((pc
& 1) == 0) {
62 /* TODO : provide handling for this. We don't really support
63 user-mode SHcompact yet, and for a kernel fault, this would
64 have to come from a module built for SHcompact. */
72 static int address_is_sign_extended(__u64 a
)
76 b
= (__u64
)(__s64
)(__s32
)(a
& 0xffffffffUL
);
77 return (b
== a
) ? 1 : 0;
79 #error "Sign extend check only works for NEFF==32"
83 /* return -1 for fault, 0 for OK */
84 static int generate_and_check_address(struct pt_regs
*regs
,
86 int displacement_not_indexed
,
90 __u64 base_address
, addr
;
93 switch (1 << width_shift
) {
94 case 1: inc_unaligned_byte_access(); break;
95 case 2: inc_unaligned_word_access(); break;
96 case 4: inc_unaligned_dword_access(); break;
97 case 8: inc_unaligned_multi_access(); break;
100 basereg
= (opcode
>> 20) & 0x3f;
101 base_address
= regs
->regs
[basereg
];
102 if (displacement_not_indexed
) {
104 displacement
= (opcode
>> 10) & 0x3ff;
105 displacement
= sign_extend64(displacement
, 9);
106 addr
= (__u64
)((__s64
)base_address
+ (displacement
<< width_shift
));
110 offsetreg
= (opcode
>> 10) & 0x3f;
111 offset
= regs
->regs
[offsetreg
];
112 addr
= base_address
+ offset
;
115 /* Check sign extended */
116 if (!address_is_sign_extended(addr
))
119 /* Check accessible. For misaligned access in the kernel, assume the
120 address is always accessible (and if not, just fault when the
121 load/store gets done.) */
122 if (user_mode(regs
)) {
123 inc_unaligned_user_access();
125 if (addr
>= TASK_SIZE
)
128 inc_unaligned_kernel_access();
132 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, addr
);
133 unaligned_fixups_notify(current
, opcode
, regs
);
138 static void misaligned_kernel_word_load(__u64 address
, int do_sign_extend
, __u64
*result
)
141 unsigned char *p
, *q
;
142 p
= (unsigned char *) (int) address
;
143 q
= (unsigned char *) &x
;
147 if (do_sign_extend
) {
148 *result
= (__u64
)(__s64
) *(short *) &x
;
154 static void misaligned_kernel_word_store(__u64 address
, __u64 value
)
157 unsigned char *p
, *q
;
158 p
= (unsigned char *) (int) address
;
159 q
= (unsigned char *) &x
;
166 static int misaligned_load(struct pt_regs
*regs
,
168 int displacement_not_indexed
,
172 /* Return -1 for a fault, 0 for OK */
177 error
= generate_and_check_address(regs
, opcode
,
178 displacement_not_indexed
, width_shift
, &address
);
182 destreg
= (opcode
>> 4) & 0x3f;
183 if (user_mode(regs
)) {
186 if (!access_ok(VERIFY_READ
, (unsigned long) address
, 1UL<<width_shift
)) {
190 if (__copy_user(&buffer
, (const void *)(int)address
, (1 << width_shift
)) > 0) {
191 return -1; /* fault */
193 switch (width_shift
) {
195 if (do_sign_extend
) {
196 regs
->regs
[destreg
] = (__u64
)(__s64
) *(__s16
*) &buffer
;
198 regs
->regs
[destreg
] = (__u64
) *(__u16
*) &buffer
;
202 regs
->regs
[destreg
] = (__u64
)(__s64
) *(__s32
*) &buffer
;
205 regs
->regs
[destreg
] = buffer
;
208 printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
209 width_shift
, (unsigned long) regs
->pc
);
213 /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
216 switch (width_shift
) {
218 misaligned_kernel_word_load(address
, do_sign_extend
, ®s
->regs
[destreg
]);
221 asm ("ldlo.l %1, 0, %0" : "=r" (lo
) : "r" (address
));
222 asm ("ldhi.l %1, 3, %0" : "=r" (hi
) : "r" (address
));
223 regs
->regs
[destreg
] = lo
| hi
;
226 asm ("ldlo.q %1, 0, %0" : "=r" (lo
) : "r" (address
));
227 asm ("ldhi.q %1, 7, %0" : "=r" (hi
) : "r" (address
));
228 regs
->regs
[destreg
] = lo
| hi
;
232 printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
233 width_shift
, (unsigned long) regs
->pc
);
241 static int misaligned_store(struct pt_regs
*regs
,
243 int displacement_not_indexed
,
246 /* Return -1 for a fault, 0 for OK */
251 error
= generate_and_check_address(regs
, opcode
,
252 displacement_not_indexed
, width_shift
, &address
);
256 srcreg
= (opcode
>> 4) & 0x3f;
257 if (user_mode(regs
)) {
260 if (!access_ok(VERIFY_WRITE
, (unsigned long) address
, 1UL<<width_shift
)) {
264 switch (width_shift
) {
266 *(__u16
*) &buffer
= (__u16
) regs
->regs
[srcreg
];
269 *(__u32
*) &buffer
= (__u32
) regs
->regs
[srcreg
];
272 buffer
= regs
->regs
[srcreg
];
275 printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
276 width_shift
, (unsigned long) regs
->pc
);
280 if (__copy_user((void *)(int)address
, &buffer
, (1 << width_shift
)) > 0) {
281 return -1; /* fault */
284 /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
285 __u64 val
= regs
->regs
[srcreg
];
287 switch (width_shift
) {
289 misaligned_kernel_word_store(address
, val
);
292 asm ("stlo.l %1, 0, %0" : : "r" (val
), "r" (address
));
293 asm ("sthi.l %1, 3, %0" : : "r" (val
), "r" (address
));
296 asm ("stlo.q %1, 0, %0" : : "r" (val
), "r" (address
));
297 asm ("sthi.q %1, 7, %0" : : "r" (val
), "r" (address
));
301 printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
302 width_shift
, (unsigned long) regs
->pc
);
310 /* Never need to fix up misaligned FPU accesses within the kernel since that's a real
312 static int misaligned_fpu_load(struct pt_regs
*regs
,
314 int displacement_not_indexed
,
318 /* Return -1 for a fault, 0 for OK */
323 error
= generate_and_check_address(regs
, opcode
,
324 displacement_not_indexed
, width_shift
, &address
);
328 destreg
= (opcode
>> 4) & 0x3f;
329 if (user_mode(regs
)) {
333 if (!access_ok(VERIFY_READ
, (unsigned long) address
, 1UL<<width_shift
)) {
337 if (__copy_user(&buffer
, (const void *)(int)address
, (1 << width_shift
)) > 0) {
338 return -1; /* fault */
340 /* 'current' may be the current owner of the FPU state, so
341 context switch the registers into memory so they can be
342 indexed by register number. */
343 if (last_task_used_math
== current
) {
347 last_task_used_math
= NULL
;
351 buflo
= *(__u32
*) &buffer
;
352 bufhi
= *(1 + (__u32
*) &buffer
);
354 switch (width_shift
) {
356 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
] = buflo
;
359 if (do_paired_load
) {
360 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
] = buflo
;
361 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
+1] = bufhi
;
363 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
364 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
] = bufhi
;
365 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
+1] = buflo
;
367 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
] = buflo
;
368 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
+1] = bufhi
;
373 printk("Unexpected width_shift %d in misaligned_fpu_load, PC=%08lx\n",
374 width_shift
, (unsigned long) regs
->pc
);
379 die ("Misaligned FPU load inside kernel", regs
, 0);
384 static int misaligned_fpu_store(struct pt_regs
*regs
,
386 int displacement_not_indexed
,
390 /* Return -1 for a fault, 0 for OK */
395 error
= generate_and_check_address(regs
, opcode
,
396 displacement_not_indexed
, width_shift
, &address
);
400 srcreg
= (opcode
>> 4) & 0x3f;
401 if (user_mode(regs
)) {
403 /* Initialise these to NaNs. */
404 __u32 buflo
=0xffffffffUL
, bufhi
=0xffffffffUL
;
406 if (!access_ok(VERIFY_WRITE
, (unsigned long) address
, 1UL<<width_shift
)) {
410 /* 'current' may be the current owner of the FPU state, so
411 context switch the registers into memory so they can be
412 indexed by register number. */
413 if (last_task_used_math
== current
) {
417 last_task_used_math
= NULL
;
421 switch (width_shift
) {
423 buflo
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
];
426 if (do_paired_load
) {
427 buflo
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
];
428 bufhi
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
+1];
430 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
431 bufhi
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
];
432 buflo
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
+1];
434 buflo
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
];
435 bufhi
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
+1];
440 printk("Unexpected width_shift %d in misaligned_fpu_store, PC=%08lx\n",
441 width_shift
, (unsigned long) regs
->pc
);
445 *(__u32
*) &buffer
= buflo
;
446 *(1 + (__u32
*) &buffer
) = bufhi
;
447 if (__copy_user((void *)(int)address
, &buffer
, (1 << width_shift
)) > 0) {
448 return -1; /* fault */
452 die ("Misaligned FPU load inside kernel", regs
, 0);
457 static int misaligned_fixup(struct pt_regs
*regs
)
462 unsigned int user_action
;
464 user_action
= unaligned_user_action();
465 if (!(user_action
& UM_FIXUP
))
468 error
= read_opcode(regs
->pc
, &opcode
, user_mode(regs
));
472 major
= (opcode
>> 26) & 0x3f;
473 minor
= (opcode
>> 16) & 0xf;
476 case (0x84>>2): /* LD.W */
477 error
= misaligned_load(regs
, opcode
, 1, 1, 1);
479 case (0xb0>>2): /* LD.UW */
480 error
= misaligned_load(regs
, opcode
, 1, 1, 0);
482 case (0x88>>2): /* LD.L */
483 error
= misaligned_load(regs
, opcode
, 1, 2, 1);
485 case (0x8c>>2): /* LD.Q */
486 error
= misaligned_load(regs
, opcode
, 1, 3, 0);
489 case (0xa4>>2): /* ST.W */
490 error
= misaligned_store(regs
, opcode
, 1, 1);
492 case (0xa8>>2): /* ST.L */
493 error
= misaligned_store(regs
, opcode
, 1, 2);
495 case (0xac>>2): /* ST.Q */
496 error
= misaligned_store(regs
, opcode
, 1, 3);
499 case (0x40>>2): /* indexed loads */
501 case 0x1: /* LDX.W */
502 error
= misaligned_load(regs
, opcode
, 0, 1, 1);
504 case 0x5: /* LDX.UW */
505 error
= misaligned_load(regs
, opcode
, 0, 1, 0);
507 case 0x2: /* LDX.L */
508 error
= misaligned_load(regs
, opcode
, 0, 2, 1);
510 case 0x3: /* LDX.Q */
511 error
= misaligned_load(regs
, opcode
, 0, 3, 0);
519 case (0x60>>2): /* indexed stores */
521 case 0x1: /* STX.W */
522 error
= misaligned_store(regs
, opcode
, 0, 1);
524 case 0x2: /* STX.L */
525 error
= misaligned_store(regs
, opcode
, 0, 2);
527 case 0x3: /* STX.Q */
528 error
= misaligned_store(regs
, opcode
, 0, 3);
536 case (0x94>>2): /* FLD.S */
537 error
= misaligned_fpu_load(regs
, opcode
, 1, 2, 0);
539 case (0x98>>2): /* FLD.P */
540 error
= misaligned_fpu_load(regs
, opcode
, 1, 3, 1);
542 case (0x9c>>2): /* FLD.D */
543 error
= misaligned_fpu_load(regs
, opcode
, 1, 3, 0);
545 case (0x1c>>2): /* floating indexed loads */
547 case 0x8: /* FLDX.S */
548 error
= misaligned_fpu_load(regs
, opcode
, 0, 2, 0);
550 case 0xd: /* FLDX.P */
551 error
= misaligned_fpu_load(regs
, opcode
, 0, 3, 1);
553 case 0x9: /* FLDX.D */
554 error
= misaligned_fpu_load(regs
, opcode
, 0, 3, 0);
561 case (0xb4>>2): /* FLD.S */
562 error
= misaligned_fpu_store(regs
, opcode
, 1, 2, 0);
564 case (0xb8>>2): /* FLD.P */
565 error
= misaligned_fpu_store(regs
, opcode
, 1, 3, 1);
567 case (0xbc>>2): /* FLD.D */
568 error
= misaligned_fpu_store(regs
, opcode
, 1, 3, 0);
570 case (0x3c>>2): /* floating indexed stores */
572 case 0x8: /* FSTX.S */
573 error
= misaligned_fpu_store(regs
, opcode
, 0, 2, 0);
575 case 0xd: /* FSTX.P */
576 error
= misaligned_fpu_store(regs
, opcode
, 0, 3, 1);
578 case 0x9: /* FSTX.D */
579 error
= misaligned_fpu_store(regs
, opcode
, 0, 3, 0);
596 regs
->pc
+= 4; /* Skip the instruction that's just been emulated */
601 static void do_unhandled_exception(int signr
, char *str
, unsigned long error
,
602 struct pt_regs
*regs
)
605 force_sig(signr
, current
);
607 die_if_no_fixup(str
, regs
, error
);
610 #define DO_ERROR(signr, str, name) \
611 asmlinkage void do_##name(unsigned long error_code, struct pt_regs *regs) \
613 do_unhandled_exception(signr, str, error_code, regs); \
616 DO_ERROR(SIGILL
, "illegal slot instruction", illegal_slot_inst
)
617 DO_ERROR(SIGSEGV
, "address error (exec)", address_error_exec
)
619 #if defined(CONFIG_SH64_ID2815_WORKAROUND)
621 #define OPCODE_INVALID 0
622 #define OPCODE_USER_VALID 1
623 #define OPCODE_PRIV_VALID 2
625 /* getcon/putcon - requires checking which control register is referenced. */
626 #define OPCODE_CTRL_REG 3
628 /* Table of valid opcodes for SHmedia mode.
629 Form a 10-bit value by concatenating the major/minor opcodes i.e.
630 opcode[31:26,20:16]. The 6 MSBs of this value index into the following
631 array. The 4 LSBs select the bit-pair in the entry (bits 1:0 correspond to
632 LSBs==4'b0000 etc). */
633 static unsigned long shmedia_opcode_table
[64] = {
634 0x55554044,0x54445055,0x15141514,0x14541414,0x00000000,0x10001000,0x01110055,0x04050015,
635 0x00000444,0xc0000000,0x44545515,0x40405555,0x55550015,0x10005555,0x55555505,0x04050000,
636 0x00000555,0x00000404,0x00040445,0x15151414,0x00000000,0x00000000,0x00000000,0x00000000,
637 0x00000055,0x40404444,0x00000404,0xc0009495,0x00000000,0x00000000,0x00000000,0x00000000,
638 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
639 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
640 0x80005050,0x04005055,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
641 0x81055554,0x00000404,0x55555555,0x55555555,0x00000000,0x00000000,0x00000000,0x00000000
644 /* Workaround SH5-101 cut2 silicon defect #2815 :
645 in some situations, inter-mode branches from SHcompact -> SHmedia
646 which should take ITLBMISS or EXECPROT exceptions at the target
647 falsely take RESINST at the target instead. */
648 void do_reserved_inst(unsigned long error_code
, struct pt_regs
*regs
)
650 insn_size_t opcode
= 0x6ff4fff0; /* guaranteed reserved opcode */
651 unsigned long pc
, aligned_pc
;
652 unsigned long index
, shift
;
653 unsigned long major
, minor
, combined
;
654 unsigned long reserved_field
;
658 char *exception_name
= "reserved_instruction";
662 /* SHcompact is not handled */
663 if (unlikely((pc
& 3) == 0))
666 /* SHmedia : check for defect. This requires executable vmas
667 to be readable too. */
668 aligned_pc
= pc
& ~3;
669 if (!access_ok(VERIFY_READ
, aligned_pc
, sizeof(insn_size_t
)))
670 get_user_error
= -EFAULT
;
672 get_user_error
= __get_user(opcode
, (insn_size_t
*)aligned_pc
);
674 if (get_user_error
< 0) {
676 * Error trying to read opcode. This typically means a
677 * real fault, not a RESINST any more. So change the
680 exception_name
= "address error (exec)";
685 /* These bits are currently reserved as zero in all valid opcodes */
686 reserved_field
= opcode
& 0xf;
687 if (unlikely(reserved_field
))
688 goto out
; /* invalid opcode */
690 major
= (opcode
>> 26) & 0x3f;
691 minor
= (opcode
>> 16) & 0xf;
692 combined
= (major
<< 4) | minor
;
695 opcode_state
= (shmedia_opcode_table
[index
] >> shift
) & 0x3;
696 switch (opcode_state
) {
700 case OPCODE_USER_VALID
:
702 * Restart the instruction: the branch to the instruction
703 * will now be from an RTE not from SHcompact so the
704 * silicon defect won't be triggered.
707 case OPCODE_PRIV_VALID
:
708 if (!user_mode(regs
)) {
710 * Should only ever get here if a module has
711 * SHcompact code inside it. If so, the same fix
714 return; /* same reason */
718 * Otherwise, user mode trying to execute a privileged
719 * instruction - fall through to trap.
722 case OPCODE_CTRL_REG
:
723 /* If in privileged mode, return as above. */
724 if (!user_mode(regs
))
727 /* In user mode ... */
728 if (combined
== 0x9f) { /* GETCON */
729 unsigned long regno
= (opcode
>> 20) & 0x3f;
734 /* reserved/privileged control register => trap */
735 } else if (combined
== 0x1bf) { /* PUTCON */
736 unsigned long regno
= (opcode
>> 4) & 0x3f;
741 /* reserved/privileged control register => trap */
746 /* Fall through to trap. */
751 do_unhandled_exception(signr
, exception_name
, error_code
, regs
);
754 #else /* CONFIG_SH64_ID2815_WORKAROUND */
756 /* If the workaround isn't needed, this is just a straightforward reserved
758 DO_ERROR(SIGILL
, "reserved instruction", reserved_inst
)
760 #endif /* CONFIG_SH64_ID2815_WORKAROUND */
762 /* Called with interrupts disabled */
763 asmlinkage
void do_exception_error(unsigned long ex
, struct pt_regs
*regs
)
765 die_if_kernel("exception", regs
, ex
);
768 asmlinkage
int do_unknown_trapa(unsigned long scId
, struct pt_regs
*regs
)
771 printk("System call ID error: [0x1#args:8 #syscall:16 0x%lx]\n", scId
);
773 die_if_kernel("unknown trapa", regs
, scId
);
778 /* Implement misaligned load/store handling for kernel (and optionally for user
779 mode too). Limitation : only SHmedia mode code is handled - there is no
780 handling at all for misaligned accesses occurring in SHcompact code yet. */
782 asmlinkage
void do_address_error_load(unsigned long error_code
, struct pt_regs
*regs
)
784 if (misaligned_fixup(regs
) < 0)
785 do_unhandled_exception(SIGSEGV
, "address error(load)",
789 asmlinkage
void do_address_error_store(unsigned long error_code
, struct pt_regs
*regs
)
791 if (misaligned_fixup(regs
) < 0)
792 do_unhandled_exception(SIGSEGV
, "address error(store)",
796 asmlinkage
void do_debug_interrupt(unsigned long code
, struct pt_regs
*regs
)
798 u64
peek_real_address_q(u64 addr
);
799 u64
poke_real_address_q(u64 addr
, u64 val
);
800 unsigned long long DM_EXP_CAUSE_PHY
= 0x0c100010;
801 unsigned long long exp_cause
;
802 /* It's not worth ioremapping the debug module registers for the amount
803 of access we make to them - just go direct to their physical
805 exp_cause
= peek_real_address_q(DM_EXP_CAUSE_PHY
);
807 printk("DM.EXP_CAUSE had unexpected bits set (=%08lx)\n",
808 (unsigned long)(exp_cause
& 0xffffffff));
810 /* Clear all DEBUGINT causes */
811 poke_real_address_q(DM_EXP_CAUSE_PHY
, 0x0);
814 void per_cpu_trap_init(void)
816 /* Nothing to do for now, VBR initialization later. */