GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / arm / probes / decode.h
1 /*
2  * arch/arm/probes/decode.h
3  *
4  * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
5  *
6  * Some contents moved here from arch/arm/include/asm/kprobes.h 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  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  */
18
19 #ifndef _ARM_KERNEL_PROBES_H
20 #define  _ARM_KERNEL_PROBES_H
21
22 #include <linux/types.h>
23 #include <linux/stddef.h>
24 #include <asm/probes.h>
25 #include <asm/ptrace.h>
26 #include <asm/kprobes.h>
27
28 void __init arm_probes_decode_init(void);
29
30 extern probes_check_cc * const probes_condition_checks[16];
31
32 #if __LINUX_ARM_ARCH__ >= 7
33
34 /* str_pc_offset is architecturally defined from ARMv7 onwards */
35 #define str_pc_offset 8
36 #define find_str_pc_offset()
37
38 #else /* __LINUX_ARM_ARCH__ < 7 */
39
40 /* We need a run-time check to determine str_pc_offset */
41 extern int str_pc_offset;
42 void __init find_str_pc_offset(void);
43
44 #endif
45
46
47 static inline void __kprobes bx_write_pc(long pcv, struct pt_regs *regs)
48 {
49         long cpsr = regs->ARM_cpsr;
50         if (pcv & 0x1) {
51                 cpsr |= PSR_T_BIT;
52                 pcv &= ~0x1;
53         } else {
54                 cpsr &= ~PSR_T_BIT;
55                 pcv &= ~0x2;    /* Avoid UNPREDICTABLE address allignment */
56         }
57         regs->ARM_cpsr = cpsr;
58         regs->ARM_pc = pcv;
59 }
60
61
62 #if __LINUX_ARM_ARCH__ >= 6
63
64 /* Kernels built for >= ARMv6 should never run on <= ARMv5 hardware, so... */
65 #define load_write_pc_interworks true
66 #define test_load_write_pc_interworking()
67
68 #else /* __LINUX_ARM_ARCH__ < 6 */
69
70 /* We need run-time testing to determine if load_write_pc() should interwork. */
71 extern bool load_write_pc_interworks;
72 void __init test_load_write_pc_interworking(void);
73
74 #endif
75
76 static inline void __kprobes load_write_pc(long pcv, struct pt_regs *regs)
77 {
78         if (load_write_pc_interworks)
79                 bx_write_pc(pcv, regs);
80         else
81                 regs->ARM_pc = pcv;
82 }
83
84
85 #if __LINUX_ARM_ARCH__ >= 7
86
87 #define alu_write_pc_interworks true
88 #define test_alu_write_pc_interworking()
89
90 #elif __LINUX_ARM_ARCH__ <= 5
91
92 /* Kernels built for <= ARMv5 should never run on >= ARMv6 hardware, so... */
93 #define alu_write_pc_interworks false
94 #define test_alu_write_pc_interworking()
95
96 #else /* __LINUX_ARM_ARCH__ == 6 */
97
98 /* We could be an ARMv6 binary on ARMv7 hardware so we need a run-time check. */
99 extern bool alu_write_pc_interworks;
100 void __init test_alu_write_pc_interworking(void);
101
102 #endif /* __LINUX_ARM_ARCH__ == 6 */
103
104 static inline void __kprobes alu_write_pc(long pcv, struct pt_regs *regs)
105 {
106         if (alu_write_pc_interworks)
107                 bx_write_pc(pcv, regs);
108         else
109                 regs->ARM_pc = pcv;
110 }
111
112
113 /*
114  * Test if load/store instructions writeback the address register.
115  * if P (bit 24) == 0 or W (bit 21) == 1
116  */
117 #define is_writeback(insn) ((insn ^ 0x01000000) & 0x01200000)
118
119 /*
120  * The following definitions and macros are used to build instruction
121  * decoding tables for use by probes_decode_insn.
122  *
123  * These tables are a concatenation of entries each of which consist of one of
124  * the decode_* structs. All of the fields in every type of decode structure
125  * are of the union type decode_item, therefore the entire decode table can be
126  * viewed as an array of these and declared like:
127  *
128  *      static const union decode_item table_name[] = {};
129  *
130  * In order to construct each entry in the table, macros are used to
131  * initialise a number of sequential decode_item values in a layout which
132  * matches the relevant struct. E.g. DECODE_SIMULATE initialise a struct
133  * decode_simulate by initialising four decode_item objects like this...
134  *
135  *      {.bits = _type},
136  *      {.bits = _mask},
137  *      {.bits = _value},
138  *      {.action = _handler},
139  *
140  * Initialising a specified member of the union means that the compiler
141  * will produce a warning if the argument is of an incorrect type.
142  *
143  * Below is a list of each of the macros used to initialise entries and a
144  * description of the action performed when that entry is matched to an
145  * instruction. A match is found when (instruction & mask) == value.
146  *
147  * DECODE_TABLE(mask, value, table)
148  *      Instruction decoding jumps to parsing the new sub-table 'table'.
149  *
150  * DECODE_CUSTOM(mask, value, decoder)
151  *      The value of 'decoder' is used as an index into the array of
152  *      action functions, and the retrieved decoder function is invoked
153  *      to complete decoding of the instruction.
154  *
155  * DECODE_SIMULATE(mask, value, handler)
156  *      The probes instruction handler is set to the value found by
157  *      indexing into the action array using the value of 'handler'. This
158  *      will be used to simulate the instruction when the probe is hit.
159  *      Decoding returns with INSN_GOOD_NO_SLOT.
160  *
161  * DECODE_EMULATE(mask, value, handler)
162  *      The probes instruction handler is set to the value found by
163  *      indexing into the action array using the value of 'handler'. This
164  *      will be used to emulate the instruction when the probe is hit. The
165  *      modified instruction (see below) is placed in the probes instruction
166  *      slot so it may be called by the emulation code. Decoding returns
167  *      with INSN_GOOD.
168  *
169  * DECODE_REJECT(mask, value)
170  *      Instruction decoding fails with INSN_REJECTED
171  *
172  * DECODE_OR(mask, value)
173  *      This allows the mask/value test of multiple table entries to be
174  *      logically ORed. Once an 'or' entry is matched the decoding action to
175  *      be performed is that of the next entry which isn't an 'or'. E.g.
176  *
177  *              DECODE_OR       (mask1, value1)
178  *              DECODE_OR       (mask2, value2)
179  *              DECODE_SIMULATE (mask3, value3, simulation_handler)
180  *
181  *      This means that if any of the three mask/value pairs match the
182  *      instruction being decoded, then 'simulation_handler' will be used
183  *      for it.
184  *
185  * Both the SIMULATE and EMULATE macros have a second form which take an
186  * additional 'regs' argument.
187  *
188  *      DECODE_SIMULATEX(mask, value, handler, regs)
189  *      DECODE_EMULATEX (mask, value, handler, regs)
190  *
191  * These are used to specify what kind of CPU register is encoded in each of the
192  * least significant 5 nibbles of the instruction being decoded. The regs value
193  * is specified using the REGS macro, this takes any of the REG_TYPE_* values
194  * from enum decode_reg_type as arguments; only the '*' part of the name is
195  * given. E.g.
196  *
197  *      REGS(0, ANY, NOPC, 0, ANY)
198  *
199  * This indicates an instruction is encoded like:
200  *
201  *      bits 19..16     ignore
202  *      bits 15..12     any register allowed here
203  *      bits 11.. 8     any register except PC allowed here
204  *      bits  7.. 4     ignore
205  *      bits  3.. 0     any register allowed here
206  *
207  * This register specification is checked after a decode table entry is found to
208  * match an instruction (through the mask/value test). Any invalid register then
209  * found in the instruction will cause decoding to fail with INSN_REJECTED. In
210  * the above example this would happen if bits 11..8 of the instruction were
211  * 1111, indicating R15 or PC.
212  *
213  * As well as checking for legal combinations of registers, this data is also
214  * used to modify the registers encoded in the instructions so that an
215  * emulation routines can use it. (See decode_regs() and INSN_NEW_BITS.)
216  *
217  * Here is a real example which matches ARM instructions of the form
218  * "AND <Rd>,<Rn>,<Rm>,<shift> <Rs>"
219  *
220  *      DECODE_EMULATEX (0x0e000090, 0x00000010, PROBES_DATA_PROCESSING_REG,
221  *                                               REGS(ANY, ANY, NOPC, 0, ANY)),
222  *                                                    ^    ^    ^        ^
223  *                                                    Rn   Rd   Rs       Rm
224  *
225  * Decoding the instruction "AND R4, R5, R6, ASL R15" will be rejected because
226  * Rs == R15
227  *
228  * Decoding the instruction "AND R4, R5, R6, ASL R7" will be accepted and the
229  * instruction will be modified to "AND R0, R2, R3, ASL R1" and then placed into
230  * the kprobes instruction slot. This can then be called later by the handler
231  * function emulate_rd12rn16rm0rs8_rwflags (a pointer to which is retrieved from
232  * the indicated slot in the action array), in order to simulate the instruction.
233  */
234
235 enum decode_type {
236         DECODE_TYPE_END,
237         DECODE_TYPE_TABLE,
238         DECODE_TYPE_CUSTOM,
239         DECODE_TYPE_SIMULATE,
240         DECODE_TYPE_EMULATE,
241         DECODE_TYPE_OR,
242         DECODE_TYPE_REJECT,
243         NUM_DECODE_TYPES /* Must be last enum */
244 };
245
246 #define DECODE_TYPE_BITS        4
247 #define DECODE_TYPE_MASK        ((1 << DECODE_TYPE_BITS) - 1)
248
249 enum decode_reg_type {
250         REG_TYPE_NONE = 0, /* Not a register, ignore */
251         REG_TYPE_ANY,      /* Any register allowed */
252         REG_TYPE_SAMEAS16, /* Register should be same as that at bits 19..16 */
253         REG_TYPE_SP,       /* Register must be SP */
254         REG_TYPE_PC,       /* Register must be PC */
255         REG_TYPE_NOSP,     /* Register must not be SP */
256         REG_TYPE_NOSPPC,   /* Register must not be SP or PC */
257         REG_TYPE_NOPC,     /* Register must not be PC */
258         REG_TYPE_NOPCWB,   /* No PC if load/store write-back flag also set */
259
260         /* The following types are used when the encoding for PC indicates
261          * another instruction form. This distiction only matters for test
262          * case coverage checks.
263          */
264         REG_TYPE_NOPCX,    /* Register must not be PC */
265         REG_TYPE_NOSPPCX,  /* Register must not be SP or PC */
266
267         /* Alias to allow '0' arg to be used in REGS macro. */
268         REG_TYPE_0 = REG_TYPE_NONE
269 };
270
271 #define REGS(r16, r12, r8, r4, r0)      \
272         (((REG_TYPE_##r16) << 16) +     \
273         ((REG_TYPE_##r12) << 12) +      \
274         ((REG_TYPE_##r8) << 8) +        \
275         ((REG_TYPE_##r4) << 4) +        \
276         (REG_TYPE_##r0))
277
278 union decode_item {
279         u32                     bits;
280         const union decode_item *table;
281         int                     action;
282 };
283
284 struct decode_header;
285 typedef enum probes_insn (probes_custom_decode_t)(probes_opcode_t,
286                                                   struct arch_probes_insn *,
287                                                   const struct decode_header *);
288
289 union decode_action {
290         probes_insn_handler_t   *handler;
291         probes_custom_decode_t  *decoder;
292 };
293
294 typedef enum probes_insn (probes_check_t)(probes_opcode_t,
295                                            struct arch_probes_insn *,
296                                            const struct decode_header *);
297
298 struct decode_checker {
299         probes_check_t  *checker;
300 };
301
302 #define DECODE_END                      \
303         {.bits = DECODE_TYPE_END}
304
305
306 struct decode_header {
307         union decode_item       type_regs;
308         union decode_item       mask;
309         union decode_item       value;
310 };
311
312 #define DECODE_HEADER(_type, _mask, _value, _regs)              \
313         {.bits = (_type) | ((_regs) << DECODE_TYPE_BITS)},      \
314         {.bits = (_mask)},                                      \
315         {.bits = (_value)}
316
317
318 struct decode_table {
319         struct decode_header    header;
320         union decode_item       table;
321 };
322
323 #define DECODE_TABLE(_mask, _value, _table)                     \
324         DECODE_HEADER(DECODE_TYPE_TABLE, _mask, _value, 0),     \
325         {.table = (_table)}
326
327
328 struct decode_custom {
329         struct decode_header    header;
330         union decode_item       decoder;
331 };
332
333 #define DECODE_CUSTOM(_mask, _value, _decoder)                  \
334         DECODE_HEADER(DECODE_TYPE_CUSTOM, _mask, _value, 0),    \
335         {.action = (_decoder)}
336
337
338 struct decode_simulate {
339         struct decode_header    header;
340         union decode_item       handler;
341 };
342
343 #define DECODE_SIMULATEX(_mask, _value, _handler, _regs)                \
344         DECODE_HEADER(DECODE_TYPE_SIMULATE, _mask, _value, _regs),      \
345         {.action = (_handler)}
346
347 #define DECODE_SIMULATE(_mask, _value, _handler)        \
348         DECODE_SIMULATEX(_mask, _value, _handler, 0)
349
350
351 struct decode_emulate {
352         struct decode_header    header;
353         union decode_item       handler;
354 };
355
356 #define DECODE_EMULATEX(_mask, _value, _handler, _regs)                 \
357         DECODE_HEADER(DECODE_TYPE_EMULATE, _mask, _value, _regs),       \
358         {.action = (_handler)}
359
360 #define DECODE_EMULATE(_mask, _value, _handler)         \
361         DECODE_EMULATEX(_mask, _value, _handler, 0)
362
363
364 struct decode_or {
365         struct decode_header    header;
366 };
367
368 #define DECODE_OR(_mask, _value)                                \
369         DECODE_HEADER(DECODE_TYPE_OR, _mask, _value, 0)
370
371 enum probes_insn {
372         INSN_REJECTED,
373         INSN_GOOD,
374         INSN_GOOD_NO_SLOT
375 };
376
377 struct decode_reject {
378         struct decode_header    header;
379 };
380
381 #define DECODE_REJECT(_mask, _value)                            \
382         DECODE_HEADER(DECODE_TYPE_REJECT, _mask, _value, 0)
383
384 probes_insn_handler_t probes_simulate_nop;
385 probes_insn_handler_t probes_emulate_none;
386
387 int __kprobes
388 probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
389                 const union decode_item *table, bool thumb, bool emulate,
390                 const union decode_action *actions,
391                 const struct decode_checker **checkers);
392
393 #endif