bpf_jit.c 35 KB

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