blob: b359b08a8e33a9cb827145abe0c02c6ad66dc9d9 [file] [log] [blame]
Paul Mundt6b002232006-10-12 17:07:45 +09001/*
2 * 'traps.c' handles hardware traps and faults after we have saved some
3 * state in 'entry.S'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * SuperH version: Copyright (C) 1999 Niibe Yutaka
6 * Copyright (C) 2000 Philipp Rumpf
7 * Copyright (C) 2000 David Howells
Paul Mundt3a2e1172007-05-01 16:33:10 +09008 * Copyright (C) 2002 - 2007 Paul Mundt
Paul Mundt6b002232006-10-12 17:07:45 +09009 *
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 Torvalds1da177e2005-04-16 15:20:36 -070013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/spinlock.h>
18#include <linux/module.h>
19#include <linux/kallsyms.h>
Paul Mundt1f666582006-10-19 16:20:25 +090020#include <linux/io.h>
Paul Mundtfa691512007-03-08 19:41:21 +090021#include <linux/bug.h>
Paul Mundt9b8c90e2006-12-06 11:07:51 +090022#include <linux/debug_locks.h>
Paul Mundtb118ca52007-05-09 10:55:38 +090023#include <linux/kdebug.h>
Paul Mundte1132762007-05-15 08:36:36 +090024#include <linux/kexec.h>
Paul Mundtdc34d312006-12-08 17:41:43 +090025#include <linux/limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/system.h>
27#include <asm/uaccess.h>
Andrew Mortonfad0f902008-04-16 02:03:51 +090028#include <asm/fpu.h>
Chris Smithd39f5452008-09-05 17:15:39 +090029#include <asm/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#ifdef CONFIG_SH_KGDB
32#include <asm/kgdb.h>
Stuart Menefyf0bc8142006-11-21 11:16:57 +090033#define CHK_REMOTE_DEBUG(regs) \
34{ \
Takashi YOSHII4b565682006-09-27 17:15:32 +090035 if (kgdb_debug_hook && !user_mode(regs))\
36 (*kgdb_debug_hook)(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38#else
39#define CHK_REMOTE_DEBUG(regs)
40#endif
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef CONFIG_CPU_SH2
Yoshinori Sato0983b312006-11-05 15:58:47 +090043# define TRAP_RESERVED_INST 4
44# define TRAP_ILLEGAL_SLOT_INST 6
45# define TRAP_ADDRESS_ERROR 9
46# ifdef CONFIG_CPU_SH2A
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +090047# define TRAP_FPU_ERROR 13
Yoshinori Sato0983b312006-11-05 15:58:47 +090048# define TRAP_DIVZERO_ERROR 17
49# define TRAP_DIVOVF_ERROR 18
50# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#else
52#define TRAP_RESERVED_INST 12
53#define TRAP_ILLEGAL_SLOT_INST 13
54#endif
55
Paul Mundt6b002232006-10-12 17:07:45 +090056static 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 Torvalds1da177e2005-04-16 15:20:36 -070082
Paul Mundt3a2e1172007-05-01 16:33:10 +090083static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85void die(const char * str, struct pt_regs * regs, long err)
86{
87 static int die_counter;
88
Paul Mundt55273982007-06-18 18:57:13 +090089 oops_enter();
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 console_verbose();
92 spin_lock_irq(&die_lock);
Paul Mundt6b002232006-10-12 17:07:45 +090093 bust_spinlocks(1);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Paul Mundt6b002232006-10-12 17:07:45 +090096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 CHK_REMOTE_DEBUG(regs);
Paul Mundt6b002232006-10-12 17:07:45 +090098 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 show_regs(regs);
Paul Mundt6b002232006-10-12 17:07:45 +0900100
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700101 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
102 task_pid_nr(current), task_stack_page(current) + 1);
Paul Mundt6b002232006-10-12 17:07:45 +0900103
104 if (!user_mode(regs) || in_interrupt())
105 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900106 (unsigned long)task_stack_page(current));
Paul Mundt6b002232006-10-12 17:07:45 +0900107
108 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700109 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 spin_unlock_irq(&die_lock);
Paul Mundte1132762007-05-15 08:36:36 +0900111
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 Mundt55273982007-06-18 18:57:13 +0900121 oops_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 do_exit(SIGSEGV);
123}
124
Paul Mundt6b002232006-10-12 17:07:45 +0900125static inline void die_if_kernel(const char *str, struct pt_regs *regs,
126 long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 if (!user_mode(regs))
129 die(str, regs, err);
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
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 */
139static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
140{
Paul Mundt6b002232006-10-12 17:07:45 +0900141 if (!user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 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 Damm86c01792008-02-07 00:02:50 +0900153static inline void sign_extend(unsigned int count, unsigned char *dst)
154{
155#ifdef __LITTLE_ENDIAN__
Magnus Damm4252c652008-02-07 19:58:46 +0900156 if ((count == 1) && dst[0] & 0x80) {
157 dst[1] = 0xff;
158 dst[2] = 0xff;
159 dst[3] = 0xff;
160 }
Magnus Damm86c01792008-02-07 00:02:50 +0900161 if ((count == 2) && dst[1] & 0x80) {
162 dst[2] = 0xff;
163 dst[3] = 0xff;
164 }
165#else
Magnus Damm4252c652008-02-07 19:58:46 +0900166 if ((count == 1) && dst[3] & 0x80) {
167 dst[2] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900168 dst[1] = 0xff;
Magnus Damm4252c652008-02-07 19:58:46 +0900169 dst[0] = 0xff;
170 }
171 if ((count == 2) && dst[2] & 0x80) {
172 dst[1] = 0xff;
173 dst[0] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900174 }
175#endif
176}
177
Magnus Damme7cc9a72008-02-07 20:18:21 +0900178static struct mem_access user_mem_access = {
179 copy_from_user,
180 copy_to_user,
181};
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
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 Damme7cc9a72008-02-07 20:18:21 +0900190static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
191 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int ret, index, count;
194 unsigned long *rm, *rn;
195 unsigned char *src, *dst;
Paul Mundtfa439722008-09-04 18:53:58 +0900196 unsigned char __user *srcu, *dstu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 index = (instruction>>8)&15; /* 0x0F00 */
199 rn = &regs->regs[index];
200
201 index = (instruction>>4)&15; /* 0x00F0 */
202 rm = &regs->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 Mundtfa439722008-09-04 18:53:58 +0900211 srcu = (unsigned char __user *)*rm;
212 srcu += regs->regs[0];
213 dst = (unsigned char *)rn;
214 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Magnus Damm86c01792008-02-07 00:02:50 +0900216#if !defined(__LITTLE_ENDIAN__)
217 dst += 4-count;
218#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900219 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto fetch_fault;
221
Magnus Damm86c01792008-02-07 00:02:50 +0900222 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else {
224 /* to memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900225 src = (unsigned char *)rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#if !defined(__LITTLE_ENDIAN__)
227 src += 4-count;
228#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900229 dstu = (unsigned char __user *)*rn;
230 dstu += regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Paul Mundtfa439722008-09-04 18:53:58 +0900232 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 goto fetch_fault;
234 }
235 ret = 0;
236 break;
237
238 case 1: /* mov.l Rm,@(disp,Rn) */
239 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900240 dstu = (unsigned char __user *)*rn;
241 dstu += (instruction&0x000F)<<2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Paul Mundtfa439722008-09-04 18:53:58 +0900243 if (ma->to(dstu, src, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto fetch_fault;
245 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
249 if (instruction & 4)
250 *rn -= count;
251 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900252 dstu = (unsigned char __user *)*rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#if !defined(__LITTLE_ENDIAN__)
254 src += 4-count;
255#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900256 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto fetch_fault;
258 ret = 0;
259 break;
260
261 case 5: /* mov.l @(disp,Rm),Rn */
Paul Mundtfa439722008-09-04 18:53:58 +0900262 srcu = (unsigned char __user *)*rm;
263 srcu += (instruction & 0x000F) << 2;
264 dst = (unsigned char *)rn;
265 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Paul Mundtfa439722008-09-04 18:53:58 +0900267 if (ma->from(dst, srcu, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto fetch_fault;
269 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 case 6: /* mov.[bwl] from memory, possibly with post-increment */
Paul Mundtfa439722008-09-04 18:53:58 +0900273 srcu = (unsigned char __user *)*rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (instruction & 4)
275 *rm += count;
276 dst = (unsigned char*) rn;
277 *(unsigned long*)dst = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900278
Magnus Damm86c01792008-02-07 00:02:50 +0900279#if !defined(__LITTLE_ENDIAN__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dst += 4-count;
Magnus Damm86c01792008-02-07 00:02:50 +0900281#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900282 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900284 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 ret = 0;
286 break;
287
288 case 8:
289 switch ((instruction&0xFF00)>>8) {
290 case 0x81: /* mov.w R0,@(disp,Rn) */
Paul Mundtfa439722008-09-04 18:53:58 +0900291 src = (unsigned char *) &regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292#if !defined(__LITTLE_ENDIAN__)
293 src += 2;
294#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900295 dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
296 dstu += (instruction & 0x000F) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Paul Mundtfa439722008-09-04 18:53:58 +0900298 if (ma->to(dstu, src, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto fetch_fault;
300 ret = 0;
301 break;
302
303 case 0x85: /* mov.w @(disp,Rm),R0 */
Paul Mundtfa439722008-09-04 18:53:58 +0900304 srcu = (unsigned char __user *)*rm;
305 srcu += (instruction & 0x000F) << 1;
306 dst = (unsigned char *) &regs->regs[0];
307 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309#if !defined(__LITTLE_ENDIAN__)
310 dst += 2;
311#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900312 if (ma->from(dst, srcu, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900314 sign_extend(2, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 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 Damme7cc9a72008-02-07 20:18:21 +0900333static inline int handle_delayslot(struct pt_regs *regs,
334 opcode_t old_instruction,
335 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900337 opcode_t instruction;
Paul Mundtfa439722008-09-04 18:53:58 +0900338 void __user *addr = (void __user *)(regs->pc +
339 instruction_size(old_instruction));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900341 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* the instruction-fetch faulted */
343 if (user_mode(regs))
344 return -EFAULT;
345
346 /* kernel */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900347 die("delay-slot-insn faulting in handle_unaligned_delayslot",
348 regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
Magnus Damme7cc9a72008-02-07 20:18:21 +0900351 return handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
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 Mundt710ee0c2006-11-05 16:48:42 +0900370/*
371 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
372 * opcodes..
373 */
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900374
Paul Mundt710ee0c2006-11-05 16:48:42 +0900375static int handle_unaligned_notify_count = 10;
376
Magnus Damme7cc9a72008-02-07 20:18:21 +0900377int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs,
378 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
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 Menefyb5a1bcb2006-11-21 13:34:04 +0900390 printk(KERN_NOTICE "Fixing up unaligned userspace access "
391 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700392 current->comm, task_pid_nr(current),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900393 (void *)regs->pc, instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 ret = -EFAULT;
397 switch (instruction&0xF000) {
398 case 0x0000:
399 if (instruction==0x000B) {
400 /* rts */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900401 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (ret==0)
403 regs->pc = regs->pr;
404 }
405 else if ((instruction&0x00FF)==0x0023) {
406 /* braf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900407 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (ret==0)
409 regs->pc += rm + 4;
410 }
411 else if ((instruction&0x00FF)==0x0003) {
412 /* bsrf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900413 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 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 Damme7cc9a72008-02-07 20:18:21 +0900434 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (ret==0)
436 regs->pc = rm;
437 }
438 else if ((instruction&0x00FF)==0x000B) {
439 /* jsr @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900440 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 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 Damme7cc9a72008-02-07 20:18:21 +0900467 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 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 Damme7cc9a72008-02-07 20:18:21 +0900480 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 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 Damme7cc9a72008-02-07 20:18:21 +0900494 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (ret==0)
496 regs->pc += SH_PC_12BIT_OFFSET(instruction);
497 break;
498
499 case 0xB000: /* bsr label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900500 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 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 Damme7cc9a72008-02-07 20:18:21 +0900511 ret = handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (ret==0)
Paul Mundt53f983a2007-05-08 15:31:48 +0900513 regs->pc += instruction_size(instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return ret;
515}
516
517/*
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900518 * Handle various address error exceptions:
519 * - instruction address error:
520 * misaligned PC
521 * PC >= 0x80000000 in user mode
522 * - data address error (read and write)
523 * misaligned data access
524 * access to >= 0x80000000 is user mode
525 * Unfortuntaly we can't distinguish between instruction address error
Simon Arlotte868d612007-05-14 08:15:10 +0900526 * and data address errors caused by read accesses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900528asmlinkage void do_address_error(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 unsigned long writeaccess,
530 unsigned long address)
531{
Yoshinori Sato0983b312006-11-05 15:58:47 +0900532 unsigned long error_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 mm_segment_t oldfs;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900534 siginfo_t info;
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900535 opcode_t instruction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int tmp;
537
Yoshinori Sato0983b312006-11-05 15:58:47 +0900538 /* Intentional ifdef */
539#ifdef CONFIG_CPU_HAS_SR_RB
Paul Mundt4c59e292008-09-21 12:00:23 +0900540 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900541#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 oldfs = get_fs();
544
545 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900546 int si_code = BUS_ADRERR;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* bad PC is not something we can fix */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900551 if (regs->pc & 1) {
552 si_code = BUS_ADRALN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto uspace_segv;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 set_fs(USER_DS);
Paul Mundtfa439722008-09-04 18:53:58 +0900557 if (copy_from_user(&instruction, (void __user *)(regs->pc),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900558 sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Argh. Fault on the instruction itself.
560 This should never happen non-SMP
561 */
562 set_fs(oldfs);
563 goto uspace_segv;
564 }
565
Magnus Damme7cc9a72008-02-07 20:18:21 +0900566 tmp = handle_unaligned_access(instruction, regs,
567 &user_mem_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 set_fs(oldfs);
569
570 if (tmp==0)
571 return; /* sorted */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900572uspace_segv:
573 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
574 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
575 regs->pr);
576
577 info.si_signo = SIGBUS;
578 info.si_errno = 0;
579 info.si_code = si_code;
Paul Mundte08f4572007-05-14 12:52:56 +0900580 info.si_addr = (void __user *)address;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900581 force_sig_info(SIGBUS, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 } else {
583 if (regs->pc & 1)
584 die("unaligned program counter", regs, error_code);
585
586 set_fs(KERNEL_DS);
Paul Mundtfa439722008-09-04 18:53:58 +0900587 if (copy_from_user(&instruction, (void __user *)(regs->pc),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900588 sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Argh. Fault on the instruction itself.
590 This should never happen non-SMP
591 */
592 set_fs(oldfs);
593 die("insn faulting in do_address_error", regs, 0);
594 }
595
Magnus Damme7cc9a72008-02-07 20:18:21 +0900596 handle_unaligned_access(instruction, regs, &user_mem_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 set_fs(oldfs);
598 }
599}
600
601#ifdef CONFIG_SH_DSP
602/*
603 * SH-DSP support gerg@snapgear.com.
604 */
605int is_dsp_inst(struct pt_regs *regs)
606{
Paul Mundt882c12c2007-05-14 17:26:34 +0900607 unsigned short inst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900609 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * Safe guard if DSP mode is already enabled or we're lacking
611 * the DSP altogether.
612 */
Paul Mundt11c19652006-12-25 10:19:56 +0900613 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 0;
615
616 get_user(inst, ((unsigned short *) regs->pc));
617
618 inst &= 0xf000;
619
620 /* Check for any type of DSP or support instruction */
621 if ((inst == 0xf000) || (inst == 0x4000))
622 return 1;
623
624 return 0;
625}
626#else
627#define is_dsp_inst(regs) (0)
628#endif /* CONFIG_SH_DSP */
629
Yoshinori Sato0983b312006-11-05 15:58:47 +0900630#ifdef CONFIG_CPU_SH2A
631asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
632 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900633 struct pt_regs __regs)
Yoshinori Sato0983b312006-11-05 15:58:47 +0900634{
635 siginfo_t info;
636
Yoshinori Sato0983b312006-11-05 15:58:47 +0900637 switch (r4) {
638 case TRAP_DIVZERO_ERROR:
639 info.si_code = FPE_INTDIV;
640 break;
641 case TRAP_DIVOVF_ERROR:
642 info.si_code = FPE_INTOVF;
643 break;
644 }
645
646 force_sig_info(SIGFPE, &info, current);
647}
648#endif
649
Takashi YOSHII4b565682006-09-27 17:15:32 +0900650asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
651 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900652 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900653{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900654 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900655 unsigned long error_code;
656 struct task_struct *tsk = current;
657
658#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900659 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900660 int err;
661
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900662 get_user(inst, (unsigned short*)regs->pc);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900663
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900664 err = do_fpu_inst(inst, regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900665 if (!err) {
Paul Mundt53f983a2007-05-08 15:31:48 +0900666 regs->pc += instruction_size(inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900667 return;
668 }
669 /* not a FPU inst. */
670#endif
671
672#ifdef CONFIG_SH_DSP
673 /* Check if it's a DSP instruction */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900674 if (is_dsp_inst(regs)) {
Takashi YOSHII4b565682006-09-27 17:15:32 +0900675 /* Enable DSP mode, and restart instruction. */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900676 regs->sr |= SR_DSP;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900677 return;
678 }
679#endif
680
Paul Mundt4c59e292008-09-21 12:00:23 +0900681 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900682
Takashi YOSHII4b565682006-09-27 17:15:32 +0900683 local_irq_enable();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900684 CHK_REMOTE_DEBUG(regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900685 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900686 die_if_no_fixup("reserved instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900687}
688
689#ifdef CONFIG_SH_FPU_EMU
690static int emulate_branch(unsigned short inst, struct pt_regs* regs)
691{
692 /*
693 * bfs: 8fxx: PC+=d*2+4;
694 * bts: 8dxx: PC+=d*2+4;
695 * bra: axxx: PC+=D*2+4;
696 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
697 * braf:0x23: PC+=Rn*2+4;
698 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
699 * jmp: 4x2b: PC=Rn;
700 * jsr: 4x0b: PC=Rn after PR=PC+4;
701 * rts: 000b: PC=PR;
702 */
703 if ((inst & 0xfd00) == 0x8d00) {
704 regs->pc += SH_PC_8BIT_OFFSET(inst);
705 return 0;
706 }
707
708 if ((inst & 0xe000) == 0xa000) {
709 regs->pc += SH_PC_12BIT_OFFSET(inst);
710 return 0;
711 }
712
713 if ((inst & 0xf0df) == 0x0003) {
714 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
715 return 0;
716 }
717
718 if ((inst & 0xf0df) == 0x400b) {
719 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
720 return 0;
721 }
722
723 if ((inst & 0xffff) == 0x000b) {
724 regs->pc = regs->pr;
725 return 0;
726 }
727
728 return 1;
729}
730#endif
731
732asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
733 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900734 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900735{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900736 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900737 unsigned long inst;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900738 struct task_struct *tsk = current;
Chris Smithd39f5452008-09-05 17:15:39 +0900739
740 if (kprobe_handle_illslot(regs->pc) == 0)
741 return;
742
Takashi YOSHII4b565682006-09-27 17:15:32 +0900743#ifdef CONFIG_SH_FPU_EMU
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900744 get_user(inst, (unsigned short *)regs->pc + 1);
745 if (!do_fpu_inst(inst, regs)) {
746 get_user(inst, (unsigned short *)regs->pc);
747 if (!emulate_branch(inst, regs))
Takashi YOSHII4b565682006-09-27 17:15:32 +0900748 return;
749 /* fault in branch.*/
750 }
751 /* not a FPU inst. */
752#endif
753
Paul Mundt4c59e292008-09-21 12:00:23 +0900754 inst = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900755
Takashi YOSHII4b565682006-09-27 17:15:32 +0900756 local_irq_enable();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900757 CHK_REMOTE_DEBUG(regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900758 force_sig(SIGILL, tsk);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900759 die_if_no_fixup("illegal slot instruction", regs, inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
763 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900764 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900766 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 long ex;
Yoshinori Sato0983b312006-11-05 15:58:47 +0900768
Paul Mundt4c59e292008-09-21 12:00:23 +0900769 ex = lookup_exception_vector();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900770 die_if_kernel("exception", regs, ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773#if defined(CONFIG_SH_STANDARD_BIOS)
774void *gdb_vbr_vector;
775
776static inline void __init gdb_vbr_init(void)
777{
778 register unsigned long vbr;
779
780 /*
781 * Read the old value of the VBR register to initialise
782 * the vector through which debug and BIOS traps are
783 * delegated by the Linux trap handler.
784 */
785 asm volatile("stc vbr, %0" : "=r" (vbr));
786
787 gdb_vbr_vector = (void *)(vbr + 0x100);
788 printk("Setting GDB trap vector to 0x%08lx\n",
789 (unsigned long)gdb_vbr_vector);
790}
791#endif
792
Paul Mundtaba10302007-09-21 18:32:32 +0900793void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 extern void *vbr_base;
796
797#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtaba10302007-09-21 18:32:32 +0900798 if (raw_smp_processor_id() == 0)
799 gdb_vbr_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800#endif
801
802 /* NOTE: The VBR value should be at P1
803 (or P2, virtural "fixed" address space).
804 It's definitely should not in physical address. */
805
806 asm volatile("ldc %0, vbr"
807 : /* no output */
808 : "r" (&vbr_base)
809 : "memory");
810}
811
Paul Mundt1f666582006-10-19 16:20:25 +0900812void *set_exception_table_vec(unsigned int vec, void *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 extern void *exception_handling_table[];
Paul Mundt1f666582006-10-19 16:20:25 +0900815 void *old_handler;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900816
Paul Mundt1f666582006-10-19 16:20:25 +0900817 old_handler = exception_handling_table[vec];
818 exception_handling_table[vec] = handler;
819 return old_handler;
820}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Paul Mundt1f666582006-10-19 16:20:25 +0900822void __init trap_init(void)
823{
824 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
825 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Takashi YOSHII4b565682006-09-27 17:15:32 +0900827#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
828 defined(CONFIG_SH_FPU_EMU)
829 /*
830 * For SH-4 lacking an FPU, treat floating point instructions as
831 * reserved. They'll be handled in the math-emu case, or faulted on
832 * otherwise.
833 */
Paul Mundt1f666582006-10-19 16:20:25 +0900834 set_exception_table_evt(0x800, do_reserved_inst);
835 set_exception_table_evt(0x820, do_illegal_slot_inst);
836#elif defined(CONFIG_SH_FPU)
Paul Mundte0a36472007-08-01 16:55:07 +0900837#ifdef CONFIG_CPU_SUBTYPE_SHX3
Paul Mundt74d99a52007-11-26 20:38:36 +0900838 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
839 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
Paul Mundte0a36472007-08-01 16:55:07 +0900840#else
Paul Mundt74d99a52007-11-26 20:38:36 +0900841 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
842 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843#endif
Paul Mundte0a36472007-08-01 16:55:07 +0900844#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900845
846#ifdef CONFIG_CPU_SH2
Paul Mundt5a4f7c62007-11-20 18:08:06 +0900847 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900848#endif
849#ifdef CONFIG_CPU_SH2A
850 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
851 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +0900852#ifdef CONFIG_SH_FPU
853 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
854#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900855#endif
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* Setup VBR for boot cpu */
858 per_cpu_trap_init();
859}
860
Paul Mundt6b002232006-10-12 17:07:45 +0900861void show_trace(struct task_struct *tsk, unsigned long *sp,
862 struct pt_regs *regs)
863{
864 unsigned long addr;
865
866 if (regs && user_mode(regs))
867 return;
868
869 printk("\nCall trace: ");
870#ifdef CONFIG_KALLSYMS
871 printk("\n");
872#endif
873
874 while (!kstack_end(sp)) {
875 addr = *sp++;
876 if (kernel_text_address(addr))
877 print_ip_sym(addr);
878 }
879
880 printk("\n");
Paul Mundt9b8c90e2006-12-06 11:07:51 +0900881
882 if (!tsk)
883 tsk = current;
884
885 debug_show_held_locks(tsk);
Paul Mundt6b002232006-10-12 17:07:45 +0900886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888void show_stack(struct task_struct *tsk, unsigned long *sp)
889{
Paul Mundt6b002232006-10-12 17:07:45 +0900890 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Paul Mundta6a311392006-09-27 18:22:14 +0900892 if (!tsk)
893 tsk = current;
894 if (tsk == current)
895 sp = (unsigned long *)current_stack_pointer;
896 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Paul Mundt6b002232006-10-12 17:07:45 +0900899 stack = (unsigned long)sp;
900 dump_mem("Stack: ", stack, THREAD_SIZE +
901 (unsigned long)task_stack_page(tsk));
902 show_trace(tsk, sp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905void dump_stack(void)
906{
907 show_stack(NULL, NULL);
908}
909EXPORT_SYMBOL(dump_stack);