bpf_jit.c 32 KB

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