From b838b53942eb9680c150d27a0645b8967d91a29d Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 21 Jul 2026 08:10:22 -0400 Subject: [PATCH 1/3] slab: add CLEANUP_KFREE Signed-off-by: Michael Shaw --- include/kernel/slab.h | 2 ++ mem/slab.c | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/include/kernel/slab.h b/include/kernel/slab.h index b2f822d..6ab93dc 100644 --- a/include/kernel/slab.h +++ b/include/kernel/slab.h @@ -8,6 +8,8 @@ #define ALLOC_KERN 0x1 #define ALLOC_USER 0x2 +#define CLEANUP_KFREE __attribute__((cleanup(_cleanup_kfree))) + typedef struct _mem_cache_t { char *name; size_t item_size; diff --git a/mem/slab.c b/mem/slab.c index 1140582..0c301c0 100644 --- a/mem/slab.c +++ b/mem/slab.c @@ -105,3 +105,9 @@ void slab_free(mem_cache_t *cache, void *ptr) spinlock_release(&cache->lock); } + +void _cleanup_kfree(void **ptr) +{ + if (ptr && *ptr) + kfree(ptr); +} From 3ca26e606804ad7f89d29ed32fe8a0da6a7772fd Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 21 Jul 2026 20:21:44 -0400 Subject: [PATCH 2/3] proc: add current_pt_regs Signed-off-by: Michael Shaw --- amd64/include/arch/proc.h | 1 + amd64/syscall.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/amd64/include/arch/proc.h b/amd64/include/arch/proc.h index 600ed67..bf7cf15 100644 --- a/amd64/include/arch/proc.h +++ b/amd64/include/arch/proc.h @@ -38,5 +38,6 @@ struct cpu_local *tls_get(); struct proc *get_cur_proc(); struct thread *get_cur_thread(); void enter_userspace_init(struct thread *thread, uaddr_t entry); +struct pt_regs *current_pt_regs(); #endif /* _ARCH_PROC_H_ */ diff --git a/amd64/syscall.c b/amd64/syscall.c index 6422f8c..ad7417d 100644 --- a/amd64/syscall.c +++ b/amd64/syscall.c @@ -2,6 +2,11 @@ #include #include +struct pt_regs *current_pt_regs() +{ + return (struct pt_regs *)(tls_get()->kernel_rsp - sizeof(struct pt_regs)); +} + static inline i64 _do_syscall(struct pt_regs *regs) { syscall_t c = get_syscall(regs->rax); From bc39293c8366f55ce72c87182b141258dfd81e35 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 21 Jul 2026 20:41:14 -0400 Subject: [PATCH 3/3] amd64: simplify entry path move all IRQs and exceptions into smaller stubs, that then jmp into `irq_common` or `exception_common`. handle swap GS on userspace-kernel interrupt switches Signed-off-by: Michael Shaw --- amd64/arch/entry.S | 64 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/amd64/arch/entry.S b/amd64/arch/entry.S index 1ba6df5..aeab2d4 100644 --- a/amd64/arch/entry.S +++ b/amd64/arch/entry.S @@ -40,34 +40,66 @@ .macro exception nr .global _exception_\nr _exception_\nr: - push_all - mov rdi, \nr - call exception_handler - pop_all - iretq + push 0 + push \nr + jmp exception_common .endm .macro exception_err nr .global _exception_\nr _exception_\nr: - pop rsi - push_all - mov rdi, \nr - call exception_handler - pop_all - iretq + push \nr + jmp exception_common .endm .macro irq nr .global _irq_\nr _irq_\nr: - push_all - mov rdi, \nr - call irq_handler - pop_all - iretq + push \nr + jmp irq_common .endm +exception_common: + test qword ptr [rsp + 24], 3 /* is this from userspace? */ + jz 1f + swapgs +1: + push_all /* pushes 15 registers */ + mov rdi, [rsp + 120] /* vector: 15 regs up = 120 */ + mov rsi, [rsp + 128] /* error code we pushed in the macro */ + + /* we skip storing the user stack in these interrupt entries because + * the iretq frame stores it + */ + + call exception_handler + pop_all + test qword ptr [rsp + 24], 3 /* from userspace? */ + jz 2f + swapgs +2: + add rsp, 16 /* the two pushed items, from the macro */ + iretq + +/* same as exception_common, but we don't get the error code */ +irq_common: + test qword ptr [rsp + 24], 3 /* is this from userspace? */ + jz 1f + swapgs +1: + push_all /* pushes 15 registers */ + mov rdi, [rsp + 120] /* irq no */ + + call irq_handler + pop_all + test qword ptr [rsp + 24], 3 /* from userspace? */ + jz 2f + swapgs +2: + add rsp, 8 + iretq + + .global _syscall_enter _syscall_enter: swapgs