bpf_jit_comp.c 21 KB

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