Skip to content

Commit b21d55e

Browse files
vitkyrkaRussell King
authored andcommitted
ARM: 7332/1: extract out code patch function from kprobes
Extract out the code patching code from kprobes so that it can be used from the jump label code. Additionally, the separated code: - Uses the IS_ENABLED() macros instead of the #ifdefs for THUMB2 support - Unifies the two separate functions in kprobes, providing one function that uses stop_machine() internally, and one that can be called from stop_machine() directly - Patches the text on all CPUs only on processors requiring software broadcasting of cache operations Acked-by: Jon Medhurst <tixy@yxit.co.uk> Tested-by: Jon Medhurst <tixy@yxit.co.uk> Signed-off-by: Rabin Vincent <rabin@rab.in> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent d82227c commit b21d55e

4 files changed

Lines changed: 108 additions & 63 deletions

File tree

‎arch/arm/kernel/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
88
ifdef CONFIG_FUNCTION_TRACER
99
CFLAGS_REMOVE_ftrace.o = -pg
1010
CFLAGS_REMOVE_insn.o = -pg
11+
CFLAGS_REMOVE_patch.o = -pg
1112
endif
1213

1314
CFLAGS_REMOVE_return_address.o = -pg
@@ -38,7 +39,7 @@ obj-$(CONFIG_HAVE_ARM_TWD) += smp_twd.o
3839
obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o insn.o
3940
obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o insn.o
4041
obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
41-
obj-$(CONFIG_KPROBES) += kprobes.o kprobes-common.o
42+
obj-$(CONFIG_KPROBES) += kprobes.o kprobes-common.o patch.o
4243
ifdef CONFIG_THUMB2_KERNEL
4344
obj-$(CONFIG_KPROBES) += kprobes-thumb.o
4445
else

‎arch/arm/kernel/kprobes.c‎

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <asm/cacheflush.h>
3030

3131
#include "kprobes.h"
32+
#include "patch.h"
3233

3334
#define MIN_STACK_SIZE(addr) \
3435
min((unsigned long)MAX_STACK_SIZE, \
@@ -103,57 +104,33 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
103104
return 0;
104105
}
105106

106-
#ifdef CONFIG_THUMB2_KERNEL
107-
108-
/*
109-
* For a 32-bit Thumb breakpoint spanning two memory words we need to take
110-
* special precautions to insert the breakpoint atomically, especially on SMP
111-
* systems. This is achieved by calling this arming function using stop_machine.
112-
*/
113-
static int __kprobes set_t32_breakpoint(void *addr)
114-
{
115-
((u16 *)addr)[0] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION >> 16;
116-
((u16 *)addr)[1] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION & 0xffff;
117-
flush_insns(addr, 2*sizeof(u16));
118-
return 0;
119-
}
120-
121107
void __kprobes arch_arm_kprobe(struct kprobe *p)
122108
{
123-
uintptr_t addr = (uintptr_t)p->addr & ~1; /* Remove any Thumb flag */
124-
125-
if (!is_wide_instruction(p->opcode)) {
126-
*(u16 *)addr = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
127-
flush_insns(addr, sizeof(u16));
128-
} else if (addr & 2) {
129-
/* A 32-bit instruction spanning two words needs special care */
130-
stop_machine(set_t32_breakpoint, (void *)addr, &cpu_online_map);
109+
unsigned int brkp;
110+
void *addr;
111+
112+
if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
113+
/* Remove any Thumb flag */
114+
addr = (void *)((uintptr_t)p->addr & ~1);
115+
116+
if (is_wide_instruction(p->opcode))
117+
brkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
118+
else
119+
brkp = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
131120
} else {
132-
/* Word aligned 32-bit instruction can be written atomically */
133-
u32 bkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
134-
#ifndef __ARMEB__ /* Swap halfwords for little-endian */
135-
bkp = (bkp >> 16) | (bkp << 16);
136-
#endif
137-
*(u32 *)addr = bkp;
138-
flush_insns(addr, sizeof(u32));
139-
}
140-
}
121+
kprobe_opcode_t insn = p->opcode;
141122

142-
#else /* !CONFIG_THUMB2_KERNEL */
123+
addr = p->addr;
124+
brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
143125

144-
void __kprobes arch_arm_kprobe(struct kprobe *p)
145-
{
146-
kprobe_opcode_t insn = p->opcode;
147-
kprobe_opcode_t brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
148-
if (insn >= 0xe0000000)
149-
brkp |= 0xe0000000; /* Unconditional instruction */
150-
else
151-
brkp |= insn & 0xf0000000; /* Copy condition from insn */
152-
*p->addr = brkp;
153-
flush_insns(p->addr, sizeof(p->addr[0]));
154-
}
126+
if (insn >= 0xe0000000)
127+
brkp |= 0xe0000000; /* Unconditional instruction */
128+
else
129+
brkp |= insn & 0xf0000000; /* Copy condition from insn */
130+
}
155131

156-
#endif /* !CONFIG_THUMB2_KERNEL */
132+
patch_text(addr, brkp);
133+
}
157134

158135
/*
159136
* The actual disarming is done here on each CPU and synchronized using
@@ -166,25 +143,10 @@ void __kprobes arch_arm_kprobe(struct kprobe *p)
166143
int __kprobes __arch_disarm_kprobe(void *p)
167144
{
168145
struct kprobe *kp = p;
169-
#ifdef CONFIG_THUMB2_KERNEL
170-
u16 *addr = (u16 *)((uintptr_t)kp->addr & ~1);
171-
kprobe_opcode_t insn = kp->opcode;
172-
unsigned int len;
146+
void *addr = (void *)((uintptr_t)kp->addr & ~1);
173147

174-
if (is_wide_instruction(insn)) {
175-
((u16 *)addr)[0] = insn>>16;
176-
((u16 *)addr)[1] = insn;
177-
len = 2*sizeof(u16);
178-
} else {
179-
((u16 *)addr)[0] = insn;
180-
len = sizeof(u16);
181-
}
182-
flush_insns(addr, len);
148+
__patch_text(addr, kp->opcode);
183149

184-
#else /* !CONFIG_THUMB2_KERNEL */
185-
*kp->addr = kp->opcode;
186-
flush_insns(kp->addr, sizeof(kp->addr[0]));
187-
#endif
188150
return 0;
189151
}
190152

‎arch/arm/kernel/patch.c‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <linux/kernel.h>
2+
#include <linux/kprobes.h>
3+
#include <linux/stop_machine.h>
4+
5+
#include <asm/cacheflush.h>
6+
#include <asm/smp_plat.h>
7+
#include <asm/opcodes.h>
8+
9+
#include "patch.h"
10+
11+
struct patch {
12+
void *addr;
13+
unsigned int insn;
14+
};
15+
16+
void __kprobes __patch_text(void *addr, unsigned int insn)
17+
{
18+
bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL);
19+
int size;
20+
21+
if (thumb2 && __opcode_is_thumb16(insn)) {
22+
*(u16 *)addr = __opcode_to_mem_thumb16(insn);
23+
size = sizeof(u16);
24+
} else if (thumb2 && ((uintptr_t)addr & 2)) {
25+
u16 first = __opcode_thumb32_first(insn);
26+
u16 second = __opcode_thumb32_second(insn);
27+
u16 *addrh = addr;
28+
29+
addrh[0] = __opcode_to_mem_thumb16(first);
30+
addrh[1] = __opcode_to_mem_thumb16(second);
31+
32+
size = sizeof(u32);
33+
} else {
34+
if (thumb2)
35+
insn = __opcode_to_mem_thumb32(insn);
36+
else
37+
insn = __opcode_to_mem_arm(insn);
38+
39+
*(u32 *)addr = insn;
40+
size = sizeof(u32);
41+
}
42+
43+
flush_icache_range((uintptr_t)(addr),
44+
(uintptr_t)(addr) + size);
45+
}
46+
47+
static int __kprobes patch_text_stop_machine(void *data)
48+
{
49+
struct patch *patch = data;
50+
51+
__patch_text(patch->addr, patch->insn);
52+
53+
return 0;
54+
}
55+
56+
void __kprobes patch_text(void *addr, unsigned int insn)
57+
{
58+
struct patch patch = {
59+
.addr = addr,
60+
.insn = insn,
61+
};
62+
63+
if (cache_ops_need_broadcast()) {
64+
stop_machine(patch_text_stop_machine, &patch, cpu_online_mask);
65+
} else {
66+
bool straddles_word = IS_ENABLED(CONFIG_THUMB2_KERNEL)
67+
&& __opcode_is_thumb32(insn)
68+
&& ((uintptr_t)addr & 2);
69+
70+
if (straddles_word)
71+
stop_machine(patch_text_stop_machine, &patch, NULL);
72+
else
73+
__patch_text(addr, insn);
74+
}
75+
}

‎arch/arm/kernel/patch.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _ARM_KERNEL_PATCH_H
2+
#define _ARM_KERNEL_PATCH_H
3+
4+
void patch_text(void *addr, unsigned int insn);
5+
void __patch_text(void *addr, unsigned int insn);
6+
7+
#endif

0 commit comments

Comments
 (0)