blob: 706c1c18a2f2bb125a50c89ae80c8e10d7182ffb [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>
Alexey Dobriyan9a160702009-11-27 06:42:16 +000028#include <linux/seq_file.h>
Paul Mundtaf67c3a2009-10-13 10:57:52 +090029#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/system.h>
31#include <asm/uaccess.h>
Andrew Mortonfad0f902008-04-16 02:03:51 +090032#include <asm/fpu.h>
Chris Smithd39f5452008-09-05 17:15:39 +090033#include <asm/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#ifdef CONFIG_CPU_SH2
Yoshinori Sato0983b312006-11-05 15:58:47 +090036# define TRAP_RESERVED_INST 4
37# define TRAP_ILLEGAL_SLOT_INST 6
38# define TRAP_ADDRESS_ERROR 9
39# ifdef CONFIG_CPU_SH2A
Peter Griffincd894362009-05-08 15:51:51 +010040# define TRAP_UBC 12
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +090041# define TRAP_FPU_ERROR 13
Yoshinori Sato0983b312006-11-05 15:58:47 +090042# define TRAP_DIVZERO_ERROR 17
43# define TRAP_DIVOVF_ERROR 18
44# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#else
46#define TRAP_RESERVED_INST 12
47#define TRAP_ILLEGAL_SLOT_INST 13
48#endif
49
Andre Draszik7436cde2009-08-24 14:53:46 +090050static unsigned long se_user;
51static unsigned long se_sys;
Andre Draszik7436cde2009-08-24 14:53:46 +090052static unsigned long se_half;
53static unsigned long se_word;
54static unsigned long se_dword;
55static unsigned long se_multi;
56/* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not
57 valid! */
58static int se_usermode = 3;
Paul Mundt84066382009-09-30 14:02:42 +090059/* 0: no warning 1: print a warning message, disabled by default */
60static int se_kernmode_warn;
Andre Draszik7436cde2009-08-24 14:53:46 +090061
62#ifdef CONFIG_PROC_FS
63static const char *se_usermode_action[] = {
64 "ignored",
65 "warn",
66 "fixup",
67 "fixup+warn",
68 "signal",
69 "signal+warn"
70};
71
Alexey Dobriyan9a160702009-11-27 06:42:16 +000072static int alignment_proc_show(struct seq_file *m, void *v)
Andre Draszik7436cde2009-08-24 14:53:46 +090073{
Alexey Dobriyan9a160702009-11-27 06:42:16 +000074 seq_printf(m, "User:\t\t%lu\n", se_user);
75 seq_printf(m, "System:\t\t%lu\n", se_sys);
76 seq_printf(m, "Half:\t\t%lu\n", se_half);
77 seq_printf(m, "Word:\t\t%lu\n", se_word);
78 seq_printf(m, "DWord:\t\t%lu\n", se_dword);
79 seq_printf(m, "Multi:\t\t%lu\n", se_multi);
80 seq_printf(m, "User faults:\t%i (%s)\n", se_usermode,
Andre Draszik7436cde2009-08-24 14:53:46 +090081 se_usermode_action[se_usermode]);
Alexey Dobriyan9a160702009-11-27 06:42:16 +000082 seq_printf(m, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn,
Andre Draszik7436cde2009-08-24 14:53:46 +090083 se_kernmode_warn ? "+warn" : "");
Alexey Dobriyan9a160702009-11-27 06:42:16 +000084 return 0;
Andre Draszik7436cde2009-08-24 14:53:46 +090085}
86
Alexey Dobriyan9a160702009-11-27 06:42:16 +000087static int alignment_proc_open(struct inode *inode, struct file *file)
Andre Draszik7436cde2009-08-24 14:53:46 +090088{
Alexey Dobriyan9a160702009-11-27 06:42:16 +000089 return single_open(file, alignment_proc_show, NULL);
90}
91
92static ssize_t alignment_proc_write(struct file *file,
93 const char __user *buffer, size_t count, loff_t *pos)
94{
95 int *data = PDE(file->f_path.dentry->d_inode)->data;
Andre Draszik7436cde2009-08-24 14:53:46 +090096 char mode;
97
98 if (count > 0) {
99 if (get_user(mode, buffer))
100 return -EFAULT;
101 if (mode >= '0' && mode <= '5')
Alexey Dobriyan9a160702009-11-27 06:42:16 +0000102 *data = mode - '0';
Andre Draszik7436cde2009-08-24 14:53:46 +0900103 }
104 return count;
105}
106
Alexey Dobriyan9a160702009-11-27 06:42:16 +0000107static const struct file_operations alignment_proc_fops = {
108 .owner = THIS_MODULE,
109 .open = alignment_proc_open,
110 .read = seq_read,
111 .llseek = seq_lseek,
112 .release = single_release,
113 .write = alignment_proc_write,
114};
Andre Draszik7436cde2009-08-24 14:53:46 +0900115#endif
116
Paul Mundt6b002232006-10-12 17:07:45 +0900117static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
118{
119 unsigned long p;
120 int i;
121
122 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
123
124 for (p = bottom & ~31; p < top; ) {
125 printk("%04lx: ", p & 0xffff);
126
127 for (i = 0; i < 8; i++, p += 4) {
128 unsigned int val;
129
130 if (p < bottom || p >= top)
131 printk(" ");
132 else {
133 if (__get_user(val, (unsigned int __user *)p)) {
134 printk("\n");
135 return;
136 }
137 printk("%08x ", val);
138 }
139 }
140 printk("\n");
141 }
142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Paul Mundt3a2e1172007-05-01 16:33:10 +0900144static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146void die(const char * str, struct pt_regs * regs, long err)
147{
148 static int die_counter;
149
Paul Mundt55273982007-06-18 18:57:13 +0900150 oops_enter();
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 spin_lock_irq(&die_lock);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900153 console_verbose();
Paul Mundt6b002232006-10-12 17:07:45 +0900154 bust_spinlocks(1);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900157 sysfs_printk_last_file();
Paul Mundt6b002232006-10-12 17:07:45 +0900158 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 show_regs(regs);
Paul Mundt6b002232006-10-12 17:07:45 +0900160
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700161 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
162 task_pid_nr(current), task_stack_page(current) + 1);
Paul Mundt6b002232006-10-12 17:07:45 +0900163
164 if (!user_mode(regs) || in_interrupt())
165 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900166 (unsigned long)task_stack_page(current));
Paul Mundt6b002232006-10-12 17:07:45 +0900167
Paul Mundtc9306f02008-10-21 18:33:36 +0900168 notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
169
Paul Mundt6b002232006-10-12 17:07:45 +0900170 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700171 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spin_unlock_irq(&die_lock);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900173 oops_exit();
Paul Mundte1132762007-05-15 08:36:36 +0900174
175 if (kexec_should_crash(current))
176 crash_kexec(regs);
177
178 if (in_interrupt())
179 panic("Fatal exception in interrupt");
180
181 if (panic_on_oops)
182 panic("Fatal exception");
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 do_exit(SIGSEGV);
185}
186
Paul Mundt6b002232006-10-12 17:07:45 +0900187static inline void die_if_kernel(const char *str, struct pt_regs *regs,
188 long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 if (!user_mode(regs))
191 die(str, regs, err);
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * try and fix up kernelspace address errors
196 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
197 * - kernel/userspace interfaces cause a jump to an appropriate handler
198 * - other kernel errors are bad
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900200static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Paul Mundt6b002232006-10-12 17:07:45 +0900202 if (!user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 const struct exception_table_entry *fixup;
204 fixup = search_exception_tables(regs->pc);
205 if (fixup) {
206 regs->pc = fixup->fixup;
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Matt Flemingb344e24a2009-08-16 21:54:48 +0100209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 die(str, regs, err);
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Magnus Damm86c01792008-02-07 00:02:50 +0900214static inline void sign_extend(unsigned int count, unsigned char *dst)
215{
216#ifdef __LITTLE_ENDIAN__
Magnus Damm4252c652008-02-07 19:58:46 +0900217 if ((count == 1) && dst[0] & 0x80) {
218 dst[1] = 0xff;
219 dst[2] = 0xff;
220 dst[3] = 0xff;
221 }
Magnus Damm86c01792008-02-07 00:02:50 +0900222 if ((count == 2) && dst[1] & 0x80) {
223 dst[2] = 0xff;
224 dst[3] = 0xff;
225 }
226#else
Magnus Damm4252c652008-02-07 19:58:46 +0900227 if ((count == 1) && dst[3] & 0x80) {
228 dst[2] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900229 dst[1] = 0xff;
Magnus Damm4252c652008-02-07 19:58:46 +0900230 dst[0] = 0xff;
231 }
232 if ((count == 2) && dst[2] & 0x80) {
233 dst[1] = 0xff;
234 dst[0] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900235 }
236#endif
237}
238
Magnus Damme7cc9a72008-02-07 20:18:21 +0900239static struct mem_access user_mem_access = {
240 copy_from_user,
241 copy_to_user,
242};
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * handle an instruction that does an unaligned memory access by emulating the
246 * desired behaviour
247 * - note that PC _may not_ point to the faulting instruction
248 * (if that instruction is in a branch delay slot)
249 * - return 0 if emulation okay, -EFAULT on existential error
250 */
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900251static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
Magnus Damme7cc9a72008-02-07 20:18:21 +0900252 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 int ret, index, count;
255 unsigned long *rm, *rn;
256 unsigned char *src, *dst;
Paul Mundtfa439722008-09-04 18:53:58 +0900257 unsigned char __user *srcu, *dstu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 index = (instruction>>8)&15; /* 0x0F00 */
260 rn = &regs->regs[index];
261
262 index = (instruction>>4)&15; /* 0x00F0 */
263 rm = &regs->regs[index];
264
265 count = 1<<(instruction&3);
266
Andre Draszik7436cde2009-08-24 14:53:46 +0900267 switch (count) {
268 case 1: se_half += 1; break;
269 case 2: se_word += 1; break;
270 case 4: se_dword += 1; break;
271 case 8: se_multi += 1; break; /* ??? */
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ret = -EFAULT;
275 switch (instruction>>12) {
276 case 0: /* mov.[bwl] to/from memory via r0+rn */
277 if (instruction & 8) {
278 /* from memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900279 srcu = (unsigned char __user *)*rm;
280 srcu += regs->regs[0];
281 dst = (unsigned char *)rn;
282 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Magnus Damm86c01792008-02-07 00:02:50 +0900284#if !defined(__LITTLE_ENDIAN__)
285 dst += 4-count;
286#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900287 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 goto fetch_fault;
289
Magnus Damm86c01792008-02-07 00:02:50 +0900290 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 } else {
292 /* to memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900293 src = (unsigned char *)rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#if !defined(__LITTLE_ENDIAN__)
295 src += 4-count;
296#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900297 dstu = (unsigned char __user *)*rn;
298 dstu += regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Paul Mundtfa439722008-09-04 18:53:58 +0900300 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto fetch_fault;
302 }
303 ret = 0;
304 break;
305
306 case 1: /* mov.l Rm,@(disp,Rn) */
307 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900308 dstu = (unsigned char __user *)*rn;
309 dstu += (instruction&0x000F)<<2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Paul Mundtfa439722008-09-04 18:53:58 +0900311 if (ma->to(dstu, src, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto fetch_fault;
313 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
317 if (instruction & 4)
318 *rn -= count;
319 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900320 dstu = (unsigned char __user *)*rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321#if !defined(__LITTLE_ENDIAN__)
322 src += 4-count;
323#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900324 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto fetch_fault;
326 ret = 0;
327 break;
328
329 case 5: /* mov.l @(disp,Rm),Rn */
Paul Mundtfa439722008-09-04 18:53:58 +0900330 srcu = (unsigned char __user *)*rm;
331 srcu += (instruction & 0x000F) << 2;
332 dst = (unsigned char *)rn;
333 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Paul Mundtfa439722008-09-04 18:53:58 +0900335 if (ma->from(dst, srcu, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto fetch_fault;
337 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 case 6: /* mov.[bwl] from memory, possibly with post-increment */
Paul Mundtfa439722008-09-04 18:53:58 +0900341 srcu = (unsigned char __user *)*rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (instruction & 4)
343 *rm += count;
344 dst = (unsigned char*) rn;
345 *(unsigned long*)dst = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900346
Magnus Damm86c01792008-02-07 00:02:50 +0900347#if !defined(__LITTLE_ENDIAN__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 dst += 4-count;
Magnus Damm86c01792008-02-07 00:02:50 +0900349#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900350 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900352 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ret = 0;
354 break;
355
356 case 8:
357 switch ((instruction&0xFF00)>>8) {
358 case 0x81: /* mov.w R0,@(disp,Rn) */
Paul Mundtfa439722008-09-04 18:53:58 +0900359 src = (unsigned char *) &regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#if !defined(__LITTLE_ENDIAN__)
361 src += 2;
362#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900363 dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
364 dstu += (instruction & 0x000F) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Paul Mundtfa439722008-09-04 18:53:58 +0900366 if (ma->to(dstu, src, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto fetch_fault;
368 ret = 0;
369 break;
370
371 case 0x85: /* mov.w @(disp,Rm),R0 */
Paul Mundtfa439722008-09-04 18:53:58 +0900372 srcu = (unsigned char __user *)*rm;
373 srcu += (instruction & 0x000F) << 1;
374 dst = (unsigned char *) &regs->regs[0];
375 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377#if !defined(__LITTLE_ENDIAN__)
378 dst += 2;
379#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900380 if (ma->from(dst, srcu, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900382 sign_extend(2, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 ret = 0;
384 break;
385 }
386 break;
387 }
388 return ret;
389
390 fetch_fault:
391 /* Argh. Address not only misaligned but also non-existent.
392 * Raise an EFAULT and see if it's trapped
393 */
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900394 die_if_no_fixup("Fault in unaligned fixup", regs, 0);
395 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/*
399 * emulate the instruction in the delay slot
400 * - fetches the instruction from PC+2
401 */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900402static inline int handle_delayslot(struct pt_regs *regs,
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900403 insn_size_t old_instruction,
Magnus Damme7cc9a72008-02-07 20:18:21 +0900404 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900406 insn_size_t instruction;
Paul Mundtfa439722008-09-04 18:53:58 +0900407 void __user *addr = (void __user *)(regs->pc +
408 instruction_size(old_instruction));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900410 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* the instruction-fetch faulted */
412 if (user_mode(regs))
413 return -EFAULT;
414
415 /* kernel */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900416 die("delay-slot-insn faulting in handle_unaligned_delayslot",
417 regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
Magnus Damme7cc9a72008-02-07 20:18:21 +0900420 return handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/*
424 * handle an instruction that does an unaligned memory access
425 * - have to be careful of branch delay-slot instructions that fault
426 * SH3:
427 * - if the branch would be taken PC points to the branch
428 * - if the branch would not be taken, PC points to delay-slot
429 * SH4:
430 * - PC always points to delayed branch
431 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
432 */
433
434/* Macros to determine offset from current PC for branch instructions */
435/* Explicit type coercion is used to force sign extension where needed */
436#define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
437#define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
438
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900439int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000440 struct mem_access *ma, int expected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 u_int rm;
443 int ret, index;
444
Paul Mundt23c4c822009-09-24 17:38:18 +0900445 /*
446 * XXX: We can't handle mixed 16/32-bit instructions yet
447 */
448 if (instruction_size(instruction) != 2)
449 return -EINVAL;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 index = (instruction>>8)&15; /* 0x0F00 */
452 rm = regs->regs[index];
453
Andre Draszik9a4af022009-08-24 14:38:27 +0900454 /* shout about fixups */
Paul Mundt1232d882009-12-14 11:46:09 +0900455 if (!expected) {
456 if (user_mode(regs) && (se_usermode & 1) && printk_ratelimit())
457 pr_notice("Fixing up unaligned userspace access "
458 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
459 current->comm, task_pid_nr(current),
460 (void *)regs->pc, instruction);
461 else if (se_kernmode_warn && printk_ratelimit())
462 pr_notice("Fixing up unaligned kernel access "
463 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
464 current->comm, task_pid_nr(current),
465 (void *)regs->pc, instruction);
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 ret = -EFAULT;
469 switch (instruction&0xF000) {
470 case 0x0000:
471 if (instruction==0x000B) {
472 /* rts */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900473 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (ret==0)
475 regs->pc = regs->pr;
476 }
477 else if ((instruction&0x00FF)==0x0023) {
478 /* braf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900479 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (ret==0)
481 regs->pc += rm + 4;
482 }
483 else if ((instruction&0x00FF)==0x0003) {
484 /* bsrf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900485 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (ret==0) {
487 regs->pr = regs->pc + 4;
488 regs->pc += rm + 4;
489 }
490 }
491 else {
492 /* mov.[bwl] to/from memory via r0+rn */
493 goto simple;
494 }
495 break;
496
497 case 0x1000: /* mov.l Rm,@(disp,Rn) */
498 goto simple;
499
500 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
501 goto simple;
502
503 case 0x4000:
504 if ((instruction&0x00FF)==0x002B) {
505 /* jmp @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900506 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (ret==0)
508 regs->pc = rm;
509 }
510 else if ((instruction&0x00FF)==0x000B) {
511 /* jsr @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900512 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (ret==0) {
514 regs->pr = regs->pc + 4;
515 regs->pc = rm;
516 }
517 }
518 else {
519 /* mov.[bwl] to/from memory via r0+rn */
520 goto simple;
521 }
522 break;
523
524 case 0x5000: /* mov.l @(disp,Rm),Rn */
525 goto simple;
526
527 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
528 goto simple;
529
530 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
531 switch (instruction&0x0F00) {
532 case 0x0100: /* mov.w R0,@(disp,Rm) */
533 goto simple;
534 case 0x0500: /* mov.w @(disp,Rm),R0 */
535 goto simple;
536 case 0x0B00: /* bf lab - no delayslot*/
537 break;
538 case 0x0F00: /* bf/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900539 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (ret==0) {
541#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
542 if ((regs->sr & 0x00000001) != 0)
543 regs->pc += 4; /* next after slot */
544 else
545#endif
546 regs->pc += SH_PC_8BIT_OFFSET(instruction);
547 }
548 break;
549 case 0x0900: /* bt lab - no delayslot */
550 break;
551 case 0x0D00: /* bt/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900552 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (ret==0) {
554#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
555 if ((regs->sr & 0x00000001) == 0)
556 regs->pc += 4; /* next after slot */
557 else
558#endif
559 regs->pc += SH_PC_8BIT_OFFSET(instruction);
560 }
561 break;
562 }
563 break;
564
565 case 0xA000: /* bra label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900566 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (ret==0)
568 regs->pc += SH_PC_12BIT_OFFSET(instruction);
569 break;
570
571 case 0xB000: /* bsr label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900572 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (ret==0) {
574 regs->pr = regs->pc + 4;
575 regs->pc += SH_PC_12BIT_OFFSET(instruction);
576 }
577 break;
578 }
579 return ret;
580
581 /* handle non-delay-slot instruction */
582 simple:
Magnus Damme7cc9a72008-02-07 20:18:21 +0900583 ret = handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (ret==0)
Paul Mundt53f983a2007-05-08 15:31:48 +0900585 regs->pc += instruction_size(instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return ret;
587}
588
589/*
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900590 * Handle various address error exceptions:
591 * - instruction address error:
592 * misaligned PC
593 * PC >= 0x80000000 in user mode
594 * - data address error (read and write)
595 * misaligned data access
596 * access to >= 0x80000000 is user mode
597 * Unfortuntaly we can't distinguish between instruction address error
Simon Arlotte868d612007-05-14 08:15:10 +0900598 * and data address errors caused by read accesses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900600asmlinkage void do_address_error(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 unsigned long writeaccess,
602 unsigned long address)
603{
Yoshinori Sato0983b312006-11-05 15:58:47 +0900604 unsigned long error_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 mm_segment_t oldfs;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900606 siginfo_t info;
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900607 insn_size_t instruction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int tmp;
609
Yoshinori Sato0983b312006-11-05 15:58:47 +0900610 /* Intentional ifdef */
611#ifdef CONFIG_CPU_HAS_SR_RB
Paul Mundt4c59e292008-09-21 12:00:23 +0900612 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900613#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 oldfs = get_fs();
616
617 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900618 int si_code = BUS_ADRERR;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Andre Draszik7436cde2009-08-24 14:53:46 +0900622 se_user += 1;
623
Andre Draszik5a0ab352009-08-24 15:01:10 +0900624 set_fs(USER_DS);
Paul Mundt23c4c822009-09-24 17:38:18 +0900625 if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
626 sizeof(instruction))) {
Andre Draszik5a0ab352009-08-24 15:01:10 +0900627 set_fs(oldfs);
628 goto uspace_segv;
629 }
630 set_fs(oldfs);
631
Andre Draszik7436cde2009-08-24 14:53:46 +0900632 /* shout about userspace fixups */
633 if (se_usermode & 1)
634 printk(KERN_NOTICE "Unaligned userspace access "
635 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
636 current->comm, current->pid, (void *)regs->pc,
637 instruction);
638
639 if (se_usermode & 2)
640 goto fixup;
641
642 if (se_usermode & 4)
643 goto uspace_segv;
644 else {
645 /* ignore */
Andre Draszik5a0ab352009-08-24 15:01:10 +0900646 regs->pc += instruction_size(instruction);
Andre Draszik7436cde2009-08-24 14:53:46 +0900647 return;
648 }
649
650fixup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* bad PC is not something we can fix */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900652 if (regs->pc & 1) {
653 si_code = BUS_ADRALN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto uspace_segv;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 set_fs(USER_DS);
Magnus Damme7cc9a72008-02-07 20:18:21 +0900658 tmp = handle_unaligned_access(instruction, regs,
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000659 &user_mem_access, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 set_fs(oldfs);
661
662 if (tmp==0)
663 return; /* sorted */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900664uspace_segv:
665 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
666 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
667 regs->pr);
668
669 info.si_signo = SIGBUS;
670 info.si_errno = 0;
671 info.si_code = si_code;
Paul Mundte08f4572007-05-14 12:52:56 +0900672 info.si_addr = (void __user *)address;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900673 force_sig_info(SIGBUS, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 } else {
Andre Draszik7436cde2009-08-24 14:53:46 +0900675 se_sys += 1;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (regs->pc & 1)
678 die("unaligned program counter", regs, error_code);
679
680 set_fs(KERNEL_DS);
Paul Mundtfa439722008-09-04 18:53:58 +0900681 if (copy_from_user(&instruction, (void __user *)(regs->pc),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900682 sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* Argh. Fault on the instruction itself.
684 This should never happen non-SMP
685 */
686 set_fs(oldfs);
687 die("insn faulting in do_address_error", regs, 0);
688 }
689
Paul Mundt40258ee2009-09-24 17:48:15 +0900690 if (se_kernmode_warn)
691 printk(KERN_NOTICE "Unaligned kernel access "
692 "on behalf of \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
693 current->comm, current->pid, (void *)regs->pc,
694 instruction);
695
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000696 handle_unaligned_access(instruction, regs,
697 &user_mem_access, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 set_fs(oldfs);
699 }
700}
701
702#ifdef CONFIG_SH_DSP
703/*
704 * SH-DSP support gerg@snapgear.com.
705 */
706int is_dsp_inst(struct pt_regs *regs)
707{
Paul Mundt882c12c2007-05-14 17:26:34 +0900708 unsigned short inst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900710 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 * Safe guard if DSP mode is already enabled or we're lacking
712 * the DSP altogether.
713 */
Paul Mundt11c19652006-12-25 10:19:56 +0900714 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return 0;
716
717 get_user(inst, ((unsigned short *) regs->pc));
718
719 inst &= 0xf000;
720
721 /* Check for any type of DSP or support instruction */
722 if ((inst == 0xf000) || (inst == 0x4000))
723 return 1;
724
725 return 0;
726}
727#else
728#define is_dsp_inst(regs) (0)
729#endif /* CONFIG_SH_DSP */
730
Yoshinori Sato0983b312006-11-05 15:58:47 +0900731#ifdef CONFIG_CPU_SH2A
732asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
733 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900734 struct pt_regs __regs)
Yoshinori Sato0983b312006-11-05 15:58:47 +0900735{
736 siginfo_t info;
737
Yoshinori Sato0983b312006-11-05 15:58:47 +0900738 switch (r4) {
739 case TRAP_DIVZERO_ERROR:
740 info.si_code = FPE_INTDIV;
741 break;
742 case TRAP_DIVOVF_ERROR:
743 info.si_code = FPE_INTOVF;
744 break;
745 }
746
747 force_sig_info(SIGFPE, &info, current);
748}
749#endif
750
Takashi YOSHII4b565682006-09-27 17:15:32 +0900751asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
752 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900753 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900754{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900755 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900756 unsigned long error_code;
757 struct task_struct *tsk = current;
758
759#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900760 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900761 int err;
762
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900763 get_user(inst, (unsigned short*)regs->pc);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900764
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900765 err = do_fpu_inst(inst, regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900766 if (!err) {
Paul Mundt53f983a2007-05-08 15:31:48 +0900767 regs->pc += instruction_size(inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900768 return;
769 }
770 /* not a FPU inst. */
771#endif
772
773#ifdef CONFIG_SH_DSP
774 /* Check if it's a DSP instruction */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900775 if (is_dsp_inst(regs)) {
Takashi YOSHII4b565682006-09-27 17:15:32 +0900776 /* Enable DSP mode, and restart instruction. */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900777 regs->sr |= SR_DSP;
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000778 /* Save DSP mode */
779 tsk->thread.dsp_status.status |= SR_DSP;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900780 return;
781 }
782#endif
783
Paul Mundt4c59e292008-09-21 12:00:23 +0900784 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900785
Takashi YOSHII4b565682006-09-27 17:15:32 +0900786 local_irq_enable();
Takashi YOSHII4b565682006-09-27 17:15:32 +0900787 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900788 die_if_no_fixup("reserved instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900789}
790
791#ifdef CONFIG_SH_FPU_EMU
Paul Mundtedfd6da2008-11-26 13:06:04 +0900792static int emulate_branch(unsigned short inst, struct pt_regs *regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900793{
794 /*
795 * bfs: 8fxx: PC+=d*2+4;
796 * bts: 8dxx: PC+=d*2+4;
797 * bra: axxx: PC+=D*2+4;
798 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
799 * braf:0x23: PC+=Rn*2+4;
800 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
801 * jmp: 4x2b: PC=Rn;
802 * jsr: 4x0b: PC=Rn after PR=PC+4;
803 * rts: 000b: PC=PR;
804 */
Paul Mundtedfd6da2008-11-26 13:06:04 +0900805 if (((inst & 0xf000) == 0xb000) || /* bsr */
806 ((inst & 0xf0ff) == 0x0003) || /* bsrf */
807 ((inst & 0xf0ff) == 0x400b)) /* jsr */
808 regs->pr = regs->pc + 4;
809
810 if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900811 regs->pc += SH_PC_8BIT_OFFSET(inst);
812 return 0;
813 }
814
Paul Mundtedfd6da2008-11-26 13:06:04 +0900815 if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900816 regs->pc += SH_PC_12BIT_OFFSET(inst);
817 return 0;
818 }
819
Paul Mundtedfd6da2008-11-26 13:06:04 +0900820 if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900821 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
822 return 0;
823 }
824
Paul Mundtedfd6da2008-11-26 13:06:04 +0900825 if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900826 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
827 return 0;
828 }
829
Paul Mundtedfd6da2008-11-26 13:06:04 +0900830 if ((inst & 0xffff) == 0x000b) { /* rts */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900831 regs->pc = regs->pr;
832 return 0;
833 }
834
835 return 1;
836}
837#endif
838
839asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
840 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900841 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900842{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900843 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900844 unsigned long inst;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900845 struct task_struct *tsk = current;
Chris Smithd39f5452008-09-05 17:15:39 +0900846
847 if (kprobe_handle_illslot(regs->pc) == 0)
848 return;
849
Takashi YOSHII4b565682006-09-27 17:15:32 +0900850#ifdef CONFIG_SH_FPU_EMU
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900851 get_user(inst, (unsigned short *)regs->pc + 1);
852 if (!do_fpu_inst(inst, regs)) {
853 get_user(inst, (unsigned short *)regs->pc);
854 if (!emulate_branch(inst, regs))
Takashi YOSHII4b565682006-09-27 17:15:32 +0900855 return;
856 /* fault in branch.*/
857 }
858 /* not a FPU inst. */
859#endif
860
Paul Mundt4c59e292008-09-21 12:00:23 +0900861 inst = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900862
Takashi YOSHII4b565682006-09-27 17:15:32 +0900863 local_irq_enable();
Takashi YOSHII4b565682006-09-27 17:15:32 +0900864 force_sig(SIGILL, tsk);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900865 die_if_no_fixup("illegal slot instruction", regs, inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900866}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
869 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900870 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900872 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 long ex;
Yoshinori Sato0983b312006-11-05 15:58:47 +0900874
Paul Mundt4c59e292008-09-21 12:00:23 +0900875 ex = lookup_exception_vector();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900876 die_if_kernel("exception", regs, ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879#if defined(CONFIG_SH_STANDARD_BIOS)
880void *gdb_vbr_vector;
881
882static inline void __init gdb_vbr_init(void)
883{
884 register unsigned long vbr;
885
886 /*
887 * Read the old value of the VBR register to initialise
888 * the vector through which debug and BIOS traps are
889 * delegated by the Linux trap handler.
890 */
891 asm volatile("stc vbr, %0" : "=r" (vbr));
892
893 gdb_vbr_vector = (void *)(vbr + 0x100);
894 printk("Setting GDB trap vector to 0x%08lx\n",
895 (unsigned long)gdb_vbr_vector);
896}
897#endif
898
Paul Mundtaba10302007-09-21 18:32:32 +0900899void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 extern void *vbr_base;
902
903#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtaba10302007-09-21 18:32:32 +0900904 if (raw_smp_processor_id() == 0)
905 gdb_vbr_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#endif
907
908 /* NOTE: The VBR value should be at P1
909 (or P2, virtural "fixed" address space).
910 It's definitely should not in physical address. */
911
912 asm volatile("ldc %0, vbr"
913 : /* no output */
914 : "r" (&vbr_base)
915 : "memory");
916}
917
Paul Mundt1f666582006-10-19 16:20:25 +0900918void *set_exception_table_vec(unsigned int vec, void *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 extern void *exception_handling_table[];
Paul Mundt1f666582006-10-19 16:20:25 +0900921 void *old_handler;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900922
Paul Mundt1f666582006-10-19 16:20:25 +0900923 old_handler = exception_handling_table[vec];
924 exception_handling_table[vec] = handler;
925 return old_handler;
926}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Paul Mundt1f666582006-10-19 16:20:25 +0900928void __init trap_init(void)
929{
930 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
931 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Takashi YOSHII4b565682006-09-27 17:15:32 +0900933#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
934 defined(CONFIG_SH_FPU_EMU)
935 /*
936 * For SH-4 lacking an FPU, treat floating point instructions as
937 * reserved. They'll be handled in the math-emu case, or faulted on
938 * otherwise.
939 */
Paul Mundt1f666582006-10-19 16:20:25 +0900940 set_exception_table_evt(0x800, do_reserved_inst);
941 set_exception_table_evt(0x820, do_illegal_slot_inst);
942#elif defined(CONFIG_SH_FPU)
Paul Mundt74d99a52007-11-26 20:38:36 +0900943 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
944 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900946
947#ifdef CONFIG_CPU_SH2
Paul Mundt5a4f7c62007-11-20 18:08:06 +0900948 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900949#endif
950#ifdef CONFIG_CPU_SH2A
951 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
952 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +0900953#ifdef CONFIG_SH_FPU
954 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
955#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900956#endif
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900957
Peter Griffincd894362009-05-08 15:51:51 +0100958#ifdef TRAP_UBC
Paul Mundtc4761812010-01-05 12:44:02 +0900959 set_exception_table_vec(TRAP_UBC, breakpoint_trap_handler);
Peter Griffincd894362009-05-08 15:51:51 +0100960#endif
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* Setup VBR for boot cpu */
963 per_cpu_trap_init();
964}
965
966void show_stack(struct task_struct *tsk, unsigned long *sp)
967{
Paul Mundt6b002232006-10-12 17:07:45 +0900968 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Paul Mundta6a311392006-09-27 18:22:14 +0900970 if (!tsk)
971 tsk = current;
972 if (tsk == current)
973 sp = (unsigned long *)current_stack_pointer;
974 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Paul Mundt6b002232006-10-12 17:07:45 +0900977 stack = (unsigned long)sp;
978 dump_mem("Stack: ", stack, THREAD_SIZE +
979 (unsigned long)task_stack_page(tsk));
980 show_trace(tsk, sp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983void dump_stack(void)
984{
985 show_stack(NULL, NULL);
986}
987EXPORT_SYMBOL(dump_stack);
Andre Draszik7436cde2009-08-24 14:53:46 +0900988
989#ifdef CONFIG_PROC_FS
990/*
991 * This needs to be done after sysctl_init, otherwise sys/ will be
992 * overwritten. Actually, this shouldn't be in sys/ at all since
993 * it isn't a sysctl, and it doesn't contain sysctl information.
994 * We now locate it in /proc/cpu/alignment instead.
995 */
996static int __init alignment_init(void)
997{
998 struct proc_dir_entry *dir, *res;
999
1000 dir = proc_mkdir("cpu", NULL);
1001 if (!dir)
1002 return -ENOMEM;
1003
Alexey Dobriyan9a160702009-11-27 06:42:16 +00001004 res = proc_create_data("alignment", S_IWUSR | S_IRUGO, dir,
1005 &alignment_proc_fops, &se_usermode);
Andre Draszik7436cde2009-08-24 14:53:46 +09001006 if (!res)
1007 return -ENOMEM;
1008
Alexey Dobriyan9a160702009-11-27 06:42:16 +00001009 res = proc_create_data("kernel_alignment", S_IWUSR | S_IRUGO, dir,
1010 &alignment_proc_fops, &se_kernmode_warn);
Andre Draszik7436cde2009-08-24 14:53:46 +09001011 if (!res)
1012 return -ENOMEM;
1013
Andre Draszik7436cde2009-08-24 14:53:46 +09001014 return 0;
1015}
1016
1017fs_initcall(alignment_init);
1018#endif