All of lore.kernel.org
 help / color / mirror / Atom feed
From: tixy@yxit.co.uk (Tixy)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 12/51] ARM: kprobes: Add Thumb breakpoint support
Date: Sat,  9 Jul 2011 11:56:59 +0100	[thread overview]
Message-ID: <1310209058-20980-13-git-send-email-tixy@yxit.co.uk> (raw)
In-Reply-To: <1310209058-20980-1-git-send-email-tixy@yxit.co.uk>

From: Jon Medhurst <tixy@yxit.co.uk>

Extend the breakpoint insertion and catching functions to support Thumb
code.

As breakpoints are no longer of a fixed size, the flush_insns macro
is modified to take a size argument instead of an instruction count.

Note, we need both 16- and 32-bit Thumb breakpoints, because if we
were to use a 16-bit breakpoint to replace a 32-bit instruction which
was in an IT block, and the condition check failed, then the breakpoint
may not fire (it's unpredictable behaviour) and the CPU could then try
and execute the second half of the 32-bit Thumb instruction.

Signed-off-by: Jon Medhurst <tixy@yxit.co.uk>
---
 arch/arm/kernel/kprobes.h |    7 ++-
 arch/arm/kernel/kprobes.c |  122 ++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 115 insertions(+), 14 deletions(-)

diff --git a/arch/arm/kernel/kprobes.h b/arch/arm/kernel/kprobes.h
index 86abfab..a84b14d 100644
--- a/arch/arm/kernel/kprobes.h
+++ b/arch/arm/kernel/kprobes.h
@@ -18,10 +18,13 @@
 #define _ARM_KERNEL_KPROBES_H
 
 /*
- * This undefined instruction must be unique and
+ * These undefined instructions must be unique and
  * reserved solely for kprobes' use.
  */
-#define KPROBE_BREAKPOINT_INSTRUCTION	0xe7f001f8
+#define KPROBE_ARM_BREAKPOINT_INSTRUCTION	0xe7f001f8
+#define KPROBE_THUMB16_BREAKPOINT_INSTRUCTION	0xde18
+#define KPROBE_THUMB32_BREAKPOINT_INSTRUCTION	0xf7f0a018
+
 
 enum kprobe_insn {
 	INSN_REJECTED,
diff --git a/arch/arm/kernel/kprobes.c b/arch/arm/kernel/kprobes.c
index 0df2d6d..b8f5b3b 100644
--- a/arch/arm/kernel/kprobes.c
+++ b/arch/arm/kernel/kprobes.c
@@ -34,10 +34,10 @@
 	min((unsigned long)MAX_STACK_SIZE,		\
 	    (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
 
-#define flush_insns(addr, cnt) 				\
+#define flush_insns(addr, size)				\
 	flush_icache_range((unsigned long)(addr),	\
 			   (unsigned long)(addr) +	\
-			   sizeof(kprobe_opcode_t) * (cnt))
+			   (size))
 
 /* Used as a marker in ARM_pc to note when we're in a jprobe. */
 #define JPROBE_MAGIC_ADDR		0xffffffff
@@ -86,7 +86,8 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 			return -ENOMEM;
 		for (is = 0; is < MAX_INSN_SIZE; ++is)
 			p->ainsn.insn[is] = tmp_insn[is];
-		flush_insns(p->ainsn.insn, MAX_INSN_SIZE);
+		flush_insns(p->ainsn.insn,
+				sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
 		break;
 
 	case INSN_GOOD_NO_SLOT:	/* instruction doesn't need insn slot */
@@ -97,24 +98,82 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	return 0;
 }
 
+#ifdef CONFIG_THUMB2_KERNEL
+
+/*
+ * For a 32-bit Thumb breakpoint spanning two memory words we need to take
+ * special precautions to insert the breakpoint atomically, especially on SMP
+ * systems. This is achieved by calling this arming function using stop_machine.
+ */
+int __kprobes set_t32_breakpoint(void *addr)
+{
+	((u16 *)addr)[0] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION >> 16;
+	((u16 *)addr)[1] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION & 0xffff;
+	flush_insns(addr, 2*sizeof(u16));
+	return 0;
+}
+
 void __kprobes arch_arm_kprobe(struct kprobe *p)
 {
-	*p->addr = KPROBE_BREAKPOINT_INSTRUCTION;
-	flush_insns(p->addr, 1);
+	uintptr_t addr = (uintptr_t)p->addr & ~1; /* Remove any Thumb flag */
+
+	if (!is_wide_instruction(p->opcode)) {
+		*(u16 *)addr = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
+		flush_insns(addr, sizeof(u16));
+	} else if (addr & 2) {
+		/* A 32-bit instruction spanning two words needs special care */
+		stop_machine(set_t32_breakpoint, (void *)addr, &cpu_online_map);
+	} else {
+		/* Word aligned 32-bit instruction can be written atomically */
+		u32 bkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
+#ifndef __ARMEB__ /* Swap halfwords for little-endian */
+		bkp = (bkp >> 16) | (bkp << 16);
+#endif
+		*(u32 *)addr = bkp;
+		flush_insns(addr, sizeof(u32));
+	}
 }
 
+#else /* !CONFIG_THUMB2_KERNEL */
+
+void __kprobes arch_arm_kprobe(struct kprobe *p)
+{
+	*p->addr = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
+	flush_insns(p->addr, sizeof(p->addr[0]));
+}
+
+#endif /* !CONFIG_THUMB2_KERNEL */
+
 /*
  * The actual disarming is done here on each CPU and synchronized using
  * stop_machine. This synchronization is necessary on SMP to avoid removing
  * a probe between the moment the 'Undefined Instruction' exception is raised
  * and the moment the exception handler reads the faulting instruction from
- * memory.
+ * memory. It is also needed to atomically set the two half-words of a 32-bit
+ * Thumb breakpoint.
  */
 int __kprobes __arch_disarm_kprobe(void *p)
 {
 	struct kprobe *kp = p;
+#ifdef CONFIG_THUMB2_KERNEL
+	u16 *addr = (u16 *)((uintptr_t)kp->addr & ~1);
+	kprobe_opcode_t insn = kp->opcode;
+	unsigned int len;
+
+	if (is_wide_instruction(insn)) {
+		((u16 *)addr)[0] = insn>>16;
+		((u16 *)addr)[1] = insn;
+		len = 2*sizeof(u16);
+	} else {
+		((u16 *)addr)[0] = insn;
+		len = sizeof(u16);
+	}
+	flush_insns(addr, len);
+
+#else /* !CONFIG_THUMB2_KERNEL */
 	*kp->addr = kp->opcode;
-	flush_insns(kp->addr, 1);
+	flush_insns(kp->addr, sizeof(kp->addr[0]));
+#endif
 	return 0;
 }
 
@@ -167,11 +226,23 @@ void __kprobes kprobe_handler(struct pt_regs *regs)
 {
 	struct kprobe *p, *cur;
 	struct kprobe_ctlblk *kcb;
-	kprobe_opcode_t	*addr = (kprobe_opcode_t *)regs->ARM_pc;
 
 	kcb = get_kprobe_ctlblk();
 	cur = kprobe_running();
-	p = get_kprobe(addr);
+
+#ifdef CONFIG_THUMB2_KERNEL
+	/*
+	 * First look for a probe which was registered using an address with
+	 * bit 0 set, this is the usual situation for pointers to Thumb code.
+	 * If not found, fallback to looking for one with bit 0 clear.
+	 */
+	p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
+	if (!p)
+		p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
+
+#else /* ! CONFIG_THUMB2_KERNEL */
+	p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
+#endif
 
 	if (p) {
 		if (cur) {
@@ -511,17 +582,44 @@ int __kprobes arch_trampoline_kprobe(struct kprobe *p)
 	return 0;
 }
 
-static struct undef_hook kprobes_break_hook = {
+#ifdef CONFIG_THUMB2_KERNEL
+
+static struct undef_hook kprobes_thumb16_break_hook = {
+	.instr_mask	= 0xffff,
+	.instr_val	= KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
+	.cpsr_mask	= MODE_MASK,
+	.cpsr_val	= SVC_MODE,
+	.fn		= kprobe_trap_handler,
+};
+
+static struct undef_hook kprobes_thumb32_break_hook = {
+	.instr_mask	= 0xffffffff,
+	.instr_val	= KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
+	.cpsr_mask	= MODE_MASK,
+	.cpsr_val	= SVC_MODE,
+	.fn		= kprobe_trap_handler,
+};
+
+#else  /* !CONFIG_THUMB2_KERNEL */
+
+static struct undef_hook kprobes_arm_break_hook = {
 	.instr_mask	= 0xffffffff,
-	.instr_val	= KPROBE_BREAKPOINT_INSTRUCTION,
+	.instr_val	= KPROBE_ARM_BREAKPOINT_INSTRUCTION,
 	.cpsr_mask	= MODE_MASK,
 	.cpsr_val	= SVC_MODE,
 	.fn		= kprobe_trap_handler,
 };
 
+#endif /* !CONFIG_THUMB2_KERNEL */
+
 int __init arch_init_kprobes()
 {
 	arm_kprobe_decode_init();
-	register_undef_hook(&kprobes_break_hook);
+#ifdef CONFIG_THUMB2_KERNEL
+	register_undef_hook(&kprobes_thumb16_break_hook);
+	register_undef_hook(&kprobes_thumb32_break_hook);
+#else
+	register_undef_hook(&kprobes_arm_break_hook);
+#endif
 	return 0;
 }
-- 
1.7.2.5

  parent reply	other threads:[~2011-07-09 10:56 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-09 10:56 ARM: kprobes: Add support for Thumb-2 Tixy
2011-07-09 10:56 ` [PATCH 01/51] ARM: Thumb-2: Fix exception return sequence to restore stack correctly Tixy
2011-07-11 18:07   ` Nicolas Pitre
2011-07-09 10:56 ` [PATCH 02/51] ARM: Thumb-2: Support Thumb-2 in undefined instruction handler Tixy
2011-07-11 18:14   ` Nicolas Pitre
2011-07-09 10:56 ` [PATCH 03/51] ARM: kprobes: Rename kprobes-decode.c to kprobes-arm.c Tixy
2011-07-09 10:56 ` [PATCH 04/51] ARM: kprobes: Split out internal parts of kprobes.h Tixy
2011-07-09 10:56 ` [PATCH 05/51] ARM: kprobes: Add kprobes-common.c Tixy
2011-07-09 10:56 ` [PATCH 06/51] ARM: kprobes: Move is_writeback define to header file Tixy
2011-07-09 10:56 ` [PATCH 07/51] ARM: kprobes: Move find_str_pc_offset into kprobes-common.c Tixy
2011-07-09 10:56 ` [PATCH 08/51] ARM: kprobes: Make str_pc_offset a constant on ARMv7 Tixy
2011-07-09 10:56 ` [PATCH 09/51] ARM: kprobes: Make kprobes framework work on Thumb-2 kernels Tixy
2011-07-09 10:56 ` [PATCH 10/51] ARM: kprobes: Add Thumb instruction decoding stubs Tixy
2011-07-09 10:56 ` [PATCH 11/51] ARM: Kconfig: Allow kprobes on Thumb-2 kernels Tixy
2011-07-11 11:01   ` Sergei Shtylyov
2011-07-11 11:33     ` Tixy
2011-07-11 11:42       ` Russell King - ARM Linux
2011-07-11 11:47         ` Tixy
2011-07-09 10:56 ` Tixy [this message]
2011-07-09 10:57 ` [PATCH 13/51] ARM: kprobes: Add condition code checking to Thumb emulation Tixy
2011-07-09 10:57 ` [PATCH 14/51] ARM: kprobes: Add it_advance() Tixy
2011-07-09 10:57 ` [PATCH 15/51] ARM: kprobes: Don't trigger probes on conditional instructions when condition is false Tixy
2011-07-11 19:04   ` Nicolas Pitre
2011-07-09 10:57 ` [PATCH 16/51] ARM: kprobes: Use conditional breakpoints for ARM probes Tixy
2011-07-09 10:57 ` [PATCH 17/51] ARM: kprobes: Add hooks to override singlestep() Tixy
2011-07-09 10:57 ` [PATCH 18/51] ARM: kprobes: Extend arch_specific_insn to add pointer to emulated instruction Tixy
2011-07-11 19:19   ` Nicolas Pitre
2011-07-09 10:57 ` [PATCH 19/51] ARM: kprobes: Infrastructure for table driven decoding of CPU instructions Tixy
2011-07-11 20:05   ` Nicolas Pitre
2011-07-12  7:14     ` Tixy
2011-07-09 10:57 ` [PATCH 20/51] ARM: kprobes: Decode 16-bit Thumb hint instructions Tixy
2011-07-09 10:57 ` [PATCH 21/51] ARM: ptrace: Add APSR_MASK definition to ptrace.h Tixy
2011-07-09 10:57 ` [PATCH 22/51] ARM: kprobes: Decode 16-bit Thumb data-processing instructions Tixy
2011-07-09 10:57 ` [PATCH 23/51] ARM: kprobes: Add bx_write_pc() Tixy
2011-07-09 10:57 ` [PATCH 24/51] ARM: kprobes: Decode 16-bit Thumb BX and BLX instructions Tixy
2011-07-09 10:57 ` [PATCH 25/51] ARM: kprobes: Decode 16-bit Thumb special data instructions Tixy
2011-07-09 10:57 ` [PATCH 26/51] ARM: kprobes: Decode 16-bit Thumb load and store instructions Tixy
2011-07-09 10:57 ` [PATCH 27/51] ARM: kprobes: Decode 16-bit Thumb PC- and SP-relative address instructions Tixy
2011-07-09 10:57 ` [PATCH 28/51] ARM: kprobes: Decode 16-bit Thumb CBZ and bit manipulation instructions Tixy
2011-07-09 10:57 ` [PATCH 29/51] ARM: kprobes: Decode 16-bit Thumb PUSH and POP instructions Tixy
2011-07-09 10:57 ` [PATCH 30/51] ARM: kprobes: Decode 16-bit Thumb IT instruction Tixy
2011-07-09 10:57 ` [PATCH 31/51] ARM: kprobes: Reject 16-bit Thumb SVC and UNDEFINED instructions Tixy
2011-07-09 10:57 ` [PATCH 32/51] ARM: kprobes: Decode 16-bit Thumb branch instructions Tixy
2011-07-09 10:57 ` [PATCH 33/51] ARM: kprobes: Reject 16-bit Thumb SETEND, CPS and BKPT instructions Tixy
2011-07-09 10:57 ` [PATCH 34/51] ARM: kprobes: Decode 32-bit Thumb hint instructions Tixy
2011-07-09 10:57 ` [PATCH 35/51] ARM: kprobes: Add load_write_pc() Tixy
2011-07-09 10:57 ` [PATCH 36/51] ARM: kprobes: Add common decoding function for LDM and STM Tixy
2011-07-09 10:57 ` [PATCH 37/51] ARM: kprobes: Optimise emulation of " Tixy
2011-07-12  0:45   ` Nicolas Pitre
2011-07-12  7:20     ` Tixy
2011-07-09 10:57 ` [PATCH 38/51] ARM: kprobes: Decode 32-bit Thumb load/store multiple instructions Tixy
2011-07-09 10:57 ` [PATCH 39/51] ARM: kprobes: Decode 32-bit Thumb load/store dual and load/store exclusive instructions Tixy
2011-07-09 10:57 ` [PATCH 40/51] ARM: kprobes: Decode 32-bit Thumb table branch instructions Tixy
2011-07-09 10:57 ` [PATCH 41/51] ARM: kprobes: Decode 32-bit Thumb data-processing (shifted register) instructions Tixy
2011-07-09 10:57 ` [PATCH 42/51] ARM: kprobes: Decode 32-bit Thumb data-processing (modified immediate) instructions Tixy
2011-07-09 10:57 ` [PATCH 43/51] ARM: kprobes: Decode 32-bit Thumb data-processing (plain binary " Tixy
2011-07-09 10:57 ` [PATCH 44/51] ARM: kprobes: Decode 32-bit miscellaneous control instructions Tixy
2011-07-09 10:57 ` [PATCH 45/51] ARM: kprobes: Decode 32-bit Thumb branch instructions Tixy
2011-07-09 10:57 ` [PATCH 46/51] ARM: kprobes: Reject 32-bit Thumb coprocessor and SIMD instructions Tixy
2011-07-09 10:57 ` [PATCH 47/51] ARM: kprobes: Decode 32-bit Thumb memory hint instructions Tixy
2011-07-09 10:57 ` [PATCH 48/51] ARM: kprobes: Decode 32-bit Thumb load/store single data item instructions Tixy
2011-07-09 10:57 ` [PATCH 49/51] ARM: kprobes: Decode 32-bit Thumb data-processing (register) instructions Tixy
2011-07-09 10:57 ` [PATCH 50/51] ARM: kprobes: Decode 32-bit Thumb long multiply and divide instructions Tixy
2011-07-09 10:57 ` [PATCH 51/51] ARM: kprobes: Decode 32-bit Thumb multiply and absolute difference instructions Tixy
2011-07-12  1:02 ` ARM: kprobes: Add support for Thumb-2 Nicolas Pitre

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=1310209058-20980-13-git-send-email-tixy@yxit.co.uk \
    --to=tixy@yxit.co.uk \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.