summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-08-06 00:30:29 -0400
committerIan Moffett <ian@osmora.org>2025-08-06 00:33:32 -0400
commitd097c4299109f056a425684955fc118e84f94fa1 (patch)
tree414ca51fa272fb6161d011b97af8d711335210b3
parenta731d4e3d7cb1cae46899af30f12cabe851c191d (diff)
kernel/amd64: Limit frame depth in backtrace
A backtrace usually occurs during critcal system events like panics. When the system in a error state, we cannot trust the correctness of the stack. This commit aims to improve checks done in a731d4e by adding a max frame depth that limits how many frames can be walked. Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--sys/arch/amd64/amd64/machdep.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c
index 52ad64c..9ff96e1 100644
--- a/sys/arch/amd64/amd64/machdep.c
+++ b/sys/arch/amd64/amd64/machdep.c
@@ -47,6 +47,13 @@
#include <dev/cons/cons.h>
#include <string.h>
+/*
+ * This defines the max number of frames
+ * we will pass while walking the callstack
+ * in md_backtrace()
+ */
+#define MAX_FRAME_DEPTH 16
+
#define pr_trace(fmt, ...) kprintf("cpu: " fmt, ##__VA_ARGS__)
#define pr_error(...) pr_trace(__VA_ARGS__)
#define pr_trace_bsp(...) \
@@ -263,9 +270,14 @@ md_backtrace(void)
off_t off;
const char *name;
char line[256];
+ uint8_t n = 0;
__ASMV("mov %%rbp, %0" : "=r" (rbp) :: "memory");
while (1) {
+ if (n >= MAX_FRAME_DEPTH) {
+ break;
+ }
+
rip = rbp[1];
rbp = (uintptr_t *)rbp[0];
@@ -290,6 +302,7 @@ md_backtrace(void)
name = backtrace_addr_to_name(rip, &off);
snprintf(line, sizeof(line), "%p @ <%s+0x%x>\n", rip, name, off);
cons_putstr(&g_root_scr, line, strlen(line));
+ ++n;
}
}