bpf_jit_comp.c 19 KB

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