Re: [PATCH] kernel/kprobes: Add test to validate pt_regs

2017-06-13 Thread Masami Hiramatsu
On Wed, 14 Jun 2017 11:40:08 +0900
Masami Hiramatsu  wrote:

> On Fri,  9 Jun 2017 00:53:08 +0530
> "Naveen N. Rao"  wrote:
> 
> > Add a test to verify that the registers passed in pt_regs on kprobe
> > (trap), optprobe (jump) and kprobe_on_ftrace (ftrace_caller) are
> > accurate. The tests are exercized if KPROBES_SANITY_TEST is enabled.
> 
> Great!
> 
> > 
> > Implemented for powerpc64. Other architectures will have to implement
> > the relevant arch_* helpers and define HAVE_KPROBES_REGS_SANITY_TEST.
> 
> Hmm, why don't you define that in arch/powerpc/Kconfig ?
> Also, could you split this into 3 patches for each case ?
> 
> > 
> > Signed-off-by: Naveen N. Rao 
> > ---
> >  arch/powerpc/include/asm/kprobes.h  |   4 +
> >  arch/powerpc/lib/Makefile   |   3 +-
> >  arch/powerpc/lib/test_kprobe_regs.S |  62 
> >  arch/powerpc/lib/test_kprobes.c | 115 ++
> >  include/linux/kprobes.h |  11 +++
> >  kernel/test_kprobes.c   | 183 
> > 
> >  6 files changed, 377 insertions(+), 1 deletion(-)
> >  create mode 100644 arch/powerpc/lib/test_kprobe_regs.S
> >  create mode 100644 arch/powerpc/lib/test_kprobes.c
> > 
> > diff --git a/arch/powerpc/include/asm/kprobes.h 
> > b/arch/powerpc/include/asm/kprobes.h
> > index 566da372e02b..10c91d3132a1 100644
> > --- a/arch/powerpc/include/asm/kprobes.h
> > +++ b/arch/powerpc/include/asm/kprobes.h
> > @@ -124,6 +124,10 @@ static inline int skip_singlestep(struct kprobe *p, 
> > struct pt_regs *regs,
> > return 0;
> >  }
> >  #endif
> > +#if defined(CONFIG_KPROBES_SANITY_TEST) && defined(CONFIG_PPC64)
> > +#define HAVE_KPROBES_REGS_SANITY_TEST
> > +void arch_kprobe_regs_set_ptregs(struct pt_regs *regs);
> > +#endif
> >  #else
> >  static inline int kprobe_handler(struct pt_regs *regs) { return 0; }
> >  static inline int kprobe_post_handler(struct pt_regs *regs) { return 0; }
> > diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
> > index 3c3146ba62da..8a0bb8e20179 100644
> > --- a/arch/powerpc/lib/Makefile
> > +++ b/arch/powerpc/lib/Makefile
> > @@ -27,7 +27,8 @@ obj64-y   += copypage_64.o copyuser_64.o mem_64.o 
> > hweight_64.o \
> >  
> >  obj64-$(CONFIG_SMP)+= locks.o
> >  obj64-$(CONFIG_ALTIVEC)+= vmx-helper.o
> > -obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o
> > +obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o 
> > test_kprobe_regs.o \
> > +  test_kprobes.o
> >  
> >  obj-y  += checksum_$(BITS).o checksum_wrappers.o
> >  
> > diff --git a/arch/powerpc/lib/test_kprobe_regs.S 
> > b/arch/powerpc/lib/test_kprobe_regs.S
> > new file mode 100644
> > index ..4e95eca6dcd3
> > --- /dev/null
> > +++ b/arch/powerpc/lib/test_kprobe_regs.S
> > @@ -0,0 +1,62 @@
> > +/*
> > + * test_kprobe_regs: architectural helpers for validating pt_regs
> > + *  received on a kprobe.
> > + *
> > + * Copyright 2017 Naveen N. Rao 
> > + *   IBM Corporation
> > + *
> > + * This program is free software; you can redistribute it and/or
> > + * modify it under the terms of the GNU General Public License
> > + * as published by the Free Software Foundation; version 2
> > + * of the License.
> > + */
> > +
> > +#include 
> > +#include 
> > +#include 
> > +
> > +_GLOBAL(arch_kprobe_regs_function)
> > +   mflrr0
> > +   std r0, LRSAVE(r1)
> > +   stdur1, -SWITCH_FRAME_SIZE(r1)
> > +
> > +   /* Tell pre handler about our pt_regs location */
> > +   addir3, r1, STACK_FRAME_OVERHEAD
> > +   bl  arch_kprobe_regs_set_ptregs
> > +
> > +   /* Load back our true LR */
> > +   ld  r0, (SWITCH_FRAME_SIZE + LRSAVE)(r1)
> > +   mtlrr0
> > +
> > +   /* Save all SPRs that we care about */
> > +   mfctr   r0
> > +   std r0, _CTR(r1)
> > +   mflrr0
> > +   std r0, _LINK(r1)
> > +   mfspr   r0, SPRN_XER
> > +   std r0, _XER(r1)
> > +   mfcrr0
> > +   std r0, _CCR(r1)
> > +
> > +   /* Now, save all GPRs */
> > +   SAVE_2GPRS(0, r1)
> > +   SAVE_10GPRS(2, r1)
> > +   SAVE_10GPRS(12, r1)
> > +   SAVE_10GPRS(22, r1)
> > +
> > +   /* We're now ready to be probed */
> > +.global arch_kprobe_regs_probepoint
> > +arch_kprobe_regs_probepoint:
> > +   nop
> > +
> > +#ifdef CONFIG_KPROBES_ON_FTRACE
> > +   /* Let's also test KPROBES_ON_FTRACE */
> > +   bl  kprobe_regs_kp_on_ftrace_target
> > +   nop
> > +#endif
> > +
> > +   /* All done */
> > +   addir1, r1, SWITCH_FRAME_SIZE
> > +   ld  r0, LRSAVE(r1)
> > +   mtlrr0
> > +   blr
> > diff --git a/arch/powerpc/lib/test_kprobes.c 
> > b/arch/powerpc/lib/test_kprobes.c
> > new file mode 100644
> > index ..23f7a7ffcdd6
> > --- /dev/null
> > +++ b/arch/powerpc/lib/test_kprobes.c
> > @@ -0,0 +1,115 @@
> > +/*
> > + * test_kprobes: architectural 

Re: [PATCH] kernel/kprobes: Add test to validate pt_regs

2017-06-13 Thread Masami Hiramatsu
On Wed, 14 Jun 2017 11:40:08 +0900
Masami Hiramatsu  wrote:

> On Fri,  9 Jun 2017 00:53:08 +0530
> "Naveen N. Rao"  wrote:
> 
> > Add a test to verify that the registers passed in pt_regs on kprobe
> > (trap), optprobe (jump) and kprobe_on_ftrace (ftrace_caller) are
> > accurate. The tests are exercized if KPROBES_SANITY_TEST is enabled.
> 
> Great!
> 
> > 
> > Implemented for powerpc64. Other architectures will have to implement
> > the relevant arch_* helpers and define HAVE_KPROBES_REGS_SANITY_TEST.
> 
> Hmm, why don't you define that in arch/powerpc/Kconfig ?
> Also, could you split this into 3 patches for each case ?
> 
> > 
> > Signed-off-by: Naveen N. Rao 
> > ---
> >  arch/powerpc/include/asm/kprobes.h  |   4 +
> >  arch/powerpc/lib/Makefile   |   3 +-
> >  arch/powerpc/lib/test_kprobe_regs.S |  62 
> >  arch/powerpc/lib/test_kprobes.c | 115 ++
> >  include/linux/kprobes.h |  11 +++
> >  kernel/test_kprobes.c   | 183 
> > 
> >  6 files changed, 377 insertions(+), 1 deletion(-)
> >  create mode 100644 arch/powerpc/lib/test_kprobe_regs.S
> >  create mode 100644 arch/powerpc/lib/test_kprobes.c
> > 
> > diff --git a/arch/powerpc/include/asm/kprobes.h 
> > b/arch/powerpc/include/asm/kprobes.h
> > index 566da372e02b..10c91d3132a1 100644
> > --- a/arch/powerpc/include/asm/kprobes.h
> > +++ b/arch/powerpc/include/asm/kprobes.h
> > @@ -124,6 +124,10 @@ static inline int skip_singlestep(struct kprobe *p, 
> > struct pt_regs *regs,
> > return 0;
> >  }
> >  #endif
> > +#if defined(CONFIG_KPROBES_SANITY_TEST) && defined(CONFIG_PPC64)
> > +#define HAVE_KPROBES_REGS_SANITY_TEST
> > +void arch_kprobe_regs_set_ptregs(struct pt_regs *regs);
> > +#endif
> >  #else
> >  static inline int kprobe_handler(struct pt_regs *regs) { return 0; }
> >  static inline int kprobe_post_handler(struct pt_regs *regs) { return 0; }
> > diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
> > index 3c3146ba62da..8a0bb8e20179 100644
> > --- a/arch/powerpc/lib/Makefile
> > +++ b/arch/powerpc/lib/Makefile
> > @@ -27,7 +27,8 @@ obj64-y   += copypage_64.o copyuser_64.o mem_64.o 
> > hweight_64.o \
> >  
> >  obj64-$(CONFIG_SMP)+= locks.o
> >  obj64-$(CONFIG_ALTIVEC)+= vmx-helper.o
> > -obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o
> > +obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o 
> > test_kprobe_regs.o \
> > +  test_kprobes.o
> >  
> >  obj-y  += checksum_$(BITS).o checksum_wrappers.o
> >  
> > diff --git a/arch/powerpc/lib/test_kprobe_regs.S 
> > b/arch/powerpc/lib/test_kprobe_regs.S
> > new file mode 100644
> > index ..4e95eca6dcd3
> > --- /dev/null
> > +++ b/arch/powerpc/lib/test_kprobe_regs.S
> > @@ -0,0 +1,62 @@
> > +/*
> > + * test_kprobe_regs: architectural helpers for validating pt_regs
> > + *  received on a kprobe.
> > + *
> > + * Copyright 2017 Naveen N. Rao 
> > + *   IBM Corporation
> > + *
> > + * This program is free software; you can redistribute it and/or
> > + * modify it under the terms of the GNU General Public License
> > + * as published by the Free Software Foundation; version 2
> > + * of the License.
> > + */
> > +
> > +#include 
> > +#include 
> > +#include 
> > +
> > +_GLOBAL(arch_kprobe_regs_function)
> > +   mflrr0
> > +   std r0, LRSAVE(r1)
> > +   stdur1, -SWITCH_FRAME_SIZE(r1)
> > +
> > +   /* Tell pre handler about our pt_regs location */
> > +   addir3, r1, STACK_FRAME_OVERHEAD
> > +   bl  arch_kprobe_regs_set_ptregs
> > +
> > +   /* Load back our true LR */
> > +   ld  r0, (SWITCH_FRAME_SIZE + LRSAVE)(r1)
> > +   mtlrr0
> > +
> > +   /* Save all SPRs that we care about */
> > +   mfctr   r0
> > +   std r0, _CTR(r1)
> > +   mflrr0
> > +   std r0, _LINK(r1)
> > +   mfspr   r0, SPRN_XER
> > +   std r0, _XER(r1)
> > +   mfcrr0
> > +   std r0, _CCR(r1)
> > +
> > +   /* Now, save all GPRs */
> > +   SAVE_2GPRS(0, r1)
> > +   SAVE_10GPRS(2, r1)
> > +   SAVE_10GPRS(12, r1)
> > +   SAVE_10GPRS(22, r1)
> > +
> > +   /* We're now ready to be probed */
> > +.global arch_kprobe_regs_probepoint
> > +arch_kprobe_regs_probepoint:
> > +   nop
> > +
> > +#ifdef CONFIG_KPROBES_ON_FTRACE
> > +   /* Let's also test KPROBES_ON_FTRACE */
> > +   bl  kprobe_regs_kp_on_ftrace_target
> > +   nop
> > +#endif
> > +
> > +   /* All done */
> > +   addir1, r1, SWITCH_FRAME_SIZE
> > +   ld  r0, LRSAVE(r1)
> > +   mtlrr0
> > +   blr
> > diff --git a/arch/powerpc/lib/test_kprobes.c 
> > b/arch/powerpc/lib/test_kprobes.c
> > new file mode 100644
> > index ..23f7a7ffcdd6
> > --- /dev/null
> > +++ b/arch/powerpc/lib/test_kprobes.c
> > @@ -0,0 +1,115 @@
> > +/*
> > + * test_kprobes: architectural helpers for validating pt_regs
> > + *  received on a kprobe.
> > + *
> > + * Copyright 2017 Naveen N. Rao 
> > + *

Re: [PATCH] kernel/kprobes: Add test to validate pt_regs

2017-06-13 Thread Masami Hiramatsu
On Fri,  9 Jun 2017 00:53:08 +0530
"Naveen N. Rao"  wrote:

> Add a test to verify that the registers passed in pt_regs on kprobe
> (trap), optprobe (jump) and kprobe_on_ftrace (ftrace_caller) are
> accurate. The tests are exercized if KPROBES_SANITY_TEST is enabled.

Great!

> 
> Implemented for powerpc64. Other architectures will have to implement
> the relevant arch_* helpers and define HAVE_KPROBES_REGS_SANITY_TEST.

Hmm, why don't you define that in arch/powerpc/Kconfig ?
Also, could you split this into 3 patches for each case ?

> 
> Signed-off-by: Naveen N. Rao 
> ---
>  arch/powerpc/include/asm/kprobes.h  |   4 +
>  arch/powerpc/lib/Makefile   |   3 +-
>  arch/powerpc/lib/test_kprobe_regs.S |  62 
>  arch/powerpc/lib/test_kprobes.c | 115 ++
>  include/linux/kprobes.h |  11 +++
>  kernel/test_kprobes.c   | 183 
> 
>  6 files changed, 377 insertions(+), 1 deletion(-)
>  create mode 100644 arch/powerpc/lib/test_kprobe_regs.S
>  create mode 100644 arch/powerpc/lib/test_kprobes.c
> 
> diff --git a/arch/powerpc/include/asm/kprobes.h 
> b/arch/powerpc/include/asm/kprobes.h
> index 566da372e02b..10c91d3132a1 100644
> --- a/arch/powerpc/include/asm/kprobes.h
> +++ b/arch/powerpc/include/asm/kprobes.h
> @@ -124,6 +124,10 @@ static inline int skip_singlestep(struct kprobe *p, 
> struct pt_regs *regs,
>   return 0;
>  }
>  #endif
> +#if defined(CONFIG_KPROBES_SANITY_TEST) && defined(CONFIG_PPC64)
> +#define HAVE_KPROBES_REGS_SANITY_TEST
> +void arch_kprobe_regs_set_ptregs(struct pt_regs *regs);
> +#endif
>  #else
>  static inline int kprobe_handler(struct pt_regs *regs) { return 0; }
>  static inline int kprobe_post_handler(struct pt_regs *regs) { return 0; }
> diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
> index 3c3146ba62da..8a0bb8e20179 100644
> --- a/arch/powerpc/lib/Makefile
> +++ b/arch/powerpc/lib/Makefile
> @@ -27,7 +27,8 @@ obj64-y += copypage_64.o copyuser_64.o mem_64.o 
> hweight_64.o \
>  
>  obj64-$(CONFIG_SMP)  += locks.o
>  obj64-$(CONFIG_ALTIVEC)  += vmx-helper.o
> -obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o
> +obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o 
> test_kprobe_regs.o \
> +test_kprobes.o
>  
>  obj-y+= checksum_$(BITS).o checksum_wrappers.o
>  
> diff --git a/arch/powerpc/lib/test_kprobe_regs.S 
> b/arch/powerpc/lib/test_kprobe_regs.S
> new file mode 100644
> index ..4e95eca6dcd3
> --- /dev/null
> +++ b/arch/powerpc/lib/test_kprobe_regs.S
> @@ -0,0 +1,62 @@
> +/*
> + * test_kprobe_regs: architectural helpers for validating pt_regs
> + *received on a kprobe.
> + *
> + * Copyright 2017 Naveen N. Rao 
> + * IBM Corporation
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * as published by the Free Software Foundation; version 2
> + * of the License.
> + */
> +
> +#include 
> +#include 
> +#include 
> +
> +_GLOBAL(arch_kprobe_regs_function)
> + mflrr0
> + std r0, LRSAVE(r1)
> + stdur1, -SWITCH_FRAME_SIZE(r1)
> +
> + /* Tell pre handler about our pt_regs location */
> + addir3, r1, STACK_FRAME_OVERHEAD
> + bl  arch_kprobe_regs_set_ptregs
> +
> + /* Load back our true LR */
> + ld  r0, (SWITCH_FRAME_SIZE + LRSAVE)(r1)
> + mtlrr0
> +
> + /* Save all SPRs that we care about */
> + mfctr   r0
> + std r0, _CTR(r1)
> + mflrr0
> + std r0, _LINK(r1)
> + mfspr   r0, SPRN_XER
> + std r0, _XER(r1)
> + mfcrr0
> + std r0, _CCR(r1)
> +
> + /* Now, save all GPRs */
> + SAVE_2GPRS(0, r1)
> + SAVE_10GPRS(2, r1)
> + SAVE_10GPRS(12, r1)
> + SAVE_10GPRS(22, r1)
> +
> + /* We're now ready to be probed */
> +.global arch_kprobe_regs_probepoint
> +arch_kprobe_regs_probepoint:
> + nop
> +
> +#ifdef CONFIG_KPROBES_ON_FTRACE
> + /* Let's also test KPROBES_ON_FTRACE */
> + bl  kprobe_regs_kp_on_ftrace_target
> + nop
> +#endif
> +
> + /* All done */
> + addir1, r1, SWITCH_FRAME_SIZE
> + ld  r0, LRSAVE(r1)
> + mtlrr0
> + blr
> diff --git a/arch/powerpc/lib/test_kprobes.c b/arch/powerpc/lib/test_kprobes.c
> new file mode 100644
> index ..23f7a7ffcdd6
> --- /dev/null
> +++ b/arch/powerpc/lib/test_kprobes.c
> @@ -0,0 +1,115 @@
> +/*
> + * test_kprobes: architectural helpers for validating pt_regs
> + *received on a kprobe.
> + *
> + * Copyright 2017 Naveen N. Rao 
> + * IBM Corporation
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU 

Re: [PATCH] kernel/kprobes: Add test to validate pt_regs

2017-06-13 Thread Masami Hiramatsu
On Fri,  9 Jun 2017 00:53:08 +0530
"Naveen N. Rao"  wrote:

> Add a test to verify that the registers passed in pt_regs on kprobe
> (trap), optprobe (jump) and kprobe_on_ftrace (ftrace_caller) are
> accurate. The tests are exercized if KPROBES_SANITY_TEST is enabled.

Great!

> 
> Implemented for powerpc64. Other architectures will have to implement
> the relevant arch_* helpers and define HAVE_KPROBES_REGS_SANITY_TEST.

Hmm, why don't you define that in arch/powerpc/Kconfig ?
Also, could you split this into 3 patches for each case ?

> 
> Signed-off-by: Naveen N. Rao 
> ---
>  arch/powerpc/include/asm/kprobes.h  |   4 +
>  arch/powerpc/lib/Makefile   |   3 +-
>  arch/powerpc/lib/test_kprobe_regs.S |  62 
>  arch/powerpc/lib/test_kprobes.c | 115 ++
>  include/linux/kprobes.h |  11 +++
>  kernel/test_kprobes.c   | 183 
> 
>  6 files changed, 377 insertions(+), 1 deletion(-)
>  create mode 100644 arch/powerpc/lib/test_kprobe_regs.S
>  create mode 100644 arch/powerpc/lib/test_kprobes.c
> 
> diff --git a/arch/powerpc/include/asm/kprobes.h 
> b/arch/powerpc/include/asm/kprobes.h
> index 566da372e02b..10c91d3132a1 100644
> --- a/arch/powerpc/include/asm/kprobes.h
> +++ b/arch/powerpc/include/asm/kprobes.h
> @@ -124,6 +124,10 @@ static inline int skip_singlestep(struct kprobe *p, 
> struct pt_regs *regs,
>   return 0;
>  }
>  #endif
> +#if defined(CONFIG_KPROBES_SANITY_TEST) && defined(CONFIG_PPC64)
> +#define HAVE_KPROBES_REGS_SANITY_TEST
> +void arch_kprobe_regs_set_ptregs(struct pt_regs *regs);
> +#endif
>  #else
>  static inline int kprobe_handler(struct pt_regs *regs) { return 0; }
>  static inline int kprobe_post_handler(struct pt_regs *regs) { return 0; }
> diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
> index 3c3146ba62da..8a0bb8e20179 100644
> --- a/arch/powerpc/lib/Makefile
> +++ b/arch/powerpc/lib/Makefile
> @@ -27,7 +27,8 @@ obj64-y += copypage_64.o copyuser_64.o mem_64.o 
> hweight_64.o \
>  
>  obj64-$(CONFIG_SMP)  += locks.o
>  obj64-$(CONFIG_ALTIVEC)  += vmx-helper.o
> -obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o
> +obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o 
> test_kprobe_regs.o \
> +test_kprobes.o
>  
>  obj-y+= checksum_$(BITS).o checksum_wrappers.o
>  
> diff --git a/arch/powerpc/lib/test_kprobe_regs.S 
> b/arch/powerpc/lib/test_kprobe_regs.S
> new file mode 100644
> index ..4e95eca6dcd3
> --- /dev/null
> +++ b/arch/powerpc/lib/test_kprobe_regs.S
> @@ -0,0 +1,62 @@
> +/*
> + * test_kprobe_regs: architectural helpers for validating pt_regs
> + *received on a kprobe.
> + *
> + * Copyright 2017 Naveen N. Rao 
> + * IBM Corporation
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * as published by the Free Software Foundation; version 2
> + * of the License.
> + */
> +
> +#include 
> +#include 
> +#include 
> +
> +_GLOBAL(arch_kprobe_regs_function)
> + mflrr0
> + std r0, LRSAVE(r1)
> + stdur1, -SWITCH_FRAME_SIZE(r1)
> +
> + /* Tell pre handler about our pt_regs location */
> + addir3, r1, STACK_FRAME_OVERHEAD
> + bl  arch_kprobe_regs_set_ptregs
> +
> + /* Load back our true LR */
> + ld  r0, (SWITCH_FRAME_SIZE + LRSAVE)(r1)
> + mtlrr0
> +
> + /* Save all SPRs that we care about */
> + mfctr   r0
> + std r0, _CTR(r1)
> + mflrr0
> + std r0, _LINK(r1)
> + mfspr   r0, SPRN_XER
> + std r0, _XER(r1)
> + mfcrr0
> + std r0, _CCR(r1)
> +
> + /* Now, save all GPRs */
> + SAVE_2GPRS(0, r1)
> + SAVE_10GPRS(2, r1)
> + SAVE_10GPRS(12, r1)
> + SAVE_10GPRS(22, r1)
> +
> + /* We're now ready to be probed */
> +.global arch_kprobe_regs_probepoint
> +arch_kprobe_regs_probepoint:
> + nop
> +
> +#ifdef CONFIG_KPROBES_ON_FTRACE
> + /* Let's also test KPROBES_ON_FTRACE */
> + bl  kprobe_regs_kp_on_ftrace_target
> + nop
> +#endif
> +
> + /* All done */
> + addir1, r1, SWITCH_FRAME_SIZE
> + ld  r0, LRSAVE(r1)
> + mtlrr0
> + blr
> diff --git a/arch/powerpc/lib/test_kprobes.c b/arch/powerpc/lib/test_kprobes.c
> new file mode 100644
> index ..23f7a7ffcdd6
> --- /dev/null
> +++ b/arch/powerpc/lib/test_kprobes.c
> @@ -0,0 +1,115 @@
> +/*
> + * test_kprobes: architectural helpers for validating pt_regs
> + *received on a kprobe.
> + *
> + * Copyright 2017 Naveen N. Rao 
> + * IBM Corporation
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * as published by the Free Software Foundation; version 2
> + * of the License.
> + */
> +
> +#define 

[PATCH] kernel/kprobes: Add test to validate pt_regs

2017-06-08 Thread Naveen N. Rao
Add a test to verify that the registers passed in pt_regs on kprobe
(trap), optprobe (jump) and kprobe_on_ftrace (ftrace_caller) are
accurate. The tests are exercized if KPROBES_SANITY_TEST is enabled.

Implemented for powerpc64. Other architectures will have to implement
the relevant arch_* helpers and define HAVE_KPROBES_REGS_SANITY_TEST.

Signed-off-by: Naveen N. Rao 
---
 arch/powerpc/include/asm/kprobes.h  |   4 +
 arch/powerpc/lib/Makefile   |   3 +-
 arch/powerpc/lib/test_kprobe_regs.S |  62 
 arch/powerpc/lib/test_kprobes.c | 115 ++
 include/linux/kprobes.h |  11 +++
 kernel/test_kprobes.c   | 183 
 6 files changed, 377 insertions(+), 1 deletion(-)
 create mode 100644 arch/powerpc/lib/test_kprobe_regs.S
 create mode 100644 arch/powerpc/lib/test_kprobes.c

diff --git a/arch/powerpc/include/asm/kprobes.h 
b/arch/powerpc/include/asm/kprobes.h
index 566da372e02b..10c91d3132a1 100644
--- a/arch/powerpc/include/asm/kprobes.h
+++ b/arch/powerpc/include/asm/kprobes.h
@@ -124,6 +124,10 @@ static inline int skip_singlestep(struct kprobe *p, struct 
pt_regs *regs,
return 0;
 }
 #endif
+#if defined(CONFIG_KPROBES_SANITY_TEST) && defined(CONFIG_PPC64)
+#define HAVE_KPROBES_REGS_SANITY_TEST
+void arch_kprobe_regs_set_ptregs(struct pt_regs *regs);
+#endif
 #else
 static inline int kprobe_handler(struct pt_regs *regs) { return 0; }
 static inline int kprobe_post_handler(struct pt_regs *regs) { return 0; }
diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
index 3c3146ba62da..8a0bb8e20179 100644
--- a/arch/powerpc/lib/Makefile
+++ b/arch/powerpc/lib/Makefile
@@ -27,7 +27,8 @@ obj64-y   += copypage_64.o copyuser_64.o mem_64.o 
hweight_64.o \
 
 obj64-$(CONFIG_SMP)+= locks.o
 obj64-$(CONFIG_ALTIVEC)+= vmx-helper.o
-obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o
+obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o test_kprobe_regs.o \
+  test_kprobes.o
 
 obj-y  += checksum_$(BITS).o checksum_wrappers.o
 
diff --git a/arch/powerpc/lib/test_kprobe_regs.S 
b/arch/powerpc/lib/test_kprobe_regs.S
new file mode 100644
index ..4e95eca6dcd3
--- /dev/null
+++ b/arch/powerpc/lib/test_kprobe_regs.S
@@ -0,0 +1,62 @@
+/*
+ * test_kprobe_regs: architectural helpers for validating pt_regs
+ *  received on a kprobe.
+ *
+ * Copyright 2017 Naveen N. Rao 
+ *   IBM Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ */
+
+#include 
+#include 
+#include 
+
+_GLOBAL(arch_kprobe_regs_function)
+   mflrr0
+   std r0, LRSAVE(r1)
+   stdur1, -SWITCH_FRAME_SIZE(r1)
+
+   /* Tell pre handler about our pt_regs location */
+   addir3, r1, STACK_FRAME_OVERHEAD
+   bl  arch_kprobe_regs_set_ptregs
+
+   /* Load back our true LR */
+   ld  r0, (SWITCH_FRAME_SIZE + LRSAVE)(r1)
+   mtlrr0
+
+   /* Save all SPRs that we care about */
+   mfctr   r0
+   std r0, _CTR(r1)
+   mflrr0
+   std r0, _LINK(r1)
+   mfspr   r0, SPRN_XER
+   std r0, _XER(r1)
+   mfcrr0
+   std r0, _CCR(r1)
+
+   /* Now, save all GPRs */
+   SAVE_2GPRS(0, r1)
+   SAVE_10GPRS(2, r1)
+   SAVE_10GPRS(12, r1)
+   SAVE_10GPRS(22, r1)
+
+   /* We're now ready to be probed */
+.global arch_kprobe_regs_probepoint
+arch_kprobe_regs_probepoint:
+   nop
+
+#ifdef CONFIG_KPROBES_ON_FTRACE
+   /* Let's also test KPROBES_ON_FTRACE */
+   bl  kprobe_regs_kp_on_ftrace_target
+   nop
+#endif
+
+   /* All done */
+   addir1, r1, SWITCH_FRAME_SIZE
+   ld  r0, LRSAVE(r1)
+   mtlrr0
+   blr
diff --git a/arch/powerpc/lib/test_kprobes.c b/arch/powerpc/lib/test_kprobes.c
new file mode 100644
index ..23f7a7ffcdd6
--- /dev/null
+++ b/arch/powerpc/lib/test_kprobes.c
@@ -0,0 +1,115 @@
+/*
+ * test_kprobes: architectural helpers for validating pt_regs
+ *  received on a kprobe.
+ *
+ * Copyright 2017 Naveen N. Rao 
+ *   IBM Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ */
+
+#define pr_fmt(fmt) "Kprobe smoke test (regs): " fmt
+
+#include 
+#include 
+#include 
+
+static struct pt_regs *r;
+
+void arch_kprobe_regs_set_ptregs(struct pt_regs *regs)
+{
+   r = regs;
+}
+
+static int validate_regs(struct kprobe *p, struct pt_regs *regs,
+   

[PATCH] kernel/kprobes: Add test to validate pt_regs

2017-06-08 Thread Naveen N. Rao
Add a test to verify that the registers passed in pt_regs on kprobe
(trap), optprobe (jump) and kprobe_on_ftrace (ftrace_caller) are
accurate. The tests are exercized if KPROBES_SANITY_TEST is enabled.

Implemented for powerpc64. Other architectures will have to implement
the relevant arch_* helpers and define HAVE_KPROBES_REGS_SANITY_TEST.

Signed-off-by: Naveen N. Rao 
---
 arch/powerpc/include/asm/kprobes.h  |   4 +
 arch/powerpc/lib/Makefile   |   3 +-
 arch/powerpc/lib/test_kprobe_regs.S |  62 
 arch/powerpc/lib/test_kprobes.c | 115 ++
 include/linux/kprobes.h |  11 +++
 kernel/test_kprobes.c   | 183 
 6 files changed, 377 insertions(+), 1 deletion(-)
 create mode 100644 arch/powerpc/lib/test_kprobe_regs.S
 create mode 100644 arch/powerpc/lib/test_kprobes.c

diff --git a/arch/powerpc/include/asm/kprobes.h 
b/arch/powerpc/include/asm/kprobes.h
index 566da372e02b..10c91d3132a1 100644
--- a/arch/powerpc/include/asm/kprobes.h
+++ b/arch/powerpc/include/asm/kprobes.h
@@ -124,6 +124,10 @@ static inline int skip_singlestep(struct kprobe *p, struct 
pt_regs *regs,
return 0;
 }
 #endif
+#if defined(CONFIG_KPROBES_SANITY_TEST) && defined(CONFIG_PPC64)
+#define HAVE_KPROBES_REGS_SANITY_TEST
+void arch_kprobe_regs_set_ptregs(struct pt_regs *regs);
+#endif
 #else
 static inline int kprobe_handler(struct pt_regs *regs) { return 0; }
 static inline int kprobe_post_handler(struct pt_regs *regs) { return 0; }
diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
index 3c3146ba62da..8a0bb8e20179 100644
--- a/arch/powerpc/lib/Makefile
+++ b/arch/powerpc/lib/Makefile
@@ -27,7 +27,8 @@ obj64-y   += copypage_64.o copyuser_64.o mem_64.o 
hweight_64.o \
 
 obj64-$(CONFIG_SMP)+= locks.o
 obj64-$(CONFIG_ALTIVEC)+= vmx-helper.o
-obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o
+obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o test_kprobe_regs.o \
+  test_kprobes.o
 
 obj-y  += checksum_$(BITS).o checksum_wrappers.o
 
diff --git a/arch/powerpc/lib/test_kprobe_regs.S 
b/arch/powerpc/lib/test_kprobe_regs.S
new file mode 100644
index ..4e95eca6dcd3
--- /dev/null
+++ b/arch/powerpc/lib/test_kprobe_regs.S
@@ -0,0 +1,62 @@
+/*
+ * test_kprobe_regs: architectural helpers for validating pt_regs
+ *  received on a kprobe.
+ *
+ * Copyright 2017 Naveen N. Rao 
+ *   IBM Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ */
+
+#include 
+#include 
+#include 
+
+_GLOBAL(arch_kprobe_regs_function)
+   mflrr0
+   std r0, LRSAVE(r1)
+   stdur1, -SWITCH_FRAME_SIZE(r1)
+
+   /* Tell pre handler about our pt_regs location */
+   addir3, r1, STACK_FRAME_OVERHEAD
+   bl  arch_kprobe_regs_set_ptregs
+
+   /* Load back our true LR */
+   ld  r0, (SWITCH_FRAME_SIZE + LRSAVE)(r1)
+   mtlrr0
+
+   /* Save all SPRs that we care about */
+   mfctr   r0
+   std r0, _CTR(r1)
+   mflrr0
+   std r0, _LINK(r1)
+   mfspr   r0, SPRN_XER
+   std r0, _XER(r1)
+   mfcrr0
+   std r0, _CCR(r1)
+
+   /* Now, save all GPRs */
+   SAVE_2GPRS(0, r1)
+   SAVE_10GPRS(2, r1)
+   SAVE_10GPRS(12, r1)
+   SAVE_10GPRS(22, r1)
+
+   /* We're now ready to be probed */
+.global arch_kprobe_regs_probepoint
+arch_kprobe_regs_probepoint:
+   nop
+
+#ifdef CONFIG_KPROBES_ON_FTRACE
+   /* Let's also test KPROBES_ON_FTRACE */
+   bl  kprobe_regs_kp_on_ftrace_target
+   nop
+#endif
+
+   /* All done */
+   addir1, r1, SWITCH_FRAME_SIZE
+   ld  r0, LRSAVE(r1)
+   mtlrr0
+   blr
diff --git a/arch/powerpc/lib/test_kprobes.c b/arch/powerpc/lib/test_kprobes.c
new file mode 100644
index ..23f7a7ffcdd6
--- /dev/null
+++ b/arch/powerpc/lib/test_kprobes.c
@@ -0,0 +1,115 @@
+/*
+ * test_kprobes: architectural helpers for validating pt_regs
+ *  received on a kprobe.
+ *
+ * Copyright 2017 Naveen N. Rao 
+ *   IBM Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ */
+
+#define pr_fmt(fmt) "Kprobe smoke test (regs): " fmt
+
+#include 
+#include 
+#include 
+
+static struct pt_regs *r;
+
+void arch_kprobe_regs_set_ptregs(struct pt_regs *regs)
+{
+   r = regs;
+}
+
+static int validate_regs(struct kprobe *p, struct pt_regs *regs,
+   int kp_on_ftrace, int post_handler)
+{
+   int i, ret = 1;
+
+   if (!r) {
+