blob: 6aba9af79eaf023a818434a01166dac1cd557c24 [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>
Russell Kingba84be22009-01-06 14:41:07 -080016#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/spinlock.h>
19#include <linux/module.h>
20#include <linux/kallsyms.h>
Paul Mundt1f666582006-10-19 16:20:25 +090021#include <linux/io.h>
Paul Mundtfa691512007-03-08 19:41:21 +090022#include <linux/bug.h>
Paul Mundt9b8c90e2006-12-06 11:07:51 +090023#include <linux/debug_locks.h>
Paul Mundtb118ca52007-05-09 10:55:38 +090024#include <linux/kdebug.h>
Paul Mundte1132762007-05-15 08:36:36 +090025#include <linux/kexec.h>
Paul Mundtdc34d312006-12-08 17:41:43 +090026#include <linux/limits.h>
Andre Draszik7436cde2009-08-24 14:53:46 +090027#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/system.h>
29#include <asm/uaccess.h>
Andrew Mortonfad0f902008-04-16 02:03:51 +090030#include <asm/fpu.h>
Chris Smithd39f5452008-09-05 17:15:39 +090031#include <asm/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#ifdef CONFIG_CPU_SH2
Yoshinori Sato0983b312006-11-05 15:58:47 +090034# define TRAP_RESERVED_INST 4
35# define TRAP_ILLEGAL_SLOT_INST 6
36# define TRAP_ADDRESS_ERROR 9
37# ifdef CONFIG_CPU_SH2A
Peter Griffincd894362009-05-08 15:51:51 +010038# define TRAP_UBC 12
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +090039# define TRAP_FPU_ERROR 13
Yoshinori Sato0983b312006-11-05 15:58:47 +090040# define TRAP_DIVZERO_ERROR 17
41# define TRAP_DIVOVF_ERROR 18
42# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define TRAP_RESERVED_INST 12
45#define TRAP_ILLEGAL_SLOT_INST 13
46#endif
47
Andre Draszik7436cde2009-08-24 14:53:46 +090048static unsigned long se_user;
49static unsigned long se_sys;
Andre Draszik7436cde2009-08-24 14:53:46 +090050static unsigned long se_half;
51static unsigned long se_word;
52static unsigned long se_dword;
53static unsigned long se_multi;
54/* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not
55 valid! */
56static int se_usermode = 3;
57/* 0: no warning 1: print a warning message */
58static int se_kernmode_warn = 1;
59
60#ifdef CONFIG_PROC_FS
61static const char *se_usermode_action[] = {
62 "ignored",
63 "warn",
64 "fixup",
65 "fixup+warn",
66 "signal",
67 "signal+warn"
68};
69
70static int
71proc_alignment_read(char *page, char **start, off_t off, int count, int *eof,
72 void *data)
73{
74 char *p = page;
75 int len;
76
77 p += sprintf(p, "User:\t\t%lu\n", se_user);
78 p += sprintf(p, "System:\t\t%lu\n", se_sys);
Andre Draszik7436cde2009-08-24 14:53:46 +090079 p += sprintf(p, "Half:\t\t%lu\n", se_half);
80 p += sprintf(p, "Word:\t\t%lu\n", se_word);
81 p += sprintf(p, "DWord:\t\t%lu\n", se_dword);
82 p += sprintf(p, "Multi:\t\t%lu\n", se_multi);
83 p += sprintf(p, "User faults:\t%i (%s)\n", se_usermode,
84 se_usermode_action[se_usermode]);
85 p += sprintf(p, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn,
86 se_kernmode_warn ? "+warn" : "");
87
88 len = (p - page) - off;
89 if (len < 0)
90 len = 0;
91
92 *eof = (len <= count) ? 1 : 0;
93 *start = page + off;
94
95 return len;
96}
97
98static int proc_alignment_write(struct file *file, const char __user *buffer,
99 unsigned long count, void *data)
100{
101 char mode;
102
103 if (count > 0) {
104 if (get_user(mode, buffer))
105 return -EFAULT;
106 if (mode >= '0' && mode <= '5')
107 se_usermode = mode - '0';
108 }
109 return count;
110}
111
112static int proc_alignment_kern_write(struct file *file, const char __user *buffer,
113 unsigned long count, void *data)
114{
115 char mode;
116
117 if (count > 0) {
118 if (get_user(mode, buffer))
119 return -EFAULT;
120 if (mode >= '0' && mode <= '1')
121 se_kernmode_warn = mode - '0';
122 }
123 return count;
124}
125#endif
126
Paul Mundt6b002232006-10-12 17:07:45 +0900127static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
128{
129 unsigned long p;
130 int i;
131
132 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
133
134 for (p = bottom & ~31; p < top; ) {
135 printk("%04lx: ", p & 0xffff);
136
137 for (i = 0; i < 8; i++, p += 4) {
138 unsigned int val;
139
140 if (p < bottom || p >= top)
141 printk(" ");
142 else {
143 if (__get_user(val, (unsigned int __user *)p)) {
144 printk("\n");
145 return;
146 }
147 printk("%08x ", val);
148 }
149 }
150 printk("\n");
151 }
152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Paul Mundt3a2e1172007-05-01 16:33:10 +0900154static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156void die(const char * str, struct pt_regs * regs, long err)
157{
158 static int die_counter;
159
Paul Mundt55273982007-06-18 18:57:13 +0900160 oops_enter();
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 console_verbose();
163 spin_lock_irq(&die_lock);
Paul Mundt6b002232006-10-12 17:07:45 +0900164 bust_spinlocks(1);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Paul Mundt6b002232006-10-12 17:07:45 +0900167
Paul Mundt6b002232006-10-12 17:07:45 +0900168 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 show_regs(regs);
Paul Mundt6b002232006-10-12 17:07:45 +0900170
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700171 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
172 task_pid_nr(current), task_stack_page(current) + 1);
Paul Mundt6b002232006-10-12 17:07:45 +0900173
174 if (!user_mode(regs) || in_interrupt())
175 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900176 (unsigned long)task_stack_page(current));
Paul Mundt6b002232006-10-12 17:07:45 +0900177
Paul Mundtc9306f02008-10-21 18:33:36 +0900178 notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
179
Paul Mundt6b002232006-10-12 17:07:45 +0900180 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700181 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 spin_unlock_irq(&die_lock);
Paul Mundte1132762007-05-15 08:36:36 +0900183
184 if (kexec_should_crash(current))
185 crash_kexec(regs);
186
187 if (in_interrupt())
188 panic("Fatal exception in interrupt");
189
190 if (panic_on_oops)
191 panic("Fatal exception");
192
Paul Mundt55273982007-06-18 18:57:13 +0900193 oops_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 do_exit(SIGSEGV);
195}
196
Paul Mundt6b002232006-10-12 17:07:45 +0900197static inline void die_if_kernel(const char *str, struct pt_regs *regs,
198 long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 if (!user_mode(regs))
201 die(str, regs, err);
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * try and fix up kernelspace address errors
206 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
207 * - kernel/userspace interfaces cause a jump to an appropriate handler
208 * - other kernel errors are bad
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900210static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Paul Mundt6b002232006-10-12 17:07:45 +0900212 if (!user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 const struct exception_table_entry *fixup;
214 fixup = search_exception_tables(regs->pc);
215 if (fixup) {
216 regs->pc = fixup->fixup;
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900217 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Matt Flemingb344e242009-08-16 21:54:48 +0100219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 die(str, regs, err);
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Magnus Damm86c01792008-02-07 00:02:50 +0900224static inline void sign_extend(unsigned int count, unsigned char *dst)
225{
226#ifdef __LITTLE_ENDIAN__
Magnus Damm4252c652008-02-07 19:58:46 +0900227 if ((count == 1) && dst[0] & 0x80) {
228 dst[1] = 0xff;
229 dst[2] = 0xff;
230 dst[3] = 0xff;
231 }
Magnus Damm86c01792008-02-07 00:02:50 +0900232 if ((count == 2) && dst[1] & 0x80) {
233 dst[2] = 0xff;
234 dst[3] = 0xff;
235 }
236#else
Magnus Damm4252c652008-02-07 19:58:46 +0900237 if ((count == 1) && dst[3] & 0x80) {
238 dst[2] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900239 dst[1] = 0xff;
Magnus Damm4252c652008-02-07 19:58:46 +0900240 dst[0] = 0xff;
241 }
242 if ((count == 2) && dst[2] & 0x80) {
243 dst[1] = 0xff;
244 dst[0] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900245 }
246#endif
247}
248
Magnus Damme7cc9a72008-02-07 20:18:21 +0900249static struct mem_access user_mem_access = {
250 copy_from_user,
251 copy_to_user,
252};
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
255 * handle an instruction that does an unaligned memory access by emulating the
256 * desired behaviour
257 * - note that PC _may not_ point to the faulting instruction
258 * (if that instruction is in a branch delay slot)
259 * - return 0 if emulation okay, -EFAULT on existential error
260 */
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900261static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
Magnus Damme7cc9a72008-02-07 20:18:21 +0900262 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int ret, index, count;
265 unsigned long *rm, *rn;
266 unsigned char *src, *dst;
Paul Mundtfa439722008-09-04 18:53:58 +0900267 unsigned char __user *srcu, *dstu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 index = (instruction>>8)&15; /* 0x0F00 */
270 rn = &regs->regs[index];
271
272 index = (instruction>>4)&15; /* 0x00F0 */
273 rm = &regs->regs[index];
274
275 count = 1<<(instruction&3);
276
Andre Draszik7436cde2009-08-24 14:53:46 +0900277 switch (count) {
278 case 1: se_half += 1; break;
279 case 2: se_word += 1; break;
280 case 4: se_dword += 1; break;
281 case 8: se_multi += 1; break; /* ??? */
282 }
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ret = -EFAULT;
285 switch (instruction>>12) {
286 case 0: /* mov.[bwl] to/from memory via r0+rn */
287 if (instruction & 8) {
288 /* from memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900289 srcu = (unsigned char __user *)*rm;
290 srcu += regs->regs[0];
291 dst = (unsigned char *)rn;
292 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Magnus Damm86c01792008-02-07 00:02:50 +0900294#if !defined(__LITTLE_ENDIAN__)
295 dst += 4-count;
296#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900297 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto fetch_fault;
299
Magnus Damm86c01792008-02-07 00:02:50 +0900300 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 } else {
302 /* to memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900303 src = (unsigned char *)rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#if !defined(__LITTLE_ENDIAN__)
305 src += 4-count;
306#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900307 dstu = (unsigned char __user *)*rn;
308 dstu += regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Paul Mundtfa439722008-09-04 18:53:58 +0900310 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto fetch_fault;
312 }
313 ret = 0;
314 break;
315
316 case 1: /* mov.l Rm,@(disp,Rn) */
317 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900318 dstu = (unsigned char __user *)*rn;
319 dstu += (instruction&0x000F)<<2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Paul Mundtfa439722008-09-04 18:53:58 +0900321 if (ma->to(dstu, src, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 goto fetch_fault;
323 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900324 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
327 if (instruction & 4)
328 *rn -= count;
329 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900330 dstu = (unsigned char __user *)*rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#if !defined(__LITTLE_ENDIAN__)
332 src += 4-count;
333#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900334 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto fetch_fault;
336 ret = 0;
337 break;
338
339 case 5: /* mov.l @(disp,Rm),Rn */
Paul Mundtfa439722008-09-04 18:53:58 +0900340 srcu = (unsigned char __user *)*rm;
341 srcu += (instruction & 0x000F) << 2;
342 dst = (unsigned char *)rn;
343 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Paul Mundtfa439722008-09-04 18:53:58 +0900345 if (ma->from(dst, srcu, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto fetch_fault;
347 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 case 6: /* mov.[bwl] from memory, possibly with post-increment */
Paul Mundtfa439722008-09-04 18:53:58 +0900351 srcu = (unsigned char __user *)*rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (instruction & 4)
353 *rm += count;
354 dst = (unsigned char*) rn;
355 *(unsigned long*)dst = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900356
Magnus Damm86c01792008-02-07 00:02:50 +0900357#if !defined(__LITTLE_ENDIAN__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dst += 4-count;
Magnus Damm86c01792008-02-07 00:02:50 +0900359#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900360 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900362 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ret = 0;
364 break;
365
366 case 8:
367 switch ((instruction&0xFF00)>>8) {
368 case 0x81: /* mov.w R0,@(disp,Rn) */
Paul Mundtfa439722008-09-04 18:53:58 +0900369 src = (unsigned char *) &regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#if !defined(__LITTLE_ENDIAN__)
371 src += 2;
372#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900373 dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
374 dstu += (instruction & 0x000F) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Paul Mundtfa439722008-09-04 18:53:58 +0900376 if (ma->to(dstu, src, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto fetch_fault;
378 ret = 0;
379 break;
380
381 case 0x85: /* mov.w @(disp,Rm),R0 */
Paul Mundtfa439722008-09-04 18:53:58 +0900382 srcu = (unsigned char __user *)*rm;
383 srcu += (instruction & 0x000F) << 1;
384 dst = (unsigned char *) &regs->regs[0];
385 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387#if !defined(__LITTLE_ENDIAN__)
388 dst += 2;
389#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900390 if (ma->from(dst, srcu, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900392 sign_extend(2, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 ret = 0;
394 break;
395 }
396 break;
397 }
398 return ret;
399
400 fetch_fault:
401 /* Argh. Address not only misaligned but also non-existent.
402 * Raise an EFAULT and see if it's trapped
403 */
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900404 die_if_no_fixup("Fault in unaligned fixup", regs, 0);
405 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408/*
409 * emulate the instruction in the delay slot
410 * - fetches the instruction from PC+2
411 */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900412static inline int handle_delayslot(struct pt_regs *regs,
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900413 insn_size_t old_instruction,
Magnus Damme7cc9a72008-02-07 20:18:21 +0900414 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900416 insn_size_t instruction;
Paul Mundtfa439722008-09-04 18:53:58 +0900417 void __user *addr = (void __user *)(regs->pc +
418 instruction_size(old_instruction));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900420 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* the instruction-fetch faulted */
422 if (user_mode(regs))
423 return -EFAULT;
424
425 /* kernel */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900426 die("delay-slot-insn faulting in handle_unaligned_delayslot",
427 regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
Magnus Damme7cc9a72008-02-07 20:18:21 +0900430 return handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433/*
434 * handle an instruction that does an unaligned memory access
435 * - have to be careful of branch delay-slot instructions that fault
436 * SH3:
437 * - if the branch would be taken PC points to the branch
438 * - if the branch would not be taken, PC points to delay-slot
439 * SH4:
440 * - PC always points to delayed branch
441 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
442 */
443
444/* Macros to determine offset from current PC for branch instructions */
445/* Explicit type coercion is used to force sign extension where needed */
446#define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
447#define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
448
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900449int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000450 struct mem_access *ma, int expected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 u_int rm;
453 int ret, index;
454
455 index = (instruction>>8)&15; /* 0x0F00 */
456 rm = regs->regs[index];
457
Andre Draszik9a4af022009-08-24 14:38:27 +0900458 /* shout about fixups */
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000459 if (!expected && printk_ratelimit())
Andre Draszik9a4af022009-08-24 14:38:27 +0900460 printk(KERN_NOTICE "Fixing up unaligned %s access "
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900461 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
Andre Draszik9a4af022009-08-24 14:38:27 +0900462 user_mode(regs) ? "userspace" : "kernel",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700463 current->comm, task_pid_nr(current),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900464 (void *)regs->pc, instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 ret = -EFAULT;
467 switch (instruction&0xF000) {
468 case 0x0000:
469 if (instruction==0x000B) {
470 /* rts */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900471 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (ret==0)
473 regs->pc = regs->pr;
474 }
475 else if ((instruction&0x00FF)==0x0023) {
476 /* braf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900477 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (ret==0)
479 regs->pc += rm + 4;
480 }
481 else if ((instruction&0x00FF)==0x0003) {
482 /* bsrf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900483 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (ret==0) {
485 regs->pr = regs->pc + 4;
486 regs->pc += rm + 4;
487 }
488 }
489 else {
490 /* mov.[bwl] to/from memory via r0+rn */
491 goto simple;
492 }
493 break;
494
495 case 0x1000: /* mov.l Rm,@(disp,Rn) */
496 goto simple;
497
498 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
499 goto simple;
500
501 case 0x4000:
502 if ((instruction&0x00FF)==0x002B) {
503 /* jmp @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900504 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (ret==0)
506 regs->pc = rm;
507 }
508 else if ((instruction&0x00FF)==0x000B) {
509 /* jsr @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900510 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (ret==0) {
512 regs->pr = regs->pc + 4;
513 regs->pc = rm;
514 }
515 }
516 else {
517 /* mov.[bwl] to/from memory via r0+rn */
518 goto simple;
519 }
520 break;
521
522 case 0x5000: /* mov.l @(disp,Rm),Rn */
523 goto simple;
524
525 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
526 goto simple;
527
528 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
529 switch (instruction&0x0F00) {
530 case 0x0100: /* mov.w R0,@(disp,Rm) */
531 goto simple;
532 case 0x0500: /* mov.w @(disp,Rm),R0 */
533 goto simple;
534 case 0x0B00: /* bf lab - no delayslot*/
535 break;
536 case 0x0F00: /* bf/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900537 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (ret==0) {
539#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
540 if ((regs->sr & 0x00000001) != 0)
541 regs->pc += 4; /* next after slot */
542 else
543#endif
544 regs->pc += SH_PC_8BIT_OFFSET(instruction);
545 }
546 break;
547 case 0x0900: /* bt lab - no delayslot */
548 break;
549 case 0x0D00: /* bt/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900550 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (ret==0) {
552#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
553 if ((regs->sr & 0x00000001) == 0)
554 regs->pc += 4; /* next after slot */
555 else
556#endif
557 regs->pc += SH_PC_8BIT_OFFSET(instruction);
558 }
559 break;
560 }
561 break;
562
563 case 0xA000: /* bra label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900564 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (ret==0)
566 regs->pc += SH_PC_12BIT_OFFSET(instruction);
567 break;
568
569 case 0xB000: /* bsr label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900570 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (ret==0) {
572 regs->pr = regs->pc + 4;
573 regs->pc += SH_PC_12BIT_OFFSET(instruction);
574 }
575 break;
576 }
577 return ret;
578
579 /* handle non-delay-slot instruction */
580 simple:
Magnus Damme7cc9a72008-02-07 20:18:21 +0900581 ret = handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (ret==0)
Paul Mundt53f983a2007-05-08 15:31:48 +0900583 regs->pc += instruction_size(instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return ret;
585}
586
587/*
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900588 * Handle various address error exceptions:
589 * - instruction address error:
590 * misaligned PC
591 * PC >= 0x80000000 in user mode
592 * - data address error (read and write)
593 * misaligned data access
594 * access to >= 0x80000000 is user mode
595 * Unfortuntaly we can't distinguish between instruction address error
Simon Arlotte868d612007-05-14 08:15:10 +0900596 * and data address errors caused by read accesses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900598asmlinkage void do_address_error(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 unsigned long writeaccess,
600 unsigned long address)
601{
Yoshinori Sato0983b312006-11-05 15:58:47 +0900602 unsigned long error_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 mm_segment_t oldfs;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900604 siginfo_t info;
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900605 insn_size_t instruction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int tmp;
607
Yoshinori Sato0983b312006-11-05 15:58:47 +0900608 /* Intentional ifdef */
609#ifdef CONFIG_CPU_HAS_SR_RB
Paul Mundt4c59e292008-09-21 12:00:23 +0900610 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900611#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 oldfs = get_fs();
614
615 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900616 int si_code = BUS_ADRERR;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Andre Draszik7436cde2009-08-24 14:53:46 +0900620 se_user += 1;
621
Andre Draszik5a0ab352009-08-24 15:01:10 +0900622#ifndef CONFIG_CPU_SH2A
623 set_fs(USER_DS);
624 if (copy_from_user(&instruction, (u16 *)(regs->pc & ~1), 2)) {
625 set_fs(oldfs);
626 goto uspace_segv;
627 }
628 set_fs(oldfs);
629
Andre Draszik7436cde2009-08-24 14:53:46 +0900630 /* shout about userspace fixups */
631 if (se_usermode & 1)
632 printk(KERN_NOTICE "Unaligned userspace access "
633 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
634 current->comm, current->pid, (void *)regs->pc,
635 instruction);
Andre Draszik5a0ab352009-08-24 15:01:10 +0900636#endif
Andre Draszik7436cde2009-08-24 14:53:46 +0900637
638 if (se_usermode & 2)
639 goto fixup;
640
641 if (se_usermode & 4)
642 goto uspace_segv;
643 else {
644 /* ignore */
Andre Draszik5a0ab352009-08-24 15:01:10 +0900645 regs->pc += instruction_size(instruction);
Andre Draszik7436cde2009-08-24 14:53:46 +0900646 return;
647 }
648
649fixup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* bad PC is not something we can fix */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900651 if (regs->pc & 1) {
652 si_code = BUS_ADRALN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto uspace_segv;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 set_fs(USER_DS);
Magnus Damme7cc9a72008-02-07 20:18:21 +0900657 tmp = handle_unaligned_access(instruction, regs,
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000658 &user_mem_access, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 set_fs(oldfs);
660
661 if (tmp==0)
662 return; /* sorted */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900663uspace_segv:
664 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
665 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
666 regs->pr);
667
668 info.si_signo = SIGBUS;
669 info.si_errno = 0;
670 info.si_code = si_code;
Paul Mundte08f4572007-05-14 12:52:56 +0900671 info.si_addr = (void __user *)address;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900672 force_sig_info(SIGBUS, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 } else {
Andre Draszik7436cde2009-08-24 14:53:46 +0900674 se_sys += 1;
675
676 if (se_kernmode_warn)
677 printk(KERN_NOTICE "Unaligned kernel access "
678 "on behalf of \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
679 current->comm, current->pid, (void *)regs->pc,
680 instruction);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (regs->pc & 1)
683 die("unaligned program counter", regs, error_code);
684
685 set_fs(KERNEL_DS);
Paul Mundtfa439722008-09-04 18:53:58 +0900686 if (copy_from_user(&instruction, (void __user *)(regs->pc),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900687 sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* Argh. Fault on the instruction itself.
689 This should never happen non-SMP
690 */
691 set_fs(oldfs);
692 die("insn faulting in do_address_error", regs, 0);
693 }
694
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000695 handle_unaligned_access(instruction, regs,
696 &user_mem_access, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 set_fs(oldfs);
698 }
699}
700
701#ifdef CONFIG_SH_DSP
702/*
703 * SH-DSP support gerg@snapgear.com.
704 */
705int is_dsp_inst(struct pt_regs *regs)
706{
Paul Mundt882c12c2007-05-14 17:26:34 +0900707 unsigned short inst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900709 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 * Safe guard if DSP mode is already enabled or we're lacking
711 * the DSP altogether.
712 */
Paul Mundt11c19652006-12-25 10:19:56 +0900713 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return 0;
715
716 get_user(inst, ((unsigned short *) regs->pc));
717
718 inst &= 0xf000;
719
720 /* Check for any type of DSP or support instruction */
721 if ((inst == 0xf000) || (inst == 0x4000))
722 return 1;
723
724 return 0;
725}
726#else
727#define is_dsp_inst(regs) (0)
728#endif /* CONFIG_SH_DSP */
729
Yoshinori Sato0983b312006-11-05 15:58:47 +0900730#ifdef CONFIG_CPU_SH2A
731asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
732 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900733 struct pt_regs __regs)
Yoshinori Sato0983b312006-11-05 15:58:47 +0900734{
735 siginfo_t info;
736
Yoshinori Sato0983b312006-11-05 15:58:47 +0900737 switch (r4) {
738 case TRAP_DIVZERO_ERROR:
739 info.si_code = FPE_INTDIV;
740 break;
741 case TRAP_DIVOVF_ERROR:
742 info.si_code = FPE_INTOVF;
743 break;
744 }
745
746 force_sig_info(SIGFPE, &info, current);
747}
748#endif
749
Takashi YOSHII4b565682006-09-27 17:15:32 +0900750asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
751 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900752 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900753{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900754 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900755 unsigned long error_code;
756 struct task_struct *tsk = current;
757
758#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900759 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900760 int err;
761
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900762 get_user(inst, (unsigned short*)regs->pc);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900763
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900764 err = do_fpu_inst(inst, regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900765 if (!err) {
Paul Mundt53f983a2007-05-08 15:31:48 +0900766 regs->pc += instruction_size(inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900767 return;
768 }
769 /* not a FPU inst. */
770#endif
771
772#ifdef CONFIG_SH_DSP
773 /* Check if it's a DSP instruction */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900774 if (is_dsp_inst(regs)) {
Takashi YOSHII4b565682006-09-27 17:15:32 +0900775 /* Enable DSP mode, and restart instruction. */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900776 regs->sr |= SR_DSP;
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000777 /* Save DSP mode */
778 tsk->thread.dsp_status.status |= SR_DSP;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900779 return;
780 }
781#endif
782
Paul Mundt4c59e292008-09-21 12:00:23 +0900783 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900784
Takashi YOSHII4b565682006-09-27 17:15:32 +0900785 local_irq_enable();
Takashi YOSHII4b565682006-09-27 17:15:32 +0900786 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900787 die_if_no_fixup("reserved instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900788}
789
790#ifdef CONFIG_SH_FPU_EMU
Paul Mundtedfd6da2008-11-26 13:06:04 +0900791static int emulate_branch(unsigned short inst, struct pt_regs *regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900792{
793 /*
794 * bfs: 8fxx: PC+=d*2+4;
795 * bts: 8dxx: PC+=d*2+4;
796 * bra: axxx: PC+=D*2+4;
797 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
798 * braf:0x23: PC+=Rn*2+4;
799 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
800 * jmp: 4x2b: PC=Rn;
801 * jsr: 4x0b: PC=Rn after PR=PC+4;
802 * rts: 000b: PC=PR;
803 */
Paul Mundtedfd6da2008-11-26 13:06:04 +0900804 if (((inst & 0xf000) == 0xb000) || /* bsr */
805 ((inst & 0xf0ff) == 0x0003) || /* bsrf */
806 ((inst & 0xf0ff) == 0x400b)) /* jsr */
807 regs->pr = regs->pc + 4;
808
809 if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900810 regs->pc += SH_PC_8BIT_OFFSET(inst);
811 return 0;
812 }
813
Paul Mundtedfd6da2008-11-26 13:06:04 +0900814 if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900815 regs->pc += SH_PC_12BIT_OFFSET(inst);
816 return 0;
817 }
818
Paul Mundtedfd6da2008-11-26 13:06:04 +0900819 if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900820 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
821 return 0;
822 }
823
Paul Mundtedfd6da2008-11-26 13:06:04 +0900824 if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900825 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
826 return 0;
827 }
828
Paul Mundtedfd6da2008-11-26 13:06:04 +0900829 if ((inst & 0xffff) == 0x000b) { /* rts */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900830 regs->pc = regs->pr;
831 return 0;
832 }
833
834 return 1;
835}
836#endif
837
838asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
839 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900840 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900841{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900842 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900843 unsigned long inst;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900844 struct task_struct *tsk = current;
Chris Smithd39f5452008-09-05 17:15:39 +0900845
846 if (kprobe_handle_illslot(regs->pc) == 0)
847 return;
848
Takashi YOSHII4b565682006-09-27 17:15:32 +0900849#ifdef CONFIG_SH_FPU_EMU
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900850 get_user(inst, (unsigned short *)regs->pc + 1);
851 if (!do_fpu_inst(inst, regs)) {
852 get_user(inst, (unsigned short *)regs->pc);
853 if (!emulate_branch(inst, regs))
Takashi YOSHII4b565682006-09-27 17:15:32 +0900854 return;
855 /* fault in branch.*/
856 }
857 /* not a FPU inst. */
858#endif
859
Paul Mundt4c59e292008-09-21 12:00:23 +0900860 inst = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900861
Takashi YOSHII4b565682006-09-27 17:15:32 +0900862 local_irq_enable();
Takashi YOSHII4b565682006-09-27 17:15:32 +0900863 force_sig(SIGILL, tsk);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900864 die_if_no_fixup("illegal slot instruction", regs, inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900865}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
868 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900869 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900871 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 long ex;
Yoshinori Sato0983b312006-11-05 15:58:47 +0900873
Paul Mundt4c59e292008-09-21 12:00:23 +0900874 ex = lookup_exception_vector();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900875 die_if_kernel("exception", regs, ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878#if defined(CONFIG_SH_STANDARD_BIOS)
879void *gdb_vbr_vector;
880
881static inline void __init gdb_vbr_init(void)
882{
883 register unsigned long vbr;
884
885 /*
886 * Read the old value of the VBR register to initialise
887 * the vector through which debug and BIOS traps are
888 * delegated by the Linux trap handler.
889 */
890 asm volatile("stc vbr, %0" : "=r" (vbr));
891
892 gdb_vbr_vector = (void *)(vbr + 0x100);
893 printk("Setting GDB trap vector to 0x%08lx\n",
894 (unsigned long)gdb_vbr_vector);
895}
896#endif
897
Paul Mundtaba10302007-09-21 18:32:32 +0900898void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 extern void *vbr_base;
901
902#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtaba10302007-09-21 18:32:32 +0900903 if (raw_smp_processor_id() == 0)
904 gdb_vbr_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#endif
906
907 /* NOTE: The VBR value should be at P1
908 (or P2, virtural "fixed" address space).
909 It's definitely should not in physical address. */
910
911 asm volatile("ldc %0, vbr"
912 : /* no output */
913 : "r" (&vbr_base)
914 : "memory");
915}
916
Paul Mundt1f666582006-10-19 16:20:25 +0900917void *set_exception_table_vec(unsigned int vec, void *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 extern void *exception_handling_table[];
Paul Mundt1f666582006-10-19 16:20:25 +0900920 void *old_handler;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900921
Paul Mundt1f666582006-10-19 16:20:25 +0900922 old_handler = exception_handling_table[vec];
923 exception_handling_table[vec] = handler;
924 return old_handler;
925}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Paul Mundt1f666582006-10-19 16:20:25 +0900927void __init trap_init(void)
928{
929 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
930 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Takashi YOSHII4b565682006-09-27 17:15:32 +0900932#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
933 defined(CONFIG_SH_FPU_EMU)
934 /*
935 * For SH-4 lacking an FPU, treat floating point instructions as
936 * reserved. They'll be handled in the math-emu case, or faulted on
937 * otherwise.
938 */
Paul Mundt1f666582006-10-19 16:20:25 +0900939 set_exception_table_evt(0x800, do_reserved_inst);
940 set_exception_table_evt(0x820, do_illegal_slot_inst);
941#elif defined(CONFIG_SH_FPU)
Paul Mundte0a36472007-08-01 16:55:07 +0900942#ifdef CONFIG_CPU_SUBTYPE_SHX3
Paul Mundt74d99a52007-11-26 20:38:36 +0900943 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
944 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
Paul Mundte0a36472007-08-01 16:55:07 +0900945#else
Paul Mundt74d99a52007-11-26 20:38:36 +0900946 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
947 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948#endif
Paul Mundte0a36472007-08-01 16:55:07 +0900949#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900950
951#ifdef CONFIG_CPU_SH2
Paul Mundt5a4f7c62007-11-20 18:08:06 +0900952 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900953#endif
954#ifdef CONFIG_CPU_SH2A
955 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
956 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +0900957#ifdef CONFIG_SH_FPU
958 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
959#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900960#endif
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900961
Peter Griffincd894362009-05-08 15:51:51 +0100962#ifdef TRAP_UBC
963 set_exception_table_vec(TRAP_UBC, break_point_trap);
964#endif
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* Setup VBR for boot cpu */
967 per_cpu_trap_init();
968}
969
970void show_stack(struct task_struct *tsk, unsigned long *sp)
971{
Paul Mundt6b002232006-10-12 17:07:45 +0900972 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Paul Mundta6a311392006-09-27 18:22:14 +0900974 if (!tsk)
975 tsk = current;
976 if (tsk == current)
977 sp = (unsigned long *)current_stack_pointer;
978 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Paul Mundt6b002232006-10-12 17:07:45 +0900981 stack = (unsigned long)sp;
982 dump_mem("Stack: ", stack, THREAD_SIZE +
983 (unsigned long)task_stack_page(tsk));
984 show_trace(tsk, sp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987void dump_stack(void)
988{
989 show_stack(NULL, NULL);
990}
991EXPORT_SYMBOL(dump_stack);
Andre Draszik7436cde2009-08-24 14:53:46 +0900992
993#ifdef CONFIG_PROC_FS
994/*
995 * This needs to be done after sysctl_init, otherwise sys/ will be
996 * overwritten. Actually, this shouldn't be in sys/ at all since
997 * it isn't a sysctl, and it doesn't contain sysctl information.
998 * We now locate it in /proc/cpu/alignment instead.
999 */
1000static int __init alignment_init(void)
1001{
1002 struct proc_dir_entry *dir, *res;
1003
1004 dir = proc_mkdir("cpu", NULL);
1005 if (!dir)
1006 return -ENOMEM;
1007
1008 res = create_proc_entry("alignment", S_IWUSR | S_IRUGO, dir);
1009 if (!res)
1010 return -ENOMEM;
1011
1012 res->read_proc = proc_alignment_read;
1013 res->write_proc = proc_alignment_write;
1014
1015 res = create_proc_entry("kernel_alignment", S_IWUSR | S_IRUGO, dir);
1016 if (!res)
1017 return -ENOMEM;
1018
1019 res->read_proc = proc_alignment_read;
1020 res->write_proc = proc_alignment_kern_write;
1021
1022 return 0;
1023}
1024
1025fs_initcall(alignment_init);
1026#endif