Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 1 | /* |
| 2 | * 'traps.c' handles hardware traps and faults after we have saved some |
| 3 | * state in 'entry.S'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * SuperH version: Copyright (C) 1999 Niibe Yutaka |
| 6 | * Copyright (C) 2000 Philipp Rumpf |
| 7 | * Copyright (C) 2000 David Howells |
Paul Mundt | 3a2e117 | 2007-05-01 16:33:10 +0900 | [diff] [blame] | 8 | * Copyright (C) 2002 - 2007 Paul Mundt |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 9 | * |
| 10 | * This file is subject to the terms and conditions of the GNU General Public |
| 11 | * License. See the file "COPYING" in the main directory of this archive |
| 12 | * for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/kallsyms.h> |
Paul Mundt | 1f66658 | 2006-10-19 16:20:25 +0900 | [diff] [blame] | 20 | #include <linux/io.h> |
Paul Mundt | fa69151 | 2007-03-08 19:41:21 +0900 | [diff] [blame] | 21 | #include <linux/bug.h> |
Paul Mundt | 9b8c90e | 2006-12-06 11:07:51 +0900 | [diff] [blame] | 22 | #include <linux/debug_locks.h> |
Paul Mundt | b118ca5 | 2007-05-09 10:55:38 +0900 | [diff] [blame] | 23 | #include <linux/kdebug.h> |
Paul Mundt | e113276 | 2007-05-15 08:36:36 +0900 | [diff] [blame] | 24 | #include <linux/kexec.h> |
Paul Mundt | dc34d31 | 2006-12-08 17:41:43 +0900 | [diff] [blame] | 25 | #include <linux/limits.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/system.h> |
| 27 | #include <asm/uaccess.h> |
Andrew Morton | fad0f90 | 2008-04-16 02:03:51 +0900 | [diff] [blame] | 28 | #include <asm/fpu.h> |
Chris Smith | d39f545 | 2008-09-05 17:15:39 +0900 | [diff] [blame^] | 29 | #include <asm/kprobes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #ifdef CONFIG_SH_KGDB |
| 32 | #include <asm/kgdb.h> |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 33 | #define CHK_REMOTE_DEBUG(regs) \ |
| 34 | { \ |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 35 | if (kgdb_debug_hook && !user_mode(regs))\ |
| 36 | (*kgdb_debug_hook)(regs); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | #else |
| 39 | #define CHK_REMOTE_DEBUG(regs) |
| 40 | #endif |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #ifdef CONFIG_CPU_SH2 |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 43 | # define TRAP_RESERVED_INST 4 |
| 44 | # define TRAP_ILLEGAL_SLOT_INST 6 |
| 45 | # define TRAP_ADDRESS_ERROR 9 |
| 46 | # ifdef CONFIG_CPU_SH2A |
Yoshinori Sato | 6e80f5e | 2008-07-10 01:20:03 +0900 | [diff] [blame] | 47 | # define TRAP_FPU_ERROR 13 |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 48 | # define TRAP_DIVZERO_ERROR 17 |
| 49 | # define TRAP_DIVOVF_ERROR 18 |
| 50 | # endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #else |
| 52 | #define TRAP_RESERVED_INST 12 |
| 53 | #define TRAP_ILLEGAL_SLOT_INST 13 |
| 54 | #endif |
| 55 | |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 56 | static void dump_mem(const char *str, unsigned long bottom, unsigned long top) |
| 57 | { |
| 58 | unsigned long p; |
| 59 | int i; |
| 60 | |
| 61 | printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top); |
| 62 | |
| 63 | for (p = bottom & ~31; p < top; ) { |
| 64 | printk("%04lx: ", p & 0xffff); |
| 65 | |
| 66 | for (i = 0; i < 8; i++, p += 4) { |
| 67 | unsigned int val; |
| 68 | |
| 69 | if (p < bottom || p >= top) |
| 70 | printk(" "); |
| 71 | else { |
| 72 | if (__get_user(val, (unsigned int __user *)p)) { |
| 73 | printk("\n"); |
| 74 | return; |
| 75 | } |
| 76 | printk("%08x ", val); |
| 77 | } |
| 78 | } |
| 79 | printk("\n"); |
| 80 | } |
| 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Paul Mundt | 3a2e117 | 2007-05-01 16:33:10 +0900 | [diff] [blame] | 83 | static DEFINE_SPINLOCK(die_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | void die(const char * str, struct pt_regs * regs, long err) |
| 86 | { |
| 87 | static int die_counter; |
| 88 | |
Paul Mundt | 5527398 | 2007-06-18 18:57:13 +0900 | [diff] [blame] | 89 | oops_enter(); |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | console_verbose(); |
| 92 | spin_lock_irq(&die_lock); |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 93 | bust_spinlocks(1); |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter); |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | CHK_REMOTE_DEBUG(regs); |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 98 | print_modules(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | show_regs(regs); |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 100 | |
Alexey Dobriyan | 19c5870 | 2007-10-18 23:40:41 -0700 | [diff] [blame] | 101 | printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm, |
| 102 | task_pid_nr(current), task_stack_page(current) + 1); |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 103 | |
| 104 | if (!user_mode(regs) || in_interrupt()) |
| 105 | dump_mem("Stack: ", regs->regs[15], THREAD_SIZE + |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 106 | (unsigned long)task_stack_page(current)); |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 107 | |
| 108 | bust_spinlocks(0); |
Pavel Emelianov | bcdcd8e | 2007-07-17 04:03:42 -0700 | [diff] [blame] | 109 | add_taint(TAINT_DIE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | spin_unlock_irq(&die_lock); |
Paul Mundt | e113276 | 2007-05-15 08:36:36 +0900 | [diff] [blame] | 111 | |
| 112 | if (kexec_should_crash(current)) |
| 113 | crash_kexec(regs); |
| 114 | |
| 115 | if (in_interrupt()) |
| 116 | panic("Fatal exception in interrupt"); |
| 117 | |
| 118 | if (panic_on_oops) |
| 119 | panic("Fatal exception"); |
| 120 | |
Paul Mundt | 5527398 | 2007-06-18 18:57:13 +0900 | [diff] [blame] | 121 | oops_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | do_exit(SIGSEGV); |
| 123 | } |
| 124 | |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 125 | static inline void die_if_kernel(const char *str, struct pt_regs *regs, |
| 126 | long err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
| 128 | if (!user_mode(regs)) |
| 129 | die(str, regs, err); |
| 130 | } |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | /* |
| 133 | * try and fix up kernelspace address errors |
| 134 | * - userspace errors just cause EFAULT to be returned, resulting in SEGV |
| 135 | * - kernel/userspace interfaces cause a jump to an appropriate handler |
| 136 | * - other kernel errors are bad |
| 137 | * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault |
| 138 | */ |
| 139 | static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err) |
| 140 | { |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 141 | if (!user_mode(regs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | const struct exception_table_entry *fixup; |
| 143 | fixup = search_exception_tables(regs->pc); |
| 144 | if (fixup) { |
| 145 | regs->pc = fixup->fixup; |
| 146 | return 0; |
| 147 | } |
| 148 | die(str, regs, err); |
| 149 | } |
| 150 | return -EFAULT; |
| 151 | } |
| 152 | |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 153 | static inline void sign_extend(unsigned int count, unsigned char *dst) |
| 154 | { |
| 155 | #ifdef __LITTLE_ENDIAN__ |
Magnus Damm | 4252c65 | 2008-02-07 19:58:46 +0900 | [diff] [blame] | 156 | if ((count == 1) && dst[0] & 0x80) { |
| 157 | dst[1] = 0xff; |
| 158 | dst[2] = 0xff; |
| 159 | dst[3] = 0xff; |
| 160 | } |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 161 | if ((count == 2) && dst[1] & 0x80) { |
| 162 | dst[2] = 0xff; |
| 163 | dst[3] = 0xff; |
| 164 | } |
| 165 | #else |
Magnus Damm | 4252c65 | 2008-02-07 19:58:46 +0900 | [diff] [blame] | 166 | if ((count == 1) && dst[3] & 0x80) { |
| 167 | dst[2] = 0xff; |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 168 | dst[1] = 0xff; |
Magnus Damm | 4252c65 | 2008-02-07 19:58:46 +0900 | [diff] [blame] | 169 | dst[0] = 0xff; |
| 170 | } |
| 171 | if ((count == 2) && dst[2] & 0x80) { |
| 172 | dst[1] = 0xff; |
| 173 | dst[0] = 0xff; |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 174 | } |
| 175 | #endif |
| 176 | } |
| 177 | |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 178 | static struct mem_access user_mem_access = { |
| 179 | copy_from_user, |
| 180 | copy_to_user, |
| 181 | }; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* |
| 184 | * handle an instruction that does an unaligned memory access by emulating the |
| 185 | * desired behaviour |
| 186 | * - note that PC _may not_ point to the faulting instruction |
| 187 | * (if that instruction is in a branch delay slot) |
| 188 | * - return 0 if emulation okay, -EFAULT on existential error |
| 189 | */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 190 | static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs, |
| 191 | struct mem_access *ma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | int ret, index, count; |
| 194 | unsigned long *rm, *rn; |
| 195 | unsigned char *src, *dst; |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 196 | unsigned char __user *srcu, *dstu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | index = (instruction>>8)&15; /* 0x0F00 */ |
| 199 | rn = ®s->regs[index]; |
| 200 | |
| 201 | index = (instruction>>4)&15; /* 0x00F0 */ |
| 202 | rm = ®s->regs[index]; |
| 203 | |
| 204 | count = 1<<(instruction&3); |
| 205 | |
| 206 | ret = -EFAULT; |
| 207 | switch (instruction>>12) { |
| 208 | case 0: /* mov.[bwl] to/from memory via r0+rn */ |
| 209 | if (instruction & 8) { |
| 210 | /* from memory */ |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 211 | srcu = (unsigned char __user *)*rm; |
| 212 | srcu += regs->regs[0]; |
| 213 | dst = (unsigned char *)rn; |
| 214 | *(unsigned long *)dst = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 216 | #if !defined(__LITTLE_ENDIAN__) |
| 217 | dst += 4-count; |
| 218 | #endif |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 219 | if (ma->from(dst, srcu, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | goto fetch_fault; |
| 221 | |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 222 | sign_extend(count, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } else { |
| 224 | /* to memory */ |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 225 | src = (unsigned char *)rm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | #if !defined(__LITTLE_ENDIAN__) |
| 227 | src += 4-count; |
| 228 | #endif |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 229 | dstu = (unsigned char __user *)*rn; |
| 230 | dstu += regs->regs[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 232 | if (ma->to(dstu, src, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | goto fetch_fault; |
| 234 | } |
| 235 | ret = 0; |
| 236 | break; |
| 237 | |
| 238 | case 1: /* mov.l Rm,@(disp,Rn) */ |
| 239 | src = (unsigned char*) rm; |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 240 | dstu = (unsigned char __user *)*rn; |
| 241 | dstu += (instruction&0x000F)<<2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 243 | if (ma->to(dstu, src, 4)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | goto fetch_fault; |
| 245 | ret = 0; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 246 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | case 2: /* mov.[bwl] to memory, possibly with pre-decrement */ |
| 249 | if (instruction & 4) |
| 250 | *rn -= count; |
| 251 | src = (unsigned char*) rm; |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 252 | dstu = (unsigned char __user *)*rn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | #if !defined(__LITTLE_ENDIAN__) |
| 254 | src += 4-count; |
| 255 | #endif |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 256 | if (ma->to(dstu, src, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | goto fetch_fault; |
| 258 | ret = 0; |
| 259 | break; |
| 260 | |
| 261 | case 5: /* mov.l @(disp,Rm),Rn */ |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 262 | srcu = (unsigned char __user *)*rm; |
| 263 | srcu += (instruction & 0x000F) << 2; |
| 264 | dst = (unsigned char *)rn; |
| 265 | *(unsigned long *)dst = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 267 | if (ma->from(dst, srcu, 4)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | goto fetch_fault; |
| 269 | ret = 0; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 270 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | case 6: /* mov.[bwl] from memory, possibly with post-increment */ |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 273 | srcu = (unsigned char __user *)*rm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | if (instruction & 4) |
| 275 | *rm += count; |
| 276 | dst = (unsigned char*) rn; |
| 277 | *(unsigned long*)dst = 0; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 278 | |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 279 | #if !defined(__LITTLE_ENDIAN__) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | dst += 4-count; |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 281 | #endif |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 282 | if (ma->from(dst, srcu, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | goto fetch_fault; |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 284 | sign_extend(count, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | ret = 0; |
| 286 | break; |
| 287 | |
| 288 | case 8: |
| 289 | switch ((instruction&0xFF00)>>8) { |
| 290 | case 0x81: /* mov.w R0,@(disp,Rn) */ |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 291 | src = (unsigned char *) ®s->regs[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | #if !defined(__LITTLE_ENDIAN__) |
| 293 | src += 2; |
| 294 | #endif |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 295 | dstu = (unsigned char __user *)*rm; /* called Rn in the spec */ |
| 296 | dstu += (instruction & 0x000F) << 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 298 | if (ma->to(dstu, src, 2)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | goto fetch_fault; |
| 300 | ret = 0; |
| 301 | break; |
| 302 | |
| 303 | case 0x85: /* mov.w @(disp,Rm),R0 */ |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 304 | srcu = (unsigned char __user *)*rm; |
| 305 | srcu += (instruction & 0x000F) << 1; |
| 306 | dst = (unsigned char *) ®s->regs[0]; |
| 307 | *(unsigned long *)dst = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | #if !defined(__LITTLE_ENDIAN__) |
| 310 | dst += 2; |
| 311 | #endif |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 312 | if (ma->from(dst, srcu, 2)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | goto fetch_fault; |
Magnus Damm | 86c0179 | 2008-02-07 00:02:50 +0900 | [diff] [blame] | 314 | sign_extend(2, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | ret = 0; |
| 316 | break; |
| 317 | } |
| 318 | break; |
| 319 | } |
| 320 | return ret; |
| 321 | |
| 322 | fetch_fault: |
| 323 | /* Argh. Address not only misaligned but also non-existent. |
| 324 | * Raise an EFAULT and see if it's trapped |
| 325 | */ |
| 326 | return die_if_no_fixup("Fault in unaligned fixup", regs, 0); |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * emulate the instruction in the delay slot |
| 331 | * - fetches the instruction from PC+2 |
| 332 | */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 333 | static inline int handle_delayslot(struct pt_regs *regs, |
| 334 | opcode_t old_instruction, |
| 335 | struct mem_access *ma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
Magnus Damm | 4b5a9ef | 2008-02-07 20:04:12 +0900 | [diff] [blame] | 337 | opcode_t instruction; |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 338 | void __user *addr = (void __user *)(regs->pc + |
| 339 | instruction_size(old_instruction)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Magnus Damm | 4b5a9ef | 2008-02-07 20:04:12 +0900 | [diff] [blame] | 341 | if (copy_from_user(&instruction, addr, sizeof(instruction))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /* the instruction-fetch faulted */ |
| 343 | if (user_mode(regs)) |
| 344 | return -EFAULT; |
| 345 | |
| 346 | /* kernel */ |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 347 | die("delay-slot-insn faulting in handle_unaligned_delayslot", |
| 348 | regs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 351 | return handle_unaligned_ins(instruction, regs, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | * handle an instruction that does an unaligned memory access |
| 356 | * - have to be careful of branch delay-slot instructions that fault |
| 357 | * SH3: |
| 358 | * - if the branch would be taken PC points to the branch |
| 359 | * - if the branch would not be taken, PC points to delay-slot |
| 360 | * SH4: |
| 361 | * - PC always points to delayed branch |
| 362 | * - return 0 if handled, -EFAULT if failed (may not return if in kernel) |
| 363 | */ |
| 364 | |
| 365 | /* Macros to determine offset from current PC for branch instructions */ |
| 366 | /* Explicit type coercion is used to force sign extension where needed */ |
| 367 | #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4) |
| 368 | #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4) |
| 369 | |
Paul Mundt | 710ee0c | 2006-11-05 16:48:42 +0900 | [diff] [blame] | 370 | /* |
| 371 | * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit |
| 372 | * opcodes.. |
| 373 | */ |
Magnus Damm | 4b5a9ef | 2008-02-07 20:04:12 +0900 | [diff] [blame] | 374 | |
Paul Mundt | 710ee0c | 2006-11-05 16:48:42 +0900 | [diff] [blame] | 375 | static int handle_unaligned_notify_count = 10; |
| 376 | |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 377 | int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs, |
| 378 | struct mem_access *ma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
| 380 | u_int rm; |
| 381 | int ret, index; |
| 382 | |
| 383 | index = (instruction>>8)&15; /* 0x0F00 */ |
| 384 | rm = regs->regs[index]; |
| 385 | |
| 386 | /* shout about the first ten userspace fixups */ |
| 387 | if (user_mode(regs) && handle_unaligned_notify_count>0) { |
| 388 | handle_unaligned_notify_count--; |
| 389 | |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 390 | printk(KERN_NOTICE "Fixing up unaligned userspace access " |
| 391 | "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n", |
Alexey Dobriyan | 19c5870 | 2007-10-18 23:40:41 -0700 | [diff] [blame] | 392 | current->comm, task_pid_nr(current), |
Magnus Damm | 4b5a9ef | 2008-02-07 20:04:12 +0900 | [diff] [blame] | 393 | (void *)regs->pc, instruction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | ret = -EFAULT; |
| 397 | switch (instruction&0xF000) { |
| 398 | case 0x0000: |
| 399 | if (instruction==0x000B) { |
| 400 | /* rts */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 401 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (ret==0) |
| 403 | regs->pc = regs->pr; |
| 404 | } |
| 405 | else if ((instruction&0x00FF)==0x0023) { |
| 406 | /* braf @Rm */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 407 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (ret==0) |
| 409 | regs->pc += rm + 4; |
| 410 | } |
| 411 | else if ((instruction&0x00FF)==0x0003) { |
| 412 | /* bsrf @Rm */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 413 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (ret==0) { |
| 415 | regs->pr = regs->pc + 4; |
| 416 | regs->pc += rm + 4; |
| 417 | } |
| 418 | } |
| 419 | else { |
| 420 | /* mov.[bwl] to/from memory via r0+rn */ |
| 421 | goto simple; |
| 422 | } |
| 423 | break; |
| 424 | |
| 425 | case 0x1000: /* mov.l Rm,@(disp,Rn) */ |
| 426 | goto simple; |
| 427 | |
| 428 | case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */ |
| 429 | goto simple; |
| 430 | |
| 431 | case 0x4000: |
| 432 | if ((instruction&0x00FF)==0x002B) { |
| 433 | /* jmp @Rm */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 434 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (ret==0) |
| 436 | regs->pc = rm; |
| 437 | } |
| 438 | else if ((instruction&0x00FF)==0x000B) { |
| 439 | /* jsr @Rm */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 440 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (ret==0) { |
| 442 | regs->pr = regs->pc + 4; |
| 443 | regs->pc = rm; |
| 444 | } |
| 445 | } |
| 446 | else { |
| 447 | /* mov.[bwl] to/from memory via r0+rn */ |
| 448 | goto simple; |
| 449 | } |
| 450 | break; |
| 451 | |
| 452 | case 0x5000: /* mov.l @(disp,Rm),Rn */ |
| 453 | goto simple; |
| 454 | |
| 455 | case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */ |
| 456 | goto simple; |
| 457 | |
| 458 | case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */ |
| 459 | switch (instruction&0x0F00) { |
| 460 | case 0x0100: /* mov.w R0,@(disp,Rm) */ |
| 461 | goto simple; |
| 462 | case 0x0500: /* mov.w @(disp,Rm),R0 */ |
| 463 | goto simple; |
| 464 | case 0x0B00: /* bf lab - no delayslot*/ |
| 465 | break; |
| 466 | case 0x0F00: /* bf/s lab */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 467 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if (ret==0) { |
| 469 | #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB) |
| 470 | if ((regs->sr & 0x00000001) != 0) |
| 471 | regs->pc += 4; /* next after slot */ |
| 472 | else |
| 473 | #endif |
| 474 | regs->pc += SH_PC_8BIT_OFFSET(instruction); |
| 475 | } |
| 476 | break; |
| 477 | case 0x0900: /* bt lab - no delayslot */ |
| 478 | break; |
| 479 | case 0x0D00: /* bt/s lab */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 480 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (ret==0) { |
| 482 | #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB) |
| 483 | if ((regs->sr & 0x00000001) == 0) |
| 484 | regs->pc += 4; /* next after slot */ |
| 485 | else |
| 486 | #endif |
| 487 | regs->pc += SH_PC_8BIT_OFFSET(instruction); |
| 488 | } |
| 489 | break; |
| 490 | } |
| 491 | break; |
| 492 | |
| 493 | case 0xA000: /* bra label */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 494 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (ret==0) |
| 496 | regs->pc += SH_PC_12BIT_OFFSET(instruction); |
| 497 | break; |
| 498 | |
| 499 | case 0xB000: /* bsr label */ |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 500 | ret = handle_delayslot(regs, instruction, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (ret==0) { |
| 502 | regs->pr = regs->pc + 4; |
| 503 | regs->pc += SH_PC_12BIT_OFFSET(instruction); |
| 504 | } |
| 505 | break; |
| 506 | } |
| 507 | return ret; |
| 508 | |
| 509 | /* handle non-delay-slot instruction */ |
| 510 | simple: |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 511 | ret = handle_unaligned_ins(instruction, regs, ma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (ret==0) |
Paul Mundt | 53f983a | 2007-05-08 15:31:48 +0900 | [diff] [blame] | 513 | regs->pc += instruction_size(instruction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | return ret; |
| 515 | } |
| 516 | |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 517 | #ifdef CONFIG_CPU_HAS_SR_RB |
| 518 | #define lookup_exception_vector(x) \ |
| 519 | __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x))) |
| 520 | #else |
| 521 | #define lookup_exception_vector(x) \ |
| 522 | __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x))) |
| 523 | #endif |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | /* |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 526 | * Handle various address error exceptions: |
| 527 | * - instruction address error: |
| 528 | * misaligned PC |
| 529 | * PC >= 0x80000000 in user mode |
| 530 | * - data address error (read and write) |
| 531 | * misaligned data access |
| 532 | * access to >= 0x80000000 is user mode |
| 533 | * Unfortuntaly we can't distinguish between instruction address error |
Simon Arlott | e868d61 | 2007-05-14 08:15:10 +0900 | [diff] [blame] | 534 | * and data address errors caused by read accesses. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | */ |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 536 | asmlinkage void do_address_error(struct pt_regs *regs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | unsigned long writeaccess, |
| 538 | unsigned long address) |
| 539 | { |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 540 | unsigned long error_code = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | mm_segment_t oldfs; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 542 | siginfo_t info; |
Magnus Damm | 4b5a9ef | 2008-02-07 20:04:12 +0900 | [diff] [blame] | 543 | opcode_t instruction; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | int tmp; |
| 545 | |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 546 | /* Intentional ifdef */ |
| 547 | #ifdef CONFIG_CPU_HAS_SR_RB |
| 548 | lookup_exception_vector(error_code); |
| 549 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | oldfs = get_fs(); |
| 552 | |
| 553 | if (user_mode(regs)) { |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 554 | int si_code = BUS_ADRERR; |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
| 558 | /* bad PC is not something we can fix */ |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 559 | if (regs->pc & 1) { |
| 560 | si_code = BUS_ADRALN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | goto uspace_segv; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | set_fs(USER_DS); |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 565 | if (copy_from_user(&instruction, (void __user *)(regs->pc), |
Magnus Damm | 4b5a9ef | 2008-02-07 20:04:12 +0900 | [diff] [blame] | 566 | sizeof(instruction))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | /* Argh. Fault on the instruction itself. |
| 568 | This should never happen non-SMP |
| 569 | */ |
| 570 | set_fs(oldfs); |
| 571 | goto uspace_segv; |
| 572 | } |
| 573 | |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 574 | tmp = handle_unaligned_access(instruction, regs, |
| 575 | &user_mem_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | set_fs(oldfs); |
| 577 | |
| 578 | if (tmp==0) |
| 579 | return; /* sorted */ |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 580 | uspace_segv: |
| 581 | printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned " |
| 582 | "access (PC %lx PR %lx)\n", current->comm, regs->pc, |
| 583 | regs->pr); |
| 584 | |
| 585 | info.si_signo = SIGBUS; |
| 586 | info.si_errno = 0; |
| 587 | info.si_code = si_code; |
Paul Mundt | e08f457 | 2007-05-14 12:52:56 +0900 | [diff] [blame] | 588 | info.si_addr = (void __user *)address; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 589 | force_sig_info(SIGBUS, &info, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } else { |
| 591 | if (regs->pc & 1) |
| 592 | die("unaligned program counter", regs, error_code); |
| 593 | |
| 594 | set_fs(KERNEL_DS); |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 595 | if (copy_from_user(&instruction, (void __user *)(regs->pc), |
Magnus Damm | 4b5a9ef | 2008-02-07 20:04:12 +0900 | [diff] [blame] | 596 | sizeof(instruction))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | /* Argh. Fault on the instruction itself. |
| 598 | This should never happen non-SMP |
| 599 | */ |
| 600 | set_fs(oldfs); |
| 601 | die("insn faulting in do_address_error", regs, 0); |
| 602 | } |
| 603 | |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 604 | handle_unaligned_access(instruction, regs, &user_mem_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | set_fs(oldfs); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | #ifdef CONFIG_SH_DSP |
| 610 | /* |
| 611 | * SH-DSP support gerg@snapgear.com. |
| 612 | */ |
| 613 | int is_dsp_inst(struct pt_regs *regs) |
| 614 | { |
Paul Mundt | 882c12c | 2007-05-14 17:26:34 +0900 | [diff] [blame] | 615 | unsigned short inst = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 617 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | * Safe guard if DSP mode is already enabled or we're lacking |
| 619 | * the DSP altogether. |
| 620 | */ |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 621 | if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return 0; |
| 623 | |
| 624 | get_user(inst, ((unsigned short *) regs->pc)); |
| 625 | |
| 626 | inst &= 0xf000; |
| 627 | |
| 628 | /* Check for any type of DSP or support instruction */ |
| 629 | if ((inst == 0xf000) || (inst == 0x4000)) |
| 630 | return 1; |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | #else |
| 635 | #define is_dsp_inst(regs) (0) |
| 636 | #endif /* CONFIG_SH_DSP */ |
| 637 | |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 638 | #ifdef CONFIG_CPU_SH2A |
| 639 | asmlinkage void do_divide_error(unsigned long r4, unsigned long r5, |
| 640 | unsigned long r6, unsigned long r7, |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 641 | struct pt_regs __regs) |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 642 | { |
| 643 | siginfo_t info; |
| 644 | |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 645 | switch (r4) { |
| 646 | case TRAP_DIVZERO_ERROR: |
| 647 | info.si_code = FPE_INTDIV; |
| 648 | break; |
| 649 | case TRAP_DIVOVF_ERROR: |
| 650 | info.si_code = FPE_INTOVF; |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | force_sig_info(SIGFPE, &info, current); |
| 655 | } |
| 656 | #endif |
| 657 | |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 658 | asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5, |
| 659 | unsigned long r6, unsigned long r7, |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 660 | struct pt_regs __regs) |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 661 | { |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 662 | struct pt_regs *regs = RELOC_HIDE(&__regs, 0); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 663 | unsigned long error_code; |
| 664 | struct task_struct *tsk = current; |
| 665 | |
| 666 | #ifdef CONFIG_SH_FPU_EMU |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 667 | unsigned short inst = 0; |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 668 | int err; |
| 669 | |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 670 | get_user(inst, (unsigned short*)regs->pc); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 671 | |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 672 | err = do_fpu_inst(inst, regs); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 673 | if (!err) { |
Paul Mundt | 53f983a | 2007-05-08 15:31:48 +0900 | [diff] [blame] | 674 | regs->pc += instruction_size(inst); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 675 | return; |
| 676 | } |
| 677 | /* not a FPU inst. */ |
| 678 | #endif |
| 679 | |
| 680 | #ifdef CONFIG_SH_DSP |
| 681 | /* Check if it's a DSP instruction */ |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 682 | if (is_dsp_inst(regs)) { |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 683 | /* Enable DSP mode, and restart instruction. */ |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 684 | regs->sr |= SR_DSP; |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 685 | return; |
| 686 | } |
| 687 | #endif |
| 688 | |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 689 | lookup_exception_vector(error_code); |
| 690 | |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 691 | local_irq_enable(); |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 692 | CHK_REMOTE_DEBUG(regs); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 693 | force_sig(SIGILL, tsk); |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 694 | die_if_no_fixup("reserved instruction", regs, error_code); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | #ifdef CONFIG_SH_FPU_EMU |
| 698 | static int emulate_branch(unsigned short inst, struct pt_regs* regs) |
| 699 | { |
| 700 | /* |
| 701 | * bfs: 8fxx: PC+=d*2+4; |
| 702 | * bts: 8dxx: PC+=d*2+4; |
| 703 | * bra: axxx: PC+=D*2+4; |
| 704 | * bsr: bxxx: PC+=D*2+4 after PR=PC+4; |
| 705 | * braf:0x23: PC+=Rn*2+4; |
| 706 | * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4; |
| 707 | * jmp: 4x2b: PC=Rn; |
| 708 | * jsr: 4x0b: PC=Rn after PR=PC+4; |
| 709 | * rts: 000b: PC=PR; |
| 710 | */ |
| 711 | if ((inst & 0xfd00) == 0x8d00) { |
| 712 | regs->pc += SH_PC_8BIT_OFFSET(inst); |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | if ((inst & 0xe000) == 0xa000) { |
| 717 | regs->pc += SH_PC_12BIT_OFFSET(inst); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | if ((inst & 0xf0df) == 0x0003) { |
| 722 | regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4; |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | if ((inst & 0xf0df) == 0x400b) { |
| 727 | regs->pc = regs->regs[(inst & 0x0f00) >> 8]; |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | if ((inst & 0xffff) == 0x000b) { |
| 732 | regs->pc = regs->pr; |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | return 1; |
| 737 | } |
| 738 | #endif |
| 739 | |
| 740 | asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5, |
| 741 | unsigned long r6, unsigned long r7, |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 742 | struct pt_regs __regs) |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 743 | { |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 744 | struct pt_regs *regs = RELOC_HIDE(&__regs, 0); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 745 | unsigned long error_code; |
| 746 | struct task_struct *tsk = current; |
Chris Smith | d39f545 | 2008-09-05 17:15:39 +0900 | [diff] [blame^] | 747 | |
| 748 | if (kprobe_handle_illslot(regs->pc) == 0) |
| 749 | return; |
| 750 | |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 751 | #ifdef CONFIG_SH_FPU_EMU |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 752 | unsigned short inst = 0; |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 753 | |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 754 | get_user(inst, (unsigned short *)regs->pc + 1); |
| 755 | if (!do_fpu_inst(inst, regs)) { |
| 756 | get_user(inst, (unsigned short *)regs->pc); |
| 757 | if (!emulate_branch(inst, regs)) |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 758 | return; |
| 759 | /* fault in branch.*/ |
| 760 | } |
| 761 | /* not a FPU inst. */ |
| 762 | #endif |
| 763 | |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 764 | lookup_exception_vector(error_code); |
| 765 | |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 766 | local_irq_enable(); |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 767 | CHK_REMOTE_DEBUG(regs); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 768 | force_sig(SIGILL, tsk); |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 769 | die_if_no_fixup("illegal slot instruction", regs, error_code); |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
| 772 | asmlinkage void do_exception_error(unsigned long r4, unsigned long r5, |
| 773 | unsigned long r6, unsigned long r7, |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 774 | struct pt_regs __regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 776 | struct pt_regs *regs = RELOC_HIDE(&__regs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | long ex; |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 778 | |
| 779 | lookup_exception_vector(ex); |
Stuart Menefy | f0bc814 | 2006-11-21 11:16:57 +0900 | [diff] [blame] | 780 | die_if_kernel("exception", regs, ex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | #if defined(CONFIG_SH_STANDARD_BIOS) |
| 784 | void *gdb_vbr_vector; |
| 785 | |
| 786 | static inline void __init gdb_vbr_init(void) |
| 787 | { |
| 788 | register unsigned long vbr; |
| 789 | |
| 790 | /* |
| 791 | * Read the old value of the VBR register to initialise |
| 792 | * the vector through which debug and BIOS traps are |
| 793 | * delegated by the Linux trap handler. |
| 794 | */ |
| 795 | asm volatile("stc vbr, %0" : "=r" (vbr)); |
| 796 | |
| 797 | gdb_vbr_vector = (void *)(vbr + 0x100); |
| 798 | printk("Setting GDB trap vector to 0x%08lx\n", |
| 799 | (unsigned long)gdb_vbr_vector); |
| 800 | } |
| 801 | #endif |
| 802 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 803 | void __cpuinit per_cpu_trap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | { |
| 805 | extern void *vbr_base; |
| 806 | |
| 807 | #ifdef CONFIG_SH_STANDARD_BIOS |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 808 | if (raw_smp_processor_id() == 0) |
| 809 | gdb_vbr_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | #endif |
| 811 | |
| 812 | /* NOTE: The VBR value should be at P1 |
| 813 | (or P2, virtural "fixed" address space). |
| 814 | It's definitely should not in physical address. */ |
| 815 | |
| 816 | asm volatile("ldc %0, vbr" |
| 817 | : /* no output */ |
| 818 | : "r" (&vbr_base) |
| 819 | : "memory"); |
| 820 | } |
| 821 | |
Paul Mundt | 1f66658 | 2006-10-19 16:20:25 +0900 | [diff] [blame] | 822 | void *set_exception_table_vec(unsigned int vec, void *handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
| 824 | extern void *exception_handling_table[]; |
Paul Mundt | 1f66658 | 2006-10-19 16:20:25 +0900 | [diff] [blame] | 825 | void *old_handler; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 826 | |
Paul Mundt | 1f66658 | 2006-10-19 16:20:25 +0900 | [diff] [blame] | 827 | old_handler = exception_handling_table[vec]; |
| 828 | exception_handling_table[vec] = handler; |
| 829 | return old_handler; |
| 830 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
Paul Mundt | 1f66658 | 2006-10-19 16:20:25 +0900 | [diff] [blame] | 832 | void __init trap_init(void) |
| 833 | { |
| 834 | set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst); |
| 835 | set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
Takashi YOSHII | 4b56568 | 2006-09-27 17:15:32 +0900 | [diff] [blame] | 837 | #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \ |
| 838 | defined(CONFIG_SH_FPU_EMU) |
| 839 | /* |
| 840 | * For SH-4 lacking an FPU, treat floating point instructions as |
| 841 | * reserved. They'll be handled in the math-emu case, or faulted on |
| 842 | * otherwise. |
| 843 | */ |
Paul Mundt | 1f66658 | 2006-10-19 16:20:25 +0900 | [diff] [blame] | 844 | set_exception_table_evt(0x800, do_reserved_inst); |
| 845 | set_exception_table_evt(0x820, do_illegal_slot_inst); |
| 846 | #elif defined(CONFIG_SH_FPU) |
Paul Mundt | e0a3647 | 2007-08-01 16:55:07 +0900 | [diff] [blame] | 847 | #ifdef CONFIG_CPU_SUBTYPE_SHX3 |
Paul Mundt | 74d99a5 | 2007-11-26 20:38:36 +0900 | [diff] [blame] | 848 | set_exception_table_evt(0xd80, fpu_state_restore_trap_handler); |
| 849 | set_exception_table_evt(0xda0, fpu_state_restore_trap_handler); |
Paul Mundt | e0a3647 | 2007-08-01 16:55:07 +0900 | [diff] [blame] | 850 | #else |
Paul Mundt | 74d99a5 | 2007-11-26 20:38:36 +0900 | [diff] [blame] | 851 | set_exception_table_evt(0x800, fpu_state_restore_trap_handler); |
| 852 | set_exception_table_evt(0x820, fpu_state_restore_trap_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | #endif |
Paul Mundt | e0a3647 | 2007-08-01 16:55:07 +0900 | [diff] [blame] | 854 | #endif |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 855 | |
| 856 | #ifdef CONFIG_CPU_SH2 |
Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 857 | set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler); |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 858 | #endif |
| 859 | #ifdef CONFIG_CPU_SH2A |
| 860 | set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error); |
| 861 | set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error); |
Yoshinori Sato | 6e80f5e | 2008-07-10 01:20:03 +0900 | [diff] [blame] | 862 | #ifdef CONFIG_SH_FPU |
| 863 | set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler); |
| 864 | #endif |
Yoshinori Sato | 0983b31 | 2006-11-05 15:58:47 +0900 | [diff] [blame] | 865 | #endif |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | /* Setup VBR for boot cpu */ |
| 868 | per_cpu_trap_init(); |
| 869 | } |
| 870 | |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 871 | void show_trace(struct task_struct *tsk, unsigned long *sp, |
| 872 | struct pt_regs *regs) |
| 873 | { |
| 874 | unsigned long addr; |
| 875 | |
| 876 | if (regs && user_mode(regs)) |
| 877 | return; |
| 878 | |
| 879 | printk("\nCall trace: "); |
| 880 | #ifdef CONFIG_KALLSYMS |
| 881 | printk("\n"); |
| 882 | #endif |
| 883 | |
| 884 | while (!kstack_end(sp)) { |
| 885 | addr = *sp++; |
| 886 | if (kernel_text_address(addr)) |
| 887 | print_ip_sym(addr); |
| 888 | } |
| 889 | |
| 890 | printk("\n"); |
Paul Mundt | 9b8c90e | 2006-12-06 11:07:51 +0900 | [diff] [blame] | 891 | |
| 892 | if (!tsk) |
| 893 | tsk = current; |
| 894 | |
| 895 | debug_show_held_locks(tsk); |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 896 | } |
| 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | void show_stack(struct task_struct *tsk, unsigned long *sp) |
| 899 | { |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 900 | unsigned long stack; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
Paul Mundt | a6a31139 | 2006-09-27 18:22:14 +0900 | [diff] [blame] | 902 | if (!tsk) |
| 903 | tsk = current; |
| 904 | if (tsk == current) |
| 905 | sp = (unsigned long *)current_stack_pointer; |
| 906 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | sp = (unsigned long *)tsk->thread.sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Paul Mundt | 6b00223 | 2006-10-12 17:07:45 +0900 | [diff] [blame] | 909 | stack = (unsigned long)sp; |
| 910 | dump_mem("Stack: ", stack, THREAD_SIZE + |
| 911 | (unsigned long)task_stack_page(tsk)); |
| 912 | show_trace(tsk, sp, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void dump_stack(void) |
| 916 | { |
| 917 | show_stack(NULL, NULL); |
| 918 | } |
| 919 | EXPORT_SYMBOL(dump_stack); |