bpf_jit.c 32 KB

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