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: 13711043 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F2B68C2BBCA for ; Tue, 25 Jun 2024 12:26:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=X+04jLQ0xggoAf+x+Qhn+6UHiiWCRKimmVIx8IBswkc=; b=cJwXx/rS1CeoKBIKO9P7hGcLmn T5XaDNWrU1JReyAD8fZuL5t8VrF/mGzPLiimnm8pGlOlqwfvbwDGW8/3t20cRT7FavXKHWWyZA6I5 fnzGS7/E7r5Fz7SKJ2XDawPrl9cR55TUOE1220qO9/Wbn4y8flrNwOC3wT4Si8RoE75sJvjaluwTG JASDpopFGEUEDMKAJzDnlbvVcWe1y3nhfzIYpJhsDpl2Grn5ReoB/grIOf+gNp9p1cjBJL4GQ7sxH 4uMmQSBzQGoEjBgC6A201GapDRa9RiN+13d4ljxTdvX4vqqFJhp2lZP9tMmPn+xBx0YCmJV66fGi3 4vZPgVQA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sM5F9-00000002kC0-1l2J; Tue, 25 Jun 2024 12:25:51 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sM5EP-00000002jfk-1u8K for linux-arm-kernel@lists.infradead.org; Tue, 25 Jun 2024 12:25:06 +0000 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240625_052505_665311_38A1CE29 X-CRM114-Status: GOOD ( 23.04 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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; +}