traps_32.c 21.0 KB
Newer Older
1 2 3
/*
 * 'traps.c' handles hardware traps and faults after we have saved some
 * state in 'entry.S'.
L
Linus Torvalds 已提交
4 5 6 7
 *
 *  SuperH version: Copyright (C) 1999 Niibe Yutaka
 *                  Copyright (C) 2000 Philipp Rumpf
 *                  Copyright (C) 2000 David Howells
P
Paul Mundt 已提交
8
 *                  Copyright (C) 2002 - 2007 Paul Mundt
9 10 11 12
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
L
Linus Torvalds 已提交
13 14 15 16 17 18 19
 */
#include <linux/kernel.h>
#include <linux/ptrace.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <linux/kallsyms.h>
20
#include <linux/io.h>
P
Paul Mundt 已提交
21
#include <linux/bug.h>
22
#include <linux/debug_locks.h>
P
Paul Mundt 已提交
23
#include <linux/kdebug.h>
24
#include <linux/kexec.h>
25
#include <linux/limits.h>
L
Linus Torvalds 已提交
26 27
#include <asm/system.h>
#include <asm/uaccess.h>
A
 
Andrew Morton 已提交
28
#include <asm/fpu.h>
C
Chris Smith 已提交
29
#include <asm/kprobes.h>
L
Linus Torvalds 已提交
30 31 32

#ifdef CONFIG_SH_KGDB
#include <asm/kgdb.h>
S
Stuart Menefy 已提交
33 34
#define CHK_REMOTE_DEBUG(regs)			\
{						\
T
Takashi YOSHII 已提交
35 36
	if (kgdb_debug_hook && !user_mode(regs))\
		(*kgdb_debug_hook)(regs);       \
L
Linus Torvalds 已提交
37 38 39 40 41 42
}
#else
#define CHK_REMOTE_DEBUG(regs)
#endif

#ifdef CONFIG_CPU_SH2
43 44 45 46
# define TRAP_RESERVED_INST	4
# define TRAP_ILLEGAL_SLOT_INST	6
# define TRAP_ADDRESS_ERROR	9
# ifdef CONFIG_CPU_SH2A
Y
Yoshinori Sato 已提交
47
#  define TRAP_FPU_ERROR	13
48 49 50
#  define TRAP_DIVZERO_ERROR	17
#  define TRAP_DIVOVF_ERROR	18
# endif
L
Linus Torvalds 已提交
51 52 53 54 55
#else
#define TRAP_RESERVED_INST	12
#define TRAP_ILLEGAL_SLOT_INST	13
#endif

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
{
	unsigned long p;
	int i;

	printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);

	for (p = bottom & ~31; p < top; ) {
		printk("%04lx: ", p & 0xffff);

		for (i = 0; i < 8; i++, p += 4) {
			unsigned int val;

			if (p < bottom || p >= top)
				printk("         ");
			else {
				if (__get_user(val, (unsigned int __user *)p)) {
					printk("\n");
					return;
				}
				printk("%08x ", val);
			}
		}
		printk("\n");
	}
}
L
Linus Torvalds 已提交
82

P
Paul Mundt 已提交
83
static DEFINE_SPINLOCK(die_lock);
L
Linus Torvalds 已提交
84 85 86 87 88

void die(const char * str, struct pt_regs * regs, long err)
{
	static int die_counter;

89 90
	oops_enter();

L
Linus Torvalds 已提交
91 92
	console_verbose();
	spin_lock_irq(&die_lock);
93 94
	bust_spinlocks(1);

L
Linus Torvalds 已提交
95
	printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
96

L
Linus Torvalds 已提交
97
	CHK_REMOTE_DEBUG(regs);
98
	print_modules();
L
Linus Torvalds 已提交
99
	show_regs(regs);
100

101 102
	printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
			task_pid_nr(current), task_stack_page(current) + 1);
103 104 105

	if (!user_mode(regs) || in_interrupt())
		dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
106
			 (unsigned long)task_stack_page(current));
107

108 109
	notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);

110
	bust_spinlocks(0);
111
	add_taint(TAINT_DIE);
L
Linus Torvalds 已提交
112
	spin_unlock_irq(&die_lock);
113 114 115 116 117 118 119 120 121 122

	if (kexec_should_crash(current))
		crash_kexec(regs);

	if (in_interrupt())
		panic("Fatal exception in interrupt");

	if (panic_on_oops)
		panic("Fatal exception");

123
	oops_exit();
L
Linus Torvalds 已提交
124 125 126
	do_exit(SIGSEGV);
}

127 128
static inline void die_if_kernel(const char *str, struct pt_regs *regs,
				 long err)
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142
{
	if (!user_mode(regs))
		die(str, regs, err);
}

/*
 * try and fix up kernelspace address errors
 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
 * - kernel/userspace interfaces cause a jump to an appropriate handler
 * - other kernel errors are bad
 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
 */
static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
{
143
	if (!user_mode(regs)) {
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154
		const struct exception_table_entry *fixup;
		fixup = search_exception_tables(regs->pc);
		if (fixup) {
			regs->pc = fixup->fixup;
			return 0;
		}
		die(str, regs, err);
	}
	return -EFAULT;
}

155 156 157
static inline void sign_extend(unsigned int count, unsigned char *dst)
{
#ifdef __LITTLE_ENDIAN__
158 159 160 161 162
	if ((count == 1) && dst[0] & 0x80) {
		dst[1] = 0xff;
		dst[2] = 0xff;
		dst[3] = 0xff;
	}
163 164 165 166 167
	if ((count == 2) && dst[1] & 0x80) {
		dst[2] = 0xff;
		dst[3] = 0xff;
	}
#else
168 169 170
	if ((count == 1) && dst[3] & 0x80) {
		dst[2] = 0xff;
		dst[1] = 0xff;
171
		dst[0] = 0xff;
172 173
	}
	if ((count == 2) && dst[2] & 0x80) {
174
		dst[1] = 0xff;
175
		dst[0] = 0xff;
176 177 178 179
	}
#endif
}

M
Magnus Damm 已提交
180 181 182 183 184
static struct mem_access user_mem_access = {
	copy_from_user,
	copy_to_user,
};

L
Linus Torvalds 已提交
185 186 187 188 189 190 191
/*
 * handle an instruction that does an unaligned memory access by emulating the
 * desired behaviour
 * - note that PC _may not_ point to the faulting instruction
 *   (if that instruction is in a branch delay slot)
 * - return 0 if emulation okay, -EFAULT on existential error
 */
M
Magnus Damm 已提交
192 193
static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
				struct mem_access *ma)
L
Linus Torvalds 已提交
194 195 196 197
{
	int ret, index, count;
	unsigned long *rm, *rn;
	unsigned char *src, *dst;
P
Paul Mundt 已提交
198
	unsigned char __user *srcu, *dstu;
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212

	index = (instruction>>8)&15;	/* 0x0F00 */
	rn = &regs->regs[index];

	index = (instruction>>4)&15;	/* 0x00F0 */
	rm = &regs->regs[index];

	count = 1<<(instruction&3);

	ret = -EFAULT;
	switch (instruction>>12) {
	case 0: /* mov.[bwl] to/from memory via r0+rn */
		if (instruction & 8) {
			/* from memory */
P
Paul Mundt 已提交
213 214 215 216
			srcu = (unsigned char __user *)*rm;
			srcu += regs->regs[0];
			dst = (unsigned char *)rn;
			*(unsigned long *)dst = 0;
L
Linus Torvalds 已提交
217

218
#if !defined(__LITTLE_ENDIAN__)
L
Linus Torvalds 已提交
219
			dst += 4-count;
220
#endif
P
Paul Mundt 已提交
221
			if (ma->from(dst, srcu, count))
L
Linus Torvalds 已提交
222 223
				goto fetch_fault;

224
			sign_extend(count, dst);
L
Linus Torvalds 已提交
225 226
		} else {
			/* to memory */
P
Paul Mundt 已提交
227
			src = (unsigned char *)rm;
L
Linus Torvalds 已提交
228 229 230
#if !defined(__LITTLE_ENDIAN__)
			src += 4-count;
#endif
P
Paul Mundt 已提交
231 232
			dstu = (unsigned char __user *)*rn;
			dstu += regs->regs[0];
L
Linus Torvalds 已提交
233

P
Paul Mundt 已提交
234
			if (ma->to(dstu, src, count))
L
Linus Torvalds 已提交
235 236 237 238 239 240 241
				goto fetch_fault;
		}
		ret = 0;
		break;

	case 1: /* mov.l Rm,@(disp,Rn) */
		src = (unsigned char*) rm;
P
Paul Mundt 已提交
242 243
		dstu = (unsigned char __user *)*rn;
		dstu += (instruction&0x000F)<<2;
L
Linus Torvalds 已提交
244

P
Paul Mundt 已提交
245
		if (ma->to(dstu, src, 4))
L
Linus Torvalds 已提交
246 247
			goto fetch_fault;
		ret = 0;
248
		break;
L
Linus Torvalds 已提交
249 250 251 252 253

	case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
		if (instruction & 4)
			*rn -= count;
		src = (unsigned char*) rm;
P
Paul Mundt 已提交
254
		dstu = (unsigned char __user *)*rn;
L
Linus Torvalds 已提交
255 256 257
#if !defined(__LITTLE_ENDIAN__)
		src += 4-count;
#endif
P
Paul Mundt 已提交
258
		if (ma->to(dstu, src, count))
L
Linus Torvalds 已提交
259 260 261 262 263
			goto fetch_fault;
		ret = 0;
		break;

	case 5: /* mov.l @(disp,Rm),Rn */
P
Paul Mundt 已提交
264 265 266 267
		srcu = (unsigned char __user *)*rm;
		srcu += (instruction & 0x000F) << 2;
		dst = (unsigned char *)rn;
		*(unsigned long *)dst = 0;
L
Linus Torvalds 已提交
268

P
Paul Mundt 已提交
269
		if (ma->from(dst, srcu, 4))
L
Linus Torvalds 已提交
270 271
			goto fetch_fault;
		ret = 0;
272
		break;
L
Linus Torvalds 已提交
273 274

	case 6:	/* mov.[bwl] from memory, possibly with post-increment */
P
Paul Mundt 已提交
275
		srcu = (unsigned char __user *)*rm;
L
Linus Torvalds 已提交
276 277 278 279
		if (instruction & 4)
			*rm += count;
		dst = (unsigned char*) rn;
		*(unsigned long*)dst = 0;
280

281
#if !defined(__LITTLE_ENDIAN__)
L
Linus Torvalds 已提交
282
		dst += 4-count;
283
#endif
P
Paul Mundt 已提交
284
		if (ma->from(dst, srcu, count))
L
Linus Torvalds 已提交
285
			goto fetch_fault;
286
		sign_extend(count, dst);
L
Linus Torvalds 已提交
287 288 289 290 291 292
		ret = 0;
		break;

	case 8:
		switch ((instruction&0xFF00)>>8) {
		case 0x81: /* mov.w R0,@(disp,Rn) */
P
Paul Mundt 已提交
293
			src = (unsigned char *) &regs->regs[0];
L
Linus Torvalds 已提交
294 295 296
#if !defined(__LITTLE_ENDIAN__)
			src += 2;
#endif
P
Paul Mundt 已提交
297 298
			dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
			dstu += (instruction & 0x000F) << 1;
L
Linus Torvalds 已提交
299

P
Paul Mundt 已提交
300
			if (ma->to(dstu, src, 2))
L
Linus Torvalds 已提交
301 302 303 304 305
				goto fetch_fault;
			ret = 0;
			break;

		case 0x85: /* mov.w @(disp,Rm),R0 */
P
Paul Mundt 已提交
306 307 308 309
			srcu = (unsigned char __user *)*rm;
			srcu += (instruction & 0x000F) << 1;
			dst = (unsigned char *) &regs->regs[0];
			*(unsigned long *)dst = 0;
L
Linus Torvalds 已提交
310 311 312 313

#if !defined(__LITTLE_ENDIAN__)
			dst += 2;
#endif
P
Paul Mundt 已提交
314
			if (ma->from(dst, srcu, 2))
L
Linus Torvalds 已提交
315
				goto fetch_fault;
316
			sign_extend(2, dst);
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
			ret = 0;
			break;
		}
		break;
	}
	return ret;

 fetch_fault:
	/* Argh. Address not only misaligned but also non-existent.
	 * Raise an EFAULT and see if it's trapped
	 */
	return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
}

/*
 * emulate the instruction in the delay slot
 * - fetches the instruction from PC+2
 */
M
Magnus Damm 已提交
335 336 337
static inline int handle_delayslot(struct pt_regs *regs,
				   opcode_t old_instruction,
				   struct mem_access *ma)
L
Linus Torvalds 已提交
338
{
339
	opcode_t instruction;
P
Paul Mundt 已提交
340 341
	void __user *addr = (void __user *)(regs->pc +
		instruction_size(old_instruction));
L
Linus Torvalds 已提交
342

343
	if (copy_from_user(&instruction, addr, sizeof(instruction))) {
L
Linus Torvalds 已提交
344 345 346 347 348
		/* the instruction-fetch faulted */
		if (user_mode(regs))
			return -EFAULT;

		/* kernel */
349 350
		die("delay-slot-insn faulting in handle_unaligned_delayslot",
		    regs, 0);
L
Linus Torvalds 已提交
351 352
	}

M
Magnus Damm 已提交
353
	return handle_unaligned_ins(instruction, regs, ma);
L
Linus Torvalds 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
}

/*
 * handle an instruction that does an unaligned memory access
 * - have to be careful of branch delay-slot instructions that fault
 *  SH3:
 *   - if the branch would be taken PC points to the branch
 *   - if the branch would not be taken, PC points to delay-slot
 *  SH4:
 *   - PC always points to delayed branch
 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
 */

/* Macros to determine offset from current PC for branch instructions */
/* Explicit type coercion is used to force sign extension where needed */
#define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
#define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)

P
Paul Mundt 已提交
372 373 374 375
/*
 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
 * opcodes..
 */
376

P
Paul Mundt 已提交
377 378
static int handle_unaligned_notify_count = 10;

M
Magnus Damm 已提交
379 380
int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs,
			    struct mem_access *ma)
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389 390 391
{
	u_int rm;
	int ret, index;

	index = (instruction>>8)&15;	/* 0x0F00 */
	rm = regs->regs[index];

	/* shout about the first ten userspace fixups */
	if (user_mode(regs) && handle_unaligned_notify_count>0) {
		handle_unaligned_notify_count--;

392 393
		printk(KERN_NOTICE "Fixing up unaligned userspace access "
		       "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
394
		       current->comm, task_pid_nr(current),
395
		       (void *)regs->pc, instruction);
L
Linus Torvalds 已提交
396 397 398 399 400 401 402
	}

	ret = -EFAULT;
	switch (instruction&0xF000) {
	case 0x0000:
		if (instruction==0x000B) {
			/* rts */
M
Magnus Damm 已提交
403
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
404 405 406 407 408
			if (ret==0)
				regs->pc = regs->pr;
		}
		else if ((instruction&0x00FF)==0x0023) {
			/* braf @Rm */
M
Magnus Damm 已提交
409
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
410 411 412 413 414
			if (ret==0)
				regs->pc += rm + 4;
		}
		else if ((instruction&0x00FF)==0x0003) {
			/* bsrf @Rm */
M
Magnus Damm 已提交
415
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
			if (ret==0) {
				regs->pr = regs->pc + 4;
				regs->pc += rm + 4;
			}
		}
		else {
			/* mov.[bwl] to/from memory via r0+rn */
			goto simple;
		}
		break;

	case 0x1000: /* mov.l Rm,@(disp,Rn) */
		goto simple;

	case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
		goto simple;

	case 0x4000:
		if ((instruction&0x00FF)==0x002B) {
			/* jmp @Rm */
M
Magnus Damm 已提交
436
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
437 438 439 440 441
			if (ret==0)
				regs->pc = rm;
		}
		else if ((instruction&0x00FF)==0x000B) {
			/* jsr @Rm */
M
Magnus Damm 已提交
442
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
			if (ret==0) {
				regs->pr = regs->pc + 4;
				regs->pc = rm;
			}
		}
		else {
			/* mov.[bwl] to/from memory via r0+rn */
			goto simple;
		}
		break;

	case 0x5000: /* mov.l @(disp,Rm),Rn */
		goto simple;

	case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
		goto simple;

	case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
		switch (instruction&0x0F00) {
		case 0x0100: /* mov.w R0,@(disp,Rm) */
			goto simple;
		case 0x0500: /* mov.w @(disp,Rm),R0 */
			goto simple;
		case 0x0B00: /* bf   lab - no delayslot*/
			break;
		case 0x0F00: /* bf/s lab */
M
Magnus Damm 已提交
469
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
470 471 472 473 474 475 476 477 478 479 480 481
			if (ret==0) {
#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
				if ((regs->sr & 0x00000001) != 0)
					regs->pc += 4; /* next after slot */
				else
#endif
					regs->pc += SH_PC_8BIT_OFFSET(instruction);
			}
			break;
		case 0x0900: /* bt   lab - no delayslot */
			break;
		case 0x0D00: /* bt/s lab */
M
Magnus Damm 已提交
482
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495
			if (ret==0) {
#if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
				if ((regs->sr & 0x00000001) == 0)
					regs->pc += 4; /* next after slot */
				else
#endif
					regs->pc += SH_PC_8BIT_OFFSET(instruction);
			}
			break;
		}
		break;

	case 0xA000: /* bra label */
M
Magnus Damm 已提交
496
		ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
497 498 499 500 501
		if (ret==0)
			regs->pc += SH_PC_12BIT_OFFSET(instruction);
		break;

	case 0xB000: /* bsr label */
M
Magnus Damm 已提交
502
		ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510 511 512
		if (ret==0) {
			regs->pr = regs->pc + 4;
			regs->pc += SH_PC_12BIT_OFFSET(instruction);
		}
		break;
	}
	return ret;

	/* handle non-delay-slot instruction */
 simple:
M
Magnus Damm 已提交
513
	ret = handle_unaligned_ins(instruction, regs, ma);
L
Linus Torvalds 已提交
514
	if (ret==0)
515
		regs->pc += instruction_size(instruction);
L
Linus Torvalds 已提交
516 517 518 519
	return ret;
}

/*
520 521 522 523 524 525 526 527
 * Handle various address error exceptions:
 *  - instruction address error:
 *       misaligned PC
 *       PC >= 0x80000000 in user mode
 *  - data address error (read and write)
 *       misaligned data access
 *       access to >= 0x80000000 is user mode
 * Unfortuntaly we can't distinguish between instruction address error
S
Simon Arlott 已提交
528
 * and data address errors caused by read accesses.
L
Linus Torvalds 已提交
529
 */
S
Stuart Menefy 已提交
530
asmlinkage void do_address_error(struct pt_regs *regs,
L
Linus Torvalds 已提交
531 532 533
				 unsigned long writeaccess,
				 unsigned long address)
{
534
	unsigned long error_code = 0;
L
Linus Torvalds 已提交
535
	mm_segment_t oldfs;
536
	siginfo_t info;
537
	opcode_t instruction;
L
Linus Torvalds 已提交
538 539
	int tmp;

540 541
	/* Intentional ifdef */
#ifdef CONFIG_CPU_HAS_SR_RB
542
	error_code = lookup_exception_vector();
543
#endif
L
Linus Torvalds 已提交
544 545 546 547

	oldfs = get_fs();

	if (user_mode(regs)) {
548 549
		int si_code = BUS_ADRERR;

L
Linus Torvalds 已提交
550 551 552
		local_irq_enable();

		/* bad PC is not something we can fix */
553 554
		if (regs->pc & 1) {
			si_code = BUS_ADRALN;
L
Linus Torvalds 已提交
555
			goto uspace_segv;
556
		}
L
Linus Torvalds 已提交
557 558

		set_fs(USER_DS);
P
Paul Mundt 已提交
559
		if (copy_from_user(&instruction, (void __user *)(regs->pc),
560
				   sizeof(instruction))) {
L
Linus Torvalds 已提交
561 562 563 564 565 566 567
			/* Argh. Fault on the instruction itself.
			   This should never happen non-SMP
			*/
			set_fs(oldfs);
			goto uspace_segv;
		}

M
Magnus Damm 已提交
568 569
		tmp = handle_unaligned_access(instruction, regs,
					      &user_mem_access);
L
Linus Torvalds 已提交
570 571 572 573
		set_fs(oldfs);

		if (tmp==0)
			return; /* sorted */
574 575 576 577 578 579 580 581
uspace_segv:
		printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
		       "access (PC %lx PR %lx)\n", current->comm, regs->pc,
		       regs->pr);

		info.si_signo = SIGBUS;
		info.si_errno = 0;
		info.si_code = si_code;
582
		info.si_addr = (void __user *)address;
583
		force_sig_info(SIGBUS, &info, current);
L
Linus Torvalds 已提交
584 585 586 587 588
	} else {
		if (regs->pc & 1)
			die("unaligned program counter", regs, error_code);

		set_fs(KERNEL_DS);
P
Paul Mundt 已提交
589
		if (copy_from_user(&instruction, (void __user *)(regs->pc),
590
				   sizeof(instruction))) {
L
Linus Torvalds 已提交
591 592 593 594 595 596 597
			/* Argh. Fault on the instruction itself.
			   This should never happen non-SMP
			*/
			set_fs(oldfs);
			die("insn faulting in do_address_error", regs, 0);
		}

M
Magnus Damm 已提交
598
		handle_unaligned_access(instruction, regs, &user_mem_access);
L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606 607 608
		set_fs(oldfs);
	}
}

#ifdef CONFIG_SH_DSP
/*
 *	SH-DSP support gerg@snapgear.com.
 */
int is_dsp_inst(struct pt_regs *regs)
{
609
	unsigned short inst = 0;
L
Linus Torvalds 已提交
610

S
Stuart Menefy 已提交
611
	/*
L
Linus Torvalds 已提交
612 613 614
	 * Safe guard if DSP mode is already enabled or we're lacking
	 * the DSP altogether.
	 */
615
	if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
L
Linus Torvalds 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
		return 0;

	get_user(inst, ((unsigned short *) regs->pc));

	inst &= 0xf000;

	/* Check for any type of DSP or support instruction */
	if ((inst == 0xf000) || (inst == 0x4000))
		return 1;

	return 0;
}
#else
#define is_dsp_inst(regs)	(0)
#endif /* CONFIG_SH_DSP */

632 633 634
#ifdef CONFIG_CPU_SH2A
asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
				unsigned long r6, unsigned long r7,
S
Stuart Menefy 已提交
635
				struct pt_regs __regs)
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
{
	siginfo_t info;

	switch (r4) {
	case TRAP_DIVZERO_ERROR:
		info.si_code = FPE_INTDIV;
		break;
	case TRAP_DIVOVF_ERROR:
		info.si_code = FPE_INTOVF;
		break;
	}

	force_sig_info(SIGFPE, &info, current);
}
#endif

T
Takashi YOSHII 已提交
652 653
asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
				unsigned long r6, unsigned long r7,
S
Stuart Menefy 已提交
654
				struct pt_regs __regs)
T
Takashi YOSHII 已提交
655
{
S
Stuart Menefy 已提交
656
	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
T
Takashi YOSHII 已提交
657 658 659 660
	unsigned long error_code;
	struct task_struct *tsk = current;

#ifdef CONFIG_SH_FPU_EMU
661
	unsigned short inst = 0;
T
Takashi YOSHII 已提交
662 663
	int err;

S
Stuart Menefy 已提交
664
	get_user(inst, (unsigned short*)regs->pc);
T
Takashi YOSHII 已提交
665

S
Stuart Menefy 已提交
666
	err = do_fpu_inst(inst, regs);
T
Takashi YOSHII 已提交
667
	if (!err) {
668
		regs->pc += instruction_size(inst);
T
Takashi YOSHII 已提交
669 670 671 672 673 674 675
		return;
	}
	/* not a FPU inst. */
#endif

#ifdef CONFIG_SH_DSP
	/* Check if it's a DSP instruction */
676
	if (is_dsp_inst(regs)) {
T
Takashi YOSHII 已提交
677
		/* Enable DSP mode, and restart instruction. */
S
Stuart Menefy 已提交
678
		regs->sr |= SR_DSP;
T
Takashi YOSHII 已提交
679 680 681 682
		return;
	}
#endif

683
	error_code = lookup_exception_vector();
684

T
Takashi YOSHII 已提交
685
	local_irq_enable();
S
Stuart Menefy 已提交
686
	CHK_REMOTE_DEBUG(regs);
T
Takashi YOSHII 已提交
687
	force_sig(SIGILL, tsk);
S
Stuart Menefy 已提交
688
	die_if_no_fixup("reserved instruction", regs, error_code);
T
Takashi YOSHII 已提交
689 690 691
}

#ifdef CONFIG_SH_FPU_EMU
692
static int emulate_branch(unsigned short inst, struct pt_regs *regs)
T
Takashi YOSHII 已提交
693 694 695 696 697 698 699 700 701 702 703 704
{
	/*
	 * bfs: 8fxx: PC+=d*2+4;
	 * bts: 8dxx: PC+=d*2+4;
	 * bra: axxx: PC+=D*2+4;
	 * bsr: bxxx: PC+=D*2+4  after PR=PC+4;
	 * braf:0x23: PC+=Rn*2+4;
	 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
	 * jmp: 4x2b: PC=Rn;
	 * jsr: 4x0b: PC=Rn      after PR=PC+4;
	 * rts: 000b: PC=PR;
	 */
705 706 707 708 709 710
	if (((inst & 0xf000) == 0xb000)  ||	/* bsr */
	    ((inst & 0xf0ff) == 0x0003)  ||	/* bsrf */
	    ((inst & 0xf0ff) == 0x400b))	/* jsr */
		regs->pr = regs->pc + 4;

	if ((inst & 0xfd00) == 0x8d00) {	/* bfs, bts */
T
Takashi YOSHII 已提交
711 712 713 714
		regs->pc += SH_PC_8BIT_OFFSET(inst);
		return 0;
	}

715
	if ((inst & 0xe000) == 0xa000) {	/* bra, bsr */
T
Takashi YOSHII 已提交
716 717 718 719
		regs->pc += SH_PC_12BIT_OFFSET(inst);
		return 0;
	}

720
	if ((inst & 0xf0df) == 0x0003) {	/* braf, bsrf */
T
Takashi YOSHII 已提交
721 722 723 724
		regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
		return 0;
	}

725
	if ((inst & 0xf0df) == 0x400b) {	/* jmp, jsr */
T
Takashi YOSHII 已提交
726 727 728 729
		regs->pc = regs->regs[(inst & 0x0f00) >> 8];
		return 0;
	}

730
	if ((inst & 0xffff) == 0x000b) {	/* rts */
T
Takashi YOSHII 已提交
731 732 733 734 735 736 737 738 739 740
		regs->pc = regs->pr;
		return 0;
	}

	return 1;
}
#endif

asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
				unsigned long r6, unsigned long r7,
S
Stuart Menefy 已提交
741
				struct pt_regs __regs)
T
Takashi YOSHII 已提交
742
{
S
Stuart Menefy 已提交
743
	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
P
Paul Mundt 已提交
744
	unsigned long inst;
T
Takashi YOSHII 已提交
745
	struct task_struct *tsk = current;
C
Chris Smith 已提交
746 747 748 749

	if (kprobe_handle_illslot(regs->pc) == 0)
		return;

T
Takashi YOSHII 已提交
750
#ifdef CONFIG_SH_FPU_EMU
S
Stuart Menefy 已提交
751 752 753 754
	get_user(inst, (unsigned short *)regs->pc + 1);
	if (!do_fpu_inst(inst, regs)) {
		get_user(inst, (unsigned short *)regs->pc);
		if (!emulate_branch(inst, regs))
T
Takashi YOSHII 已提交
755 756 757 758 759 760
			return;
		/* fault in branch.*/
	}
	/* not a FPU inst. */
#endif

761
	inst = lookup_exception_vector();
762

T
Takashi YOSHII 已提交
763
	local_irq_enable();
S
Stuart Menefy 已提交
764
	CHK_REMOTE_DEBUG(regs);
T
Takashi YOSHII 已提交
765
	force_sig(SIGILL, tsk);
P
Paul Mundt 已提交
766
	die_if_no_fixup("illegal slot instruction", regs, inst);
T
Takashi YOSHII 已提交
767
}
L
Linus Torvalds 已提交
768 769 770

asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
				   unsigned long r6, unsigned long r7,
S
Stuart Menefy 已提交
771
				   struct pt_regs __regs)
L
Linus Torvalds 已提交
772
{
S
Stuart Menefy 已提交
773
	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
L
Linus Torvalds 已提交
774
	long ex;
775

776
	ex = lookup_exception_vector();
S
Stuart Menefy 已提交
777
	die_if_kernel("exception", regs, ex);
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
}

#if defined(CONFIG_SH_STANDARD_BIOS)
void *gdb_vbr_vector;

static inline void __init gdb_vbr_init(void)
{
	register unsigned long vbr;

	/*
	 * Read the old value of the VBR register to initialise
	 * the vector through which debug and BIOS traps are
	 * delegated by the Linux trap handler.
	 */
	asm volatile("stc vbr, %0" : "=r" (vbr));

	gdb_vbr_vector = (void *)(vbr + 0x100);
	printk("Setting GDB trap vector to 0x%08lx\n",
	       (unsigned long)gdb_vbr_vector);
}
#endif

800
void __cpuinit per_cpu_trap_init(void)
L
Linus Torvalds 已提交
801 802 803 804
{
	extern void *vbr_base;

#ifdef CONFIG_SH_STANDARD_BIOS
805 806
	if (raw_smp_processor_id() == 0)
		gdb_vbr_init();
L
Linus Torvalds 已提交
807 808 809 810 811 812 813 814 815 816 817 818
#endif

	/* NOTE: The VBR value should be at P1
	   (or P2, virtural "fixed" address space).
	   It's definitely should not in physical address.  */

	asm volatile("ldc	%0, vbr"
		     : /* no output */
		     : "r" (&vbr_base)
		     : "memory");
}

819
void *set_exception_table_vec(unsigned int vec, void *handler)
L
Linus Torvalds 已提交
820 821
{
	extern void *exception_handling_table[];
822
	void *old_handler;
823

824 825 826 827
	old_handler = exception_handling_table[vec];
	exception_handling_table[vec] = handler;
	return old_handler;
}
L
Linus Torvalds 已提交
828

829 830 831 832
void __init trap_init(void)
{
	set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
	set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
L
Linus Torvalds 已提交
833

T
Takashi YOSHII 已提交
834 835 836 837 838 839 840
#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
    defined(CONFIG_SH_FPU_EMU)
	/*
	 * For SH-4 lacking an FPU, treat floating point instructions as
	 * reserved. They'll be handled in the math-emu case, or faulted on
	 * otherwise.
	 */
841 842 843
	set_exception_table_evt(0x800, do_reserved_inst);
	set_exception_table_evt(0x820, do_illegal_slot_inst);
#elif defined(CONFIG_SH_FPU)
844
#ifdef CONFIG_CPU_SUBTYPE_SHX3
P
Paul Mundt 已提交
845 846
	set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
	set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
847
#else
P
Paul Mundt 已提交
848 849
	set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
	set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
L
Linus Torvalds 已提交
850
#endif
851
#endif
852 853

#ifdef CONFIG_CPU_SH2
854
	set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
855 856 857 858
#endif
#ifdef CONFIG_CPU_SH2A
	set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
	set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
Y
Yoshinori Sato 已提交
859 860 861
#ifdef CONFIG_SH_FPU
	set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
#endif
862
#endif
863

L
Linus Torvalds 已提交
864 865 866 867
	/* Setup VBR for boot cpu */
	per_cpu_trap_init();
}

868 869
void show_trace(struct task_struct *tsk, unsigned long *sp,
		struct pt_regs *regs)
L
Linus Torvalds 已提交
870
{
871
	unsigned long addr;
L
Linus Torvalds 已提交
872

873 874
	if (regs && user_mode(regs))
		return;
L
Linus Torvalds 已提交
875 876 877 878 879 880

	printk("\nCall trace: ");
#ifdef CONFIG_KALLSYMS
	printk("\n");
#endif

881 882 883 884
	while (!kstack_end(sp)) {
		addr = *sp++;
		if (kernel_text_address(addr))
			print_ip_sym(addr);
L
Linus Torvalds 已提交
885 886 887
	}

	printk("\n");
888 889 890 891 892

	if (!tsk)
		tsk = current;

	debug_show_held_locks(tsk);
L
Linus Torvalds 已提交
893 894
}

895
void show_stack(struct task_struct *tsk, unsigned long *sp)
L
Linus Torvalds 已提交
896
{
897 898 899 900 901 902 903 904 905 906 907 908 909
	unsigned long stack;

	if (!tsk)
		tsk = current;
	if (tsk == current)
		sp = (unsigned long *)current_stack_pointer;
	else
		sp = (unsigned long *)tsk->thread.sp;

	stack = (unsigned long)sp;
	dump_mem("Stack: ", stack, THREAD_SIZE +
		 (unsigned long)task_stack_page(tsk));
	show_trace(tsk, sp, NULL);
L
Linus Torvalds 已提交
910 911 912 913 914 915 916
}

void dump_stack(void)
{
	show_stack(NULL, NULL);
}
EXPORT_SYMBOL(dump_stack);