preliminaryDataProcessing.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * arch/arm/kernel/kprobes.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. #ifndef _ARM_KERNEL_KPROBES_H
  19. #define _ARM_KERNEL_KPROBES_H
  20. /*
  21. * These undefined instructions must be unique and
  22. * reserved solely for kprobes' use.
  23. */
  24. #define KPROBE_ARM_BREAKPOINT_INSTRUCTION 0x07f001f8
  25. #define KPROBE_THUMB16_BREAKPOINT_INSTRUCTION 0xde18
  26. #define KPROBE_THUMB32_BREAKPOINT_INSTRUCTION 0xf7f0a018
  27. enum kprobe_insn {
  28. INSN_REJECTED,
  29. INSN_GOOD,
  30. INSN_GOOD_NO_SLOT
  31. };
  32. typedef enum kprobe_insn (kprobe_decode_insn_t)(kprobe_opcode_t,
  33. struct arch_specific_insn *);
  34. #ifdef CONFIG_THUMB2_KERNEL
  35. enum kprobe_insn thumb16_kprobe_decode_insn(kprobe_opcode_t,
  36. struct arch_specific_insn *);
  37. enum kprobe_insn thumb32_kprobe_decode_insn(kprobe_opcode_t,
  38. struct arch_specific_insn *);
  39. #else /* !CONFIG_THUMB2_KERNEL */
  40. enum kprobe_insn arm_kprobe_decode_insn(kprobe_opcode_t,
  41. struct arch_specific_insn *);
  42. #endif
  43. void __init arm_kprobe_decode_init(void);
  44. extern kprobe_check_cc * const kprobe_condition_checks[16];
  45. #if __LINUX_ARM_ARCH__ >= 7
  46. /* str_pc_offset is architecturally defined from ARMv7 onwards */
  47. #define str_pc_offset 8
  48. #define find_str_pc_offset()
  49. #else /* __LINUX_ARM_ARCH__ < 7 */
  50. /* We need a run-time check to determine str_pc_offset */
  51. extern int str_pc_offset;
  52. void __init find_str_pc_offset(void);
  53. #endif
  54. /*
  55. * Update ITSTATE after normal execution of an IT block instruction.
  56. *
  57. * The 8 IT state bits are split into two parts in CPSR:
  58. * ITSTATE<1:0> are in CPSR<26:25>
  59. * ITSTATE<7:2> are in CPSR<15:10>
  60. */
  61. static inline unsigned long it_advance(unsigned long cpsr)
  62. {
  63. if ((cpsr & 0x06000400) == 0) {
  64. /* ITSTATE<2:0> == 0 means end of IT block, so clear IT state */
  65. cpsr &= ~PSR_IT_MASK;
  66. } else {
  67. /* We need to shift left ITSTATE<4:0> */
  68. const unsigned long mask = 0x06001c00; /* Mask ITSTATE<4:0> */
  69. unsigned long it = cpsr & mask;
  70. it <<= 1;
  71. it |= it >> (27 - 10); /* Carry ITSTATE<2> to correct place */
  72. it &= mask;
  73. cpsr &= ~mask;
  74. cpsr |= it;
  75. }
  76. return cpsr;
  77. }
  78. static inline void __kprobes bx_write_pc(long pcv, struct pt_regs *regs)
  79. {
  80. long cpsr = regs->ARM_cpsr;
  81. if (pcv & 0x1) {
  82. cpsr |= PSR_T_BIT;
  83. pcv &= ~0x1;
  84. } else {
  85. cpsr &= ~PSR_T_BIT;
  86. pcv &= ~0x2; /* Avoid UNPREDICTABLE address allignment */
  87. }
  88. regs->ARM_cpsr = cpsr;
  89. regs->ARM_pc = pcv;
  90. }
  91. #if __LINUX_ARM_ARCH__ >= 6
  92. /* Kernels built for >= ARMv6 should never run on <= ARMv5 hardware, so... */
  93. #define load_write_pc_interworks true
  94. #define test_load_write_pc_interworking()
  95. #else /* __LINUX_ARM_ARCH__ < 6 */
  96. /* We need run-time testing to determine if load_write_pc() should interwork. */
  97. extern bool load_write_pc_interworks;
  98. void __init test_load_write_pc_interworking(void);
  99. #endif
  100. static inline void __kprobes load_write_pc(long pcv, struct pt_regs *regs)
  101. {
  102. if (load_write_pc_interworks)
  103. bx_write_pc(pcv, regs);
  104. else
  105. regs->ARM_pc = pcv;
  106. }
  107. #if __LINUX_ARM_ARCH__ >= 7
  108. #define alu_write_pc_interworks true
  109. #define test_alu_write_pc_interworking()
  110. #elif __LINUX_ARM_ARCH__ <= 5
  111. /* Kernels built for <= ARMv5 should never run on >= ARMv6 hardware, so... */
  112. #define alu_write_pc_interworks false
  113. #define test_alu_write_pc_interworking()
  114. #else /* __LINUX_ARM_ARCH__ == 6 */
  115. /* We could be an ARMv6 binary on ARMv7 hardware so we need a run-time check. */
  116. extern bool alu_write_pc_interworks;
  117. void __init test_alu_write_pc_interworking(void);
  118. #endif /* __LINUX_ARM_ARCH__ == 6 */
  119. static inline void __kprobes alu_write_pc(long pcv, struct pt_regs *regs)
  120. {
  121. if (alu_write_pc_interworks)
  122. bx_write_pc(pcv, regs);
  123. else
  124. regs->ARM_pc = pcv;
  125. }
  126. void __kprobes kprobe_simulate_nop(struct kprobe *p, struct pt_regs *regs);
  127. void __kprobes kprobe_emulate_none(struct kprobe *p, struct pt_regs *regs);
  128. enum kprobe_insn __kprobes
  129. kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_specific_insn *asi);
  130. /*
  131. * Test if load/store instructions writeback the address register.
  132. * if P (bit 24) == 0 or W (bit 21) == 1
  133. */
  134. #define is_writeback(insn) ((insn ^ 0x01000000) & 0x01200000)
  135. /*
  136. * The following definitions and macros are used to build instruction
  137. * decoding tables for use by kprobe_decode_insn.
  138. *
  139. * These tables are a concatenation of entries each of which consist of one of
  140. * the decode_* structs. All of the fields in every type of decode structure
  141. * are of the union type decode_item, therefore the entire decode table can be
  142. * viewed as an array of these and declared like:
  143. *
  144. * static const union decode_item table_name[] = {};
  145. *
  146. * In order to construct each entry in the table, macros are used to
  147. * initialise a number of sequential decode_item values in a layout which
  148. * matches the relevant struct. E.g. DECODE_SIMULATE initialise a struct
  149. * decode_simulate by initialising four decode_item objects like this...
  150. *
  151. * {.bits = _type},
  152. * {.bits = _mask},
  153. * {.bits = _value},
  154. * {.handler = _handler},
  155. *
  156. * Initialising a specified member of the union means that the compiler
  157. * will produce a warning if the argument is of an incorrect type.
  158. *
  159. * Below is a list of each of the macros used to initialise entries and a
  160. * description of the action performed when that entry is matched to an
  161. * instruction. A match is found when (instruction & mask) == value.
  162. *
  163. * DECODE_TABLE(mask, value, table)
  164. * Instruction decoding jumps to parsing the new sub-table 'table'.
  165. *
  166. * DECODE_CUSTOM(mask, value, decoder)
  167. * The custom function 'decoder' is called to the complete decoding
  168. * of an instruction.
  169. *
  170. * DECODE_SIMULATE(mask, value, handler)
  171. * Set the probes instruction handler to 'handler', this will be used
  172. * to simulate the instruction when the probe is hit. Decoding returns
  173. * with INSN_GOOD_NO_SLOT.
  174. *
  175. * DECODE_EMULATE(mask, value, handler)
  176. * Set the probes instruction handler to 'handler', this will be used
  177. * to emulate the instruction when the probe is hit. The modified
  178. * instruction (see below) is placed in the probes instruction slot so it
  179. * may be called by the emulation code. Decoding returns with INSN_GOOD.
  180. *
  181. * DECODE_REJECT(mask, value)
  182. * Instruction decoding fails with INSN_REJECTED
  183. *
  184. * DECODE_OR(mask, value)
  185. * This allows the mask/value test of multiple table entries to be
  186. * logically ORed. Once an 'or' entry is matched the decoding action to
  187. * be performed is that of the next entry which isn't an 'or'. E.g.
  188. *
  189. * DECODE_OR (mask1, value1)
  190. * DECODE_OR (mask2, value2)
  191. * DECODE_SIMULATE (mask3, value3, simulation_handler)
  192. *
  193. * This means that if any of the three mask/value pairs match the
  194. * instruction being decoded, then 'simulation_handler' will be used
  195. * for it.
  196. *
  197. * Both the SIMULATE and EMULATE macros have a second form which take an
  198. * additional 'regs' argument.
  199. *
  200. * DECODE_SIMULATEX(mask, value, handler, regs)
  201. * DECODE_EMULATEX (mask, value, handler, regs)
  202. *
  203. * These are used to specify what kind of CPU register is encoded in each of the
  204. * least significant 5 nibbles of the instruction being decoded. The regs value
  205. * is specified using the REGS macro, this takes any of the REG_TYPE_* values
  206. * from enum decode_reg_type as arguments; only the '*' part of the name is
  207. * given. E.g.
  208. *
  209. * REGS(0, ANY, NOPC, 0, ANY)
  210. *
  211. * This indicates an instruction is encoded like:
  212. *
  213. * bits 19..16 ignore
  214. * bits 15..12 any register allowed here
  215. * bits 11.. 8 any register except PC allowed here
  216. * bits 7.. 4 ignore
  217. * bits 3.. 0 any register allowed here
  218. *
  219. * This register specification is checked after a decode table entry is found to
  220. * match an instruction (through the mask/value test). Any invalid register then
  221. * found in the instruction will cause decoding to fail with INSN_REJECTED. In
  222. * the above example this would happen if bits 11..8 of the instruction were
  223. * 1111, indicating R15 or PC.
  224. *
  225. * As well as checking for legal combinations of registers, this data is also