blob: efcbdfe52f5212f36a1f1f178e0581cdf24a03a8 [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>
Paul Mundt191d0d22010-01-12 14:50:43 +090034#include <asm/sh_bios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#ifdef CONFIG_CPU_SH2
Yoshinori Sato0983b312006-11-05 15:58:47 +090037# define TRAP_RESERVED_INST 4
38# define TRAP_ILLEGAL_SLOT_INST 6
39# define TRAP_ADDRESS_ERROR 9
40# ifdef CONFIG_CPU_SH2A
Peter Griffincd894362009-05-08 15:51:51 +010041# define TRAP_UBC 12
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +090042# define TRAP_FPU_ERROR 13
Yoshinori Sato0983b312006-11-05 15:58:47 +090043# define TRAP_DIVZERO_ERROR 17
44# define TRAP_DIVOVF_ERROR 18
45# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#else
47#define TRAP_RESERVED_INST 12
48#define TRAP_ILLEGAL_SLOT_INST 13
49#endif
50
Andre Draszik7436cde2009-08-24 14:53:46 +090051static unsigned long se_user;
52static unsigned long se_sys;
Andre Draszik7436cde2009-08-24 14:53:46 +090053static unsigned long se_half;
54static unsigned long se_word;
55static unsigned long se_dword;
56static unsigned long se_multi;
57/* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not
58 valid! */
59static int se_usermode = 3;
Paul Mundt84066382009-09-30 14:02:42 +090060/* 0: no warning 1: print a warning message, disabled by default */
61static int se_kernmode_warn;
Andre Draszik7436cde2009-08-24 14:53:46 +090062
63#ifdef CONFIG_PROC_FS
64static const char *se_usermode_action[] = {
65 "ignored",
66 "warn",
67 "fixup",
68 "fixup+warn",
69 "signal",
70 "signal+warn"
71};
72
Alexey Dobriyan9a160702009-11-27 06:42:16 +000073static int alignment_proc_show(struct seq_file *m, void *v)
Andre Draszik7436cde2009-08-24 14:53:46 +090074{
Alexey Dobriyan9a160702009-11-27 06:42:16 +000075 seq_printf(m, "User:\t\t%lu\n", se_user);
76 seq_printf(m, "System:\t\t%lu\n", se_sys);
77 seq_printf(m, "Half:\t\t%lu\n", se_half);
78 seq_printf(m, "Word:\t\t%lu\n", se_word);
79 seq_printf(m, "DWord:\t\t%lu\n", se_dword);
80 seq_printf(m, "Multi:\t\t%lu\n", se_multi);
81 seq_printf(m, "User faults:\t%i (%s)\n", se_usermode,
Andre Draszik7436cde2009-08-24 14:53:46 +090082 se_usermode_action[se_usermode]);
Alexey Dobriyan9a160702009-11-27 06:42:16 +000083 seq_printf(m, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn,
Andre Draszik7436cde2009-08-24 14:53:46 +090084 se_kernmode_warn ? "+warn" : "");
Alexey Dobriyan9a160702009-11-27 06:42:16 +000085 return 0;
Andre Draszik7436cde2009-08-24 14:53:46 +090086}
87
Alexey Dobriyan9a160702009-11-27 06:42:16 +000088static int alignment_proc_open(struct inode *inode, struct file *file)
Andre Draszik7436cde2009-08-24 14:53:46 +090089{
Alexey Dobriyan9a160702009-11-27 06:42:16 +000090 return single_open(file, alignment_proc_show, NULL);
91}
92
93static ssize_t alignment_proc_write(struct file *file,
94 const char __user *buffer, size_t count, loff_t *pos)
95{
96 int *data = PDE(file->f_path.dentry->d_inode)->data;
Andre Draszik7436cde2009-08-24 14:53:46 +090097 char mode;
98
99 if (count > 0) {
100 if (get_user(mode, buffer))
101 return -EFAULT;
102 if (mode >= '0' && mode <= '5')
Alexey Dobriyan9a160702009-11-27 06:42:16 +0000103 *data = mode - '0';
Andre Draszik7436cde2009-08-24 14:53:46 +0900104 }
105 return count;
106}
107
Alexey Dobriyan9a160702009-11-27 06:42:16 +0000108static const struct file_operations alignment_proc_fops = {
109 .owner = THIS_MODULE,
110 .open = alignment_proc_open,
111 .read = seq_read,
112 .llseek = seq_lseek,
113 .release = single_release,
114 .write = alignment_proc_write,
115};
Andre Draszik7436cde2009-08-24 14:53:46 +0900116#endif
117
Paul Mundt6b002232006-10-12 17:07:45 +0900118static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
119{
120 unsigned long p;
121 int i;
122
123 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
124
125 for (p = bottom & ~31; p < top; ) {
126 printk("%04lx: ", p & 0xffff);
127
128 for (i = 0; i < 8; i++, p += 4) {
129 unsigned int val;
130
131 if (p < bottom || p >= top)
132 printk(" ");
133 else {
134 if (__get_user(val, (unsigned int __user *)p)) {
135 printk("\n");
136 return;
137 }
138 printk("%08x ", val);
139 }
140 }
141 printk("\n");
142 }
143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Paul Mundt3a2e1172007-05-01 16:33:10 +0900145static DEFINE_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147void die(const char * str, struct pt_regs * regs, long err)
148{
149 static int die_counter;
150
Paul Mundt55273982007-06-18 18:57:13 +0900151 oops_enter();
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 spin_lock_irq(&die_lock);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900154 console_verbose();
Paul Mundt6b002232006-10-12 17:07:45 +0900155 bust_spinlocks(1);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900158 sysfs_printk_last_file();
Paul Mundt6b002232006-10-12 17:07:45 +0900159 print_modules();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 show_regs(regs);
Paul Mundt6b002232006-10-12 17:07:45 +0900161
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700162 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
163 task_pid_nr(current), task_stack_page(current) + 1);
Paul Mundt6b002232006-10-12 17:07:45 +0900164
165 if (!user_mode(regs) || in_interrupt())
166 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900167 (unsigned long)task_stack_page(current));
Paul Mundt6b002232006-10-12 17:07:45 +0900168
Paul Mundtc9306f02008-10-21 18:33:36 +0900169 notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
170
Paul Mundt6b002232006-10-12 17:07:45 +0900171 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700172 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 spin_unlock_irq(&die_lock);
Paul Mundtaf67c3a2009-10-13 10:57:52 +0900174 oops_exit();
Paul Mundte1132762007-05-15 08:36:36 +0900175
176 if (kexec_should_crash(current))
177 crash_kexec(regs);
178
179 if (in_interrupt())
180 panic("Fatal exception in interrupt");
181
182 if (panic_on_oops)
183 panic("Fatal exception");
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 do_exit(SIGSEGV);
186}
187
Paul Mundt6b002232006-10-12 17:07:45 +0900188static inline void die_if_kernel(const char *str, struct pt_regs *regs,
189 long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 if (!user_mode(regs))
192 die(str, regs, err);
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
196 * try and fix up kernelspace address errors
197 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
198 * - kernel/userspace interfaces cause a jump to an appropriate handler
199 * - other kernel errors are bad
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900201static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Paul Mundt6b002232006-10-12 17:07:45 +0900203 if (!user_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 const struct exception_table_entry *fixup;
205 fixup = search_exception_tables(regs->pc);
206 if (fixup) {
207 regs->pc = fixup->fixup;
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900208 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Matt Flemingb344e24a2009-08-16 21:54:48 +0100210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 die(str, regs, err);
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Magnus Damm86c01792008-02-07 00:02:50 +0900215static inline void sign_extend(unsigned int count, unsigned char *dst)
216{
217#ifdef __LITTLE_ENDIAN__
Magnus Damm4252c652008-02-07 19:58:46 +0900218 if ((count == 1) && dst[0] & 0x80) {
219 dst[1] = 0xff;
220 dst[2] = 0xff;
221 dst[3] = 0xff;
222 }
Magnus Damm86c01792008-02-07 00:02:50 +0900223 if ((count == 2) && dst[1] & 0x80) {
224 dst[2] = 0xff;
225 dst[3] = 0xff;
226 }
227#else
Magnus Damm4252c652008-02-07 19:58:46 +0900228 if ((count == 1) && dst[3] & 0x80) {
229 dst[2] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900230 dst[1] = 0xff;
Magnus Damm4252c652008-02-07 19:58:46 +0900231 dst[0] = 0xff;
232 }
233 if ((count == 2) && dst[2] & 0x80) {
234 dst[1] = 0xff;
235 dst[0] = 0xff;
Magnus Damm86c01792008-02-07 00:02:50 +0900236 }
237#endif
238}
239
Magnus Damme7cc9a72008-02-07 20:18:21 +0900240static struct mem_access user_mem_access = {
241 copy_from_user,
242 copy_to_user,
243};
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * handle an instruction that does an unaligned memory access by emulating the
247 * desired behaviour
248 * - note that PC _may not_ point to the faulting instruction
249 * (if that instruction is in a branch delay slot)
250 * - return 0 if emulation okay, -EFAULT on existential error
251 */
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900252static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
Magnus Damme7cc9a72008-02-07 20:18:21 +0900253 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int ret, index, count;
256 unsigned long *rm, *rn;
257 unsigned char *src, *dst;
Paul Mundtfa439722008-09-04 18:53:58 +0900258 unsigned char __user *srcu, *dstu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 index = (instruction>>8)&15; /* 0x0F00 */
261 rn = &regs->regs[index];
262
263 index = (instruction>>4)&15; /* 0x00F0 */
264 rm = &regs->regs[index];
265
266 count = 1<<(instruction&3);
267
Andre Draszik7436cde2009-08-24 14:53:46 +0900268 switch (count) {
269 case 1: se_half += 1; break;
270 case 2: se_word += 1; break;
271 case 4: se_dword += 1; break;
272 case 8: se_multi += 1; break; /* ??? */
273 }
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ret = -EFAULT;
276 switch (instruction>>12) {
277 case 0: /* mov.[bwl] to/from memory via r0+rn */
278 if (instruction & 8) {
279 /* from memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900280 srcu = (unsigned char __user *)*rm;
281 srcu += regs->regs[0];
282 dst = (unsigned char *)rn;
283 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Magnus Damm86c01792008-02-07 00:02:50 +0900285#if !defined(__LITTLE_ENDIAN__)
286 dst += 4-count;
287#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900288 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto fetch_fault;
290
Magnus Damm86c01792008-02-07 00:02:50 +0900291 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 } else {
293 /* to memory */
Paul Mundtfa439722008-09-04 18:53:58 +0900294 src = (unsigned char *)rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#if !defined(__LITTLE_ENDIAN__)
296 src += 4-count;
297#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900298 dstu = (unsigned char __user *)*rn;
299 dstu += regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Paul Mundtfa439722008-09-04 18:53:58 +0900301 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 goto fetch_fault;
303 }
304 ret = 0;
305 break;
306
307 case 1: /* mov.l Rm,@(disp,Rn) */
308 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900309 dstu = (unsigned char __user *)*rn;
310 dstu += (instruction&0x000F)<<2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Paul Mundtfa439722008-09-04 18:53:58 +0900312 if (ma->to(dstu, src, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto fetch_fault;
314 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
318 if (instruction & 4)
319 *rn -= count;
320 src = (unsigned char*) rm;
Paul Mundtfa439722008-09-04 18:53:58 +0900321 dstu = (unsigned char __user *)*rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#if !defined(__LITTLE_ENDIAN__)
323 src += 4-count;
324#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900325 if (ma->to(dstu, src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto fetch_fault;
327 ret = 0;
328 break;
329
330 case 5: /* mov.l @(disp,Rm),Rn */
Paul Mundtfa439722008-09-04 18:53:58 +0900331 srcu = (unsigned char __user *)*rm;
332 srcu += (instruction & 0x000F) << 2;
333 dst = (unsigned char *)rn;
334 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Paul Mundtfa439722008-09-04 18:53:58 +0900336 if (ma->from(dst, srcu, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto fetch_fault;
338 ret = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 case 6: /* mov.[bwl] from memory, possibly with post-increment */
Paul Mundtfa439722008-09-04 18:53:58 +0900342 srcu = (unsigned char __user *)*rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (instruction & 4)
344 *rm += count;
345 dst = (unsigned char*) rn;
346 *(unsigned long*)dst = 0;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900347
Magnus Damm86c01792008-02-07 00:02:50 +0900348#if !defined(__LITTLE_ENDIAN__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 dst += 4-count;
Magnus Damm86c01792008-02-07 00:02:50 +0900350#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900351 if (ma->from(dst, srcu, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900353 sign_extend(count, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 ret = 0;
355 break;
356
357 case 8:
358 switch ((instruction&0xFF00)>>8) {
359 case 0x81: /* mov.w R0,@(disp,Rn) */
Paul Mundtfa439722008-09-04 18:53:58 +0900360 src = (unsigned char *) &regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#if !defined(__LITTLE_ENDIAN__)
362 src += 2;
363#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900364 dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
365 dstu += (instruction & 0x000F) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Paul Mundtfa439722008-09-04 18:53:58 +0900367 if (ma->to(dstu, src, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto fetch_fault;
369 ret = 0;
370 break;
371
372 case 0x85: /* mov.w @(disp,Rm),R0 */
Paul Mundtfa439722008-09-04 18:53:58 +0900373 srcu = (unsigned char __user *)*rm;
374 srcu += (instruction & 0x000F) << 1;
375 dst = (unsigned char *) &regs->regs[0];
376 *(unsigned long *)dst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378#if !defined(__LITTLE_ENDIAN__)
379 dst += 2;
380#endif
Paul Mundtfa439722008-09-04 18:53:58 +0900381 if (ma->from(dst, srcu, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 goto fetch_fault;
Magnus Damm86c01792008-02-07 00:02:50 +0900383 sign_extend(2, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 ret = 0;
385 break;
386 }
387 break;
388 }
389 return ret;
390
391 fetch_fault:
392 /* Argh. Address not only misaligned but also non-existent.
393 * Raise an EFAULT and see if it's trapped
394 */
SUGIOKA Toshinobu2afb4472009-01-21 09:42:10 +0900395 die_if_no_fixup("Fault in unaligned fixup", regs, 0);
396 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399/*
400 * emulate the instruction in the delay slot
401 * - fetches the instruction from PC+2
402 */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900403static inline int handle_delayslot(struct pt_regs *regs,
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900404 insn_size_t old_instruction,
Magnus Damme7cc9a72008-02-07 20:18:21 +0900405 struct mem_access *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900407 insn_size_t instruction;
Paul Mundtfa439722008-09-04 18:53:58 +0900408 void __user *addr = (void __user *)(regs->pc +
409 instruction_size(old_instruction));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900411 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* the instruction-fetch faulted */
413 if (user_mode(regs))
414 return -EFAULT;
415
416 /* kernel */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900417 die("delay-slot-insn faulting in handle_unaligned_delayslot",
418 regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Magnus Damme7cc9a72008-02-07 20:18:21 +0900421 return handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/*
425 * handle an instruction that does an unaligned memory access
426 * - have to be careful of branch delay-slot instructions that fault
427 * SH3:
428 * - if the branch would be taken PC points to the branch
429 * - if the branch would not be taken, PC points to delay-slot
430 * SH4:
431 * - PC always points to delayed branch
432 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
433 */
434
435/* Macros to determine offset from current PC for branch instructions */
436/* Explicit type coercion is used to force sign extension where needed */
437#define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
438#define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
439
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900440int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000441 struct mem_access *ma, int expected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 u_int rm;
444 int ret, index;
445
Paul Mundt23c4c822009-09-24 17:38:18 +0900446 /*
447 * XXX: We can't handle mixed 16/32-bit instructions yet
448 */
449 if (instruction_size(instruction) != 2)
450 return -EINVAL;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 index = (instruction>>8)&15; /* 0x0F00 */
453 rm = regs->regs[index];
454
Andre Draszik9a4af022009-08-24 14:38:27 +0900455 /* shout about fixups */
Paul Mundt1232d882009-12-14 11:46:09 +0900456 if (!expected) {
457 if (user_mode(regs) && (se_usermode & 1) && printk_ratelimit())
458 pr_notice("Fixing up unaligned userspace access "
459 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
460 current->comm, task_pid_nr(current),
461 (void *)regs->pc, instruction);
462 else if (se_kernmode_warn && printk_ratelimit())
463 pr_notice("Fixing up unaligned kernel access "
464 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
465 current->comm, task_pid_nr(current),
466 (void *)regs->pc, instruction);
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 ret = -EFAULT;
470 switch (instruction&0xF000) {
471 case 0x0000:
472 if (instruction==0x000B) {
473 /* rts */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900474 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (ret==0)
476 regs->pc = regs->pr;
477 }
478 else if ((instruction&0x00FF)==0x0023) {
479 /* braf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900480 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (ret==0)
482 regs->pc += rm + 4;
483 }
484 else if ((instruction&0x00FF)==0x0003) {
485 /* bsrf @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900486 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (ret==0) {
488 regs->pr = regs->pc + 4;
489 regs->pc += rm + 4;
490 }
491 }
492 else {
493 /* mov.[bwl] to/from memory via r0+rn */
494 goto simple;
495 }
496 break;
497
498 case 0x1000: /* mov.l Rm,@(disp,Rn) */
499 goto simple;
500
501 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
502 goto simple;
503
504 case 0x4000:
505 if ((instruction&0x00FF)==0x002B) {
506 /* jmp @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900507 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (ret==0)
509 regs->pc = rm;
510 }
511 else if ((instruction&0x00FF)==0x000B) {
512 /* jsr @Rm */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900513 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (ret==0) {
515 regs->pr = regs->pc + 4;
516 regs->pc = rm;
517 }
518 }
519 else {
520 /* mov.[bwl] to/from memory via r0+rn */
521 goto simple;
522 }
523 break;
524
525 case 0x5000: /* mov.l @(disp,Rm),Rn */
526 goto simple;
527
528 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
529 goto simple;
530
531 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
532 switch (instruction&0x0F00) {
533 case 0x0100: /* mov.w R0,@(disp,Rm) */
534 goto simple;
535 case 0x0500: /* mov.w @(disp,Rm),R0 */
536 goto simple;
537 case 0x0B00: /* bf lab - no delayslot*/
538 break;
539 case 0x0F00: /* bf/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900540 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (ret==0) {
542#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
543 if ((regs->sr & 0x00000001) != 0)
544 regs->pc += 4; /* next after slot */
545 else
546#endif
547 regs->pc += SH_PC_8BIT_OFFSET(instruction);
548 }
549 break;
550 case 0x0900: /* bt lab - no delayslot */
551 break;
552 case 0x0D00: /* bt/s lab */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900553 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (ret==0) {
555#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
556 if ((regs->sr & 0x00000001) == 0)
557 regs->pc += 4; /* next after slot */
558 else
559#endif
560 regs->pc += SH_PC_8BIT_OFFSET(instruction);
561 }
562 break;
563 }
564 break;
565
566 case 0xA000: /* bra label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900567 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (ret==0)
569 regs->pc += SH_PC_12BIT_OFFSET(instruction);
570 break;
571
572 case 0xB000: /* bsr label */
Magnus Damme7cc9a72008-02-07 20:18:21 +0900573 ret = handle_delayslot(regs, instruction, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (ret==0) {
575 regs->pr = regs->pc + 4;
576 regs->pc += SH_PC_12BIT_OFFSET(instruction);
577 }
578 break;
579 }
580 return ret;
581
582 /* handle non-delay-slot instruction */
583 simple:
Magnus Damme7cc9a72008-02-07 20:18:21 +0900584 ret = handle_unaligned_ins(instruction, regs, ma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (ret==0)
Paul Mundt53f983a2007-05-08 15:31:48 +0900586 regs->pc += instruction_size(instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return ret;
588}
589
590/*
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900591 * Handle various address error exceptions:
592 * - instruction address error:
593 * misaligned PC
594 * PC >= 0x80000000 in user mode
595 * - data address error (read and write)
596 * misaligned data access
597 * access to >= 0x80000000 is user mode
598 * Unfortuntaly we can't distinguish between instruction address error
Simon Arlotte868d612007-05-14 08:15:10 +0900599 * and data address errors caused by read accesses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900601asmlinkage void do_address_error(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 unsigned long writeaccess,
603 unsigned long address)
604{
Yoshinori Sato0983b312006-11-05 15:58:47 +0900605 unsigned long error_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 mm_segment_t oldfs;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900607 siginfo_t info;
Paul Mundt2bcfffa2009-05-09 16:02:08 +0900608 insn_size_t instruction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int tmp;
610
Yoshinori Sato0983b312006-11-05 15:58:47 +0900611 /* Intentional ifdef */
612#ifdef CONFIG_CPU_HAS_SR_RB
Paul Mundt4c59e292008-09-21 12:00:23 +0900613 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 oldfs = get_fs();
617
618 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900619 int si_code = BUS_ADRERR;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Andre Draszik7436cde2009-08-24 14:53:46 +0900623 se_user += 1;
624
Andre Draszik5a0ab352009-08-24 15:01:10 +0900625 set_fs(USER_DS);
Paul Mundt23c4c822009-09-24 17:38:18 +0900626 if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
627 sizeof(instruction))) {
Andre Draszik5a0ab352009-08-24 15:01:10 +0900628 set_fs(oldfs);
629 goto uspace_segv;
630 }
631 set_fs(oldfs);
632
Andre Draszik7436cde2009-08-24 14:53:46 +0900633 /* shout about userspace fixups */
634 if (se_usermode & 1)
635 printk(KERN_NOTICE "Unaligned userspace access "
636 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
637 current->comm, current->pid, (void *)regs->pc,
638 instruction);
639
640 if (se_usermode & 2)
641 goto fixup;
642
643 if (se_usermode & 4)
644 goto uspace_segv;
645 else {
646 /* ignore */
Andre Draszik5a0ab352009-08-24 15:01:10 +0900647 regs->pc += instruction_size(instruction);
Andre Draszik7436cde2009-08-24 14:53:46 +0900648 return;
649 }
650
651fixup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* bad PC is not something we can fix */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900653 if (regs->pc & 1) {
654 si_code = BUS_ADRALN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto uspace_segv;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 set_fs(USER_DS);
Magnus Damme7cc9a72008-02-07 20:18:21 +0900659 tmp = handle_unaligned_access(instruction, regs,
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000660 &user_mem_access, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 set_fs(oldfs);
662
663 if (tmp==0)
664 return; /* sorted */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900665uspace_segv:
666 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
667 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
668 regs->pr);
669
670 info.si_signo = SIGBUS;
671 info.si_errno = 0;
672 info.si_code = si_code;
Paul Mundte08f4572007-05-14 12:52:56 +0900673 info.si_addr = (void __user *)address;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900674 force_sig_info(SIGBUS, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 } else {
Andre Draszik7436cde2009-08-24 14:53:46 +0900676 se_sys += 1;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (regs->pc & 1)
679 die("unaligned program counter", regs, error_code);
680
681 set_fs(KERNEL_DS);
Paul Mundtfa439722008-09-04 18:53:58 +0900682 if (copy_from_user(&instruction, (void __user *)(regs->pc),
Magnus Damm4b5a9ef2008-02-07 20:04:12 +0900683 sizeof(instruction))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* Argh. Fault on the instruction itself.
685 This should never happen non-SMP
686 */
687 set_fs(oldfs);
688 die("insn faulting in do_address_error", regs, 0);
689 }
690
Paul Mundt40258ee2009-09-24 17:48:15 +0900691 if (se_kernmode_warn)
692 printk(KERN_NOTICE "Unaligned kernel access "
693 "on behalf of \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
694 current->comm, current->pid, (void *)regs->pc,
695 instruction);
696
Matt Fleming4aa5ac42009-08-28 21:37:20 +0000697 handle_unaligned_access(instruction, regs,
698 &user_mem_access, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 set_fs(oldfs);
700 }
701}
702
703#ifdef CONFIG_SH_DSP
704/*
705 * SH-DSP support gerg@snapgear.com.
706 */
707int is_dsp_inst(struct pt_regs *regs)
708{
Paul Mundt882c12c2007-05-14 17:26:34 +0900709 unsigned short inst = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900711 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * Safe guard if DSP mode is already enabled or we're lacking
713 * the DSP altogether.
714 */
Paul Mundt11c19652006-12-25 10:19:56 +0900715 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717
718 get_user(inst, ((unsigned short *) regs->pc));
719
720 inst &= 0xf000;
721
722 /* Check for any type of DSP or support instruction */
723 if ((inst == 0xf000) || (inst == 0x4000))
724 return 1;
725
726 return 0;
727}
728#else
729#define is_dsp_inst(regs) (0)
730#endif /* CONFIG_SH_DSP */
731
Yoshinori Sato0983b312006-11-05 15:58:47 +0900732#ifdef CONFIG_CPU_SH2A
733asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
734 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900735 struct pt_regs __regs)
Yoshinori Sato0983b312006-11-05 15:58:47 +0900736{
737 siginfo_t info;
738
Yoshinori Sato0983b312006-11-05 15:58:47 +0900739 switch (r4) {
740 case TRAP_DIVZERO_ERROR:
741 info.si_code = FPE_INTDIV;
742 break;
743 case TRAP_DIVOVF_ERROR:
744 info.si_code = FPE_INTOVF;
745 break;
746 }
747
748 force_sig_info(SIGFPE, &info, current);
749}
750#endif
751
Takashi YOSHII4b565682006-09-27 17:15:32 +0900752asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
753 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900754 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900755{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900756 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900757 unsigned long error_code;
758 struct task_struct *tsk = current;
759
760#ifdef CONFIG_SH_FPU_EMU
Yoshinori Sato0983b312006-11-05 15:58:47 +0900761 unsigned short inst = 0;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900762 int err;
763
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900764 get_user(inst, (unsigned short*)regs->pc);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900765
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900766 err = do_fpu_inst(inst, regs);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900767 if (!err) {
Paul Mundt53f983a2007-05-08 15:31:48 +0900768 regs->pc += instruction_size(inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900769 return;
770 }
771 /* not a FPU inst. */
772#endif
773
774#ifdef CONFIG_SH_DSP
775 /* Check if it's a DSP instruction */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900776 if (is_dsp_inst(regs)) {
Takashi YOSHII4b565682006-09-27 17:15:32 +0900777 /* Enable DSP mode, and restart instruction. */
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900778 regs->sr |= SR_DSP;
Michael Trimarchi01ab1032009-04-03 17:32:33 +0000779 /* Save DSP mode */
780 tsk->thread.dsp_status.status |= SR_DSP;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900781 return;
782 }
783#endif
784
Paul Mundt4c59e292008-09-21 12:00:23 +0900785 error_code = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900786
Takashi YOSHII4b565682006-09-27 17:15:32 +0900787 local_irq_enable();
Takashi YOSHII4b565682006-09-27 17:15:32 +0900788 force_sig(SIGILL, tsk);
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900789 die_if_no_fixup("reserved instruction", regs, error_code);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900790}
791
792#ifdef CONFIG_SH_FPU_EMU
Paul Mundtedfd6da2008-11-26 13:06:04 +0900793static int emulate_branch(unsigned short inst, struct pt_regs *regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900794{
795 /*
796 * bfs: 8fxx: PC+=d*2+4;
797 * bts: 8dxx: PC+=d*2+4;
798 * bra: axxx: PC+=D*2+4;
799 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
800 * braf:0x23: PC+=Rn*2+4;
801 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
802 * jmp: 4x2b: PC=Rn;
803 * jsr: 4x0b: PC=Rn after PR=PC+4;
804 * rts: 000b: PC=PR;
805 */
Paul Mundtedfd6da2008-11-26 13:06:04 +0900806 if (((inst & 0xf000) == 0xb000) || /* bsr */
807 ((inst & 0xf0ff) == 0x0003) || /* bsrf */
808 ((inst & 0xf0ff) == 0x400b)) /* jsr */
809 regs->pr = regs->pc + 4;
810
811 if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900812 regs->pc += SH_PC_8BIT_OFFSET(inst);
813 return 0;
814 }
815
Paul Mundtedfd6da2008-11-26 13:06:04 +0900816 if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900817 regs->pc += SH_PC_12BIT_OFFSET(inst);
818 return 0;
819 }
820
Paul Mundtedfd6da2008-11-26 13:06:04 +0900821 if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900822 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
823 return 0;
824 }
825
Paul Mundtedfd6da2008-11-26 13:06:04 +0900826 if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900827 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
828 return 0;
829 }
830
Paul Mundtedfd6da2008-11-26 13:06:04 +0900831 if ((inst & 0xffff) == 0x000b) { /* rts */
Takashi YOSHII4b565682006-09-27 17:15:32 +0900832 regs->pc = regs->pr;
833 return 0;
834 }
835
836 return 1;
837}
838#endif
839
840asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
841 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900842 struct pt_regs __regs)
Takashi YOSHII4b565682006-09-27 17:15:32 +0900843{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900844 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900845 unsigned long inst;
Takashi YOSHII4b565682006-09-27 17:15:32 +0900846 struct task_struct *tsk = current;
Chris Smithd39f5452008-09-05 17:15:39 +0900847
848 if (kprobe_handle_illslot(regs->pc) == 0)
849 return;
850
Takashi YOSHII4b565682006-09-27 17:15:32 +0900851#ifdef CONFIG_SH_FPU_EMU
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900852 get_user(inst, (unsigned short *)regs->pc + 1);
853 if (!do_fpu_inst(inst, regs)) {
854 get_user(inst, (unsigned short *)regs->pc);
855 if (!emulate_branch(inst, regs))
Takashi YOSHII4b565682006-09-27 17:15:32 +0900856 return;
857 /* fault in branch.*/
858 }
859 /* not a FPU inst. */
860#endif
861
Paul Mundt4c59e292008-09-21 12:00:23 +0900862 inst = lookup_exception_vector();
Yoshinori Sato0983b312006-11-05 15:58:47 +0900863
Takashi YOSHII4b565682006-09-27 17:15:32 +0900864 local_irq_enable();
Takashi YOSHII4b565682006-09-27 17:15:32 +0900865 force_sig(SIGILL, tsk);
Paul Mundtb3d765f2008-09-17 23:12:11 +0900866 die_if_no_fixup("illegal slot instruction", regs, inst);
Takashi YOSHII4b565682006-09-27 17:15:32 +0900867}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
870 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900871 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900873 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 long ex;
Yoshinori Sato0983b312006-11-05 15:58:47 +0900875
Paul Mundt4c59e292008-09-21 12:00:23 +0900876 ex = lookup_exception_vector();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900877 die_if_kernel("exception", regs, ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Paul Mundtaba10302007-09-21 18:32:32 +0900880void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 extern void *vbr_base;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* NOTE: The VBR value should be at P1
885 (or P2, virtural "fixed" address space).
886 It's definitely should not in physical address. */
887
888 asm volatile("ldc %0, vbr"
889 : /* no output */
890 : "r" (&vbr_base)
891 : "memory");
892}
893
Paul Mundt1f666582006-10-19 16:20:25 +0900894void *set_exception_table_vec(unsigned int vec, void *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 extern void *exception_handling_table[];
Paul Mundt1f666582006-10-19 16:20:25 +0900897 void *old_handler;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900898
Paul Mundt1f666582006-10-19 16:20:25 +0900899 old_handler = exception_handling_table[vec];
900 exception_handling_table[vec] = handler;
901 return old_handler;
902}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Paul Mundt1f666582006-10-19 16:20:25 +0900904void __init trap_init(void)
905{
906 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
907 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Takashi YOSHII4b565682006-09-27 17:15:32 +0900909#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
910 defined(CONFIG_SH_FPU_EMU)
911 /*
912 * For SH-4 lacking an FPU, treat floating point instructions as
913 * reserved. They'll be handled in the math-emu case, or faulted on
914 * otherwise.
915 */
Paul Mundt1f666582006-10-19 16:20:25 +0900916 set_exception_table_evt(0x800, do_reserved_inst);
917 set_exception_table_evt(0x820, do_illegal_slot_inst);
918#elif defined(CONFIG_SH_FPU)
Paul Mundt74d99a52007-11-26 20:38:36 +0900919 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
920 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900922
923#ifdef CONFIG_CPU_SH2
Paul Mundt5a4f7c62007-11-20 18:08:06 +0900924 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
Yoshinori Sato0983b312006-11-05 15:58:47 +0900925#endif
926#ifdef CONFIG_CPU_SH2A
927 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
928 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
Yoshinori Sato6e80f5e2008-07-10 01:20:03 +0900929#ifdef CONFIG_SH_FPU
930 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
931#endif
Yoshinori Sato0983b312006-11-05 15:58:47 +0900932#endif
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900933
Peter Griffincd894362009-05-08 15:51:51 +0100934#ifdef TRAP_UBC
935 set_exception_table_vec(TRAP_UBC, break_point_trap);
936#endif
937
Paul Mundt191d0d22010-01-12 14:50:43 +0900938 /* Save off the BIOS VBR, if there is one */
939 sh_bios_vbr_init();
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* Setup VBR for boot cpu */
942 per_cpu_trap_init();
943}
944
945void show_stack(struct task_struct *tsk, unsigned long *sp)
946{
Paul Mundt6b002232006-10-12 17:07:45 +0900947 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Paul Mundta6a311392006-09-27 18:22:14 +0900949 if (!tsk)
950 tsk = current;
951 if (tsk == current)
952 sp = (unsigned long *)current_stack_pointer;
953 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 sp = (unsigned long *)tsk->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Paul Mundt6b002232006-10-12 17:07:45 +0900956 stack = (unsigned long)sp;
957 dump_mem("Stack: ", stack, THREAD_SIZE +
958 (unsigned long)task_stack_page(tsk));
959 show_trace(tsk, sp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962void dump_stack(void)
963{
964 show_stack(NULL, NULL);
965}
966EXPORT_SYMBOL(dump_stack);
Andre Draszik7436cde2009-08-24 14:53:46 +0900967
968#ifdef CONFIG_PROC_FS
969/*
970 * This needs to be done after sysctl_init, otherwise sys/ will be
971 * overwritten. Actually, this shouldn't be in sys/ at all since
972 * it isn't a sysctl, and it doesn't contain sysctl information.
973 * We now locate it in /proc/cpu/alignment instead.
974 */
975static int __init alignment_init(void)
976{
977 struct proc_dir_entry *dir, *res;
978
979 dir = proc_mkdir("cpu", NULL);
980 if (!dir)
981 return -ENOMEM;
982
Alexey Dobriyan9a160702009-11-27 06:42:16 +0000983 res = proc_create_data("alignment", S_IWUSR | S_IRUGO, dir,
984 &alignment_proc_fops, &se_usermode);
Andre Draszik7436cde2009-08-24 14:53:46 +0900985 if (!res)
986 return -ENOMEM;
987
Alexey Dobriyan9a160702009-11-27 06:42:16 +0000988 res = proc_create_data("kernel_alignment", S_IWUSR | S_IRUGO, dir,
989 &alignment_proc_fops, &se_kernmode_warn);
Andre Draszik7436cde2009-08-24 14:53:46 +0900990 if (!res)
991 return -ENOMEM;
992
Andre Draszik7436cde2009-08-24 14:53:46 +0900993 return 0;
994}
995
996fs_initcall(alignment_init);
997#endif