traps.c 20.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 *  linux/arch/arm/kernel/traps.c
 *
4
 *  Copyright (C) 1995-2009 Russell King
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16 17
 *  Fragments that appear the same as linux/arch/i386/kernel/traps.c (C) Linus Torvalds
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  'traps.c' handles hardware exceptions after we have saved some state in
 *  'linux/arch/arm/lib/traps.S'.  Mostly a debugging aid, but will probably
 *  kill the offending process.
 */
#include <linux/signal.h>
#include <linux/personality.h>
#include <linux/kallsyms.h>
Russell King's avatar
Russell King committed
18 19
#include <linux/spinlock.h>
#include <linux/uaccess.h>
20
#include <linux/hardirq.h>
Russell King's avatar
Russell King committed
21 22 23
#include <linux/kdebug.h>
#include <linux/module.h>
#include <linux/kexec.h>
24
#include <linux/bug.h>
Russell King's avatar
Russell King committed
25
#include <linux/delay.h>
Linus Torvalds's avatar
Linus Torvalds committed
26
#include <linux/init.h>
27
#include <linux/sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
28

Arun Sharma's avatar
Arun Sharma committed
29
#include <linux/atomic.h>
Linus Torvalds's avatar
Linus Torvalds committed
30
#include <asm/cacheflush.h>
31
#include <asm/exception.h>
Linus Torvalds's avatar
Linus Torvalds committed
32 33
#include <asm/unistd.h>
#include <asm/traps.h>
34
#include <asm/unwind.h>
35
#include <asm/tls.h>
36
#include <asm/system_misc.h>
Linus Torvalds's avatar
Linus Torvalds committed
37

38 39 40 41 42 43 44
static const char *handler[]= {
	"prefetch abort",
	"data abort",
	"address exception",
	"interrupt",
	"undefined instruction",
};
Linus Torvalds's avatar
Linus Torvalds committed
45

46 47
void *vectors_page;

Linus Torvalds's avatar
Linus Torvalds committed
48 49 50 51 52 53 54 55 56 57 58
#ifdef CONFIG_DEBUG_USER
unsigned int user_debug;

static int __init user_debug_setup(char *str)
{
	get_option(&str, &user_debug);
	return 1;
}
__setup("user_debug=", user_debug_setup);
#endif

59
static void dump_mem(const char *, const char *, unsigned long, unsigned long);
60 61

void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame)
Linus Torvalds's avatar
Linus Torvalds committed
62 63
{
#ifdef CONFIG_KALLSYMS
64
	printk("[<%08lx>] (%pS) from [<%08lx>] (%pS)\n", where, (void *)where, from, (void *)from);
Linus Torvalds's avatar
Linus Torvalds committed
65 66 67
#else
	printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
#endif
68 69

	if (in_exception_text(where))
70
		dump_mem("", "Exception stack", frame + 4, frame + 4 + sizeof(struct pt_regs));
Linus Torvalds's avatar
Linus Torvalds committed
71 72
}

73
#ifndef CONFIG_ARM_UNWIND
Linus Torvalds's avatar
Linus Torvalds committed
74 75 76 77 78 79 80
/*
 * Stack pointers should always be within the kernels view of
 * physical memory.  If it is not there, then we can't dump
 * out any information relating to the stack.
 */
static int verify_stack(unsigned long sp)
{
81 82
	if (sp < PAGE_OFFSET ||
	    (sp > (unsigned long)high_memory && high_memory != NULL))
Linus Torvalds's avatar
Linus Torvalds committed
83 84 85 86
		return -EFAULT;

	return 0;
}
87
#endif
Linus Torvalds's avatar
Linus Torvalds committed
88 89 90 91

/*
 * Dump out the contents of some memory nicely...
 */
92 93
static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
		     unsigned long top)
Linus Torvalds's avatar
Linus Torvalds committed
94
{
95
	unsigned long first;
Linus Torvalds's avatar
Linus Torvalds committed
96 97 98 99 100 101 102 103 104 105 106
	mm_segment_t fs;
	int i;

	/*
	 * We need to switch to kernel mode so that we can use __get_user
	 * to safely read from kernel space.  Note that we now dump the
	 * code first, just in case the backtrace kills us.
	 */
	fs = get_fs();
	set_fs(KERNEL_DS);

107
	printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top);
Linus Torvalds's avatar
Linus Torvalds committed
108

109 110 111
	for (first = bottom & ~31; first < top; first += 32) {
		unsigned long p;
		char str[sizeof(" 12345678") * 8 + 1];
Linus Torvalds's avatar
Linus Torvalds committed
112

113 114
		memset(str, ' ', sizeof(str));
		str[sizeof(str) - 1] = '\0';
Linus Torvalds's avatar
Linus Torvalds committed
115

116 117 118 119 120 121 122
		for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
			if (p >= bottom && p < top) {
				unsigned long val;
				if (__get_user(val, (unsigned long *)p) == 0)
					sprintf(str + i * 9, " %08lx", val);
				else
					sprintf(str + i * 9, " ????????");
Linus Torvalds's avatar
Linus Torvalds committed
123 124
			}
		}
125
		printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
Linus Torvalds's avatar
Linus Torvalds committed
126 127 128 129 130
	}

	set_fs(fs);
}

131
static void dump_instr(const char *lvl, struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
132 133 134 135 136
{
	unsigned long addr = instruction_pointer(regs);
	const int thumb = thumb_mode(regs);
	const int width = thumb ? 4 : 8;
	mm_segment_t fs;
137
	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140 141 142 143 144 145 146 147
	int i;

	/*
	 * We need to switch to kernel mode so that we can use __get_user
	 * to safely read from kernel space.  Note that we now dump the
	 * code first, just in case the backtrace kills us.
	 */
	fs = get_fs();
	set_fs(KERNEL_DS);

148
	for (i = -4; i < 1 + !!thumb; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152 153 154 155 156
		unsigned int val, bad;

		if (thumb)
			bad = __get_user(val, &((u16 *)addr)[i]);
		else
			bad = __get_user(val, &((u32 *)addr)[i]);

		if (!bad)
157 158
			p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ",
					width, val);
Linus Torvalds's avatar
Linus Torvalds committed
159
		else {
160
			p += sprintf(p, "bad PC value");
Linus Torvalds's avatar
Linus Torvalds committed
161 162 163
			break;
		}
	}
164
	printk("%sCode: %s\n", lvl, str);
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168

	set_fs(fs);
}

169 170 171 172 173 174
#ifdef CONFIG_ARM_UNWIND
static inline void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
{
	unwind_backtrace(regs, tsk);
}
#else
Linus Torvalds's avatar
Linus Torvalds committed
175 176
static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
{
177
	unsigned int fp, mode;
Linus Torvalds's avatar
Linus Torvalds committed
178 179 180
	int ok = 1;

	printk("Backtrace: ");
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

	if (!tsk)
		tsk = current;

	if (regs) {
		fp = regs->ARM_fp;
		mode = processor_mode(regs);
	} else if (tsk != current) {
		fp = thread_saved_fp(tsk);
		mode = 0x10;
	} else {
		asm("mov %0, fp" : "=r" (fp) : : "cc");
		mode = 0x10;
	}

Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199 200 201
	if (!fp) {
		printk("no frame pointer");
		ok = 0;
	} else if (verify_stack(fp)) {
		printk("invalid frame pointer 0x%08x", fp);
		ok = 0;
Al Viro's avatar
Al Viro committed
202
	} else if (fp < (unsigned long)end_of_stack(tsk))
Linus Torvalds's avatar
Linus Torvalds committed
203 204 205 206
		printk("frame pointer underflow");
	printk("\n");

	if (ok)
207
		c_backtrace(fp, mode);
Linus Torvalds's avatar
Linus Torvalds committed
208
}
209
#endif
Linus Torvalds's avatar
Linus Torvalds committed
210 211 212

void show_stack(struct task_struct *tsk, unsigned long *sp)
{
213
	dump_backtrace(NULL, tsk);
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216
	barrier();
}

217 218 219 220 221 222 223 224 225 226
#ifdef CONFIG_PREEMPT
#define S_PREEMPT " PREEMPT"
#else
#define S_PREEMPT ""
#endif
#ifdef CONFIG_SMP
#define S_SMP " SMP"
#else
#define S_SMP ""
#endif
227 228 229 230 231
#ifdef CONFIG_THUMB2_KERNEL
#define S_ISA " THUMB2"
#else
#define S_ISA " ARM"
#endif
232

233
static int __die(const char *str, int err, struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
234
{
235
	struct task_struct *tsk = current;
Linus Torvalds's avatar
Linus Torvalds committed
236
	static int die_counter;
Russell King's avatar
Russell King committed
237
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
238

239 240
	printk(KERN_EMERG "Internal error: %s: %x [#%d]" S_PREEMPT S_SMP
	       S_ISA "\n", str, err, ++die_counter);
Russell King's avatar
Russell King committed
241 242 243 244

	/* trap and error numbers are mostly meaningless on ARM */
	ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, SIGSEGV);
	if (ret == NOTIFY_STOP)
245
		return 1;
Russell King's avatar
Russell King committed
246

Linus Torvalds's avatar
Linus Torvalds committed
247
	print_modules();
Russell King's avatar
Russell King committed
248
	__show_regs(regs);
249
	printk(KERN_EMERG "Process %.*s (pid: %d, stack limit = 0x%p)\n",
250
		TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), end_of_stack(tsk));
Linus Torvalds's avatar
Linus Torvalds committed
251 252

	if (!user_mode(regs) || in_interrupt()) {
253
		dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp,
Al Viro's avatar
Al Viro committed
254
			 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
Linus Torvalds's avatar
Linus Torvalds committed
255
		dump_backtrace(regs, tsk);
256
		dump_instr(KERN_EMERG, regs);
Linus Torvalds's avatar
Linus Torvalds committed
257
	}
Russell King's avatar
Russell King committed
258

259
	return 0;
Russell King's avatar
Russell King committed
260
}
Linus Torvalds's avatar
Linus Torvalds committed
261

262 263 264
static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
static int die_owner = -1;
static unsigned int die_nest_count;
Russell King's avatar
Russell King committed
265

266
static unsigned long oops_begin(void)
Russell King's avatar
Russell King committed
267
{
268 269
	int cpu;
	unsigned long flags;
Russell King's avatar
Russell King committed
270

271 272
	oops_enter();

273 274 275 276 277 278 279 280 281 282 283
	/* racy, but better than risking deadlock. */
	raw_local_irq_save(flags);
	cpu = smp_processor_id();
	if (!arch_spin_trylock(&die_lock)) {
		if (cpu == die_owner)
			/* nested oops. should stop eventually */;
		else
			arch_spin_lock(&die_lock);
	}
	die_nest_count++;
	die_owner = cpu;
Russell King's avatar
Russell King committed
284
	console_verbose();
Russell King's avatar
Russell King committed
285
	bust_spinlocks(1);
286 287
	return flags;
}
Russell King's avatar
Russell King committed
288

289 290 291
static void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
{
	if (regs && kexec_should_crash(current))
Russell King's avatar
Russell King committed
292 293
		crash_kexec(regs);

Linus Torvalds's avatar
Linus Torvalds committed
294
	bust_spinlocks(0);
295
	die_owner = -1;
296
	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
297 298 299 300 301
	die_nest_count--;
	if (!die_nest_count)
		/* Nest count reaches zero, release the lock. */
		arch_spin_unlock(&die_lock);
	raw_local_irq_restore(flags);
Russell King's avatar
Russell King committed
302
	oops_exit();
303

304 305
	if (in_interrupt())
		panic("Fatal exception in interrupt");
306
	if (panic_on_oops)
307
		panic("Fatal exception");
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	if (signr)
		do_exit(signr);
}

/*
 * This function is protected against re-entrancy.
 */
void die(const char *str, struct pt_regs *regs, int err)
{
	enum bug_trap_type bug_type = BUG_TRAP_TYPE_NONE;
	unsigned long flags = oops_begin();
	int sig = SIGSEGV;

	if (!user_mode(regs))
		bug_type = report_bug(regs->ARM_pc, regs);
	if (bug_type != BUG_TRAP_TYPE_NONE)
		str = "Oops - BUG";

	if (__die(str, err, regs))
		sig = 0;

	oops_end(flags, regs, sig);
Linus Torvalds's avatar
Linus Torvalds committed
330 331
}

332 333
void arm_notify_die(const char *str, struct pt_regs *regs,
		struct siginfo *info, unsigned long err, unsigned long trap)
Linus Torvalds's avatar
Linus Torvalds committed
334 335 336 337 338 339 340 341 342 343 344
{
	if (user_mode(regs)) {
		current->thread.error_code = err;
		current->thread.trap_no = trap;

		force_sig_info(info->si_signo, info, current);
	} else {
		die(str, regs, err);
	}
}

345 346 347 348 349
#ifdef CONFIG_GENERIC_BUG

int is_valid_bugaddr(unsigned long pc)
{
#ifdef CONFIG_THUMB2_KERNEL
350 351
	u16 bkpt;
	u16 insn = __opcode_to_mem_thumb16(BUG_INSTR_VALUE);
352
#else
353 354
	u32 bkpt;
	u32 insn = __opcode_to_mem_arm(BUG_INSTR_VALUE);
355 356 357 358 359
#endif

	if (probe_kernel_address((unsigned *)pc, bkpt))
		return 0;

360
	return bkpt == insn;
361 362 363 364
}

#endif

Linus Torvalds's avatar
Linus Torvalds committed
365
static LIST_HEAD(undef_hook);
366
static DEFINE_RAW_SPINLOCK(undef_lock);
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369

void register_undef_hook(struct undef_hook *hook)
{
370 371
	unsigned long flags;

372
	raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
373
	list_add(&hook->node, &undef_hook);
374
	raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
375 376 377 378
}

void unregister_undef_hook(struct undef_hook *hook)
{
379 380
	unsigned long flags;

381
	raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
382
	list_del(&hook->node);
383
	raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
384 385
}

386 387 388 389 390 391
static int call_undef_hook(struct pt_regs *regs, unsigned int instr)
{
	struct undef_hook *hook;
	unsigned long flags;
	int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;

392
	raw_spin_lock_irqsave(&undef_lock, flags);
393 394 395 396
	list_for_each_entry(hook, &undef_hook, node)
		if ((instr & hook->instr_mask) == hook->instr_val &&
		    (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val)
			fn = hook->fn;
397
	raw_spin_unlock_irqrestore(&undef_lock, flags);
398 399 400 401

	return fn ? fn(regs, instr) : 1;
}

402
asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
403 404 405 406 407 408
{
	unsigned int instr;
	siginfo_t info;
	void __user *pc;

	pc = (void __user *)instruction_pointer(regs);
409 410

	if (processor_mode(regs) == SVC_MODE) {
411 412 413 414 415 416 417 418 419 420
#ifdef CONFIG_THUMB2_KERNEL
		if (thumb_mode(regs)) {
			instr = ((u16 *)pc)[0];
			if (is_wide_instruction(instr)) {
				instr <<= 16;
				instr |= ((u16 *)pc)[1];
			}
		} else
#endif
			instr = *(u32 *) pc;
421
	} else if (thumb_mode(regs)) {
422 423
		if (get_user(instr, (u16 __user *)pc))
			goto die_sig;
424 425
		if (is_wide_instruction(instr)) {
			unsigned int instr2;
426 427
			if (get_user(instr2, (u16 __user *)pc+1))
				goto die_sig;
428 429 430
			instr <<= 16;
			instr |= instr2;
		}
431 432
	} else if (get_user(instr, (u32 __user *)pc)) {
		goto die_sig;
Linus Torvalds's avatar
Linus Torvalds committed
433 434
	}

435 436
	if (call_undef_hook(regs, instr) == 0)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
437

438
die_sig:
Linus Torvalds's avatar
Linus Torvalds committed
439 440 441
#ifdef CONFIG_DEBUG_USER
	if (user_debug & UDBG_UNDEFINED) {
		printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
442
			current->comm, task_pid_nr(current), pc);
443
		dump_instr(KERN_INFO, regs);
Linus Torvalds's avatar
Linus Torvalds committed
444 445 446 447 448 449 450 451
	}
#endif

	info.si_signo = SIGILL;
	info.si_errno = 0;
	info.si_code  = ILL_ILLOPC;
	info.si_addr  = pc;

452
	arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6);
Linus Torvalds's avatar
Linus Torvalds committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466
}

asmlinkage void do_unexp_fiq (struct pt_regs *regs)
{
	printk("Hmm.  Unexpected FIQ received, but trying to continue\n");
	printk("You may have a hardware problem...\n");
}

/*
 * bad_mode handles the impossible case in the vectors.  If you see one of
 * these, then it's extremely serious, and could mean you have buggy hardware.
 * It never returns, and never tries to sync.  We hope that we can at least
 * dump out some state information...
 */
467
asmlinkage void bad_mode(struct pt_regs *regs, int reason)
Linus Torvalds's avatar
Linus Torvalds committed
468 469 470
{
	console_verbose();

471
	printk(KERN_CRIT "Bad mode in %s handler detected\n", handler[reason]);
Linus Torvalds's avatar
Linus Torvalds committed
472 473 474 475 476 477 478 479 480 481 482

	die("Oops - bad mode", regs, 0);
	local_irq_disable();
	panic("bad mode");
}

static int bad_syscall(int n, struct pt_regs *regs)
{
	struct thread_info *thread = current_thread_info();
	siginfo_t info;

483
	if ((current->personality & PER_MASK) != PER_LINUX &&
484
	    thread->exec_domain->handler) {
Linus Torvalds's avatar
Linus Torvalds committed
485 486 487 488 489 490 491
		thread->exec_domain->handler(n, regs);
		return regs->ARM_r0;
	}

#ifdef CONFIG_DEBUG_USER
	if (user_debug & UDBG_SYSCALL) {
		printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
492
			task_pid_nr(current), current->comm, n);
493
		dump_instr(KERN_ERR, regs);
Linus Torvalds's avatar
Linus Torvalds committed
494 495 496 497 498 499 500 501 502
	}
#endif

	info.si_signo = SIGILL;
	info.si_errno = 0;
	info.si_code  = ILL_ILLTRP;
	info.si_addr  = (void __user *)instruction_pointer(regs) -
			 (thumb_mode(regs) ? 2 : 4);

503
	arm_notify_die("Oops - bad syscall", regs, &info, n, 0);
Linus Torvalds's avatar
Linus Torvalds committed
504 505 506 507

	return regs->ARM_r0;
}

508
static inline int
509 510 511 512 513
__do_cache_op(unsigned long start, unsigned long end)
{
	int ret;

	do {
514 515
		unsigned long chunk = min(PAGE_SIZE, end - start);

516 517
		if (fatal_signal_pending(current))
			return 0;
518 519 520 521 522 523 524 525 526 527 528 529

		ret = flush_cache_user_range(start, start + chunk);
		if (ret)
			return ret;

		cond_resched();
		start += chunk;
	} while (start < end);

	return 0;
}

530
static inline int
Linus Torvalds's avatar
Linus Torvalds committed
531 532 533
do_cache_op(unsigned long start, unsigned long end, int flags)
{
	if (end < start || flags)
534
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
535

536 537
	if (!access_ok(VERIFY_READ, start, end - start))
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
538

539
	return __do_cache_op(start, end);
Linus Torvalds's avatar
Linus Torvalds committed
540 541 542 543 544 545 546 547 548 549 550
}

/*
 * Handle all unrecognised system calls.
 *  0x9f0000 - 0x9fffff are some more esoteric system calls
 */
#define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
asmlinkage int arm_syscall(int no, struct pt_regs *regs)
{
	siginfo_t info;

551
	if ((no >> 16) != (__ARM_NR_BASE>> 16))
Linus Torvalds's avatar
Linus Torvalds committed
552 553 554 555 556 557 558 559 560
		return bad_syscall(no, regs);

	switch (no & 0xffff) {
	case 0: /* branch through 0 */
		info.si_signo = SIGSEGV;
		info.si_errno = 0;
		info.si_code  = SEGV_MAPERR;
		info.si_addr  = NULL;

561
		arm_notify_die("branch through zero", regs, &info, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
		return 0;

	case NR(breakpoint): /* SWI BREAK_POINT */
		regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
		ptrace_break(current, regs);
		return regs->ARM_r0;

	/*
	 * Flush a region from virtual address 'r0' to virtual address 'r1'
	 * _exclusive_.  There is no alignment requirement on either address;
	 * user space does not need to know the hardware cache layout.
	 *
	 * r2 contains flags.  It should ALWAYS be passed as ZERO until it
	 * is defined to be something else.  For now we ignore it, but may
	 * the fires of hell burn in your belly if you break this rule. ;)
	 *
	 * (at a later date, we may want to allow this call to not flush
	 * various aspects of the cache.  Passing '0' will guarantee that
	 * everything necessary gets flushed to maintain consistency in
	 * the specified region).
	 */
	case NR(cacheflush):
584
		return do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
Linus Torvalds's avatar
Linus Torvalds committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598

	case NR(usr26):
		if (!(elf_hwcap & HWCAP_26BIT))
			break;
		regs->ARM_cpsr &= ~MODE32_BIT;
		return regs->ARM_r0;

	case NR(usr32):
		if (!(elf_hwcap & HWCAP_26BIT))
			break;
		regs->ARM_cpsr |= MODE32_BIT;
		return regs->ARM_r0;

	case NR(set_tls):
599
		set_tls(regs->ARM_r0);
Linus Torvalds's avatar
Linus Torvalds committed
600 601
		return 0;

602 603 604 605 606 607 608 609 610 611 612 613
#ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG
	/*
	 * Atomically store r1 in *r2 if *r2 is equal to r0 for user space.
	 * Return zero in r0 if *MEM was changed or non-zero if no exchange
	 * happened.  Also set the user C flag accordingly.
	 * If access permissions have to be fixed up then non-zero is
	 * returned and the operation has to be re-attempted.
	 *
	 * *NOTE*: This is a ghost syscall private to the kernel.  Only the
	 * __kuser_cmpxchg code in entry-armv.S should be aware of its
	 * existence.  Don't ever use this from user code.
	 */
614
	case NR(cmpxchg):
615
	for (;;) {
616 617 618 619 620 621
		extern void do_DataAbort(unsigned long addr, unsigned int fsr,
					 struct pt_regs *regs);
		unsigned long val;
		unsigned long addr = regs->ARM_r2;
		struct mm_struct *mm = current->mm;
		pgd_t *pgd; pmd_t *pmd; pte_t *pte;
622
		spinlock_t *ptl;
623 624

		regs->ARM_cpsr &= ~PSR_C_BIT;
625
		down_read(&mm->mmap_sem);
626 627 628 629 630 631
		pgd = pgd_offset(mm, addr);
		if (!pgd_present(*pgd))
			goto bad_access;
		pmd = pmd_offset(pgd, addr);
		if (!pmd_present(*pmd))
			goto bad_access;
632
		pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
633
		if (!pte_present(*pte) || !pte_write(*pte) || !pte_dirty(*pte)) {
634
			pte_unmap_unlock(pte, ptl);
635
			goto bad_access;
636
		}
637 638 639 640 641 642
		val = *(unsigned long *)addr;
		val -= regs->ARM_r0;
		if (val == 0) {
			*(unsigned long *)addr = regs->ARM_r1;
			regs->ARM_cpsr |= PSR_C_BIT;
		}
643 644
		pte_unmap_unlock(pte, ptl);
		up_read(&mm->mmap_sem);
645 646 647
		return val;

		bad_access:
648
		up_read(&mm->mmap_sem);
649
		/* simulate a write access fault */
650 651 652 653
		do_DataAbort(addr, 15 + (1 << 11), regs);
	}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
654 655 656 657 658
	default:
		/* Calls 9f00xx..9f07ff are defined to return -ENOSYS
		   if not implemented, rather than raising SIGILL.  This
		   way the calling program can gracefully determine whether
		   a feature is supported.  */
659
		if ((no & 0xffff) <= 0x7ff)
Linus Torvalds's avatar
Linus Torvalds committed
660 661 662 663 664 665 666 667 668 669
			return -ENOSYS;
		break;
	}
#ifdef CONFIG_DEBUG_USER
	/*
	 * experience shows that these seem to indicate that
	 * something catastrophic has happened
	 */
	if (user_debug & UDBG_SYSCALL) {
		printk("[%d] %s: arm syscall %d\n",
670
		       task_pid_nr(current), current->comm, no);
671
		dump_instr("", regs);
Linus Torvalds's avatar
Linus Torvalds committed
672
		if (user_mode(regs)) {
Russell King's avatar
Russell King committed
673
			__show_regs(regs);
Linus Torvalds's avatar
Linus Torvalds committed
674 675 676 677 678 679 680 681 682 683
			c_backtrace(regs->ARM_fp, processor_mode(regs));
		}
	}
#endif
	info.si_signo = SIGILL;
	info.si_errno = 0;
	info.si_code  = ILL_ILLTRP;
	info.si_addr  = (void __user *)instruction_pointer(regs) -
			 (thumb_mode(regs) ? 2 : 4);

684
	arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
Linus Torvalds's avatar
Linus Torvalds committed
685 686 687
	return 0;
}

688
#ifdef CONFIG_TLS_REG_EMUL
689 690 691

/*
 * We might be running on an ARMv6+ processor which should have the TLS
692 693 694 695
 * register but for some reason we can't use it, or maybe an SMP system
 * using a pre-ARMv6 processor (there are apparently a few prototypes like
 * that in existence) and therefore access to that register must be
 * emulated.
696 697 698 699 700 701 702
 */

static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
{
	int reg = (instr >> 12) & 15;
	if (reg == 15)
		return 1;
703
	regs->uregs[reg] = current_thread_info()->tp_value[0];
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	regs->ARM_pc += 4;
	return 0;
}

static struct undef_hook arm_mrc_hook = {
	.instr_mask	= 0x0fff0fff,
	.instr_val	= 0x0e1d0f70,
	.cpsr_mask	= PSR_T_BIT,
	.cpsr_val	= 0,
	.fn		= get_tp_trap,
};

static int __init arm_mrc_hook_init(void)
{
	register_undef_hook(&arm_mrc_hook);
	return 0;
}

late_initcall(arm_mrc_hook_init);

#endif

Linus Torvalds's avatar
Linus Torvalds committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
void __bad_xchg(volatile void *ptr, int size)
{
	printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
		__builtin_return_address(0), ptr, size);
	BUG();
}
EXPORT_SYMBOL(__bad_xchg);

/*
 * A data abort trap was taken, but we did not handle the instruction.
 * Try to abort the user program, or panic if it was the kernel.
 */
asmlinkage void
baddataabort(int code, unsigned long instr, struct pt_regs *regs)
{
	unsigned long addr = instruction_pointer(regs);
	siginfo_t info;

#ifdef CONFIG_DEBUG_USER
	if (user_debug & UDBG_BADABORT) {
		printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
747
			task_pid_nr(current), current->comm, code, instr);
748
		dump_instr(KERN_ERR, regs);
Linus Torvalds's avatar
Linus Torvalds committed
749 750 751 752 753 754 755 756 757
		show_pte(current->mm, addr);
	}
#endif

	info.si_signo = SIGILL;
	info.si_errno = 0;
	info.si_code  = ILL_ILLOPC;
	info.si_addr  = (void __user *)addr;

758
	arm_notify_die("unknown data abort code", regs, &info, instr, 0);
Linus Torvalds's avatar
Linus Torvalds committed
759 760 761 762 763 764 765 766 767
}

void __readwrite_bug(const char *fn)
{
	printk("%s called, but not implemented\n", fn);
	BUG();
}
EXPORT_SYMBOL(__readwrite_bug);

768
void __pte_error(const char *file, int line, pte_t pte)
Linus Torvalds's avatar
Linus Torvalds committed
769
{
770
	printk("%s:%d: bad pte %08llx.\n", file, line, (long long)pte_val(pte));
Linus Torvalds's avatar
Linus Torvalds committed
771 772
}

773
void __pmd_error(const char *file, int line, pmd_t pmd)
Linus Torvalds's avatar
Linus Torvalds committed
774
{
775
	printk("%s:%d: bad pmd %08llx.\n", file, line, (long long)pmd_val(pmd));
Linus Torvalds's avatar
Linus Torvalds committed
776 777
}

778
void __pgd_error(const char *file, int line, pgd_t pgd)
Linus Torvalds's avatar
Linus Torvalds committed
779
{
780
	printk("%s:%d: bad pgd %08llx.\n", file, line, (long long)pgd_val(pgd));
Linus Torvalds's avatar
Linus Torvalds committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
}

asmlinkage void __div0(void)
{
	printk("Division by zero in kernel.\n");
	dump_stack();
}
EXPORT_SYMBOL(__div0);

void abort(void)
{
	BUG();

	/* if that doesn't kill us, halt */
	panic("Oops failed to kill thread");
}
EXPORT_SYMBOL(abort);

void __init trap_init(void)
Jason Wessel's avatar
Jason Wessel committed
800 801 802 803
{
	return;
}

804 805
#ifdef CONFIG_KUSER_HELPERS
static void __init kuser_init(void *vectors)
806
{
807 808 809 810 811
	extern char __kuser_helper_start[], __kuser_helper_end[];
	int kuser_sz = __kuser_helper_end - __kuser_helper_start;

	memcpy(vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);

812 813 814 815 816
	/*
	 * vectors + 0xfe0 = __kuser_get_tls
	 * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
	 */
	if (tls_emu || has_tls_reg)
817 818 819 820 821
		memcpy(vectors + 0xfe0, vectors + 0xfe8, 4);
}
#else
static void __init kuser_init(void *vectors)
{
822
}
823
#endif
824

825
void __init early_trap_init(void *vectors_base)
Linus Torvalds's avatar
Linus Torvalds committed
826
{
827
#ifndef CONFIG_CPU_V7M
828
	unsigned long vectors = (unsigned long)vectors_base;
829 830
	extern char __stubs_start[], __stubs_end[];
	extern char __vectors_start[], __vectors_end[];
Russell King's avatar
Russell King committed
831
	unsigned i;
Linus Torvalds's avatar
Linus Torvalds committed
832

833 834
	vectors_page = vectors_base;

Russell King's avatar
Russell King committed
835 836 837 838 839 840 841 842 843
	/*
	 * Poison the vectors page with an undefined instruction.  This
	 * instruction is chosen to be undefined for both ARM and Thumb
	 * ISAs.  The Thumb version is an undefined instruction with a
	 * branch back to the undefined instruction.
	 */
	for (i = 0; i < PAGE_SIZE / sizeof(u32); i++)
		((u32 *)vectors_base)[i] = 0xe7fddef1;

844
	/*
845 846 847
	 * Copy the vectors, stubs and kuser helpers (in entry-armv.S)
	 * into the vector page, mapped at 0xffff0000, and ensure these
	 * are visible to the instruction stream.
848
	 */
849
	memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
Russell King's avatar
Russell King committed
850
	memcpy((void *)vectors + 0x1000, __stubs_start, __stubs_end - __stubs_start);
851

852
	kuser_init(vectors_base);
853

Russell King's avatar
Russell King committed
854
	flush_icache_range(vectors, vectors + PAGE_SIZE * 2);
Linus Torvalds's avatar
Linus Torvalds committed
855
	modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
856 857 858 859 860 861 862
#else /* ifndef CONFIG_CPU_V7M */
	/*
	 * on V7-M there is no need to copy the vector table to a dedicated
	 * memory area. The address is configurable and so a table in the kernel
	 * image can be used.
	 */
#endif
Linus Torvalds's avatar
Linus Torvalds committed
863
}