git: d33684f3710e - stable/13 - linux: Implement some bits of PTRACE_PEEKUSER

From: Edward Tomasz Napierala <trasz_at_FreeBSD.org>
Date: Mon, 21 Feb 2022 13:48:55 UTC
The branch stable/13 has been updated by trasz:

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

commit d33684f3710e579322835d4737dc8a92ac30144b
Author:     Edward Tomasz Napierala <trasz@FreeBSD.org>
AuthorDate: 2021-10-17 11:20:16 +0000
Commit:     Edward Tomasz Napierala <trasz@FreeBSD.org>
CommitDate: 2022-02-21 13:34:56 +0000

    linux: Implement some bits of PTRACE_PEEKUSER
    
    This makes Linux gdb from Bionic a little less broken.
    
    Sponsored By:   EPSRC
    Differential Revision:  https://reviews.freebsd.org/D32455
    
    (cherry picked from commit f9246e14848820664539763b72b6fdef408d20e4)
---
 sys/amd64/linux/linux_ptrace.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/sys/amd64/linux/linux_ptrace.c b/sys/amd64/linux/linux_ptrace.c
index cccef2ad5461..5f1e0eb93f5a 100644
--- a/sys/amd64/linux/linux_ptrace.c
+++ b/sys/amd64/linux/linux_ptrace.c
@@ -98,6 +98,11 @@ __FBSDID("$FreeBSD$");
 #define	LINUX_PTRACE_SYSCALL_INFO_ENTRY	1
 #define	LINUX_PTRACE_SYSCALL_INFO_EXIT	2
 
+#define LINUX_PTRACE_PEEKUSER_ORIG_RAX	120
+#define LINUX_PTRACE_PEEKUSER_RIP	128
+#define LINUX_PTRACE_PEEKUSER_CS	136
+#define LINUX_PTRACE_PEEKUSER_DS	184
+
 #define	LINUX_ARCH_AMD64		0xc000003e
 
 static int
@@ -320,9 +325,37 @@ linux_ptrace_peek(struct thread *td, pid_t pid, void *addr, void *data)
 static int
 linux_ptrace_peekuser(struct thread *td, pid_t pid, void *addr, void *data)
 {
+	struct reg b_reg;
+	uint64_t val;
+	int error;
 
-	linux_msg(td, "PTRACE_PEEKUSER not implemented; returning EINVAL");
-	return (EINVAL);
+	error = kern_ptrace(td, PT_GETREGS, pid, &b_reg, 0);
+	if (error != 0)
+		return (error);
+
+	switch ((uintptr_t)addr) {
+	case LINUX_PTRACE_PEEKUSER_ORIG_RAX:
+		val = b_reg.r_rax;
+		break;
+	case LINUX_PTRACE_PEEKUSER_RIP:
+		val = b_reg.r_rip;
+		break;
+	case LINUX_PTRACE_PEEKUSER_CS:
+		val = b_reg.r_cs;
+		break;
+	case LINUX_PTRACE_PEEKUSER_DS:
+		val = b_reg.r_ds;
+		break;
+	default:
+		linux_msg(td, "PTRACE_PEEKUSER offset %ld not implemented; "
+		    "returning EINVAL", (uintptr_t)addr);
+		return (EINVAL);
+	}
+
+	error = copyout(&val, data, sizeof(val));
+	td->td_retval[0] = error;
+
+	return (error);
 }
 
 static int