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