blob: c1cdc0d27e051084ab1994f28ee65881e81ccebc [file] [log] [blame]
David A. Longc18377c2014-03-07 11:16:10 -05001/*
2 * arch/arm/kernel/probes.c
3 *
4 * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
5 *
6 * Some contents moved here from arch/arm/include/asm/kprobes-arm.c which is
7 * Copyright (C) 2006, 2007 Motorola Inc.
8 *
9 * 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/types.h>
David A. Longc18377c2014-03-07 11:16:10 -050016#include <asm/system_info.h>
17#include <asm/ptrace.h>
18#include <linux/bug.h>
19
David A. Long7579f4b32014-03-07 11:19:32 -050020#include "probes.h"
David A. Longc18377c2014-03-07 11:16:10 -050021
22
23#ifndef find_str_pc_offset
24
25/*
26 * For STR and STM instructions, an ARM core may choose to use either
27 * a +8 or a +12 displacement from the current instruction's address.
28 * Whichever value is chosen for a given core, it must be the same for
29 * both instructions and may not change. This function measures it.
30 */
31
32int str_pc_offset;
33
34void __init find_str_pc_offset(void)
35{
36 int addr, scratch, ret;
37
38 __asm__ (
39 "sub %[ret], pc, #4 \n\t"
40 "str pc, %[addr] \n\t"
41 "ldr %[scr], %[addr] \n\t"
42 "sub %[ret], %[scr], %[ret] \n\t"
43 : [ret] "=r" (ret), [scr] "=r" (scratch), [addr] "+m" (addr));
44
45 str_pc_offset = ret;
46}
47
48#endif /* !find_str_pc_offset */
49
50
51#ifndef test_load_write_pc_interworking
52
53bool load_write_pc_interworks;
54
55void __init test_load_write_pc_interworking(void)
56{
57 int arch = cpu_architecture();
58 BUG_ON(arch == CPU_ARCH_UNKNOWN);
59 load_write_pc_interworks = arch >= CPU_ARCH_ARMv5T;
60}
61
62#endif /* !test_load_write_pc_interworking */
63
64
65#ifndef test_alu_write_pc_interworking
66
67bool alu_write_pc_interworks;
68
69void __init test_alu_write_pc_interworking(void)
70{
71 int arch = cpu_architecture();
72 BUG_ON(arch == CPU_ARCH_UNKNOWN);
73 alu_write_pc_interworks = arch >= CPU_ARCH_ARMv7;
74}
75
76#endif /* !test_alu_write_pc_interworking */
77
78
79void __init arm_kprobe_decode_init(void)
80{
81 find_str_pc_offset();
82 test_load_write_pc_interworking();
83 test_alu_write_pc_interworking();
84}
85
86
87static unsigned long __kprobes __check_eq(unsigned long cpsr)
88{
89 return cpsr & PSR_Z_BIT;
90}
91
92static unsigned long __kprobes __check_ne(unsigned long cpsr)
93{
94 return (~cpsr) & PSR_Z_BIT;
95}
96
97static unsigned long __kprobes __check_cs(unsigned long cpsr)
98{
99 return cpsr & PSR_C_BIT;
100}
101
102static unsigned long __kprobes __check_cc(unsigned long cpsr)
103{
104 return (~cpsr) & PSR_C_BIT;
105}
106
107static unsigned long __kprobes __check_mi(unsigned long cpsr)
108{
109 return cpsr & PSR_N_BIT;
110}
111
112static unsigned long __kprobes __check_pl(unsigned long cpsr)
113{
114 return (~cpsr) & PSR_N_BIT;
115}
116
117static unsigned long __kprobes __check_vs(unsigned long cpsr)
118{
119 return cpsr & PSR_V_BIT;
120}
121
122static unsigned long __kprobes __check_vc(unsigned long cpsr)
123{
124 return (~cpsr) & PSR_V_BIT;
125}
126
127static unsigned long __kprobes __check_hi(unsigned long cpsr)
128{
129 cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
130 return cpsr & PSR_C_BIT;
131}
132
133static unsigned long __kprobes __check_ls(unsigned long cpsr)
134{
135 cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
136 return (~cpsr) & PSR_C_BIT;
137}
138
139static unsigned long __kprobes __check_ge(unsigned long cpsr)
140{
141 cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
142 return (~cpsr) & PSR_N_BIT;
143}
144
145static unsigned long __kprobes __check_lt(unsigned long cpsr)
146{
147 cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
148 return cpsr & PSR_N_BIT;
149}
150
151static unsigned long __kprobes __check_gt(unsigned long cpsr)
152{
153 unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
154 temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */
155 return (~temp) & PSR_N_BIT;
156}
157
158static unsigned long __kprobes __check_le(unsigned long cpsr)
159{
160 unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
161 temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */
162 return temp & PSR_N_BIT;
163}
164
165static unsigned long __kprobes __check_al(unsigned long cpsr)
166{
167 return true;
168}
169
David A. Longf145d662014-03-05 21:17:23 -0500170probes_check_cc * const probes_condition_checks[16] = {
David A. Longc18377c2014-03-07 11:16:10 -0500171 &__check_eq, &__check_ne, &__check_cs, &__check_cc,
172 &__check_mi, &__check_pl, &__check_vs, &__check_vc,
173 &__check_hi, &__check_ls, &__check_ge, &__check_lt,
174 &__check_gt, &__check_le, &__check_al, &__check_al
175};
176
177
David A. Longf145d662014-03-05 21:17:23 -0500178void __kprobes kprobe_simulate_nop(probes_opcode_t opcode,
David A. Long7579f4b32014-03-07 11:19:32 -0500179 struct arch_specific_insn *asi,
180 struct pt_regs *regs)
David A. Longc18377c2014-03-07 11:16:10 -0500181{
182}
183
David A. Longf145d662014-03-05 21:17:23 -0500184void __kprobes kprobe_emulate_none(probes_opcode_t opcode,
David A. Long7579f4b32014-03-07 11:19:32 -0500185 struct arch_specific_insn *asi,
186 struct pt_regs *regs)
David A. Longc18377c2014-03-07 11:16:10 -0500187{
David A. Long7579f4b32014-03-07 11:19:32 -0500188 asi->insn_fn();
David A. Longc18377c2014-03-07 11:16:10 -0500189}
190
191/*
192 * Prepare an instruction slot to receive an instruction for emulating.
193 * This is done by placing a subroutine return after the location where the
194 * instruction will be placed. We also modify ARM instructions to be
195 * unconditional as the condition code will already be checked before any
196 * emulation handler is called.
197 */
David A. Longf145d662014-03-05 21:17:23 -0500198static probes_opcode_t __kprobes
199prepare_emulated_insn(probes_opcode_t insn, struct arch_specific_insn *asi,
David A. Longc18377c2014-03-07 11:16:10 -0500200 bool thumb)
201{
202#ifdef CONFIG_THUMB2_KERNEL
203 if (thumb) {
204 u16 *thumb_insn = (u16 *)asi->insn;
205 thumb_insn[1] = 0x4770; /* Thumb bx lr */
206 thumb_insn[2] = 0x4770; /* Thumb bx lr */
207 return insn;
208 }
209 asi->insn[1] = 0xe12fff1e; /* ARM bx lr */
210#else
211 asi->insn[1] = 0xe1a0f00e; /* mov pc, lr */
212#endif
213 /* Make an ARM instruction unconditional */
214 if (insn < 0xe0000000)
215 insn = (insn | 0xe0000000) & ~0x10000000;
216 return insn;
217}
218
219/*
220 * Write a (probably modified) instruction into the slot previously prepared by
221 * prepare_emulated_insn
222 */
223static void __kprobes
David A. Longf145d662014-03-05 21:17:23 -0500224set_emulated_insn(probes_opcode_t insn, struct arch_specific_insn *asi,
David A. Longc18377c2014-03-07 11:16:10 -0500225 bool thumb)
226{
227#ifdef CONFIG_THUMB2_KERNEL
228 if (thumb) {
229 u16 *ip = (u16 *)asi->insn;
230 if (is_wide_instruction(insn))
231 *ip++ = insn >> 16;
232 *ip++ = insn;
233 return;
234 }
235#endif
236 asi->insn[0] = insn;
237}
238
239/*
240 * When we modify the register numbers encoded in an instruction to be emulated,
241 * the new values come from this define. For ARM and 32-bit Thumb instructions
242 * this gives...
243 *
244 * bit position 16 12 8 4 0
245 * ---------------+---+---+---+---+---+
246 * register r2 r0 r1 -- r3
247 */
248#define INSN_NEW_BITS 0x00020103
249
250/* Each nibble has same value as that at INSN_NEW_BITS bit 16 */
251#define INSN_SAMEAS16_BITS 0x22222222
252
253/*
254 * Validate and modify each of the registers encoded in an instruction.
255 *
256 * Each nibble in regs contains a value from enum decode_reg_type. For each
257 * non-zero value, the corresponding nibble in pinsn is validated and modified
258 * according to the type.
259 */
David A. Longf145d662014-03-05 21:17:23 -0500260static bool __kprobes decode_regs(probes_opcode_t *pinsn, u32 regs)
David A. Longc18377c2014-03-07 11:16:10 -0500261{
David A. Longf145d662014-03-05 21:17:23 -0500262 probes_opcode_t insn = *pinsn;
263 probes_opcode_t mask = 0xf; /* Start at least significant nibble */
David A. Longc18377c2014-03-07 11:16:10 -0500264
265 for (; regs != 0; regs >>= 4, mask <<= 4) {
266
David A. Longf145d662014-03-05 21:17:23 -0500267 probes_opcode_t new_bits = INSN_NEW_BITS;
David A. Longc18377c2014-03-07 11:16:10 -0500268
269 switch (regs & 0xf) {
270
271 case REG_TYPE_NONE:
272 /* Nibble not a register, skip to next */
273 continue;
274
275 case REG_TYPE_ANY:
276 /* Any register is allowed */
277 break;
278
279 case REG_TYPE_SAMEAS16:
280 /* Replace register with same as at bit position 16 */
281 new_bits = INSN_SAMEAS16_BITS;
282 break;
283
284 case REG_TYPE_SP:
285 /* Only allow SP (R13) */
286 if ((insn ^ 0xdddddddd) & mask)
287 goto reject;
288 break;
289
290 case REG_TYPE_PC:
291 /* Only allow PC (R15) */
292 if ((insn ^ 0xffffffff) & mask)
293 goto reject;
294 break;
295
296 case REG_TYPE_NOSP:
297 /* Reject SP (R13) */
298 if (((insn ^ 0xdddddddd) & mask) == 0)
299 goto reject;
300 break;
301
302 case REG_TYPE_NOSPPC:
303 case REG_TYPE_NOSPPCX:
304 /* Reject SP and PC (R13 and R15) */
305 if (((insn ^ 0xdddddddd) & 0xdddddddd & mask) == 0)
306 goto reject;
307 break;
308
309 case REG_TYPE_NOPCWB:
310 if (!is_writeback(insn))
311 break; /* No writeback, so any register is OK */
312 /* fall through... */
313 case REG_TYPE_NOPC:
314 case REG_TYPE_NOPCX:
315 /* Reject PC (R15) */
316 if (((insn ^ 0xffffffff) & mask) == 0)
317 goto reject;
318 break;
319 }
320
321 /* Replace value of nibble with new register number... */
322 insn &= ~mask;
323 insn |= new_bits & mask;
324 }
325
326 *pinsn = insn;
327 return true;
328
329reject:
330 return false;
331}
332
333static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
334 [DECODE_TYPE_TABLE] = sizeof(struct decode_table),
335 [DECODE_TYPE_CUSTOM] = sizeof(struct decode_custom),
336 [DECODE_TYPE_SIMULATE] = sizeof(struct decode_simulate),
337 [DECODE_TYPE_EMULATE] = sizeof(struct decode_emulate),
338 [DECODE_TYPE_OR] = sizeof(struct decode_or),
339 [DECODE_TYPE_REJECT] = sizeof(struct decode_reject)
340};
341
342/*
343 * kprobe_decode_insn operates on data tables in order to decode an ARM
344 * architecture instruction onto which a kprobe has been placed.
345 *
346 * These instruction decoding tables are a concatenation of entries each
347 * of which consist of one of the following structs:
348 *
349 * decode_table
350 * decode_custom
351 * decode_simulate
352 * decode_emulate
353 * decode_or
354 * decode_reject
355 *
356 * Each of these starts with a struct decode_header which has the following
357 * fields:
358 *
359 * type_regs
360 * mask
361 * value
362 *
363 * The least significant DECODE_TYPE_BITS of type_regs contains a value
364 * from enum decode_type, this indicates which of the decode_* structs
365 * the entry contains. The value DECODE_TYPE_END indicates the end of the
366 * table.
367 *
368 * When the table is parsed, each entry is checked in turn to see if it
369 * matches the instruction to be decoded using the test:
370 *
371 * (insn & mask) == value
372 *
373 * If no match is found before the end of the table is reached then decoding
374 * fails with INSN_REJECTED.
375 *
376 * When a match is found, decode_regs() is called to validate and modify each
377 * of the registers encoded in the instruction; the data it uses to do this
378 * is (type_regs >> DECODE_TYPE_BITS). A validation failure will cause decoding
379 * to fail with INSN_REJECTED.
380 *
381 * Once the instruction has passed the above tests, further processing
382 * depends on the type of the table entry's decode struct.
383 *
384 */
385int __kprobes
David A. Longf145d662014-03-05 21:17:23 -0500386kprobe_decode_insn(probes_opcode_t insn, struct arch_specific_insn *asi,
David A. Long3e6cd392014-03-06 18:06:43 -0500387 const union decode_item *table, bool thumb,
388 const union decode_action *actions)
David A. Longc18377c2014-03-07 11:16:10 -0500389{
390 const struct decode_header *h = (struct decode_header *)table;
391 const struct decode_header *next;
392 bool matched = false;
393
394 insn = prepare_emulated_insn(insn, asi, thumb);
395
396 for (;; h = next) {
397 enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
398 u32 regs = h->type_regs.bits >> DECODE_TYPE_BITS;
399
400 if (type == DECODE_TYPE_END)
401 return INSN_REJECTED;
402
403 next = (struct decode_header *)
404 ((uintptr_t)h + decode_struct_sizes[type]);
405
406 if (!matched && (insn & h->mask.bits) != h->value.bits)
407 continue;
408
409 if (!decode_regs(&insn, regs))
410 return INSN_REJECTED;
411
412 switch (type) {
413
414 case DECODE_TYPE_TABLE: {
415 struct decode_table *d = (struct decode_table *)h;
416 next = (struct decode_header *)d->table.table;
417 break;
418 }
419
420 case DECODE_TYPE_CUSTOM: {
421 struct decode_custom *d = (struct decode_custom *)h;
David A. Long3e6cd392014-03-06 18:06:43 -0500422 return actions[d->decoder.action].decoder(insn, asi, h);
David A. Longc18377c2014-03-07 11:16:10 -0500423 }
424
425 case DECODE_TYPE_SIMULATE: {
426 struct decode_simulate *d = (struct decode_simulate *)h;
David A. Long3e6cd392014-03-06 18:06:43 -0500427 asi->insn_handler = actions[d->handler.action].handler;
David A. Longc18377c2014-03-07 11:16:10 -0500428 return INSN_GOOD_NO_SLOT;
429 }
430
431 case DECODE_TYPE_EMULATE: {
432 struct decode_emulate *d = (struct decode_emulate *)h;
David A. Long3e6cd392014-03-06 18:06:43 -0500433 asi->insn_handler = actions[d->handler.action].handler;
David A. Longc18377c2014-03-07 11:16:10 -0500434 set_emulated_insn(insn, asi, thumb);
435 return INSN_GOOD;
436 }
437
438 case DECODE_TYPE_OR:
439 matched = true;
440 break;
441
442 case DECODE_TYPE_REJECT:
443 default:
444 return INSN_REJECTED;
445 }
446 }
447}