blob: 4da6d87ab1d918b8e64bfe7d29f37686ea66fbba [file] [log] [blame]
Robert Zieba3f01cd12022-04-14 10:36:15 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2#include <arch/breakpoint.h>
3#include <arch/null_breakpoint.h>
Arthur Heymans6fc12542022-05-14 10:40:24 +02004#include <bootstate.h>
Robert Zieba3f01cd12022-04-14 10:36:15 -06005#include <console/console.h>
6#include <stdint.h>
7
8static struct breakpoint_handle null_deref_bp;
9static struct breakpoint_handle null_fetch_bp;
10
11static int handle_fetch_breakpoint(struct breakpoint_handle handle, struct eregs *regs)
12{
13 printk(BIOS_ERR, "Instruction fetch from address zero\n");
14 return CONFIG(DEBUG_NULL_DEREF_HALT);
15}
16
17static int handle_deref_breakpoint(struct breakpoint_handle handle, struct eregs *regs)
18{
19#if ENV_X86_64
Paul Menzel7151e0e2022-05-30 12:08:31 +020020 printk(BIOS_ERR, "Null dereference at rip: 0x%llx\n", regs->rip);
Robert Zieba3f01cd12022-04-14 10:36:15 -060021#else
Paul Menzel7151e0e2022-05-30 12:08:31 +020022 printk(BIOS_ERR, "Null dereference at eip: 0x%x\n", regs->eip);
Robert Zieba3f01cd12022-04-14 10:36:15 -060023#endif
24 return CONFIG(DEBUG_NULL_DEREF_HALT);
25}
26
27static void create_deref_breakpoint(void)
28{
29 enum breakpoint_result res =
30 breakpoint_create_data(&null_deref_bp, NULL, sizeof(uintptr_t), false);
31
32 if (res != BREAKPOINT_RES_OK) {
33 printk(BIOS_ERR, "Failed to create NULL dereference breakpoint\n");
34 return;
35 }
36
37 breakpoint_set_handler(null_deref_bp, &handle_deref_breakpoint);
38 breakpoint_enable(null_deref_bp, true);
39}
40
41static void create_instruction_breakpoint(void)
42{
43 enum breakpoint_result res = breakpoint_create_instruction(&null_fetch_bp, NULL);
44
45 if (res != BREAKPOINT_RES_OK) {
46 printk(BIOS_ERR, "Failed to create address zero instruction fetch breakpoint\n");
47 return;
48 }
49
50 breakpoint_set_handler(null_fetch_bp, &handle_fetch_breakpoint);
51 breakpoint_enable(null_fetch_bp, true);
52}
53
54void null_breakpoint_init(void)
55{
56 create_deref_breakpoint();
57 create_instruction_breakpoint();
58}
Arthur Heymans6fc12542022-05-14 10:40:24 +020059
60static void null_breakpoint_disable(void *unused)
61{
62 breakpoint_remove(null_fetch_bp);
63 breakpoint_remove(null_deref_bp);
64}
65
66BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, null_breakpoint_disable, NULL);
67BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, null_breakpoint_disable, NULL);