bpf_jit_comp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 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_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 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_LD_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. #ifdef CONFIG_SMP
  393. /*
  394. * PACA ptr is r13:
  395. * raw_smp_processor_id() = local_paca->paca_index
  396. */
  397. BUILD_BUG_ON(FIELD_SIZEOF(struct paca_struct,
  398. paca_index) != 2);
  399. PPC_LHZ_OFFS(r_A, 13,
  400. offsetof(struct paca_struct, paca_index));
  401. #else
  402. PPC_LI(r_A, 0);
  403. #endif
  404. break;
  405. /*** Absolute loads from packet header/data ***/
  406. case BPF_LD | BPF_W | BPF_ABS:
  407. func = CHOOSE_LOAD_FUNC(K, sk_load_word);
  408. goto common_load;
  409. case BPF_LD | BPF_H | BPF_ABS:
  410. func = CHOOSE_LOAD_FUNC(K, sk_load_half);
  411. goto common_load;
  412. case BPF_LD | BPF_B | BPF_ABS:
  413. func = CHOOSE_LOAD_FUNC(K, sk_load_byte);
  414. common_load:
  415. /* Load from [K]. */
  416. ctx->seen |= SEEN_DATAREF;
  417. PPC_LI64(r_scratch1, func);
  418. PPC_MTLR(r_scratch1);
  419. PPC_LI32(r_addr, K);
  420. PPC_BLRL();
  421. /*
  422. * Helper returns 'lt' condition on error, and an
  423. * appropriate return value in r3
  424. */
  425. PPC_BCC(COND_LT, exit_addr);
  426. break;
  427. /*** Indirect loads from packet header/data ***/
  428. case BPF_LD | BPF_W | BPF_IND:
  429. func = sk_load_word;
  430. goto common_load_ind;
  431. case BPF_LD | BPF_H | BPF_IND:
  432. func = sk_load_half;
  433. goto common_load_ind;
  434. case BPF_LD | BPF_B | BPF_IND:
  435. func = sk_load_byte;
  436. common_load_ind:
  437. /*
  438. * Load from [X + K]. Negative offsets are tested for
  439. * in the helper functions.
  440. */
  441. ctx->seen |= SEEN_DATAREF | SEEN_XREG;
  442. PPC_LI64(r_scratch1, func);
  443. PPC_MTLR(r_scratch1);
  444. PPC_ADDI(r_addr, r_X, IMM_L(K));
  445. if (K >= 32768)
  446. PPC_ADDIS(r_addr, r_addr, IMM_HA(K));
  447. PPC_BLRL();
  448. /* If error, cr0.LT set */
  449. PPC_BCC(COND_LT, exit_addr);
  450. break;
  451. case BPF_LDX | BPF_B | BPF_MSH:
  452. func = CHOOSE_LOAD_FUNC(K, sk_load_byte_msh);
  453. goto common_load;
  454. break;
  455. /*** Jump and branches ***/
  456. case BPF_JMP | BPF_JA:
  457. if (K != 0)
  458. PPC_JMP(addrs[i + 1 + K]);
  459. break;
  460. case BPF_JMP | BPF_JGT | BPF_K:
  461. case BPF_JMP | BPF_JGT | BPF_X:
  462. true_cond = COND_GT;
  463. goto cond_branch;
  464. case BPF_JMP | BPF_JGE | BPF_K:
  465. case BPF_JMP | BPF_JGE | BPF_X:
  466. true_cond = COND_GE;
  467. goto cond_branch;
  468. case BPF_JMP | BPF_JEQ | BPF_K:
  469. case BPF_JMP | BPF_JEQ | BPF_X:
  470. true_cond = COND_EQ;
  471. goto cond_branch;
  472. case BPF_JMP | BPF_JSET | BPF_K:
  473. case BPF_JMP | BPF_JSET | BPF_X:
  474. true_cond = COND_NE;
  475. /* Fall through */
  476. cond_branch:
  477. /* same targets, can avoid doing the test :) */
  478. if (filter[i].jt == filter[i].jf) {
  479. if (filter[i].jt > 0)
  480. PPC_JMP(addrs[i + 1 + filter[i].jt]);
  481. break;
  482. }
  483. switch (code) {
  484. case BPF_JMP | BPF_JGT | BPF_X:
  485. case BPF_JMP | BPF_JGE | BPF_X:
  486. case BPF_JMP | BPF_JEQ | BPF_X:
  487. ctx->seen |= SEEN_XREG;
  488. PPC_CMPLW(r_A, r_X);
  489. break;
  490. case BPF_JMP | BPF_JSET | BPF_X:
  491. ctx->seen |= SEEN_XREG;
  492. PPC_AND_DOT(r_scratch1, r_A, r_X);
  493. break;
  494. case BPF_JMP | BPF_JEQ | BPF_K:
  495. case BPF_JMP | BPF_JGT | BPF_K:
  496. case BPF_JMP | BPF_JGE | BPF_K:
  497. if (K < 32768)
  498. PPC_CMPLWI(r_A, K);
  499. else {
  500. PPC_LI32(r_scratch1, K);
  501. PPC_CMPLW(r_A, r_scratch1);
  502. }
  503. break;
  504. case BPF_JMP | BPF_JSET | BPF_K:
  505. if (K < 32768)
  506. /* PPC_ANDI is /only/ dot-form */
  507. PPC_ANDI(r_scratch1, r_A, K);
  508. else {
  509. PPC_LI32(r_scratch1, K);
  510. PPC_AND_DOT(r_scratch1, r_A,
  511. r_scratch1);
  512. }
  513. break;
  514. }
  515. /* Sometimes branches are constructed "backward", with
  516. * the false path being the branch and true path being
  517. * a fallthrough to the next instruction.
  518. */
  519. if (filter[i].jt == 0)
  520. /* Swap the sense of the branch */
  521. PPC_BCC(true_cond ^ COND_CMP_TRUE,
  522. addrs[i + 1 + filter[i].jf]);
  523. else {
  524. PPC_BCC(true_cond, addrs[i + 1 + filter[i].jt]);
  525. if (filter[i].jf != 0)
  526. PPC_JMP(addrs[i + 1 + filter[i].jf]);
  527. }
  528. break;
  529. default:
  530. /* The filter contains something cruel & unusual.
  531. * We don't handle it, but also there shouldn't be
  532. * anything missing from our list.
  533. */
  534. if (printk_ratelimit())
  535. pr_err("BPF filter opcode %04x (@%d) unsupported\n",
  536. filter[i].code, i);
  537. return -ENOTSUPP;
  538. }
  539. }
  540. /* Set end-of-body-code address for exit. */
  541. addrs[i] = ctx->idx * 4;
  542. return 0;
  543. }
  544. void bpf_jit_compile(struct bpf_prog *fp)
  545. {
  546. unsigned int proglen;
  547. unsigned int alloclen;
  548. u32 *image = NULL;
  549. u32 *code_base;
  550. unsigned int *addrs;
  551. struct codegen_context cgctx;
  552. int pass;
  553. int flen = fp->len;
  554. if (!bpf_jit_enable)
  555. return;
  556. addrs = kzalloc((flen+1) * sizeof(*addrs), GFP_KERNEL);
  557. if (addrs == NULL)
  558. return;
  559. /*
  560. * There are multiple assembly passes as the generated code will change
  561. * size as it settles down, figuring out the max branch offsets/exit
  562. * paths required.
  563. *
  564. * The range of standard conditional branches is +/- 32Kbytes. Since
  565. * BPF_MAXINSNS = 4096, we can only jump from (worst case) start to
  566. * finish with 8 bytes/instruction. Not feasible, so long jumps are
  567. * used, distinct from short branches.
  568. *
  569. * Current:
  570. *
  571. * For now, both branch types assemble to 2 words (short branches padded
  572. * with a NOP); this is less efficient, but assembly will always complete
  573. * after exactly 3 passes:
  574. *
  575. * First pass: No code buffer; Program is "faux-generated" -- no code
  576. * emitted but maximum size of output determined (and addrs[] filled
  577. * in). Also, we note whether we use M[], whether we use skb data, etc.
  578. * All generation choices assumed to be 'worst-case', e.g. branches all
  579. * far (2 instructions), return path code reduction not available, etc.
  580. *
  581. * Second pass: Code buffer allocated with size determined previously.
  582. * Prologue generated to support features we have seen used. Exit paths
  583. * determined and addrs[] is filled in again, as code may be slightly
  584. * smaller as a result.
  585. *
  586. * Third pass: Code generated 'for real', and branch destinations
  587. * determined from now-accurate addrs[] map.
  588. *
  589. * Ideal:
  590. *
  591. * If we optimise this, near branches will be shorter. On the
  592. * first assembly pass, we should err on the side of caution and
  593. * generate the biggest code. On subsequent passes, branches will be
  594. * generated short or long and code size will reduce. With smaller
  595. * code, more branches may fall into the short category, and code will
  596. * reduce more.
  597. *
  598. * Finally, if we see one pass generate code the same size as the
  599. * previous pass we have converged and should now generate code for
  600. * real. Allocating at the end will also save the memory that would
  601. * otherwise be wasted by the (small) current code shrinkage.
  602. * Preferably, we should do a small number of passes (e.g. 5) and if we
  603. * haven't converged by then, get impatient and force code to generate
  604. * as-is, even if the odd branch would be left long. The chances of a
  605. * long jump are tiny with all but the most enormous of BPF filter
  606. * inputs, so we should usually converge on the third pass.
  607. */
  608. cgctx.idx = 0;
  609. cgctx.seen = 0;
  610. cgctx.pc_ret0 = -1;
  611. /* Scouting faux-generate pass 0 */
  612. if (bpf_jit_build_body(fp, 0, &cgctx, addrs))
  613. /* We hit something illegal or unsupported. */
  614. goto out;
  615. /*
  616. * Pretend to build prologue, given the features we've seen. This will
  617. * update ctgtx.idx as it pretends to output instructions, then we can
  618. * calculate total size from idx.
  619. */
  620. bpf_jit_build_prologue(fp, 0, &cgctx);
  621. bpf_jit_build_epilogue(0, &cgctx);
  622. proglen = cgctx.idx * 4;
  623. alloclen = proglen + FUNCTION_DESCR_SIZE;
  624. image = module_alloc(alloclen);
  625. if (!image)
  626. goto out;
  627. code_base = image + (FUNCTION_DESCR_SIZE/4);
  628. /* Code generation passes 1-2 */
  629. for (pass = 1; pass < 3; pass++) {
  630. /* Now build the prologue, body code & epilogue for real. */
  631. cgctx.idx = 0;
  632. bpf_jit_build_prologue(fp, code_base, &cgctx);
  633. bpf_jit_build_body(fp, code_base, &cgctx, addrs);
  634. bpf_jit_build_epilogue(code_base, &cgctx);
  635. if (bpf_jit_enable > 1)
  636. pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
  637. proglen - (cgctx.idx * 4), cgctx.seen);
  638. }
  639. if (bpf_jit_enable > 1)
  640. /* Note that we output the base address of the code_base
  641. * rather than image, since opcodes are in code_base.
  642. */
  643. bpf_jit_dump(flen, proglen, pass, code_base);
  644. if (image) {
  645. bpf_flush_icache(code_base, code_base + (proglen/4));
  646. /* Function descriptor nastiness: Address + TOC */
  647. ((u64 *)image)[0] = (u64)code_base;
  648. ((u64 *)image)[1] = local_paca->kernel_toc;
  649. fp->bpf_func = (void *)image;
  650. fp->jited = true;
  651. }
  652. out:
  653. kfree(addrs);
  654. return;
  655. }
  656. void bpf_jit_free(struct bpf_prog *fp)
  657. {
  658. if (fp->jited)
  659. module_free(NULL, fp->bpf_func);
  660. bpf_prog_unlock_free(fp);
  661. }