bpf_jit_comp.c 19 KB

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