bpf_jit_comp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 = reciprocal_divide(A, K); */
  209. PPC_LI32(r_scratch1, K);
  210. /* Top 32 bits of 64bit result -> A */
  211. PPC_MULHWU(r_A, r_A, r_scratch1);
  212. break;
  213. case BPF_S_ALU_AND_X:
  214. ctx->seen |= SEEN_XREG;
  215. PPC_AND(r_A, r_A, r_X);
  216. break;
  217. case BPF_S_ALU_AND_K:
  218. if (!IMM_H(K))
  219. PPC_ANDI(r_A, r_A, K);
  220. else {
  221. PPC_LI32(r_scratch1, K);
  222. PPC_AND(r_A, r_A, r_scratch1);
  223. }
  224. break;
  225. case BPF_S_ALU_OR_X:
  226. ctx->seen |= SEEN_XREG;
  227. PPC_OR(r_A, r_A, r_X);
  228. break;
  229. case BPF_S_ALU_OR_K:
  230. if (IMM_L(K))
  231. PPC_ORI(r_A, r_A, IMM_L(K));
  232. if (K >= 65536)
  233. PPC_ORIS(r_A, r_A, IMM_H(K));
  234. break;
  235. case BPF_S_ANC_ALU_XOR_X:
  236. case BPF_S_ALU_XOR_X: /* A ^= X */
  237. ctx->seen |= SEEN_XREG;
  238. PPC_XOR(r_A, r_A, r_X);
  239. break;
  240. case BPF_S_ALU_XOR_K: /* A ^= K */
  241. if (IMM_L(K))
  242. PPC_XORI(r_A, r_A, IMM_L(K));
  243. if (K >= 65536)
  244. PPC_XORIS(r_A, r_A, IMM_H(K));
  245. break;
  246. case BPF_S_ALU_LSH_X: /* A <<= X; */
  247. ctx->seen |= SEEN_XREG;
  248. PPC_SLW(r_A, r_A, r_X);
  249. break;
  250. case BPF_S_ALU_LSH_K:
  251. if (K == 0)
  252. break;
  253. else
  254. PPC_SLWI(r_A, r_A, K);
  255. break;
  256. case BPF_S_ALU_RSH_X: /* A >>= X; */
  257. ctx->seen |= SEEN_XREG;
  258. PPC_SRW(r_A, r_A, r_X);
  259. break;
  260. case BPF_S_ALU_RSH_K: /* A >>= K; */
  261. if (K == 0)
  262. break;
  263. else
  264. PPC_SRWI(r_A, r_A, K);
  265. break;
  266. case BPF_S_ALU_NEG:
  267. PPC_NEG(r_A, r_A);
  268. break;
  269. case BPF_S_RET_K:
  270. PPC_LI32(r_ret, K);
  271. if (!K) {
  272. if (ctx->pc_ret0 == -1)
  273. ctx->pc_ret0 = i;
  274. }
  275. /*
  276. * If this isn't the very last instruction, branch to
  277. * the epilogue if we've stuff to clean up. Otherwise,
  278. * if there's nothing to tidy, just return. If we /are/
  279. * the last instruction, we're about to fall through to
  280. * the epilogue to return.
  281. */
  282. if (i != flen - 1) {
  283. /*
  284. * Note: 'seen' is properly valid only on pass
  285. * #2. Both parts of this conditional are the
  286. * same instruction size though, meaning the
  287. * first pass will still correctly determine the
  288. * code size/addresses.
  289. */
  290. if (ctx->seen)
  291. PPC_JMP(exit_addr);
  292. else
  293. PPC_BLR();
  294. }
  295. break;
  296. case BPF_S_RET_A:
  297. PPC_MR(r_ret, r_A);
  298. if (i != flen - 1) {
  299. if (ctx->seen)
  300. PPC_JMP(exit_addr);
  301. else
  302. PPC_BLR();
  303. }
  304. break;
  305. case BPF_S_MISC_TAX: /* X = A */
  306. PPC_MR(r_X, r_A);
  307. break;
  308. case BPF_S_MISC_TXA: /* A = X */
  309. ctx->seen |= SEEN_XREG;
  310. PPC_MR(r_A, r_X);
  311. break;
  312. /*** Constant loads/M[] access ***/
  313. case BPF_S_LD_IMM: /* A = K */
  314. PPC_LI32(r_A, K);
  315. break;
  316. case BPF_S_LDX_IMM: /* X = K */
  317. PPC_LI32(r_X, K);
  318. break;
  319. case BPF_S_LD_MEM: /* A = mem[K] */
  320. PPC_MR(r_A, r_M + (K & 0xf));
  321. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  322. break;
  323. case BPF_S_LDX_MEM: /* X = mem[K] */
  324. PPC_MR(r_X, r_M + (K & 0xf));
  325. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  326. break;
  327. case BPF_S_ST: /* mem[K] = A */
  328. PPC_MR(r_M + (K & 0xf), r_A);
  329. ctx->seen |= SEEN_MEM | (1<<(K & 0xf));
  330. break;
  331. case BPF_S_STX: /* mem[K] = X */
  332. PPC_MR(r_M + (K & 0xf), r_X);
  333. ctx->seen |= SEEN_XREG | SEEN_MEM | (1<<(K & 0xf));
  334. break;
  335. case BPF_S_LD_W_LEN: /* A = skb->len; */
  336. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
  337. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff, len));
  338. break;
  339. case BPF_S_LDX_W_LEN: /* X = skb->len; */
  340. PPC_LWZ_OFFS(r_X, r_skb, offsetof(struct sk_buff, len));
  341. break;
  342. /*** Ancillary info loads ***/
  343. case BPF_S_ANC_PROTOCOL: /* A = ntohs(skb->protocol); */
  344. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  345. protocol) != 2);
  346. PPC_NTOHS_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  347. protocol));
  348. break;
  349. case BPF_S_ANC_IFINDEX:
  350. PPC_LD_OFFS(r_scratch1, r_skb, offsetof(struct sk_buff,
  351. dev));
  352. PPC_CMPDI(r_scratch1, 0);
  353. if (ctx->pc_ret0 != -1) {
  354. PPC_BCC(COND_EQ, addrs[ctx->pc_ret0]);
  355. } else {
  356. /* Exit, returning 0; first pass hits here. */
  357. PPC_BCC_SHORT(COND_NE, (ctx->idx*4)+12);
  358. PPC_LI(r_ret, 0);
  359. PPC_JMP(exit_addr);
  360. }
  361. BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
  362. ifindex) != 4);
  363. PPC_LWZ_OFFS(r_A, r_scratch1,
  364. offsetof(struct net_device, ifindex));
  365. break;
  366. case BPF_S_ANC_MARK:
  367. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
  368. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  369. mark));
  370. break;
  371. case BPF_S_ANC_RXHASH:
  372. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, rxhash) != 4);
  373. PPC_LWZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  374. rxhash));
  375. break;
  376. case BPF_S_ANC_VLAN_TAG:
  377. case BPF_S_ANC_VLAN_TAG_PRESENT:
  378. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
  379. PPC_LHZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  380. vlan_tci));
  381. if (filter[i].code == BPF_S_ANC_VLAN_TAG)
  382. PPC_ANDI(r_A, r_A, VLAN_VID_MASK);
  383. else
  384. PPC_ANDI(r_A, r_A, VLAN_TAG_PRESENT);
  385. break;
  386. case BPF_S_ANC_QUEUE:
  387. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  388. queue_mapping) != 2);
  389. PPC_LHZ_OFFS(r_A, r_skb, offsetof(struct sk_buff,
  390. queue_mapping));
  391. break;
  392. case BPF_S_ANC_CPU:
  393. #ifdef CONFIG_SMP
  394. /*
  395. * PACA ptr is r13:
  396. * raw_smp_processor_id() = local_paca->paca_index
  397. */
  398. BUILD_BUG_ON(FIELD_SIZEOF(struct paca_struct,
  399. paca_index) != 2);
  400. PPC_LHZ_OFFS(r_A, 13,
  401. offsetof(struct paca_struct, paca_index));
  402. #else
  403. PPC_LI(r_A, 0);
  404. #endif
  405. break;
  406. /*** Absolute loads from packet header/data ***/
  407. case BPF_S_LD_W_ABS:
  408. func = CHOOSE_LOAD_FUNC(K, sk_load_word);
  409. goto common_load;
  410. case BPF_S_LD_H_ABS:
  411. func = CHOOSE_LOAD_FUNC(K, sk_load_half);
  412. goto common_load;
  413. case BPF_S_LD_B_ABS:
  414. func = CHOOSE_LOAD_FUNC(K, sk_load_byte);
  415. common_load:
  416. /* Load from [K]. */
  417. ctx->seen |= SEEN_DATAREF;
  418. PPC_LI64(r_scratch1, func);
  419. PPC_MTLR(r_scratch1);
  420. PPC_LI32(r_addr, K);
  421. PPC_BLRL();
  422. /*
  423. * Helper returns 'lt' condition on error, and an
  424. * appropriate return value in r3
  425. */
  426. PPC_BCC(COND_LT, exit_addr);
  427. break;
  428. /*** Indirect loads from packet header/data ***/
  429. case BPF_S_LD_W_IND:
  430. func = sk_load_word;
  431. goto common_load_ind;
  432. case BPF_S_LD_H_IND:
  433. func = sk_load_half;
  434. goto common_load_ind;
  435. case BPF_S_LD_B_IND:
  436. func = sk_load_byte;
  437. common_load_ind:
  438. /*
  439. * Load from [X + K]. Negative offsets are tested for
  440. * in the helper functions.
  441. */
  442. ctx->seen |= SEEN_DATAREF | SEEN_XREG;
  443. PPC_LI64(r_scratch1, func);
  444. PPC_MTLR(r_scratch1);
  445. PPC_ADDI(r_addr, r_X, IMM_L(K));
  446. if (K >= 32768)
  447. PPC_ADDIS(r_addr, r_addr, IMM_HA(K));
  448. PPC_BLRL();
  449. /* If error, cr0.LT set */
  450. PPC_BCC(COND_LT, exit_addr);
  451. break;
  452. case BPF_S_LDX_B_MSH:
  453. func = CHOOSE_LOAD_FUNC(K, sk_load_byte_msh);
  454. goto common_load;
  455. break;
  456. /*** Jump and branches ***/
  457. case BPF_S_JMP_JA:
  458. if (K != 0)
  459. PPC_JMP(addrs[i + 1 + K]);
  460. break;
  461. case BPF_S_JMP_JGT_K:
  462. case BPF_S_JMP_JGT_X:
  463. true_cond = COND_GT;
  464. goto cond_branch;
  465. case BPF_S_JMP_JGE_K:
  466. case BPF_S_JMP_JGE_X:
  467. true_cond = COND_GE;
  468. goto cond_branch;
  469. case BPF_S_JMP_JEQ_K:
  470. case BPF_S_JMP_JEQ_X:
  471. true_cond = COND_EQ;
  472. goto cond_branch;
  473. case BPF_S_JMP_JSET_K:
  474. case BPF_S_JMP_JSET_X:
  475. true_cond = COND_NE;
  476. /* Fall through */
  477. cond_branch:
  478. /* same targets, can avoid doing the test :) */
  479. if (filter[i].jt == filter[i].jf) {
  480. if (filter[i].jt > 0)
  481. PPC_JMP(addrs[i + 1 + filter[i].jt]);
  482. break;
  483. }
  484. switch (filter[i].code) {
  485. case BPF_S_JMP_JGT_X:
  486. case BPF_S_JMP_JGE_X:
  487. case BPF_S_JMP_JEQ_X:
  488. ctx->seen |= SEEN_XREG;
  489. PPC_CMPLW(r_A, r_X);
  490. break;
  491. case BPF_S_JMP_JSET_X:
  492. ctx->seen |= SEEN_XREG;
  493. PPC_AND_DOT(r_scratch1, r_A, r_X);
  494. break;
  495. case BPF_S_JMP_JEQ_K:
  496. case BPF_S_JMP_JGT_K:
  497. case BPF_S_JMP_JGE_K:
  498. if (K < 32768)
  499. PPC_CMPLWI(r_A, K);
  500. else {
  501. PPC_LI32(r_scratch1, K);
  502. PPC_CMPLW(r_A, r_scratch1);
  503. }
  504. break;
  505. case BPF_S_JMP_JSET_K:
  506. if (K < 32768)
  507. /* PPC_ANDI is /only/ dot-form */
  508. PPC_ANDI(r_scratch1, r_A, K);
  509. else {
  510. PPC_LI32(r_scratch1, K);
  511. PPC_AND_DOT(r_scratch1, r_A,
  512. r_scratch1);
  513. }
  514. break;
  515. }
  516. /* Sometimes branches are constructed "backward", with
  517. * the false path being the branch and true path being
  518. * a fallthrough to the next instruction.
  519. */
  520. if (filter[i].jt == 0)
  521. /* Swap the sense of the branch */
  522. PPC_BCC(true_cond ^ COND_CMP_TRUE,
  523. addrs[i + 1 + filter[i].jf]);
  524. else {
  525. PPC_BCC(true_cond, addrs[i + 1 + filter[i].jt]);
  526. if (filter[i].jf != 0)
  527. PPC_JMP(addrs[i + 1 + filter[i].jf]);
  528. }
  529. break;
  530. default:
  531. /* The filter contains something cruel & unusual.
  532. * We don't handle it, but also there shouldn't be
  533. * anything missing from our list.
  534. */
  535. if (printk_ratelimit())
  536. pr_err("BPF filter opcode %04x (@%d) unsupported\n",
  537. filter[i].code, i);
  538. return -ENOTSUPP;
  539. }
  540. }
  541. /* Set end-of-body-code address for exit. */
  542. addrs[i] = ctx->idx * 4;
  543. return 0;
  544. }
  545. void bpf_jit_compile(struct sk_filter *fp)
  546. {
  547. unsigned int proglen;
  548. unsigned int alloclen;
  549. u32 *image = NULL;
  550. u32 *code_base;
  551. unsigned int *addrs;
  552. struct codegen_context cgctx;
  553. int pass;
  554. int flen = fp->len;
  555. if (!bpf_jit_enable)
  556. return;
  557. addrs = kzalloc((flen+1) * sizeof(*addrs), GFP_KERNEL);
  558. if (addrs == NULL)
  559. return;
  560. /*
  561. * There are multiple assembly passes as the generated code will change
  562. * size as it settles down, figuring out the max branch offsets/exit
  563. * paths required.
  564. *
  565. * The range of standard conditional branches is +/- 32Kbytes. Since
  566. * BPF_MAXINSNS = 4096, we can only jump from (worst case) start to
  567. * finish with 8 bytes/instruction. Not feasible, so long jumps are
  568. * used, distinct from short branches.
  569. *
  570. * Current:
  571. *
  572. * For now, both branch types assemble to 2 words (short branches padded
  573. * with a NOP); this is less efficient, but assembly will always complete
  574. * after exactly 3 passes:
  575. *
  576. * First pass: No code buffer; Program is "faux-generated" -- no code
  577. * emitted but maximum size of output determined (and addrs[] filled
  578. * in). Also, we note whether we use M[], whether we use skb data, etc.
  579. * All generation choices assumed to be 'worst-case', e.g. branches all
  580. * far (2 instructions), return path code reduction not available, etc.
  581. *
  582. * Second pass: Code buffer allocated with size determined previously.
  583. * Prologue generated to support features we have seen used. Exit paths
  584. * determined and addrs[] is filled in again, as code may be slightly
  585. * smaller as a result.
  586. *
  587. * Third pass: Code generated 'for real', and branch destinations
  588. * determined from now-accurate addrs[] map.
  589. *
  590. * Ideal:
  591. *
  592. * If we optimise this, near branches will be shorter. On the
  593. * first assembly pass, we should err on the side of caution and
  594. * generate the biggest code. On subsequent passes, branches will be
  595. * generated short or long and code size will reduce. With smaller
  596. * code, more branches may fall into the short category, and code will
  597. * reduce more.
  598. *
  599. * Finally, if we see one pass generate code the same size as the
  600. * previous pass we have converged and should now generate code for
  601. * real. Allocating at the end will also save the memory that would
  602. * otherwise be wasted by the (small) current code shrinkage.
  603. * Preferably, we should do a small number of passes (e.g. 5) and if we
  604. * haven't converged by then, get impatient and force code to generate
  605. * as-is, even if the odd branch would be left long. The chances of a
  606. * long jump are tiny with all but the most enormous of BPF filter
  607. * inputs, so we should usually converge on the third pass.
  608. */
  609. cgctx.idx = 0;
  610. cgctx.seen = 0;
  611. cgctx.pc_ret0 = -1;
  612. /* Scouting faux-generate pass 0 */
  613. if (bpf_jit_build_body(fp, 0, &cgctx, addrs))
  614. /* We hit something illegal or unsupported. */
  615. goto out;
  616. /*
  617. * Pretend to build prologue, given the features we've seen. This will
  618. * update ctgtx.idx as it pretends to output instructions, then we can
  619. * calculate total size from idx.
  620. */
  621. bpf_jit_build_prologue(fp, 0, &cgctx);
  622. bpf_jit_build_epilogue(0, &cgctx);
  623. proglen = cgctx.idx * 4;
  624. alloclen = proglen + FUNCTION_DESCR_SIZE;
  625. image = module_alloc(alloclen);
  626. if (!image)
  627. goto out;
  628. code_base = image + (FUNCTION_DESCR_SIZE/4);
  629. /* Code generation passes 1-2 */
  630. for (pass = 1; pass < 3; pass++) {
  631. /* Now build the prologue, body code & epilogue for real. */
  632. cgctx.idx = 0;
  633. bpf_jit_build_prologue(fp, code_base, &cgctx);
  634. bpf_jit_build_body(fp, code_base, &cgctx, addrs);
  635. bpf_jit_build_epilogue(code_base, &cgctx);
  636. if (bpf_jit_enable > 1)
  637. pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
  638. proglen - (cgctx.idx * 4), cgctx.seen);
  639. }
  640. if (bpf_jit_enable > 1)
  641. /* Note that we output the base address of the code_base
  642. * rather than image, since opcodes are in code_base.
  643. */
  644. bpf_jit_dump(flen, proglen, pass, code_base);
  645. if (image) {
  646. bpf_flush_icache(code_base, code_base + (proglen/4));
  647. /* Function descriptor nastiness: Address + TOC */
  648. ((u64 *)image)[0] = (u64)code_base;
  649. ((u64 *)image)[1] = local_paca->kernel_toc;
  650. fp->bpf_func = (void *)image;
  651. }
  652. out:
  653. kfree(addrs);
  654. return;
  655. }
  656. void bpf_jit_free(struct sk_filter *fp)
  657. {
  658. if (fp->bpf_func != sk_run_filter)
  659. module_free(NULL, fp->bpf_func);
  660. kfree(fp);
  661. }