linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: masami.hiramatsu.pt@hitachi.com (Masami Hiramatsu)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v18 11/11] ARM: optprobes: execute instruction during restoring if possible.
Date: Sun, 04 Jan 2015 14:25:48 +0900	[thread overview]
Message-ID: <54A8CEDC.8060808@hitachi.com> (raw)
In-Reply-To: <1419826053-27988-1-git-send-email-wangnan0@huawei.com>

(2014/12/29 13:07), Wang Nan wrote:
> This patch removes software emulation or simulation for most of probed
> instructions. If the instruction doesn't use PC relative addressing,
> it will be translated into following instructions in the restore code
> in code template:
> 
>  ldmia {r0 - r14}  // restore all instruction except PC
>  <instruction>     // direct execute the probed instruction
>  b next_insn       // branch to next instruction.
> 
> Signed-off-by: Wang Nan <wangnan0@huawei.com>

Reviewed-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>

[...]
>  /*
> + * See register_usage_mask. If the probed instruction doesn't use PC,
> + * we can copy it into template and have it executed directly without
> + * simulation or emulation.
> + */
> +#define can_kprobe_direct_exec(m)	(!((m) & __register_usage_mask(15)))

BTW, is there any macro which indicates 15 is the number of PC register?
If we can do something like below, it will be easier to understand.

#define can_kprobe_direct_exec(m)	(!test_bit(ARM_REG_PC/* == 15*/, &m))

Thank you,

> +
> +/*
>   * NOTE: the first sub and add instruction will be modified according
>   * to the stack cost of the instruction.
>   */
> @@ -66,7 +73,15 @@ asm (
>  			"	orrne	r2, #1\n"
>  			"	strne	r2, [sp, #60] @ set bit0 of PC for thumb\n"
>  			"	msr	cpsr_cxsf, r1\n"
> +			".global optprobe_template_restore_begin\n"
> +			"optprobe_template_restore_begin:\n"
>  			"	ldmia	sp, {r0 - r15}\n"
> +			".global optprobe_template_restore_orig_insn\n"
> +			"optprobe_template_restore_orig_insn:\n"
> +			"	nop\n"
> +			".global optprobe_template_restore_end\n"
> +			"optprobe_template_restore_end:\n"
> +			"	nop\n"
>  			".global optprobe_template_val\n"
>  			"optprobe_template_val:\n"
>  			"1:	.long 0\n"
> @@ -86,6 +101,12 @@ asm (
>  	((unsigned long *)&optprobe_template_add_sp - (unsigned long *)&optprobe_template_entry)
>  #define TMPL_SUB_SP \
>  	((unsigned long *)&optprobe_template_sub_sp - (unsigned long *)&optprobe_template_entry)
> +#define TMPL_RESTORE_BEGIN \
> +	((unsigned long *)&optprobe_template_restore_begin - (unsigned long *)&optprobe_template_entry)
> +#define TMPL_RESTORE_ORIGN_INSN \
> +	((unsigned long *)&optprobe_template_restore_orig_insn - (unsigned long *)&optprobe_template_entry)
> +#define TMPL_RESTORE_END \
> +	((unsigned long *)&optprobe_template_restore_end - (unsigned long *)&optprobe_template_entry)
>  
>  /*
>   * ARM can always optimize an instruction when using ARM ISA, except
> @@ -155,8 +176,12 @@ optimized_callback(struct optimized_kprobe *op, struct pt_regs *regs)
>  		__this_cpu_write(current_kprobe, NULL);
>  	}
>  
> -	/* In each case, we must singlestep the replaced instruction. */
> -	op->kp.ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
> +	/*
> +	 * We singlestep the replaced instruction only when it can't be
> +	 * executed directly during restore.
> +	 */
> +	if (!p->ainsn.kprobe_direct_exec)
> +		op->kp.ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
>  
>  	local_irq_restore(flags);
>  }
> @@ -238,6 +263,28 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *or
>  	val = (unsigned long)optimized_callback;
>  	code[TMPL_CALL_IDX] = val;
>  
> +	/* If possible, copy insn and have it executed during restore */
> +	orig->ainsn.kprobe_direct_exec = false;
> +	if (can_kprobe_direct_exec(orig->ainsn.register_usage_mask)) {
> +		kprobe_opcode_t final_branch = arm_gen_branch(
> +				(unsigned long)(&code[TMPL_RESTORE_END]),
> +				(unsigned long)(op->kp.addr) + 4);
> +		if (final_branch != 0) {
> +			/*
> +			 * Replace original 'ldmia sp, {r0 - r15}' with
> +			 * 'ldmia {r0 - r14}', restore all registers except pc.
> +			 */
> +			code[TMPL_RESTORE_BEGIN] = __opcode_to_mem_arm(0xe89d7fff);
> +
> +			/* The original probed instruction */
> +			code[TMPL_RESTORE_ORIGN_INSN] = __opcode_to_mem_arm(orig->opcode);
> +
> +			/* Jump back to next instruction */
> +			code[TMPL_RESTORE_END] = __opcode_to_mem_arm(final_branch);
> +			orig->ainsn.kprobe_direct_exec = true;
> +		}
> +	}
> +
>  	flush_icache_range((unsigned long)code,
>  			   (unsigned long)(&code[TMPL_END_IDX]));
>  
> 


-- 
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt at hitachi.com

  reply	other threads:[~2015-01-04  5:25 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-27  7:33 [PATCH v17 00/11] ARM: kprobes: OPTPROBES and other improvements Wang Nan
2014-12-27  7:34 ` [PATCH v17 01/11] ARM: probes: move all probe code to dedicate directory Wang Nan
2014-12-27  7:34 ` [PATCH v17 02/11] ARM: kprobes: remove unused ARM decoder actions Wang Nan
2014-12-27  7:35 ` [PATCH v17 03/11] ARM: kprobes: introduces checker Wang Nan
2014-12-27  7:35 ` [PATCH v17 04/11] ARM: kprobes: collects stack consumption for store instructions Wang Nan
2014-12-27  7:35 ` [PATCH v17 05/11] ARM: kprobes: disallow probing stack consuming instructions Wang Nan
2014-12-27  7:35 ` [PATCH v17 06/11] ARM: kprobes: Add test cases for " Wang Nan
2014-12-27  7:35 ` [PATCH v17 07/11] kprobes: Pass the original kprobe for preparing optimized kprobe Wang Nan
2014-12-27  7:35 ` [PATCH v17 08/11] ARM: kprobes: enable OPTPROBES for ARM 32 Wang Nan
2014-12-27  7:35 ` [PATCH v17 09/11] ARM: kprobes: Fix unreliable MRS instruction tests Wang Nan
2014-12-27  7:35 ` [PATCH v17 10/11] ARM: kprobes: check register usage for probed instruction Wang Nan
2014-12-27  7:36 ` [PATCH v17 11/11] ARM: optprobes: execute instruction during restoring if possible Wang Nan
2014-12-28 22:10   ` Masami Hiramatsu
2014-12-29  4:07     ` [PATCH v18 10/11] ARM: kprobes: check register usage for probed instruction Wang Nan
2015-01-04  5:05       ` Masami Hiramatsu
2015-01-04  5:16         ` Wang Nan
2014-12-29  4:07     ` [PATCH v18 11/11] ARM: optprobes: execute instruction during restoring if possible Wang Nan
2015-01-04  5:25       ` Masami Hiramatsu [this message]
2014-12-29  4:10     ` [PATCH v17 " Wang Nan

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=54A8CEDC.8060808@hitachi.com \
    --to=masami.hiramatsu.pt@hitachi.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    /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).