traps.c 2.1 KB
Newer Older
1 2 3 4
#include <linux/bug.h>
#include <linux/io.h>
#include <linux/types.h>
#include <linux/kdebug.h>
5 6
#include <linux/signal.h>
#include <linux/sched.h>
7
#include <linux/uaccess.h>
P
Paul Mundt 已提交
8
#include <linux/hardirq.h>
9
#include <asm/unwinder.h>
10 11 12
#include <asm/system.h>

#ifdef CONFIG_BUG
13
void handle_BUG(struct pt_regs *regs)
14
{
15 16
	const struct bug_entry *bug;
	unsigned long bugaddr = regs->pc;
17
	enum bug_trap_type tt;
18 19 20 21 22 23 24 25 26 27 28

	if (!is_valid_bugaddr(bugaddr))
		goto invalid;

	bug = find_bug(bugaddr);

	/* Switch unwinders when unwind_stack() is called */
	if (bug->flags & BUGFLAG_UNWINDER)
		unwinder_faulted = 1;

	tt = report_bug(bugaddr, regs);
29
	if (tt == BUG_TRAP_TYPE_WARN) {
30
		regs->pc += instruction_size(bugaddr);
31 32 33
		return;
	}

34
invalid:
35 36 37 38 39
	die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
}

int is_valid_bugaddr(unsigned long addr)
{
P
Paul Mundt 已提交
40
	insn_size_t opcode;
41 42 43

	if (addr < PAGE_OFFSET)
		return 0;
P
Paul Mundt 已提交
44
	if (probe_kernel_address((insn_size_t *)addr, opcode))
45
		return 0;
46
	if (opcode == TRAPA_BUG_OPCODE)
47 48 49
		return 1;

	return 0;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}
#endif

/*
 * Generic trap handler.
 */
BUILD_TRAP_HANDLER(debug)
{
	TRAP_HANDLER_DECL;

	/* Rewind */
	regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));

	if (notify_die(DIE_TRAP, "debug trap", regs, 0, vec & 0xff,
		       SIGTRAP) == NOTIFY_STOP)
		return;

	force_sig(SIGTRAP, current);
}

/*
 * Special handler for BUG() traps.
 */
BUILD_TRAP_HANDLER(bug)
{
	TRAP_HANDLER_DECL;

	/* Rewind */
	regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));

	if (notify_die(DIE_TRAP, "bug trap", regs, 0, TRAPA_BUG_OPCODE & 0xff,
		       SIGTRAP) == NOTIFY_STOP)
		return;

#ifdef CONFIG_BUG
	if (__kernel_text_address(instruction_pointer(regs))) {
P
Paul Mundt 已提交
86
		insn_size_t insn = *(insn_size_t *)instruction_pointer(regs);
87 88
		if (insn == TRAPA_BUG_OPCODE)
			handle_BUG(regs);
M
Magnus Damm 已提交
89
		return;
90 91 92 93 94
	}
#endif

	force_sig(SIGTRAP, current);
}
P
Paul Mundt 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

BUILD_TRAP_HANDLER(nmi)
{
	TRAP_HANDLER_DECL;

	nmi_enter();

	switch (notify_die(DIE_NMI, "NMI", regs, 0, vec & 0xff, SIGINT)) {
	case NOTIFY_OK:
	case NOTIFY_STOP:
		break;
	case NOTIFY_BAD:
		die("Fatal Non-Maskable Interrupt", regs, SIGINT);
	default:
		printk(KERN_ALERT "Got NMI, but nobody cared. Ignoring...\n");
		break;
	}

	nmi_exit();
}