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