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