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/kernel.h>
14 #include <linux/string.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/timer.h>
19 #include <linux/smp.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/spinlock.h>
23 #include <linux/kallsyms.h>
24 #include <linux/interrupt.h>
25 #include <linux/sysctl.h>
26 #include <linux/module.h>
27 #include <linux/perf_event.h>
28 #include <asm/uaccess.h>
30 #include <asm/alignment.h>
31 #include <asm/processor.h>
32 #include <asm/pgtable.h>
35 static int read_opcode(reg_size_t pc
, insn_size_t
*result_opcode
, int from_user_mode
)
38 unsigned long aligned_pc
;
45 if (!access_ok(VERIFY_READ
, aligned_pc
, sizeof(insn_size_t
))) {
46 get_user_error
= -EFAULT
;
48 get_user_error
= __get_user(opcode
, (insn_size_t
*)aligned_pc
);
49 *result_opcode
= opcode
;
51 return get_user_error
;
53 /* If the fault was in the kernel, we can either read
54 * this directly, or if not, we fault.
56 *result_opcode
= *(insn_size_t
*)aligned_pc
;
59 } else if ((pc
& 1) == 0) {
61 /* TODO : provide handling for this. We don't really support
62 user-mode SHcompact yet, and for a kernel fault, this would
63 have to come from a module built for SHcompact. */
71 static int address_is_sign_extended(__u64 a
)
75 b
= (__u64
)(__s64
)(__s32
)(a
& 0xffffffffUL
);
76 return (b
== a
) ? 1 : 0;
78 #error "Sign extend check only works for NEFF==32"
82 /* return -1 for fault, 0 for OK */
83 static int generate_and_check_address(struct pt_regs
*regs
,
85 int displacement_not_indexed
,
89 __u64 base_address
, addr
;
92 switch (1 << width_shift
) {
93 case 1: inc_unaligned_byte_access(); break;
94 case 2: inc_unaligned_word_access(); break;
95 case 4: inc_unaligned_dword_access(); break;
96 case 8: inc_unaligned_multi_access(); break;
99 basereg
= (opcode
>> 20) & 0x3f;
100 base_address
= regs
->regs
[basereg
];
101 if (displacement_not_indexed
) {
103 displacement
= (opcode
>> 10) & 0x3ff;
104 displacement
= ((displacement
<< 54) >> 54); /* sign extend */
105 addr
= (__u64
)((__s64
)base_address
+ (displacement
<< width_shift
));
109 offsetreg
= (opcode
>> 10) & 0x3f;
110 offset
= regs
->regs
[offsetreg
];
111 addr
= base_address
+ offset
;
114 /* Check sign extended */
115 if (!address_is_sign_extended(addr
))
118 /* Check accessible. For misaligned access in the kernel, assume the
119 address is always accessible (and if not, just fault when the
120 load/store gets done.) */
121 if (user_mode(regs
)) {
122 inc_unaligned_user_access();
124 if (addr
>= TASK_SIZE
)
127 inc_unaligned_kernel_access();
131 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, addr
);
132 unaligned_fixups_notify(current
, opcode
, regs
);
137 static void misaligned_kernel_word_load(__u64 address
, int do_sign_extend
, __u64
*result
)
140 unsigned char *p
, *q
;
141 p
= (unsigned char *) (int) address
;
142 q
= (unsigned char *) &x
;
146 if (do_sign_extend
) {
147 *result
= (__u64
)(__s64
) *(short *) &x
;
153 static void misaligned_kernel_word_store(__u64 address
, __u64 value
)
156 unsigned char *p
, *q
;
157 p
= (unsigned char *) (int) address
;
158 q
= (unsigned char *) &x
;
165 static int misaligned_load(struct pt_regs
*regs
,
167 int displacement_not_indexed
,
171 /* Return -1 for a fault, 0 for OK */
176 error
= generate_and_check_address(regs
, opcode
,
177 displacement_not_indexed
, width_shift
, &address
);
181 destreg
= (opcode
>> 4) & 0x3f;
182 if (user_mode(regs
)) {
185 if (!access_ok(VERIFY_READ
, (unsigned long) address
, 1UL<<width_shift
)) {
189 if (__copy_user(&buffer
, (const void *)(int)address
, (1 << width_shift
)) > 0) {
190 return -1; /* fault */
192 switch (width_shift
) {
194 if (do_sign_extend
) {
195 regs
->regs
[destreg
] = (__u64
)(__s64
) *(__s16
*) &buffer
;
197 regs
->regs
[destreg
] = (__u64
) *(__u16
*) &buffer
;
201 regs
->regs
[destreg
] = (__u64
)(__s64
) *(__s32
*) &buffer
;
204 regs
->regs
[destreg
] = buffer
;
207 printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
208 width_shift
, (unsigned long) regs
->pc
);
212 /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
215 switch (width_shift
) {
217 misaligned_kernel_word_load(address
, do_sign_extend
, ®s
->regs
[destreg
]);
220 asm ("ldlo.l %1, 0, %0" : "=r" (lo
) : "r" (address
));
221 asm ("ldhi.l %1, 3, %0" : "=r" (hi
) : "r" (address
));
222 regs
->regs
[destreg
] = lo
| hi
;
225 asm ("ldlo.q %1, 0, %0" : "=r" (lo
) : "r" (address
));
226 asm ("ldhi.q %1, 7, %0" : "=r" (hi
) : "r" (address
));
227 regs
->regs
[destreg
] = lo
| hi
;
231 printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
232 width_shift
, (unsigned long) regs
->pc
);
240 static int misaligned_store(struct pt_regs
*regs
,
242 int displacement_not_indexed
,
245 /* Return -1 for a fault, 0 for OK */
250 error
= generate_and_check_address(regs
, opcode
,
251 displacement_not_indexed
, width_shift
, &address
);
255 srcreg
= (opcode
>> 4) & 0x3f;
256 if (user_mode(regs
)) {
259 if (!access_ok(VERIFY_WRITE
, (unsigned long) address
, 1UL<<width_shift
)) {
263 switch (width_shift
) {
265 *(__u16
*) &buffer
= (__u16
) regs
->regs
[srcreg
];
268 *(__u32
*) &buffer
= (__u32
) regs
->regs
[srcreg
];
271 buffer
= regs
->regs
[srcreg
];
274 printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
275 width_shift
, (unsigned long) regs
->pc
);
279 if (__copy_user((void *)(int)address
, &buffer
, (1 << width_shift
)) > 0) {
280 return -1; /* fault */
283 /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
284 __u64 val
= regs
->regs
[srcreg
];
286 switch (width_shift
) {
288 misaligned_kernel_word_store(address
, val
);
291 asm ("stlo.l %1, 0, %0" : : "r" (val
), "r" (address
));
292 asm ("sthi.l %1, 3, %0" : : "r" (val
), "r" (address
));
295 asm ("stlo.q %1, 0, %0" : : "r" (val
), "r" (address
));
296 asm ("sthi.q %1, 7, %0" : : "r" (val
), "r" (address
));
300 printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
301 width_shift
, (unsigned long) regs
->pc
);
309 /* Never need to fix up misaligned FPU accesses within the kernel since that's a real
311 static int misaligned_fpu_load(struct pt_regs
*regs
,
313 int displacement_not_indexed
,
317 /* Return -1 for a fault, 0 for OK */
322 error
= generate_and_check_address(regs
, opcode
,
323 displacement_not_indexed
, width_shift
, &address
);
327 destreg
= (opcode
>> 4) & 0x3f;
328 if (user_mode(regs
)) {
332 if (!access_ok(VERIFY_READ
, (unsigned long) address
, 1UL<<width_shift
)) {
336 if (__copy_user(&buffer
, (const void *)(int)address
, (1 << width_shift
)) > 0) {
337 return -1; /* fault */
339 /* 'current' may be the current owner of the FPU state, so
340 context switch the registers into memory so they can be
341 indexed by register number. */
342 if (last_task_used_math
== current
) {
346 last_task_used_math
= NULL
;
350 buflo
= *(__u32
*) &buffer
;
351 bufhi
= *(1 + (__u32
*) &buffer
);
353 switch (width_shift
) {
355 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
] = buflo
;
358 if (do_paired_load
) {
359 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
] = buflo
;
360 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
+1] = bufhi
;
362 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
363 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
] = bufhi
;
364 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
+1] = buflo
;
366 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
] = buflo
;
367 current
->thread
.xstate
->hardfpu
.fp_regs
[destreg
+1] = bufhi
;
372 printk("Unexpected width_shift %d in misaligned_fpu_load, PC=%08lx\n",
373 width_shift
, (unsigned long) regs
->pc
);
378 die ("Misaligned FPU load inside kernel", regs
, 0);
383 static int misaligned_fpu_store(struct pt_regs
*regs
,
385 int displacement_not_indexed
,
389 /* Return -1 for a fault, 0 for OK */
394 error
= generate_and_check_address(regs
, opcode
,
395 displacement_not_indexed
, width_shift
, &address
);
399 srcreg
= (opcode
>> 4) & 0x3f;
400 if (user_mode(regs
)) {
402 /* Initialise these to NaNs. */
403 __u32 buflo
=0xffffffffUL
, bufhi
=0xffffffffUL
;
405 if (!access_ok(VERIFY_WRITE
, (unsigned long) address
, 1UL<<width_shift
)) {
409 /* 'current' may be the current owner of the FPU state, so
410 context switch the registers into memory so they can be
411 indexed by register number. */
412 if (last_task_used_math
== current
) {
416 last_task_used_math
= NULL
;
420 switch (width_shift
) {
422 buflo
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
];
425 if (do_paired_load
) {
426 buflo
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
];
427 bufhi
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
+1];
429 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
430 bufhi
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
];
431 buflo
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
+1];
433 buflo
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
];
434 bufhi
= current
->thread
.xstate
->hardfpu
.fp_regs
[srcreg
+1];
439 printk("Unexpected width_shift %d in misaligned_fpu_store, PC=%08lx\n",
440 width_shift
, (unsigned long) regs
->pc
);
444 *(__u32
*) &buffer
= buflo
;
445 *(1 + (__u32
*) &buffer
) = bufhi
;
446 if (__copy_user((void *)(int)address
, &buffer
, (1 << width_shift
)) > 0) {
447 return -1; /* fault */
451 die ("Misaligned FPU load inside kernel", regs
, 0);
456 static int misaligned_fixup(struct pt_regs
*regs
)
461 unsigned int user_action
;
463 user_action
= unaligned_user_action();
464 if (!(user_action
& UM_FIXUP
))
467 error
= read_opcode(regs
->pc
, &opcode
, user_mode(regs
));
471 major
= (opcode
>> 26) & 0x3f;
472 minor
= (opcode
>> 16) & 0xf;
475 case (0x84>>2): /* LD.W */
476 error
= misaligned_load(regs
, opcode
, 1, 1, 1);
478 case (0xb0>>2): /* LD.UW */
479 error
= misaligned_load(regs
, opcode
, 1, 1, 0);
481 case (0x88>>2): /* LD.L */
482 error
= misaligned_load(regs
, opcode
, 1, 2, 1);
484 case (0x8c>>2): /* LD.Q */
485 error
= misaligned_load(regs
, opcode
, 1, 3, 0);
488 case (0xa4>>2): /* ST.W */
489 error
= misaligned_store(regs
, opcode
, 1, 1);
491 case (0xa8>>2): /* ST.L */
492 error
= misaligned_store(regs
, opcode
, 1, 2);
494 case (0xac>>2): /* ST.Q */
495 error
= misaligned_store(regs
, opcode
, 1, 3);
498 case (0x40>>2): /* indexed loads */
500 case 0x1: /* LDX.W */
501 error
= misaligned_load(regs
, opcode
, 0, 1, 1);
503 case 0x5: /* LDX.UW */
504 error
= misaligned_load(regs
, opcode
, 0, 1, 0);
506 case 0x2: /* LDX.L */
507 error
= misaligned_load(regs
, opcode
, 0, 2, 1);
509 case 0x3: /* LDX.Q */
510 error
= misaligned_load(regs
, opcode
, 0, 3, 0);
518 case (0x60>>2): /* indexed stores */
520 case 0x1: /* STX.W */
521 error
= misaligned_store(regs
, opcode
, 0, 1);
523 case 0x2: /* STX.L */
524 error
= misaligned_store(regs
, opcode
, 0, 2);
526 case 0x3: /* STX.Q */
527 error
= misaligned_store(regs
, opcode
, 0, 3);
535 case (0x94>>2): /* FLD.S */
536 error
= misaligned_fpu_load(regs
, opcode
, 1, 2, 0);
538 case (0x98>>2): /* FLD.P */
539 error
= misaligned_fpu_load(regs
, opcode
, 1, 3, 1);
541 case (0x9c>>2): /* FLD.D */
542 error
= misaligned_fpu_load(regs
, opcode
, 1, 3, 0);
544 case (0x1c>>2): /* floating indexed loads */
546 case 0x8: /* FLDX.S */
547 error
= misaligned_fpu_load(regs
, opcode
, 0, 2, 0);
549 case 0xd: /* FLDX.P */
550 error
= misaligned_fpu_load(regs
, opcode
, 0, 3, 1);
552 case 0x9: /* FLDX.D */
553 error
= misaligned_fpu_load(regs
, opcode
, 0, 3, 0);
560 case (0xb4>>2): /* FLD.S */
561 error
= misaligned_fpu_store(regs
, opcode
, 1, 2, 0);
563 case (0xb8>>2): /* FLD.P */
564 error
= misaligned_fpu_store(regs
, opcode
, 1, 3, 1);
566 case (0xbc>>2): /* FLD.D */
567 error
= misaligned_fpu_store(regs
, opcode
, 1, 3, 0);
569 case (0x3c>>2): /* floating indexed stores */
571 case 0x8: /* FSTX.S */
572 error
= misaligned_fpu_store(regs
, opcode
, 0, 2, 0);
574 case 0xd: /* FSTX.P */
575 error
= misaligned_fpu_store(regs
, opcode
, 0, 3, 1);
577 case 0x9: /* FSTX.D */
578 error
= misaligned_fpu_store(regs
, opcode
, 0, 3, 0);
595 regs
->pc
+= 4; /* Skip the instruction that's just been emulated */
600 static void do_unhandled_exception(int signr
, char *str
, unsigned long error
,
601 struct pt_regs
*regs
)
604 force_sig(signr
, current
);
606 die_if_no_fixup(str
, regs
, error
);
609 #define DO_ERROR(signr, str, name) \
610 asmlinkage void do_##name(unsigned long error_code, struct pt_regs *regs) \
612 do_unhandled_exception(signr, str, error_code, regs); \
615 DO_ERROR(SIGILL
, "illegal slot instruction", illegal_slot_inst
)
616 DO_ERROR(SIGSEGV
, "address error (exec)", address_error_exec
)
618 #if defined(CONFIG_SH64_ID2815_WORKAROUND)
620 #define OPCODE_INVALID 0
621 #define OPCODE_USER_VALID 1
622 #define OPCODE_PRIV_VALID 2
624 /* getcon/putcon - requires checking which control register is referenced. */
625 #define OPCODE_CTRL_REG 3
627 /* Table of valid opcodes for SHmedia mode.
628 Form a 10-bit value by concatenating the major/minor opcodes i.e.
629 opcode[31:26,20:16]. The 6 MSBs of this value index into the following
630 array. The 4 LSBs select the bit-pair in the entry (bits 1:0 correspond to
631 LSBs==4'b0000 etc). */
632 static unsigned long shmedia_opcode_table
[64] = {
633 0x55554044,0x54445055,0x15141514,0x14541414,0x00000000,0x10001000,0x01110055,0x04050015,
634 0x00000444,0xc0000000,0x44545515,0x40405555,0x55550015,0x10005555,0x55555505,0x04050000,
635 0x00000555,0x00000404,0x00040445,0x15151414,0x00000000,0x00000000,0x00000000,0x00000000,
636 0x00000055,0x40404444,0x00000404,0xc0009495,0x00000000,0x00000000,0x00000000,0x00000000,
637 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
638 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
639 0x80005050,0x04005055,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
640 0x81055554,0x00000404,0x55555555,0x55555555,0x00000000,0x00000000,0x00000000,0x00000000
643 /* Workaround SH5-101 cut2 silicon defect #2815 :
644 in some situations, inter-mode branches from SHcompact -> SHmedia
645 which should take ITLBMISS or EXECPROT exceptions at the target
646 falsely take RESINST at the target instead. */
647 void do_reserved_inst(unsigned long error_code
, struct pt_regs
*regs
)
649 insn_size_t opcode
= 0x6ff4fff0; /* guaranteed reserved opcode */
650 unsigned long pc
, aligned_pc
;
651 unsigned long index
, shift
;
652 unsigned long major
, minor
, combined
;
653 unsigned long reserved_field
;
657 char *exception_name
= "reserved_instruction";
661 /* SHcompact is not handled */
662 if (unlikely((pc
& 3) == 0))
665 /* SHmedia : check for defect. This requires executable vmas
666 to be readable too. */
667 aligned_pc
= pc
& ~3;
668 if (!access_ok(VERIFY_READ
, aligned_pc
, sizeof(insn_size_t
)))
669 get_user_error
= -EFAULT
;
671 get_user_error
= __get_user(opcode
, (insn_size_t
*)aligned_pc
);
673 if (get_user_error
< 0) {
675 * Error trying to read opcode. This typically means a
676 * real fault, not a RESINST any more. So change the
679 exception_name
= "address error (exec)";
684 /* These bits are currently reserved as zero in all valid opcodes */
685 reserved_field
= opcode
& 0xf;
686 if (unlikely(reserved_field
))
687 goto out
; /* invalid opcode */
689 major
= (opcode
>> 26) & 0x3f;
690 minor
= (opcode
>> 16) & 0xf;
691 combined
= (major
<< 4) | minor
;
694 opcode_state
= (shmedia_opcode_table
[index
] >> shift
) & 0x3;
695 switch (opcode_state
) {
699 case OPCODE_USER_VALID
:
701 * Restart the instruction: the branch to the instruction
702 * will now be from an RTE not from SHcompact so the
703 * silicon defect won't be triggered.
706 case OPCODE_PRIV_VALID
:
707 if (!user_mode(regs
)) {
709 * Should only ever get here if a module has
710 * SHcompact code inside it. If so, the same fix
713 return; /* same reason */
717 * Otherwise, user mode trying to execute a privileged
718 * instruction - fall through to trap.
721 case OPCODE_CTRL_REG
:
722 /* If in privileged mode, return as above. */
723 if (!user_mode(regs
))
726 /* In user mode ... */
727 if (combined
== 0x9f) { /* GETCON */
728 unsigned long regno
= (opcode
>> 20) & 0x3f;
733 /* reserved/privileged control register => trap */
734 } else if (combined
== 0x1bf) { /* PUTCON */
735 unsigned long regno
= (opcode
>> 4) & 0x3f;
740 /* reserved/privileged control register => trap */
745 /* Fall through to trap. */
750 do_unhandled_exception(signr
, exception_name
, error_code
, regs
);
753 #else /* CONFIG_SH64_ID2815_WORKAROUND */
755 /* If the workaround isn't needed, this is just a straightforward reserved
757 DO_ERROR(SIGILL
, "reserved instruction", reserved_inst
)
759 #endif /* CONFIG_SH64_ID2815_WORKAROUND */
761 /* Called with interrupts disabled */
762 asmlinkage
void do_exception_error(unsigned long ex
, struct pt_regs
*regs
)
764 die_if_kernel("exception", regs
, ex
);
767 asmlinkage
int do_unknown_trapa(unsigned long scId
, struct pt_regs
*regs
)
770 printk("System call ID error: [0x1#args:8 #syscall:16 0x%lx]\n", scId
);
772 die_if_kernel("unknown trapa", regs
, scId
);
777 /* Implement misaligned load/store handling for kernel (and optionally for user
778 mode too). Limitation : only SHmedia mode code is handled - there is no
779 handling at all for misaligned accesses occurring in SHcompact code yet. */
781 asmlinkage
void do_address_error_load(unsigned long error_code
, struct pt_regs
*regs
)
783 if (misaligned_fixup(regs
) < 0)
784 do_unhandled_exception(SIGSEGV
, "address error(load)",
788 asmlinkage
void do_address_error_store(unsigned long error_code
, struct pt_regs
*regs
)
790 if (misaligned_fixup(regs
) < 0)
791 do_unhandled_exception(SIGSEGV
, "address error(store)",
795 asmlinkage
void do_debug_interrupt(unsigned long code
, struct pt_regs
*regs
)
797 u64
peek_real_address_q(u64 addr
);
798 u64
poke_real_address_q(u64 addr
, u64 val
);
799 unsigned long long DM_EXP_CAUSE_PHY
= 0x0c100010;
800 unsigned long long exp_cause
;
801 /* It's not worth ioremapping the debug module registers for the amount
802 of access we make to them - just go direct to their physical
804 exp_cause
= peek_real_address_q(DM_EXP_CAUSE_PHY
);
806 printk("DM.EXP_CAUSE had unexpected bits set (=%08lx)\n",
807 (unsigned long)(exp_cause
& 0xffffffff));
809 /* Clear all DEBUGINT causes */
810 poke_real_address_q(DM_EXP_CAUSE_PHY
, 0x0);
813 void per_cpu_trap_init(void)
815 /* Nothing to do for now, VBR initialization later. */