summaryrefslogtreecommitdiff
path: root/src/arch/x86/null_breakpoint.c
blob: 44c7ae91c44e99bd2d9a4c8da6fb5ec23f03c96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* SPDX-License-Identifier: GPL-2.0-only */

#include <arch/breakpoint.h>
#include <arch/null_breakpoint.h>
#include <bootstate.h>
#include <console/console.h>
#include <types.h>

static struct breakpoint_handle null_deref_bp;
static struct breakpoint_handle null_fetch_bp;

static int handle_fetch_breakpoint(struct breakpoint_handle handle, struct eregs *regs)
{
	printk(BIOS_ERR, "Instruction fetch from address zero\n");
	return 0;
}

static int handle_deref_breakpoint(struct breakpoint_handle handle, struct eregs *regs)
{
#if ENV_X86_64
	printk(BIOS_ERR, "Null dereference at rip: 0x%llx\n", regs->rip);
#else
	printk(BIOS_ERR, "Null dereference at eip: 0x%x\n", regs->eip);
#endif
	return 0;
}

static void create_deref_breakpoint(void)
{
	enum breakpoint_result res =
		breakpoint_create_data(&null_deref_bp, NULL, sizeof(uintptr_t), false);

	if (res != BREAKPOINT_RES_OK) {
		printk(BIOS_ERR, "Failed to create NULL dereference breakpoint\n");
		return;
	}

	breakpoint_set_handler(null_deref_bp, &handle_deref_breakpoint);
	breakpoint_enable(null_deref_bp, true);
}

static void create_instruction_breakpoint(void)
{
	enum breakpoint_result res = breakpoint_create_instruction(&null_fetch_bp, NULL);

	if (res != BREAKPOINT_RES_OK) {
		printk(BIOS_ERR, "Failed to create address zero instruction fetch breakpoint\n");
		return;
	}

	breakpoint_set_handler(null_fetch_bp, &handle_fetch_breakpoint);
	breakpoint_enable(null_fetch_bp, true);
}

void null_breakpoint_init(void)
{
	create_deref_breakpoint();
	create_instruction_breakpoint();
}

void null_breakpoint_disable(void)
{
	breakpoint_remove(null_fetch_bp);
	breakpoint_remove(null_deref_bp);
}

static void null_breakpoint_disable_hook(void *unused)
{
	null_breakpoint_disable();
}

BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, null_breakpoint_disable_hook, NULL);
BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, null_breakpoint_disable_hook, NULL);