traps_32.c 18.8 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
8
 *                  Copyright (C) 2002 - 2010 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
 */
#include <linux/kernel.h>
#include <linux/ptrace.h>
16
#include <linux/hardirq.h>
L
Linus Torvalds 已提交
17 18 19
#include <linux/init.h>
#include <linux/spinlock.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/limits.h>
P
Paul Mundt 已提交
25
#include <linux/sysfs.h>
26
#include <linux/uaccess.h>
27
#include <linux/perf_event.h>
28
#include <asm/alignment.h>
A
 
Andrew Morton 已提交
29
#include <asm/fpu.h>
C
Chris Smith 已提交
30
#include <asm/kprobes.h>
D
David Howells 已提交
31 32
#include <asm/traps.h>
#include <asm/bl_bit.h>
L
Linus Torvalds 已提交
33 34

#ifdef CONFIG_CPU_SH2
35 36 37 38
# define TRAP_RESERVED_INST	4
# define TRAP_ILLEGAL_SLOT_INST	6
# define TRAP_ADDRESS_ERROR	9
# ifdef CONFIG_CPU_SH2A
P
Peter Griffin 已提交
39
#  define TRAP_UBC		12
Y
Yoshinori Sato 已提交
40
#  define TRAP_FPU_ERROR	13
41 42 43
#  define TRAP_DIVZERO_ERROR	17
#  define TRAP_DIVOVF_ERROR	18
# endif
L
Linus Torvalds 已提交
44 45 46 47 48
#else
#define TRAP_RESERVED_INST	12
#define TRAP_ILLEGAL_SLOT_INST	13
#endif

49 50 51
static inline void sign_extend(unsigned int count, unsigned char *dst)
{
#ifdef __LITTLE_ENDIAN__
52 53 54 55 56
	if ((count == 1) && dst[0] & 0x80) {
		dst[1] = 0xff;
		dst[2] = 0xff;
		dst[3] = 0xff;
	}
57 58 59 60 61
	if ((count == 2) && dst[1] & 0x80) {
		dst[2] = 0xff;
		dst[3] = 0xff;
	}
#else
62 63 64
	if ((count == 1) && dst[3] & 0x80) {
		dst[2] = 0xff;
		dst[1] = 0xff;
65
		dst[0] = 0xff;
66 67
	}
	if ((count == 2) && dst[2] & 0x80) {
68
		dst[1] = 0xff;
69
		dst[0] = 0xff;
70 71 72 73
	}
#endif
}

M
Magnus Damm 已提交
74 75 76 77 78
static struct mem_access user_mem_access = {
	copy_from_user,
	copy_to_user,
};

L
Linus Torvalds 已提交
79 80 81 82 83 84 85
/*
 * 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
 */
P
Paul Mundt 已提交
86
static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
M
Magnus Damm 已提交
87
				struct mem_access *ma)
L
Linus Torvalds 已提交
88 89 90 91
{
	int ret, index, count;
	unsigned long *rm, *rn;
	unsigned char *src, *dst;
P
Paul Mundt 已提交
92
	unsigned char __user *srcu, *dstu;
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101

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

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

	count = 1<<(instruction&3);

102
	switch (count) {
103 104 105 106
	case 1: inc_unaligned_byte_access(); break;
	case 2: inc_unaligned_word_access(); break;
	case 4: inc_unaligned_dword_access(); break;
	case 8: inc_unaligned_multi_access(); break;
107 108
	}

L
Linus Torvalds 已提交
109 110 111 112 113
	ret = -EFAULT;
	switch (instruction>>12) {
	case 0: /* mov.[bwl] to/from memory via r0+rn */
		if (instruction & 8) {
			/* from memory */
P
Paul Mundt 已提交
114 115 116 117
			srcu = (unsigned char __user *)*rm;
			srcu += regs->regs[0];
			dst = (unsigned char *)rn;
			*(unsigned long *)dst = 0;
L
Linus Torvalds 已提交
118

119
#if !defined(__LITTLE_ENDIAN__)
L
Linus Torvalds 已提交
120
			dst += 4-count;
121
#endif
P
Paul Mundt 已提交
122
			if (ma->from(dst, srcu, count))
L
Linus Torvalds 已提交
123 124
				goto fetch_fault;

125
			sign_extend(count, dst);
L
Linus Torvalds 已提交
126 127
		} else {
			/* to memory */
P
Paul Mundt 已提交
128
			src = (unsigned char *)rm;
L
Linus Torvalds 已提交
129 130 131
#if !defined(__LITTLE_ENDIAN__)
			src += 4-count;
#endif
P
Paul Mundt 已提交
132 133
			dstu = (unsigned char __user *)*rn;
			dstu += regs->regs[0];
L
Linus Torvalds 已提交
134

P
Paul Mundt 已提交
135
			if (ma->to(dstu, src, count))
L
Linus Torvalds 已提交
136 137 138 139 140 141 142
				goto fetch_fault;
		}
		ret = 0;
		break;

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

P
Paul Mundt 已提交
146
		if (ma->to(dstu, src, 4))
L
Linus Torvalds 已提交
147 148
			goto fetch_fault;
		ret = 0;
149
		break;
L
Linus Torvalds 已提交
150 151 152 153 154

	case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
		if (instruction & 4)
			*rn -= count;
		src = (unsigned char*) rm;
P
Paul Mundt 已提交
155
		dstu = (unsigned char __user *)*rn;
L
Linus Torvalds 已提交
156 157 158
#if !defined(__LITTLE_ENDIAN__)
		src += 4-count;
#endif
P
Paul Mundt 已提交
159
		if (ma->to(dstu, src, count))
L
Linus Torvalds 已提交
160 161 162 163 164
			goto fetch_fault;
		ret = 0;
		break;

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

P
Paul Mundt 已提交
170
		if (ma->from(dst, srcu, 4))
L
Linus Torvalds 已提交
171 172
			goto fetch_fault;
		ret = 0;
173
		break;
L
Linus Torvalds 已提交
174 175

	case 6:	/* mov.[bwl] from memory, possibly with post-increment */
P
Paul Mundt 已提交
176
		srcu = (unsigned char __user *)*rm;
L
Linus Torvalds 已提交
177 178 179 180
		if (instruction & 4)
			*rm += count;
		dst = (unsigned char*) rn;
		*(unsigned long*)dst = 0;
181

182
#if !defined(__LITTLE_ENDIAN__)
L
Linus Torvalds 已提交
183
		dst += 4-count;
184
#endif
P
Paul Mundt 已提交
185
		if (ma->from(dst, srcu, count))
L
Linus Torvalds 已提交
186
			goto fetch_fault;
187
		sign_extend(count, dst);
L
Linus Torvalds 已提交
188 189 190 191 192 193
		ret = 0;
		break;

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

P
Paul Mundt 已提交
201
			if (ma->to(dstu, src, 2))
L
Linus Torvalds 已提交
202 203 204 205 206
				goto fetch_fault;
			ret = 0;
			break;

		case 0x85: /* mov.w @(disp,Rm),R0 */
P
Paul Mundt 已提交
207 208 209 210
			srcu = (unsigned char __user *)*rm;
			srcu += (instruction & 0x000F) << 1;
			dst = (unsigned char *) &regs->regs[0];
			*(unsigned long *)dst = 0;
L
Linus Torvalds 已提交
211 212 213 214

#if !defined(__LITTLE_ENDIAN__)
			dst += 2;
#endif
P
Paul Mundt 已提交
215
			if (ma->from(dst, srcu, 2))
L
Linus Torvalds 已提交
216
				goto fetch_fault;
217
			sign_extend(2, dst);
L
Linus Torvalds 已提交
218 219 220 221
			ret = 0;
			break;
		}
		break;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

	case 9: /* mov.w @(disp,PC),Rn */
		srcu = (unsigned char __user *)regs->pc;
		srcu += 4;
		srcu += (instruction & 0x00FF) << 1;
		dst = (unsigned char *)rn;
		*(unsigned long *)dst = 0;

#if !defined(__LITTLE_ENDIAN__)
		dst += 2;
#endif

		if (ma->from(dst, srcu, 2))
			goto fetch_fault;
		sign_extend(2, dst);
		ret = 0;
		break;

	case 0xd: /* mov.l @(disp,PC),Rn */
		srcu = (unsigned char __user *)(regs->pc & ~0x3);
		srcu += 4;
		srcu += (instruction & 0x00FF) << 2;
		dst = (unsigned char *)rn;
		*(unsigned long *)dst = 0;

		if (ma->from(dst, srcu, 4))
			goto fetch_fault;
		ret = 0;
		break;
L
Linus Torvalds 已提交
251 252 253 254 255 256 257
	}
	return ret;

 fetch_fault:
	/* Argh. Address not only misaligned but also non-existent.
	 * Raise an EFAULT and see if it's trapped
	 */
258 259
	die_if_no_fixup("Fault in unaligned fixup", regs, 0);
	return -EFAULT;
L
Linus Torvalds 已提交
260 261 262 263 264 265
}

/*
 * emulate the instruction in the delay slot
 * - fetches the instruction from PC+2
 */
M
Magnus Damm 已提交
266
static inline int handle_delayslot(struct pt_regs *regs,
P
Paul Mundt 已提交
267
				   insn_size_t old_instruction,
M
Magnus Damm 已提交
268
				   struct mem_access *ma)
L
Linus Torvalds 已提交
269
{
P
Paul Mundt 已提交
270
	insn_size_t instruction;
P
Paul Mundt 已提交
271 272
	void __user *addr = (void __user *)(regs->pc +
		instruction_size(old_instruction));
L
Linus Torvalds 已提交
273

274
	if (copy_from_user(&instruction, addr, sizeof(instruction))) {
L
Linus Torvalds 已提交
275 276 277 278 279
		/* the instruction-fetch faulted */
		if (user_mode(regs))
			return -EFAULT;

		/* kernel */
280 281
		die("delay-slot-insn faulting in handle_unaligned_delayslot",
		    regs, 0);
L
Linus Torvalds 已提交
282 283
	}

M
Magnus Damm 已提交
284
	return handle_unaligned_ins(instruction, regs, ma);
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
}

/*
 * 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 已提交
303
int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
304 305
			    struct mem_access *ma, int expected,
			    unsigned long address)
L
Linus Torvalds 已提交
306 307 308 309
{
	u_int rm;
	int ret, index;

310 311 312 313 314 315
	/*
	 * XXX: We can't handle mixed 16/32-bit instructions yet
	 */
	if (instruction_size(instruction) != 2)
		return -EINVAL;

L
Linus Torvalds 已提交
316 317 318
	index = (instruction>>8)&15;	/* 0x0F00 */
	rm = regs->regs[index];

319 320 321 322 323 324 325 326
	/*
	 * Log the unexpected fixups, and then pass them on to perf.
	 *
	 * We intentionally don't report the expected cases to perf as
	 * otherwise the trapped I/O case will skew the results too much
	 * to be useful.
	 */
	if (!expected) {
327
		unaligned_fixups_notify(current, instruction, regs);
328
		perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1,
329 330
			      regs, address);
	}
L
Linus Torvalds 已提交
331 332 333 334 335 336

	ret = -EFAULT;
	switch (instruction&0xF000) {
	case 0x0000:
		if (instruction==0x000B) {
			/* rts */
M
Magnus Damm 已提交
337
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
338 339 340 341 342
			if (ret==0)
				regs->pc = regs->pr;
		}
		else if ((instruction&0x00FF)==0x0023) {
			/* braf @Rm */
M
Magnus Damm 已提交
343
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
344 345 346 347 348
			if (ret==0)
				regs->pc += rm + 4;
		}
		else if ((instruction&0x00FF)==0x0003) {
			/* bsrf @Rm */
M
Magnus Damm 已提交
349
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
			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 已提交
370
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
371 372 373 374 375
			if (ret==0)
				regs->pc = rm;
		}
		else if ((instruction&0x00FF)==0x000B) {
			/* jsr @Rm */
M
Magnus Damm 已提交
376
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
			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*/
401
			ret = 0;
L
Linus Torvalds 已提交
402 403
			break;
		case 0x0F00: /* bf/s lab */
M
Magnus Damm 已提交
404
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413 414
			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 */
415
			ret = 0;
L
Linus Torvalds 已提交
416 417
			break;
		case 0x0D00: /* bt/s lab */
M
Magnus Damm 已提交
418
			ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
419 420 421 422 423 424 425 426 427 428 429 430
			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;

431 432 433
	case 0x9000: /* mov.w @(disp,Rm),Rn */
		goto simple;

L
Linus Torvalds 已提交
434
	case 0xA000: /* bra label */
M
Magnus Damm 已提交
435
		ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
436 437 438 439 440
		if (ret==0)
			regs->pc += SH_PC_12BIT_OFFSET(instruction);
		break;

	case 0xB000: /* bsr label */
M
Magnus Damm 已提交
441
		ret = handle_delayslot(regs, instruction, ma);
L
Linus Torvalds 已提交
442 443 444 445 446
		if (ret==0) {
			regs->pr = regs->pc + 4;
			regs->pc += SH_PC_12BIT_OFFSET(instruction);
		}
		break;
447 448 449

	case 0xD000: /* mov.l @(disp,Rm),Rn */
		goto simple;
L
Linus Torvalds 已提交
450 451 452 453 454
	}
	return ret;

	/* handle non-delay-slot instruction */
 simple:
M
Magnus Damm 已提交
455
	ret = handle_unaligned_ins(instruction, regs, ma);
L
Linus Torvalds 已提交
456
	if (ret==0)
457
		regs->pc += instruction_size(instruction);
L
Linus Torvalds 已提交
458 459 460 461
	return ret;
}

/*
462 463 464 465 466 467 468 469
 * 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 已提交
470
 * and data address errors caused by read accesses.
L
Linus Torvalds 已提交
471
 */
S
Stuart Menefy 已提交
472
asmlinkage void do_address_error(struct pt_regs *regs,
L
Linus Torvalds 已提交
473 474 475
				 unsigned long writeaccess,
				 unsigned long address)
{
476
	unsigned long error_code = 0;
L
Linus Torvalds 已提交
477
	mm_segment_t oldfs;
478
	siginfo_t info;
P
Paul Mundt 已提交
479
	insn_size_t instruction;
L
Linus Torvalds 已提交
480 481
	int tmp;

482 483
	/* Intentional ifdef */
#ifdef CONFIG_CPU_HAS_SR_RB
484
	error_code = lookup_exception_vector();
485
#endif
L
Linus Torvalds 已提交
486 487 488 489

	oldfs = get_fs();

	if (user_mode(regs)) {
490
		int si_code = BUS_ADRERR;
491
		unsigned int user_action;
492

L
Linus Torvalds 已提交
493
		local_irq_enable();
494
		inc_unaligned_user_access();
495

496
		set_fs(USER_DS);
497 498
		if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
				   sizeof(instruction))) {
499 500 501 502 503
			set_fs(oldfs);
			goto uspace_segv;
		}
		set_fs(oldfs);

504
		/* shout about userspace fixups */
505
		unaligned_fixups_notify(current, instruction, regs);
506

507 508
		user_action = unaligned_user_action();
		if (user_action & UM_FIXUP)
509
			goto fixup;
510
		if (user_action & UM_SIGNAL)
511 512 513
			goto uspace_segv;
		else {
			/* ignore */
514
			regs->pc += instruction_size(instruction);
515 516 517 518
			return;
		}

fixup:
L
Linus Torvalds 已提交
519
		/* bad PC is not something we can fix */
520 521
		if (regs->pc & 1) {
			si_code = BUS_ADRALN;
L
Linus Torvalds 已提交
522
			goto uspace_segv;
523
		}
L
Linus Torvalds 已提交
524 525

		set_fs(USER_DS);
M
Magnus Damm 已提交
526
		tmp = handle_unaligned_access(instruction, regs,
527 528
					      &user_mem_access, 0,
					      address);
L
Linus Torvalds 已提交
529 530
		set_fs(oldfs);

531
		if (tmp == 0)
L
Linus Torvalds 已提交
532
			return; /* sorted */
533 534 535 536 537 538 539 540
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;
541
		info.si_addr = (void __user *)address;
542
		force_sig_info(SIGBUS, &info, current);
L
Linus Torvalds 已提交
543
	} else {
544
		inc_unaligned_kernel_access();
545

L
Linus Torvalds 已提交
546 547 548 549
		if (regs->pc & 1)
			die("unaligned program counter", regs, error_code);

		set_fs(KERNEL_DS);
P
Paul Mundt 已提交
550
		if (copy_from_user(&instruction, (void __user *)(regs->pc),
551
				   sizeof(instruction))) {
L
Linus Torvalds 已提交
552 553 554 555 556 557 558
			/* Argh. Fault on the instruction itself.
			   This should never happen non-SMP
			*/
			set_fs(oldfs);
			die("insn faulting in do_address_error", regs, 0);
		}

559
		unaligned_fixups_notify(current, instruction, regs);
560

561 562
		handle_unaligned_access(instruction, regs, &user_mem_access,
					0, address);
L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572
		set_fs(oldfs);
	}
}

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

S
Stuart Menefy 已提交
575
	/*
L
Linus Torvalds 已提交
576 577 578
	 * Safe guard if DSP mode is already enabled or we're lacking
	 * the DSP altogether.
	 */
579
	if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
		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 */

596 597 598
#ifdef CONFIG_CPU_SH2A
asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
				unsigned long r6, unsigned long r7,
S
Stuart Menefy 已提交
599
				struct pt_regs __regs)
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
{
	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 已提交
616 617
asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
				unsigned long r6, unsigned long r7,
S
Stuart Menefy 已提交
618
				struct pt_regs __regs)
T
Takashi YOSHII 已提交
619
{
S
Stuart Menefy 已提交
620
	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
T
Takashi YOSHII 已提交
621 622 623 624
	unsigned long error_code;
	struct task_struct *tsk = current;

#ifdef CONFIG_SH_FPU_EMU
625
	unsigned short inst = 0;
T
Takashi YOSHII 已提交
626 627
	int err;

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

S
Stuart Menefy 已提交
630
	err = do_fpu_inst(inst, regs);
T
Takashi YOSHII 已提交
631
	if (!err) {
632
		regs->pc += instruction_size(inst);
T
Takashi YOSHII 已提交
633 634 635 636 637 638 639
		return;
	}
	/* not a FPU inst. */
#endif

#ifdef CONFIG_SH_DSP
	/* Check if it's a DSP instruction */
640
	if (is_dsp_inst(regs)) {
T
Takashi YOSHII 已提交
641
		/* Enable DSP mode, and restart instruction. */
S
Stuart Menefy 已提交
642
		regs->sr |= SR_DSP;
643 644
		/* Save DSP mode */
		tsk->thread.dsp_status.status |= SR_DSP;
T
Takashi YOSHII 已提交
645 646 647 648
		return;
	}
#endif

649
	error_code = lookup_exception_vector();
650

T
Takashi YOSHII 已提交
651 652
	local_irq_enable();
	force_sig(SIGILL, tsk);
S
Stuart Menefy 已提交
653
	die_if_no_fixup("reserved instruction", regs, error_code);
T
Takashi YOSHII 已提交
654 655 656
}

#ifdef CONFIG_SH_FPU_EMU
657
static int emulate_branch(unsigned short inst, struct pt_regs *regs)
T
Takashi YOSHII 已提交
658 659 660 661 662 663 664 665 666 667 668 669
{
	/*
	 * 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;
	 */
670 671 672 673 674 675
	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 已提交
676 677 678 679
		regs->pc += SH_PC_8BIT_OFFSET(inst);
		return 0;
	}

680
	if ((inst & 0xe000) == 0xa000) {	/* bra, bsr */
T
Takashi YOSHII 已提交
681 682 683 684
		regs->pc += SH_PC_12BIT_OFFSET(inst);
		return 0;
	}

685
	if ((inst & 0xf0df) == 0x0003) {	/* braf, bsrf */
T
Takashi YOSHII 已提交
686 687 688 689
		regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
		return 0;
	}

690
	if ((inst & 0xf0df) == 0x400b) {	/* jmp, jsr */
T
Takashi YOSHII 已提交
691 692 693 694
		regs->pc = regs->regs[(inst & 0x0f00) >> 8];
		return 0;
	}

695
	if ((inst & 0xffff) == 0x000b) {	/* rts */
T
Takashi YOSHII 已提交
696 697 698 699 700 701 702 703 704 705
		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 已提交
706
				struct pt_regs __regs)
T
Takashi YOSHII 已提交
707
{
S
Stuart Menefy 已提交
708
	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
P
Paul Mundt 已提交
709
	unsigned long inst;
T
Takashi YOSHII 已提交
710
	struct task_struct *tsk = current;
C
Chris Smith 已提交
711 712 713 714

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

T
Takashi YOSHII 已提交
715
#ifdef CONFIG_SH_FPU_EMU
S
Stuart Menefy 已提交
716 717 718 719
	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 已提交
720 721 722 723 724 725
			return;
		/* fault in branch.*/
	}
	/* not a FPU inst. */
#endif

726
	inst = lookup_exception_vector();
727

T
Takashi YOSHII 已提交
728 729
	local_irq_enable();
	force_sig(SIGILL, tsk);
P
Paul Mundt 已提交
730
	die_if_no_fixup("illegal slot instruction", regs, inst);
T
Takashi YOSHII 已提交
731
}
L
Linus Torvalds 已提交
732 733 734

asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
				   unsigned long r6, unsigned long r7,
S
Stuart Menefy 已提交
735
				   struct pt_regs __regs)
L
Linus Torvalds 已提交
736
{
S
Stuart Menefy 已提交
737
	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
L
Linus Torvalds 已提交
738
	long ex;
739

740
	ex = lookup_exception_vector();
S
Stuart Menefy 已提交
741
	die_if_kernel("exception", regs, ex);
L
Linus Torvalds 已提交
742 743
}

744
void per_cpu_trap_init(void)
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753 754 755
{
	extern void *vbr_base;

	/* 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");
M
Magnus Damm 已提交
756 757 758

	/* disable exception blocking now when the vbr has been setup */
	clear_bl_bit();
L
Linus Torvalds 已提交
759 760
}

761
void *set_exception_table_vec(unsigned int vec, void *handler)
L
Linus Torvalds 已提交
762 763
{
	extern void *exception_handling_table[];
764
	void *old_handler;
765

766 767 768 769
	old_handler = exception_handling_table[vec];
	exception_handling_table[vec] = handler;
	return old_handler;
}
L
Linus Torvalds 已提交
770

771 772 773 774
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 已提交
775

T
Takashi YOSHII 已提交
776 777 778 779 780 781 782
#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.
	 */
783 784 785
	set_exception_table_evt(0x800, do_reserved_inst);
	set_exception_table_evt(0x820, do_illegal_slot_inst);
#elif defined(CONFIG_SH_FPU)
P
Paul Mundt 已提交
786 787
	set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
	set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
L
Linus Torvalds 已提交
788
#endif
789 790

#ifdef CONFIG_CPU_SH2
791
	set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
792 793 794 795
#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 已提交
796 797 798
#ifdef CONFIG_SH_FPU
	set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
#endif
799
#endif
800

P
Peter Griffin 已提交
801
#ifdef TRAP_UBC
802
	set_exception_table_vec(TRAP_UBC, breakpoint_trap_handler);
P
Peter Griffin 已提交
803
#endif
L
Linus Torvalds 已提交
804
}