Skip to contentSkip to conversation

S390-HWBKPT v4:S390 architecture specific Hardware

PATCH In review Mahesh Salgaonkar 2 participants 2 messages Feb 3, 2010 – Mar 26, 2010 s390

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 S390-HWBKPT v4:S390 architecture specific Hardware

Introduce s390 specific implementation for the perf-events based hardware breakpoint interfaces defined in kernel/hw_breakpoint.c. Enable the HAVE_HW_BREAKPOINT flag and the Makefile.

---

 arch/s390/Kconfig                       |    1 
 arch/s390/include/asm/Kbuild            |    1 
 arch/s390/include/asm/hw_breakpoint.h   |   60 +++++
 arch/s390/include/asm/processor.h       |    6 +
 arch/s390/kernel/Makefile               |    2 
 arch/s390/kernel/hw_breakpoint.c        |  347 +++++++++++++++++++++++++++++++
 samples/hw_breakpoint/data_breakpoint.c |    4 
 7 files changed, 420 insertions(+), 1 deletions(-)
 create mode 100644 arch/s390/include/asm/hw_breakpoint.h
 create mode 100644 arch/s390/kernel/hw_breakpoint.c
arch/s390/Kconfig +1 -0
index c802352..1f3bfc5 100644
@@ -123,6 +123,7 @@ config S390
 	select ARCH_INLINE_WRITE_UNLOCK_BH
 	select ARCH_INLINE_WRITE_UNLOCK_IRQ
 	select ARCH_INLINE_WRITE_UNLOCK_IRQRESTORE
+	select HAVE_HW_BREAKPOINT
 
 config SCHED_OMIT_FRAME_POINTER
 	bool
arch/s390/include/asm/Kbuild +1 -0
index 63a2341..a80fcdb 100644
@@ -8,6 +8,7 @@ header-y += ucontext.h
 header-y += vtoc.h
 header-y += zcrypt.h
 header-y += chsc.h
+header-y += hw_breakpoint.h
 
 unifdef-y += cmb.h
 unifdef-y += debug.h
arch/s390/include/asm/hw_breakpoint.h new file +60 -0

60 lines not on this page · raw message

arch/s390/include/asm/processor.h +6 -0
index b427154..d7750c4 100644
@@ -25,6 +25,8 @@
  * instruction pointer ("program counter").
  */
 #define current_text_addr() ({ void *pc; asm("basr %0,0" : "=a" (pc)); pc; })
+/* Total number of available HW breakpoint registers */
+#define HBP_NUM	1
 
 static inline void get_cpu_id(struct cpuid *ptr)
 {
@@ -71,6 +73,8 @@ typedef struct {
         __u32 ar4;
 } mm_segment_t;
 
+struct perf_event;
+
 /*
  * Thread structure
  */
@@ -86,6 +90,8 @@ struct thread_struct {
 	unsigned long ieee_instruction_pointer; 
         /* pfault_wait is used to block the process on a pfault event */
 	unsigned long pfault_wait;
+	/* Save middle states of ptrace breakpoints */
+	struct perf_event *ptrace_bps[HBP_NUM];
 };
 
 typedef struct thread_struct thread_struct;
arch/s390/kernel/Makefile +1 -1
index 683f638..62e72b2 100644
@@ -23,7 +23,7 @@ CFLAGS_sysinfo.o += -Iinclude/math-emu -Iarch/s390/math-emu -w
 obj-y	:=  bitmap.o traps.o time.o process.o base.o early.o setup.o \
 	    processor.o sys_s390.o ptrace.o signal.o cpcmd.o ebcdic.o \
 	    s390_ext.o debug.o irq.o ipl.o dis.o diag.o mem_detect.o \
-	    vdso.o vtime.o sysinfo.o nmi.o sclp.o
+	    vdso.o vtime.o sysinfo.o nmi.o sclp.o hw_breakpoint.o
 
 obj-y	+= $(if $(CONFIG_64BIT),entry64.o,entry.o)
 obj-y	+= $(if $(CONFIG_64BIT),reipl64.o,reipl.o)
arch/s390/kernel/hw_breakpoint.c new file +347 -0

347 lines not on this page · raw message

samples/hw_breakpoint/data_breakpoint.c +4 -0
index c69cbe9..465ee4a 100644
@@ -58,7 +58,11 @@ static int __init hw_break_module_init(void)
 	hw_breakpoint_init(&attr);
 	attr.bp_addr = kallsyms_lookup_name(ksym_name);
 	attr.bp_len = HW_BREAKPOINT_LEN_4;
+#ifdef CONFIG_S390
+	attr.bp_type = HW_BREAKPOINT_W;
+#else
 	attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
+#endif
 
 	sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler);
 	if (IS_ERR(sample_hbp)) {
#1 lore raw

Sorry for the late review :-(

Quoted 39 lines · Mahesh Salgaonkar
arch/s390/kernel/hw_breakpoint.c new file +39 -0
+int arch_install_hw_breakpoint(struct perf_event *bp)
+{
+	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
+	struct perf_event **slot;
+	struct task_struct *tsk = bp->ctx->task;
+	per_cr_bits per_regs[1];
+
+	memset(per_regs, 0, sizeof(per_cr_bits));
+
+	slot = &__get_cpu_var(bp_per_reg[0]);
+	if (!*slot) {
+		*slot = bp;
+	} else {
+		WARN_ONCE(1 , "Can't find any breakpoint slot");
+		return -EBUSY;
+	}
+
+	if (info->type == S390_BREAKPOINT_WRITE)
+		per_regs[0].em_storage_alteration = 1;
+	if (info->type == S390_BREAKPOINT_EXECUTE)
+		per_regs[0].em_instruction_fetch = 1;
+	per_regs[0].starting_addr = info->address;
+	per_regs[0].ending_addr = info->address + info->len - 1;
+
+	/* Load the control register 9, 10 and 11 with per info */
+	__ctl_load(per_regs, 9, 11);
+	__get_cpu_var(cpu_per_regs[0]) = per_regs[0];
+
+	if (((per_cr_words *)per_regs)->cr[0] & PER_EM_MASK) {
+		if (!tsk) {
+			/* wide breakpoint in the kernel */
+			/* FIXME:
+			 * It's not good idea to use existing flag in lowcore
+			 * for turning on/off PER tracing in kernel. instead
+			 * define a new flag and handle PER tracing checks in
+			 * entry*.S
+			 */
+
+

arch/s390/kernel/hw_breakpoint.c line 109

Quoted 10 lines
arch/s390/kernel/hw_breakpoint.c new file +10 -0
+			/* set PER bit int psw_kernel_bits to avoid loosing it
+			 * accidently if someone modifies PSW bit directly.
+			 */
+			psw_kernel_bits |= PSW_MASK_PER;
+
+		} else {
+			/* user-space hardware breakpoint */
+			struct pt_regs *regs = task_pt_regs(tsk);
+
+			regs->psw.mask |= PSW_MASK_PER;

That doesn't look right. You may want to setup a breakpoint in the kernel that only triggers in a given task context. Your approach only allows breakpoints in userspace if it is bound to a task context.

arch/s390/kernel/hw_breakpoint.c line 158

Quoted 6 lines
arch/s390/kernel/hw_breakpoint.c new file +6 -0
+static inline int is_kernel(unsigned long addr)
+{
+	if (addr >= (unsigned long)_stext && addr < (unsigned long)_end)
+		return 1;
+	return in_gate_area_no_task(addr);
+}

We have a kernel_text_address() already, it also handles modules.

arch/s390/kernel/hw_breakpoint.c line 185

Quoted 12 lines
arch/s390/kernel/hw_breakpoint.c new file +12 -0
+/*
+ * Store a breakpoint's encoded address, length, and type.
+ */
+static int arch_store_info(struct perf_event *bp)
+{
+	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
+	/*
+	 * For kernel-addresses, either the address or symbol name can be
+	 * specified.
+	 */
+	if (info->name)
+		info->address = kallsyms_lookup_name(info->name);

We don't need the name anymore. Name to addr resolving is done in the generic code now. You can remove the info->name field.

Quoted 11 lines
arch/s390/kernel/hw_breakpoint.c new file +11 -0
+/*
+ * Populate arch specific bp info. s390 arch supports EXECUTE and WRITE
+ * access types.
+ */
+static int arch_build_bp_info(struct perf_event *bp)
+{
+	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
+
+	info->address = bp->attr.bp_addr;
+	info->len = bp->attr.bp_len;
+

arch/s390/kernel/hw_breakpoint.c line 213

Quoted 10 lines
arch/s390/kernel/hw_breakpoint.c new file +10 -0
+	switch (bp->attr.bp_type) {
+	case HW_BREAKPOINT_W:
+		info->type = S390_BREAKPOINT_WRITE;
+		break;
+	case HW_BREAKPOINT_X:
+		info->type = S390_BREAKPOINT_EXECUTE;
+		if (info->len == 0)
+			info->len = 1;
+		break;
+	case HW_BREAKPOINT_W | HW_BREAKPOINT_R:

You can drop the above as you'd fall down to default anyway.

Quoted 13 lines
arch/s390/kernel/hw_breakpoint.c new file +13 -0
+/*
+ * Validate the arch-specific HW Breakpoint register settings
+ */
+int arch_validate_hwbkpt_settings(struct perf_event *bp,
+					struct task_struct *tsk)
+{
+	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
+	int ret;
+
+	ret = arch_build_bp_info(bp);
+	if (ret)
+		return ret;
+

arch/s390/kernel/hw_breakpoint.c line 243

Quoted 10 lines
arch/s390/kernel/hw_breakpoint.c new file +10 -0
+	ret = arch_store_info(bp);
+
+	if (ret)
+		return ret;
+
+	/* Check that the virtual address is in the proper range */
+	if (tsk) {
+		/* user space. */
+		if (!access_ok(VERIFY_WRITE,
+				(void __user *)info->address, info->len - 1))

Seems like access_ok() is a noop is S390. But even if the breakpoint is task bound, it should be able to trigger everywhere.

Quoted 56 lines
arch/s390/kernel/hw_breakpoint.c new file +56 -0
+			return -EFAULT;
+	} else {
+		if (!arch_check_va_in_kernelspace(info->address, info->len))
+			return -EFAULT;
+	}
+
+	return 0;
+}
+
+/*
+ * Release the user breakpoints used by ptrace
+ */
+void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
+{
+	struct thread_struct *t = &tsk->thread;
+
+	unregister_hw_breakpoint(t->ptrace_bps[0]);
+	t->ptrace_bps[0] = NULL;
+}
+
+/*
+ * Handle debug exception notifications.
+ */
+static int __kprobes hw_breakpoint_handler(struct die_args *args)
+{
+	struct perf_event *bp;
+	per_cr_bits saved_cregs[1];
+	per_cr_bits per_regs[1];
+	per_lowcore_bits *per_code;
+	int cpu, rc = NOTIFY_STOP;
+	struct pt_regs *regs = args->regs;
+
+	/* Do an early return if this is not a storage-alteration/instruction
+	 * fetch event.
+	 *
+	 * We may be racing with interrupts by the time we reach here. Check
+	 * if old psw was a kernel/user space. If user space then PER code
+	 * is copied to thread_struct, otherwise it is in lowcore.
+	 */
+	if (regs->psw.mask & PSW_MASK_PSTATE)
+		per_code = &current->thread.per_info.lowcore.bits;
+	else
+		per_code = (per_lowcore_bits *)&S390_lowcore.per_perc_atmid;
+
+	if ((!per_code->perc_storage_alteration &&
+			!per_code->perc_instruction_fetch) ||
+		(per_code->perc_storage_alteration &&
+			per_code->perc_store_real_address))
+		return NOTIFY_DONE;
+
+	/* Disable breakpoints during exception handling */
+	__ctl_store(saved_cregs, 9, 11);
+	memset(per_regs, 0, sizeof(per_cr_bits));
+	__ctl_load(per_regs, 9, 11);
+
+	cpu = get_cpu();

arch/s390/kernel/hw_breakpoint.c line 309

Quoted 10 lines
arch/s390/kernel/hw_breakpoint.c new file +10 -0
+
+	/*
+	 * The counter may be concurrently released but that can only
+	 * occur from a call_rcu() path. We can then safely fetch
+	 * the breakpoint, use its callback, touch its counter
+	 * while we are in an rcu_read_lock() path.
+	 */
+
+	rcu_read_lock();
+	bp = per_cpu(bp_per_reg[0], cpu);

This could be get_cpu_var(), more simply.

Quoted 11 lines
arch/s390/kernel/hw_breakpoint.c new file +11 -0
+	/*
+	 * bp can be NULL due to lazy debug register switching
+	 * or due to concurrent perf counter removing.
+	 */
+	if (bp)
+		rc = NOTIFY_DONE;
+
+	perf_bp_event(bp, args->regs);
+	rcu_read_unlock();
+
+	/* Enable breakpoints */

arch/s390/kernel/hw_breakpoint.c line 347

Quoted 10 lines
arch/s390/kernel/hw_breakpoint.c new file +9 -0
+	__ctl_load(saved_cregs, 9, 11);
+	put_cpu();
+
+	return rc;
+}
 
+void hw_breakpoint_pmu_unthrottle(struct perf_event *bp)
+{
+	/* TODO */
+}

We have removed the need for this stub recently, you can check the commit "1e259e0a9982078896f3404240096cbea01daca4" (hw-breakpoints: Remove stub unthrottle callback)

samples/hw_breakpoint/data_breakpoint.c static int __init hw_break_module_init(void) · line 63

Quoted 11 lines
samples/hw_breakpoint/data_breakpoint.c +3 -0
index c69cbe9..465ee4a 100644
@@ -58,7 +58,11 @@ static int __init hw_break_module_init(void)
 	hw_breakpoint_init(&attr);
 	attr.bp_addr = kallsyms_lookup_name(ksym_name);
 	attr.bp_len = HW_BREAKPOINT_LEN_4;
+#ifdef CONFIG_S390
+	attr.bp_type = HW_BREAKPOINT_W;
+#else

You can just keep HW_BREAKPOINT_W for everyone in this case.

samples/hw_breakpoint/data_breakpoint.c static int __init hw_break_module_init(void) · line 68

Quoted 6 lines
samples/hw_breakpoint/data_breakpoint.c +1 -0
 	attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
+#endif
 
 	sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler);
 	if (IS_ERR(sample_hbp)) {

Thanks.