ARM: kprobes: Decode 16-bit Thumb load and store instructions
[deliverable/linux.git] / arch / arm / kernel / kprobes-thumb.c
CommitLineData
24371707
JM
1/*
2 * arch/arm/kernel/kprobes-thumb.c
3 *
4 * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/kernel.h>
12#include <linux/kprobes.h>
13
14#include "kprobes.h"
15
eaf4f33f
JM
16
17/*
18 * True if current instruction is in an IT block.
19 */
20#define in_it_block(cpsr) ((cpsr & 0x06000c00) != 0x00000000)
21
22/*
23 * Return the condition code to check for the currently executing instruction.
24 * This is in ITSTATE<7:4> which is in CPSR<15:12> but is only valid if
25 * in_it_block returns true.
26 */
27#define current_cond(cpsr) ((cpsr >> 12) & 0xf)
28
a9c3c29e
JM
29/*
30 * Return the PC value for a probe in thumb code.
31 * This is the address of the probed instruction plus 4.
32 * We subtract one because the address will have bit zero set to indicate
33 * a pointer to thumb code.
34 */
35static inline unsigned long __kprobes thumb_probe_pc(struct kprobe *p)
36{
37 return (unsigned long)p->addr - 1 + 4;
38}
39
40static void __kprobes
41t16_simulate_bxblx(struct kprobe *p, struct pt_regs *regs)
42{
43 kprobe_opcode_t insn = p->opcode;
44 unsigned long pc = thumb_probe_pc(p);
45 int rm = (insn >> 3) & 0xf;
46 unsigned long rmv = (rm == 15) ? pc : regs->uregs[rm];
47
48 if (insn & (1 << 7)) /* BLX ? */
49 regs->ARM_lr = (unsigned long)p->addr + 2;
50
51 bx_write_pc(rmv, regs);
52}
53
f8695142
JM
54static void __kprobes
55t16_simulate_ldr_literal(struct kprobe *p, struct pt_regs *regs)
56{
57 kprobe_opcode_t insn = p->opcode;
58 unsigned long* base = (unsigned long *)(thumb_probe_pc(p) & ~3);
59 long index = insn & 0xff;
60 int rt = (insn >> 8) & 0x7;
61 regs->uregs[rt] = base[index];
62}
63
64static void __kprobes
65t16_simulate_ldrstr_sp_relative(struct kprobe *p, struct pt_regs *regs)
66{
67 kprobe_opcode_t insn = p->opcode;
68 unsigned long* base = (unsigned long *)regs->ARM_sp;
69 long index = insn & 0xff;
70 int rt = (insn >> 8) & 0x7;
71 if (insn & 0x800) /* LDR */
72 regs->uregs[rt] = base[index];
73 else /* STR */
74 base[index] = regs->uregs[rt];
75}
76
02d194f6
JM
77static unsigned long __kprobes
78t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs)
79{
80 unsigned long oldcpsr = regs->ARM_cpsr;
81 unsigned long newcpsr;
82
83 __asm__ __volatile__ (
84 "msr cpsr_fs, %[oldcpsr] \n\t"
85 "ldmia %[regs], {r0-r7} \n\t"
86 "blx %[fn] \n\t"
87 "stmia %[regs], {r0-r7} \n\t"
88 "mrs %[newcpsr], cpsr \n\t"
89 : [newcpsr] "=r" (newcpsr)
90 : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs),
91 [fn] "r" (p->ainsn.insn_fn)
92 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
93 "lr", "memory", "cc"
94 );
95
96 return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK);
97}
98
99static void __kprobes
100t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs)
101{
102 regs->ARM_cpsr = t16_emulate_loregs(p, regs);
103}
104
105static void __kprobes
106t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs)
107{
108 unsigned long cpsr = t16_emulate_loregs(p, regs);
109 if (!in_it_block(cpsr))
110 regs->ARM_cpsr = cpsr;
111}
112
3b5940e8
JM
113static void __kprobes
114t16_emulate_hiregs(struct kprobe *p, struct pt_regs *regs)
115{
116 kprobe_opcode_t insn = p->opcode;
117 unsigned long pc = thumb_probe_pc(p);
118 int rdn = (insn & 0x7) | ((insn & 0x80) >> 4);
119 int rm = (insn >> 3) & 0xf;
120
121 register unsigned long rdnv asm("r1");
122 register unsigned long rmv asm("r0");
123 unsigned long cpsr = regs->ARM_cpsr;
124
125 rdnv = (rdn == 15) ? pc : regs->uregs[rdn];
126 rmv = (rm == 15) ? pc : regs->uregs[rm];
127
128 __asm__ __volatile__ (
129 "msr cpsr_fs, %[cpsr] \n\t"
130 "blx %[fn] \n\t"
131 "mrs %[cpsr], cpsr \n\t"
132 : "=r" (rdnv), [cpsr] "=r" (cpsr)
133 : "0" (rdnv), "r" (rmv), "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
134 : "lr", "memory", "cc"
135 );
136
137 if (rdn == 15)
138 rdnv &= ~1;
139
140 regs->uregs[rdn] = rdnv;
141 regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
142}
143
144static enum kprobe_insn __kprobes
145t16_decode_hiregs(kprobe_opcode_t insn, struct arch_specific_insn *asi)
146{
147 insn &= ~0x00ff;
148 insn |= 0x001; /* Set Rdn = R1 and Rm = R0 */
149 ((u16 *)asi->insn)[0] = insn;
150 asi->insn_handler = t16_emulate_hiregs;
151 return INSN_GOOD;
152}
153
3f92dfed
JM
154static const union decode_item t16_table_1011[] = {
155 /* Miscellaneous 16-bit instructions */
156
157 /*
158 * If-Then, and hints
159 * 1011 1111 xxxx xxxx
160 */
161
162 /* YIELD 1011 1111 0001 0000 */
163 DECODE_OR (0xffff, 0xbf10),
164 /* SEV 1011 1111 0100 0000 */
165 DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none),
166 /* NOP 1011 1111 0000 0000 */
167 /* WFE 1011 1111 0010 0000 */
168 /* WFI 1011 1111 0011 0000 */
169 DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop),
170 /* Unassigned hints 1011 1111 xxxx 0000 */
171 DECODE_REJECT (0xff0f, 0xbf00),
172
173 DECODE_END
174};
175
176const union decode_item kprobe_decode_thumb16_table[] = {
177
02d194f6
JM
178 /*
179 * Shift (immediate), add, subtract, move, and compare
180 * 00xx xxxx xxxx xxxx
181 */
182
183 /* CMP (immediate) 0010 1xxx xxxx xxxx */
184 DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags),
185
186 /* ADD (register) 0001 100x xxxx xxxx */
187 /* SUB (register) 0001 101x xxxx xxxx */
188 /* LSL (immediate) 0000 0xxx xxxx xxxx */
189 /* LSR (immediate) 0000 1xxx xxxx xxxx */
190 /* ASR (immediate) 0001 0xxx xxxx xxxx */
191 /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */
192 /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */
193 /* MOV (immediate) 0010 0xxx xxxx xxxx */
194 /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */
195 /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */
196 DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags),
197
198 /*
199 * 16-bit Thumb data-processing instructions
200 * 0100 00xx xxxx xxxx
201 */
202
203 /* TST (register) 0100 0010 00xx xxxx */
204 DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags),
205 /* CMP (register) 0100 0010 10xx xxxx */
206 /* CMN (register) 0100 0010 11xx xxxx */
207 DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags),
208 /* AND (register) 0100 0000 00xx xxxx */
209 /* EOR (register) 0100 0000 01xx xxxx */
210 /* LSL (register) 0100 0000 10xx xxxx */
211 /* LSR (register) 0100 0000 11xx xxxx */
212 /* ASR (register) 0100 0001 00xx xxxx */
213 /* ADC (register) 0100 0001 01xx xxxx */
214 /* SBC (register) 0100 0001 10xx xxxx */
215 /* ROR (register) 0100 0001 11xx xxxx */
216 /* RSB (immediate) 0100 0010 01xx xxxx */
217 /* ORR (register) 0100 0011 00xx xxxx */
218 /* MUL 0100 0011 00xx xxxx */
219 /* BIC (register) 0100 0011 10xx xxxx */
220 /* MVN (register) 0100 0011 10xx xxxx */
221 DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags),
222
a9c3c29e
JM
223 /*
224 * Special data instructions and branch and exchange
225 * 0100 01xx xxxx xxxx
226 */
227
228 /* BLX pc 0100 0111 1111 1xxx */
229 DECODE_REJECT (0xfff8, 0x47f8),
230
231 /* BX (register) 0100 0111 0xxx xxxx */
232 /* BLX (register) 0100 0111 1xxx xxxx */
233 DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx),
234
3b5940e8
JM
235 /* ADD pc, pc 0100 0100 1111 1111 */
236 DECODE_REJECT (0xffff, 0x44ff),
237
238 /* ADD (register) 0100 0100 xxxx xxxx */
239 /* CMP (register) 0100 0101 xxxx xxxx */
240 /* MOV (register) 0100 0110 xxxx xxxx */
241 DECODE_CUSTOM (0xfc00, 0x4400, t16_decode_hiregs),
242
f8695142
JM
243 /*
244 * Load from Literal Pool
245 * LDR (literal) 0100 1xxx xxxx xxxx
246 */
247 DECODE_SIMULATE (0xf800, 0x4800, t16_simulate_ldr_literal),
248
249 /*
250 * 16-bit Thumb Load/store instructions
251 * 0101 xxxx xxxx xxxx
252 * 011x xxxx xxxx xxxx
253 * 100x xxxx xxxx xxxx
254 */
255
256 /* STR (register) 0101 000x xxxx xxxx */
257 /* STRH (register) 0101 001x xxxx xxxx */
258 /* STRB (register) 0101 010x xxxx xxxx */
259 /* LDRSB (register) 0101 011x xxxx xxxx */
260 /* LDR (register) 0101 100x xxxx xxxx */
261 /* LDRH (register) 0101 101x xxxx xxxx */
262 /* LDRB (register) 0101 110x xxxx xxxx */
263 /* LDRSH (register) 0101 111x xxxx xxxx */
264 /* STR (immediate, Thumb) 0110 0xxx xxxx xxxx */
265 /* LDR (immediate, Thumb) 0110 1xxx xxxx xxxx */
266 /* STRB (immediate, Thumb) 0111 0xxx xxxx xxxx */
267 /* LDRB (immediate, Thumb) 0111 1xxx xxxx xxxx */
268 DECODE_EMULATE (0xc000, 0x4000, t16_emulate_loregs_rwflags),
269 /* STRH (immediate, Thumb) 1000 0xxx xxxx xxxx */
270 /* LDRH (immediate, Thumb) 1000 1xxx xxxx xxxx */
271 DECODE_EMULATE (0xf000, 0x8000, t16_emulate_loregs_rwflags),
272 /* STR (immediate, Thumb) 1001 0xxx xxxx xxxx */
273 /* LDR (immediate, Thumb) 1001 1xxx xxxx xxxx */
274 DECODE_SIMULATE (0xf000, 0x9000, t16_simulate_ldrstr_sp_relative),
275
3f92dfed
JM
276 /*
277 * Miscellaneous 16-bit instructions
278 * 1011 xxxx xxxx xxxx
279 */
280 DECODE_TABLE (0xf000, 0xb000, t16_table_1011),
281
f8695142
JM
282 /* STM 1100 0xxx xxxx xxxx */
283 /* LDM 1100 1xxx xxxx xxxx */
284 DECODE_EMULATE (0xf000, 0xc000, t16_emulate_loregs_rwflags),
285
3f92dfed
JM
286 DECODE_END
287};
288
eaf4f33f
JM
289static unsigned long __kprobes thumb_check_cc(unsigned long cpsr)
290{
291 if (unlikely(in_it_block(cpsr)))
292 return kprobe_condition_checks[current_cond(cpsr)](cpsr);
293 return true;
294}
295
c6a7d97d
JM
296static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs)
297{
298 regs->ARM_pc += 2;
299 p->ainsn.insn_handler(p, regs);
300 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
301}
302
303static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs)
304{
305 regs->ARM_pc += 4;
306 p->ainsn.insn_handler(p, regs);
307 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
308}
309
24371707
JM
310enum kprobe_insn __kprobes
311thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
312{
c6a7d97d 313 asi->insn_singlestep = thumb16_singlestep;
eaf4f33f 314 asi->insn_check_cc = thumb_check_cc;
3f92dfed 315 return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true);
24371707
JM
316}
317
318enum kprobe_insn __kprobes
319thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
320{
c6a7d97d 321 asi->insn_singlestep = thumb32_singlestep;
eaf4f33f 322 asi->insn_check_cc = thumb_check_cc;
24371707
JM
323 return INSN_REJECTED;
324}
This page took 0.125759 seconds and 5 git commands to generate.