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