[PATCH v4 07/16] powerpc: Introduce functions for instruction nullity and equality

Jordan Niethe jniethe5 at gmail.com
Fri Mar 20 16:18:00 AEDT 2020


In preparation for an instruction data type that can not be directly
used with the '==' operator use functions for checking equality and
nullity.

Signed-off-by: Jordan Niethe <jniethe5 at gmail.com>
---
 arch/powerpc/kernel/optprobes.c      |  2 +-
 arch/powerpc/kernel/trace/ftrace.c   | 33 +++++++++++++++-------------
 arch/powerpc/lib/code-patching.c     | 16 +++++++-------
 arch/powerpc/lib/feature-fixups.c    |  2 +-
 arch/powerpc/lib/test_emulate_step.c |  4 ++--
 arch/powerpc/xmon/xmon.c             |  4 ++--
 6 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/arch/powerpc/kernel/optprobes.c b/arch/powerpc/kernel/optprobes.c
index 1025a7a3b3a8..6027425a85f2 100644
--- a/arch/powerpc/kernel/optprobes.c
+++ b/arch/powerpc/kernel/optprobes.c
@@ -259,7 +259,7 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
 				(unsigned long)emulate_step_addr,
 				BRANCH_SET_LINK);
 
-	if (!branch_op_callback || !branch_emulate_step)
+	if (ppc_inst_null(branch_op_callback) || ppc_inst_null(branch_emulate_step))
 		goto error;
 
 	patch_instruction(buff + TMPL_CALL_HDLR_IDX, branch_op_callback);
diff --git a/arch/powerpc/kernel/trace/ftrace.c b/arch/powerpc/kernel/trace/ftrace.c
index b189a34baaa2..b3645b664819 100644
--- a/arch/powerpc/kernel/trace/ftrace.c
+++ b/arch/powerpc/kernel/trace/ftrace.c
@@ -72,7 +72,7 @@ ftrace_modify_code(unsigned long ip, ppc_inst old, ppc_inst new)
 		return -EFAULT;
 
 	/* Make sure it is what we expect it to be */
-	if (replaced != old) {
+	if (!ppc_inst_equal(replaced, old)) {
 		pr_err("%p: replaced (%#x) != old (%#x)",
 		(void *)ip, replaced, old);
 		return -EINVAL;
@@ -169,7 +169,8 @@ __ftrace_make_nop(struct module *mod,
 	}
 
 	/* We expect either a mflr r0, or a std r0, LRSAVE(r1) */
-	if (op != PPC_INST(PPC_INST_MFLR) && op != PPC_INST(PPC_INST_STD_LR)) {
+	if (!ppc_inst_equal(op, PPC_INST(PPC_INST_MFLR)) &&
+	    !ppc_inst_equal(op, PPC_INST(PPC_INST_STD_LR))) {
 		pr_err("Unexpected instruction %08x around bl _mcount\n", op);
 		return -EINVAL;
 	}
@@ -199,7 +200,7 @@ __ftrace_make_nop(struct module *mod,
 		return -EFAULT;
 	}
 
-	if (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, op);
 		return -EINVAL;
 	}
@@ -296,7 +297,7 @@ static unsigned long find_ftrace_tramp(unsigned long ip)
 	for (i = NUM_FTRACE_TRAMPS - 1; i >= 0; i--)
 		if (!ftrace_tramps[i])
 			continue;
-		else if (create_branch((void *)ip, ftrace_tramps[i], 0))
+		else if (!ppc_inst_null(create_branch((void *)ip, ftrace_tramps[i], 0)))
 			return ftrace_tramps[i];
 
 	return 0;
@@ -368,7 +369,7 @@ static int setup_mcount_compiler_tramp(unsigned long tramp)
 #else
 	ptr = ppc_global_function_entry((void *)ftrace_caller);
 #endif
-	if (!create_branch((void *)tramp, ptr, 0)) {
+	if (ppc_inst_null(create_branch((void *)tramp, ptr, 0))) {
 		pr_debug("%ps is not reachable from existing mcount tramp\n",
 				(void *)ptr);
 		return -1;
@@ -437,7 +438,7 @@ int ftrace_make_nop(struct module *mod,
 	 * then we had to use a trampoline to make the call.
 	 * Otherwise just update the call site.
 	 */
-	if (test_24bit_addr(ip, addr)) {
+	if (!ppc_inst_null(test_24bit_addr(ip, addr))) {
 		/* within range */
 		old = ftrace_call_replace(ip, addr, 1);
 		new = PPC_INST(PPC_INST_NOP);
@@ -494,7 +495,8 @@ expected_nop_sequence(void *ip, ppc_inst op0, ppc_inst op1)
 	 * The load offset is different depending on the ABI. For simplicity
 	 * just mask it out when doing the compare.
 	 */
-	if ((op0 != 0x48000008) || (ppc_inst_mask(op1, 0xffff0000) != 0xe8410000))
+	if ((!ppc_inst_equal(op0, PPC_INST(0x48000008)) ||
+	     ((ppc_inst_mask(op1, 0xffff0000) != 0xe8410000))
 		return 0;
 	return 1;
 }
@@ -503,7 +505,7 @@ static int
 expected_nop_sequence(void *ip, ppc_inst op0, ppc_inst op1)
 {
 	/* look for patched "NOP" on ppc64 with -mprofile-kernel */
-	if (op0 != PPC_INST(PPC_INST_NOP))
+	if (!ppc_inst_equal(op0, PPC_INST(PPC_INST_NOP)))
 		return 0;
 	return 1;
 }
@@ -559,7 +561,7 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 	}
 
 	/* Ensure branch is within 24 bits */
-	if (!create_branch(ip, tramp, BRANCH_SET_LINK)) {
+	if (ppc_inst_null(create_branch(ip, tramp, BRANCH_SET_LINK))) {
 		pr_err("Branch out of range\n");
 		return -EINVAL;
 	}
@@ -584,7 +586,7 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 		return -EFAULT;
 
 	/* It should be pointing to a nop */
-	if (op != PPC_INST(PPC_INST_NOP)) {
+	if (!ppc_inst_equal(op,  PPC_INST(PPC_INST_NOP))) {
 		pr_err("Expected NOP but have %x\n", op);
 		return -EINVAL;
 	}
@@ -641,7 +643,7 @@ static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr)
 		return -EFAULT;
 	}
 
-	if (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, op);
 		return -EINVAL;
 	}
@@ -670,7 +672,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 	 * then we had to use a trampoline to make the call.
 	 * Otherwise just update the call site.
 	 */
-	if (test_24bit_addr(ip, addr)) {
+	if (!ppc_inst_null(test_24bit_addr(ip, addr))) {
 		/* within range */
 		old = PPC_INST(PPC_INST_NOP);
 		new = ftrace_call_replace(ip, addr, 1);
@@ -748,7 +750,7 @@ __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
 	}
 
 	/* The new target may be within range */
-	if (test_24bit_addr(ip, addr)) {
+	if (!ppc_inst_null(test_24bit_addr(ip, addr))) {
 		/* within range */
 		if (patch_branch((ppc_inst *)ip, addr, BRANCH_SET_LINK)) {
 			pr_err("REL24 out of range!\n");
@@ -778,7 +780,7 @@ __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
 	}
 
 	/* Ensure branch is within 24 bits */
-	if (!create_branch((ppc_inst *)ip, tramp, BRANCH_SET_LINK)) {
+	if (ppc_inst_null(create_branch((ppc_inst *)ip, tramp, BRANCH_SET_LINK))) {
 		pr_err("Branch out of range\n");
 		return -EINVAL;
 	}
@@ -803,7 +805,8 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
 	 * then we had to use a trampoline to make the call.
 	 * Otherwise just update the call site.
 	 */
-	if (test_24bit_addr(ip, addr) && test_24bit_addr(ip, old_addr)) {
+	if (!ppc_inst_null(test_24bit_addr(ip, addr)) &&
+	    !ppc_inst_null(test_24bit_addr(ip, old_addr))) {
 		/* within range */
 		old = ftrace_call_replace(ip, old_addr, 1);
 		new = ftrace_call_replace(ip, addr, 1);
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index 04a303c059e2..ec3abe1a6927 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -461,20 +461,20 @@ static void __init test_branch_iform(void)
 
 	/* Out of range relative negative offset, - 32 MB + 4*/
 	instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
-	check(instr == 0);
+	check(ppc_inst_null(instr));
 
 	/* Out of range relative positive offset, + 32 MB */
 	instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
-	check(instr == 0);
+	check(ppc_inst_null(instr));
 
 	/* Unaligned target */
 	instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
-	check(instr == 0);
+	check(ppc_inst_null(instr));
 
 	/* Check flags are masked correctly */
 	instr = create_branch(&instr, addr, 0xFFFFFFFC);
 	check(instr_is_branch_to_addr(&instr, addr));
-	check(instr == PPC_INST(0x48000000));
+	check(ppc_inst_equal(instr, PPC_INST(0x48000000)));
 }
 
 static void __init test_create_function_call(void)
@@ -544,20 +544,20 @@ static void __init test_branch_bform(void)
 
 	/* Out of range relative negative offset, - 32 KB + 4*/
 	instr = create_cond_branch(iptr, addr - 0x8004, flags);
-	check(instr == 0);
+	check(ppc_inst_null(instr));
 
 	/* Out of range relative positive offset, + 32 KB */
 	instr = create_cond_branch(iptr, addr + 0x8000, flags);
-	check(instr == 0);
+	check(ppc_inst_null(instr));
 
 	/* Unaligned target */
 	instr = create_cond_branch(iptr, addr + 3, flags);
-	check(instr == 0);
+	check(ppc_inst_null(instr));
 
 	/* Check flags are masked correctly */
 	instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
 	check(instr_is_branch_to_addr(&instr, addr));
-	check(instr == 0x43FF0000);
+	check(ppc_inst_equal(instr, PPC_INST(0x43FF0000)));
 }
 
 static void __init test_translate_branch(void)
diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c
index a5f3d98862e9..552106d1f64a 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -55,7 +55,7 @@ static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
 		/* Branch within the section doesn't need translating */
 		if (target < alt_start || target > alt_end) {
 			instr = translate_branch(dest, src);
-			if (!instr)
+			if (ppc_inst_null(instr))
 				return 1;
 		}
 	}
diff --git a/arch/powerpc/lib/test_emulate_step.c b/arch/powerpc/lib/test_emulate_step.c
index 227ebae9ba5a..486e057e5be1 100644
--- a/arch/powerpc/lib/test_emulate_step.c
+++ b/arch/powerpc/lib/test_emulate_step.c
@@ -846,7 +846,7 @@ static int __init emulate_compute_instr(struct pt_regs *regs,
 {
 	struct instruction_op op;
 
-	if (!regs || !instr)
+	if (!regs || ppc_inst_null(instr))
 		return -EINVAL;
 
 	if (analyse_instr(&op, regs, instr) != 1 ||
@@ -865,7 +865,7 @@ static int __init execute_compute_instr(struct pt_regs *regs,
 	extern int exec_instr(struct pt_regs *regs);
 	extern s32 patch__exec_instr;
 
-	if (!regs || !instr)
+	if (!regs || ppc_inst_null(instr))
 		return -EINVAL;
 
 	/* Patch the NOP with the actual instruction */
diff --git a/arch/powerpc/xmon/xmon.c b/arch/powerpc/xmon/xmon.c
index f8a7a55e6ab2..d045e583f1c9 100644
--- a/arch/powerpc/xmon/xmon.c
+++ b/arch/powerpc/xmon/xmon.c
@@ -951,7 +951,7 @@ static void remove_bpts(void)
 		if ((bp->enabled & (BP_TRAP|BP_CIABR)) != BP_TRAP)
 			continue;
 		if (mread(bp->address, &instr, 4) == 4
-		    && instr == PPC_INST(bpinstr)
+		    && ppc_inst_equal(instr, PPC_INST(bpinstr))
 		    && patch_instruction(
 			(ppc_inst *)bp->address, bp->instr[0]) != 0)
 			printf("Couldn't remove breakpoint at %lx\n",
@@ -2861,7 +2861,7 @@ generic_inst_dump(unsigned long adr, long count, int praddr,
 			break;
 		}
 		inst = PPC_INST(GETWORD(val));
-		if (adr > first_adr && inst == last_inst) {
+		if (adr > first_adr && ppc_inst_equal(inst, last_inst)) {
 			if (!dotted) {
 				printf(" ...\n");
 				dotted = 1;
-- 
2.17.1



More information about the Linuxppc-dev mailing list