Commit 50428fdc authored by Jordan Niethe's avatar Jordan Niethe Committed by Michael Ellerman

powerpc: Add a ppc_inst_as_str() helper

There are quite a few places where instructions are printed, this is
done using a '%x' format specifier. With the introduction of prefixed
instructions, this does not work well. Currently in these places,
ppc_inst_val() is used for the value for %x so only the first word of
prefixed instructions are printed.

When the instructions are word instructions, only a single word should
be printed. For prefixed instructions both the prefix and suffix should
be printed. To accommodate both of these situations, instead of a '%x'
specifier use '%s' and introduce a helper, __ppc_inst_as_str() which
returns a char *. The char * __ppc_inst_as_str() returns is buffer that
is passed to it by the caller.

It is cumbersome to require every caller of __ppc_inst_as_str() to now
declare a buffer. To make it more convenient to use __ppc_inst_as_str(),
wrap it in a macro that uses a compound statement to allocate a buffer
on the caller's stack before calling it.
Signed-off-by: default avatarJordan Niethe <jniethe5@gmail.com>
Reviewed-by: default avatarJoel Stanley <joel@jms.id.au>
Acked-by: default avatarSegher Boessenkool <segher@kernel.crashing.org>
[mpe: Drop 0x prefix to match most existings uses, especially xmon]
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20200602052728.18227-1-jniethe5@gmail.com
parent 4f825900
...@@ -122,6 +122,25 @@ static inline u64 ppc_inst_as_u64(struct ppc_inst x) ...@@ -122,6 +122,25 @@ static inline u64 ppc_inst_as_u64(struct ppc_inst x)
#endif #endif
} }
#define PPC_INST_STR_LEN sizeof("00000000 00000000")
static inline char *__ppc_inst_as_str(char str[PPC_INST_STR_LEN], struct ppc_inst x)
{
if (ppc_inst_prefixed(x))
sprintf(str, "%08x %08x", ppc_inst_val(x), ppc_inst_suffix(x));
else
sprintf(str, "%08x", ppc_inst_val(x));
return str;
}
#define ppc_inst_as_str(x) \
({ \
char __str[PPC_INST_STR_LEN]; \
__ppc_inst_as_str(__str, x); \
__str; \
})
int probe_user_read_inst(struct ppc_inst *inst, int probe_user_read_inst(struct ppc_inst *inst,
struct ppc_inst __user *nip); struct ppc_inst __user *nip);
......
...@@ -244,7 +244,7 @@ static int try_to_emulate(struct kprobe *p, struct pt_regs *regs) ...@@ -244,7 +244,7 @@ static int try_to_emulate(struct kprobe *p, struct pt_regs *regs)
* So, we should never get here... but, its still * So, we should never get here... but, its still
* good to catch them, just in case... * good to catch them, just in case...
*/ */
printk("Can't step on instruction %x\n", ppc_inst_val(insn)); printk("Can't step on instruction %s\n", ppc_inst_as_str(insn));
BUG(); BUG();
} else { } else {
/* /*
......
...@@ -73,8 +73,8 @@ ftrace_modify_code(unsigned long ip, struct ppc_inst old, struct ppc_inst new) ...@@ -73,8 +73,8 @@ ftrace_modify_code(unsigned long ip, struct ppc_inst old, struct ppc_inst new)
/* Make sure it is what we expect it to be */ /* Make sure it is what we expect it to be */
if (!ppc_inst_equal(replaced, old)) { if (!ppc_inst_equal(replaced, old)) {
pr_err("%p: replaced (%#x) != old (%#x)", pr_err("%p: replaced (%s) != old (%s)",
(void *)ip, ppc_inst_val(replaced), ppc_inst_val(old)); (void *)ip, ppc_inst_as_str(replaced), ppc_inst_as_str(old));
return -EINVAL; return -EINVAL;
} }
...@@ -137,7 +137,7 @@ __ftrace_make_nop(struct module *mod, ...@@ -137,7 +137,7 @@ __ftrace_make_nop(struct module *mod,
/* Make sure that that this is still a 24bit jump */ /* Make sure that that this is still a 24bit jump */
if (!is_bl_op(op)) { if (!is_bl_op(op)) {
pr_err("Not expected bl: opcode is %x\n", ppc_inst_val(op)); pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
return -EINVAL; return -EINVAL;
} }
...@@ -172,8 +172,8 @@ __ftrace_make_nop(struct module *mod, ...@@ -172,8 +172,8 @@ __ftrace_make_nop(struct module *mod,
/* We expect either a mflr r0, or a std r0, LRSAVE(r1) */ /* We expect either a mflr r0, or a std r0, LRSAVE(r1) */
if (!ppc_inst_equal(op, ppc_inst(PPC_INST_MFLR)) && if (!ppc_inst_equal(op, ppc_inst(PPC_INST_MFLR)) &&
!ppc_inst_equal(op, ppc_inst(PPC_INST_STD_LR))) { !ppc_inst_equal(op, ppc_inst(PPC_INST_STD_LR))) {
pr_err("Unexpected instruction %08x around bl _mcount\n", pr_err("Unexpected instruction %s around bl _mcount\n",
ppc_inst_val(op)); ppc_inst_as_str(op));
return -EINVAL; return -EINVAL;
} }
#else #else
...@@ -203,7 +203,7 @@ __ftrace_make_nop(struct module *mod, ...@@ -203,7 +203,7 @@ __ftrace_make_nop(struct module *mod,
} }
if (!ppc_inst_equal(op, ppc_inst(PPC_INST_LD_TOC))) { if (!ppc_inst_equal(op, ppc_inst(PPC_INST_LD_TOC))) {
pr_err("Expected %08x found %08x\n", PPC_INST_LD_TOC, ppc_inst_val(op)); pr_err("Expected %08x found %s\n", PPC_INST_LD_TOC, ppc_inst_as_str(op));
return -EINVAL; return -EINVAL;
} }
#endif /* CONFIG_MPROFILE_KERNEL */ #endif /* CONFIG_MPROFILE_KERNEL */
...@@ -231,7 +231,7 @@ __ftrace_make_nop(struct module *mod, ...@@ -231,7 +231,7 @@ __ftrace_make_nop(struct module *mod,
/* Make sure that that this is still a 24bit jump */ /* Make sure that that this is still a 24bit jump */
if (!is_bl_op(op)) { if (!is_bl_op(op)) {
pr_err("Not expected bl: opcode is %x\n", ppc_inst_val(op)); pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
return -EINVAL; return -EINVAL;
} }
...@@ -406,7 +406,7 @@ static int __ftrace_make_nop_kernel(struct dyn_ftrace *rec, unsigned long addr) ...@@ -406,7 +406,7 @@ static int __ftrace_make_nop_kernel(struct dyn_ftrace *rec, unsigned long addr)
/* Make sure that that this is still a 24bit jump */ /* Make sure that that this is still a 24bit jump */
if (!is_bl_op(op)) { if (!is_bl_op(op)) {
pr_err("Not expected bl: opcode is %x\n", ppc_inst_val(op)); pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
return -EINVAL; return -EINVAL;
} }
...@@ -533,8 +533,8 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) ...@@ -533,8 +533,8 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
return -EFAULT; return -EFAULT;
if (!expected_nop_sequence(ip, op[0], op[1])) { if (!expected_nop_sequence(ip, op[0], op[1])) {
pr_err("Unexpected call sequence at %p: %x %x\n", pr_err("Unexpected call sequence at %p: %s %s\n",
ip, ppc_inst_val(op[0]), ppc_inst_val(op[1])); ip, ppc_inst_as_str(op[0]), ppc_inst_as_str(op[1]));
return -EINVAL; return -EINVAL;
} }
...@@ -597,7 +597,7 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) ...@@ -597,7 +597,7 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
/* It should be pointing to a nop */ /* It should be pointing to a nop */
if (!ppc_inst_equal(op, ppc_inst(PPC_INST_NOP))) { if (!ppc_inst_equal(op, ppc_inst(PPC_INST_NOP))) {
pr_err("Expected NOP but have %x\n", ppc_inst_val(op)); pr_err("Expected NOP but have %s\n", ppc_inst_as_str(op));
return -EINVAL; return -EINVAL;
} }
...@@ -654,7 +654,7 @@ static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr) ...@@ -654,7 +654,7 @@ static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr)
} }
if (!ppc_inst_equal(op, ppc_inst(PPC_INST_NOP))) { if (!ppc_inst_equal(op, ppc_inst(PPC_INST_NOP))) {
pr_err("Unexpected call sequence at %p: %x\n", ip, ppc_inst_val(op)); pr_err("Unexpected call sequence at %p: %s\n", ip, ppc_inst_as_str(op));
return -EINVAL; return -EINVAL;
} }
...@@ -733,7 +733,7 @@ __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, ...@@ -733,7 +733,7 @@ __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
/* Make sure that that this is still a 24bit jump */ /* Make sure that that this is still a 24bit jump */
if (!is_bl_op(op)) { if (!is_bl_op(op)) {
pr_err("Not expected bl: opcode is %x\n", ppc_inst_val(op)); pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
return -EINVAL; return -EINVAL;
} }
......
...@@ -1147,7 +1147,7 @@ static int __init emulate_compute_instr(struct pt_regs *regs, ...@@ -1147,7 +1147,7 @@ static int __init emulate_compute_instr(struct pt_regs *regs,
if (analyse_instr(&op, regs, instr) != 1 || if (analyse_instr(&op, regs, instr) != 1 ||
GETTYPE(op.type) != COMPUTE) { GETTYPE(op.type) != COMPUTE) {
pr_info("emulation failed, instruction = 0x%08x\n", ppc_inst_val(instr)); pr_info("execution failed, instruction = %s\n", ppc_inst_as_str(instr));
return -EFAULT; return -EFAULT;
} }
...@@ -1167,7 +1167,7 @@ static int __init execute_compute_instr(struct pt_regs *regs, ...@@ -1167,7 +1167,7 @@ static int __init execute_compute_instr(struct pt_regs *regs,
/* Patch the NOP with the actual instruction */ /* Patch the NOP with the actual instruction */
patch_instruction_site(&patch__exec_instr, instr); patch_instruction_site(&patch__exec_instr, instr);
if (exec_instr(regs)) { if (exec_instr(regs)) {
pr_info("execution failed, instruction = 0x%08x\n", ppc_inst_val(instr)); pr_info("execution failed, instruction = %s\n", ppc_inst_as_str(instr));
return -EFAULT; return -EFAULT;
} }
......
...@@ -2977,7 +2977,7 @@ generic_inst_dump(unsigned long adr, long count, int praddr, ...@@ -2977,7 +2977,7 @@ generic_inst_dump(unsigned long adr, long count, int praddr,
dotted = 0; dotted = 0;
last_inst = inst; last_inst = inst;
if (praddr) if (praddr)
printf(REG" %.8x", adr, ppc_inst_val(inst)); printf(REG" %s", adr, ppc_inst_as_str(inst));
printf("\t"); printf("\t");
dump_func(ppc_inst_val(inst), adr); dump_func(ppc_inst_val(inst), adr);
printf("\n"); printf("\n");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment