Skip to contentSkip to conversation

S390-HWBKPT v5: Modify ptrace to use Hardware

PATCH In review Mahesh Salgaonkar 2 participants 2 messages May 27, 2010 – Aug 5, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 3/3 S390-HWBKPT v5: Modify ptrace to use Hardware

Modify the ptrace code to use the hardware breakpoint interfaces for user-space

---

 arch/s390/include/asm/ptrace.h |    3 -
 arch/s390/kernel/process.c     |    3 +
 arch/s390/kernel/ptrace.c      |  218 ++++++++++++++++++++++++++++------------
 3 files changed, 155 insertions(+), 69 deletions(-)
arch/s390/include/asm/ptrace.h +2 -1
index fef9b33..b65f48c 100644
@@ -406,7 +406,8 @@ typedef struct
 	 */
 	unsigned  single_step       : 1;
 	unsigned  instruction_fetch : 1;
-	unsigned                    : 30;
+	unsigned  storage_alteration: 1;
+	unsigned                    : 29;
 	/*
 	 * These addresses are copied into cr10 & cr11 if single
 	 * stepping is switched off
arch/s390/kernel/process.c +3 -0
index 1039fde..7f630a6 100644
@@ -32,6 +32,7 @@
 #include <linux/kernel_stat.h>
 #include <linux/syscalls.h>
 #include <linux/compat.h>
+#include <linux/hw_breakpoint.h>
 #include <asm/compat.h>
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
@@ -153,6 +154,7 @@ void exit_thread(void)
 
 void flush_thread(void)
 {
+	flush_ptrace_hw_breakpoint(current);
 }
 
 void release_thread(struct task_struct *dead_task)
@@ -215,6 +217,7 @@ int copy_thread(unsigned long clone_flags, unsigned long new_stackp,
 	p->thread.mm_segment = get_fs();
 	/* Don't copy debug registers */
 	memset(&p->thread.per_info, 0, sizeof(p->thread.per_info));
+	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 	clear_tsk_thread_flag(p, TIF_SINGLE_STEP);
 	/* Initialize per thread user and system timer values */
 	ti = task_thread_info(p);
arch/s390/kernel/ptrace.c +150 -68

314 lines not on this page · raw message

#1 lore raw

Hi Mahesh,

Quoted 3 lines · Mahesh Salgaonkar

index 9f654da..7393d34 100644

arch/s390/kernel/ptrace.c +0 -0

[...]

Quoted 12 lines
arch/s390/kernel/ptrace.c +12 -0
+static void ptrace_set_breakpoint(struct task_struct *task, int disabled)
+{
+	per_struct *per_info;
+	struct perf_event *bp;
+	struct thread_struct *t = &task->thread;
+	struct perf_event_attr attr;
+
+	ptrace_breakpoint_init(&attr);
+	per_info = (per_struct *) &task->thread.per_info;
+
+	if (per_info->single_step | per_info->instruction_fetch)
+		attr.bp_type = HW_BREAKPOINT_X;

arch/s390/kernel/ptrace.c line 129

Quoted 10 lines
arch/s390/kernel/ptrace.c +10 -0
+	else if (per_info->storage_alteration)
+		attr.bp_type = HW_BREAKPOINT_W;
+	else {
+		ptrace_remove_breakpoint(task);
+		return;
+	}
+
+	attr.disabled = disabled;
+	attr.bp_addr = get_per_addr(per_info);
+	attr.bp_len = get_per_len(per_info);

Ok, this is supposed to convert the arch breakpoints to generic hw breakpoints, but what about the other breakpoint types and breakpoint controls we have and may use on s390? E.g. Successful-Branching event and its control bit?

We could encode that somehow into the bp_type field, however that are generic breakpoint types. Considering that we only have 32 bits in there and that s390 has some special breakpoint types like "Store-using-real-address event" it would eat up a considerable amount of bits in there.
Other architectures would eat up even more bits.
Or a specific amount of bits could be reserved for architecture usage? Too bad that the perf_event_attr structure is user space visible and can't be extended so that we could add another arch specific field for defining special arch breakpoint types.
At least that's how I read the current code.

arch/s390/kernel/ptrace.c ptrace_disable(struct task_struct *child) · line 169

Quoted 5 lines
arch/s390/kernel/ptrace.c +5 -0
+static inline addr_t get_psw_mask(struct perf_event *bp)
+{
+	return bp->hw.info.type == HW_BREAKPOINT_X ? PER_INST_FETCH :
+			(bp->hw.info.type == HW_BREAKPOINT_W ? PER_STG_ALT : 0);
+}

get_per_mask() would be a more appropriate name. And the function is close to unreadable. The following would be much more readable:

	if (bp->hw.info.type == HW_BREAKPOINT_X)
		return PER_INST_FETCH;
	if (bp->hw.info.type == HW_BREAKPOINT_W)
		return PER_STG_ALT;
	return 0;

But...

arch/s390/kernel/ptrace.c ptrace_disable(struct task_struct *child) · line 183

Quoted 13 lines
arch/s390/kernel/ptrace.c +13 -0
+static addr_t ptrace_get_per_info(struct task_struct *child, addr_t offset)
+{
+	per_struct *dummy = NULL;
+	addr_t tmp;
+	struct thread_struct *thread = &(child->thread);
+
+	if (offset < (addr_t) (&dummy->control_regs + 1)) {
+		struct perf_event *bp = thread->ptrace_bps[0];
+
+		if (!bp)
+			return 0;
+		else if (offset == 0)
+			tmp = get_psw_mask(bp);

This changes the semantics of the ptrace interface: for example if the ptrace caller would have set the Successful-branching event bit and maybe in addition the Branch-Address Control bit it would be lost. Not only that these bits wouldn't be returned to user space via the PTRACE_PEEKUSR interface, it wouldn't be enabled and work at all anymore.

Quoted 23 lines

+ * Use hardware breakpoint interface for PER info.
+ */

arch/s390/kernel/ptrace.c +21 -0
+static void ptrace_set_per_info(struct task_struct *child, addr_t offset,
+								addr_t data)
+{
+	per_struct *dummy = NULL;
+	int disabled = 0;
+
+	/*
+	 * gdb tries to set the PER storage alteration bit in
+	 * perf_info->control_regs. With hardware breakpoint interface
+	 * in place, we do not want anything to be set in
+	 * perf_info->control_regs. Instead we will store this info
+	 * in per_info->storage_alteration. Hence make sure that any
+	 * further writes from gdb does not wipe out
+	 * per_info->storage_alteration info.
+	 */
+
+	if (offset == (addr_t) &dummy->control_regs) {
+		if (data & PER_STG_ALT)
+			child->thread.per_info.storage_alteration = 1;
+		else
+			child->thread.per_info.storage_alteration = 0;

arch/s390/kernel/ptrace.c peek_user(struct task_struct *child, addr_t addr, addr_t data) · line 333

Quoted 10 lines
arch/s390/kernel/ptrace.c +10 -0
+	} else if (offset == (addr_t) (&dummy->control_regs + 1)) {
+		if (child->thread.per_info.storage_alteration) {
+			*(addr_t *)((addr_t) &child->thread.per_info + offset)
+									= data;
+			child->thread.per_info.storage_alteration = 1;
+		} else
+			*(addr_t *)((addr_t) &child->thread.per_info + offset)
+									= data;
+	} else if (offset > (addr_t) (&dummy->control_regs + 1))
+		*(addr_t *)((addr_t) &child->thread.per_info + offset) = data;

Now, this is really ugly code and hard to maintain if there would be a need to extend it. We need to come up with something better.