From nobody Sat Oct 4 00:32:12 2025 Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) (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 058A029AB00 for ; Fri, 22 Aug 2025 17:47:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755884854; cv=none; b=dkZHLP6OEXynFHi7uhUSAT0PRegWT6VfpmKZLu9RGAcb2LNru1MtFpyMIT4gB9bfpaOR5vYD/CT9aPnuQsO+nQG9crHw/RnKC71v5WwIVWYAdDIjqH5HgAuTDJRpSQuD8hQckNL8IlGHnUH4kbuzfPKXmbsjQz+1F12Vf/8mmLY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755884854; c=relaxed/simple; bh=X+6QdUGJFZatXPjCaqp4CDf25eqedNjl9QERE4DuDXU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T6uxq+pPThiz2VVDBUTHdgdIGVJ7vu3rb29NeKd9vIeU4eYgVIBWKR9EWQbHZRLTOEOozNWJ7RWnsbt5sV/tYaF+qM33r5iT6JCu0wpNfuGDgyysLBuJlpi4nBPckr4EjmG4HJcrzKD8fO11NXjAhZcPQRdvWq+O44gli1RULkk= 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=e3EerpOg; arc=none smtp.client-ip=209.85.160.179 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="e3EerpOg" Received: by mail-qt1-f179.google.com with SMTP id d75a77b69052e-4b10c1abfe4so37045821cf.2 for ; Fri, 22 Aug 2025 10:47:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc.com; s=google; t=1755884852; x=1756489652; 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=485omdnoiZ8q/zyCXrPmnF91ALfkIJJ0rUsYbVB32HY=; b=e3EerpOgTOs0gdxSVrqCa1+zdvvnZR9QCOokmGA4jjPoGX5JVzTbqBN8UFDTxwL5cK wu2dNoiw83L79ooas5VO3jhKZZ/rba3+fT8WgZrlRKRxaThc2QDQLQxJo/ysTVj5rWUo C9uwdU7LRRlgdNoRbuwxjIjxc+HD2MM2SE7SGCZXMqMkAYgUxTTc1R/93hJ42HTTPj67 B6vYFtrkkpKTkXt0ICA6uKEBvmK8Qa1iGxnU6OLXs4a37G9iXESh1TKi76PzLEARGfRx namfD9IlT++91I93NMvZYw4kinNZGqKxTy1OrKy+5RUKjP1gwhULxuDLhQhPoTezdVJw z8eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755884852; x=1756489652; 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=485omdnoiZ8q/zyCXrPmnF91ALfkIJJ0rUsYbVB32HY=; b=Z7Ju7pwBh1alzdflEjvDdWgjM28sGJ4YufKFKUm+1++xZmTEYNvD6ZmQJhC7OtWs8s J8ZO2VNhGhvYCkYoEIXR0qzkO3t7d6a7Tr/+Y0nGkK+anV52p4deJCGzvPLw7sHK13eF hRaoAHirl/f5DYxU30EyeN+JS7rSERvhoQVNRPswJnyz5BpvHrnyOL/qiT3NFnClZPwQ 891jcH3JrXYzy4afYV5Hui9PijB2NO/TRDvEha+ex3sYejT/vDk6rV/NypbZ9v8TokJQ 0TQcN/Pz/5ofkFcJXkC+ztnRo+uc+xVcY4JcKzADQiufAB27675nJWlokWFTJlCUg4VG VLrA== X-Forwarded-Encrypted: i=1; AJvYcCU0kPSeBNw+cxVAe903qIlU1NLWjoGFn3gbcJzMxbmi1vhJY6T1zCBQe6ZAxi5CfQux/VrHNy9xWFb8d3s=@vger.kernel.org X-Gm-Message-State: AOJu0YwnIsXqgT6WOs6YemS5Q5aVKqoYt/Rbc9H0w+y5NzR+puKY7dT0 je0XcgVt2ai7H508FOj9B0Nr54hK9+QOz5kOBARcf7amzH6j60JelIZuhcpxr8jIAz8= X-Gm-Gg: ASbGncvssRsvRoPfY2to36NSTx5H1503+ASbICZyTJN4RekPUyhvYDfAelXjCqAfnH2 aXY0yfX+2fPGHBLoaJHbYqKS6YUlRzoR+G6Wmjv8zI0rn6/OtpR6Kryb3zDlRk3nB+MbQq+1nx2 wRxaNLjND2zyIyrqYHad3CIsU7Jc20kInuQ8uOCKO8aHfJMBej2URaiHkdfkK/ljaKdwAP1mgIJ 3EXz8RkPXqYfj278dobzWUGPY1QgHvfxF+uN9YJYAGQh4XObWdWQaC/iUUyJtG3OXq/Sz5cANBT GTyPYtO4DZoV6JA0Fa/Ve5O8n9MjRUrvXV3grrAal8GuEk+RMLoqaJutXFNIgNGOi6XQCx8Qk1i cLZPJ9B4EMc9xqqCYl4azpfltB5ruvFH6UU+FI1O8+gOPN68BfsZbxeP/bZrt86ade80Y2FPYNe thUhp4rgeOuM6iTi0J X-Google-Smtp-Source: AGHT+IGoenLDbU6hbLQEKjRKYVSKG56q+cISud/2IPoQ+SsuP/D2JDv3vRwFBNiEIIl5J7Ci/Xlcfg== X-Received: by 2002:a05:622a:1115:b0:4b2:8ac4:f07f with SMTP id d75a77b69052e-4b2aab8c4e6mr52180651cf.81.1755884851684; Fri, 22 Aug 2025 10:47:31 -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.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Aug 2025 10:47:31 -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 6/8] riscv: ptrace: Add hw breakpoint support Date: Fri, 22 Aug 2025 10:47:13 -0700 Message-ID: <20250822174715.1269138-7-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 ability to setup hw breakpoints to ptrace. Call defines a new structure of __riscv_hwdebug_state which will be passed to ptrace. Signed-off-by: Jesse Taube --- RFC -> V1: - Add struct __riscv_hwdebug_state for ptrace_hbp_set/get - Break out ptrace_hbp_set/get so regset can use them - Check for NULL instead of IS_ERR_OR_NULL - Move ptrace_get/sethbpregs above user_regset V1 -> V2: - No change --- arch/riscv/include/asm/processor.h | 4 + arch/riscv/include/uapi/asm/ptrace.h | 9 +++ arch/riscv/kernel/hw_breakpoint.c | 14 +++- arch/riscv/kernel/process.c | 4 + arch/riscv/kernel/ptrace.c | 110 +++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/pr= ocessor.h index 5f56eb9d114a..488d956a951f 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -12,6 +12,7 @@ =20 #include =20 +#include #include =20 #define arch_get_mmap_end(addr, len, flags) \ @@ -108,6 +109,9 @@ struct thread_struct { struct __riscv_v_ext_state vstate; unsigned long align_ctl; struct __riscv_v_ext_state kernel_vstate; +#ifdef CONFIG_HAVE_HW_BREAKPOINT + struct perf_event *ptrace_bps[RV_MAX_TRIGGERS]; +#endif #ifdef CONFIG_SMP /* Flush the icache on migration */ bool force_icache_flush; diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi= /asm/ptrace.h index a38268b19c3d..20d1aa595cbd 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -14,6 +14,8 @@ =20 #define PTRACE_GETFDPIC_EXEC 0 #define PTRACE_GETFDPIC_INTERP 1 +#define PTRACE_GETHBPREGS 2 +#define PTRACE_SETHBPREGS 3 =20 /* * User-mode register state for core dumps, ptrace, sigcontext @@ -120,6 +122,13 @@ struct __riscv_v_regset_state { char vreg[]; }; =20 +struct __riscv_hwdebug_state { + unsigned long addr; + unsigned long type; + unsigned long len; + unsigned long ctrl; +} __packed; + /* * According to spec: The number of bits in a single vector register, * VLEN >=3D ELEN, which must be a power of 2, and must be no greater than diff --git a/arch/riscv/kernel/hw_breakpoint.c b/arch/riscv/kernel/hw_break= point.c index f12306247436..f8841941f2ab 100644 --- a/arch/riscv/kernel/hw_breakpoint.c +++ b/arch/riscv/kernel/hw_breakpoint.c @@ -715,7 +715,19 @@ void arch_uninstall_hw_breakpoint(struct perf_event *e= vent) pr_warn("%s: Failed to uninstall trigger %d. error: %ld\n", __func__, i,= ret.error); } =20 -void flush_ptrace_hw_breakpoint(struct task_struct *tsk) { } +/* + * Release the user breakpoints used by ptrace + */ +void flush_ptrace_hw_breakpoint(struct task_struct *tsk) +{ + int i; + struct thread_struct *t =3D &tsk->thread; + + for (i =3D 0; i < dbtr_total_num; i++) { + unregister_hw_breakpoint(t->ptrace_bps[i]); + t->ptrace_bps[i] =3D NULL; + } +} =20 void hw_breakpoint_pmu_read(struct perf_event *bp) { } =20 diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 15d8f75902f8..9cf07ecfb523 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -9,6 +9,7 @@ =20 #include #include +#include #include #include #include @@ -164,6 +165,7 @@ void start_thread(struct pt_regs *regs, unsigned long p= c, =20 void flush_thread(void) { + flush_ptrace_hw_breakpoint(current); #ifdef CONFIG_FPU /* * Reset FPU state and context @@ -218,6 +220,8 @@ int copy_thread(struct task_struct *p, const struct ker= nel_clone_args *args) set_bit(MM_CONTEXT_LOCK_PMLEN, &p->mm->context.flags); =20 memset(&p->thread.s, 0, sizeof(p->thread.s)); + if (IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT)) + memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps)); =20 /* p->thread holds context to be restored by __switch_to() */ if (unlikely(args->fn)) { diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index ea67e9fb7a58..e097e6a61910 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -9,11 +9,13 @@ =20 #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -184,6 +186,104 @@ static int tagged_addr_ctrl_set(struct task_struct *t= arget, } #endif =20 +#ifdef CONFIG_HAVE_HW_BREAKPOINT +static void ptrace_hbptriggered(struct perf_event *bp, + struct perf_sample_data *data, + struct pt_regs *regs) +{ + struct arch_hw_breakpoint *bkpt =3D counter_arch_bp(bp); + int num =3D 0; + + force_sig_ptrace_errno_trap(num, (void __user *)bkpt->address); +} + +static int ptrace_hbp_get(struct task_struct *child, unsigned long idx, + struct __riscv_hwdebug_state *state) +{ + struct perf_event *bp; + + if (idx >=3D RV_MAX_TRIGGERS) + return -EINVAL; + + bp =3D child->thread.ptrace_bps[idx]; + + if (!bp) + return -ENOENT; + + state->addr =3D bp->attr.bp_addr; + state->len =3D bp->attr.bp_len; + state->type =3D bp->attr.bp_type; + state->ctrl =3D bp->attr.disabled =3D=3D 1; + + return 0; +} + +static int ptrace_hbp_set(struct task_struct *child, unsigned long idx, + struct __riscv_hwdebug_state *state) +{ + struct perf_event *bp; + struct perf_event_attr attr; + + if (idx >=3D RV_MAX_TRIGGERS) + return -EINVAL; + + bp =3D child->thread.ptrace_bps[idx]; + if (bp) + attr =3D bp->attr; + else + ptrace_breakpoint_init(&attr); + + attr.bp_addr =3D state->addr; + attr.bp_len =3D state->len; + attr.bp_type =3D state->type; + attr.disabled =3D state->ctrl =3D=3D 1; + + if (!bp) { + bp =3D register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, + child); + if (IS_ERR(bp)) + return PTR_ERR(bp); + + child->thread.ptrace_bps[idx] =3D bp; + return 0; + } + + return modify_user_hw_breakpoint(bp, &attr); +} + +/* + * idx selects the breakpoint index. + * Both PTRACE_GETHBPREGS and PTRACE_SETHBPREGS transfer __riscv_hwdebug_s= tate + */ + +static long ptrace_gethbpregs(struct task_struct *child, unsigned long idx, + unsigned long __user *datap) +{ + struct __riscv_hwdebug_state state; + long ret; + + ret =3D ptrace_hbp_get(child, idx, &state); + if (ret) + return ret; + if (copy_to_user(datap, &state, sizeof(state))) + return -EFAULT; + + return 0; +} + +static long ptrace_sethbpregs(struct task_struct *child, unsigned long idx, + unsigned long __user *datap) +{ + struct __riscv_hwdebug_state state; + + if (copy_from_user(&state, datap, sizeof(state))) + return -EFAULT; + + return ptrace_hbp_set(child, idx, &state); + +} +#endif + static const struct user_regset riscv_user_regset[] =3D { [REGSET_X] =3D { .core_note_type =3D NT_PRSTATUS, @@ -340,8 +440,18 @@ long arch_ptrace(struct task_struct *child, long reque= st, unsigned long addr, unsigned long data) { long ret =3D -EIO; + unsigned long __user *datap =3D (unsigned long __user *) data; =20 switch (request) { +#ifdef CONFIG_HAVE_HW_BREAKPOINT + case PTRACE_GETHBPREGS: + ret =3D ptrace_gethbpregs(child, addr, datap); + break; + + case PTRACE_SETHBPREGS: + ret =3D ptrace_sethbpregs(child, addr, datap); + break; +#endif default: ret =3D ptrace_request(child, request, addr, data); break; --=20 2.43.0