bpf_jit_comp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* bpf_jit_comp.c: BPF JIT compiler
  3. *
  4. * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation
  5. *
  6. * Based on the x86 BPF compiler, by Eric Dumazet (eric.dumazet@gmail.com)
  7. * Ported to ppc32 by Denis Kirjanov <kda@linux-powerpc.org>
  8. */
  9. #include <linux/moduleloader.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/asm-compat.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/filter.h>
  14. #include <linux/if_vlan.h>
  15. #include "bpf_jit32.h"
  16. static inline void bpf_flush_icache(void *start, void *end)
  17. {
  18. smp_wmb();
  19. flush_icache_range((unsigned long)start, (unsigned long)end);
  20. }
  21. static void bpf_jit_build_prologue(struct bpf_prog *fp, u32 *image,
  22. struct codegen_context *ctx)
  23. {
  24. int i;
  25. const struct sock_filter *filter = fp->insns;
  26. if (ctx->seen & (SEEN_MEM | SEEN_DATAREF)) {
  27. /* Make stackframe */
  28. if (ctx->seen & SEEN_DATAREF) {
  29. /* If we call any helpers (for loads), save LR */
  30. EMIT(PPC_INST_MFLR | __PPC_RT(R0));
  31. PPC_BPF_STL(0, 1, PPC_LR_STKOFF);
  32. /* Back up non-volatile regs. */
  33. PPC_BPF_STL(r_D, 1, -(REG_SZ*(32-r_D)));
  34. PPC_BPF_STL(r_HL, 1, -(REG_SZ*(32-r_HL)));
  35. }
  36. if (ctx->seen & SEEN_MEM) {
  37. /*
  38. * Conditionally save regs r15-r31 as some will be used
  39. * for M[] data.
  40. */
  41. for (i = r_M; i < (r_M+16); i++) {
  42. if (ctx->seen & (1 << (i-r_M)))
  43. PPC_BPF_STL(i, 1, -(REG_SZ*(32-i)));
  44. }
  45. }
  46. PPC_BPF_STLU(1, 1, -BPF_PPC_STACKFRAME);
  47. }
  48. if (ctx->seen & SEEN_DATAREF) {
  49. /*
  50. * If this filter needs to access skb data,
  51. * prepare r_D and r_HL:
  52. * r_HL = skb->len - skb->data_len
  53. * r_D = skb->data
  54. */
  55. PPC_LWZ_OFFS(r_scratch1, r_skb, offsetof(struct sk_buff,
  56. data_len));
  57. PPC_LWZ_OFFS(r_HL, r_skb, offsetof(struct sk_buff, len));
  58. EMIT(PPC_RAW_SUB(r_HL, r_HL, r_scratch1));
  59. PPC_LL_OFFS(r_D, r_skb, offsetof(struct sk_buff, data));
  60. }
  61. if (ctx->seen & SEEN_XREG) {
  62. /*
  63. * TODO: Could also detect whether first instr. sets X and
  64. * avoid this (as below, with A).
  65. */
  66. EMIT(PPC_RAW_LI(r_X, 0));
  67. }
  68. /* make sure we dont leak kernel information to user */
  69. if (bpf_needs_clear_a(&filter[0]))
  70. EMIT(PPC_RAW_LI(r_A, 0));
  71. }
  72. static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
  73. {
  74. int i;
  75. if (ctx->seen & (SEEN_MEM | SEEN_DATAREF)) {
  76. EMIT(PPC_RAW_ADDI(1, 1, BPF_PPC_STACKFRAME));
  77. if (ctx->seen & SEEN_DATAREF) {
  78. PPC_BPF_LL(0, 1, PPC_LR_STKOFF);
  79. EMIT(PPC_RAW_MTLR(0));
  80. PPC_BPF_LL(r_D, 1, -(REG_SZ*(32-r_D)));
  81. PPC_BPF_LL(r_HL, 1, -(REG_SZ*(32-r_HL)));
  82. }
  83. if (ctx->seen & SEEN_MEM) {
  84. /* Restore any saved non-vol registers */
  85. for (i = r_M; i < (r_M+16); i++) {
  86. if (ctx->seen & (1 << (i-r_M)))
  87. PPC_BPF_LL(i, 1, -(REG_SZ*(32-i)));
  88. }
  89. }
  90. }
  91. /* The RETs have left a return value in R3. */
  92. EMIT(PPC_RAW_BLR());
  93. }
  94. #define CHOOSE_LOAD_FUNC(K, func) \
  95. ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
  96. /* Assemble the body code between the prologue & epilogue. */
  97. static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
  98. struct codegen_context *ctx,
  99. unsigned int *addrs)
  100. {
  101. const struct sock_filter *filter = fp->insns;
  102. int flen = fp->len;
  103. u8 *func;
  104. unsigned int true_cond;
  105. int i;
  106. /* Start of epilogue code */
  107. unsigned int exit_addr = addrs[flen];
  108. for (i = 0; i < flen; i++) {
  109. unsigned int K = filter[i].k;
  110. u16 code = bpf_anc_helper(&filter[i]);
  111. /*
  112. * addrs[] maps a BPF bytecode address into a real offset from
  113. * the start of the body code.
  114. */
  115. addrs[i] = ctx->idx * 4;
  116. switch (code) {
  117. /*** ALU ops ***/
  118. case BPF_ALU | BPF_ADD | BPF_X: /* A += X; */
  119. ctx->seen |= SEEN_XREG;
  120. EMIT(PPC_RAW_ADD(r_A, r_A, r_X));
  121. break;
  122. case BPF_ALU | BPF_ADD | BPF_K: /* A += K; */
  123. if (!K)
  124. break;
  125. EMIT(PPC_RAW_ADDI(r_A, r_A, IMM_L(K)));
  126. if (K >= 32768)
  127. EMIT(PPC_RAW_ADDIS(r_A, r_A, IMM_HA(K)));
  128. break;
  129. case BPF_ALU | BPF_SUB | BPF_X: /* A -= X; */
  130. ctx->seen |= SEEN_XREG;
  131. EMIT(PPC_RAW_SUB(r_A, r_A, r_X));
  132. break;
  133. case BPF_ALU | BPF_SUB | BPF_K: /* A -= K */
  134. if (!K)
  135. break;
  136. EMIT(PPC_RAW_ADDI(r_A, r_A, IMM_L(-K)));
  137. if (K >= 32768)
  138. EMIT(PPC_RAW_ADDIS(r_A, r_A, IMM_HA(-K)));
  139. break;
  140. case BPF_ALU | BPF_MUL | BPF_X: /* A *= X; */
  141. ctx->seen |= SEEN_XREG;
  142. EMIT(PPC_RAW_MULW(r_A, r_A, r_X));
  143. break;
  144. case BPF_ALU | BPF_MUL | BPF_K: /* A *= K */
  145. if (K < 32768)
  146. EMIT(PPC_RAW_MULI(r_A, r_A, K));
  147. else {
  148. PPC_LI32(r_scratch1, K);
  149. EMIT(PPC_RAW_MULW(r_A, r_A, r_scratch1));
  150. }
  151. break;
  152. case BPF_ALU | BPF_MOD | BPF_X: /* A %= X; */
  153. case BPF_ALU | BPF_DIV | BPF_X: /* A /= X; */
  154. ctx->seen |= SEEN_XREG;
  155. EMIT(PPC_RAW_CMPWI(r_X, 0));
  156. if (ctx->pc_ret0 != -1) {
  157. PPC_BCC(COND_EQ, addrs[ctx->pc_ret0]);
  158. } else {
  159. PPC_BCC_SHORT(COND_NE, (ctx->idx*4)+12);
  160. EMIT(PPC_RAW_LI(r_ret, 0));
  161. PPC_JMP(exit_addr);
  162. }
  163. if (code == (BPF_ALU | BPF_MOD | BPF_X)) {
  164. EMIT(PPC_RAW_DIVWU(r_scratch1, r_A, r_X));
  165. EMIT(PPC_RAW_MULW(r_scratch1, r_X, r_scratch1));
  166. EMIT(PPC_RAW_SUB(r_A, r_A, r_scratch1));
  167. } else {
  168. EMIT(PPC_RAW_DIVWU(r_A, r_A, r_X));
  169. }
  170. break;
  171. case BPF_ALU | BPF_MOD | BPF_K: /* A %= K; */
  172. PPC_LI32(r_scratch2, K);
  173. EMIT(PPC_RAW_DIVWU(r_scratch1, r_A, r_scratch2));
  174. EMIT(PPC_RAW_MULW(r_scratch1, r_scratch2, r_scratch1));
  175. EMIT(PPC_RAW_SUB(r_A, r_A, r_scratch1));
  176. break;
  177. case BPF_ALU | BPF_DIV | BPF_K: /* A /= K */
  178. if (K == 1)
  179. break;
  180. PPC_LI32(r_scratch1, K);
  181. EMIT(PPC_RAW_DIVWU(r_A, r_A, r_scratch1));
  182. break;
  183. case BPF_ALU | BPF_AND | BPF_X:
  184. ctx->seen |= SEEN_XREG;
  185. EMIT(PPC_RAW_AND(r_A, r_A, r_X));
  186. break;
  187. case BPF_ALU | BPF_AND | BPF_K:
  188. if (!IMM_H(K))
  189. EMIT(PPC_RAW_ANDI(r_A, r_A, K));
  190. else {
  191. PPC_LI32(r_scratch1, K);
  192. EMIT(PPC_RAW_AND(r_A, r_A, r_scratch1));
  193. }
  194. break;
  195. case BPF_ALU | BPF_OR | BPF_X:
  196. ctx->seen |= SEEN_XREG;
  197. EMIT(PPC_RAW_OR(r_A, r_A, r_X));
  198. break;
  199. case BPF_ALU | BPF_OR | BPF_K:
  200. if (IMM_L(K))
  201. EMIT(PPC_RAW_ORI(r_A, r_A, IMM_L(K)));
  202. if (K >= 65536)
  203. EMIT(PPC_RAW_ORIS(r_A, r_A, IMM_H(K)));
  204. break;
  205. case BPF_ANC | SKF_AD_ALU_XOR_X:
  206. case BPF_ALU | BPF_XOR | BPF_X: /* A ^= X */
  207. ctx->seen |= SEEN_XREG;
  208. EMIT(PPC_RAW_XOR(r_A, r_A, r_X));
  209. break;
  210. case BPF_ALU | BPF_XOR | BPF_K: /* A ^= K */
  211. if (IMM_L(K))
  212. EMIT(PPC_RAW_XORI(r_A, r_A, IMM_L(K)));
  213. if (K >= 65536)
  214. EMIT(PPC_RAW_XORIS(r_A, r_A, IMM_H(K)));
  215. break;
  216. case BPF_ALU | BPF_LSH | BPF_X: /* A <<= X; */
  217. ctx->seen |= SEEN_XREG;
  218. EMIT(PPC_RAW_SLW(r_A, r_A, r_X));
  219. break;
  220. case BPF_ALU | BPF_LSH | BPF_K:
  221. if (K == 0)
  222. break;
  223. else
  224. EMIT(PPC_RAW_SLWI(r_A, r_A, K));
  225. break;
  226. case BPF_ALU | BPF_RSH | BPF_X: /* A >>= X; */
  227. ctx->seen |= SEEN_XREG;
  228. EMIT(PPC_RAW_SRW(r_A, r_A, r_X));
  229. break;
  230. case BPF_ALU | BPF_RSH | BPF_K: /* A >>= K; */
  231. if (K == 0)
  232. break;
  233. else
  234. EMIT(PPC_RAW_SRWI(r_A, r_A, K));
  235. break;
  236. case BPF_ALU | BPF_NEG:
  237. EMIT(PPC_RAW_NEG(r_A, r_A));
  238. break;
  239. case BPF_RET | BPF_K:
  240. PPC_LI32(r_ret, K);
  241. if (!K) {
  242. if (ctx->pc_ret0 == -1)
  243. ctx->pc_ret0 = i;
  244. }
  245. /*
  246. * If this isn't the very last instruction, branch to
  247. * the epilogue if we've stuff to clean up. Otherwise,
  248. * if there's nothing to tidy, just return. If we /are/
  249. * the last instruction, we're about to fall through to
  250. * the epilogue to return.
  251. */
  252. if (i != flen - 1) {
  253. /*
  254. * Note: 'seen' is properly valid only on pass
  255. * #2. Both parts of this conditional are the
  256. * same instruction size though, meaning the
  257. * first pass will still correctly determine the
  258. * code size/addresses.
  259. */
  260. if (ctx->seen)
  261. PPC_JMP(exit_addr);
  262. else
  263. EMIT(PPC_RAW_BLR());
  264. }
  265. break;
  266. case BPF_RET | BPF_A:
  267. EMIT(PPC_RAW_MR(r_ret, r_A));
  268. if (i != flen - 1) {
  269. if (ctx->seen)
  270. PPC_JMP(exit_addr);
  271. else
  272. EMIT(PPC_RAW_BLR());
  273. }
  274. break;
  275. case BPF_MISC | BPF_TAX: /* X = A */
  276. EMIT(PPC_RAW_MR(r_X, r_A));
  277. break;
  278. case BPF_MISC | BPF_TXA: /* A = X */
  279. ctx->seen |= SEEN_XREG;
  280. EMIT(PPC_RAW_MR(r_A, r_X));
  281. break;
  282. /*** Constant loads/M[] access ***/
  283. case BPF_LD | BPF_IMM: /* A = K */
  284. PPC_LI32(r_A, K);
  285. break;
  286. case BPF_LDX | BPF_IMM: /* X = K */
  287. PPC_LI32(r_X, K);
  288. break;
  289. case BPF_LD | BPF_MEM: /* A = mem[K] */
  290. EMIT(PPC_RAW_MR(r_A, r_M + (K & 0xf)));
  291. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  292. break;
  293. case BPF_LDX | BPF_MEM: /* X = mem[K] */
  294. EMIT(PPC_RAW_MR(r_X, r_M + (K & 0xf)));
  295. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  296. break;
  297. case BPF_ST: /* mem[K] = A */
  298. EMIT(PPC_RAW_MR(r_M + (K & 0xf), r_A));
  299. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  300. break;
  301. case BPF_STX: /* mem[K] = X */
  302. EMIT(PPC_RAW_MR(r_M + (K & 0xf), r_X));
  303. ctx->seen |= SEEN_XREG | SEEN_MEM | (1<<(K & 0xf));
  304. break;
  305. case BPF_LD | BPF_W | BPF_LEN: /* A = skb->len; */
  306. BUILD_BUG_ON(sizeof_field(struct sk_buff, len) != 4);
  307. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff, len));
  308. break;
  309. case BPF_LDX | BPF_W | BPF_ABS: /* A = *((u32 *)(seccomp_data + K)); */
  310. PPC_LWZ_OFFS(r_A, r_skb, K);
  311. break;
  312. case BPF_LDX | BPF_W | BPF_LEN: /* X = skb->len; */
  313. PPC_LWZ_OFFS(r_X, r_skb, offsetof(struct sk_buff, len));
  314. break;
  315. /*** Ancillary info loads ***/
  316. case BPF_ANC | SKF_AD_PROTOCOL: /* A = ntohs(skb->protocol); */
  317. BUILD_BUG_ON(sizeof_field(struct sk_buff,
  318. protocol) != 2);
  319. PPC_NTOHS_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  320. protocol));
  321. break;
  322. case BPF_ANC | SKF_AD_IFINDEX:
  323. case BPF_ANC | SKF_AD_HATYPE:
  324. BUILD_BUG_ON(sizeof_field(struct net_device,
  325. ifindex) != 4);
  326. BUILD_BUG_ON(sizeof_field(struct net_device,
  327. type) != 2);
  328. PPC_LL_OFFS(r_scratch1, r_skb, offsetof(struct sk_buff,
  329. dev));
  330. EMIT(PPC_RAW_CMPDI(r_scratch1, 0));
  331. if (ctx->pc_ret0 != -1) {
  332. PPC_BCC(COND_EQ, addrs[ctx->pc_ret0]);
  333. } else {
  334. /* Exit, returning 0; first pass hits here. */
  335. PPC_BCC_SHORT(COND_NE, ctx->idx * 4 + 12);
  336. EMIT(PPC_RAW_LI(r_ret, 0));
  337. PPC_JMP(exit_addr);
  338. }
  339. if (code == (BPF_ANC | SKF_AD_IFINDEX)) {
  340. PPC_LWZ_OFFS(r_A, r_scratch1,
  341. offsetof(struct net_device, ifindex));
  342. } else {
  343. PPC_LHZ_OFFS(r_A, r_scratch1,
  344. offsetof(struct net_device, type));
  345. }
  346. break;
  347. case BPF_ANC | SKF_AD_MARK:
  348. BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
  349. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  350. mark));
  351. break;
  352. case BPF_ANC | SKF_AD_RXHASH:
  353. BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
  354. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  355. hash));
  356. break;
  357. case BPF_ANC | SKF_AD_VLAN_TAG:
  358. BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
  359. PPC_LHZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  360. vlan_tci));
  361. break;
  362. case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
  363. PPC_LBZ_OFFS(r_A, r_skb, PKT_VLAN_PRESENT_OFFSET());
  364. if (PKT_VLAN_PRESENT_BIT)
  365. EMIT(PPC_RAW_SRWI(r_A, r_A, PKT_VLAN_PRESENT_BIT));
  366. if (PKT_VLAN_PRESENT_BIT < 7)
  367. EMIT(PPC_RAW_ANDI(r_A, r_A, 1));
  368. break;
  369. case BPF_ANC | SKF_AD_QUEUE:
  370. BUILD_BUG_ON(sizeof_field(struct sk_buff,
  371. queue_mapping) != 2);
  372. PPC_LHZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  373. queue_mapping));
  374. break;
  375. case BPF_ANC | SKF_AD_PKTTYPE:
  376. PPC_LBZ_OFFS(r_A, r_skb, PKT_TYPE_OFFSET());
  377. EMIT(PPC_RAW_ANDI(r_A, r_A, PKT_TYPE_MAX));
  378. EMIT(PPC_RAW_SRWI(r_A, r_A, 5));
  379. break;
  380. case BPF_ANC | SKF_AD_CPU:
  381. PPC_BPF_LOAD_CPU(r_A);
  382. break;
  383. /*** Absolute loads from packet header/data ***/
  384. case BPF_LD | BPF_W | BPF_ABS:
  385. func = CHOOSE_LOAD_FUNC(K, sk_load_word);
  386. goto common_load;
  387. case BPF_LD | BPF_H | BPF_ABS:
  388. func = CHOOSE_LOAD_FUNC(K, sk_load_half);
  389. goto common_load;
  390. case BPF_LD | BPF_B | BPF_ABS:
  391. func = CHOOSE_LOAD_FUNC(K, sk_load_byte);
  392. common_load:
  393. /* Load from [K]. */
  394. ctx->seen |= SEEN_DATAREF;
  395. PPC_FUNC_ADDR(r_scratch1, func);
  396. EMIT(PPC_RAW_MTLR(r_scratch1));
  397. PPC_LI32(r_addr, K);
  398. EMIT(PPC_RAW_BLRL());
  399. /*
  400. * Helper returns 'lt' condition on error, and an
  401. * appropriate return value in r3
  402. */
  403. PPC_BCC(COND_LT, exit_addr);
  404. break;
  405. /*** Indirect loads from packet header/data ***/
  406. case BPF_LD | BPF_W | BPF_IND:
  407. func = sk_load_word;
  408. goto common_load_ind;
  409. case BPF_LD | BPF_H | BPF_IND:
  410. func = sk_load_half;
  411. goto common_load_ind;
  412. case BPF_LD | BPF_B | BPF_IND:
  413. func = sk_load_byte;
  414. common_load_ind:
  415. /*
  416. * Load from [X + K]. Negative offsets are tested for
  417. * in the helper functions.
  418. */
  419. ctx->seen |= SEEN_DATAREF | SEEN_XREG;
  420. PPC_FUNC_ADDR(r_scratch1, func);
  421. EMIT(PPC_RAW_MTLR(r_scratch1));
  422. EMIT(PPC_RAW_ADDI(r_addr, r_X, IMM_L(K)));
  423. if (K >= 32768)
  424. EMIT(PPC_RAW_ADDIS(r_addr, r_addr, IMM_HA(K)));
  425. EMIT(PPC_RAW_BLRL());
  426. /* If error, cr0.LT set */
  427. PPC_BCC(COND_LT, exit_addr);
  428. break;
  429. case BPF_LDX | BPF_B | BPF_MSH:
  430. func = CHOOSE_LOAD_FUNC(K, sk_load_byte_msh);
  431. goto common_load;
  432. break;
  433. /*** Jump and branches ***/
  434. case BPF_JMP | BPF_JA:
  435. if (K != 0)
  436. PPC_JMP(addrs[i + 1 + K]);
  437. break;
  438. case BPF_JMP | BPF_JGT | BPF_K:
  439. case BPF_JMP | BPF_JGT | BPF_X:
  440. true_cond = COND_GT;
  441. goto cond_branch;
  442. case BPF_JMP | BPF_JGE | BPF_K:
  443. case BPF_JMP | BPF_JGE | BPF_X:
  444. true_cond = COND_GE;
  445. goto cond_branch;
  446. case BPF_JMP | BPF_JEQ | BPF_K:
  447. case BPF_JMP | BPF_JEQ | BPF_X:
  448. true_cond = COND_EQ;
  449. goto cond_branch;
  450. case BPF_JMP | BPF_JSET | BPF_K:
  451. case BPF_JMP | BPF_JSET | BPF_X:
  452. true_cond = COND_NE;
  453. cond_branch:
  454. /* same targets, can avoid doing the test :) */
  455. if (filter[i].jt == filter[i].jf) {
  456. if (filter[i].jt > 0)
  457. PPC_JMP(addrs[i + 1 + filter[i].jt]);
  458. break;
  459. }
  460. switch (code) {
  461. case BPF_JMP | BPF_JGT | BPF_X:
  462. case BPF_JMP | BPF_JGE | BPF_X:
  463. case BPF_JMP | BPF_JEQ | BPF_X:
  464. ctx->seen |= SEEN_XREG;
  465. EMIT(PPC_RAW_CMPLW(r_A, r_X));
  466. break;
  467. case BPF_JMP | BPF_JSET | BPF_X:
  468. ctx->seen |= SEEN_XREG;
  469. EMIT(PPC_RAW_AND_DOT(r_scratch1, r_A, r_X));
  470. break;
  471. case BPF_JMP | BPF_JEQ | BPF_K:
  472. case BPF_JMP | BPF_JGT | BPF_K:
  473. case BPF_JMP | BPF_JGE | BPF_K:
  474. if (K < 32768)
  475. EMIT(PPC_RAW_CMPLWI(r_A, K));
  476. else {
  477. PPC_LI32(r_scratch1, K);
  478. EMIT(PPC_RAW_CMPLW(r_A, r_scratch1));
  479. }
  480. break;
  481. case BPF_JMP | BPF_JSET | BPF_K:
  482. if (K < 32768)
  483. /* PPC_ANDI is /only/ dot-form */
  484. EMIT(PPC_RAW_ANDI(r_scratch1, r_A, K));
  485. else {
  486. PPC_LI32(r_scratch1, K);
  487. EMIT(PPC_RAW_AND_DOT(r_scratch1, r_A,
  488. r_scratch1));
  489. }
  490. break;
  491. }
  492. /* Sometimes branches are constructed "backward", with
  493. * the false path being the branch and true path being
  494. * a fallthrough to the next instruction.
  495. */
  496. if (filter[i].jt == 0)
  497. /* Swap the sense of the branch */
  498. PPC_BCC(true_cond ^ COND_CMP_TRUE,
  499. addrs[i + 1 + filter[i].jf]);
  500. else {
  501. PPC_BCC(true_cond, addrs[i + 1 + filter[i].jt]);
  502. if (filter[i].jf != 0)
  503. PPC_JMP(addrs[i + 1 + filter[i].jf]);
  504. }
  505. break;
  506. default:
  507. /* The filter contains something cruel & unusual.
  508. * We don't handle it, but also there shouldn't be
  509. * anything missing from our list.
  510. */
  511. if (printk_ratelimit())
  512. pr_err("BPF filter opcode %04x (@%d) unsupported\n",
  513. filter[i].code, i);
  514. return -ENOTSUPP;
  515. }
  516. }
  517. /* Set end-of-body-code address for exit. */
  518. addrs[i] = ctx->idx * 4;
  519. return 0;
  520. }
  521. void bpf_jit_compile(struct bpf_prog *fp)
  522. {
  523. unsigned int proglen;
  524. unsigned int alloclen;
  525. u32 *image = NULL;
  526. u32 *code_base;
  527. unsigned int *addrs;
  528. struct codegen_context cgctx;
  529. int pass;
  530. int flen = fp->len;
  531. if (!bpf_jit_enable)
  532. return;
  533. addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
  534. if (addrs == NULL)
  535. return;
  536. /*
  537. * There are multiple assembly passes as the generated code will change
  538. * size as it settles down, figuring out the max branch offsets/exit
  539. * paths required.
  540. *
  541. * The range of standard conditional branches is +/- 32Kbytes. Since
  542. * BPF_MAXINSNS = 4096, we can only jump from (worst case) start to
  543. * finish with 8 bytes/instruction. Not feasible, so long jumps are
  544. * used, distinct from short branches.
  545. *
  546. * Current:
  547. *
  548. * For now, both branch types assemble to 2 words (short branches padded
  549. * with a NOP); this is less efficient, but assembly will always complete
  550. * after exactly 3 passes:
  551. *
  552. * First pass: No code buffer; Program is "faux-generated" -- no code
  553. * emitted but maximum size of output determined (and addrs[] filled
  554. * in). Also, we note whether we use M[], whether we use skb data, etc.
  555. * All generation choices assumed to be 'worst-case', e.g. branches all
  556. * far (2 instructions), return path code reduction not available, etc.
  557. *
  558. * Second pass: Code buffer allocated with size determined previously.
  559. * Prologue generated to support features we have seen used. Exit paths
  560. * determined and addrs[] is filled in again, as code may be slightly
  561. * smaller as a result.
  562. *
  563. * Third pass: Code generated 'for real', and branch destinations
  564. * determined from now-accurate addrs[] map.
  565. *
  566. * Ideal:
  567. *
  568. * If we optimise this, near branches will be shorter. On the
  569. * first assembly pass, we should err on the side of caution and
  570. * generate the biggest code. On subsequent passes, branches will be
  571. * generated short or long and code size will reduce. With smaller
  572. * code, more branches may fall into the short category, and code will
  573. * reduce more.
  574. *
  575. * Finally, if we see one pass generate code the same size as the
  576. * previous pass we have converged and should now generate code for
  577. * real. Allocating at the end will also save the memory that would
  578. * otherwise be wasted by the (small) current code shrinkage.
  579. * Preferably, we should do a small number of passes (e.g. 5) and if we
  580. * haven't converged by then, get impatient and force code to generate
  581. * as-is, even if the odd branch would be left long. The chances of a
  582. * long jump are tiny with all but the most enormous of BPF filter
  583. * inputs, so we should usually converge on the third pass.
  584. */
  585. cgctx.idx = 0;
  586. cgctx.seen = 0;
  587. cgctx.pc_ret0 = -1;
  588. /* Scouting faux-generate pass 0 */
  589. if (bpf_jit_build_body(fp, 0, &cgctx, addrs))
  590. /* We hit something illegal or unsupported. */
  591. goto out;
  592. /*
  593. * Pretend to build prologue, given the features we've seen. This will
  594. * update ctgtx.idx as it pretends to output instructions, then we can
  595. * calculate total size from idx.
  596. */
  597. bpf_jit_build_prologue(fp, 0, &cgctx);
  598. bpf_jit_build_epilogue(0, &cgctx);
  599. proglen = cgctx.idx * 4;
  600. alloclen = proglen + FUNCTION_DESCR_SIZE;
  601. image = module_alloc(alloclen);
  602. if (!image)
  603. goto out;
  604. code_base = image + (FUNCTION_DESCR_SIZE/4);
  605. /* Code generation passes 1-2 */
  606. for (pass = 1; pass < 3; pass++) {
  607. /* Now build the prologue, body code & epilogue for real. */
  608. cgctx.idx = 0;
  609. bpf_jit_build_prologue(fp, code_base, &cgctx);
  610. bpf_jit_build_body(fp, code_base, &cgctx, addrs);
  611. bpf_jit_build_epilogue(code_base, &cgctx);
  612. if (bpf_jit_enable > 1)
  613. pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
  614. proglen - (cgctx.idx * 4), cgctx.seen);
  615. }
  616. if (bpf_jit_enable > 1)
  617. /* Note that we output the base address of the code_base
  618. * rather than image, since opcodes are in code_base.
  619. */
  620. bpf_jit_dump(flen, proglen, pass, code_base);
  621. bpf_flush_icache(code_base, code_base + (proglen/4));
  622. #ifdef CONFIG_PPC64
  623. /* Function descriptor nastiness: Address + TOC */
  624. ((u64 *)image)[0] = (u64)code_base;
  625. ((u64 *)image)[1] = local_paca->kernel_toc;
  626. #endif
  627. fp->bpf_func = (void *)image;
  628. fp->jited = 1;
  629. out:
  630. kfree(addrs);
  631. return;
  632. }
  633. void bpf_jit_free(struct bpf_prog *fp)
  634. {
  635. if (fp->jited)
  636. module_memfree(fp->bpf_func);
  637. bpf_prog_unlock_free(fp);
  638. }