blob: 9fe3467a51339a8565bed472d8444fc519dcc3de [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m32r/kernel/traps.c
3 *
4 * Copyright (C) 2001, 2002 Hirokazu Takata, Hiroyuki Kondo,
5 * Hitoshi Yamamoto
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008/*
9 * 'traps.c' handles hardware traps and faults after we have saved some
10 * state in 'entry.S'.
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/kallsyms.h>
15#include <linux/stddef.h>
16#include <linux/ptrace.h>
17#include <linux/mm.h>
18#include <asm/page.h>
19#include <asm/processor.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
22#include <asm/io.h>
Arun Sharma600634972011-07-26 16:09:06 -070023#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/smp.h>
26
27#include <linux/module.h>
28
29asmlinkage void alignment_check(void);
30asmlinkage void ei_handler(void);
31asmlinkage void rie_handler(void);
32asmlinkage void debug_trap(void);
33asmlinkage void cache_flushing_handler(void);
Hirokazu Takata9de11aa2005-10-11 08:29:09 -070034asmlinkage void ill_trap(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#ifdef CONFIG_SMP
37extern void smp_reschedule_interrupt(void);
38extern void smp_invalidate_interrupt(void);
39extern void smp_call_function_interrupt(void);
40extern void smp_ipi_timer_interrupt(void);
41extern void smp_flush_cache_all_interrupt(void);
Jens Axboe7b7426c2008-06-10 20:49:30 +020042extern void smp_call_function_single_interrupt(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * for Boot AP function
46 */
47asm (
48 " .section .eit_vector4,\"ax\" \n"
49 " .global _AP_RE \n"
50 " .global startup_AP \n"
51 "_AP_RE: \n"
52 " .fill 32, 4, 0 \n"
53 "_AP_EI: bra startup_AP \n"
54 " .previous \n"
55);
56#endif /* CONFIG_SMP */
57
58extern unsigned long eit_vector[];
59#define BRA_INSN(func, entry) \
60 ((unsigned long)func - (unsigned long)eit_vector - entry*4)/4 \
61 + 0xff000000UL
62
Adrian Bunk81e48072008-09-24 15:01:47 +090063static void set_eit_vector_entries(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 extern void default_eit_handler(void);
66 extern void system_call(void);
67 extern void pie_handler(void);
68 extern void ace_handler(void);
69 extern void tme_handler(void);
70 extern void _flush_cache_copyback_all(void);
71
72 eit_vector[0] = 0xd0c00001; /* seth r0, 0x01 */
73 eit_vector[1] = BRA_INSN(default_eit_handler, 1);
74 eit_vector[4] = 0xd0c00010; /* seth r0, 0x10 */
75 eit_vector[5] = BRA_INSN(default_eit_handler, 5);
76 eit_vector[8] = BRA_INSN(rie_handler, 8);
77 eit_vector[12] = BRA_INSN(alignment_check, 12);
Hirokazu Takata9de11aa2005-10-11 08:29:09 -070078 eit_vector[16] = BRA_INSN(ill_trap, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 eit_vector[17] = BRA_INSN(debug_trap, 17);
80 eit_vector[18] = BRA_INSN(system_call, 18);
Hirokazu Takata9de11aa2005-10-11 08:29:09 -070081 eit_vector[19] = BRA_INSN(ill_trap, 19);
82 eit_vector[20] = BRA_INSN(ill_trap, 20);
83 eit_vector[21] = BRA_INSN(ill_trap, 21);
84 eit_vector[22] = BRA_INSN(ill_trap, 22);
85 eit_vector[23] = BRA_INSN(ill_trap, 23);
86 eit_vector[24] = BRA_INSN(ill_trap, 24);
87 eit_vector[25] = BRA_INSN(ill_trap, 25);
88 eit_vector[26] = BRA_INSN(ill_trap, 26);
89 eit_vector[27] = BRA_INSN(ill_trap, 27);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 eit_vector[28] = BRA_INSN(cache_flushing_handler, 28);
Hirokazu Takata9de11aa2005-10-11 08:29:09 -070091 eit_vector[29] = BRA_INSN(ill_trap, 29);
92 eit_vector[30] = BRA_INSN(ill_trap, 30);
93 eit_vector[31] = BRA_INSN(ill_trap, 31);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 eit_vector[32] = BRA_INSN(ei_handler, 32);
95 eit_vector[64] = BRA_INSN(pie_handler, 64);
96#ifdef CONFIG_MMU
97 eit_vector[68] = BRA_INSN(ace_handler, 68);
98 eit_vector[72] = BRA_INSN(tme_handler, 72);
99#endif /* CONFIG_MMU */
100#ifdef CONFIG_SMP
101 eit_vector[184] = (unsigned long)smp_reschedule_interrupt;
102 eit_vector[185] = (unsigned long)smp_invalidate_interrupt;
103 eit_vector[186] = (unsigned long)smp_call_function_interrupt;
104 eit_vector[187] = (unsigned long)smp_ipi_timer_interrupt;
105 eit_vector[188] = (unsigned long)smp_flush_cache_all_interrupt;
Toshihiro HANAWA0a3d31b2009-10-02 17:52:54 +0900106 eit_vector[189] = 0; /* CPU_BOOT_IPI */
107 eit_vector[190] = (unsigned long)smp_call_function_single_interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 eit_vector[191] = 0;
109#endif
110 _flush_cache_copyback_all();
111}
112
113void __init trap_init(void)
114{
115 set_eit_vector_entries();
116
117 /*
118 * Should be a barrier for any external CPU state.
119 */
120 cpu_init();
121}
122
Adrian Bunk81e48072008-09-24 15:01:47 +0900123static int kstack_depth_to_print = 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Adrian Bunk81e48072008-09-24 15:01:47 +0900125static void show_trace(struct task_struct *task, unsigned long *stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 unsigned long addr;
128
129 if (!stack)
130 stack = (unsigned long*)&stack;
131
132 printk("Call Trace: ");
133 while (!kstack_end(stack)) {
134 addr = *stack++;
Joe Perchesc8d5cb72012-12-12 10:18:57 -0800135 if (__kernel_text_address(addr))
136 printk("[<%08lx>] %pSR\n", addr, (void *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 printk("\n");
139}
140
141void show_stack(struct task_struct *task, unsigned long *sp)
142{
143 unsigned long *stack;
144 int i;
145
146 /*
147 * debugging aid: "show_stack(NULL);" prints the
148 * back trace for this cpu.
149 */
150
151 if(sp==NULL) {
152 if (task)
153 sp = (unsigned long *)task->thread.sp;
154 else
155 sp=(unsigned long*)&sp;
156 }
157
158 stack = sp;
159 for(i=0; i < kstack_depth_to_print; i++) {
160 if (kstack_end(stack))
161 break;
162 if (i && ((i % 4) == 0))
163 printk("\n ");
164 printk("%08lx ", *stack++);
165 }
166 printk("\n");
167 show_trace(task, sp);
168}
169
170void dump_stack(void)
171{
172 unsigned long stack;
173
174 show_trace(current, &stack);
175}
176
177EXPORT_SYMBOL(dump_stack);
178
179static void show_registers(struct pt_regs *regs)
180{
181 int i = 0;
182 int in_kernel = 1;
183 unsigned long sp;
184
185 printk("CPU: %d\n", smp_processor_id());
186 show_regs(regs);
187
188 sp = (unsigned long) (1+regs);
189 if (user_mode(regs)) {
190 in_kernel = 0;
191 sp = regs->spu;
192 printk("SPU: %08lx\n", sp);
193 } else {
194 printk("SPI: %08lx\n", sp);
195 }
196 printk("Process %s (pid: %d, process nr: %d, stackpage=%08lx)",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700197 current->comm, task_pid_nr(current), 0xffff & i, 4096+(unsigned long)current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /*
200 * When in-kernel, we also print out the stack and code at the
201 * time of the fault..
202 */
203 if (in_kernel) {
204 printk("\nStack: ");
205 show_stack(current, (unsigned long*) sp);
206
207 printk("\nCode: ");
208 if (regs->bpc < PAGE_OFFSET)
209 goto bad;
210
211 for(i=0;i<20;i++) {
212 unsigned char c;
213 if (__get_user(c, &((unsigned char*)regs->bpc)[i])) {
214bad:
215 printk(" Bad PC value.");
216 break;
217 }
218 printk("%02x ", c);
219 }
220 }
221 printk("\n");
222}
223
Adrian Bunk81e48072008-09-24 15:01:47 +0900224static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226void die(const char * str, struct pt_regs * regs, long err)
227{
228 console_verbose();
229 spin_lock_irq(&die_lock);
230 bust_spinlocks(1);
231 printk("%s: %04lx\n", str, err & 0xffff);
232 show_registers(regs);
233 bust_spinlocks(0);
234 spin_unlock_irq(&die_lock);
235 do_exit(SIGSEGV);
236}
237
238static __inline__ void die_if_kernel(const char * str,
239 struct pt_regs * regs, long err)
240{
241 if (!user_mode(regs))
242 die(str, regs, err);
243}
244
245static __inline__ void do_trap(int trapnr, int signr, const char * str,
246 struct pt_regs * regs, long error_code, siginfo_t *info)
247{
248 if (user_mode(regs)) {
249 /* trap_signal */
250 struct task_struct *tsk = current;
251 tsk->thread.error_code = error_code;
252 tsk->thread.trap_no = trapnr;
253 if (info)
254 force_sig_info(signr, info, tsk);
255 else
256 force_sig(signr, tsk);
257 return;
258 } else {
259 /* kernel_trap */
260 if (!fixup_exception(regs))
261 die(str, regs, error_code);
262 return;
263 }
264}
265
266#define DO_ERROR(trapnr, signr, str, name) \
267asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
268{ \
Al Viro12ea59e2006-10-11 17:24:35 +0100269 do_trap(trapnr, signr, NULL, regs, error_code, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272#define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
273asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
274{ \
275 siginfo_t info; \
276 info.si_signo = signr; \
277 info.si_errno = 0; \
278 info.si_code = sicode; \
279 info.si_addr = (void __user *)siaddr; \
280 do_trap(trapnr, signr, str, regs, error_code, &info); \
281}
282
283DO_ERROR( 1, SIGTRAP, "debug trap", debug_trap)
284DO_ERROR_INFO(0x20, SIGILL, "reserved instruction ", rie_handler, ILL_ILLOPC, regs->bpc)
Hirokazu Takata9de11aa2005-10-11 08:29:09 -0700285DO_ERROR_INFO(0x100, SIGILL, "privileged instruction", pie_handler, ILL_PRVOPC, regs->bpc)
286DO_ERROR_INFO(-1, SIGILL, "illegal trap", ill_trap, ILL_ILLTRP, regs->bpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288extern int handle_unaligned_access(unsigned long, struct pt_regs *);
289
290/* This code taken from arch/sh/kernel/traps.c */
291asmlinkage void do_alignment_check(struct pt_regs *regs, long error_code)
292{
293 mm_segment_t oldfs;
294 unsigned long insn;
295 int tmp;
296
297 oldfs = get_fs();
298
299 if (user_mode(regs)) {
300 local_irq_enable();
301 current->thread.error_code = error_code;
302 current->thread.trap_no = 0x17;
303
304 set_fs(USER_DS);
305 if (copy_from_user(&insn, (void *)regs->bpc, 4)) {
306 set_fs(oldfs);
307 goto uspace_segv;
308 }
309 tmp = handle_unaligned_access(insn, regs);
310 set_fs(oldfs);
311
312 if (!tmp)
313 return;
314
315 uspace_segv:
316 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
317 "access\n", current->comm);
318 force_sig(SIGSEGV, current);
319 } else {
320 set_fs(KERNEL_DS);
321 if (copy_from_user(&insn, (void *)regs->bpc, 4)) {
322 set_fs(oldfs);
323 die("insn faulting in do_address_error", regs, 0);
324 }
325 handle_unaligned_access(insn, regs);
326 set_fs(oldfs);
327 }
328}