From patchwork Tue Jun 25 12:24:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13711019 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EC77D1553AF; Tue, 25 Jun 2024 12:24:37 +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=1719318279; cv=none; b=dbIYE5i9EOmT9CxTVjDPcvOMQKx6NlLc4jJsyTawwcsDx9tQWDokmqYDBsHFm5GWz8jDJkIkkdTNCDwGReT00K3jB9EJcJeaOP/VUZs8LEWXSQyctQbP/+XmHRpfz4N8wqWTbc+aZfAvEUNaigEn+Z7kVUeViBDQi29m6DOg46s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318279; c=relaxed/simple; bh=MVs5z0/RG/otjMsFB/U3mOThMHQjhs1d7gbRxQCETTM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LSHYiNpsu2uiQm4kCgMgasWFdWyxz0qkq1LXV6X/TIm4Elyu/LD1brYY2uW7/1vclERADUbga/mIeAvXdvNOO0J0xtRoIVGlIeSCFDK9LwY5r9tU3QTbHYCleWOIe+mQJzIaQD25sgqIuRu9R430gZMKdBgnopWOKxtUFANJOYw= 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 795F5339; Tue, 25 Jun 2024 05:25:02 -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 B600F3F766; Tue, 25 Jun 2024 05:24:32 -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 1/9] selftests/arm: Add mm test Date: Tue, 25 Jun 2024 17:54:00 +0530 Message-Id: <20240625122408.1439097-2-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 tests the 4GB VA restriction for 32-bit processes; it is required to test the compat layer, whether the kernel knows that it is running a 32-bit process or not. Chunks are allocated until the VA gets exhausted; mmap must fail beyond 4GB. This is asserted against the VA mappings found in /proc/self/maps. Signed-off-by: Dev Jain --- v2->v3: - Split into multiple testcases tools/testing/selftests/arm/mm/compat_va.c | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tools/testing/selftests/arm/mm/compat_va.c diff --git a/tools/testing/selftests/arm/mm/compat_va.c b/tools/testing/selftests/arm/mm/compat_va.c new file mode 100644 index 000000000000..20aa419eff29 --- /dev/null +++ b/tools/testing/selftests/arm/mm/compat_va.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 ARM Limited + * + * Author : Dev Jain + * + * Tests 4GB VA restriction for 32 bit process + */ + +#define _GNU_SOURCE +#include +#include +#include +#include + +#include +#include + +#define MAP_CHUNK_SIZE SZ_1M +#define NR_CHUNKS_4G ((SZ_1G / MAP_CHUNK_SIZE) * 4) /* prevent overflow */ + +static int validate_address_hint(void) +{ + char *ptr; + + ptr = mmap((void *) (1UL << 29), MAP_CHUNK_SIZE, PROT_READ | + PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (ptr == MAP_FAILED) + return 0; + + return -1; +} + +int main(int argc, char *argv[]) +{ + char *ptr[NR_CHUNKS_4G + 3]; + char line[1000]; + int chunks; + FILE *file; + int ret; + int i; + + ksft_print_header(); + ksft_set_plan(2); + + /* try allocation beyond 4 GB */ + for (i = 0; i < NR_CHUNKS_4G + 3; ++i) { + ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (ptr[i] == MAP_FAILED) { + if (validate_address_hint()) + ksft_exit_fail_msg("VA exhaustion failed\n"); + break; + } + } + + chunks = i; + + ret = (chunks < NR_CHUNKS_4G); + ksft_test_result(ret, "mmapped chunks under 4GB\n"); + + /* parse /proc/self/maps, confirm 32 bit VA mappings */ + file = fopen("/proc/self/maps", "r"); + if (!file) + ksft_exit_fail_perror("/proc/self/maps"); + + ret = 0; + while (fgets(line, sizeof(line), file)) { + const char *whitespace_loc, *hyphen_loc; + + hyphen_loc = strchr(line, '-'); + whitespace_loc = strchr(line, ' '); + + if (!(hyphen_loc && whitespace_loc)) + ksft_exit_fail_msg("Unexpected format\n"); + + ret |= ((hyphen_loc - line > 8) || + (whitespace_loc - hyphen_loc > 9)); + } + + ksft_test_result(!ret, "Memory map within 32 bits\n"); + + for (i = 0; i < chunks; ++i) + munmap(ptr[i], MAP_CHUNK_SIZE); + + ksft_finished(); +} 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: 13711020 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:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13711021 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D9EC4156674; Tue, 25 Jun 2024 12:24:48 +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=1719318290; cv=none; b=n/1SxZEJ5ECJ4LCVbtRUcMbVyCw8q8dMKTMGYI1b/E5SvcvlM2NakWUhZbXKFJF3Wjg9uoeaIjHU5pBRIe1kP2NOGmave3qOhILzBL4l4uFUecHaJMhqeYzUzsz0h+cwi8BKWixvF89NRB2zdPV8IxGOUvJmTjBK9Ch74zBxD+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318290; c=relaxed/simple; bh=1jBivvCaS2aU21W9Z49uC3SHEXnYWT3rd4jUkaDa2w8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uOVhlTUJfpKyV8ohAJys+UiGT60Bj60sfT0O6vwbMsHz4rD20PE8/+dNi9coUkqbVTns+uMk7ttLxlF1aTvlNkIzRHshBwsyF2g7lhZixi1OiF9fQlbTewJ5cSvNZ5eMjup3nvXCy4/G1Zji0z9+Vpdicq91HsTjMIMUwHIFRgQ= 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 3553D339; Tue, 25 Jun 2024 05:25:13 -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 D0FA13F766; Tue, 25 Jun 2024 05:24:43 -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 3/9] selftests: arm, arm64: Use ifdeffery to pull signal infrastructure Date: Tue, 25 Jun 2024 17:54:02 +0530 Message-Id: <20240625122408.1439097-4-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 Use ifdeffery to guard code chunks meant specifically for arm64, in preparation for putting signal tests in selftests/arm. Signed-off-by: Dev Jain --- .../selftests/arm/signal/test_signals.c | 2 + .../selftests/arm/signal/test_signals.h | 2 + .../selftests/arm/signal/test_signals_utils.c | 2 + .../selftests/arm/signal/test_signals_utils.h | 2 + .../selftests/arm64/signal/test_signals.h | 12 +++++ .../arm64/signal/test_signals_utils.c | 51 +++++++++++++++---- .../arm64/signal/test_signals_utils.h | 3 ++ 7 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/arm/signal/test_signals.c create mode 100644 tools/testing/selftests/arm/signal/test_signals.h create mode 100644 tools/testing/selftests/arm/signal/test_signals_utils.c create mode 100644 tools/testing/selftests/arm/signal/test_signals_utils.h diff --git a/tools/testing/selftests/arm/signal/test_signals.c b/tools/testing/selftests/arm/signal/test_signals.c new file mode 100644 index 000000000000..6b47c26ee218 --- /dev/null +++ b/tools/testing/selftests/arm/signal/test_signals.c @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "../../arm64/signal/test_signals.c" diff --git a/tools/testing/selftests/arm/signal/test_signals.h b/tools/testing/selftests/arm/signal/test_signals.h new file mode 100644 index 000000000000..946913d29636 --- /dev/null +++ b/tools/testing/selftests/arm/signal/test_signals.h @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "../../arm64/signal/test_signals.h" diff --git a/tools/testing/selftests/arm/signal/test_signals_utils.c b/tools/testing/selftests/arm/signal/test_signals_utils.c new file mode 100644 index 000000000000..2b16d53545be --- /dev/null +++ b/tools/testing/selftests/arm/signal/test_signals_utils.c @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "../../arm64/signal/test_signals_utils.c" diff --git a/tools/testing/selftests/arm/signal/test_signals_utils.h b/tools/testing/selftests/arm/signal/test_signals_utils.h new file mode 100644 index 000000000000..a4d61697a8dd --- /dev/null +++ b/tools/testing/selftests/arm/signal/test_signals_utils.h @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "../../arm64/signal/test_signals_utils.h" diff --git a/tools/testing/selftests/arm64/signal/test_signals.h b/tools/testing/selftests/arm64/signal/test_signals.h index 1e6273d81575..9c1bd6560501 100644 --- a/tools/testing/selftests/arm64/signal/test_signals.h +++ b/tools/testing/selftests/arm64/signal/test_signals.h @@ -14,6 +14,8 @@ #include #include +/* Not used by selftests/arm */ +#ifdef __aarch64__ #define __stringify_1(x...) #x #define __stringify(x...) __stringify_1(x) @@ -43,6 +45,7 @@ enum { #define FEAT_SME (1UL << FSME_BIT) #define FEAT_SME_FA64 (1UL << FSME_FA64_BIT) #define FEAT_SME2 (1UL << FSME2_BIT) +#endif /* * A descriptor used to describe and configure a test case. @@ -56,10 +59,13 @@ struct tdescr { /* just a name for the test-case; manadatory field */ char *name; char *descr; +/* Not used by selftests/arm */ +#ifdef __aarch64__ unsigned long feats_required; unsigned long feats_incompatible; /* bitmask of effectively supported feats: populated at run-time */ unsigned long feats_supported; +#endif bool initialized; unsigned int minsigstksz; /* signum used as a test trigger. Zero if no trigger-signal is used */ @@ -69,8 +75,11 @@ struct tdescr { * Zero when no signal is expected on success */ int sig_ok; +/* Not used by selftests/arm */ +#ifdef __aarch64__ /* signum expected on unsupported CPU features. */ int sig_unsupp; +#endif /* a timeout in second for test completion */ unsigned int timeout; bool triggered; @@ -79,10 +88,13 @@ struct tdescr { /* optional sa_flags for the installed handler */ int sa_flags; ucontext_t saved_uc; +/* Not used by selftests/arm */ +#ifdef __aarch64__ /* used by get_current_ctx() */ size_t live_sz; ucontext_t *live_uc; volatile sig_atomic_t live_uc_valid; +#endif /* optional test private data */ void *priv; diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.c b/tools/testing/selftests/arm64/signal/test_signals_utils.c index 0dc948db3a4a..8396d9748b48 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.c +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.c @@ -17,11 +17,16 @@ #include "test_signals.h" #include "test_signals_utils.h" + +#ifdef __aarch64__ #include "testcases/testcases.h" +#endif extern struct tdescr *current; +/* Not used by selftests/arm */ +#ifdef __aarch64__ static int sig_copyctx = SIGTRAP; static char const *const feats_names[FMAX_END] = { @@ -53,6 +58,7 @@ static inline char *feats_to_string(unsigned long feats) return feats_string; } +#endif static void unblock_signal(int signum) { @@ -87,6 +93,7 @@ static void default_result(struct tdescr *td, bool force_exit) * take care of such unexpected situations. */ +#ifdef __aarch64__ static bool handle_signal_unsupported(struct tdescr *td, siginfo_t *si, void *uc) { @@ -108,6 +115,7 @@ static bool handle_signal_unsupported(struct tdescr *td, return true; } +#endif static bool handle_signal_trigger(struct tdescr *td, siginfo_t *si, void *uc) @@ -127,10 +135,21 @@ static bool handle_signal_ok(struct tdescr *td, * if sig_trig was defined, it must have been used before getting here. */ assert(!td->sig_trig || td->triggered); + +#ifdef __aarch64__ fprintf(stderr, "SIG_OK -- SP:0x%llX si_addr@:%p si_code:%d token@:%p offset:%ld\n", ((ucontext_t *)uc)->uc_mcontext.sp, si->si_addr, si->si_code, td->token, td->token - si->si_addr); +#else + fprintf(stderr, + "SIG_OK -- SP:0x%lX si_addr@:%p si_code:%d token@:%p offset:%d\n", + ((ucontext_t *)uc)->uc_mcontext.arm_sp, + si->si_addr, si->si_code, td->token, td->token - si->si_addr); +#endif + +#ifdef __aarch64__ + /* * fake_sigreturn tests, which have sanity_enabled=1, set, at the very * last time, the token field to the SP address used to place the fake @@ -153,6 +172,7 @@ static bool handle_signal_ok(struct tdescr *td, "si_code != SEGV_ACCERR...test is probably broken!\n"); abort(); } +#endif td->pass = 1; /* * Some tests can lead to SEGV loops: in such a case we want to @@ -165,6 +185,7 @@ static bool handle_signal_ok(struct tdescr *td, return true; } +#ifdef __aarch64__ static bool handle_signal_copyctx(struct tdescr *td, siginfo_t *si, void *uc_in) { @@ -229,22 +250,31 @@ static bool handle_signal_copyctx(struct tdescr *td, return true; } +#endif static void default_handler(int signum, siginfo_t *si, void *uc) { +#ifdef __aarch64__ if (current->sig_unsupp && signum == current->sig_unsupp && handle_signal_unsupported(current, si, uc)) { fprintf(stderr, "Handled SIG_UNSUPP\n"); - } else if (current->sig_trig && signum == current->sig_trig && + } +#endif + + if (current->sig_trig && signum == current->sig_trig && handle_signal_trigger(current, si, uc)) { fprintf(stderr, "Handled SIG_TRIG\n"); } else if (current->sig_ok && signum == current->sig_ok && handle_signal_ok(current, si, uc)) { fprintf(stderr, "Handled SIG_OK\n"); - } else if (signum == sig_copyctx && current->live_uc && + } +#ifdef __aarch64__ + else if (signum == sig_copyctx && current->live_uc && handle_signal_copyctx(current, si, uc)) { fprintf(stderr, "Handled SIG_COPYCTX\n"); - } else { + } +#endif + else { if (signum == SIGALRM && current->timeout) { fprintf(stderr, "-- Timeout !\n"); } else { @@ -280,9 +310,10 @@ static int default_setup(struct tdescr *td) unblock_signal(td->sig_trig); if (td->sig_ok) unblock_signal(td->sig_ok); +#ifdef __aarch64__ if (td->sig_unsupp) unblock_signal(td->sig_unsupp); - +#endif if (td->timeout) { unblock_signal(SIGALRM); alarm(td->timeout); @@ -299,6 +330,12 @@ static inline int default_trigger(struct tdescr *td) int test_init(struct tdescr *td) { + td->minsigstksz = getauxval(AT_MINSIGSTKSZ); + if (!td->minsigstksz) + td->minsigstksz = MINSIGSTKSZ; + fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz); + +#ifdef __aarch64__ if (td->sig_trig == sig_copyctx) { fprintf(stdout, "Signal %d is RESERVED, cannot be used as a trigger. Aborting\n", @@ -308,11 +345,6 @@ int test_init(struct tdescr *td) /* just in case */ unblock_signal(sig_copyctx); - td->minsigstksz = getauxval(AT_MINSIGSTKSZ); - if (!td->minsigstksz) - td->minsigstksz = MINSIGSTKSZ; - fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz); - if (td->feats_required || td->feats_incompatible) { td->feats_supported = 0; /* @@ -357,6 +389,7 @@ int test_init(struct tdescr *td) return 0; } } +#endif /* Perform test specific additional initialization */ if (td->init && !td->init(td)) { diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.h b/tools/testing/selftests/arm64/signal/test_signals_utils.h index 762c8fe9c54a..50acfc1a1692 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.h +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.h @@ -18,6 +18,8 @@ void test_cleanup(struct tdescr *td); int test_run(struct tdescr *td); void test_result(struct tdescr *td); +/* Not used by selftests/arm */ +#ifdef __aarch64__ static inline bool feats_ok(struct tdescr *td) { if (td->feats_incompatible & td->feats_supported) @@ -146,3 +148,4 @@ static __always_inline bool get_current_context(struct tdescr *td, int fake_sigreturn(void *sigframe, size_t sz, int misalign_bytes); #endif +#endif 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: 13711022 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 Reviewed-by: Mark Brown --- .../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:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13711023 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 087E2157A6C; Tue, 25 Jun 2024 12:25:00 +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=1719318301; cv=none; b=iu8SuhN201Z5q4XbE5zeUKVQxXt3ohp9DDT5ltVRA+U7GKSh6UZIrqQ8DxvatNqLHoMxDTIU5bArru5ZOjcJmImGhehxSU7U0kDEV9t9AfR3oPH9VlwkuFH072hj9YUtmrXvzJZ0A4fZKbYDWAOdF8LNPuBnKWAGA1w1U4GlrqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318301; c=relaxed/simple; bh=scqmnxgNz27zPBIBIvH13F67ZFzsL+21QGepEUJWBsQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EVxwL0iBIQ3iSzzofaquU4oY3rtr+tOSUByToiUGDDtcfrt02VxHgb6a2dE9TnZsXfHz9KxUh1xqx5cp9nteZZ6d0BNGqtS1XBIuH7GeDxfqjwI/pGG7292cYyvGCbmSKBr9WSX88zlj3IrfW5sP+/A2AO5l7Cv7cIUVldW07L8= 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 4B7B1339; Tue, 25 Jun 2024 05:25:24 -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 B9ED83F766; Tue, 25 Jun 2024 05:24:54 -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 5/9] selftests/arm64: Fix build warnings for ptrace Date: Tue, 25 Jun 2024 17:54:04 +0530 Message-Id: <20240625122408.1439097-6-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 "%s" should have been used in ksft_exit_fail_msg(). Anyways, replace that with the recently introduced ksft_exit_fail_perror(). Also fix no mention of type_name in ksft_test_result_skip(). NOTE: This patch can be applied independently of the series, but the next patch depends on this one. Fixes: ecaf4d3f734f ("kselftest/arm64: Add test coverage for NT_ARM_TLS") Fixes: cb5aa6379438 ("kselftest/arm64: Add a smoke test for ptracing hardware break/watch points") Signed-off-by: Dev Jain Reviewed-by: Mark Brown --- tools/testing/selftests/arm64/abi/ptrace.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/arm64/abi/ptrace.c b/tools/testing/selftests/arm64/abi/ptrace.c index abe4d58d731d..c83f0441e9d0 100644 --- a/tools/testing/selftests/arm64/abi/ptrace.c +++ b/tools/testing/selftests/arm64/abi/ptrace.c @@ -156,17 +156,17 @@ static void test_hw_debug(pid_t child, int type, const char *type_name) /* 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"); + 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_msg("PTRACE_TRACEME", strerror(errno)); + ksft_exit_fail_perror("PTRACE_TRACEME"); if (raise(SIGSTOP)) - ksft_exit_fail_msg("raise(SIGSTOP)", strerror(errno)); + ksft_exit_fail_perror("raise(SIGSTOP)"); return EXIT_SUCCESS; } 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: 13711024 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:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13711025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BD97A15A85B; Tue, 25 Jun 2024 12:25:10 +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=1719318312; cv=none; b=Nve7xP/2iAvoVXwZssi78WLpzP2A1zzE1hIQ9eunSlkshDqrOdwQ2k2Ua9mCGY74WlRjjNspKxlEVbWup0irDzl3GI8/lPZX1NthrtxtZY7gkpusT/RN3oyjRtp2WNubBjMczLAod3b3a0zvAa89hg+iSJaP2rXWTjtQoua3DbI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318312; c=relaxed/simple; bh=OS+oG8hagHM3ro0Lo1ETW7m/q1ZvXOye43hTaulyPk0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V82L7hZdy3C54BVy7KFDxszXIjTebKTda6M1VjOCXUU8E6KIpHWHAIcNJwQ6ILgC9+o5Nl0gHXveQ3zE4NoHSYy/qPJVtaPqFlrNKF4Gdr9OWsDw9WSPid6QmCmsbuGpGNMfVhFt4CaPM0SjvdDK1lc1t1X6I0mOUMICphHUeUc= 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 40833339; Tue, 25 Jun 2024 05:25:35 -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 898A83F766; Tue, 25 Jun 2024 05:25:05 -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 7/9] selftests/arm: Add ptrace test Date: Tue, 25 Jun 2024 17:54:06 +0530 Message-Id: <20240625122408.1439097-8-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 32-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.c | 82 ++++++++++++++++++++++++ tools/testing/selftests/arm/abi/ptrace.h | 57 ++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/arm/abi/ptrace.c create mode 100644 tools/testing/selftests/arm/abi/ptrace.h diff --git a/tools/testing/selftests/arm/abi/ptrace.c b/tools/testing/selftests/arm/abi/ptrace.c new file mode 100644 index 000000000000..2079065c48fd --- /dev/null +++ b/tools/testing/selftests/arm/abi/ptrace.c @@ -0,0 +1,82 @@ +// 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 "ptrace.h" +#include "../../kselftest.h" + +#define EXPECTED_TESTS 6 +#define NUM_TLS_REGS 2 + +static void test_tpidr(pid_t child) +{ + unsigned long read_val[NUM_TLS_REGS]; + struct iovec read_iov; + int ret; + + read_iov.iov_base = read_val; + + /* TLS registers must not be accessible */ + read_iov.iov_len = 2 * sizeof(unsigned long); + ret = ptrace(PTRACE_GETREGSET, child, NT_ARM_TLS, &read_iov); + ksft_test_result(ret != 0, "cannot read TLS\n"); +} + +static void run_tests(pid_t child) +{ + test_tpidr(child); + test_user_regs(child); +} + +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)"); + + if (raise(SIGSTOP)) + ksft_exit_fail_perror("raise(SIGSTOP)"); + + 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/ptrace.h b/tools/testing/selftests/arm/abi/ptrace.h new file mode 100644 index 000000000000..17ba8aa32726 --- /dev/null +++ b/tools/testing/selftests/arm/abi/ptrace.h @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "../../arm64/abi/ptrace.h" + +/* Do not pull from asm/ptrace.h since the macro names change for 32-bit */ +#define PSR_MODE32_BIT 0x00000010 +#define PSR_MODE_EL1t 0x00000004 + +static void test_user_regs(pid_t child) +{ + unsigned int read_val[18]; + struct iovec read_iov; + int status; + int ret; + + read_iov.iov_base = read_val; + read_iov.iov_len = 18 * sizeof(unsigned int); + + ret = ptrace(PTRACE_GETREGSET, child, NT_PRSTATUS, &read_iov); + ksft_test_result(!ret, "read general-purpose registers\n"); + + /* Change a random user register */ + read_val[2] = read_val[2] + 1; + ret = ptrace(PTRACE_SETREGSET, child, NT_PRSTATUS, &read_iov); + ksft_test_result(!ret, "set user register\n"); + + /* 16th register is the CPSR */ + read_val[16] &= (~PSR_MODE32_BIT); + + ret = ptrace(PTRACE_SETREGSET, child, NT_PRSTATUS, &read_iov); + ksft_test_result(ret, "cannot toggle MODE32 bit\n"); + + ret = ptrace(PTRACE_CONT, child, NULL, 0); + if (ret) { + perror("ptrace"); + goto error; + } + + if (wait(&status) == -1) { + perror("wait"); + goto error; + } + + read_val[16] = 0; + + ret = ptrace(PTRACE_GETREGSET, child, NT_PRSTATUS, &read_iov); + ksft_test_result(!ret, "read general-purpose registers again\n"); + + read_val[16] |= PSR_MODE_EL1t; + ret = ptrace(PTRACE_SETREGSET, child, NT_PRSTATUS, &read_iov); + ksft_test_result(ret, "cannot escalate privilege\n"); + return; + +error: + kill(child, SIGKILL); +} + + 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: 13711026 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; +} From patchwork Tue Jun 25 12:24:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13711027 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 018E715B562; Tue, 25 Jun 2024 12:25:20 +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=1719318322; cv=none; b=HGmQ0Hkw+MaBbUFz3l2uhRzAGQyk9PmbKU2LI9ofnPbt36IvCkcaWzHpwVxL59cGStkVYnWYwuT6U5KlwoqyqrAk458mwAkDNsylRBKGwY4auEj374H5u+I+KeIhpwXeqcKvvgedovVO1/S5jgEnyKBqb+JQRXN/+d5leYd8k3o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719318322; c=relaxed/simple; bh=Pm58IP1DolRyMTmvoQUmXN2JDiAxDZ2mg7rtbhnilnQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fSPMsXRortXNShd58fImVtl4k54eeh9xt92fZhBoJ5J0PLc8Au4yaAXHLAj9OHSP/RubMvEm1k+lkg+gCBNaThAFSEOregsNHnYvB3hRMvyd7fMQXoK4bBrpvz3NuX/CeK76qqpExRiu1AOgQpCDwGu15q011G4WokV06xL3bdk= 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 85D00339; Tue, 25 Jun 2024 05:25:45 -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 27F0C3F766; Tue, 25 Jun 2024 05:25:15 -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 9/9] selftests: Add build infrastructure along with README Date: Tue, 25 Jun 2024 17:54:08 +0530 Message-Id: <20240625122408.1439097-10-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 Add arm target, individual Makefile targets, and instructions to build the tests, along with .gitignore files. All the Makefiles are similar to selftests/arm64, except abi: use TEST_CUSTOM_PROGS to override the make rule from lib.mk. Also, do not build ptrace_64 if we are running a 32-bit kernel. Signed-off-by: Dev Jain --- v2->v3: - Add abi Makefile and .gitignore - Remove config file v1->v2: - Add config file tools/testing/selftests/Makefile | 1 + tools/testing/selftests/arm/Makefile | 56 +++++++++++++++++++ tools/testing/selftests/arm/README | 32 +++++++++++ tools/testing/selftests/arm/abi/.gitignore | 4 ++ tools/testing/selftests/arm/abi/Makefile | 26 +++++++++ tools/testing/selftests/arm/elf/.gitignore | 2 + tools/testing/selftests/arm/elf/Makefile | 6 ++ tools/testing/selftests/arm/mm/.gitignore | 2 + tools/testing/selftests/arm/mm/Makefile | 6 ++ tools/testing/selftests/arm/signal/.gitignore | 3 + tools/testing/selftests/arm/signal/Makefile | 30 ++++++++++ 11 files changed, 168 insertions(+) create mode 100644 tools/testing/selftests/arm/Makefile create mode 100644 tools/testing/selftests/arm/README create mode 100644 tools/testing/selftests/arm/abi/.gitignore create mode 100644 tools/testing/selftests/arm/abi/Makefile create mode 100644 tools/testing/selftests/arm/elf/.gitignore create mode 100644 tools/testing/selftests/arm/elf/Makefile create mode 100644 tools/testing/selftests/arm/mm/.gitignore create mode 100644 tools/testing/selftests/arm/mm/Makefile create mode 100644 tools/testing/selftests/arm/signal/.gitignore create mode 100644 tools/testing/selftests/arm/signal/Makefile diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 9039f3709aff..d7420825f165 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 TARGETS += alsa TARGETS += amd-pstate +TARGETS += arm TARGETS += arm64 TARGETS += bpf TARGETS += breakpoints diff --git a/tools/testing/selftests/arm/Makefile b/tools/testing/selftests/arm/Makefile new file mode 100644 index 000000000000..54e44c4a62bf --- /dev/null +++ b/tools/testing/selftests/arm/Makefile @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: GPL-2.0 + +ARCH ?= $(shell uname -m 2>/dev/null || echo not) + +ifneq (,$(filter $(ARCH),aarch64 arm64 arm armv7l armv8l)) +ARM_SUBTARGETS ?= abi elf mm signal +else +ARM_SUBTARGETS := +endif + +CFLAGS := -Wall -O2 -g -static + +# A proper top_srcdir is needed by KSFT(lib.mk) +top_srcdir = $(realpath ../../../../) + +# Additional include paths needed by kselftest.h and local headers +CFLAGS += -I$(top_srcdir)/tools/testing/selftests/ + +CFLAGS += -I$(top_srcdir)/tools/include + +export CFLAGS +export top_srcdir + +all: + @for DIR in $(ARM_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + mkdir -p $$BUILD_TARGET; \ + make OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +install: all + @for DIR in $(ARM_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + make OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +run_tests: all + @for DIR in $(ARM_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + make OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +# Avoid any output on non arm on emit_tests +emit_tests: + @for DIR in $(ARM_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + make OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +clean: + @for DIR in $(ARM_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + make OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +.PHONY: all clean install run_tests emit_tests diff --git a/tools/testing/selftests/arm/README b/tools/testing/selftests/arm/README new file mode 100644 index 000000000000..75b7cf4baec3 --- /dev/null +++ b/tools/testing/selftests/arm/README @@ -0,0 +1,32 @@ +KSelfTest ARM +=============== + +- This is a series of compatibility tests, wherein the source files are + built statically into a 32 bit ELF; they should pass on both 32 and 64 + bit kernels. They are not built or run but just skipped completely when + env-variable ARCH is found to be different than 'arm64' or 'arm' and + `uname -m` reports other than 'aarch64', 'armv7l' or 'armv8l'. + +- If building the tests on a 64-bit kernel, please ensure that the kernel is + built with CONFIG_COMPAT enabled. + +- Holding true the above, ARM KSFT tests can be run within the KSelfTest + framework using standard Linux top-level-makefile targets. Please set + $(CROSS_COMPILE) to 'arm-linux-gnueabi-' or 'arm-linux-gnueabihf-'. + + $ make TARGETS=arm kselftest-clean + $ make $(CROSS_COMPILE) TARGETS=arm kselftest + + or + + $ make $(CROSS_COMPILE) -C tools/testing/selftests TARGETS=arm \ + INSTALL_PATH= install + + or, alternatively, only specific arm/ subtargets can be picked: + + $ make $(CROSS_COMPILE) -C tools/testing/selftests TARGETS=arm \ + ARM_SUBTARGETS="signal" INSTALL_PATH= \ + install + + Further details on building and running KFST can be found in: + Documentation/dev-tools/kselftest.rst diff --git a/tools/testing/selftests/arm/abi/.gitignore b/tools/testing/selftests/arm/abi/.gitignore new file mode 100644 index 000000000000..75af3c416fc3 --- /dev/null +++ b/tools/testing/selftests/arm/abi/.gitignore @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-only +ptrace +ptrace_64 +trivial_32bit_program diff --git a/tools/testing/selftests/arm/abi/Makefile b/tools/testing/selftests/arm/abi/Makefile new file mode 100644 index 000000000000..160b6aadb064 --- /dev/null +++ b/tools/testing/selftests/arm/abi/Makefile @@ -0,0 +1,26 @@ +# SPDX-License-Identifier: GPL-2.0-only +# Copyright (C) 2024 ARM Limited + +TEST_GEN_PROGS := ptrace + +include ../../lib.mk + +# Do not build 64-bit programs if running on a native 32-bit kernel +UNAME_M := $(shell uname -m) +ifneq (,$(filter $(UNAME_M),aarch64 arm64)) +TEST_CUSTOM_PROGS := $(OUTPUT)/ptrace_64 + +TRIVIAL_32BIT := $(OUTPUT)/trivial_32bit_program + +all: $(TEST_CUSTOM_PROGS) $(TRIVIAL_32BIT) + + +$(TRIVIAL_32BIT): $(OUTPUT)/%: %.c + $(CC) -o $@ $^ -static + +$(OUTPUT)/ptrace_64: ptrace_64.c ptrace.h + gcc -o $@ $^ + +EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(TRIVIAL_32BIT) +endif + diff --git a/tools/testing/selftests/arm/elf/.gitignore b/tools/testing/selftests/arm/elf/.gitignore new file mode 100644 index 000000000000..41458ecbcd72 --- /dev/null +++ b/tools/testing/selftests/arm/elf/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +parse_elf diff --git a/tools/testing/selftests/arm/elf/Makefile b/tools/testing/selftests/arm/elf/Makefile new file mode 100644 index 000000000000..86636fe02994 --- /dev/null +++ b/tools/testing/selftests/arm/elf/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 ARM Limited + +TEST_GEN_PROGS := parse_elf + +include ../../lib.mk diff --git a/tools/testing/selftests/arm/mm/.gitignore b/tools/testing/selftests/arm/mm/.gitignore new file mode 100644 index 000000000000..eb28169bb1b5 --- /dev/null +++ b/tools/testing/selftests/arm/mm/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +compat_va diff --git a/tools/testing/selftests/arm/mm/Makefile b/tools/testing/selftests/arm/mm/Makefile new file mode 100644 index 000000000000..d8bfa45df98c --- /dev/null +++ b/tools/testing/selftests/arm/mm/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 ARM Limited + +TEST_GEN_PROGS := compat_va + +include ../../lib.mk diff --git a/tools/testing/selftests/arm/signal/.gitignore b/tools/testing/selftests/arm/signal/.gitignore new file mode 100644 index 000000000000..85b81356bf41 --- /dev/null +++ b/tools/testing/selftests/arm/signal/.gitignore @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +mangle_cpsr_invalid_aif_bits +mangle_cpsr_invalid_compat_toggle diff --git a/tools/testing/selftests/arm/signal/Makefile b/tools/testing/selftests/arm/signal/Makefile new file mode 100644 index 000000000000..3540a25de75a --- /dev/null +++ b/tools/testing/selftests/arm/signal/Makefile @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 ARM Limited + +# Additional include paths needed by kselftest.h and local headers +CFLAGS += -D_GNU_SOURCE -std=gnu99 -I. + +SRCS := $(filter-out testcases/testcases.c,$(wildcard testcases/*.c)) +PROGS := $(patsubst %.c,%,$(SRCS)) + +# Generated binaries to be installed by top KSFT script +TEST_GEN_PROGS := $(notdir $(PROGS)) + +# Get Kernel headers installed and use them. + +# Including KSFT lib.mk here will also mangle the TEST_GEN_PROGS list +# to account for any OUTPUT target-dirs optionally provided by +# the toplevel makefile +include ../../lib.mk + +$(TEST_GEN_PROGS): $(PROGS) + cp $(PROGS) $(OUTPUT)/ + +# Common test-unit targets to build common-layout test-cases executables +# Needs secondary expansion to properly include the testcase c-file in pre-reqs +COMMON_SOURCES := test_signals.c test_signals_utils.c +COMMON_HEADERS := test_signals.h test_signals_utils.h + +.SECONDEXPANSION: +$(PROGS): $$@.c ${COMMON_SOURCES} ${COMMON_HEADERS} + $(CC) $(CFLAGS) ${@}.c ${COMMON_SOURCES} -o $@