bpf_jit.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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_mul(unsigned int dst, unsigned int src1,
  323. unsigned int src2, struct jit_ctx *ctx)
  324. {
  325. emit_instr(ctx, mul, dst, src1, src2);
  326. }
  327. static inline void emit_div(unsigned int dst, unsigned int src,
  328. struct jit_ctx *ctx)
  329. {
  330. if (ctx->target != NULL) {
  331. u32 *p = &ctx->target[ctx->idx];
  332. uasm_i_divu(&p, dst, src);
  333. p = &ctx->target[ctx->idx + 1];
  334. uasm_i_mflo(&p, dst);
  335. }
  336. ctx->idx += 2; /* 2 insts */
  337. }
  338. static inline void emit_mod(unsigned int dst, unsigned int src,
  339. struct jit_ctx *ctx)
  340. {
  341. if (ctx->target != NULL) {
  342. u32 *p = &ctx->target[ctx->idx];
  343. uasm_i_divu(&p, dst, src);
  344. p = &ctx->target[ctx->idx + 1];
  345. uasm_i_mfhi(&p, dst);
  346. }
  347. ctx->idx += 2; /* 2 insts */
  348. }
  349. static inline void emit_dsll(unsigned int dst, unsigned int src,
  350. unsigned int sa, struct jit_ctx *ctx)
  351. {
  352. emit_instr(ctx, dsll, dst, src, sa);
  353. }
  354. static inline void emit_dsrl32(unsigned int dst, unsigned int src,
  355. unsigned int sa, struct jit_ctx *ctx)
  356. {
  357. emit_instr(ctx, dsrl32, dst, src, sa);
  358. }
  359. static inline void emit_wsbh(unsigned int dst, unsigned int src,
  360. struct jit_ctx *ctx)
  361. {
  362. emit_instr(ctx, wsbh, dst, src);
  363. }
  364. /* load pointer to register */
  365. static inline void emit_load_ptr(unsigned int dst, unsigned int src,
  366. int imm, struct jit_ctx *ctx)
  367. {
  368. /* src contains the base addr of the 32/64-pointer */
  369. emit_long_instr(ctx, LW, dst, imm, src);
  370. }
  371. /* load a function pointer to register */
  372. static inline void emit_load_func(unsigned int reg, ptr imm,
  373. struct jit_ctx *ctx)
  374. {
  375. if (IS_ENABLED(CONFIG_64BIT)) {
  376. /* At this point imm is always 64-bit */
  377. emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
  378. emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
  379. emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
  380. emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
  381. emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
  382. } else {
  383. emit_load_imm(reg, imm, ctx);
  384. }
  385. }
  386. /* Move to real MIPS register */
  387. static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
  388. {
  389. emit_long_instr(ctx, ADDU, dst, src, r_zero);
  390. }
  391. /* Move to JIT (32-bit) register */
  392. static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
  393. {
  394. emit_addu(dst, src, r_zero, ctx);
  395. }
  396. /* Compute the immediate value for PC-relative branches. */
  397. static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
  398. {
  399. if (ctx->target == NULL)
  400. return 0;
  401. /*
  402. * We want a pc-relative branch. We only do forward branches
  403. * so tgt is always after pc. tgt is the instruction offset
  404. * we want to jump to.
  405. * Branch on MIPS:
  406. * I: target_offset <- sign_extend(offset)
  407. * I+1: PC += target_offset (delay slot)
  408. *
  409. * ctx->idx currently points to the branch instruction
  410. * but the offset is added to the delay slot so we need
  411. * to subtract 4.
  412. */
  413. return ctx->offsets[tgt] -
  414. (ctx->idx * 4 - ctx->prologue_bytes) - 4;
  415. }
  416. static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
  417. unsigned int imm, struct jit_ctx *ctx)
  418. {
  419. if (ctx->target != NULL) {
  420. u32 *p = &ctx->target[ctx->idx];
  421. switch (cond) {
  422. case MIPS_COND_EQ:
  423. uasm_i_beq(&p, reg1, reg2, imm);
  424. break;
  425. case MIPS_COND_NE:
  426. uasm_i_bne(&p, reg1, reg2, imm);
  427. break;
  428. case MIPS_COND_ALL:
  429. uasm_i_b(&p, imm);
  430. break;
  431. default:
  432. pr_warn("%s: Unhandled branch conditional: %d\n",
  433. __func__, cond);
  434. }
  435. }
  436. ctx->idx++;
  437. }
  438. static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
  439. {
  440. emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
  441. }
  442. static inline void emit_jalr(unsigned int link, unsigned int reg,
  443. struct jit_ctx *ctx)
  444. {
  445. emit_instr(ctx, jalr, link, reg);
  446. }
  447. static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
  448. {
  449. emit_instr(ctx, jr, reg);
  450. }
  451. static inline u16 align_sp(unsigned int num)
  452. {
  453. /* Double word alignment for 32-bit, quadword for 64-bit */
  454. unsigned int align = IS_ENABLED(CONFIG_64BIT) ? 16 : 8;
  455. num = (num + (align - 1)) & -align;
  456. return num;
  457. }
  458. static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
  459. {
  460. int i = 0, real_off = 0;
  461. u32 sflags, tmp_flags;
  462. /* Adjust the stack pointer */
  463. if (offset)
  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. if (offset)
  510. emit_stack_offset(align_sp(offset), ctx);
  511. }
  512. static unsigned int get_stack_depth(struct jit_ctx *ctx)
  513. {
  514. int sp_off = 0;
  515. /* How may s* regs do we need to preserved? */
  516. sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * SZREG;
  517. if (ctx->flags & SEEN_MEM)
  518. sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
  519. if (ctx->flags & SEEN_CALL)
  520. sp_off += SZREG; /* Space for our ra register */
  521. return sp_off;
  522. }
  523. static void build_prologue(struct jit_ctx *ctx)
  524. {
  525. int sp_off;
  526. /* Calculate the total offset for the stack pointer */
  527. sp_off = get_stack_depth(ctx);
  528. save_bpf_jit_regs(ctx, sp_off);
  529. if (ctx->flags & SEEN_SKB)
  530. emit_reg_move(r_skb, MIPS_R_A0, ctx);
  531. if (ctx->flags & SEEN_SKB_DATA) {
  532. /* Load packet length */
  533. emit_load(r_skb_len, r_skb, offsetof(struct sk_buff, len),
  534. ctx);
  535. emit_load(r_tmp, r_skb, offsetof(struct sk_buff, data_len),
  536. ctx);
  537. /* Load the data pointer */
  538. emit_load_ptr(r_skb_data, r_skb,
  539. offsetof(struct sk_buff, data), ctx);
  540. /* Load the header length */
  541. emit_subu(r_skb_hl, r_skb_len, r_tmp, ctx);
  542. }
  543. if (ctx->flags & SEEN_X)
  544. emit_jit_reg_move(r_X, r_zero, ctx);
  545. /*
  546. * Do not leak kernel data to userspace, we only need to clear
  547. * r_A if it is ever used. In fact if it is never used, we
  548. * will not save/restore it, so clearing it in this case would
  549. * corrupt the state of the caller.
  550. */
  551. if (bpf_needs_clear_a(&ctx->skf->insns[0]) &&
  552. (ctx->flags & SEEN_A))
  553. emit_jit_reg_move(r_A, r_zero, ctx);
  554. }
  555. static void build_epilogue(struct jit_ctx *ctx)
  556. {
  557. unsigned int sp_off;
  558. /* Calculate the total offset for the stack pointer */
  559. sp_off = get_stack_depth(ctx);
  560. restore_bpf_jit_regs(ctx, sp_off);
  561. /* Return */
  562. emit_jr(r_ra, ctx);
  563. emit_nop(ctx);
  564. }
  565. #define CHOOSE_LOAD_FUNC(K, func) \
  566. ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \
  567. func##_positive)
  568. static int build_body(struct jit_ctx *ctx)
  569. {
  570. const struct bpf_prog *prog = ctx->skf;
  571. const struct sock_filter *inst;
  572. unsigned int i, off, condt;
  573. u32 k, b_off __maybe_unused;
  574. u8 (*sk_load_func)(unsigned long *skb, int offset);
  575. for (i = 0; i < prog->len; i++) {
  576. u16 code;
  577. inst = &(prog->insns[i]);
  578. pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
  579. __func__, inst->code, inst->jt, inst->jf, inst->k);
  580. k = inst->k;
  581. code = bpf_anc_helper(inst);
  582. if (ctx->target == NULL)
  583. ctx->offsets[i] = ctx->idx * 4;
  584. switch (code) {
  585. case BPF_LD | BPF_IMM:
  586. /* A <- k ==> li r_A, k */
  587. ctx->flags |= SEEN_A;
  588. emit_load_imm(r_A, k, ctx);
  589. break;
  590. case BPF_LD | BPF_W | BPF_LEN:
  591. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
  592. /* A <- len ==> lw r_A, offset(skb) */
  593. ctx->flags |= SEEN_SKB | SEEN_A;
  594. off = offsetof(struct sk_buff, len);
  595. emit_load(r_A, r_skb, off, ctx);
  596. break;
  597. case BPF_LD | BPF_MEM:
  598. /* A <- M[k] ==> lw r_A, offset(M) */
  599. ctx->flags |= SEEN_MEM | SEEN_A;
  600. emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
  601. break;
  602. case BPF_LD | BPF_W | BPF_ABS:
  603. /* A <- P[k:4] */
  604. sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_word);
  605. goto load;
  606. case BPF_LD | BPF_H | BPF_ABS:
  607. /* A <- P[k:2] */
  608. sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_half);
  609. goto load;
  610. case BPF_LD | BPF_B | BPF_ABS:
  611. /* A <- P[k:1] */
  612. sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_byte);
  613. load:
  614. emit_load_imm(r_off, k, ctx);
  615. load_common:
  616. ctx->flags |= SEEN_CALL | SEEN_OFF |
  617. SEEN_SKB | SEEN_A | SEEN_SKB_DATA;
  618. emit_load_func(r_s0, (ptr)sk_load_func, ctx);
  619. emit_reg_move(MIPS_R_A0, r_skb, ctx);
  620. emit_jalr(MIPS_R_RA, r_s0, ctx);
  621. /* Load second argument to delay slot */
  622. emit_reg_move(MIPS_R_A1, r_off, ctx);
  623. /* Check the error value */
  624. emit_bcond(MIPS_COND_EQ, r_ret, 0, b_imm(i + 1, ctx),
  625. ctx);
  626. /* Load return register on DS for failures */
  627. emit_reg_move(r_ret, r_zero, ctx);
  628. /* Return with error */
  629. emit_b(b_imm(prog->len, ctx), ctx);
  630. emit_nop(ctx);
  631. break;
  632. case BPF_LD | BPF_W | BPF_IND:
  633. /* A <- P[X + k:4] */
  634. sk_load_func = sk_load_word;
  635. goto load_ind;
  636. case BPF_LD | BPF_H | BPF_IND:
  637. /* A <- P[X + k:2] */
  638. sk_load_func = sk_load_half;
  639. goto load_ind;
  640. case BPF_LD | BPF_B | BPF_IND:
  641. /* A <- P[X + k:1] */
  642. sk_load_func = sk_load_byte;
  643. load_ind:
  644. ctx->flags |= SEEN_OFF | SEEN_X;
  645. emit_addiu(r_off, r_X, k, ctx);
  646. goto load_common;
  647. case BPF_LDX | BPF_IMM:
  648. /* X <- k */
  649. ctx->flags |= SEEN_X;
  650. emit_load_imm(r_X, k, ctx);
  651. break;
  652. case BPF_LDX | BPF_MEM:
  653. /* X <- M[k] */
  654. ctx->flags |= SEEN_X | SEEN_MEM;
  655. emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
  656. break;
  657. case BPF_LDX | BPF_W | BPF_LEN:
  658. /* X <- len */
  659. ctx->flags |= SEEN_X | SEEN_SKB;
  660. off = offsetof(struct sk_buff, len);
  661. emit_load(r_X, r_skb, off, ctx);
  662. break;
  663. case BPF_LDX | BPF_B | BPF_MSH:
  664. /* X <- 4 * (P[k:1] & 0xf) */
  665. ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB;
  666. /* Load offset to a1 */
  667. emit_load_func(r_s0, (ptr)sk_load_byte, ctx);
  668. /*
  669. * This may emit two instructions so it may not fit
  670. * in the delay slot. So use a0 in the delay slot.
  671. */
  672. emit_load_imm(MIPS_R_A1, k, ctx);
  673. emit_jalr(MIPS_R_RA, r_s0, ctx);
  674. emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
  675. /* Check the error value */
  676. emit_bcond(MIPS_COND_NE, r_ret, 0,
  677. b_imm(prog->len, ctx), ctx);
  678. emit_reg_move(r_ret, r_zero, ctx);
  679. /* We are good */
  680. /* X <- P[1:K] & 0xf */
  681. emit_andi(r_X, r_A, 0xf, ctx);
  682. /* X << 2 */
  683. emit_b(b_imm(i + 1, ctx), ctx);
  684. emit_sll(r_X, r_X, 2, ctx); /* delay slot */
  685. break;
  686. case BPF_ST:
  687. /* M[k] <- A */
  688. ctx->flags |= SEEN_MEM | SEEN_A;
  689. emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
  690. break;
  691. case BPF_STX:
  692. /* M[k] <- X */
  693. ctx->flags |= SEEN_MEM | SEEN_X;
  694. emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
  695. break;
  696. case BPF_ALU | BPF_ADD | BPF_K:
  697. /* A += K */
  698. ctx->flags |= SEEN_A;
  699. emit_addiu(r_A, r_A, k, ctx);
  700. break;
  701. case BPF_ALU | BPF_ADD | BPF_X:
  702. /* A += X */
  703. ctx->flags |= SEEN_A | SEEN_X;
  704. emit_addu(r_A, r_A, r_X, ctx);
  705. break;
  706. case BPF_ALU | BPF_SUB | BPF_K:
  707. /* A -= K */
  708. ctx->flags |= SEEN_A;
  709. emit_addiu(r_A, r_A, -k, ctx);
  710. break;
  711. case BPF_ALU | BPF_SUB | BPF_X:
  712. /* A -= X */
  713. ctx->flags |= SEEN_A | SEEN_X;
  714. emit_subu(r_A, r_A, r_X, ctx);
  715. break;
  716. case BPF_ALU | BPF_MUL | BPF_K:
  717. /* A *= K */
  718. /* Load K to scratch register before MUL */
  719. ctx->flags |= SEEN_A;
  720. emit_load_imm(r_s0, k, ctx);
  721. emit_mul(r_A, r_A, r_s0, ctx);
  722. break;
  723. case BPF_ALU | BPF_MUL | BPF_X:
  724. /* A *= X */
  725. ctx->flags |= SEEN_A | SEEN_X;
  726. emit_mul(r_A, r_A, r_X, ctx);
  727. break;
  728. case BPF_ALU | BPF_DIV | BPF_K:
  729. /* A /= k */
  730. if (k == 1)
  731. break;
  732. if (optimize_div(&k)) {
  733. ctx->flags |= SEEN_A;
  734. emit_srl(r_A, r_A, k, ctx);
  735. break;
  736. }
  737. ctx->flags |= SEEN_A;
  738. emit_load_imm(r_s0, k, ctx);
  739. emit_div(r_A, r_s0, ctx);
  740. break;
  741. case BPF_ALU | BPF_MOD | BPF_K:
  742. /* A %= k */
  743. if (k == 1) {
  744. ctx->flags |= SEEN_A;
  745. emit_jit_reg_move(r_A, r_zero, ctx);
  746. } else {
  747. ctx->flags |= SEEN_A;
  748. emit_load_imm(r_s0, k, ctx);
  749. emit_mod(r_A, r_s0, ctx);
  750. }
  751. break;
  752. case BPF_ALU | BPF_DIV | BPF_X:
  753. /* A /= X */
  754. ctx->flags |= SEEN_X | SEEN_A;
  755. /* Check if r_X is zero */
  756. emit_bcond(MIPS_COND_EQ, r_X, r_zero,
  757. b_imm(prog->len, ctx), ctx);
  758. emit_load_imm(r_ret, 0, ctx); /* delay slot */
  759. emit_div(r_A, r_X, ctx);
  760. break;
  761. case BPF_ALU | BPF_MOD | BPF_X:
  762. /* A %= X */
  763. ctx->flags |= SEEN_X | SEEN_A;
  764. /* Check if r_X is zero */
  765. emit_bcond(MIPS_COND_EQ, r_X, r_zero,
  766. b_imm(prog->len, ctx), ctx);
  767. emit_load_imm(r_ret, 0, ctx); /* delay slot */
  768. emit_mod(r_A, r_X, ctx);
  769. break;
  770. case BPF_ALU | BPF_OR | BPF_K:
  771. /* A |= K */
  772. ctx->flags |= SEEN_A;
  773. emit_ori(r_A, r_A, k, ctx);
  774. break;
  775. case BPF_ALU | BPF_OR | BPF_X:
  776. /* A |= X */
  777. ctx->flags |= SEEN_A;
  778. emit_ori(r_A, r_A, r_X, ctx);
  779. break;
  780. case BPF_ALU | BPF_XOR | BPF_K:
  781. /* A ^= k */
  782. ctx->flags |= SEEN_A;
  783. emit_xori(r_A, r_A, k, ctx);
  784. break;
  785. case BPF_ANC | SKF_AD_ALU_XOR_X:
  786. case BPF_ALU | BPF_XOR | BPF_X:
  787. /* A ^= X */
  788. ctx->flags |= SEEN_A;
  789. emit_xor(r_A, r_A, r_X, ctx);
  790. break;
  791. case BPF_ALU | BPF_AND | BPF_K:
  792. /* A &= K */
  793. ctx->flags |= SEEN_A;
  794. emit_andi(r_A, r_A, k, ctx);
  795. break;
  796. case BPF_ALU | BPF_AND | BPF_X:
  797. /* A &= X */
  798. ctx->flags |= SEEN_A | SEEN_X;
  799. emit_and(r_A, r_A, r_X, ctx);
  800. break;
  801. case BPF_ALU | BPF_LSH | BPF_K:
  802. /* A <<= K */
  803. ctx->flags |= SEEN_A;
  804. emit_sll(r_A, r_A, k, ctx);
  805. break;
  806. case BPF_ALU | BPF_LSH | BPF_X:
  807. /* A <<= X */
  808. ctx->flags |= SEEN_A | SEEN_X;
  809. emit_sllv(r_A, r_A, r_X, ctx);
  810. break;
  811. case BPF_ALU | BPF_RSH | BPF_K:
  812. /* A >>= K */
  813. ctx->flags |= SEEN_A;
  814. emit_srl(r_A, r_A, k, ctx);
  815. break;
  816. case BPF_ALU | BPF_RSH | BPF_X:
  817. ctx->flags |= SEEN_A | SEEN_X;
  818. emit_srlv(r_A, r_A, r_X, ctx);
  819. break;
  820. case BPF_ALU | BPF_NEG:
  821. /* A = -A */
  822. ctx->flags |= SEEN_A;
  823. emit_neg(r_A, ctx);
  824. break;
  825. case BPF_JMP | BPF_JA:
  826. /* pc += K */
  827. emit_b(b_imm(i + k + 1, ctx), ctx);
  828. emit_nop(ctx);
  829. break;
  830. case BPF_JMP | BPF_JEQ | BPF_K:
  831. /* pc += ( A == K ) ? pc->jt : pc->jf */
  832. condt = MIPS_COND_EQ | MIPS_COND_K;
  833. goto jmp_cmp;
  834. case BPF_JMP | BPF_JEQ | BPF_X:
  835. ctx->flags |= SEEN_X;
  836. /* pc += ( A == X ) ? pc->jt : pc->jf */
  837. condt = MIPS_COND_EQ | MIPS_COND_X;
  838. goto jmp_cmp;
  839. case BPF_JMP | BPF_JGE | BPF_K:
  840. /* pc += ( A >= K ) ? pc->jt : pc->jf */
  841. condt = MIPS_COND_GE | MIPS_COND_K;
  842. goto jmp_cmp;
  843. case BPF_JMP | BPF_JGE | BPF_X:
  844. ctx->flags |= SEEN_X;
  845. /* pc += ( A >= X ) ? pc->jt : pc->jf */
  846. condt = MIPS_COND_GE | MIPS_COND_X;
  847. goto jmp_cmp;
  848. case BPF_JMP | BPF_JGT | BPF_K:
  849. /* pc += ( A > K ) ? pc->jt : pc->jf */
  850. condt = MIPS_COND_GT | MIPS_COND_K;
  851. goto jmp_cmp;
  852. case BPF_JMP | BPF_JGT | BPF_X:
  853. ctx->flags |= SEEN_X;
  854. /* pc += ( A > X ) ? pc->jt : pc->jf */
  855. condt = MIPS_COND_GT | MIPS_COND_X;
  856. jmp_cmp:
  857. /* Greater or Equal */
  858. if ((condt & MIPS_COND_GE) ||
  859. (condt & MIPS_COND_GT)) {
  860. if (condt & MIPS_COND_K) { /* K */
  861. ctx->flags |= SEEN_A;
  862. emit_sltiu(r_s0, r_A, k, ctx);
  863. } else { /* X */
  864. ctx->flags |= SEEN_A |
  865. SEEN_X;
  866. emit_sltu(r_s0, r_A, r_X, ctx);
  867. }
  868. /* A < (K|X) ? r_scrach = 1 */
  869. b_off = b_imm(i + inst->jf + 1, ctx);
  870. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
  871. ctx);
  872. emit_nop(ctx);
  873. /* A > (K|X) ? scratch = 0 */
  874. if (condt & MIPS_COND_GT) {
  875. /* Checking for equality */
  876. ctx->flags |= SEEN_A | SEEN_X;
  877. if (condt & MIPS_COND_K)
  878. emit_load_imm(r_s0, k, ctx);
  879. else
  880. emit_jit_reg_move(r_s0, r_X,
  881. ctx);
  882. b_off = b_imm(i + inst->jf + 1, ctx);
  883. emit_bcond(MIPS_COND_EQ, r_A, r_s0,
  884. b_off, ctx);
  885. emit_nop(ctx);
  886. /* Finally, A > K|X */
  887. b_off = b_imm(i + inst->jt + 1, ctx);
  888. emit_b(b_off, ctx);
  889. emit_nop(ctx);
  890. } else {
  891. /* A >= (K|X) so jump */
  892. b_off = b_imm(i + inst->jt + 1, ctx);
  893. emit_b(b_off, ctx);
  894. emit_nop(ctx);
  895. }
  896. } else {
  897. /* A == K|X */
  898. if (condt & MIPS_COND_K) { /* K */
  899. ctx->flags |= SEEN_A;
  900. emit_load_imm(r_s0, k, ctx);
  901. /* jump true */
  902. b_off = b_imm(i + inst->jt + 1, ctx);
  903. emit_bcond(MIPS_COND_EQ, r_A, r_s0,
  904. b_off, ctx);
  905. emit_nop(ctx);
  906. /* jump false */
  907. b_off = b_imm(i + inst->jf + 1,
  908. ctx);
  909. emit_bcond(MIPS_COND_NE, r_A, r_s0,
  910. b_off, ctx);
  911. emit_nop(ctx);
  912. } else { /* X */
  913. /* jump true */
  914. ctx->flags |= SEEN_A | SEEN_X;
  915. b_off = b_imm(i + inst->jt + 1,
  916. ctx);
  917. emit_bcond(MIPS_COND_EQ, r_A, r_X,
  918. b_off, ctx);
  919. emit_nop(ctx);
  920. /* jump false */
  921. b_off = b_imm(i + inst->jf + 1, ctx);
  922. emit_bcond(MIPS_COND_NE, r_A, r_X,
  923. b_off, ctx);
  924. emit_nop(ctx);
  925. }
  926. }
  927. break;
  928. case BPF_JMP | BPF_JSET | BPF_K:
  929. ctx->flags |= SEEN_A;
  930. /* pc += (A & K) ? pc -> jt : pc -> jf */
  931. emit_load_imm(r_s1, k, ctx);
  932. emit_and(r_s0, r_A, r_s1, ctx);
  933. /* jump true */
  934. b_off = b_imm(i + inst->jt + 1, ctx);
  935. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
  936. emit_nop(ctx);
  937. /* jump false */
  938. b_off = b_imm(i + inst->jf + 1, ctx);
  939. emit_b(b_off, ctx);
  940. emit_nop(ctx);
  941. break;
  942. case BPF_JMP | BPF_JSET | BPF_X:
  943. ctx->flags |= SEEN_X | SEEN_A;
  944. /* pc += (A & X) ? pc -> jt : pc -> jf */
  945. emit_and(r_s0, r_A, r_X, ctx);
  946. /* jump true */
  947. b_off = b_imm(i + inst->jt + 1, ctx);
  948. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
  949. emit_nop(ctx);
  950. /* jump false */
  951. b_off = b_imm(i + inst->jf + 1, ctx);
  952. emit_b(b_off, ctx);
  953. emit_nop(ctx);
  954. break;
  955. case BPF_RET | BPF_A:
  956. ctx->flags |= SEEN_A;
  957. if (i != prog->len - 1)
  958. /*
  959. * If this is not the last instruction
  960. * then jump to the epilogue
  961. */
  962. emit_b(b_imm(prog->len, ctx), ctx);
  963. emit_reg_move(r_ret, r_A, ctx); /* delay slot */
  964. break;
  965. case BPF_RET | BPF_K:
  966. /*
  967. * It can emit two instructions so it does not fit on
  968. * the delay slot.
  969. */
  970. emit_load_imm(r_ret, k, ctx);
  971. if (i != prog->len - 1) {
  972. /*
  973. * If this is not the last instruction
  974. * then jump to the epilogue
  975. */
  976. emit_b(b_imm(prog->len, ctx), ctx);
  977. emit_nop(ctx);
  978. }
  979. break;
  980. case BPF_MISC | BPF_TAX:
  981. /* X = A */
  982. ctx->flags |= SEEN_X | SEEN_A;
  983. emit_jit_reg_move(r_X, r_A, ctx);
  984. break;
  985. case BPF_MISC | BPF_TXA:
  986. /* A = X */
  987. ctx->flags |= SEEN_A | SEEN_X;
  988. emit_jit_reg_move(r_A, r_X, ctx);
  989. break;
  990. /* AUX */
  991. case BPF_ANC | SKF_AD_PROTOCOL:
  992. /* A = ntohs(skb->protocol */
  993. ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
  994. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  995. protocol) != 2);
  996. off = offsetof(struct sk_buff, protocol);
  997. emit_half_load(r_A, r_skb, off, ctx);
  998. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  999. /* This needs little endian fixup */
  1000. if (cpu_has_wsbh) {
  1001. /* R2 and later have the wsbh instruction */
  1002. emit_wsbh(r_A, r_A, ctx);
  1003. } else {
  1004. /* Get first byte */
  1005. emit_andi(r_tmp_imm, r_A, 0xff, ctx);
  1006. /* Shift it */
  1007. emit_sll(r_tmp, r_tmp_imm, 8, ctx);
  1008. /* Get second byte */
  1009. emit_srl(r_tmp_imm, r_A, 8, ctx);
  1010. emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
  1011. /* Put everyting together in r_A */
  1012. emit_or(r_A, r_tmp, r_tmp_imm, ctx);
  1013. }
  1014. #endif
  1015. break;
  1016. case BPF_ANC | SKF_AD_CPU:
  1017. ctx->flags |= SEEN_A | SEEN_OFF;
  1018. /* A = current_thread_info()->cpu */
  1019. BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
  1020. cpu) != 4);
  1021. off = offsetof(struct thread_info, cpu);
  1022. /* $28/gp points to the thread_info struct */
  1023. emit_load(r_A, 28, off, ctx);
  1024. break;
  1025. case BPF_ANC | SKF_AD_IFINDEX:
  1026. /* A = skb->dev->ifindex */
  1027. ctx->flags |= SEEN_SKB | SEEN_A;
  1028. off = offsetof(struct sk_buff, dev);
  1029. /* Load *dev pointer */
  1030. emit_load_ptr(r_s0, r_skb, off, ctx);
  1031. /* error (0) in the delay slot */
  1032. emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
  1033. b_imm(prog->len, ctx), ctx);
  1034. emit_reg_move(r_ret, r_zero, ctx);
  1035. BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
  1036. ifindex) != 4);
  1037. off = offsetof(struct net_device, ifindex);
  1038. emit_load(r_A, r_s0, off, ctx);
  1039. break;
  1040. case BPF_ANC | SKF_AD_MARK:
  1041. ctx->flags |= SEEN_SKB | SEEN_A;
  1042. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
  1043. off = offsetof(struct sk_buff, mark);
  1044. emit_load(r_A, r_skb, off, ctx);
  1045. break;
  1046. case BPF_ANC | SKF_AD_RXHASH:
  1047. ctx->flags |= SEEN_SKB | SEEN_A;
  1048. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
  1049. off = offsetof(struct sk_buff, hash);
  1050. emit_load(r_A, r_skb, off, ctx);
  1051. break;
  1052. case BPF_ANC | SKF_AD_VLAN_TAG:
  1053. case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
  1054. ctx->flags |= SEEN_SKB | SEEN_A;
  1055. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  1056. vlan_tci) != 2);
  1057. off = offsetof(struct sk_buff, vlan_tci);
  1058. emit_half_load(r_s0, r_skb, off, ctx);
  1059. if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
  1060. emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
  1061. } else {
  1062. emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
  1063. /* return 1 if present */
  1064. emit_sltu(r_A, r_zero, r_A, ctx);
  1065. }
  1066. break;
  1067. case BPF_ANC | SKF_AD_PKTTYPE:
  1068. ctx->flags |= SEEN_SKB;
  1069. emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
  1070. /* Keep only the last 3 bits */
  1071. emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
  1072. #ifdef __BIG_ENDIAN_BITFIELD
  1073. /* Get the actual packet type to the lower 3 bits */
  1074. emit_srl(r_A, r_A, 5, ctx);
  1075. #endif
  1076. break;
  1077. case BPF_ANC | SKF_AD_QUEUE:
  1078. ctx->flags |= SEEN_SKB | SEEN_A;
  1079. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  1080. queue_mapping) != 2);
  1081. BUILD_BUG_ON(offsetof(struct sk_buff,
  1082. queue_mapping) > 0xff);
  1083. off = offsetof(struct sk_buff, queue_mapping);
  1084. emit_half_load(r_A, r_skb, off, ctx);
  1085. break;
  1086. default:
  1087. pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
  1088. inst->code);
  1089. return -1;
  1090. }
  1091. }
  1092. /* compute offsets only during the first pass */
  1093. if (ctx->target == NULL)
  1094. ctx->offsets[i] = ctx->idx * 4;
  1095. return 0;
  1096. }
  1097. int bpf_jit_enable __read_mostly;
  1098. void bpf_jit_compile(struct bpf_prog *fp)
  1099. {
  1100. struct jit_ctx ctx;
  1101. unsigned int alloc_size, tmp_idx;
  1102. if (!bpf_jit_enable)
  1103. return;
  1104. memset(&ctx, 0, sizeof(ctx));
  1105. ctx.offsets = kcalloc(fp->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
  1106. if (ctx.offsets == NULL)
  1107. return;
  1108. ctx.skf = fp;
  1109. if (build_body(&ctx))
  1110. goto out;
  1111. tmp_idx = ctx.idx;
  1112. build_prologue(&ctx);
  1113. ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
  1114. /* just to complete the ctx.idx count */
  1115. build_epilogue(&ctx);
  1116. alloc_size = 4 * ctx.idx;
  1117. ctx.target = module_alloc(alloc_size);
  1118. if (ctx.target == NULL)
  1119. goto out;
  1120. /* Clean it */
  1121. memset(ctx.target, 0, alloc_size);
  1122. ctx.idx = 0;
  1123. /* Generate the actual JIT code */
  1124. build_prologue(&ctx);
  1125. build_body(&ctx);
  1126. build_epilogue(&ctx);
  1127. /* Update the icache */
  1128. flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
  1129. if (bpf_jit_enable > 1)
  1130. /* Dump JIT code */
  1131. bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
  1132. fp->bpf_func = (void *)ctx.target;
  1133. fp->jited = 1;
  1134. out:
  1135. kfree(ctx.offsets);
  1136. }
  1137. void bpf_jit_free(struct bpf_prog *fp)
  1138. {
  1139. if (fp->jited)
  1140. module_memfree(fp->bpf_func);
  1141. bpf_prog_unlock_free(fp);
  1142. }