Skip to contentSkip to conversation

Introduce support for hardware break/watchpoints

RFC PATCH In review Himanshu Chauhan 3 participants 10 messages Feb 22, 2024 – May 12, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

cover Introduce support for hardware break/watchpoints

This patchset adds support of hardware breakpoints and watchpoints in RISC-V architecture. The framework is built on top of perf subsystem and SBI debug trigger extension.

Currently following features are not supported and are in works:
- Ptrace support
- Single stepping
- Virtualization of debug triggers

The SBI debug trigger extension proposal (Draft v6) can be found at:
https://lists.riscv.org/g/sig-hypervisors/message/361

The Sdtrig ISA is part of RISC-V debug specification which can be found at:
https://github.com/riscv/riscv-debug-spec

Himanshu Chauhan (2):
riscv: Add SBI debug trigger extension and function ids
riscv: Introduce support for hardware break/watchpoints

 arch/riscv/Kconfig                     |   1 +
 arch/riscv/include/asm/hw_breakpoint.h | 327 ++++++++++++
 arch/riscv/include/asm/kdebug.h        |   3 +-
 arch/riscv/include/asm/sbi.h           |  31 ++
 arch/riscv/kernel/Makefile             |   1 +
 arch/riscv/kernel/hw_breakpoint.c      | 659 +++++++++++++++++++++++++
 arch/riscv/kernel/traps.c              |   6 +
 7 files changed, 1027 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/include/asm/hw_breakpoint.h
 create mode 100644 arch/riscv/kernel/hw_breakpoint.c
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH 1/2 riscv: Add SBI debug trigger extension and function ids

Debug trigger extension is a proposed SBI extension to support native debugging in S-mode and VS-mode.

The proposal for the extension can be found at:
https://lists.riscv.org/g/sig-hypervisors/message/361

This patch adds the extension and the function IDs defined by the extension.

---
 arch/riscv/include/asm/sbi.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
arch/riscv/include/asm/sbi.h +31 -0
index 6e68f8dff76b..e79ff1a2f387 100644
@@ -34,6 +34,9 @@ enum sbi_ext_id {
 	SBI_EXT_DBCN = 0x4442434E,
 	SBI_EXT_STA = 0x535441,
 
+	/* SBI Debug Trigger Extension */
+	SBI_EXT_DBTR = 0x44425452,
+
 	/* Experimentals extensions must lie within this range */
 	SBI_EXT_EXPERIMENTAL_START = 0x08000000,
 	SBI_EXT_EXPERIMENTAL_END = 0x08FFFFFF,
@@ -124,6 +127,34 @@ enum sbi_ext_susp_sleep_type {
 	SBI_SUSP_SLEEP_TYPE_SUSPEND_TO_RAM = 0,
 };
 
+/* SBI debug triggers function IDs */
+enum sbi_ext_dbtr_fid {
+	SBI_EXT_DBTR_NUM_TRIGGERS = 0,
+	SBI_EXT_DBTR_SETUP_SHMEM,
+	SBI_EXT_DBTR_TRIG_READ,
+	SBI_EXT_DBTR_TRIG_INSTALL,
+	SBI_EXT_DBTR_TRIG_UPDATE,
+	SBI_EXT_DBTR_TRIG_UNINSTALL,
+	SBI_EXT_DBTR_TRIG_ENABLE,
+	SBI_EXT_DBTR_TRIG_DISABLE,
+};
+
+struct sbi_dbtr_data_msg {
+	unsigned long tstate;
+	unsigned long tdata1;
+	unsigned long tdata2;
+	unsigned long tdata3;
+};
+
+struct sbi_dbtr_id_msg {
+	unsigned long idx;
+};
+
+struct sbi_dbtr_shmem_entry {
+        struct sbi_dbtr_data_msg data;
+        struct sbi_dbtr_id_msg id;
+};
+
 enum sbi_ext_pmu_fid {
 	SBI_EXT_PMU_NUM_COUNTERS = 0,
 	SBI_EXT_PMU_COUNTER_GET_INFO,
Signature · 1 line
2.34.1
#2 lore raw

RFC PATCH 2/2 riscv: Introduce support for hardware break/watchpoints

In reply to Himanshu Chauhan

RISC-V hardware breakpoint framework is built on top of perf subsystem and uses SBI debug trigger extension to install/uninstall/update/enable/disable hardware triggers as specified in Sdtrig ISA extension.

---
 arch/riscv/Kconfig                     |   1 +
 arch/riscv/include/asm/hw_breakpoint.h | 327 ++++++++++++
 arch/riscv/include/asm/kdebug.h        |   3 +-
 arch/riscv/kernel/Makefile             |   1 +
 arch/riscv/kernel/hw_breakpoint.c      | 659 +++++++++++++++++++++++++
 arch/riscv/kernel/traps.c              |   6 +
 6 files changed, 996 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/include/asm/hw_breakpoint.h
 create mode 100644 arch/riscv/kernel/hw_breakpoint.c
arch/riscv/Kconfig +1 -0
index bffbd869a068..7fbc068a11d5 100644
@@ -128,6 +128,7 @@ config RISCV
 	select HAVE_FUNCTION_ERROR_INJECTION
 	select HAVE_GCC_PLUGINS
 	select HAVE_GENERIC_VDSO if MMU && 64BIT
+	select HAVE_HW_BREAKPOINT if PERF_EVENTS
 	select HAVE_IRQ_TIME_ACCOUNTING
 	select HAVE_KPROBES if !XIP_KERNEL
 	select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL
arch/riscv/include/asm/hw_breakpoint.h new file +327 -0

327 lines not on this page · raw message

arch/riscv/include/asm/kdebug.h +2 -1
index 85ac00411f6e..53e989781aa1 100644
@@ -6,7 +6,8 @@
 enum die_val {
 	DIE_UNUSED,
 	DIE_TRAP,
-	DIE_OOPS
+	DIE_OOPS,
+	DIE_DEBUG
 };
 
 #endif
arch/riscv/kernel/Makefile +1 -0
index f71910718053..7d0205031d63 100644
@@ -81,6 +81,7 @@ obj-$(CONFIG_DYNAMIC_FTRACE)	+= mcount-dyn.o
 
 obj-$(CONFIG_PERF_EVENTS)	+= perf_callchain.o
 obj-$(CONFIG_HAVE_PERF_REGS)	+= perf_regs.o
+obj-$(CONFIG_HAVE_HW_BREAKPOINT)	+= hw_breakpoint.o
 obj-$(CONFIG_RISCV_SBI)		+= sbi.o
 ifeq ($(CONFIG_RISCV_SBI), y)
 obj-$(CONFIG_SMP)		+= sbi-ipi.o
arch/riscv/kernel/hw_breakpoint.c new file +659 -0

659 lines not on this page · raw message

arch/riscv/kernel/traps.c +6 -0
index a1b9be3c4332..53e1dfe5746b 100644
@@ -277,6 +277,12 @@ void handle_break(struct pt_regs *regs)
 	if (probe_breakpoint_handler(regs))
 		return;
 
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+	if (notify_die(DIE_DEBUG, "EBREAK", regs, 0, regs->cause, SIGTRAP)
+	    == NOTIFY_STOP)
+		return;
+#endif
+
 	current->thread.bad_cause = regs->cause;
 
 	if (user_mode(regs))
Signature · 1 line
2.34.1
#3 lore raw

Re: [RFC PATCH 2/2] riscv: Introduce support for hardware break/watchpoints

Quoted 16 lines · Himanshu Chauhan

RISC-V hardware breakpoint framework is built on top of perf subsystem and uses SBI debug trigger extension to install/uninstall/update/enable/disable hardware triggers as specified in Sdtrig ISA extension.

---
 arch/riscv/Kconfig                     |   1 +
 arch/riscv/include/asm/hw_breakpoint.h | 327 ++++++++++++
 arch/riscv/include/asm/kdebug.h        |   3 +-
 arch/riscv/kernel/Makefile             |   1 +
 arch/riscv/kernel/hw_breakpoint.c      | 659 +++++++++++++++++++++++++
 arch/riscv/kernel/traps.c              |   6 +
 6 files changed, 996 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/include/asm/hw_breakpoint.h
 create mode 100644 arch/riscv/kernel/hw_breakpoint.c

...

Quoted 28 lines
arch/riscv/kernel/hw_breakpoint.c new file +23 -0
new file mode 100644
index 000000000000..7787123c7180
+
+void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
+static int __init arch_hw_breakpoint_init(void)
+{
+	unsigned int cpu;
+	int rc = 0;
+
+	for_each_possible_cpu(cpu)
+		raw_spin_lock_init(&per_cpu(ecall_lock, cpu));
+
+	if (!dbtr_init)
+		init_sbi_dbtr();
+
+	if (dbtr_total_num) {
+		pr_info("%s: total number of type %d triggers: %u\n",
+			__func__, dbtr_type, dbtr_total_num);
+	} else {
+		pr_info("%s: No hardware triggers available\n", __func__);
+		goto out;
+	}
+
+	/* Allocate per-cpu shared memory */
+	sbi_dbtr_shmem = __alloc_percpu(sizeof(*sbi_dbtr_shmem) * dbtr_total_num,

arch/riscv/kernel/hw_breakpoint.c line 646

Quoted 10 lines
arch/riscv/kernel/hw_breakpoint.c new file +10 -0
+					PAGE_SIZE);
+
+	if (!sbi_dbtr_shmem) {
+		pr_warn("%s: Failed to allocate shared memory.\n", __func__);
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	/* Hotplug handler to register/unregister shared memory with SBI */
+	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,

When using this, only hart 0 is getting setup. I think instead we want the following to have all harts get setup:

	for_each_online_cpu(cpu)
		arch_smp_setup_sbi_shmem(cpu);

	/* Hotplug handler to register/unregister shared memory with SBI */
	rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,

However, I am testing against tip-of-tree opensbi and am hitting an issue during the setup on all harts:

[    0.202332] arch_smp_setup_sbi_shmem: Invalid address parameter (18446744073709551611)
[    0.202794] CPU 0: HW Breakpoint shared memory registered.

Additionally, this seems like it should be a fatal error, but it continues on to print that the shared memory is registered because there is no check before printing this seemingly successful message.

I know I am reviving an old thread, but do you have any insight into what might be happening?

- Charlie

Quoted 38 lines
arch/riscv/kernel/hw_breakpoint.c new file +13 -0
+			       "riscv/hw_breakpoint:prepare",
+			       arch_smp_setup_sbi_shmem,
+			       arch_smp_teardown_sbi_shmem);
+
+	if (rc < 0) {
+		pr_warn("%s: Failed to setup CPU hotplug state\n", __func__);
+		free_percpu(sbi_dbtr_shmem);
+		return rc;
+	}
+ out:
+	return rc;
+}
+arch_initcall(arch_hw_breakpoint_init);
arch/riscv/kernel/traps.c +6 -0
index a1b9be3c4332..53e1dfe5746b 100644
@@ -277,6 +277,12 @@ void handle_break(struct pt_regs *regs)
 	if (probe_breakpoint_handler(regs))
 		return;
 
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+	if (notify_die(DIE_DEBUG, "EBREAK", regs, 0, regs->cause, SIGTRAP)
+	    == NOTIFY_STOP)
+		return;
+#endif
+
 	current->thread.bad_cause = regs->cause;
 
 	if (user_mode(regs))
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#4 lore raw

Re: [RFC PATCH 2/2] riscv: Introduce support for hardware break/watchpoints

Hi Charlie,

Quoted 60 lines · Charlie Jenkins
Quoted 16 lines · Himanshu Chauhan

RISC-V hardware breakpoint framework is built on top of perf subsystem and uses SBI debug trigger extension to install/uninstall/update/enable/disable hardware triggers as specified in Sdtrig ISA extension.

---
  arch/riscv/Kconfig                     |   1 +
  arch/riscv/include/asm/hw_breakpoint.h | 327 ++++++++++++
  arch/riscv/include/asm/kdebug.h        |   3 +-
  arch/riscv/kernel/Makefile             |   1 +
  arch/riscv/kernel/hw_breakpoint.c      | 659 +++++++++++++++++++++++++
  arch/riscv/kernel/traps.c              |   6 +
  6 files changed, 996 insertions(+), 1 deletion(-)

create mode 100644 arch/riscv/include/asm/hw_breakpoint.h create mode 100644 arch/riscv/kernel/hw_breakpoint.c

...

Quoted 28 lines
arch/riscv/kernel/hw_breakpoint.c new file +23 -0
new file mode 100644
index 000000000000..7787123c7180
+
+void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
+static int __init arch_hw_breakpoint_init(void)
+{
+	unsigned int cpu;
+	int rc = 0;
+
+	for_each_possible_cpu(cpu)
+		raw_spin_lock_init(&per_cpu(ecall_lock, cpu));
+
+	if (!dbtr_init)
+		init_sbi_dbtr();
+
+	if (dbtr_total_num) {
+		pr_info("%s: total number of type %d triggers: %u\n",
+			__func__, dbtr_type, dbtr_total_num);
+	} else {
+		pr_info("%s: No hardware triggers available\n", __func__);
+		goto out;
+	}
+
+	/* Allocate per-cpu shared memory */
+	sbi_dbtr_shmem = __alloc_percpu(sizeof(*sbi_dbtr_shmem) * dbtr_total_num,

arch/riscv/kernel/hw_breakpoint.c line 646

Quoted 10 lines
arch/riscv/kernel/hw_breakpoint.c new file +10 -0
+					PAGE_SIZE);
+
+	if (!sbi_dbtr_shmem) {
+		pr_warn("%s: Failed to allocate shared memory.\n", __func__);
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	/* Hotplug handler to register/unregister shared memory with SBI */
+	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,

When using this, only hart 0 is getting setup. I think instead we want the following to have all harts get setup:

Quoted 5 lines
	for_each_online_cpu(cpu)
		arch_smp_setup_sbi_shmem(cpu);

	/* Hotplug handler to register/unregister shared memory with SBI */
	rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,

cpuhp_setup_state() install the callbacks and invoke the @startup callback (if not NULL) for all online CPUs. So there is no need to call "arch_smp_setup_sbi_shmem" for each CPU and then install the hotplug handler.

If you are running this on QEMU, could you please share the qemu command you are invoking? I will test at my end and update you.

Regards

Himanshu

Quoted 55 lines

However, I am testing against tip-of-tree opensbi and am hitting an issue during the setup on all harts:

[    0.202332] arch_smp_setup_sbi_shmem: Invalid address parameter (18446744073709551611)
[    0.202794] CPU 0: HW Breakpoint shared memory registered.

Additionally, this seems like it should be a fatal error, but it continues on to print that the shared memory is registered because there is no check before printing this seemingly successful message.

I know I am reviving an old thread, but do you have any insight into what might be happening?

- Charlie

Quoted 38 lines
arch/riscv/kernel/hw_breakpoint.c new file +13 -0
+			       "riscv/hw_breakpoint:prepare",
+			       arch_smp_setup_sbi_shmem,
+			       arch_smp_teardown_sbi_shmem);
+
+	if (rc < 0) {
+		pr_warn("%s: Failed to setup CPU hotplug state\n", __func__);
+		free_percpu(sbi_dbtr_shmem);
+		return rc;
+	}
+ out:
+	return rc;
+}
+arch_initcall(arch_hw_breakpoint_init);
arch/riscv/kernel/traps.c +6 -0
index a1b9be3c4332..53e1dfe5746b 100644
@@ -277,6 +277,12 @@ void handle_break(struct pt_regs *regs)
  	if (probe_breakpoint_handler(regs))
  		return;
 
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+	if (notify_die(DIE_DEBUG, "EBREAK", regs, 0, regs->cause, SIGTRAP)
+	    == NOTIFY_STOP)
+		return;
+#endif
+
  	current->thread.bad_cause = regs->cause;
 
  	if (user_mode(regs))
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#5 lore raw

Re: [RFC PATCH 2/2] riscv: Introduce support for hardware break/watchpoints

Quoted 66 lines · Himanshu Chauhan

Hi Charlie,

Quoted 63 lines · Charlie Jenkins
Quoted 16 lines · Himanshu Chauhan

RISC-V hardware breakpoint framework is built on top of perf subsystem and uses SBI debug trigger extension to install/uninstall/update/enable/disable hardware triggers as specified in Sdtrig ISA extension.

---
  arch/riscv/Kconfig                     |   1 +
  arch/riscv/include/asm/hw_breakpoint.h | 327 ++++++++++++
  arch/riscv/include/asm/kdebug.h        |   3 +-
  arch/riscv/kernel/Makefile             |   1 +
  arch/riscv/kernel/hw_breakpoint.c      | 659 +++++++++++++++++++++++++
  arch/riscv/kernel/traps.c              |   6 +
  6 files changed, 996 insertions(+), 1 deletion(-)

create mode 100644 arch/riscv/include/asm/hw_breakpoint.h create mode 100644 arch/riscv/kernel/hw_breakpoint.c

...

Quoted 28 lines
arch/riscv/kernel/hw_breakpoint.c new file +23 -0
new file mode 100644
index 000000000000..7787123c7180
+
+void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
+static int __init arch_hw_breakpoint_init(void)
+{
+	unsigned int cpu;
+	int rc = 0;
+
+	for_each_possible_cpu(cpu)
+		raw_spin_lock_init(&per_cpu(ecall_lock, cpu));
+
+	if (!dbtr_init)
+		init_sbi_dbtr();
+
+	if (dbtr_total_num) {
+		pr_info("%s: total number of type %d triggers: %u\n",
+			__func__, dbtr_type, dbtr_total_num);
+	} else {
+		pr_info("%s: No hardware triggers available\n", __func__);
+		goto out;
+	}
+
+	/* Allocate per-cpu shared memory */
+	sbi_dbtr_shmem = __alloc_percpu(sizeof(*sbi_dbtr_shmem) * dbtr_total_num,

arch/riscv/kernel/hw_breakpoint.c line 646

Quoted 10 lines
arch/riscv/kernel/hw_breakpoint.c new file +10 -0
+					PAGE_SIZE);
+
+	if (!sbi_dbtr_shmem) {
+		pr_warn("%s: Failed to allocate shared memory.\n", __func__);
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	/* Hotplug handler to register/unregister shared memory with SBI */
+	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,

When using this, only hart 0 is getting setup. I think instead we want the following to have all harts get setup:

	for_each_online_cpu(cpu)
		arch_smp_setup_sbi_shmem(cpu);
Quoted 7 lines
Quoted 2 lines
	/* Hotplug handler to register/unregister shared memory with SBI */
	rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,

cpuhp_setup_state() install the callbacks and invoke the @startup callback (if not NULL) for all online CPUs. So there is no need to call "arch_smp_setup_sbi_shmem" for each CPU and then install the hotplug handler.

That's what I thought as well, but when testing that is not what was happening.

Quoted 3 lines

If you are running this on QEMU, could you please share the qemu command you are invoking? I will test at my end and update you.

This is my qemu command:

qemu-system-riscv64 -nographic -m 1G -machine virt -smp 4 \
    -kernel arch/riscv/boot/Image -bios /home/charlie/opensbi/build/platform/generic/firmware/fw_dynamic.bin \
    -append "root=/dev/vda rw earlycon console=ttyS0" \
    -drive file=/home/charlie/buildroot/output/images/rootfs.ext2,format=raw,id=hd0,if=none \
    -cpu rv64,zicond=true \
    -device virtio-blk-device,drive=hd0 -gdb tcp::1234

- Charlie

Quoted 58 lines

Regards

Himanshu

Quoted 53 lines

However, I am testing against tip-of-tree opensbi and am hitting an issue during the setup on all harts:

[    0.202332] arch_smp_setup_sbi_shmem: Invalid address parameter (18446744073709551611)
[    0.202794] CPU 0: HW Breakpoint shared memory registered.

Additionally, this seems like it should be a fatal error, but it continues on to print that the shared memory is registered because there is no check before printing this seemingly successful message.

I know I am reviving an old thread, but do you have any insight into what might be happening?

- Charlie

Quoted 36 lines
arch/riscv/kernel/hw_breakpoint.c new file +13 -0
+			       "riscv/hw_breakpoint:prepare",
+			       arch_smp_setup_sbi_shmem,
+			       arch_smp_teardown_sbi_shmem);
+
+	if (rc < 0) {
+		pr_warn("%s: Failed to setup CPU hotplug state\n", __func__);
+		free_percpu(sbi_dbtr_shmem);
+		return rc;
+	}
+ out:
+	return rc;
+}
+arch_initcall(arch_hw_breakpoint_init);
arch/riscv/kernel/traps.c +6 -0
index a1b9be3c4332..53e1dfe5746b 100644
@@ -277,6 +277,12 @@ void handle_break(struct pt_regs *regs)
  	if (probe_breakpoint_handler(regs))
  		return;
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+	if (notify_die(DIE_DEBUG, "EBREAK", regs, 0, regs->cause, SIGTRAP)
+	    == NOTIFY_STOP)
+		return;
+#endif
+
  	current->thread.bad_cause = regs->cause;
  	if (user_mode(regs))
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#6 lore raw

Re: [RFC PATCH 2/2] riscv: Introduce support for hardware break/watchpoints

Quoted 67 lines · Charlie Jenkins
Quoted 66 lines · Himanshu Chauhan

Hi Charlie,

Quoted 63 lines · Charlie Jenkins
Quoted 16 lines · Himanshu Chauhan

RISC-V hardware breakpoint framework is built on top of perf subsystem and uses SBI debug trigger extension to install/uninstall/update/enable/disable hardware triggers as specified in Sdtrig ISA extension.

---
   arch/riscv/Kconfig                     |   1 +
   arch/riscv/include/asm/hw_breakpoint.h | 327 ++++++++++++
   arch/riscv/include/asm/kdebug.h        |   3 +-
   arch/riscv/kernel/Makefile             |   1 +
   arch/riscv/kernel/hw_breakpoint.c      | 659 +++++++++++++++++++++++++
   arch/riscv/kernel/traps.c              |   6 +
   6 files changed, 996 insertions(+), 1 deletion(-)

create mode 100644 arch/riscv/include/asm/hw_breakpoint.h create mode 100644 arch/riscv/kernel/hw_breakpoint.c

...

Quoted 28 lines
arch/riscv/kernel/hw_breakpoint.c new file +23 -0
new file mode 100644
index 000000000000..7787123c7180
+
+void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
+static int __init arch_hw_breakpoint_init(void)
+{
+	unsigned int cpu;
+	int rc = 0;
+
+	for_each_possible_cpu(cpu)
+		raw_spin_lock_init(&per_cpu(ecall_lock, cpu));
+
+	if (!dbtr_init)
+		init_sbi_dbtr();
+
+	if (dbtr_total_num) {
+		pr_info("%s: total number of type %d triggers: %u\n",
+			__func__, dbtr_type, dbtr_total_num);
+	} else {
+		pr_info("%s: No hardware triggers available\n", __func__);
+		goto out;
+	}
+
+	/* Allocate per-cpu shared memory */
+	sbi_dbtr_shmem = __alloc_percpu(sizeof(*sbi_dbtr_shmem) * dbtr_total_num,

arch/riscv/kernel/hw_breakpoint.c line 646

Quoted 10 lines
arch/riscv/kernel/hw_breakpoint.c new file +10 -0
+					PAGE_SIZE);
+
+	if (!sbi_dbtr_shmem) {
+		pr_warn("%s: Failed to allocate shared memory.\n", __func__);
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	/* Hotplug handler to register/unregister shared memory with SBI */
+	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,

When using this, only hart 0 is getting setup. I think instead we want the following to have all harts get setup:

	for_each_online_cpu(cpu)
		arch_smp_setup_sbi_shmem(cpu);
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
	/* Hotplug handler to register/unregister shared memory with SBI */
	rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,

cpuhp_setup_state() install the callbacks and invoke the @startup callback (if not NULL) for all online CPUs. So there is no need to call "arch_smp_setup_sbi_shmem" for each CPU and then install the hotplug handler.

That's what I thought as well, but when testing that is not what was happening.

I see the initialization happening on all the CPUs. I am using OpenSBI Top-of-line.

Linux kernel is from https://github.com/ventanamicro/linux.git [branch: dev-upstream]

Part of boot log:

[    0.267250] arch_hw_breakpoint_init: total number of type 6 triggers: 2 [    0.268108] CPU 0: HW Breakpoint shared memory registered.
[    0.268835] CPU 1: HW Breakpoint shared memory registered.
[    0.269932] CPU 2: HW Breakpoint shared memory registered.
[    0.270468] CPU 3: HW Breakpoint shared memory registered.
[    0.276554] sse: SBI SSE extension detected
[    0.308172] HugeTLB: allocation took 0ms with hugepage_allocation_threads=1 [    0.309682] HugeTLB: registered 2.00 MiB page size, pre-allocated 0 pages

Can you please send me your config and bootlog?

Regards

Himanshu

Quoted 69 lines
Quoted 2 lines

If you are running this on QEMU, could you please share the qemu command you are invoking? I will test at my end and update you.

This is my qemu command:

qemu-system-riscv64 -nographic -m 1G -machine virt -smp 4 \
     -kernel arch/riscv/boot/Image -bios /home/charlie/opensbi/build/platform/generic/firmware/fw_dynamic.bin \
     -append "root=/dev/vda rw earlycon console=ttyS0" \
     -drive file=/home/charlie/buildroot/output/images/rootfs.ext2,format=raw,id=hd0,if=none \
     -cpu rv64,zicond=true \
     -device virtio-blk-device,drive=hd0 -gdb tcp::1234

- Charlie

Quoted 56 lines

Regards

Himanshu

Quoted 52 lines

However, I am testing against tip-of-tree opensbi and am hitting an issue during the setup on all harts:

[    0.202332] arch_smp_setup_sbi_shmem: Invalid address parameter (18446744073709551611)
[    0.202794] CPU 0: HW Breakpoint shared memory registered.

Additionally, this seems like it should be a fatal error, but it continues on to print that the shared memory is registered because there is no check before printing this seemingly successful message.

I know I am reviving an old thread, but do you have any insight into what might be happening?

- Charlie

Quoted 36 lines
arch/riscv/kernel/hw_breakpoint.c new file +13 -0
+			       "riscv/hw_breakpoint:prepare",
+			       arch_smp_setup_sbi_shmem,
+			       arch_smp_teardown_sbi_shmem);
+
+	if (rc < 0) {
+		pr_warn("%s: Failed to setup CPU hotplug state\n", __func__);
+		free_percpu(sbi_dbtr_shmem);
+		return rc;
+	}
+ out:
+	return rc;
+}
+arch_initcall(arch_hw_breakpoint_init);
arch/riscv/kernel/traps.c +6 -0
index a1b9be3c4332..53e1dfe5746b 100644
@@ -277,6 +277,12 @@ void handle_break(struct pt_regs *regs)
   	if (probe_breakpoint_handler(regs))
   		return;
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+	if (notify_die(DIE_DEBUG, "EBREAK", regs, 0, regs->cause, SIGTRAP)
+	    == NOTIFY_STOP)
+		return;
+#endif
+
   	current->thread.bad_cause = regs->cause;
   	if (user_mode(regs))
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#7 lore raw

Re: [RFC PATCH 2/2] riscv: Introduce support for hardware break/watchpoints

Quoted 74 lines · Himanshu Chauhan
Quoted 73 lines · Charlie Jenkins
Quoted 72 lines · Himanshu Chauhan

Hi Charlie,

Quoted 60 lines · Charlie Jenkins
Quoted 16 lines · Himanshu Chauhan

RISC-V hardware breakpoint framework is built on top of perf subsystem and uses SBI debug trigger extension to install/uninstall/update/enable/disable hardware triggers as specified in Sdtrig ISA extension.

---
   arch/riscv/Kconfig                     |   1 +
   arch/riscv/include/asm/hw_breakpoint.h | 327 ++++++++++++
   arch/riscv/include/asm/kdebug.h        |   3 +-
   arch/riscv/kernel/Makefile             |   1 +
   arch/riscv/kernel/hw_breakpoint.c      | 659 +++++++++++++++++++++++++
   arch/riscv/kernel/traps.c              |   6 +
   6 files changed, 996 insertions(+), 1 deletion(-)

create mode 100644 arch/riscv/include/asm/hw_breakpoint.h create mode 100644 arch/riscv/kernel/hw_breakpoint.c

...

Quoted 28 lines
arch/riscv/kernel/hw_breakpoint.c new file +23 -0
new file mode 100644
index 000000000000..7787123c7180
+
+void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
+static int __init arch_hw_breakpoint_init(void)
+{
+	unsigned int cpu;
+	int rc = 0;
+
+	for_each_possible_cpu(cpu)
+		raw_spin_lock_init(&per_cpu(ecall_lock, cpu));
+
+	if (!dbtr_init)
+		init_sbi_dbtr();
+
+	if (dbtr_total_num) {
+		pr_info("%s: total number of type %d triggers: %u\n",
+			__func__, dbtr_type, dbtr_total_num);
+	} else {
+		pr_info("%s: No hardware triggers available\n", __func__);
+		goto out;
+	}
+
+	/* Allocate per-cpu shared memory */
+	sbi_dbtr_shmem = __alloc_percpu(sizeof(*sbi_dbtr_shmem) * dbtr_total_num,

arch/riscv/kernel/hw_breakpoint.c line 646

Quoted 10 lines
arch/riscv/kernel/hw_breakpoint.c new file +10 -0
+					PAGE_SIZE);
+
+	if (!sbi_dbtr_shmem) {
+		pr_warn("%s: Failed to allocate shared memory.\n", __func__);
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	/* Hotplug handler to register/unregister shared memory with SBI */
+	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,

When using this, only hart 0 is getting setup. I think instead we want the following to have all harts get setup:

Quoted 5 lines
	for_each_online_cpu(cpu)
		arch_smp_setup_sbi_shmem(cpu);

	/* Hotplug handler to register/unregister shared memory with SBI */
	rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,

cpuhp_setup_state() install the callbacks and invoke the @startup callback (if not NULL) for all online CPUs. So there is no need to call "arch_smp_setup_sbi_shmem" for each CPU and then install the hotplug handler.

Quoted 8 lines
Quoted 2 lines

That's what I thought as well, but when testing that is not what was happening.

I see the initialization happening on all the CPUs. I am using OpenSBI Top-of-line.

Linux kernel is from https://github.com/ventanamicro/linux.git [branch:
dev-upstream]

The patch in that branch is different! In this series there is:

#if __riscv_xlen == 64
#define MEM_HI(_m) ((u64)_m >> 32)
#define MEM_LO(_m) ((u64)_m & 0xFFFFFFFFUL)
#elif __riscv_xlen == 32
#define MEM_HI(_m) (((u64)_m >> 32) & 0x3)
#define MEM_LO(_m) ((u64)_m & 0xFFFFFFFFUL)
#else
#error "Unknown __riscv_xlen"
#endif

static int arch_smp_setup_sbi_shmem(unsigned int cpu)

{
	struct sbi_dbtr_shmem_entry *dbtr_shmem;
	unsigned long shmem_pa;
	struct sbiret ret;
	int rc;

	dbtr_shmem = per_cpu_ptr(sbi_dbtr_shmem, cpu);
	if (!dbtr_shmem) {
		pr_err("Invalid per-cpu shared memory for debug triggers\n");
		return -ENODEV;
	}

	shmem_pa = __pa(dbtr_shmem);

	ret = sbi_ecall(SBI_EXT_DBTR, SBI_EXT_DBTR_SETUP_SHMEM,
			(!MEM_LO(shmem_pa) ? 0xFFFFFFFFUL : MEM_LO(shmem_pa)),
			(!MEM_HI(shmem_pa) ? 0xFFFFFFFFUL : MEM_HI(shmem_pa)),
			 0, 0, 0, 0);

But in the Ventana branch there is:

#if __riscv_xlen == 64
#define MEM_HI(_m) 0
#define MEM_LO(_m) ((u64)_m)
#elif __riscv_xlen == 32
#define MEM_HI(_m) ((u64)_m >> 32)
#define MEM_LO(_m) ((u64)_m & 0xFFFFFFFFUL)
#else
#error "Unknown __riscv_xlen"
#endif

static int arch_smp_setup_sbi_shmem(unsigned int cpu)

{
	struct sbi_dbtr_shmem_entry *dbtr_shmem;
	unsigned long shmem_pa;
	struct sbiret ret;
	int rc;

	dbtr_shmem = per_cpu_ptr(sbi_dbtr_shmem, cpu);
	if (!dbtr_shmem) {
		pr_err("Invalid per-cpu shared memory for debug triggers\n");
		return -ENODEV;
	}

	shmem_pa = __pa(dbtr_shmem);

	ret = sbi_ecall(SBI_EXT_DBTR, SBI_EXT_DBTR_SETUP_SHMEM,
			MEM_LO(shmem_pa), MEM_HI(shmem_pa), 0, 0, 0, 0);

I suppose this was causing the shmem setup to fail and preventing the kernel from bringing up the other harts.

Can you send a version 2 that reflects the version in the Ventana tree?

- Charlie

Quoted 89 lines

Part of boot log:

[    0.267250] arch_hw_breakpoint_init: total number of type 6 triggers: 2 [    0.268108] CPU 0: HW Breakpoint shared memory registered.
[    0.268835] CPU 1: HW Breakpoint shared memory registered.
[    0.269932] CPU 2: HW Breakpoint shared memory registered.
[    0.270468] CPU 3: HW Breakpoint shared memory registered.
[    0.276554] sse: SBI SSE extension detected
[    0.308172] HugeTLB: allocation took 0ms with hugepage_allocation_threads=1
[    0.309682] HugeTLB: registered 2.00 MiB page size, pre-allocated 0 pages

Can you please send me your config and bootlog?

Regards

Himanshu

Quoted 69 lines
Quoted 2 lines

If you are running this on QEMU, could you please share the qemu command you are invoking? I will test at my end and update you.

This is my qemu command:

qemu-system-riscv64 -nographic -m 1G -machine virt -smp 4 \
     -kernel arch/riscv/boot/Image -bios /home/charlie/opensbi/build/platform/generic/firmware/fw_dynamic.bin \
     -append "root=/dev/vda rw earlycon console=ttyS0" \
     -drive file=/home/charlie/buildroot/output/images/rootfs.ext2,format=raw,id=hd0,if=none \
     -cpu rv64,zicond=true \
     -device virtio-blk-device,drive=hd0 -gdb tcp::1234

- Charlie

Quoted 56 lines

Regards

Himanshu

Quoted 52 lines

However, I am testing against tip-of-tree opensbi and am hitting an issue during the setup on all harts:

[    0.202332] arch_smp_setup_sbi_shmem: Invalid address parameter (18446744073709551611)
[    0.202794] CPU 0: HW Breakpoint shared memory registered.

Additionally, this seems like it should be a fatal error, but it continues on to print that the shared memory is registered because there is no check before printing this seemingly successful message.

I know I am reviving an old thread, but do you have any insight into what might be happening?

- Charlie

Quoted 36 lines
arch/riscv/kernel/hw_breakpoint.c new file +13 -0
+			       "riscv/hw_breakpoint:prepare",
+			       arch_smp_setup_sbi_shmem,
+			       arch_smp_teardown_sbi_shmem);
+
+	if (rc < 0) {
+		pr_warn("%s: Failed to setup CPU hotplug state\n", __func__);
+		free_percpu(sbi_dbtr_shmem);
+		return rc;
+	}
+ out:
+	return rc;
+}
+arch_initcall(arch_hw_breakpoint_init);
arch/riscv/kernel/traps.c +6 -0
index a1b9be3c4332..53e1dfe5746b 100644
@@ -277,6 +277,12 @@ void handle_break(struct pt_regs *regs)
   	if (probe_breakpoint_handler(regs))
   		return;
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+	if (notify_die(DIE_DEBUG, "EBREAK", regs, 0, regs->cause, SIGTRAP)
+	    == NOTIFY_STOP)
+		return;
+#endif
+
   	current->thread.bad_cause = regs->cause;
   	if (user_mode(regs))
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#8 lore raw

Re: [RFC PATCH 2/2] riscv: Introduce support for hardware break/watchpoints

In reply to Himanshu Chauhan

Hi, Himanshu and Charlie,

Quoted 45 lines
arch/riscv/kernel/hw_breakpoint.c new file +45 -0
+static int arch_smp_setup_sbi_shmem(unsigned int cpu)
+{
+	struct sbi_dbtr_shmem_entry *dbtr_shmem;
+	unsigned long shmem_pa;
+	struct sbiret ret;
+	int rc;
+
+	dbtr_shmem = per_cpu_ptr(sbi_dbtr_shmem, cpu);
+	if (!dbtr_shmem) {
+		pr_err("Invalid per-cpu shared memory for debug triggers\n");
+		return -ENODEV;
+	}
+
+	shmem_pa = __pa(dbtr_shmem);
+
+	ret = sbi_ecall(SBI_EXT_DBTR, SBI_EXT_DBTR_SETUP_SHMEM,
+			(!MEM_LO(shmem_pa) ? 0xFFFFFFFFUL : MEM_LO(shmem_pa)),
+			(!MEM_HI(shmem_pa) ? 0xFFFFFFFFUL : MEM_HI(shmem_pa)),
+			 0, 0, 0, 0);
+
+	if (ret.error) {
+		switch(ret.error) {
+		case SBI_ERR_DENIED:
+			pr_warn("%s: Access denied for shared memory at %lx\n",
+				__func__, shmem_pa);
+			rc = -EPERM;
+			break;
+
+		case SBI_ERR_INVALID_PARAM:
+		case SBI_ERR_INVALID_ADDRESS:
+			pr_warn("%s: Invalid address parameter (%lu)\n",
+				__func__, ret.error);
+			rc = -EINVAL;
+			break;
+
+		case SBI_ERR_ALREADY_AVAILABLE:
+			pr_warn("%s: Shared memory is already set\n",
+				__func__);
+			rc = -EADDRINUSE;
+			break;
+
+		case SBI_ERR_FAILURE:
+			pr_err("%s: Internal sdtrig state error\n",
+			       __func__);
+			rc = -ENXIO;

arch/riscv/kernel/hw_breakpoint.c line 94

Quoted 10 lines
arch/riscv/kernel/hw_breakpoint.c new file +10 -0
+			break;
+
+		default:
+			pr_warn("%s: Unknown error %lu\n", __func__, ret.error);
+			rc = -ENXIO;
+			break;
+		}
+	}
+
+	pr_warn("CPU %d: HW Breakpoint shared memory registered.\n", cpu);

This is printed unconditionally, even if there is an error above.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	return rc;

If ret.error is 0, rc is uninitialized here, which may explain the error that Charlie came across.

Regards,

-- Qingfang

#9 lore raw

Re: [RFC PATCH 2/2] riscv: Introduce support for hardware break/watchpoints

Hi Qingfang,

Thanks for pointing it out! That explains the difference in behaviour. I will fix it.

Regards
Himanshu

Quoted 70 lines · Qingfang Deng

Hi, Himanshu and Charlie,

Quoted 45 lines
arch/riscv/kernel/hw_breakpoint.c new file +45 -0
+static int arch_smp_setup_sbi_shmem(unsigned int cpu)
+{
+     struct sbi_dbtr_shmem_entry *dbtr_shmem;
+     unsigned long shmem_pa;
+     struct sbiret ret;
+     int rc;
+
+     dbtr_shmem = per_cpu_ptr(sbi_dbtr_shmem, cpu);
+     if (!dbtr_shmem) {
+             pr_err("Invalid per-cpu shared memory for debug triggers\n");
+             return -ENODEV;
+     }
+
+     shmem_pa = __pa(dbtr_shmem);
+
+     ret = sbi_ecall(SBI_EXT_DBTR, SBI_EXT_DBTR_SETUP_SHMEM,
+                     (!MEM_LO(shmem_pa) ? 0xFFFFFFFFUL : MEM_LO(shmem_pa)),
+                     (!MEM_HI(shmem_pa) ? 0xFFFFFFFFUL : MEM_HI(shmem_pa)),
+                      0, 0, 0, 0);
+
+     if (ret.error) {
+             switch(ret.error) {
+             case SBI_ERR_DENIED:
+                     pr_warn("%s: Access denied for shared memory at %lx\n",
+                             __func__, shmem_pa);
+                     rc = -EPERM;
+                     break;
+
+             case SBI_ERR_INVALID_PARAM:
+             case SBI_ERR_INVALID_ADDRESS:
+                     pr_warn("%s: Invalid address parameter (%lu)\n",
+                             __func__, ret.error);
+                     rc = -EINVAL;
+                     break;
+
+             case SBI_ERR_ALREADY_AVAILABLE:
+                     pr_warn("%s: Shared memory is already set\n",
+                             __func__);
+                     rc = -EADDRINUSE;
+                     break;
+
+             case SBI_ERR_FAILURE:
+                     pr_err("%s: Internal sdtrig state error\n",
+                            __func__);
+                     rc = -ENXIO;

arch/riscv/kernel/hw_breakpoint.c line 94

Quoted 10 lines
arch/riscv/kernel/hw_breakpoint.c new file +10 -0
+                     break;
+
+             default:
+                     pr_warn("%s: Unknown error %lu\n", __func__, ret.error);
+                     rc = -ENXIO;
+                     break;
+             }
+     }
+
+     pr_warn("CPU %d: HW Breakpoint shared memory registered.\n", cpu);

This is printed unconditionally, even if there is an error above.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+     return rc;

If ret.error is 0, rc is uninitialized here, which may explain the error that Charlie came across.

Regards,

-- Qingfang