blob: d25e5cbdd73610c433c1d4d13c49472f4b65813a [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 Medhursteaf1d062011-07-07 08:59:32 +010040static enum kprobe_insn __kprobes
41t32_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi)
42{
43 enum kprobe_insn ret = kprobe_decode_ldmstm(insn, asi);
44
45 /* Fixup modified instruction to have halfwords in correct order...*/
46 insn = asi->insn[0];
47 ((u16 *)asi->insn)[0] = insn >> 16;
48 ((u16 *)asi->insn)[1] = insn & 0xffff;
49
50 return ret;
51}
52
53static const union decode_item t32_table_1110_100x_x0xx[] = {
54 /* Load/store multiple instructions */
55
56 /* Rn is PC 1110 100x x0xx 1111 xxxx xxxx xxxx xxxx */
57 DECODE_REJECT (0xfe4f0000, 0xe80f0000),
58
59 /* SRS 1110 1000 00x0 xxxx xxxx xxxx xxxx xxxx */
60 /* RFE 1110 1000 00x1 xxxx xxxx xxxx xxxx xxxx */
61 DECODE_REJECT (0xffc00000, 0xe8000000),
62 /* SRS 1110 1001 10x0 xxxx xxxx xxxx xxxx xxxx */
63 /* RFE 1110 1001 10x1 xxxx xxxx xxxx xxxx xxxx */
64 DECODE_REJECT (0xffc00000, 0xe9800000),
65
66 /* STM Rn, {...pc} 1110 100x x0x0 xxxx 1xxx xxxx xxxx xxxx */
67 DECODE_REJECT (0xfe508000, 0xe8008000),
68 /* LDM Rn, {...lr,pc} 1110 100x x0x1 xxxx 11xx xxxx xxxx xxxx */
69 DECODE_REJECT (0xfe50c000, 0xe810c000),
70 /* LDM/STM Rn, {...sp} 1110 100x x0xx xxxx xx1x xxxx xxxx xxxx */
71 DECODE_REJECT (0xfe402000, 0xe8002000),
72
73 /* STMIA 1110 1000 10x0 xxxx xxxx xxxx xxxx xxxx */
74 /* LDMIA 1110 1000 10x1 xxxx xxxx xxxx xxxx xxxx */
75 /* STMDB 1110 1001 00x0 xxxx xxxx xxxx xxxx xxxx */
76 /* LDMDB 1110 1001 00x1 xxxx xxxx xxxx xxxx xxxx */
77 DECODE_CUSTOM (0xfe400000, 0xe8000000, t32_decode_ldmstm),
78
79 DECODE_END
80};
81
Jon Medhurstf39ca8b2011-07-03 13:55:47 +010082static const union decode_item t32_table_1111_0xxx___1[] = {
83 /* Branches and miscellaneous control */
84
85 /* YIELD 1111 0011 1010 xxxx 10x0 x000 0000 0001 */
86 DECODE_OR (0xfff0d7ff, 0xf3a08001),
87 /* SEV 1111 0011 1010 xxxx 10x0 x000 0000 0100 */
88 DECODE_EMULATE (0xfff0d7ff, 0xf3a08004, kprobe_emulate_none),
89 /* NOP 1111 0011 1010 xxxx 10x0 x000 0000 0000 */
90 /* WFE 1111 0011 1010 xxxx 10x0 x000 0000 0010 */
91 /* WFI 1111 0011 1010 xxxx 10x0 x000 0000 0011 */
92 DECODE_SIMULATE (0xfff0d7fc, 0xf3a08000, kprobe_simulate_nop),
93
94 DECODE_END
95};
96
97const union decode_item kprobe_decode_thumb32_table[] = {
98
99 /*
Jon Medhursteaf1d062011-07-07 08:59:32 +0100100 * Load/store multiple instructions
101 * 1110 100x x0xx xxxx xxxx xxxx xxxx xxxx
102 */
103 DECODE_TABLE (0xfe400000, 0xe8000000, t32_table_1110_100x_x0xx),
104
105 /*
Jon Medhurstf39ca8b2011-07-03 13:55:47 +0100106 * Branches and miscellaneous control
107 * 1111 0xxx xxxx xxxx 1xxx xxxx xxxx xxxx
108 */
109 DECODE_TABLE (0xf8008000, 0xf0008000, t32_table_1111_0xxx___1),
110
111 DECODE_END
112};
113
Jon Medhursta9c3c292011-07-02 15:51:03 +0100114static void __kprobes
115t16_simulate_bxblx(struct kprobe *p, struct pt_regs *regs)
116{
117 kprobe_opcode_t insn = p->opcode;
118 unsigned long pc = thumb_probe_pc(p);
119 int rm = (insn >> 3) & 0xf;
120 unsigned long rmv = (rm == 15) ? pc : regs->uregs[rm];
121
122 if (insn & (1 << 7)) /* BLX ? */
123 regs->ARM_lr = (unsigned long)p->addr + 2;
124
125 bx_write_pc(rmv, regs);
126}
127
Jon Medhurstf8695142011-07-02 16:00:09 +0100128static void __kprobes
129t16_simulate_ldr_literal(struct kprobe *p, struct pt_regs *regs)
130{
131 kprobe_opcode_t insn = p->opcode;
132 unsigned long* base = (unsigned long *)(thumb_probe_pc(p) & ~3);
133 long index = insn & 0xff;
134 int rt = (insn >> 8) & 0x7;
135 regs->uregs[rt] = base[index];
136}
137
138static void __kprobes
139t16_simulate_ldrstr_sp_relative(struct kprobe *p, struct pt_regs *regs)
140{
141 kprobe_opcode_t insn = p->opcode;
142 unsigned long* base = (unsigned long *)regs->ARM_sp;
143 long index = insn & 0xff;
144 int rt = (insn >> 8) & 0x7;
145 if (insn & 0x800) /* LDR */
146 regs->uregs[rt] = base[index];
147 else /* STR */
148 base[index] = regs->uregs[rt];
149}
150
Jon Medhurst2f335822011-07-02 16:05:53 +0100151static void __kprobes
152t16_simulate_reladr(struct kprobe *p, struct pt_regs *regs)
153{
154 kprobe_opcode_t insn = p->opcode;
155 unsigned long base = (insn & 0x800) ? regs->ARM_sp
156 : (thumb_probe_pc(p) & ~3);
157 long offset = insn & 0xff;
158 int rt = (insn >> 8) & 0x7;
159 regs->uregs[rt] = base + offset * 4;
160}
161
162static void __kprobes
163t16_simulate_add_sp_imm(struct kprobe *p, struct pt_regs *regs)
164{
165 kprobe_opcode_t insn = p->opcode;
166 long imm = insn & 0x7f;
167 if (insn & 0x80) /* SUB */
168 regs->ARM_sp -= imm * 4;
169 else /* ADD */
170 regs->ARM_sp += imm * 4;
171}
172
Jon Medhurst32818f32011-07-02 16:10:44 +0100173static void __kprobes
174t16_simulate_cbz(struct kprobe *p, struct pt_regs *regs)
175{
176 kprobe_opcode_t insn = p->opcode;
177 int rn = insn & 0x7;
178 kprobe_opcode_t nonzero = regs->uregs[rn] ? insn : ~insn;
179 if (nonzero & 0x800) {
180 long i = insn & 0x200;
181 long imm5 = insn & 0xf8;
182 unsigned long pc = thumb_probe_pc(p);
183 regs->ARM_pc = pc + (i >> 3) + (imm5 >> 2);
184 }
185}
186
Jon Medhurst5b94faf2011-07-02 16:16:05 +0100187static void __kprobes
188t16_simulate_it(struct kprobe *p, struct pt_regs *regs)
189{
190 /*
191 * The 8 IT state bits are split into two parts in CPSR:
192 * ITSTATE<1:0> are in CPSR<26:25>
193 * ITSTATE<7:2> are in CPSR<15:10>
194 * The new IT state is in the lower byte of insn.
195 */
196 kprobe_opcode_t insn = p->opcode;
197 unsigned long cpsr = regs->ARM_cpsr;
198 cpsr &= ~PSR_IT_MASK;
199 cpsr |= (insn & 0xfc) << 8;
200 cpsr |= (insn & 0x03) << 25;
201 regs->ARM_cpsr = cpsr;
202}
203
204static void __kprobes
205t16_singlestep_it(struct kprobe *p, struct pt_regs *regs)
206{
207 regs->ARM_pc += 2;
208 t16_simulate_it(p, regs);
209}
210
211static enum kprobe_insn __kprobes
212t16_decode_it(kprobe_opcode_t insn, struct arch_specific_insn *asi)
213{
214 asi->insn_singlestep = t16_singlestep_it;
215 return INSN_GOOD_NO_SLOT;
216}
217
Jon Medhurst396b41f2011-07-02 16:30:43 +0100218static void __kprobes
219t16_simulate_cond_branch(struct kprobe *p, struct pt_regs *regs)
220{
221 kprobe_opcode_t insn = p->opcode;
222 unsigned long pc = thumb_probe_pc(p);
223 long offset = insn & 0x7f;
224 offset -= insn & 0x80; /* Apply sign bit */
225 regs->ARM_pc = pc + (offset * 2);
226}
227
228static enum kprobe_insn __kprobes
229t16_decode_cond_branch(kprobe_opcode_t insn, struct arch_specific_insn *asi)
230{
231 int cc = (insn >> 8) & 0xf;
232 asi->insn_check_cc = kprobe_condition_checks[cc];
233 asi->insn_handler = t16_simulate_cond_branch;
234 return INSN_GOOD_NO_SLOT;
235}
236
237static void __kprobes
238t16_simulate_branch(struct kprobe *p, struct pt_regs *regs)
239{
240 kprobe_opcode_t insn = p->opcode;
241 unsigned long pc = thumb_probe_pc(p);
242 long offset = insn & 0x3ff;
243 offset -= insn & 0x400; /* Apply sign bit */
244 regs->ARM_pc = pc + (offset * 2);
245}
246
Jon Medhurst02d194f2011-07-02 15:46:05 +0100247static unsigned long __kprobes
248t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs)
249{
250 unsigned long oldcpsr = regs->ARM_cpsr;
251 unsigned long newcpsr;
252
253 __asm__ __volatile__ (
254 "msr cpsr_fs, %[oldcpsr] \n\t"
255 "ldmia %[regs], {r0-r7} \n\t"
256 "blx %[fn] \n\t"
257 "stmia %[regs], {r0-r7} \n\t"
258 "mrs %[newcpsr], cpsr \n\t"
259 : [newcpsr] "=r" (newcpsr)
260 : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs),
261 [fn] "r" (p->ainsn.insn_fn)
262 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
263 "lr", "memory", "cc"
264 );
265
266 return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK);
267}
268
269static void __kprobes
270t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs)
271{
272 regs->ARM_cpsr = t16_emulate_loregs(p, regs);
273}
274
275static void __kprobes
276t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs)
277{
278 unsigned long cpsr = t16_emulate_loregs(p, regs);
279 if (!in_it_block(cpsr))
280 regs->ARM_cpsr = cpsr;
281}
282
Jon Medhurst3b5940e2011-07-02 15:54:57 +0100283static void __kprobes
284t16_emulate_hiregs(struct kprobe *p, struct pt_regs *regs)
285{
286 kprobe_opcode_t insn = p->opcode;
287 unsigned long pc = thumb_probe_pc(p);
288 int rdn = (insn & 0x7) | ((insn & 0x80) >> 4);
289 int rm = (insn >> 3) & 0xf;
290
291 register unsigned long rdnv asm("r1");
292 register unsigned long rmv asm("r0");
293 unsigned long cpsr = regs->ARM_cpsr;
294
295 rdnv = (rdn == 15) ? pc : regs->uregs[rdn];
296 rmv = (rm == 15) ? pc : regs->uregs[rm];
297
298 __asm__ __volatile__ (
299 "msr cpsr_fs, %[cpsr] \n\t"
300 "blx %[fn] \n\t"
301 "mrs %[cpsr], cpsr \n\t"
302 : "=r" (rdnv), [cpsr] "=r" (cpsr)
303 : "0" (rdnv), "r" (rmv), "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
304 : "lr", "memory", "cc"
305 );
306
307 if (rdn == 15)
308 rdnv &= ~1;
309
310 regs->uregs[rdn] = rdnv;
311 regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
312}
313
314static enum kprobe_insn __kprobes
315t16_decode_hiregs(kprobe_opcode_t insn, struct arch_specific_insn *asi)
316{
317 insn &= ~0x00ff;
318 insn |= 0x001; /* Set Rdn = R1 and Rm = R0 */
319 ((u16 *)asi->insn)[0] = insn;
320 asi->insn_handler = t16_emulate_hiregs;
321 return INSN_GOOD;
322}
323
Jon Medhurstfd0c8d82011-07-02 16:13:29 +0100324static void __kprobes
325t16_emulate_push(struct kprobe *p, struct pt_regs *regs)
326{
327 __asm__ __volatile__ (
328 "ldr r9, [%[regs], #13*4] \n\t"
329 "ldr r8, [%[regs], #14*4] \n\t"
330 "ldmia %[regs], {r0-r7} \n\t"
331 "blx %[fn] \n\t"
332 "str r9, [%[regs], #13*4] \n\t"
333 :
334 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
335 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9",
336 "lr", "memory", "cc"
337 );
338}
339
340static enum kprobe_insn __kprobes
341t16_decode_push(kprobe_opcode_t insn, struct arch_specific_insn *asi)
342{
343 /*
344 * To simulate a PUSH we use a Thumb-2 "STMDB R9!, {registers}"
345 * and call it with R9=SP and LR in the register list represented
346 * by R8.
347 */
348 ((u16 *)asi->insn)[0] = 0xe929; /* 1st half STMDB R9!,{} */
349 ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
350 asi->insn_handler = t16_emulate_push;
351 return INSN_GOOD;
352}
353
354static void __kprobes
355t16_emulate_pop_nopc(struct kprobe *p, struct pt_regs *regs)
356{
357 __asm__ __volatile__ (
358 "ldr r9, [%[regs], #13*4] \n\t"
359 "ldmia %[regs], {r0-r7} \n\t"
360 "blx %[fn] \n\t"
361 "stmia %[regs], {r0-r7} \n\t"
362 "str r9, [%[regs], #13*4] \n\t"
363 :
364 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
365 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
366 "lr", "memory", "cc"
367 );
368}
369
370static void __kprobes
371t16_emulate_pop_pc(struct kprobe *p, struct pt_regs *regs)
372{
373 register unsigned long pc asm("r8");
374
375 __asm__ __volatile__ (
376 "ldr r9, [%[regs], #13*4] \n\t"
377 "ldmia %[regs], {r0-r7} \n\t"
378 "blx %[fn] \n\t"
379 "stmia %[regs], {r0-r7} \n\t"
380 "str r9, [%[regs], #13*4] \n\t"
381 : "=r" (pc)
382 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
383 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
384 "lr", "memory", "cc"
385 );
386
387 bx_write_pc(pc, regs);
388}
389
390static enum kprobe_insn __kprobes
391t16_decode_pop(kprobe_opcode_t insn, struct arch_specific_insn *asi)
392{
393 /*
394 * To simulate a POP we use a Thumb-2 "LDMDB R9!, {registers}"
395 * and call it with R9=SP and PC in the register list represented
396 * by R8.
397 */
398 ((u16 *)asi->insn)[0] = 0xe8b9; /* 1st half LDMIA R9!,{} */
399 ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
400 asi->insn_handler = insn & 0x100 ? t16_emulate_pop_pc
401 : t16_emulate_pop_nopc;
402 return INSN_GOOD;
403}
404
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100405static const union decode_item t16_table_1011[] = {
406 /* Miscellaneous 16-bit instructions */
407
Jon Medhurst2f335822011-07-02 16:05:53 +0100408 /* ADD (SP plus immediate) 1011 0000 0xxx xxxx */
409 /* SUB (SP minus immediate) 1011 0000 1xxx xxxx */
410 DECODE_SIMULATE (0xff00, 0xb000, t16_simulate_add_sp_imm),
411
Jon Medhurst32818f32011-07-02 16:10:44 +0100412 /* CBZ 1011 00x1 xxxx xxxx */
413 /* CBNZ 1011 10x1 xxxx xxxx */
414 DECODE_SIMULATE (0xf500, 0xb100, t16_simulate_cbz),
415
416 /* SXTH 1011 0010 00xx xxxx */
417 /* SXTB 1011 0010 01xx xxxx */
418 /* UXTH 1011 0010 10xx xxxx */
419 /* UXTB 1011 0010 11xx xxxx */
420 /* REV 1011 1010 00xx xxxx */
421 /* REV16 1011 1010 01xx xxxx */
422 /* ??? 1011 1010 10xx xxxx */
423 /* REVSH 1011 1010 11xx xxxx */
424 DECODE_REJECT (0xffc0, 0xba80),
425 DECODE_EMULATE (0xf500, 0xb000, t16_emulate_loregs_rwflags),
426
Jon Medhurstfd0c8d82011-07-02 16:13:29 +0100427 /* PUSH 1011 010x xxxx xxxx */
428 DECODE_CUSTOM (0xfe00, 0xb400, t16_decode_push),
429 /* POP 1011 110x xxxx xxxx */
430 DECODE_CUSTOM (0xfe00, 0xbc00, t16_decode_pop),
431
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100432 /*
433 * If-Then, and hints
434 * 1011 1111 xxxx xxxx
435 */
436
437 /* YIELD 1011 1111 0001 0000 */
438 DECODE_OR (0xffff, 0xbf10),
439 /* SEV 1011 1111 0100 0000 */
440 DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none),
441 /* NOP 1011 1111 0000 0000 */
442 /* WFE 1011 1111 0010 0000 */
443 /* WFI 1011 1111 0011 0000 */
444 DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop),
445 /* Unassigned hints 1011 1111 xxxx 0000 */
446 DECODE_REJECT (0xff0f, 0xbf00),
Jon Medhurst5b94faf2011-07-02 16:16:05 +0100447 /* IT 1011 1111 xxxx xxxx */
448 DECODE_CUSTOM (0xff00, 0xbf00, t16_decode_it),
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100449
Jon Medhurst0a188cc2011-07-02 16:39:07 +0100450 /* SETEND 1011 0110 010x xxxx */
451 /* CPS 1011 0110 011x xxxx */
452 /* BKPT 1011 1110 xxxx xxxx */
453 /* And unallocated instructions... */
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100454 DECODE_END
455};
456
457const union decode_item kprobe_decode_thumb16_table[] = {
458
459 /*
Jon Medhurst02d194f2011-07-02 15:46:05 +0100460 * Shift (immediate), add, subtract, move, and compare
461 * 00xx xxxx xxxx xxxx
462 */
463
464 /* CMP (immediate) 0010 1xxx xxxx xxxx */
465 DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags),
466
467 /* ADD (register) 0001 100x xxxx xxxx */
468 /* SUB (register) 0001 101x xxxx xxxx */
469 /* LSL (immediate) 0000 0xxx xxxx xxxx */
470 /* LSR (immediate) 0000 1xxx xxxx xxxx */
471 /* ASR (immediate) 0001 0xxx xxxx xxxx */
472 /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */
473 /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */
474 /* MOV (immediate) 0010 0xxx xxxx xxxx */
475 /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */
476 /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */
477 DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags),
478
479 /*
480 * 16-bit Thumb data-processing instructions
481 * 0100 00xx xxxx xxxx
482 */
483
484 /* TST (register) 0100 0010 00xx xxxx */
485 DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags),
486 /* CMP (register) 0100 0010 10xx xxxx */
487 /* CMN (register) 0100 0010 11xx xxxx */
488 DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags),
489 /* AND (register) 0100 0000 00xx xxxx */
490 /* EOR (register) 0100 0000 01xx xxxx */
491 /* LSL (register) 0100 0000 10xx xxxx */
492 /* LSR (register) 0100 0000 11xx xxxx */
493 /* ASR (register) 0100 0001 00xx xxxx */
494 /* ADC (register) 0100 0001 01xx xxxx */
495 /* SBC (register) 0100 0001 10xx xxxx */
496 /* ROR (register) 0100 0001 11xx xxxx */
497 /* RSB (immediate) 0100 0010 01xx xxxx */
498 /* ORR (register) 0100 0011 00xx xxxx */
499 /* MUL 0100 0011 00xx xxxx */
500 /* BIC (register) 0100 0011 10xx xxxx */
501 /* MVN (register) 0100 0011 10xx xxxx */
502 DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags),
503
504 /*
Jon Medhursta9c3c292011-07-02 15:51:03 +0100505 * Special data instructions and branch and exchange
506 * 0100 01xx xxxx xxxx
507 */
508
509 /* BLX pc 0100 0111 1111 1xxx */
510 DECODE_REJECT (0xfff8, 0x47f8),
511
512 /* BX (register) 0100 0111 0xxx xxxx */
513 /* BLX (register) 0100 0111 1xxx xxxx */
514 DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx),
515
Jon Medhurst3b5940e2011-07-02 15:54:57 +0100516 /* ADD pc, pc 0100 0100 1111 1111 */
517 DECODE_REJECT (0xffff, 0x44ff),
518
519 /* ADD (register) 0100 0100 xxxx xxxx */
520 /* CMP (register) 0100 0101 xxxx xxxx */
521 /* MOV (register) 0100 0110 xxxx xxxx */
522 DECODE_CUSTOM (0xfc00, 0x4400, t16_decode_hiregs),
523
Jon Medhursta9c3c292011-07-02 15:51:03 +0100524 /*
Jon Medhurstf8695142011-07-02 16:00:09 +0100525 * Load from Literal Pool
526 * LDR (literal) 0100 1xxx xxxx xxxx
527 */
528 DECODE_SIMULATE (0xf800, 0x4800, t16_simulate_ldr_literal),
529
530 /*
531 * 16-bit Thumb Load/store instructions
532 * 0101 xxxx xxxx xxxx
533 * 011x xxxx xxxx xxxx
534 * 100x xxxx xxxx xxxx
535 */
536
537 /* STR (register) 0101 000x xxxx xxxx */
538 /* STRH (register) 0101 001x xxxx xxxx */
539 /* STRB (register) 0101 010x xxxx xxxx */
540 /* LDRSB (register) 0101 011x xxxx xxxx */
541 /* LDR (register) 0101 100x xxxx xxxx */
542 /* LDRH (register) 0101 101x xxxx xxxx */
543 /* LDRB (register) 0101 110x xxxx xxxx */
544 /* LDRSH (register) 0101 111x xxxx xxxx */
545 /* STR (immediate, Thumb) 0110 0xxx xxxx xxxx */
546 /* LDR (immediate, Thumb) 0110 1xxx xxxx xxxx */
547 /* STRB (immediate, Thumb) 0111 0xxx xxxx xxxx */
548 /* LDRB (immediate, Thumb) 0111 1xxx xxxx xxxx */
549 DECODE_EMULATE (0xc000, 0x4000, t16_emulate_loregs_rwflags),
550 /* STRH (immediate, Thumb) 1000 0xxx xxxx xxxx */
551 /* LDRH (immediate, Thumb) 1000 1xxx xxxx xxxx */
552 DECODE_EMULATE (0xf000, 0x8000, t16_emulate_loregs_rwflags),
553 /* STR (immediate, Thumb) 1001 0xxx xxxx xxxx */
554 /* LDR (immediate, Thumb) 1001 1xxx xxxx xxxx */
555 DECODE_SIMULATE (0xf000, 0x9000, t16_simulate_ldrstr_sp_relative),
556
557 /*
Jon Medhurst2f335822011-07-02 16:05:53 +0100558 * Generate PC-/SP-relative address
559 * ADR (literal) 1010 0xxx xxxx xxxx
560 * ADD (SP plus immediate) 1010 1xxx xxxx xxxx
561 */
562 DECODE_SIMULATE (0xf000, 0xa000, t16_simulate_reladr),
563
564 /*
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100565 * Miscellaneous 16-bit instructions
566 * 1011 xxxx xxxx xxxx
567 */
568 DECODE_TABLE (0xf000, 0xb000, t16_table_1011),
569
Jon Medhurstf8695142011-07-02 16:00:09 +0100570 /* STM 1100 0xxx xxxx xxxx */
571 /* LDM 1100 1xxx xxxx xxxx */
572 DECODE_EMULATE (0xf000, 0xc000, t16_emulate_loregs_rwflags),
573
Jon Medhurst44495662011-07-02 16:25:47 +0100574 /*
575 * Conditional branch, and Supervisor Call
576 */
577
578 /* Permanently UNDEFINED 1101 1110 xxxx xxxx */
579 /* SVC 1101 1111 xxxx xxxx */
580 DECODE_REJECT (0xfe00, 0xde00),
581
Jon Medhurst396b41f2011-07-02 16:30:43 +0100582 /* Conditional branch 1101 xxxx xxxx xxxx */
583 DECODE_CUSTOM (0xf000, 0xd000, t16_decode_cond_branch),
584
585 /*
586 * Unconditional branch
587 * B 1110 0xxx xxxx xxxx
588 */
589 DECODE_SIMULATE (0xf800, 0xe000, t16_simulate_branch),
590
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100591 DECODE_END
592};
593
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100594static unsigned long __kprobes thumb_check_cc(unsigned long cpsr)
595{
596 if (unlikely(in_it_block(cpsr)))
597 return kprobe_condition_checks[current_cond(cpsr)](cpsr);
598 return true;
599}
600
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100601static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs)
602{
603 regs->ARM_pc += 2;
604 p->ainsn.insn_handler(p, regs);
605 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
606}
607
608static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs)
609{
610 regs->ARM_pc += 4;
611 p->ainsn.insn_handler(p, regs);
612 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
613}
614
Jon Medhurst24371702011-04-19 17:56:58 +0100615enum kprobe_insn __kprobes
616thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
617{
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100618 asi->insn_singlestep = thumb16_singlestep;
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100619 asi->insn_check_cc = thumb_check_cc;
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100620 return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true);
Jon Medhurst24371702011-04-19 17:56:58 +0100621}
622
623enum kprobe_insn __kprobes
624thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
625{
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100626 asi->insn_singlestep = thumb32_singlestep;
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100627 asi->insn_check_cc = thumb_check_cc;
Jon Medhurstf39ca8b2011-07-03 13:55:47 +0100628 return kprobe_decode_insn(insn, asi, kprobe_decode_thumb32_table, true);
Jon Medhurst24371702011-04-19 17:56:58 +0100629}