blob: eac945ccf618973a3c2361fde1a554981de452ee [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 Medhurstf39ca8b2011-07-03 13:55:47 +0100244static const union decode_item t32_table_1111_0xxx___1[] = {
245 /* Branches and miscellaneous control */
246
247 /* YIELD 1111 0011 1010 xxxx 10x0 x000 0000 0001 */
248 DECODE_OR (0xfff0d7ff, 0xf3a08001),
249 /* SEV 1111 0011 1010 xxxx 10x0 x000 0000 0100 */
250 DECODE_EMULATE (0xfff0d7ff, 0xf3a08004, kprobe_emulate_none),
251 /* NOP 1111 0011 1010 xxxx 10x0 x000 0000 0000 */
252 /* WFE 1111 0011 1010 xxxx 10x0 x000 0000 0010 */
253 /* WFI 1111 0011 1010 xxxx 10x0 x000 0000 0011 */
254 DECODE_SIMULATE (0xfff0d7fc, 0xf3a08000, kprobe_simulate_nop),
255
256 DECODE_END
257};
258
259const union decode_item kprobe_decode_thumb32_table[] = {
260
261 /*
Jon Medhursteaf1d062011-07-07 08:59:32 +0100262 * Load/store multiple instructions
263 * 1110 100x x0xx xxxx xxxx xxxx xxxx xxxx
264 */
265 DECODE_TABLE (0xfe400000, 0xe8000000, t32_table_1110_100x_x0xx),
266
267 /*
Jon Medhurstb48354d2011-07-03 14:23:21 +0100268 * Load/store dual, load/store exclusive, table branch
269 * 1110 100x x1xx xxxx xxxx xxxx xxxx xxxx
270 */
271 DECODE_TABLE (0xfe400000, 0xe8400000, t32_table_1110_100x_x1xx),
272
273 /*
Jon Medhurst080e0012011-07-03 14:31:58 +0100274 * Data-processing (shifted register)
275 * 1110 101x xxxx xxxx xxxx xxxx xxxx xxxx
276 */
277 DECODE_TABLE (0xfe000000, 0xea000000, t32_table_1110_101x),
278
279 /*
Jon Medhurstf39ca8b2011-07-03 13:55:47 +0100280 * Branches and miscellaneous control
281 * 1111 0xxx xxxx xxxx 1xxx xxxx xxxx xxxx
282 */
283 DECODE_TABLE (0xf8008000, 0xf0008000, t32_table_1111_0xxx___1),
284
285 DECODE_END
286};
287
Jon Medhursta9c3c292011-07-02 15:51:03 +0100288static void __kprobes
289t16_simulate_bxblx(struct kprobe *p, struct pt_regs *regs)
290{
291 kprobe_opcode_t insn = p->opcode;
292 unsigned long pc = thumb_probe_pc(p);
293 int rm = (insn >> 3) & 0xf;
294 unsigned long rmv = (rm == 15) ? pc : regs->uregs[rm];
295
296 if (insn & (1 << 7)) /* BLX ? */
297 regs->ARM_lr = (unsigned long)p->addr + 2;
298
299 bx_write_pc(rmv, regs);
300}
301
Jon Medhurstf8695142011-07-02 16:00:09 +0100302static void __kprobes
303t16_simulate_ldr_literal(struct kprobe *p, struct pt_regs *regs)
304{
305 kprobe_opcode_t insn = p->opcode;
306 unsigned long* base = (unsigned long *)(thumb_probe_pc(p) & ~3);
307 long index = insn & 0xff;
308 int rt = (insn >> 8) & 0x7;
309 regs->uregs[rt] = base[index];
310}
311
312static void __kprobes
313t16_simulate_ldrstr_sp_relative(struct kprobe *p, struct pt_regs *regs)
314{
315 kprobe_opcode_t insn = p->opcode;
316 unsigned long* base = (unsigned long *)regs->ARM_sp;
317 long index = insn & 0xff;
318 int rt = (insn >> 8) & 0x7;
319 if (insn & 0x800) /* LDR */
320 regs->uregs[rt] = base[index];
321 else /* STR */
322 base[index] = regs->uregs[rt];
323}
324
Jon Medhurst2f335822011-07-02 16:05:53 +0100325static void __kprobes
326t16_simulate_reladr(struct kprobe *p, struct pt_regs *regs)
327{
328 kprobe_opcode_t insn = p->opcode;
329 unsigned long base = (insn & 0x800) ? regs->ARM_sp
330 : (thumb_probe_pc(p) & ~3);
331 long offset = insn & 0xff;
332 int rt = (insn >> 8) & 0x7;
333 regs->uregs[rt] = base + offset * 4;
334}
335
336static void __kprobes
337t16_simulate_add_sp_imm(struct kprobe *p, struct pt_regs *regs)
338{
339 kprobe_opcode_t insn = p->opcode;
340 long imm = insn & 0x7f;
341 if (insn & 0x80) /* SUB */
342 regs->ARM_sp -= imm * 4;
343 else /* ADD */
344 regs->ARM_sp += imm * 4;
345}
346
Jon Medhurst32818f32011-07-02 16:10:44 +0100347static void __kprobes
348t16_simulate_cbz(struct kprobe *p, struct pt_regs *regs)
349{
350 kprobe_opcode_t insn = p->opcode;
351 int rn = insn & 0x7;
352 kprobe_opcode_t nonzero = regs->uregs[rn] ? insn : ~insn;
353 if (nonzero & 0x800) {
354 long i = insn & 0x200;
355 long imm5 = insn & 0xf8;
356 unsigned long pc = thumb_probe_pc(p);
357 regs->ARM_pc = pc + (i >> 3) + (imm5 >> 2);
358 }
359}
360
Jon Medhurst5b94faf2011-07-02 16:16:05 +0100361static void __kprobes
362t16_simulate_it(struct kprobe *p, struct pt_regs *regs)
363{
364 /*
365 * The 8 IT state bits are split into two parts in CPSR:
366 * ITSTATE<1:0> are in CPSR<26:25>
367 * ITSTATE<7:2> are in CPSR<15:10>
368 * The new IT state is in the lower byte of insn.
369 */
370 kprobe_opcode_t insn = p->opcode;
371 unsigned long cpsr = regs->ARM_cpsr;
372 cpsr &= ~PSR_IT_MASK;
373 cpsr |= (insn & 0xfc) << 8;
374 cpsr |= (insn & 0x03) << 25;
375 regs->ARM_cpsr = cpsr;
376}
377
378static void __kprobes
379t16_singlestep_it(struct kprobe *p, struct pt_regs *regs)
380{
381 regs->ARM_pc += 2;
382 t16_simulate_it(p, regs);
383}
384
385static enum kprobe_insn __kprobes
386t16_decode_it(kprobe_opcode_t insn, struct arch_specific_insn *asi)
387{
388 asi->insn_singlestep = t16_singlestep_it;
389 return INSN_GOOD_NO_SLOT;
390}
391
Jon Medhurst396b41f2011-07-02 16:30:43 +0100392static void __kprobes
393t16_simulate_cond_branch(struct kprobe *p, struct pt_regs *regs)
394{
395 kprobe_opcode_t insn = p->opcode;
396 unsigned long pc = thumb_probe_pc(p);
397 long offset = insn & 0x7f;
398 offset -= insn & 0x80; /* Apply sign bit */
399 regs->ARM_pc = pc + (offset * 2);
400}
401
402static enum kprobe_insn __kprobes
403t16_decode_cond_branch(kprobe_opcode_t insn, struct arch_specific_insn *asi)
404{
405 int cc = (insn >> 8) & 0xf;
406 asi->insn_check_cc = kprobe_condition_checks[cc];
407 asi->insn_handler = t16_simulate_cond_branch;
408 return INSN_GOOD_NO_SLOT;
409}
410
411static void __kprobes
412t16_simulate_branch(struct kprobe *p, struct pt_regs *regs)
413{
414 kprobe_opcode_t insn = p->opcode;
415 unsigned long pc = thumb_probe_pc(p);
416 long offset = insn & 0x3ff;
417 offset -= insn & 0x400; /* Apply sign bit */
418 regs->ARM_pc = pc + (offset * 2);
419}
420
Jon Medhurst02d194f2011-07-02 15:46:05 +0100421static unsigned long __kprobes
422t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs)
423{
424 unsigned long oldcpsr = regs->ARM_cpsr;
425 unsigned long newcpsr;
426
427 __asm__ __volatile__ (
428 "msr cpsr_fs, %[oldcpsr] \n\t"
429 "ldmia %[regs], {r0-r7} \n\t"
430 "blx %[fn] \n\t"
431 "stmia %[regs], {r0-r7} \n\t"
432 "mrs %[newcpsr], cpsr \n\t"
433 : [newcpsr] "=r" (newcpsr)
434 : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs),
435 [fn] "r" (p->ainsn.insn_fn)
436 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
437 "lr", "memory", "cc"
438 );
439
440 return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK);
441}
442
443static void __kprobes
444t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs)
445{
446 regs->ARM_cpsr = t16_emulate_loregs(p, regs);
447}
448
449static void __kprobes
450t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs)
451{
452 unsigned long cpsr = t16_emulate_loregs(p, regs);
453 if (!in_it_block(cpsr))
454 regs->ARM_cpsr = cpsr;
455}
456
Jon Medhurst3b5940e2011-07-02 15:54:57 +0100457static void __kprobes
458t16_emulate_hiregs(struct kprobe *p, struct pt_regs *regs)
459{
460 kprobe_opcode_t insn = p->opcode;
461 unsigned long pc = thumb_probe_pc(p);
462 int rdn = (insn & 0x7) | ((insn & 0x80) >> 4);
463 int rm = (insn >> 3) & 0xf;
464
465 register unsigned long rdnv asm("r1");
466 register unsigned long rmv asm("r0");
467 unsigned long cpsr = regs->ARM_cpsr;
468
469 rdnv = (rdn == 15) ? pc : regs->uregs[rdn];
470 rmv = (rm == 15) ? pc : regs->uregs[rm];
471
472 __asm__ __volatile__ (
473 "msr cpsr_fs, %[cpsr] \n\t"
474 "blx %[fn] \n\t"
475 "mrs %[cpsr], cpsr \n\t"
476 : "=r" (rdnv), [cpsr] "=r" (cpsr)
477 : "0" (rdnv), "r" (rmv), "1" (cpsr), [fn] "r" (p->ainsn.insn_fn)
478 : "lr", "memory", "cc"
479 );
480
481 if (rdn == 15)
482 rdnv &= ~1;
483
484 regs->uregs[rdn] = rdnv;
485 regs->ARM_cpsr = (regs->ARM_cpsr & ~APSR_MASK) | (cpsr & APSR_MASK);
486}
487
488static enum kprobe_insn __kprobes
489t16_decode_hiregs(kprobe_opcode_t insn, struct arch_specific_insn *asi)
490{
491 insn &= ~0x00ff;
492 insn |= 0x001; /* Set Rdn = R1 and Rm = R0 */
493 ((u16 *)asi->insn)[0] = insn;
494 asi->insn_handler = t16_emulate_hiregs;
495 return INSN_GOOD;
496}
497
Jon Medhurstfd0c8d82011-07-02 16:13:29 +0100498static void __kprobes
499t16_emulate_push(struct kprobe *p, struct pt_regs *regs)
500{
501 __asm__ __volatile__ (
502 "ldr r9, [%[regs], #13*4] \n\t"
503 "ldr r8, [%[regs], #14*4] \n\t"
504 "ldmia %[regs], {r0-r7} \n\t"
505 "blx %[fn] \n\t"
506 "str r9, [%[regs], #13*4] \n\t"
507 :
508 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
509 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9",
510 "lr", "memory", "cc"
511 );
512}
513
514static enum kprobe_insn __kprobes
515t16_decode_push(kprobe_opcode_t insn, struct arch_specific_insn *asi)
516{
517 /*
518 * To simulate a PUSH we use a Thumb-2 "STMDB R9!, {registers}"
519 * and call it with R9=SP and LR in the register list represented
520 * by R8.
521 */
522 ((u16 *)asi->insn)[0] = 0xe929; /* 1st half STMDB R9!,{} */
523 ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
524 asi->insn_handler = t16_emulate_push;
525 return INSN_GOOD;
526}
527
528static void __kprobes
529t16_emulate_pop_nopc(struct kprobe *p, struct pt_regs *regs)
530{
531 __asm__ __volatile__ (
532 "ldr r9, [%[regs], #13*4] \n\t"
533 "ldmia %[regs], {r0-r7} \n\t"
534 "blx %[fn] \n\t"
535 "stmia %[regs], {r0-r7} \n\t"
536 "str r9, [%[regs], #13*4] \n\t"
537 :
538 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
539 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
540 "lr", "memory", "cc"
541 );
542}
543
544static void __kprobes
545t16_emulate_pop_pc(struct kprobe *p, struct pt_regs *regs)
546{
547 register unsigned long pc asm("r8");
548
549 __asm__ __volatile__ (
550 "ldr r9, [%[regs], #13*4] \n\t"
551 "ldmia %[regs], {r0-r7} \n\t"
552 "blx %[fn] \n\t"
553 "stmia %[regs], {r0-r7} \n\t"
554 "str r9, [%[regs], #13*4] \n\t"
555 : "=r" (pc)
556 : [regs] "r" (regs), [fn] "r" (p->ainsn.insn_fn)
557 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r9",
558 "lr", "memory", "cc"
559 );
560
561 bx_write_pc(pc, regs);
562}
563
564static enum kprobe_insn __kprobes
565t16_decode_pop(kprobe_opcode_t insn, struct arch_specific_insn *asi)
566{
567 /*
568 * To simulate a POP we use a Thumb-2 "LDMDB R9!, {registers}"
569 * and call it with R9=SP and PC in the register list represented
570 * by R8.
571 */
572 ((u16 *)asi->insn)[0] = 0xe8b9; /* 1st half LDMIA R9!,{} */
573 ((u16 *)asi->insn)[1] = insn & 0x1ff; /* 2nd half (register list) */
574 asi->insn_handler = insn & 0x100 ? t16_emulate_pop_pc
575 : t16_emulate_pop_nopc;
576 return INSN_GOOD;
577}
578
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100579static const union decode_item t16_table_1011[] = {
580 /* Miscellaneous 16-bit instructions */
581
Jon Medhurst2f335822011-07-02 16:05:53 +0100582 /* ADD (SP plus immediate) 1011 0000 0xxx xxxx */
583 /* SUB (SP minus immediate) 1011 0000 1xxx xxxx */
584 DECODE_SIMULATE (0xff00, 0xb000, t16_simulate_add_sp_imm),
585
Jon Medhurst32818f32011-07-02 16:10:44 +0100586 /* CBZ 1011 00x1 xxxx xxxx */
587 /* CBNZ 1011 10x1 xxxx xxxx */
588 DECODE_SIMULATE (0xf500, 0xb100, t16_simulate_cbz),
589
590 /* SXTH 1011 0010 00xx xxxx */
591 /* SXTB 1011 0010 01xx xxxx */
592 /* UXTH 1011 0010 10xx xxxx */
593 /* UXTB 1011 0010 11xx xxxx */
594 /* REV 1011 1010 00xx xxxx */
595 /* REV16 1011 1010 01xx xxxx */
596 /* ??? 1011 1010 10xx xxxx */
597 /* REVSH 1011 1010 11xx xxxx */
598 DECODE_REJECT (0xffc0, 0xba80),
599 DECODE_EMULATE (0xf500, 0xb000, t16_emulate_loregs_rwflags),
600
Jon Medhurstfd0c8d82011-07-02 16:13:29 +0100601 /* PUSH 1011 010x xxxx xxxx */
602 DECODE_CUSTOM (0xfe00, 0xb400, t16_decode_push),
603 /* POP 1011 110x xxxx xxxx */
604 DECODE_CUSTOM (0xfe00, 0xbc00, t16_decode_pop),
605
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100606 /*
607 * If-Then, and hints
608 * 1011 1111 xxxx xxxx
609 */
610
611 /* YIELD 1011 1111 0001 0000 */
612 DECODE_OR (0xffff, 0xbf10),
613 /* SEV 1011 1111 0100 0000 */
614 DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none),
615 /* NOP 1011 1111 0000 0000 */
616 /* WFE 1011 1111 0010 0000 */
617 /* WFI 1011 1111 0011 0000 */
618 DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop),
619 /* Unassigned hints 1011 1111 xxxx 0000 */
620 DECODE_REJECT (0xff0f, 0xbf00),
Jon Medhurst5b94faf2011-07-02 16:16:05 +0100621 /* IT 1011 1111 xxxx xxxx */
622 DECODE_CUSTOM (0xff00, 0xbf00, t16_decode_it),
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100623
Jon Medhurst0a188cc2011-07-02 16:39:07 +0100624 /* SETEND 1011 0110 010x xxxx */
625 /* CPS 1011 0110 011x xxxx */
626 /* BKPT 1011 1110 xxxx xxxx */
627 /* And unallocated instructions... */
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100628 DECODE_END
629};
630
631const union decode_item kprobe_decode_thumb16_table[] = {
632
633 /*
Jon Medhurst02d194f2011-07-02 15:46:05 +0100634 * Shift (immediate), add, subtract, move, and compare
635 * 00xx xxxx xxxx xxxx
636 */
637
638 /* CMP (immediate) 0010 1xxx xxxx xxxx */
639 DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags),
640
641 /* ADD (register) 0001 100x xxxx xxxx */
642 /* SUB (register) 0001 101x xxxx xxxx */
643 /* LSL (immediate) 0000 0xxx xxxx xxxx */
644 /* LSR (immediate) 0000 1xxx xxxx xxxx */
645 /* ASR (immediate) 0001 0xxx xxxx xxxx */
646 /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */
647 /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */
648 /* MOV (immediate) 0010 0xxx xxxx xxxx */
649 /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */
650 /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */
651 DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags),
652
653 /*
654 * 16-bit Thumb data-processing instructions
655 * 0100 00xx xxxx xxxx
656 */
657
658 /* TST (register) 0100 0010 00xx xxxx */
659 DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags),
660 /* CMP (register) 0100 0010 10xx xxxx */
661 /* CMN (register) 0100 0010 11xx xxxx */
662 DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags),
663 /* AND (register) 0100 0000 00xx xxxx */
664 /* EOR (register) 0100 0000 01xx xxxx */
665 /* LSL (register) 0100 0000 10xx xxxx */
666 /* LSR (register) 0100 0000 11xx xxxx */
667 /* ASR (register) 0100 0001 00xx xxxx */
668 /* ADC (register) 0100 0001 01xx xxxx */
669 /* SBC (register) 0100 0001 10xx xxxx */
670 /* ROR (register) 0100 0001 11xx xxxx */
671 /* RSB (immediate) 0100 0010 01xx xxxx */
672 /* ORR (register) 0100 0011 00xx xxxx */
673 /* MUL 0100 0011 00xx xxxx */
674 /* BIC (register) 0100 0011 10xx xxxx */
675 /* MVN (register) 0100 0011 10xx xxxx */
676 DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags),
677
678 /*
Jon Medhursta9c3c292011-07-02 15:51:03 +0100679 * Special data instructions and branch and exchange
680 * 0100 01xx xxxx xxxx
681 */
682
683 /* BLX pc 0100 0111 1111 1xxx */
684 DECODE_REJECT (0xfff8, 0x47f8),
685
686 /* BX (register) 0100 0111 0xxx xxxx */
687 /* BLX (register) 0100 0111 1xxx xxxx */
688 DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx),
689
Jon Medhurst3b5940e2011-07-02 15:54:57 +0100690 /* ADD pc, pc 0100 0100 1111 1111 */
691 DECODE_REJECT (0xffff, 0x44ff),
692
693 /* ADD (register) 0100 0100 xxxx xxxx */
694 /* CMP (register) 0100 0101 xxxx xxxx */
695 /* MOV (register) 0100 0110 xxxx xxxx */
696 DECODE_CUSTOM (0xfc00, 0x4400, t16_decode_hiregs),
697
Jon Medhursta9c3c292011-07-02 15:51:03 +0100698 /*
Jon Medhurstf8695142011-07-02 16:00:09 +0100699 * Load from Literal Pool
700 * LDR (literal) 0100 1xxx xxxx xxxx
701 */
702 DECODE_SIMULATE (0xf800, 0x4800, t16_simulate_ldr_literal),
703
704 /*
705 * 16-bit Thumb Load/store instructions
706 * 0101 xxxx xxxx xxxx
707 * 011x xxxx xxxx xxxx
708 * 100x xxxx xxxx xxxx
709 */
710
711 /* STR (register) 0101 000x xxxx xxxx */
712 /* STRH (register) 0101 001x xxxx xxxx */
713 /* STRB (register) 0101 010x xxxx xxxx */
714 /* LDRSB (register) 0101 011x xxxx xxxx */
715 /* LDR (register) 0101 100x xxxx xxxx */
716 /* LDRH (register) 0101 101x xxxx xxxx */
717 /* LDRB (register) 0101 110x xxxx xxxx */
718 /* LDRSH (register) 0101 111x xxxx xxxx */
719 /* STR (immediate, Thumb) 0110 0xxx xxxx xxxx */
720 /* LDR (immediate, Thumb) 0110 1xxx xxxx xxxx */
721 /* STRB (immediate, Thumb) 0111 0xxx xxxx xxxx */
722 /* LDRB (immediate, Thumb) 0111 1xxx xxxx xxxx */
723 DECODE_EMULATE (0xc000, 0x4000, t16_emulate_loregs_rwflags),
724 /* STRH (immediate, Thumb) 1000 0xxx xxxx xxxx */
725 /* LDRH (immediate, Thumb) 1000 1xxx xxxx xxxx */
726 DECODE_EMULATE (0xf000, 0x8000, t16_emulate_loregs_rwflags),
727 /* STR (immediate, Thumb) 1001 0xxx xxxx xxxx */
728 /* LDR (immediate, Thumb) 1001 1xxx xxxx xxxx */
729 DECODE_SIMULATE (0xf000, 0x9000, t16_simulate_ldrstr_sp_relative),
730
731 /*
Jon Medhurst2f335822011-07-02 16:05:53 +0100732 * Generate PC-/SP-relative address
733 * ADR (literal) 1010 0xxx xxxx xxxx
734 * ADD (SP plus immediate) 1010 1xxx xxxx xxxx
735 */
736 DECODE_SIMULATE (0xf000, 0xa000, t16_simulate_reladr),
737
738 /*
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100739 * Miscellaneous 16-bit instructions
740 * 1011 xxxx xxxx xxxx
741 */
742 DECODE_TABLE (0xf000, 0xb000, t16_table_1011),
743
Jon Medhurstf8695142011-07-02 16:00:09 +0100744 /* STM 1100 0xxx xxxx xxxx */
745 /* LDM 1100 1xxx xxxx xxxx */
746 DECODE_EMULATE (0xf000, 0xc000, t16_emulate_loregs_rwflags),
747
Jon Medhurst44495662011-07-02 16:25:47 +0100748 /*
749 * Conditional branch, and Supervisor Call
750 */
751
752 /* Permanently UNDEFINED 1101 1110 xxxx xxxx */
753 /* SVC 1101 1111 xxxx xxxx */
754 DECODE_REJECT (0xfe00, 0xde00),
755
Jon Medhurst396b41f2011-07-02 16:30:43 +0100756 /* Conditional branch 1101 xxxx xxxx xxxx */
757 DECODE_CUSTOM (0xf000, 0xd000, t16_decode_cond_branch),
758
759 /*
760 * Unconditional branch
761 * B 1110 0xxx xxxx xxxx
762 */
763 DECODE_SIMULATE (0xf800, 0xe000, t16_simulate_branch),
764
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100765 DECODE_END
766};
767
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100768static unsigned long __kprobes thumb_check_cc(unsigned long cpsr)
769{
770 if (unlikely(in_it_block(cpsr)))
771 return kprobe_condition_checks[current_cond(cpsr)](cpsr);
772 return true;
773}
774
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100775static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs)
776{
777 regs->ARM_pc += 2;
778 p->ainsn.insn_handler(p, regs);
779 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
780}
781
782static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs)
783{
784 regs->ARM_pc += 4;
785 p->ainsn.insn_handler(p, regs);
786 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
787}
788
Jon Medhurst24371702011-04-19 17:56:58 +0100789enum kprobe_insn __kprobes
790thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
791{
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100792 asi->insn_singlestep = thumb16_singlestep;
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100793 asi->insn_check_cc = thumb_check_cc;
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100794 return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true);
Jon Medhurst24371702011-04-19 17:56:58 +0100795}
796
797enum kprobe_insn __kprobes
798thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
799{
Jon Medhurstc6a7d972011-06-09 12:11:27 +0100800 asi->insn_singlestep = thumb32_singlestep;
Jon Medhursteaf4f33f2011-04-20 19:29:52 +0100801 asi->insn_check_cc = thumb_check_cc;
Jon Medhurstf39ca8b2011-07-03 13:55:47 +0100802 return kprobe_decode_insn(insn, asi, kprobe_decode_thumb32_table, true);
Jon Medhurst24371702011-04-19 17:56:58 +0100803}