bpf_jit_comp.c 19 KB

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