bpf_jit_comp.c 19 KB

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