PPC64-HWBKPT: Implement hw-breakpoints for PPC64
Subsystems (2)
PATCH 1/1 PPC64-HWBKPT: Implement hw-breakpoints for PPC64
Implement perf-events based hw-breakpoint interfaces for PPC64 processors. These interfaces help arbitrate requests from various users and schedules them as appropriate.
--- arch/powerpc/Kconfig | 1 arch/powerpc/include/asm/hw_breakpoint.h | 55 +++++ arch/powerpc/include/asm/processor.h | 1 arch/powerpc/include/asm/reg.h | 1 arch/powerpc/kernel/Makefile | 2 arch/powerpc/kernel/hw_breakpoint.c | 325 +++++++++++++++++++++++++++++++ arch/powerpc/kernel/process.c | 5 arch/powerpc/kernel/ptrace.c | 77 +++++++ arch/powerpc/mm/fault.c | 14 - 9 files changed, 472 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/hw_breakpoint.h new file +55 -0
@@ -0,0 +1,55 @@ +#ifndef _PPC64_HW_BREAKPOINT_H +#define _PPC64_HW_BREAKPOINT_H + +#ifdef __KERNEL__ +#define __ARCH_HW_BREAKPOINT_H +#ifdef CONFIG_PPC64 + +struct arch_hw_breakpoint { +u8 len;/* length of the target symbol */ +int type; +char *name;/* Contains name of the symbol to set bkpt */ +unsigned long address; +}; + +#include <linux/kdebug.h> +#include <asm/reg.h> +#include <asm/system.h> + +/* Total number of available HW breakpoint registers */ +#define HBP_NUM 1 + +struct perf_event ; +struct pmu ; +struct perf_sample_data ; + +#define HW_BREAKPOINT_ALIGN 0x7 +/* Maximum permissible length of any HW Breakpoint */ +#define HW_BREAKPOINT_LEN 0x8 + +extern int arch_validate_hwbkpt_settings (struct perf_event *bp, +struct task_struct *tsk); +extern int hw_breakpoint_exceptions_notify (struct notifier_block *unused, +unsigned long val,void *data); +int arch_install_hw_breakpoint (struct perf_event *bp); +void arch_uninstall_hw_breakpoint (struct perf_event *bp); +void hw_breakpoint_pmu_read (struct perf_event *bp); +void hw_breakpoint_pmu_unthrottle (struct perf_event *bp); +extern void flush_ptrace_hw_breakpoint (struct task_struct *tsk); + +extern struct pmu perf_ops_bp; +extern void ptrace_triggered (struct perf_event *bp,int nmi, +struct perf_sample_data *data,struct pt_regs *regs); +static inline void hw_breakpoint_disable (void ) +{ +set_dabr (0 ); +} + +#else +static inline void hw_breakpoint_disable (void ) +{ +/* Function is defined only on PPC64 for now */ +} +#endif /* CONFIG_PPC64 */ +#endif /* __KERNEL__ */ +#endif /* _PPC64_HW_BREAKPOINT_H */
arch/powerpc/kernel/hw_breakpoint.c new file +325 -0
325 lines not on this page · raw message
arch/powerpc/Kconfig +1 -0
@@ -130,6 +130,7 @@ config PPC select HAVE_SYSCALL_WRAPPERS if PPC64 select GENERIC_ATOMIC64 if PPC32 select HAVE_PERF_EVENTS + select HAVE_HW_BREAKPOINT if PPC64 config EARLY_PRINTK bool
arch/powerpc/kernel/Makefile +1 -1
@@ -33,7 +33,7 @@ obj-y := cputable.o ptrace.o syscalls obj-y += vdso32/ obj-$(CONFIG_PPC64) += setup_64.o sys_ppc32.o \ signal_64.o ptrace32.o \ - paca.o nvram_64.o firmware.o + paca.o nvram_64.o firmware.o hw_breakpoint.o obj-$(CONFIG_PPC_BOOK3S_64) += cpu_setup_ppc970.o cpu_setup_pa6t.o obj64-$(CONFIG_RELOCATABLE) += reloc_64.o obj-$(CONFIG_PPC_BOOK3E_64) += exceptions-64e.o
arch/powerpc/include/asm/reg.h +1 -0
@@ -180,6 +180,7 @@#define CTRL_TE 0x00c00000/* thread enable */ #define CTRL_RUNLATCH 0x1#define SPRN_DABR 0x3F5/* Data Address Breakpoint Register */ +#define HBP_NUM 1/* Number of physical HW breakpoint registers */ #define DABR_TRANSLATION (1UL << 2)#define DABR_DATA_WRITE (1UL << 1)#define DABR_DATA_READ (1UL << 0)
arch/powerpc/mm/fault.c +6 -8
@@ -137,6 +137,12 @@ int __kprobes do_page_fault(struct pt_re error_code &=0x48200000 ; else is_write = error_code &DSISR_ISSTORE ; + +if (error_code &DSISR_DABRMATCH ) { +/* DABR match */ +do_dabr (regs, address, error_code); +return 0 ; + }#else is_write = error_code &ESR_DST ;#endif /* CONFIG_4xx || CONFIG_BOOKE */
@@ -151,14 +157,6 @@ int __kprobes do_page_fault(struct pt_reif (!user_mode (regs) && (address >=TASK_SIZE ))return SIGSEGV ; -#if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE )) -if (error_code &DSISR_DABRMATCH ) { -/* DABR match */ -do_dabr (regs, address, error_code); -return 0 ; - } -#endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/ -if (in_atomic () || mm ==NULL ) {if (!user_mode (regs))return SIGSEGV ;
arch/powerpc/include/asm/processor.h +1 -0
@@ -177,6 +177,7 @@ struct thread_struct {#ifdef CONFIG_PPC64 unsigned long start_tb;/* Start purr when proc switched in */ unsigned long accum_tb;/* Total accumilated purr for process */ +struct perf_event *ptrace_bps[HBP_NUM ];#endif unsigned long dabr;/* Data address breakpoint register */ #ifdef CONFIG_ALTIVEC
arch/powerpc/kernel/ptrace.c +77 -0
98 lines not on this page · raw message
arch/powerpc/kernel/process.c +5 -0
@@ -48,6 +48,7 @@#include <asm/machdep.h> #include <asm/time.h> #include <asm/syscalls.h> +#include <asm/hw_breakpoint.h> #ifdef CONFIG_PPC64 #include <asm/firmware.h> #endif
@@ -376,8 +377,11 @@ struct task_struct *__switch_to(struct tif (new->thread.dabr)set_dabr (new->thread.dabr);#else +/* For PPC64, we use the hw-breakpoint interfaces that would schedule DABR */ +#ifndef CONFIG_PPC64 if (unlikely (__get_cpu_var (current_dabr) != new->thread.dabr))set_dabr (new->thread.dabr); +#endif /* CONFIG_PPC64 */ #endif
@@ -564,6 +568,7 @@ void flush_thread(void) else set_ti_thread_flag(t, TIF_32BIT); } +flush_ptrace_hw_breakpoint (current);#endif discard_lazy_cpu_state ();
I can't see anything you've done to keep this use of MSR_SE in the user-mode register state from interfering with user_enable_single_step(). It looks to me like you'd swallow the normal step indications.
Likewise I'm not very clear on the interaction with kprobes, kgdb, or whatnot for kernel-mode cases. But I'll leave those concerns to others, since I know more about the user-mode situations.
Back to the user-mode case, is it really reasonable to disable preemption in hw_breakpoint_handler and leave it so across returning to user mode? (Is that even possible? I thought user mode was always preemptible.) That is done very casually with little comment in hw_breakpoint_handler and single_step_dabr_instruction, but it seems like an extremely deep and magical thing that merits more explanation. I guess the need for it has to do with the per_cpu variable you're using, but the whole situation is not very clear on first reading. Even for kernel mode, what does this mean when the stepped instruction does a page fault?
Thanks,
Roland
Quoted 4 lines · Roland McGrath
I can't see anything you've done to keep this use of
MSR_SEin the user-mode register state from interfering withuser_enable_single_step(). It looks to me like you'd swallow the normal step indications.
Yes, it does unset MSR_SE bit in single_step_dabr_instruction() irrespective of whether it was previously enabled through user_enable_single_step(). This could be mitigated with the use of a separate flag which can be used to conditionally unset MSR_SE, however given further concerns about pre-emption (as expressed by you below), I'm afraid of substantial revamp of the user-space semantics.
Quoted 4 lines
Likewise I'm not very clear on the interaction with kprobes, kgdb, or whatnot for kernel-mode cases. But I'll leave those concerns to others, since I know more about the user-mode situations.
Kprobes has been tested to work simultaneously with hw-breakpoints. KGDB has not been ported yet to use the hw-breakpoint interfaces (KGDB had issues in it, that prevented it from being tested during our development...though its maintainer has begun showing interest recently).
Xmon was (and I believe is still) in a state where data breakpoints did not work. It needs to be ported too, to benefit from the hw-breakpoint interfaces.
Quoted 11 lines
Back to the user-mode case, is it really reasonable to disable preemption in
hw_breakpoint_handlerand leave it so across returning to user mode? (Is that even possible? I thought user mode was always preemptible.) That is done very casually with little comment inhw_breakpoint_handlerandsingle_step_dabr_instruction, but it seems like an extremely deep and magical thing that merits more explanation. I guess the need for it has to do with theper_cpuvariable you're using, but the whole situation is not very clear on first reading. Even for kernel mode, what does this mean when the stepped instruction does a page fault?
I must admit that the issue of pre-emption should have been given more thought. Suppose there's a stream of user-space instructions "i1, i2, i3, .....i<n>" and if 'i2' instruction can cause a hw-breakpoint exception, then there exists a small window between i1 and i2 where pre-emption is disabled (while a schedule operation could have taken place otherwise).
Disabling pre-emption is necessary to ensure that hw-breakpoints are enabled immediately after the causative instruction has finished execution (the control flow may go astray if pre-emption occurs between i1 and i2). The root cause of this behaviour is a combination of 'trigger-before-execute' behaviour (for data-exceptions) and a desire for 'continuous' exceptions (as opposed to one-shot behaviour seen in ptrace). The per-cpu variable 'last_hit_bp' just helps identify a single-step exception resulting from a hbp_handler vs other sources.
Resorting to one-shot behaviour (which is the easiest workaround available) will break the desired uniformity in behaviour for hw-breakpoint interfaces - say every register_user_<> interface must be accompanied by a unregister_<> interface, etc. Post perf-events' integration, ensuring a one-shot behaviour might also have its own bunch of undesirable consequences (such as circular locks), that must be overcome.
Unless I see a way to re-instate the breakpoints (surviving a pre-emption), I will send out a new patch that resorts to a one-shot behaviour for user-space (kernel-space is fine though).
Thank you for the insightful comments!
K.Prasad
Quoted 6 lines
Yes, it does unset
MSR_SEbit insingle_step_dabr_instruction()irrespective of whether it was previously enabled throughuser_enable_single_step(). This could be mitigated with the use of a separate flag which can be used to conditionally unsetMSR_SE, however given further concerns about pre-emption (as expressed by you below), I'm afraid of substantial revamp of the user-space semantics.
There is already TIF_SINGLESTEP set by user_enable_single_step. So for that aspect, it is probably relatively straightforward to cover that interaction. The code has to be pretty exact and will merit some comments about subtleties, but I suspect the actual new code required will be just a tiny amount.
Quoted 9 lines
Kprobes has been tested to work simultaneously with hw-breakpoints. KGDB has not been ported yet to use the hw-breakpoint interfaces (KGDB had issues in it, that prevented it from being tested during our development...though its maintainer has begun showing interest recently).
Xmon was (and I believe is still) in a state where data breakpoints did not work. It needs to be ported too, to benefit from the hw-breakpoint interfaces.
That is not really what I meant at all. That is good stuff to work out. But I just meant the interactions with kprobes/kgdb's use of single-stepping, the direct analogy to the user_enable_single_step issue.
Quoted 1 line
I must admit that the issue of pre-emption [...]
I understand the reason for using stepping. (I have advised in the past that I thought this magical implicit step logic was too hairy to roll in under the covers and that a low-level facility expressing the different hardware semantics to a kernel API would be OK. I do agree with the motivation of cross-arch uniformity of the semantics. I don't object to making it magically right--I just expressed general skepticism/fear about getting that right so that I didn't want to try writing that magic. Now I'm just responding about the particular details I've noticed about that can of worms. It's certainly great if you can resolve all that. But I'll note that I am still by no means confident that the details I have raised cover all the worms in that can.)
What remains less than clear is how preemption relates. For any per-thread hw_breakpoint, there is no high-level reason to care one way or the other. The thread, its HW breakpoints, its register state including state of stepping, are all part of per-thread state and no reason to do any less (or more) preemption than normally happens.
Quoted 4 lines
Disabling pre-emption is necessary to ensure that hw-breakpoints are enabled immediately after the causative instruction has finished execution (the control flow may go astray if pre-emption occurs between i1 and i2).
I don't understand what "go astray" means here. The only thing I can think of is the effect on any per-cpu variables you are using in hw_breakpoint implementation.
Thanks,
Roland
On Mon, Dec 14, 2009 at 11:26:26AM -0800, Roland McGrath wrote:
<snipped>
Quoted 12 lines
I understand the reason for using stepping. (I have advised in the past that I thought this magical implicit step logic was too hairy to roll in under the covers and that a low-level facility expressing the different hardware semantics to a kernel API would be OK. I do agree with the motivation of cross-arch uniformity of the semantics. I don't object to making it magically right--I just expressed general skepticism/fear about getting that right so that I didn't want to try writing that magic. Now I'm just responding about the particular details I've noticed about that can of worms. It's certainly great if you can resolve all that. But I'll note that I am still by no means confident that the details I have raised cover all the worms in that can.)
Quoted 6 lines
What remains less than clear is how preemption relates. For any per-thread
hw_breakpoint, there is no high-level reason to care one way or the other. The thread, its HW breakpoints, its register state including state of stepping, are all part of per-thread state and no reason to do any less (or more) preemption than normally happens.
I get that reasoning now...I'd been unduly worried about pre-emption and hence the introduction of pre-emption disabled state.
But of course, in the existing design, the per-cpu variables would be affected because if pre-emption was to occur. I'll see how that can be factored in, while retaining the abstraction provided by the interfaces.
Quoted 9 lines
Quoted 4 lines
Disabling pre-emption is necessary to ensure that hw-breakpoints are enabled immediately after the causative instruction has finished execution (the control flow may go astray if pre-emption occurs between i1 and i2).
I don't understand what "go astray" means here. The only thing I can think of is the effect on any per-cpu variables you are using in
hw_breakpointimplementation.
As stated above, I was worried about a pre-emption happening between a return from breakpoint exception handler and the execution of the causative instruction....but as I learn, it seems fine now. It is just that the kernel code needs to be tweaked keeping this in mind.
Thanks,
K.Prasad
Quoted 1 line · K.Prasad
On Mon, Dec 14, 2009 at 11:26:26AM -0800, Roland McGrath wrote:
<snipped>
Quoted 14 lines
Quoted 6 lines
What remains less than clear is how preemption relates. For any per-thread
hw_breakpoint, there is no high-level reason to care one way or the other. The thread, its HW breakpoints, its register state including state of stepping, are all part of per-thread state and no reason to do any less (or more) preemption than normally happens.I get that reasoning now...I'd been unduly worried about pre-emption and hence the introduction of pre-emption disabled state.
But of course, in the existing design, the per-cpu variables would be affected because if pre-emption was to occur. I'll see how that can be factored in, while retaining the abstraction provided by the interfaces.
<snipped>
Quoted 9 lines
As stated above, I was worried about a pre-emption happening between a return from breakpoint exception handler and the execution of the causative instruction....but as I learn, it seems fine now. It is just that the kernel code needs to be tweaked keeping this in mind.
Thanks,
K.Prasad
Hi Roland,
The cost of allowing pre-emption (such as storing the per-cpu
variables into per-thread structures for user-space breakpoints and offsetting the effect of a new process between the hw-breakpoint handler and single-step handler) appears to far out-weigh the present approach (where pre-emption is disabled between two user-space instructions).
It is also not clear to me if disabling pre-emption for the user-space (albeit for a very tiny time-window) is incorrect and if their side-effects are known. If otherwise, I think we should choose to operate in pre-empt safe mode and avoid all costs associated when done without it.
I'm eager to know what you think.
Thanks,
K.Prasad
Quoted 4 lines
It is also not clear to me if disabling pre-emption for the user-space (albeit for a very tiny time-window) is incorrect and if their side-effects are known. If otherwise, I think we should choose to operate in pre-empt safe mode and avoid all costs associated when done without it.
I never really gave much consideration to returning to user mode with preemption disabled. It would not really have occurred to me that was even possible. I can't say it seems to me like it could ever be a very good idea. I find it hard even to start listing the cans of worms that might be opened by that. Perhaps the powerpc maintainers have a clearer picture of it than I do.
What does it mean when there is something that prevents it from returning to user mode? i.e., TIF_SIGPENDING or TIF_NEED_RESCHED, or whatever. It could do a lot in the kernel before it gets back to user mode. What if in there somewhere it blocks voluntarily?
Similarly, what does it mean if you get to user mode but the single-stepped instruction is a load/store that gets a page fault? What if it blocks in the page fault handler?
For that matter, what about a page fault for the kernel-mode case?
Perhaps I'm imagining gremlins where there aren't any, but I just cannot really get my head around this "disable preemption while running some unknown instruction that normally runs with preemption enabled" thing--let alone "disable preemption while returning to user mode".
Thanks,
Roland
Quoted 23 lines · Roland McGrath
Quoted 4 lines
It is also not clear to me if disabling pre-emption for the user-space (albeit for a very tiny time-window) is incorrect and if their side-effects are known. If otherwise, I think we should choose to operate in pre-empt safe mode and avoid all costs associated when done without it.
I never really gave much consideration to returning to user mode with preemption disabled. It would not really have occurred to me that was even possible. I can't say it seems to me like it could ever be a very good idea. I find it hard even to start listing the cans of worms that might be opened by that. Perhaps the powerpc maintainers have a clearer picture of it than I do.
What does it mean when there is something that prevents it from returning to user mode? i.e.,
TIF_SIGPENDINGorTIF_NEED_RESCHED, or whatever. It could do a lot in the kernel before it gets back to user mode. What if in there somewhere it blocks voluntarily?Similarly, what does it mean if you get to user mode but the single-stepped instruction is a load/store that gets a page fault? What if it blocks in the page fault handler?
For that matter, what about a page fault for the kernel-mode case?
Quoted 8 lines
Perhaps I'm imagining gremlins where there aren't any, but I just cannot really get my head around this "disable preemption while running some unknown instruction that normally runs with preemption enabled" thing--let alone "disable preemption while returning to user mode".
Thanks,
Roland
I posted a patch which re-enables pre-emption after a hw-breakpoint is processed (linuxppc-dev ref: [email protected]). It does lead to clumsiness (due to the new variables to track states, prior breakpoints, etc.) but with the reasons you pointed out, it is much better than having uncertain/incorrect code.
Thanks for your comments.
-- K.Prasad