blob: 7a2ee3a6b8e726f61b47534fbc469f0fdbd9b766 [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>
Paul Mundtaf67c3a2009-10-13 10:57:52 +090028#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/system.h>
30#include <asm/uaccess.h>
Andrew Mortonfad0f902008-04-16 02:03:51 +090031#include <asm/fpu.h>
Chris Smithd39f5452008-09-05 17:15:39 +090032#include <asm/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#ifdef CONFIG_CPU_SH2
Yoshinori Sato0983b312006-11-05 15:58:47 +090035# define TRAP_RESERVED_INST 4
36# define TRAP_ILLEGAL_SLOT_INST 6
37# define TRAP_ADDRESS_ERROR 9
38# ifdef CONFIG_CPU_SH2A
Peter Griffincd894362009-05-08 15:51:51 +010039# define TRAP_UBC 12
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +090040# define TRAP_FPU_ERROR 13
Yoshinori Sato0983b312006-11-05 15:58:47 +090041# define TRAP_DIVZERO_ERROR 17
42# define TRAP_DIVOVF_ERROR 18
43# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
45#define TRAP_RESERVED_INST 12
46#define TRAP_ILLEGAL_SLOT_INST 13
47#endif
48
Andre Draszik7436cde2009-08-24 14:53:46 +090049static unsigned long se_user;
50static unsigned long se_sys;
Andre Draszik7436cde2009-08-24 14:53:46 +090051static unsigned long se_half;
52static unsigned long se_word;
53static unsigned long se_dword;
54static unsigned long se_multi;
55/* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not
56 valid! */
57static int se_usermode = 3;
Paul Mundt84066382009-09-30 14:02:42 +090058/* 0: no warning 1: print a warning message, disabled by default */
59static int se_kernmode_warn;
Andre Draszik7436cde2009-08-24 14:53:46 +090060
61#ifdef CONFIG_PROC_FS
62static const char *se_usermode_action[] = {
63 "ignored",
64 "warn",
65 "fixup",
66 "fixup+warn",
67 "signal",
68 "signal+warn"
69};
70
71static int
72proc_alignment_read(char *page, char **start, off_t off, int count, int *eof,
73 void *data)
74{
75 char *p = page;
76 int len;
77
78 p += sprintf(p, "User:\t\t%lu\n", se_user);
79 p += sprintf(p, "System:\t\t%lu\n", se_sys);
Andre Draszik7436cde2009-08-24 14:53:46 +090080 p += sprintf(p, "Half:\t\t%lu\n", se_half);
81 p += sprintf(p, "Word:\t\t%lu\n", se_word);
82 p += sprintf(p, "DWord:\t\t%lu\n", se_dword);
83 p += sprintf(p, "Multi:\t\t%lu\n", se_multi);
84 p += sprintf(p, "User faults:\t%i (%s)\n", se_usermode,
85 se_usermode_action[se_usermode]);
86 p += sprintf(p, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn,
87 se_kernmode_warn ? "+warn" : "");
88
89 len = (p - page) - off;
90 if (len < 0)
91 len = 0;
92
93 *eof = (len <= count) ? 1 : 0;
94 *start = page + off;
95
96 return len;
97}
98
99static int proc_alignment_write(struct file *file, const char __user *buffer,
100 unsigned long count, void *data)
101{
102 char mode;
103
104 if (count > 0) {
105 if (get_user(mode, buffer))
106 return -EFAULT;
107 if (mode >= '0' && mode <= '5')
108 se_usermode = mode - '0';
109 }
110 return count;
111}
112
113static int proc_alignment_kern_write(struct file *file, const char __user *buffer,
114 unsigned long count, void *data)
115{
116 char mode;
117
118 if (count > 0) {
119 if (get_user(mode, buffer))
120 return -EFAULT;
121 if (mode >= '0' && mode <= '1')
122 se_kernmode_warn = mode - '0';
123 }
124 return count;
125}
126#endif
127
Paul Mundt6b002232006-10-12 17:07:45 +0900128static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
129{
130 unsigned long p;
131 int i;
132
133 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
134
135 for (p = bottom & ~31; p < top; ) {
136 printk("%04lx: ", p & 0xffff);
137
138 for (i = 0; i < 8; i++, p += 4) {
139 unsigned int val;
140
141 if (p < bottom || p >= top)
142 printk(" ");
143 else {
144 if (__get_user(val, (unsigned int __user *)p)) {
145 printk("\n");
146 return;
147 }
148 printk("%08x ", val);
149 }
150 }
151 printk("\n");
152 }
153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Paul Mundt3a2e1172007-05-01 16:33:10 +0900155static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157void die(const char * str, struct pt_regs * regs, long err)
158{
159 static int die_counter;
160
Paul Mundt55273982007-06-18 18:57:13 +0900161 oops_enter();
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 spin_lock_irq(&die_lock);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900164 console_verbose();
Paul Mundt6b002232006-10-12 17:07:45 +0900165 bust_spinlocks(1);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900168 sysfs_printk_last_file();
Paul Mundt6b002232006-10-12 17:07:45 +0900169 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 show_regs(regs);
Paul Mundt6b002232006-10-12 17:07:45 +0900171
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700172 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
173 task_pid_nr(current), task_stack_page(current) + 1);
Paul Mundt6b002232006-10-12 17:07:45 +0900174
175 if (!user_mode(regs) || in_interrupt())
176 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900177 (unsigned long)task_stack_page(current));
Paul Mundt6b002232006-10-12 17:07:45 +0900178
Paul Mundtc9306f02008-10-21 18:33:36 +0900179 notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
180
Paul Mundt6b002232006-10-12 17:07:45 +0900181 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700182 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 spin_unlock_irq(&die_lock);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900184 oops_exit();
Paul Mundte1132762007-05-15 08:36:36 +0900185
186 if (kexec_should_crash(current))
187 crash_kexec(regs);
188
189 if (in_interrupt())
190 panic("Fatal exception in interrupt");
191
192 if (panic_on_oops)
193 panic("Fatal exception");
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 do_exit(SIGSEGV);
196}
197
Paul Mundt6b002232006-10-12 17:07:45 +0900198static inline void die_if_kernel(const char *str, struct pt_regs *regs,
199 long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 if (!user_mode(regs))
202 die(str, regs, err);
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * try and fix up kernelspace address errors
207 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
208 * - kernel/userspace interfaces cause a jump to an appropriate handler
209 * - other kernel errors are bad
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900211static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Paul Mundt6b002232006-10-12 17:07:45 +0900213 if (!user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 const struct exception_table_entry *fixup;
215 fixup = search_exception_tables(regs->pc);
216 if (fixup) {
217 regs->pc = fixup->fixup;
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900218 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Matt Flemingb344e24a2009-08-16 21:54:48 +0100220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 die(str, regs, err);
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Magnus Damm86c01792008-02-07 00:02:50 +0900225static inline void sign_extend(unsigned int count, unsigned char *dst)
226{
227#ifdef __LITTLE_ENDIAN__
Magnus Damm4252c652008-02-07 19:58:46 +0900228 if ((count == 1) && dst[0] & 0x80) {
229 dst[1] = 0xff;
230 dst[2] = 0xff;
231 dst[3] = 0xff;
232 }
Magnus Damm86c01792008-02-07 00:02:50 +0900233 if ((count == 2) && dst[1] & 0x80) {
234 dst[2] = 0xff;
235 dst[3] = 0xff;
236 }
237#else
Magnus Damm4252c652008-02-07 19:58:46 +0900238 if ((count == 1) && dst[3] & 0x80) {
239 dst[2] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900240 dst[1] = 0xff;
Magnus Damm4252c652008-02-07 19:58:46 +0900241 dst[0] = 0xff;
242 }
243 if ((count == 2) && dst[2] & 0x80) {
244 dst[1] = 0xff;
245 dst[0] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900246 }
247#endif
248}
249
Magnus Damme7cc9a72008-02-07 20:18:21 +0900250static struct mem_access user_mem_access = {
251 copy_from_user,
252 copy_to_user,
253};
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
256 * handle an instruction that does an unaligned memory access by emulating the
257 * desired behaviour
258 * - note that PC _may not_ point to the faulting instruction
259 * (if that instruction is in a branch delay slot)
260 * - return 0 if emulation okay, -EFAULT on existential error
261 */
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900262static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
Magnus Damme7cc9a72008-02-07 20:18:21 +0900263 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 int ret, index, count;
266 unsigned long *rm, *rn;
267 unsigned char *src, *dst;
Paul Mundtfa439722008-09-04 18:53:58 +0900268 unsigned char __user *srcu, *dstu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 index = (instruction>>8)&15; /* 0x0F00 */
271 rn = &regs->regs[index];
272
273 index = (instruction>>4)&15; /* 0x00F0 */
274 rm = &regs->regs[index];
275
276 count = 1<<(instruction&3);
277
Andre Draszik7436cde2009-08-24 14:53:46 +0900278 switch (count) {
279 case 1: se_half += 1; break;
280 case 2: se_word += 1; break;
281 case 4: se_dword += 1; break;
282 case 8: se_multi += 1; break; /* ??? */
283 }
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 ret = -EFAULT;
286 switch (instruction>>12) {
287 case 0: /* mov.[bwl] to/from memory via r0+rn */
288 if (instruction & 8) {
289 /* from memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900290 srcu = (unsigned char __user *)*rm;
291 srcu += regs->regs[0];
292 dst = (unsigned char *)rn;
293 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Magnus Damm86c01792008-02-07 00:02:50 +0900295#if !defined(__LITTLE_ENDIAN__)
296 dst += 4-count;
297#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900298 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto fetch_fault;
300
Magnus Damm86c01792008-02-07 00:02:50 +0900301 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 } else {
303 /* to memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900304 src = (unsigned char *)rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#if !defined(__LITTLE_ENDIAN__)
306 src += 4-count;
307#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900308 dstu = (unsigned char __user *)*rn;
309 dstu += regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Paul Mundtfa439722008-09-04 18:53:58 +0900311 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto fetch_fault;
313 }
314 ret = 0;
315 break;
316
317 case 1: /* mov.l Rm,@(disp,Rn) */
318 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900319 dstu = (unsigned char __user *)*rn;
320 dstu += (instruction&0x000F)<<2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Paul Mundtfa439722008-09-04 18:53:58 +0900322 if (ma->to(dstu, src, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto fetch_fault;
324 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
328 if (instruction & 4)
329 *rn -= count;
330 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900331 dstu = (unsigned char __user *)*rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#if !defined(__LITTLE_ENDIAN__)
333 src += 4-count;
334#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900335 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto fetch_fault;
337 ret = 0;
338 break;
339
340 case 5: /* mov.l @(disp,Rm),Rn */
Paul Mundtfa439722008-09-04 18:53:58 +0900341 srcu = (unsigned char __user *)*rm;
342 srcu += (instruction & 0x000F) << 2;
343 dst = (unsigned char *)rn;
344 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Paul Mundtfa439722008-09-04 18:53:58 +0900346 if (ma->from(dst, srcu, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 goto fetch_fault;
348 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 case 6: /* mov.[bwl] from memory, possibly with post-increment */
Paul Mundtfa439722008-09-04 18:53:58 +0900352 srcu = (unsigned char __user *)*rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (instruction & 4)
354 *rm += count;
355 dst = (unsigned char*) rn;
356 *(unsigned long*)dst = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900357
Magnus Damm86c01792008-02-07 00:02:50 +0900358#if !defined(__LITTLE_ENDIAN__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dst += 4-count;
Magnus Damm86c01792008-02-07 00:02:50 +0900360#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900361 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900363 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 ret = 0;
365 break;
366
367 case 8:
368 switch ((instruction&0xFF00)>>8) {
369 case 0x81: /* mov.w R0,@(disp,Rn) */
Paul Mundtfa439722008-09-04 18:53:58 +0900370 src = (unsigned char *) &regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#if !defined(__LITTLE_ENDIAN__)
372 src += 2;
373#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900374 dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
375 dstu += (instruction & 0x000F) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Paul Mundtfa439722008-09-04 18:53:58 +0900377 if (ma->to(dstu, src, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto fetch_fault;
379 ret = 0;
380 break;
381
382 case 0x85: /* mov.w @(disp,Rm),R0 */
Paul Mundtfa439722008-09-04 18:53:58 +0900383 srcu = (unsigned char __user *)*rm;
384 srcu += (instruction & 0x000F) << 1;
385 dst = (unsigned char *) &regs->regs[0];
386 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388#if !defined(__LITTLE_ENDIAN__)
389 dst += 2;
390#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900391 if (ma->from(dst, srcu, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900393 sign_extend(2, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ret = 0;
395 break;
396 }
397 break;
398 }
399 return ret;
400
401 fetch_fault:
402 /* Argh. Address not only misaligned but also non-existent.
403 * Raise an EFAULT and see if it's trapped
404 */
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900405 die_if_no_fixup("Fault in unaligned fixup", regs, 0);
406 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409/*
410 * emulate the instruction in the delay slot
411 * - fetches the instruction from PC+2
412 */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900413static inline int handle_delayslot(struct pt_regs *regs,
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900414 insn_size_t old_instruction,
Magnus Damme7cc9a72008-02-07 20:18:21 +0900415 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900417 insn_size_t instruction;
Paul Mundtfa439722008-09-04 18:53:58 +0900418 void __user *addr = (void __user *)(regs->pc +
419 instruction_size(old_instruction));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900421 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* the instruction-fetch faulted */
423 if (user_mode(regs))
424 return -EFAULT;
425
426 /* kernel */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900427 die("delay-slot-insn faulting in handle_unaligned_delayslot",
428 regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Magnus Damme7cc9a72008-02-07 20:18:21 +0900431 return handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * handle an instruction that does an unaligned memory access
436 * - have to be careful of branch delay-slot instructions that fault
437 * SH3:
438 * - if the branch would be taken PC points to the branch
439 * - if the branch would not be taken, PC points to delay-slot
440 * SH4:
441 * - PC always points to delayed branch
442 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
443 */
444
445/* Macros to determine offset from current PC for branch instructions */
446/* Explicit type coercion is used to force sign extension where needed */
447#define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
448#define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
449
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900450int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000451 struct mem_access *ma, int expected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 u_int rm;
454 int ret, index;
455
Paul Mundt23c4c822009-09-24 17:38:18 +0900456 /*
457 * XXX: We can't handle mixed 16/32-bit instructions yet
458 */
459 if (instruction_size(instruction) != 2)
460 return -EINVAL;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 index = (instruction>>8)&15; /* 0x0F00 */
463 rm = regs->regs[index];
464
Andre Draszik9a4af022009-08-24 14:38:27 +0900465 /* shout about fixups */
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000466 if (!expected && printk_ratelimit())
Andre Draszik9a4af022009-08-24 14:38:27 +0900467 printk(KERN_NOTICE "Fixing up unaligned %s access "
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900468 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
Andre Draszik9a4af022009-08-24 14:38:27 +0900469 user_mode(regs) ? "userspace" : "kernel",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700470 current->comm, task_pid_nr(current),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900471 (void *)regs->pc, instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 ret = -EFAULT;
474 switch (instruction&0xF000) {
475 case 0x0000:
476 if (instruction==0x000B) {
477 /* rts */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900478 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (ret==0)
480 regs->pc = regs->pr;
481 }
482 else if ((instruction&0x00FF)==0x0023) {
483 /* braf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900484 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (ret==0)
486 regs->pc += rm + 4;
487 }
488 else if ((instruction&0x00FF)==0x0003) {
489 /* bsrf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900490 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (ret==0) {
492 regs->pr = regs->pc + 4;
493 regs->pc += rm + 4;
494 }
495 }
496 else {
497 /* mov.[bwl] to/from memory via r0+rn */
498 goto simple;
499 }
500 break;
501
502 case 0x1000: /* mov.l Rm,@(disp,Rn) */
503 goto simple;
504
505 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
506 goto simple;
507
508 case 0x4000:
509 if ((instruction&0x00FF)==0x002B) {
510 /* jmp @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900511 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (ret==0)
513 regs->pc = rm;
514 }
515 else if ((instruction&0x00FF)==0x000B) {
516 /* jsr @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900517 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (ret==0) {
519 regs->pr = regs->pc + 4;
520 regs->pc = rm;
521 }
522 }
523 else {
524 /* mov.[bwl] to/from memory via r0+rn */
525 goto simple;
526 }
527 break;
528
529 case 0x5000: /* mov.l @(disp,Rm),Rn */
530 goto simple;
531
532 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
533 goto simple;
534
535 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
536 switch (instruction&0x0F00) {
537 case 0x0100: /* mov.w R0,@(disp,Rm) */
538 goto simple;
539 case 0x0500: /* mov.w @(disp,Rm),R0 */
540 goto simple;
541 case 0x0B00: /* bf lab - no delayslot*/
542 break;
543 case 0x0F00: /* bf/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900544 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (ret==0) {
546#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
547 if ((regs->sr & 0x00000001) != 0)
548 regs->pc += 4; /* next after slot */
549 else
550#endif
551 regs->pc += SH_PC_8BIT_OFFSET(instruction);
552 }
553 break;
554 case 0x0900: /* bt lab - no delayslot */
555 break;
556 case 0x0D00: /* bt/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900557 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (ret==0) {
559#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
560 if ((regs->sr & 0x00000001) == 0)
561 regs->pc += 4; /* next after slot */
562 else
563#endif
564 regs->pc += SH_PC_8BIT_OFFSET(instruction);
565 }
566 break;
567 }
568 break;
569
570 case 0xA000: /* bra label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900571 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (ret==0)
573 regs->pc += SH_PC_12BIT_OFFSET(instruction);
574 break;
575
576 case 0xB000: /* bsr label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900577 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (ret==0) {
579 regs->pr = regs->pc + 4;
580 regs->pc += SH_PC_12BIT_OFFSET(instruction);
581 }
582 break;
583 }
584 return ret;
585
586 /* handle non-delay-slot instruction */
587 simple:
Magnus Damme7cc9a72008-02-07 20:18:21 +0900588 ret = handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (ret==0)
Paul Mundt53f983a2007-05-08 15:31:48 +0900590 regs->pc += instruction_size(instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return ret;
592}
593
594/*
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900595 * Handle various address error exceptions:
596 * - instruction address error:
597 * misaligned PC
598 * PC >= 0x80000000 in user mode
599 * - data address error (read and write)
600 * misaligned data access
601 * access to >= 0x80000000 is user mode
602 * Unfortuntaly we can't distinguish between instruction address error
Simon Arlotte868d612007-05-14 08:15:10 +0900603 * and data address errors caused by read accesses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900605asmlinkage void do_address_error(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unsigned long writeaccess,
607 unsigned long address)
608{
Yoshinori Sato0983b312006-11-05 15:58:47 +0900609 unsigned long error_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 mm_segment_t oldfs;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900611 siginfo_t info;
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900612 insn_size_t instruction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int tmp;
614
Yoshinori Sato0983b312006-11-05 15:58:47 +0900615 /* Intentional ifdef */
616#ifdef CONFIG_CPU_HAS_SR_RB
Paul Mundt4c59e292008-09-21 12:00:23 +0900617 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900618#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 oldfs = get_fs();
621
622 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900623 int si_code = BUS_ADRERR;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Andre Draszik7436cde2009-08-24 14:53:46 +0900627 se_user += 1;
628
Andre Draszik5a0ab352009-08-24 15:01:10 +0900629 set_fs(USER_DS);
Paul Mundt23c4c822009-09-24 17:38:18 +0900630 if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
631 sizeof(instruction))) {
Andre Draszik5a0ab352009-08-24 15:01:10 +0900632 set_fs(oldfs);
633 goto uspace_segv;
634 }
635 set_fs(oldfs);
636
Andre Draszik7436cde2009-08-24 14:53:46 +0900637 /* shout about userspace fixups */
638 if (se_usermode & 1)
639 printk(KERN_NOTICE "Unaligned userspace access "
640 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
641 current->comm, current->pid, (void *)regs->pc,
642 instruction);
643
644 if (se_usermode & 2)
645 goto fixup;
646
647 if (se_usermode & 4)
648 goto uspace_segv;
649 else {
650 /* ignore */
Andre Draszik5a0ab352009-08-24 15:01:10 +0900651 regs->pc += instruction_size(instruction);
Andre Draszik7436cde2009-08-24 14:53:46 +0900652 return;
653 }
654
655fixup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* bad PC is not something we can fix */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900657 if (regs->pc & 1) {
658 si_code = BUS_ADRALN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto uspace_segv;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 set_fs(USER_DS);
Magnus Damme7cc9a72008-02-07 20:18:21 +0900663 tmp = handle_unaligned_access(instruction, regs,
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000664 &user_mem_access, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 set_fs(oldfs);
666
667 if (tmp==0)
668 return; /* sorted */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900669uspace_segv:
670 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
671 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
672 regs->pr);
673
674 info.si_signo = SIGBUS;
675 info.si_errno = 0;
676 info.si_code = si_code;
Paul Mundte08f4572007-05-14 12:52:56 +0900677 info.si_addr = (void __user *)address;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900678 force_sig_info(SIGBUS, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 } else {
Andre Draszik7436cde2009-08-24 14:53:46 +0900680 se_sys += 1;
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
Paul Mundt40258ee2009-09-24 17:48:15 +0900695 if (se_kernmode_warn)
696 printk(KERN_NOTICE "Unaligned kernel access "
697 "on behalf of \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
698 current->comm, current->pid, (void *)regs->pc,
699 instruction);
700
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000701 handle_unaligned_access(instruction, regs,
702 &user_mem_access, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 set_fs(oldfs);
704 }
705}
706
707#ifdef CONFIG_SH_DSP
708/*
709 * SH-DSP support gerg@snapgear.com.
710 */
711int is_dsp_inst(struct pt_regs *regs)
712{
Paul Mundt882c12c2007-05-14 17:26:34 +0900713 unsigned short inst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900715 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * Safe guard if DSP mode is already enabled or we're lacking
717 * the DSP altogether.
718 */
Paul Mundt11c19652006-12-25 10:19:56 +0900719 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 0;
721
722 get_user(inst, ((unsigned short *) regs->pc));
723
724 inst &= 0xf000;
725
726 /* Check for any type of DSP or support instruction */
727 if ((inst == 0xf000) || (inst == 0x4000))
728 return 1;
729
730 return 0;
731}
732#else
733#define is_dsp_inst(regs) (0)
734#endif /* CONFIG_SH_DSP */
735
Yoshinori Sato0983b312006-11-05 15:58:47 +0900736#ifdef CONFIG_CPU_SH2A
737asmlinkage void do_divide_error(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)
Yoshinori Sato0983b312006-11-05 15:58:47 +0900740{
741 siginfo_t info;
742
Yoshinori Sato0983b312006-11-05 15:58:47 +0900743 switch (r4) {
744 case TRAP_DIVZERO_ERROR:
745 info.si_code = FPE_INTDIV;
746 break;
747 case TRAP_DIVOVF_ERROR:
748 info.si_code = FPE_INTOVF;
749 break;
750 }
751
752 force_sig_info(SIGFPE, &info, current);
753}
754#endif
755
Takashi YOSHII4b565682006-09-27 17:15:32 +0900756asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
757 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900758 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900759{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900760 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900761 unsigned long error_code;
762 struct task_struct *tsk = current;
763
764#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900765 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900766 int err;
767
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900768 get_user(inst, (unsigned short*)regs->pc);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900769
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900770 err = do_fpu_inst(inst, regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900771 if (!err) {
Paul Mundt53f983a2007-05-08 15:31:48 +0900772 regs->pc += instruction_size(inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900773 return;
774 }
775 /* not a FPU inst. */
776#endif
777
778#ifdef CONFIG_SH_DSP
779 /* Check if it's a DSP instruction */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900780 if (is_dsp_inst(regs)) {
Takashi YOSHII4b565682006-09-27 17:15:32 +0900781 /* Enable DSP mode, and restart instruction. */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900782 regs->sr |= SR_DSP;
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000783 /* Save DSP mode */
784 tsk->thread.dsp_status.status |= SR_DSP;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900785 return;
786 }
787#endif
788
Paul Mundt4c59e292008-09-21 12:00:23 +0900789 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900790
Takashi YOSHII4b565682006-09-27 17:15:32 +0900791 local_irq_enable();
Takashi YOSHII4b565682006-09-27 17:15:32 +0900792 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900793 die_if_no_fixup("reserved instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900794}
795
796#ifdef CONFIG_SH_FPU_EMU
Paul Mundtedfd6da2008-11-26 13:06:04 +0900797static int emulate_branch(unsigned short inst, struct pt_regs *regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900798{
799 /*
800 * bfs: 8fxx: PC+=d*2+4;
801 * bts: 8dxx: PC+=d*2+4;
802 * bra: axxx: PC+=D*2+4;
803 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
804 * braf:0x23: PC+=Rn*2+4;
805 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
806 * jmp: 4x2b: PC=Rn;
807 * jsr: 4x0b: PC=Rn after PR=PC+4;
808 * rts: 000b: PC=PR;
809 */
Paul Mundtedfd6da2008-11-26 13:06:04 +0900810 if (((inst & 0xf000) == 0xb000) || /* bsr */
811 ((inst & 0xf0ff) == 0x0003) || /* bsrf */
812 ((inst & 0xf0ff) == 0x400b)) /* jsr */
813 regs->pr = regs->pc + 4;
814
815 if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900816 regs->pc += SH_PC_8BIT_OFFSET(inst);
817 return 0;
818 }
819
Paul Mundtedfd6da2008-11-26 13:06:04 +0900820 if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900821 regs->pc += SH_PC_12BIT_OFFSET(inst);
822 return 0;
823 }
824
Paul Mundtedfd6da2008-11-26 13:06:04 +0900825 if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900826 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
827 return 0;
828 }
829
Paul Mundtedfd6da2008-11-26 13:06:04 +0900830 if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900831 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
832 return 0;
833 }
834
Paul Mundtedfd6da2008-11-26 13:06:04 +0900835 if ((inst & 0xffff) == 0x000b) { /* rts */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900836 regs->pc = regs->pr;
837 return 0;
838 }
839
840 return 1;
841}
842#endif
843
844asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
845 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900846 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900847{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900848 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900849 unsigned long inst;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900850 struct task_struct *tsk = current;
Chris Smithd39f5452008-09-05 17:15:39 +0900851
852 if (kprobe_handle_illslot(regs->pc) == 0)
853 return;
854
Takashi YOSHII4b565682006-09-27 17:15:32 +0900855#ifdef CONFIG_SH_FPU_EMU
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900856 get_user(inst, (unsigned short *)regs->pc + 1);
857 if (!do_fpu_inst(inst, regs)) {
858 get_user(inst, (unsigned short *)regs->pc);
859 if (!emulate_branch(inst, regs))
Takashi YOSHII4b565682006-09-27 17:15:32 +0900860 return;
861 /* fault in branch.*/
862 }
863 /* not a FPU inst. */
864#endif
865
Paul Mundt4c59e292008-09-21 12:00:23 +0900866 inst = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900867
Takashi YOSHII4b565682006-09-27 17:15:32 +0900868 local_irq_enable();
Takashi YOSHII4b565682006-09-27 17:15:32 +0900869 force_sig(SIGILL, tsk);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900870 die_if_no_fixup("illegal slot instruction", regs, inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900871}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
874 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900875 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900877 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 long ex;
Yoshinori Sato0983b312006-11-05 15:58:47 +0900879
Paul Mundt4c59e292008-09-21 12:00:23 +0900880 ex = lookup_exception_vector();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900881 die_if_kernel("exception", regs, ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884#if defined(CONFIG_SH_STANDARD_BIOS)
885void *gdb_vbr_vector;
886
887static inline void __init gdb_vbr_init(void)
888{
889 register unsigned long vbr;
890
891 /*
892 * Read the old value of the VBR register to initialise
893 * the vector through which debug and BIOS traps are
894 * delegated by the Linux trap handler.
895 */
896 asm volatile("stc vbr, %0" : "=r" (vbr));
897
898 gdb_vbr_vector = (void *)(vbr + 0x100);
899 printk("Setting GDB trap vector to 0x%08lx\n",
900 (unsigned long)gdb_vbr_vector);
901}
902#endif
903
Paul Mundtaba10302007-09-21 18:32:32 +0900904void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 extern void *vbr_base;
907
908#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtaba10302007-09-21 18:32:32 +0900909 if (raw_smp_processor_id() == 0)
910 gdb_vbr_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911#endif
912
913 /* NOTE: The VBR value should be at P1
914 (or P2, virtural "fixed" address space).
915 It's definitely should not in physical address. */
916
917 asm volatile("ldc %0, vbr"
918 : /* no output */
919 : "r" (&vbr_base)
920 : "memory");
921}
922
Paul Mundt1f666582006-10-19 16:20:25 +0900923void *set_exception_table_vec(unsigned int vec, void *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 extern void *exception_handling_table[];
Paul Mundt1f666582006-10-19 16:20:25 +0900926 void *old_handler;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900927
Paul Mundt1f666582006-10-19 16:20:25 +0900928 old_handler = exception_handling_table[vec];
929 exception_handling_table[vec] = handler;
930 return old_handler;
931}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Paul Mundt1f666582006-10-19 16:20:25 +0900933void __init trap_init(void)
934{
935 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
936 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Takashi YOSHII4b565682006-09-27 17:15:32 +0900938#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
939 defined(CONFIG_SH_FPU_EMU)
940 /*
941 * For SH-4 lacking an FPU, treat floating point instructions as
942 * reserved. They'll be handled in the math-emu case, or faulted on
943 * otherwise.
944 */
Paul Mundt1f666582006-10-19 16:20:25 +0900945 set_exception_table_evt(0x800, do_reserved_inst);
946 set_exception_table_evt(0x820, do_illegal_slot_inst);
947#elif defined(CONFIG_SH_FPU)
Paul Mundte0a36472007-08-01 16:55:07 +0900948#ifdef CONFIG_CPU_SUBTYPE_SHX3
Paul Mundt74d99a52007-11-26 20:38:36 +0900949 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
950 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
Paul Mundte0a36472007-08-01 16:55:07 +0900951#else
Paul Mundt74d99a52007-11-26 20:38:36 +0900952 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
953 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954#endif
Paul Mundte0a36472007-08-01 16:55:07 +0900955#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900956
957#ifdef CONFIG_CPU_SH2
Paul Mundt5a4f7c62007-11-20 18:08:06 +0900958 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900959#endif
960#ifdef CONFIG_CPU_SH2A
961 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
962 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +0900963#ifdef CONFIG_SH_FPU
964 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
965#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900966#endif
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900967
Peter Griffincd894362009-05-08 15:51:51 +0100968#ifdef TRAP_UBC
969 set_exception_table_vec(TRAP_UBC, break_point_trap);
970#endif
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Setup VBR for boot cpu */
973 per_cpu_trap_init();
974}
975
976void show_stack(struct task_struct *tsk, unsigned long *sp)
977{
Paul Mundt6b002232006-10-12 17:07:45 +0900978 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Paul Mundta6a311392006-09-27 18:22:14 +0900980 if (!tsk)
981 tsk = current;
982 if (tsk == current)
983 sp = (unsigned long *)current_stack_pointer;
984 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Paul Mundt6b002232006-10-12 17:07:45 +0900987 stack = (unsigned long)sp;
988 dump_mem("Stack: ", stack, THREAD_SIZE +
989 (unsigned long)task_stack_page(tsk));
990 show_trace(tsk, sp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993void dump_stack(void)
994{
995 show_stack(NULL, NULL);
996}
997EXPORT_SYMBOL(dump_stack);
Andre Draszik7436cde2009-08-24 14:53:46 +0900998
999#ifdef CONFIG_PROC_FS
1000/*
1001 * This needs to be done after sysctl_init, otherwise sys/ will be
1002 * overwritten. Actually, this shouldn't be in sys/ at all since
1003 * it isn't a sysctl, and it doesn't contain sysctl information.
1004 * We now locate it in /proc/cpu/alignment instead.
1005 */
1006static int __init alignment_init(void)
1007{
1008 struct proc_dir_entry *dir, *res;
1009
1010 dir = proc_mkdir("cpu", NULL);
1011 if (!dir)
1012 return -ENOMEM;
1013
1014 res = create_proc_entry("alignment", S_IWUSR | S_IRUGO, dir);
1015 if (!res)
1016 return -ENOMEM;
1017
1018 res->read_proc = proc_alignment_read;
1019 res->write_proc = proc_alignment_write;
1020
1021 res = create_proc_entry("kernel_alignment", S_IWUSR | S_IRUGO, dir);
1022 if (!res)
1023 return -ENOMEM;
1024
1025 res->read_proc = proc_alignment_read;
1026 res->write_proc = proc_alignment_kern_write;
1027
1028 return 0;
1029}
1030
1031fs_initcall(alignment_init);
1032#endif