git: f0d9a6a781f3 - main - linux: make PTRACE_SETREGS use a correct struct

From: Edward Tomasz Napierala <trasz_at_FreeBSD.org>
Date: Sat, 30 Oct 2021 10:14:28 UTC
The branch main has been updated by trasz:

URL: https://cgit.FreeBSD.org/src/commit/?id=f0d9a6a781f331440baf9a846e773e44a297d59c

commit f0d9a6a781f331440baf9a846e773e44a297d59c
Author:     Edward Tomasz Napierala <trasz@FreeBSD.org>
AuthorDate: 2021-10-30 09:13:32 +0000
Commit:     Edward Tomasz Napierala <trasz@FreeBSD.org>
CommitDate: 2021-10-30 09:13:37 +0000

    linux: make PTRACE_SETREGS use a correct struct
    
    Note that this is largely untested at this point, as was
    the previous version; I'm committing this mostly to get
    rid of `struct linux_pt_reg`.
    
    Sponsored By:   EPSRC
    Differential Revision:  https://reviews.freebsd.org/D32735
---
 sys/amd64/linux/linux.h         |  3 ++
 sys/amd64/linux/linux_machdep.c | 31 +++++++++++++++++++
 sys/amd64/linux/linux_ptrace.c  | 66 ++---------------------------------------
 3 files changed, 37 insertions(+), 63 deletions(-)

diff --git a/sys/amd64/linux/linux.h b/sys/amd64/linux/linux.h
index 519b6bd200ac..16fe3793eae7 100644
--- a/sys/amd64/linux/linux.h
+++ b/sys/amd64/linux/linux.h
@@ -461,5 +461,8 @@ struct reg;
 
 void	bsd_to_linux_regset(const struct reg *b_reg,
 	    struct linux_pt_regset *l_regset);
+void	linux_to_bsd_regset(struct reg *b_reg,
+	    const struct linux_pt_regset *l_regset);
+
 
 #endif /* !_AMD64_LINUX_H_ */
diff --git a/sys/amd64/linux/linux_machdep.c b/sys/amd64/linux/linux_machdep.c
index c34d98e86d0b..e2346f68da3a 100644
--- a/sys/amd64/linux/linux_machdep.c
+++ b/sys/amd64/linux/linux_machdep.c
@@ -330,3 +330,34 @@ bsd_to_linux_regset(const struct reg *b_reg, struct linux_pt_regset *l_regset)
 	l_regset->fs = b_reg->r_fs;
 	l_regset->gs = b_reg->r_gs;
 }
+
+void
+linux_to_bsd_regset(struct reg *b_reg, const struct linux_pt_regset *l_regset)
+{
+
+	b_reg->r_r15 = l_regset->r15;
+	b_reg->r_r14 = l_regset->r14;
+	b_reg->r_r13 = l_regset->r13;
+	b_reg->r_r12 = l_regset->r12;
+	b_reg->r_rbp = l_regset->rbp;
+	b_reg->r_rbx = l_regset->rbx;
+	b_reg->r_r11 = l_regset->r11;
+	b_reg->r_r10 = l_regset->r10;
+	b_reg->r_r9 = l_regset->r9;
+	b_reg->r_r8 = l_regset->r8;
+	b_reg->r_rax = l_regset->rax;
+	b_reg->r_rcx = l_regset->rcx;
+	b_reg->r_rdx = l_regset->rdx;
+	b_reg->r_rsi = l_regset->rsi;
+	b_reg->r_rdi = l_regset->rdi;
+	b_reg->r_rax = l_regset->orig_rax;
+	b_reg->r_rip = l_regset->rip;
+	b_reg->r_cs = l_regset->cs;
+	b_reg->r_rflags = l_regset->eflags;
+	b_reg->r_rsp = l_regset->rsp;
+	b_reg->r_ss = l_regset->ss;
+	b_reg->r_ds = l_regset->ds;
+	b_reg->r_es = l_regset->es;
+	b_reg->r_fs = l_regset->fs;
+	b_reg->r_gs = l_regset->gs;
+}
diff --git a/sys/amd64/linux/linux_ptrace.c b/sys/amd64/linux/linux_ptrace.c
index 0f06f2aa9c5c..191e6265b39c 100644
--- a/sys/amd64/linux/linux_ptrace.c
+++ b/sys/amd64/linux/linux_ptrace.c
@@ -168,30 +168,6 @@ linux_ptrace_status(struct thread *td, pid_t pid, int status)
 	return (status);
 }
 
-struct linux_pt_reg {
-	l_ulong	r15;
-	l_ulong	r14;
-	l_ulong	r13;
-	l_ulong	r12;
-	l_ulong	rbp;
-	l_ulong	rbx;
-	l_ulong	r11;
-	l_ulong	r10;
-	l_ulong	r9;
-	l_ulong	r8;
-	l_ulong	rax;
-	l_ulong	rcx;
-	l_ulong	rdx;
-	l_ulong	rsi;
-	l_ulong	rdi;
-	l_ulong	orig_rax;
-	l_ulong	rip;
-	l_ulong	cs;
-	l_ulong	eflags;
-	l_ulong	rsp;
-	l_ulong	ss;
-};
-
 struct syscall_info {
 	uint8_t op;
 	uint32_t arch;
@@ -214,42 +190,6 @@ struct syscall_info {
 	};
 };
 
-static void
-map_regs_from_linux(struct reg *b_reg, struct linux_pt_reg *l_reg)
-{
-	b_reg->r_r15 = l_reg->r15;
-	b_reg->r_r14 = l_reg->r14;
-	b_reg->r_r13 = l_reg->r13;
-	b_reg->r_r12 = l_reg->r12;
-	b_reg->r_r11 = l_reg->r11;
-	b_reg->r_r10 = l_reg->r10;
-	b_reg->r_r9 = l_reg->r9;
-	b_reg->r_r8 = l_reg->r8;
-	b_reg->r_rdi = l_reg->rdi;
-	b_reg->r_rsi = l_reg->rsi;
-	b_reg->r_rbp = l_reg->rbp;
-	b_reg->r_rbx = l_reg->rbx;
-	b_reg->r_rdx = l_reg->rdx;
-	b_reg->r_rcx = l_reg->rcx;
-	b_reg->r_rax = l_reg->rax;
-
-	/*
-	 * XXX: Are zeroes the right thing to put here?
-	 */
-	b_reg->r_trapno = 0;
-	b_reg->r_fs = 0;
-	b_reg->r_gs = 0;
-	b_reg->r_err = 0;
-	b_reg->r_es = 0;
-	b_reg->r_ds = 0;
-
-	b_reg->r_rip = l_reg->rip;
-	b_reg->r_cs = l_reg->cs;
-	b_reg->r_rflags = l_reg->eflags;
-	b_reg->r_rsp = l_reg->rsp;
-	b_reg->r_ss = l_reg->ss;
-}
-
 static int
 linux_ptrace_peek(struct thread *td, pid_t pid, void *addr, void *data)
 {
@@ -446,13 +386,13 @@ static int
 linux_ptrace_setregs(struct thread *td, pid_t pid, void *data)
 {
 	struct reg b_reg;
-	struct linux_pt_reg l_reg;
+	struct linux_pt_regset l_regset;
 	int error;
 
-	error = copyin(data, &l_reg, sizeof(l_reg));
+	error = copyin(data, &l_regset, sizeof(l_regset));
 	if (error != 0)
 		return (error);
-	map_regs_from_linux(&b_reg, &l_reg);
+	linux_to_bsd_regset(&b_reg, &l_regset);
 	error = kern_ptrace(td, PT_SETREGS, pid, &b_reg, 0);
 	return (error);
 }