blob: 43d663cafdd1eae3c112baeb996ea5bd420b6054 [file] [log] [blame]
Jon Medhurst0ab4c022011-07-06 11:25:18 +01001/*
2 * arch/arm/kernel/kprobes-common.c
3 *
4 * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
5 *
Jon Medhurst6c8df332011-07-07 10:21:40 +01006 * Some contents moved here from arch/arm/include/asm/kprobes-arm.c which is
7 * Copyright (C) 2006, 2007 Motorola Inc.
8 *
Jon Medhurst0ab4c022011-07-06 11:25:18 +01009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/kernel.h>
15#include <linux/kprobes.h>
16
17#include "kprobes.h"
18
19
Jon Medhurstaea49022011-07-07 19:58:29 +010020#ifndef find_str_pc_offset
21
Jon Medhurst6c8df332011-07-07 10:21:40 +010022/*
23 * For STR and STM instructions, an ARM core may choose to use either
24 * a +8 or a +12 displacement from the current instruction's address.
25 * Whichever value is chosen for a given core, it must be the same for
26 * both instructions and may not change. This function measures it.
27 */
28
29int str_pc_offset;
30
31void __init find_str_pc_offset(void)
32{
33 int addr, scratch, ret;
34
35 __asm__ (
36 "sub %[ret], pc, #4 \n\t"
37 "str pc, %[addr] \n\t"
38 "ldr %[scr], %[addr] \n\t"
39 "sub %[ret], %[scr], %[ret] \n\t"
40 : [ret] "=r" (ret), [scr] "=r" (scratch), [addr] "+m" (addr));
41
42 str_pc_offset = ret;
43}
44
Jon Medhurstaea49022011-07-07 19:58:29 +010045#endif /* !find_str_pc_offset */
46
Jon Medhurst6c8df332011-07-07 10:21:40 +010047
Jon Medhurst263e3682011-06-10 20:29:04 +010048#ifndef test_load_write_pc_interworking
49
50bool load_write_pc_interworks;
51
52void __init test_load_write_pc_interworking(void)
53{
54 int arch = cpu_architecture();
55 BUG_ON(arch == CPU_ARCH_UNKNOWN);
56 load_write_pc_interworks = arch >= CPU_ARCH_ARMv5T;
57}
58
59#endif /* !test_load_write_pc_interworking */
60
61
Jon Medhurst6c8df332011-07-07 10:21:40 +010062void __init arm_kprobe_decode_init(void)
63{
64 find_str_pc_offset();
Jon Medhurst263e3682011-06-10 20:29:04 +010065 test_load_write_pc_interworking();
Jon Medhurst6c8df332011-07-07 10:21:40 +010066}
67
68
Jon Medhurst0ab4c022011-07-06 11:25:18 +010069static unsigned long __kprobes __check_eq(unsigned long cpsr)
70{
71 return cpsr & PSR_Z_BIT;
72}
73
74static unsigned long __kprobes __check_ne(unsigned long cpsr)
75{
76 return (~cpsr) & PSR_Z_BIT;
77}
78
79static unsigned long __kprobes __check_cs(unsigned long cpsr)
80{
81 return cpsr & PSR_C_BIT;
82}
83
84static unsigned long __kprobes __check_cc(unsigned long cpsr)
85{
86 return (~cpsr) & PSR_C_BIT;
87}
88
89static unsigned long __kprobes __check_mi(unsigned long cpsr)
90{
91 return cpsr & PSR_N_BIT;
92}
93
94static unsigned long __kprobes __check_pl(unsigned long cpsr)
95{
96 return (~cpsr) & PSR_N_BIT;
97}
98
99static unsigned long __kprobes __check_vs(unsigned long cpsr)
100{
101 return cpsr & PSR_V_BIT;
102}
103
104static unsigned long __kprobes __check_vc(unsigned long cpsr)
105{
106 return (~cpsr) & PSR_V_BIT;
107}
108
109static unsigned long __kprobes __check_hi(unsigned long cpsr)
110{
111 cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
112 return cpsr & PSR_C_BIT;
113}
114
115static unsigned long __kprobes __check_ls(unsigned long cpsr)
116{
117 cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
118 return (~cpsr) & PSR_C_BIT;
119}
120
121static unsigned long __kprobes __check_ge(unsigned long cpsr)
122{
123 cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
124 return (~cpsr) & PSR_N_BIT;
125}
126
127static unsigned long __kprobes __check_lt(unsigned long cpsr)
128{
129 cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
130 return cpsr & PSR_N_BIT;
131}
132
133static unsigned long __kprobes __check_gt(unsigned long cpsr)
134{
135 unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
136 temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */
137 return (~temp) & PSR_N_BIT;
138}
139
140static unsigned long __kprobes __check_le(unsigned long cpsr)
141{
142 unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
143 temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */
144 return temp & PSR_N_BIT;
145}
146
147static unsigned long __kprobes __check_al(unsigned long cpsr)
148{
149 return true;
150}
151
152kprobe_check_cc * const kprobe_condition_checks[16] = {
153 &__check_eq, &__check_ne, &__check_cs, &__check_cc,
154 &__check_mi, &__check_pl, &__check_vs, &__check_vc,
155 &__check_hi, &__check_ls, &__check_ge, &__check_lt,
156 &__check_gt, &__check_le, &__check_al, &__check_al
157};
Jon Medhurst0d1a0952011-04-26 15:15:56 +0100158
159
Jon Medhurst3f92dfe2011-07-02 15:36:32 +0100160void __kprobes kprobe_simulate_nop(struct kprobe *p, struct pt_regs *regs)
161{
162}
163
164void __kprobes kprobe_emulate_none(struct kprobe *p, struct pt_regs *regs)
165{
166 p->ainsn.insn_fn();
167}
168
Jon Medhurst235a4ce2011-07-07 08:57:22 +0100169static void __kprobes simulate_ldm1stm1(struct kprobe *p, struct pt_regs *regs)
170{
171 kprobe_opcode_t insn = p->opcode;
172 int rn = (insn >> 16) & 0xf;
173 int lbit = insn & (1 << 20);
174 int wbit = insn & (1 << 21);
175 int ubit = insn & (1 << 23);
176 int pbit = insn & (1 << 24);
177 long *addr = (long *)regs->uregs[rn];
178 int reg_bit_vector;
179 int reg_count;
180
181 reg_count = 0;
182 reg_bit_vector = insn & 0xffff;
183 while (reg_bit_vector) {
184 reg_bit_vector &= (reg_bit_vector - 1);
185 ++reg_count;
186 }
187
188 if (!ubit)
189 addr -= reg_count;
190 addr += (!pbit == !ubit);
191
192 reg_bit_vector = insn & 0xffff;
193 while (reg_bit_vector) {
194 int reg = __ffs(reg_bit_vector);
195 reg_bit_vector &= (reg_bit_vector - 1);
196 if (lbit)
197 regs->uregs[reg] = *addr++;
198 else
199 *addr++ = regs->uregs[reg];
200 }
201
202 if (wbit) {
203 if (!ubit)
204 addr -= reg_count;
205 addr -= (!pbit == !ubit);
206 regs->uregs[rn] = (long)addr;
207 }
208}
209
210static void __kprobes simulate_stm1_pc(struct kprobe *p, struct pt_regs *regs)
211{
212 regs->ARM_pc = (long)p->addr + str_pc_offset;
213 simulate_ldm1stm1(p, regs);
214 regs->ARM_pc = (long)p->addr + 4;
215}
216
217static void __kprobes simulate_ldm1_pc(struct kprobe *p, struct pt_regs *regs)
218{
219 simulate_ldm1stm1(p, regs);
220 load_write_pc(regs->ARM_pc, regs);
221}
222
223enum kprobe_insn __kprobes
224kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi)
225{
226 kprobe_insn_handler_t *handler = 0;
227 unsigned reglist = insn & 0xffff;
228 int is_ldm = insn & 0x100000;
229
230 if (reglist & 0x8000)
231 handler = is_ldm ? simulate_ldm1_pc : simulate_stm1_pc;
232 else
233 handler = simulate_ldm1stm1;
234 asi->insn_handler = handler;
235 return INSN_GOOD_NO_SLOT;
236}
237
238
Jon Medhurst0d1a0952011-04-26 15:15:56 +0100239/*
240 * Prepare an instruction slot to receive an instruction for emulating.
241 * This is done by placing a subroutine return after the location where the
242 * instruction will be placed. We also modify ARM instructions to be
243 * unconditional as the condition code will already be checked before any
244 * emulation handler is called.
245 */
246static kprobe_opcode_t __kprobes
247prepare_emulated_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
248 bool thumb)
249{
250#ifdef CONFIG_THUMB2_KERNEL
251 if (thumb) {
252 u16 *thumb_insn = (u16 *)asi->insn;
253 thumb_insn[1] = 0x4770; /* Thumb bx lr */
254 thumb_insn[2] = 0x4770; /* Thumb bx lr */
255 return insn;
256 }
257 asi->insn[1] = 0xe12fff1e; /* ARM bx lr */
258#else
259 asi->insn[1] = 0xe1a0f00e; /* mov pc, lr */
260#endif
261 /* Make an ARM instruction unconditional */
262 if (insn < 0xe0000000)
263 insn = (insn | 0xe0000000) & ~0x10000000;
264 return insn;
265}
266
267/*
268 * Write a (probably modified) instruction into the slot previously prepared by
269 * prepare_emulated_insn
270 */
271static void __kprobes
272set_emulated_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
273 bool thumb)
274{
275#ifdef CONFIG_THUMB2_KERNEL
276 if (thumb) {
277 u16 *ip = (u16 *)asi->insn;
278 if (is_wide_instruction(insn))
279 *ip++ = insn >> 16;
280 *ip++ = insn;
281 return;
282 }
283#endif
284 asi->insn[0] = insn;
285}
286
287/*
288 * When we modify the register numbers encoded in an instruction to be emulated,
289 * the new values come from this define. For ARM and 32-bit Thumb instructions
290 * this gives...
291 *
292 * bit position 16 12 8 4 0
293 * ---------------+---+---+---+---+---+
294 * register r2 r0 r1 -- r3
295 */
296#define INSN_NEW_BITS 0x00020103
297
298/* Each nibble has same value as that at INSN_NEW_BITS bit 16 */
299#define INSN_SAMEAS16_BITS 0x22222222
300
301/*
302 * Validate and modify each of the registers encoded in an instruction.
303 *
304 * Each nibble in regs contains a value from enum decode_reg_type. For each
305 * non-zero value, the corresponding nibble in pinsn is validated and modified
306 * according to the type.
307 */
308static bool __kprobes decode_regs(kprobe_opcode_t* pinsn, u32 regs)
309{
310 kprobe_opcode_t insn = *pinsn;
311 kprobe_opcode_t mask = 0xf; /* Start at least significant nibble */
312
313 for (; regs != 0; regs >>= 4, mask <<= 4) {
314
315 kprobe_opcode_t new_bits = INSN_NEW_BITS;
316
317 switch (regs & 0xf) {
318
319 case REG_TYPE_NONE:
320 /* Nibble not a register, skip to next */
321 continue;
322
323 case REG_TYPE_ANY:
324 /* Any register is allowed */
325 break;
326
327 case REG_TYPE_SAMEAS16:
328 /* Replace register with same as at bit position 16 */
329 new_bits = INSN_SAMEAS16_BITS;
330 break;
331
332 case REG_TYPE_SP:
333 /* Only allow SP (R13) */
334 if ((insn ^ 0xdddddddd) & mask)
335 goto reject;
336 break;
337
338 case REG_TYPE_PC:
339 /* Only allow PC (R15) */
340 if ((insn ^ 0xffffffff) & mask)
341 goto reject;
342 break;
343
344 case REG_TYPE_NOSP:
345 /* Reject SP (R13) */
346 if (((insn ^ 0xdddddddd) & mask) == 0)
347 goto reject;
348 break;
349
350 case REG_TYPE_NOSPPC:
351 case REG_TYPE_NOSPPCX:
352 /* Reject SP and PC (R13 and R15) */
353 if (((insn ^ 0xdddddddd) & 0xdddddddd & mask) == 0)
354 goto reject;
355 break;
356
357 case REG_TYPE_NOPCWB:
358 if (!is_writeback(insn))
359 break; /* No writeback, so any register is OK */
360 /* fall through... */
361 case REG_TYPE_NOPC:
362 case REG_TYPE_NOPCX:
363 /* Reject PC (R15) */
364 if (((insn ^ 0xffffffff) & mask) == 0)
365 goto reject;
366 break;
367 }
368
369 /* Replace value of nibble with new register number... */
370 insn &= ~mask;
371 insn |= new_bits & mask;
372 }
373
374 *pinsn = insn;
375 return true;
376
377reject:
378 return false;
379}
380
381static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
382 [DECODE_TYPE_TABLE] = sizeof(struct decode_table),
383 [DECODE_TYPE_CUSTOM] = sizeof(struct decode_custom),
384 [DECODE_TYPE_SIMULATE] = sizeof(struct decode_simulate),
385 [DECODE_TYPE_EMULATE] = sizeof(struct decode_emulate),
386 [DECODE_TYPE_OR] = sizeof(struct decode_or),
387 [DECODE_TYPE_REJECT] = sizeof(struct decode_reject)
388};
389
390/*
391 * kprobe_decode_insn operates on data tables in order to decode an ARM
392 * architecture instruction onto which a kprobe has been placed.
393 *
394 * These instruction decoding tables are a concatenation of entries each
395 * of which consist of one of the following structs:
396 *
397 * decode_table
398 * decode_custom
399 * decode_simulate
400 * decode_emulate
401 * decode_or
402 * decode_reject
403 *
404 * Each of these starts with a struct decode_header which has the following
405 * fields:
406 *
407 * type_regs
408 * mask
409 * value
410 *
411 * The least significant DECODE_TYPE_BITS of type_regs contains a value
412 * from enum decode_type, this indicates which of the decode_* structs
413 * the entry contains. The value DECODE_TYPE_END indicates the end of the
414 * table.
415 *
416 * When the table is parsed, each entry is checked in turn to see if it
417 * matches the instruction to be decoded using the test:
418 *
419 * (insn & mask) == value
420 *
421 * If no match is found before the end of the table is reached then decoding
422 * fails with INSN_REJECTED.
423 *
424 * When a match is found, decode_regs() is called to validate and modify each
425 * of the registers encoded in the instruction; the data it uses to do this
426 * is (type_regs >> DECODE_TYPE_BITS). A validation failure will cause decoding
427 * to fail with INSN_REJECTED.
428 *
429 * Once the instruction has passed the above tests, further processing
430 * depends on the type of the table entry's decode struct.
431 *
432 */
433int __kprobes
434kprobe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi,
435 const union decode_item *table, bool thumb)
436{
437 const struct decode_header *h = (struct decode_header *)table;
438 const struct decode_header *next;
439 bool matched = false;
440
441 insn = prepare_emulated_insn(insn, asi, thumb);
442
443 for (;; h = next) {
444 enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
445 u32 regs = h->type_regs.bits >> DECODE_TYPE_BITS;
446
447 if (type == DECODE_TYPE_END)
448 return INSN_REJECTED;
449
450 next = (struct decode_header *)
451 ((uintptr_t)h + decode_struct_sizes[type]);
452
453 if (!matched && (insn & h->mask.bits) != h->value.bits)
454 continue;
455
456 if (!decode_regs(&insn, regs))
457 return INSN_REJECTED;
458
459 switch (type) {
460
461 case DECODE_TYPE_TABLE: {
462 struct decode_table *d = (struct decode_table *)h;
463 next = (struct decode_header *)d->table.table;
464 break;
465 }
466
467 case DECODE_TYPE_CUSTOM: {
468 struct decode_custom *d = (struct decode_custom *)h;
469 return (*d->decoder.decoder)(insn, asi);
470 }
471
472 case DECODE_TYPE_SIMULATE: {
473 struct decode_simulate *d = (struct decode_simulate *)h;
474 asi->insn_handler = d->handler.handler;
475 return INSN_GOOD_NO_SLOT;
476 }
477
478 case DECODE_TYPE_EMULATE: {
479 struct decode_emulate *d = (struct decode_emulate *)h;
480 asi->insn_handler = d->handler.handler;
481 set_emulated_insn(insn, asi, thumb);
482 return INSN_GOOD;
483 }
484
485 case DECODE_TYPE_OR:
486 matched = true;
487 break;
488
489 case DECODE_TYPE_REJECT:
490 default:
491 return INSN_REJECTED;
492 }
493 }
494 }