Re: [PATCH] powerpc: Add ppc_inst_as_u64()

2020-06-08 Thread Michael Ellerman
On Mon, 25 May 2020 15:50:04 +1000, Michael Ellerman wrote:
> The code patching code wants to get the value of a struct ppc_inst as
> a u64 when the instruction is prefixed, so we can pass the u64 down to
> __put_user_asm() and write it with a single store.
> 
> This is a bit awkward because the value differs based on the CPU
> endianness, so add a helper to do the conversion.

Applied to powerpc/next.

[1/1] powerpc: Add ppc_inst_as_u64()
  https://git.kernel.org/powerpc/c/16ef9767e4dc5cf03a71ae7bc2bc588dbbe7983e

cheers


Re: [PATCH] powerpc: Add ppc_inst_as_u64()

2020-05-25 Thread Jordan Niethe
On Mon, May 25, 2020 at 3:49 PM Michael Ellerman  wrote:
>
> The code patching code wants to get the value of a struct ppc_inst as
Might need to change the wording here as it also gets used in
arch_prepare_optimized_kprobe()
> a u64 when the instruction is prefixed, so we can pass the u64 down to
> __put_user_asm() and write it with a single store.
>
> This is a bit awkward because the value differs based on the CPU
> endianness, so add a helper to do the conversion.
>
> Signed-off-by: Michael Ellerman 
> ---
>  arch/powerpc/include/asm/inst.h  | 9 +
>  arch/powerpc/kernel/optprobes.c  | 3 +--
>  arch/powerpc/lib/code-patching.c | 8 +---
>  3 files changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/arch/powerpc/include/asm/inst.h b/arch/powerpc/include/asm/inst.h
> index d82e0c99cfa1..d61e07fb2937 100644
> --- a/arch/powerpc/include/asm/inst.h
> +++ b/arch/powerpc/include/asm/inst.h
> @@ -100,6 +100,15 @@ static inline int ppc_inst_len(struct ppc_inst x)
> return ppc_inst_prefixed(x) ? 8 : 4;
>  }
>
> +static inline u64 ppc_inst_as_u64(struct ppc_inst x)
> +{
> +#ifdef CONFIG_CPU_LITTLE_ENDIAN
> +   return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x);
> +#else
> +   return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x);
> +#endif
> +}
> +
>  int probe_user_read_inst(struct ppc_inst *inst,
>  struct ppc_inst __user *nip);
>
> diff --git a/arch/powerpc/kernel/optprobes.c b/arch/powerpc/kernel/optprobes.c
> index 3ac105e7faae..69bfe96884e2 100644
> --- a/arch/powerpc/kernel/optprobes.c
> +++ b/arch/powerpc/kernel/optprobes.c
> @@ -283,8 +283,7 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe 
> *op, struct kprobe *p)
>  * 3. load instruction to be emulated into relevant register, and
>  */
> temp = ppc_inst_read((struct ppc_inst *)p->ainsn.insn);
> -   patch_imm64_load_insns(ppc_inst_val(temp) | 
> ((u64)ppc_inst_suffix(temp) << 32),
> -  4, buff + TMPL_INSN_IDX);
> +   patch_imm64_load_insns(ppc_inst_as_u64(temp), 4, buff + 
> TMPL_INSN_IDX);
>
> /*
>  * 4. branch back from trampoline
> diff --git a/arch/powerpc/lib/code-patching.c 
> b/arch/powerpc/lib/code-patching.c
> index 64cf621e5b00..5ecf0d635a8d 100644
> --- a/arch/powerpc/lib/code-patching.c
> +++ b/arch/powerpc/lib/code-patching.c
> @@ -27,13 +27,7 @@ static int __patch_instruction(struct ppc_inst *exec_addr, 
> struct ppc_inst instr
> if (!ppc_inst_prefixed(instr)) {
> __put_user_asm(ppc_inst_val(instr), patch_addr, err, "stw");
> } else {
> -#ifdef CONFIG_CPU_LITTLE_ENDIAN
> -   __put_user_asm((u64)ppc_inst_suffix(instr) << 32 |
> -  ppc_inst_val(instr), patch_addr, err, "std");
> -#else
> -   __put_user_asm((u64)ppc_inst_val(instr) << 32 |
> -  ppc_inst_suffix(instr), patch_addr, err, 
> "std");
> -#endif
> +   __put_user_asm(ppc_inst_as_u64(instr), patch_addr, err, 
> "std");
> }
>
> if (err)
> --
> 2.25.1
>
I booted a BE and LE kernel - test_prefixed_patching() worked on both.
Also on BE and LE kernels I put optprobes on prefixed and non prefixed
instructions.
The correct value was passed via r4 to emulate_step().

Tested-by: Jordan Niethe 


[PATCH] powerpc: Add ppc_inst_as_u64()

2020-05-24 Thread Michael Ellerman
The code patching code wants to get the value of a struct ppc_inst as
a u64 when the instruction is prefixed, so we can pass the u64 down to
__put_user_asm() and write it with a single store.

This is a bit awkward because the value differs based on the CPU
endianness, so add a helper to do the conversion.

Signed-off-by: Michael Ellerman 
---
 arch/powerpc/include/asm/inst.h  | 9 +
 arch/powerpc/kernel/optprobes.c  | 3 +--
 arch/powerpc/lib/code-patching.c | 8 +---
 3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/arch/powerpc/include/asm/inst.h b/arch/powerpc/include/asm/inst.h
index d82e0c99cfa1..d61e07fb2937 100644
--- a/arch/powerpc/include/asm/inst.h
+++ b/arch/powerpc/include/asm/inst.h
@@ -100,6 +100,15 @@ static inline int ppc_inst_len(struct ppc_inst x)
return ppc_inst_prefixed(x) ? 8 : 4;
 }
 
+static inline u64 ppc_inst_as_u64(struct ppc_inst x)
+{
+#ifdef CONFIG_CPU_LITTLE_ENDIAN
+   return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x);
+#else
+   return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x);
+#endif
+}
+
 int probe_user_read_inst(struct ppc_inst *inst,
 struct ppc_inst __user *nip);
 
diff --git a/arch/powerpc/kernel/optprobes.c b/arch/powerpc/kernel/optprobes.c
index 3ac105e7faae..69bfe96884e2 100644
--- a/arch/powerpc/kernel/optprobes.c
+++ b/arch/powerpc/kernel/optprobes.c
@@ -283,8 +283,7 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe 
*op, struct kprobe *p)
 * 3. load instruction to be emulated into relevant register, and
 */
temp = ppc_inst_read((struct ppc_inst *)p->ainsn.insn);
-   patch_imm64_load_insns(ppc_inst_val(temp) | ((u64)ppc_inst_suffix(temp) 
<< 32),
-  4, buff + TMPL_INSN_IDX);
+   patch_imm64_load_insns(ppc_inst_as_u64(temp), 4, buff + TMPL_INSN_IDX);
 
/*
 * 4. branch back from trampoline
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index 64cf621e5b00..5ecf0d635a8d 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -27,13 +27,7 @@ static int __patch_instruction(struct ppc_inst *exec_addr, 
struct ppc_inst instr
if (!ppc_inst_prefixed(instr)) {
__put_user_asm(ppc_inst_val(instr), patch_addr, err, "stw");
} else {
-#ifdef CONFIG_CPU_LITTLE_ENDIAN
-   __put_user_asm((u64)ppc_inst_suffix(instr) << 32 |
-  ppc_inst_val(instr), patch_addr, err, "std");
-#else
-   __put_user_asm((u64)ppc_inst_val(instr) << 32 |
-  ppc_inst_suffix(instr), patch_addr, err, "std");
-#endif
+   __put_user_asm(ppc_inst_as_u64(instr), patch_addr, err, "std");
}
 
if (err)
-- 
2.25.1