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 | 02d194f | 2011-07-02 15:46:05 +0100 | [diff] [blame] | 54 | static unsigned long __kprobes |
| 55 | t16_emulate_loregs(struct kprobe *p, struct pt_regs *regs) |
| 56 | { |
| 57 | unsigned long oldcpsr = regs->ARM_cpsr; |
| 58 | unsigned long newcpsr; |
| 59 | |
| 60 | __asm__ __volatile__ ( |
| 61 | "msr cpsr_fs, %[oldcpsr] \n\t" |
| 62 | "ldmia %[regs], {r0-r7} \n\t" |
| 63 | "blx %[fn] \n\t" |
| 64 | "stmia %[regs], {r0-r7} \n\t" |
| 65 | "mrs %[newcpsr], cpsr \n\t" |
| 66 | : [newcpsr] "=r" (newcpsr) |
| 67 | : [oldcpsr] "r" (oldcpsr), [regs] "r" (regs), |
| 68 | [fn] "r" (p->ainsn.insn_fn) |
| 69 | : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", |
| 70 | "lr", "memory", "cc" |
| 71 | ); |
| 72 | |
| 73 | return (oldcpsr & ~APSR_MASK) | (newcpsr & APSR_MASK); |
| 74 | } |
| 75 | |
| 76 | static void __kprobes |
| 77 | t16_emulate_loregs_rwflags(struct kprobe *p, struct pt_regs *regs) |
| 78 | { |
| 79 | regs->ARM_cpsr = t16_emulate_loregs(p, regs); |
| 80 | } |
| 81 | |
| 82 | static void __kprobes |
| 83 | t16_emulate_loregs_noitrwflags(struct kprobe *p, struct pt_regs *regs) |
| 84 | { |
| 85 | unsigned long cpsr = t16_emulate_loregs(p, regs); |
| 86 | if (!in_it_block(cpsr)) |
| 87 | regs->ARM_cpsr = cpsr; |
| 88 | } |
| 89 | |
Jon Medhurst | 3f92dfe | 2011-07-02 15:36:32 +0100 | [diff] [blame] | 90 | static const union decode_item t16_table_1011[] = { |
| 91 | /* Miscellaneous 16-bit instructions */ |
| 92 | |
| 93 | /* |
| 94 | * If-Then, and hints |
| 95 | * 1011 1111 xxxx xxxx |
| 96 | */ |
| 97 | |
| 98 | /* YIELD 1011 1111 0001 0000 */ |
| 99 | DECODE_OR (0xffff, 0xbf10), |
| 100 | /* SEV 1011 1111 0100 0000 */ |
| 101 | DECODE_EMULATE (0xffff, 0xbf40, kprobe_emulate_none), |
| 102 | /* NOP 1011 1111 0000 0000 */ |
| 103 | /* WFE 1011 1111 0010 0000 */ |
| 104 | /* WFI 1011 1111 0011 0000 */ |
| 105 | DECODE_SIMULATE (0xffcf, 0xbf00, kprobe_simulate_nop), |
| 106 | /* Unassigned hints 1011 1111 xxxx 0000 */ |
| 107 | DECODE_REJECT (0xff0f, 0xbf00), |
| 108 | |
| 109 | DECODE_END |
| 110 | }; |
| 111 | |
| 112 | const union decode_item kprobe_decode_thumb16_table[] = { |
| 113 | |
| 114 | /* |
Jon Medhurst | 02d194f | 2011-07-02 15:46:05 +0100 | [diff] [blame] | 115 | * Shift (immediate), add, subtract, move, and compare |
| 116 | * 00xx xxxx xxxx xxxx |
| 117 | */ |
| 118 | |
| 119 | /* CMP (immediate) 0010 1xxx xxxx xxxx */ |
| 120 | DECODE_EMULATE (0xf800, 0x2800, t16_emulate_loregs_rwflags), |
| 121 | |
| 122 | /* ADD (register) 0001 100x xxxx xxxx */ |
| 123 | /* SUB (register) 0001 101x xxxx xxxx */ |
| 124 | /* LSL (immediate) 0000 0xxx xxxx xxxx */ |
| 125 | /* LSR (immediate) 0000 1xxx xxxx xxxx */ |
| 126 | /* ASR (immediate) 0001 0xxx xxxx xxxx */ |
| 127 | /* ADD (immediate, Thumb) 0001 110x xxxx xxxx */ |
| 128 | /* SUB (immediate, Thumb) 0001 111x xxxx xxxx */ |
| 129 | /* MOV (immediate) 0010 0xxx xxxx xxxx */ |
| 130 | /* ADD (immediate, Thumb) 0011 0xxx xxxx xxxx */ |
| 131 | /* SUB (immediate, Thumb) 0011 1xxx xxxx xxxx */ |
| 132 | DECODE_EMULATE (0xc000, 0x0000, t16_emulate_loregs_noitrwflags), |
| 133 | |
| 134 | /* |
| 135 | * 16-bit Thumb data-processing instructions |
| 136 | * 0100 00xx xxxx xxxx |
| 137 | */ |
| 138 | |
| 139 | /* TST (register) 0100 0010 00xx xxxx */ |
| 140 | DECODE_EMULATE (0xffc0, 0x4200, t16_emulate_loregs_rwflags), |
| 141 | /* CMP (register) 0100 0010 10xx xxxx */ |
| 142 | /* CMN (register) 0100 0010 11xx xxxx */ |
| 143 | DECODE_EMULATE (0xff80, 0x4280, t16_emulate_loregs_rwflags), |
| 144 | /* AND (register) 0100 0000 00xx xxxx */ |
| 145 | /* EOR (register) 0100 0000 01xx xxxx */ |
| 146 | /* LSL (register) 0100 0000 10xx xxxx */ |
| 147 | /* LSR (register) 0100 0000 11xx xxxx */ |
| 148 | /* ASR (register) 0100 0001 00xx xxxx */ |
| 149 | /* ADC (register) 0100 0001 01xx xxxx */ |
| 150 | /* SBC (register) 0100 0001 10xx xxxx */ |
| 151 | /* ROR (register) 0100 0001 11xx xxxx */ |
| 152 | /* RSB (immediate) 0100 0010 01xx xxxx */ |
| 153 | /* ORR (register) 0100 0011 00xx xxxx */ |
| 154 | /* MUL 0100 0011 00xx xxxx */ |
| 155 | /* BIC (register) 0100 0011 10xx xxxx */ |
| 156 | /* MVN (register) 0100 0011 10xx xxxx */ |
| 157 | DECODE_EMULATE (0xfc00, 0x4000, t16_emulate_loregs_noitrwflags), |
| 158 | |
| 159 | /* |
Jon Medhurst | a9c3c29 | 2011-07-02 15:51:03 +0100 | [diff] [blame^] | 160 | * Special data instructions and branch and exchange |
| 161 | * 0100 01xx xxxx xxxx |
| 162 | */ |
| 163 | |
| 164 | /* BLX pc 0100 0111 1111 1xxx */ |
| 165 | DECODE_REJECT (0xfff8, 0x47f8), |
| 166 | |
| 167 | /* BX (register) 0100 0111 0xxx xxxx */ |
| 168 | /* BLX (register) 0100 0111 1xxx xxxx */ |
| 169 | DECODE_SIMULATE (0xff00, 0x4700, t16_simulate_bxblx), |
| 170 | |
| 171 | /* |
Jon Medhurst | 3f92dfe | 2011-07-02 15:36:32 +0100 | [diff] [blame] | 172 | * Miscellaneous 16-bit instructions |
| 173 | * 1011 xxxx xxxx xxxx |
| 174 | */ |
| 175 | DECODE_TABLE (0xf000, 0xb000, t16_table_1011), |
| 176 | |
| 177 | DECODE_END |
| 178 | }; |
| 179 | |
Jon Medhurst | eaf4f33f | 2011-04-20 19:29:52 +0100 | [diff] [blame] | 180 | static unsigned long __kprobes thumb_check_cc(unsigned long cpsr) |
| 181 | { |
| 182 | if (unlikely(in_it_block(cpsr))) |
| 183 | return kprobe_condition_checks[current_cond(cpsr)](cpsr); |
| 184 | return true; |
| 185 | } |
| 186 | |
Jon Medhurst | c6a7d97 | 2011-06-09 12:11:27 +0100 | [diff] [blame] | 187 | static void __kprobes thumb16_singlestep(struct kprobe *p, struct pt_regs *regs) |
| 188 | { |
| 189 | regs->ARM_pc += 2; |
| 190 | p->ainsn.insn_handler(p, regs); |
| 191 | regs->ARM_cpsr = it_advance(regs->ARM_cpsr); |
| 192 | } |
| 193 | |
| 194 | static void __kprobes thumb32_singlestep(struct kprobe *p, struct pt_regs *regs) |
| 195 | { |
| 196 | regs->ARM_pc += 4; |
| 197 | p->ainsn.insn_handler(p, regs); |
| 198 | regs->ARM_cpsr = it_advance(regs->ARM_cpsr); |
| 199 | } |
| 200 | |
Jon Medhurst | 2437170 | 2011-04-19 17:56:58 +0100 | [diff] [blame] | 201 | enum kprobe_insn __kprobes |
| 202 | thumb16_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 203 | { |
Jon Medhurst | c6a7d97 | 2011-06-09 12:11:27 +0100 | [diff] [blame] | 204 | asi->insn_singlestep = thumb16_singlestep; |
Jon Medhurst | eaf4f33f | 2011-04-20 19:29:52 +0100 | [diff] [blame] | 205 | asi->insn_check_cc = thumb_check_cc; |
Jon Medhurst | 3f92dfe | 2011-07-02 15:36:32 +0100 | [diff] [blame] | 206 | return kprobe_decode_insn(insn, asi, kprobe_decode_thumb16_table, true); |
Jon Medhurst | 2437170 | 2011-04-19 17:56:58 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | enum kprobe_insn __kprobes |
| 210 | thumb32_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 211 | { |
Jon Medhurst | c6a7d97 | 2011-06-09 12:11:27 +0100 | [diff] [blame] | 212 | asi->insn_singlestep = thumb32_singlestep; |
Jon Medhurst | eaf4f33f | 2011-04-20 19:29:52 +0100 | [diff] [blame] | 213 | asi->insn_check_cc = thumb_check_cc; |
Jon Medhurst | 2437170 | 2011-04-19 17:56:58 +0100 | [diff] [blame] | 214 | return INSN_REJECTED; |
| 215 | } |