traps.c 13.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * arch/xtensa/kernel/traps.c
 *
 * Exception handling.
 *
 * Derived from code with the following copyrights:
 * Copyright (C) 1994 - 1999 by Ralf Baechle
 * Modified for R3000 by Paul M. Antoine, 1995, 1996
 * Complete output from die() by Ulf Carlsson, 1998
 * Copyright (C) 1999 Silicon Graphics, Inc.
 *
 * Essentially rewritten for the Xtensa architecture port.
 *
Max Filippov's avatar
Max Filippov committed
14
 * Copyright (C) 2001 - 2013 Tensilica Inc.
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
 * Chris Zankel	<chris@zankel.net>
 * Marc Gauthier<marc@tensilica.com, marc@alumni.uwaterloo.ca>
 * Kevin Chea
 *
 * 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.
 */

#include <linux/kernel.h>
27
#include <linux/sched/signal.h>
28
#include <linux/sched/debug.h>
29
#include <linux/sched/task_stack.h>
30
31
32
33
#include <linux/init.h>
#include <linux/module.h>
#include <linux/stringify.h>
#include <linux/kallsyms.h>
34
#include <linux/delay.h>
35
#include <linux/hardirq.h>
36
#include <linux/ratelimit.h>
37

Max Filippov's avatar
Max Filippov committed
38
#include <asm/stacktrace.h>
39
40
#include <asm/ptrace.h>
#include <asm/timex.h>
41
#include <linux/uaccess.h>
42
43
#include <asm/pgtable.h>
#include <asm/processor.h>
44
#include <asm/traps.h>
45
#include <asm/hw_breakpoint.h>
46
47
48
49
50
51
52
53

/*
 * Machine specific interrupt handlers
 */

extern void kernel_exception(void);
extern void user_exception(void);

54
extern void fast_illegal_instruction_user(void);
55
56
57
58
59
60
61
62
63
extern void fast_syscall_user(void);
extern void fast_alloca(void);
extern void fast_unaligned(void);
extern void fast_second_level_miss(void);
extern void fast_store_prohibited(void);
extern void fast_coprocessor(void);

extern void do_illegal_instruction (struct pt_regs*);
extern void do_interrupt (struct pt_regs*);
Max Filippov's avatar
Max Filippov committed
64
extern void do_nmi(struct pt_regs *);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
extern void do_unaligned_user (struct pt_regs*);
extern void do_multihit (struct pt_regs*, unsigned long);
extern void do_page_fault (struct pt_regs*, unsigned long);
extern void do_debug (struct pt_regs*);
extern void system_call (struct pt_regs*);

/*
 * The vector table must be preceded by a save area (which
 * implies it must be in RAM, unless one places RAM immediately
 * before a ROM and puts the vector at the start of the ROM (!))
 */

#define KRNL		0x01
#define USER		0x02

#define COPROCESSOR(x)							\
81
{ EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
82
83
84
85
86
87
88

typedef struct {
	int cause;
	int fast;
	void* handler;
} dispatch_init_table_t;

89
static dispatch_init_table_t __initdata dispatch_init_table[] = {
90

91
92
93
#ifdef CONFIG_USER_ABI_CALL0_PROBE
{ EXCCAUSE_ILLEGAL_INSTRUCTION,	USER,	   fast_illegal_instruction_user },
#endif
94
95
96
97
98
99
100
101
102
{ EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
{ EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
{ EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
/* EXCCAUSE_INSTRUCTION_FETCH unhandled */
/* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
{ EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
{ EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
/* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
/* EXCCAUSE_PRIVILEGED unhandled */
103
#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
104
#ifdef CONFIG_XTENSA_UNALIGNED_USER
105
{ EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
106
#endif
107
{ EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
108
{ EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
109
#endif
Johannes Weiner's avatar
Johannes Weiner committed
110
#ifdef CONFIG_MMU
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{ EXCCAUSE_ITLB_MISS,		0,	   do_page_fault },
{ EXCCAUSE_ITLB_MISS,		USER|KRNL, fast_second_level_miss},
{ EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
{ EXCCAUSE_ITLB_PRIVILEGE,	0,	   do_page_fault },
/* EXCCAUSE_SIZE_RESTRICTION unhandled */
{ EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
{ EXCCAUSE_DTLB_MISS,		USER|KRNL, fast_second_level_miss},
{ EXCCAUSE_DTLB_MISS,		0,	   do_page_fault },
{ EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
{ EXCCAUSE_DTLB_PRIVILEGE,	0,	   do_page_fault },
/* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
{ EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
{ EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
{ EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
Johannes Weiner's avatar
Johannes Weiner committed
125
#endif /* CONFIG_MMU */
126
/* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
127
#if XTENSA_HAVE_COPROCESSOR(0)
128
129
COPROCESSOR(0),
#endif
130
#if XTENSA_HAVE_COPROCESSOR(1)
131
132
COPROCESSOR(1),
#endif
133
#if XTENSA_HAVE_COPROCESSOR(2)
134
135
COPROCESSOR(2),
#endif
136
#if XTENSA_HAVE_COPROCESSOR(3)
137
138
COPROCESSOR(3),
#endif
139
#if XTENSA_HAVE_COPROCESSOR(4)
140
141
COPROCESSOR(4),
#endif
142
#if XTENSA_HAVE_COPROCESSOR(5)
143
144
COPROCESSOR(5),
#endif
145
#if XTENSA_HAVE_COPROCESSOR(6)
146
147
COPROCESSOR(6),
#endif
148
#if XTENSA_HAVE_COPROCESSOR(7)
149
150
COPROCESSOR(7),
#endif
Max Filippov's avatar
Max Filippov committed
151
152
153
#if XTENSA_FAKE_NMI
{ EXCCAUSE_MAPPED_NMI,			0,		do_nmi },
#endif
154
155
156
157
158
159
160
161
162
163
{ EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
{ -1, -1, 0 }

};

/* The exception table <exc_table> serves two functions:
 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
 * 2. it is a temporary memory buffer for the exception handlers.
 */

164
DEFINE_PER_CPU(struct exc_table, exc_table);
165
166
DEFINE_PER_CPU(struct debug_table, debug_table);

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
void die(const char*, struct pt_regs*, long);

static inline void
__die_if_kernel(const char *str, struct pt_regs *regs, long err)
{
	if (!user_mode(regs))
		die(str, regs, err);
}

/*
 * Unhandled Exceptions. Kill user task or panic if in kernel space.
 */

void do_unhandled(struct pt_regs *regs, unsigned long exccause)
{
	__die_if_kernel("Caught unhandled exception - should not happen",
183
			regs, SIGKILL);
184
185

	/* If in user mode, send SIGILL signal to current process */
186
187
188
189
190
	pr_info_ratelimited("Caught unhandled exception in '%s' "
			    "(pid = %d, pc = %#010lx) - should not happen\n"
			    "\tEXCCAUSE is %ld\n",
			    current->comm, task_pid_nr(current), regs->pc,
			    exccause);
191
	force_sig(SIGILL);
192
193
194
195
196
197
198
199
200
201
202
203
}

/*
 * Multi-hit exception. This if fatal!
 */

void do_multihit(struct pt_regs *regs, unsigned long exccause)
{
	die("Caught multihit exception", regs, SIGKILL);
}

/*
204
 * IRQ handler.
205
206
207
208
 */

extern void do_IRQ(int, struct pt_regs *);

Max Filippov's avatar
Max Filippov committed
209
210
#if XTENSA_FAKE_NMI

211
212
213
214
215
216
217
218
219
#define IS_POW2(v) (((v) & ((v) - 1)) == 0)

#if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
      IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
#warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
#warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."

static inline void check_valid_nmi(void)
{
220
221
	unsigned intread = xtensa_get_sr(interrupt);
	unsigned intenable = xtensa_get_sr(intenable);
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

	BUG_ON(intread & intenable &
	       ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
		 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
		 BIT(XCHAL_PROFILING_INTERRUPT)));
}

#else

static inline void check_valid_nmi(void)
{
}

#endif

Max Filippov's avatar
Max Filippov committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);

DEFINE_PER_CPU(unsigned long, nmi_count);

void do_nmi(struct pt_regs *regs)
{
	struct pt_regs *old_regs;

	if ((regs->ps & PS_INTLEVEL_MASK) < LOCKLEVEL)
		trace_hardirqs_off();

	old_regs = set_irq_regs(regs);
	nmi_enter();
	++*this_cpu_ptr(&nmi_count);
251
	check_valid_nmi();
Max Filippov's avatar
Max Filippov committed
252
253
254
255
256
257
	xtensa_pmu_irq_handler(0, NULL);
	nmi_exit();
	set_irq_regs(old_regs);
}
#endif

258
void do_interrupt(struct pt_regs *regs)
259
{
260
261
262
263
264
265
266
267
268
269
	static const unsigned int_level_mask[] = {
		0,
		XCHAL_INTLEVEL1_MASK,
		XCHAL_INTLEVEL2_MASK,
		XCHAL_INTLEVEL3_MASK,
		XCHAL_INTLEVEL4_MASK,
		XCHAL_INTLEVEL5_MASK,
		XCHAL_INTLEVEL6_MASK,
		XCHAL_INTLEVEL7_MASK,
	};
270
	struct pt_regs *old_regs;
271

272
273
274
	trace_hardirqs_off();

	old_regs = set_irq_regs(regs);
275
	irq_enter();
276

277
	for (;;) {
278
279
		unsigned intread = xtensa_get_sr(interrupt);
		unsigned intenable = xtensa_get_sr(intenable);
280
281
282
283
284
285
286
287
288
		unsigned int_at_level = intread & intenable;
		unsigned level;

		for (level = LOCKLEVEL; level > 0; --level) {
			if (int_at_level & int_level_mask[level]) {
				int_at_level &= int_level_mask[level];
				break;
			}
		}
289

290
		if (level == 0)
291
292
293
			break;

		do_IRQ(__ffs(int_at_level), regs);
294
	}
295
296
297

	irq_exit();
	set_irq_regs(old_regs);
298
299
300
301
302
303
304
305
306
307
308
309
310
}

/*
 * Illegal instruction. Fatal if in kernel space.
 */

void
do_illegal_instruction(struct pt_regs *regs)
{
	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);

	/* If in user mode, send SIGILL signal to current process. */

311
312
	pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
			    current->comm, task_pid_nr(current), regs->pc);
313
	force_sig(SIGILL);
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
}


/*
 * Handle unaligned memory accesses from user space. Kill task.
 *
 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
 * accesses causes from user space.
 */

#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
void
do_unaligned_user (struct pt_regs *regs)
{
	__die_if_kernel("Unhandled unaligned exception in kernel",
329
			regs, SIGKILL);
330
331
332

	current->thread.bad_vaddr = regs->excvaddr;
	current->thread.error_code = -3;
333
334
335
336
	pr_info_ratelimited("Unaligned memory access to %08lx in '%s' "
			    "(pid = %d, pc = %#010lx)\n",
			    regs->excvaddr, current->comm,
			    task_pid_nr(current), regs->pc);
337
	force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr);
338
339
340
}
#endif

341
342
343
344
345
346
/* Handle debug events.
 * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with
 * preemption disabled to avoid rescheduling and keep mapping of hardware
 * breakpoint structures to debug registers intact, so that
 * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit.
 */
347
348
349
void
do_debug(struct pt_regs *regs)
{
350
351
352
353
354
355
356
#ifdef CONFIG_HAVE_HW_BREAKPOINT
	int ret = check_hw_breakpoint(regs);

	preempt_enable();
	if (ret == 0)
		return;
#endif
357
358
359
360
	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);

	/* If in user mode, send SIGTRAP signal to current process */

361
	force_sig(SIGTRAP);
362
363
364
}


365
366
367
368
369
370
371
#define set_handler(type, cause, handler)				\
	do {								\
		unsigned int cpu;					\
									\
		for_each_possible_cpu(cpu)				\
			per_cpu(exc_table, cpu).type[cause] = (handler);\
	} while (0)
Max Filippov's avatar
Max Filippov committed
372

373
374
375
376
/* Set exception C handler - for temporary use when probing exceptions */

void * __init trap_set_handler(int cause, void *handler)
{
377
378
379
	void *previous = per_cpu(exc_table, 0).default_handler[cause];

	set_handler(default_handler, cause, handler);
380
381
382
383
	return previous;
}


Max Filippov's avatar
Max Filippov committed
384
static void trap_init_excsave(void)
Max Filippov's avatar
Max Filippov committed
385
{
386
	unsigned long excsave1 = (unsigned long)this_cpu_ptr(&exc_table);
Max Filippov's avatar
Max Filippov committed
387
388
389
	__asm__ __volatile__("wsr  %0, excsave1\n" : : "a" (excsave1));
}

390
391
392
393
394
395
396
397
398
static void trap_init_debug(void)
{
	unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table);

	this_cpu_ptr(&debug_table)->debug_exception = debug_exception;
	__asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL)
			     :: "a"(debugsave));
}

399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Initialize dispatch tables.
 *
 * The exception vectors are stored compressed the __init section in the
 * dispatch_init_table. This function initializes the following three tables
 * from that compressed table:
 * - fast user		first dispatch table for user exceptions
 * - fast kernel	first dispatch table for kernel exceptions
 * - default C-handler	C-handler called by the default fast handler.
 *
 * See vectors.S for more details.
 */

412
void __init trap_init(void)
413
414
415
416
417
{
	int i;

	/* Setup default vectors. */

418
419
420
421
	for (i = 0; i < EXCCAUSE_N; i++) {
		set_handler(fast_user_handler, i, user_exception);
		set_handler(fast_kernel_handler, i, kernel_exception);
		set_handler(default_handler, i, do_unhandled);
422
423
424
425
426
427
428
429
430
431
	}

	/* Setup specific handlers. */

	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
		int fast = dispatch_init_table[i].fast;
		int cause = dispatch_init_table[i].cause;
		void *handler = dispatch_init_table[i].handler;

		if (fast == 0)
432
			set_handler(default_handler, cause, handler);
Max Filippov's avatar
Max Filippov committed
433
		if ((fast & USER) != 0)
434
			set_handler(fast_user_handler, cause, handler);
Max Filippov's avatar
Max Filippov committed
435
		if ((fast & KRNL) != 0)
436
			set_handler(fast_kernel_handler, cause, handler);
437
438
439
	}

	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
Max Filippov's avatar
Max Filippov committed
440
	trap_init_excsave();
441
	trap_init_debug();
Max Filippov's avatar
Max Filippov committed
442
}
443

Max Filippov's avatar
Max Filippov committed
444
#ifdef CONFIG_SMP
Max Filippov's avatar
Max Filippov committed
445
void secondary_trap_init(void)
Max Filippov's avatar
Max Filippov committed
446
447
{
	trap_init_excsave();
448
	trap_init_debug();
449
}
Max Filippov's avatar
Max Filippov committed
450
#endif
451
452
453
454
455
456
457
458
459

/*
 * This function dumps the current valid window frame and other base registers.
 */

void show_regs(struct pt_regs * regs)
{
	int i, wmask;

460
461
	show_regs_print_info(KERN_DEFAULT);

462
463
	wmask = regs->wmask & ~1;

Chris Zankel's avatar
Chris Zankel committed
464
	for (i = 0; i < 16; i++) {
465
		if ((i % 8) == 0)
466
467
			pr_info("a%02d:", i);
		pr_cont(" %08lx", regs->areg[i]);
468
	}
469
470
471
472
473
	pr_cont("\n");
	pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
		regs->pc, regs->ps, regs->depc, regs->excvaddr);
	pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
		regs->lbeg, regs->lend, regs->lcount, regs->sar);
474
	if (user_mode(regs))
475
476
477
		pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
			regs->windowbase, regs->windowstart, regs->wmask,
			regs->syscall);
478
479
}

Max Filippov's avatar
Max Filippov committed
480
static int show_trace_cb(struct stackframe *frame, void *data)
481
{
Max Filippov's avatar
Max Filippov committed
482
483
	if (kernel_text_address(frame->pc))
		pr_cont(" [<%08lx>] %pB\n", frame->pc, (void *)frame->pc);
Max Filippov's avatar
Max Filippov committed
484
	return 0;
485
486
}

487
488
void show_trace(struct task_struct *task, unsigned long *sp)
{
Max Filippov's avatar
Max Filippov committed
489
490
	if (!sp)
		sp = stack_pointer(task);
491

492
	pr_info("Call Trace:\n");
Max Filippov's avatar
Max Filippov committed
493
	walk_stackframe(sp, show_trace_cb, NULL);
494
495
496
#ifndef CONFIG_KALLSYMS
	pr_cont("\n");
#endif
497
498
499
500
501
502
503
504
505
}

static int kstack_depth_to_print = 24;

void show_stack(struct task_struct *task, unsigned long *sp)
{
	int i = 0;
	unsigned long *stack;

506
	if (!sp)
507
		sp = stack_pointer(task);
508
	stack = sp;
509

510
	pr_info("Stack:\n");
511
512
513
514

	for (i = 0; i < kstack_depth_to_print; i++) {
		if (kstack_end(sp))
			break;
515
516
517
		pr_cont(" %08lx", *sp++);
		if (i % 8 == 7)
			pr_cont("\n");
518
519
520
521
	}
	show_trace(task, stack);
}

522
DEFINE_SPINLOCK(die_lock);
523
524
525
526
527
528
529
530

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

	console_verbose();
	spin_lock_irq(&die_lock);

531
532
	pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter,
		IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "");
533
534
535
536
	show_regs(regs);
	if (!user_mode(regs))
		show_stack(NULL, (unsigned long*)regs->areg[1]);

537
	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
538
539
540
541
542
	spin_unlock_irq(&die_lock);

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

Horms's avatar
Horms committed
543
	if (panic_on_oops)
544
		panic("Fatal exception");
Horms's avatar
Horms committed
545

546
547
	do_exit(err);
}