Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/kernel/kprobes-decode.c |
| 3 | * |
| 4 | * Copyright (C) 2006, 2007 Motorola Inc. |
| 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 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * We do not have hardware single-stepping on ARM, This |
| 18 | * effort is further complicated by the ARM not having a |
| 19 | * "next PC" register. Instructions that change the PC |
| 20 | * can't be safely single-stepped in a MP environment, so |
| 21 | * we have a lot of work to do: |
| 22 | * |
| 23 | * In the prepare phase: |
| 24 | * *) If it is an instruction that does anything |
| 25 | * with the CPU mode, we reject it for a kprobe. |
| 26 | * (This is out of laziness rather than need. The |
| 27 | * instructions could be simulated.) |
| 28 | * |
| 29 | * *) Otherwise, decode the instruction rewriting its |
| 30 | * registers to take fixed, ordered registers and |
| 31 | * setting a handler for it to run the instruction. |
| 32 | * |
| 33 | * In the execution phase by an instruction's handler: |
| 34 | * |
| 35 | * *) If the PC is written to by the instruction, the |
| 36 | * instruction must be fully simulated in software. |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 37 | * |
| 38 | * *) Otherwise, a modified form of the instruction is |
| 39 | * directly executed. Its handler calls the |
| 40 | * instruction in insn[0]. In insn[1] is a |
| 41 | * "mov pc, lr" to return. |
| 42 | * |
| 43 | * Before calling, load up the reordered registers |
| 44 | * from the original instruction's registers. If one |
| 45 | * of the original input registers is the PC, compute |
| 46 | * and adjust the appropriate input register. |
| 47 | * |
| 48 | * After call completes, copy the output registers to |
| 49 | * the original instruction's original registers. |
| 50 | * |
| 51 | * We don't use a real breakpoint instruction since that |
| 52 | * would have us in the kernel go from SVC mode to SVC |
| 53 | * mode losing the link register. Instead we use an |
| 54 | * undefined instruction. To simplify processing, the |
| 55 | * undefined instruction used for kprobes must be reserved |
| 56 | * exclusively for kprobes use. |
| 57 | * |
| 58 | * TODO: ifdef out some instruction decoding based on architecture. |
| 59 | */ |
| 60 | |
| 61 | #include <linux/kernel.h> |
| 62 | #include <linux/kprobes.h> |
| 63 | |
Jon Medhurst | 221bf15 | 2011-04-20 10:52:38 +0100 | [diff] [blame] | 64 | #include "kprobes.h" |
| 65 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 66 | #define sign_extend(x, signbit) ((x) | (0 - ((x) & (1 << (signbit))))) |
| 67 | |
| 68 | #define branch_displacement(insn) sign_extend(((insn) & 0xffffff) << 2, 25) |
| 69 | |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 70 | #define is_r15(insn, bitpos) (((insn) & (0xf << bitpos)) == (0xf << bitpos)) |
| 71 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 72 | #define PSR_fs (PSR_f|PSR_s) |
| 73 | |
| 74 | #define KPROBE_RETURN_INSTRUCTION 0xe1a0f00e /* mov pc, lr */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 75 | |
| 76 | typedef long (insn_0arg_fn_t)(void); |
| 77 | typedef long (insn_1arg_fn_t)(long); |
| 78 | typedef long (insn_2arg_fn_t)(long, long); |
| 79 | typedef long (insn_3arg_fn_t)(long, long, long); |
| 80 | typedef long (insn_4arg_fn_t)(long, long, long, long); |
| 81 | typedef long long (insn_llret_0arg_fn_t)(void); |
| 82 | typedef long long (insn_llret_3arg_fn_t)(long, long, long); |
| 83 | typedef long long (insn_llret_4arg_fn_t)(long, long, long, long); |
| 84 | |
| 85 | union reg_pair { |
| 86 | long long dr; |
| 87 | #ifdef __LITTLE_ENDIAN |
| 88 | struct { long r0, r1; }; |
| 89 | #else |
| 90 | struct { long r1, r0; }; |
| 91 | #endif |
| 92 | }; |
| 93 | |
| 94 | /* |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 95 | * The insnslot_?arg_r[w]flags() functions below are to keep the |
| 96 | * msr -> *fn -> mrs instruction sequences indivisible so that |
| 97 | * the state of the CPSR flags aren't inadvertently modified |
| 98 | * just before or just after the call. |
| 99 | */ |
| 100 | |
| 101 | static inline long __kprobes |
| 102 | insnslot_0arg_rflags(long cpsr, insn_0arg_fn_t *fn) |
| 103 | { |
| 104 | register long ret asm("r0"); |
| 105 | |
| 106 | __asm__ __volatile__ ( |
| 107 | "msr cpsr_fs, %[cpsr] \n\t" |
| 108 | "mov lr, pc \n\t" |
| 109 | "mov pc, %[fn] \n\t" |
| 110 | : "=r" (ret) |
| 111 | : [cpsr] "r" (cpsr), [fn] "r" (fn) |
| 112 | : "lr", "cc" |
| 113 | ); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static inline long long __kprobes |
| 118 | insnslot_llret_0arg_rflags(long cpsr, insn_llret_0arg_fn_t *fn) |
| 119 | { |
| 120 | register long ret0 asm("r0"); |
| 121 | register long ret1 asm("r1"); |
| 122 | union reg_pair fnr; |
| 123 | |
| 124 | __asm__ __volatile__ ( |
| 125 | "msr cpsr_fs, %[cpsr] \n\t" |
| 126 | "mov lr, pc \n\t" |
| 127 | "mov pc, %[fn] \n\t" |
| 128 | : "=r" (ret0), "=r" (ret1) |
| 129 | : [cpsr] "r" (cpsr), [fn] "r" (fn) |
| 130 | : "lr", "cc" |
| 131 | ); |
| 132 | fnr.r0 = ret0; |
| 133 | fnr.r1 = ret1; |
| 134 | return fnr.dr; |
| 135 | } |
| 136 | |
| 137 | static inline long __kprobes |
| 138 | insnslot_1arg_rflags(long r0, long cpsr, insn_1arg_fn_t *fn) |
| 139 | { |
| 140 | register long rr0 asm("r0") = r0; |
| 141 | register long ret asm("r0"); |
| 142 | |
| 143 | __asm__ __volatile__ ( |
| 144 | "msr cpsr_fs, %[cpsr] \n\t" |
| 145 | "mov lr, pc \n\t" |
| 146 | "mov pc, %[fn] \n\t" |
| 147 | : "=r" (ret) |
| 148 | : "0" (rr0), [cpsr] "r" (cpsr), [fn] "r" (fn) |
| 149 | : "lr", "cc" |
| 150 | ); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | static inline long __kprobes |
| 155 | insnslot_2arg_rflags(long r0, long r1, long cpsr, insn_2arg_fn_t *fn) |
| 156 | { |
| 157 | register long rr0 asm("r0") = r0; |
| 158 | register long rr1 asm("r1") = r1; |
| 159 | register long ret asm("r0"); |
| 160 | |
| 161 | __asm__ __volatile__ ( |
| 162 | "msr cpsr_fs, %[cpsr] \n\t" |
| 163 | "mov lr, pc \n\t" |
| 164 | "mov pc, %[fn] \n\t" |
| 165 | : "=r" (ret) |
| 166 | : "0" (rr0), "r" (rr1), |
| 167 | [cpsr] "r" (cpsr), [fn] "r" (fn) |
| 168 | : "lr", "cc" |
| 169 | ); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static inline long __kprobes |
| 174 | insnslot_3arg_rflags(long r0, long r1, long r2, long cpsr, insn_3arg_fn_t *fn) |
| 175 | { |
| 176 | register long rr0 asm("r0") = r0; |
| 177 | register long rr1 asm("r1") = r1; |
| 178 | register long rr2 asm("r2") = r2; |
| 179 | register long ret asm("r0"); |
| 180 | |
| 181 | __asm__ __volatile__ ( |
| 182 | "msr cpsr_fs, %[cpsr] \n\t" |
| 183 | "mov lr, pc \n\t" |
| 184 | "mov pc, %[fn] \n\t" |
| 185 | : "=r" (ret) |
| 186 | : "0" (rr0), "r" (rr1), "r" (rr2), |
| 187 | [cpsr] "r" (cpsr), [fn] "r" (fn) |
| 188 | : "lr", "cc" |
| 189 | ); |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static inline long long __kprobes |
| 194 | insnslot_llret_3arg_rflags(long r0, long r1, long r2, long cpsr, |
| 195 | insn_llret_3arg_fn_t *fn) |
| 196 | { |
| 197 | register long rr0 asm("r0") = r0; |
| 198 | register long rr1 asm("r1") = r1; |
| 199 | register long rr2 asm("r2") = r2; |
| 200 | register long ret0 asm("r0"); |
| 201 | register long ret1 asm("r1"); |
| 202 | union reg_pair fnr; |
| 203 | |
| 204 | __asm__ __volatile__ ( |
| 205 | "msr cpsr_fs, %[cpsr] \n\t" |
| 206 | "mov lr, pc \n\t" |
| 207 | "mov pc, %[fn] \n\t" |
| 208 | : "=r" (ret0), "=r" (ret1) |
| 209 | : "0" (rr0), "r" (rr1), "r" (rr2), |
| 210 | [cpsr] "r" (cpsr), [fn] "r" (fn) |
| 211 | : "lr", "cc" |
| 212 | ); |
| 213 | fnr.r0 = ret0; |
| 214 | fnr.r1 = ret1; |
| 215 | return fnr.dr; |
| 216 | } |
| 217 | |
| 218 | static inline long __kprobes |
| 219 | insnslot_4arg_rflags(long r0, long r1, long r2, long r3, long cpsr, |
| 220 | insn_4arg_fn_t *fn) |
| 221 | { |
| 222 | register long rr0 asm("r0") = r0; |
| 223 | register long rr1 asm("r1") = r1; |
| 224 | register long rr2 asm("r2") = r2; |
| 225 | register long rr3 asm("r3") = r3; |
| 226 | register long ret asm("r0"); |
| 227 | |
| 228 | __asm__ __volatile__ ( |
| 229 | "msr cpsr_fs, %[cpsr] \n\t" |
| 230 | "mov lr, pc \n\t" |
| 231 | "mov pc, %[fn] \n\t" |
| 232 | : "=r" (ret) |
| 233 | : "0" (rr0), "r" (rr1), "r" (rr2), "r" (rr3), |
| 234 | [cpsr] "r" (cpsr), [fn] "r" (fn) |
| 235 | : "lr", "cc" |
| 236 | ); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static inline long __kprobes |
| 241 | insnslot_1arg_rwflags(long r0, long *cpsr, insn_1arg_fn_t *fn) |
| 242 | { |
| 243 | register long rr0 asm("r0") = r0; |
| 244 | register long ret asm("r0"); |
| 245 | long oldcpsr = *cpsr; |
| 246 | long newcpsr; |
| 247 | |
| 248 | __asm__ __volatile__ ( |
| 249 | "msr cpsr_fs, %[oldcpsr] \n\t" |
| 250 | "mov lr, pc \n\t" |
| 251 | "mov pc, %[fn] \n\t" |
| 252 | "mrs %[newcpsr], cpsr \n\t" |
| 253 | : "=r" (ret), [newcpsr] "=r" (newcpsr) |
| 254 | : "0" (rr0), [oldcpsr] "r" (oldcpsr), [fn] "r" (fn) |
| 255 | : "lr", "cc" |
| 256 | ); |
| 257 | *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs); |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | static inline long __kprobes |
| 262 | insnslot_2arg_rwflags(long r0, long r1, long *cpsr, insn_2arg_fn_t *fn) |
| 263 | { |
| 264 | register long rr0 asm("r0") = r0; |
| 265 | register long rr1 asm("r1") = r1; |
| 266 | register long ret asm("r0"); |
| 267 | long oldcpsr = *cpsr; |
| 268 | long newcpsr; |
| 269 | |
| 270 | __asm__ __volatile__ ( |
| 271 | "msr cpsr_fs, %[oldcpsr] \n\t" |
| 272 | "mov lr, pc \n\t" |
| 273 | "mov pc, %[fn] \n\t" |
| 274 | "mrs %[newcpsr], cpsr \n\t" |
| 275 | : "=r" (ret), [newcpsr] "=r" (newcpsr) |
| 276 | : "0" (rr0), "r" (rr1), [oldcpsr] "r" (oldcpsr), [fn] "r" (fn) |
| 277 | : "lr", "cc" |
| 278 | ); |
| 279 | *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static inline long __kprobes |
| 284 | insnslot_3arg_rwflags(long r0, long r1, long r2, long *cpsr, |
| 285 | insn_3arg_fn_t *fn) |
| 286 | { |
| 287 | register long rr0 asm("r0") = r0; |
| 288 | register long rr1 asm("r1") = r1; |
| 289 | register long rr2 asm("r2") = r2; |
| 290 | register long ret asm("r0"); |
| 291 | long oldcpsr = *cpsr; |
| 292 | long newcpsr; |
| 293 | |
| 294 | __asm__ __volatile__ ( |
| 295 | "msr cpsr_fs, %[oldcpsr] \n\t" |
| 296 | "mov lr, pc \n\t" |
| 297 | "mov pc, %[fn] \n\t" |
| 298 | "mrs %[newcpsr], cpsr \n\t" |
| 299 | : "=r" (ret), [newcpsr] "=r" (newcpsr) |
| 300 | : "0" (rr0), "r" (rr1), "r" (rr2), |
| 301 | [oldcpsr] "r" (oldcpsr), [fn] "r" (fn) |
| 302 | : "lr", "cc" |
| 303 | ); |
| 304 | *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static inline long __kprobes |
| 309 | insnslot_4arg_rwflags(long r0, long r1, long r2, long r3, long *cpsr, |
| 310 | insn_4arg_fn_t *fn) |
| 311 | { |
| 312 | register long rr0 asm("r0") = r0; |
| 313 | register long rr1 asm("r1") = r1; |
| 314 | register long rr2 asm("r2") = r2; |
| 315 | register long rr3 asm("r3") = r3; |
| 316 | register long ret asm("r0"); |
| 317 | long oldcpsr = *cpsr; |
| 318 | long newcpsr; |
| 319 | |
| 320 | __asm__ __volatile__ ( |
| 321 | "msr cpsr_fs, %[oldcpsr] \n\t" |
| 322 | "mov lr, pc \n\t" |
| 323 | "mov pc, %[fn] \n\t" |
| 324 | "mrs %[newcpsr], cpsr \n\t" |
| 325 | : "=r" (ret), [newcpsr] "=r" (newcpsr) |
| 326 | : "0" (rr0), "r" (rr1), "r" (rr2), "r" (rr3), |
| 327 | [oldcpsr] "r" (oldcpsr), [fn] "r" (fn) |
| 328 | : "lr", "cc" |
| 329 | ); |
| 330 | *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static inline long long __kprobes |
| 335 | insnslot_llret_4arg_rwflags(long r0, long r1, long r2, long r3, long *cpsr, |
| 336 | insn_llret_4arg_fn_t *fn) |
| 337 | { |
| 338 | register long rr0 asm("r0") = r0; |
| 339 | register long rr1 asm("r1") = r1; |
| 340 | register long rr2 asm("r2") = r2; |
| 341 | register long rr3 asm("r3") = r3; |
| 342 | register long ret0 asm("r0"); |
| 343 | register long ret1 asm("r1"); |
| 344 | long oldcpsr = *cpsr; |
| 345 | long newcpsr; |
| 346 | union reg_pair fnr; |
| 347 | |
| 348 | __asm__ __volatile__ ( |
| 349 | "msr cpsr_fs, %[oldcpsr] \n\t" |
| 350 | "mov lr, pc \n\t" |
| 351 | "mov pc, %[fn] \n\t" |
| 352 | "mrs %[newcpsr], cpsr \n\t" |
| 353 | : "=r" (ret0), "=r" (ret1), [newcpsr] "=r" (newcpsr) |
| 354 | : "0" (rr0), "r" (rr1), "r" (rr2), "r" (rr3), |
| 355 | [oldcpsr] "r" (oldcpsr), [fn] "r" (fn) |
| 356 | : "lr", "cc" |
| 357 | ); |
| 358 | *cpsr = (oldcpsr & ~PSR_fs) | (newcpsr & PSR_fs); |
| 359 | fnr.r0 = ret0; |
| 360 | fnr.r1 = ret1; |
| 361 | return fnr.dr; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * To avoid the complications of mimicing single-stepping on a |
| 366 | * processor without a Next-PC or a single-step mode, and to |
| 367 | * avoid having to deal with the side-effects of boosting, we |
| 368 | * simulate or emulate (almost) all ARM instructions. |
| 369 | * |
| 370 | * "Simulation" is where the instruction's behavior is duplicated in |
| 371 | * C code. "Emulation" is where the original instruction is rewritten |
| 372 | * and executed, often by altering its registers. |
| 373 | * |
| 374 | * By having all behavior of the kprobe'd instruction completed before |
| 375 | * returning from the kprobe_handler(), all locks (scheduler and |
| 376 | * interrupt) can safely be released. There is no need for secondary |
| 377 | * breakpoints, no race with MP or preemptable kernels, nor having to |
| 378 | * clean up resources counts at a later time impacting overall system |
| 379 | * performance. By rewriting the instruction, only the minimum registers |
| 380 | * need to be loaded and saved back optimizing performance. |
| 381 | * |
| 382 | * Calling the insnslot_*_rwflags version of a function doesn't hurt |
| 383 | * anything even when the CPSR flags aren't updated by the |
| 384 | * instruction. It's just a little slower in return for saving |
| 385 | * a little space by not having a duplicate function that doesn't |
| 386 | * update the flags. (The same optimization can be said for |
| 387 | * instructions that do or don't perform register writeback) |
| 388 | * Also, instructions can either read the flags, only write the |
| 389 | * flags, or read and write the flags. To save combinations |
| 390 | * rather than for sheer performance, flag functions just assume |
| 391 | * read and write of flags. |
| 392 | */ |
| 393 | |
| 394 | static void __kprobes simulate_bbl(struct kprobe *p, struct pt_regs *regs) |
| 395 | { |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 396 | kprobe_opcode_t insn = p->opcode; |
| 397 | long iaddr = (long)p->addr; |
| 398 | int disp = branch_displacement(insn); |
| 399 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 400 | if (insn & (1 << 24)) |
| 401 | regs->ARM_lr = iaddr + 4; |
| 402 | |
| 403 | regs->ARM_pc = iaddr + 8 + disp; |
| 404 | } |
| 405 | |
| 406 | static void __kprobes simulate_blx1(struct kprobe *p, struct pt_regs *regs) |
| 407 | { |
| 408 | kprobe_opcode_t insn = p->opcode; |
| 409 | long iaddr = (long)p->addr; |
| 410 | int disp = branch_displacement(insn); |
| 411 | |
| 412 | regs->ARM_lr = iaddr + 4; |
| 413 | regs->ARM_pc = iaddr + 8 + disp + ((insn >> 23) & 0x2); |
| 414 | regs->ARM_cpsr |= PSR_T_BIT; |
| 415 | } |
| 416 | |
| 417 | static void __kprobes simulate_blx2bx(struct kprobe *p, struct pt_regs *regs) |
| 418 | { |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 419 | kprobe_opcode_t insn = p->opcode; |
| 420 | int rm = insn & 0xf; |
| 421 | long rmv = regs->uregs[rm]; |
| 422 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 423 | if (insn & (1 << 5)) |
| 424 | regs->ARM_lr = (long)p->addr + 4; |
| 425 | |
| 426 | regs->ARM_pc = rmv & ~0x1; |
| 427 | regs->ARM_cpsr &= ~PSR_T_BIT; |
| 428 | if (rmv & 0x1) |
| 429 | regs->ARM_cpsr |= PSR_T_BIT; |
| 430 | } |
| 431 | |
Jon Medhurst | c412aba | 2011-04-07 13:25:16 +0100 | [diff] [blame] | 432 | static void __kprobes simulate_mrs(struct kprobe *p, struct pt_regs *regs) |
| 433 | { |
| 434 | kprobe_opcode_t insn = p->opcode; |
| 435 | int rd = (insn >> 12) & 0xf; |
| 436 | unsigned long mask = 0xf8ff03df; /* Mask out execution state */ |
| 437 | regs->uregs[rd] = regs->ARM_cpsr & mask; |
| 438 | } |
| 439 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 440 | static void __kprobes simulate_mov_ipsp(struct kprobe *p, struct pt_regs *regs) |
| 441 | { |
| 442 | regs->uregs[12] = regs->uregs[13]; |
| 443 | } |
| 444 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 445 | static void __kprobes emulate_ldrd(struct kprobe *p, struct pt_regs *regs) |
| 446 | { |
| 447 | insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0]; |
| 448 | kprobe_opcode_t insn = p->opcode; |
Viktor Rosendahl | cf3cc1a | 2011-03-28 18:56:05 +0300 | [diff] [blame] | 449 | long ppc = (long)p->addr + 8; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 450 | int rd = (insn >> 12) & 0xf; |
| 451 | int rn = (insn >> 16) & 0xf; |
| 452 | int rm = insn & 0xf; /* rm may be invalid, don't care. */ |
Viktor Rosendahl | cf3cc1a | 2011-03-28 18:56:05 +0300 | [diff] [blame] | 453 | long rmv = (rm == 15) ? ppc : regs->uregs[rm]; |
| 454 | long rnv = (rn == 15) ? ppc : regs->uregs[rn]; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 455 | |
| 456 | /* Not following the C calling convention here, so need asm(). */ |
| 457 | __asm__ __volatile__ ( |
| 458 | "ldr r0, %[rn] \n\t" |
| 459 | "ldr r1, %[rm] \n\t" |
| 460 | "msr cpsr_fs, %[cpsr]\n\t" |
| 461 | "mov lr, pc \n\t" |
| 462 | "mov pc, %[i_fn] \n\t" |
| 463 | "str r0, %[rn] \n\t" /* in case of writeback */ |
| 464 | "str r2, %[rd0] \n\t" |
| 465 | "str r3, %[rd1] \n\t" |
Viktor Rosendahl | cf3cc1a | 2011-03-28 18:56:05 +0300 | [diff] [blame] | 466 | : [rn] "+m" (rnv), |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 467 | [rd0] "=m" (regs->uregs[rd]), |
| 468 | [rd1] "=m" (regs->uregs[rd+1]) |
Viktor Rosendahl | cf3cc1a | 2011-03-28 18:56:05 +0300 | [diff] [blame] | 469 | : [rm] "m" (rmv), |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 470 | [cpsr] "r" (regs->ARM_cpsr), |
| 471 | [i_fn] "r" (i_fn) |
| 472 | : "r0", "r1", "r2", "r3", "lr", "cc" |
| 473 | ); |
Jon Medhurst | 5c6b76f | 2011-04-08 15:32:56 +0100 | [diff] [blame] | 474 | if (is_writeback(insn)) |
| 475 | regs->uregs[rn] = rnv; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static void __kprobes emulate_strd(struct kprobe *p, struct pt_regs *regs) |
| 479 | { |
| 480 | insn_4arg_fn_t *i_fn = (insn_4arg_fn_t *)&p->ainsn.insn[0]; |
| 481 | kprobe_opcode_t insn = p->opcode; |
Viktor Rosendahl | cf3cc1a | 2011-03-28 18:56:05 +0300 | [diff] [blame] | 482 | long ppc = (long)p->addr + 8; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 483 | int rd = (insn >> 12) & 0xf; |
| 484 | int rn = (insn >> 16) & 0xf; |
| 485 | int rm = insn & 0xf; |
Viktor Rosendahl | cf3cc1a | 2011-03-28 18:56:05 +0300 | [diff] [blame] | 486 | long rnv = (rn == 15) ? ppc : regs->uregs[rn]; |
| 487 | /* rm/rmv may be invalid, don't care. */ |
| 488 | long rmv = (rm == 15) ? ppc : regs->uregs[rm]; |
| 489 | long rnv_wb; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 490 | |
Viktor Rosendahl | cf3cc1a | 2011-03-28 18:56:05 +0300 | [diff] [blame] | 491 | rnv_wb = insnslot_4arg_rflags(rnv, rmv, regs->uregs[rd], |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 492 | regs->uregs[rd+1], |
| 493 | regs->ARM_cpsr, i_fn); |
Jon Medhurst | 5c6b76f | 2011-04-08 15:32:56 +0100 | [diff] [blame] | 494 | if (is_writeback(insn)) |
| 495 | regs->uregs[rn] = rnv_wb; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static void __kprobes emulate_ldr(struct kprobe *p, struct pt_regs *regs) |
| 499 | { |
| 500 | insn_llret_3arg_fn_t *i_fn = (insn_llret_3arg_fn_t *)&p->ainsn.insn[0]; |
| 501 | kprobe_opcode_t insn = p->opcode; |
Nicolas Pitre | 0ebe25f | 2010-07-14 05:21:22 +0100 | [diff] [blame] | 502 | long ppc = (long)p->addr + 8; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 503 | union reg_pair fnr; |
| 504 | int rd = (insn >> 12) & 0xf; |
| 505 | int rn = (insn >> 16) & 0xf; |
| 506 | int rm = insn & 0xf; |
| 507 | long rdv; |
Nicolas Pitre | 0ebe25f | 2010-07-14 05:21:22 +0100 | [diff] [blame] | 508 | long rnv = (rn == 15) ? ppc : regs->uregs[rn]; |
| 509 | long rmv = (rm == 15) ? ppc : regs->uregs[rm]; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 510 | long cpsr = regs->ARM_cpsr; |
| 511 | |
| 512 | fnr.dr = insnslot_llret_3arg_rflags(rnv, 0, rmv, cpsr, i_fn); |
Viktor Rosendahl | 0652f06 | 2011-03-26 18:11:01 +0100 | [diff] [blame] | 513 | if (rn != 15) |
| 514 | regs->uregs[rn] = fnr.r0; /* Save Rn in case of writeback. */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 515 | rdv = fnr.r1; |
| 516 | |
| 517 | if (rd == 15) { |
| 518 | #if __LINUX_ARM_ARCH__ >= 5 |
| 519 | cpsr &= ~PSR_T_BIT; |
| 520 | if (rdv & 0x1) |
| 521 | cpsr |= PSR_T_BIT; |
| 522 | regs->ARM_cpsr = cpsr; |
| 523 | rdv &= ~0x1; |
| 524 | #else |
| 525 | rdv &= ~0x2; |
| 526 | #endif |
| 527 | } |
| 528 | regs->uregs[rd] = rdv; |
| 529 | } |
| 530 | |
| 531 | static void __kprobes emulate_str(struct kprobe *p, struct pt_regs *regs) |
| 532 | { |
| 533 | insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0]; |
| 534 | kprobe_opcode_t insn = p->opcode; |
| 535 | long iaddr = (long)p->addr; |
| 536 | int rd = (insn >> 12) & 0xf; |
| 537 | int rn = (insn >> 16) & 0xf; |
| 538 | int rm = insn & 0xf; |
| 539 | long rdv = (rd == 15) ? iaddr + str_pc_offset : regs->uregs[rd]; |
| 540 | long rnv = (rn == 15) ? iaddr + 8 : regs->uregs[rn]; |
| 541 | long rmv = regs->uregs[rm]; /* rm/rmv may be invalid, don't care. */ |
Viktor Rosendahl | 0652f06 | 2011-03-26 18:11:01 +0100 | [diff] [blame] | 542 | long rnv_wb; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 543 | |
Viktor Rosendahl | 0652f06 | 2011-03-26 18:11:01 +0100 | [diff] [blame] | 544 | rnv_wb = insnslot_3arg_rflags(rnv, rdv, rmv, regs->ARM_cpsr, i_fn); |
| 545 | if (rn != 15) |
| 546 | regs->uregs[rn] = rnv_wb; /* Save Rn in case of writeback. */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 549 | static void __kprobes emulate_sat(struct kprobe *p, struct pt_regs *regs) |
| 550 | { |
| 551 | insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0]; |
| 552 | kprobe_opcode_t insn = p->opcode; |
| 553 | int rd = (insn >> 12) & 0xf; |
| 554 | int rm = insn & 0xf; |
| 555 | long rmv = regs->uregs[rm]; |
| 556 | |
| 557 | /* Writes Q flag */ |
| 558 | regs->uregs[rd] = insnslot_1arg_rwflags(rmv, ®s->ARM_cpsr, i_fn); |
| 559 | } |
| 560 | |
| 561 | static void __kprobes emulate_sel(struct kprobe *p, struct pt_regs *regs) |
| 562 | { |
| 563 | insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0]; |
| 564 | kprobe_opcode_t insn = p->opcode; |
| 565 | int rd = (insn >> 12) & 0xf; |
| 566 | int rn = (insn >> 16) & 0xf; |
| 567 | int rm = insn & 0xf; |
| 568 | long rnv = regs->uregs[rn]; |
| 569 | long rmv = regs->uregs[rm]; |
| 570 | |
| 571 | /* Reads GE bits */ |
| 572 | regs->uregs[rd] = insnslot_2arg_rflags(rnv, rmv, regs->ARM_cpsr, i_fn); |
| 573 | } |
| 574 | |
| 575 | static void __kprobes emulate_none(struct kprobe *p, struct pt_regs *regs) |
| 576 | { |
| 577 | insn_0arg_fn_t *i_fn = (insn_0arg_fn_t *)&p->ainsn.insn[0]; |
| 578 | |
| 579 | insnslot_0arg_rflags(regs->ARM_cpsr, i_fn); |
| 580 | } |
| 581 | |
Jon Medhurst | 41713d1 | 2011-04-18 08:53:57 +0100 | [diff] [blame] | 582 | static void __kprobes emulate_nop(struct kprobe *p, struct pt_regs *regs) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 583 | { |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Jon Medhurst | c983677 | 2011-04-19 10:52:17 +0100 | [diff] [blame] | 586 | static void __kprobes |
| 587 | emulate_rd12_modify(struct kprobe *p, struct pt_regs *regs) |
| 588 | { |
| 589 | insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0]; |
| 590 | kprobe_opcode_t insn = p->opcode; |
| 591 | int rd = (insn >> 12) & 0xf; |
| 592 | long rdv = regs->uregs[rd]; |
| 593 | |
| 594 | regs->uregs[rd] = insnslot_1arg_rflags(rdv, regs->ARM_cpsr, i_fn); |
| 595 | } |
| 596 | |
Jon Medhurst | 20e8155 | 2011-04-19 10:52:18 +0100 | [diff] [blame] | 597 | static void __kprobes |
| 598 | emulate_rd12rn0_modify(struct kprobe *p, struct pt_regs *regs) |
| 599 | { |
| 600 | insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0]; |
| 601 | kprobe_opcode_t insn = p->opcode; |
| 602 | int rd = (insn >> 12) & 0xf; |
| 603 | int rn = insn & 0xf; |
| 604 | long rdv = regs->uregs[rd]; |
| 605 | long rnv = regs->uregs[rn]; |
| 606 | |
| 607 | regs->uregs[rd] = insnslot_2arg_rflags(rdv, rnv, regs->ARM_cpsr, i_fn); |
| 608 | } |
| 609 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 610 | static void __kprobes emulate_rd12rm0(struct kprobe *p, struct pt_regs *regs) |
| 611 | { |
| 612 | insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0]; |
| 613 | kprobe_opcode_t insn = p->opcode; |
| 614 | int rd = (insn >> 12) & 0xf; |
| 615 | int rm = insn & 0xf; |
| 616 | long rmv = regs->uregs[rm]; |
| 617 | |
| 618 | regs->uregs[rd] = insnslot_1arg_rflags(rmv, regs->ARM_cpsr, i_fn); |
| 619 | } |
| 620 | |
| 621 | static void __kprobes |
| 622 | emulate_rd12rn16rm0_rwflags(struct kprobe *p, struct pt_regs *regs) |
| 623 | { |
| 624 | insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0]; |
| 625 | kprobe_opcode_t insn = p->opcode; |
| 626 | int rd = (insn >> 12) & 0xf; |
| 627 | int rn = (insn >> 16) & 0xf; |
| 628 | int rm = insn & 0xf; |
| 629 | long rnv = regs->uregs[rn]; |
| 630 | long rmv = regs->uregs[rm]; |
| 631 | |
| 632 | regs->uregs[rd] = |
| 633 | insnslot_2arg_rwflags(rnv, rmv, ®s->ARM_cpsr, i_fn); |
| 634 | } |
| 635 | |
| 636 | static void __kprobes |
| 637 | emulate_rd16rn12rs8rm0_rwflags(struct kprobe *p, struct pt_regs *regs) |
| 638 | { |
| 639 | insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0]; |
| 640 | kprobe_opcode_t insn = p->opcode; |
| 641 | int rd = (insn >> 16) & 0xf; |
| 642 | int rn = (insn >> 12) & 0xf; |
| 643 | int rs = (insn >> 8) & 0xf; |
| 644 | int rm = insn & 0xf; |
| 645 | long rnv = regs->uregs[rn]; |
| 646 | long rsv = regs->uregs[rs]; |
| 647 | long rmv = regs->uregs[rm]; |
| 648 | |
| 649 | regs->uregs[rd] = |
| 650 | insnslot_3arg_rwflags(rnv, rsv, rmv, ®s->ARM_cpsr, i_fn); |
| 651 | } |
| 652 | |
| 653 | static void __kprobes |
| 654 | emulate_rd16rs8rm0_rwflags(struct kprobe *p, struct pt_regs *regs) |
| 655 | { |
| 656 | insn_2arg_fn_t *i_fn = (insn_2arg_fn_t *)&p->ainsn.insn[0]; |
| 657 | kprobe_opcode_t insn = p->opcode; |
| 658 | int rd = (insn >> 16) & 0xf; |
| 659 | int rs = (insn >> 8) & 0xf; |
| 660 | int rm = insn & 0xf; |
| 661 | long rsv = regs->uregs[rs]; |
| 662 | long rmv = regs->uregs[rm]; |
| 663 | |
| 664 | regs->uregs[rd] = |
| 665 | insnslot_2arg_rwflags(rsv, rmv, ®s->ARM_cpsr, i_fn); |
| 666 | } |
| 667 | |
| 668 | static void __kprobes |
| 669 | emulate_rdhi16rdlo12rs8rm0_rwflags(struct kprobe *p, struct pt_regs *regs) |
| 670 | { |
| 671 | insn_llret_4arg_fn_t *i_fn = (insn_llret_4arg_fn_t *)&p->ainsn.insn[0]; |
| 672 | kprobe_opcode_t insn = p->opcode; |
| 673 | union reg_pair fnr; |
| 674 | int rdhi = (insn >> 16) & 0xf; |
| 675 | int rdlo = (insn >> 12) & 0xf; |
| 676 | int rs = (insn >> 8) & 0xf; |
| 677 | int rm = insn & 0xf; |
| 678 | long rsv = regs->uregs[rs]; |
| 679 | long rmv = regs->uregs[rm]; |
| 680 | |
| 681 | fnr.dr = insnslot_llret_4arg_rwflags(regs->uregs[rdhi], |
| 682 | regs->uregs[rdlo], rsv, rmv, |
| 683 | ®s->ARM_cpsr, i_fn); |
| 684 | regs->uregs[rdhi] = fnr.r0; |
| 685 | regs->uregs[rdlo] = fnr.r1; |
| 686 | } |
| 687 | |
| 688 | static void __kprobes |
| 689 | emulate_alu_imm_rflags(struct kprobe *p, struct pt_regs *regs) |
| 690 | { |
| 691 | insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0]; |
| 692 | kprobe_opcode_t insn = p->opcode; |
| 693 | int rd = (insn >> 12) & 0xf; |
| 694 | int rn = (insn >> 16) & 0xf; |
| 695 | long rnv = (rn == 15) ? (long)p->addr + 8 : regs->uregs[rn]; |
| 696 | |
| 697 | regs->uregs[rd] = insnslot_1arg_rflags(rnv, regs->ARM_cpsr, i_fn); |
| 698 | } |
| 699 | |
| 700 | static void __kprobes |
| 701 | emulate_alu_imm_rwflags(struct kprobe *p, struct pt_regs *regs) |
| 702 | { |
| 703 | insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0]; |
| 704 | kprobe_opcode_t insn = p->opcode; |
| 705 | int rd = (insn >> 12) & 0xf; |
| 706 | int rn = (insn >> 16) & 0xf; |
| 707 | long rnv = (rn == 15) ? (long)p->addr + 8 : regs->uregs[rn]; |
| 708 | |
| 709 | regs->uregs[rd] = insnslot_1arg_rwflags(rnv, ®s->ARM_cpsr, i_fn); |
| 710 | } |
| 711 | |
| 712 | static void __kprobes |
Jon Medhurst | ad111ce | 2011-04-06 11:17:11 +0100 | [diff] [blame] | 713 | emulate_alu_tests_imm(struct kprobe *p, struct pt_regs *regs) |
| 714 | { |
| 715 | insn_1arg_fn_t *i_fn = (insn_1arg_fn_t *)&p->ainsn.insn[0]; |
| 716 | kprobe_opcode_t insn = p->opcode; |
| 717 | int rn = (insn >> 16) & 0xf; |
| 718 | long rnv = (rn == 15) ? (long)p->addr + 8 : regs->uregs[rn]; |
| 719 | |
| 720 | insnslot_1arg_rwflags(rnv, ®s->ARM_cpsr, i_fn); |
| 721 | } |
| 722 | |
| 723 | static void __kprobes |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 724 | emulate_alu_rflags(struct kprobe *p, struct pt_regs *regs) |
| 725 | { |
| 726 | insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0]; |
| 727 | kprobe_opcode_t insn = p->opcode; |
| 728 | long ppc = (long)p->addr + 8; |
| 729 | int rd = (insn >> 12) & 0xf; |
| 730 | int rn = (insn >> 16) & 0xf; /* rn/rnv/rs/rsv may be */ |
| 731 | int rs = (insn >> 8) & 0xf; /* invalid, don't care. */ |
| 732 | int rm = insn & 0xf; |
| 733 | long rnv = (rn == 15) ? ppc : regs->uregs[rn]; |
| 734 | long rmv = (rm == 15) ? ppc : regs->uregs[rm]; |
| 735 | long rsv = regs->uregs[rs]; |
| 736 | |
| 737 | regs->uregs[rd] = |
| 738 | insnslot_3arg_rflags(rnv, rmv, rsv, regs->ARM_cpsr, i_fn); |
| 739 | } |
| 740 | |
| 741 | static void __kprobes |
| 742 | emulate_alu_rwflags(struct kprobe *p, struct pt_regs *regs) |
| 743 | { |
| 744 | insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0]; |
| 745 | kprobe_opcode_t insn = p->opcode; |
| 746 | long ppc = (long)p->addr + 8; |
| 747 | int rd = (insn >> 12) & 0xf; |
| 748 | int rn = (insn >> 16) & 0xf; /* rn/rnv/rs/rsv may be */ |
| 749 | int rs = (insn >> 8) & 0xf; /* invalid, don't care. */ |
| 750 | int rm = insn & 0xf; |
| 751 | long rnv = (rn == 15) ? ppc : regs->uregs[rn]; |
| 752 | long rmv = (rm == 15) ? ppc : regs->uregs[rm]; |
| 753 | long rsv = regs->uregs[rs]; |
| 754 | |
| 755 | regs->uregs[rd] = |
| 756 | insnslot_3arg_rwflags(rnv, rmv, rsv, ®s->ARM_cpsr, i_fn); |
| 757 | } |
| 758 | |
Jon Medhurst | ad111ce | 2011-04-06 11:17:11 +0100 | [diff] [blame] | 759 | static void __kprobes |
| 760 | emulate_alu_tests(struct kprobe *p, struct pt_regs *regs) |
| 761 | { |
| 762 | insn_3arg_fn_t *i_fn = (insn_3arg_fn_t *)&p->ainsn.insn[0]; |
| 763 | kprobe_opcode_t insn = p->opcode; |
| 764 | long ppc = (long)p->addr + 8; |
| 765 | int rn = (insn >> 16) & 0xf; |
| 766 | int rs = (insn >> 8) & 0xf; /* rs/rsv may be invalid, don't care. */ |
| 767 | int rm = insn & 0xf; |
| 768 | long rnv = (rn == 15) ? ppc : regs->uregs[rn]; |
| 769 | long rmv = (rm == 15) ? ppc : regs->uregs[rm]; |
| 770 | long rsv = regs->uregs[rs]; |
| 771 | |
| 772 | insnslot_3arg_rwflags(rnv, rmv, rsv, ®s->ARM_cpsr, i_fn); |
| 773 | } |
| 774 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 775 | static enum kprobe_insn __kprobes |
| 776 | prep_emulate_ldr_str(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 777 | { |
Jon Medhurst | 6823fc8 | 2011-04-08 15:32:54 +0100 | [diff] [blame] | 778 | int not_imm = (insn & (1 << 26)) ? (insn & (1 << 25)) |
| 779 | : (~insn & (1 << 22)); |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 780 | |
Jon Medhurst | 54823ac | 2011-04-08 15:32:55 +0100 | [diff] [blame] | 781 | if (is_writeback(insn) && is_r15(insn, 16)) |
| 782 | return INSN_REJECTED; /* Writeback to PC */ |
| 783 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 784 | insn &= 0xfff00fff; |
| 785 | insn |= 0x00001000; /* Rn = r0, Rd = r1 */ |
Jon Medhurst | 6823fc8 | 2011-04-08 15:32:54 +0100 | [diff] [blame] | 786 | if (not_imm) { |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 787 | insn &= ~0xf; |
| 788 | insn |= 2; /* Rm = r2 */ |
| 789 | } |
| 790 | asi->insn[0] = insn; |
| 791 | asi->insn_handler = (insn & (1 << 20)) ? emulate_ldr : emulate_str; |
| 792 | return INSN_GOOD; |
| 793 | } |
| 794 | |
| 795 | static enum kprobe_insn __kprobes |
Jon Medhurst | c983677 | 2011-04-19 10:52:17 +0100 | [diff] [blame] | 796 | prep_emulate_rd12_modify(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 797 | { |
| 798 | if (is_r15(insn, 12)) |
| 799 | return INSN_REJECTED; /* Rd is PC */ |
| 800 | |
| 801 | insn &= 0xffff0fff; /* Rd = r0 */ |
| 802 | asi->insn[0] = insn; |
| 803 | asi->insn_handler = emulate_rd12_modify; |
| 804 | return INSN_GOOD; |
| 805 | } |
| 806 | |
| 807 | static enum kprobe_insn __kprobes |
Jon Medhurst | 20e8155 | 2011-04-19 10:52:18 +0100 | [diff] [blame] | 808 | prep_emulate_rd12rn0_modify(kprobe_opcode_t insn, |
| 809 | struct arch_specific_insn *asi) |
| 810 | { |
| 811 | if (is_r15(insn, 12)) |
| 812 | return INSN_REJECTED; /* Rd is PC */ |
| 813 | |
| 814 | insn &= 0xffff0ff0; /* Rd = r0 */ |
| 815 | insn |= 0x00000001; /* Rn = r1 */ |
| 816 | asi->insn[0] = insn; |
| 817 | asi->insn_handler = emulate_rd12rn0_modify; |
| 818 | return INSN_GOOD; |
| 819 | } |
| 820 | |
| 821 | static enum kprobe_insn __kprobes |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 822 | prep_emulate_rd12rm0(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 823 | { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 824 | if (is_r15(insn, 12)) |
| 825 | return INSN_REJECTED; /* Rd is PC */ |
| 826 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 827 | insn &= 0xffff0ff0; /* Rd = r0, Rm = r0 */ |
| 828 | asi->insn[0] = insn; |
| 829 | asi->insn_handler = emulate_rd12rm0; |
| 830 | return INSN_GOOD; |
| 831 | } |
| 832 | |
| 833 | static enum kprobe_insn __kprobes |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 834 | prep_emulate_rd12rn16rm0_wflags(kprobe_opcode_t insn, |
| 835 | struct arch_specific_insn *asi) |
| 836 | { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 837 | if (is_r15(insn, 12)) |
| 838 | return INSN_REJECTED; /* Rd is PC */ |
| 839 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 840 | insn &= 0xfff00ff0; /* Rd = r0, Rn = r0 */ |
| 841 | insn |= 0x00000001; /* Rm = r1 */ |
| 842 | asi->insn[0] = insn; |
| 843 | asi->insn_handler = emulate_rd12rn16rm0_rwflags; |
| 844 | return INSN_GOOD; |
| 845 | } |
| 846 | |
| 847 | static enum kprobe_insn __kprobes |
| 848 | prep_emulate_rd16rs8rm0_wflags(kprobe_opcode_t insn, |
| 849 | struct arch_specific_insn *asi) |
| 850 | { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 851 | if (is_r15(insn, 16)) |
| 852 | return INSN_REJECTED; /* Rd is PC */ |
| 853 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 854 | insn &= 0xfff0f0f0; /* Rd = r0, Rs = r0 */ |
| 855 | insn |= 0x00000001; /* Rm = r1 */ |
| 856 | asi->insn[0] = insn; |
| 857 | asi->insn_handler = emulate_rd16rs8rm0_rwflags; |
| 858 | return INSN_GOOD; |
| 859 | } |
| 860 | |
| 861 | static enum kprobe_insn __kprobes |
| 862 | prep_emulate_rd16rn12rs8rm0_wflags(kprobe_opcode_t insn, |
| 863 | struct arch_specific_insn *asi) |
| 864 | { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 865 | if (is_r15(insn, 16)) |
| 866 | return INSN_REJECTED; /* Rd is PC */ |
| 867 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 868 | insn &= 0xfff000f0; /* Rd = r0, Rn = r0 */ |
| 869 | insn |= 0x00000102; /* Rs = r1, Rm = r2 */ |
| 870 | asi->insn[0] = insn; |
| 871 | asi->insn_handler = emulate_rd16rn12rs8rm0_rwflags; |
| 872 | return INSN_GOOD; |
| 873 | } |
| 874 | |
| 875 | static enum kprobe_insn __kprobes |
| 876 | prep_emulate_rdhi16rdlo12rs8rm0_wflags(kprobe_opcode_t insn, |
| 877 | struct arch_specific_insn *asi) |
| 878 | { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 879 | if (is_r15(insn, 16) || is_r15(insn, 12)) |
| 880 | return INSN_REJECTED; /* RdHi or RdLo is PC */ |
| 881 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 882 | insn &= 0xfff000f0; /* RdHi = r0, RdLo = r1 */ |
| 883 | insn |= 0x00001203; /* Rs = r2, Rm = r3 */ |
| 884 | asi->insn[0] = insn; |
| 885 | asi->insn_handler = emulate_rdhi16rdlo12rs8rm0_rwflags; |
| 886 | return INSN_GOOD; |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * For the instruction masking and comparisons in all the "space_*" |
| 891 | * functions below, Do _not_ rearrange the order of tests unless |
| 892 | * you're very, very sure of what you are doing. For the sake of |
| 893 | * efficiency, the masks for some tests sometimes assume other test |
| 894 | * have been done prior to them so the number of patterns to test |
| 895 | * for an instruction set can be as broad as possible to reduce the |
| 896 | * number of tests needed. |
| 897 | */ |
| 898 | |
| 899 | static enum kprobe_insn __kprobes |
| 900 | space_1111(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 901 | { |
Jon Medhurst | 41713d1 | 2011-04-18 08:53:57 +0100 | [diff] [blame] | 902 | /* memory hint : 1111 0100 x001 xxxx xxxx xxxx xxxx xxxx : */ |
| 903 | /* PLDI : 1111 0100 x101 xxxx xxxx xxxx xxxx xxxx : */ |
| 904 | /* PLDW : 1111 0101 x001 xxxx xxxx xxxx xxxx xxxx : */ |
| 905 | /* PLD : 1111 0101 x101 xxxx xxxx xxxx xxxx xxxx : */ |
| 906 | if ((insn & 0xfe300000) == 0xf4100000) { |
| 907 | asi->insn_handler = emulate_nop; |
| 908 | return INSN_GOOD_NO_SLOT; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /* BLX(1) : 1111 101x xxxx xxxx xxxx xxxx xxxx xxxx : */ |
| 912 | if ((insn & 0xfe000000) == 0xfa000000) { |
| 913 | asi->insn_handler = simulate_blx1; |
| 914 | return INSN_GOOD_NO_SLOT; |
| 915 | } |
| 916 | |
Jon Medhurst | 72c2bab | 2011-04-18 08:53:58 +0100 | [diff] [blame] | 917 | /* CPS : 1111 0001 0000 xxx0 xxxx xxxx xx0x xxxx */ |
| 918 | /* SETEND: 1111 0001 0000 0001 xxxx xxxx 0000 xxxx */ |
| 919 | |
| 920 | /* SRS : 1111 100x x1x0 xxxx xxxx xxxx xxxx xxxx */ |
| 921 | /* RFE : 1111 100x x0x1 xxxx xxxx xxxx xxxx xxxx */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 922 | |
Jon Medhurst | fa1a03b | 2011-04-18 08:53:54 +0100 | [diff] [blame] | 923 | /* Coprocessor instructions... */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 924 | /* MCRR2 : 1111 1100 0100 xxxx xxxx xxxx xxxx xxxx : (Rd != Rn) */ |
| 925 | /* MRRC2 : 1111 1100 0101 xxxx xxxx xxxx xxxx xxxx : (Rd != Rn) */ |
Jon Medhurst | fa1a03b | 2011-04-18 08:53:54 +0100 | [diff] [blame] | 926 | /* LDC2 : 1111 110x xxx1 xxxx xxxx xxxx xxxx xxxx */ |
| 927 | /* STC2 : 1111 110x xxx0 xxxx xxxx xxxx xxxx xxxx */ |
| 928 | /* CDP2 : 1111 1110 xxxx xxxx xxxx xxxx xxx0 xxxx */ |
| 929 | /* MCR2 : 1111 1110 xxx0 xxxx xxxx xxxx xxx1 xxxx */ |
| 930 | /* MRC2 : 1111 1110 xxx1 xxxx xxxx xxxx xxx1 xxxx */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 931 | |
Jon Medhurst | fa1a03b | 2011-04-18 08:53:54 +0100 | [diff] [blame] | 932 | return INSN_REJECTED; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | static enum kprobe_insn __kprobes |
| 936 | space_cccc_000x(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 937 | { |
| 938 | /* cccc 0001 0xx0 xxxx xxxx xxxx xxxx xxx0 xxxx */ |
| 939 | if ((insn & 0x0f900010) == 0x01000000) { |
| 940 | |
Jon Medhurst | 51468ea | 2011-04-07 13:25:15 +0100 | [diff] [blame] | 941 | /* MRS cpsr : cccc 0001 0000 xxxx xxxx xxxx 0000 xxxx */ |
Jon Medhurst | c412aba | 2011-04-07 13:25:16 +0100 | [diff] [blame] | 942 | if ((insn & 0x0ff000f0) == 0x01000000) { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 943 | if (is_r15(insn, 12)) |
| 944 | return INSN_REJECTED; /* Rd is PC */ |
Jon Medhurst | c412aba | 2011-04-07 13:25:16 +0100 | [diff] [blame] | 945 | asi->insn_handler = simulate_mrs; |
| 946 | return INSN_GOOD_NO_SLOT; |
| 947 | } |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 948 | |
| 949 | /* SMLALxy : cccc 0001 0100 xxxx xxxx xxxx 1xx0 xxxx */ |
| 950 | if ((insn & 0x0ff00090) == 0x01400080) |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 951 | return prep_emulate_rdhi16rdlo12rs8rm0_wflags(insn, |
| 952 | asi); |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 953 | |
| 954 | /* SMULWy : cccc 0001 0010 xxxx xxxx xxxx 1x10 xxxx */ |
| 955 | /* SMULxy : cccc 0001 0110 xxxx xxxx xxxx 1xx0 xxxx */ |
| 956 | if ((insn & 0x0ff000b0) == 0x012000a0 || |
| 957 | (insn & 0x0ff00090) == 0x01600080) |
| 958 | return prep_emulate_rd16rs8rm0_wflags(insn, asi); |
| 959 | |
| 960 | /* SMLAxy : cccc 0001 0000 xxxx xxxx xxxx 1xx0 xxxx : Q */ |
Jon Medhurst | 75539ae | 2011-04-07 13:25:18 +0100 | [diff] [blame] | 961 | /* SMLAWy : cccc 0001 0010 xxxx xxxx xxxx 1x00 xxxx : Q */ |
Jon Medhurst | f704a6e | 2011-04-19 10:52:16 +0100 | [diff] [blame] | 962 | if ((insn & 0x0ff00090) == 0x01000080 || |
| 963 | (insn & 0x0ff000b0) == 0x01200080) |
| 964 | return prep_emulate_rd16rn12rs8rm0_wflags(insn, asi); |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 965 | |
Jon Medhurst | f704a6e | 2011-04-19 10:52:16 +0100 | [diff] [blame] | 966 | /* BXJ : cccc 0001 0010 xxxx xxxx xxxx 0010 xxxx */ |
| 967 | /* MSR : cccc 0001 0x10 xxxx xxxx xxxx 0000 xxxx */ |
| 968 | /* MRS spsr : cccc 0001 0100 xxxx xxxx xxxx 0000 xxxx */ |
| 969 | |
| 970 | /* Other instruction encodings aren't yet defined */ |
| 971 | return INSN_REJECTED; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /* cccc 0001 0xx0 xxxx xxxx xxxx xxxx 0xx1 xxxx */ |
| 975 | else if ((insn & 0x0f900090) == 0x01000010) { |
| 976 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 977 | /* BLX(2) : cccc 0001 0010 xxxx xxxx xxxx 0011 xxxx */ |
| 978 | /* BX : cccc 0001 0010 xxxx xxxx xxxx 0001 xxxx */ |
| 979 | if ((insn & 0x0ff000d0) == 0x01200010) { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 980 | if ((insn & 0x0ff000ff) == 0x0120003f) |
| 981 | return INSN_REJECTED; /* BLX pc */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 982 | asi->insn_handler = simulate_blx2bx; |
Jon Medhurst | a539f5d | 2011-04-06 11:17:10 +0100 | [diff] [blame] | 983 | return INSN_GOOD_NO_SLOT; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | /* CLZ : cccc 0001 0110 xxxx xxxx xxxx 0001 xxxx */ |
| 987 | if ((insn & 0x0ff000f0) == 0x01600010) |
| 988 | return prep_emulate_rd12rm0(insn, asi); |
| 989 | |
| 990 | /* QADD : cccc 0001 0000 xxxx xxxx xxxx 0101 xxxx :Q */ |
| 991 | /* QSUB : cccc 0001 0010 xxxx xxxx xxxx 0101 xxxx :Q */ |
| 992 | /* QDADD : cccc 0001 0100 xxxx xxxx xxxx 0101 xxxx :Q */ |
| 993 | /* QDSUB : cccc 0001 0110 xxxx xxxx xxxx 0101 xxxx :Q */ |
Jon Medhurst | f704a6e | 2011-04-19 10:52:16 +0100 | [diff] [blame] | 994 | if ((insn & 0x0f9000f0) == 0x01000050) |
| 995 | return prep_emulate_rd12rn16rm0_wflags(insn, asi); |
| 996 | |
| 997 | /* BKPT : 1110 0001 0010 xxxx xxxx xxxx 0111 xxxx */ |
| 998 | /* SMC : cccc 0001 0110 xxxx xxxx xxxx 0111 xxxx */ |
| 999 | |
| 1000 | /* Other instruction encodings aren't yet defined */ |
| 1001 | return INSN_REJECTED; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* cccc 0000 xxxx xxxx xxxx xxxx xxxx 1001 xxxx */ |
Jon Medhurst | ba48d40 | 2011-04-07 13:25:19 +0100 | [diff] [blame] | 1005 | else if ((insn & 0x0f0000f0) == 0x00000090) { |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1006 | |
| 1007 | /* MUL : cccc 0000 0000 xxxx xxxx xxxx 1001 xxxx : */ |
| 1008 | /* MULS : cccc 0000 0001 xxxx xxxx xxxx 1001 xxxx :cc */ |
| 1009 | /* MLA : cccc 0000 0010 xxxx xxxx xxxx 1001 xxxx : */ |
| 1010 | /* MLAS : cccc 0000 0011 xxxx xxxx xxxx 1001 xxxx :cc */ |
| 1011 | /* UMAAL : cccc 0000 0100 xxxx xxxx xxxx 1001 xxxx : */ |
Jon Medhurst | ba48d40 | 2011-04-07 13:25:19 +0100 | [diff] [blame] | 1012 | /* undef : cccc 0000 0101 xxxx xxxx xxxx 1001 xxxx : */ |
| 1013 | /* MLS : cccc 0000 0110 xxxx xxxx xxxx 1001 xxxx : */ |
| 1014 | /* undef : cccc 0000 0111 xxxx xxxx xxxx 1001 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1015 | /* UMULL : cccc 0000 1000 xxxx xxxx xxxx 1001 xxxx : */ |
| 1016 | /* UMULLS : cccc 0000 1001 xxxx xxxx xxxx 1001 xxxx :cc */ |
| 1017 | /* UMLAL : cccc 0000 1010 xxxx xxxx xxxx 1001 xxxx : */ |
| 1018 | /* UMLALS : cccc 0000 1011 xxxx xxxx xxxx 1001 xxxx :cc */ |
| 1019 | /* SMULL : cccc 0000 1100 xxxx xxxx xxxx 1001 xxxx : */ |
| 1020 | /* SMULLS : cccc 0000 1101 xxxx xxxx xxxx 1001 xxxx :cc */ |
| 1021 | /* SMLAL : cccc 0000 1110 xxxx xxxx xxxx 1001 xxxx : */ |
| 1022 | /* SMLALS : cccc 0000 1111 xxxx xxxx xxxx 1001 xxxx :cc */ |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1023 | if ((insn & 0x00d00000) == 0x00500000) |
Jon Medhurst | ba48d40 | 2011-04-07 13:25:19 +0100 | [diff] [blame] | 1024 | return INSN_REJECTED; |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1025 | else if ((insn & 0x00e00000) == 0x00000000) |
| 1026 | return prep_emulate_rd16rs8rm0_wflags(insn, asi); |
| 1027 | else if ((insn & 0x00a00000) == 0x00200000) |
| 1028 | return prep_emulate_rd16rn12rs8rm0_wflags(insn, asi); |
| 1029 | else |
| 1030 | return prep_emulate_rdhi16rdlo12rs8rm0_wflags(insn, |
| 1031 | asi); |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /* cccc 000x xxxx xxxx xxxx xxxx xxxx 1xx1 xxxx */ |
| 1035 | else if ((insn & 0x0e000090) == 0x00000090) { |
| 1036 | |
| 1037 | /* SWP : cccc 0001 0000 xxxx xxxx xxxx 1001 xxxx */ |
| 1038 | /* SWPB : cccc 0001 0100 xxxx xxxx xxxx 1001 xxxx */ |
Jon Medhurst | ec58d7f | 2011-04-08 15:32:53 +0100 | [diff] [blame] | 1039 | /* ??? : cccc 0001 0x01 xxxx xxxx xxxx 1001 xxxx */ |
| 1040 | /* ??? : cccc 0001 0x10 xxxx xxxx xxxx 1001 xxxx */ |
| 1041 | /* ??? : cccc 0001 0x11 xxxx xxxx xxxx 1001 xxxx */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1042 | /* STREX : cccc 0001 1000 xxxx xxxx xxxx 1001 xxxx */ |
| 1043 | /* LDREX : cccc 0001 1001 xxxx xxxx xxxx 1001 xxxx */ |
Jon Medhurst | ec58d7f | 2011-04-08 15:32:53 +0100 | [diff] [blame] | 1044 | /* STREXD: cccc 0001 1010 xxxx xxxx xxxx 1001 xxxx */ |
| 1045 | /* LDREXD: cccc 0001 1011 xxxx xxxx xxxx 1001 xxxx */ |
| 1046 | /* STREXB: cccc 0001 1100 xxxx xxxx xxxx 1001 xxxx */ |
| 1047 | /* LDREXB: cccc 0001 1101 xxxx xxxx xxxx 1001 xxxx */ |
| 1048 | /* STREXH: cccc 0001 1110 xxxx xxxx xxxx 1001 xxxx */ |
| 1049 | /* LDREXH: cccc 0001 1111 xxxx xxxx xxxx 1001 xxxx */ |
| 1050 | |
| 1051 | /* LDRD : cccc 000x xxx0 xxxx xxxx xxxx 1101 xxxx */ |
| 1052 | /* STRD : cccc 000x xxx0 xxxx xxxx xxxx 1111 xxxx */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1053 | /* LDRH : cccc 000x xxx1 xxxx xxxx xxxx 1011 xxxx */ |
| 1054 | /* STRH : cccc 000x xxx0 xxxx xxxx xxxx 1011 xxxx */ |
| 1055 | /* LDRSB : cccc 000x xxx1 xxxx xxxx xxxx 1101 xxxx */ |
| 1056 | /* LDRSH : cccc 000x xxx1 xxxx xxxx xxxx 1111 xxxx */ |
Jon Medhurst | ec58d7f | 2011-04-08 15:32:53 +0100 | [diff] [blame] | 1057 | if ((insn & 0x0f0000f0) == 0x01000090) { |
| 1058 | if ((insn & 0x0fb000f0) == 0x01000090) { |
| 1059 | /* SWP/SWPB */ |
| 1060 | return prep_emulate_rd12rn16rm0_wflags(insn, |
| 1061 | asi); |
| 1062 | } else { |
| 1063 | /* STREX/LDREX variants and unallocaed space */ |
| 1064 | return INSN_REJECTED; |
| 1065 | } |
| 1066 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1067 | } else if ((insn & 0x0e1000d0) == 0x00000d0) { |
| 1068 | /* STRD/LDRD */ |
Jon Medhurst | 54823ac | 2011-04-08 15:32:55 +0100 | [diff] [blame] | 1069 | if ((insn & 0x0000e000) == 0x0000e000) |
| 1070 | return INSN_REJECTED; /* Rd is LR or PC */ |
| 1071 | if (is_writeback(insn) && is_r15(insn, 16)) |
| 1072 | return INSN_REJECTED; /* Writeback to PC */ |
| 1073 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1074 | insn &= 0xfff00fff; |
| 1075 | insn |= 0x00002000; /* Rn = r0, Rd = r2 */ |
Jon Medhurst | 5c6b76f | 2011-04-08 15:32:56 +0100 | [diff] [blame] | 1076 | if (!(insn & (1 << 22))) { |
| 1077 | /* Register index */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1078 | insn &= ~0xf; |
| 1079 | insn |= 1; /* Rm = r1 */ |
| 1080 | } |
| 1081 | asi->insn[0] = insn; |
| 1082 | asi->insn_handler = |
| 1083 | (insn & (1 << 5)) ? emulate_strd : emulate_ldrd; |
| 1084 | return INSN_GOOD; |
| 1085 | } |
| 1086 | |
Jon Medhurst | 54823ac | 2011-04-08 15:32:55 +0100 | [diff] [blame] | 1087 | /* LDRH/STRH/LDRSB/LDRSH */ |
| 1088 | if (is_r15(insn, 12)) |
| 1089 | return INSN_REJECTED; /* Rd is PC */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1090 | return prep_emulate_ldr_str(insn, asi); |
| 1091 | } |
| 1092 | |
| 1093 | /* cccc 000x xxxx xxxx xxxx xxxx xxxx xxxx xxxx */ |
| 1094 | |
| 1095 | /* |
| 1096 | * ALU op with S bit and Rd == 15 : |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1097 | * cccc 000x xxx1 xxxx 1111 xxxx xxxx xxxx |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1098 | */ |
| 1099 | if ((insn & 0x0e10f000) == 0x0010f000) |
| 1100 | return INSN_REJECTED; |
| 1101 | |
| 1102 | /* |
| 1103 | * "mov ip, sp" is the most common kprobe'd instruction by far. |
| 1104 | * Check and optimize for it explicitly. |
| 1105 | */ |
| 1106 | if (insn == 0xe1a0c00d) { |
| 1107 | asi->insn_handler = simulate_mov_ipsp; |
| 1108 | return INSN_GOOD_NO_SLOT; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Data processing: Immediate-shift / Register-shift |
| 1113 | * ALU op : cccc 000x xxxx xxxx xxxx xxxx xxxx xxxx |
| 1114 | * CPY : cccc 0001 1010 xxxx xxxx 0000 0000 xxxx |
| 1115 | * MOV : cccc 0001 101x xxxx xxxx xxxx xxxx xxxx |
| 1116 | * *S (bit 20) updates condition codes |
| 1117 | * ADC/SBC/RSC reads the C flag |
| 1118 | */ |
| 1119 | insn &= 0xfff00ff0; /* Rn = r0, Rd = r0 */ |
| 1120 | insn |= 0x00000001; /* Rm = r1 */ |
| 1121 | if (insn & 0x010) { |
| 1122 | insn &= 0xfffff0ff; /* register shift */ |
| 1123 | insn |= 0x00000200; /* Rs = r2 */ |
| 1124 | } |
| 1125 | asi->insn[0] = insn; |
Jon Medhurst | ad111ce | 2011-04-06 11:17:11 +0100 | [diff] [blame] | 1126 | |
| 1127 | if ((insn & 0x0f900000) == 0x01100000) { |
| 1128 | /* |
| 1129 | * TST : cccc 0001 0001 xxxx xxxx xxxx xxxx xxxx |
| 1130 | * TEQ : cccc 0001 0011 xxxx xxxx xxxx xxxx xxxx |
| 1131 | * CMP : cccc 0001 0101 xxxx xxxx xxxx xxxx xxxx |
| 1132 | * CMN : cccc 0001 0111 xxxx xxxx xxxx xxxx xxxx |
| 1133 | */ |
| 1134 | asi->insn_handler = emulate_alu_tests; |
| 1135 | } else { |
| 1136 | /* ALU ops which write to Rd */ |
| 1137 | asi->insn_handler = (insn & (1 << 20)) ? /* S-bit */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1138 | emulate_alu_rwflags : emulate_alu_rflags; |
Jon Medhurst | ad111ce | 2011-04-06 11:17:11 +0100 | [diff] [blame] | 1139 | } |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1140 | return INSN_GOOD; |
| 1141 | } |
| 1142 | |
| 1143 | static enum kprobe_insn __kprobes |
| 1144 | space_cccc_001x(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 1145 | { |
Jon Medhurst | c983677 | 2011-04-19 10:52:17 +0100 | [diff] [blame] | 1146 | /* MOVW : cccc 0011 0000 xxxx xxxx xxxx xxxx xxxx */ |
| 1147 | /* MOVT : cccc 0011 0100 xxxx xxxx xxxx xxxx xxxx */ |
| 1148 | if ((insn & 0x0fb00000) == 0x03000000) |
| 1149 | return prep_emulate_rd12_modify(insn, asi); |
| 1150 | |
Jon Medhurst | 9425493 | 2011-04-19 10:52:19 +0100 | [diff] [blame] | 1151 | /* hints : cccc 0011 0010 0000 xxxx xxxx xxxx xxxx */ |
| 1152 | if ((insn & 0x0fff0000) == 0x03200000) { |
| 1153 | unsigned op2 = insn & 0x000000ff; |
| 1154 | if (op2 == 0x01 || op2 == 0x04) { |
| 1155 | /* YIELD : cccc 0011 0010 0000 xxxx xxxx 0000 0001 */ |
| 1156 | /* SEV : cccc 0011 0010 0000 xxxx xxxx 0000 0100 */ |
| 1157 | asi->insn[0] = insn; |
| 1158 | asi->insn_handler = emulate_none; |
| 1159 | return INSN_GOOD; |
| 1160 | } else if (op2 <= 0x03) { |
| 1161 | /* NOP : cccc 0011 0010 0000 xxxx xxxx 0000 0000 */ |
| 1162 | /* WFE : cccc 0011 0010 0000 xxxx xxxx 0000 0010 */ |
| 1163 | /* WFI : cccc 0011 0010 0000 xxxx xxxx 0000 0011 */ |
| 1164 | /* |
| 1165 | * We make WFE and WFI true NOPs to avoid stalls due |
| 1166 | * to missing events whilst processing the probe. |
| 1167 | */ |
| 1168 | asi->insn_handler = emulate_nop; |
| 1169 | return INSN_GOOD_NO_SLOT; |
| 1170 | } |
| 1171 | /* For DBG and unallocated hints it's safest to reject them */ |
| 1172 | return INSN_REJECTED; |
| 1173 | } |
| 1174 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1175 | /* |
| 1176 | * MSR : cccc 0011 0x10 xxxx xxxx xxxx xxxx xxxx |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1177 | * ALU op with S bit and Rd == 15 : |
| 1178 | * cccc 001x xxx1 xxxx 1111 xxxx xxxx xxxx |
| 1179 | */ |
Will Deacon | ccdf2e1 | 2010-09-27 18:12:12 +0100 | [diff] [blame] | 1180 | if ((insn & 0x0fb00000) == 0x03200000 || /* MSR */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1181 | (insn & 0x0e10f000) == 0x0210f000) /* ALU s-bit, R15 */ |
| 1182 | return INSN_REJECTED; |
| 1183 | |
| 1184 | /* |
| 1185 | * Data processing: 32-bit Immediate |
| 1186 | * ALU op : cccc 001x xxxx xxxx xxxx xxxx xxxx xxxx |
| 1187 | * MOV : cccc 0011 101x xxxx xxxx xxxx xxxx xxxx |
| 1188 | * *S (bit 20) updates condition codes |
| 1189 | * ADC/SBC/RSC reads the C flag |
| 1190 | */ |
Jon Medhurst | 896a74e | 2011-04-06 11:17:12 +0100 | [diff] [blame] | 1191 | insn &= 0xfff00fff; /* Rn = r0 and Rd = r0 */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1192 | asi->insn[0] = insn; |
Jon Medhurst | ad111ce | 2011-04-06 11:17:11 +0100 | [diff] [blame] | 1193 | |
| 1194 | if ((insn & 0x0f900000) == 0x03100000) { |
| 1195 | /* |
| 1196 | * TST : cccc 0011 0001 xxxx xxxx xxxx xxxx xxxx |
| 1197 | * TEQ : cccc 0011 0011 xxxx xxxx xxxx xxxx xxxx |
| 1198 | * CMP : cccc 0011 0101 xxxx xxxx xxxx xxxx xxxx |
| 1199 | * CMN : cccc 0011 0111 xxxx xxxx xxxx xxxx xxxx |
| 1200 | */ |
| 1201 | asi->insn_handler = emulate_alu_tests_imm; |
| 1202 | } else { |
| 1203 | /* ALU ops which write to Rd */ |
| 1204 | asi->insn_handler = (insn & (1 << 20)) ? /* S-bit */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1205 | emulate_alu_imm_rwflags : emulate_alu_imm_rflags; |
Jon Medhurst | ad111ce | 2011-04-06 11:17:11 +0100 | [diff] [blame] | 1206 | } |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1207 | return INSN_GOOD; |
| 1208 | } |
| 1209 | |
| 1210 | static enum kprobe_insn __kprobes |
| 1211 | space_cccc_0110__1(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 1212 | { |
| 1213 | /* SEL : cccc 0110 1000 xxxx xxxx xxxx 1011 xxxx GE: !!! */ |
| 1214 | if ((insn & 0x0ff000f0) == 0x068000b0) { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 1215 | if (is_r15(insn, 12)) |
| 1216 | return INSN_REJECTED; /* Rd is PC */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1217 | insn &= 0xfff00ff0; /* Rd = r0, Rn = r0 */ |
| 1218 | insn |= 0x00000001; /* Rm = r1 */ |
| 1219 | asi->insn[0] = insn; |
| 1220 | asi->insn_handler = emulate_sel; |
| 1221 | return INSN_GOOD; |
| 1222 | } |
| 1223 | |
| 1224 | /* SSAT : cccc 0110 101x xxxx xxxx xxxx xx01 xxxx :Q */ |
| 1225 | /* USAT : cccc 0110 111x xxxx xxxx xxxx xx01 xxxx :Q */ |
| 1226 | /* SSAT16 : cccc 0110 1010 xxxx xxxx xxxx 0011 xxxx :Q */ |
| 1227 | /* USAT16 : cccc 0110 1110 xxxx xxxx xxxx 0011 xxxx :Q */ |
| 1228 | if ((insn & 0x0fa00030) == 0x06a00010 || |
| 1229 | (insn & 0x0fb000f0) == 0x06a00030) { |
Jon Medhurst | 983ebd9 | 2011-04-07 13:25:17 +0100 | [diff] [blame] | 1230 | if (is_r15(insn, 12)) |
| 1231 | return INSN_REJECTED; /* Rd is PC */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1232 | insn &= 0xffff0ff0; /* Rd = r0, Rm = r0 */ |
| 1233 | asi->insn[0] = insn; |
| 1234 | asi->insn_handler = emulate_sat; |
| 1235 | return INSN_GOOD; |
| 1236 | } |
| 1237 | |
| 1238 | /* REV : cccc 0110 1011 xxxx xxxx xxxx 0011 xxxx */ |
| 1239 | /* REV16 : cccc 0110 1011 xxxx xxxx xxxx 1011 xxxx */ |
Jon Medhurst | 0e384ed | 2011-04-12 07:45:22 +0100 | [diff] [blame] | 1240 | /* RBIT : cccc 0110 1111 xxxx xxxx xxxx 0011 xxxx */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1241 | /* REVSH : cccc 0110 1111 xxxx xxxx xxxx 1011 xxxx */ |
| 1242 | if ((insn & 0x0ff00070) == 0x06b00030 || |
Jon Medhurst | 0e384ed | 2011-04-12 07:45:22 +0100 | [diff] [blame] | 1243 | (insn & 0x0ff00070) == 0x06f00030) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1244 | return prep_emulate_rd12rm0(insn, asi); |
| 1245 | |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1246 | /* ??? : cccc 0110 0000 xxxx xxxx xxxx xxx1 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1247 | /* SADD16 : cccc 0110 0001 xxxx xxxx xxxx 0001 xxxx :GE */ |
| 1248 | /* SADDSUBX : cccc 0110 0001 xxxx xxxx xxxx 0011 xxxx :GE */ |
| 1249 | /* SSUBADDX : cccc 0110 0001 xxxx xxxx xxxx 0101 xxxx :GE */ |
| 1250 | /* SSUB16 : cccc 0110 0001 xxxx xxxx xxxx 0111 xxxx :GE */ |
| 1251 | /* SADD8 : cccc 0110 0001 xxxx xxxx xxxx 1001 xxxx :GE */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1252 | /* ??? : cccc 0110 0001 xxxx xxxx xxxx 1011 xxxx : */ |
| 1253 | /* ??? : cccc 0110 0001 xxxx xxxx xxxx 1101 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1254 | /* SSUB8 : cccc 0110 0001 xxxx xxxx xxxx 1111 xxxx :GE */ |
| 1255 | /* QADD16 : cccc 0110 0010 xxxx xxxx xxxx 0001 xxxx : */ |
| 1256 | /* QADDSUBX : cccc 0110 0010 xxxx xxxx xxxx 0011 xxxx : */ |
| 1257 | /* QSUBADDX : cccc 0110 0010 xxxx xxxx xxxx 0101 xxxx : */ |
| 1258 | /* QSUB16 : cccc 0110 0010 xxxx xxxx xxxx 0111 xxxx : */ |
| 1259 | /* QADD8 : cccc 0110 0010 xxxx xxxx xxxx 1001 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1260 | /* ??? : cccc 0110 0010 xxxx xxxx xxxx 1011 xxxx : */ |
| 1261 | /* ??? : cccc 0110 0010 xxxx xxxx xxxx 1101 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1262 | /* QSUB8 : cccc 0110 0010 xxxx xxxx xxxx 1111 xxxx : */ |
| 1263 | /* SHADD16 : cccc 0110 0011 xxxx xxxx xxxx 0001 xxxx : */ |
| 1264 | /* SHADDSUBX : cccc 0110 0011 xxxx xxxx xxxx 0011 xxxx : */ |
| 1265 | /* SHSUBADDX : cccc 0110 0011 xxxx xxxx xxxx 0101 xxxx : */ |
| 1266 | /* SHSUB16 : cccc 0110 0011 xxxx xxxx xxxx 0111 xxxx : */ |
| 1267 | /* SHADD8 : cccc 0110 0011 xxxx xxxx xxxx 1001 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1268 | /* ??? : cccc 0110 0011 xxxx xxxx xxxx 1011 xxxx : */ |
| 1269 | /* ??? : cccc 0110 0011 xxxx xxxx xxxx 1101 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1270 | /* SHSUB8 : cccc 0110 0011 xxxx xxxx xxxx 1111 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1271 | /* ??? : cccc 0110 0100 xxxx xxxx xxxx xxx1 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1272 | /* UADD16 : cccc 0110 0101 xxxx xxxx xxxx 0001 xxxx :GE */ |
| 1273 | /* UADDSUBX : cccc 0110 0101 xxxx xxxx xxxx 0011 xxxx :GE */ |
| 1274 | /* USUBADDX : cccc 0110 0101 xxxx xxxx xxxx 0101 xxxx :GE */ |
| 1275 | /* USUB16 : cccc 0110 0101 xxxx xxxx xxxx 0111 xxxx :GE */ |
| 1276 | /* UADD8 : cccc 0110 0101 xxxx xxxx xxxx 1001 xxxx :GE */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1277 | /* ??? : cccc 0110 0101 xxxx xxxx xxxx 1011 xxxx : */ |
| 1278 | /* ??? : cccc 0110 0101 xxxx xxxx xxxx 1101 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1279 | /* USUB8 : cccc 0110 0101 xxxx xxxx xxxx 1111 xxxx :GE */ |
| 1280 | /* UQADD16 : cccc 0110 0110 xxxx xxxx xxxx 0001 xxxx : */ |
| 1281 | /* UQADDSUBX : cccc 0110 0110 xxxx xxxx xxxx 0011 xxxx : */ |
| 1282 | /* UQSUBADDX : cccc 0110 0110 xxxx xxxx xxxx 0101 xxxx : */ |
| 1283 | /* UQSUB16 : cccc 0110 0110 xxxx xxxx xxxx 0111 xxxx : */ |
| 1284 | /* UQADD8 : cccc 0110 0110 xxxx xxxx xxxx 1001 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1285 | /* ??? : cccc 0110 0110 xxxx xxxx xxxx 1011 xxxx : */ |
| 1286 | /* ??? : cccc 0110 0110 xxxx xxxx xxxx 1101 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1287 | /* UQSUB8 : cccc 0110 0110 xxxx xxxx xxxx 1111 xxxx : */ |
| 1288 | /* UHADD16 : cccc 0110 0111 xxxx xxxx xxxx 0001 xxxx : */ |
| 1289 | /* UHADDSUBX : cccc 0110 0111 xxxx xxxx xxxx 0011 xxxx : */ |
| 1290 | /* UHSUBADDX : cccc 0110 0111 xxxx xxxx xxxx 0101 xxxx : */ |
| 1291 | /* UHSUB16 : cccc 0110 0111 xxxx xxxx xxxx 0111 xxxx : */ |
| 1292 | /* UHADD8 : cccc 0110 0111 xxxx xxxx xxxx 1001 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1293 | /* ??? : cccc 0110 0111 xxxx xxxx xxxx 1011 xxxx : */ |
| 1294 | /* ??? : cccc 0110 0111 xxxx xxxx xxxx 1101 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1295 | /* UHSUB8 : cccc 0110 0111 xxxx xxxx xxxx 1111 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1296 | if ((insn & 0x0f800010) == 0x06000010) { |
| 1297 | if ((insn & 0x00300000) == 0x00000000 || |
| 1298 | (insn & 0x000000e0) == 0x000000a0 || |
| 1299 | (insn & 0x000000e0) == 0x000000c0) |
| 1300 | return INSN_REJECTED; /* Unallocated space */ |
| 1301 | return prep_emulate_rd12rn16rm0_wflags(insn, asi); |
| 1302 | } |
| 1303 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1304 | /* PKHBT : cccc 0110 1000 xxxx xxxx xxxx x001 xxxx : */ |
| 1305 | /* PKHTB : cccc 0110 1000 xxxx xxxx xxxx x101 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1306 | if ((insn & 0x0ff00030) == 0x06800010) |
| 1307 | return prep_emulate_rd12rn16rm0_wflags(insn, asi); |
| 1308 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1309 | /* SXTAB16 : cccc 0110 1000 xxxx xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1310 | /* SXTB16 : cccc 0110 1000 1111 xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1311 | /* ??? : cccc 0110 1001 xxxx xxxx xxxx 0111 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1312 | /* SXTAB : cccc 0110 1010 xxxx xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1313 | /* SXTB : cccc 0110 1010 1111 xxxx xxxx 0111 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1314 | /* SXTAH : cccc 0110 1011 xxxx xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1315 | /* SXTH : cccc 0110 1011 1111 xxxx xxxx 0111 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1316 | /* UXTAB16 : cccc 0110 1100 xxxx xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1317 | /* UXTB16 : cccc 0110 1100 1111 xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1318 | /* ??? : cccc 0110 1101 xxxx xxxx xxxx 0111 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1319 | /* UXTAB : cccc 0110 1110 xxxx xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1320 | /* UXTB : cccc 0110 1110 1111 xxxx xxxx 0111 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1321 | /* UXTAH : cccc 0110 1111 xxxx xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1322 | /* UXTH : cccc 0110 1111 1111 xxxx xxxx 0111 xxxx : */ |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1323 | if ((insn & 0x0f8000f0) == 0x06800070) { |
| 1324 | if ((insn & 0x00300000) == 0x00100000) |
| 1325 | return INSN_REJECTED; /* Unallocated space */ |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1326 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1327 | if ((insn & 0x000f0000) == 0x000f0000) |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1328 | return prep_emulate_rd12rm0(insn, asi); |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1329 | else |
Jon Medhurst | 8dd7cfb | 2011-04-12 07:45:24 +0100 | [diff] [blame] | 1330 | return prep_emulate_rd12rn16rm0_wflags(insn, asi); |
Jon Medhurst | 780b5c1 | 2011-04-12 07:45:23 +0100 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | /* Other instruction encodings aren't yet defined */ |
| 1334 | return INSN_REJECTED; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static enum kprobe_insn __kprobes |
| 1338 | space_cccc_0111__1(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 1339 | { |
| 1340 | /* Undef : cccc 0111 1111 xxxx xxxx xxxx 1111 xxxx */ |
| 1341 | if ((insn & 0x0ff000f0) == 0x03f000f0) |
| 1342 | return INSN_REJECTED; |
| 1343 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1344 | /* SMLALD : cccc 0111 0100 xxxx xxxx xxxx 00x1 xxxx */ |
| 1345 | /* SMLSLD : cccc 0111 0100 xxxx xxxx xxxx 01x1 xxxx */ |
| 1346 | if ((insn & 0x0ff00090) == 0x07400010) |
| 1347 | return prep_emulate_rdhi16rdlo12rs8rm0_wflags(insn, asi); |
| 1348 | |
| 1349 | /* SMLAD : cccc 0111 0000 xxxx xxxx xxxx 00x1 xxxx :Q */ |
Jon Medhurst | 038c383 | 2011-04-12 07:45:25 +0100 | [diff] [blame] | 1350 | /* SMUAD : cccc 0111 0000 xxxx 1111 xxxx 00x1 xxxx :Q */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1351 | /* SMLSD : cccc 0111 0000 xxxx xxxx xxxx 01x1 xxxx :Q */ |
Jon Medhurst | 038c383 | 2011-04-12 07:45:25 +0100 | [diff] [blame] | 1352 | /* SMUSD : cccc 0111 0000 xxxx 1111 xxxx 01x1 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1353 | /* SMMLA : cccc 0111 0101 xxxx xxxx xxxx 00x1 xxxx : */ |
Jon Medhurst | 038c383 | 2011-04-12 07:45:25 +0100 | [diff] [blame] | 1354 | /* SMMUL : cccc 0111 0101 xxxx 1111 xxxx 00x1 xxxx : */ |
Jon Medhurst | c6e4ae3 | 2011-04-12 07:45:26 +0100 | [diff] [blame] | 1355 | /* USADA8 : cccc 0111 1000 xxxx xxxx xxxx 0001 xxxx : */ |
| 1356 | /* USAD8 : cccc 0111 1000 xxxx 1111 xxxx 0001 xxxx : */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1357 | if ((insn & 0x0ff00090) == 0x07000010 || |
Jon Medhurst | c6e4ae3 | 2011-04-12 07:45:26 +0100 | [diff] [blame] | 1358 | (insn & 0x0ff000d0) == 0x07500010 || |
| 1359 | (insn & 0x0ff000f0) == 0x07800010) { |
Jon Medhurst | 038c383 | 2011-04-12 07:45:25 +0100 | [diff] [blame] | 1360 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1361 | if ((insn & 0x0000f000) == 0x0000f000) |
Jon Medhurst | 038c383 | 2011-04-12 07:45:25 +0100 | [diff] [blame] | 1362 | return prep_emulate_rd16rs8rm0_wflags(insn, asi); |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1363 | else |
Jon Medhurst | 038c383 | 2011-04-12 07:45:25 +0100 | [diff] [blame] | 1364 | return prep_emulate_rd16rn12rs8rm0_wflags(insn, asi); |
Jon Medhurst | 038c383 | 2011-04-12 07:45:25 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | /* SMMLS : cccc 0111 0101 xxxx xxxx xxxx 11x1 xxxx : */ |
| 1368 | if ((insn & 0x0ff000d0) == 0x075000d0) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1369 | return prep_emulate_rd16rn12rs8rm0_wflags(insn, asi); |
| 1370 | |
Jon Medhurst | 20e8155 | 2011-04-19 10:52:18 +0100 | [diff] [blame] | 1371 | /* SBFX : cccc 0111 101x xxxx xxxx xxxx x101 xxxx : */ |
| 1372 | /* UBFX : cccc 0111 111x xxxx xxxx xxxx x101 xxxx : */ |
| 1373 | if ((insn & 0x0fa00070) == 0x07a00050) |
| 1374 | return prep_emulate_rd12rm0(insn, asi); |
| 1375 | |
| 1376 | /* BFI : cccc 0111 110x xxxx xxxx xxxx x001 xxxx : */ |
| 1377 | /* BFC : cccc 0111 110x xxxx xxxx xxxx x001 1111 : */ |
| 1378 | if ((insn & 0x0fe00070) == 0x07c00010) { |
| 1379 | |
| 1380 | if ((insn & 0x0000000f) == 0x0000000f) |
| 1381 | return prep_emulate_rd12_modify(insn, asi); |
| 1382 | else |
| 1383 | return prep_emulate_rd12rn0_modify(insn, asi); |
| 1384 | } |
| 1385 | |
Jon Medhurst | 038c383 | 2011-04-12 07:45:25 +0100 | [diff] [blame] | 1386 | return INSN_REJECTED; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | static enum kprobe_insn __kprobes |
| 1390 | space_cccc_01xx(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 1391 | { |
| 1392 | /* LDR : cccc 01xx x0x1 xxxx xxxx xxxx xxxx xxxx */ |
| 1393 | /* LDRB : cccc 01xx x1x1 xxxx xxxx xxxx xxxx xxxx */ |
| 1394 | /* LDRBT : cccc 01x0 x111 xxxx xxxx xxxx xxxx xxxx */ |
| 1395 | /* LDRT : cccc 01x0 x011 xxxx xxxx xxxx xxxx xxxx */ |
| 1396 | /* STR : cccc 01xx x0x0 xxxx xxxx xxxx xxxx xxxx */ |
| 1397 | /* STRB : cccc 01xx x1x0 xxxx xxxx xxxx xxxx xxxx */ |
| 1398 | /* STRBT : cccc 01x0 x110 xxxx xxxx xxxx xxxx xxxx */ |
| 1399 | /* STRT : cccc 01x0 x010 xxxx xxxx xxxx xxxx xxxx */ |
Jon Medhurst | 81ff572 | 2011-04-12 07:45:21 +0100 | [diff] [blame] | 1400 | |
| 1401 | if ((insn & 0x00500000) == 0x00500000 && is_r15(insn, 12)) |
| 1402 | return INSN_REJECTED; /* LDRB into PC */ |
| 1403 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1404 | return prep_emulate_ldr_str(insn, asi); |
| 1405 | } |
| 1406 | |
| 1407 | static enum kprobe_insn __kprobes |
| 1408 | space_cccc_100x(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 1409 | { |
| 1410 | /* LDM(2) : cccc 100x x101 xxxx 0xxx xxxx xxxx xxxx */ |
| 1411 | /* LDM(3) : cccc 100x x1x1 xxxx 1xxx xxxx xxxx xxxx */ |
| 1412 | if ((insn & 0x0e708000) == 0x85000000 || |
| 1413 | (insn & 0x0e508000) == 0x85010000) |
| 1414 | return INSN_REJECTED; |
| 1415 | |
| 1416 | /* LDM(1) : cccc 100x x0x1 xxxx xxxx xxxx xxxx xxxx */ |
| 1417 | /* STM(1) : cccc 100x x0x0 xxxx xxxx xxxx xxxx xxxx */ |
Jon Medhurst | 235a4ce | 2011-07-07 08:57:22 +0100 | [diff] [blame^] | 1418 | |
| 1419 | /* |
| 1420 | * Make the instruction unconditional because the new emulation |
| 1421 | * functions don't bother to setup the PSR context. |
| 1422 | */ |
| 1423 | insn = (insn | 0xe0000000) & ~0x10000000; |
| 1424 | return kprobe_decode_ldmstm(insn, asi); |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | static enum kprobe_insn __kprobes |
| 1428 | space_cccc_101x(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 1429 | { |
| 1430 | /* B : cccc 1010 xxxx xxxx xxxx xxxx xxxx xxxx */ |
| 1431 | /* BL : cccc 1011 xxxx xxxx xxxx xxxx xxxx xxxx */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1432 | asi->insn_handler = simulate_bbl; |
Jon Medhurst | a539f5d | 2011-04-06 11:17:10 +0100 | [diff] [blame] | 1433 | return INSN_GOOD_NO_SLOT; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | static enum kprobe_insn __kprobes |
Jon Medhurst | ac211c6 | 2011-04-18 08:53:55 +0100 | [diff] [blame] | 1437 | space_cccc_11xx(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1438 | { |
Jon Medhurst | ac211c6 | 2011-04-18 08:53:55 +0100 | [diff] [blame] | 1439 | /* Coprocessor instructions... */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1440 | /* MCRR : cccc 1100 0100 xxxx xxxx xxxx xxxx xxxx : (Rd!=Rn) */ |
| 1441 | /* MRRC : cccc 1100 0101 xxxx xxxx xxxx xxxx xxxx : (Rd!=Rn) */ |
Jon Medhurst | ac211c6 | 2011-04-18 08:53:55 +0100 | [diff] [blame] | 1442 | /* LDC : cccc 110x xxx1 xxxx xxxx xxxx xxxx xxxx */ |
| 1443 | /* STC : cccc 110x xxx0 xxxx xxxx xxxx xxxx xxxx */ |
| 1444 | /* CDP : cccc 1110 xxxx xxxx xxxx xxxx xxx0 xxxx */ |
| 1445 | /* MCR : cccc 1110 xxx0 xxxx xxxx xxxx xxx1 xxxx */ |
| 1446 | /* MRC : cccc 1110 xxx1 xxxx xxxx xxxx xxx1 xxxx */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1447 | |
Jon Medhurst | ac211c6 | 2011-04-18 08:53:55 +0100 | [diff] [blame] | 1448 | /* SVC : cccc 1111 xxxx xxxx xxxx xxxx xxxx xxxx */ |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1449 | |
Jon Medhurst | fa1a03b | 2011-04-18 08:53:54 +0100 | [diff] [blame] | 1450 | return INSN_REJECTED; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Jon Medhurst | c6a7d97 | 2011-06-09 12:11:27 +0100 | [diff] [blame] | 1453 | static void __kprobes arm_singlestep(struct kprobe *p, struct pt_regs *regs) |
| 1454 | { |
| 1455 | regs->ARM_pc += 4; |
| 1456 | p->ainsn.insn_handler(p, regs); |
| 1457 | } |
| 1458 | |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1459 | /* Return: |
| 1460 | * INSN_REJECTED If instruction is one not allowed to kprobe, |
| 1461 | * INSN_GOOD If instruction is supported and uses instruction slot, |
| 1462 | * INSN_GOOD_NO_SLOT If instruction is supported but doesn't use its slot. |
| 1463 | * |
| 1464 | * For instructions we don't want to kprobe (INSN_REJECTED return result): |
| 1465 | * These are generally ones that modify the processor state making |
| 1466 | * them "hard" to simulate such as switches processor modes or |
| 1467 | * make accesses in alternate modes. Any of these could be simulated |
| 1468 | * if the work was put into it, but low return considering they |
| 1469 | * should also be very rare. |
| 1470 | */ |
| 1471 | enum kprobe_insn __kprobes |
| 1472 | arm_kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi) |
| 1473 | { |
Jon Medhurst | c6a7d97 | 2011-06-09 12:11:27 +0100 | [diff] [blame] | 1474 | asi->insn_singlestep = arm_singlestep; |
Jon Medhurst | 0ab4c02 | 2011-07-06 11:25:18 +0100 | [diff] [blame] | 1475 | asi->insn_check_cc = kprobe_condition_checks[insn>>28]; |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1476 | asi->insn[1] = KPROBE_RETURN_INSTRUCTION; |
| 1477 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1478 | if ((insn & 0xf0000000) == 0xf0000000) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1479 | |
| 1480 | return space_1111(insn, asi); |
| 1481 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1482 | else if ((insn & 0x0e000000) == 0x00000000) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1483 | |
| 1484 | return space_cccc_000x(insn, asi); |
| 1485 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1486 | else if ((insn & 0x0e000000) == 0x02000000) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1487 | |
| 1488 | return space_cccc_001x(insn, asi); |
| 1489 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1490 | else if ((insn & 0x0f000010) == 0x06000010) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1491 | |
| 1492 | return space_cccc_0110__1(insn, asi); |
| 1493 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1494 | else if ((insn & 0x0f000010) == 0x07000010) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1495 | |
| 1496 | return space_cccc_0111__1(insn, asi); |
| 1497 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1498 | else if ((insn & 0x0c000000) == 0x04000000) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1499 | |
| 1500 | return space_cccc_01xx(insn, asi); |
| 1501 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1502 | else if ((insn & 0x0e000000) == 0x08000000) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1503 | |
| 1504 | return space_cccc_100x(insn, asi); |
| 1505 | |
Jon Medhurst | cdc2536 | 2011-04-19 10:52:20 +0100 | [diff] [blame] | 1506 | else if ((insn & 0x0e000000) == 0x0a000000) |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1507 | |
| 1508 | return space_cccc_101x(insn, asi); |
| 1509 | |
Jon Medhurst | ac211c6 | 2011-04-18 08:53:55 +0100 | [diff] [blame] | 1510 | return space_cccc_11xx(insn, asi); |
Quentin Barnes | 35aa1df | 2007-06-11 22:20:10 +0000 | [diff] [blame] | 1511 | } |