blob: 7154a7b2135bf8df7f0e45c5dbb3e27e5cd40aeb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#ifdef CONFIG_SH_KGDB
30#include <asm/kgdb.h>
Stuart Menefyf0bc8142006-11-21 11:16:57 +090031#define CHK_REMOTE_DEBUG(regs) \
32{ \
Takashi YOSHII4b565682006-09-27 17:15:32 +090033 if (kgdb_debug_hook && !user_mode(regs))\
34 (*kgdb_debug_hook)(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36#else
37#define CHK_REMOTE_DEBUG(regs)
38#endif
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#ifdef CONFIG_CPU_SH2
Yoshinori Sato0983b312006-11-05 15:58:47 +090041# define TRAP_RESERVED_INST 4
42# define TRAP_ILLEGAL_SLOT_INST 6
43# define TRAP_ADDRESS_ERROR 9
44# ifdef CONFIG_CPU_SH2A
45# define TRAP_DIVZERO_ERROR 17
46# define TRAP_DIVOVF_ERROR 18
47# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#else
49#define TRAP_RESERVED_INST 12
50#define TRAP_ILLEGAL_SLOT_INST 13
51#endif
52
Paul Mundt6b002232006-10-12 17:07:45 +090053static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
54{
55 unsigned long p;
56 int i;
57
58 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
59
60 for (p = bottom & ~31; p < top; ) {
61 printk("%04lx: ", p & 0xffff);
62
63 for (i = 0; i < 8; i++, p += 4) {
64 unsigned int val;
65
66 if (p < bottom || p >= top)
67 printk(" ");
68 else {
69 if (__get_user(val, (unsigned int __user *)p)) {
70 printk("\n");
71 return;
72 }
73 printk("%08x ", val);
74 }
75 }
76 printk("\n");
77 }
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Paul Mundt3a2e1172007-05-01 16:33:10 +090080static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82void die(const char * str, struct pt_regs * regs, long err)
83{
84 static int die_counter;
85
Paul Mundt55273982007-06-18 18:57:13 +090086 oops_enter();
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 console_verbose();
89 spin_lock_irq(&die_lock);
Paul Mundt6b002232006-10-12 17:07:45 +090090 bust_spinlocks(1);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Paul Mundt6b002232006-10-12 17:07:45 +090093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 CHK_REMOTE_DEBUG(regs);
Paul Mundt6b002232006-10-12 17:07:45 +090095 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 show_regs(regs);
Paul Mundt6b002232006-10-12 17:07:45 +090097
Alexey Dobriyan19c58702007-10-18 23:40:41 -070098 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
99 task_pid_nr(current), task_stack_page(current) + 1);
Paul Mundt6b002232006-10-12 17:07:45 +0900100
101 if (!user_mode(regs) || in_interrupt())
102 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900103 (unsigned long)task_stack_page(current));
Paul Mundt6b002232006-10-12 17:07:45 +0900104
105 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700106 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 spin_unlock_irq(&die_lock);
Paul Mundte1132762007-05-15 08:36:36 +0900108
109 if (kexec_should_crash(current))
110 crash_kexec(regs);
111
112 if (in_interrupt())
113 panic("Fatal exception in interrupt");
114
115 if (panic_on_oops)
116 panic("Fatal exception");
117
Paul Mundt55273982007-06-18 18:57:13 +0900118 oops_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 do_exit(SIGSEGV);
120}
121
Paul Mundt6b002232006-10-12 17:07:45 +0900122static inline void die_if_kernel(const char *str, struct pt_regs *regs,
123 long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 if (!user_mode(regs))
126 die(str, regs, err);
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/*
130 * try and fix up kernelspace address errors
131 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
132 * - kernel/userspace interfaces cause a jump to an appropriate handler
133 * - other kernel errors are bad
134 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
135 */
136static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
137{
Paul Mundt6b002232006-10-12 17:07:45 +0900138 if (!user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 const struct exception_table_entry *fixup;
140 fixup = search_exception_tables(regs->pc);
141 if (fixup) {
142 regs->pc = fixup->fixup;
143 return 0;
144 }
145 die(str, regs, err);
146 }
147 return -EFAULT;
148}
149
Magnus Damm86c01792008-02-07 00:02:50 +0900150static inline void sign_extend(unsigned int count, unsigned char *dst)
151{
152#ifdef __LITTLE_ENDIAN__
153 if ((count == 2) && dst[1] & 0x80) {
154 dst[2] = 0xff;
155 dst[3] = 0xff;
156 }
157#else
158 if ((count == 2) && dst[2] & 0x80) {
159 dst[0] = 0xff;
160 dst[1] = 0xff;
161 }
162#endif
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
166 * handle an instruction that does an unaligned memory access by emulating the
167 * desired behaviour
168 * - note that PC _may not_ point to the faulting instruction
169 * (if that instruction is in a branch delay slot)
170 * - return 0 if emulation okay, -EFAULT on existential error
171 */
172static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
173{
174 int ret, index, count;
175 unsigned long *rm, *rn;
176 unsigned char *src, *dst;
177
178 index = (instruction>>8)&15; /* 0x0F00 */
179 rn = &regs->regs[index];
180
181 index = (instruction>>4)&15; /* 0x00F0 */
182 rm = &regs->regs[index];
183
184 count = 1<<(instruction&3);
185
186 ret = -EFAULT;
187 switch (instruction>>12) {
188 case 0: /* mov.[bwl] to/from memory via r0+rn */
189 if (instruction & 8) {
190 /* from memory */
191 src = (unsigned char*) *rm;
192 src += regs->regs[0];
193 dst = (unsigned char*) rn;
194 *(unsigned long*)dst = 0;
195
Magnus Damm86c01792008-02-07 00:02:50 +0900196#if !defined(__LITTLE_ENDIAN__)
197 dst += 4-count;
198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (copy_from_user(dst, src, count))
200 goto fetch_fault;
201
Magnus Damm86c01792008-02-07 00:02:50 +0900202 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
204 /* to memory */
205 src = (unsigned char*) rm;
206#if !defined(__LITTLE_ENDIAN__)
207 src += 4-count;
208#endif
209 dst = (unsigned char*) *rn;
210 dst += regs->regs[0];
211
212 if (copy_to_user(dst, src, count))
213 goto fetch_fault;
214 }
215 ret = 0;
216 break;
217
218 case 1: /* mov.l Rm,@(disp,Rn) */
219 src = (unsigned char*) rm;
220 dst = (unsigned char*) *rn;
221 dst += (instruction&0x000F)<<2;
222
223 if (copy_to_user(dst,src,4))
224 goto fetch_fault;
225 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
229 if (instruction & 4)
230 *rn -= count;
231 src = (unsigned char*) rm;
232 dst = (unsigned char*) *rn;
233#if !defined(__LITTLE_ENDIAN__)
234 src += 4-count;
235#endif
236 if (copy_to_user(dst, src, count))
237 goto fetch_fault;
238 ret = 0;
239 break;
240
241 case 5: /* mov.l @(disp,Rm),Rn */
242 src = (unsigned char*) *rm;
243 src += (instruction&0x000F)<<2;
244 dst = (unsigned char*) rn;
245 *(unsigned long*)dst = 0;
246
247 if (copy_from_user(dst,src,4))
248 goto fetch_fault;
249 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 case 6: /* mov.[bwl] from memory, possibly with post-increment */
253 src = (unsigned char*) *rm;
254 if (instruction & 4)
255 *rm += count;
256 dst = (unsigned char*) rn;
257 *(unsigned long*)dst = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900258
Magnus Damm86c01792008-02-07 00:02:50 +0900259#if !defined(__LITTLE_ENDIAN__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dst += 4-count;
Magnus Damm86c01792008-02-07 00:02:50 +0900261#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (copy_from_user(dst, src, count))
263 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900264 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 ret = 0;
266 break;
267
268 case 8:
269 switch ((instruction&0xFF00)>>8) {
270 case 0x81: /* mov.w R0,@(disp,Rn) */
271 src = (unsigned char*) &regs->regs[0];
272#if !defined(__LITTLE_ENDIAN__)
273 src += 2;
274#endif
275 dst = (unsigned char*) *rm; /* called Rn in the spec */
276 dst += (instruction&0x000F)<<1;
277
278 if (copy_to_user(dst, src, 2))
279 goto fetch_fault;
280 ret = 0;
281 break;
282
283 case 0x85: /* mov.w @(disp,Rm),R0 */
284 src = (unsigned char*) *rm;
285 src += (instruction&0x000F)<<1;
286 dst = (unsigned char*) &regs->regs[0];
287 *(unsigned long*)dst = 0;
288
289#if !defined(__LITTLE_ENDIAN__)
290 dst += 2;
291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (copy_from_user(dst, src, 2))
293 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900294 sign_extend(2, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ret = 0;
296 break;
297 }
298 break;
299 }
300 return ret;
301
302 fetch_fault:
303 /* Argh. Address not only misaligned but also non-existent.
304 * Raise an EFAULT and see if it's trapped
305 */
306 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
307}
308
309/*
310 * emulate the instruction in the delay slot
311 * - fetches the instruction from PC+2
312 */
313static inline int handle_unaligned_delayslot(struct pt_regs *regs)
314{
315 u16 instruction;
316
317 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
318 /* the instruction-fetch faulted */
319 if (user_mode(regs))
320 return -EFAULT;
321
322 /* kernel */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900323 die("delay-slot-insn faulting in handle_unaligned_delayslot",
324 regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
327 return handle_unaligned_ins(instruction,regs);
328}
329
330/*
331 * handle an instruction that does an unaligned memory access
332 * - have to be careful of branch delay-slot instructions that fault
333 * SH3:
334 * - if the branch would be taken PC points to the branch
335 * - if the branch would not be taken, PC points to delay-slot
336 * SH4:
337 * - PC always points to delayed branch
338 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
339 */
340
341/* Macros to determine offset from current PC for branch instructions */
342/* Explicit type coercion is used to force sign extension where needed */
343#define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
344#define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
345
Paul Mundt710ee0c2006-11-05 16:48:42 +0900346/*
347 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
348 * opcodes..
349 */
350#ifndef CONFIG_CPU_SH2A
351static int handle_unaligned_notify_count = 10;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
354{
355 u_int rm;
356 int ret, index;
357
358 index = (instruction>>8)&15; /* 0x0F00 */
359 rm = regs->regs[index];
360
361 /* shout about the first ten userspace fixups */
362 if (user_mode(regs) && handle_unaligned_notify_count>0) {
363 handle_unaligned_notify_count--;
364
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900365 printk(KERN_NOTICE "Fixing up unaligned userspace access "
366 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700367 current->comm, task_pid_nr(current),
368 (u16 *)regs->pc, instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 ret = -EFAULT;
372 switch (instruction&0xF000) {
373 case 0x0000:
374 if (instruction==0x000B) {
375 /* rts */
376 ret = handle_unaligned_delayslot(regs);
377 if (ret==0)
378 regs->pc = regs->pr;
379 }
380 else if ((instruction&0x00FF)==0x0023) {
381 /* braf @Rm */
382 ret = handle_unaligned_delayslot(regs);
383 if (ret==0)
384 regs->pc += rm + 4;
385 }
386 else if ((instruction&0x00FF)==0x0003) {
387 /* bsrf @Rm */
388 ret = handle_unaligned_delayslot(regs);
389 if (ret==0) {
390 regs->pr = regs->pc + 4;
391 regs->pc += rm + 4;
392 }
393 }
394 else {
395 /* mov.[bwl] to/from memory via r0+rn */
396 goto simple;
397 }
398 break;
399
400 case 0x1000: /* mov.l Rm,@(disp,Rn) */
401 goto simple;
402
403 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
404 goto simple;
405
406 case 0x4000:
407 if ((instruction&0x00FF)==0x002B) {
408 /* jmp @Rm */
409 ret = handle_unaligned_delayslot(regs);
410 if (ret==0)
411 regs->pc = rm;
412 }
413 else if ((instruction&0x00FF)==0x000B) {
414 /* jsr @Rm */
415 ret = handle_unaligned_delayslot(regs);
416 if (ret==0) {
417 regs->pr = regs->pc + 4;
418 regs->pc = rm;
419 }
420 }
421 else {
422 /* mov.[bwl] to/from memory via r0+rn */
423 goto simple;
424 }
425 break;
426
427 case 0x5000: /* mov.l @(disp,Rm),Rn */
428 goto simple;
429
430 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
431 goto simple;
432
433 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
434 switch (instruction&0x0F00) {
435 case 0x0100: /* mov.w R0,@(disp,Rm) */
436 goto simple;
437 case 0x0500: /* mov.w @(disp,Rm),R0 */
438 goto simple;
439 case 0x0B00: /* bf lab - no delayslot*/
440 break;
441 case 0x0F00: /* bf/s lab */
442 ret = handle_unaligned_delayslot(regs);
443 if (ret==0) {
444#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
445 if ((regs->sr & 0x00000001) != 0)
446 regs->pc += 4; /* next after slot */
447 else
448#endif
449 regs->pc += SH_PC_8BIT_OFFSET(instruction);
450 }
451 break;
452 case 0x0900: /* bt lab - no delayslot */
453 break;
454 case 0x0D00: /* bt/s lab */
455 ret = handle_unaligned_delayslot(regs);
456 if (ret==0) {
457#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
458 if ((regs->sr & 0x00000001) == 0)
459 regs->pc += 4; /* next after slot */
460 else
461#endif
462 regs->pc += SH_PC_8BIT_OFFSET(instruction);
463 }
464 break;
465 }
466 break;
467
468 case 0xA000: /* bra label */
469 ret = handle_unaligned_delayslot(regs);
470 if (ret==0)
471 regs->pc += SH_PC_12BIT_OFFSET(instruction);
472 break;
473
474 case 0xB000: /* bsr label */
475 ret = handle_unaligned_delayslot(regs);
476 if (ret==0) {
477 regs->pr = regs->pc + 4;
478 regs->pc += SH_PC_12BIT_OFFSET(instruction);
479 }
480 break;
481 }
482 return ret;
483
484 /* handle non-delay-slot instruction */
485 simple:
486 ret = handle_unaligned_ins(instruction,regs);
487 if (ret==0)
Paul Mundt53f983a2007-05-08 15:31:48 +0900488 regs->pc += instruction_size(instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return ret;
490}
Paul Mundt710ee0c2006-11-05 16:48:42 +0900491#endif /* CONFIG_CPU_SH2A */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Yoshinori Sato0983b312006-11-05 15:58:47 +0900493#ifdef CONFIG_CPU_HAS_SR_RB
494#define lookup_exception_vector(x) \
495 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
496#else
497#define lookup_exception_vector(x) \
498 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
499#endif
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900502 * Handle various address error exceptions:
503 * - instruction address error:
504 * misaligned PC
505 * PC >= 0x80000000 in user mode
506 * - data address error (read and write)
507 * misaligned data access
508 * access to >= 0x80000000 is user mode
509 * Unfortuntaly we can't distinguish between instruction address error
Simon Arlotte868d612007-05-14 08:15:10 +0900510 * and data address errors caused by read accesses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900512asmlinkage void do_address_error(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unsigned long writeaccess,
514 unsigned long address)
515{
Yoshinori Sato0983b312006-11-05 15:58:47 +0900516 unsigned long error_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 mm_segment_t oldfs;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900518 siginfo_t info;
Paul Mundt710ee0c2006-11-05 16:48:42 +0900519#ifndef CONFIG_CPU_SH2A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 u16 instruction;
521 int tmp;
Paul Mundt710ee0c2006-11-05 16:48:42 +0900522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Yoshinori Sato0983b312006-11-05 15:58:47 +0900524 /* Intentional ifdef */
525#ifdef CONFIG_CPU_HAS_SR_RB
526 lookup_exception_vector(error_code);
527#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 oldfs = get_fs();
530
531 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900532 int si_code = BUS_ADRERR;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* bad PC is not something we can fix */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900537 if (regs->pc & 1) {
538 si_code = BUS_ADRALN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto uspace_segv;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Yoshinori Sato0983b312006-11-05 15:58:47 +0900542#ifndef CONFIG_CPU_SH2A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 set_fs(USER_DS);
544 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
545 /* Argh. Fault on the instruction itself.
546 This should never happen non-SMP
547 */
548 set_fs(oldfs);
549 goto uspace_segv;
550 }
551
552 tmp = handle_unaligned_access(instruction, regs);
553 set_fs(oldfs);
554
555 if (tmp==0)
556 return; /* sorted */
Yoshinori Sato0983b312006-11-05 15:58:47 +0900557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900559uspace_segv:
560 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
561 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
562 regs->pr);
563
564 info.si_signo = SIGBUS;
565 info.si_errno = 0;
566 info.si_code = si_code;
Paul Mundte08f4572007-05-14 12:52:56 +0900567 info.si_addr = (void __user *)address;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900568 force_sig_info(SIGBUS, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 } else {
570 if (regs->pc & 1)
571 die("unaligned program counter", regs, error_code);
572
Yoshinori Sato0983b312006-11-05 15:58:47 +0900573#ifndef CONFIG_CPU_SH2A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 set_fs(KERNEL_DS);
575 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
576 /* Argh. Fault on the instruction itself.
577 This should never happen non-SMP
578 */
579 set_fs(oldfs);
580 die("insn faulting in do_address_error", regs, 0);
581 }
582
583 handle_unaligned_access(instruction, regs);
584 set_fs(oldfs);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900585#else
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900586 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
587 "access\n", current->comm);
588
Yoshinori Sato0983b312006-11-05 15:58:47 +0900589 force_sig(SIGSEGV, current);
590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592}
593
594#ifdef CONFIG_SH_DSP
595/*
596 * SH-DSP support gerg@snapgear.com.
597 */
598int is_dsp_inst(struct pt_regs *regs)
599{
Paul Mundt882c12c2007-05-14 17:26:34 +0900600 unsigned short inst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900602 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * Safe guard if DSP mode is already enabled or we're lacking
604 * the DSP altogether.
605 */
Paul Mundt11c19652006-12-25 10:19:56 +0900606 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
608
609 get_user(inst, ((unsigned short *) regs->pc));
610
611 inst &= 0xf000;
612
613 /* Check for any type of DSP or support instruction */
614 if ((inst == 0xf000) || (inst == 0x4000))
615 return 1;
616
617 return 0;
618}
619#else
620#define is_dsp_inst(regs) (0)
621#endif /* CONFIG_SH_DSP */
622
Yoshinori Sato0983b312006-11-05 15:58:47 +0900623#ifdef CONFIG_CPU_SH2A
624asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
625 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900626 struct pt_regs __regs)
Yoshinori Sato0983b312006-11-05 15:58:47 +0900627{
628 siginfo_t info;
629
Yoshinori Sato0983b312006-11-05 15:58:47 +0900630 switch (r4) {
631 case TRAP_DIVZERO_ERROR:
632 info.si_code = FPE_INTDIV;
633 break;
634 case TRAP_DIVOVF_ERROR:
635 info.si_code = FPE_INTOVF;
636 break;
637 }
638
639 force_sig_info(SIGFPE, &info, current);
640}
641#endif
642
Takashi YOSHII4b565682006-09-27 17:15:32 +0900643asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
644 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900645 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900646{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900647 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900648 unsigned long error_code;
649 struct task_struct *tsk = current;
650
651#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900652 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900653 int err;
654
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900655 get_user(inst, (unsigned short*)regs->pc);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900656
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900657 err = do_fpu_inst(inst, regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900658 if (!err) {
Paul Mundt53f983a2007-05-08 15:31:48 +0900659 regs->pc += instruction_size(inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900660 return;
661 }
662 /* not a FPU inst. */
663#endif
664
665#ifdef CONFIG_SH_DSP
666 /* Check if it's a DSP instruction */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900667 if (is_dsp_inst(regs)) {
Takashi YOSHII4b565682006-09-27 17:15:32 +0900668 /* Enable DSP mode, and restart instruction. */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900669 regs->sr |= SR_DSP;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900670 return;
671 }
672#endif
673
Yoshinori Sato0983b312006-11-05 15:58:47 +0900674 lookup_exception_vector(error_code);
675
Takashi YOSHII4b565682006-09-27 17:15:32 +0900676 local_irq_enable();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900677 CHK_REMOTE_DEBUG(regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900678 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900679 die_if_no_fixup("reserved instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900680}
681
682#ifdef CONFIG_SH_FPU_EMU
683static int emulate_branch(unsigned short inst, struct pt_regs* regs)
684{
685 /*
686 * bfs: 8fxx: PC+=d*2+4;
687 * bts: 8dxx: PC+=d*2+4;
688 * bra: axxx: PC+=D*2+4;
689 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
690 * braf:0x23: PC+=Rn*2+4;
691 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
692 * jmp: 4x2b: PC=Rn;
693 * jsr: 4x0b: PC=Rn after PR=PC+4;
694 * rts: 000b: PC=PR;
695 */
696 if ((inst & 0xfd00) == 0x8d00) {
697 regs->pc += SH_PC_8BIT_OFFSET(inst);
698 return 0;
699 }
700
701 if ((inst & 0xe000) == 0xa000) {
702 regs->pc += SH_PC_12BIT_OFFSET(inst);
703 return 0;
704 }
705
706 if ((inst & 0xf0df) == 0x0003) {
707 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
708 return 0;
709 }
710
711 if ((inst & 0xf0df) == 0x400b) {
712 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
713 return 0;
714 }
715
716 if ((inst & 0xffff) == 0x000b) {
717 regs->pc = regs->pr;
718 return 0;
719 }
720
721 return 1;
722}
723#endif
724
725asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
726 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900727 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900728{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900729 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900730 unsigned long error_code;
731 struct task_struct *tsk = current;
732#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900733 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900734
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900735 get_user(inst, (unsigned short *)regs->pc + 1);
736 if (!do_fpu_inst(inst, regs)) {
737 get_user(inst, (unsigned short *)regs->pc);
738 if (!emulate_branch(inst, regs))
Takashi YOSHII4b565682006-09-27 17:15:32 +0900739 return;
740 /* fault in branch.*/
741 }
742 /* not a FPU inst. */
743#endif
744
Yoshinori Sato0983b312006-11-05 15:58:47 +0900745 lookup_exception_vector(error_code);
746
Takashi YOSHII4b565682006-09-27 17:15:32 +0900747 local_irq_enable();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900748 CHK_REMOTE_DEBUG(regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900749 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900750 die_if_no_fixup("illegal slot instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900751}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
754 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900755 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900757 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 long ex;
Yoshinori Sato0983b312006-11-05 15:58:47 +0900759
760 lookup_exception_vector(ex);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900761 die_if_kernel("exception", regs, ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764#if defined(CONFIG_SH_STANDARD_BIOS)
765void *gdb_vbr_vector;
766
767static inline void __init gdb_vbr_init(void)
768{
769 register unsigned long vbr;
770
771 /*
772 * Read the old value of the VBR register to initialise
773 * the vector through which debug and BIOS traps are
774 * delegated by the Linux trap handler.
775 */
776 asm volatile("stc vbr, %0" : "=r" (vbr));
777
778 gdb_vbr_vector = (void *)(vbr + 0x100);
779 printk("Setting GDB trap vector to 0x%08lx\n",
780 (unsigned long)gdb_vbr_vector);
781}
782#endif
783
Paul Mundtaba10302007-09-21 18:32:32 +0900784void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 extern void *vbr_base;
787
788#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtaba10302007-09-21 18:32:32 +0900789 if (raw_smp_processor_id() == 0)
790 gdb_vbr_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791#endif
792
793 /* NOTE: The VBR value should be at P1
794 (or P2, virtural "fixed" address space).
795 It's definitely should not in physical address. */
796
797 asm volatile("ldc %0, vbr"
798 : /* no output */
799 : "r" (&vbr_base)
800 : "memory");
801}
802
Paul Mundt1f666582006-10-19 16:20:25 +0900803void *set_exception_table_vec(unsigned int vec, void *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 extern void *exception_handling_table[];
Paul Mundt1f666582006-10-19 16:20:25 +0900806 void *old_handler;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900807
Paul Mundt1f666582006-10-19 16:20:25 +0900808 old_handler = exception_handling_table[vec];
809 exception_handling_table[vec] = handler;
810 return old_handler;
811}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Paul Mundt1f666582006-10-19 16:20:25 +0900813void __init trap_init(void)
814{
815 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
816 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Takashi YOSHII4b565682006-09-27 17:15:32 +0900818#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
819 defined(CONFIG_SH_FPU_EMU)
820 /*
821 * For SH-4 lacking an FPU, treat floating point instructions as
822 * reserved. They'll be handled in the math-emu case, or faulted on
823 * otherwise.
824 */
Paul Mundt1f666582006-10-19 16:20:25 +0900825 set_exception_table_evt(0x800, do_reserved_inst);
826 set_exception_table_evt(0x820, do_illegal_slot_inst);
827#elif defined(CONFIG_SH_FPU)
Paul Mundte0a36472007-08-01 16:55:07 +0900828#ifdef CONFIG_CPU_SUBTYPE_SHX3
Paul Mundt74d99a52007-11-26 20:38:36 +0900829 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
830 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
Paul Mundte0a36472007-08-01 16:55:07 +0900831#else
Paul Mundt74d99a52007-11-26 20:38:36 +0900832 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
833 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834#endif
Paul Mundte0a36472007-08-01 16:55:07 +0900835#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900836
837#ifdef CONFIG_CPU_SH2
Paul Mundt5a4f7c62007-11-20 18:08:06 +0900838 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900839#endif
840#ifdef CONFIG_CPU_SH2A
841 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
842 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
843#endif
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* Setup VBR for boot cpu */
846 per_cpu_trap_init();
847}
848
Paul Mundt6b002232006-10-12 17:07:45 +0900849void show_trace(struct task_struct *tsk, unsigned long *sp,
850 struct pt_regs *regs)
851{
852 unsigned long addr;
853
854 if (regs && user_mode(regs))
855 return;
856
857 printk("\nCall trace: ");
858#ifdef CONFIG_KALLSYMS
859 printk("\n");
860#endif
861
862 while (!kstack_end(sp)) {
863 addr = *sp++;
864 if (kernel_text_address(addr))
865 print_ip_sym(addr);
866 }
867
868 printk("\n");
Paul Mundt9b8c90e2006-12-06 11:07:51 +0900869
870 if (!tsk)
871 tsk = current;
872
873 debug_show_held_locks(tsk);
Paul Mundt6b002232006-10-12 17:07:45 +0900874}
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876void show_stack(struct task_struct *tsk, unsigned long *sp)
877{
Paul Mundt6b002232006-10-12 17:07:45 +0900878 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Paul Mundta6a311392006-09-27 18:22:14 +0900880 if (!tsk)
881 tsk = current;
882 if (tsk == current)
883 sp = (unsigned long *)current_stack_pointer;
884 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Paul Mundt6b002232006-10-12 17:07:45 +0900887 stack = (unsigned long)sp;
888 dump_mem("Stack: ", stack, THREAD_SIZE +
889 (unsigned long)task_stack_page(tsk));
890 show_trace(tsk, sp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893void dump_stack(void)
894{
895 show_stack(NULL, NULL);
896}
897EXPORT_SYMBOL(dump_stack);