git: 7171e591a4c7 - main - amd64: add ptrace PT_{GET,SET}TLSBASE requests

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 28 May 2025 11:11:43 UTC
The branch main has been updated by kib:

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

commit 7171e591a4c70eb4ee616e33373fd62773ad5a08
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-05-22 06:56:24 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-05-28 11:11:23 +0000

    amd64: add ptrace PT_{GET,SET}TLSBASE requests
    
    Tested by:      pho
    Reviewed by:    olce
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D50482
---
 sys/amd64/amd64/ptrace_machdep.c | 21 +++++++++++++++++++++
 sys/x86/include/ptrace.h         |  2 ++
 2 files changed, 23 insertions(+)

diff --git a/sys/amd64/amd64/ptrace_machdep.c b/sys/amd64/amd64/ptrace_machdep.c
index 715f0749bda9..69616e80b708 100644
--- a/sys/amd64/amd64/ptrace_machdep.c
+++ b/sys/amd64/amd64/ptrace_machdep.c
@@ -368,6 +368,27 @@ cpu_ptrace(struct thread *td, int req, void *addr, int data)
 		cpu_ptrace_setbase(td, req, rv);
 		break;
 
+	case PT_GETTLSBASE:
+		pcb = td->td_pcb;
+		if ((pcb->pcb_flags & PCB_TLSBASE) != 0)
+			error = copyout(&pcb->pcb_tlsbase, addr, sizeof(*r));
+		else
+			error = ESRCH;
+		break;
+
+	case PT_SETTLSBASE:
+		pcb = td->td_pcb;
+		error = copyin(addr, &rv, sizeof(rv));
+		if (error != 0)
+			break;
+		if (rv >= td->td_proc->p_sysent->sv_maxuser) {
+			error = EINVAL;
+			break;
+		}
+		pcb->pcb_tlsbase = rv;
+		set_pcb_flags(pcb, PCB_TLSBASE);
+		break;
+
 	default:
 		error = EINVAL;
 		break;
diff --git a/sys/x86/include/ptrace.h b/sys/x86/include/ptrace.h
index f477094bdd14..d4fa1fe45a3c 100644
--- a/sys/x86/include/ptrace.h
+++ b/sys/x86/include/ptrace.h
@@ -54,6 +54,8 @@
 #define	PT_SETFSBASE	(PT_FIRSTMACH + 8)
 #define	PT_GETGSBASE	(PT_FIRSTMACH + 9)
 #define	PT_SETGSBASE	(PT_FIRSTMACH + 10)
+#define	PT_GETTLSBASE	(PT_FIRSTMACH + 11)
+#define	PT_SETTLSBASE	(PT_FIRSTMACH + 12)
 
 /* Argument structure for PT_GETXSTATE_INFO. */
 struct ptrace_xstate_info {