arm64/sme: ptrace support for TPIDR2_EL0
Subsystems (4)
v2 cover arm64/sme: ptrace support for TPIDR2_EL0
When SME was initially merged we did not add support for TPIDR2_EL0 to the ptrace interface, creating difficulties for debuggers in accessing lazy save state for ZA. This series implements that support, extending the existing NT_ARM_TLS regset to support the register when available, and adds kselftest coverage for the existing and new NT_ARM_TLS functionality.
Existing programs that query the size of the register set will be able to observe the increased size of the register set. Programs that assume the register set is single register will see no change. On systems that do not support SME TPIDR2_EL0 will read as 0 and writes will be ignored, support for SME should be queried via hwcaps as normal.
v2:
- Rebase onto v6.0-rc1.
Mark Brown (4):
kselftest/arm64: Add test coverage for NT_ARM_TLS arm64/ptrace: Document extension of NT_ARM_TLS to cover TPIDR2_EL0 arm64/ptrace: Support access to TPIDR2_EL0 kselftest/arm64: Add coverage of TPIDR2_EL0 ptrace interface
Documentation/arm64/sme.rst | 3 + arch/arm64/kernel/ptrace.c | 25 +- tools/testing/selftests/arm64/abi/.gitignore | 1 + tools/testing/selftests/arm64/abi/Makefile | 2 +- tools/testing/selftests/arm64/abi/ptrace.c | 241 +++++++++++++++++++ 5 files changed, 266 insertions(+), 6 deletions(-) create mode 100644 tools/testing/selftests/arm64/abi/ptrace.c
base-commit: 568035b01cfb107af8d2e4bd2fb9aea22cf5b868
Signature · 1 line
2.30.2
PATCH v2 1/4 kselftest/arm64: Add test coverage for NT_ARM_TLS
In preparation for extending support for NT_ARM_TLS to cover additional TPIDRs add some tests for the existing interface. Do this in a generic ptrace test program to provide a place to collect additional tests in the future.
--- tools/testing/selftests/arm64/abi/.gitignore | 1 + tools/testing/selftests/arm64/abi/Makefile | 2 +- tools/testing/selftests/arm64/abi/ptrace.c | 165 +++++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/arm64/abi/ptrace.c
tools/testing/selftests/arm64/abi/.gitignore +1 -0
@@ -1,2 +1,3 @@ +ptrace syscall-abi tpidr2
tools/testing/selftests/arm64/abi/Makefile +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # Copyright (C) 2021 ARM Limited -TEST_GEN_PROGS := syscall-abi tpidr2 +TEST_GEN_PROGS := ptrace syscall-abi tpidr2 include ../../lib.mk
tools/testing/selftests/arm64/abi/ptrace.c new file +165 -0
165 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH v2 1/4] kselftest/arm64: Add test coverage for NT_ARM_TLS
Quoted 40 lines · Mark Brown
In preparation for extending support for
NT_ARM_TLSto cover additional TPIDRs add some tests for the existing interface. Do this in a generic ptrace test program to provide a place to collect additional tests in the future.Signed-off-by Mark Brown <[email protected]>--- tools/testing/selftests/arm64/abi/.gitignore | 1 + tools/testing/selftests/arm64/abi/Makefile | 2 +- tools/testing/selftests/arm64/abi/ptrace.c | 165 +++++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-)create mode 100644
tools/testing/selftests/arm64/abi/ptrace.ctools/testing/selftests/arm64/abi/.gitignore +1
-0@@ -1,2 +1,3 @@ +ptrace syscall-abi tpidr2tools/testing/selftests/arm64/abi/Makefile +1
-1@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # Copyright (C) 2021 ARM Limited -TEST_GEN_PROGS := syscall-abi tpidr2 +TEST_GEN_PROGS := ptrace syscall-abi tpidr2 include ../../lib.mktools/testing/selftests/arm64/abi/ptrace.c new file +0
-0
tools/testing/selftests/arm64/abi/ptrace.c line 3
Quoted 3 lines
tools/testing/selftests/arm64/abi/ptrace.c new file +3
-0@@ -0,0 +1,165 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2021 ARM Limited.
I suppose 2021 -> 2022?
Quoted 162 lines
tools/testing/selftests/arm64/abi/ptrace.c new file +162
-0+*/ +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/auxv.h> +#include <sys/prctl.h> +#include <sys/ptrace.h> +#include <sys/types.h> +#include <sys/uio.h> +#include <sys/wait.h> +#include <asm/sigcontext.h> +#include <asm/ptrace.h> + +#include "../../kselftest.h" + +#define EXPECTED_TESTS 3 + +#define MAX_TPIDRS 1 + +static bool have_sme (void ) +{ +return getauxval (AT_HWCAP2 ) &HWCAP2_SME ; +} + +static void test_tpidr (pid_t child) +{ +uint64_t read_val[MAX_TPIDRS ]; +uint64_t write_val[MAX_TPIDRS ]; +struct iovec read_iov, write_iov; +int ret; + + read_iov.iov_base = read_val; + write_iov.iov_base = write_val; + +/* Should be able to read a single TPIDR... */ + read_iov.iov_len =sizeof (uint64_t ); + ret =ptrace (PTRACE_GETREGSET , child,NT_ARM_TLS , &read_iov); +ksft_test_result (ret ==0 ,"read_tpidr_one\n" ); + +/* ...write a new value.. */ + write_iov.iov_len =sizeof (uint64_t ); + write_val[0 ] = read_val[0 ]++; + ret =ptrace (PTRACE_SETREGSET , child,NT_ARM_TLS , &write_iov); +ksft_test_result (ret ==0 ,"write_tpidr_one\n" ); + +/* ...then read it back */ + ret =ptrace (PTRACE_GETREGSET , child,NT_ARM_TLS , &read_iov); +ksft_test_result (ret ==0 && write_val[0 ] == read_val[0 ], +"verify_tpidr_one\n" ); +} + +static int do_child (void ) +{ +if (ptrace (PTRACE_TRACEME ,-1 ,NULL ,NULL )) +ksft_exit_fail_msg ("PTRACE_TRACEME" ,strerror (errno)); + +if (raise (SIGSTOP )) +ksft_exit_fail_msg ("raise(SIGSTOP)" ,strerror (errno)); + +return EXIT_SUCCESS ; +} + +static int do_parent (pid_t child) +{ +int ret =EXIT_FAILURE ; +pid_t pid; +int status; +siginfo_t si; + +/* Attach to the child */ +while (1 ) { +int sig; + + pid =wait (&status); +if (pid ==-1 ) { +perror ("wait" ); + goto error; + } + +/* +* This should never happen but it's hard to flag in +* the framework. +*/ +if (pid != child) +continue ; + +if (WIFEXITED (status) ||WIFSIGNALED (status)) +ksft_exit_fail_msg ("Child died unexpectedly\n" ); + +if (!WIFSTOPPED (status)) + goto error; + + sig =WSTOPSIG (status); + +if (ptrace (PTRACE_GETSIGINFO , pid,NULL , &si)) { +if (errno ==ESRCH ) + goto disappeared; + +if (errno ==EINVAL ) { + sig =0 ;/* bust group-stop */ + goto cont; + } + +ksft_test_result_fail ("PTRACE_GETSIGINFO: %s\n" , +strerror (errno)); + goto error; + } + +if (sig ==SIGSTOP && si.si_code ==SI_TKILL && + si.si_pid == pid) +break ; + + cont: +if (ptrace (PTRACE_CONT , pid,NULL , sig)) { +if (errno ==ESRCH ) + goto disappeared; + +ksft_test_result_fail ("PTRACE_CONT: %s\n" , +strerror (errno)); + goto error; + } + } + +ksft_print_msg ("Parent is %d, child is %d\n" ,getpid (), child); + +test_tpidr (child); + + ret =EXIT_SUCCESS ; + +error: +kill (child,SIGKILL ); + +disappeared: +return ret; +} + +int main (void ) +{ +int ret =EXIT_SUCCESS ; +pid_t child; + +srandom (getpid ()); + +ksft_print_header (); + +ksft_set_plan (EXPECTED_TESTS ); + + child =fork (); +if (!child) +return do_child (); + +if (do_parent (child)) + ret =EXIT_FAILURE ; + +ksft_print_cnts (); + +return ret; +}
PATCH v2 2/4 arm64/ptrace: Document extension of NT_ARM_TLS to cover TPIDR2_EL0
In reply to Mark BrownIn order to allow debuggers to discover lazily saved SME state we need to provide access to TPIDR2_EL0, we will extend the existing NT_ARM_TLS used for TPIDR to also include TPIDR2_EL0 as the second register in the regset.
--- Documentation/arm64/sme.rst | 3 +++ 1 file changed, 3 insertions(+)
Documentation/arm64/sme.rst +3 -0
@@ -331,6 +331,9 @@ The regset data starts with struct user_za_header, containing: been read if a PTRACE_GETREGSET of NT_ARM_ZA were executed for each thread when the coredump was generated. +* The NT_ARM_TLS note will be extended to two registers, the second register + will contain TPIDR2_EL0 on systems that support SME and will be read as + zero with writes ignored otherwise. 9. System runtime configuration --------------------------------
Signature · 1 line
2.30.2
Re: [PATCH v2 2/4] arm64/ptrace: Document extension of NT_ARM_TLS to cover TPIDR2_EL0
Quoted 15 lines · Mark Brown
In order to allow debuggers to discover lazily saved SME state we need to provide access to
TPIDR2_EL0, we will extend the existingNT_ARM_TLSused for TPIDR to also includeTPIDR2_EL0as the second register in the regset.Signed-off-by Mark Brown <[email protected]>--- Documentation/arm64/sme.rst | 3 +++ 1 file changed, 3 insertions(+)Documentation/arm64/sme.rst +0
-0
Documentation/arm64/sme.rst The regset data starts with struct user_za_header, containing: · line 339
Quoted 9 lines
Documentation/arm64/sme.rst +3
-0@@ -331,6 +331,9 @@ The regset data starts with struct user_za_header, containing: been read if a PTRACE_GETREGSET of NT_ARM_ZA were executed for each thread when the coredump was generated. +* The NT_ARM_TLS note will be extended to two registers, the second register + will contain TPIDR2_EL0 on systems that support SME and will be read as + zero with writes ignored otherwise. 9. System runtime configuration --------------------------------
I wonder if we should document a bit more about the use of TPIDR2, its states, values and block format when TPIDR2 points to valid ZA state.
Would that make sense?
Re: [PATCH v2 2/4] arm64/ptrace: Document extension of NT_ARM_TLS to cover TPIDR2_EL0
Quoted 10 lines · Luis Machado
Documentation/arm64/sme.rst The regset data starts with struct user_za_header, containing: · line 336
Quoted 3 lines · Mark Brown
Documentation/arm64/sme.rst +3
-0+* The NT_ARM_TLS note will be extended to two registers, the second register + will contain TPIDR2_EL0 on systems that support SME and will be read as + zero with writes ignored otherwise.I wonder if we should document a bit more about the use of TPIDR2, its states, values and block format when TPIDR2 points to valid ZA state.
Would that make sense?
That seems somewhat out of scope for the kernel, it's doesn't have any idea about the use of TPIDR2 and I'm not sure we want to give anyone the impression that it might try to do anything clever with it. From the perspective of the kernel ABI this is simply another TPIDR that needs to be context switched, I have heard some suggestions that the kernel should try to do a spill to the TPIDR2 block allocated in user memory directly but that feels like it's getting a bit more hairy than we want and problematic for anyone doing off piste with regard to ABI.
Also note that the spec for TPIDR2 didn't get merged into the PCS yet so it's not quite finalised.
PATCH v2 3/4 arm64/ptrace: Support access to TPIDR2_EL0
In reply to Mark BrownSME introduces an additional EL0 register, TPIDR2_EL0, intended for use by userspace as part of the SME. Provide ptrace access to it through the existing NT_ARM_TLS regset used for TPIDR_EL0 by expanding it to two registers with TPIDR2_EL0 being the second one.
Existing programs that query the size of the register set will be able to observe the increased size of the register set. Programs that assume the register set is single register will see no change. On systems that do not support SME TPIDR2_EL0 will read as 0 and writes will be ignored, support for SME should be queried via hwcaps as normal.
--- arch/arm64/kernel/ptrace.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
arch/arm64/kernel/ptrace.c +20 -5
@@ -666,10 +666,18 @@ static int fpr_set(struct task_struct *target, const struct user_regset *regset,static int tls_get (struct task_struct *target,const struct user_regset *regset,struct membuf to) { +int ret; +if (target == current)tls_preserve_current_state (); -return membuf_store (&to, target->thread.uw.tp_value); + ret =membuf_store (&to, target->thread.uw.tp_value); +if (system_supports_tpidr2 ()) + ret =membuf_store (&to, target->thread.tpidr2_el0); +else + ret =membuf_zero (&to,sizeof (u64)); + +return ret; } static int tls_set(struct task_struct *target, const struct user_regset *regset,
@@ -677,13 +685,20 @@ static int tls_set(struct task_struct *target, const struct user_regset *regset, const void *kbuf, const void __user *ubuf) { int ret; -unsigned long tls = target->thread.uw.tp_value; +unsigned long tls[2 ]; - ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, &tls,0 ,-1 ); + tls[0 ] = target->thread.uw.tp_value; +if (system_supports_sme ()) + tls[1 ] = target->thread.tpidr2_el0; + + ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, tls,0 , count);if (ret)return ret; - target->thread.uw.tp_value = tls; + target->thread.uw.tp_value = tls[0 ]; +if (system_supports_sme ()) + target->thread.tpidr2_el0 = tls[1 ]; +return ret; }
@@ -1392,7 +1407,7 @@ static const struct user_regset aarch64_regsets[] = { }, [REGSET_TLS] = { .core_note_type = NT_ARM_TLS, - .n = 1, + .n = 2, .size = sizeof(void *), .align = sizeof(void *), .regset_get = tls_get,
Signature · 1 line
2.30.2
Re: [PATCH v2 3/4] arm64/ptrace: Support access to TPIDR2_EL0
Quoted 65 lines · Mark Brown
SME introduces an additional EL0 register,
TPIDR2_EL0, intended for use by userspace as part of the SME. Provide ptrace access to it through the existingNT_ARM_TLSregset used forTPIDR_EL0by expanding it to two registers withTPIDR2_EL0being the second one.Existing programs that query the size of the register set will be able to observe the increased size of the register set. Programs that assume the register set is single register will see no change. On systems that do not support SME
TPIDR2_EL0will read as 0 and writes will be ignored, support for SME should be queried via hwcaps as normal.Signed-off-by Mark Brown <[email protected]>--- arch/arm64/kernel/ptrace.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)arch/arm64/kernel/ptrace.c +19
-4@@ -666,10 +666,18 @@ static int fpr_set(struct task_struct *target, const struct user_regset *regset,static int tls_get (struct task_struct *target,const struct user_regset *regset,struct membuf to) { +int ret; +if (target == current)tls_preserve_current_state (); -return membuf_store (&to, target->thread.uw.tp_value); + ret =membuf_store (&to, target->thread.uw.tp_value); +if (system_supports_tpidr2 ()) + ret =membuf_store (&to, target->thread.tpidr2_el0); +else + ret =membuf_zero (&to,sizeof (u64)); + +return ret; } static int tls_set(struct task_struct *target, const struct user_regset *regset,@@ -677,13 +685,20 @@ static int tls_set(struct task_struct *target, const struct user_regset *regset, const void *kbuf, const void __user *ubuf) { int ret; -unsigned long tls = target->thread.uw.tp_value; +unsigned long tls[2 ]; - ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, &tls,0 ,-1 ); + tls[0 ] = target->thread.uw.tp_value; +if (system_supports_sme ()) + tls[1 ] = target->thread.tpidr2_el0; + + ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, tls,0 , count);if (ret)return ret; - target->thread.uw.tp_value = tls; + target->thread.uw.tp_value = tls[0 ]; +if (system_supports_sme ()) + target->thread.tpidr2_el0 = tls[1 ]; +return ret; }
arch/arm64/kernel/ptrace.c static const struct user_regset aarch64_regsets[] = { · line 1413
Quoted 8 lines
arch/arm64/kernel/ptrace.c +1
-1@@ -1392,7 +1407,7 @@ static const struct user_regset aarch64_regsets[] = { }, [REGSET_TLS] = { .core_note_type = NT_ARM_TLS, - .n = 1, + .n = 2, .size = sizeof(void *), .align = sizeof(void *), .regset_get = tls_get,
This looks good from GDB's perspective. I tried it with an unpatched GDB and it still works as it should.
TPIDR can be read correctly. I'll check TPIDR2 once it gets implemented.
Thanks,
Luis
PATCH v2 4/4 kselftest/arm64: Add coverage of TPIDR2_EL0 ptrace interface
In reply to Mark BrownExtend the ptrace test support for NT_ARM_TLS to cover TPIDR2_EL0 - on systems that support SME the NT_ARM_TLS regset can be up to 2 elements long with the second element containing TPIDR2_EL0. On systems supporting SME we verify that this value can be read and written while on systems that do not support SME we verify correct truncation of reads and writes.
--- tools/testing/selftests/arm64/abi/ptrace.c | 82 +++++++++++++++++++++- 1 file changed, 79 insertions(+), 3 deletions(-)
tools/testing/selftests/arm64/abi/ptrace.c +79 -3
@@ -20,9 +20,9 @@#include "../../kselftest.h" -#define EXPECTED_TESTS 3 +#define EXPECTED_TESTS 7 -#define MAX_TPIDRS 1 +#define MAX_TPIDRS 2static bool have_sme (void ) {
@@ -34,7 +34,8 @@ static void test_tpidr(pid_t child)uint64_t read_val[MAX_TPIDRS ];uint64_t write_val[MAX_TPIDRS ];struct iovec read_iov, write_iov; -int ret; +bool test_tpidr2 = false; +int ret, i; read_iov.iov_base = read_val; write_iov.iov_base = write_val;
@@ -54,6 +55,81 @@ static void test_tpidr(pid_t child) ret =ptrace (PTRACE_GETREGSET , child,NT_ARM_TLS , &read_iov);ksft_test_result (ret ==0 && write_val[0 ] == read_val[0 ],"verify_tpidr_one\n" ); + +/* If we have TPIDR2 we should be able to read it */ + read_iov.iov_len =sizeof (read_val); + ret =ptrace (PTRACE_GETREGSET , child,NT_ARM_TLS , &read_iov); +if (ret ==0 ) { +/* If we have SME there should be two TPIDRs */ +if (read_iov.iov_len >=sizeof (read_val)) + test_tpidr2 = true; + +if (have_sme () && test_tpidr2) { +ksft_test_result (test_tpidr2,"count_tpidrs\n" ); + }else { +ksft_test_result (read_iov.iov_len %sizeof (uint64_t ) ==0 , +"count_tpidrs\n" ); + } + }else { +ksft_test_result_fail ("count_tpidrs\n" ); + } + +if (test_tpidr2) { +/* Try to write new values to all known TPIDRs... */ + write_iov.iov_len =sizeof (write_val); +for (i =0 ; i <MAX_TPIDRS ; i++) + write_val[i] = read_val[i] +1 ; + ret =ptrace (PTRACE_SETREGSET , child,NT_ARM_TLS , &write_iov); + +ksft_test_result (ret ==0 && + write_iov.iov_len ==sizeof (write_val), +"tpidr2_write\n" ); + +/* ...then read them back */ + read_iov.iov_len =sizeof (read_val); + ret =ptrace (PTRACE_GETREGSET , child,NT_ARM_TLS , &read_iov); + +if (have_sme ()) { +/* Should read back the written value */ +ksft_test_result (ret ==0 && + read_iov.iov_len >=sizeof (read_val) && +memcmp (read_val, write_val, +sizeof (read_val)) ==0 , +"tpidr2_read\n" ); + }else { +/* TPIDR2 should read as zero */ +ksft_test_result (ret ==0 && + read_iov.iov_len >=sizeof (read_val) && + read_val[0 ] == write_val[0 ] && + read_val[1 ] ==0 , +"tpidr2_read\n" ); + } + +/* Writing only TPIDR... */ + write_iov.iov_len =sizeof (uint64_t ); +memcpy (write_val, read_val,sizeof (read_val)); + write_val[0 ] +=1 ; + ret =ptrace (PTRACE_SETREGSET , child,NT_ARM_TLS , &write_iov); + +if (ret ==0 ) { +/* ...should leave TPIDR2 untouched */ + read_iov.iov_len =sizeof (read_val); + ret =ptrace (PTRACE_GETREGSET , child,NT_ARM_TLS , + &read_iov); + +ksft_test_result (ret ==0 && + read_iov.iov_len >=sizeof (read_val) && +memcmp (read_val, write_val, +sizeof (read_val)) ==0 , +"write_tpidr_only\n" ); + }else { +ksft_test_result_fail ("write_tpidr_only\n" ); + } + }else { +ksft_test_result_skip ("tpidr2_write\n" ); +ksft_test_result_skip ("tpidr2_read\n" ); +ksft_test_result_skip ("write_tpidr_only\n" ); + } }static int do_child (void )
Signature · 1 line
2.30.2