git: 3bd4724794a5 - main - arm64: write PID in CONTEXTIDR_EL1 on ctx switch

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Mon, 21 Oct 2024 12:24:26 UTC
The branch main has been updated by andrew:

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

commit 3bd4724794a5e5838f09420a615227416d6b2903
Author:     Zachary Leaf <zachary.leaf@arm.com>
AuthorDate: 2024-07-31 08:23:24 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2024-10-21 12:23:16 +0000

    arm64: write PID in CONTEXTIDR_EL1 on ctx switch
    
    Debug and trace features such as Statistical Profiling Extension (SPE)
    use the CONTEXTIDR_EL1 register to get the PID of the current process.
    
    Add a sysctl switch to toggle writing the current PID into this register
    in the thread switcher.
    
    To make use of the feature, the following sysctl switch must be set:
    
        sysctl machdep.pid_in_contextidr=1
    
    Kernel code can also toggle the sysctl by writing directly to the global
    var behind the sysctl arm64_pid_in_contextidr:
    
        extern bool arm64_pid_in_contextidr;
    
    Sponsored by:   Arm Ltd
---
 sys/arm64/arm64/genassym.c    |  2 ++
 sys/arm64/arm64/swtch.S       | 21 +++++++++++++++++++++
 sys/arm64/arm64/sys_machdep.c |  6 ++++++
 3 files changed, 29 insertions(+)

diff --git a/sys/arm64/arm64/genassym.c b/sys/arm64/arm64/genassym.c
index a4db825e976c..8cbafba45a7d 100644
--- a/sys/arm64/arm64/genassym.c
+++ b/sys/arm64/arm64/genassym.c
@@ -58,6 +58,8 @@ ASSYM(PCB_TPIDRRO, offsetof(struct pcb, pcb_tpidrro_el0));
 ASSYM(PCB_ONFAULT, offsetof(struct pcb, pcb_onfault));
 ASSYM(PCB_FLAGS, offsetof(struct pcb, pcb_flags));
 
+ASSYM(P_PID, offsetof(struct proc, p_pid));
+
 ASSYM(SF_UC, offsetof(struct sigframe, sf_uc));
 
 ASSYM(TD_PROC, offsetof(struct thread, td_proc));
diff --git a/sys/arm64/arm64/swtch.S b/sys/arm64/arm64/swtch.S
index c683a7e25314..7b6010a5f51f 100644
--- a/sys/arm64/arm64/swtch.S
+++ b/sys/arm64/arm64/swtch.S
@@ -55,6 +55,21 @@
 999:
 .endm
 
+/*
+ * Lower 32 bits of CONTEXTIDR_EL1 are PID
+ * Upper 32 bits are reserved for future use e.g. TID
+ */
+.macro pid_in_context_idr
+	adrp	x9, arm64_pid_in_contextidr
+	ldrb	w10, [x9, :lo12:arm64_pid_in_contextidr]
+	cbz	w10, 998f
+	ldr	x9, [x1, #TD_PROC]
+	/* PID is always 0 or positive, do not sign extend */
+	ldr	w10, [x9, #P_PID]
+	msr	contextidr_el1, x10
+998:
+.endm
+
 /*
  * void cpu_throw(struct thread *old, struct thread *new)
  */
@@ -66,7 +81,10 @@ ENTRY(cpu_throw)
 	ldr	x4, [x0, #TD_PCB]
 	ldr	w5, [x4, #PCB_FLAGS]
 	clear_step_flag w5, x6
+
 1:
+	/* debug/trace: set CONTEXTIDR_EL1 to current PID, if enabled */
+	pid_in_context_idr
 
 #ifdef VFP
 	/* Backup the new thread pointer around a call to C code */
@@ -147,6 +165,9 @@ ENTRY(cpu_switch)
 	mov	x20, x1
 	mov	x21, x2
 
+	/* debug/trace: set CONTEXTIDR_EL1 to current PID, if enabled */
+	pid_in_context_idr
+
 #ifdef VFP
 	bl	vfp_save_state_switch
 	mov	x0, x20
diff --git a/sys/arm64/arm64/sys_machdep.c b/sys/arm64/arm64/sys_machdep.c
index efe2931e6b8a..33000b6c223b 100644
--- a/sys/arm64/arm64/sys_machdep.c
+++ b/sys/arm64/arm64/sys_machdep.c
@@ -30,6 +30,7 @@
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/proc.h>
+#include <sys/sysctl.h>
 #include <sys/sysproto.h>
 
 #include <vm/vm.h>
@@ -92,3 +93,8 @@ sysarch(struct thread *td, struct sysarch_args *uap)
 
 	return (error);
 }
+
+bool arm64_pid_in_contextidr = false;
+SYSCTL_BOOL(_machdep, OID_AUTO, pid_in_contextidr, CTLFLAG_RW,
+    &arm64_pid_in_contextidr, false,
+    "Save PID into CONTEXTIDR_EL1 register on context switch");