bpf_jit.c 35 KB

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