LoongArch: ptrace: Fix some watchpoint function
Subsystems (2)
v3 cover LoongArch: ptrace: Fix some watchpoint function
LoongArch defines hardware watchpoint functions for fetch and load/store operations. After the software configures the watchpoints for fetch and load/store, the processor hardware will monitor the access addresses of the fetch and load/store operations and trigger a watchpoint exception when the watchpoint setting conditions are met.
Fix some hardware watchpoint issues on user-space and kernel interface.
All changes are made according to the LoongArch Reference Manual:
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#control-and-status-registers-related-to-watchpoints
Testing for this series can use this gdb patch:
https://sourceware.org/pipermail/gdb-patches/2024-June/209856.html
Changes v1 -> v2:
Suggested by Jinyang and Huacai:
- Modify the commit message.
- Modify the privilege level judgment in ptrace_hbp_set_ctrl() and move the settings of regs->csr_prmd to hw_breakpoint_control().
- Add judgment in ptrace_hbp_set_addr() to ensure that kernel-space address cannot be monitored in user mode.
Changes v2 -> v3:
Suggested by Huacai:
- Modify address and privilege level judgment.
- Modify privilege level settings.
- Make minor changes to the commit message.
Hui Li (3):
LoongArch: ptrace: Fix watchpoint setting error
LoongArch: Trigger user-space watchpoints correctly
LoongArch: Fix multiple hardware watchpoint issues
arch/loongarch/include/asm/hw_breakpoint.h | 4 +- arch/loongarch/kernel/hw_breakpoint.c | 96 +++++++++++++--------- arch/loongarch/kernel/ptrace.c | 48 ++++++----- 3 files changed, 86 insertions(+), 62 deletions(-)
Signature · 1 line
2.38.1
PATCH v3 1/3 LoongArch: ptrace: Fix watchpoint setting error
In the current code, when debugging the following code using gdb, "invalid argument ..." message will be displayed.
lihui@bogon:~$ cat test.c
#include <stdio.h>
int a = 0;
int main()
{
a = 1;
return 0;
}
lihui@bogon:~$ gcc -g test.c -o test
lihui@bogon:~$ gdb test
...
(gdb) watch a
Hardware watchpoint 1: a
(gdb) r
...
Invalid argument setting hardware debug registersIt mainly includes the two types of issues.
1. Some incorrect judgment condition existed in user_watch_state argument parsing, causing -EINVAL to be returned.
When setting up a watchpoint, gdb uses the ptrace interface, ptrace(PTRACE_SETREGSET, tid, NT_LOONGARCH_HW_WATCH, (void *) &iov)). Register values in user_watch_state as follows:
addr[0] = 0x0, mask[0] = 0x0, ctrl[0] = 0x0 addr[1] = 0x0, mask[1] = 0x0, ctrl[1] = 0x0 addr[2] = 0x0, mask[2] = 0x0, ctrl[2] = 0x0 addr[3] = 0x0, mask[3] = 0x0, ctrl[3] = 0x0 addr[4] = 0x0, mask[4] = 0x0, ctrl[4] = 0x0 addr[5] = 0x0, mask[5] = 0x0, ctrl[5] = 0x0 addr[6] = 0x0, mask[6] = 0x0, ctrl[6] = 0x0 addr[7] = 0x12000803c, mask[7] = 0x0, ctrl[7] = 0x610
In arch_bp_generic_fields(), return -EINVAL when ctrl.len is LOONGARCH_BREAKPOINT_LEN_8(0b00). Delete incorrect judgment here.
In ptrace_hbp_fill_attr_ctrl(), when note_type is NT_LOONGARCH_HW_WATCH and ctrl[0] == 0x0, if ((type & HW_BREAKPOINT_RW) != type) will return -EINVAL. Here, ctrl.type should be set based on note_type, and unnecessary judgments can be removed.
2. The watchpoint argument was not set correctly due to unnecessary offset and alignment_mask.
Modify ptrace_hbp_fill_attr_ctrl() and hw_breakpoint_arch_parse(). Ensure the watchpont argument is set correctly.
All changes according to the LoongArch Reference Manual:
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#control-and-status-registers-related-to-watchpoints
--- arch/loongarch/include/asm/hw_breakpoint.h | 2 +- arch/loongarch/kernel/hw_breakpoint.c | 20 ++++---------- arch/loongarch/kernel/ptrace.c | 32 ++++++++++------------ 3 files changed, 21 insertions(+), 33 deletions(-)
arch/loongarch/include/asm/hw_breakpoint.h +1 -1
@@ -101,7 +101,7 @@ struct perf_event;struct perf_event_attr ;extern int arch_bp_generic_fields (struct arch_hw_breakpoint_ctrl ctrl, -int *gen_len,int *gen_type,int *offset); +int *gen_len,int *gen_type);extern int arch_check_bp_in_kernelspace (struct arch_hw_breakpoint *hw); extern int hw_breakpoint_arch_parse(struct perf_event *bp, const struct perf_event_attr *attr,
arch/loongarch/kernel/hw_breakpoint.c +5 -15
@@ -283,7 +283,7 @@ int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)* to generic breakpoint descriptions. */ int arch_bp_generic_fields (struct arch_hw_breakpoint_ctrl ctrl, -int *gen_len,int *gen_type,int *offset) +int *gen_len,int *gen_type) {/* Type */ switch (ctrl.type) {
@@ -303,11 +303,6 @@ int arch_bp_generic_fields(struct arch_hw_breakpoint_ctrl ctrl,return -EINVAL ; } -if (!ctrl.len) -return -EINVAL ; - - *offset =__ffs (ctrl.len); -/* Len */ switch (ctrl.len) {case LOONGARCH_BREAKPOINT_LEN_1 :
@@ -386,22 +381,17 @@ int hw_breakpoint_arch_parse(struct perf_event *bp, struct arch_hw_breakpoint *hw) {int ret; -u64 alignment_mask, offset; +u64 alignment_mask;/* Build the arch_hw_breakpoint. */ ret =arch_build_bp_info (bp, attr, hw);if (ret)return ret; -if (hw->ctrl.type !=LOONGARCH_BREAKPOINT_EXECUTE ) - alignment_mask =0x7 ; -else +if (hw->ctrl.type ==LOONGARCH_BREAKPOINT_EXECUTE ) { alignment_mask =0x3 ; - offset = hw->address & alignment_mask; - - hw->address &= ~alignment_mask; - hw->ctrl.len <<= offset; - + hw->address &= ~alignment_mask; + }return 0 ; }
arch/loongarch/kernel/ptrace.c +15 -17
@@ -494,28 +494,14 @@ static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,struct arch_hw_breakpoint_ctrl ctrl, struct perf_event_attr *attr) { - int err, len, type, offset; + int err, len, type; - err =arch_bp_generic_fields (ctrl, &len, &type, &offset); + err =arch_bp_generic_fields (ctrl, &len, &type);if (err)return err; -switch (note_type) { -case NT_LOONGARCH_HW_BREAK : -if ((type &HW_BREAKPOINT_X ) != type) -return -EINVAL ; -break ; -case NT_LOONGARCH_HW_WATCH : -if ((type &HW_BREAKPOINT_RW ) != type) -return -EINVAL ; -break ; -default : -return -EINVAL ; - } - attr->bp_len = len; attr->bp_type = type; - attr->bp_addr += offset;return 0 ; }
@@ -609,7 +595,19 @@ static int ptrace_hbp_set_ctrl(unsigned int note_type,return PTR_ERR (bp); attr = bp->attr; -decode_ctrl_reg (uctrl, &ctrl); + +switch (note_type) { +case NT_LOONGARCH_HW_BREAK : + ctrl.type =LOONGARCH_BREAKPOINT_EXECUTE ; + ctrl.len =LOONGARCH_BREAKPOINT_LEN_4 ; +break ; +case NT_LOONGARCH_HW_WATCH : +decode_ctrl_reg (uctrl, &ctrl); +break ; +default : +return -EINVAL ; + } + err =ptrace_hbp_fill_attr_ctrl (note_type, ctrl, &attr);if (err)return err;
Signature · 1 line
2.38.1
PATCH v3 2/3 LoongArch: Trigger user-space watchpoints correctly
In reply to Hui LiIn the current code, gdb can set the watchpoint successfully through ptrace interface, but watchpoint will not be triggered.
When debugging the following code using gdb.
lihui@bogon:~$ cat test.c
#include <stdio.h>
int a = 0;
int main()
{
a = 1;
printf("a = %d\n", a);
return 0;
}
lihui@bogon:~$ gcc -g test.c -o test
lihui@bogon:~$ gdb test
...
(gdb) watch a
...
(gdb) r
...
a = 1
[Inferior 1 (process 4650) exited normally]No watchpoints were triggered, the root causes are:
1. Kernel uses perf_event and hw_breakpoint framework to control watchpoint, but the perf_event corresponding to watchpoint is not enabled. So it needs to be enabled according to MWPnCFG3 or FWPnCFG3 PLV bit field in ptrace_hbp_set_ctrl(), and privilege is set according to the monitored addr in hw_breakpoint_control(). Furthermore, add a judgment in ptrace_hbp_set_addr() to ensure kernel-space addr cannot be monitored in user mode.
2. The global enable control for all watchpoints is the WE bit of CSR.CRMD, and hardware sets the value to 0 when an exception is triggered. When the ERTN instruction is executed to return, the hardware restores the value of the PWE field of CSR.PRMD here. So, before a thread containing watchpoints be scheduled, the PWE field of CSR.PRMD needs to be set to 1. Add this modification in hw_breakpoint_control().
All changes according to the LoongArch Reference Manual:
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#control-and-status-registers-related-to-watchpoints
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#basic-control-and-status-registers
with this patch:
lihui@bogon:~$ gdb test ... (gdb) watch a Hardware watchpoint 1: a (gdb) r ... Hardware watchpoint 1: a
Old value = 0
New value = 1
main () at test.c:6
6 printf("a = %d\n", a);
(gdb) c
Continuing.
a = 1
[Inferior 1 (process 775) exited normally]
--- arch/loongarch/include/asm/hw_breakpoint.h | 2 ++ arch/loongarch/kernel/hw_breakpoint.c | 19 +++++++++++++++++-- arch/loongarch/kernel/ptrace.c | 16 +++++++++++++--- 3 files changed, 32 insertions(+), 5 deletions(-)
arch/loongarch/include/asm/hw_breakpoint.h +2 -0
@@ -75,6 +75,8 @@ do { \#define CSR_MWPC_NUM 0x3f#define CTRL_PLV_ENABLE 0x1e +#define CTRL_PLV3_ENABLE 0x10 +#define CTRL_PLV0_ENABLE 0x02#define MWPnCFG3_LoadEn 8#define MWPnCFG3_StoreEn 9
arch/loongarch/kernel/hw_breakpoint.c +17 -2
@@ -178,6 +178,17 @@ static int hw_breakpoint_control(struct perf_event *bp,int i, max_slots, enable;struct perf_event **slots;struct arch_hw_breakpoint *info =counter_arch_bp (bp); +struct pt_regs *regs; +u8 privilege; + +if (arch_check_bp_in_kernelspace (info)) + privilege =CTRL_PLV0_ENABLE ; +else + privilege =CTRL_PLV3_ENABLE ; + +/* Whether bp belongs to a task. */ +if (bp->hw.target) + regs =task_pt_regs (bp->hw.target);if (info->ctrl.type ==LOONGARCH_BREAKPOINT_EXECUTE ) {/* Breakpoint */
@@ -204,13 +215,15 @@ static int hw_breakpoint_control(struct perf_event *bp,write_wb_reg (CSR_CFG_ASID , i,0 ,0 );write_wb_reg (CSR_CFG_ASID , i,1 ,0 );if (info->ctrl.type ==LOONGARCH_BREAKPOINT_EXECUTE ) { -write_wb_reg (CSR_CFG_CTRL , i,0 ,CTRL_PLV_ENABLE ); +write_wb_reg (CSR_CFG_CTRL , i,0 , privilege); }else { ctrl =encode_ctrl_reg (info->ctrl); -write_wb_reg (CSR_CFG_CTRL , i,1 , ctrl |CTRL_PLV_ENABLE ); +write_wb_reg (CSR_CFG_CTRL , i,1 , ctrl | privilege); } enable =csr_read64 (LOONGARCH_CSR_CRMD );csr_write64 (CSR_CRMD_WE | enable,LOONGARCH_CSR_CRMD ); +if (bp->hw.target) + regs->csr_prmd |=CSR_PRMD_PWE ;break ;case HW_BREAKPOINT_UNINSTALL :/* Reset the FWPnCFG/MWPnCFG 1~4 register. */
@@ -222,6 +235,8 @@ static int hw_breakpoint_control(struct perf_event *bp,write_wb_reg (CSR_CFG_CTRL , i,1 ,0 );write_wb_reg (CSR_CFG_ASID , i,0 ,0 );write_wb_reg (CSR_CFG_ASID , i,1 ,0 ); +if (bp->hw.target) + regs->csr_prmd &= ~CSR_PRMD_PWE ;break ; }
arch/loongarch/kernel/ptrace.c +13 -3
@@ -589,6 +589,7 @@ static int ptrace_hbp_set_ctrl(unsigned int note_type,struct perf_event *bp;struct perf_event_attr attr;struct arch_hw_breakpoint_ctrl ctrl; +int hbp_enable = uctrl &CTRL_PLV_ENABLE ; bp =ptrace_hbp_get_initialised_bp (note_type, tsk, idx);if (IS_ERR (bp))
@@ -608,9 +609,14 @@ static int ptrace_hbp_set_ctrl(unsigned int note_type,return -EINVAL ; } - err =ptrace_hbp_fill_attr_ctrl (note_type, ctrl, &attr); -if (err) -return err; +if (hbp_enable &CTRL_PLV_ENABLE ) { + err =ptrace_hbp_fill_attr_ctrl (note_type, ctrl, &attr); +if (err) +return err; + attr.disabled =0 ; + }else { + attr.disabled =1 ; + }return modify_user_hw_breakpoint (bp, &attr); }
@@ -641,6 +647,10 @@ static int ptrace_hbp_set_addr(unsigned int note_type,struct perf_event *bp;struct perf_event_attr attr; +/* Kernel-space address cannot be monitored by user-space */ +if ((unsigned long )addr >=XKPRANGE ) +return -EINVAL ; + bp =ptrace_hbp_get_initialised_bp (note_type, tsk, idx);if (IS_ERR (bp))return PTR_ERR (bp);
Signature · 1 line
2.38.1
PATCH v3 3/3 LoongArch: Fix multiple hardware watchpoint issues
In reply to Hui LiIn the current code, if multiple hardware breakpoint/watchpoint in a user-space thread, some hardware breakpoint/watchpoint will not be triggered.
When debugging the following code using gdb
lihui@bogon:~$ cat test.c
#include <stdio.h>
int a = 0;
int main()
{
printf("start test\n");
a = 1;
printf("a = %d\n", a);
printf("end test\n");
return 0;
}
lihui@bogon:~$ gcc -g test.c -o test
lihui@bogon:~$ gdb test
...
(gdb) start
...
Temporary breakpoint 1, main () at test.c:5
5 printf("start test\n");
(gdb) watch a
Hardware watchpoint 2: a
(gdb) hbreak 8
Hardware assisted breakpoint 3 at 0x1200006ec: file test.c, line 8.
(gdb) c
Continuing.
start test
a = 1Breakpoint 3, main () at test.c:8
8 printf("end test\n");
...
The first hardware watchpoint is not triggered, the root causes are:
1. In hw_breakpoint_control(), The FWPnCFG1.2.4/MWPnCFG1.2.4 register setting are not distinguished. they should be set based on hardware watchpoint functions (fetch or load/store operations).
2. In breakpoint_handler() and watchpoint_handler(), not identifying which watchpoint was triggered, all watchpoint-related perf_event callbacks are called and send siginfo to the user space. Causing user-space unable to determine which watchpoint is triggered. The kernel need to identity which watchpoint is triggered via MWPS/FWPS register and call the corresponding perf event callback to report siginfoto the user-space.
Modify the relevant code to solve above issues. All changes according to the LoongArch Reference Manual:
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#control-and-status-registers-related-to-watchpoints
with this patch:
lihui@bogon:~$ gdb test
...
(gdb) start
...
Temporary breakpoint 1, main () at test.c:5
5 printf("start test\n");
(gdb) watch a
Hardware watchpoint 2: a
(gdb) hbreak 8
Hardware assisted breakpoint 3 at 0x1200006ec: file test.c, line 8.
(gdb) c
Continuing.
start testHardware watchpoint 2: a
Old value = 0
New value = 1
main () at test.c:7
7 printf("a = %d\n", a);
(gdb) c
Continuing.
a = 1
Breakpoint 3, main () at test.c:8
8 printf("end test\n");
(gdb) c
Continuing.
end test
[Inferior 1 (process 778) exited normally]
--- arch/loongarch/kernel/hw_breakpoint.c | 57 ++++++++++++++++----------- 1 file changed, 33 insertions(+), 24 deletions(-)
arch/loongarch/kernel/hw_breakpoint.c +33 -24
@@ -208,15 +208,15 @@ static int hw_breakpoint_control(struct perf_event *bp,switch (ops) {case HW_BREAKPOINT_INSTALL :/* Set the FWPnCFG/MWPnCFG 1~4 register. */ -write_wb_reg (CSR_CFG_ADDR , i,0 , info->address); -write_wb_reg (CSR_CFG_ADDR , i,1 , info->address); -write_wb_reg (CSR_CFG_MASK , i,0 , info->mask); -write_wb_reg (CSR_CFG_MASK , i,1 , info->mask); -write_wb_reg (CSR_CFG_ASID , i,0 ,0 ); -write_wb_reg (CSR_CFG_ASID , i,1 ,0 );if (info->ctrl.type ==LOONGARCH_BREAKPOINT_EXECUTE ) { +write_wb_reg (CSR_CFG_ADDR , i,0 , info->address); +write_wb_reg (CSR_CFG_MASK , i,0 , info->mask); +write_wb_reg (CSR_CFG_ASID , i,0 ,0 );write_wb_reg (CSR_CFG_CTRL , i,0 , privilege); }else { +write_wb_reg (CSR_CFG_ADDR , i,1 , info->address); +write_wb_reg (CSR_CFG_MASK , i,1 , info->mask); +write_wb_reg (CSR_CFG_ASID , i,1 ,0 ); ctrl =encode_ctrl_reg (info->ctrl);write_wb_reg (CSR_CFG_CTRL , i,1 , ctrl | privilege); }
@@ -227,14 +227,17 @@ static int hw_breakpoint_control(struct perf_event *bp,break ;case HW_BREAKPOINT_UNINSTALL :/* Reset the FWPnCFG/MWPnCFG 1~4 register. */ -write_wb_reg (CSR_CFG_ADDR , i,0 ,0 ); -write_wb_reg (CSR_CFG_ADDR , i,1 ,0 ); -write_wb_reg (CSR_CFG_MASK , i,0 ,0 ); -write_wb_reg (CSR_CFG_MASK , i,1 ,0 ); -write_wb_reg (CSR_CFG_CTRL , i,0 ,0 ); -write_wb_reg (CSR_CFG_CTRL , i,1 ,0 ); -write_wb_reg (CSR_CFG_ASID , i,0 ,0 ); -write_wb_reg (CSR_CFG_ASID , i,1 ,0 ); +if (info->ctrl.type ==LOONGARCH_BREAKPOINT_EXECUTE ) { +write_wb_reg (CSR_CFG_ADDR , i,0 ,0 ); +write_wb_reg (CSR_CFG_MASK , i,0 ,0 ); +write_wb_reg (CSR_CFG_CTRL , i,0 ,0 ); +write_wb_reg (CSR_CFG_ASID , i,0 ,0 ); + }else { +write_wb_reg (CSR_CFG_ADDR , i,1 ,0 ); +write_wb_reg (CSR_CFG_MASK , i,1 ,0 ); +write_wb_reg (CSR_CFG_CTRL , i,1 ,0 ); +write_wb_reg (CSR_CFG_ASID , i,1 ,0 ); + }if (bp->hw.target) regs->csr_prmd &= ~CSR_PRMD_PWE ;break ;
@@ -476,12 +479,15 @@ void breakpoint_handler(struct pt_regs *regs) slots =this_cpu_ptr (bp_on_reg);for (i =0 ; i < boot_cpu_data.watch_ireg_count; ++i) { - bp = slots[i]; -if (bp ==NULL ) -continue ; -perf_bp_event (bp, regs); +if ((csr_read32 (LOONGARCH_CSR_FWPS ) & (0x1 << i))) { + bp = slots[i]; +if (bp ==NULL ) +continue ; +perf_bp_event (bp, regs); +csr_write32 (0x1 << i,LOONGARCH_CSR_FWPS ); +update_bp_registers (regs,0 ,0 ); + } } -update_bp_registers (regs,0 ,0 ); }NOKPROBE_SYMBOL (breakpoint_handler);
@@ -493,12 +499,15 @@ void watchpoint_handler(struct pt_regs *regs) slots =this_cpu_ptr (wp_on_reg);for (i =0 ; i < boot_cpu_data.watch_dreg_count; ++i) { - wp = slots[i]; -if (wp ==NULL ) -continue ; -perf_bp_event (wp, regs); +if ((csr_read32 (LOONGARCH_CSR_MWPS ) & (0x1 << i))) { + wp = slots[i]; +if (wp ==NULL ) +continue ; +perf_bp_event (wp, regs); +csr_write32 (0x1 << i,LOONGARCH_CSR_MWPS ); +update_bp_registers (regs,0 ,1 ); + } } -update_bp_registers (regs,0 ,1 ); }NOKPROBE_SYMBOL (watchpoint_handler);
Signature · 1 line
2.38.1
Huacai Chen
maintainerSeries applied, thanks.
Huacai
Quoted 42 lines · Hui Li
LoongArch defines hardware watchpoint functions for fetch and load/store operations. After the software configures the watchpoints for fetch and load/store, the processor hardware will monitor the access addresses of the fetch and load/store operations and trigger a watchpoint exception when the watchpoint setting conditions are met.
Fix some hardware watchpoint issues on user-space and kernel interface.
All changes are made according to the LoongArch Reference Manual:
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#control-and-status-registers-related-to-watchpointsTesting for this series can use this gdb patch:
https://sourceware.org/pipermail/gdb-patches/2024-June/209856.htmlChanges v1 -> v2:
Suggested by Jinyang and Huacai:
- Modify the commit message.
- Modify the privilege level judgment inptrace_hbp_set_ctrl()and move the settings ofregs->csr_prmdtohw_breakpoint_control().
- Add judgment inptrace_hbp_set_addr()to ensure that kernel-space address cannot be monitored in user mode.Changes v2 -> v3:
Suggested by Huacai:
- Modify address and privilege level judgment.
- Modify privilege level settings.
- Make minor changes to the commit message.Hui Li (3):
LoongArch: ptrace: Fix watchpoint setting error
LoongArch: Trigger user-space watchpoints correctly
LoongArch: Fix multiple hardware watchpoint issuesarch/loongarch/include/asm/hw_breakpoint.h | 4 +- arch/loongarch/kernel/hw_breakpoint.c | 96 +++++++++++++--------- arch/loongarch/kernel/ptrace.c | 48 ++++++----- 3 files changed, 86 insertions(+), 62 deletions(-)--
2.38.1