From 3b697b3f3b7c3ab2824322d0329e12df82df41a4 Mon Sep 17 00:00:00 2001 From: guenther Date: Wed, 31 Jan 2024 06:06:28 +0000 Subject: [PATCH] Swap the r10 and rcx registers in the amd64 trapframe so that the first six entries are in the same order as syscall arguments, such that syscall() can just use the trapframe as the argument vector for mi_syscall() and not need to reorder into another buffer on the stack. This doesn't affect coredump layout or ptrace(2), but does affect kernel crash dumps. Possibility noted during miod@'s cleanup of the MD syscall() implementations ok mlarkin@ kurt@ --- gnu/usr.bin/binutils/gdb/amd64obsd-tdep.c | 4 ++-- sys/arch/amd64/amd64/trap.c | 22 ++++------------------ sys/arch/amd64/include/frame.h | 14 +++++++------- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/gnu/usr.bin/binutils/gdb/amd64obsd-tdep.c b/gnu/usr.bin/binutils/gdb/amd64obsd-tdep.c index b845c2ec5da..726d3e5df0c 100644 --- a/gnu/usr.bin/binutils/gdb/amd64obsd-tdep.c +++ b/gnu/usr.bin/binutils/gdb/amd64obsd-tdep.c @@ -362,7 +362,7 @@ static int amd64obsd_tf_reg_offset[] = { 14 * 8, /* %rax */ 13 * 8, /* %rbx */ - 3 * 8, /* %rcx */ + 6 * 8, /* %rcx */ 2 * 8, /* %rdx */ 1 * 8, /* %rsi */ 0 * 8, /* %rdi */ @@ -370,7 +370,7 @@ static int amd64obsd_tf_reg_offset[] = 20 * 8, /* %rsp */ 4 * 8, /* %r8 ... */ 5 * 8, - 6 * 8, + 3 * 8, 7 * 8, 8 * 8, 9 * 8, diff --git a/sys/arch/amd64/amd64/trap.c b/sys/arch/amd64/amd64/trap.c index d64f7c1af08..4df17486b78 100644 --- a/sys/arch/amd64/amd64/trap.c +++ b/sys/arch/amd64/amd64/trap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: trap.c,v 1.103 2024/01/11 19:16:26 miod Exp $ */ +/* $OpenBSD: trap.c,v 1.104 2024/01/31 06:06:28 guenther Exp $ */ /* $NetBSD: trap.c,v 1.2 2003/05/04 23:51:56 fvdl Exp $ */ /*- @@ -553,7 +553,7 @@ syscall(struct trapframe *frame) const struct sysent *callp; struct proc *p; int error = ENOSYS; - register_t code, args[6], rval[2], *argp; + register_t code, *args, rval[2]; verify_smap(__func__); uvmexp.syscalls++; @@ -565,30 +565,16 @@ syscall(struct trapframe *frame) } code = frame->tf_rax; - argp = &args[0]; + args = (register_t *)&frame->tf_rdi; if (code <= 0 || code >= SYS_MAXSYSCALL) goto bad; callp = sysent + code; - switch (callp->sy_narg) { - case 6: - args[5] = frame->tf_r9; - case 5: - args[4] = frame->tf_r8; - case 4: - args[3] = frame->tf_r10; - case 3: - args[2] = frame->tf_rdx; - case 2: - args[1] = frame->tf_rsi; - case 1: - args[0] = frame->tf_rdi; - } rval[0] = 0; rval[1] = 0; - error = mi_syscall(p, code, callp, argp, rval); + error = mi_syscall(p, code, callp, args, rval); switch (error) { case 0: diff --git a/sys/arch/amd64/include/frame.h b/sys/arch/amd64/include/frame.h index 19691ff19c5..d9b043363ea 100644 --- a/sys/arch/amd64/include/frame.h +++ b/sys/arch/amd64/include/frame.h @@ -1,4 +1,4 @@ -/* $OpenBSD: frame.h,v 1.10 2018/07/10 08:57:44 guenther Exp $ */ +/* $OpenBSD: frame.h,v 1.11 2024/01/31 06:06:28 guenther Exp $ */ /* $NetBSD: frame.h,v 1.1 2003/04/26 18:39:40 fvdl Exp $ */ /*- @@ -82,13 +82,13 @@ * Exception/Trap Stack Frame */ struct trapframe { - int64_t tf_rdi; + int64_t tf_rdi; /* ordered by syscall args... */ int64_t tf_rsi; int64_t tf_rdx; - int64_t tf_rcx; - int64_t tf_r8; - int64_t tf_r9; int64_t tf_r10; + int64_t tf_r8; + int64_t tf_r9; /* ...to here */ + int64_t tf_rcx; int64_t tf_r11; int64_t tf_r12; int64_t tf_r13; @@ -115,10 +115,10 @@ struct intrframe { int64_t if_rdi; int64_t if_rsi; int64_t if_rdx; - int64_t if_rcx; + int64_t if_r10; int64_t if_r8; int64_t if_r9; - int64_t if_r10; + int64_t if_rcx; int64_t if_r11; int64_t if_r12; int64_t if_r13; -- 2.20.1