blob: e08b3bfeb6566fc430e5a50c0ed93cde07eedf3c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#ifdef CONFIG_SH_KGDB
31#include <asm/kgdb.h>
Stuart Menefyf0bc8142006-11-21 11:16:57 +090032#define CHK_REMOTE_DEBUG(regs) \
33{ \
Takashi YOSHII4b565682006-09-27 17:15:32 +090034 if (kgdb_debug_hook && !user_mode(regs))\
35 (*kgdb_debug_hook)(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37#else
38#define CHK_REMOTE_DEBUG(regs)
39#endif
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef CONFIG_CPU_SH2
Yoshinori Sato0983b312006-11-05 15:58:47 +090042# define TRAP_RESERVED_INST 4
43# define TRAP_ILLEGAL_SLOT_INST 6
44# define TRAP_ADDRESS_ERROR 9
45# ifdef CONFIG_CPU_SH2A
46# define TRAP_DIVZERO_ERROR 17
47# define TRAP_DIVOVF_ERROR 18
48# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#else
50#define TRAP_RESERVED_INST 12
51#define TRAP_ILLEGAL_SLOT_INST 13
52#endif
53
Paul Mundt6b002232006-10-12 17:07:45 +090054static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
55{
56 unsigned long p;
57 int i;
58
59 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
60
61 for (p = bottom & ~31; p < top; ) {
62 printk("%04lx: ", p & 0xffff);
63
64 for (i = 0; i < 8; i++, p += 4) {
65 unsigned int val;
66
67 if (p < bottom || p >= top)
68 printk(" ");
69 else {
70 if (__get_user(val, (unsigned int __user *)p)) {
71 printk("\n");
72 return;
73 }
74 printk("%08x ", val);
75 }
76 }
77 printk("\n");
78 }
79}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Paul Mundt3a2e1172007-05-01 16:33:10 +090081static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83void die(const char * str, struct pt_regs * regs, long err)
84{
85 static int die_counter;
86
Paul Mundt55273982007-06-18 18:57:13 +090087 oops_enter();
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 console_verbose();
90 spin_lock_irq(&die_lock);
Paul Mundt6b002232006-10-12 17:07:45 +090091 bust_spinlocks(1);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Paul Mundt6b002232006-10-12 17:07:45 +090094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 CHK_REMOTE_DEBUG(regs);
Paul Mundt6b002232006-10-12 17:07:45 +090096 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 show_regs(regs);
Paul Mundt6b002232006-10-12 17:07:45 +090098
Alexey Dobriyan19c58702007-10-18 23:40:41 -070099 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
100 task_pid_nr(current), task_stack_page(current) + 1);
Paul Mundt6b002232006-10-12 17:07:45 +0900101
102 if (!user_mode(regs) || in_interrupt())
103 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900104 (unsigned long)task_stack_page(current));
Paul Mundt6b002232006-10-12 17:07:45 +0900105
106 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700107 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 spin_unlock_irq(&die_lock);
Paul Mundte1132762007-05-15 08:36:36 +0900109
110 if (kexec_should_crash(current))
111 crash_kexec(regs);
112
113 if (in_interrupt())
114 panic("Fatal exception in interrupt");
115
116 if (panic_on_oops)
117 panic("Fatal exception");
118
Paul Mundt55273982007-06-18 18:57:13 +0900119 oops_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 do_exit(SIGSEGV);
121}
122
Paul Mundt6b002232006-10-12 17:07:45 +0900123static inline void die_if_kernel(const char *str, struct pt_regs *regs,
124 long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 if (!user_mode(regs))
127 die(str, regs, err);
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * try and fix up kernelspace address errors
132 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
133 * - kernel/userspace interfaces cause a jump to an appropriate handler
134 * - other kernel errors are bad
135 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
136 */
137static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
138{
Paul Mundt6b002232006-10-12 17:07:45 +0900139 if (!user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 const struct exception_table_entry *fixup;
141 fixup = search_exception_tables(regs->pc);
142 if (fixup) {
143 regs->pc = fixup->fixup;
144 return 0;
145 }
146 die(str, regs, err);
147 }
148 return -EFAULT;
149}
150
Magnus Damm86c01792008-02-07 00:02:50 +0900151static inline void sign_extend(unsigned int count, unsigned char *dst)
152{
153#ifdef __LITTLE_ENDIAN__
Magnus Damm4252c652008-02-07 19:58:46 +0900154 if ((count == 1) && dst[0] & 0x80) {
155 dst[1] = 0xff;
156 dst[2] = 0xff;
157 dst[3] = 0xff;
158 }
Magnus Damm86c01792008-02-07 00:02:50 +0900159 if ((count == 2) && dst[1] & 0x80) {
160 dst[2] = 0xff;
161 dst[3] = 0xff;
162 }
163#else
Magnus Damm4252c652008-02-07 19:58:46 +0900164 if ((count == 1) && dst[3] & 0x80) {
165 dst[2] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900166 dst[1] = 0xff;
Magnus Damm4252c652008-02-07 19:58:46 +0900167 dst[0] = 0xff;
168 }
169 if ((count == 2) && dst[2] & 0x80) {
170 dst[1] = 0xff;
171 dst[0] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900172 }
173#endif
174}
175
Magnus Damme7cc9a72008-02-07 20:18:21 +0900176static struct mem_access user_mem_access = {
177 copy_from_user,
178 copy_to_user,
179};
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
182 * handle an instruction that does an unaligned memory access by emulating the
183 * desired behaviour
184 * - note that PC _may not_ point to the faulting instruction
185 * (if that instruction is in a branch delay slot)
186 * - return 0 if emulation okay, -EFAULT on existential error
187 */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900188static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
189 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int ret, index, count;
192 unsigned long *rm, *rn;
193 unsigned char *src, *dst;
194
195 index = (instruction>>8)&15; /* 0x0F00 */
196 rn = &regs->regs[index];
197
198 index = (instruction>>4)&15; /* 0x00F0 */
199 rm = &regs->regs[index];
200
201 count = 1<<(instruction&3);
202
203 ret = -EFAULT;
204 switch (instruction>>12) {
205 case 0: /* mov.[bwl] to/from memory via r0+rn */
206 if (instruction & 8) {
207 /* from memory */
208 src = (unsigned char*) *rm;
209 src += regs->regs[0];
210 dst = (unsigned char*) rn;
211 *(unsigned long*)dst = 0;
212
Magnus Damm86c01792008-02-07 00:02:50 +0900213#if !defined(__LITTLE_ENDIAN__)
214 dst += 4-count;
215#endif
Magnus Damme7cc9a72008-02-07 20:18:21 +0900216 if (ma->from(dst, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto fetch_fault;
218
Magnus Damm86c01792008-02-07 00:02:50 +0900219 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 } else {
221 /* to memory */
222 src = (unsigned char*) rm;
223#if !defined(__LITTLE_ENDIAN__)
224 src += 4-count;
225#endif
226 dst = (unsigned char*) *rn;
227 dst += regs->regs[0];
228
Magnus Damme7cc9a72008-02-07 20:18:21 +0900229 if (ma->to(dst, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto fetch_fault;
231 }
232 ret = 0;
233 break;
234
235 case 1: /* mov.l Rm,@(disp,Rn) */
236 src = (unsigned char*) rm;
237 dst = (unsigned char*) *rn;
238 dst += (instruction&0x000F)<<2;
239
Magnus Damme7cc9a72008-02-07 20:18:21 +0900240 if (ma->to(dst, src, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto fetch_fault;
242 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
246 if (instruction & 4)
247 *rn -= count;
248 src = (unsigned char*) rm;
249 dst = (unsigned char*) *rn;
250#if !defined(__LITTLE_ENDIAN__)
251 src += 4-count;
252#endif
Magnus Damme7cc9a72008-02-07 20:18:21 +0900253 if (ma->to(dst, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto fetch_fault;
255 ret = 0;
256 break;
257
258 case 5: /* mov.l @(disp,Rm),Rn */
259 src = (unsigned char*) *rm;
260 src += (instruction&0x000F)<<2;
261 dst = (unsigned char*) rn;
262 *(unsigned long*)dst = 0;
263
Magnus Damme7cc9a72008-02-07 20:18:21 +0900264 if (ma->from(dst, src, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto fetch_fault;
266 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 case 6: /* mov.[bwl] from memory, possibly with post-increment */
270 src = (unsigned char*) *rm;
271 if (instruction & 4)
272 *rm += count;
273 dst = (unsigned char*) rn;
274 *(unsigned long*)dst = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900275
Magnus Damm86c01792008-02-07 00:02:50 +0900276#if !defined(__LITTLE_ENDIAN__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dst += 4-count;
Magnus Damm86c01792008-02-07 00:02:50 +0900278#endif
Magnus Damme7cc9a72008-02-07 20:18:21 +0900279 if (ma->from(dst, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900281 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 ret = 0;
283 break;
284
285 case 8:
286 switch ((instruction&0xFF00)>>8) {
287 case 0x81: /* mov.w R0,@(disp,Rn) */
288 src = (unsigned char*) &regs->regs[0];
289#if !defined(__LITTLE_ENDIAN__)
290 src += 2;
291#endif
292 dst = (unsigned char*) *rm; /* called Rn in the spec */
293 dst += (instruction&0x000F)<<1;
294
Magnus Damme7cc9a72008-02-07 20:18:21 +0900295 if (ma->to(dst, src, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto fetch_fault;
297 ret = 0;
298 break;
299
300 case 0x85: /* mov.w @(disp,Rm),R0 */
301 src = (unsigned char*) *rm;
302 src += (instruction&0x000F)<<1;
303 dst = (unsigned char*) &regs->regs[0];
304 *(unsigned long*)dst = 0;
305
306#if !defined(__LITTLE_ENDIAN__)
307 dst += 2;
308#endif
Magnus Damme7cc9a72008-02-07 20:18:21 +0900309 if (ma->from(dst, src, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900311 sign_extend(2, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ret = 0;
313 break;
314 }
315 break;
316 }
317 return ret;
318
319 fetch_fault:
320 /* Argh. Address not only misaligned but also non-existent.
321 * Raise an EFAULT and see if it's trapped
322 */
323 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
324}
325
326/*
327 * emulate the instruction in the delay slot
328 * - fetches the instruction from PC+2
329 */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900330static inline int handle_delayslot(struct pt_regs *regs,
331 opcode_t old_instruction,
332 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900334 opcode_t instruction;
335 void *addr = (void *)(regs->pc + instruction_size(old_instruction));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900337 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* the instruction-fetch faulted */
339 if (user_mode(regs))
340 return -EFAULT;
341
342 /* kernel */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900343 die("delay-slot-insn faulting in handle_unaligned_delayslot",
344 regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Magnus Damme7cc9a72008-02-07 20:18:21 +0900347 return handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/*
351 * handle an instruction that does an unaligned memory access
352 * - have to be careful of branch delay-slot instructions that fault
353 * SH3:
354 * - if the branch would be taken PC points to the branch
355 * - if the branch would not be taken, PC points to delay-slot
356 * SH4:
357 * - PC always points to delayed branch
358 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
359 */
360
361/* Macros to determine offset from current PC for branch instructions */
362/* Explicit type coercion is used to force sign extension where needed */
363#define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
364#define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
365
Paul Mundt710ee0c2006-11-05 16:48:42 +0900366/*
367 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
368 * opcodes..
369 */
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900370
Paul Mundt710ee0c2006-11-05 16:48:42 +0900371static int handle_unaligned_notify_count = 10;
372
Magnus Damme7cc9a72008-02-07 20:18:21 +0900373int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs,
374 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 u_int rm;
377 int ret, index;
378
379 index = (instruction>>8)&15; /* 0x0F00 */
380 rm = regs->regs[index];
381
382 /* shout about the first ten userspace fixups */
383 if (user_mode(regs) && handle_unaligned_notify_count>0) {
384 handle_unaligned_notify_count--;
385
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900386 printk(KERN_NOTICE "Fixing up unaligned userspace access "
387 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700388 current->comm, task_pid_nr(current),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900389 (void *)regs->pc, instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 ret = -EFAULT;
393 switch (instruction&0xF000) {
394 case 0x0000:
395 if (instruction==0x000B) {
396 /* rts */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900397 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (ret==0)
399 regs->pc = regs->pr;
400 }
401 else if ((instruction&0x00FF)==0x0023) {
402 /* braf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900403 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (ret==0)
405 regs->pc += rm + 4;
406 }
407 else if ((instruction&0x00FF)==0x0003) {
408 /* bsrf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900409 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (ret==0) {
411 regs->pr = regs->pc + 4;
412 regs->pc += rm + 4;
413 }
414 }
415 else {
416 /* mov.[bwl] to/from memory via r0+rn */
417 goto simple;
418 }
419 break;
420
421 case 0x1000: /* mov.l Rm,@(disp,Rn) */
422 goto simple;
423
424 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
425 goto simple;
426
427 case 0x4000:
428 if ((instruction&0x00FF)==0x002B) {
429 /* jmp @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900430 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (ret==0)
432 regs->pc = rm;
433 }
434 else if ((instruction&0x00FF)==0x000B) {
435 /* jsr @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900436 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (ret==0) {
438 regs->pr = regs->pc + 4;
439 regs->pc = rm;
440 }
441 }
442 else {
443 /* mov.[bwl] to/from memory via r0+rn */
444 goto simple;
445 }
446 break;
447
448 case 0x5000: /* mov.l @(disp,Rm),Rn */
449 goto simple;
450
451 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
452 goto simple;
453
454 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
455 switch (instruction&0x0F00) {
456 case 0x0100: /* mov.w R0,@(disp,Rm) */
457 goto simple;
458 case 0x0500: /* mov.w @(disp,Rm),R0 */
459 goto simple;
460 case 0x0B00: /* bf lab - no delayslot*/
461 break;
462 case 0x0F00: /* bf/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900463 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (ret==0) {
465#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
466 if ((regs->sr & 0x00000001) != 0)
467 regs->pc += 4; /* next after slot */
468 else
469#endif
470 regs->pc += SH_PC_8BIT_OFFSET(instruction);
471 }
472 break;
473 case 0x0900: /* bt lab - no delayslot */
474 break;
475 case 0x0D00: /* bt/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900476 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (ret==0) {
478#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
479 if ((regs->sr & 0x00000001) == 0)
480 regs->pc += 4; /* next after slot */
481 else
482#endif
483 regs->pc += SH_PC_8BIT_OFFSET(instruction);
484 }
485 break;
486 }
487 break;
488
489 case 0xA000: /* bra label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900490 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (ret==0)
492 regs->pc += SH_PC_12BIT_OFFSET(instruction);
493 break;
494
495 case 0xB000: /* bsr label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900496 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (ret==0) {
498 regs->pr = regs->pc + 4;
499 regs->pc += SH_PC_12BIT_OFFSET(instruction);
500 }
501 break;
502 }
503 return ret;
504
505 /* handle non-delay-slot instruction */
506 simple:
Magnus Damme7cc9a72008-02-07 20:18:21 +0900507 ret = handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (ret==0)
Paul Mundt53f983a2007-05-08 15:31:48 +0900509 regs->pc += instruction_size(instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return ret;
511}
512
Yoshinori Sato0983b312006-11-05 15:58:47 +0900513#ifdef CONFIG_CPU_HAS_SR_RB
514#define lookup_exception_vector(x) \
515 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
516#else
517#define lookup_exception_vector(x) \
518 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
519#endif
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/*
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900522 * Handle various address error exceptions:
523 * - instruction address error:
524 * misaligned PC
525 * PC >= 0x80000000 in user mode
526 * - data address error (read and write)
527 * misaligned data access
528 * access to >= 0x80000000 is user mode
529 * Unfortuntaly we can't distinguish between instruction address error
Simon Arlotte868d612007-05-14 08:15:10 +0900530 * and data address errors caused by read accesses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900532asmlinkage void do_address_error(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned long writeaccess,
534 unsigned long address)
535{
Yoshinori Sato0983b312006-11-05 15:58:47 +0900536 unsigned long error_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 mm_segment_t oldfs;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900538 siginfo_t info;
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900539 opcode_t instruction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int tmp;
541
Yoshinori Sato0983b312006-11-05 15:58:47 +0900542 /* Intentional ifdef */
543#ifdef CONFIG_CPU_HAS_SR_RB
544 lookup_exception_vector(error_code);
545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 oldfs = get_fs();
548
549 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900550 int si_code = BUS_ADRERR;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* bad PC is not something we can fix */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900555 if (regs->pc & 1) {
556 si_code = BUS_ADRALN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto uspace_segv;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 set_fs(USER_DS);
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900561 if (copy_from_user(&instruction, (void *)(regs->pc),
562 sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Argh. Fault on the instruction itself.
564 This should never happen non-SMP
565 */
566 set_fs(oldfs);
567 goto uspace_segv;
568 }
569
Magnus Damme7cc9a72008-02-07 20:18:21 +0900570 tmp = handle_unaligned_access(instruction, regs,
571 &user_mem_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 set_fs(oldfs);
573
574 if (tmp==0)
575 return; /* sorted */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900576uspace_segv:
577 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
578 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
579 regs->pr);
580
581 info.si_signo = SIGBUS;
582 info.si_errno = 0;
583 info.si_code = si_code;
Paul Mundte08f4572007-05-14 12:52:56 +0900584 info.si_addr = (void __user *)address;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900585 force_sig_info(SIGBUS, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 } else {
587 if (regs->pc & 1)
588 die("unaligned program counter", regs, error_code);
589
590 set_fs(KERNEL_DS);
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900591 if (copy_from_user(&instruction, (void *)(regs->pc),
592 sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* Argh. Fault on the instruction itself.
594 This should never happen non-SMP
595 */
596 set_fs(oldfs);
597 die("insn faulting in do_address_error", regs, 0);
598 }
599
Magnus Damme7cc9a72008-02-07 20:18:21 +0900600 handle_unaligned_access(instruction, regs, &user_mem_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 set_fs(oldfs);
602 }
603}
604
605#ifdef CONFIG_SH_DSP
606/*
607 * SH-DSP support gerg@snapgear.com.
608 */
609int is_dsp_inst(struct pt_regs *regs)
610{
Paul Mundt882c12c2007-05-14 17:26:34 +0900611 unsigned short inst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900613 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * Safe guard if DSP mode is already enabled or we're lacking
615 * the DSP altogether.
616 */
Paul Mundt11c19652006-12-25 10:19:56 +0900617 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619
620 get_user(inst, ((unsigned short *) regs->pc));
621
622 inst &= 0xf000;
623
624 /* Check for any type of DSP or support instruction */
625 if ((inst == 0xf000) || (inst == 0x4000))
626 return 1;
627
628 return 0;
629}
630#else
631#define is_dsp_inst(regs) (0)
632#endif /* CONFIG_SH_DSP */
633
Yoshinori Sato0983b312006-11-05 15:58:47 +0900634#ifdef CONFIG_CPU_SH2A
635asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
636 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900637 struct pt_regs __regs)
Yoshinori Sato0983b312006-11-05 15:58:47 +0900638{
639 siginfo_t info;
640
Yoshinori Sato0983b312006-11-05 15:58:47 +0900641 switch (r4) {
642 case TRAP_DIVZERO_ERROR:
643 info.si_code = FPE_INTDIV;
644 break;
645 case TRAP_DIVOVF_ERROR:
646 info.si_code = FPE_INTOVF;
647 break;
648 }
649
650 force_sig_info(SIGFPE, &info, current);
651}
652#endif
653
Takashi YOSHII4b565682006-09-27 17:15:32 +0900654asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
655 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900656 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900657{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900658 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900659 unsigned long error_code;
660 struct task_struct *tsk = current;
661
662#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900663 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900664 int err;
665
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900666 get_user(inst, (unsigned short*)regs->pc);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900667
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900668 err = do_fpu_inst(inst, regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900669 if (!err) {
Paul Mundt53f983a2007-05-08 15:31:48 +0900670 regs->pc += instruction_size(inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900671 return;
672 }
673 /* not a FPU inst. */
674#endif
675
676#ifdef CONFIG_SH_DSP
677 /* Check if it's a DSP instruction */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900678 if (is_dsp_inst(regs)) {
Takashi YOSHII4b565682006-09-27 17:15:32 +0900679 /* Enable DSP mode, and restart instruction. */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900680 regs->sr |= SR_DSP;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900681 return;
682 }
683#endif
684
Yoshinori Sato0983b312006-11-05 15:58:47 +0900685 lookup_exception_vector(error_code);
686
Takashi YOSHII4b565682006-09-27 17:15:32 +0900687 local_irq_enable();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900688 CHK_REMOTE_DEBUG(regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900689 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900690 die_if_no_fixup("reserved instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900691}
692
693#ifdef CONFIG_SH_FPU_EMU
694static int emulate_branch(unsigned short inst, struct pt_regs* regs)
695{
696 /*
697 * bfs: 8fxx: PC+=d*2+4;
698 * bts: 8dxx: PC+=d*2+4;
699 * bra: axxx: PC+=D*2+4;
700 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
701 * braf:0x23: PC+=Rn*2+4;
702 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
703 * jmp: 4x2b: PC=Rn;
704 * jsr: 4x0b: PC=Rn after PR=PC+4;
705 * rts: 000b: PC=PR;
706 */
707 if ((inst & 0xfd00) == 0x8d00) {
708 regs->pc += SH_PC_8BIT_OFFSET(inst);
709 return 0;
710 }
711
712 if ((inst & 0xe000) == 0xa000) {
713 regs->pc += SH_PC_12BIT_OFFSET(inst);
714 return 0;
715 }
716
717 if ((inst & 0xf0df) == 0x0003) {
718 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
719 return 0;
720 }
721
722 if ((inst & 0xf0df) == 0x400b) {
723 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
724 return 0;
725 }
726
727 if ((inst & 0xffff) == 0x000b) {
728 regs->pc = regs->pr;
729 return 0;
730 }
731
732 return 1;
733}
734#endif
735
736asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
737 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900738 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900739{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900740 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900741 unsigned long error_code;
742 struct task_struct *tsk = current;
743#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900744 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900745
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900746 get_user(inst, (unsigned short *)regs->pc + 1);
747 if (!do_fpu_inst(inst, regs)) {
748 get_user(inst, (unsigned short *)regs->pc);
749 if (!emulate_branch(inst, regs))
Takashi YOSHII4b565682006-09-27 17:15:32 +0900750 return;
751 /* fault in branch.*/
752 }
753 /* not a FPU inst. */
754#endif
755
Yoshinori Sato0983b312006-11-05 15:58:47 +0900756 lookup_exception_vector(error_code);
757
Takashi YOSHII4b565682006-09-27 17:15:32 +0900758 local_irq_enable();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900759 CHK_REMOTE_DEBUG(regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900760 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900761 die_if_no_fixup("illegal slot instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900762}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
765 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900766 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900768 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 long ex;
Yoshinori Sato0983b312006-11-05 15:58:47 +0900770
771 lookup_exception_vector(ex);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900772 die_if_kernel("exception", regs, ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775#if defined(CONFIG_SH_STANDARD_BIOS)
776void *gdb_vbr_vector;
777
778static inline void __init gdb_vbr_init(void)
779{
780 register unsigned long vbr;
781
782 /*
783 * Read the old value of the VBR register to initialise
784 * the vector through which debug and BIOS traps are
785 * delegated by the Linux trap handler.
786 */
787 asm volatile("stc vbr, %0" : "=r" (vbr));
788
789 gdb_vbr_vector = (void *)(vbr + 0x100);
790 printk("Setting GDB trap vector to 0x%08lx\n",
791 (unsigned long)gdb_vbr_vector);
792}
793#endif
794
Paul Mundtaba10302007-09-21 18:32:32 +0900795void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 extern void *vbr_base;
798
799#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtaba10302007-09-21 18:32:32 +0900800 if (raw_smp_processor_id() == 0)
801 gdb_vbr_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#endif
803
804 /* NOTE: The VBR value should be at P1
805 (or P2, virtural "fixed" address space).
806 It's definitely should not in physical address. */
807
808 asm volatile("ldc %0, vbr"
809 : /* no output */
810 : "r" (&vbr_base)
811 : "memory");
812}
813
Paul Mundt1f666582006-10-19 16:20:25 +0900814void *set_exception_table_vec(unsigned int vec, void *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 extern void *exception_handling_table[];
Paul Mundt1f666582006-10-19 16:20:25 +0900817 void *old_handler;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900818
Paul Mundt1f666582006-10-19 16:20:25 +0900819 old_handler = exception_handling_table[vec];
820 exception_handling_table[vec] = handler;
821 return old_handler;
822}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Paul Mundt1f666582006-10-19 16:20:25 +0900824void __init trap_init(void)
825{
826 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
827 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Takashi YOSHII4b565682006-09-27 17:15:32 +0900829#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
830 defined(CONFIG_SH_FPU_EMU)
831 /*
832 * For SH-4 lacking an FPU, treat floating point instructions as
833 * reserved. They'll be handled in the math-emu case, or faulted on
834 * otherwise.
835 */
Paul Mundt1f666582006-10-19 16:20:25 +0900836 set_exception_table_evt(0x800, do_reserved_inst);
837 set_exception_table_evt(0x820, do_illegal_slot_inst);
838#elif defined(CONFIG_SH_FPU)
Paul Mundte0a36472007-08-01 16:55:07 +0900839#ifdef CONFIG_CPU_SUBTYPE_SHX3
Paul Mundt74d99a52007-11-26 20:38:36 +0900840 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
841 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
Paul Mundte0a36472007-08-01 16:55:07 +0900842#else
Paul Mundt74d99a52007-11-26 20:38:36 +0900843 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
844 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845#endif
Paul Mundte0a36472007-08-01 16:55:07 +0900846#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900847
848#ifdef CONFIG_CPU_SH2
Paul Mundt5a4f7c62007-11-20 18:08:06 +0900849 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900850#endif
851#ifdef CONFIG_CPU_SH2A
852 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
853 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
854#endif
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* Setup VBR for boot cpu */
857 per_cpu_trap_init();
858}
859
Paul Mundt6b002232006-10-12 17:07:45 +0900860void show_trace(struct task_struct *tsk, unsigned long *sp,
861 struct pt_regs *regs)
862{
863 unsigned long addr;
864
865 if (regs && user_mode(regs))
866 return;
867
868 printk("\nCall trace: ");
869#ifdef CONFIG_KALLSYMS
870 printk("\n");
871#endif
872
873 while (!kstack_end(sp)) {
874 addr = *sp++;
875 if (kernel_text_address(addr))
876 print_ip_sym(addr);
877 }
878
879 printk("\n");
Paul Mundt9b8c90e2006-12-06 11:07:51 +0900880
881 if (!tsk)
882 tsk = current;
883
884 debug_show_held_locks(tsk);
Paul Mundt6b002232006-10-12 17:07:45 +0900885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887void show_stack(struct task_struct *tsk, unsigned long *sp)
888{
Paul Mundt6b002232006-10-12 17:07:45 +0900889 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Paul Mundta6a311392006-09-27 18:22:14 +0900891 if (!tsk)
892 tsk = current;
893 if (tsk == current)
894 sp = (unsigned long *)current_stack_pointer;
895 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Paul Mundt6b002232006-10-12 17:07:45 +0900898 stack = (unsigned long)sp;
899 dump_mem("Stack: ", stack, THREAD_SIZE +
900 (unsigned long)task_stack_page(tsk));
901 show_trace(tsk, sp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904void dump_stack(void)
905{
906 show_stack(NULL, NULL);
907}
908EXPORT_SYMBOL(dump_stack);