From patchwork Tue Jun 25 12:24:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 807392 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C84DF1553AF; Tue, 25 Jun 2024 12:24:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318285; cv=none; b=EO8nWxkfE6sLvDwRvuvDvla/rWwI4Gg967YIKX0C8Wk6ljLUmcsVoe+rJQCo29ww2CEWF+MgWjQhsz+eo0SbC8MjU4e+G/6cSRFGcYOLAJCNbf2ZKX9dv1gPwzWPyAOWV1gt2DeAGpmY+3kqRHgBF6Hp/+rKsMH9mIiMrJ56pcs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318285; c=relaxed/simple; bh=1PSrTGcZOk8NrG6nmdRKoCA3QkgwzXuWtNM9SyFayRI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fknvmXIR0SjResHu9Qps1ROsVZxdFfdO/houUMQUu+fV42IpUUpHvtrp5YXRraza19SPrAKqV0BAgR3L2CMbhgW33aT2uRN/jmKR84qGff4The9EdIXjHSGk3aqavSbWnK+jdVqj54K156wA8VgSYkc73ntLErmXWvfCTNGn7Z4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1F259339; Tue, 25 Jun 2024 05:25:08 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.41.12]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 370573F766; Tue, 25 Jun 2024 05:24:37 -0700 (PDT) From: Dev Jain To: shuah@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kselftest@vger.kernel.org, Catalin.Marinas@arm.com, will@kernel.org Cc: broonie@kernel.org, ryan.roberts@arm.com, rob.herring@arm.com, mark.rutland@arm.com, linux@armlinux.org.uk, suzuki.poulose@arm.com, Anshuman.Khandual@arm.com, aneesh.kumar@kernel.org, linux-kernel@vger.kernel.org, Dev Jain Subject: [PATCH v3 2/9] selftests/arm: Add elf test Date: Tue, 25 Jun 2024 17:54:01 +0530 Message-Id: <20240625122408.1439097-3-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240625122408.1439097-1-dev.jain@arm.com> References: <20240625122408.1439097-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch introduces an ELF parsing test. A basic sanity check is required to ensure that we are actually testing a 32-bit build. Signed-off-by: Dev Jain --- v2->v3: - Introduce two more testcases tools/testing/selftests/arm/elf/parse_elf.c | 77 +++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tools/testing/selftests/arm/elf/parse_elf.c diff --git a/tools/testing/selftests/arm/elf/parse_elf.c b/tools/testing/selftests/arm/elf/parse_elf.c new file mode 100644 index 000000000000..6eb8fb91f6a7 --- /dev/null +++ b/tools/testing/selftests/arm/elf/parse_elf.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 ARM Limited + * + * Author : Dev Jain + * + * Parse elf header to confirm 32-bit process + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include + +/* The ELF file header. This appears at the start of every ELF file. */ + +struct elf_header { + unsigned char e_ident[16]; /* Magic number and other info */ + uint16_t e_type; /* Object file type */ + uint16_t e_machine; /* Architecture */ + uint32_t e_version; /* Object file version */ + uint32_t e_entry; /* Entry point virtual address */ + uint32_t e_phoff; /* Program header table file offset */ + uint32_t e_shoff; /* Section header table file offset */ + uint32_t e_flags; /* Processor-specific flags */ + uint16_t e_ehsize; /* ELF header size in bytes */ + uint16_t e_phentsize; /* Program header table entry size */ + uint16_t e_phnum; /* Program header table entry count */ + uint16_t e_shentsize; /* Section header table entry size */ + uint16_t e_shnum; /* Section header table entry count */ + uint16_t e_shstrndx; /* Section header string table index */ +}; + +#define ELFCLASS32 1 +#define EM_ARM 40 + +void read_elf_header(const char *elfFile) +{ + struct elf_header header; + FILE *file; + int ret; + + file = fopen(elfFile, "r"); + if (!file) + ksft_exit_fail_perror("/proc/self/exe"); + + /* store header in struct */ + if (fread(&header, 1, sizeof(header), file) != sizeof(header)) + ksft_exit_fail_perror("fread"); + + if (fclose(file)) + ksft_exit_fail_perror("fclose"); + + /* sanity check: does it really follow ELF format */ + ret = (header.e_ident[0] == 0x7f && header.e_ident[1] == 'E' + && header.e_ident[2] == 'L' && header.e_ident[3] == 'F'); + + ksft_test_result(ret == 1, "Follows ELF format\n"); + + ksft_test_result(header.e_ident[4] == ELFCLASS32, "ELF is 32 bit\n"); + + ksft_test_result(header.e_machine == EM_ARM, "Machine type\n"); +} + +int main(int argc, char *argv[]) +{ + ksft_print_header(); + ksft_set_plan(3); + + read_elf_header("/proc/self/exe"); + + ksft_finished(); +} From patchwork Tue Jun 25 12:24:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 807391 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AE640156679; Tue, 25 Jun 2024 12:24:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318296; cv=none; b=FMB9mMklRTOjojBkf/TBfYH02N15nPa1k35mo8cDWg1TL/2q9PUhlFjwi5wirfr4oc8JSJSJaxZcraYeQlM+al+5ItyIw40yQ58AWbFyprisjluv/6CH1y9oP2e35CIhVGFu4Km5aR/wmZLSqLcEaIDGc4hG3jWKThE3BmCmYQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318296; c=relaxed/simple; bh=Tm8cM8AhVmCOmUWGzLCuc4tp8hTRTQMFMct6WXa0saM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hRbFCjFvs4s7zZdHRQ8FWrqtdHy4njHRtQhfw1oTXiyJ5rFfTghDPD4C/GAC1Zj/PGDFWVHglnQA329HcAMbrTSJIkTGy9HR3oWitwlnNGZNoakPLO3TG8tmdfd+YwfnPmhXo8/QUiEM6p7pVsk4P8VgUyec0l4FwArZbYS+9aA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id F011D339; Tue, 25 Jun 2024 05:25:18 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.41.12]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 06D743F766; Tue, 25 Jun 2024 05:24:48 -0700 (PDT) From: Dev Jain To: shuah@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kselftest@vger.kernel.org, Catalin.Marinas@arm.com, will@kernel.org Cc: broonie@kernel.org, ryan.roberts@arm.com, rob.herring@arm.com, mark.rutland@arm.com, linux@armlinux.org.uk, suzuki.poulose@arm.com, Anshuman.Khandual@arm.com, aneesh.kumar@kernel.org, linux-kernel@vger.kernel.org, Dev Jain Subject: [PATCH v3 4/9] selftests/arm: Add signal tests Date: Tue, 25 Jun 2024 17:54:03 +0530 Message-Id: <20240625122408.1439097-5-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240625122408.1439097-1-dev.jain@arm.com> References: <20240625122408.1439097-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch introduces two signal tests, and generic test wrappers similar to selftests/arm64/signal directory, along with the mangling testcases found therein. arm_cpsr, dumped by the kernel to user space in the ucontext structure to the signal handler, is mangled with. The kernel must spot this illegal attempt and the testcases are expected to terminate via SEGV. Signed-off-by: Dev Jain --- .../testcases/mangle_cpsr_invalid_aif_bits.c | 33 +++++++++++++++++++ .../mangle_cpsr_invalid_compat_toggle.c | 29 ++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_aif_bits.c create mode 100644 tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_compat_toggle.c diff --git a/tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_aif_bits.c b/tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_aif_bits.c new file mode 100644 index 000000000000..ea73a96fb229 --- /dev/null +++ b/tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_aif_bits.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 ARM Limited + * + * Try to mangle the ucontext from inside a signal handler, mangling the + * AIF bits in an illegal manner: this attempt must be spotted by Kernel + * and the test case is expected to be terminated via SEGV. + * + */ + +#include "test_signals_utils.h" + +static int mangle_invalid_cpsr_run(struct tdescr *td, siginfo_t *si, + ucontext_t *uc) +{ + + /* + * This config should trigger a SIGSEGV by Kernel when it checks + * the sigframe consistency in valid_user_regs() routine. + */ + uc->uc_mcontext.arm_cpsr |= PSR_A_BIT | PSR_I_BIT | PSR_F_BIT; + + return 1; +} + +struct tdescr tde = { + .sanity_disabled = true, + .name = "MANGLE_CPSR_INVALID_AIF_BITS", + .descr = "Mangling uc_mcontext with INVALID AIF_BITS", + .sig_trig = SIGUSR1, + .sig_ok = SIGSEGV, + .run = mangle_invalid_cpsr_run, +}; diff --git a/tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_compat_toggle.c b/tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_compat_toggle.c new file mode 100644 index 000000000000..f7ccbccb24e5 --- /dev/null +++ b/tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_compat_toggle.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 ARM Limited + * + * Try to mangle the ucontext from inside a signal handler, toggling + * the execution state bit: this attempt must be spotted by Kernel and + * the test case is expected to be terminated via SEGV. + */ + +#include "test_signals_utils.h" + +static int mangle_invalid_cpsr_run(struct tdescr *td, siginfo_t *si, + ucontext_t *uc) +{ + + /* This config should trigger a SIGSEGV by Kernel */ + uc->uc_mcontext.arm_cpsr ^= MODE32_BIT; + + return 1; +} + +struct tdescr tde = { + .sanity_disabled = true, + .name = "MANGLE_CPSR_INVALID_STATE_TOGGLE", + .descr = "Mangling uc_mcontext with INVALID STATE_TOGGLE", + .sig_trig = SIGUSR1, + .sig_ok = SIGSEGV, + .run = mangle_invalid_cpsr_run, +}; From patchwork Tue Jun 25 12:24:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 807390 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 78003149DE2; Tue, 25 Jun 2024 12:25:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318307; cv=none; b=Fc5HPofwIC+YjrGhSDZMEFcWrqJdfbTp8VPx27gYCK2sE1rMzHdMCCX3FSYLFaJUkftkH4brAj9/Fu7azWvqd7AjdrPmy/Yq1gJPNUi2YHjI9tBs+D4V2UGLcPH+lKgqw9rjCdMIOLnwLd0laOhol4ix7YpdYbfNbcaH75scMnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318307; c=relaxed/simple; bh=IlZp6wQnbI/JouxeEG5y1ZenUcixYpEag24ZP+83ciM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CRNPDG6FZUvN0rnQ+PCxyT3dRxZUrDymQVZBc7/ca4krRCdfQs1m7KQV6EHXwlXbtPuJWXXuPjwbBmdZ8Piedrs9+1/0YQGFlYXezZxKVNiFlklVRjExb4cT2nicFi+qLw04dTwmncj6G7wQdkOcyhKSROZcnRZZQat5yHE58+0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CC2A9339; Tue, 25 Jun 2024 05:25:29 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.41.12]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 08F493F766; Tue, 25 Jun 2024 05:24:59 -0700 (PDT) From: Dev Jain To: shuah@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kselftest@vger.kernel.org, Catalin.Marinas@arm.com, will@kernel.org Cc: broonie@kernel.org, ryan.roberts@arm.com, rob.herring@arm.com, mark.rutland@arm.com, linux@armlinux.org.uk, suzuki.poulose@arm.com, Anshuman.Khandual@arm.com, aneesh.kumar@kernel.org, linux-kernel@vger.kernel.org, Dev Jain Subject: [PATCH v3 6/9] selftests/arm64: Split ptrace, use ifdeffery Date: Tue, 25 Jun 2024 17:54:05 +0530 Message-Id: <20240625122408.1439097-7-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240625122408.1439097-1-dev.jain@arm.com> References: <20240625122408.1439097-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Split some of the common code to be used by selftests/arm, into ptrace.h. Signed-off-by: Dev Jain --- tools/testing/selftests/arm64/abi/ptrace.c | 121 ++---------------- tools/testing/selftests/arm64/abi/ptrace.h | 135 +++++++++++++++++++++ 2 files changed, 145 insertions(+), 111 deletions(-) create mode 100644 tools/testing/selftests/arm64/abi/ptrace.h diff --git a/tools/testing/selftests/arm64/abi/ptrace.c b/tools/testing/selftests/arm64/abi/ptrace.c index c83f0441e9d0..9e8494d950fe 100644 --- a/tools/testing/selftests/arm64/abi/ptrace.c +++ b/tools/testing/selftests/arm64/abi/ptrace.c @@ -18,15 +18,22 @@ #include #include +#include "ptrace.h" #include "../../kselftest.h" #define EXPECTED_TESTS 11 #define MAX_TPIDRS 2 -static bool have_sme(void) +static int do_child(void) { - return getauxval(AT_HWCAP2) & HWCAP2_SME; + if (ptrace(PTRACE_TRACEME, -1, NULL, NULL)) + ksft_exit_fail_perror("PTRACE_TRACEME"); + + if (raise(SIGSTOP)) + ksft_exit_fail_perror("raise(SIGSTOP)"); + + return EXIT_SUCCESS; } static void test_tpidr(pid_t child) @@ -132,119 +139,11 @@ static void test_tpidr(pid_t child) } } -static void test_hw_debug(pid_t child, int type, const char *type_name) -{ - struct user_hwdebug_state state; - struct iovec iov; - int slots, arch, ret; - - iov.iov_len = sizeof(state); - iov.iov_base = &state; - - /* Should be able to read the values */ - ret = ptrace(PTRACE_GETREGSET, child, type, &iov); - ksft_test_result(ret == 0, "read_%s\n", type_name); - - if (ret == 0) { - /* Low 8 bits is the number of slots, next 4 bits the arch */ - slots = state.dbg_info & 0xff; - arch = (state.dbg_info >> 8) & 0xf; - - ksft_print_msg("%s version %d with %d slots\n", type_name, - arch, slots); - - /* Zero is not currently architecturally valid */ - ksft_test_result(arch, "%s_arch_set\n", type_name); - } else { - ksft_test_result_skip("%s_arch_set\n", type_name); - } -} - -static int do_child(void) -{ - if (ptrace(PTRACE_TRACEME, -1, NULL, NULL)) - ksft_exit_fail_perror("PTRACE_TRACEME"); - - if (raise(SIGSTOP)) - ksft_exit_fail_perror("raise(SIGSTOP)"); - - return EXIT_SUCCESS; -} - -static int do_parent(pid_t child) +static void run_tests(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); test_hw_debug(child, NT_ARM_HW_WATCH, "NT_ARM_HW_WATCH"); test_hw_debug(child, NT_ARM_HW_BREAK, "NT_ARM_HW_BREAK"); - - ret = EXIT_SUCCESS; - -error: - kill(child, SIGKILL); - -disappeared: - return ret; } int main(void) diff --git a/tools/testing/selftests/arm64/abi/ptrace.h b/tools/testing/selftests/arm64/abi/ptrace.h new file mode 100644 index 000000000000..ae65c58cd3bf --- /dev/null +++ b/tools/testing/selftests/arm64/abi/ptrace.h @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 ARM Limited. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" + +static void run_tests(pid_t child); + +static int do_child(void); + +#ifdef __aarch64__ +static bool have_sme(void) +{ + return getauxval(AT_HWCAP2) & HWCAP2_SME; +} + +static void test_hw_debug(pid_t child, int type, const char *type_name) +{ + struct user_hwdebug_state state; + struct iovec iov; + int slots, arch, ret; + + iov.iov_len = sizeof(state); + iov.iov_base = &state; + + /* Should be able to read the values */ + ret = ptrace(PTRACE_GETREGSET, child, type, &iov); + ksft_test_result(ret == 0, "read_%s\n", type_name); + + if (ret == 0) { + /* Low 8 bits is the number of slots, next 4 bits the arch */ + slots = state.dbg_info & 0xff; + arch = (state.dbg_info >> 8) & 0xf; + + ksft_print_msg("%s version %d with %d slots\n", type_name, + arch, slots); + + /* Zero is not currently architecturally valid */ + ksft_test_result(arch, "%s_arch_set\n", type_name); + } else { + ksft_test_result_skip("%s_arch_set\n", type_name); + } +} +#endif + +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 (sig == SIGTRAP) + ksft_print_msg("Child received SIGTRAP\n"); + + if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &si)) { + if (errno == ESRCH) + goto disappeared; + + if (errno == EINVAL) + 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: + /* bust group-stop */ + if (ptrace(PTRACE_CONT, pid, NULL, 0)) { + 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); + + ret = EXIT_SUCCESS; + run_tests(child); + +error: + kill(child, SIGKILL); + +disappeared: + return ret; +} From patchwork Tue Jun 25 12:24:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 807389 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1140715A85B; Tue, 25 Jun 2024 12:25:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318317; cv=none; b=KSJZzjc9iozJNQ6bc1wMQRRrOTw9W0qHiY47Q6AKI6euQRJjct1CVK2eAjWsJ1TXEAsVThP3oU73MsbMWKx94gz6gBaTl4WbTABqqd6LR94F66nZG51NDRjJl4ozxp1/n26Fbp7263MoZpkJlGcfxvXTNKtElYTMLUThBKLDC3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318317; c=relaxed/simple; bh=APogV9FZtDCsWPwBH6klc3BpkCkJIW4jO0JO4TfYsxA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dhwVuQaFXyuMi4RXRGUQwAJaU+EviI6ynQinsLQlySNfICciNjBN1FkZeziyucGV59cSMuCJktGRuEQpHyHCBToPp1r4W/yIGHWM1hTtzLvlmP7Oy/z1he9dUimYCD2hRRAd+7Okn8vLNju1zhH+BbHA+a4WG/p9D/1kMng8Ax0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6ACA7339; Tue, 25 Jun 2024 05:25:40 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.41.12]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E5FD33F766; Tue, 25 Jun 2024 05:25:10 -0700 (PDT) From: Dev Jain To: shuah@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kselftest@vger.kernel.org, Catalin.Marinas@arm.com, will@kernel.org Cc: broonie@kernel.org, ryan.roberts@arm.com, rob.herring@arm.com, mark.rutland@arm.com, linux@armlinux.org.uk, suzuki.poulose@arm.com, Anshuman.Khandual@arm.com, aneesh.kumar@kernel.org, linux-kernel@vger.kernel.org, Dev Jain Subject: [PATCH v3 8/9] selftests/arm: Add ptrace_64 test Date: Tue, 25 Jun 2024 17:54:07 +0530 Message-Id: <20240625122408.1439097-9-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240625122408.1439097-1-dev.jain@arm.com> References: <20240625122408.1439097-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For a 64-bit parent debugging a 32-bit child, add tests for reading the TLS registers, and mangling with the mode bits in CPSR. Signed-off-by: Dev Jain --- tools/testing/selftests/arm/abi/ptrace_64.c | 91 +++++++++++++++++++ .../selftests/arm/abi/trivial_32bit_program.c | 14 +++ 2 files changed, 105 insertions(+) create mode 100644 tools/testing/selftests/arm/abi/ptrace_64.c create mode 100644 tools/testing/selftests/arm/abi/trivial_32bit_program.c diff --git a/tools/testing/selftests/arm/abi/ptrace_64.c b/tools/testing/selftests/arm/abi/ptrace_64.c new file mode 100644 index 000000000000..97398cf59052 --- /dev/null +++ b/tools/testing/selftests/arm/abi/ptrace_64.c @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 ARM Limited. + * + * Inspired from selftests/arm64/abi/ptrace.c + * + * Author: Dev Jain + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ptrace.h" +#include "../../kselftest.h" + +#define EXPECTED_TESTS 12 +#define NUM_TLS_REGS 2 + +static void test_tpidr(pid_t child) +{ + unsigned int read_val[NUM_TLS_REGS]; + struct iovec read_iov; + int ret; + + memset(read_val, 0, sizeof(read_val)); + + read_iov.iov_base = read_val; + + /* Should be able to read a single TLS register... */ + read_iov.iov_len = 2 * sizeof(unsigned int); + ret = ptrace(PTRACE_GETREGSET, child, NT_ARM_TLS, &read_iov); + ksft_test_result(ret == 0, "read_tls\n"); + + ksft_test_result(read_val[0], "read_tls_1\n"); + ksft_test_result(!read_val[1], "cannot read_tls_2\n"); +} + +static void run_tests(pid_t child) +{ + test_tpidr(child); + test_user_regs(child); + test_hw_debug(child, NT_ARM_HW_WATCH, "NT_ARM_HW_WATCH"); + test_hw_debug(child, NT_ARM_HW_BREAK, "NT_ARM_HW_BREAK"); +} + +static int do_child(void) +{ + if (ptrace(PTRACE_TRACEME, -1, NULL, NULL)) + ksft_exit_fail_perror("PTRACE_TRACEME"); + + /* SIGTRAP makes the child stop after exec; do_parent() resumes it */ + execv("trivial_32bit_program", NULL); + return EXIT_SUCCESS; +} + +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; +} diff --git a/tools/testing/selftests/arm/abi/trivial_32bit_program.c b/tools/testing/selftests/arm/abi/trivial_32bit_program.c new file mode 100644 index 000000000000..c5ad7abb23ed --- /dev/null +++ b/tools/testing/selftests/arm/abi/trivial_32bit_program.c @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 ARM Limited. + */ + +#include +#include + +int main(void) +{ + raise(SIGSTOP); + raise(SIGSTOP); + return 0; +}