Bug 20207

Summary: kernel RFE: aarch64: ptrace: BAS: Support any contiguous range
Product: gdb Reporter: Jan Kratochvil <jan>
Component: externalAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: jan, pedro
Priority: P2    
Version: HEAD   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Bug Depends on: 19806    
Bug Blocks:    

Description Jan Kratochvil 2016-06-05 15:40:47 UTC
A tracker for Linux kernel RFE:
https://bugzilla.redhat.com/show_bug.cgi?id=1342821

GDB currently misses some unaligned watchpoints.  GDB can be modified not to miss then but then it will have some unaligned watchpoints false positives.
  Aarch64: watchpoints set on non-8-byte-aligned addresses are always missed
  https://sourceware.org/bugzilla/show_bug.cgi?id=19806
With this proposed kernel change GDB could be changed to support unaligned watchpoints without misses and without false positives.

ARM Architecture Reference Manual ARMv8, for ARMv8-A architecture profile
The valid values for BAS are 0b0000000 , or a binary number all of whose set bits are contiguous.

But current: linux-2.6/arch/arm64/kernel/hw_breakpoint.c
arch_bp_generic_fields()
permits only these 4 combinations:
#define ARM_BREAKPOINT_LEN_1    0x1
#define ARM_BREAKPOINT_LEN_2    0x3
#define ARM_BREAKPOINT_LEN_4    0xf
#define ARM_BREAKPOINT_LEN_8    0xff

Therefore Linux kernel should support arbitrary contiguous LEN value, not just those 4 values above.

wget https://bugzilla.redhat.com/attachment.cgi?id=1164917;gcc -o aarch64-watchpoint aarch64-watchpoint.c -Wall -g;./aarch64-watchpoint
./aarch64-watchpoint: PTRACE_SETREGSET: NT_ARM_HW_WATCH: Invalid argument
Comment 1 Jan Kratochvil 2017-03-26 21:43:02 UTC
f43365ee17f8 selftests: arm64: add test for unaligned/inexact watchpoint handling
0ddb8e0b784b arm64: Allow hw watchpoint of length 3,5,6 and 7
fdfeff0f9e3d arm64: hw_breakpoint: Handle inexact watchpoint addresses
b08fb180bb88 arm64: Allow hw watchpoint at varied offset from base address
651be3cb0853 hw_breakpoint: Allow watchpoint of length 3,5,6 and 7
Comment 2 Sourceware Commits 2018-05-04 20:30:35 UTC
The master branch has been updated by Jan Kratochvil <jkratoch@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a3b60e4588606354b93508a0008a5ca04b68fad8

commit a3b60e4588606354b93508a0008a5ca04b68fad8
Author: Jan Kratochvil <jan.kratochvil@redhat.com>
Date:   Fri May 4 22:22:04 2018 +0200

    aarch64: PR 19806: watchpoints: false negatives + PR 20207 contiguous ones
    
    Some unaligned watchpoints were currently missed.
    
    On old kernels as specified in
    	kernel RFE: aarch64: ptrace: BAS: Support any contiguous range (edit)
    	https://sourceware.org/bugzilla/show_bug.cgi?id=20207
    after this patch some other unaligned watchpoints will get reported as false
    positives.
    
    With new kernels all the watchpoints should work exactly.
    
    There may be a regresion that it now less merges watchpoints so that with
    multiple overlapping watchpoints it may run out of the 4 hardware watchpoint
    registers.  But as discussed in the original thread GDB needs some generic
    watchpoints merging framework to be used by all the target specific code.
    Even current FSF GDB code does not merge it perfectly.  Also with the more
    precise watchpoints one can technically merge them less.  And I do not think
    it matters too much to improve mergeability only for old kernels.
    Still even on new kernels some better merging logic would make sense.
    
    There remains one issue:
    	kernel-4.15.14-300.fc27.armv7hl
    	FAIL: gdb.base/watchpoint-unaligned.exp: continue
    	FAIL: gdb.base/watchpoint-unaligned.exp: continue
    	(gdb) continue
    	Continuing.
    	Unexpected error setting watchpoint: Invalid argument.
    	(gdb) FAIL: gdb.base/watchpoint-unaligned.exp: continue
    But that looks as a kernel bug to me.
    (1) It is not a regression by this patch.
    (2) It is unrelated to this patch.
    
    gdb/ChangeLog
    2018-05-04  Jan Kratochvil  <jan.kratochvil@redhat.com>
    	    Pedro Alves <palves@redhat.com>
    
    	PR breakpoints/19806 and support for PR external/20207.
    	* NEWS: Mention Aarch64 watchpoint improvements.
    	* aarch64-linux-nat.c (aarch64_linux_stopped_data_address): Fix missed
    	watchpoints and PR external/20207 watchpoints.
    	* nat/aarch64-linux-hw-point.c
    	(kernel_supports_any_contiguous_range): New.
    	(aarch64_watchpoint_offset): New.
    	(aarch64_watchpoint_length): Support PR external/20207 watchpoints.
    	(aarch64_point_encode_ctrl_reg): New parameter offset, new asserts.
    	(aarch64_point_is_aligned): Support PR external/20207 watchpoints.
    	(aarch64_align_watchpoint): New parameters aligned_offset_p and
    	next_addr_orig_p.  Support PR external/20207 watchpoints.
    	(aarch64_downgrade_regs): New.
    	(aarch64_dr_state_insert_one_point): New parameters offset and
    	addr_orig.
    	(aarch64_dr_state_remove_one_point): Likewise.
    	(aarch64_handle_breakpoint): Update caller.
    	(aarch64_handle_aligned_watchpoint): Likewise.
    	(aarch64_handle_unaligned_watchpoint): Support addr_orig and
    	aligned_offset.
    	(aarch64_linux_set_debug_regs): Remove const from state.  Call
    	aarch64_downgrade_regs.
    	(aarch64_show_debug_reg_state): Print also dr_addr_orig_wp.
    	* nat/aarch64-linux-hw-point.h (DR_CONTROL_LENGTH): Rename to ...
    	(DR_CONTROL_MASK): ... this.
    	(struct aarch64_debug_reg_state): New field dr_addr_orig_wp.
    	(unsigned int aarch64_watchpoint_offset): New prototype.
    	(aarch64_linux_set_debug_regs): Remove const from state.
    	* utils.c (align_up, align_down): Move to ...
    	* common/common-utils.c (align_up, align_down): ... here.
    	* utils.h (align_up, align_down): Move to ...
    	* common/common-utils.h (align_up, align_down): ... here.
    
    gdb/gdbserver/ChangeLog
    2018-05-04  Jan Kratochvil  <jan.kratochvil@redhat.com>
    	    Pedro Alves <palves@redhat.com>
    
    	* linux-aarch64-low.c (aarch64_stopped_data_address):
    	Likewise.
    
    gdb/testsuite/ChangeLog
    2018-05-04  Jan Kratochvil  <jan.kratochvil@redhat.com>
    	    Pedro Alves <palves@redhat.com>
    
    	PR breakpoints/19806 and support for PR external/20207.
    	* gdb.base/watchpoint-unaligned.c: New file.
    	* gdb.base/watchpoint-unaligned.exp: New file.