bpf_jit_comp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /* bpf_jit_comp.c: BPF JIT compiler for PPC64
  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. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. */
  12. #include <linux/moduleloader.h>
  13. #include <asm/cacheflush.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/filter.h>
  16. #include <linux/if_vlan.h>
  17. #include "bpf_jit.h"
  18. int bpf_jit_enable __read_mostly;
  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 sk_filter *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_STD(0, 1, 16);
  35. /* Back up non-volatile regs. */
  36. PPC_STD(r_D, 1, -(8*(32-r_D)));
  37. PPC_STD(r_HL, 1, -(8*(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_STD(i, 1, -(8*(32-i)));
  47. }
  48. }
  49. EMIT(PPC_INST_STDU | __PPC_RS(R1) | __PPC_RA(R1) |
  50. (-BPF_PPC_STACKFRAME & 0xfffc));
  51. }
  52. if (ctx->seen & SEEN_DATAREF) {
  53. /*
  54. * If this filter needs to access skb data,
  55. * prepare r_D and r_HL:
  56. * r_HL = skb->len - skb->data_len
  57. * r_D = skb->data
  58. */
  59. PPC_LWZ_OFFS(r_scratch1, r_skb, offsetof(struct sk_buff,
  60. data_len));
  61. PPC_LWZ_OFFS(r_HL, r_skb, offsetof(struct sk_buff, len));
  62. PPC_SUB(r_HL, r_HL, r_scratch1);
  63. PPC_LD_OFFS(r_D, r_skb, offsetof(struct sk_buff, data));
  64. }
  65. if (ctx->seen & SEEN_XREG) {
  66. /*
  67. * TODO: Could also detect whether first instr. sets X and
  68. * avoid this (as below, with A).
  69. */
  70. PPC_LI(r_X, 0);
  71. }
  72. switch (filter[0].code) {
  73. case BPF_S_RET_K:
  74. case BPF_S_LD_W_LEN:
  75. case BPF_S_ANC_PROTOCOL:
  76. case BPF_S_ANC_IFINDEX:
  77. case BPF_S_ANC_MARK:
  78. case BPF_S_ANC_RXHASH:
  79. case BPF_S_ANC_VLAN_TAG:
  80. case BPF_S_ANC_VLAN_TAG_PRESENT:
  81. case BPF_S_ANC_CPU:
  82. case BPF_S_ANC_QUEUE:
  83. case BPF_S_LD_W_ABS:
  84. case BPF_S_LD_H_ABS:
  85. case BPF_S_LD_B_ABS:
  86. /* first instruction sets A register (or is RET 'constant') */
  87. break;
  88. default:
  89. /* make sure we dont leak kernel information to user */
  90. PPC_LI(r_A, 0);
  91. }
  92. }
  93. static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
  94. {
  95. int i;
  96. if (ctx->seen & (SEEN_MEM | SEEN_DATAREF)) {
  97. PPC_ADDI(1, 1, BPF_PPC_STACKFRAME);
  98. if (ctx->seen & SEEN_DATAREF) {
  99. PPC_LD(0, 1, 16);
  100. PPC_MTLR(0);
  101. PPC_LD(r_D, 1, -(8*(32-r_D)));
  102. PPC_LD(r_HL, 1, -(8*(32-r_HL)));
  103. }
  104. if (ctx->seen & SEEN_MEM) {
  105. /* Restore any saved non-vol registers */
  106. for (i = r_M; i < (r_M+16); i++) {
  107. if (ctx->seen & (1 << (i-r_M)))
  108. PPC_LD(i, 1, -(8*(32-i)));
  109. }
  110. }
  111. }
  112. /* The RETs have left a return value in R3. */
  113. PPC_BLR();
  114. }
  115. #define CHOOSE_LOAD_FUNC(K, func) \
  116. ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
  117. /* Assemble the body code between the prologue & epilogue. */
  118. static int bpf_jit_build_body(struct sk_filter *fp, u32 *image,
  119. struct codegen_context *ctx,
  120. unsigned int *addrs)
  121. {
  122. const struct sock_filter *filter = fp->insns;
  123. int flen = fp->len;
  124. u8 *func;
  125. unsigned int true_cond;
  126. int i;
  127. /* Start of epilogue code */
  128. unsigned int exit_addr = addrs[flen];
  129. for (i = 0; i < flen; i++) {
  130. unsigned int K = filter[i].k;
  131. /*
  132. * addrs[] maps a BPF bytecode address into a real offset from
  133. * the start of the body code.
  134. */
  135. addrs[i] = ctx->idx * 4;
  136. switch (filter[i].code) {
  137. /*** ALU ops ***/
  138. case BPF_S_ALU_ADD_X: /* A += X; */
  139. ctx->seen |= SEEN_XREG;
  140. PPC_ADD(r_A, r_A, r_X);
  141. break;
  142. case BPF_S_ALU_ADD_K: /* A += K; */
  143. if (!K)
  144. break;
  145. PPC_ADDI(r_A, r_A, IMM_L(K));
  146. if (K >= 32768)
  147. PPC_ADDIS(r_A, r_A, IMM_HA(K));
  148. break;
  149. case BPF_S_ALU_SUB_X: /* A -= X; */
  150. ctx->seen |= SEEN_XREG;
  151. PPC_SUB(r_A, r_A, r_X);
  152. break;
  153. case BPF_S_ALU_SUB_K: /* A -= K */
  154. if (!K)
  155. break;
  156. PPC_ADDI(r_A, r_A, IMM_L(-K));
  157. if (K >= 32768)
  158. PPC_ADDIS(r_A, r_A, IMM_HA(-K));
  159. break;
  160. case BPF_S_ALU_MUL_X: /* A *= X; */
  161. ctx->seen |= SEEN_XREG;
  162. PPC_MUL(r_A, r_A, r_X);
  163. break;
  164. case BPF_S_ALU_MUL_K: /* A *= K */
  165. if (K < 32768)
  166. PPC_MULI(r_A, r_A, K);
  167. else {
  168. PPC_LI32(r_scratch1, K);
  169. PPC_MUL(r_A, r_A, r_scratch1);
  170. }
  171. break;
  172. case BPF_S_ALU_MOD_X: /* A %= X; */
  173. ctx->seen |= SEEN_XREG;
  174. PPC_CMPWI(r_X, 0);
  175. if (ctx->pc_ret0 != -1) {
  176. PPC_BCC(COND_EQ, addrs[ctx->pc_ret0]);
  177. } else {
  178. PPC_BCC_SHORT(COND_NE, (ctx->idx*4)+12);
  179. PPC_LI(r_ret, 0);
  180. PPC_JMP(exit_addr);
  181. }
  182. PPC_DIVWU(r_scratch1, r_A, r_X);
  183. PPC_MUL(r_scratch1, r_X, r_scratch1);
  184. PPC_SUB(r_A, r_A, r_scratch1);
  185. break;
  186. case BPF_S_ALU_MOD_K: /* A %= K; */
  187. PPC_LI32(r_scratch2, K);
  188. PPC_DIVWU(r_scratch1, r_A, r_scratch2);
  189. PPC_MUL(r_scratch1, r_scratch2, r_scratch1);
  190. PPC_SUB(r_A, r_A, r_scratch1);
  191. break;
  192. case BPF_S_ALU_DIV_X: /* A /= X; */
  193. ctx->seen |= SEEN_XREG;
  194. PPC_CMPWI(r_X, 0);
  195. if (ctx->pc_ret0 != -1) {
  196. PPC_BCC(COND_EQ, addrs[ctx->pc_ret0]);
  197. } else {
  198. /*
  199. * Exit, returning 0; first pass hits here
  200. * (longer worst-case code size).
  201. */
  202. PPC_BCC_SHORT(COND_NE, (ctx->idx*4)+12);
  203. PPC_LI(r_ret, 0);
  204. PPC_JMP(exit_addr);
  205. }
  206. PPC_DIVWU(r_A, r_A, r_X);
  207. break;
  208. case BPF_S_ALU_DIV_K: /* A /= K */
  209. if (K == 1)
  210. break;
  211. PPC_LI32(r_scratch1, K);
  212. PPC_DIVWU(r_A, r_A, r_scratch1);
  213. break;
  214. case BPF_S_ALU_AND_X:
  215. ctx->seen |= SEEN_XREG;
  216. PPC_AND(r_A, r_A, r_X);
  217. break;
  218. case BPF_S_ALU_AND_K:
  219. if (!IMM_H(K))
  220. PPC_ANDI(r_A, r_A, K);
  221. else {
  222. PPC_LI32(r_scratch1, K);
  223. PPC_AND(r_A, r_A, r_scratch1);
  224. }
  225. break;
  226. case BPF_S_ALU_OR_X:
  227. ctx->seen |= SEEN_XREG;
  228. PPC_OR(r_A, r_A, r_X);
  229. break;
  230. case BPF_S_ALU_OR_K:
  231. if (IMM_L(K))
  232. PPC_ORI(r_A, r_A, IMM_L(K));
  233. if (K >= 65536)
  234. PPC_ORIS(r_A, r_A, IMM_H(K));
  235. break;
  236. case BPF_S_ANC_ALU_XOR_X:
  237. case BPF_S_ALU_XOR_X: /* A ^= X */
  238. ctx->seen |= SEEN_XREG;
  239. PPC_XOR(r_A, r_A, r_X);
  240. break;
  241. case BPF_S_ALU_XOR_K: /* A ^= K */
  242. if (IMM_L(K))
  243. PPC_XORI(r_A, r_A, IMM_L(K));
  244. if (K >= 65536)
  245. PPC_XORIS(r_A, r_A, IMM_H(K));
  246. break;
  247. case BPF_S_ALU_LSH_X: /* A <<= X; */
  248. ctx->seen |= SEEN_XREG;
  249. PPC_SLW(r_A, r_A, r_X);
  250. break;
  251. case BPF_S_ALU_LSH_K:
  252. if (K == 0)
  253. break;
  254. else
  255. PPC_SLWI(r_A, r_A, K);
  256. break;
  257. case BPF_S_ALU_RSH_X: /* A >>= X; */
  258. ctx->seen |= SEEN_XREG;
  259. PPC_SRW(r_A, r_A, r_X);
  260. break;
  261. case BPF_S_ALU_RSH_K: /* A >>= K; */
  262. if (K == 0)
  263. break;
  264. else
  265. PPC_SRWI(r_A, r_A, K);
  266. break;
  267. case BPF_S_ALU_NEG:
  268. PPC_NEG(r_A, r_A);
  269. break;
  270. case BPF_S_RET_K:
  271. PPC_LI32(r_ret, K);
  272. if (!K) {
  273. if (ctx->pc_ret0 == -1)
  274. ctx->pc_ret0 = i;
  275. }
  276. /*
  277. * If this isn't the very last instruction, branch to
  278. * the epilogue if we've stuff to clean up. Otherwise,
  279. * if there's nothing to tidy, just return. If we /are/
  280. * the last instruction, we're about to fall through to
  281. * the epilogue to return.
  282. */
  283. if (i != flen - 1) {
  284. /*
  285. * Note: 'seen' is properly valid only on pass
  286. * #2. Both parts of this conditional are the
  287. * same instruction size though, meaning the
  288. * first pass will still correctly determine the
  289. * code size/addresses.
  290. */
  291. if (ctx->seen)
  292. PPC_JMP(exit_addr);
  293. else
  294. PPC_BLR();
  295. }
  296. break;
  297. case BPF_S_RET_A:
  298. PPC_MR(r_ret, r_A);
  299. if (i != flen - 1) {
  300. if (ctx->seen)
  301. PPC_JMP(exit_addr);
  302. else
  303. PPC_BLR();
  304. }
  305. break;
  306. case BPF_S_MISC_TAX: /* X = A */
  307. PPC_MR(r_X, r_A);
  308. break;
  309. case BPF_S_MISC_TXA: /* A = X */
  310. ctx->seen |= SEEN_XREG;
  311. PPC_MR(r_A, r_X);
  312. break;
  313. /*** Constant loads/M[] access ***/
  314. case BPF_S_LD_IMM: /* A = K */
  315. PPC_LI32(r_A, K);
  316. break;
  317. case BPF_S_LDX_IMM: /* X = K */
  318. PPC_LI32(r_X, K);
  319. break;
  320. case BPF_S_LD_MEM: /* A = mem[K] */
  321. PPC_MR(r_A, r_M + (K & 0xf));
  322. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  323. break;
  324. case BPF_S_LDX_MEM: /* X = mem[K] */
  325. PPC_MR(r_X, r_M + (K & 0xf));
  326. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  327. break;
  328. case BPF_S_ST: /* mem[K] = A */
  329. PPC_MR(r_M + (K & 0xf), r_A);
  330. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  331. break;
  332. case BPF_S_STX: /* mem[K] = X */
  333. PPC_MR(r_M + (K & 0xf), r_X);
  334. ctx->seen |= SEEN_XREG | SEEN_MEM | (1<<(K & 0xf));
  335. break;
  336. case BPF_S_LD_W_LEN: /* A = skb->len; */
  337. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
  338. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff, len));
  339. break;
  340. case BPF_S_LDX_W_LEN: /* X = skb->len; */
  341. PPC_LWZ_OFFS(r_X, r_skb, offsetof(struct sk_buff, len));
  342. break;
  343. /*** Ancillary info loads ***/
  344. case BPF_S_ANC_PROTOCOL: /* A = ntohs(skb->protocol); */
  345. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  346. protocol) != 2);
  347. PPC_NTOHS_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  348. protocol));
  349. break;
  350. case BPF_S_ANC_IFINDEX:
  351. PPC_LD_OFFS(r_scratch1, r_skb, offsetof(struct sk_buff,
  352. dev));
  353. PPC_CMPDI(r_scratch1, 0);
  354. if (ctx->pc_ret0 != -1) {
  355. PPC_BCC(COND_EQ, addrs[ctx->pc_ret0]);
  356. } else {
  357. /* Exit, returning 0; first pass hits here. */
  358. PPC_BCC_SHORT(COND_NE, (ctx->idx*4)+12);
  359. PPC_LI(r_ret, 0);
  360. PPC_JMP(exit_addr);
  361. }
  362. BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
  363. ifindex) != 4);
  364. PPC_LWZ_OFFS(r_A, r_scratch1,
  365. offsetof(struct net_device, ifindex));
  366. break;
  367. case BPF_S_ANC_MARK:
  368. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
  369. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  370. mark));
  371. break;
  372. case BPF_S_ANC_RXHASH:
  373. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, rxhash) != 4);
  374. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  375. rxhash));
  376. break;
  377. case BPF_S_ANC_VLAN_TAG:
  378. case BPF_S_ANC_VLAN_TAG_PRESENT:
  379. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
  380. PPC_LHZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  381. vlan_tci));
  382. if (filter[i].code == BPF_S_ANC_VLAN_TAG)
  383. PPC_ANDI(r_A, r_A, VLAN_VID_MASK);
  384. else
  385. PPC_ANDI(r_A, r_A, VLAN_TAG_PRESENT);
  386. break;
  387. case BPF_S_ANC_QUEUE:
  388. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  389. queue_mapping) != 2);
  390. PPC_LHZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  391. queue_mapping));
  392. break;
  393. case BPF_S_ANC_CPU:
  394. #ifdef CONFIG_SMP
  395. /*
  396. * PACA ptr is r13:
  397. * raw_smp_processor_id() = local_paca->paca_index
  398. */
  399. BUILD_BUG_ON(FIELD_SIZEOF(struct paca_struct,
  400. paca_index) != 2);
  401. PPC_LHZ_OFFS(r_A, 13,
  402. offsetof(struct paca_struct, paca_index));
  403. #else
  404. PPC_LI(r_A, 0);
  405. #endif
  406. break;
  407. /*** Absolute loads from packet header/data ***/
  408. case BPF_S_LD_W_ABS:
  409. func = CHOOSE_LOAD_FUNC(K, sk_load_word);
  410. goto common_load;
  411. case BPF_S_LD_H_ABS:
  412. func = CHOOSE_LOAD_FUNC(K, sk_load_half);
  413. goto common_load;
  414. case BPF_S_LD_B_ABS:
  415. func = CHOOSE_LOAD_FUNC(K, sk_load_byte);
  416. common_load:
  417. /* Load from [K]. */
  418. ctx->seen |= SEEN_DATAREF;
  419. PPC_LI64(r_scratch1, func);
  420. PPC_MTLR(r_scratch1);
  421. PPC_LI32(r_addr, K);
  422. PPC_BLRL();
  423. /*
  424. * Helper returns 'lt' condition on error, and an
  425. * appropriate return value in r3
  426. */
  427. PPC_BCC(COND_LT, exit_addr);
  428. break;
  429. /*** Indirect loads from packet header/data ***/
  430. case BPF_S_LD_W_IND:
  431. func = sk_load_word;
  432. goto common_load_ind;
  433. case BPF_S_LD_H_IND:
  434. func = sk_load_half;
  435. goto common_load_ind;
  436. case BPF_S_LD_B_IND:
  437. func = sk_load_byte;
  438. common_load_ind:
  439. /*
  440. * Load from [X + K]. Negative offsets are tested for
  441. * in the helper functions.
  442. */
  443. ctx->seen |= SEEN_DATAREF | SEEN_XREG;
  444. PPC_LI64(r_scratch1, func);
  445. PPC_MTLR(r_scratch1);
  446. PPC_ADDI(r_addr, r_X, IMM_L(K));
  447. if (K >= 32768)
  448. PPC_ADDIS(r_addr, r_addr, IMM_HA(K));
  449. PPC_BLRL();
  450. /* If error, cr0.LT set */
  451. PPC_BCC(COND_LT, exit_addr);
  452. break;
  453. case BPF_S_LDX_B_MSH:
  454. func = CHOOSE_LOAD_FUNC(K, sk_load_byte_msh);
  455. goto common_load;
  456. break;
  457. /*** Jump and branches ***/
  458. case BPF_S_JMP_JA:
  459. if (K != 0)
  460. PPC_JMP(addrs[i + 1 + K]);
  461. break;
  462. case BPF_S_JMP_JGT_K:
  463. case BPF_S_JMP_JGT_X:
  464. true_cond = COND_GT;
  465. goto cond_branch;
  466. case BPF_S_JMP_JGE_K:
  467. case BPF_S_JMP_JGE_X:
  468. true_cond = COND_GE;
  469. goto cond_branch;
  470. case BPF_S_JMP_JEQ_K:
  471. case BPF_S_JMP_JEQ_X:
  472. true_cond = COND_EQ;
  473. goto cond_branch;
  474. case BPF_S_JMP_JSET_K:
  475. case BPF_S_JMP_JSET_X:
  476. true_cond = COND_NE;
  477. /* Fall through */
  478. cond_branch:
  479. /* same targets, can avoid doing the test :) */
  480. if (filter[i].jt == filter[i].jf) {
  481. if (filter[i].jt > 0)
  482. PPC_JMP(addrs[i + 1 + filter[i].jt]);
  483. break;
  484. }
  485. switch (filter[i].code) {
  486. case BPF_S_JMP_JGT_X:
  487. case BPF_S_JMP_JGE_X:
  488. case BPF_S_JMP_JEQ_X:
  489. ctx->seen |= SEEN_XREG;
  490. PPC_CMPLW(r_A, r_X);
  491. break;
  492. case BPF_S_JMP_JSET_X:
  493. ctx->seen |= SEEN_XREG;
  494. PPC_AND_DOT(r_scratch1, r_A, r_X);
  495. break;
  496. case BPF_S_JMP_JEQ_K:
  497. case BPF_S_JMP_JGT_K:
  498. case BPF_S_JMP_JGE_K:
  499. if (K < 32768)
  500. PPC_CMPLWI(r_A, K);
  501. else {
  502. PPC_LI32(r_scratch1, K);
  503. PPC_CMPLW(r_A, r_scratch1);
  504. }
  505. break;
  506. case BPF_S_JMP_JSET_K:
  507. if (K < 32768)
  508. /* PPC_ANDI is /only/ dot-form */
  509. PPC_ANDI(r_scratch1, r_A, K);
  510. else {
  511. PPC_LI32(r_scratch1, K);
  512. PPC_AND_DOT(r_scratch1, r_A,
  513. r_scratch1);
  514. }
  515. break;
  516. }
  517. /* Sometimes branches are constructed "backward", with
  518. * the false path being the branch and true path being
  519. * a fallthrough to the next instruction.
  520. */
  521. if (filter[i].jt == 0)
  522. /* Swap the sense of the branch */
  523. PPC_BCC(true_cond ^ COND_CMP_TRUE,
  524. addrs[i + 1 + filter[i].jf]);
  525. else {
  526. PPC_BCC(true_cond, addrs[i + 1 + filter[i].jt]);
  527. if (filter[i].jf != 0)
  528. PPC_JMP(addrs[i + 1 + filter[i].jf]);
  529. }
  530. break;
  531. default:
  532. /* The filter contains something cruel & unusual.
  533. * We don't handle it, but also there shouldn't be
  534. * anything missing from our list.
  535. */
  536. if (printk_ratelimit())
  537. pr_err("BPF filter opcode %04x (@%d) unsupported\n",
  538. filter[i].code, i);
  539. return -ENOTSUPP;
  540. }
  541. }
  542. /* Set end-of-body-code address for exit. */
  543. addrs[i] = ctx->idx * 4;
  544. return 0;
  545. }
  546. void bpf_jit_compile(struct sk_filter *fp)
  547. {
  548. unsigned int proglen;
  549. unsigned int alloclen;
  550. u32 *image = NULL;
  551. u32 *code_base;
  552. unsigned int *addrs;
  553. struct codegen_context cgctx;
  554. int pass;
  555. int flen = fp->len;
  556. if (!bpf_jit_enable)
  557. return;
  558. addrs = kzalloc((flen+1) * sizeof(*addrs), GFP_KERNEL);
  559. if (addrs == NULL)
  560. return;
  561. /*
  562. * There are multiple assembly passes as the generated code will change
  563. * size as it settles down, figuring out the max branch offsets/exit
  564. * paths required.
  565. *
  566. * The range of standard conditional branches is +/- 32Kbytes. Since
  567. * BPF_MAXINSNS = 4096, we can only jump from (worst case) start to
  568. * finish with 8 bytes/instruction. Not feasible, so long jumps are
  569. * used, distinct from short branches.
  570. *
  571. * Current:
  572. *
  573. * For now, both branch types assemble to 2 words (short branches padded
  574. * with a NOP); this is less efficient, but assembly will always complete
  575. * after exactly 3 passes:
  576. *
  577. * First pass: No code buffer; Program is "faux-generated" -- no code
  578. * emitted but maximum size of output determined (and addrs[] filled
  579. * in). Also, we note whether we use M[], whether we use skb data, etc.
  580. * All generation choices assumed to be 'worst-case', e.g. branches all
  581. * far (2 instructions), return path code reduction not available, etc.
  582. *
  583. * Second pass: Code buffer allocated with size determined previously.
  584. * Prologue generated to support features we have seen used. Exit paths
  585. * determined and addrs[] is filled in again, as code may be slightly
  586. * smaller as a result.
  587. *
  588. * Third pass: Code generated 'for real', and branch destinations
  589. * determined from now-accurate addrs[] map.
  590. *
  591. * Ideal:
  592. *
  593. * If we optimise this, near branches will be shorter. On the
  594. * first assembly pass, we should err on the side of caution and
  595. * generate the biggest code. On subsequent passes, branches will be
  596. * generated short or long and code size will reduce. With smaller
  597. * code, more branches may fall into the short category, and code will
  598. * reduce more.
  599. *
  600. * Finally, if we see one pass generate code the same size as the
  601. * previous pass we have converged and should now generate code for
  602. * real. Allocating at the end will also save the memory that would
  603. * otherwise be wasted by the (small) current code shrinkage.
  604. * Preferably, we should do a small number of passes (e.g. 5) and if we
  605. * haven't converged by then, get impatient and force code to generate
  606. * as-is, even if the odd branch would be left long. The chances of a
  607. * long jump are tiny with all but the most enormous of BPF filter
  608. * inputs, so we should usually converge on the third pass.
  609. */
  610. cgctx.idx = 0;
  611. cgctx.seen = 0;
  612. cgctx.pc_ret0 = -1;
  613. /* Scouting faux-generate pass 0 */
  614. if (bpf_jit_build_body(fp, 0, &cgctx, addrs))
  615. /* We hit something illegal or unsupported. */
  616. goto out;
  617. /*
  618. * Pretend to build prologue, given the features we've seen. This will
  619. * update ctgtx.idx as it pretends to output instructions, then we can
  620. * calculate total size from idx.
  621. */
  622. bpf_jit_build_prologue(fp, 0, &cgctx);
  623. bpf_jit_build_epilogue(0, &cgctx);
  624. proglen = cgctx.idx * 4;
  625. alloclen = proglen + FUNCTION_DESCR_SIZE;
  626. image = module_alloc(alloclen);
  627. if (!image)
  628. goto out;
  629. code_base = image + (FUNCTION_DESCR_SIZE/4);
  630. /* Code generation passes 1-2 */
  631. for (pass = 1; pass < 3; pass++) {
  632. /* Now build the prologue, body code & epilogue for real. */
  633. cgctx.idx = 0;
  634. bpf_jit_build_prologue(fp, code_base, &cgctx);
  635. bpf_jit_build_body(fp, code_base, &cgctx, addrs);
  636. bpf_jit_build_epilogue(code_base, &cgctx);
  637. if (bpf_jit_enable > 1)
  638. pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
  639. proglen - (cgctx.idx * 4), cgctx.seen);
  640. }
  641. if (bpf_jit_enable > 1)
  642. /* Note that we output the base address of the code_base
  643. * rather than image, since opcodes are in code_base.
  644. */
  645. bpf_jit_dump(flen, proglen, pass, code_base);
  646. if (image) {
  647. bpf_flush_icache(code_base, code_base + (proglen/4));
  648. /* Function descriptor nastiness: Address + TOC */
  649. ((u64 *)image)[0] = (u64)code_base;
  650. ((u64 *)image)[1] = local_paca->kernel_toc;
  651. fp->bpf_func = (void *)image;
  652. }
  653. out:
  654. kfree(addrs);
  655. return;
  656. }
  657. void bpf_jit_free(struct sk_filter *fp)
  658. {
  659. if (fp->bpf_func != sk_run_filter)
  660. module_free(NULL, fp->bpf_func);
  661. kfree(fp);
  662. }