linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Balbir Singh <bsingharora@gmail.com>
To: linuxppc-dev@lists.ozlabs.org, mpe@ellerman.id.au
Cc: naveen.n.rao@linux.vnet.ibm.com, christophe.leroy@c-s.fr,
	paulus@samba.org, rashmica.g@gmail.com, labbott@redhat.com,
	Balbir Singh <bsingharora@gmail.com>
Subject: [PATCH v2 3/9] powerpc/kprobes/optprobes: Move over to patch_instruction
Date: Sat,  3 Jun 2017 17:18:37 +1000	[thread overview]
Message-ID: <20170603071843.11966-4-bsingharora@gmail.com> (raw)
In-Reply-To: <20170603071843.11966-1-bsingharora@gmail.com>

With text moving to read-only migrate optprobes to using
the patch_instruction infrastructure. Without this optprobes
will fail and complain.

Signed-off-by: Balbir Singh <bsingharora@gmail.com>
---
 arch/powerpc/kernel/optprobes.c | 58 ++++++++++++++++++++++++++---------------
 1 file changed, 37 insertions(+), 21 deletions(-)

diff --git a/arch/powerpc/kernel/optprobes.c b/arch/powerpc/kernel/optprobes.c
index ec60ed0..1c7326c 100644
--- a/arch/powerpc/kernel/optprobes.c
+++ b/arch/powerpc/kernel/optprobes.c
@@ -158,12 +158,13 @@ void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
 void patch_imm32_load_insns(unsigned int val, kprobe_opcode_t *addr)
 {
 	/* addis r4,0,(insn)@h */
-	*addr++ = PPC_INST_ADDIS | ___PPC_RT(4) |
-		  ((val >> 16) & 0xffff);
+	patch_instruction((unsigned int *)addr, PPC_INST_ADDIS | ___PPC_RT(4) |
+					((val >> 16) & 0xffff));
+	addr++;
 
 	/* ori r4,r4,(insn)@l */
-	*addr = PPC_INST_ORI | ___PPC_RA(4) | ___PPC_RS(4) |
-		(val & 0xffff);
+	patch_instruction((unsigned int *)addr, PPC_INST_ORI | ___PPC_RA(4) |
+					___PPC_RS(4) | (val & 0xffff));
 }
 
 /*
@@ -173,24 +174,28 @@ void patch_imm32_load_insns(unsigned int val, kprobe_opcode_t *addr)
 void patch_imm64_load_insns(unsigned long val, kprobe_opcode_t *addr)
 {
 	/* lis r3,(op)@highest */
-	*addr++ = PPC_INST_ADDIS | ___PPC_RT(3) |
-		  ((val >> 48) & 0xffff);
+	patch_instruction((unsigned int *)addr, PPC_INST_ADDIS | ___PPC_RT(3) |
+					((val >> 48) & 0xffff));
+	addr++;
 
 	/* ori r3,r3,(op)@higher */
-	*addr++ = PPC_INST_ORI | ___PPC_RA(3) | ___PPC_RS(3) |
-		  ((val >> 32) & 0xffff);
+	patch_instruction((unsigned int *)addr, PPC_INST_ORI | ___PPC_RA(3) |
+				___PPC_RS(3) | ((val >> 32) & 0xffff));
+	addr++;
 
 	/* rldicr r3,r3,32,31 */
-	*addr++ = PPC_INST_RLDICR | ___PPC_RA(3) | ___PPC_RS(3) |
-		  __PPC_SH64(32) | __PPC_ME64(31);
+	patch_instruction((unsigned int *)addr, PPC_INST_RLDICR | ___PPC_RA(3) |
+				___PPC_RS(3) | __PPC_SH64(32) | __PPC_ME64(31));
+	addr++;
 
 	/* oris r3,r3,(op)@h */
-	*addr++ = PPC_INST_ORIS | ___PPC_RA(3) | ___PPC_RS(3) |
-		  ((val >> 16) & 0xffff);
+	patch_instruction((unsigned int *)addr, PPC_INST_ORIS | ___PPC_RA(3) |
+					___PPC_RS(3) | ((val >> 16) & 0xffff));
+	addr++;
 
 	/* ori r3,r3,(op)@l */
-	*addr = PPC_INST_ORI | ___PPC_RA(3) | ___PPC_RS(3) |
-		(val & 0xffff);
+	patch_instruction((unsigned int *)addr, PPC_INST_ORI | ___PPC_RA(3) |
+				___PPC_RS(3) | (val & 0xffff));
 }
 
 int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
@@ -198,7 +203,8 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
 	kprobe_opcode_t *buff, branch_op_callback, branch_emulate_step;
 	kprobe_opcode_t *op_callback_addr, *emulate_step_addr;
 	long b_offset;
-	unsigned long nip;
+	unsigned long nip, size;
+	int rc, i;
 
 	kprobe_ppc_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
 
@@ -231,8 +237,15 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
 		goto error;
 
 	/* Setup template */
-	memcpy(buff, optprobe_template_entry,
-			TMPL_END_IDX * sizeof(kprobe_opcode_t));
+	/* We can optimize this via patch_instruction_window later */
+	size = (TMPL_END_IDX * sizeof(kprobe_opcode_t)) / sizeof(int);
+	pr_devel("Copying template to %p, size %lu\n", buff, size);
+	for (i = 0; i < size; i++) {
+		rc = patch_instruction((unsigned int *)buff + i,
+			*((unsigned int *)(optprobe_template_entry) + i));
+		if (rc < 0)
+			goto error;
+	}
 
 	/*
 	 * Fixup the template with instructions to:
@@ -261,8 +274,10 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
 	if (!branch_op_callback || !branch_emulate_step)
 		goto error;
 
-	buff[TMPL_CALL_HDLR_IDX] = branch_op_callback;
-	buff[TMPL_EMULATE_IDX] = branch_emulate_step;
+	patch_instruction((unsigned int *)buff + TMPL_CALL_HDLR_IDX,
+				branch_op_callback);
+	patch_instruction((unsigned int *)buff + TMPL_EMULATE_IDX,
+				branch_emulate_step);
 
 	/*
 	 * 3. load instruction to be emulated into relevant register, and
@@ -272,8 +287,9 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
 	/*
 	 * 4. branch back from trampoline
 	 */
-	buff[TMPL_RET_IDX] = create_branch((unsigned int *)buff + TMPL_RET_IDX,
-				(unsigned long)nip, 0);
+	patch_instruction((unsigned int *)buff + TMPL_RET_IDX,
+				create_branch((unsigned int *)buff +
+				TMPL_RET_IDX, (unsigned long)nip, 0));
 
 	flush_icache_range((unsigned long)buff,
 			   (unsigned long)(&buff[TMPL_END_IDX]));
-- 
2.9.3

  parent reply	other threads:[~2017-06-03  7:19 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-03  7:18 [PATCH v2 0/9] Enable STRICT_KERNEL_RWX Balbir Singh
2017-06-03  7:18 ` [PATCH v2 1/9] powerpc/lib/code-patching: Enhance code patching Balbir Singh
2017-06-03 23:45   ` kbuild test robot
2017-06-07 10:25     ` Christophe LEROY
2017-06-07 11:04       ` Balbir Singh
2017-06-07 11:52         ` Michael Ellerman
2017-06-03  7:18 ` [PATCH v2 2/9] powerpc/kprobes: Move kprobes over to patch_instruction Balbir Singh
2017-06-03  7:18 ` Balbir Singh [this message]
2017-06-03  7:18 ` [PATCH v2 4/9] powerpc/xmon: Add patch_instruction supporf for xmon Balbir Singh
2017-06-03  7:18 ` [PATCH v2 5/9] powerpc/vmlinux.lds: Align __init_begin to 16M Balbir Singh
2017-06-04  4:22   ` Nicholas Piggin
2017-06-04 22:42     ` Balbir Singh
2017-06-05  6:51       ` Nicholas Piggin
2017-06-03  7:18 ` [PATCH v2 6/9] powerpc/platform/pseries/lpar: Fix updatepp and updateboltedpp Balbir Singh
2017-06-03  7:18 ` [PATCH v2 7/9] powerpc/mm/hash: Implement mark_rodata_ro() for hash Balbir Singh
2017-06-03  7:18 ` [PATCH v2 8/9] powerpc/Kconfig: Enable STRICT_KERNEL_RWX Balbir Singh
2017-06-03  7:18 ` [PATCH v2 9/9] powerpc/mm/ptdump: Dump the first entry of the linear mapping as well Balbir Singh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170603071843.11966-4-bsingharora@gmail.com \
    --to=bsingharora@gmail.com \
    --cc=christophe.leroy@c-s.fr \
    --cc=labbott@redhat.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mpe@ellerman.id.au \
    --cc=naveen.n.rao@linux.vnet.ibm.com \
    --cc=paulus@samba.org \
    --cc=rashmica.g@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).