diff mbox

arm64: ptrace: hw_break_set take into account hardware breakpoints number

Message ID 1411977842-16515-2-git-send-email-victor.kamensky@linaro.org (mailing list archive)
State New, archived
Headers show

Commit Message

Victor Kamensky Sept. 29, 2014, 8:04 a.m. UTC
hw_break_set function that performs ptrace_regset for hardware
breakpoints and watchpoints needs to take into account actual
number of hardware breakpoints and watchpoints available in CPU.

Current code iterates over all 16 entries of 'struct user_hwdebug_state'
and tries to reserve hardware breakpoint for each index, which fails
if CPU supports less than 16 hardware breakpoints. One manifestation of
the issue is that gdb fails to debug multithreaded user land application
and exits with 'Unexpected error setting hardware debug registers'
error - ptrace system call for hardware breakpoints regset fails with
ENOSPC.

Solution is to read number of available hardware breakpoints and
watchpoints available in CPU and process only that number of first
entries in passed 'struct user_hwdebug_state' regset. Code assumes
that ptrace caller uses only first entries in 'struct
user_hwdebug_state' up to number of hardware breakpoints and watchpoints
supported by CPU. I.e there are no "ignore me, empty" entries in
the middle of 'struct user_hwdebug_state' entries array.

Signed-off-by: Victor Kamensky <victor.kamensky@linaro.org>
---
 arch/arm64/kernel/ptrace.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

Comments

Will Deacon Sept. 29, 2014, 10:16 a.m. UTC | #1
Hi Victor,

On Mon, Sep 29, 2014 at 09:04:02AM +0100, Victor Kamensky wrote:
> hw_break_set function that performs ptrace_regset for hardware
> breakpoints and watchpoints needs to take into account actual
> number of hardware breakpoints and watchpoints available in CPU.
> 
> Current code iterates over all 16 entries of 'struct user_hwdebug_state'
> and tries to reserve hardware breakpoint for each index, which fails
> if CPU supports less than 16 hardware breakpoints. One manifestation of
> the issue is that gdb fails to debug multithreaded user land application
> and exits with 'Unexpected error setting hardware debug registers'
> error - ptrace system call for hardware breakpoints regset fails with
> ENOSPC.

When does this happen? hw_break_set is driven by userspace, so if GDB is
asking for more registers than we actually have, then this is a GDB bug and
the kernel is doing the right thing.

Have you reproduced this with the latest version of GDB?

Will
Victor Kamensky Sept. 29, 2014, 5:49 p.m. UTC | #2
Hi Will,

On 29 September 2014 03:16, Will Deacon <will.deacon@arm.com> wrote:
> Hi Victor,
>
> On Mon, Sep 29, 2014 at 09:04:02AM +0100, Victor Kamensky wrote:
>> hw_break_set function that performs ptrace_regset for hardware
>> breakpoints and watchpoints needs to take into account actual
>> number of hardware breakpoints and watchpoints available in CPU.
>>
>> Current code iterates over all 16 entries of 'struct user_hwdebug_state'
>> and tries to reserve hardware breakpoint for each index, which fails
>> if CPU supports less than 16 hardware breakpoints. One manifestation of
>> the issue is that gdb fails to debug multithreaded user land application
>> and exits with 'Unexpected error setting hardware debug registers'
>> error - ptrace system call for hardware breakpoints regset fails with
>> ENOSPC.
>
> When does this happen? hw_break_set is driven by userspace, so if GDB is
> asking for more registers than we actually have, then this is a GDB bug and
> the kernel is doing the right thing.
>
> Have you reproduced this with the latest version of GDB?

Thank you for the reply. Just checked latest version of GDB, indeed it
does not have my original issue. It turns out my gdb is based on some
old linaro gdb version that unconditionally passes sizeof(user_hwdebug_state)
as iov.len to PTRACE_SETREGSET with note type NT_ARM_HW_BREAK.
Latest gdb adjusts iov.len based on available number of hardware breakpoints.
I should have checked latest gdb before posting this. Naturally previously
suggested patch is withdrawn.

Question is there any place where variable payload nature of
PTRACE_SETREGSET with note type NT_ARM_HW_BREAK and
NT_ARM_HW_WATCH documented? I've tried kernel Documentation
directory, aarch64 abi document, http://infocenter.arm.com search, none
of them mention NT_ARM_HW_BREAK. Just curious is there any place
for that sort of information to look in the future. If answer that it is
documented by existing kernel/gdb code :), please feel free to ignore
this question.

Thanks,
Victor

> Will
Christopher Covington Oct. 1, 2014, 2:24 p.m. UTC | #3
Hi Victor,

On 09/29/2014 01:49 PM, Victor Kamensky wrote:
> Hi Will,
> 
> On 29 September 2014 03:16, Will Deacon <will.deacon@arm.com> wrote:
>> Hi Victor,
>>
>> On Mon, Sep 29, 2014 at 09:04:02AM +0100, Victor Kamensky wrote:
>>> hw_break_set function that performs ptrace_regset for hardware
>>> breakpoints and watchpoints needs to take into account actual
>>> number of hardware breakpoints and watchpoints available in CPU.
>>>
>>> Current code iterates over all 16 entries of 'struct user_hwdebug_state'
>>> and tries to reserve hardware breakpoint for each index, which fails
>>> if CPU supports less than 16 hardware breakpoints. One manifestation of
>>> the issue is that gdb fails to debug multithreaded user land application
>>> and exits with 'Unexpected error setting hardware debug registers'
>>> error - ptrace system call for hardware breakpoints regset fails with
>>> ENOSPC.
>>
>> When does this happen? hw_break_set is driven by userspace, so if GDB is
>> asking for more registers than we actually have, then this is a GDB bug and
>> the kernel is doing the right thing.
>>
>> Have you reproduced this with the latest version of GDB?
> 
> Thank you for the reply. Just checked latest version of GDB, indeed it
> does not have my original issue. It turns out my gdb is based on some
> old linaro gdb version that unconditionally passes sizeof(user_hwdebug_state)
> as iov.len to PTRACE_SETREGSET with note type NT_ARM_HW_BREAK.
> Latest gdb adjusts iov.len based on available number of hardware breakpoints.
> I should have checked latest gdb before posting this. Naturally previously
> suggested patch is withdrawn.
> 
> Question is there any place where variable payload nature of
> PTRACE_SETREGSET with note type NT_ARM_HW_BREAK and
> NT_ARM_HW_WATCH documented? I've tried kernel Documentation
> directory, aarch64 abi document, http://infocenter.arm.com search, none
> of them mention NT_ARM_HW_BREAK. Just curious is there any place
> for that sort of information to look in the future. If answer that it is
> documented by existing kernel/gdb code :), please feel free to ignore
> this question.

While it doesn't address those specific note types (yet):

http://man7.org/linux/man-pages/man2/ptrace.2.html

       PTRACE_GETREGSET (since Linux 2.6.34)
              Read the tracee's registers.  addr specifies, in an
              architecture-dependent way, the type of registers to be read.
              NT_PRSTATUS (with numerical value 1) usually results in
              reading of general-purpose registers.  If the CPU has, for
              example, floating-point and/or vector registers, they can be
              retrieved by setting addr to the corresponding NT_foo
              constant.  data points to a struct iovec, which describes the
              destination buffer's location and length.  On return, the
              kernel modifies iov.len to indicate the actual number of bytes
              returned.

       PTRACE_SETREGSET (since Linux 2.6.34)
              Modify the tracee's registers.  The meaning of addr and data
              is analogous to PTRACE_GETREGSET.

Perhaps this could be expanded upon.

https://www.kernel.org/doc/man-pages/patches.html

Christopher
diff mbox

Patch

diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c
index fe63ac5..4178ba1 100644
--- a/arch/arm64/kernel/ptrace.c
+++ b/arch/arm64/kernel/ptrace.c
@@ -248,22 +248,32 @@  static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
 	return 0;
 }
 
-static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
+static int ptrace_hbp_get_num_slots(unsigned int note_type, u8 *num)
 {
-	u8 num;
-	u32 reg = 0;
-
 	switch (note_type) {
 	case NT_ARM_HW_BREAK:
-		num = hw_breakpoint_slots(TYPE_INST);
+		*num = hw_breakpoint_slots(TYPE_INST);
 		break;
 	case NT_ARM_HW_WATCH:
-		num = hw_breakpoint_slots(TYPE_DATA);
+		*num = hw_breakpoint_slots(TYPE_DATA);
 		break;
 	default:
 		return -EINVAL;
 	}
 
+	return 0;
+}
+
+static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
+{
+	u8 num;
+	u32 reg = 0;
+	int err;
+
+	err = ptrace_hbp_get_num_slots(note_type, &num);
+	if (err)
+		return err;
+
 	reg |= debug_monitors_arch();
 	reg <<= 8;
 	reg |= num;
@@ -432,6 +442,11 @@  static int hw_break_set(struct task_struct *target,
 	int ret, idx = 0, offset, limit;
 	u32 ctrl;
 	u64 addr;
+	u8 num_slots;
+
+	ret = ptrace_hbp_get_num_slots(note_type, &num_slots);
+	if (ret)
+		return ret;
 
 	/* Resource info and pad */
 	offset = offsetof(struct user_hwdebug_state, dbg_regs);
@@ -441,7 +456,7 @@  static int hw_break_set(struct task_struct *target,
 
 	/* (address, ctrl) registers */
 	limit = regset->n * regset->size;
-	while (count && offset < limit) {
+	while ((count && offset < limit) && (idx < num_slots)) {
 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
 					 offset, offset + PTRACE_HBP_ADDR_SZ);
 		if (ret)