From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3791415F3F for ; Mon, 3 Aug 2026 13:49:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785764979; cv=none; b=nqAeQXtHR2v5X9ZukdzPyM01qiTUVJecCv3W2FMIxk0i67ThUjZG8h9j7jS7ZaLGBwTbRrVtLqKgj+JL2zz9tpPNLK/NGMbFkGB8ITm/Q7qlytESQx9NEUJW1dycY9x7Jb+DHww1J6OFdBQCyZ6DWhzvmPCuuOzJiq2B014P+YA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785764979; c=relaxed/simple; bh=pMPMtfZB8VBITwn9uHDlV1BkaeFTtWFb4ZIRKS14VJ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ncej+1Y8Lq9nN6K72e6CUkO33ub8l4GTUN56mMrfLezOSILtIjRl1HIhsSGaO/VQoP/FWN01H+Fns8swROpNzcveH7kLj4KrQ9Il1EHcQnfSrPiW/JOokxWNV84r3SErMltmgtCMchCEBOkcrllVol2791G3hBTxpnrexaayKqk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=PcvdoPEz; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=RRClV0v0; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="PcvdoPEz"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="RRClV0v0" Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 673DTLoW822698 for ; Mon, 3 Aug 2026 13:49:36 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=qcppdkim1; bh=JDL8L5Ufeca w9rYAA5nxqMzomuFEvy76+Mr0sJjLj5Y=; b=PcvdoPEzsECcN+Uj3R7ZuUQZAsQ sUl9+c/jhDZAbgmcnyV8qwbX6rssqi6ITatWqFwUDx3owcOXirwn7mIbt70zkdjL optjzwOPiRf9CRatpuOTkufgwDeHOEexD7mCySsLgL4+O3kYsPBLTW9omhI9cNT2 2gc7b7Hdk6jj8edP6GONDUwltrNiDma2QUh6ZeUyZubCCttp14dN8wVecbCyh8En vWMjBjGo410zRizW5Ie4/MVFi7nGCpQl+G+iWFOrfG2uhA5VsstGeO2z4QcGoNkW yLSAiW5wx3zLzIh12TBGlay6xzyfXzPWrDymNOTD8bvEDVJsGVJwsL0LQfw== Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4ftnrt9u0n-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Mon, 03 Aug 2026 13:49:36 +0000 (GMT) Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2cca5e0a0c9so60762255ad.1 for ; Mon, 03 Aug 2026 06:49:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1785764975; x=1786369775; 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:content-type; bh=JDL8L5Ufecaw9rYAA5nxqMzomuFEvy76+Mr0sJjLj5Y=; b=RRClV0v0+MgbfrYC3QlO3f9N347rB5erI3OtH1Fj8FVG5dJkJSG6H9z1ZoIdTVel0Q 0JqyGMUTkFSIKG6bbgQ9wuZCKFHaV8MdVkfLgPqC0twOh+IA74Vt0cPUtuue2CjOBDOJ FnVNUO9fNtFhoe/WTNBBRsbTA8M8809ULCOG6gDTpavJrc8lzYdl2fKGjfOaG5PqlL15 iKbrUuMb7bkKv3MxI3KmVOn8JYK9fw9i8BmwkD986Mfn/6bfE3tZ+V1MB/jwBiOGeljN /YA8FoDdZk5VfE7AA8P05LqfwB5mv7jXgUKnR2yr5ur4uygXmGSeJULd17F4DVN0mk7t cu8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785764975; x=1786369775; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=JDL8L5Ufecaw9rYAA5nxqMzomuFEvy76+Mr0sJjLj5Y=; b=JEwOSfuSkXGWteXB0WvL5NNpitLc4RpoHPAUL/h8Q62ZtzUZ6IEFoGgPkVUPCshnS4 FNc29vlw4DTxaMW9FrJoPn5Xysf/0jBepNZDRggzLi3JGH6VEzcIIQ1HqAi3CdzIVePu V/0w4Z2WM9QEzd6GG5clfTRCUaPkLPAT2c/Z2BCjXaCEKUsBdt1ZE5PfAjl4ErkY+k5D d2MLgBs6mdQlxZoomKBXf/5frfayHt6RLNwURasi+pCirH3bTqsEQXxzGgq4gK3HeU+H 3V207+K5zyFo+8VhJpKo6khjODpHXqlOUUWnnwGqT07tHwxu0VGFPPZdQRC5RO3SoWgQ E8nA== X-Forwarded-Encrypted: i=1; AHgh+Rq/0fxqk4nY+nypVWJacpfAi5gjsE6ziY1sZg/MexNUjaE57ewLZHJFaf/Z8VTH0vzF7wcrWRpwFFUwq6I=@vger.kernel.org X-Gm-Message-State: AOJu0YwEbejPwMtC/UkYDFgFBojakqT9qDM7zksO6vuf9BLDTuqnOWsq Nx7LzI1++86RISzBPG6gykvcy7KMX5QrwbNuDHMHCkVY3RWFHF38xkF+T8Tyhd5yK7dlNSDRG7d tCdNdZJqryATiPnNXSWfVddNm7B5/FnakFLeFd/S8fUpOXDZUJbEiKCa7B1zOYF2AT8M= X-Gm-Gg: AR+sD13Xk9uIvtbLCngxV2OK+7gYEgbjvII8SkxAYNRnUCtuQ4rgkvs4ebF6NKhPn0H zEDST95Nrtqfgrzqsx4tFsOdRkHEPF5bgH28wKe3dc2iBHkxh5Iv/SH+Km3/ShuqFwwcnSjCyus weAXh4lsxhgVfIbGcVKN2+Y49YFP24rqGQC8XTnucQIzRO0/0eeuoWFPIKwjvsqkO9mGR9VZ/rC WBijLvEjI3WyvyDnsYlSfIPOIO7G4M6qhP4vvd0PtBwShI8GXuP+XjTFyTm3PNONoztfXKCfH5h woK05GfLe/MohnwhszlMIdxyQSemGKTD0wgUk2oJYsl1miIOXXFPB/2wMzGC52m914F/TmDGcGd QNA6qJFWqA6rwhQbcgLStlgxnsxEHp8mrBQKhk0qtN8ns9TR9YDC0VQcA1vFFU267sA+P0nFKwg lpJg+lz7TaW2GYyfpjuMB9ySsD X-Received: by 2002:a05:6a20:431f:b0:3c3:875d:c52f with SMTP id adf61e73a8af0-3c92a540ceemr9489633637.10.1785764975350; Mon, 03 Aug 2026 06:49:35 -0700 (PDT) X-Received: by 2002:a05:6a20:431f:b0:3c3:875d:c52f with SMTP id adf61e73a8af0-3c92a540ceemr9489614637.10.1785764974876; Mon, 03 Aug 2026 06:49:34 -0700 (PDT) Received: from hu-himchau-blr.qualcomm.com (blr-bdr-fw-01_GlobalNAT_AllZones-Outside.qualcomm.com. [103.229.18.19]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3153e18d9c6sm39278870eec.30.2026.08.03.06.49.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 06:49:34 -0700 (PDT) From: Himanshu Chauhan To: linux-riscv@lists.infradead.org Cc: qingfang.deng@linux.dev, alex@ghiti.fr, aou@eecs.berkeley.edu, palmer@dabbelt.com, pjw@kernel.org, shuah@kernel.org, linux-kernel@vger.kernel.org, thecharlesjenkins@gmail.com, jtaubepe@redhat.com, Himanshu Chauhan Subject: [PATCH v6 2/5] riscv: Add breakpoint and watchpoint test for riscv Date: Mon, 3 Aug 2026 19:19:10 +0530 Message-ID: <20260803134913.2013674-3-himanshu.chauhan@oss.qualcomm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260803134913.2013674-1-himanshu.chauhan@oss.qualcomm.com> References: <20260803134913.2013674-1-himanshu.chauhan@oss.qualcomm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODAzMDEyNCBTYWx0ZWRfX/kKjgG9a4qE0 xpBerMrOkb+C9HOpUw426nMoU6n/Gre+hRTJgX5PJTUv+hE2FyjEzvahAnxDN1NVHjdBr9YMpL0 VA67UoiZqvkau/xNb6PIl6NlPVABYiZ9ucYerYnbY0bO3+mqPQcVy05tykcd92qyvvoM7IFwcuX SZHPaDyKjeCKM9gwK0fUmKZZkCWtKf0+F7vrmAXC2PkN9BNxjbCZmcwQr3oB+P96AWiaU50jyU5 mD0hu69vg502gJNyVdx3PqrQGfENSYwzeS8yRgIqjlAKnAR7V1PAFdRXX7xYk8brqw7svvG9NT1 6dsi3/tlq9LrBod8w6+nbXSdyNi3goi65lluCpsKOX8ZeQ1yOI16aX4pgMyxdLwvb9M6d6tC3Oe JoO05KFvvOFUbQAjbdAz03D/Nr8Hip1F0dj6MvHALtQ8/HMixBqHzT/eVvAyqr02ImtQdhlx/X0 KF9rhdPgu4uzIeDKqOg== X-Proofpoint-GUID: LDyaRZoHU6ZtvJckHu_xHIB3NN4u4pFA X-Proofpoint-ORIG-GUID: LDyaRZoHU6ZtvJckHu_xHIB3NN4u4pFA X-Proofpoint-Spam-Info: AW1haW4tMjYwODAzMDEyNCBTYWx0ZWRfX+g43Qn37uUY3 oInD2wkJigl7/9itvcmyKWQp1a3wQcio2j3m0whWTaFNy3/wwjI88EZMJpzS5v2JMIKDE9+GiGb PoQytFNi6yg9XtD6m5mdsE+U1S8KNAI= X-Authority-Analysis: v=2.4 cv=Ft81OWrq c=1 sm=1 tr=0 ts=6a709c70 cx=c_pps a=cmESyDAEBpBGqyK7t0alAg==:117 a=Ou0eQOY4+eZoSc0qltEV5Q==:17 a=Sv0fKeRqtYgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=gowsoOTTUOVcmtlkKump:22 a=EUspDBNiAAAA:8 a=qd3H3YWZb18LjlhzOR0A:9 a=1OuFwYUASf3TG4hYMiVC:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-03_03,2026-08-03_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 impostorscore=0 lowpriorityscore=0 clxscore=1015 adultscore=0 phishscore=0 bulkscore=0 suspectscore=0 priorityscore=1501 spamscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608030124 Add self test for riscv architecture. It uses ptrace to ptrace framework to set/unset break/watchpoint and uses signals to check triggers. Also add $(KHDR_INCLUDES) to CFLAGS for the riscv test build so the UAPI headers exercised by the test can be located during compilation. Signed-off-by: Himanshu Chauhan --- tools/testing/selftests/breakpoints/Makefile | 5 + .../breakpoints/breakpoint_test_riscv.c | 219 ++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 tools/testing/selftests/breakpoints/breakpoint_test_riscv.c diff --git a/tools/testing/selftests/breakpoints/Makefile b/tools/testing/selftests/breakpoints/Makefile index 0b8f5acf7c78..b32c56b3c1db 100644 --- a/tools/testing/selftests/breakpoints/Makefile +++ b/tools/testing/selftests/breakpoints/Makefile @@ -12,5 +12,10 @@ ifneq (,$(filter $(ARCH),aarch64 arm64)) TEST_GEN_PROGS += breakpoint_test_arm64 endif +ifneq (,$(filter $(ARCH),riscv)) +CFLAGS += -static $(KHDR_INCLUDES) +TEST_GEN_PROGS += breakpoint_test_riscv +endif + include ../lib.mk diff --git a/tools/testing/selftests/breakpoints/breakpoint_test_riscv.c b/tools/testing/selftests/breakpoints/breakpoint_test_riscv.c new file mode 100644 index 000000000000..0649940b709e --- /dev/null +++ b/tools/testing/selftests/breakpoints/breakpoint_test_riscv.c @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2026 Qualcomm Technologies, Inc. + * + * Author: Himanshu Chauhan + */ + +#define _GNU_SOURCE +#include /* Definition of PERF_* constants */ +#include /* Definition of HW_* constants */ +#include /* Definition of SYS_* constants */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef noinline +#define noinline __attribute__((noinline)) +#endif + +static int gfd; +sem_t ib_mtx, wp_mtx; +static int bp_triggered, wp_triggered; +static int test_func_sink; +static const int wait_timeout_sec = 5; + +int setup_bp(bool is_x, void *addr, int sig) +{ + struct perf_event_attr pe; + int fd; + + memset(&pe, 0, sizeof(struct perf_event_attr)); + pe.type = PERF_TYPE_BREAKPOINT; + pe.size = sizeof(struct perf_event_attr); + + pe.config = 0; + pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W; + pe.bp_addr = (unsigned long)addr; + pe.bp_len = sizeof(long); + + pe.sample_period = 1; + pe.sample_type = PERF_SAMPLE_IP; + pe.wakeup_events = 1; + + pe.disabled = 1; + pe.exclude_kernel = 1; + pe.exclude_hv = 1; + + fd = syscall(SYS_perf_event_open, &pe, 0, -1, -1, 0); + if (fd < 0) { + printf("Failed to open event: %llx\n", pe.config); + return -1; + } + + fcntl(fd, F_SETFL, O_RDWR | O_NONBLOCK | O_ASYNC); + fcntl(fd, F_SETSIG, sig); + fcntl(fd, F_SETOWN, getpid()); + + ioctl(fd, PERF_EVENT_IOC_RESET, 0); + + return fd; +} + +static void sig_handler_bp(int signum, siginfo_t *oh, void *uc) +{ + int ret; + + bp_triggered++; + + printf("Breakpoint triggered!\n"); + ioctl(gfd, PERF_EVENT_IOC_DISABLE, 0); + ret = sem_post(&ib_mtx); + if (ret) { + printf("Failed to report BP success\n"); + return; + } +} + +static void sig_handler_wp(int signum, siginfo_t *oh, void *uc) +{ + int ret; + + printf("Watchpoint triggered!\n"); + ioctl(gfd, PERF_EVENT_IOC_DISABLE, 0); + wp_triggered++; + + ret = sem_post(&wp_mtx); + + if (ret) { + printf("Failed to report WP success\n"); + return; + } +} + +/* + * Keep a real instruction address for HW execute breakpoints: prevent inlining + * and force a visible side effect so the function can't be optimized away. + */ +static noinline void test_func(void) +{ + test_func_sink++; + __asm__ __volatile__("" : : "g" (test_func_sink)); +} + +static int trigger_bp(void) +{ + struct sigaction sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_sigaction = (void *)sig_handler_bp; + sa.sa_flags = SA_SIGINFO; + + if (sigaction(SIGIO, &sa, NULL) < 0) { + printf("Failed to setup signal handler\n"); + return -1; + } + + gfd = setup_bp(1, test_func, SIGIO); + + if (gfd < 0) { + printf("Failed to setup breakpoint.\n"); + return -1; + } + + ioctl(gfd, PERF_EVENT_IOC_ENABLE, 0); + + test_func(); + + ioctl(gfd, PERF_EVENT_IOC_DISABLE, 0); + + close(gfd); + + return 0; +} + +static int trigger_wp(void) +{ + struct sigaction sa; + unsigned long test_data; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_sigaction = (void *)sig_handler_wp; + sa.sa_flags = SA_SIGINFO; + + if (sigaction(SIGUSR1, &sa, NULL) < 0) { + printf("Failed to setup signal handler\n"); + return -1; + } + + gfd = setup_bp(0, &test_data, SIGUSR1); + + if (gfd < 0) { + printf("Failed to setup watchpoint\n"); + return -1; + } + + ioctl(gfd, PERF_EVENT_IOC_ENABLE, 0); + test_data = 0xdeadbeef; + ioctl(gfd, PERF_EVENT_IOC_DISABLE, 0); + + return 0; +} + +static int wait_event(sem_t *sem, const char *name) +{ + struct timespec ts; + + if (clock_gettime(CLOCK_REALTIME, &ts)) { + printf("%s: Failed to get current time\n", name); + return -1; + } + + /* + * Deadlock fix: avoid blocking forever on sem_wait() if the breakpoint/ + * watchpoint signal never arrives. Use a bounded wait and fail the test + * on timeout instead. + */ + ts.tv_sec += wait_timeout_sec; + if (!sem_timedwait(sem, &ts)) + return 0; + + if (errno == ETIMEDOUT) + printf("%s: Timed out waiting for event\n", name); + else + printf("%s: sem_timedwait() failed with %d\n", name, errno); + + return -1; +} + +int main(int argc, char *argv[]) +{ + sem_init(&ib_mtx, 0, 0); + if (trigger_bp() < 0) + return -1; + if (wait_event(&ib_mtx, "Breakpoint") < 0) + return -1; + + if (bp_triggered) + printf("Breakpoint test passed!\n"); + + sem_init(&wp_mtx, 0, 0); + if (trigger_wp() < 0) + return -1; + if (wait_event(&wp_mtx, "Watchpoint") < 0) + return -1; + + if (wp_triggered) + printf("Watchpoint test passed!\n"); + + return 0; +} -- 2.43.0