1 #include <dbi_kprobes.h>
2 #include <asm/dbi_kprobes.h>
4 #include <swap_uprobes.h>
5 #include <asm/swap_uprobes.h>
6 #include <dbi_insn_slots.h>
9 #include <dbi_kdebug.h>
10 extern struct hlist_head uprobe_insn_pages;
11 int arch_check_insn_arm(struct arch_specific_insn *ainsn);
12 int prep_pc_dep_insn_execbuf(kprobe_opcode_t *insns, kprobe_opcode_t insn, int uregs);
13 void pc_dep_insn_execbuf(void);
14 void gen_insn_execbuf(void);
15 void gen_insn_execbuf_thumb(void);
16 void pc_dep_insn_execbuf_thumb(void);
17 int kprobe_trap_handler(struct pt_regs *regs, unsigned int instr);
18 void add_rp_inst(struct kretprobe_instance *ri);
21 #define sign_extend(x, signbit) ((x) | (0 - ((x) & (1 << (signbit)))))
22 #define branch_displacement(insn) sign_extend(((insn) & 0xffffff) << 2, 25)
24 static kprobe_opcode_t get_addr_b(kprobe_opcode_t insn, kprobe_opcode_t *addr)
26 // real position less then PC by 8
27 return (kprobe_opcode_t)((long)addr + 8 + branch_displacement(insn));
30 /* is instruction Thumb2 and NOT a branch, etc... */
31 static int is_thumb2(kprobe_opcode_t insn)
33 return ((insn & 0xf800) == 0xe800 ||
34 (insn & 0xf800) == 0xf000 ||
35 (insn & 0xf800) == 0xf800);
38 static int arch_copy_trampoline_arm_uprobe(struct kprobe *p, struct task_struct *task, int atomic)
40 kprobe_opcode_t insns[UPROBES_TRAMP_LEN];
42 kprobe_opcode_t insn[MAX_INSN_SIZE];
43 struct arch_specific_insn ainsn;
46 if ((unsigned long)p->addr & 0x01) {
47 printk("Error in %s at %d: attempt to register kprobe at an unaligned address\n", __FILE__, __LINE__);
52 ainsn.insn_arm = insn;
53 if (!arch_check_insn_arm(&ainsn)) {
59 if (ARM_INSN_MATCH(DPIS, insn[0]) || ARM_INSN_MATCH(LRO, insn[0]) ||
60 ARM_INSN_MATCH(SRO, insn[0])) {
62 if ((ARM_INSN_REG_RN(insn[0]) == 15) || (ARM_INSN_REG_RM(insn[0]) == 15) ||
63 (ARM_INSN_MATCH(SRO, insn[0]) && (ARM_INSN_REG_RD(insn[0]) == 15))) {
64 DBPRINTF("Unboostable insn %lx, DPIS/LRO/SRO\n", insn[0]);
69 } else if (ARM_INSN_MATCH(DPI, insn[0]) || ARM_INSN_MATCH(LIO, insn[0]) ||
70 ARM_INSN_MATCH (SIO, insn[0])) {
72 if ((ARM_INSN_REG_RN(insn[0]) == 15) || (ARM_INSN_MATCH(SIO, insn[0]) &&
73 (ARM_INSN_REG_RD(insn[0]) == 15))) {
75 DBPRINTF("Unboostable insn %lx/%p, DPI/LIO/SIO\n", insn[0], p);
79 } else if (ARM_INSN_MATCH(DPRS, insn[0])) {
81 if ((ARM_INSN_REG_RN(insn[0]) == 15) || (ARM_INSN_REG_RM(insn[0]) == 15) ||
82 (ARM_INSN_REG_RS(insn[0]) == 15)) {
84 DBPRINTF("Unboostable insn %lx, DPRS\n", insn[0]);
88 } else if (ARM_INSN_MATCH(SM, insn[0])) {
90 if (ARM_INSN_REG_MR (insn[0], 15))
92 DBPRINTF ("Unboostable insn %lx, SM\n", insn[0]);
97 // check instructions that can write result to SP andu uses PC
98 if (pc_dep && (ARM_INSN_REG_RD (ainsn.insn_arm[0]) == 13)) {
99 printk("Error in %s at %d: instruction check failed (arm)\n", __FILE__, __LINE__);
101 // TODO: move free to later phase
102 //free_insn_slot (&uprobe_insn_pages, task, p->ainsn.insn_arm, 0);
106 if (unlikely(uregs && pc_dep)) {
107 memcpy(insns, pc_dep_insn_execbuf, sizeof(insns));
108 if (prep_pc_dep_insn_execbuf(insns, insn[0], uregs) != 0) {
109 printk("Error in %s at %d: failed to prepare exec buffer for insn %lx!",
110 __FILE__, __LINE__, insn[0]);
112 // TODO: move free to later phase
113 //free_insn_slot (&uprobe_insn_pages, task, p->ainsn.insn_arm, 0);
117 insns[6] = (kprobe_opcode_t) (p->addr + 2);
119 memcpy(insns, gen_insn_execbuf, sizeof(insns));
120 insns[UPROBES_TRAMP_INSN_IDX] = insn[0];
123 insns[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
124 insns[7] = (kprobe_opcode_t) (p->addr + 1);
127 if(ARM_INSN_MATCH(B, ainsn.insn_arm[0])) {
128 memcpy(insns, pc_dep_insn_execbuf, sizeof(insns));
129 insns[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
130 insns[6] = (kprobe_opcode_t)(p->addr + 2);
131 insns[7] = get_addr_b(p->opcode, p->addr);
134 DBPRINTF("arch_prepare_uprobe: to %p - %lx %lx %lx %lx %lx %lx %lx %lx %lx",
135 p->ainsn.insn_arm, insns[0], insns[1], insns[2], insns[3], insns[4],
136 insns[5], insns[6], insns[7], insns[8]);
137 if (!write_proc_vm_atomic(task, (unsigned long)p->ainsn.insn_arm, insns, sizeof(insns))) {
138 panic("failed to write memory %p!\n", p->ainsn.insn_arm);
139 // Mr_Nobody: we have to panic, really??...
140 //free_insn_slot (&uprobe_insn_pages, task, p->ainsn.insn_arm, 0);
147 static int arch_check_insn_thumb(struct arch_specific_insn *ainsn)
151 // check instructions that can change PC
152 if (THUMB_INSN_MATCH(UNDEF, ainsn->insn_thumb[0]) ||
153 THUMB_INSN_MATCH(SWI, ainsn->insn_thumb[0]) ||
154 THUMB_INSN_MATCH(BREAK, ainsn->insn_thumb[0]) ||
155 THUMB2_INSN_MATCH(BL, ainsn->insn_thumb[0]) ||
156 THUMB_INSN_MATCH(B1, ainsn->insn_thumb[0]) ||
157 THUMB_INSN_MATCH(B2, ainsn->insn_thumb[0]) ||
158 THUMB_INSN_MATCH(CBZ, ainsn->insn_thumb[0]) ||
159 THUMB2_INSN_MATCH(B1, ainsn->insn_thumb[0]) ||
160 THUMB2_INSN_MATCH(B2, ainsn->insn_thumb[0]) ||
161 THUMB2_INSN_MATCH(BLX1, ainsn->insn_thumb[0]) ||
162 THUMB_INSN_MATCH(BLX2, ainsn->insn_thumb[0]) ||
163 THUMB_INSN_MATCH(BX, ainsn->insn_thumb[0]) ||
164 THUMB2_INSN_MATCH(BXJ, ainsn->insn_thumb[0]) ||
165 (THUMB2_INSN_MATCH(ADR, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
166 (THUMB2_INSN_MATCH(LDRW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RT(ainsn->insn_thumb[0]) == 15) ||
167 (THUMB2_INSN_MATCH(LDRW1, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RT(ainsn->insn_thumb[0]) == 15) ||
168 (THUMB2_INSN_MATCH(LDRHW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RT(ainsn->insn_thumb[0]) == 15) ||
169 (THUMB2_INSN_MATCH(LDRHW1, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RT(ainsn->insn_thumb[0]) == 15) ||
170 (THUMB2_INSN_MATCH(LDRWL, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RT(ainsn->insn_thumb[0]) == 15) ||
171 THUMB2_INSN_MATCH(LDMIA, ainsn->insn_thumb[0]) ||
172 THUMB2_INSN_MATCH(LDMDB, ainsn->insn_thumb[0]) ||
173 (THUMB2_INSN_MATCH(DP, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
174 (THUMB2_INSN_MATCH(RSBW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
175 (THUMB2_INSN_MATCH(RORW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
176 (THUMB2_INSN_MATCH(ROR, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
177 (THUMB2_INSN_MATCH(LSLW1, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
178 (THUMB2_INSN_MATCH(LSLW2, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
179 (THUMB2_INSN_MATCH(LSRW1, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
180 (THUMB2_INSN_MATCH(LSRW2, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RD(ainsn->insn_thumb[0]) == 15) ||
181 /* skip PC, #-imm12 -> SP, #-imm8 and Tegra-hanging instructions */
182 (THUMB2_INSN_MATCH(STRW1, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RN(ainsn->insn_thumb[0]) == 15) ||
183 (THUMB2_INSN_MATCH(STRBW1, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RN(ainsn->insn_thumb[0]) == 15) ||
184 (THUMB2_INSN_MATCH(STRHW1, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RN(ainsn->insn_thumb[0]) == 15) ||
185 (THUMB2_INSN_MATCH(STRW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RN(ainsn->insn_thumb[0]) == 15) ||
186 (THUMB2_INSN_MATCH(STRHW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RN(ainsn->insn_thumb[0]) == 15) ||
187 (THUMB2_INSN_MATCH(LDRW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RN(ainsn->insn_thumb[0]) == 15) ||
188 (THUMB2_INSN_MATCH(LDRBW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RN(ainsn->insn_thumb[0]) == 15) ||
189 (THUMB2_INSN_MATCH(LDRHW, ainsn->insn_thumb[0]) && THUMB2_INSN_REG_RN(ainsn->insn_thumb[0]) == 15) ||
190 /* skip STRDx/LDRDx Rt, Rt2, [Rd, ...] */
191 (THUMB2_INSN_MATCH(LDRD, ainsn->insn_thumb[0]) || THUMB2_INSN_MATCH(LDRD1, ainsn->insn_thumb[0]) || THUMB2_INSN_MATCH(STRD, ainsn->insn_thumb[0])) ) {
192 DBPRINTF("Bad insn arch_check_insn_thumb: %lx\n", ainsn->insn_thumb[0]);
199 static int prep_pc_dep_insn_execbuf_thumb(kprobe_opcode_t * insns, kprobe_opcode_t insn, int uregs)
201 unsigned char mreg = 0;
202 unsigned char reg = 0;
204 if (THUMB_INSN_MATCH(APC, insn) || THUMB_INSN_MATCH(LRO3, insn)) {
205 reg = ((insn & 0xffff) & uregs) >> 8;
207 if (THUMB_INSN_MATCH(MOV3, insn)) {
208 if (((((unsigned char) insn) & 0xff) >> 3) == 15) {
209 reg = (insn & 0xffff) & uregs;
214 if (THUMB2_INSN_MATCH(ADR, insn)) {
215 reg = ((insn >> 16) & uregs) >> 8;
220 if (THUMB2_INSN_MATCH(LDRW, insn) || THUMB2_INSN_MATCH(LDRW1, insn) ||
221 THUMB2_INSN_MATCH(LDRHW, insn) || THUMB2_INSN_MATCH(LDRHW1, insn) ||
222 THUMB2_INSN_MATCH(LDRWL, insn)) {
223 reg = ((insn >> 16) & uregs) >> 12;
228 // LDRB.W PC, [PC, #immed] => PLD [PC, #immed], so Rt == PC is skipped
229 if (THUMB2_INSN_MATCH(LDRBW, insn) || THUMB2_INSN_MATCH(LDRBW1, insn) ||
230 THUMB2_INSN_MATCH(LDREX, insn)) {
231 reg = ((insn >> 16) & uregs) >> 12;
233 if (THUMB2_INSN_MATCH(DP, insn)) {
234 reg = ((insn >> 16) & uregs) >> 12;
239 if (THUMB2_INSN_MATCH(RSBW, insn)) {
240 reg = ((insn >> 12) & uregs) >> 8;
245 if (THUMB2_INSN_MATCH(RORW, insn)) {
246 reg = ((insn >> 12) & uregs) >> 8;
251 if (THUMB2_INSN_MATCH(ROR, insn) || THUMB2_INSN_MATCH(LSLW1, insn) ||
252 THUMB2_INSN_MATCH(LSLW2, insn) || THUMB2_INSN_MATCH(LSRW1, insn) ||
253 THUMB2_INSN_MATCH(LSRW2, insn)) {
254 reg = ((insn >> 12) & uregs) >> 8;
259 if (THUMB2_INSN_MATCH(TEQ1, insn) || THUMB2_INSN_MATCH(TST1, insn)) {
262 if (THUMB2_INSN_MATCH(TEQ2, insn) || THUMB2_INSN_MATCH(TST2, insn)) {
263 reg = THUMB2_INSN_REG_RM(insn);
276 if ((THUMB2_INSN_MATCH(STRW, insn) || THUMB2_INSN_MATCH(STRBW, insn) ||
277 THUMB2_INSN_MATCH(STRD, insn) || THUMB2_INSN_MATCH(STRHT, insn) ||
278 THUMB2_INSN_MATCH(STRT, insn) || THUMB2_INSN_MATCH(STRHW1, insn) ||
279 THUMB2_INSN_MATCH(STRHW, insn)) && THUMB2_INSN_REG_RT(insn) == 15) {
280 reg = THUMB2_INSN_REG_RT(insn);
283 if (reg == 6 || reg == 7) {
284 *((unsigned short*)insns + 0) = (*((unsigned short*)insns + 0) & 0x00ff) | ((1 << mreg) | (1 << (mreg + 1)));
285 *((unsigned short*)insns + 1) = (*((unsigned short*)insns + 1) & 0xf8ff) | (mreg << 8);
286 *((unsigned short*)insns + 2) = (*((unsigned short*)insns + 2) & 0xfff8) | (mreg + 1);
287 *((unsigned short*)insns + 3) = (*((unsigned short*)insns + 3) & 0xffc7) | (mreg << 3);
288 *((unsigned short*)insns + 7) = (*((unsigned short*)insns + 7) & 0xf8ff) | (mreg << 8);
289 *((unsigned short*)insns + 8) = (*((unsigned short*)insns + 8) & 0xffc7) | (mreg << 3);
290 *((unsigned short*)insns + 9) = (*((unsigned short*)insns + 9) & 0xffc7) | ((mreg + 1) << 3);
291 *((unsigned short*)insns + 10) = (*((unsigned short*)insns + 10) & 0x00ff) | (( 1 << mreg) | (1 << (mreg + 1)));
294 if (THUMB_INSN_MATCH(APC, insn)) {
295 // ADD Rd, PC, #immed_8*4 -> ADD Rd, SP, #immed_8*4
296 *((unsigned short*)insns + 4) = ((insn & 0xffff) | 0x800); // ADD Rd, SP, #immed_8*4
298 if (THUMB_INSN_MATCH(LRO3, insn)) {
299 // LDR Rd, [PC, #immed_8*4] -> LDR Rd, [SP, #immed_8*4]
300 *((unsigned short*)insns + 4) = ((insn & 0xffff) + 0x5000); // LDR Rd, [SP, #immed_8*4]
302 if (THUMB_INSN_MATCH(MOV3, insn)) {
303 // MOV Rd, PC -> MOV Rd, SP
304 *((unsigned short*)insns + 4) = ((insn & 0xffff) ^ 0x10); // MOV Rd, SP
306 if (THUMB2_INSN_MATCH(ADR, insn)) {
307 // ADDW Rd, PC, #imm -> ADDW Rd, SP, #imm
308 insns[2] = (insn & 0xfffffff0) | 0x0d; // ADDW Rd, SP, #imm
310 if (THUMB2_INSN_MATCH(LDRW, insn) || THUMB2_INSN_MATCH(LDRBW, insn) ||
311 THUMB2_INSN_MATCH(LDRHW, insn)) {
312 // LDR.W Rt, [PC, #-<imm_12>] -> LDR.W Rt, [SP, #-<imm_8>]
313 // !!!!!!!!!!!!!!!!!!!!!!!!
314 // !!! imm_12 vs. imm_8 !!!
315 // !!!!!!!!!!!!!!!!!!!!!!!!
316 insns[2] = (insn & 0xf0fffff0) | 0x0c00000d; // LDR.W Rt, [SP, #-<imm_8>]
318 if (THUMB2_INSN_MATCH(LDRW1, insn) || THUMB2_INSN_MATCH(LDRBW1, insn) ||
319 THUMB2_INSN_MATCH(LDRHW1, insn) || THUMB2_INSN_MATCH(LDRD, insn) ||
320 THUMB2_INSN_MATCH(LDRD1, insn) || THUMB2_INSN_MATCH(LDREX, insn)) {
321 // LDRx.W Rt, [PC, #+<imm_12>] -> LDRx.W Rt, [SP, #+<imm_12>] (+/-imm_8 for LDRD Rt, Rt2, [PC, #<imm_8>]
322 insns[2] = (insn & 0xfffffff0) | 0xd; // LDRx.W Rt, [SP, #+<imm_12>]
324 if (THUMB2_INSN_MATCH(MUL, insn)) {
325 insns[2] = (insn & 0xfff0ffff) | 0x000d0000; // MUL Rd, Rn, SP
327 if (THUMB2_INSN_MATCH(DP, insn)) {
328 if (THUMB2_INSN_REG_RM(insn) == 15) {
329 insns[2] = (insn & 0xfff0ffff) | 0x000d0000; // DP Rd, Rn, PC
330 } else if (THUMB2_INSN_REG_RN(insn) == 15) {
331 insns[2] = (insn & 0xfffffff0) | 0xd; // DP Rd, PC, Rm
334 if (THUMB2_INSN_MATCH(LDRWL, insn)) {
335 // LDRx.W Rt, [PC, #<imm_12>] -> LDRx.W Rt, [SP, #+<imm_12>] (+/-imm_8 for LDRD Rt, Rt2, [PC, #<imm_8>]
336 insns[2] = (insn & 0xfffffff0) | 0xd; // LDRx.W Rt, [SP, #+<imm_12>]
338 if (THUMB2_INSN_MATCH(RSBW, insn)) {
339 insns[2] = (insn & 0xfffffff0) | 0xd; // RSB{S}.W Rd, PC, #<const> -> RSB{S}.W Rd, SP, #<const>
341 if (THUMB2_INSN_MATCH(RORW, insn) || THUMB2_INSN_MATCH(LSLW1, insn) || THUMB2_INSN_MATCH(LSRW1, insn)) {
342 if ((THUMB2_INSN_REG_RM(insn) == 15) && (THUMB2_INSN_REG_RN(insn) == 15)) {
343 insns[2] = (insn & 0xfffdfffd); // ROR.W Rd, PC, PC
344 } else if (THUMB2_INSN_REG_RM(insn) == 15) {
345 insns[2] = (insn & 0xfff0ffff) | 0xd0000; // ROR.W Rd, Rn, PC
346 } else if (THUMB2_INSN_REG_RN(insn) == 15) {
347 insns[2] = (insn & 0xfffffff0) | 0xd; // ROR.W Rd, PC, Rm
350 if (THUMB2_INSN_MATCH(ROR, insn) || THUMB2_INSN_MATCH(LSLW2, insn) || THUMB2_INSN_MATCH(LSRW2, insn)) {
351 insns[2] = (insn & 0xfff0ffff) | 0xd0000; // ROR{S} Rd, PC, #<const> -> ROR{S} Rd, SP, #<const>
365 if (THUMB2_INSN_MATCH(STRW, insn) || THUMB2_INSN_MATCH(STRBW, insn)) {
366 insns[2] = (insn & 0xfff0ffff) | 0x000d0000; // STRx.W Rt, [Rn, SP]
368 if (THUMB2_INSN_MATCH(STRD, insn) || THUMB2_INSN_MATCH(STRHT, insn) ||
369 THUMB2_INSN_MATCH(STRT, insn) || THUMB2_INSN_MATCH(STRHW1, insn)) {
370 if (THUMB2_INSN_REG_RN(insn) == 15) {
371 insns[2] = (insn & 0xfffffff0) | 0xd; // STRD/T/HT{.W} Rt, [SP, ...]
376 if (THUMB2_INSN_MATCH(STRHW, insn) && (THUMB2_INSN_REG_RN(insn) == 15)) {
377 if (THUMB2_INSN_REG_RN(insn) == 15) {
378 insns[2] = (insn & 0xf0fffff0) | 0x0c00000d; // STRH.W Rt, [SP, #-<imm_8>]
387 if ((reg == 15) && (THUMB2_INSN_MATCH(STRW, insn) ||
388 THUMB2_INSN_MATCH(STRBW, insn) ||
389 THUMB2_INSN_MATCH(STRD, insn) ||
390 THUMB2_INSN_MATCH(STRHT, insn) ||
391 THUMB2_INSN_MATCH(STRT, insn) ||
392 THUMB2_INSN_MATCH(STRHW1, insn) ||
393 THUMB2_INSN_MATCH(STRHW, insn) )) {
394 insns[2] = (insns[2] & 0x0fffffff) | 0xd0000000;
397 if (THUMB2_INSN_MATCH(TEQ1, insn) || THUMB2_INSN_MATCH(TST1, insn)) {
398 insns[2] = (insn & 0xfffffff0) | 0xd; // TEQ SP, #<const>
400 if (THUMB2_INSN_MATCH(TEQ2, insn) || THUMB2_INSN_MATCH(TST2, insn)) {
401 if ((THUMB2_INSN_REG_RN(insn) == 15) && (THUMB2_INSN_REG_RM(insn) == 15)) {
402 insns[2] = (insn & 0xfffdfffd); // TEQ/TST PC, PC
403 } else if (THUMB2_INSN_REG_RM(insn) == 15) {
404 insns[2] = (insn & 0xfff0ffff) | 0xd0000; // TEQ/TST Rn, PC
405 } else if (THUMB2_INSN_REG_RN(insn) == 15) {
406 insns[2] = (insn & 0xfffffff0) | 0xd; // TEQ/TST PC, Rm
414 static int arch_copy_trampoline_thumb_uprobe(struct kprobe *p, struct task_struct *task, int atomic)
418 kprobe_opcode_t insn[MAX_INSN_SIZE];
419 struct arch_specific_insn ainsn;
420 kprobe_opcode_t insns[UPROBES_TRAMP_LEN * 2];
423 if ((unsigned long)p->addr & 0x01) {
424 printk("Error in %s at %d: attempt to register kprobe at an unaligned address\n", __FILE__, __LINE__);
429 ainsn.insn_thumb = insn;
430 if (!arch_check_insn_thumb(&ainsn)) {
437 if (THUMB_INSN_MATCH(APC, insn[0]) || THUMB_INSN_MATCH(LRO3, insn[0])) {
438 uregs = 0x0700; // 8-10
440 } else if (THUMB_INSN_MATCH(MOV3, insn[0]) && (((((unsigned char)insn[0]) & 0xff) >> 3) == 15)) {
444 } else if THUMB2_INSN_MATCH(ADR, insn[0]) {
445 uregs = 0x0f00; // Rd 8-11
447 } else if (((THUMB2_INSN_MATCH(LDRW, insn[0]) || THUMB2_INSN_MATCH(LDRW1, insn[0]) ||
448 THUMB2_INSN_MATCH(LDRBW, insn[0]) || THUMB2_INSN_MATCH(LDRBW1, insn[0]) ||
449 THUMB2_INSN_MATCH(LDRHW, insn[0]) || THUMB2_INSN_MATCH(LDRHW1, insn[0]) ||
450 THUMB2_INSN_MATCH(LDRWL, insn[0])) && THUMB2_INSN_REG_RN(insn[0]) == 15) ||
451 THUMB2_INSN_MATCH(LDREX, insn[0]) ||
452 ((THUMB2_INSN_MATCH(STRW, insn[0]) || THUMB2_INSN_MATCH(STRBW, insn[0]) ||
453 THUMB2_INSN_MATCH(STRHW, insn[0]) || THUMB2_INSN_MATCH(STRHW1, insn[0])) &&
454 (THUMB2_INSN_REG_RN(insn[0]) == 15 || THUMB2_INSN_REG_RT(insn[0]) == 15)) ||
455 ((THUMB2_INSN_MATCH(STRT, insn[0]) || THUMB2_INSN_MATCH(STRHT, insn[0])) &&
456 (THUMB2_INSN_REG_RN(insn[0]) == 15 || THUMB2_INSN_REG_RT(insn[0]) == 15))) {
457 uregs = 0xf000; // Rt 12-15
459 } else if ((THUMB2_INSN_MATCH(LDRD, insn[0]) || THUMB2_INSN_MATCH(LDRD1, insn[0])) && (THUMB2_INSN_REG_RN(insn[0]) == 15)) {
460 uregs = 0xff00; // Rt 12-15, Rt2 8-11
462 } else if (THUMB2_INSN_MATCH(MUL, insn[0]) && THUMB2_INSN_REG_RM(insn[0]) == 15) {
465 } else if (THUMB2_INSN_MATCH(DP, insn[0]) && (THUMB2_INSN_REG_RN(insn[0]) == 15 || THUMB2_INSN_REG_RM(insn[0]) == 15)) {
466 uregs = 0xf000; // Rd 12-15
468 } else if (THUMB2_INSN_MATCH(STRD, insn[0]) && ((THUMB2_INSN_REG_RN(insn[0]) == 15) || (THUMB2_INSN_REG_RT(insn[0]) == 15) || THUMB2_INSN_REG_RT2(insn[0]) == 15)) {
469 uregs = 0xff00; // Rt 12-15, Rt2 8-11
471 } else if (THUMB2_INSN_MATCH(RSBW, insn[0]) && THUMB2_INSN_REG_RN(insn[0]) == 15) {
472 uregs = 0x0f00; // Rd 8-11
474 } else if (THUMB2_INSN_MATCH (RORW, insn[0]) && (THUMB2_INSN_REG_RN(insn[0]) == 15 || THUMB2_INSN_REG_RM(insn[0]) == 15)) {
477 } else if ((THUMB2_INSN_MATCH(ROR, insn[0]) || THUMB2_INSN_MATCH(LSLW2, insn[0]) || THUMB2_INSN_MATCH(LSRW2, insn[0])) && THUMB2_INSN_REG_RM(insn[0]) == 15) {
478 uregs = 0x0f00; // Rd 8-11
480 } else if ((THUMB2_INSN_MATCH(LSLW1, insn[0]) || THUMB2_INSN_MATCH(LSRW1, insn[0])) && (THUMB2_INSN_REG_RN(insn[0]) == 15 || THUMB2_INSN_REG_RM(insn[0]) == 15)) {
481 uregs = 0x0f00; // Rd 8-11
483 } else if ((THUMB2_INSN_MATCH(TEQ1, insn[0]) || THUMB2_INSN_MATCH(TST1, insn[0])) && THUMB2_INSN_REG_RN(insn[0]) == 15) {
484 uregs = 0xf0000; //Rn 0-3 (16-19)
486 } else if ((THUMB2_INSN_MATCH(TEQ2, insn[0]) || THUMB2_INSN_MATCH(TST2, insn[0])) &&
487 (THUMB2_INSN_REG_RN(insn[0]) == 15 || THUMB2_INSN_REG_RM(insn[0]) == 15)) {
488 uregs = 0xf0000; //Rn 0-3 (16-19)
492 if (unlikely(uregs && pc_dep)) {
493 memcpy(insns, pc_dep_insn_execbuf_thumb, 18 * 2);
494 if (prep_pc_dep_insn_execbuf_thumb(insns, insn[0], uregs) != 0) {
495 printk("Error in %s at %d: failed to prepare exec buffer for insn %lx!",
496 __FILE__, __LINE__, insn[0]);
498 //free_insn_slot (&uprobe_insn_pages, task, p->ainsn.insn_thumb, 0);
502 addr = ((unsigned int)p->addr) + 4;
503 *((unsigned short*)insns + 13) = 0xdeff;
504 *((unsigned short*)insns + 14) = addr & 0x0000ffff;
505 *((unsigned short*)insns + 15) = addr >> 16;
506 if (!is_thumb2(insn[0])) {
507 addr = ((unsigned int)p->addr) + 2;
508 *((unsigned short*)insns + 16) = (addr & 0x0000ffff) | 0x1;
509 *((unsigned short*)insns + 17) = addr >> 16;
511 addr = ((unsigned int)p->addr) + 4;
512 *((unsigned short*)insns + 16) = (addr & 0x0000ffff) | 0x1;
513 *((unsigned short*)insns + 17) = addr >> 16;
516 memcpy(insns, gen_insn_execbuf_thumb, 18 * 2);
517 *((unsigned short*)insns + 13) = 0xdeff;
518 if (!is_thumb2(insn[0])) {
519 addr = ((unsigned int)p->addr) + 2;
520 *((unsigned short*)insns + 2) = insn[0];
521 *((unsigned short*)insns + 16) = (addr & 0x0000ffff) | 0x1;
522 *((unsigned short*)insns + 17) = addr >> 16;
524 addr = ((unsigned int)p->addr) + 4;
526 *((unsigned short*)insns + 16) = (addr & 0x0000ffff) | 0x1;
527 *((unsigned short*)insns + 17) = addr >> 16;
531 if (!write_proc_vm_atomic (task, (unsigned long)p->ainsn.insn_thumb, insns, 18 * 2)) {
532 panic("failed to write memory %p!\n", p->ainsn.insn_thumb);
533 // Mr_Nobody: we have to panic, really??...
534 //free_insn_slot (&uprobe_insn_pages, task, p->ainsn.insn_thumb, 0);
541 int arch_prepare_uprobe(struct kprobe *p, struct task_struct *task, int atomic)
544 kprobe_opcode_t insn[MAX_INSN_SIZE];
546 if ((unsigned long)p->addr & 0x01) {
547 printk("Error in %s at %d: attempt to register kprobe at an unaligned address\n", __FILE__, __LINE__);
551 if (!read_proc_vm_atomic(task, (unsigned long)p->addr, &insn, MAX_INSN_SIZE * sizeof(kprobe_opcode_t))) {
552 panic("Failed to read memory task[tgid=%u, comm=%s] %p!\n", task->tgid, task->comm, p->addr);
556 p->ainsn.insn_arm = get_insn_slot(task, &uprobe_insn_pages, atomic);
557 if (!p->ainsn.insn_arm) {
558 printk("Error in %s at %d: kprobe slot allocation error (arm)\n", __FILE__, __LINE__);
562 ret = arch_copy_trampoline_arm_uprobe(p, task, 1);
564 free_insn_slot(&uprobe_insn_pages, task, p->ainsn.insn_arm);
568 p->ainsn.insn_thumb = get_insn_slot(task, &uprobe_insn_pages, atomic);
569 if (!p->ainsn.insn_thumb) {
570 printk("Error in %s at %d: kprobe slot allocation error (thumb)\n", __FILE__, __LINE__);
574 ret = arch_copy_trampoline_thumb_uprobe(p, task, 1);
576 free_insn_slot(&uprobe_insn_pages, task, p->ainsn.insn_arm);
577 free_insn_slot(&uprobe_insn_pages, task, p->ainsn.insn_thumb);
581 if ((p->safe_arm == -1) && (p->safe_thumb == -1)) {
582 printk("Error in %s at %d: failed arch_copy_trampoline_*_uprobe() (both) [tgid=%u, addr=%lx, data=%lx]\n",
583 __FILE__, __LINE__, task->tgid, (unsigned long)p->addr, (unsigned long)p->opcode);
584 if (!write_proc_vm_atomic(task, (unsigned long)p->addr, &p->opcode, sizeof(p->opcode))) {
585 panic("Failed to write memory %p!\n", p->addr);
588 free_insn_slot(&uprobe_insn_pages, task, p->ainsn.insn_arm);
589 free_insn_slot(&uprobe_insn_pages, task, p->ainsn.insn_thumb);
597 void arch_prepare_uretprobe_hl(struct uretprobe_instance *ri,
598 struct pt_regs *regs)
600 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
601 ri->sp = (kprobe_opcode_t *)regs->ARM_sp;
603 /* Set flag of current mode */
604 ri->sp = (kprobe_opcode_t *)((long)ri->sp | !!thumb_mode(regs));
606 if (thumb_mode(regs)) {
607 regs->ARM_lr = (unsigned long)(ri->rp->kp.ainsn.insn) + 0x1b;
609 regs->ARM_lr = (unsigned long)(ri->rp->kp.ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
613 int setjmp_upre_handler(struct kprobe *p, struct pt_regs *regs)
615 struct jprobe *jp = container_of(p, struct jprobe, kp);
616 kprobe_pre_entry_handler_t pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
617 entry_point_t entry = (entry_point_t)jp->entry;
620 p->ss_addr = (kprobe_opcode_t *)pre_entry(jp->priv_arg, regs);
624 entry(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2,
625 regs->ARM_r3, regs->ARM_r4, regs->ARM_r5);
627 dbi_arch_uprobe_return();
633 unsigned long arch_get_trampoline_addr(struct kprobe *p, struct pt_regs *regs)
635 return thumb_mode(regs) ?
636 (unsigned long)(p->ainsn.insn) + 0x1b :
637 (unsigned long)(p->ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
640 void arch_set_orig_ret_addr(unsigned long orig_ret_addr, struct pt_regs *regs)
642 regs->ARM_lr = orig_ret_addr;
643 regs->ARM_pc = orig_ret_addr;
645 if (thumb_mode(regs) && !(regs->ARM_lr & 0x01)) {
646 regs->ARM_cpsr &= 0xFFFFFFDF;
647 } else if (user_mode(regs) && (regs->ARM_lr & 0x01)) {
648 regs->ARM_cpsr |= 0x20;
652 static int check_validity_insn(struct kprobe *p, struct pt_regs *regs, struct task_struct *task)
656 if (unlikely(thumb_mode(regs))) {
657 if (p->safe_thumb != -1) {
658 p->ainsn.insn = p->ainsn.insn_thumb;
659 list_for_each_entry_rcu(kp, &p->list, list) {
660 kp->ainsn.insn = p->ainsn.insn_thumb;
663 printk("Error in %s at %d: we are in thumb mode (!) and check instruction was fail \
664 (%0lX instruction at %p address)!\n", __FILE__, __LINE__, p->opcode, p->addr);
665 // Test case when we do our actions on already running application
666 disarm_uprobe(p, task);
670 if (p->safe_arm != -1) {
671 p->ainsn.insn = p->ainsn.insn_arm;
672 list_for_each_entry_rcu(kp, &p->list, list) {
673 kp->ainsn.insn = p->ainsn.insn_arm;
676 printk("Error in %s at %d: we are in arm mode (!) and check instruction was fail \
677 (%0lX instruction at %p address)!\n", __FILE__, __LINE__, p->opcode, p->addr);
678 // Test case when we do our actions on already running application
679 disarm_uprobe(p, task);
687 static int uprobe_handler(struct pt_regs *regs)
689 kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->ARM_pc);
690 struct task_struct *task = current;
691 pid_t tgid = task->tgid;
694 p = get_uprobe(addr, tgid);
696 if (p && (check_validity_insn(p, regs, task) != 0)) {
697 printk("no_uprobe live\n");
702 p = get_kprobe_by_insn_slot(addr, tgid, regs);
704 printk("no_uprobe\n");
708 trampoline_uprobe_handler(p, regs);
712 /* restore opcode for thumb app */
713 if (thumb_mode(regs)) {
714 if (!is_thumb2(p->opcode)) {
715 unsigned long tmp = p->opcode >> 16;
716 write_proc_vm_atomic(task, (unsigned long)((unsigned short*)p->addr + 1), &tmp, 2);
718 // "2*sizeof(kprobe_opcode_t)" - strange. Should be "sizeof(kprobe_opcode_t)", need to test
719 flush_icache_range((unsigned int)p->addr, ((unsigned int)p->addr) + (2 * sizeof(kprobe_opcode_t)));
723 if (!p->pre_handler || !p->pre_handler(p, regs)) {
724 prepare_singlestep(p, regs);
730 int uprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
734 local_irq_save(flags);
737 ret = uprobe_handler(regs);
738 preempt_enable_no_resched();
740 local_irq_restore(flags);
744 /* userspace probes hook (arm) */
745 static struct undef_hook undef_hook_for_us_arm = {
746 .instr_mask = 0xffffffff,
747 .instr_val = BREAKPOINT_INSTRUCTION,
748 .cpsr_mask = MODE_MASK,
749 .cpsr_val = USR_MODE,
750 .fn = uprobe_trap_handler
753 /* userspace probes hook (thumb) */
754 static struct undef_hook undef_hook_for_us_thumb = {
755 .instr_mask = 0xffffffff,
756 .instr_val = BREAKPOINT_INSTRUCTION & 0x0000ffff,
757 .cpsr_mask = MODE_MASK,
758 .cpsr_val = USR_MODE,
759 .fn = uprobe_trap_handler
762 int swap_arch_init_uprobes(void)
764 swap_register_undef_hook(&undef_hook_for_us_arm);
765 swap_register_undef_hook(&undef_hook_for_us_thumb);
770 void swap_arch_exit_uprobes(void)
772 swap_unregister_undef_hook(&undef_hook_for_us_thumb);
773 swap_unregister_undef_hook(&undef_hook_for_us_arm);