blob: a01e36705c5f7444fa69ebd278f70bb937a9d70a [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 Mundtdc34d312006-12-08 17:41:43 +090024#include <linux/limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/system.h>
26#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#ifdef CONFIG_SH_KGDB
29#include <asm/kgdb.h>
Stuart Menefyf0bc8142006-11-21 11:16:57 +090030#define CHK_REMOTE_DEBUG(regs) \
31{ \
Takashi YOSHII4b565682006-09-27 17:15:32 +090032 if (kgdb_debug_hook && !user_mode(regs))\
33 (*kgdb_debug_hook)(regs); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35#else
36#define CHK_REMOTE_DEBUG(regs)
37#endif
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#ifdef CONFIG_CPU_SH2
Yoshinori Sato0983b312006-11-05 15:58:47 +090040# define TRAP_RESERVED_INST 4
41# define TRAP_ILLEGAL_SLOT_INST 6
42# define TRAP_ADDRESS_ERROR 9
43# ifdef CONFIG_CPU_SH2A
44# define TRAP_DIVZERO_ERROR 17
45# define TRAP_DIVOVF_ERROR 18
46# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#else
48#define TRAP_RESERVED_INST 12
49#define TRAP_ILLEGAL_SLOT_INST 13
50#endif
51
Paul Mundt6b002232006-10-12 17:07:45 +090052static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
53{
54 unsigned long p;
55 int i;
56
57 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
58
59 for (p = bottom & ~31; p < top; ) {
60 printk("%04lx: ", p & 0xffff);
61
62 for (i = 0; i < 8; i++, p += 4) {
63 unsigned int val;
64
65 if (p < bottom || p >= top)
66 printk(" ");
67 else {
68 if (__get_user(val, (unsigned int __user *)p)) {
69 printk("\n");
70 return;
71 }
72 printk("%08x ", val);
73 }
74 }
75 printk("\n");
76 }
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Paul Mundt3a2e1172007-05-01 16:33:10 +090079static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81void die(const char * str, struct pt_regs * regs, long err)
82{
83 static int die_counter;
84
85 console_verbose();
86 spin_lock_irq(&die_lock);
Paul Mundt6b002232006-10-12 17:07:45 +090087 bust_spinlocks(1);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Paul Mundt6b002232006-10-12 17:07:45 +090090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 CHK_REMOTE_DEBUG(regs);
Paul Mundt6b002232006-10-12 17:07:45 +090092 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 show_regs(regs);
Paul Mundt6b002232006-10-12 17:07:45 +090094
95 printk("Process: %s (pid: %d, stack limit = %p)\n",
96 current->comm, current->pid, task_stack_page(current) + 1);
97
98 if (!user_mode(regs) || in_interrupt())
99 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900100 (unsigned long)task_stack_page(current));
Paul Mundt6b002232006-10-12 17:07:45 +0900101
102 bust_spinlocks(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 spin_unlock_irq(&die_lock);
104 do_exit(SIGSEGV);
105}
106
Paul Mundt6b002232006-10-12 17:07:45 +0900107static inline void die_if_kernel(const char *str, struct pt_regs *regs,
108 long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 if (!user_mode(regs))
111 die(str, regs, err);
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * try and fix up kernelspace address errors
116 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
117 * - kernel/userspace interfaces cause a jump to an appropriate handler
118 * - other kernel errors are bad
119 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
120 */
121static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
122{
Paul Mundt6b002232006-10-12 17:07:45 +0900123 if (!user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 const struct exception_table_entry *fixup;
125 fixup = search_exception_tables(regs->pc);
126 if (fixup) {
127 regs->pc = fixup->fixup;
128 return 0;
129 }
130 die(str, regs, err);
131 }
132 return -EFAULT;
133}
134
135/*
136 * handle an instruction that does an unaligned memory access by emulating the
137 * desired behaviour
138 * - note that PC _may not_ point to the faulting instruction
139 * (if that instruction is in a branch delay slot)
140 * - return 0 if emulation okay, -EFAULT on existential error
141 */
142static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
143{
144 int ret, index, count;
145 unsigned long *rm, *rn;
146 unsigned char *src, *dst;
147
148 index = (instruction>>8)&15; /* 0x0F00 */
149 rn = &regs->regs[index];
150
151 index = (instruction>>4)&15; /* 0x00F0 */
152 rm = &regs->regs[index];
153
154 count = 1<<(instruction&3);
155
156 ret = -EFAULT;
157 switch (instruction>>12) {
158 case 0: /* mov.[bwl] to/from memory via r0+rn */
159 if (instruction & 8) {
160 /* from memory */
161 src = (unsigned char*) *rm;
162 src += regs->regs[0];
163 dst = (unsigned char*) rn;
164 *(unsigned long*)dst = 0;
165
166#ifdef __LITTLE_ENDIAN__
167 if (copy_from_user(dst, src, count))
168 goto fetch_fault;
169
170 if ((count == 2) && dst[1] & 0x80) {
171 dst[2] = 0xff;
172 dst[3] = 0xff;
173 }
174#else
175 dst += 4-count;
176
177 if (__copy_user(dst, src, count))
178 goto fetch_fault;
179
180 if ((count == 2) && dst[2] & 0x80) {
181 dst[0] = 0xff;
182 dst[1] = 0xff;
183 }
184#endif
185 } else {
186 /* to memory */
187 src = (unsigned char*) rm;
188#if !defined(__LITTLE_ENDIAN__)
189 src += 4-count;
190#endif
191 dst = (unsigned char*) *rn;
192 dst += regs->regs[0];
193
194 if (copy_to_user(dst, src, count))
195 goto fetch_fault;
196 }
197 ret = 0;
198 break;
199
200 case 1: /* mov.l Rm,@(disp,Rn) */
201 src = (unsigned char*) rm;
202 dst = (unsigned char*) *rn;
203 dst += (instruction&0x000F)<<2;
204
205 if (copy_to_user(dst,src,4))
206 goto fetch_fault;
207 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
211 if (instruction & 4)
212 *rn -= count;
213 src = (unsigned char*) rm;
214 dst = (unsigned char*) *rn;
215#if !defined(__LITTLE_ENDIAN__)
216 src += 4-count;
217#endif
218 if (copy_to_user(dst, src, count))
219 goto fetch_fault;
220 ret = 0;
221 break;
222
223 case 5: /* mov.l @(disp,Rm),Rn */
224 src = (unsigned char*) *rm;
225 src += (instruction&0x000F)<<2;
226 dst = (unsigned char*) rn;
227 *(unsigned long*)dst = 0;
228
229 if (copy_from_user(dst,src,4))
230 goto fetch_fault;
231 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 case 6: /* mov.[bwl] from memory, possibly with post-increment */
235 src = (unsigned char*) *rm;
236 if (instruction & 4)
237 *rm += count;
238 dst = (unsigned char*) rn;
239 *(unsigned long*)dst = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#ifdef __LITTLE_ENDIAN__
242 if (copy_from_user(dst, src, count))
243 goto fetch_fault;
244
245 if ((count == 2) && dst[1] & 0x80) {
246 dst[2] = 0xff;
247 dst[3] = 0xff;
248 }
249#else
250 dst += 4-count;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (copy_from_user(dst, src, count))
253 goto fetch_fault;
254
255 if ((count == 2) && dst[2] & 0x80) {
256 dst[0] = 0xff;
257 dst[1] = 0xff;
258 }
259#endif
260 ret = 0;
261 break;
262
263 case 8:
264 switch ((instruction&0xFF00)>>8) {
265 case 0x81: /* mov.w R0,@(disp,Rn) */
266 src = (unsigned char*) &regs->regs[0];
267#if !defined(__LITTLE_ENDIAN__)
268 src += 2;
269#endif
270 dst = (unsigned char*) *rm; /* called Rn in the spec */
271 dst += (instruction&0x000F)<<1;
272
273 if (copy_to_user(dst, src, 2))
274 goto fetch_fault;
275 ret = 0;
276 break;
277
278 case 0x85: /* mov.w @(disp,Rm),R0 */
279 src = (unsigned char*) *rm;
280 src += (instruction&0x000F)<<1;
281 dst = (unsigned char*) &regs->regs[0];
282 *(unsigned long*)dst = 0;
283
284#if !defined(__LITTLE_ENDIAN__)
285 dst += 2;
286#endif
287
288 if (copy_from_user(dst, src, 2))
289 goto fetch_fault;
290
291#ifdef __LITTLE_ENDIAN__
292 if (dst[1] & 0x80) {
293 dst[2] = 0xff;
294 dst[3] = 0xff;
295 }
296#else
297 if (dst[2] & 0x80) {
298 dst[0] = 0xff;
299 dst[1] = 0xff;
300 }
301#endif
302 ret = 0;
303 break;
304 }
305 break;
306 }
307 return ret;
308
309 fetch_fault:
310 /* Argh. Address not only misaligned but also non-existent.
311 * Raise an EFAULT and see if it's trapped
312 */
313 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
314}
315
316/*
317 * emulate the instruction in the delay slot
318 * - fetches the instruction from PC+2
319 */
320static inline int handle_unaligned_delayslot(struct pt_regs *regs)
321{
322 u16 instruction;
323
324 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
325 /* the instruction-fetch faulted */
326 if (user_mode(regs))
327 return -EFAULT;
328
329 /* kernel */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900330 die("delay-slot-insn faulting in handle_unaligned_delayslot",
331 regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 return handle_unaligned_ins(instruction,regs);
335}
336
337/*
338 * handle an instruction that does an unaligned memory access
339 * - have to be careful of branch delay-slot instructions that fault
340 * SH3:
341 * - if the branch would be taken PC points to the branch
342 * - if the branch would not be taken, PC points to delay-slot
343 * SH4:
344 * - PC always points to delayed branch
345 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
346 */
347
348/* Macros to determine offset from current PC for branch instructions */
349/* Explicit type coercion is used to force sign extension where needed */
350#define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
351#define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
352
Paul Mundt710ee0c2006-11-05 16:48:42 +0900353/*
354 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
355 * opcodes..
356 */
357#ifndef CONFIG_CPU_SH2A
358static int handle_unaligned_notify_count = 10;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
361{
362 u_int rm;
363 int ret, index;
364
365 index = (instruction>>8)&15; /* 0x0F00 */
366 rm = regs->regs[index];
367
368 /* shout about the first ten userspace fixups */
369 if (user_mode(regs) && handle_unaligned_notify_count>0) {
370 handle_unaligned_notify_count--;
371
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900372 printk(KERN_NOTICE "Fixing up unaligned userspace access "
373 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 current->comm,current->pid,(u16*)regs->pc,instruction);
375 }
376
377 ret = -EFAULT;
378 switch (instruction&0xF000) {
379 case 0x0000:
380 if (instruction==0x000B) {
381 /* rts */
382 ret = handle_unaligned_delayslot(regs);
383 if (ret==0)
384 regs->pc = regs->pr;
385 }
386 else if ((instruction&0x00FF)==0x0023) {
387 /* braf @Rm */
388 ret = handle_unaligned_delayslot(regs);
389 if (ret==0)
390 regs->pc += rm + 4;
391 }
392 else if ((instruction&0x00FF)==0x0003) {
393 /* bsrf @Rm */
394 ret = handle_unaligned_delayslot(regs);
395 if (ret==0) {
396 regs->pr = regs->pc + 4;
397 regs->pc += rm + 4;
398 }
399 }
400 else {
401 /* mov.[bwl] to/from memory via r0+rn */
402 goto simple;
403 }
404 break;
405
406 case 0x1000: /* mov.l Rm,@(disp,Rn) */
407 goto simple;
408
409 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
410 goto simple;
411
412 case 0x4000:
413 if ((instruction&0x00FF)==0x002B) {
414 /* jmp @Rm */
415 ret = handle_unaligned_delayslot(regs);
416 if (ret==0)
417 regs->pc = rm;
418 }
419 else if ((instruction&0x00FF)==0x000B) {
420 /* jsr @Rm */
421 ret = handle_unaligned_delayslot(regs);
422 if (ret==0) {
423 regs->pr = regs->pc + 4;
424 regs->pc = rm;
425 }
426 }
427 else {
428 /* mov.[bwl] to/from memory via r0+rn */
429 goto simple;
430 }
431 break;
432
433 case 0x5000: /* mov.l @(disp,Rm),Rn */
434 goto simple;
435
436 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
437 goto simple;
438
439 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
440 switch (instruction&0x0F00) {
441 case 0x0100: /* mov.w R0,@(disp,Rm) */
442 goto simple;
443 case 0x0500: /* mov.w @(disp,Rm),R0 */
444 goto simple;
445 case 0x0B00: /* bf lab - no delayslot*/
446 break;
447 case 0x0F00: /* bf/s lab */
448 ret = handle_unaligned_delayslot(regs);
449 if (ret==0) {
450#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
451 if ((regs->sr & 0x00000001) != 0)
452 regs->pc += 4; /* next after slot */
453 else
454#endif
455 regs->pc += SH_PC_8BIT_OFFSET(instruction);
456 }
457 break;
458 case 0x0900: /* bt lab - no delayslot */
459 break;
460 case 0x0D00: /* bt/s lab */
461 ret = handle_unaligned_delayslot(regs);
462 if (ret==0) {
463#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
464 if ((regs->sr & 0x00000001) == 0)
465 regs->pc += 4; /* next after slot */
466 else
467#endif
468 regs->pc += SH_PC_8BIT_OFFSET(instruction);
469 }
470 break;
471 }
472 break;
473
474 case 0xA000: /* bra label */
475 ret = handle_unaligned_delayslot(regs);
476 if (ret==0)
477 regs->pc += SH_PC_12BIT_OFFSET(instruction);
478 break;
479
480 case 0xB000: /* bsr label */
481 ret = handle_unaligned_delayslot(regs);
482 if (ret==0) {
483 regs->pr = regs->pc + 4;
484 regs->pc += SH_PC_12BIT_OFFSET(instruction);
485 }
486 break;
487 }
488 return ret;
489
490 /* handle non-delay-slot instruction */
491 simple:
492 ret = handle_unaligned_ins(instruction,regs);
493 if (ret==0)
Paul Mundt53f983a2007-05-08 15:31:48 +0900494 regs->pc += instruction_size(instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return ret;
496}
Paul Mundt710ee0c2006-11-05 16:48:42 +0900497#endif /* CONFIG_CPU_SH2A */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Yoshinori Sato0983b312006-11-05 15:58:47 +0900499#ifdef CONFIG_CPU_HAS_SR_RB
500#define lookup_exception_vector(x) \
501 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
502#else
503#define lookup_exception_vector(x) \
504 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
505#endif
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900508 * Handle various address error exceptions:
509 * - instruction address error:
510 * misaligned PC
511 * PC >= 0x80000000 in user mode
512 * - data address error (read and write)
513 * misaligned data access
514 * access to >= 0x80000000 is user mode
515 * Unfortuntaly we can't distinguish between instruction address error
Simon Arlotte868d612007-05-14 08:15:10 +0900516 * and data address errors caused by read accesses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900518asmlinkage void do_address_error(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 unsigned long writeaccess,
520 unsigned long address)
521{
Yoshinori Sato0983b312006-11-05 15:58:47 +0900522 unsigned long error_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 mm_segment_t oldfs;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900524 siginfo_t info;
Paul Mundt710ee0c2006-11-05 16:48:42 +0900525#ifndef CONFIG_CPU_SH2A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 u16 instruction;
527 int tmp;
Paul Mundt710ee0c2006-11-05 16:48:42 +0900528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Yoshinori Sato0983b312006-11-05 15:58:47 +0900530 /* Intentional ifdef */
531#ifdef CONFIG_CPU_HAS_SR_RB
532 lookup_exception_vector(error_code);
533#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 oldfs = get_fs();
536
537 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900538 int si_code = BUS_ADRERR;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* bad PC is not something we can fix */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900543 if (regs->pc & 1) {
544 si_code = BUS_ADRALN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto uspace_segv;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Yoshinori Sato0983b312006-11-05 15:58:47 +0900548#ifndef CONFIG_CPU_SH2A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 set_fs(USER_DS);
550 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
551 /* Argh. Fault on the instruction itself.
552 This should never happen non-SMP
553 */
554 set_fs(oldfs);
555 goto uspace_segv;
556 }
557
558 tmp = handle_unaligned_access(instruction, regs);
559 set_fs(oldfs);
560
561 if (tmp==0)
562 return; /* sorted */
Yoshinori Sato0983b312006-11-05 15:58:47 +0900563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900565uspace_segv:
566 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
567 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
568 regs->pr);
569
570 info.si_signo = SIGBUS;
571 info.si_errno = 0;
572 info.si_code = si_code;
573 info.si_addr = (void *) address;
574 force_sig_info(SIGBUS, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 } else {
576 if (regs->pc & 1)
577 die("unaligned program counter", regs, error_code);
578
Yoshinori Sato0983b312006-11-05 15:58:47 +0900579#ifndef CONFIG_CPU_SH2A
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 set_fs(KERNEL_DS);
581 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
582 /* Argh. Fault on the instruction itself.
583 This should never happen non-SMP
584 */
585 set_fs(oldfs);
586 die("insn faulting in do_address_error", regs, 0);
587 }
588
589 handle_unaligned_access(instruction, regs);
590 set_fs(oldfs);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900591#else
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900592 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
593 "access\n", current->comm);
594
Yoshinori Sato0983b312006-11-05 15:58:47 +0900595 force_sig(SIGSEGV, current);
596#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598}
599
600#ifdef CONFIG_SH_DSP
601/*
602 * SH-DSP support gerg@snapgear.com.
603 */
604int is_dsp_inst(struct pt_regs *regs)
605{
606 unsigned short inst;
607
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900608 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * Safe guard if DSP mode is already enabled or we're lacking
610 * the DSP altogether.
611 */
Paul Mundt11c19652006-12-25 10:19:56 +0900612 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
614
615 get_user(inst, ((unsigned short *) regs->pc));
616
617 inst &= 0xf000;
618
619 /* Check for any type of DSP or support instruction */
620 if ((inst == 0xf000) || (inst == 0x4000))
621 return 1;
622
623 return 0;
624}
625#else
626#define is_dsp_inst(regs) (0)
627#endif /* CONFIG_SH_DSP */
628
Yoshinori Sato0983b312006-11-05 15:58:47 +0900629#ifdef CONFIG_CPU_SH2A
630asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
631 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900632 struct pt_regs __regs)
Yoshinori Sato0983b312006-11-05 15:58:47 +0900633{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900634 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900635 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
Paul Mundt1f666582006-10-19 16:20:25 +0900650/* arch/sh/kernel/cpu/sh4/fpu.c */
651extern int do_fpu_inst(unsigned short, struct pt_regs *);
652extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900653 unsigned long r6, unsigned long r7, struct pt_regs __regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900654
655asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
656 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900657 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900658{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900659 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900660 unsigned long error_code;
661 struct task_struct *tsk = current;
662
663#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900664 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900665 int err;
666
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900667 get_user(inst, (unsigned short*)regs->pc);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900668
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900669 err = do_fpu_inst(inst, regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900670 if (!err) {
Paul Mundt53f983a2007-05-08 15:31:48 +0900671 regs->pc += instruction_size(inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900672 return;
673 }
674 /* not a FPU inst. */
675#endif
676
677#ifdef CONFIG_SH_DSP
678 /* Check if it's a DSP instruction */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900679 if (is_dsp_inst(regs)) {
Takashi YOSHII4b565682006-09-27 17:15:32 +0900680 /* Enable DSP mode, and restart instruction. */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900681 regs->sr |= SR_DSP;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900682 return;
683 }
684#endif
685
Yoshinori Sato0983b312006-11-05 15:58:47 +0900686 lookup_exception_vector(error_code);
687
Takashi YOSHII4b565682006-09-27 17:15:32 +0900688 local_irq_enable();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900689 CHK_REMOTE_DEBUG(regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900690 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900691 die_if_no_fixup("reserved instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900692}
693
694#ifdef CONFIG_SH_FPU_EMU
695static int emulate_branch(unsigned short inst, struct pt_regs* regs)
696{
697 /*
698 * bfs: 8fxx: PC+=d*2+4;
699 * bts: 8dxx: PC+=d*2+4;
700 * bra: axxx: PC+=D*2+4;
701 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
702 * braf:0x23: PC+=Rn*2+4;
703 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
704 * jmp: 4x2b: PC=Rn;
705 * jsr: 4x0b: PC=Rn after PR=PC+4;
706 * rts: 000b: PC=PR;
707 */
708 if ((inst & 0xfd00) == 0x8d00) {
709 regs->pc += SH_PC_8BIT_OFFSET(inst);
710 return 0;
711 }
712
713 if ((inst & 0xe000) == 0xa000) {
714 regs->pc += SH_PC_12BIT_OFFSET(inst);
715 return 0;
716 }
717
718 if ((inst & 0xf0df) == 0x0003) {
719 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
720 return 0;
721 }
722
723 if ((inst & 0xf0df) == 0x400b) {
724 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
725 return 0;
726 }
727
728 if ((inst & 0xffff) == 0x000b) {
729 regs->pc = regs->pr;
730 return 0;
731 }
732
733 return 1;
734}
735#endif
736
737asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
738 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900739 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900740{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900741 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900742 unsigned long error_code;
743 struct task_struct *tsk = current;
744#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900745 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900746
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900747 get_user(inst, (unsigned short *)regs->pc + 1);
748 if (!do_fpu_inst(inst, regs)) {
749 get_user(inst, (unsigned short *)regs->pc);
750 if (!emulate_branch(inst, regs))
Takashi YOSHII4b565682006-09-27 17:15:32 +0900751 return;
752 /* fault in branch.*/
753 }
754 /* not a FPU inst. */
755#endif
756
Yoshinori Sato0983b312006-11-05 15:58:47 +0900757 lookup_exception_vector(error_code);
758
Takashi YOSHII4b565682006-09-27 17:15:32 +0900759 local_irq_enable();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900760 CHK_REMOTE_DEBUG(regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900761 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900762 die_if_no_fixup("illegal slot instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900763}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
766 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900767 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900769 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 long ex;
Yoshinori Sato0983b312006-11-05 15:58:47 +0900771
772 lookup_exception_vector(ex);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900773 die_if_kernel("exception", regs, ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776#if defined(CONFIG_SH_STANDARD_BIOS)
777void *gdb_vbr_vector;
778
779static inline void __init gdb_vbr_init(void)
780{
781 register unsigned long vbr;
782
783 /*
784 * Read the old value of the VBR register to initialise
785 * the vector through which debug and BIOS traps are
786 * delegated by the Linux trap handler.
787 */
788 asm volatile("stc vbr, %0" : "=r" (vbr));
789
790 gdb_vbr_vector = (void *)(vbr + 0x100);
791 printk("Setting GDB trap vector to 0x%08lx\n",
792 (unsigned long)gdb_vbr_vector);
793}
794#endif
795
796void __init per_cpu_trap_init(void)
797{
798 extern void *vbr_base;
799
800#ifdef CONFIG_SH_STANDARD_BIOS
801 gdb_vbr_init();
802#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
Yoshinori Sato0983b312006-11-05 15:58:47 +0900824extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
825 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900826 struct pt_regs __regs);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900827
Paul Mundt1f666582006-10-19 16:20:25 +0900828void __init trap_init(void)
829{
830 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
831 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Takashi YOSHII4b565682006-09-27 17:15:32 +0900833#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
834 defined(CONFIG_SH_FPU_EMU)
835 /*
836 * For SH-4 lacking an FPU, treat floating point instructions as
837 * reserved. They'll be handled in the math-emu case, or faulted on
838 * otherwise.
839 */
Paul Mundt1f666582006-10-19 16:20:25 +0900840 set_exception_table_evt(0x800, do_reserved_inst);
841 set_exception_table_evt(0x820, do_illegal_slot_inst);
842#elif defined(CONFIG_SH_FPU)
843 set_exception_table_evt(0x800, do_fpu_state_restore);
844 set_exception_table_evt(0x820, do_fpu_state_restore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900846
847#ifdef CONFIG_CPU_SH2
848 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_handler);
849#endif
850#ifdef CONFIG_CPU_SH2A
851 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
852 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
853#endif
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* Setup VBR for boot cpu */
856 per_cpu_trap_init();
857}
858
Paul Mundtfa691512007-03-08 19:41:21 +0900859#ifdef CONFIG_BUG
860void handle_BUG(struct pt_regs *regs)
861{
862 enum bug_trap_type tt;
863 tt = report_bug(regs->pc);
864 if (tt == BUG_TRAP_TYPE_WARN) {
865 regs->pc += 2;
866 return;
867 }
868
869 die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
870}
871
872int is_valid_bugaddr(unsigned long addr)
873{
874 return addr >= PAGE_OFFSET;
875}
876#endif
877
Paul Mundt6b002232006-10-12 17:07:45 +0900878void show_trace(struct task_struct *tsk, unsigned long *sp,
879 struct pt_regs *regs)
880{
881 unsigned long addr;
882
883 if (regs && user_mode(regs))
884 return;
885
886 printk("\nCall trace: ");
887#ifdef CONFIG_KALLSYMS
888 printk("\n");
889#endif
890
891 while (!kstack_end(sp)) {
892 addr = *sp++;
893 if (kernel_text_address(addr))
894 print_ip_sym(addr);
895 }
896
897 printk("\n");
Paul Mundt9b8c90e2006-12-06 11:07:51 +0900898
899 if (!tsk)
900 tsk = current;
901
902 debug_show_held_locks(tsk);
Paul Mundt6b002232006-10-12 17:07:45 +0900903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905void show_stack(struct task_struct *tsk, unsigned long *sp)
906{
Paul Mundt6b002232006-10-12 17:07:45 +0900907 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Paul Mundta6a311392006-09-27 18:22:14 +0900909 if (!tsk)
910 tsk = current;
911 if (tsk == current)
912 sp = (unsigned long *)current_stack_pointer;
913 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Paul Mundt6b002232006-10-12 17:07:45 +0900916 stack = (unsigned long)sp;
917 dump_mem("Stack: ", stack, THREAD_SIZE +
918 (unsigned long)task_stack_page(tsk));
919 show_trace(tsk, sp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922void dump_stack(void)
923{
924 show_stack(NULL, NULL);
925}
926EXPORT_SYMBOL(dump_stack);