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;

Reviewed by:	andrew
Sponsored by:   Arm Ltd
Differential Revision:	https://reviews.freebsd.org/D46192
This commit is contained in:
Zachary Leaf 2024-07-31 09:23:24 +01:00 committed by Andrew Turner
parent 610348a904
commit f05795e3f6
3 changed files with 26 additions and 1 deletions

View file

@ -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(PR_PID, offsetof(struct proc, p_pid));
ASSYM(SF_UC, offsetof(struct sigframe, sf_uc));
ASSYM(TD_PROC, offsetof(struct thread, td_proc));

View file

@ -55,6 +55,15 @@
999:
.endm
.macro pid_in_context_idr label
adrp x9, arm64_pid_in_contextidr
ldr x10, [x9, :lo12:arm64_pid_in_contextidr]
cbz x10, \label
ldr x9, [x1, #TD_PROC]
ldr x10, [x9, #PR_PID]
msr contextidr_el1, x10
.endm
/*
* void cpu_throw(struct thread *old, struct thread *new)
*/
@ -66,8 +75,12 @@ ENTRY(cpu_throw)
ldr x4, [x0, #TD_PCB]
ldr w5, [x4, #PCB_FLAGS]
clear_step_flag w5, x6
1:
1:
/* debug/trace: set CONTEXTIDR_EL1 to current PID, if enabled */
pid_in_context_idr 2f
2:
#ifdef VFP
/* Backup the new thread pointer around a call to C code */
mov x19, x1
@ -147,6 +160,10 @@ ENTRY(cpu_switch)
mov x20, x1
mov x21, x2
/* debug/trace: set CONTEXTIDR_EL1 to current PID, if enabled */
pid_in_context_idr 0f
0:
#ifdef VFP
bl vfp_save_state_switch
mov x0, x20

View file

@ -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>
@ -80,3 +81,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");