insn.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (C) 2013 Huawei Ltd.
  3. * Author: Jiang Liu <liuj97@gmail.com>
  4. *
  5. * Copyright (C) 2014 Zi Shen Lim <zlim.lnx@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __ASM_INSN_H
  20. #define __ASM_INSN_H
  21. #include <linux/types.h>
  22. /* A64 instructions are always 32 bits. */
  23. #define AARCH64_INSN_SIZE 4
  24. #ifndef __ASSEMBLY__
  25. /*
  26. * ARM Architecture Reference Manual for ARMv8 Profile-A, Issue A.a
  27. * Section C3.1 "A64 instruction index by encoding":
  28. * AArch64 main encoding table
  29. * Bit position
  30. * 28 27 26 25 Encoding Group
  31. * 0 0 - - Unallocated
  32. * 1 0 0 - Data processing, immediate
  33. * 1 0 1 - Branch, exception generation and system instructions
  34. * - 1 - 0 Loads and stores
  35. * - 1 0 1 Data processing - register
  36. * 0 1 1 1 Data processing - SIMD and floating point
  37. * 1 1 1 1 Data processing - SIMD and floating point
  38. * "-" means "don't care"
  39. */
  40. enum aarch64_insn_encoding_class {
  41. AARCH64_INSN_CLS_UNKNOWN, /* UNALLOCATED */
  42. AARCH64_INSN_CLS_DP_IMM, /* Data processing - immediate */
  43. AARCH64_INSN_CLS_DP_REG, /* Data processing - register */
  44. AARCH64_INSN_CLS_DP_FPSIMD, /* Data processing - SIMD and FP */
  45. AARCH64_INSN_CLS_LDST, /* Loads and stores */
  46. AARCH64_INSN_CLS_BR_SYS, /* Branch, exception generation and
  47. * system instructions */
  48. };
  49. enum aarch64_insn_hint_op {
  50. AARCH64_INSN_HINT_NOP = 0x0 << 5,
  51. AARCH64_INSN_HINT_YIELD = 0x1 << 5,
  52. AARCH64_INSN_HINT_WFE = 0x2 << 5,
  53. AARCH64_INSN_HINT_WFI = 0x3 << 5,
  54. AARCH64_INSN_HINT_SEV = 0x4 << 5,
  55. AARCH64_INSN_HINT_SEVL = 0x5 << 5,
  56. };
  57. enum aarch64_insn_imm_type {
  58. AARCH64_INSN_IMM_ADR,
  59. AARCH64_INSN_IMM_26,
  60. AARCH64_INSN_IMM_19,
  61. AARCH64_INSN_IMM_16,
  62. AARCH64_INSN_IMM_14,
  63. AARCH64_INSN_IMM_12,
  64. AARCH64_INSN_IMM_9,
  65. AARCH64_INSN_IMM_7,
  66. AARCH64_INSN_IMM_6,
  67. AARCH64_INSN_IMM_S,
  68. AARCH64_INSN_IMM_R,
  69. AARCH64_INSN_IMM_MAX
  70. };
  71. enum aarch64_insn_register_type {
  72. AARCH64_INSN_REGTYPE_RT,
  73. AARCH64_INSN_REGTYPE_RN,
  74. AARCH64_INSN_REGTYPE_RT2,
  75. AARCH64_INSN_REGTYPE_RM,
  76. AARCH64_INSN_REGTYPE_RD,
  77. AARCH64_INSN_REGTYPE_RA,
  78. };
  79. enum aarch64_insn_register {
  80. AARCH64_INSN_REG_0 = 0,
  81. AARCH64_INSN_REG_1 = 1,
  82. AARCH64_INSN_REG_2 = 2,
  83. AARCH64_INSN_REG_3 = 3,
  84. AARCH64_INSN_REG_4 = 4,
  85. AARCH64_INSN_REG_5 = 5,
  86. AARCH64_INSN_REG_6 = 6,
  87. AARCH64_INSN_REG_7 = 7,
  88. AARCH64_INSN_REG_8 = 8,
  89. AARCH64_INSN_REG_9 = 9,
  90. AARCH64_INSN_REG_10 = 10,
  91. AARCH64_INSN_REG_11 = 11,
  92. AARCH64_INSN_REG_12 = 12,
  93. AARCH64_INSN_REG_13 = 13,
  94. AARCH64_INSN_REG_14 = 14,
  95. AARCH64_INSN_REG_15 = 15,
  96. AARCH64_INSN_REG_16 = 16,
  97. AARCH64_INSN_REG_17 = 17,
  98. AARCH64_INSN_REG_18 = 18,
  99. AARCH64_INSN_REG_19 = 19,
  100. AARCH64_INSN_REG_20 = 20,
  101. AARCH64_INSN_REG_21 = 21,
  102. AARCH64_INSN_REG_22 = 22,
  103. AARCH64_INSN_REG_23 = 23,
  104. AARCH64_INSN_REG_24 = 24,
  105. AARCH64_INSN_REG_25 = 25,
  106. AARCH64_INSN_REG_26 = 26,
  107. AARCH64_INSN_REG_27 = 27,
  108. AARCH64_INSN_REG_28 = 28,
  109. AARCH64_INSN_REG_29 = 29,
  110. AARCH64_INSN_REG_FP = 29, /* Frame pointer */
  111. AARCH64_INSN_REG_30 = 30,
  112. AARCH64_INSN_REG_LR = 30, /* Link register */
  113. AARCH64_INSN_REG_ZR = 31, /* Zero: as source register */
  114. AARCH64_INSN_REG_SP = 31 /* Stack pointer: as load/store base reg */
  115. };
  116. enum aarch64_insn_variant {
  117. AARCH64_INSN_VARIANT_32BIT,
  118. AARCH64_INSN_VARIANT_64BIT
  119. };
  120. enum aarch64_insn_condition {
  121. AARCH64_INSN_COND_EQ = 0x0, /* == */
  122. AARCH64_INSN_COND_NE = 0x1, /* != */
  123. AARCH64_INSN_COND_CS = 0x2, /* unsigned >= */
  124. AARCH64_INSN_COND_CC = 0x3, /* unsigned < */
  125. AARCH64_INSN_COND_MI = 0x4, /* < 0 */
  126. AARCH64_INSN_COND_PL = 0x5, /* >= 0 */
  127. AARCH64_INSN_COND_VS = 0x6, /* overflow */
  128. AARCH64_INSN_COND_VC = 0x7, /* no overflow */
  129. AARCH64_INSN_COND_HI = 0x8, /* unsigned > */
  130. AARCH64_INSN_COND_LS = 0x9, /* unsigned <= */
  131. AARCH64_INSN_COND_GE = 0xa, /* signed >= */
  132. AARCH64_INSN_COND_LT = 0xb, /* signed < */
  133. AARCH64_INSN_COND_GT = 0xc, /* signed > */
  134. AARCH64_INSN_COND_LE = 0xd, /* signed <= */
  135. AARCH64_INSN_COND_AL = 0xe, /* always */
  136. };
  137. enum aarch64_insn_branch_type {
  138. AARCH64_INSN_BRANCH_NOLINK,
  139. AARCH64_INSN_BRANCH_LINK,
  140. AARCH64_INSN_BRANCH_RETURN,
  141. AARCH64_INSN_BRANCH_COMP_ZERO,
  142. AARCH64_INSN_BRANCH_COMP_NONZERO,
  143. };
  144. enum aarch64_insn_size_type {
  145. AARCH64_INSN_SIZE_8,
  146. AARCH64_INSN_SIZE_16,
  147. AARCH64_INSN_SIZE_32,
  148. AARCH64_INSN_SIZE_64,
  149. };
  150. enum aarch64_insn_ldst_type {
  151. AARCH64_INSN_LDST_LOAD_REG_OFFSET,
  152. AARCH64_INSN_LDST_STORE_REG_OFFSET,
  153. AARCH64_INSN_LDST_LOAD_PAIR_PRE_INDEX,
  154. AARCH64_INSN_LDST_STORE_PAIR_PRE_INDEX,
  155. AARCH64_INSN_LDST_LOAD_PAIR_POST_INDEX,
  156. AARCH64_INSN_LDST_STORE_PAIR_POST_INDEX,
  157. };
  158. enum aarch64_insn_adsb_type {
  159. AARCH64_INSN_ADSB_ADD,
  160. AARCH64_INSN_ADSB_SUB,
  161. AARCH64_INSN_ADSB_ADD_SETFLAGS,
  162. AARCH64_INSN_ADSB_SUB_SETFLAGS
  163. };
  164. enum aarch64_insn_movewide_type {
  165. AARCH64_INSN_MOVEWIDE_ZERO,
  166. AARCH64_INSN_MOVEWIDE_KEEP,
  167. AARCH64_INSN_MOVEWIDE_INVERSE
  168. };
  169. enum aarch64_insn_bitfield_type {
  170. AARCH64_INSN_BITFIELD_MOVE,
  171. AARCH64_INSN_BITFIELD_MOVE_UNSIGNED,
  172. AARCH64_INSN_BITFIELD_MOVE_SIGNED
  173. };
  174. enum aarch64_insn_data1_type {
  175. AARCH64_INSN_DATA1_REVERSE_16,
  176. AARCH64_INSN_DATA1_REVERSE_32,
  177. AARCH64_INSN_DATA1_REVERSE_64,
  178. };
  179. enum aarch64_insn_data2_type {
  180. AARCH64_INSN_DATA2_UDIV,
  181. AARCH64_INSN_DATA2_SDIV,
  182. AARCH64_INSN_DATA2_LSLV,
  183. AARCH64_INSN_DATA2_LSRV,
  184. AARCH64_INSN_DATA2_ASRV,
  185. AARCH64_INSN_DATA2_RORV,
  186. };
  187. enum aarch64_insn_data3_type {
  188. AARCH64_INSN_DATA3_MADD,
  189. AARCH64_INSN_DATA3_MSUB,
  190. };
  191. enum aarch64_insn_logic_type {
  192. AARCH64_INSN_LOGIC_AND,
  193. AARCH64_INSN_LOGIC_BIC,
  194. AARCH64_INSN_LOGIC_ORR,
  195. AARCH64_INSN_LOGIC_ORN,
  196. AARCH64_INSN_LOGIC_EOR,
  197. AARCH64_INSN_LOGIC_EON,
  198. AARCH64_INSN_LOGIC_AND_SETFLAGS,
  199. AARCH64_INSN_LOGIC_BIC_SETFLAGS
  200. };
  201. #define __AARCH64_INSN_FUNCS(abbr, mask, val) \
  202. static __always_inline bool aarch64_insn_is_##abbr(u32 code) \
  203. { return (code & (mask)) == (val); } \
  204. static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \
  205. { return (val); }
  206. __AARCH64_INSN_FUNCS(str_reg, 0x3FE0EC00, 0x38206800)
  207. __AARCH64_INSN_FUNCS(ldr_reg, 0x3FE0EC00, 0x38606800)
  208. __AARCH64_INSN_FUNCS(stp_post, 0x7FC00000, 0x28800000)
  209. __AARCH64_INSN_FUNCS(ldp_post, 0x7FC00000, 0x28C00000)
  210. __AARCH64_INSN_FUNCS(stp_pre, 0x7FC00000, 0x29800000)
  211. __AARCH64_INSN_FUNCS(ldp_pre, 0x7FC00000, 0x29C00000)
  212. __AARCH64_INSN_FUNCS(add_imm, 0x7F000000, 0x11000000)
  213. __AARCH64_INSN_FUNCS(adds_imm, 0x7F000000, 0x31000000)
  214. __AARCH64_INSN_FUNCS(sub_imm, 0x7F000000, 0x51000000)
  215. __AARCH64_INSN_FUNCS(subs_imm, 0x7F000000, 0x71000000)
  216. __AARCH64_INSN_FUNCS(movn, 0x7F800000, 0x12800000)
  217. __AARCH64_INSN_FUNCS(sbfm, 0x7F800000, 0x13000000)
  218. __AARCH64_INSN_FUNCS(bfm, 0x7F800000, 0x33000000)
  219. __AARCH64_INSN_FUNCS(movz, 0x7F800000, 0x52800000)
  220. __AARCH64_INSN_FUNCS(ubfm, 0x7F800000, 0x53000000)
  221. __AARCH64_INSN_FUNCS(movk, 0x7F800000, 0x72800000)
  222. __AARCH64_INSN_FUNCS(add, 0x7F200000, 0x0B000000)
  223. __AARCH64_INSN_FUNCS(adds, 0x7F200000, 0x2B000000)
  224. __AARCH64_INSN_FUNCS(sub, 0x7F200000, 0x4B000000)
  225. __AARCH64_INSN_FUNCS(subs, 0x7F200000, 0x6B000000)
  226. __AARCH64_INSN_FUNCS(madd, 0x7FE08000, 0x1B000000)
  227. __AARCH64_INSN_FUNCS(msub, 0x7FE08000, 0x1B008000)
  228. __AARCH64_INSN_FUNCS(udiv, 0x7FE0FC00, 0x1AC00800)
  229. __AARCH64_INSN_FUNCS(sdiv, 0x7FE0FC00, 0x1AC00C00)
  230. __AARCH64_INSN_FUNCS(lslv, 0x7FE0FC00, 0x1AC02000)
  231. __AARCH64_INSN_FUNCS(lsrv, 0x7FE0FC00, 0x1AC02400)
  232. __AARCH64_INSN_FUNCS(asrv, 0x7FE0FC00, 0x1AC02800)
  233. __AARCH64_INSN_FUNCS(rorv, 0x7FE0FC00, 0x1AC02C00)
  234. __AARCH64_INSN_FUNCS(rev16, 0x7FFFFC00, 0x5AC00400)
  235. __AARCH64_INSN_FUNCS(rev32, 0x7FFFFC00, 0x5AC00800)
  236. __AARCH64_INSN_FUNCS(rev64, 0x7FFFFC00, 0x5AC00C00)
  237. __AARCH64_INSN_FUNCS(and, 0x7F200000, 0x0A000000)
  238. __AARCH64_INSN_FUNCS(bic, 0x7F200000, 0x0A200000)
  239. __AARCH64_INSN_FUNCS(orr, 0x7F200000, 0x2A000000)
  240. __AARCH64_INSN_FUNCS(orn, 0x7F200000, 0x2A200000)
  241. __AARCH64_INSN_FUNCS(eor, 0x7F200000, 0x4A000000)
  242. __AARCH64_INSN_FUNCS(eon, 0x7F200000, 0x4A200000)
  243. __AARCH64_INSN_FUNCS(ands, 0x7F200000, 0x6A000000)
  244. __AARCH64_INSN_FUNCS(bics, 0x7F200000, 0x6A200000)
  245. __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000)
  246. __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)
  247. __AARCH64_INSN_FUNCS(cbz, 0x7F000000, 0x34000000)
  248. __AARCH64_INSN_FUNCS(cbnz, 0x7F000000, 0x35000000)
  249. __AARCH64_INSN_FUNCS(tbz, 0x7F000000, 0x36000000)
  250. __AARCH64_INSN_FUNCS(tbnz, 0x7F000000, 0x37000000)
  251. __AARCH64_INSN_FUNCS(bcond, 0xFF000010, 0x54000000)
  252. __AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001)
  253. __AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002)
  254. __AARCH64_INSN_FUNCS(smc, 0xFFE0001F, 0xD4000003)
  255. __AARCH64_INSN_FUNCS(brk, 0xFFE0001F, 0xD4200000)
  256. __AARCH64_INSN_FUNCS(hint, 0xFFFFF01F, 0xD503201F)
  257. __AARCH64_INSN_FUNCS(br, 0xFFFFFC1F, 0xD61F0000)
  258. __AARCH64_INSN_FUNCS(blr, 0xFFFFFC1F, 0xD63F0000)
  259. __AARCH64_INSN_FUNCS(ret, 0xFFFFFC1F, 0xD65F0000)
  260. #undef __AARCH64_INSN_FUNCS
  261. bool aarch64_insn_is_nop(u32 insn);
  262. bool aarch64_insn_is_branch_imm(u32 insn);
  263. int aarch64_insn_read(void *addr, u32 *insnp);
  264. int aarch64_insn_write(void *addr, u32 insn);
  265. enum aarch64_insn_encoding_class aarch64_get_insn_class(u32 insn);
  266. u64 aarch64_insn_decode_immediate(enum aarch64_insn_imm_type type, u32 insn);
  267. u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,
  268. u32 insn, u64 imm);
  269. u32 aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,
  270. enum aarch64_insn_branch_type type);
  271. u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr,
  272. enum aarch64_insn_register reg,
  273. enum aarch64_insn_variant variant,
  274. enum aarch64_insn_branch_type type);
  275. u32 aarch64_insn_gen_cond_branch_imm(unsigned long pc, unsigned long addr,
  276. enum aarch64_insn_condition cond);
  277. u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op);
  278. u32 aarch64_insn_gen_nop(void);
  279. u32 aarch64_insn_gen_branch_reg(enum aarch64_insn_register reg,
  280. enum aarch64_insn_branch_type type);
  281. u32 aarch64_insn_gen_load_store_reg(enum aarch64_insn_register reg,
  282. enum aarch64_insn_register base,
  283. enum aarch64_insn_register offset,
  284. enum aarch64_insn_size_type size,
  285. enum aarch64_insn_ldst_type type);
  286. u32 aarch64_insn_gen_load_store_pair(enum aarch64_insn_register reg1,
  287. enum aarch64_insn_register reg2,
  288. enum aarch64_insn_register base,
  289. int offset,
  290. enum aarch64_insn_variant variant,
  291. enum aarch64_insn_ldst_type type);
  292. u32 aarch64_insn_gen_add_sub_imm(enum aarch64_insn_register dst,
  293. enum aarch64_insn_register src,
  294. int imm, enum aarch64_insn_variant variant,
  295. enum aarch64_insn_adsb_type type);
  296. u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst,
  297. enum aarch64_insn_register src,
  298. int immr, int imms,
  299. enum aarch64_insn_variant variant,
  300. enum aarch64_insn_bitfield_type type);
  301. u32 aarch64_insn_gen_movewide(enum aarch64_insn_register dst,
  302. int imm, int shift,
  303. enum aarch64_insn_variant variant,
  304. enum aarch64_insn_movewide_type type);
  305. u32 aarch64_insn_gen_add_sub_shifted_reg(enum aarch64_insn_register dst,
  306. enum aarch64_insn_register src,
  307. enum aarch64_insn_register reg,
  308. int shift,
  309. enum aarch64_insn_variant variant,
  310. enum aarch64_insn_adsb_type type);
  311. u32 aarch64_insn_gen_data1(enum aarch64_insn_register dst,
  312. enum aarch64_insn_register src,
  313. enum aarch64_insn_variant variant,
  314. enum aarch64_insn_data1_type type);
  315. u32 aarch64_insn_gen_data2(enum aarch64_insn_register dst,
  316. enum aarch64_insn_register src,
  317. enum aarch64_insn_register reg,
  318. enum aarch64_insn_variant variant,
  319. enum aarch64_insn_data2_type type);
  320. u32 aarch64_insn_gen_data3(enum aarch64_insn_register dst,
  321. enum aarch64_insn_register src,
  322. enum aarch64_insn_register reg1,
  323. enum aarch64_insn_register reg2,
  324. enum aarch64_insn_variant variant,
  325. enum aarch64_insn_data3_type type);
  326. u32 aarch64_insn_gen_logical_shifted_reg(enum aarch64_insn_register dst,
  327. enum aarch64_insn_register src,
  328. enum aarch64_insn_register reg,
  329. int shift,
  330. enum aarch64_insn_variant variant,
  331. enum aarch64_insn_logic_type type);
  332. s32 aarch64_get_branch_offset(u32 insn);
  333. u32 aarch64_set_branch_offset(u32 insn, s32 offset);
  334. bool aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn);
  335. int aarch64_insn_patch_text_nosync(void *addr, u32 insn);
  336. int aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt);
  337. int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt);
  338. bool aarch32_insn_is_wide(u32 insn);
  339. #define A32_RN_OFFSET 16
  340. #define A32_RT_OFFSET 12
  341. #define A32_RT2_OFFSET 0
  342. u32 aarch32_insn_extract_reg_num(u32 insn, int offset);
  343. u32 aarch32_insn_mcr_extract_opc2(u32 insn);
  344. u32 aarch32_insn_mcr_extract_crm(u32 insn);
  345. #endif /* __ASSEMBLY__ */
  346. #endif /* __ASM_INSN_H */