bpf_jit.c 36 KB

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