bpf_jit.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Just-In-Time compiler for BPF filters on MIPS
  3. *
  4. * Copyright (c) 2014 Imagination Technologies Ltd.
  5. * Author: Markos Chandras <markos.chandras@imgtec.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/compiler.h>
  13. #include <linux/errno.h>
  14. #include <linux/filter.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/kconfig.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <asm/asm.h>
  23. #include <asm/bitops.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/cpu-features.h>
  26. #include <asm/uasm.h>
  27. #include "bpf_jit.h"
  28. /* ABI
  29. * r_skb_hl SKB header length
  30. * r_data SKB data pointer
  31. * r_off Offset
  32. * r_A BPF register A
  33. * r_X BPF register X
  34. * r_skb *skb
  35. * r_M *scratch memory
  36. * r_skb_len SKB length
  37. *
  38. * On entry (*bpf_func)(*skb, *filter)
  39. * a0 = MIPS_R_A0 = skb;
  40. * a1 = MIPS_R_A1 = filter;
  41. *
  42. * Stack
  43. * ...
  44. * M[15]
  45. * M[14]
  46. * M[13]
  47. * ...
  48. * M[0] <-- r_M
  49. * saved reg k-1
  50. * saved reg k-2
  51. * ...
  52. * saved reg 0 <-- r_sp
  53. * <no argument area>
  54. *
  55. * Packet layout
  56. *
  57. * <--------------------- len ------------------------>
  58. * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
  59. * ----------------------------------------------------
  60. * | skb->data |
  61. * ----------------------------------------------------
  62. */
  63. #define ptr typeof(unsigned long)
  64. #define SCRATCH_OFF(k) (4 * (k))
  65. /* JIT flags */
  66. #define SEEN_CALL (1 << BPF_MEMWORDS)
  67. #define SEEN_SREG_SFT (BPF_MEMWORDS + 1)
  68. #define SEEN_SREG_BASE (1 << SEEN_SREG_SFT)
  69. #define SEEN_SREG(x) (SEEN_SREG_BASE << (x))
  70. #define SEEN_OFF SEEN_SREG(2)
  71. #define SEEN_A SEEN_SREG(3)
  72. #define SEEN_X SEEN_SREG(4)
  73. #define SEEN_SKB SEEN_SREG(5)
  74. #define SEEN_MEM SEEN_SREG(6)
  75. /* SEEN_SK_DATA also implies skb_hl an skb_len */
  76. #define SEEN_SKB_DATA (SEEN_SREG(7) | SEEN_SREG(1) | SEEN_SREG(0))
  77. /* Arguments used by JIT */
  78. #define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */
  79. #define SBIT(x) (1 << (x)) /* Signed version of BIT() */
  80. /**
  81. * struct jit_ctx - JIT context
  82. * @skf: The sk_filter
  83. * @prologue_bytes: Number of bytes for prologue
  84. * @idx: Instruction index
  85. * @flags: JIT flags
  86. * @offsets: Instruction offsets
  87. * @target: Memory location for the compiled filter
  88. */
  89. struct jit_ctx {
  90. const struct bpf_prog *skf;
  91. unsigned int prologue_bytes;
  92. u32 idx;
  93. u32 flags;
  94. u32 *offsets;
  95. u32 *target;
  96. };
  97. static inline int optimize_div(u32 *k)
  98. {
  99. /* power of 2 divides can be implemented with right shift */
  100. if (!(*k & (*k-1))) {
  101. *k = ilog2(*k);
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
  107. /* Simply emit the instruction if the JIT memory space has been allocated */
  108. #define emit_instr(ctx, func, ...) \
  109. do { \
  110. if ((ctx)->target != NULL) { \
  111. u32 *p = &(ctx)->target[ctx->idx]; \
  112. uasm_i_##func(&p, ##__VA_ARGS__); \
  113. } \
  114. (ctx)->idx++; \
  115. } while (0)
  116. /*
  117. * Similar to emit_instr but it must be used when we need to emit
  118. * 32-bit or 64-bit instructions
  119. */
  120. #define emit_long_instr(ctx, func, ...) \
  121. do { \
  122. if ((ctx)->target != NULL) { \
  123. u32 *p = &(ctx)->target[ctx->idx]; \
  124. UASM_i_##func(&p, ##__VA_ARGS__); \
  125. } \
  126. (ctx)->idx++; \
  127. } while (0)
  128. /* Determine if immediate is within the 16-bit signed range */
  129. static inline bool is_range16(s32 imm)
  130. {
  131. return !(imm >= SBIT(15) || imm < -SBIT(15));
  132. }
  133. static inline void emit_addu(unsigned int dst, unsigned int src1,
  134. unsigned int src2, struct jit_ctx *ctx)
  135. {
  136. emit_instr(ctx, addu, dst, src1, src2);
  137. }
  138. static inline void emit_nop(struct jit_ctx *ctx)
  139. {
  140. emit_instr(ctx, nop);
  141. }
  142. /* Load a u32 immediate to a register */
  143. static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
  144. {
  145. if (ctx->target != NULL) {
  146. /* addiu can only handle s16 */
  147. if (!is_range16(imm)) {
  148. u32 *p = &ctx->target[ctx->idx];
  149. uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
  150. p = &ctx->target[ctx->idx + 1];
  151. uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff);
  152. } else {
  153. u32 *p = &ctx->target[ctx->idx];
  154. uasm_i_addiu(&p, dst, r_zero, imm);
  155. }
  156. }
  157. ctx->idx++;
  158. if (!is_range16(imm))
  159. ctx->idx++;
  160. }
  161. static inline void emit_or(unsigned int dst, unsigned int src1,
  162. unsigned int src2, struct jit_ctx *ctx)
  163. {
  164. emit_instr(ctx, or, dst, src1, src2);
  165. }
  166. static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
  167. struct jit_ctx *ctx)
  168. {
  169. if (imm >= BIT(16)) {
  170. emit_load_imm(r_tmp, imm, ctx);
  171. emit_or(dst, src, r_tmp, ctx);
  172. } else {
  173. emit_instr(ctx, ori, dst, src, imm);
  174. }
  175. }
  176. static inline void emit_daddiu(unsigned int dst, unsigned int src,
  177. int imm, struct jit_ctx *ctx)
  178. {
  179. /*
  180. * Only used for stack, so the imm is relatively small
  181. * and it fits in 15-bits
  182. */
  183. emit_instr(ctx, daddiu, dst, src, imm);
  184. }
  185. static inline void emit_addiu(unsigned int dst, unsigned int src,
  186. u32 imm, struct jit_ctx *ctx)
  187. {
  188. if (!is_range16(imm)) {
  189. emit_load_imm(r_tmp, imm, ctx);
  190. emit_addu(dst, r_tmp, src, ctx);
  191. } else {
  192. emit_instr(ctx, addiu, dst, src, imm);
  193. }
  194. }
  195. static inline void emit_and(unsigned int dst, unsigned int src1,
  196. unsigned int src2, struct jit_ctx *ctx)
  197. {
  198. emit_instr(ctx, and, dst, src1, src2);
  199. }
  200. static inline void emit_andi(unsigned int dst, unsigned int src,
  201. u32 imm, struct jit_ctx *ctx)
  202. {
  203. /* If imm does not fit in u16 then load it to register */
  204. if (imm >= BIT(16)) {
  205. emit_load_imm(r_tmp, imm, ctx);
  206. emit_and(dst, src, r_tmp, ctx);
  207. } else {
  208. emit_instr(ctx, andi, dst, src, imm);
  209. }
  210. }
  211. static inline void emit_xor(unsigned int dst, unsigned int src1,
  212. unsigned int src2, struct jit_ctx *ctx)
  213. {
  214. emit_instr(ctx, xor, dst, src1, src2);
  215. }
  216. static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
  217. {
  218. /* If imm does not fit in u16 then load it to register */
  219. if (imm >= BIT(16)) {
  220. emit_load_imm(r_tmp, imm, ctx);
  221. emit_xor(dst, src, r_tmp, ctx);
  222. } else {
  223. emit_instr(ctx, xori, dst, src, imm);
  224. }
  225. }
  226. static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
  227. {
  228. emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset);
  229. }
  230. static inline void emit_subu(unsigned int dst, unsigned int src1,
  231. unsigned int src2, struct jit_ctx *ctx)
  232. {
  233. emit_instr(ctx, subu, dst, src1, src2);
  234. }
  235. static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
  236. {
  237. emit_subu(reg, r_zero, reg, ctx);
  238. }
  239. static inline void emit_sllv(unsigned int dst, unsigned int src,
  240. unsigned int sa, struct jit_ctx *ctx)
  241. {
  242. emit_instr(ctx, sllv, dst, src, sa);
  243. }
  244. static inline void emit_sll(unsigned int dst, unsigned int src,
  245. unsigned int sa, struct jit_ctx *ctx)
  246. {
  247. /* sa is 5-bits long */
  248. if (sa >= BIT(5))
  249. /* Shifting >= 32 results in zero */
  250. emit_jit_reg_move(dst, r_zero, ctx);
  251. else
  252. emit_instr(ctx, sll, dst, src, sa);
  253. }
  254. static inline void emit_srlv(unsigned int dst, unsigned int src,
  255. unsigned int sa, struct jit_ctx *ctx)
  256. {
  257. emit_instr(ctx, srlv, dst, src, sa);
  258. }
  259. static inline void emit_srl(unsigned int dst, unsigned int src,
  260. unsigned int sa, struct jit_ctx *ctx)
  261. {
  262. /* sa is 5-bits long */
  263. if (sa >= BIT(5))
  264. /* Shifting >= 32 results in zero */
  265. emit_jit_reg_move(dst, r_zero, ctx);
  266. else
  267. emit_instr(ctx, srl, dst, src, sa);
  268. }
  269. static inline void emit_slt(unsigned int dst, unsigned int src1,
  270. unsigned int src2, struct jit_ctx *ctx)
  271. {
  272. emit_instr(ctx, slt, dst, src1, src2);
  273. }
  274. static inline void emit_sltu(unsigned int dst, unsigned int src1,
  275. unsigned int src2, struct jit_ctx *ctx)
  276. {
  277. emit_instr(ctx, sltu, dst, src1, src2);
  278. }
  279. static inline void emit_sltiu(unsigned dst, unsigned int src,
  280. unsigned int imm, struct jit_ctx *ctx)
  281. {
  282. /* 16 bit immediate */
  283. if (!is_range16((s32)imm)) {
  284. emit_load_imm(r_tmp, imm, ctx);
  285. emit_sltu(dst, src, r_tmp, ctx);
  286. } else {
  287. emit_instr(ctx, sltiu, dst, src, imm);
  288. }
  289. }
  290. /* Store register on the stack */
  291. static inline void emit_store_stack_reg(ptr reg, ptr base,
  292. unsigned int offset,
  293. struct jit_ctx *ctx)
  294. {
  295. emit_long_instr(ctx, SW, reg, offset, base);
  296. }
  297. static inline void emit_store(ptr reg, ptr base, unsigned int offset,
  298. struct jit_ctx *ctx)
  299. {
  300. emit_instr(ctx, sw, reg, offset, base);
  301. }
  302. static inline void emit_load_stack_reg(ptr reg, ptr base,
  303. unsigned int offset,
  304. struct jit_ctx *ctx)
  305. {
  306. emit_long_instr(ctx, LW, reg, offset, base);
  307. }
  308. static inline void emit_load(unsigned int reg, unsigned int base,
  309. unsigned int offset, struct jit_ctx *ctx)
  310. {
  311. emit_instr(ctx, lw, reg, offset, base);
  312. }
  313. static inline void emit_load_byte(unsigned int reg, unsigned int base,
  314. unsigned int offset, struct jit_ctx *ctx)
  315. {
  316. emit_instr(ctx, lb, reg, offset, base);
  317. }
  318. static inline void emit_half_load(unsigned int reg, unsigned int base,
  319. unsigned int offset, struct jit_ctx *ctx)
  320. {
  321. emit_instr(ctx, lh, reg, offset, base);
  322. }
  323. static inline void emit_mul(unsigned int dst, unsigned int src1,
  324. unsigned int src2, struct jit_ctx *ctx)
  325. {
  326. emit_instr(ctx, mul, dst, src1, src2);
  327. }
  328. static inline void emit_div(unsigned int dst, unsigned int src,
  329. struct jit_ctx *ctx)
  330. {
  331. if (ctx->target != NULL) {
  332. u32 *p = &ctx->target[ctx->idx];
  333. uasm_i_divu(&p, dst, src);
  334. p = &ctx->target[ctx->idx + 1];
  335. uasm_i_mflo(&p, dst);
  336. }
  337. ctx->idx += 2; /* 2 insts */
  338. }
  339. static inline void emit_mod(unsigned int dst, unsigned int src,
  340. struct jit_ctx *ctx)
  341. {
  342. if (ctx->target != NULL) {
  343. u32 *p = &ctx->target[ctx->idx];
  344. uasm_i_divu(&p, dst, src);
  345. p = &ctx->target[ctx->idx + 1];
  346. uasm_i_mfhi(&p, dst);
  347. }
  348. ctx->idx += 2; /* 2 insts */
  349. }
  350. static inline void emit_dsll(unsigned int dst, unsigned int src,
  351. unsigned int sa, struct jit_ctx *ctx)
  352. {
  353. emit_instr(ctx, dsll, dst, src, sa);
  354. }
  355. static inline void emit_dsrl32(unsigned int dst, unsigned int src,
  356. unsigned int sa, struct jit_ctx *ctx)
  357. {
  358. emit_instr(ctx, dsrl32, dst, src, sa);
  359. }
  360. static inline void emit_wsbh(unsigned int dst, unsigned int src,
  361. struct jit_ctx *ctx)
  362. {
  363. emit_instr(ctx, wsbh, dst, src);
  364. }
  365. /* load pointer to register */
  366. static inline void emit_load_ptr(unsigned int dst, unsigned int src,
  367. int imm, struct jit_ctx *ctx)
  368. {
  369. /* src contains the base addr of the 32/64-pointer */
  370. emit_long_instr(ctx, LW, dst, imm, src);
  371. }
  372. /* load a function pointer to register */
  373. static inline void emit_load_func(unsigned int reg, ptr imm,
  374. struct jit_ctx *ctx)
  375. {
  376. if (config_enabled(CONFIG_64BIT)) {
  377. /* At this point imm is always 64-bit */
  378. emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
  379. emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
  380. emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
  381. emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
  382. emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
  383. } else {
  384. emit_load_imm(reg, imm, ctx);
  385. }
  386. }
  387. /* Move to real MIPS register */
  388. static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
  389. {
  390. emit_long_instr(ctx, ADDU, dst, src, r_zero);
  391. }
  392. /* Move to JIT (32-bit) register */
  393. static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
  394. {
  395. emit_addu(dst, src, r_zero, ctx);
  396. }
  397. /* Compute the immediate value for PC-relative branches. */
  398. static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
  399. {
  400. if (ctx->target == NULL)
  401. return 0;
  402. /*
  403. * We want a pc-relative branch. We only do forward branches
  404. * so tgt is always after pc. tgt is the instruction offset
  405. * we want to jump to.
  406. * Branch on MIPS:
  407. * I: target_offset <- sign_extend(offset)
  408. * I+1: PC += target_offset (delay slot)
  409. *
  410. * ctx->idx currently points to the branch instruction
  411. * but the offset is added to the delay slot so we need
  412. * to subtract 4.
  413. */
  414. return ctx->offsets[tgt] -
  415. (ctx->idx * 4 - ctx->prologue_bytes) - 4;
  416. }
  417. static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
  418. unsigned int imm, struct jit_ctx *ctx)
  419. {
  420. if (ctx->target != NULL) {
  421. u32 *p = &ctx->target[ctx->idx];
  422. switch (cond) {
  423. case MIPS_COND_EQ:
  424. uasm_i_beq(&p, reg1, reg2, imm);
  425. break;
  426. case MIPS_COND_NE:
  427. uasm_i_bne(&p, reg1, reg2, imm);
  428. break;
  429. case MIPS_COND_ALL:
  430. uasm_i_b(&p, imm);
  431. break;
  432. default:
  433. pr_warn("%s: Unhandled branch conditional: %d\n",
  434. __func__, cond);
  435. }
  436. }
  437. ctx->idx++;
  438. }
  439. static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
  440. {
  441. emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
  442. }
  443. static inline void emit_jalr(unsigned int link, unsigned int reg,
  444. struct jit_ctx *ctx)
  445. {
  446. emit_instr(ctx, jalr, link, reg);
  447. }
  448. static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
  449. {
  450. emit_instr(ctx, jr, reg);
  451. }
  452. static inline u16 align_sp(unsigned int num)
  453. {
  454. /* Double word alignment for 32-bit, quadword for 64-bit */
  455. unsigned int align = config_enabled(CONFIG_64BIT) ? 16 : 8;
  456. num = (num + (align - 1)) & -align;
  457. return num;
  458. }
  459. static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
  460. {
  461. int i = 0, real_off = 0;
  462. u32 sflags, tmp_flags;
  463. /* Adjust the stack pointer */
  464. emit_stack_offset(-align_sp(offset), ctx);
  465. tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
  466. /* sflags is essentially a bitmap */
  467. while (tmp_flags) {
  468. if ((sflags >> i) & 0x1) {
  469. emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
  470. ctx);
  471. real_off += SZREG;
  472. }
  473. i++;
  474. tmp_flags >>= 1;
  475. }
  476. /* save return address */
  477. if (ctx->flags & SEEN_CALL) {
  478. emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
  479. real_off += SZREG;
  480. }
  481. /* Setup r_M leaving the alignment gap if necessary */
  482. if (ctx->flags & SEEN_MEM) {
  483. if (real_off % (SZREG * 2))
  484. real_off += SZREG;
  485. emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
  486. }
  487. }
  488. static void restore_bpf_jit_regs(struct jit_ctx *ctx,
  489. unsigned int offset)
  490. {
  491. int i, real_off = 0;
  492. u32 sflags, tmp_flags;
  493. tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
  494. /* sflags is a bitmap */
  495. i = 0;
  496. while (tmp_flags) {
  497. if ((sflags >> i) & 0x1) {
  498. emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
  499. ctx);
  500. real_off += SZREG;
  501. }
  502. i++;
  503. tmp_flags >>= 1;
  504. }
  505. /* restore return address */
  506. if (ctx->flags & SEEN_CALL)
  507. emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
  508. /* Restore the sp and discard the scrach memory */
  509. emit_stack_offset(align_sp(offset), ctx);
  510. }
  511. static unsigned int get_stack_depth(struct jit_ctx *ctx)
  512. {
  513. int sp_off = 0;
  514. /* How may s* regs do we need to preserved? */
  515. sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * SZREG;
  516. if (ctx->flags & SEEN_MEM)
  517. sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
  518. if (ctx->flags & SEEN_CALL)
  519. sp_off += SZREG; /* Space for our ra register */
  520. return sp_off;
  521. }
  522. static void build_prologue(struct jit_ctx *ctx)
  523. {
  524. int sp_off;
  525. /* Calculate the total offset for the stack pointer */
  526. sp_off = get_stack_depth(ctx);
  527. save_bpf_jit_regs(ctx, sp_off);
  528. if (ctx->flags & SEEN_SKB)
  529. emit_reg_move(r_skb, MIPS_R_A0, ctx);
  530. if (ctx->flags & SEEN_SKB_DATA) {
  531. /* Load packet length */
  532. emit_load(r_skb_len, r_skb, offsetof(struct sk_buff, len),
  533. ctx);
  534. emit_load(r_tmp, r_skb, offsetof(struct sk_buff, data_len),
  535. ctx);
  536. /* Load the data pointer */
  537. emit_load_ptr(r_skb_data, r_skb,
  538. offsetof(struct sk_buff, data), ctx);
  539. /* Load the header length */
  540. emit_subu(r_skb_hl, r_skb_len, r_tmp, ctx);
  541. }
  542. if (ctx->flags & SEEN_X)
  543. emit_jit_reg_move(r_X, r_zero, ctx);
  544. /* Do not leak kernel data to userspace */
  545. if (bpf_needs_clear_a(&ctx->skf->insns[0]))
  546. emit_jit_reg_move(r_A, r_zero, ctx);
  547. }
  548. static void build_epilogue(struct jit_ctx *ctx)
  549. {
  550. unsigned int sp_off;
  551. /* Calculate the total offset for the stack pointer */
  552. sp_off = get_stack_depth(ctx);
  553. restore_bpf_jit_regs(ctx, sp_off);
  554. /* Return */
  555. emit_jr(r_ra, ctx);
  556. emit_nop(ctx);
  557. }
  558. #define CHOOSE_LOAD_FUNC(K, func) \
  559. ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \
  560. func##_positive)
  561. static int build_body(struct jit_ctx *ctx)
  562. {
  563. const struct bpf_prog *prog = ctx->skf;
  564. const struct sock_filter *inst;
  565. unsigned int i, off, condt;
  566. u32 k, b_off __maybe_unused;
  567. u8 (*sk_load_func)(unsigned long *skb, int offset);
  568. for (i = 0; i < prog->len; i++) {
  569. u16 code;
  570. inst = &(prog->insns[i]);
  571. pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
  572. __func__, inst->code, inst->jt, inst->jf, inst->k);
  573. k = inst->k;
  574. code = bpf_anc_helper(inst);
  575. if (ctx->target == NULL)
  576. ctx->offsets[i] = ctx->idx * 4;
  577. switch (code) {
  578. case BPF_LD | BPF_IMM:
  579. /* A <- k ==> li r_A, k */
  580. ctx->flags |= SEEN_A;
  581. emit_load_imm(r_A, k, ctx);
  582. break;
  583. case BPF_LD | BPF_W | BPF_LEN:
  584. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
  585. /* A <- len ==> lw r_A, offset(skb) */
  586. ctx->flags |= SEEN_SKB | SEEN_A;
  587. off = offsetof(struct sk_buff, len);
  588. emit_load(r_A, r_skb, off, ctx);
  589. break;
  590. case BPF_LD | BPF_MEM:
  591. /* A <- M[k] ==> lw r_A, offset(M) */
  592. ctx->flags |= SEEN_MEM | SEEN_A;
  593. emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
  594. break;
  595. case BPF_LD | BPF_W | BPF_ABS:
  596. /* A <- P[k:4] */
  597. sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_word);
  598. goto load;
  599. case BPF_LD | BPF_H | BPF_ABS:
  600. /* A <- P[k:2] */
  601. sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_half);
  602. goto load;
  603. case BPF_LD | BPF_B | BPF_ABS:
  604. /* A <- P[k:1] */
  605. sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_byte);
  606. load:
  607. emit_load_imm(r_off, k, ctx);
  608. load_common:
  609. ctx->flags |= SEEN_CALL | SEEN_OFF |
  610. SEEN_SKB | SEEN_A | SEEN_SKB_DATA;
  611. emit_load_func(r_s0, (ptr)sk_load_func, ctx);
  612. emit_reg_move(MIPS_R_A0, r_skb, ctx);
  613. emit_jalr(MIPS_R_RA, r_s0, ctx);
  614. /* Load second argument to delay slot */
  615. emit_reg_move(MIPS_R_A1, r_off, ctx);
  616. /* Check the error value */
  617. emit_bcond(MIPS_COND_EQ, r_ret, 0, b_imm(i + 1, ctx),
  618. ctx);
  619. /* Load return register on DS for failures */
  620. emit_reg_move(r_ret, r_zero, ctx);
  621. /* Return with error */
  622. emit_b(b_imm(prog->len, ctx), ctx);
  623. emit_nop(ctx);
  624. break;
  625. case BPF_LD | BPF_W | BPF_IND:
  626. /* A <- P[X + k:4] */
  627. sk_load_func = sk_load_word;
  628. goto load_ind;
  629. case BPF_LD | BPF_H | BPF_IND:
  630. /* A <- P[X + k:2] */
  631. sk_load_func = sk_load_half;
  632. goto load_ind;
  633. case BPF_LD | BPF_B | BPF_IND:
  634. /* A <- P[X + k:1] */
  635. sk_load_func = sk_load_byte;
  636. load_ind:
  637. ctx->flags |= SEEN_OFF | SEEN_X;
  638. emit_addiu(r_off, r_X, k, ctx);
  639. goto load_common;
  640. case BPF_LDX | BPF_IMM:
  641. /* X <- k */
  642. ctx->flags |= SEEN_X;
  643. emit_load_imm(r_X, k, ctx);
  644. break;
  645. case BPF_LDX | BPF_MEM:
  646. /* X <- M[k] */
  647. ctx->flags |= SEEN_X | SEEN_MEM;
  648. emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
  649. break;
  650. case BPF_LDX | BPF_W | BPF_LEN:
  651. /* X <- len */
  652. ctx->flags |= SEEN_X | SEEN_SKB;
  653. off = offsetof(struct sk_buff, len);
  654. emit_load(r_X, r_skb, off, ctx);
  655. break;
  656. case BPF_LDX | BPF_B | BPF_MSH:
  657. /* X <- 4 * (P[k:1] & 0xf) */
  658. ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB;
  659. /* Load offset to a1 */
  660. emit_load_func(r_s0, (ptr)sk_load_byte, ctx);
  661. /*
  662. * This may emit two instructions so it may not fit
  663. * in the delay slot. So use a0 in the delay slot.
  664. */
  665. emit_load_imm(MIPS_R_A1, k, ctx);
  666. emit_jalr(MIPS_R_RA, r_s0, ctx);
  667. emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
  668. /* Check the error value */
  669. emit_bcond(MIPS_COND_NE, r_ret, 0,
  670. b_imm(prog->len, ctx), ctx);
  671. emit_reg_move(r_ret, r_zero, ctx);
  672. /* We are good */
  673. /* X <- P[1:K] & 0xf */
  674. emit_andi(r_X, r_A, 0xf, ctx);
  675. /* X << 2 */
  676. emit_b(b_imm(i + 1, ctx), ctx);
  677. emit_sll(r_X, r_X, 2, ctx); /* delay slot */
  678. break;
  679. case BPF_ST:
  680. /* M[k] <- A */
  681. ctx->flags |= SEEN_MEM | SEEN_A;
  682. emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
  683. break;
  684. case BPF_STX:
  685. /* M[k] <- X */
  686. ctx->flags |= SEEN_MEM | SEEN_X;
  687. emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
  688. break;
  689. case BPF_ALU | BPF_ADD | BPF_K:
  690. /* A += K */
  691. ctx->flags |= SEEN_A;
  692. emit_addiu(r_A, r_A, k, ctx);
  693. break;
  694. case BPF_ALU | BPF_ADD | BPF_X:
  695. /* A += X */
  696. ctx->flags |= SEEN_A | SEEN_X;
  697. emit_addu(r_A, r_A, r_X, ctx);
  698. break;
  699. case BPF_ALU | BPF_SUB | BPF_K:
  700. /* A -= K */
  701. ctx->flags |= SEEN_A;
  702. emit_addiu(r_A, r_A, -k, ctx);
  703. break;
  704. case BPF_ALU | BPF_SUB | BPF_X:
  705. /* A -= X */
  706. ctx->flags |= SEEN_A | SEEN_X;
  707. emit_subu(r_A, r_A, r_X, ctx);
  708. break;
  709. case BPF_ALU | BPF_MUL | BPF_K:
  710. /* A *= K */
  711. /* Load K to scratch register before MUL */
  712. ctx->flags |= SEEN_A;
  713. emit_load_imm(r_s0, k, ctx);
  714. emit_mul(r_A, r_A, r_s0, ctx);
  715. break;
  716. case BPF_ALU | BPF_MUL | BPF_X:
  717. /* A *= X */
  718. ctx->flags |= SEEN_A | SEEN_X;
  719. emit_mul(r_A, r_A, r_X, ctx);
  720. break;
  721. case BPF_ALU | BPF_DIV | BPF_K:
  722. /* A /= k */
  723. if (k == 1)
  724. break;
  725. if (optimize_div(&k)) {
  726. ctx->flags |= SEEN_A;
  727. emit_srl(r_A, r_A, k, ctx);
  728. break;
  729. }
  730. ctx->flags |= SEEN_A;
  731. emit_load_imm(r_s0, k, ctx);
  732. emit_div(r_A, r_s0, ctx);
  733. break;
  734. case BPF_ALU | BPF_MOD | BPF_K:
  735. /* A %= k */
  736. if (k == 1) {
  737. ctx->flags |= SEEN_A;
  738. emit_jit_reg_move(r_A, r_zero, ctx);
  739. } else {
  740. ctx->flags |= SEEN_A;
  741. emit_load_imm(r_s0, k, ctx);
  742. emit_mod(r_A, r_s0, ctx);
  743. }
  744. break;
  745. case BPF_ALU | BPF_DIV | BPF_X:
  746. /* A /= X */
  747. ctx->flags |= SEEN_X | SEEN_A;
  748. /* Check if r_X is zero */
  749. emit_bcond(MIPS_COND_EQ, r_X, r_zero,
  750. b_imm(prog->len, ctx), ctx);
  751. emit_load_imm(r_ret, 0, ctx); /* delay slot */
  752. emit_div(r_A, r_X, ctx);
  753. break;
  754. case BPF_ALU | BPF_MOD | BPF_X:
  755. /* A %= X */
  756. ctx->flags |= SEEN_X | SEEN_A;
  757. /* Check if r_X is zero */
  758. emit_bcond(MIPS_COND_EQ, r_X, r_zero,
  759. b_imm(prog->len, ctx), ctx);
  760. emit_load_imm(r_ret, 0, ctx); /* delay slot */
  761. emit_mod(r_A, r_X, ctx);
  762. break;
  763. case BPF_ALU | BPF_OR | BPF_K:
  764. /* A |= K */
  765. ctx->flags |= SEEN_A;
  766. emit_ori(r_A, r_A, k, ctx);
  767. break;
  768. case BPF_ALU | BPF_OR | BPF_X:
  769. /* A |= X */
  770. ctx->flags |= SEEN_A;
  771. emit_ori(r_A, r_A, r_X, ctx);
  772. break;
  773. case BPF_ALU | BPF_XOR | BPF_K:
  774. /* A ^= k */
  775. ctx->flags |= SEEN_A;
  776. emit_xori(r_A, r_A, k, ctx);
  777. break;
  778. case BPF_ANC | SKF_AD_ALU_XOR_X:
  779. case BPF_ALU | BPF_XOR | BPF_X:
  780. /* A ^= X */
  781. ctx->flags |= SEEN_A;
  782. emit_xor(r_A, r_A, r_X, ctx);
  783. break;
  784. case BPF_ALU | BPF_AND | BPF_K:
  785. /* A &= K */
  786. ctx->flags |= SEEN_A;
  787. emit_andi(r_A, r_A, k, ctx);
  788. break;
  789. case BPF_ALU | BPF_AND | BPF_X:
  790. /* A &= X */
  791. ctx->flags |= SEEN_A | SEEN_X;
  792. emit_and(r_A, r_A, r_X, ctx);
  793. break;
  794. case BPF_ALU | BPF_LSH | BPF_K:
  795. /* A <<= K */
  796. ctx->flags |= SEEN_A;
  797. emit_sll(r_A, r_A, k, ctx);
  798. break;
  799. case BPF_ALU | BPF_LSH | BPF_X:
  800. /* A <<= X */
  801. ctx->flags |= SEEN_A | SEEN_X;
  802. emit_sllv(r_A, r_A, r_X, ctx);
  803. break;
  804. case BPF_ALU | BPF_RSH | BPF_K:
  805. /* A >>= K */
  806. ctx->flags |= SEEN_A;
  807. emit_srl(r_A, r_A, k, ctx);
  808. break;
  809. case BPF_ALU | BPF_RSH | BPF_X:
  810. ctx->flags |= SEEN_A | SEEN_X;
  811. emit_srlv(r_A, r_A, r_X, ctx);
  812. break;
  813. case BPF_ALU | BPF_NEG:
  814. /* A = -A */
  815. ctx->flags |= SEEN_A;
  816. emit_neg(r_A, ctx);
  817. break;
  818. case BPF_JMP | BPF_JA:
  819. /* pc += K */
  820. emit_b(b_imm(i + k + 1, ctx), ctx);
  821. emit_nop(ctx);
  822. break;
  823. case BPF_JMP | BPF_JEQ | BPF_K:
  824. /* pc += ( A == K ) ? pc->jt : pc->jf */
  825. condt = MIPS_COND_EQ | MIPS_COND_K;
  826. goto jmp_cmp;
  827. case BPF_JMP | BPF_JEQ | BPF_X:
  828. ctx->flags |= SEEN_X;
  829. /* pc += ( A == X ) ? pc->jt : pc->jf */
  830. condt = MIPS_COND_EQ | MIPS_COND_X;
  831. goto jmp_cmp;
  832. case BPF_JMP | BPF_JGE | BPF_K:
  833. /* pc += ( A >= K ) ? pc->jt : pc->jf */
  834. condt = MIPS_COND_GE | MIPS_COND_K;
  835. goto jmp_cmp;
  836. case BPF_JMP | BPF_JGE | BPF_X:
  837. ctx->flags |= SEEN_X;
  838. /* pc += ( A >= X ) ? pc->jt : pc->jf */
  839. condt = MIPS_COND_GE | MIPS_COND_X;
  840. goto jmp_cmp;
  841. case BPF_JMP | BPF_JGT | BPF_K:
  842. /* pc += ( A > K ) ? pc->jt : pc->jf */
  843. condt = MIPS_COND_GT | MIPS_COND_K;
  844. goto jmp_cmp;
  845. case BPF_JMP | BPF_JGT | BPF_X:
  846. ctx->flags |= SEEN_X;
  847. /* pc += ( A > X ) ? pc->jt : pc->jf */
  848. condt = MIPS_COND_GT | MIPS_COND_X;
  849. jmp_cmp:
  850. /* Greater or Equal */
  851. if ((condt & MIPS_COND_GE) ||
  852. (condt & MIPS_COND_GT)) {
  853. if (condt & MIPS_COND_K) { /* K */
  854. ctx->flags |= SEEN_A;
  855. emit_sltiu(r_s0, r_A, k, ctx);
  856. } else { /* X */
  857. ctx->flags |= SEEN_A |
  858. SEEN_X;
  859. emit_sltu(r_s0, r_A, r_X, ctx);
  860. }
  861. /* A < (K|X) ? r_scrach = 1 */
  862. b_off = b_imm(i + inst->jf + 1, ctx);
  863. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
  864. ctx);
  865. emit_nop(ctx);
  866. /* A > (K|X) ? scratch = 0 */
  867. if (condt & MIPS_COND_GT) {
  868. /* Checking for equality */
  869. ctx->flags |= SEEN_A | SEEN_X;
  870. if (condt & MIPS_COND_K)
  871. emit_load_imm(r_s0, k, ctx);
  872. else
  873. emit_jit_reg_move(r_s0, r_X,
  874. ctx);
  875. b_off = b_imm(i + inst->jf + 1, ctx);
  876. emit_bcond(MIPS_COND_EQ, r_A, r_s0,
  877. b_off, ctx);
  878. emit_nop(ctx);
  879. /* Finally, A > K|X */
  880. b_off = b_imm(i + inst->jt + 1, ctx);
  881. emit_b(b_off, ctx);
  882. emit_nop(ctx);
  883. } else {
  884. /* A >= (K|X) so jump */
  885. b_off = b_imm(i + inst->jt + 1, ctx);
  886. emit_b(b_off, ctx);
  887. emit_nop(ctx);
  888. }
  889. } else {
  890. /* A == K|X */
  891. if (condt & MIPS_COND_K) { /* K */
  892. ctx->flags |= SEEN_A;
  893. emit_load_imm(r_s0, k, ctx);
  894. /* jump true */
  895. b_off = b_imm(i + inst->jt + 1, ctx);
  896. emit_bcond(MIPS_COND_EQ, r_A, r_s0,
  897. b_off, ctx);
  898. emit_nop(ctx);
  899. /* jump false */
  900. b_off = b_imm(i + inst->jf + 1,
  901. ctx);
  902. emit_bcond(MIPS_COND_NE, r_A, r_s0,
  903. b_off, ctx);
  904. emit_nop(ctx);
  905. } else { /* X */
  906. /* jump true */
  907. ctx->flags |= SEEN_A | SEEN_X;
  908. b_off = b_imm(i + inst->jt + 1,
  909. ctx);
  910. emit_bcond(MIPS_COND_EQ, r_A, r_X,
  911. b_off, ctx);
  912. emit_nop(ctx);
  913. /* jump false */
  914. b_off = b_imm(i + inst->jf + 1, ctx);
  915. emit_bcond(MIPS_COND_NE, r_A, r_X,
  916. b_off, ctx);
  917. emit_nop(ctx);
  918. }
  919. }
  920. break;
  921. case BPF_JMP | BPF_JSET | BPF_K:
  922. ctx->flags |= SEEN_A;
  923. /* pc += (A & K) ? pc -> jt : pc -> jf */
  924. emit_load_imm(r_s1, k, ctx);
  925. emit_and(r_s0, r_A, r_s1, ctx);
  926. /* jump true */
  927. b_off = b_imm(i + inst->jt + 1, ctx);
  928. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
  929. emit_nop(ctx);
  930. /* jump false */
  931. b_off = b_imm(i + inst->jf + 1, ctx);
  932. emit_b(b_off, ctx);
  933. emit_nop(ctx);
  934. break;
  935. case BPF_JMP | BPF_JSET | BPF_X:
  936. ctx->flags |= SEEN_X | SEEN_A;
  937. /* pc += (A & X) ? pc -> jt : pc -> jf */
  938. emit_and(r_s0, r_A, r_X, ctx);
  939. /* jump true */
  940. b_off = b_imm(i + inst->jt + 1, ctx);
  941. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
  942. emit_nop(ctx);
  943. /* jump false */
  944. b_off = b_imm(i + inst->jf + 1, ctx);
  945. emit_b(b_off, ctx);
  946. emit_nop(ctx);
  947. break;
  948. case BPF_RET | BPF_A:
  949. ctx->flags |= SEEN_A;
  950. if (i != prog->len - 1)
  951. /*
  952. * If this is not the last instruction
  953. * then jump to the epilogue
  954. */
  955. emit_b(b_imm(prog->len, ctx), ctx);
  956. emit_reg_move(r_ret, r_A, ctx); /* delay slot */
  957. break;
  958. case BPF_RET | BPF_K:
  959. /*
  960. * It can emit two instructions so it does not fit on
  961. * the delay slot.
  962. */
  963. emit_load_imm(r_ret, k, ctx);
  964. if (i != prog->len - 1) {
  965. /*
  966. * If this is not the last instruction
  967. * then jump to the epilogue
  968. */
  969. emit_b(b_imm(prog->len, ctx), ctx);
  970. emit_nop(ctx);
  971. }
  972. break;
  973. case BPF_MISC | BPF_TAX:
  974. /* X = A */
  975. ctx->flags |= SEEN_X | SEEN_A;
  976. emit_jit_reg_move(r_X, r_A, ctx);
  977. break;
  978. case BPF_MISC | BPF_TXA:
  979. /* A = X */
  980. ctx->flags |= SEEN_A | SEEN_X;
  981. emit_jit_reg_move(r_A, r_X, ctx);
  982. break;
  983. /* AUX */
  984. case BPF_ANC | SKF_AD_PROTOCOL:
  985. /* A = ntohs(skb->protocol */
  986. ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
  987. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  988. protocol) != 2);
  989. off = offsetof(struct sk_buff, protocol);
  990. emit_half_load(r_A, r_skb, off, ctx);
  991. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  992. /* This needs little endian fixup */
  993. if (cpu_has_wsbh) {
  994. /* R2 and later have the wsbh instruction */
  995. emit_wsbh(r_A, r_A, ctx);
  996. } else {
  997. /* Get first byte */
  998. emit_andi(r_tmp_imm, r_A, 0xff, ctx);
  999. /* Shift it */
  1000. emit_sll(r_tmp, r_tmp_imm, 8, ctx);
  1001. /* Get second byte */
  1002. emit_srl(r_tmp_imm, r_A, 8, ctx);
  1003. emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
  1004. /* Put everyting together in r_A */
  1005. emit_or(r_A, r_tmp, r_tmp_imm, ctx);
  1006. }
  1007. #endif
  1008. break;
  1009. case BPF_ANC | SKF_AD_CPU:
  1010. ctx->flags |= SEEN_A | SEEN_OFF;
  1011. /* A = current_thread_info()->cpu */
  1012. BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
  1013. cpu) != 4);
  1014. off = offsetof(struct thread_info, cpu);
  1015. /* $28/gp points to the thread_info struct */
  1016. emit_load(r_A, 28, off, ctx);
  1017. break;
  1018. case BPF_ANC | SKF_AD_IFINDEX:
  1019. /* A = skb->dev->ifindex */
  1020. ctx->flags |= SEEN_SKB | SEEN_A;
  1021. off = offsetof(struct sk_buff, dev);
  1022. /* Load *dev pointer */
  1023. emit_load_ptr(r_s0, r_skb, off, ctx);
  1024. /* error (0) in the delay slot */
  1025. emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
  1026. b_imm(prog->len, ctx), ctx);
  1027. emit_reg_move(r_ret, r_zero, ctx);
  1028. BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
  1029. ifindex) != 4);
  1030. off = offsetof(struct net_device, ifindex);
  1031. emit_load(r_A, r_s0, off, ctx);
  1032. break;
  1033. case BPF_ANC | SKF_AD_MARK:
  1034. ctx->flags |= SEEN_SKB | SEEN_A;
  1035. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
  1036. off = offsetof(struct sk_buff, mark);
  1037. emit_load(r_A, r_skb, off, ctx);
  1038. break;
  1039. case BPF_ANC | SKF_AD_RXHASH:
  1040. ctx->flags |= SEEN_SKB | SEEN_A;
  1041. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
  1042. off = offsetof(struct sk_buff, hash);
  1043. emit_load(r_A, r_skb, off, ctx);
  1044. break;
  1045. case BPF_ANC | SKF_AD_VLAN_TAG:
  1046. case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
  1047. ctx->flags |= SEEN_SKB | SEEN_A;
  1048. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  1049. vlan_tci) != 2);
  1050. off = offsetof(struct sk_buff, vlan_tci);
  1051. emit_half_load(r_s0, r_skb, off, ctx);
  1052. if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
  1053. emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
  1054. } else {
  1055. emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
  1056. /* return 1 if present */
  1057. emit_sltu(r_A, r_zero, r_A, ctx);
  1058. }
  1059. break;
  1060. case BPF_ANC | SKF_AD_PKTTYPE:
  1061. ctx->flags |= SEEN_SKB;
  1062. emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
  1063. /* Keep only the last 3 bits */
  1064. emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
  1065. #ifdef __BIG_ENDIAN_BITFIELD
  1066. /* Get the actual packet type to the lower 3 bits */
  1067. emit_srl(r_A, r_A, 5, ctx);
  1068. #endif
  1069. break;
  1070. case BPF_ANC | SKF_AD_QUEUE:
  1071. ctx->flags |= SEEN_SKB | SEEN_A;
  1072. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  1073. queue_mapping) != 2);
  1074. BUILD_BUG_ON(offsetof(struct sk_buff,
  1075. queue_mapping) > 0xff);
  1076. off = offsetof(struct sk_buff, queue_mapping);
  1077. emit_half_load(r_A, r_skb, off, ctx);
  1078. break;
  1079. default:
  1080. pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
  1081. inst->code);
  1082. return -1;
  1083. }
  1084. }
  1085. /* compute offsets only during the first pass */
  1086. if (ctx->target == NULL)
  1087. ctx->offsets[i] = ctx->idx * 4;
  1088. return 0;
  1089. }
  1090. int bpf_jit_enable __read_mostly;
  1091. void bpf_jit_compile(struct bpf_prog *fp)
  1092. {
  1093. struct jit_ctx ctx;
  1094. unsigned int alloc_size, tmp_idx;
  1095. if (!bpf_jit_enable)
  1096. return;
  1097. memset(&ctx, 0, sizeof(ctx));
  1098. ctx.offsets = kcalloc(fp->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
  1099. if (ctx.offsets == NULL)
  1100. return;
  1101. ctx.skf = fp;
  1102. if (build_body(&ctx))
  1103. goto out;
  1104. tmp_idx = ctx.idx;
  1105. build_prologue(&ctx);
  1106. ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
  1107. /* just to complete the ctx.idx count */
  1108. build_epilogue(&ctx);
  1109. alloc_size = 4 * ctx.idx;
  1110. ctx.target = module_alloc(alloc_size);
  1111. if (ctx.target == NULL)
  1112. goto out;
  1113. /* Clean it */
  1114. memset(ctx.target, 0, alloc_size);
  1115. ctx.idx = 0;
  1116. /* Generate the actual JIT code */
  1117. build_prologue(&ctx);
  1118. build_body(&ctx);
  1119. build_epilogue(&ctx);
  1120. /* Update the icache */
  1121. flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
  1122. if (bpf_jit_enable > 1)
  1123. /* Dump JIT code */
  1124. bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
  1125. fp->bpf_func = (void *)ctx.target;
  1126. fp->jited = 1;
  1127. out:
  1128. kfree(ctx.offsets);
  1129. }
  1130. void bpf_jit_free(struct bpf_prog *fp)
  1131. {
  1132. if (fp->jited)
  1133. module_memfree(fp->bpf_func);
  1134. bpf_prog_unlock_free(fp);
  1135. }