bpf_jit_comp.c 19 KB

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