• Hui Li's avatar
    LoongArch: Fix watchpoint setting error · f63a47b3
    Hui Li authored
    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 registers
    
    There are mainly 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). So delete the 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(), which
    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
    
    Cc: stable@vger.kernel.org
    Signed-off-by: default avatarHui Li <lihui@loongson.cn>
    Signed-off-by: default avatarHuacai Chen <chenhuacai@loongson.cn>
    f63a47b3
hw_breakpoint.c 12.9 KB