traps.c 21.1 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>
28
#include <linux/irq.h>
Linus Torvalds's avatar
Linus Torvalds committed
29

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

41

42
43
44
45
46
47
48
static const char *handler[]= {
	"prefetch abort",
	"data abort",
	"address exception",
	"interrupt",
	"undefined instruction",
};
Linus Torvalds's avatar
Linus Torvalds committed
49

50
51
void *vectors_page;

Linus Torvalds's avatar
Linus Torvalds committed
52
53
54
55
56
57
58
59
60
61
62
#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

63
static void dump_mem(const char *, const char *, unsigned long, unsigned long);
64
65

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

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

77
#ifndef CONFIG_ARM_UNWIND
Linus Torvalds's avatar
Linus Torvalds committed
78
79
80
81
82
83
84
/*
 * 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)
{
85
86
	if (sp < PAGE_OFFSET ||
	    (sp > (unsigned long)high_memory && high_memory != NULL))
Linus Torvalds's avatar
Linus Torvalds committed
87
88
89
90
		return -EFAULT;

	return 0;
}
91
#endif
Linus Torvalds's avatar
Linus Torvalds committed
92
93
94
95

/*
 * Dump out the contents of some memory nicely...
 */
96
97
static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
		     unsigned long top)
Linus Torvalds's avatar
Linus Torvalds committed
98
{
99
	unsigned long first;
Linus Torvalds's avatar
Linus Torvalds committed
100
101
102
103
104
105
106
107
108
109
110
	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);

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

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

117
118
		memset(str, ' ', sizeof(str));
		str[sizeof(str) - 1] = '\0';
Linus Torvalds's avatar
Linus Torvalds committed
119

120
121
122
123
124
125
126
		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
127
128
			}
		}
129
		printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
Linus Torvalds's avatar
Linus Torvalds committed
130
131
132
133
134
	}

	set_fs(fs);
}

135
static void dump_instr(const char *lvl, struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
136
137
138
139
140
{
	unsigned long addr = instruction_pointer(regs);
	const int thumb = thumb_mode(regs);
	const int width = thumb ? 4 : 8;
	mm_segment_t fs;
141
	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
Linus Torvalds's avatar
Linus Torvalds committed
142
143
144
145
146
147
148
149
150
151
	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);

152
	for (i = -4; i < 1 + !!thumb; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
153
154
155
156
157
158
159
160
		unsigned int val, bad;

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

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

	set_fs(fs);
}

173
174
175
176
177
178
#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
179
180
static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
{
181
	unsigned int fp, mode;
Linus Torvalds's avatar
Linus Torvalds committed
182
183
184
	int ok = 1;

	printk("Backtrace: ");
185
186
187
188
189

	if (!tsk)
		tsk = current;

	if (regs) {
190
		fp = frame_pointer(regs);
191
192
193
194
195
196
197
198
199
		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
200
	if (!fp) {
201
		pr_cont("no frame pointer");
Linus Torvalds's avatar
Linus Torvalds committed
202
203
		ok = 0;
	} else if (verify_stack(fp)) {
204
		pr_cont("invalid frame pointer 0x%08x", fp);
Linus Torvalds's avatar
Linus Torvalds committed
205
		ok = 0;
Al Viro's avatar
Al Viro committed
206
	} else if (fp < (unsigned long)end_of_stack(tsk))
207
208
		pr_cont("frame pointer underflow");
	pr_cont("\n");
Linus Torvalds's avatar
Linus Torvalds committed
209
210

	if (ok)
211
		c_backtrace(fp, mode);
Linus Torvalds's avatar
Linus Torvalds committed
212
}
213
#endif
Linus Torvalds's avatar
Linus Torvalds committed
214
215
216

void show_stack(struct task_struct *tsk, unsigned long *sp)
{
217
	dump_backtrace(NULL, tsk);
Linus Torvalds's avatar
Linus Torvalds committed
218
219
220
	barrier();
}

221
222
223
224
225
226
227
228
229
230
#ifdef CONFIG_PREEMPT
#define S_PREEMPT " PREEMPT"
#else
#define S_PREEMPT ""
#endif
#ifdef CONFIG_SMP
#define S_SMP " SMP"
#else
#define S_SMP ""
#endif
231
232
233
234
235
#ifdef CONFIG_THUMB2_KERNEL
#define S_ISA " THUMB2"
#else
#define S_ISA " ARM"
#endif
236

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

243
244
	pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP S_ISA "\n",
	         str, err, ++die_counter);
Russell King's avatar
Russell King committed
245
246
247
248

	/* 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)
249
		return 1;
Russell King's avatar
Russell King committed
250

Linus Torvalds's avatar
Linus Torvalds committed
251
	print_modules();
Russell King's avatar
Russell King committed
252
	__show_regs(regs);
253
254
	pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
		 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), end_of_stack(tsk));
Linus Torvalds's avatar
Linus Torvalds committed
255
256

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

263
	return 0;
Russell King's avatar
Russell King committed
264
}
Linus Torvalds's avatar
Linus Torvalds committed
265

266
267
268
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
269

270
static unsigned long oops_begin(void)
Russell King's avatar
Russell King committed
271
{
272
273
	int cpu;
	unsigned long flags;
Russell King's avatar
Russell King committed
274

275
276
	oops_enter();

277
278
279
280
281
282
283
284
285
286
287
	/* 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
288
	console_verbose();
Russell King's avatar
Russell King committed
289
	bust_spinlocks(1);
290
291
	return flags;
}
Russell King's avatar
Russell King committed
292

293
294
295
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
296
297
		crash_kexec(regs);

Linus Torvalds's avatar
Linus Torvalds committed
298
	bust_spinlocks(0);
299
	die_owner = -1;
300
	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
301
302
303
304
305
	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
306
	oops_exit();
Russell King's avatar
Russell King committed
307

308
309
	if (in_interrupt())
		panic("Fatal exception in interrupt");
Horms's avatar
Horms committed
310
	if (panic_on_oops)
311
		panic("Fatal exception");
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	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
334
335
}

336
337
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
338
339
340
341
342
343
344
345
346
347
348
{
	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);
	}
}

349
350
351
352
353
#ifdef CONFIG_GENERIC_BUG

int is_valid_bugaddr(unsigned long pc)
{
#ifdef CONFIG_THUMB2_KERNEL
354
355
	u16 bkpt;
	u16 insn = __opcode_to_mem_thumb16(BUG_INSTR_VALUE);
356
#else
357
358
	u32 bkpt;
	u32 insn = __opcode_to_mem_arm(BUG_INSTR_VALUE);
359
360
361
362
363
#endif

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

364
	return bkpt == insn;
365
366
367
368
}

#endif

Linus Torvalds's avatar
Linus Torvalds committed
369
static LIST_HEAD(undef_hook);
370
static DEFINE_RAW_SPINLOCK(undef_lock);
Linus Torvalds's avatar
Linus Torvalds committed
371
372
373

void register_undef_hook(struct undef_hook *hook)
{
374
375
	unsigned long flags;

376
	raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
377
	list_add(&hook->node, &undef_hook);
378
	raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
379
380
381
382
}

void unregister_undef_hook(struct undef_hook *hook)
{
383
384
	unsigned long flags;

385
	raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
386
	list_del(&hook->node);
387
	raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
388
389
}

390
391
392
393
394
395
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;

396
	raw_spin_lock_irqsave(&undef_lock, flags);
397
398
399
400
	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;
401
	raw_spin_unlock_irqrestore(&undef_lock, flags);
402
403
404
405

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

406
asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
407
408
409
410
411
412
{
	unsigned int instr;
	siginfo_t info;
	void __user *pc;

	pc = (void __user *)instruction_pointer(regs);
413
414

	if (processor_mode(regs) == SVC_MODE) {
415
416
#ifdef CONFIG_THUMB2_KERNEL
		if (thumb_mode(regs)) {
417
			instr = __mem_to_opcode_thumb16(((u16 *)pc)[0]);
418
			if (is_wide_instruction(instr)) {
419
420
421
				u16 inst2;
				inst2 = __mem_to_opcode_thumb16(((u16 *)pc)[1]);
				instr = __opcode_thumb32_compose(instr, inst2);
422
423
424
			}
		} else
#endif
425
			instr = __mem_to_opcode_arm(*(u32 *) pc);
426
	} else if (thumb_mode(regs)) {
427
428
		if (get_user(instr, (u16 __user *)pc))
			goto die_sig;
429
		instr = __mem_to_opcode_thumb16(instr);
430
431
		if (is_wide_instruction(instr)) {
			unsigned int instr2;
432
433
			if (get_user(instr2, (u16 __user *)pc+1))
				goto die_sig;
434
435
			instr2 = __mem_to_opcode_thumb16(instr2);
			instr = __opcode_thumb32_compose(instr, instr2);
436
		}
437
438
439
	} else {
		if (get_user(instr, (u32 __user *)pc))
			goto die_sig;
440
		instr = __mem_to_opcode_arm(instr);
Linus Torvalds's avatar
Linus Torvalds committed
441
442
	}

443
444
	if (call_undef_hook(regs, instr) == 0)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
445

446
die_sig:
Linus Torvalds's avatar
Linus Torvalds committed
447
448
#ifdef CONFIG_DEBUG_USER
	if (user_debug & UDBG_UNDEFINED) {
449
		pr_info("%s (%d): undefined instruction: pc=%p\n",
450
			current->comm, task_pid_nr(current), pc);
451
		__show_regs(regs);
452
		dump_instr(KERN_INFO, regs);
Linus Torvalds's avatar
Linus Torvalds committed
453
454
455
456
457
458
459
460
	}
#endif

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

461
	arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6);
Linus Torvalds's avatar
Linus Torvalds committed
462
463
}

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Handle FIQ similarly to NMI on x86 systems.
 *
 * The runtime environment for NMIs is extremely restrictive
 * (NMIs can pre-empt critical sections meaning almost all locking is
 * forbidden) meaning this default FIQ handling must only be used in
 * circumstances where non-maskability improves robustness, such as
 * watchdog or debug logic.
 *
 * This handler is not appropriate for general purpose use in drivers
 * platform code and can be overrideen using set_fiq_handler.
 */
asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
{
	struct pt_regs *old_regs = set_irq_regs(regs);

	nmi_enter();

	/* nop. FIQ handlers for special arch/arm features can be added here. */

	nmi_exit();

	set_irq_regs(old_regs);
}

Linus Torvalds's avatar
Linus Torvalds committed
489
490
491
492
493
494
/*
 * 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...
 */
495
asmlinkage void bad_mode(struct pt_regs *regs, int reason)
Linus Torvalds's avatar
Linus Torvalds committed
496
497
498
{
	console_verbose();

499
	pr_crit("Bad mode in %s handler detected\n", handler[reason]);
Linus Torvalds's avatar
Linus Torvalds committed
500
501
502
503
504
505
506
507
508
509

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

static int bad_syscall(int n, struct pt_regs *regs)
{
	siginfo_t info;

510
511
	if ((current->personality & PER_MASK) != PER_LINUX) {
		send_sig(SIGSEGV, current, 1);
Linus Torvalds's avatar
Linus Torvalds committed
512
513
514
515
516
		return regs->ARM_r0;
	}

#ifdef CONFIG_DEBUG_USER
	if (user_debug & UDBG_SYSCALL) {
517
		pr_err("[%d] %s: obsolete system call %08x.\n",
518
			task_pid_nr(current), current->comm, n);
519
		dump_instr(KERN_ERR, regs);
Linus Torvalds's avatar
Linus Torvalds committed
520
521
522
523
524
525
526
527
528
	}
#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);

529
	arm_notify_die("Oops - bad syscall", regs, &info, n, 0);
Linus Torvalds's avatar
Linus Torvalds committed
530
531
532
533

	return regs->ARM_r0;
}

534
static inline int
535
536
537
538
539
__do_cache_op(unsigned long start, unsigned long end)
{
	int ret;

	do {
540
541
		unsigned long chunk = min(PAGE_SIZE, end - start);

542
543
		if (fatal_signal_pending(current))
			return 0;
544
545
546
547
548
549
550
551
552
553
554
555

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

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

	return 0;
}

556
static inline int
Linus Torvalds's avatar
Linus Torvalds committed
557
558
559
do_cache_op(unsigned long start, unsigned long end, int flags)
{
	if (end < start || flags)
560
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
561

562
563
	if (!access_ok(VERIFY_READ, start, end - start))
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
564

565
	return __do_cache_op(start, end);
Linus Torvalds's avatar
Linus Torvalds committed
566
567
568
569
570
571
572
573
574
575
576
}

/*
 * 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;

577
	if ((no >> 16) != (__ARM_NR_BASE>> 16))
Linus Torvalds's avatar
Linus Torvalds committed
578
579
580
581
582
583
584
585
586
		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;

587
		arm_notify_die("branch through zero", regs, &info, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
		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):
610
		return do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
Linus Torvalds's avatar
Linus Torvalds committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624

	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):
625
		set_tls(regs->ARM_r0);
Linus Torvalds's avatar
Linus Torvalds committed
626
627
		return 0;

628
629
630
631
632
633
634
635
636
637
638
639
#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.
	 */
640
	case NR(cmpxchg):
641
	for (;;) {
642
643
644
645
646
647
		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;
648
		spinlock_t *ptl;
649
650

		regs->ARM_cpsr &= ~PSR_C_BIT;
651
		down_read(&mm->mmap_sem);
652
653
654
655
656
657
		pgd = pgd_offset(mm, addr);
		if (!pgd_present(*pgd))
			goto bad_access;
		pmd = pmd_offset(pgd, addr);
		if (!pmd_present(*pmd))
			goto bad_access;
658
		pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
659
		if (!pte_present(*pte) || !pte_write(*pte) || !pte_dirty(*pte)) {
660
			pte_unmap_unlock(pte, ptl);
661
			goto bad_access;
662
		}
663
664
665
666
667
668
		val = *(unsigned long *)addr;
		val -= regs->ARM_r0;
		if (val == 0) {
			*(unsigned long *)addr = regs->ARM_r1;
			regs->ARM_cpsr |= PSR_C_BIT;
		}
669
670
		pte_unmap_unlock(pte, ptl);
		up_read(&mm->mmap_sem);
671
672
673
		return val;

		bad_access:
674
		up_read(&mm->mmap_sem);
675
		/* simulate a write access fault */
676
677
678
679
		do_DataAbort(addr, 15 + (1 << 11), regs);
	}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
680
681
682
683
684
	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.  */
685
		if ((no & 0xffff) <= 0x7ff)
Linus Torvalds's avatar
Linus Torvalds committed
686
687
688
689
690
691
692
693
694
			return -ENOSYS;
		break;
	}
#ifdef CONFIG_DEBUG_USER
	/*
	 * experience shows that these seem to indicate that
	 * something catastrophic has happened
	 */
	if (user_debug & UDBG_SYSCALL) {
695
		pr_err("[%d] %s: arm syscall %d\n",
696
		       task_pid_nr(current), current->comm, no);
697
		dump_instr("", regs);
Linus Torvalds's avatar
Linus Torvalds committed
698
		if (user_mode(regs)) {
Russell King's avatar
Russell King committed
699
			__show_regs(regs);
700
			c_backtrace(frame_pointer(regs), processor_mode(regs));
Linus Torvalds's avatar
Linus Torvalds committed
701
702
703
704
705
706
707
708
709
		}
	}
#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);

710
	arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
Linus Torvalds's avatar
Linus Torvalds committed
711
712
713
	return 0;
}

714
#ifdef CONFIG_TLS_REG_EMUL
715
716
717

/*
 * We might be running on an ARMv6+ processor which should have the TLS
718
719
720
721
 * 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.
722
723
724
725
726
727
728
 */

static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
{
	int reg = (instr >> 12) & 15;
	if (reg == 15)
		return 1;
729
	regs->uregs[reg] = current_thread_info()->tp_value[0];
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
	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
752
753
754
755
756
757
758
759
760
761
762
763
/*
 * 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) {
764
765
		pr_err("[%d] %s: bad data abort: code %d instr 0x%08lx\n",
		       task_pid_nr(current), current->comm, code, instr);
766
		dump_instr(KERN_ERR, regs);
Linus Torvalds's avatar
Linus Torvalds committed
767
768
769
770
771
772
773
774
775
		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;

776
	arm_notify_die("unknown data abort code", regs, &info, instr, 0);
Linus Torvalds's avatar
Linus Torvalds committed
777
778
779
780
}

void __readwrite_bug(const char *fn)
{
781
	pr_err("%s called, but not implemented\n", fn);
Linus Torvalds's avatar
Linus Torvalds committed
782
783
784
785
	BUG();
}
EXPORT_SYMBOL(__readwrite_bug);

786
void __pte_error(const char *file, int line, pte_t pte)
Linus Torvalds's avatar
Linus Torvalds committed
787
{
788
	pr_err("%s:%d: bad pte %08llx.\n", file, line, (long long)pte_val(pte));
Linus Torvalds's avatar
Linus Torvalds committed
789
790
}

791
void __pmd_error(const char *file, int line, pmd_t pmd)
Linus Torvalds's avatar
Linus Torvalds committed
792
{
793
	pr_err("%s:%d: bad pmd %08llx.\n", file, line, (long long)pmd_val(pmd));
Linus Torvalds's avatar
Linus Torvalds committed
794
795
}

796
void __pgd_error(const char *file, int line, pgd_t pgd)
Linus Torvalds's avatar
Linus Torvalds committed
797
{
798
	pr_err("%s:%d: bad pgd %08llx.\n", file, line, (long long)pgd_val(pgd));
Linus Torvalds's avatar
Linus Torvalds committed
799
800
801
802
}

asmlinkage void __div0(void)
{
803
	pr_err("Division by zero in kernel.\n");
Linus Torvalds's avatar
Linus Torvalds committed
804
805
806
807
808
809
810
811
812
813
814
815
816
817
	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
818
819
820
821
{
	return;
}

822
823
#ifdef CONFIG_KUSER_HELPERS
static void __init kuser_init(void *vectors)
824
{
825
826
827
828
829
	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);

830
831
832
833
834
	/*
	 * vectors + 0xfe0 = __kuser_get_tls
	 * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
	 */
	if (tls_emu || has_tls_reg)
835
836
837
		memcpy(vectors + 0xfe0, vectors + 0xfe8, 4);
}
#else
838
static inline void __init kuser_init(void *vectors)
839
{
840
}
841
#endif
842

843
void __init early_trap_init(void *vectors_base)
Linus Torvalds's avatar
Linus Torvalds committed
844
{
845
#ifndef CONFIG_CPU_V7M
846
	unsigned long vectors = (unsigned long)vectors_base;
847
848
	extern char __stubs_start[], __stubs_end[];
	extern char __vectors_start[], __vectors_end[];
Russell King's avatar
Russell King committed
849
	unsigned i;
Linus Torvalds's avatar
Linus Torvalds committed
850

851
852
	vectors_page = vectors_base;

Russell King's avatar
Russell King committed
853
854
855
856
857
858
859
860
861
	/*
	 * 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;

862
	/*
863
864
865
	 * 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.
866
	 */
867
	memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
Russell King's avatar
Russell King committed
868
	memcpy((void *)vectors + 0x1000, __stubs_start, __stubs_end - __stubs_start);
869

870
	kuser_init(vectors_base);
871

Russell King's avatar
Russell King committed
872
	flush_icache_range(vectors, vectors + PAGE_SIZE * 2);
873
874
875
876
877
878
879
#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
880
}