From 3dc995e1eb82022453da9ed9d3b639b989e485cf Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 28 May 2025 21:47:25 -0400 Subject: kernel/amd64: trap: Log page fault flags Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/trap.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'sys/arch') diff --git a/sys/arch/amd64/amd64/trap.c b/sys/arch/amd64/amd64/trap.c index 6492a29..84a6a77 100644 --- a/sys/arch/amd64/amd64/trap.c +++ b/sys/arch/amd64/amd64/trap.c @@ -60,6 +60,17 @@ static const char *trap_type[] = { [TRAP_SS] = "stack-segment fault" }; +/* Page-fault flags */ +static const char pf_flags[] = { + 'p', /* Present */ + 'w', /* Write */ + 'u', /* User */ + 'r', /* Reserved write */ + 'x', /* Instruction fetch */ + 'k', /* Protection key violation */ + 's' /* Shadow stack access */ +}; + static inline uintptr_t pf_faultaddr(void) { @@ -68,6 +79,23 @@ pf_faultaddr(void) return cr2; } +static void +pf_code(uint64_t error_code) +{ + char tab[8] = { + '-', '-', '-', + '-', '-', '-', + '-', '\0' + }; + + for (int i = 0; i < 7; ++i) { + if (ISSET(error_code, BIT(i))) { + tab[i] = pf_flags[i]; + } + } + kprintf("code=[%s]\n", tab); +} + static void regdump(struct trapframe *tf) { @@ -79,6 +107,10 @@ regdump(struct trapframe *tf) : "memory" ); + if (tf->trapno == TRAP_PAGEFLT) { + pf_code(tf->error_code); + } + kprintf(OMIT_TIMESTAMP "RAX=%p RCX=%p RDX=%p\n" "RBX=%p RSI=%p RDI=%p\n" @@ -101,6 +133,9 @@ trap_user(struct trapframe *tf) switch (tf->trapno) { case TRAP_PROTFLT: case TRAP_PAGEFLT: + if (tf->trapno == TRAP_PAGEFLT) { + pf_code(tf->error_code); + } sigaddset(&sigset, SIGSEGV); break; case TRAP_ARITH_ERR: -- cgit v1.2.3