blob: cf834484fb58cfea9e86502aa7ffb2d99bc06cf5 [file] [log] [blame]
Jon Medhurst24371702011-04-19 17:56:58 +01001/*
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
Jon Medhursteaf4f33f2011-04-20 19:29:52 +010016
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
Jon Medhursta9c3c292011-07-02 15:51:03 +010029/*
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
Jon Medhurstdd212bd2011-07-03 14:26:16 +010040static void __kprobes
41t32_simulate_table_branch(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 rn = (insn >> 16) & 0xf;
46 int rm = insn & 0xf;
47
48 unsigned long rnv = (rn == 15) ? pc : regs->uregs[rn];
49 unsigned long rmv = regs->uregs[rm];
50 unsigned int halfwords;
51
52 if (insn & 0x10)
53 halfwords = ((u16 *)rnv)[rmv];
54 else
55 halfwords = ((u8 *)rnv)[rmv];
56
57 regs->ARM_pc = pc + 2 * halfwords;
58}
59
Jon Medhursteaf1d062011-07-07 08:59:32 +010060static enum kprobe_insn __kprobes
61t32_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi)
62{
63 enum kprobe_insn ret = kprobe_decode_ldmstm(insn, asi);
64
65 /* Fixup modified instruction to have halfwords in correct order...*/
66 insn = asi->insn[0];
67 ((u16 *)asi->insn)[0] = insn >> 16;
68 ((u16 *)asi->insn)[1] = insn & 0xffff;
69
70 return ret;
71}
72
Jon Medhurstb48354d2011-07-03 14:23:21 +010073static void __kprobes
74t32_emulate_ldrdstrd(struct kprobe *p, struct pt_regs *regs)
75{
76 kprobe_opcode_t insn = p->opcode;
77 unsigned long pc = thumb_probe_pc(p) & ~3;
78 int rt1 = (insn >> 12) & 0xf;
79 int rt2 = (insn >> 8) & 0xf;
80 int rn = (insn >> 16) & 0xf;
81
82 register unsigned long rt1v asm("r0") = regs->uregs[rt1];
83 register unsigned long rt2v asm("r1") = regs->uregs[rt2];
84 register unsigned long rnv asm("r2") = (rn == 15) ? pc
85 : regs->uregs[rn];
86
87 __asm__ __volatile__ (
88 "blx %[fn]"
89 : "=r" (rt1v), "=r" (rt2v), "=r" (rnv)
90 : "0" (rt1v), "1" (rt2v), "2" (rnv), [fn] "r" (p->ainsn.insn_fn)
91 : "lr", "memory", "cc"
92 );
93
94 if (rn != 15)
95 regs->uregs[rn] = rnv; /* Writeback base register */
96 regs->uregs[rt1] = rt1v;
97 regs->uregs[rt2] = rt2v;
98}
99
Jon Medhurst080e0012011-07-03 14:31:58 +0100100static void __kprobes
101t32_emulate_rd8rn16rm0_rwflags(struct kprobe *p, struct pt_regs *regs)
102{
103 kprobe_opcode_t insn = p->opcode;
104 int rd = (insn >> 8) & 0xf;
105 int rn = (insn >> 16) & 0xf;
106 int rm = insn & 0xf;
107
108 register unsigned long rdv asm("r1") = regs->uregs[rd];
109 register unsigned long rnv asm("r2") = regs->uregs[rn];
110 register unsigned long rmv asm("r3") = regs->uregs[rm];
111 unsigned long cpsr = regs->ARM_cpsr;
112
113 __asm__ __volatile__ (
114 "msr cpsr_fs, %[cpsr] \n\t"
115 "blx %[fn] \n\t"
116 "mrs %[cpsr], cpsr \n\t"
117 : "=r" (rdv), [cpsr] "=r" (cpsr)
118 : "0" (rdv), "r" (rnv), "r" (rmv),
119 "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
120 : "lr", "memory", "cc"
121 );
122
123 regs->uregs[rd] = rdv;
124 regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
125}
126
Jon Medhursteaf1d062011-07-07 08:59:32 +0100127static const union decode_item t32_table_1110_100x_x0xx[] = {
128 /* Load/store multiple instructions */
129
130 /* Rn is PC 1110 100x x0xx 1111 xxxx xxxx xxxx xxxx */
131 DECODE_REJECT (0xfe4f0000, 0xe80f0000),
132
133 /* SRS 1110 1000 00x0 xxxx xxxx xxxx xxxx xxxx */
134 /* RFE 1110 1000 00x1 xxxx xxxx xxxx xxxx xxxx */
135 DECODE_REJECT (0xffc00000, 0xe8000000),
136 /* SRS 1110 1001 10x0 xxxx xxxx xxxx xxxx xxxx */
137 /* RFE 1110 1001 10x1 xxxx xxxx xxxx xxxx xxxx */
138 DECODE_REJECT (0xffc00000, 0xe9800000),
139
140 /* STM Rn, {...pc} 1110 100x x0x0 xxxx 1xxx xxxx xxxx xxxx */
141 DECODE_REJECT (0xfe508000, 0xe8008000),
142 /* LDM Rn, {...lr,pc} 1110 100x x0x1 xxxx 11xx xxxx xxxx xxxx */
143 DECODE_REJECT (0xfe50c000, 0xe810c000),
144 /* LDM/STM Rn, {...sp} 1110 100x x0xx xxxx xx1x xxxx xxxx xxxx */
145 DECODE_REJECT (0xfe402000, 0xe8002000),
146
147 /* STMIA 1110 1000 10x0 xxxx xxxx xxxx xxxx xxxx */
148 /* LDMIA 1110 1000 10x1 xxxx xxxx xxxx xxxx xxxx */
149 /* STMDB 1110 1001 00x0 xxxx xxxx xxxx xxxx xxxx */
150 /* LDMDB 1110 1001 00x1 xxxx xxxx xxxx xxxx xxxx */
151 DECODE_CUSTOM (0xfe400000, 0xe8000000, t32_decode_ldmstm),
152
153 DECODE_END
154};
155
Jon Medhurstb48354d2011-07-03 14:23:21 +0100156static const union decode_item t32_table_1110_100x_x1xx[] = {
157 /* Load/store dual, load/store exclusive, table branch */
158
159 /* STRD (immediate) 1110 1000 x110 xxxx xxxx xxxx xxxx xxxx */
160 /* LDRD (immediate) 1110 1000 x111 xxxx xxxx xxxx xxxx xxxx */
161 DECODE_OR (0xff600000, 0xe8600000),
162 /* STRD (immediate) 1110 1001 x1x0 xxxx xxxx xxxx xxxx xxxx */
163 /* LDRD (immediate) 1110 1001 x1x1 xxxx xxxx xxxx xxxx xxxx */
164 DECODE_EMULATEX (0xff400000, 0xe9400000, t32_emulate_ldrdstrd,
165 REGS(NOPCWB, NOSPPC, NOSPPC, 0, 0)),
166
Jon Medhurstdd212bd2011-07-03 14:26:16 +0100167 /* TBB 1110 1000 1101 xxxx xxxx xxxx 0000 xxxx */
168 /* TBH 1110 1000 1101 xxxx xxxx xxxx 0001 xxxx */
169 DECODE_SIMULATEX(0xfff000e0, 0xe8d00000, t32_simulate_table_branch,
170 REGS(NOSP, 0, 0, 0, NOSPPC)),
171
Jon Medhurstb48354d2011-07-03 14:23:21 +0100172 /* STREX 1110 1000 0100 xxxx xxxx xxxx xxxx xxxx */
173 /* LDREX 1110 1000 0101 xxxx xxxx xxxx xxxx xxxx */
174 /* STREXB 1110 1000 1100 xxxx xxxx xxxx 0100 xxxx */
175 /* STREXH 1110 1000 1100 xxxx xxxx xxxx 0101 xxxx */
176 /* STREXD 1110 1000 1100 xxxx xxxx xxxx 0111 xxxx */
177 /* LDREXB 1110 1000 1101 xxxx xxxx xxxx 0100 xxxx */
178 /* LDREXH 1110 1000 1101 xxxx xxxx xxxx 0101 xxxx */
179 /* LDREXD 1110 1000 1101 xxxx xxxx xxxx 0111 xxxx */
180 /* And unallocated instructions... */
181 DECODE_END
182};
183
Jon Medhurst080e0012011-07-03 14:31:58 +0100184static const union decode_item t32_table_1110_101x[] = {
185 /* Data-processing (shifted register) */
186
187 /* TST 1110 1010 0001 xxxx xxxx 1111 xxxx xxxx */
188 /* TEQ 1110 1010 1001 xxxx xxxx 1111 xxxx xxxx */
189 DECODE_EMULATEX (0xff700f00, 0xea100f00, t32_emulate_rd8rn16rm0_rwflags,
190 REGS(NOSPPC, 0, 0, 0, NOSPPC)),
191
192 /* CMN 1110 1011 0001 xxxx xxxx 1111 xxxx xxxx */
193 DECODE_OR (0xfff00f00, 0xeb100f00),
194 /* CMP 1110 1011 1011 xxxx xxxx 1111 xxxx xxxx */
195 DECODE_EMULATEX (0xfff00f00, 0xebb00f00, t32_emulate_rd8rn16rm0_rwflags,
196 REGS(NOPC, 0, 0, 0, NOSPPC)),
197
198 /* MOV 1110 1010 010x 1111 xxxx xxxx xxxx xxxx */
199 /* MVN 1110 1010 011x 1111 xxxx xxxx xxxx xxxx */
200 DECODE_EMULATEX (0xffcf0000, 0xea4f0000, t32_emulate_rd8rn16rm0_rwflags,
201 REGS(0, 0, NOSPPC, 0, NOSPPC)),
202
203 /* ??? 1110 1010 101x xxxx xxxx xxxx xxxx xxxx */
204 /* ??? 1110 1010 111x xxxx xxxx xxxx xxxx xxxx */
205 DECODE_REJECT (0xffa00000, 0xeaa00000),
206 /* ??? 1110 1011 001x xxxx xxxx xxxx xxxx xxxx */
207 DECODE_REJECT (0xffe00000, 0xeb200000),
208 /* ??? 1110 1011 100x xxxx xxxx xxxx xxxx xxxx */
209 DECODE_REJECT (0xffe00000, 0xeb800000),
210 /* ??? 1110 1011 111x xxxx xxxx xxxx xxxx xxxx */
211 DECODE_REJECT (0xffe00000, 0xebe00000),
212
213 /* ADD/SUB SP, SP, Rm, LSL #0..3 */
214 /* 1110 1011 x0xx 1101 x000 1101 xx00 xxxx */
215 DECODE_EMULATEX (0xff4f7f30, 0xeb0d0d00, t32_emulate_rd8rn16rm0_rwflags,
216 REGS(SP, 0, SP, 0, NOSPPC)),
217
218 /* ADD/SUB SP, SP, Rm, shift */
219 /* 1110 1011 x0xx 1101 xxxx 1101 xxxx xxxx */
220 DECODE_REJECT (0xff4f0f00, 0xeb0d0d00),
221
222 /* ADD/SUB Rd, SP, Rm, shift */
223 /* 1110 1011 x0xx 1101 xxxx xxxx xxxx xxxx */
224 DECODE_EMULATEX (0xff4f0000, 0xeb0d0000, t32_emulate_rd8rn16rm0_rwflags,
225 REGS(SP, 0, NOPC, 0, NOSPPC)),
226
227 /* AND 1110 1010 000x xxxx xxxx xxxx xxxx xxxx */
228 /* BIC 1110 1010 001x xxxx xxxx xxxx xxxx xxxx */
229 /* ORR 1110 1010 010x xxxx xxxx xxxx xxxx xxxx */
230 /* ORN 1110 1010 011x xxxx xxxx xxxx xxxx xxxx */
231 /* EOR 1110 1010 100x xxxx xxxx xxxx xxxx xxxx */
232 /* PKH 1110 1010 110x xxxx xxxx xxxx xxxx xxxx */
233 /* ADD 1110 1011 000x xxxx xxxx xxxx xxxx xxxx */
234 /* ADC 1110 1011 010x xxxx xxxx xxxx xxxx xxxx */
235 /* SBC 1110 1011 011x xxxx xxxx xxxx xxxx xxxx */
236 /* SUB 1110 1011 101x xxxx xxxx xxxx xxxx xxxx */
237 /* RSB 1110 1011 110x xxxx xxxx xxxx xxxx xxxx */
238 DECODE_EMULATEX (0xfe000000, 0xea000000, t32_emulate_rd8rn16rm0_rwflags,
239 REGS(NOSPPC, 0, NOSPPC, 0, NOSPPC)),
240
241 DECODE_END
242};
243
Jon Medhurst2fcaf7e2011-07-03 14:36:35 +0100244static const union decode_item t32_table_1111_0x0x___0[] = {
245 /* Data-processing (modified immediate) */
246
247 /* TST 1111 0x00 0001 xxxx 0xxx 1111 xxxx xxxx */
248 /* TEQ 1111 0x00 1001 xxxx 0xxx 1111 xxxx xxxx */
249 DECODE_EMULATEX (0xfb708f00, 0xf0100f00, t32_emulate_rd8rn16rm0_rwflags,
250 REGS(NOSPPC, 0, 0, 0, 0)),
251
252 /* CMN 1111 0x01 0001 xxxx 0xxx 1111 xxxx xxxx */
253 DECODE_OR (0xfbf08f00, 0xf1100f00),
254 /* CMP 1111 0x01 1011 xxxx 0xxx 1111 xxxx xxxx */
255 DECODE_EMULATEX (0xfbf08f00, 0xf1b00f00, t32_emulate_rd8rn16rm0_rwflags,
256 REGS(NOPC, 0, 0, 0, 0)),
257
258 /* MOV 1111 0x00 010x 1111 0xxx xxxx xxxx xxxx */
259 /* MVN 1111 0x00 011x 1111 0xxx xxxx xxxx xxxx */
260 DECODE_EMULATEX (0xfbcf8000, 0xf04f0000, t32_emulate_rd8rn16rm0_rwflags,
261 REGS(0, 0, NOSPPC, 0, 0)),
262
263 /* ??? 1111 0x00 101x xxxx 0xxx xxxx xxxx xxxx */
264 DECODE_REJECT (0xfbe08000, 0xf0a00000),
265 /* ??? 1111 0x00 110x xxxx 0xxx xxxx xxxx xxxx */
266 /* ??? 1111 0x00 111x xxxx 0xxx xxxx xxxx xxxx */
267 DECODE_REJECT (0xfbc08000, 0xf0c00000),
268 /* ??? 1111 0x01 001x xxxx 0xxx xxxx xxxx xxxx */
269 DECODE_REJECT (0xfbe08000, 0xf1200000),
270 /* ??? 1111 0x01 100x xxxx 0xxx xxxx xxxx xxxx */
271 DECODE_REJECT (0xfbe08000, 0xf1800000),
272 /* ??? 1111 0x01 111x xxxx 0xxx xxxx xxxx xxxx */
273 DECODE_REJECT (0xfbe08000, 0xf1e00000),
274
275 /* ADD Rd, SP, #imm 1111 0x01 000x 1101 0xxx xxxx xxxx xxxx */
276 /* SUB Rd, SP, #imm 1111 0x01 101x 1101 0xxx xxxx xxxx xxxx */
277 DECODE_EMULATEX (0xfb4f8000, 0xf10d0000, t32_emulate_rd8rn16rm0_rwflags,
278 REGS(SP, 0, NOPC, 0, 0)),
279
280 /* AND 1111 0x00 000x xxxx 0xxx xxxx xxxx xxxx */
281 /* BIC 1111 0x00 001x xxxx 0xxx xxxx xxxx xxxx */
282 /* ORR 1111 0x00 010x xxxx 0xxx xxxx xxxx xxxx */
283 /* ORN 1111 0x00 011x xxxx 0xxx xxxx xxxx xxxx */
284 /* EOR 1111 0x00 100x xxxx 0xxx xxxx xxxx xxxx */
285 /* ADD 1111 0x01 000x xxxx 0xxx xxxx xxxx xxxx */
286 /* ADC 1111 0x01 010x xxxx 0xxx xxxx xxxx xxxx */
287 /* SBC 1111 0x01 011x xxxx 0xxx xxxx xxxx xxxx */
288 /* SUB 1111 0x01 101x xxxx 0xxx xxxx xxxx xxxx */
289 /* RSB 1111 0x01 110x xxxx 0xxx xxxx xxxx xxxx */
290 DECODE_EMULATEX (0xfa008000, 0xf0000000, t32_emulate_rd8rn16rm0_rwflags,
291 REGS(NOSPPC, 0, NOSPPC, 0, 0)),
292
293 DECODE_END
294};
295
Jon Medhurstf39ca8b2011-07-03 13:55:47 +0100296static const union decode_item t32_table_1111_0xxx___1[] = {
297 /* Branches and miscellaneous control */
298
299 /* YIELD 1111 0011 1010 xxxx 10x0 x000 0000 0001 */
300 DECODE_OR (0xfff0d7ff, 0xf3a08001),
301 /* SEV 1111 0011 1010 xxxx 10x0 x000 0000 0100 */
302 DECODE_EMULATE (0xfff0d7ff, 0xf3a08004, kprobe_emulate_none),
303 /* NOP 1111 0011 1010 xxxx 10x0 x000 0000 0000 */
304 /* WFE 1111 0011 1010 xxxx 10x0 x000 0000 0010 */
305 /* WFI 1111 0011 1010 xxxx 10x0 x000 0000 0011 */
306 DECODE_SIMULATE (0xfff0d7fc, 0xf3a08000, kprobe_simulate_nop),
307
308 DECODE_END
309};
310
311const union decode_item kprobe_decode_thumb32_table[] = {
312
313 /*
Jon Medhursteaf1d062011-07-07 08:59:32 +0100314 * Load/store multiple instructions
315 * 1110 100x x0xx xxxx xxxx xxxx xxxx xxxx
316 */
317 DECODE_TABLE (0xfe400000, 0xe8000000, t32_table_1110_100x_x0xx),
318
319 /*
Jon Medhurstb48354d2011-07-03 14:23:21 +0100320 * Load/store dual, load/store exclusive, table branch
321 * 1110 100x x1xx xxxx xxxx xxxx xxxx xxxx
322 */
323 DECODE_TABLE (0xfe400000, 0xe8400000, t32_table_1110_100x_x1xx),
324
325 /*
Jon Medhurst080e0012011-07-03 14:31:58 +0100326 * Data-processing (shifted register)
327 * 1110 101x xxxx xxxx xxxx xxxx xxxx xxxx
328 */
329 DECODE_TABLE (0xfe000000, 0xea000000, t32_table_1110_101x),
330
331 /*
Jon Medhurst2fcaf7e2011-07-03 14:36:35 +0100332 * Data-processing (modified immediate)
333 * 1111 0x0x xxxx xxxx 0xxx xxxx xxxx xxxx
334 */
335 DECODE_TABLE (0xfa008000, 0xf0000000, t32_table_1111_0x0x___0),
336
337 /*
Jon Medhurstf39ca8b2011-07-03 13:55:47 +0100338 * Branches and miscellaneous control
339 * 1111 0xxx xxxx xxxx 1xxx xxxx xxxx xxxx
340 */
341 DECODE_TABLE (0xf8008000, 0xf0008000, t32_table_1111_0xxx___1),
342
343 DECODE_END
344};
345
Jon Medhursta9c3c292011-07-02 15:51:03 +0100346static void __kprobes
347t16_simulate_bxblx(struct kprobe *p, struct pt_regs *regs)
348{
349 kprobe_opcode_t insn = p->opcode;
350 unsigned long pc = thumb_probe_pc(p);
351 int rm = (insn >> 3) & 0xf;
352 unsigned long rmv = (rm == 15) ? pc : regs->uregs[rm];
353
354 if (insn & (1 << 7)) /* BLX ? */
355 regs->ARM_lr = (unsigned long)p->addr + 2;
356
357 bx_write_pc(rmv, regs);
358}
359
Jon Medhurstf8695142011-07-02 16:00:09 +0100360static void __kprobes
361t16_simulate_ldr_literal(struct kprobe *p, struct pt_regs *regs)
362{
363 kprobe_opcode_t insn = p->opcode;
364 unsigned long* base = (unsigned long *)(thumb_probe_pc(p) & ~3);
365 long index = insn & 0xff;
366 int rt = (insn >> 8) & 0x7;
367 regs->uregs[rt] = base[index];
368}
369
370static void __kprobes
371t16_simulate_ldrstr_sp_relative(struct kprobe *p, struct pt_regs *regs)
372{
373 kprobe_opcode_t insn = p->opcode;
374 unsigned long* base = (unsigned long *)regs->ARM_sp;
375 long index = insn & 0xff;
376 int rt = (insn >> 8) & 0x7;
377 if (insn & 0x800) /* LDR */
378 regs->uregs[rt] = base[index];
379 else /* STR */
380 base[index] = regs->uregs[rt];
381}
382
Jon Medhurst2f335822011-07-02 16:05:53 +0100383static void __kprobes
384t16_simulate_reladr(struct kprobe *p, struct pt_regs *regs)
385{
386 kprobe_opcode_t insn = p->opcode;
387 unsigned long base = (insn & 0x800) ? regs->ARM_sp
388 : (thumb_probe_pc(p) & ~3);
389 long offset = insn & 0xff;
390 int rt = (insn >> 8) & 0x7;
391 regs->uregs[rt] = base + offset * 4;
392}
393
394static void __kprobes
395t16_simulate_add_sp_imm(struct kprobe *p, struct pt_regs *regs)
396{
397 kprobe_opcode_t insn = p->opcode;
398 long imm = insn & 0x7f;
399 if (insn & 0x80) /* SUB */
400 regs->ARM_sp -= imm * 4;
401 else /* ADD */
402 regs->ARM_sp += imm * 4;
403}
404
Jon Medhurst32818f32011-07-02 16:10:44 +0100405static void __kprobes
406t16_simulate_cbz(struct kprobe *p, struct pt_regs *regs)
407{
408 kprobe_opcode_t insn = p->opcode;
409 int rn = insn & 0x7;
410 kprobe_opcode_t nonzero = regs->uregs[rn] ? insn : ~insn;
411 if (nonzero & 0x800) {
412 long i = insn & 0x200;
413 long imm5 = insn & 0xf8;
414 unsigned long pc = thumb_probe_pc(p);
415 regs->ARM_pc = pc + (i >> 3) + (imm5 >> 2);
416 }
417}
418
Jon Medhurst5b94faf2011-07-02 16:16:05 +0100419static void __kprobes
420t16_simulate_it(struct kprobe *p, struct pt_regs *regs)
421{
422 /*
423 * The 8 IT state bits are split into two parts in CPSR:
424 * ITSTATE<1:0> are in CPSR<26:25>
425 * ITSTATE<7:2> are in CPSR<15:10>
426 * The new IT state is in the lower byte of insn.
427 */
428 kprobe_opcode_t insn = p->opcode;
429 unsigned long cpsr = regs->ARM_cpsr;
430 cpsr &= ~PSR_IT_MASK;
431 cpsr |= (insn & 0xfc) << 8;
432 cpsr |= (insn & 0x03) << 25;
433 regs->ARM_cpsr = cpsr;
434}
435
436static void __kprobes
437t16_singlestep_it(struct kprobe *p, struct pt_regs *regs)
438{
439 regs->ARM_pc += 2;
440 t16_simulate_it(p, regs);
441}
442
443static enum kprobe_insn __kprobes
444t16_decode_it(kprobe_opcode_t insn, struct arch_specific_insn *asi)
445{
446 asi->insn_singlestep = t16_singlestep_it;
447 return INSN_GOOD_NO_SLOT;
448}
449
Jon Medhurst396b41f2011-07-02 16:30:43 +0100450static void __kprobes
451t16_simulate_cond_branch(struct kprobe *p, struct pt_regs *regs)
452{
453 kprobe_opcode_t insn = p->opcode;
454 unsigned long pc = thumb_probe_pc(p);
455 long offset = insn & 0x7f;
456 offset -= insn & 0x80; /* Apply sign bit */
457 regs->ARM_pc = pc + (offset * 2);
458}
459
460static enum kprobe_insn __kprobes
461t16_decode_cond_branch(kprobe_opcode_t insn, struct arch_specific_insn *asi)
462{
463 int cc = (insn >> 8) & 0xf;
464 asi->insn_check_cc = kprobe_condition_checks[cc];
465 asi->insn_handler = t16_simulate_cond_branch;
466 return INSN_GOOD_NO_SLOT;
467}
468
469static void __kprobes
470t16_simulate_branch(struct kprobe *p, struct pt_regs *regs)
471{
472 kprobe_opcode_t insn = p->opcode;
473 unsigned long pc = thumb_probe_pc(p);
474 long offset = insn & 0x3ff;
475 offset -= insn & 0x400; /* Apply sign bit */
476 regs->ARM_pc = pc + (offset * 2);
477}
478
Jon Medhurst02d194f2011-07-02 15:46:05 +0100479static unsigned long __kprobes
480t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs)
481{
482 unsigned long oldcpsr = regs->ARM_cpsr;
483 unsigned long newcpsr;
484
485 __asm__ __volatile__ (
486 "msr cpsr_fs, %[oldcpsr] \n\t"
487 "ldmia %[regs], {r0-r7} \n\t"
488 "blx %[fn] \n\t"
489 "stmia %[regs], {r0-r7} \n\t"
490 "mrs %[newcpsr], cpsr \n\t"
491 : [newcpsr] "=r" (newcpsr)
492 : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs),
493 [fn] "r" (p->ainsn.insn_fn)
494 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
495 "lr", "memory", "cc"
496 );
497
498 return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK);
499}
500
501static void __kprobes
502t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs)
503{
504 regs->ARM_cpsr = t16_emulate_loregs(p, regs);
505}
506
507static void __kprobes
508t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs)
509{
510 unsigned long cpsr = t16_emulate_loregs(p, regs);
511 if (!in_it_block(cpsr))
512 regs->ARM_cpsr = cpsr;
513}
514
Jon Medhurst3b5940e2011-07-02 15:54:57 +0100515static void __kprobes
516t16_emulate_hiregs(struct kprobe *p, struct pt_regs *regs)
517{
518 kprobe_opcode_t insn = p->opcode;
519 unsigned long pc = thumb_probe_pc(p);
520 int rdn = (insn & 0x7) | ((insn & 0x80) >> 4);
521 int rm = (insn >> 3) & 0xf;
522
523 register unsigned long rdnv asm("r1");
524 register unsigned long rmv asm("r0");
525 unsigned long cpsr = regs->ARM_cpsr;
526
527 rdnv = (rdn == 15) ? pc : regs->uregs[rdn];
528 rmv = (rm == 15) ? pc : regs->uregs[rm];
529
530 __asm__ __volatile__ (
531 "msr cpsr_fs, %[cpsr] \n\t"
532 "blx %[fn] \n\t"
533 "mrs %[cpsr], cpsr \n\t"
534 : "=r" (rdnv), [cpsr] "=r" (cpsr)
535 : "0" (rdnv), "r" (rmv), "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
536 : "lr", "memory", "cc"
537 );
538
539 if (rdn == 15)
540 rdnv &= ~1;
541
542 regs->uregs[rdn] = rdnv;
543 regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
544}
545
546static enum kprobe_insn __kprobes
547t16_decode_hiregs(kprobe_opcode_t insn, struct arch_specific_insn *asi)
548{
549 insn &= ~0x00ff;
550 insn |= 0x001; /* Set Rdn = R1 and Rm = R0 */
551 ((u16 *)asi->insn)[0] = insn;
552 asi->insn_handler = t16_emulate_hiregs;
553 return INSN_GOOD;
554}
555
Jon Medhurstfd0c8d82011-07-02 16:13:29 +0100556static void __kprobes
557t16_emulate_push(struct kprobe *p, struct pt_regs *regs)
558{
559 __asm__ __volatile__ (
560 "ldr r9, [%[regs], #13*4] \n\t"
561 "ldr r8, [%[regs], #14*4] \n\t"
562 "ldmia %[regs], {r0-r7} \n\t"
563 "blx %[fn] \n\t"
564 "str r9, [%[regs], #13*4] \n\t"
565 :
566 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
567 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9",
568 "lr", "memory", "cc"
569 );
570}
571
572static enum kprobe_insn __kprobes
573t16_decode_push(kprobe_opcode_t insn, struct arch_specific_insn *asi)
574{
575 /*
576 * To simulate a PUSH we use a Thumb-2 "STMDB R9!, {registers}"
577 * and call it with R9=SP and LR in the register list represented
578 * by R8.
579 */
580 ((u16 *)asi->insn)[0] = 0xe929; /* 1st half STMDB R9!,{} */
581 ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
582 asi->insn_handler = t16_emulate_push;
583 return INSN_GOOD;
584}
585
586static void __kprobes
587t16_emulate_pop_nopc(struct kprobe *p, struct pt_regs *regs)
588{
589 __asm__ __volatile__ (
590 "ldr r9, [%[regs], #13*4] \n\t"
591 "ldmia %[regs], {r0-r7} \n\t"
592 "blx %[fn] \n\t"
593 "stmia %[regs], {r0-r7} \n\t"
594 "str r9, [%[regs], #13*4] \n\t"
595 :
596 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
597 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
598 "lr", "memory", "cc"
599 );
600}
601
602static void __kprobes
603t16_emulate_pop_pc(struct kprobe *p, struct pt_regs *regs)
604{
605 register unsigned long pc asm("r8");
606
607 __asm__ __volatile__ (
608 "ldr r9, [%[regs], #13*4] \n\t"
609 "ldmia %[regs], {r0-r7} \n\t"
610 "blx %[fn] \n\t"
611 "stmia %[regs], {r0-r7} \n\t"
612 "str r9, [%[regs], #13*4] \n\t"
613 : "=r" (pc)
614 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
615 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
616 "lr", "memory", "cc"
617 );
618
619 bx_write_pc(pc, regs);
620}
621
622static enum kprobe_insn __kprobes
623t16_decode_pop(kprobe_opcode_t insn, struct arch_specific_insn *asi)
624{
625 /*
626 * To simulate a POP we use a Thumb-2 "LDMDB R9!, {registers}"
627 * and call it with R9=SP and PC in the register list represented
628 * by R8.
629 */
630 ((u16 *)asi->insn)[0] = 0xe8b9; /* 1st half LDMIA R9!,{} */
631 ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
632 asi->insn_handler = insn & 0x100 ? t16_emulate_pop_pc
633 : t16_emulate_pop_nopc;
634 return INSN_GOOD;
635}
636
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100637static const union decode_item t16_table_1011[] = {
638 /* Miscellaneous 16-bit instructions */
639
Jon Medhurst2f335822011-07-02 16:05:53 +0100640 /* ADD (SP plus immediate) 1011 0000 0xxx xxxx */
641 /* SUB (SP minus immediate) 1011 0000 1xxx xxxx */
642 DECODE_SIMULATE (0xff00, 0xb000, t16_simulate_add_sp_imm),
643
Jon Medhurst32818f32011-07-02 16:10:44 +0100644 /* CBZ 1011 00x1 xxxx xxxx */
645 /* CBNZ 1011 10x1 xxxx xxxx */
646 DECODE_SIMULATE (0xf500, 0xb100, t16_simulate_cbz),
647
648 /* SXTH 1011 0010 00xx xxxx */
649 /* SXTB 1011 0010 01xx xxxx */
650 /* UXTH 1011 0010 10xx xxxx */
651 /* UXTB 1011 0010 11xx xxxx */
652 /* REV 1011 1010 00xx xxxx */
653 /* REV16 1011 1010 01xx xxxx */
654 /* ??? 1011 1010 10xx xxxx */
655 /* REVSH 1011 1010 11xx xxxx */
656 DECODE_REJECT (0xffc0, 0xba80),
657 DECODE_EMULATE (0xf500, 0xb000, t16_emulate_loregs_rwflags),
658
Jon Medhurstfd0c8d82011-07-02 16:13:29 +0100659 /* PUSH 1011 010x xxxx xxxx */
660 DECODE_CUSTOM (0xfe00, 0xb400, t16_decode_push),
661 /* POP 1011 110x xxxx xxxx */
662 DECODE_CUSTOM (0xfe00, 0xbc00, t16_decode_pop),
663
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100664 /*
665 * If-Then, and hints
666 * 1011 1111 xxxx xxxx
667 */
668
669 /* YIELD 1011 1111 0001 0000 */
670 DECODE_OR (0xffff, 0xbf10),
671 /* SEV 1011 1111 0100 0000 */
672 DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none),
673 /* NOP 1011 1111 0000 0000 */
674 /* WFE 1011 1111 0010 0000 */
675 /* WFI 1011 1111 0011 0000 */
676 DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop),
677 /* Unassigned hints 1011 1111 xxxx 0000 */
678 DECODE_REJECT (0xff0f, 0xbf00),
Jon Medhurst5b94faf2011-07-02 16:16:05 +0100679 /* IT 1011 1111 xxxx xxxx */
680 DECODE_CUSTOM (0xff00, 0xbf00, t16_decode_it),
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100681
Jon Medhurst0a188cc2011-07-02 16:39:07 +0100682 /* SETEND 1011 0110 010x xxxx */
683 /* CPS 1011 0110 011x xxxx */
684 /* BKPT 1011 1110 xxxx xxxx */
685 /* And unallocated instructions... */
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100686 DECODE_END
687};
688
689const union decode_item kprobe_decode_thumb16_table[] = {
690
691 /*
Jon Medhurst02d194f2011-07-02 15:46:05 +0100692 * Shift (immediate), add, subtract, move, and compare
693 * 00xx xxxx xxxx xxxx
694 */
695
696 /* CMP (immediate) 0010 1xxx xxxx xxxx */
697 DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags),
698
699 /* ADD (register) 0001 100x xxxx xxxx */
700 /* SUB (register) 0001 101x xxxx xxxx */
701 /* LSL (immediate) 0000 0xxx xxxx xxxx */
702 /* LSR (immediate) 0000 1xxx xxxx xxxx */
703 /* ASR (immediate) 0001 0xxx xxxx xxxx */
704 /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */
705 /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */
706 /* MOV (immediate) 0010 0xxx xxxx xxxx */
707 /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */
708 /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */
709 DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags),
710
711 /*
712 * 16-bit Thumb data-processing instructions
713 * 0100 00xx xxxx xxxx
714 */
715
716 /* TST (register) 0100 0010 00xx xxxx */
717 DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags),
718 /* CMP (register) 0100 0010 10xx xxxx */
719 /* CMN (register) 0100 0010 11xx xxxx */
720 DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags),
721 /* AND (register) 0100 0000 00xx xxxx */
722 /* EOR (register) 0100 0000 01xx xxxx */
723 /* LSL (register) 0100 0000 10xx xxxx */
724 /* LSR (register) 0100 0000 11xx xxxx */
725 /* ASR (register) 0100 0001 00xx xxxx */
726 /* ADC (register) 0100 0001 01xx xxxx */
727 /* SBC (register) 0100 0001 10xx xxxx */
728 /* ROR (register) 0100 0001 11xx xxxx */
729 /* RSB (immediate) 0100 0010 01xx xxxx */
730 /* ORR (register) 0100 0011 00xx xxxx */
731 /* MUL 0100 0011 00xx xxxx */
732 /* BIC (register) 0100 0011 10xx xxxx */
733 /* MVN (register) 0100 0011 10xx xxxx */
734 DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags),
735
736 /*
Jon Medhursta9c3c292011-07-02 15:51:03 +0100737 * Special data instructions and branch and exchange
738 * 0100 01xx xxxx xxxx
739 */
740
741 /* BLX pc 0100 0111 1111 1xxx */
742 DECODE_REJECT (0xfff8, 0x47f8),
743
744 /* BX (register) 0100 0111 0xxx xxxx */
745 /* BLX (register) 0100 0111 1xxx xxxx */
746 DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx),
747
Jon Medhurst3b5940e2011-07-02 15:54:57 +0100748 /* ADD pc, pc 0100 0100 1111 1111 */
749 DECODE_REJECT (0xffff, 0x44ff),
750
751 /* ADD (register) 0100 0100 xxxx xxxx */
752 /* CMP (register) 0100 0101 xxxx xxxx */
753 /* MOV (register) 0100 0110 xxxx xxxx */
754 DECODE_CUSTOM (0xfc00, 0x4400, t16_decode_hiregs),
755
Jon Medhursta9c3c292011-07-02 15:51:03 +0100756 /*
Jon Medhurstf8695142011-07-02 16:00:09 +0100757 * Load from Literal Pool
758 * LDR (literal) 0100 1xxx xxxx xxxx
759 */
760 DECODE_SIMULATE (0xf800, 0x4800, t16_simulate_ldr_literal),
761
762 /*
763 * 16-bit Thumb Load/store instructions
764 * 0101 xxxx xxxx xxxx
765 * 011x xxxx xxxx xxxx
766 * 100x xxxx xxxx xxxx
767 */
768
769 /* STR (register) 0101 000x xxxx xxxx */
770 /* STRH (register) 0101 001x xxxx xxxx */
771 /* STRB (register) 0101 010x xxxx xxxx */
772 /* LDRSB (register) 0101 011x xxxx xxxx */
773 /* LDR (register) 0101 100x xxxx xxxx */
774 /* LDRH (register) 0101 101x xxxx xxxx */
775 /* LDRB (register) 0101 110x xxxx xxxx */
776 /* LDRSH (register) 0101 111x xxxx xxxx */
777 /* STR (immediate, Thumb) 0110 0xxx xxxx xxxx */
778 /* LDR (immediate, Thumb) 0110 1xxx xxxx xxxx */
779 /* STRB (immediate, Thumb) 0111 0xxx xxxx xxxx */
780 /* LDRB (immediate, Thumb) 0111 1xxx xxxx xxxx */
781 DECODE_EMULATE (0xc000, 0x4000, t16_emulate_loregs_rwflags),
782 /* STRH (immediate, Thumb) 1000 0xxx xxxx xxxx */
783 /* LDRH (immediate, Thumb) 1000 1xxx xxxx xxxx */
784 DECODE_EMULATE (0xf000, 0x8000, t16_emulate_loregs_rwflags),
785 /* STR (immediate, Thumb) 1001 0xxx xxxx xxxx */
786 /* LDR (immediate, Thumb) 1001 1xxx xxxx xxxx */
787 DECODE_SIMULATE (0xf000, 0x9000, t16_simulate_ldrstr_sp_relative),
788
789 /*
Jon Medhurst2f335822011-07-02 16:05:53 +0100790 * Generate PC-/SP-relative address
791 * ADR (literal) 1010 0xxx xxxx xxxx
792 * ADD (SP plus immediate) 1010 1xxx xxxx xxxx
793 */
794 DECODE_SIMULATE (0xf000, 0xa000, t16_simulate_reladr),
795
796 /*
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100797 * Miscellaneous 16-bit instructions
798 * 1011 xxxx xxxx xxxx
799 */
800 DECODE_TABLE (0xf000, 0xb000, t16_table_1011),
801
Jon Medhurstf8695142011-07-02 16:00:09 +0100802 /* STM 1100 0xxx xxxx xxxx */
803 /* LDM 1100 1xxx xxxx xxxx */
804 DECODE_EMULATE (0xf000, 0xc000, t16_emulate_loregs_rwflags),
805
Jon Medhurst44495662011-07-02 16:25:47 +0100806 /*
807 * Conditional branch, and Supervisor Call
808 */
809
810 /* Permanently UNDEFINED 1101 1110 xxxx xxxx */
811 /* SVC 1101 1111 xxxx xxxx */
812 DECODE_REJECT (0xfe00, 0xde00),
813
Jon Medhurst396b41f2011-07-02 16:30:43 +0100814 /* Conditional branch 1101 xxxx xxxx xxxx */
815 DECODE_CUSTOM (0xf000, 0xd000, t16_decode_cond_branch),
816
817 /*
818 * Unconditional branch
819 * B 1110 0xxx xxxx xxxx
820 */
821 DECODE_SIMULATE (0xf800, 0xe000, t16_simulate_branch),
822
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100823 DECODE_END
824};
825
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100826static unsigned long __kprobes thumb_check_cc(unsigned long cpsr)
827{
828 if (unlikely(in_it_block(cpsr)))
829 return kprobe_condition_checks[current_cond(cpsr)](cpsr);
830 return true;
831}
832
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100833static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs)
834{
835 regs->ARM_pc += 2;
836 p->ainsn.insn_handler(p, regs);
837 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
838}
839
840static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs)
841{
842 regs->ARM_pc += 4;
843 p->ainsn.insn_handler(p, regs);
844 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
845}
846
Jon Medhurst24371702011-04-19 17:56:58 +0100847enum kprobe_insn __kprobes
848thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
849{
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100850 asi->insn_singlestep = thumb16_singlestep;
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100851 asi->insn_check_cc = thumb_check_cc;
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100852 return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true);
Jon Medhurst24371702011-04-19 17:56:58 +0100853}
854
855enum kprobe_insn __kprobes
856thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
857{
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100858 asi->insn_singlestep = thumb32_singlestep;
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100859 asi->insn_check_cc = thumb_check_cc;
Jon Medhurstf39ca8b2011-07-03 13:55:47 +0100860 return kprobe_decode_insn(insn, asi, kprobe_decode_thumb32_table, true);
Jon Medhurst24371702011-04-19 17:56:58 +0100861}