1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <arch/breakpoint.h>
4 #include <arch/stack_canary_breakpoint.h>
5 #include <arch/symbols.h>
7 #include <console/console.h>
11 static struct breakpoint_handle stack_canary_bp
;
13 static int handle_stack_canary_written(struct breakpoint_handle handle
, struct eregs
*regs
)
16 printk(BIOS_ERR
, "Stack corruption detected at rip: 0x%llx\n", regs
->rip
);
18 printk(BIOS_ERR
, "Stack corruption detected at eip: 0x%x\n", regs
->eip
);
23 static void create_stack_canary_breakpoint(uintptr_t *addr
)
25 enum breakpoint_result res
=
26 breakpoint_create_data(&stack_canary_bp
, addr
, sizeof(uintptr_t), true);
28 if (res
!= BREAKPOINT_RES_OK
) {
29 printk(BIOS_ERR
, "Failed to create stack canary breakpoint\n");
33 breakpoint_set_handler(stack_canary_bp
, &handle_stack_canary_written
);
34 breakpoint_enable(stack_canary_bp
, true);
37 void stack_canary_breakpoint_init(void)
41 if (CONFIG(RESET_VECTOR_IN_RAM
)) {
42 addr
= (uintptr_t *)_earlyram_stack
;
43 } else if (ENV_CACHE_AS_RAM
) {
44 addr
= (uintptr_t *)_car_stack
;
46 addr
= (uintptr_t *)_stack
;
49 create_stack_canary_breakpoint(addr
);
52 void stack_canary_breakpoint_remove(void)
54 breakpoint_remove(stack_canary_bp
);
57 static void stack_canary_breakpoint_remove_hook(void *unused
)
59 stack_canary_breakpoint_remove();
62 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME
, BS_ON_ENTRY
, stack_canary_breakpoint_remove_hook
, NULL
);
63 BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT
, BS_ON_ENTRY
, stack_canary_breakpoint_remove_hook
, NULL
);