bpf_jit_32.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Just-In-Time compiler for BPF filters on 32bit ARM
  3. *
  4. * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; version 2 of the License.
  9. */
  10. #ifndef PFILTER_OPCODES_ARM_H
  11. #define PFILTER_OPCODES_ARM_H
  12. #define ARM_R0 0
  13. #define ARM_R1 1
  14. #define ARM_R2 2
  15. #define ARM_R3 3
  16. #define ARM_R4 4
  17. #define ARM_R5 5
  18. #define ARM_R6 6
  19. #define ARM_R7 7
  20. #define ARM_R8 8
  21. #define ARM_R9 9
  22. #define ARM_R10 10
  23. #define ARM_FP 11
  24. #define ARM_IP 12
  25. #define ARM_SP 13
  26. #define ARM_LR 14
  27. #define ARM_PC 15
  28. #define ARM_COND_EQ 0x0
  29. #define ARM_COND_NE 0x1
  30. #define ARM_COND_CS 0x2
  31. #define ARM_COND_HS ARM_COND_CS
  32. #define ARM_COND_CC 0x3
  33. #define ARM_COND_LO ARM_COND_CC
  34. #define ARM_COND_MI 0x4
  35. #define ARM_COND_PL 0x5
  36. #define ARM_COND_VS 0x6
  37. #define ARM_COND_VC 0x7
  38. #define ARM_COND_HI 0x8
  39. #define ARM_COND_LS 0x9
  40. #define ARM_COND_GE 0xa
  41. #define ARM_COND_LT 0xb
  42. #define ARM_COND_GT 0xc
  43. #define ARM_COND_LE 0xd
  44. #define ARM_COND_AL 0xe
  45. /* register shift types */
  46. #define SRTYPE_LSL 0
  47. #define SRTYPE_LSR 1
  48. #define SRTYPE_ASR 2
  49. #define SRTYPE_ROR 3
  50. #define ARM_INST_ADD_R 0x00800000
  51. #define ARM_INST_ADD_I 0x02800000
  52. #define ARM_INST_AND_R 0x00000000
  53. #define ARM_INST_AND_I 0x02000000
  54. #define ARM_INST_BIC_R 0x01c00000
  55. #define ARM_INST_BIC_I 0x03c00000
  56. #define ARM_INST_B 0x0a000000
  57. #define ARM_INST_BX 0x012FFF10
  58. #define ARM_INST_BLX_R 0x012fff30
  59. #define ARM_INST_CMP_R 0x01500000
  60. #define ARM_INST_CMP_I 0x03500000
  61. #define ARM_INST_EOR_R 0x00200000
  62. #define ARM_INST_EOR_I 0x02200000
  63. #define ARM_INST_LDRB_I 0x05d00000
  64. #define ARM_INST_LDRB_R 0x07d00000
  65. #define ARM_INST_LDRH_I 0x01d000b0
  66. #define ARM_INST_LDRH_R 0x019000b0
  67. #define ARM_INST_LDR_I 0x05900000
  68. #define ARM_INST_LDM 0x08900000
  69. #define ARM_INST_LSL_I 0x01a00000
  70. #define ARM_INST_LSL_R 0x01a00010
  71. #define ARM_INST_LSR_I 0x01a00020
  72. #define ARM_INST_LSR_R 0x01a00030
  73. #define ARM_INST_MOV_R 0x01a00000
  74. #define ARM_INST_MOV_I 0x03a00000
  75. #define ARM_INST_MOVW 0x03000000
  76. #define ARM_INST_MOVT 0x03400000
  77. #define ARM_INST_MUL 0x00000090
  78. #define ARM_INST_POP 0x08bd0000
  79. #define ARM_INST_PUSH 0x092d0000
  80. #define ARM_INST_ORR_R 0x01800000
  81. #define ARM_INST_ORR_I 0x03800000
  82. #define ARM_INST_REV 0x06bf0f30
  83. #define ARM_INST_REV16 0x06bf0fb0
  84. #define ARM_INST_RSB_I 0x02600000
  85. #define ARM_INST_SUB_R 0x00400000
  86. #define ARM_INST_SUB_I 0x02400000
  87. #define ARM_INST_STR_I 0x05800000
  88. #define ARM_INST_TST_R 0x01100000
  89. #define ARM_INST_TST_I 0x03100000
  90. #define ARM_INST_UDIV 0x0730f010
  91. #define ARM_INST_UMULL 0x00800090
  92. #define ARM_INST_MLS 0x00600090
  93. /*
  94. * Use a suitable undefined instruction to use for ARM/Thumb2 faulting.
  95. * We need to be careful not to conflict with those used by other modules
  96. * (BUG, kprobes, etc) and the register_undef_hook() system.
  97. *
  98. * The ARM architecture reference manual guarantees that the following
  99. * instruction space will produce an undefined instruction exception on
  100. * all CPUs:
  101. *
  102. * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx ARMv7-AR, section A5.4
  103. * Thumb: 1101 1110 xxxx xxxx ARMv7-M, section A5.2.6
  104. */
  105. #define ARM_INST_UDF 0xe7fddef1
  106. /* register */
  107. #define _AL3_R(op, rd, rn, rm) ((op ## _R) | (rd) << 12 | (rn) << 16 | (rm))
  108. /* immediate */
  109. #define _AL3_I(op, rd, rn, imm) ((op ## _I) | (rd) << 12 | (rn) << 16 | (imm))
  110. #define ARM_ADD_R(rd, rn, rm) _AL3_R(ARM_INST_ADD, rd, rn, rm)
  111. #define ARM_ADD_I(rd, rn, imm) _AL3_I(ARM_INST_ADD, rd, rn, imm)
  112. #define ARM_AND_R(rd, rn, rm) _AL3_R(ARM_INST_AND, rd, rn, rm)
  113. #define ARM_AND_I(rd, rn, imm) _AL3_I(ARM_INST_AND, rd, rn, imm)
  114. #define ARM_BIC_R(rd, rn, rm) _AL3_R(ARM_INST_BIC, rd, rn, rm)
  115. #define ARM_BIC_I(rd, rn, imm) _AL3_I(ARM_INST_BIC, rd, rn, imm)
  116. #define ARM_B(imm24) (ARM_INST_B | ((imm24) & 0xffffff))
  117. #define ARM_BX(rm) (ARM_INST_BX | (rm))
  118. #define ARM_BLX_R(rm) (ARM_INST_BLX_R | (rm))
  119. #define ARM_CMP_R(rn, rm) _AL3_R(ARM_INST_CMP, 0, rn, rm)
  120. #define ARM_CMP_I(rn, imm) _AL3_I(ARM_INST_CMP, 0, rn, imm)
  121. #define ARM_EOR_R(rd, rn, rm) _AL3_R(ARM_INST_EOR, rd, rn, rm)
  122. #define ARM_EOR_I(rd, rn, imm) _AL3_I(ARM_INST_EOR, rd, rn, imm)
  123. #define ARM_LDR_I(rt, rn, off) (ARM_INST_LDR_I | (rt) << 12 | (rn) << 16 \
  124. | (off))
  125. #define ARM_LDRB_I(rt, rn, off) (ARM_INST_LDRB_I | (rt) << 12 | (rn) << 16 \
  126. | (off))
  127. #define ARM_LDRB_R(rt, rn, rm) (ARM_INST_LDRB_R | (rt) << 12 | (rn) << 16 \
  128. | (rm))
  129. #define ARM_LDRH_I(rt, rn, off) (ARM_INST_LDRH_I | (rt) << 12 | (rn) << 16 \
  130. | (((off) & 0xf0) << 4) | ((off) & 0xf))
  131. #define ARM_LDRH_R(rt, rn, rm) (ARM_INST_LDRH_R | (rt) << 12 | (rn) << 16 \
  132. | (rm))
  133. #define ARM_LDM(rn, regs) (ARM_INST_LDM | (rn) << 16 | (regs))
  134. #define ARM_LSL_R(rd, rn, rm) (_AL3_R(ARM_INST_LSL, rd, 0, rn) | (rm) << 8)
  135. #define ARM_LSL_I(rd, rn, imm) (_AL3_I(ARM_INST_LSL, rd, 0, rn) | (imm) << 7)
  136. #define ARM_LSR_R(rd, rn, rm) (_AL3_R(ARM_INST_LSR, rd, 0, rn) | (rm) << 8)
  137. #define ARM_LSR_I(rd, rn, imm) (_AL3_I(ARM_INST_LSR, rd, 0, rn) | (imm) << 7)
  138. #define ARM_MOV_R(rd, rm) _AL3_R(ARM_INST_MOV, rd, 0, rm)
  139. #define ARM_MOV_I(rd, imm) _AL3_I(ARM_INST_MOV, rd, 0, imm)
  140. #define ARM_MOVW(rd, imm) \
  141. (ARM_INST_MOVW | ((imm) >> 12) << 16 | (rd) << 12 | ((imm) & 0x0fff))
  142. #define ARM_MOVT(rd, imm) \
  143. (ARM_INST_MOVT | ((imm) >> 12) << 16 | (rd) << 12 | ((imm) & 0x0fff))
  144. #define ARM_MUL(rd, rm, rn) (ARM_INST_MUL | (rd) << 16 | (rm) << 8 | (rn))
  145. #define ARM_POP(regs) (ARM_INST_POP | (regs))
  146. #define ARM_PUSH(regs) (ARM_INST_PUSH | (regs))
  147. #define ARM_ORR_R(rd, rn, rm) _AL3_R(ARM_INST_ORR, rd, rn, rm)
  148. #define ARM_ORR_I(rd, rn, imm) _AL3_I(ARM_INST_ORR, rd, rn, imm)
  149. #define ARM_ORR_S(rd, rn, rm, type, rs) \
  150. (ARM_ORR_R(rd, rn, rm) | (type) << 5 | (rs) << 7)
  151. #define ARM_REV(rd, rm) (ARM_INST_REV | (rd) << 12 | (rm))
  152. #define ARM_REV16(rd, rm) (ARM_INST_REV16 | (rd) << 12 | (rm))
  153. #define ARM_RSB_I(rd, rn, imm) _AL3_I(ARM_INST_RSB, rd, rn, imm)
  154. #define ARM_SUB_R(rd, rn, rm) _AL3_R(ARM_INST_SUB, rd, rn, rm)
  155. #define ARM_SUB_I(rd, rn, imm) _AL3_I(ARM_INST_SUB, rd, rn, imm)
  156. #define ARM_STR_I(rt, rn, off) (ARM_INST_STR_I | (rt) << 12 | (rn) << 16 \
  157. | (off))
  158. #define ARM_TST_R(rn, rm) _AL3_R(ARM_INST_TST, 0, rn, rm)
  159. #define ARM_TST_I(rn, imm) _AL3_I(ARM_INST_TST, 0, rn, imm)
  160. #define ARM_UDIV(rd, rn, rm) (ARM_INST_UDIV | (rd) << 16 | (rn) | (rm) << 8)
  161. #define ARM_UMULL(rd_lo, rd_hi, rn, rm) (ARM_INST_UMULL | (rd_hi) << 16 \
  162. | (rd_lo) << 12 | (rm) << 8 | rn)
  163. #define ARM_MLS(rd, rn, rm, ra) (ARM_INST_MLS | (rd) << 16 | (rn) | (rm) << 8 \
  164. | (ra) << 12)
  165. #endif /* PFILTER_OPCODES_ARM_H */