From nobody Sat Oct 4 00:32:11 2025 Received: from mail-qt1-f171.google.com (mail-qt1-f171.google.com [209.85.160.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE776289E07 for ; Fri, 22 Aug 2025 17:47:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755884859; cv=none; b=RNdBGl4d7ZvsLuerwE4OnZM5rmTo2Xjzo4qtrCj85FbdjMFXLdCMZH87T3aMFbskalTRsWahgE3+NU+jBPemBUVfKevdyaX1E0E00mYXWDPQUFo+KltWf2zEqHX/Pr5b0kzSL1dVEZrfmq8VaMDq02fAKjBv77k6Ix8VZhBrch0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755884859; c=relaxed/simple; bh=Tewu1o9TEDAwro0wIjOKO+bdoL8MBTkvGl+q4i50X14=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=f5o2NvZ9v9uFvIpCiH0QpjC4g1QnU3JRXhAZk6MaI84zyjSD+m9elo6uA5Y5DzrwSiRQ/77WADSNJP/ejgt1d+4su/BjkNwjxiVYJIeG1lyYjAQNB2fH3p8bRe2TtjAJaawRHsIXfwtCYAZ/CWANjiGX2UfiK/d/STNX0SCFvqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc.com header.i=@rivosinc.com header.b=H9am+3yK; arc=none smtp.client-ip=209.85.160.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc.com header.i=@rivosinc.com header.b="H9am+3yK" Received: by mail-qt1-f171.google.com with SMTP id d75a77b69052e-4b109921fe7so27751391cf.0 for ; Fri, 22 Aug 2025 10:47:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc.com; s=google; t=1755884856; x=1756489656; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lASAogWwaY4J3oXaSg+X8r5SCu4Wp/t1yzCv89toveQ=; b=H9am+3yK6oHMcrJhdU25BLMmvUy/4ftKC/0yzLUQn7CYRf2Yguiyg196cWuZW583SV Z34pSjxRXP3F7y9M0uwdqyxU44v3VIx06QIiMrZZBqpSvGHNY4xlILH3wRlfwIGKlTjS mTWNhx3eUV9XRUPD/PBG914c003I24Lhqxawebj6/2H/8KEWnap3xdJCsi0Zer7UfC4Q ZBuQQybf8/79c68HuRfBKpjjPJuRTiSq9btfRm3TSZUUOGZBfqxXTnWO09m6pBpmaxY4 R0+BmP/SVlEZD6hELrToVggKcr1mmyDdATyrnBZxbmxGoeVGZTq3qCoMdAkb6LmYlPVq YiiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755884856; x=1756489656; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lASAogWwaY4J3oXaSg+X8r5SCu4Wp/t1yzCv89toveQ=; b=kWl3dB2Yt58lo5yUTvMhOI5VrS0mdEBtSY//V6KhqWOiOn3XRvEsJpKMAj2Wi3bkYW fWeEm07O6IKpfypJsUR7Y+RZpUj5rfyFuEeHdHxpcX/mvUuhCBRvl7ToVbIM1a3zUbvo Fd7G7d/ShV8ffDokWQsFuJPD8ZIMvOviBbiA3aAGnfH/KWUTd1dl4vFw0OaoUjXgWzNT VuXrjPUMYmoIMyVyG6CEUjTVFjEpjpEa2jcax7eu3qtLArPMGjdf6bJOGsMXdQ1EPk++ Vgc2n6Dcqfd+LLIlt1Bm6SwEWUlBR4PjJSZRl23flm822BUA3UFA4rlqC8OckYtmiSA1 B84w== X-Forwarded-Encrypted: i=1; AJvYcCWT+SzTdzY0Z6uD7L5ZIMCjlPCAPhlepuXwFFZFTAThRlTlgCRH3dMuFJmQRKlgw+nef0jadC17PO8FCvI=@vger.kernel.org X-Gm-Message-State: AOJu0Ywdc7GUA89tC/WO5raY+mIbt4VJ0xDBOD83RMcD7GJLRb3gOISe 6YlGKFlzg82n9dY/hV2RHUyX825R3Q4BEiBBRz9JcoriaL5SGqlZYGfrOjS7OHLzMEQ= X-Gm-Gg: ASbGncviSacOqYK1je/kfMZdHeCzHq9aX+bt7B7ncECoANkqdcn6tG6uYtcTBw31XbT DTrLcKtHX2PAuVHHEoRFCvVCA4SYdCq5T86LqxQSbysStr2u1Q9PLxisZ4TP9jnjCsx705r2Pje SGwM0A9+nY7GG9xwqE+AgzXx+aJB83mfR5f/fWOyTElYL9R51h7bKMx/v1ajalilPg9VkYk7yh3 QdX9dnxKJy2lmE0+7zirOmiX1w0bUbMZoAfya8gmLCFCa4PfXDkNyuJpXniqlH7QFmdA3dAfJnH hDwG4ylArv2kjDKtjcuYW8GI2JUpd+GTgiYflTlKvCEgaftWUtr7R4i7eCIpIs1LTyWrtpWOXxx PGw5Cml4gPgRw6KOOuA+Pva+2nmAzewjoB4wUV9QKCwhKR6PeMGunXVKoawJUxpq9xYCAg6p5QL 5YyuR8JA== X-Google-Smtp-Source: AGHT+IF7NBwPyWijDa6fVd6FPPHBKgHBj6d8UJ7rY1WXrx0r6jLeZuvNMUy7ENqP9DpN+xFIGjFOyA== X-Received: by 2002:a05:622a:4016:b0:4ae:f8bb:7c6a with SMTP id d75a77b69052e-4b2aaaf96abmr39630891cf.54.1755884855769; Fri, 22 Aug 2025 10:47:35 -0700 (PDT) Received: from jesse-lt.jtp-bos.lab (pool-108-26-215-125.bstnma.fios.verizon.net. [108.26.215.125]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4b2b8e6023asm3121361cf.53.2025.08.22.10.47.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Aug 2025 10:47:35 -0700 (PDT) From: Jesse Taube To: linux-riscv@lists.infradead.org Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Oleg Nesterov , Kees Cook , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang Kan" , Shuah Khan , Jesse Taube , Himanshu Chauhan , Charlie Jenkins , Samuel Holland , Conor Dooley , Deepak Gupta , Andrew Jones , Atish Patra , Anup Patel , Mayuresh Chitale , Evan Green , WangYuli , Huacai Chen , Arnd Bergmann , Andrew Morton , Luis Chamberlain , "Mike Rapoport (Microsoft)" , Nam Cao , Yunhui Cui , Joel Granados , =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , Sebastian Andrzej Siewior , Celeste Liu , Chunyan Zhang , Nylon Chen , Thomas Gleixner , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Vincenzo Frascino , Joey Gouly , Ravi Bangoria , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-perf-users@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Stanley Subject: [PATCH 8/8] selftests: riscv: Add test for hardware breakpoints Date: Fri, 22 Aug 2025 10:47:15 -0700 Message-ID: <20250822174715.1269138-9-jesse@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250822174715.1269138-1-jesse@rivosinc.com> References: <20250822174715.1269138-1-jesse@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add riscv specific selftest for hardware breakpoints. These tests are based on: tools/testing/selftests/breakpoints/breakpoint_test_arm64.c Signed-off-by: Jesse Taube --- The selftest fails as register_user_hw_breakpoint seemingly does not call arch_install_hw_breakpoint. The test also seems to fail on arm64 in the same way when I tested it. RFC -> V1: - New commit V1 -> V2: - Add KHDR_INCLUDES to Makefile - Change to --- tools/perf/tests/tests.h | 3 +- tools/testing/selftests/riscv/Makefile | 2 +- .../selftests/riscv/breakpoints/.gitignore | 1 + .../selftests/riscv/breakpoints/Makefile | 13 + .../riscv/breakpoints/breakpoint_test.c | 246 ++++++++++++++++++ 5 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/riscv/breakpoints/.gitignore create mode 100644 tools/testing/selftests/riscv/breakpoints/Makefile create mode 100644 tools/testing/selftests/riscv/breakpoints/breakpoint_te= st.c diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 8aea344536b8..5ff35304c11a 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -183,7 +183,8 @@ DECLARE_SUITE(util); * Just disable the test for these architectures until these issues are * resolved. */ -#if defined(__powerpc__) || defined(__s390x__) || defined(__arm__) || defi= ned(__aarch64__) +#if defined(__powerpc__) || defined(__s390x__) || defined(__arm__) || defi= ned(__aarch64__) || \ + defined(__riscv) #define BP_SIGNAL_IS_SUPPORTED 0 #else #define BP_SIGNAL_IS_SUPPORTED 1 diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftes= ts/riscv/Makefile index 099b8c1f46f8..96aba246cb3e 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?=3D $(shell uname -m 2>/dev/null || echo not) =20 ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?=3D abi hwprobe mm sigreturn vector +RISCV_SUBTARGETS ?=3D abi hwprobe mm sigreturn vector breakpoints else RISCV_SUBTARGETS :=3D endif diff --git a/tools/testing/selftests/riscv/breakpoints/.gitignore b/tools/t= esting/selftests/riscv/breakpoints/.gitignore new file mode 100644 index 000000000000..9b3193d06608 --- /dev/null +++ b/tools/testing/selftests/riscv/breakpoints/.gitignore @@ -0,0 +1 @@ +breakpoint_test diff --git a/tools/testing/selftests/riscv/breakpoints/Makefile b/tools/tes= ting/selftests/riscv/breakpoints/Makefile new file mode 100644 index 000000000000..2315d7d874fc --- /dev/null +++ b/tools/testing/selftests/riscv/breakpoints/Makefile @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2021 ARM Limited +# Originally tools/testing/arm64/abi/Makefile + +CFLAGS +=3D -I$(top_srcdir)/tools/include +CFLAGS +=3D $(KHDR_INCLUDES) + +TEST_GEN_PROGS :=3D breakpoint_test + +include ../../lib.mk + +$(OUTPUT)/breakpoint_test: breakpoint_test.c + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/breakpoints/breakpoint_test.c b/= tools/testing/selftests/riscv/breakpoints/breakpoint_test.c new file mode 100644 index 000000000000..2675055a924e --- /dev/null +++ b/tools/testing/selftests/riscv/breakpoints/breakpoint_test.c @@ -0,0 +1,246 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2016 Google, Inc. + * + * Original Code by Pavel Labath + * + * Code modified by Pratyush Anand + * for testing different byte select for each access size. + * Originally tools/testing/selftests/breakpoints/breakpoint_test_arm64.c + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" + +#define MAX_BP_SIZE 8 + +static volatile uint8_t var[3*MAX_BP_SIZE] __attribute__((__aligned__(MAX_= BP_SIZE))); + +static void child(int size, int wr) +{ + volatile uint8_t *addr =3D &var[MAX_BP_SIZE + wr]; + + if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) !=3D 0) { + ksft_print_msg( + "ptrace(PTRACE_TRACEME) failed: %s\n", + strerror(errno)); + _exit(1); + } + + if (raise(SIGSTOP) !=3D 0) { + ksft_print_msg( + "raise(SIGSTOP) failed: %s\n", strerror(errno)); + _exit(1); + } + + if ((uintptr_t) addr % size) { + ksft_print_msg( + "Wrong address write for the given size: %s\n", + strerror(errno)); + _exit(1); + } + + switch (size) { + case 1: + *addr =3D 47; + break; + case 2: + *(uint16_t *)addr =3D 47; + break; + case 4: + *(uint32_t *)addr =3D 47; + break; + case 8: + *(uint64_t *)addr =3D 47; + break; + } + + _exit(0); +} + +static bool set_watchpoint(pid_t pid, int size, int wp) +{ + const volatile uint8_t *addr =3D &var[MAX_BP_SIZE + wp]; + const int offset =3D (uintptr_t)addr % 8; + const unsigned int type =3D 2; /* Write */ + const unsigned int enable =3D 1; + struct __riscv_hwdebug_state debug_state; + struct iovec iov; + + memset(&debug_state, 0, sizeof(debug_state)); + debug_state.addr =3D (uintptr_t)(addr - offset); + debug_state.len =3D size; + debug_state.ctrl =3D enable; + debug_state.type =3D type; + iov.iov_base =3D &debug_state; + iov.iov_len =3D sizeof(debug_state); + if (ptrace(PTRACE_SETREGSET, pid, NT_RISCV_HW_BREAK, &iov) =3D=3D 0) + return true; + + if (errno =3D=3D EIO) + ksft_print_msg( + "ptrace(PTRACE_SETREGSET, NT_RISCV_HW_BREAK) not supported on this hard= ware: %s\n", + strerror(errno)); + + ksft_print_msg( + "ptrace(PTRACE_SETREGSET, NT_RISCV_HW_BREAK) failed: %s\n", + strerror(errno)); + return false; +} + +static bool run_test(int wr_size, int wp_size, int wr, int wp) +{ + int status; + siginfo_t siginfo; + pid_t pid =3D fork(); + pid_t wpid; + + if (pid < 0) { + ksft_test_result_fail( + "fork() failed: %s\n", strerror(errno)); + return false; + } + if (pid =3D=3D 0) + child(wr_size, wr); + + wpid =3D waitpid(pid, &status, __WALL); + if (wpid !=3D pid) { + ksft_print_msg( + "waitpid() failed: %s\n", strerror(errno)); + return false; + } + if (!WIFSTOPPED(status)) { + ksft_print_msg( + "child did not stop: %s\n", strerror(errno)); + return false; + } + if (WSTOPSIG(status) !=3D SIGSTOP) { + ksft_print_msg("child did not stop with SIGSTOP\n"); + return false; + } + + if (!set_watchpoint(pid, wp_size, wp)) + return false; + + if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) { + ksft_print_msg( + "ptrace(PTRACE_CONT) failed: %s\n", + strerror(errno)); + return false; + } + + alarm(3); + wpid =3D waitpid(pid, &status, __WALL); + if (wpid !=3D pid) { + ksft_print_msg( + "waitpid() failed: %s\n", strerror(errno)); + return false; + } + alarm(0); + if (WIFEXITED(status)) { + ksft_print_msg("child exited prematurely\n"); + return false; + } + if (!WIFSTOPPED(status)) { + ksft_print_msg("child did not stop\n"); + return false; + } + if (WSTOPSIG(status) !=3D SIGTRAP) { + ksft_print_msg("child did not stop with SIGTRAP\n"); + return false; + } + if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo) !=3D 0) { + ksft_print_msg( + "ptrace(PTRACE_GETSIGINFO): %s\n", + strerror(errno)); + return false; + } + if (siginfo.si_code !=3D TRAP_HWBKPT) { + ksft_print_msg( + "Unexpected si_code %d\n", siginfo.si_code); + return false; + } + + kill(pid, SIGKILL); + wpid =3D waitpid(pid, &status, 0); + if (wpid !=3D pid) { + ksft_print_msg( + "waitpid() failed: %s\n", strerror(errno)); + return false; + } + return true; +} + +static void sigalrm(int sig) +{ +} + +int main(int argc, char **argv) +{ + int opt; + bool succeeded =3D true; + struct sigaction act; + int wr, wp, size; + bool result; + + ksft_print_header(); + ksft_set_plan(213); + + act.sa_handler =3D sigalrm; + sigemptyset(&act.sa_mask); + act.sa_flags =3D 0; + sigaction(SIGALRM, &act, NULL); + for (size =3D 1; size <=3D MAX_BP_SIZE; size =3D size*2) { + for (wr =3D 0; wr <=3D MAX_BP_SIZE; wr =3D wr + size) { + for (wp =3D wr - size; wp <=3D wr + size; wp =3D wp + size) { + result =3D run_test(size, MIN(size, 8), wr, wp); + if ((result && wr =3D=3D wp) || + (!result && wr !=3D wp)) + ksft_test_result_pass( + "Test size =3D %d write offset =3D %d watchpoint offset =3D %d\n", + size, wr, wp); + else { + ksft_test_result_fail( + "Test size =3D %d write offset =3D %d watchpoint offset =3D %d\n", + size, wr, wp); + succeeded =3D false; + } + } + } + } + + for (size =3D 1; size <=3D MAX_BP_SIZE; size =3D size*2) { + if (run_test(size, 8, -size, -8)) + ksft_test_result_pass( + "Test size =3D %d write offset =3D %d watchpoint offset =3D -8\n", + size, -size); + else { + ksft_test_result_fail( + "Test size =3D %d write offset =3D %d watchpoint offset =3D -8\n", + size, -size); + succeeded =3D false; + } + } + + if (succeeded) + ksft_exit_pass(); + else + ksft_exit_fail(); +} --=20 2.43.0