bpf_jit_comp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. #include <linux/moduleloader.h>
  2. #include <linux/workqueue.h>
  3. #include <linux/netdevice.h>
  4. #include <linux/filter.h>
  5. #include <linux/cache.h>
  6. #include <linux/if_vlan.h>
  7. #include <asm/cacheflush.h>
  8. #include <asm/ptrace.h>
  9. #include "bpf_jit.h"
  10. static inline bool is_simm13(unsigned int value)
  11. {
  12. return value + 0x1000 < 0x2000;
  13. }
  14. static void bpf_flush_icache(void *start_, void *end_)
  15. {
  16. #ifdef CONFIG_SPARC64
  17. /* Cheetah's I-cache is fully coherent. */
  18. if (tlb_type == spitfire) {
  19. unsigned long start = (unsigned long) start_;
  20. unsigned long end = (unsigned long) end_;
  21. start &= ~7UL;
  22. end = (end + 7UL) & ~7UL;
  23. while (start < end) {
  24. flushi(start);
  25. start += 32;
  26. }
  27. }
  28. #endif
  29. }
  30. #define SEEN_DATAREF 1 /* might call external helpers */
  31. #define SEEN_XREG 2 /* ebx is used */
  32. #define SEEN_MEM 4 /* use mem[] for temporary storage */
  33. #define S13(X) ((X) & 0x1fff)
  34. #define IMMED 0x00002000
  35. #define RD(X) ((X) << 25)
  36. #define RS1(X) ((X) << 14)
  37. #define RS2(X) ((X))
  38. #define OP(X) ((X) << 30)
  39. #define OP2(X) ((X) << 22)
  40. #define OP3(X) ((X) << 19)
  41. #define COND(X) ((X) << 25)
  42. #define F1(X) OP(X)
  43. #define F2(X, Y) (OP(X) | OP2(Y))
  44. #define F3(X, Y) (OP(X) | OP3(Y))
  45. #define CONDN COND(0x0)
  46. #define CONDE COND(0x1)
  47. #define CONDLE COND(0x2)
  48. #define CONDL COND(0x3)
  49. #define CONDLEU COND(0x4)
  50. #define CONDCS COND(0x5)
  51. #define CONDNEG COND(0x6)
  52. #define CONDVC COND(0x7)
  53. #define CONDA COND(0x8)
  54. #define CONDNE COND(0x9)
  55. #define CONDG COND(0xa)
  56. #define CONDGE COND(0xb)
  57. #define CONDGU COND(0xc)
  58. #define CONDCC COND(0xd)
  59. #define CONDPOS COND(0xe)
  60. #define CONDVS COND(0xf)
  61. #define CONDGEU CONDCC
  62. #define CONDLU CONDCS
  63. #define WDISP22(X) (((X) >> 2) & 0x3fffff)
  64. #define BA (F2(0, 2) | CONDA)
  65. #define BGU (F2(0, 2) | CONDGU)
  66. #define BLEU (F2(0, 2) | CONDLEU)
  67. #define BGEU (F2(0, 2) | CONDGEU)
  68. #define BLU (F2(0, 2) | CONDLU)
  69. #define BE (F2(0, 2) | CONDE)
  70. #define BNE (F2(0, 2) | CONDNE)
  71. #ifdef CONFIG_SPARC64
  72. #define BE_PTR (F2(0, 1) | CONDE | (2 << 20))
  73. #else
  74. #define BE_PTR BE
  75. #endif
  76. #define SETHI(K, REG) \
  77. (F2(0, 0x4) | RD(REG) | (((K) >> 10) & 0x3fffff))
  78. #define OR_LO(K, REG) \
  79. (F3(2, 0x02) | IMMED | RS1(REG) | ((K) & 0x3ff) | RD(REG))
  80. #define ADD F3(2, 0x00)
  81. #define AND F3(2, 0x01)
  82. #define ANDCC F3(2, 0x11)
  83. #define OR F3(2, 0x02)
  84. #define XOR F3(2, 0x03)
  85. #define SUB F3(2, 0x04)
  86. #define SUBCC F3(2, 0x14)
  87. #define MUL F3(2, 0x0a) /* umul */
  88. #define DIV F3(2, 0x0e) /* udiv */
  89. #define SLL F3(2, 0x25)
  90. #define SRL F3(2, 0x26)
  91. #define JMPL F3(2, 0x38)
  92. #define CALL F1(1)
  93. #define BR F2(0, 0x01)
  94. #define RD_Y F3(2, 0x28)
  95. #define WR_Y F3(2, 0x30)
  96. #define LD32 F3(3, 0x00)
  97. #define LD8 F3(3, 0x01)
  98. #define LD16 F3(3, 0x02)
  99. #define LD64 F3(3, 0x0b)
  100. #define ST32 F3(3, 0x04)
  101. #ifdef CONFIG_SPARC64
  102. #define LDPTR LD64
  103. #define BASE_STACKFRAME 176
  104. #else
  105. #define LDPTR LD32
  106. #define BASE_STACKFRAME 96
  107. #endif
  108. #define LD32I (LD32 | IMMED)
  109. #define LD8I (LD8 | IMMED)
  110. #define LD16I (LD16 | IMMED)
  111. #define LD64I (LD64 | IMMED)
  112. #define LDPTRI (LDPTR | IMMED)
  113. #define ST32I (ST32 | IMMED)
  114. #define emit_nop() \
  115. do { \
  116. *prog++ = SETHI(0, G0); \
  117. } while (0)
  118. #define emit_neg() \
  119. do { /* sub %g0, r_A, r_A */ \
  120. *prog++ = SUB | RS1(G0) | RS2(r_A) | RD(r_A); \
  121. } while (0)
  122. #define emit_reg_move(FROM, TO) \
  123. do { /* or %g0, FROM, TO */ \
  124. *prog++ = OR | RS1(G0) | RS2(FROM) | RD(TO); \
  125. } while (0)
  126. #define emit_clear(REG) \
  127. do { /* or %g0, %g0, REG */ \
  128. *prog++ = OR | RS1(G0) | RS2(G0) | RD(REG); \
  129. } while (0)
  130. #define emit_set_const(K, REG) \
  131. do { /* sethi %hi(K), REG */ \
  132. *prog++ = SETHI(K, REG); \
  133. /* or REG, %lo(K), REG */ \
  134. *prog++ = OR_LO(K, REG); \
  135. } while (0)
  136. /* Emit
  137. *
  138. * OP r_A, r_X, r_A
  139. */
  140. #define emit_alu_X(OPCODE) \
  141. do { \
  142. seen |= SEEN_XREG; \
  143. *prog++ = OPCODE | RS1(r_A) | RS2(r_X) | RD(r_A); \
  144. } while (0)
  145. /* Emit either:
  146. *
  147. * OP r_A, K, r_A
  148. *
  149. * or
  150. *
  151. * sethi %hi(K), r_TMP
  152. * or r_TMP, %lo(K), r_TMP
  153. * OP r_A, r_TMP, r_A
  154. *
  155. * depending upon whether K fits in a signed 13-bit
  156. * immediate instruction field. Emit nothing if K
  157. * is zero.
  158. */
  159. #define emit_alu_K(OPCODE, K) \
  160. do { \
  161. if (K || OPCODE == AND || OPCODE == MUL) { \
  162. unsigned int _insn = OPCODE; \
  163. _insn |= RS1(r_A) | RD(r_A); \
  164. if (is_simm13(K)) { \
  165. *prog++ = _insn | IMMED | S13(K); \
  166. } else { \
  167. emit_set_const(K, r_TMP); \
  168. *prog++ = _insn | RS2(r_TMP); \
  169. } \
  170. } \
  171. } while (0)
  172. #define emit_loadimm(K, DEST) \
  173. do { \
  174. if (is_simm13(K)) { \
  175. /* or %g0, K, DEST */ \
  176. *prog++ = OR | IMMED | RS1(G0) | S13(K) | RD(DEST); \
  177. } else { \
  178. emit_set_const(K, DEST); \
  179. } \
  180. } while (0)
  181. #define emit_loadptr(BASE, STRUCT, FIELD, DEST) \
  182. do { unsigned int _off = offsetof(STRUCT, FIELD); \
  183. BUILD_BUG_ON(FIELD_SIZEOF(STRUCT, FIELD) != sizeof(void *)); \
  184. *prog++ = LDPTRI | RS1(BASE) | S13(_off) | RD(DEST); \
  185. } while (0)
  186. #define emit_load32(BASE, STRUCT, FIELD, DEST) \
  187. do { unsigned int _off = offsetof(STRUCT, FIELD); \
  188. BUILD_BUG_ON(FIELD_SIZEOF(STRUCT, FIELD) != sizeof(u32)); \
  189. *prog++ = LD32I | RS1(BASE) | S13(_off) | RD(DEST); \
  190. } while (0)
  191. #define emit_load16(BASE, STRUCT, FIELD, DEST) \
  192. do { unsigned int _off = offsetof(STRUCT, FIELD); \
  193. BUILD_BUG_ON(FIELD_SIZEOF(STRUCT, FIELD) != sizeof(u16)); \
  194. *prog++ = LD16I | RS1(BASE) | S13(_off) | RD(DEST); \
  195. } while (0)
  196. #define __emit_load8(BASE, STRUCT, FIELD, DEST) \
  197. do { unsigned int _off = offsetof(STRUCT, FIELD); \
  198. *prog++ = LD8I | RS1(BASE) | S13(_off) | RD(DEST); \
  199. } while (0)
  200. #define emit_load8(BASE, STRUCT, FIELD, DEST) \
  201. do { BUILD_BUG_ON(FIELD_SIZEOF(STRUCT, FIELD) != sizeof(u8)); \
  202. __emit_load8(BASE, STRUCT, FIELD, DEST); \
  203. } while (0)
  204. #ifdef CONFIG_SPARC64
  205. #define BIAS (STACK_BIAS - 4)
  206. #else
  207. #define BIAS (-4)
  208. #endif
  209. #define emit_ldmem(OFF, DEST) \
  210. do { *prog++ = LD32I | RS1(SP) | S13(BIAS - (OFF)) | RD(DEST); \
  211. } while (0)
  212. #define emit_stmem(OFF, SRC) \
  213. do { *prog++ = ST32I | RS1(SP) | S13(BIAS - (OFF)) | RD(SRC); \
  214. } while (0)
  215. #ifdef CONFIG_SMP
  216. #ifdef CONFIG_SPARC64
  217. #define emit_load_cpu(REG) \
  218. emit_load16(G6, struct thread_info, cpu, REG)
  219. #else
  220. #define emit_load_cpu(REG) \
  221. emit_load32(G6, struct thread_info, cpu, REG)
  222. #endif
  223. #else
  224. #define emit_load_cpu(REG) emit_clear(REG)
  225. #endif
  226. #define emit_skb_loadptr(FIELD, DEST) \
  227. emit_loadptr(r_SKB, struct sk_buff, FIELD, DEST)
  228. #define emit_skb_load32(FIELD, DEST) \
  229. emit_load32(r_SKB, struct sk_buff, FIELD, DEST)
  230. #define emit_skb_load16(FIELD, DEST) \
  231. emit_load16(r_SKB, struct sk_buff, FIELD, DEST)
  232. #define __emit_skb_load8(FIELD, DEST) \
  233. __emit_load8(r_SKB, struct sk_buff, FIELD, DEST)
  234. #define emit_skb_load8(FIELD, DEST) \
  235. emit_load8(r_SKB, struct sk_buff, FIELD, DEST)
  236. #define emit_jmpl(BASE, IMM_OFF, LREG) \
  237. *prog++ = (JMPL | IMMED | RS1(BASE) | S13(IMM_OFF) | RD(LREG))
  238. #define emit_call(FUNC) \
  239. do { void *_here = image + addrs[i] - 8; \
  240. unsigned int _off = (void *)(FUNC) - _here; \
  241. *prog++ = CALL | (((_off) >> 2) & 0x3fffffff); \
  242. emit_nop(); \
  243. } while (0)
  244. #define emit_branch(BR_OPC, DEST) \
  245. do { unsigned int _here = addrs[i] - 8; \
  246. *prog++ = BR_OPC | WDISP22((DEST) - _here); \
  247. } while (0)
  248. #define emit_branch_off(BR_OPC, OFF) \
  249. do { *prog++ = BR_OPC | WDISP22(OFF); \
  250. } while (0)
  251. #define emit_jump(DEST) emit_branch(BA, DEST)
  252. #define emit_read_y(REG) *prog++ = RD_Y | RD(REG)
  253. #define emit_write_y(REG) *prog++ = WR_Y | IMMED | RS1(REG) | S13(0)
  254. #define emit_cmp(R1, R2) \
  255. *prog++ = (SUBCC | RS1(R1) | RS2(R2) | RD(G0))
  256. #define emit_cmpi(R1, IMM) \
  257. *prog++ = (SUBCC | IMMED | RS1(R1) | S13(IMM) | RD(G0));
  258. #define emit_btst(R1, R2) \
  259. *prog++ = (ANDCC | RS1(R1) | RS2(R2) | RD(G0))
  260. #define emit_btsti(R1, IMM) \
  261. *prog++ = (ANDCC | IMMED | RS1(R1) | S13(IMM) | RD(G0));
  262. #define emit_sub(R1, R2, R3) \
  263. *prog++ = (SUB | RS1(R1) | RS2(R2) | RD(R3))
  264. #define emit_subi(R1, IMM, R3) \
  265. *prog++ = (SUB | IMMED | RS1(R1) | S13(IMM) | RD(R3))
  266. #define emit_add(R1, R2, R3) \
  267. *prog++ = (ADD | RS1(R1) | RS2(R2) | RD(R3))
  268. #define emit_addi(R1, IMM, R3) \
  269. *prog++ = (ADD | IMMED | RS1(R1) | S13(IMM) | RD(R3))
  270. #define emit_and(R1, R2, R3) \
  271. *prog++ = (AND | RS1(R1) | RS2(R2) | RD(R3))
  272. #define emit_andi(R1, IMM, R3) \
  273. *prog++ = (AND | IMMED | RS1(R1) | S13(IMM) | RD(R3))
  274. #define emit_alloc_stack(SZ) \
  275. *prog++ = (SUB | IMMED | RS1(SP) | S13(SZ) | RD(SP))
  276. #define emit_release_stack(SZ) \
  277. *prog++ = (ADD | IMMED | RS1(SP) | S13(SZ) | RD(SP))
  278. /* A note about branch offset calculations. The addrs[] array,
  279. * indexed by BPF instruction, records the address after all the
  280. * sparc instructions emitted for that BPF instruction.
  281. *
  282. * The most common case is to emit a branch at the end of such
  283. * a code sequence. So this would be two instructions, the
  284. * branch and it's delay slot.
  285. *
  286. * Therefore by default the branch emitters calculate the branch
  287. * offset field as:
  288. *
  289. * destination - (addrs[i] - 8)
  290. *
  291. * This "addrs[i] - 8" is the address of the branch itself or
  292. * what "." would be in assembler notation. The "8" part is
  293. * how we take into consideration the branch and it's delay
  294. * slot mentioned above.
  295. *
  296. * Sometimes we need to emit a branch earlier in the code
  297. * sequence. And in these situations we adjust "destination"
  298. * to accommodate this difference. For example, if we needed
  299. * to emit a branch (and it's delay slot) right before the
  300. * final instruction emitted for a BPF opcode, we'd use
  301. * "destination + 4" instead of just plain "destination" above.
  302. *
  303. * This is why you see all of these funny emit_branch() and
  304. * emit_jump() calls with adjusted offsets.
  305. */
  306. void bpf_jit_compile(struct bpf_prog *fp)
  307. {
  308. unsigned int cleanup_addr, proglen, oldproglen = 0;
  309. u32 temp[8], *prog, *func, seen = 0, pass;
  310. const struct sock_filter *filter = fp->insns;
  311. int i, flen = fp->len, pc_ret0 = -1;
  312. unsigned int *addrs;
  313. void *image;
  314. if (!bpf_jit_enable)
  315. return;
  316. addrs = kmalloc(flen * sizeof(*addrs), GFP_KERNEL);
  317. if (addrs == NULL)
  318. return;
  319. /* Before first pass, make a rough estimation of addrs[]
  320. * each bpf instruction is translated to less than 64 bytes
  321. */
  322. for (proglen = 0, i = 0; i < flen; i++) {
  323. proglen += 64;
  324. addrs[i] = proglen;
  325. }
  326. cleanup_addr = proglen; /* epilogue address */
  327. image = NULL;
  328. for (pass = 0; pass < 10; pass++) {
  329. u8 seen_or_pass0 = (pass == 0) ? (SEEN_XREG | SEEN_DATAREF | SEEN_MEM) : seen;
  330. /* no prologue/epilogue for trivial filters (RET something) */
  331. proglen = 0;
  332. prog = temp;
  333. /* Prologue */
  334. if (seen_or_pass0) {
  335. if (seen_or_pass0 & SEEN_MEM) {
  336. unsigned int sz = BASE_STACKFRAME;
  337. sz += BPF_MEMWORDS * sizeof(u32);
  338. emit_alloc_stack(sz);
  339. }
  340. /* Make sure we dont leek kernel memory. */
  341. if (seen_or_pass0 & SEEN_XREG)
  342. emit_clear(r_X);
  343. /* If this filter needs to access skb data,
  344. * load %o4 and %o5 with:
  345. * %o4 = skb->len - skb->data_len
  346. * %o5 = skb->data
  347. * And also back up %o7 into r_saved_O7 so we can
  348. * invoke the stubs using 'call'.
  349. */
  350. if (seen_or_pass0 & SEEN_DATAREF) {
  351. emit_load32(r_SKB, struct sk_buff, len, r_HEADLEN);
  352. emit_load32(r_SKB, struct sk_buff, data_len, r_TMP);
  353. emit_sub(r_HEADLEN, r_TMP, r_HEADLEN);
  354. emit_loadptr(r_SKB, struct sk_buff, data, r_SKB_DATA);
  355. }
  356. }
  357. emit_reg_move(O7, r_saved_O7);
  358. /* Make sure we dont leak kernel information to the user. */
  359. if (bpf_needs_clear_a(&filter[0]))
  360. emit_clear(r_A); /* A = 0 */
  361. for (i = 0; i < flen; i++) {
  362. unsigned int K = filter[i].k;
  363. unsigned int t_offset;
  364. unsigned int f_offset;
  365. u32 t_op, f_op;
  366. u16 code = bpf_anc_helper(&filter[i]);
  367. int ilen;
  368. switch (code) {
  369. case BPF_ALU | BPF_ADD | BPF_X: /* A += X; */
  370. emit_alu_X(ADD);
  371. break;
  372. case BPF_ALU | BPF_ADD | BPF_K: /* A += K; */
  373. emit_alu_K(ADD, K);
  374. break;
  375. case BPF_ALU | BPF_SUB | BPF_X: /* A -= X; */
  376. emit_alu_X(SUB);
  377. break;
  378. case BPF_ALU | BPF_SUB | BPF_K: /* A -= K */
  379. emit_alu_K(SUB, K);
  380. break;
  381. case BPF_ALU | BPF_AND | BPF_X: /* A &= X */
  382. emit_alu_X(AND);
  383. break;
  384. case BPF_ALU | BPF_AND | BPF_K: /* A &= K */
  385. emit_alu_K(AND, K);
  386. break;
  387. case BPF_ALU | BPF_OR | BPF_X: /* A |= X */
  388. emit_alu_X(OR);
  389. break;
  390. case BPF_ALU | BPF_OR | BPF_K: /* A |= K */
  391. emit_alu_K(OR, K);
  392. break;
  393. case BPF_ANC | SKF_AD_ALU_XOR_X: /* A ^= X; */
  394. case BPF_ALU | BPF_XOR | BPF_X:
  395. emit_alu_X(XOR);
  396. break;
  397. case BPF_ALU | BPF_XOR | BPF_K: /* A ^= K */
  398. emit_alu_K(XOR, K);
  399. break;
  400. case BPF_ALU | BPF_LSH | BPF_X: /* A <<= X */
  401. emit_alu_X(SLL);
  402. break;
  403. case BPF_ALU | BPF_LSH | BPF_K: /* A <<= K */
  404. emit_alu_K(SLL, K);
  405. break;
  406. case BPF_ALU | BPF_RSH | BPF_X: /* A >>= X */
  407. emit_alu_X(SRL);
  408. break;
  409. case BPF_ALU | BPF_RSH | BPF_K: /* A >>= K */
  410. emit_alu_K(SRL, K);
  411. break;
  412. case BPF_ALU | BPF_MUL | BPF_X: /* A *= X; */
  413. emit_alu_X(MUL);
  414. break;
  415. case BPF_ALU | BPF_MUL | BPF_K: /* A *= K */
  416. emit_alu_K(MUL, K);
  417. break;
  418. case BPF_ALU | BPF_DIV | BPF_K: /* A /= K with K != 0*/
  419. if (K == 1)
  420. break;
  421. emit_write_y(G0);
  422. #ifdef CONFIG_SPARC32
  423. /* The Sparc v8 architecture requires
  424. * three instructions between a %y
  425. * register write and the first use.
  426. */
  427. emit_nop();
  428. emit_nop();
  429. emit_nop();
  430. #endif
  431. emit_alu_K(DIV, K);
  432. break;
  433. case BPF_ALU | BPF_DIV | BPF_X: /* A /= X; */
  434. emit_cmpi(r_X, 0);
  435. if (pc_ret0 > 0) {
  436. t_offset = addrs[pc_ret0 - 1];
  437. #ifdef CONFIG_SPARC32
  438. emit_branch(BE, t_offset + 20);
  439. #else
  440. emit_branch(BE, t_offset + 8);
  441. #endif
  442. emit_nop(); /* delay slot */
  443. } else {
  444. emit_branch_off(BNE, 16);
  445. emit_nop();
  446. #ifdef CONFIG_SPARC32
  447. emit_jump(cleanup_addr + 20);
  448. #else
  449. emit_jump(cleanup_addr + 8);
  450. #endif
  451. emit_clear(r_A);
  452. }
  453. emit_write_y(G0);
  454. #ifdef CONFIG_SPARC32
  455. /* The Sparc v8 architecture requires
  456. * three instructions between a %y
  457. * register write and the first use.
  458. */
  459. emit_nop();
  460. emit_nop();
  461. emit_nop();
  462. #endif
  463. emit_alu_X(DIV);
  464. break;
  465. case BPF_ALU | BPF_NEG:
  466. emit_neg();
  467. break;
  468. case BPF_RET | BPF_K:
  469. if (!K) {
  470. if (pc_ret0 == -1)
  471. pc_ret0 = i;
  472. emit_clear(r_A);
  473. } else {
  474. emit_loadimm(K, r_A);
  475. }
  476. /* Fallthrough */
  477. case BPF_RET | BPF_A:
  478. if (seen_or_pass0) {
  479. if (i != flen - 1) {
  480. emit_jump(cleanup_addr);
  481. emit_nop();
  482. break;
  483. }
  484. if (seen_or_pass0 & SEEN_MEM) {
  485. unsigned int sz = BASE_STACKFRAME;
  486. sz += BPF_MEMWORDS * sizeof(u32);
  487. emit_release_stack(sz);
  488. }
  489. }
  490. /* jmpl %r_saved_O7 + 8, %g0 */
  491. emit_jmpl(r_saved_O7, 8, G0);
  492. emit_reg_move(r_A, O0); /* delay slot */
  493. break;
  494. case BPF_MISC | BPF_TAX:
  495. seen |= SEEN_XREG;
  496. emit_reg_move(r_A, r_X);
  497. break;
  498. case BPF_MISC | BPF_TXA:
  499. seen |= SEEN_XREG;
  500. emit_reg_move(r_X, r_A);
  501. break;
  502. case BPF_ANC | SKF_AD_CPU:
  503. emit_load_cpu(r_A);
  504. break;
  505. case BPF_ANC | SKF_AD_PROTOCOL:
  506. emit_skb_load16(protocol, r_A);
  507. break;
  508. case BPF_ANC | SKF_AD_PKTTYPE:
  509. __emit_skb_load8(__pkt_type_offset, r_A);
  510. emit_andi(r_A, PKT_TYPE_MAX, r_A);
  511. emit_alu_K(SRL, 5);
  512. break;
  513. case BPF_ANC | SKF_AD_IFINDEX:
  514. emit_skb_loadptr(dev, r_A);
  515. emit_cmpi(r_A, 0);
  516. emit_branch(BE_PTR, cleanup_addr + 4);
  517. emit_nop();
  518. emit_load32(r_A, struct net_device, ifindex, r_A);
  519. break;
  520. case BPF_ANC | SKF_AD_MARK:
  521. emit_skb_load32(mark, r_A);
  522. break;
  523. case BPF_ANC | SKF_AD_QUEUE:
  524. emit_skb_load16(queue_mapping, r_A);
  525. break;
  526. case BPF_ANC | SKF_AD_HATYPE:
  527. emit_skb_loadptr(dev, r_A);
  528. emit_cmpi(r_A, 0);
  529. emit_branch(BE_PTR, cleanup_addr + 4);
  530. emit_nop();
  531. emit_load16(r_A, struct net_device, type, r_A);
  532. break;
  533. case BPF_ANC | SKF_AD_RXHASH:
  534. emit_skb_load32(hash, r_A);
  535. break;
  536. case BPF_ANC | SKF_AD_VLAN_TAG:
  537. case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
  538. emit_skb_load16(vlan_tci, r_A);
  539. if (code != (BPF_ANC | SKF_AD_VLAN_TAG)) {
  540. emit_alu_K(SRL, 12);
  541. emit_andi(r_A, 1, r_A);
  542. } else {
  543. emit_loadimm(~VLAN_TAG_PRESENT, r_TMP);
  544. emit_and(r_A, r_TMP, r_A);
  545. }
  546. break;
  547. case BPF_LD | BPF_W | BPF_LEN:
  548. emit_skb_load32(len, r_A);
  549. break;
  550. case BPF_LDX | BPF_W | BPF_LEN:
  551. emit_skb_load32(len, r_X);
  552. break;
  553. case BPF_LD | BPF_IMM:
  554. emit_loadimm(K, r_A);
  555. break;
  556. case BPF_LDX | BPF_IMM:
  557. emit_loadimm(K, r_X);
  558. break;
  559. case BPF_LD | BPF_MEM:
  560. seen |= SEEN_MEM;
  561. emit_ldmem(K * 4, r_A);
  562. break;
  563. case BPF_LDX | BPF_MEM:
  564. seen |= SEEN_MEM | SEEN_XREG;
  565. emit_ldmem(K * 4, r_X);
  566. break;
  567. case BPF_ST:
  568. seen |= SEEN_MEM;
  569. emit_stmem(K * 4, r_A);
  570. break;
  571. case BPF_STX:
  572. seen |= SEEN_MEM | SEEN_XREG;
  573. emit_stmem(K * 4, r_X);
  574. break;
  575. #define CHOOSE_LOAD_FUNC(K, func) \
  576. ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
  577. case BPF_LD | BPF_W | BPF_ABS:
  578. func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_word);
  579. common_load: seen |= SEEN_DATAREF;
  580. emit_loadimm(K, r_OFF);
  581. emit_call(func);
  582. break;
  583. case BPF_LD | BPF_H | BPF_ABS:
  584. func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_half);
  585. goto common_load;
  586. case BPF_LD | BPF_B | BPF_ABS:
  587. func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_byte);
  588. goto common_load;
  589. case BPF_LDX | BPF_B | BPF_MSH:
  590. func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_byte_msh);
  591. goto common_load;
  592. case BPF_LD | BPF_W | BPF_IND:
  593. func = bpf_jit_load_word;
  594. common_load_ind: seen |= SEEN_DATAREF | SEEN_XREG;
  595. if (K) {
  596. if (is_simm13(K)) {
  597. emit_addi(r_X, K, r_OFF);
  598. } else {
  599. emit_loadimm(K, r_TMP);
  600. emit_add(r_X, r_TMP, r_OFF);
  601. }
  602. } else {
  603. emit_reg_move(r_X, r_OFF);
  604. }
  605. emit_call(func);
  606. break;
  607. case BPF_LD | BPF_H | BPF_IND:
  608. func = bpf_jit_load_half;
  609. goto common_load_ind;
  610. case BPF_LD | BPF_B | BPF_IND:
  611. func = bpf_jit_load_byte;
  612. goto common_load_ind;
  613. case BPF_JMP | BPF_JA:
  614. emit_jump(addrs[i + K]);
  615. emit_nop();
  616. break;
  617. #define COND_SEL(CODE, TOP, FOP) \
  618. case CODE: \
  619. t_op = TOP; \
  620. f_op = FOP; \
  621. goto cond_branch
  622. COND_SEL(BPF_JMP | BPF_JGT | BPF_K, BGU, BLEU);
  623. COND_SEL(BPF_JMP | BPF_JGE | BPF_K, BGEU, BLU);
  624. COND_SEL(BPF_JMP | BPF_JEQ | BPF_K, BE, BNE);
  625. COND_SEL(BPF_JMP | BPF_JSET | BPF_K, BNE, BE);
  626. COND_SEL(BPF_JMP | BPF_JGT | BPF_X, BGU, BLEU);
  627. COND_SEL(BPF_JMP | BPF_JGE | BPF_X, BGEU, BLU);
  628. COND_SEL(BPF_JMP | BPF_JEQ | BPF_X, BE, BNE);
  629. COND_SEL(BPF_JMP | BPF_JSET | BPF_X, BNE, BE);
  630. cond_branch: f_offset = addrs[i + filter[i].jf];
  631. t_offset = addrs[i + filter[i].jt];
  632. /* same targets, can avoid doing the test :) */
  633. if (filter[i].jt == filter[i].jf) {
  634. emit_jump(t_offset);
  635. emit_nop();
  636. break;
  637. }
  638. switch (code) {
  639. case BPF_JMP | BPF_JGT | BPF_X:
  640. case BPF_JMP | BPF_JGE | BPF_X:
  641. case BPF_JMP | BPF_JEQ | BPF_X:
  642. seen |= SEEN_XREG;
  643. emit_cmp(r_A, r_X);
  644. break;
  645. case BPF_JMP | BPF_JSET | BPF_X:
  646. seen |= SEEN_XREG;
  647. emit_btst(r_A, r_X);
  648. break;
  649. case BPF_JMP | BPF_JEQ | BPF_K:
  650. case BPF_JMP | BPF_JGT | BPF_K:
  651. case BPF_JMP | BPF_JGE | BPF_K:
  652. if (is_simm13(K)) {
  653. emit_cmpi(r_A, K);
  654. } else {
  655. emit_loadimm(K, r_TMP);
  656. emit_cmp(r_A, r_TMP);
  657. }
  658. break;
  659. case BPF_JMP | BPF_JSET | BPF_K:
  660. if (is_simm13(K)) {
  661. emit_btsti(r_A, K);
  662. } else {
  663. emit_loadimm(K, r_TMP);
  664. emit_btst(r_A, r_TMP);
  665. }
  666. break;
  667. }
  668. if (filter[i].jt != 0) {
  669. if (filter[i].jf)
  670. t_offset += 8;
  671. emit_branch(t_op, t_offset);
  672. emit_nop(); /* delay slot */
  673. if (filter[i].jf) {
  674. emit_jump(f_offset);
  675. emit_nop();
  676. }
  677. break;
  678. }
  679. emit_branch(f_op, f_offset);
  680. emit_nop(); /* delay slot */
  681. break;
  682. default:
  683. /* hmm, too complex filter, give up with jit compiler */
  684. goto out;
  685. }
  686. ilen = (void *) prog - (void *) temp;
  687. if (image) {
  688. if (unlikely(proglen + ilen > oldproglen)) {
  689. pr_err("bpb_jit_compile fatal error\n");
  690. kfree(addrs);
  691. module_memfree(image);
  692. return;
  693. }
  694. memcpy(image + proglen, temp, ilen);
  695. }
  696. proglen += ilen;
  697. addrs[i] = proglen;
  698. prog = temp;
  699. }
  700. /* last bpf instruction is always a RET :
  701. * use it to give the cleanup instruction(s) addr
  702. */
  703. cleanup_addr = proglen - 8; /* jmpl; mov r_A,%o0; */
  704. if (seen_or_pass0 & SEEN_MEM)
  705. cleanup_addr -= 4; /* add %sp, X, %sp; */
  706. if (image) {
  707. if (proglen != oldproglen)
  708. pr_err("bpb_jit_compile proglen=%u != oldproglen=%u\n",
  709. proglen, oldproglen);
  710. break;
  711. }
  712. if (proglen == oldproglen) {
  713. image = module_alloc(proglen);
  714. if (!image)
  715. goto out;
  716. }
  717. oldproglen = proglen;
  718. }
  719. if (bpf_jit_enable > 1)
  720. bpf_jit_dump(flen, proglen, pass + 1, image);
  721. if (image) {
  722. bpf_flush_icache(image, image + proglen);
  723. fp->bpf_func = (void *)image;
  724. fp->jited = 1;
  725. }
  726. out:
  727. kfree(addrs);
  728. return;
  729. }
  730. void bpf_jit_free(struct bpf_prog *fp)
  731. {
  732. if (fp->jited)
  733. module_memfree(fp->bpf_func);
  734. bpf_prog_unlock_free(fp);
  735. }