bpf_jit.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* bpf_jit.h: BPF JIT compiler for PPC64
  2. *
  3. * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; version 2
  8. * of the License.
  9. */
  10. #ifndef _BPF_JIT_H
  11. #define _BPF_JIT_H
  12. #ifdef CONFIG_PPC64
  13. #define BPF_PPC_STACK_R3_OFF 48
  14. #define BPF_PPC_STACK_LOCALS 32
  15. #define BPF_PPC_STACK_BASIC (48+64)
  16. #define BPF_PPC_STACK_SAVE (18*8)
  17. #define BPF_PPC_STACKFRAME (BPF_PPC_STACK_BASIC+BPF_PPC_STACK_LOCALS+ \
  18. BPF_PPC_STACK_SAVE)
  19. #define BPF_PPC_SLOWPATH_FRAME (48+64)
  20. #else
  21. #define BPF_PPC_STACK_R3_OFF 24
  22. #define BPF_PPC_STACK_LOCALS 16
  23. #define BPF_PPC_STACK_BASIC (24+32)
  24. #define BPF_PPC_STACK_SAVE (18*4)
  25. #define BPF_PPC_STACKFRAME (BPF_PPC_STACK_BASIC+BPF_PPC_STACK_LOCALS+ \
  26. BPF_PPC_STACK_SAVE)
  27. #define BPF_PPC_SLOWPATH_FRAME (24+32)
  28. #endif
  29. #define REG_SZ (BITS_PER_LONG/8)
  30. /*
  31. * Generated code register usage:
  32. *
  33. * As normal PPC C ABI (e.g. r1=sp, r2=TOC), with:
  34. *
  35. * skb r3 (Entry parameter)
  36. * A register r4
  37. * X register r5
  38. * addr param r6
  39. * r7-r10 scratch
  40. * skb->data r14
  41. * skb headlen r15 (skb->len - skb->data_len)
  42. * m[0] r16
  43. * m[...] ...
  44. * m[15] r31
  45. */
  46. #define r_skb 3
  47. #define r_ret 3
  48. #define r_A 4
  49. #define r_X 5
  50. #define r_addr 6
  51. #define r_scratch1 7
  52. #define r_scratch2 8
  53. #define r_D 14
  54. #define r_HL 15
  55. #define r_M 16
  56. #ifndef __ASSEMBLY__
  57. /*
  58. * Assembly helpers from arch/powerpc/net/bpf_jit.S:
  59. */
  60. #define DECLARE_LOAD_FUNC(func) \
  61. extern u8 func[], func##_negative_offset[], func##_positive_offset[]
  62. DECLARE_LOAD_FUNC(sk_load_word);
  63. DECLARE_LOAD_FUNC(sk_load_half);
  64. DECLARE_LOAD_FUNC(sk_load_byte);
  65. DECLARE_LOAD_FUNC(sk_load_byte_msh);
  66. #ifdef CONFIG_PPC64
  67. #define FUNCTION_DESCR_SIZE 24
  68. #else
  69. #define FUNCTION_DESCR_SIZE 0
  70. #endif
  71. /*
  72. * 16-bit immediate helper macros: HA() is for use with sign-extending instrs
  73. * (e.g. LD, ADDI). If the bottom 16 bits is "-ve", add another bit into the
  74. * top half to negate the effect (i.e. 0xffff + 1 = 0x(1)0000).
  75. */
  76. #define IMM_H(i) ((uintptr_t)(i)>>16)
  77. #define IMM_HA(i) (((uintptr_t)(i)>>16) + \
  78. (((uintptr_t)(i) & 0x8000) >> 15))
  79. #define IMM_L(i) ((uintptr_t)(i) & 0xffff)
  80. #define PLANT_INSTR(d, idx, instr) \
  81. do { if (d) { (d)[idx] = instr; } idx++; } while (0)
  82. #define EMIT(instr) PLANT_INSTR(image, ctx->idx, instr)
  83. #define PPC_NOP() EMIT(PPC_INST_NOP)
  84. #define PPC_BLR() EMIT(PPC_INST_BLR)
  85. #define PPC_BLRL() EMIT(PPC_INST_BLRL)
  86. #define PPC_MTLR(r) EMIT(PPC_INST_MTLR | ___PPC_RT(r))
  87. #define PPC_ADDI(d, a, i) EMIT(PPC_INST_ADDI | ___PPC_RT(d) | \
  88. ___PPC_RA(a) | IMM_L(i))
  89. #define PPC_MR(d, a) PPC_OR(d, a, a)
  90. #define PPC_LI(r, i) PPC_ADDI(r, 0, i)
  91. #define PPC_ADDIS(d, a, i) EMIT(PPC_INST_ADDIS | \
  92. ___PPC_RS(d) | ___PPC_RA(a) | IMM_L(i))
  93. #define PPC_LIS(r, i) PPC_ADDIS(r, 0, i)
  94. #define PPC_STD(r, base, i) EMIT(PPC_INST_STD | ___PPC_RS(r) | \
  95. ___PPC_RA(base) | ((i) & 0xfffc))
  96. #define PPC_STDU(r, base, i) EMIT(PPC_INST_STDU | ___PPC_RS(r) | \
  97. ___PPC_RA(base) | ((i) & 0xfffc))
  98. #define PPC_STW(r, base, i) EMIT(PPC_INST_STW | ___PPC_RS(r) | \
  99. ___PPC_RA(base) | ((i) & 0xfffc))
  100. #define PPC_STWU(r, base, i) EMIT(PPC_INST_STWU | ___PPC_RS(r) | \
  101. ___PPC_RA(base) | ((i) & 0xfffc))
  102. #define PPC_LBZ(r, base, i) EMIT(PPC_INST_LBZ | ___PPC_RT(r) | \
  103. ___PPC_RA(base) | IMM_L(i))
  104. #define PPC_LD(r, base, i) EMIT(PPC_INST_LD | ___PPC_RT(r) | \
  105. ___PPC_RA(base) | IMM_L(i))
  106. #define PPC_LWZ(r, base, i) EMIT(PPC_INST_LWZ | ___PPC_RT(r) | \
  107. ___PPC_RA(base) | IMM_L(i))
  108. #define PPC_LHZ(r, base, i) EMIT(PPC_INST_LHZ | ___PPC_RT(r) | \
  109. ___PPC_RA(base) | IMM_L(i))
  110. #define PPC_LHBRX(r, base, b) EMIT(PPC_INST_LHBRX | ___PPC_RT(r) | \
  111. ___PPC_RA(base) | ___PPC_RB(b))
  112. #ifdef CONFIG_PPC64
  113. #define PPC_BPF_LL(r, base, i) do { PPC_LD(r, base, i); } while(0)
  114. #define PPC_BPF_STL(r, base, i) do { PPC_STD(r, base, i); } while(0)
  115. #define PPC_BPF_STLU(r, base, i) do { PPC_STDU(r, base, i); } while(0)
  116. #else
  117. #define PPC_BPF_LL(r, base, i) do { PPC_LWZ(r, base, i); } while(0)
  118. #define PPC_BPF_STL(r, base, i) do { PPC_STW(r, base, i); } while(0)
  119. #define PPC_BPF_STLU(r, base, i) do { PPC_STWU(r, base, i); } while(0)
  120. #endif
  121. /* Convenience helpers for the above with 'far' offsets: */
  122. #define PPC_LBZ_OFFS(r, base, i) do { if ((i) < 32768) PPC_LBZ(r, base, i); \
  123. else { PPC_ADDIS(r, base, IMM_HA(i)); \
  124. PPC_LBZ(r, r, IMM_L(i)); } } while(0)
  125. #define PPC_LD_OFFS(r, base, i) do { if ((i) < 32768) PPC_LD(r, base, i); \
  126. else { PPC_ADDIS(r, base, IMM_HA(i)); \
  127. PPC_LD(r, r, IMM_L(i)); } } while(0)
  128. #define PPC_LWZ_OFFS(r, base, i) do { if ((i) < 32768) PPC_LWZ(r, base, i); \
  129. else { PPC_ADDIS(r, base, IMM_HA(i)); \
  130. PPC_LWZ(r, r, IMM_L(i)); } } while(0)
  131. #define PPC_LHZ_OFFS(r, base, i) do { if ((i) < 32768) PPC_LHZ(r, base, i); \
  132. else { PPC_ADDIS(r, base, IMM_HA(i)); \
  133. PPC_LHZ(r, r, IMM_L(i)); } } while(0)
  134. #ifdef CONFIG_PPC64
  135. #define PPC_LL_OFFS(r, base, i) do { PPC_LD_OFFS(r, base, i); } while(0)
  136. #else
  137. #define PPC_LL_OFFS(r, base, i) do { PPC_LWZ_OFFS(r, base, i); } while(0)
  138. #endif
  139. #ifdef CONFIG_SMP
  140. #ifdef CONFIG_PPC64
  141. #define PPC_BPF_LOAD_CPU(r) \
  142. do { BUILD_BUG_ON(FIELD_SIZEOF(struct paca_struct, paca_index) != 2); \
  143. PPC_LHZ_OFFS(r, 13, offsetof(struct paca_struct, paca_index)); \
  144. } while (0)
  145. #else
  146. #define PPC_BPF_LOAD_CPU(r) \
  147. do { BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4); \
  148. PPC_LHZ_OFFS(r, (1 & ~(THREAD_SIZE - 1)), \
  149. offsetof(struct thread_info, cpu)); \
  150. } while(0)
  151. #endif
  152. #else
  153. #define PPC_BPF_LOAD_CPU(r) do { PPC_LI(r, 0); } while(0)
  154. #endif
  155. #define PPC_CMPWI(a, i) EMIT(PPC_INST_CMPWI | ___PPC_RA(a) | IMM_L(i))
  156. #define PPC_CMPDI(a, i) EMIT(PPC_INST_CMPDI | ___PPC_RA(a) | IMM_L(i))
  157. #define PPC_CMPLWI(a, i) EMIT(PPC_INST_CMPLWI | ___PPC_RA(a) | IMM_L(i))
  158. #define PPC_CMPLW(a, b) EMIT(PPC_INST_CMPLW | ___PPC_RA(a) | ___PPC_RB(b))
  159. #define PPC_SUB(d, a, b) EMIT(PPC_INST_SUB | ___PPC_RT(d) | \
  160. ___PPC_RB(a) | ___PPC_RA(b))
  161. #define PPC_ADD(d, a, b) EMIT(PPC_INST_ADD | ___PPC_RT(d) | \
  162. ___PPC_RA(a) | ___PPC_RB(b))
  163. #define PPC_MUL(d, a, b) EMIT(PPC_INST_MULLW | ___PPC_RT(d) | \
  164. ___PPC_RA(a) | ___PPC_RB(b))
  165. #define PPC_MULHWU(d, a, b) EMIT(PPC_INST_MULHWU | ___PPC_RT(d) | \
  166. ___PPC_RA(a) | ___PPC_RB(b))
  167. #define PPC_MULI(d, a, i) EMIT(PPC_INST_MULLI | ___PPC_RT(d) | \
  168. ___PPC_RA(a) | IMM_L(i))
  169. #define PPC_DIVWU(d, a, b) EMIT(PPC_INST_DIVWU | ___PPC_RT(d) | \
  170. ___PPC_RA(a) | ___PPC_RB(b))
  171. #define PPC_AND(d, a, b) EMIT(PPC_INST_AND | ___PPC_RA(d) | \
  172. ___PPC_RS(a) | ___PPC_RB(b))
  173. #define PPC_ANDI(d, a, i) EMIT(PPC_INST_ANDI | ___PPC_RA(d) | \
  174. ___PPC_RS(a) | IMM_L(i))
  175. #define PPC_AND_DOT(d, a, b) EMIT(PPC_INST_ANDDOT | ___PPC_RA(d) | \
  176. ___PPC_RS(a) | ___PPC_RB(b))
  177. #define PPC_OR(d, a, b) EMIT(PPC_INST_OR | ___PPC_RA(d) | \
  178. ___PPC_RS(a) | ___PPC_RB(b))
  179. #define PPC_ORI(d, a, i) EMIT(PPC_INST_ORI | ___PPC_RA(d) | \
  180. ___PPC_RS(a) | IMM_L(i))
  181. #define PPC_ORIS(d, a, i) EMIT(PPC_INST_ORIS | ___PPC_RA(d) | \
  182. ___PPC_RS(a) | IMM_L(i))
  183. #define PPC_XOR(d, a, b) EMIT(PPC_INST_XOR | ___PPC_RA(d) | \
  184. ___PPC_RS(a) | ___PPC_RB(b))
  185. #define PPC_XORI(d, a, i) EMIT(PPC_INST_XORI | ___PPC_RA(d) | \
  186. ___PPC_RS(a) | IMM_L(i))
  187. #define PPC_XORIS(d, a, i) EMIT(PPC_INST_XORIS | ___PPC_RA(d) | \
  188. ___PPC_RS(a) | IMM_L(i))
  189. #define PPC_SLW(d, a, s) EMIT(PPC_INST_SLW | ___PPC_RA(d) | \
  190. ___PPC_RS(a) | ___PPC_RB(s))
  191. #define PPC_SRW(d, a, s) EMIT(PPC_INST_SRW | ___PPC_RA(d) | \
  192. ___PPC_RS(a) | ___PPC_RB(s))
  193. /* slwi = rlwinm Rx, Ry, n, 0, 31-n */
  194. #define PPC_SLWI(d, a, i) EMIT(PPC_INST_RLWINM | ___PPC_RA(d) | \
  195. ___PPC_RS(a) | __PPC_SH(i) | \
  196. __PPC_MB(0) | __PPC_ME(31-(i)))
  197. /* srwi = rlwinm Rx, Ry, 32-n, n, 31 */
  198. #define PPC_SRWI(d, a, i) EMIT(PPC_INST_RLWINM | ___PPC_RA(d) | \
  199. ___PPC_RS(a) | __PPC_SH(32-(i)) | \
  200. __PPC_MB(i) | __PPC_ME(31))
  201. /* sldi = rldicr Rx, Ry, n, 63-n */
  202. #define PPC_SLDI(d, a, i) EMIT(PPC_INST_RLDICR | ___PPC_RA(d) | \
  203. ___PPC_RS(a) | __PPC_SH(i) | \
  204. __PPC_MB(63-(i)) | (((i) & 0x20) >> 4))
  205. #define PPC_NEG(d, a) EMIT(PPC_INST_NEG | ___PPC_RT(d) | ___PPC_RA(a))
  206. /* Long jump; (unconditional 'branch') */
  207. #define PPC_JMP(dest) EMIT(PPC_INST_BRANCH | \
  208. (((dest) - (ctx->idx * 4)) & 0x03fffffc))
  209. /* "cond" here covers BO:BI fields. */
  210. #define PPC_BCC_SHORT(cond, dest) EMIT(PPC_INST_BRANCH_COND | \
  211. (((cond) & 0x3ff) << 16) | \
  212. (((dest) - (ctx->idx * 4)) & \
  213. 0xfffc))
  214. #define PPC_LI32(d, i) do { PPC_LI(d, IMM_L(i)); \
  215. if ((u32)(uintptr_t)(i) >= 32768) { \
  216. PPC_ADDIS(d, d, IMM_HA(i)); \
  217. } } while(0)
  218. #define PPC_LI64(d, i) do { \
  219. if (!((uintptr_t)(i) & 0xffffffff00000000ULL)) \
  220. PPC_LI32(d, i); \
  221. else { \
  222. PPC_LIS(d, ((uintptr_t)(i) >> 48)); \
  223. if ((uintptr_t)(i) & 0x0000ffff00000000ULL) \
  224. PPC_ORI(d, d, \
  225. ((uintptr_t)(i) >> 32) & 0xffff); \
  226. PPC_SLDI(d, d, 32); \
  227. if ((uintptr_t)(i) & 0x00000000ffff0000ULL) \
  228. PPC_ORIS(d, d, \
  229. ((uintptr_t)(i) >> 16) & 0xffff); \
  230. if ((uintptr_t)(i) & 0x000000000000ffffULL) \
  231. PPC_ORI(d, d, (uintptr_t)(i) & 0xffff); \
  232. } } while (0);
  233. #ifdef CONFIG_PPC64
  234. #define PPC_FUNC_ADDR(d,i) do { PPC_LI64(d, i); } while(0)
  235. #else
  236. #define PPC_FUNC_ADDR(d,i) do { PPC_LI32(d, i); } while(0)
  237. #endif
  238. #define PPC_LHBRX_OFFS(r, base, i) \
  239. do { PPC_LI32(r, i); PPC_LHBRX(r, r, base); } while(0)
  240. #ifdef __LITTLE_ENDIAN__
  241. #define PPC_NTOHS_OFFS(r, base, i) PPC_LHBRX_OFFS(r, base, i)
  242. #else
  243. #define PPC_NTOHS_OFFS(r, base, i) PPC_LHZ_OFFS(r, base, i)
  244. #endif
  245. static inline bool is_nearbranch(int offset)
  246. {
  247. return (offset < 32768) && (offset >= -32768);
  248. }
  249. /*
  250. * The fly in the ointment of code size changing from pass to pass is
  251. * avoided by padding the short branch case with a NOP. If code size differs
  252. * with different branch reaches we will have the issue of code moving from
  253. * one pass to the next and will need a few passes to converge on a stable
  254. * state.
  255. */
  256. #define PPC_BCC(cond, dest) do { \
  257. if (is_nearbranch((dest) - (ctx->idx * 4))) { \
  258. PPC_BCC_SHORT(cond, dest); \
  259. PPC_NOP(); \
  260. } else { \
  261. /* Flip the 'T or F' bit to invert comparison */ \
  262. PPC_BCC_SHORT(cond ^ COND_CMP_TRUE, (ctx->idx+2)*4); \
  263. PPC_JMP(dest); \
  264. } } while(0)
  265. /* To create a branch condition, select a bit of cr0... */
  266. #define CR0_LT 0
  267. #define CR0_GT 1
  268. #define CR0_EQ 2
  269. /* ...and modify BO[3] */
  270. #define COND_CMP_TRUE 0x100
  271. #define COND_CMP_FALSE 0x000
  272. /* Together, they make all required comparisons: */
  273. #define COND_GT (CR0_GT | COND_CMP_TRUE)
  274. #define COND_GE (CR0_LT | COND_CMP_FALSE)
  275. #define COND_EQ (CR0_EQ | COND_CMP_TRUE)
  276. #define COND_NE (CR0_EQ | COND_CMP_FALSE)
  277. #define COND_LT (CR0_LT | COND_CMP_TRUE)
  278. #define SEEN_DATAREF 0x10000 /* might call external helpers */
  279. #define SEEN_XREG 0x20000 /* X reg is used */
  280. #define SEEN_MEM 0x40000 /* SEEN_MEM+(1<<n) = use mem[n] for temporary
  281. * storage */
  282. #define SEEN_MEM_MSK 0x0ffff
  283. struct codegen_context {
  284. unsigned int seen;
  285. unsigned int idx;
  286. int pc_ret0; /* bpf index of first RET #0 instruction (if any) */
  287. };
  288. #endif
  289. #endif