ebpf_jit.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  1. /*
  2. * Just-In-Time compiler for eBPF filters on MIPS
  3. *
  4. * Copyright (c) 2017 Cavium, Inc.
  5. *
  6. * Based on code from:
  7. *
  8. * Copyright (c) 2014 Imagination Technologies Ltd.
  9. * Author: Markos Chandras <markos.chandras@imgtec.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; version 2 of the License.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/errno.h>
  17. #include <linux/filter.h>
  18. #include <linux/bpf.h>
  19. #include <linux/slab.h>
  20. #include <asm/bitops.h>
  21. #include <asm/byteorder.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/cpu-features.h>
  24. #include <asm/uasm.h>
  25. /* Registers used by JIT */
  26. #define MIPS_R_ZERO 0
  27. #define MIPS_R_AT 1
  28. #define MIPS_R_V0 2 /* BPF_R0 */
  29. #define MIPS_R_V1 3
  30. #define MIPS_R_A0 4 /* BPF_R1 */
  31. #define MIPS_R_A1 5 /* BPF_R2 */
  32. #define MIPS_R_A2 6 /* BPF_R3 */
  33. #define MIPS_R_A3 7 /* BPF_R4 */
  34. #define MIPS_R_A4 8 /* BPF_R5 */
  35. #define MIPS_R_T4 12 /* BPF_AX */
  36. #define MIPS_R_T5 13
  37. #define MIPS_R_T6 14
  38. #define MIPS_R_T7 15
  39. #define MIPS_R_S0 16 /* BPF_R6 */
  40. #define MIPS_R_S1 17 /* BPF_R7 */
  41. #define MIPS_R_S2 18 /* BPF_R8 */
  42. #define MIPS_R_S3 19 /* BPF_R9 */
  43. #define MIPS_R_S4 20 /* BPF_TCC */
  44. #define MIPS_R_S5 21
  45. #define MIPS_R_S6 22
  46. #define MIPS_R_S7 23
  47. #define MIPS_R_T8 24
  48. #define MIPS_R_T9 25
  49. #define MIPS_R_SP 29
  50. #define MIPS_R_RA 31
  51. /* eBPF flags */
  52. #define EBPF_SAVE_S0 BIT(0)
  53. #define EBPF_SAVE_S1 BIT(1)
  54. #define EBPF_SAVE_S2 BIT(2)
  55. #define EBPF_SAVE_S3 BIT(3)
  56. #define EBPF_SAVE_S4 BIT(4)
  57. #define EBPF_SAVE_RA BIT(5)
  58. #define EBPF_SEEN_FP BIT(6)
  59. #define EBPF_SEEN_TC BIT(7)
  60. #define EBPF_TCC_IN_V1 BIT(8)
  61. /*
  62. * For the mips64 ISA, we need to track the value range or type for
  63. * each JIT register. The BPF machine requires zero extended 32-bit
  64. * values, but the mips64 ISA requires sign extended 32-bit values.
  65. * At each point in the BPF program we track the state of every
  66. * register so that we can zero extend or sign extend as the BPF
  67. * semantics require.
  68. */
  69. enum reg_val_type {
  70. /* uninitialized */
  71. REG_UNKNOWN,
  72. /* not known to be 32-bit compatible. */
  73. REG_64BIT,
  74. /* 32-bit compatible, no truncation needed for 64-bit ops. */
  75. REG_64BIT_32BIT,
  76. /* 32-bit compatible, need truncation for 64-bit ops. */
  77. REG_32BIT,
  78. /* 32-bit zero extended. */
  79. REG_32BIT_ZERO_EX,
  80. /* 32-bit no sign/zero extension needed. */
  81. REG_32BIT_POS
  82. };
  83. /*
  84. * high bit of offsets indicates if long branch conversion done at
  85. * this insn.
  86. */
  87. #define OFFSETS_B_CONV BIT(31)
  88. /**
  89. * struct jit_ctx - JIT context
  90. * @skf: The sk_filter
  91. * @stack_size: eBPF stack size
  92. * @idx: Instruction index
  93. * @flags: JIT flags
  94. * @offsets: Instruction offsets
  95. * @target: Memory location for the compiled filter
  96. * @reg_val_types Packed enum reg_val_type for each register.
  97. */
  98. struct jit_ctx {
  99. const struct bpf_prog *skf;
  100. int stack_size;
  101. u32 idx;
  102. u32 flags;
  103. u32 *offsets;
  104. u32 *target;
  105. u64 *reg_val_types;
  106. unsigned int long_b_conversion:1;
  107. unsigned int gen_b_offsets:1;
  108. unsigned int use_bbit_insns:1;
  109. };
  110. static void set_reg_val_type(u64 *rvt, int reg, enum reg_val_type type)
  111. {
  112. *rvt &= ~(7ull << (reg * 3));
  113. *rvt |= ((u64)type << (reg * 3));
  114. }
  115. static enum reg_val_type get_reg_val_type(const struct jit_ctx *ctx,
  116. int index, int reg)
  117. {
  118. return (ctx->reg_val_types[index] >> (reg * 3)) & 7;
  119. }
  120. /* Simply emit the instruction if the JIT memory space has been allocated */
  121. #define emit_instr(ctx, func, ...) \
  122. do { \
  123. if ((ctx)->target != NULL) { \
  124. u32 *p = &(ctx)->target[ctx->idx]; \
  125. uasm_i_##func(&p, ##__VA_ARGS__); \
  126. } \
  127. (ctx)->idx++; \
  128. } while (0)
  129. static unsigned int j_target(struct jit_ctx *ctx, int target_idx)
  130. {
  131. unsigned long target_va, base_va;
  132. unsigned int r;
  133. if (!ctx->target)
  134. return 0;
  135. base_va = (unsigned long)ctx->target;
  136. target_va = base_va + (ctx->offsets[target_idx] & ~OFFSETS_B_CONV);
  137. if ((base_va & ~0x0ffffffful) != (target_va & ~0x0ffffffful))
  138. return (unsigned int)-1;
  139. r = target_va & 0x0ffffffful;
  140. return r;
  141. }
  142. /* Compute the immediate value for PC-relative branches. */
  143. static u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
  144. {
  145. if (!ctx->gen_b_offsets)
  146. return 0;
  147. /*
  148. * We want a pc-relative branch. tgt is the instruction offset
  149. * we want to jump to.
  150. * Branch on MIPS:
  151. * I: target_offset <- sign_extend(offset)
  152. * I+1: PC += target_offset (delay slot)
  153. *
  154. * ctx->idx currently points to the branch instruction
  155. * but the offset is added to the delay slot so we need
  156. * to subtract 4.
  157. */
  158. return (ctx->offsets[tgt] & ~OFFSETS_B_CONV) -
  159. (ctx->idx * 4) - 4;
  160. }
  161. enum which_ebpf_reg {
  162. src_reg,
  163. src_reg_no_fp,
  164. dst_reg,
  165. dst_reg_fp_ok
  166. };
  167. /*
  168. * For eBPF, the register mapping naturally falls out of the
  169. * requirements of eBPF and the MIPS n64 ABI. We don't maintain a
  170. * separate frame pointer, so BPF_REG_10 relative accesses are
  171. * adjusted to be $sp relative.
  172. */
  173. int ebpf_to_mips_reg(struct jit_ctx *ctx, const struct bpf_insn *insn,
  174. enum which_ebpf_reg w)
  175. {
  176. int ebpf_reg = (w == src_reg || w == src_reg_no_fp) ?
  177. insn->src_reg : insn->dst_reg;
  178. switch (ebpf_reg) {
  179. case BPF_REG_0:
  180. return MIPS_R_V0;
  181. case BPF_REG_1:
  182. return MIPS_R_A0;
  183. case BPF_REG_2:
  184. return MIPS_R_A1;
  185. case BPF_REG_3:
  186. return MIPS_R_A2;
  187. case BPF_REG_4:
  188. return MIPS_R_A3;
  189. case BPF_REG_5:
  190. return MIPS_R_A4;
  191. case BPF_REG_6:
  192. ctx->flags |= EBPF_SAVE_S0;
  193. return MIPS_R_S0;
  194. case BPF_REG_7:
  195. ctx->flags |= EBPF_SAVE_S1;
  196. return MIPS_R_S1;
  197. case BPF_REG_8:
  198. ctx->flags |= EBPF_SAVE_S2;
  199. return MIPS_R_S2;
  200. case BPF_REG_9:
  201. ctx->flags |= EBPF_SAVE_S3;
  202. return MIPS_R_S3;
  203. case BPF_REG_10:
  204. if (w == dst_reg || w == src_reg_no_fp)
  205. goto bad_reg;
  206. ctx->flags |= EBPF_SEEN_FP;
  207. /*
  208. * Needs special handling, return something that
  209. * cannot be clobbered just in case.
  210. */
  211. return MIPS_R_ZERO;
  212. case BPF_REG_AX:
  213. return MIPS_R_T4;
  214. default:
  215. bad_reg:
  216. WARN(1, "Illegal bpf reg: %d\n", ebpf_reg);
  217. return -EINVAL;
  218. }
  219. }
  220. /*
  221. * eBPF stack frame will be something like:
  222. *
  223. * Entry $sp ------> +--------------------------------+
  224. * | $ra (optional) |
  225. * +--------------------------------+
  226. * | $s0 (optional) |
  227. * +--------------------------------+
  228. * | $s1 (optional) |
  229. * +--------------------------------+
  230. * | $s2 (optional) |
  231. * +--------------------------------+
  232. * | $s3 (optional) |
  233. * +--------------------------------+
  234. * | $s4 (optional) |
  235. * +--------------------------------+
  236. * | tmp-storage (if $ra saved) |
  237. * $sp + tmp_offset --> +--------------------------------+ <--BPF_REG_10
  238. * | BPF_REG_10 relative storage |
  239. * | MAX_BPF_STACK (optional) |
  240. * | . |
  241. * | . |
  242. * | . |
  243. * $sp --------> +--------------------------------+
  244. *
  245. * If BPF_REG_10 is never referenced, then the MAX_BPF_STACK sized
  246. * area is not allocated.
  247. */
  248. static int gen_int_prologue(struct jit_ctx *ctx)
  249. {
  250. int stack_adjust = 0;
  251. int store_offset;
  252. int locals_size;
  253. if (ctx->flags & EBPF_SAVE_RA)
  254. /*
  255. * If RA we are doing a function call and may need
  256. * extra 8-byte tmp area.
  257. */
  258. stack_adjust += 16;
  259. if (ctx->flags & EBPF_SAVE_S0)
  260. stack_adjust += 8;
  261. if (ctx->flags & EBPF_SAVE_S1)
  262. stack_adjust += 8;
  263. if (ctx->flags & EBPF_SAVE_S2)
  264. stack_adjust += 8;
  265. if (ctx->flags & EBPF_SAVE_S3)
  266. stack_adjust += 8;
  267. if (ctx->flags & EBPF_SAVE_S4)
  268. stack_adjust += 8;
  269. BUILD_BUG_ON(MAX_BPF_STACK & 7);
  270. locals_size = (ctx->flags & EBPF_SEEN_FP) ? MAX_BPF_STACK : 0;
  271. stack_adjust += locals_size;
  272. ctx->stack_size = stack_adjust;
  273. /*
  274. * First instruction initializes the tail call count (TCC).
  275. * On tail call we skip this instruction, and the TCC is
  276. * passed in $v1 from the caller.
  277. */
  278. emit_instr(ctx, daddiu, MIPS_R_V1, MIPS_R_ZERO, MAX_TAIL_CALL_CNT);
  279. if (stack_adjust)
  280. emit_instr(ctx, daddiu, MIPS_R_SP, MIPS_R_SP, -stack_adjust);
  281. else
  282. return 0;
  283. store_offset = stack_adjust - 8;
  284. if (ctx->flags & EBPF_SAVE_RA) {
  285. emit_instr(ctx, sd, MIPS_R_RA, store_offset, MIPS_R_SP);
  286. store_offset -= 8;
  287. }
  288. if (ctx->flags & EBPF_SAVE_S0) {
  289. emit_instr(ctx, sd, MIPS_R_S0, store_offset, MIPS_R_SP);
  290. store_offset -= 8;
  291. }
  292. if (ctx->flags & EBPF_SAVE_S1) {
  293. emit_instr(ctx, sd, MIPS_R_S1, store_offset, MIPS_R_SP);
  294. store_offset -= 8;
  295. }
  296. if (ctx->flags & EBPF_SAVE_S2) {
  297. emit_instr(ctx, sd, MIPS_R_S2, store_offset, MIPS_R_SP);
  298. store_offset -= 8;
  299. }
  300. if (ctx->flags & EBPF_SAVE_S3) {
  301. emit_instr(ctx, sd, MIPS_R_S3, store_offset, MIPS_R_SP);
  302. store_offset -= 8;
  303. }
  304. if (ctx->flags & EBPF_SAVE_S4) {
  305. emit_instr(ctx, sd, MIPS_R_S4, store_offset, MIPS_R_SP);
  306. store_offset -= 8;
  307. }
  308. if ((ctx->flags & EBPF_SEEN_TC) && !(ctx->flags & EBPF_TCC_IN_V1))
  309. emit_instr(ctx, daddu, MIPS_R_S4, MIPS_R_V1, MIPS_R_ZERO);
  310. return 0;
  311. }
  312. static int build_int_epilogue(struct jit_ctx *ctx, int dest_reg)
  313. {
  314. const struct bpf_prog *prog = ctx->skf;
  315. int stack_adjust = ctx->stack_size;
  316. int store_offset = stack_adjust - 8;
  317. int r0 = MIPS_R_V0;
  318. if (dest_reg == MIPS_R_RA &&
  319. get_reg_val_type(ctx, prog->len, BPF_REG_0) == REG_32BIT_ZERO_EX)
  320. /* Don't let zero extended value escape. */
  321. emit_instr(ctx, sll, r0, r0, 0);
  322. if (ctx->flags & EBPF_SAVE_RA) {
  323. emit_instr(ctx, ld, MIPS_R_RA, store_offset, MIPS_R_SP);
  324. store_offset -= 8;
  325. }
  326. if (ctx->flags & EBPF_SAVE_S0) {
  327. emit_instr(ctx, ld, MIPS_R_S0, store_offset, MIPS_R_SP);
  328. store_offset -= 8;
  329. }
  330. if (ctx->flags & EBPF_SAVE_S1) {
  331. emit_instr(ctx, ld, MIPS_R_S1, store_offset, MIPS_R_SP);
  332. store_offset -= 8;
  333. }
  334. if (ctx->flags & EBPF_SAVE_S2) {
  335. emit_instr(ctx, ld, MIPS_R_S2, store_offset, MIPS_R_SP);
  336. store_offset -= 8;
  337. }
  338. if (ctx->flags & EBPF_SAVE_S3) {
  339. emit_instr(ctx, ld, MIPS_R_S3, store_offset, MIPS_R_SP);
  340. store_offset -= 8;
  341. }
  342. if (ctx->flags & EBPF_SAVE_S4) {
  343. emit_instr(ctx, ld, MIPS_R_S4, store_offset, MIPS_R_SP);
  344. store_offset -= 8;
  345. }
  346. emit_instr(ctx, jr, dest_reg);
  347. if (stack_adjust)
  348. emit_instr(ctx, daddiu, MIPS_R_SP, MIPS_R_SP, stack_adjust);
  349. else
  350. emit_instr(ctx, nop);
  351. return 0;
  352. }
  353. static void gen_imm_to_reg(const struct bpf_insn *insn, int reg,
  354. struct jit_ctx *ctx)
  355. {
  356. if (insn->imm >= S16_MIN && insn->imm <= S16_MAX) {
  357. emit_instr(ctx, addiu, reg, MIPS_R_ZERO, insn->imm);
  358. } else {
  359. int lower = (s16)(insn->imm & 0xffff);
  360. int upper = insn->imm - lower;
  361. emit_instr(ctx, lui, reg, upper >> 16);
  362. emit_instr(ctx, addiu, reg, reg, lower);
  363. }
  364. }
  365. static int gen_imm_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  366. int idx)
  367. {
  368. int upper_bound, lower_bound;
  369. int dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  370. if (dst < 0)
  371. return dst;
  372. switch (BPF_OP(insn->code)) {
  373. case BPF_MOV:
  374. case BPF_ADD:
  375. upper_bound = S16_MAX;
  376. lower_bound = S16_MIN;
  377. break;
  378. case BPF_SUB:
  379. upper_bound = -(int)S16_MIN;
  380. lower_bound = -(int)S16_MAX;
  381. break;
  382. case BPF_AND:
  383. case BPF_OR:
  384. case BPF_XOR:
  385. upper_bound = 0xffff;
  386. lower_bound = 0;
  387. break;
  388. case BPF_RSH:
  389. case BPF_LSH:
  390. case BPF_ARSH:
  391. /* Shift amounts are truncated, no need for bounds */
  392. upper_bound = S32_MAX;
  393. lower_bound = S32_MIN;
  394. break;
  395. default:
  396. return -EINVAL;
  397. }
  398. /*
  399. * Immediate move clobbers the register, so no sign/zero
  400. * extension needed.
  401. */
  402. if (BPF_CLASS(insn->code) == BPF_ALU64 &&
  403. BPF_OP(insn->code) != BPF_MOV &&
  404. get_reg_val_type(ctx, idx, insn->dst_reg) == REG_32BIT)
  405. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  406. /* BPF_ALU | BPF_LSH doesn't need separate sign extension */
  407. if (BPF_CLASS(insn->code) == BPF_ALU &&
  408. BPF_OP(insn->code) != BPF_LSH &&
  409. BPF_OP(insn->code) != BPF_MOV &&
  410. get_reg_val_type(ctx, idx, insn->dst_reg) != REG_32BIT)
  411. emit_instr(ctx, sll, dst, dst, 0);
  412. if (insn->imm >= lower_bound && insn->imm <= upper_bound) {
  413. /* single insn immediate case */
  414. switch (BPF_OP(insn->code) | BPF_CLASS(insn->code)) {
  415. case BPF_ALU64 | BPF_MOV:
  416. emit_instr(ctx, daddiu, dst, MIPS_R_ZERO, insn->imm);
  417. break;
  418. case BPF_ALU64 | BPF_AND:
  419. case BPF_ALU | BPF_AND:
  420. emit_instr(ctx, andi, dst, dst, insn->imm);
  421. break;
  422. case BPF_ALU64 | BPF_OR:
  423. case BPF_ALU | BPF_OR:
  424. emit_instr(ctx, ori, dst, dst, insn->imm);
  425. break;
  426. case BPF_ALU64 | BPF_XOR:
  427. case BPF_ALU | BPF_XOR:
  428. emit_instr(ctx, xori, dst, dst, insn->imm);
  429. break;
  430. case BPF_ALU64 | BPF_ADD:
  431. emit_instr(ctx, daddiu, dst, dst, insn->imm);
  432. break;
  433. case BPF_ALU64 | BPF_SUB:
  434. emit_instr(ctx, daddiu, dst, dst, -insn->imm);
  435. break;
  436. case BPF_ALU64 | BPF_RSH:
  437. emit_instr(ctx, dsrl_safe, dst, dst, insn->imm & 0x3f);
  438. break;
  439. case BPF_ALU | BPF_RSH:
  440. emit_instr(ctx, srl, dst, dst, insn->imm & 0x1f);
  441. break;
  442. case BPF_ALU64 | BPF_LSH:
  443. emit_instr(ctx, dsll_safe, dst, dst, insn->imm & 0x3f);
  444. break;
  445. case BPF_ALU | BPF_LSH:
  446. emit_instr(ctx, sll, dst, dst, insn->imm & 0x1f);
  447. break;
  448. case BPF_ALU64 | BPF_ARSH:
  449. emit_instr(ctx, dsra_safe, dst, dst, insn->imm & 0x3f);
  450. break;
  451. case BPF_ALU | BPF_ARSH:
  452. emit_instr(ctx, sra, dst, dst, insn->imm & 0x1f);
  453. break;
  454. case BPF_ALU | BPF_MOV:
  455. emit_instr(ctx, addiu, dst, MIPS_R_ZERO, insn->imm);
  456. break;
  457. case BPF_ALU | BPF_ADD:
  458. emit_instr(ctx, addiu, dst, dst, insn->imm);
  459. break;
  460. case BPF_ALU | BPF_SUB:
  461. emit_instr(ctx, addiu, dst, dst, -insn->imm);
  462. break;
  463. default:
  464. return -EINVAL;
  465. }
  466. } else {
  467. /* multi insn immediate case */
  468. if (BPF_OP(insn->code) == BPF_MOV) {
  469. gen_imm_to_reg(insn, dst, ctx);
  470. } else {
  471. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  472. switch (BPF_OP(insn->code) | BPF_CLASS(insn->code)) {
  473. case BPF_ALU64 | BPF_AND:
  474. case BPF_ALU | BPF_AND:
  475. emit_instr(ctx, and, dst, dst, MIPS_R_AT);
  476. break;
  477. case BPF_ALU64 | BPF_OR:
  478. case BPF_ALU | BPF_OR:
  479. emit_instr(ctx, or, dst, dst, MIPS_R_AT);
  480. break;
  481. case BPF_ALU64 | BPF_XOR:
  482. case BPF_ALU | BPF_XOR:
  483. emit_instr(ctx, xor, dst, dst, MIPS_R_AT);
  484. break;
  485. case BPF_ALU64 | BPF_ADD:
  486. emit_instr(ctx, daddu, dst, dst, MIPS_R_AT);
  487. break;
  488. case BPF_ALU64 | BPF_SUB:
  489. emit_instr(ctx, dsubu, dst, dst, MIPS_R_AT);
  490. break;
  491. case BPF_ALU | BPF_ADD:
  492. emit_instr(ctx, addu, dst, dst, MIPS_R_AT);
  493. break;
  494. case BPF_ALU | BPF_SUB:
  495. emit_instr(ctx, subu, dst, dst, MIPS_R_AT);
  496. break;
  497. default:
  498. return -EINVAL;
  499. }
  500. }
  501. }
  502. return 0;
  503. }
  504. static void emit_const_to_reg(struct jit_ctx *ctx, int dst, u64 value)
  505. {
  506. if (value >= 0xffffffffffff8000ull || value < 0x8000ull) {
  507. emit_instr(ctx, daddiu, dst, MIPS_R_ZERO, (int)value);
  508. } else if (value >= 0xffffffff80000000ull ||
  509. (value < 0x80000000 && value > 0xffff)) {
  510. emit_instr(ctx, lui, dst, (s32)(s16)(value >> 16));
  511. emit_instr(ctx, ori, dst, dst, (unsigned int)(value & 0xffff));
  512. } else {
  513. int i;
  514. bool seen_part = false;
  515. int needed_shift = 0;
  516. for (i = 0; i < 4; i++) {
  517. u64 part = (value >> (16 * (3 - i))) & 0xffff;
  518. if (seen_part && needed_shift > 0 && (part || i == 3)) {
  519. emit_instr(ctx, dsll_safe, dst, dst, needed_shift);
  520. needed_shift = 0;
  521. }
  522. if (part) {
  523. if (i == 0 || (!seen_part && i < 3 && part < 0x8000)) {
  524. emit_instr(ctx, lui, dst, (s32)(s16)part);
  525. needed_shift = -16;
  526. } else {
  527. emit_instr(ctx, ori, dst,
  528. seen_part ? dst : MIPS_R_ZERO,
  529. (unsigned int)part);
  530. }
  531. seen_part = true;
  532. }
  533. if (seen_part)
  534. needed_shift += 16;
  535. }
  536. }
  537. }
  538. static int emit_bpf_tail_call(struct jit_ctx *ctx, int this_idx)
  539. {
  540. int off, b_off;
  541. ctx->flags |= EBPF_SEEN_TC;
  542. /*
  543. * if (index >= array->map.max_entries)
  544. * goto out;
  545. */
  546. off = offsetof(struct bpf_array, map.max_entries);
  547. emit_instr(ctx, lwu, MIPS_R_T5, off, MIPS_R_A1);
  548. emit_instr(ctx, sltu, MIPS_R_AT, MIPS_R_T5, MIPS_R_A2);
  549. b_off = b_imm(this_idx + 1, ctx);
  550. emit_instr(ctx, bne, MIPS_R_AT, MIPS_R_ZERO, b_off);
  551. /*
  552. * if (--TCC < 0)
  553. * goto out;
  554. */
  555. /* Delay slot */
  556. emit_instr(ctx, daddiu, MIPS_R_T5,
  557. (ctx->flags & EBPF_TCC_IN_V1) ? MIPS_R_V1 : MIPS_R_S4, -1);
  558. b_off = b_imm(this_idx + 1, ctx);
  559. emit_instr(ctx, bltz, MIPS_R_T5, b_off);
  560. /*
  561. * prog = array->ptrs[index];
  562. * if (prog == NULL)
  563. * goto out;
  564. */
  565. /* Delay slot */
  566. emit_instr(ctx, dsll, MIPS_R_T8, MIPS_R_A2, 3);
  567. emit_instr(ctx, daddu, MIPS_R_T8, MIPS_R_T8, MIPS_R_A1);
  568. off = offsetof(struct bpf_array, ptrs);
  569. emit_instr(ctx, ld, MIPS_R_AT, off, MIPS_R_T8);
  570. b_off = b_imm(this_idx + 1, ctx);
  571. emit_instr(ctx, beq, MIPS_R_AT, MIPS_R_ZERO, b_off);
  572. /* Delay slot */
  573. emit_instr(ctx, nop);
  574. /* goto *(prog->bpf_func + 4); */
  575. off = offsetof(struct bpf_prog, bpf_func);
  576. emit_instr(ctx, ld, MIPS_R_T9, off, MIPS_R_AT);
  577. /* All systems are go... propagate TCC */
  578. emit_instr(ctx, daddu, MIPS_R_V1, MIPS_R_T5, MIPS_R_ZERO);
  579. /* Skip first instruction (TCC initialization) */
  580. emit_instr(ctx, daddiu, MIPS_R_T9, MIPS_R_T9, 4);
  581. return build_int_epilogue(ctx, MIPS_R_T9);
  582. }
  583. static bool is_bad_offset(int b_off)
  584. {
  585. return b_off > 0x1ffff || b_off < -0x20000;
  586. }
  587. /* Returns the number of insn slots consumed. */
  588. static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  589. int this_idx, int exit_idx)
  590. {
  591. int src, dst, r, td, ts, mem_off, b_off;
  592. bool need_swap, did_move, cmp_eq;
  593. unsigned int target = 0;
  594. u64 t64;
  595. s64 t64s;
  596. int bpf_op = BPF_OP(insn->code);
  597. switch (insn->code) {
  598. case BPF_ALU64 | BPF_ADD | BPF_K: /* ALU64_IMM */
  599. case BPF_ALU64 | BPF_SUB | BPF_K: /* ALU64_IMM */
  600. case BPF_ALU64 | BPF_OR | BPF_K: /* ALU64_IMM */
  601. case BPF_ALU64 | BPF_AND | BPF_K: /* ALU64_IMM */
  602. case BPF_ALU64 | BPF_LSH | BPF_K: /* ALU64_IMM */
  603. case BPF_ALU64 | BPF_RSH | BPF_K: /* ALU64_IMM */
  604. case BPF_ALU64 | BPF_XOR | BPF_K: /* ALU64_IMM */
  605. case BPF_ALU64 | BPF_ARSH | BPF_K: /* ALU64_IMM */
  606. case BPF_ALU64 | BPF_MOV | BPF_K: /* ALU64_IMM */
  607. case BPF_ALU | BPF_MOV | BPF_K: /* ALU32_IMM */
  608. case BPF_ALU | BPF_ADD | BPF_K: /* ALU32_IMM */
  609. case BPF_ALU | BPF_SUB | BPF_K: /* ALU32_IMM */
  610. case BPF_ALU | BPF_OR | BPF_K: /* ALU64_IMM */
  611. case BPF_ALU | BPF_AND | BPF_K: /* ALU64_IMM */
  612. case BPF_ALU | BPF_LSH | BPF_K: /* ALU64_IMM */
  613. case BPF_ALU | BPF_RSH | BPF_K: /* ALU64_IMM */
  614. case BPF_ALU | BPF_XOR | BPF_K: /* ALU64_IMM */
  615. case BPF_ALU | BPF_ARSH | BPF_K: /* ALU64_IMM */
  616. r = gen_imm_insn(insn, ctx, this_idx);
  617. if (r < 0)
  618. return r;
  619. break;
  620. case BPF_ALU64 | BPF_MUL | BPF_K: /* ALU64_IMM */
  621. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  622. if (dst < 0)
  623. return dst;
  624. if (get_reg_val_type(ctx, this_idx, insn->dst_reg) == REG_32BIT)
  625. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  626. if (insn->imm == 1) /* Mult by 1 is a nop */
  627. break;
  628. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  629. emit_instr(ctx, dmultu, MIPS_R_AT, dst);
  630. emit_instr(ctx, mflo, dst);
  631. break;
  632. case BPF_ALU64 | BPF_NEG | BPF_K: /* ALU64_IMM */
  633. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  634. if (dst < 0)
  635. return dst;
  636. if (get_reg_val_type(ctx, this_idx, insn->dst_reg) == REG_32BIT)
  637. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  638. emit_instr(ctx, dsubu, dst, MIPS_R_ZERO, dst);
  639. break;
  640. case BPF_ALU | BPF_MUL | BPF_K: /* ALU_IMM */
  641. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  642. if (dst < 0)
  643. return dst;
  644. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  645. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX) {
  646. /* sign extend */
  647. emit_instr(ctx, sll, dst, dst, 0);
  648. }
  649. if (insn->imm == 1) /* Mult by 1 is a nop */
  650. break;
  651. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  652. emit_instr(ctx, multu, dst, MIPS_R_AT);
  653. emit_instr(ctx, mflo, dst);
  654. break;
  655. case BPF_ALU | BPF_NEG | BPF_K: /* ALU_IMM */
  656. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  657. if (dst < 0)
  658. return dst;
  659. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  660. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX) {
  661. /* sign extend */
  662. emit_instr(ctx, sll, dst, dst, 0);
  663. }
  664. emit_instr(ctx, subu, dst, MIPS_R_ZERO, dst);
  665. break;
  666. case BPF_ALU | BPF_DIV | BPF_K: /* ALU_IMM */
  667. case BPF_ALU | BPF_MOD | BPF_K: /* ALU_IMM */
  668. if (insn->imm == 0)
  669. return -EINVAL;
  670. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  671. if (dst < 0)
  672. return dst;
  673. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  674. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX)
  675. /* sign extend */
  676. emit_instr(ctx, sll, dst, dst, 0);
  677. if (insn->imm == 1) {
  678. /* div by 1 is a nop, mod by 1 is zero */
  679. if (bpf_op == BPF_MOD)
  680. emit_instr(ctx, addu, dst, MIPS_R_ZERO, MIPS_R_ZERO);
  681. break;
  682. }
  683. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  684. emit_instr(ctx, divu, dst, MIPS_R_AT);
  685. if (bpf_op == BPF_DIV)
  686. emit_instr(ctx, mflo, dst);
  687. else
  688. emit_instr(ctx, mfhi, dst);
  689. break;
  690. case BPF_ALU64 | BPF_DIV | BPF_K: /* ALU_IMM */
  691. case BPF_ALU64 | BPF_MOD | BPF_K: /* ALU_IMM */
  692. if (insn->imm == 0)
  693. return -EINVAL;
  694. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  695. if (dst < 0)
  696. return dst;
  697. if (get_reg_val_type(ctx, this_idx, insn->dst_reg) == REG_32BIT)
  698. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  699. if (insn->imm == 1) {
  700. /* div by 1 is a nop, mod by 1 is zero */
  701. if (bpf_op == BPF_MOD)
  702. emit_instr(ctx, addu, dst, MIPS_R_ZERO, MIPS_R_ZERO);
  703. break;
  704. }
  705. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  706. emit_instr(ctx, ddivu, dst, MIPS_R_AT);
  707. if (bpf_op == BPF_DIV)
  708. emit_instr(ctx, mflo, dst);
  709. else
  710. emit_instr(ctx, mfhi, dst);
  711. break;
  712. case BPF_ALU64 | BPF_MOV | BPF_X: /* ALU64_REG */
  713. case BPF_ALU64 | BPF_ADD | BPF_X: /* ALU64_REG */
  714. case BPF_ALU64 | BPF_SUB | BPF_X: /* ALU64_REG */
  715. case BPF_ALU64 | BPF_XOR | BPF_X: /* ALU64_REG */
  716. case BPF_ALU64 | BPF_OR | BPF_X: /* ALU64_REG */
  717. case BPF_ALU64 | BPF_AND | BPF_X: /* ALU64_REG */
  718. case BPF_ALU64 | BPF_MUL | BPF_X: /* ALU64_REG */
  719. case BPF_ALU64 | BPF_DIV | BPF_X: /* ALU64_REG */
  720. case BPF_ALU64 | BPF_MOD | BPF_X: /* ALU64_REG */
  721. case BPF_ALU64 | BPF_LSH | BPF_X: /* ALU64_REG */
  722. case BPF_ALU64 | BPF_RSH | BPF_X: /* ALU64_REG */
  723. case BPF_ALU64 | BPF_ARSH | BPF_X: /* ALU64_REG */
  724. src = ebpf_to_mips_reg(ctx, insn, src_reg);
  725. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  726. if (src < 0 || dst < 0)
  727. return -EINVAL;
  728. if (get_reg_val_type(ctx, this_idx, insn->dst_reg) == REG_32BIT)
  729. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  730. did_move = false;
  731. if (insn->src_reg == BPF_REG_10) {
  732. if (bpf_op == BPF_MOV) {
  733. emit_instr(ctx, daddiu, dst, MIPS_R_SP, MAX_BPF_STACK);
  734. did_move = true;
  735. } else {
  736. emit_instr(ctx, daddiu, MIPS_R_AT, MIPS_R_SP, MAX_BPF_STACK);
  737. src = MIPS_R_AT;
  738. }
  739. } else if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
  740. int tmp_reg = MIPS_R_AT;
  741. if (bpf_op == BPF_MOV) {
  742. tmp_reg = dst;
  743. did_move = true;
  744. }
  745. emit_instr(ctx, daddu, tmp_reg, src, MIPS_R_ZERO);
  746. emit_instr(ctx, dinsu, tmp_reg, MIPS_R_ZERO, 32, 32);
  747. src = MIPS_R_AT;
  748. }
  749. switch (bpf_op) {
  750. case BPF_MOV:
  751. if (!did_move)
  752. emit_instr(ctx, daddu, dst, src, MIPS_R_ZERO);
  753. break;
  754. case BPF_ADD:
  755. emit_instr(ctx, daddu, dst, dst, src);
  756. break;
  757. case BPF_SUB:
  758. emit_instr(ctx, dsubu, dst, dst, src);
  759. break;
  760. case BPF_XOR:
  761. emit_instr(ctx, xor, dst, dst, src);
  762. break;
  763. case BPF_OR:
  764. emit_instr(ctx, or, dst, dst, src);
  765. break;
  766. case BPF_AND:
  767. emit_instr(ctx, and, dst, dst, src);
  768. break;
  769. case BPF_MUL:
  770. emit_instr(ctx, dmultu, dst, src);
  771. emit_instr(ctx, mflo, dst);
  772. break;
  773. case BPF_DIV:
  774. case BPF_MOD:
  775. emit_instr(ctx, ddivu, dst, src);
  776. if (bpf_op == BPF_DIV)
  777. emit_instr(ctx, mflo, dst);
  778. else
  779. emit_instr(ctx, mfhi, dst);
  780. break;
  781. case BPF_LSH:
  782. emit_instr(ctx, dsllv, dst, dst, src);
  783. break;
  784. case BPF_RSH:
  785. emit_instr(ctx, dsrlv, dst, dst, src);
  786. break;
  787. case BPF_ARSH:
  788. emit_instr(ctx, dsrav, dst, dst, src);
  789. break;
  790. default:
  791. pr_err("ALU64_REG NOT HANDLED\n");
  792. return -EINVAL;
  793. }
  794. break;
  795. case BPF_ALU | BPF_MOV | BPF_X: /* ALU_REG */
  796. case BPF_ALU | BPF_ADD | BPF_X: /* ALU_REG */
  797. case BPF_ALU | BPF_SUB | BPF_X: /* ALU_REG */
  798. case BPF_ALU | BPF_XOR | BPF_X: /* ALU_REG */
  799. case BPF_ALU | BPF_OR | BPF_X: /* ALU_REG */
  800. case BPF_ALU | BPF_AND | BPF_X: /* ALU_REG */
  801. case BPF_ALU | BPF_MUL | BPF_X: /* ALU_REG */
  802. case BPF_ALU | BPF_DIV | BPF_X: /* ALU_REG */
  803. case BPF_ALU | BPF_MOD | BPF_X: /* ALU_REG */
  804. case BPF_ALU | BPF_LSH | BPF_X: /* ALU_REG */
  805. case BPF_ALU | BPF_RSH | BPF_X: /* ALU_REG */
  806. src = ebpf_to_mips_reg(ctx, insn, src_reg_no_fp);
  807. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  808. if (src < 0 || dst < 0)
  809. return -EINVAL;
  810. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  811. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX) {
  812. /* sign extend */
  813. emit_instr(ctx, sll, dst, dst, 0);
  814. }
  815. did_move = false;
  816. ts = get_reg_val_type(ctx, this_idx, insn->src_reg);
  817. if (ts == REG_64BIT || ts == REG_32BIT_ZERO_EX) {
  818. int tmp_reg = MIPS_R_AT;
  819. if (bpf_op == BPF_MOV) {
  820. tmp_reg = dst;
  821. did_move = true;
  822. }
  823. /* sign extend */
  824. emit_instr(ctx, sll, tmp_reg, src, 0);
  825. src = MIPS_R_AT;
  826. }
  827. switch (bpf_op) {
  828. case BPF_MOV:
  829. if (!did_move)
  830. emit_instr(ctx, addu, dst, src, MIPS_R_ZERO);
  831. break;
  832. case BPF_ADD:
  833. emit_instr(ctx, addu, dst, dst, src);
  834. break;
  835. case BPF_SUB:
  836. emit_instr(ctx, subu, dst, dst, src);
  837. break;
  838. case BPF_XOR:
  839. emit_instr(ctx, xor, dst, dst, src);
  840. break;
  841. case BPF_OR:
  842. emit_instr(ctx, or, dst, dst, src);
  843. break;
  844. case BPF_AND:
  845. emit_instr(ctx, and, dst, dst, src);
  846. break;
  847. case BPF_MUL:
  848. emit_instr(ctx, mul, dst, dst, src);
  849. break;
  850. case BPF_DIV:
  851. case BPF_MOD:
  852. emit_instr(ctx, divu, dst, src);
  853. if (bpf_op == BPF_DIV)
  854. emit_instr(ctx, mflo, dst);
  855. else
  856. emit_instr(ctx, mfhi, dst);
  857. break;
  858. case BPF_LSH:
  859. emit_instr(ctx, sllv, dst, dst, src);
  860. break;
  861. case BPF_RSH:
  862. emit_instr(ctx, srlv, dst, dst, src);
  863. break;
  864. default:
  865. pr_err("ALU_REG NOT HANDLED\n");
  866. return -EINVAL;
  867. }
  868. break;
  869. case BPF_JMP | BPF_EXIT:
  870. if (this_idx + 1 < exit_idx) {
  871. b_off = b_imm(exit_idx, ctx);
  872. if (is_bad_offset(b_off))
  873. return -E2BIG;
  874. emit_instr(ctx, beq, MIPS_R_ZERO, MIPS_R_ZERO, b_off);
  875. emit_instr(ctx, nop);
  876. }
  877. break;
  878. case BPF_JMP | BPF_JEQ | BPF_K: /* JMP_IMM */
  879. case BPF_JMP | BPF_JNE | BPF_K: /* JMP_IMM */
  880. cmp_eq = (bpf_op == BPF_JEQ);
  881. dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
  882. if (dst < 0)
  883. return dst;
  884. if (insn->imm == 0) {
  885. src = MIPS_R_ZERO;
  886. } else {
  887. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  888. src = MIPS_R_AT;
  889. }
  890. goto jeq_common;
  891. case BPF_JMP | BPF_JEQ | BPF_X: /* JMP_REG */
  892. case BPF_JMP | BPF_JNE | BPF_X:
  893. case BPF_JMP | BPF_JSLT | BPF_X:
  894. case BPF_JMP | BPF_JSLE | BPF_X:
  895. case BPF_JMP | BPF_JSGT | BPF_X:
  896. case BPF_JMP | BPF_JSGE | BPF_X:
  897. case BPF_JMP | BPF_JLT | BPF_X:
  898. case BPF_JMP | BPF_JLE | BPF_X:
  899. case BPF_JMP | BPF_JGT | BPF_X:
  900. case BPF_JMP | BPF_JGE | BPF_X:
  901. case BPF_JMP | BPF_JSET | BPF_X:
  902. src = ebpf_to_mips_reg(ctx, insn, src_reg_no_fp);
  903. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  904. if (src < 0 || dst < 0)
  905. return -EINVAL;
  906. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  907. ts = get_reg_val_type(ctx, this_idx, insn->src_reg);
  908. if (td == REG_32BIT && ts != REG_32BIT) {
  909. emit_instr(ctx, sll, MIPS_R_AT, src, 0);
  910. src = MIPS_R_AT;
  911. } else if (ts == REG_32BIT && td != REG_32BIT) {
  912. emit_instr(ctx, sll, MIPS_R_AT, dst, 0);
  913. dst = MIPS_R_AT;
  914. }
  915. if (bpf_op == BPF_JSET) {
  916. emit_instr(ctx, and, MIPS_R_AT, dst, src);
  917. cmp_eq = false;
  918. dst = MIPS_R_AT;
  919. src = MIPS_R_ZERO;
  920. } else if (bpf_op == BPF_JSGT || bpf_op == BPF_JSLE) {
  921. emit_instr(ctx, dsubu, MIPS_R_AT, dst, src);
  922. if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
  923. b_off = b_imm(exit_idx, ctx);
  924. if (is_bad_offset(b_off))
  925. return -E2BIG;
  926. if (bpf_op == BPF_JSGT)
  927. emit_instr(ctx, blez, MIPS_R_AT, b_off);
  928. else
  929. emit_instr(ctx, bgtz, MIPS_R_AT, b_off);
  930. emit_instr(ctx, nop);
  931. return 2; /* We consumed the exit. */
  932. }
  933. b_off = b_imm(this_idx + insn->off + 1, ctx);
  934. if (is_bad_offset(b_off))
  935. return -E2BIG;
  936. if (bpf_op == BPF_JSGT)
  937. emit_instr(ctx, bgtz, MIPS_R_AT, b_off);
  938. else
  939. emit_instr(ctx, blez, MIPS_R_AT, b_off);
  940. emit_instr(ctx, nop);
  941. break;
  942. } else if (bpf_op == BPF_JSGE || bpf_op == BPF_JSLT) {
  943. emit_instr(ctx, slt, MIPS_R_AT, dst, src);
  944. cmp_eq = bpf_op == BPF_JSGE;
  945. dst = MIPS_R_AT;
  946. src = MIPS_R_ZERO;
  947. } else if (bpf_op == BPF_JGT || bpf_op == BPF_JLE) {
  948. /* dst or src could be AT */
  949. emit_instr(ctx, dsubu, MIPS_R_T8, dst, src);
  950. emit_instr(ctx, sltu, MIPS_R_AT, dst, src);
  951. /* SP known to be non-zero, movz becomes boolean not */
  952. emit_instr(ctx, movz, MIPS_R_T9, MIPS_R_SP, MIPS_R_T8);
  953. emit_instr(ctx, movn, MIPS_R_T9, MIPS_R_ZERO, MIPS_R_T8);
  954. emit_instr(ctx, or, MIPS_R_AT, MIPS_R_T9, MIPS_R_AT);
  955. cmp_eq = bpf_op == BPF_JGT;
  956. dst = MIPS_R_AT;
  957. src = MIPS_R_ZERO;
  958. } else if (bpf_op == BPF_JGE || bpf_op == BPF_JLT) {
  959. emit_instr(ctx, sltu, MIPS_R_AT, dst, src);
  960. cmp_eq = bpf_op == BPF_JGE;
  961. dst = MIPS_R_AT;
  962. src = MIPS_R_ZERO;
  963. } else { /* JNE/JEQ case */
  964. cmp_eq = (bpf_op == BPF_JEQ);
  965. }
  966. jeq_common:
  967. /*
  968. * If the next insn is EXIT and we are jumping arround
  969. * only it, invert the sense of the compare and
  970. * conditionally jump to the exit. Poor man's branch
  971. * chaining.
  972. */
  973. if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
  974. b_off = b_imm(exit_idx, ctx);
  975. if (is_bad_offset(b_off)) {
  976. target = j_target(ctx, exit_idx);
  977. if (target == (unsigned int)-1)
  978. return -E2BIG;
  979. cmp_eq = !cmp_eq;
  980. b_off = 4 * 3;
  981. if (!(ctx->offsets[this_idx] & OFFSETS_B_CONV)) {
  982. ctx->offsets[this_idx] |= OFFSETS_B_CONV;
  983. ctx->long_b_conversion = 1;
  984. }
  985. }
  986. if (cmp_eq)
  987. emit_instr(ctx, bne, dst, src, b_off);
  988. else
  989. emit_instr(ctx, beq, dst, src, b_off);
  990. emit_instr(ctx, nop);
  991. if (ctx->offsets[this_idx] & OFFSETS_B_CONV) {
  992. emit_instr(ctx, j, target);
  993. emit_instr(ctx, nop);
  994. }
  995. return 2; /* We consumed the exit. */
  996. }
  997. b_off = b_imm(this_idx + insn->off + 1, ctx);
  998. if (is_bad_offset(b_off)) {
  999. target = j_target(ctx, this_idx + insn->off + 1);
  1000. if (target == (unsigned int)-1)
  1001. return -E2BIG;
  1002. cmp_eq = !cmp_eq;
  1003. b_off = 4 * 3;
  1004. if (!(ctx->offsets[this_idx] & OFFSETS_B_CONV)) {
  1005. ctx->offsets[this_idx] |= OFFSETS_B_CONV;
  1006. ctx->long_b_conversion = 1;
  1007. }
  1008. }
  1009. if (cmp_eq)
  1010. emit_instr(ctx, beq, dst, src, b_off);
  1011. else
  1012. emit_instr(ctx, bne, dst, src, b_off);
  1013. emit_instr(ctx, nop);
  1014. if (ctx->offsets[this_idx] & OFFSETS_B_CONV) {
  1015. emit_instr(ctx, j, target);
  1016. emit_instr(ctx, nop);
  1017. }
  1018. break;
  1019. case BPF_JMP | BPF_JSGT | BPF_K: /* JMP_IMM */
  1020. case BPF_JMP | BPF_JSGE | BPF_K: /* JMP_IMM */
  1021. case BPF_JMP | BPF_JSLT | BPF_K: /* JMP_IMM */
  1022. case BPF_JMP | BPF_JSLE | BPF_K: /* JMP_IMM */
  1023. cmp_eq = (bpf_op == BPF_JSGE);
  1024. dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
  1025. if (dst < 0)
  1026. return dst;
  1027. if (insn->imm == 0) {
  1028. if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
  1029. b_off = b_imm(exit_idx, ctx);
  1030. if (is_bad_offset(b_off))
  1031. return -E2BIG;
  1032. switch (bpf_op) {
  1033. case BPF_JSGT:
  1034. emit_instr(ctx, blez, dst, b_off);
  1035. break;
  1036. case BPF_JSGE:
  1037. emit_instr(ctx, bltz, dst, b_off);
  1038. break;
  1039. case BPF_JSLT:
  1040. emit_instr(ctx, bgez, dst, b_off);
  1041. break;
  1042. case BPF_JSLE:
  1043. emit_instr(ctx, bgtz, dst, b_off);
  1044. break;
  1045. }
  1046. emit_instr(ctx, nop);
  1047. return 2; /* We consumed the exit. */
  1048. }
  1049. b_off = b_imm(this_idx + insn->off + 1, ctx);
  1050. if (is_bad_offset(b_off))
  1051. return -E2BIG;
  1052. switch (bpf_op) {
  1053. case BPF_JSGT:
  1054. emit_instr(ctx, bgtz, dst, b_off);
  1055. break;
  1056. case BPF_JSGE:
  1057. emit_instr(ctx, bgez, dst, b_off);
  1058. break;
  1059. case BPF_JSLT:
  1060. emit_instr(ctx, bltz, dst, b_off);
  1061. break;
  1062. case BPF_JSLE:
  1063. emit_instr(ctx, blez, dst, b_off);
  1064. break;
  1065. }
  1066. emit_instr(ctx, nop);
  1067. break;
  1068. }
  1069. /*
  1070. * only "LT" compare available, so we must use imm + 1
  1071. * to generate "GT" and imm -1 to generate LE
  1072. */
  1073. if (bpf_op == BPF_JSGT)
  1074. t64s = insn->imm + 1;
  1075. else if (bpf_op == BPF_JSLE)
  1076. t64s = insn->imm + 1;
  1077. else
  1078. t64s = insn->imm;
  1079. cmp_eq = bpf_op == BPF_JSGT || bpf_op == BPF_JSGE;
  1080. if (t64s >= S16_MIN && t64s <= S16_MAX) {
  1081. emit_instr(ctx, slti, MIPS_R_AT, dst, (int)t64s);
  1082. src = MIPS_R_AT;
  1083. dst = MIPS_R_ZERO;
  1084. goto jeq_common;
  1085. }
  1086. emit_const_to_reg(ctx, MIPS_R_AT, (u64)t64s);
  1087. emit_instr(ctx, slt, MIPS_R_AT, dst, MIPS_R_AT);
  1088. src = MIPS_R_AT;
  1089. dst = MIPS_R_ZERO;
  1090. goto jeq_common;
  1091. case BPF_JMP | BPF_JGT | BPF_K:
  1092. case BPF_JMP | BPF_JGE | BPF_K:
  1093. case BPF_JMP | BPF_JLT | BPF_K:
  1094. case BPF_JMP | BPF_JLE | BPF_K:
  1095. cmp_eq = (bpf_op == BPF_JGE);
  1096. dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
  1097. if (dst < 0)
  1098. return dst;
  1099. /*
  1100. * only "LT" compare available, so we must use imm + 1
  1101. * to generate "GT" and imm -1 to generate LE
  1102. */
  1103. if (bpf_op == BPF_JGT)
  1104. t64s = (u64)(u32)(insn->imm) + 1;
  1105. else if (bpf_op == BPF_JLE)
  1106. t64s = (u64)(u32)(insn->imm) + 1;
  1107. else
  1108. t64s = (u64)(u32)(insn->imm);
  1109. cmp_eq = bpf_op == BPF_JGT || bpf_op == BPF_JGE;
  1110. emit_const_to_reg(ctx, MIPS_R_AT, (u64)t64s);
  1111. emit_instr(ctx, sltu, MIPS_R_AT, dst, MIPS_R_AT);
  1112. src = MIPS_R_AT;
  1113. dst = MIPS_R_ZERO;
  1114. goto jeq_common;
  1115. case BPF_JMP | BPF_JSET | BPF_K: /* JMP_IMM */
  1116. dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
  1117. if (dst < 0)
  1118. return dst;
  1119. if (ctx->use_bbit_insns && hweight32((u32)insn->imm) == 1) {
  1120. if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
  1121. b_off = b_imm(exit_idx, ctx);
  1122. if (is_bad_offset(b_off))
  1123. return -E2BIG;
  1124. emit_instr(ctx, bbit0, dst, ffs((u32)insn->imm) - 1, b_off);
  1125. emit_instr(ctx, nop);
  1126. return 2; /* We consumed the exit. */
  1127. }
  1128. b_off = b_imm(this_idx + insn->off + 1, ctx);
  1129. if (is_bad_offset(b_off))
  1130. return -E2BIG;
  1131. emit_instr(ctx, bbit1, dst, ffs((u32)insn->imm) - 1, b_off);
  1132. emit_instr(ctx, nop);
  1133. break;
  1134. }
  1135. t64 = (u32)insn->imm;
  1136. emit_const_to_reg(ctx, MIPS_R_AT, t64);
  1137. emit_instr(ctx, and, MIPS_R_AT, dst, MIPS_R_AT);
  1138. src = MIPS_R_AT;
  1139. dst = MIPS_R_ZERO;
  1140. cmp_eq = false;
  1141. goto jeq_common;
  1142. case BPF_JMP | BPF_JA:
  1143. /*
  1144. * Prefer relative branch for easier debugging, but
  1145. * fall back if needed.
  1146. */
  1147. b_off = b_imm(this_idx + insn->off + 1, ctx);
  1148. if (is_bad_offset(b_off)) {
  1149. target = j_target(ctx, this_idx + insn->off + 1);
  1150. if (target == (unsigned int)-1)
  1151. return -E2BIG;
  1152. emit_instr(ctx, j, target);
  1153. } else {
  1154. emit_instr(ctx, b, b_off);
  1155. }
  1156. emit_instr(ctx, nop);
  1157. break;
  1158. case BPF_LD | BPF_DW | BPF_IMM:
  1159. if (insn->src_reg != 0)
  1160. return -EINVAL;
  1161. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1162. if (dst < 0)
  1163. return dst;
  1164. t64 = ((u64)(u32)insn->imm) | ((u64)(insn + 1)->imm << 32);
  1165. emit_const_to_reg(ctx, dst, t64);
  1166. return 2; /* Double slot insn */
  1167. case BPF_JMP | BPF_CALL:
  1168. ctx->flags |= EBPF_SAVE_RA;
  1169. t64s = (s64)insn->imm + (s64)__bpf_call_base;
  1170. emit_const_to_reg(ctx, MIPS_R_T9, (u64)t64s);
  1171. emit_instr(ctx, jalr, MIPS_R_RA, MIPS_R_T9);
  1172. /* delay slot */
  1173. emit_instr(ctx, nop);
  1174. break;
  1175. case BPF_JMP | BPF_TAIL_CALL:
  1176. if (emit_bpf_tail_call(ctx, this_idx))
  1177. return -EINVAL;
  1178. break;
  1179. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1180. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1181. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1182. if (dst < 0)
  1183. return dst;
  1184. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  1185. if (insn->imm == 64 && td == REG_32BIT)
  1186. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  1187. if (insn->imm != 64 &&
  1188. (td == REG_64BIT || td == REG_32BIT_ZERO_EX)) {
  1189. /* sign extend */
  1190. emit_instr(ctx, sll, dst, dst, 0);
  1191. }
  1192. #ifdef __BIG_ENDIAN
  1193. need_swap = (BPF_SRC(insn->code) == BPF_FROM_LE);
  1194. #else
  1195. need_swap = (BPF_SRC(insn->code) == BPF_FROM_BE);
  1196. #endif
  1197. if (insn->imm == 16) {
  1198. if (need_swap)
  1199. emit_instr(ctx, wsbh, dst, dst);
  1200. emit_instr(ctx, andi, dst, dst, 0xffff);
  1201. } else if (insn->imm == 32) {
  1202. if (need_swap) {
  1203. emit_instr(ctx, wsbh, dst, dst);
  1204. emit_instr(ctx, rotr, dst, dst, 16);
  1205. }
  1206. } else { /* 64-bit*/
  1207. if (need_swap) {
  1208. emit_instr(ctx, dsbh, dst, dst);
  1209. emit_instr(ctx, dshd, dst, dst);
  1210. }
  1211. }
  1212. break;
  1213. case BPF_ST | BPF_B | BPF_MEM:
  1214. case BPF_ST | BPF_H | BPF_MEM:
  1215. case BPF_ST | BPF_W | BPF_MEM:
  1216. case BPF_ST | BPF_DW | BPF_MEM:
  1217. if (insn->dst_reg == BPF_REG_10) {
  1218. ctx->flags |= EBPF_SEEN_FP;
  1219. dst = MIPS_R_SP;
  1220. mem_off = insn->off + MAX_BPF_STACK;
  1221. } else {
  1222. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1223. if (dst < 0)
  1224. return dst;
  1225. mem_off = insn->off;
  1226. }
  1227. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  1228. switch (BPF_SIZE(insn->code)) {
  1229. case BPF_B:
  1230. emit_instr(ctx, sb, MIPS_R_AT, mem_off, dst);
  1231. break;
  1232. case BPF_H:
  1233. emit_instr(ctx, sh, MIPS_R_AT, mem_off, dst);
  1234. break;
  1235. case BPF_W:
  1236. emit_instr(ctx, sw, MIPS_R_AT, mem_off, dst);
  1237. break;
  1238. case BPF_DW:
  1239. emit_instr(ctx, sd, MIPS_R_AT, mem_off, dst);
  1240. break;
  1241. }
  1242. break;
  1243. case BPF_LDX | BPF_B | BPF_MEM:
  1244. case BPF_LDX | BPF_H | BPF_MEM:
  1245. case BPF_LDX | BPF_W | BPF_MEM:
  1246. case BPF_LDX | BPF_DW | BPF_MEM:
  1247. if (insn->src_reg == BPF_REG_10) {
  1248. ctx->flags |= EBPF_SEEN_FP;
  1249. src = MIPS_R_SP;
  1250. mem_off = insn->off + MAX_BPF_STACK;
  1251. } else {
  1252. src = ebpf_to_mips_reg(ctx, insn, src_reg_no_fp);
  1253. if (src < 0)
  1254. return src;
  1255. mem_off = insn->off;
  1256. }
  1257. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1258. if (dst < 0)
  1259. return dst;
  1260. switch (BPF_SIZE(insn->code)) {
  1261. case BPF_B:
  1262. emit_instr(ctx, lbu, dst, mem_off, src);
  1263. break;
  1264. case BPF_H:
  1265. emit_instr(ctx, lhu, dst, mem_off, src);
  1266. break;
  1267. case BPF_W:
  1268. emit_instr(ctx, lw, dst, mem_off, src);
  1269. break;
  1270. case BPF_DW:
  1271. emit_instr(ctx, ld, dst, mem_off, src);
  1272. break;
  1273. }
  1274. break;
  1275. case BPF_STX | BPF_B | BPF_MEM:
  1276. case BPF_STX | BPF_H | BPF_MEM:
  1277. case BPF_STX | BPF_W | BPF_MEM:
  1278. case BPF_STX | BPF_DW | BPF_MEM:
  1279. case BPF_STX | BPF_W | BPF_XADD:
  1280. case BPF_STX | BPF_DW | BPF_XADD:
  1281. if (insn->dst_reg == BPF_REG_10) {
  1282. ctx->flags |= EBPF_SEEN_FP;
  1283. dst = MIPS_R_SP;
  1284. mem_off = insn->off + MAX_BPF_STACK;
  1285. } else {
  1286. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1287. if (dst < 0)
  1288. return dst;
  1289. mem_off = insn->off;
  1290. }
  1291. src = ebpf_to_mips_reg(ctx, insn, src_reg_no_fp);
  1292. if (src < 0)
  1293. return src;
  1294. if (BPF_MODE(insn->code) == BPF_XADD) {
  1295. switch (BPF_SIZE(insn->code)) {
  1296. case BPF_W:
  1297. if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
  1298. emit_instr(ctx, sll, MIPS_R_AT, src, 0);
  1299. src = MIPS_R_AT;
  1300. }
  1301. emit_instr(ctx, ll, MIPS_R_T8, mem_off, dst);
  1302. emit_instr(ctx, addu, MIPS_R_T8, MIPS_R_T8, src);
  1303. emit_instr(ctx, sc, MIPS_R_T8, mem_off, dst);
  1304. /*
  1305. * On failure back up to LL (-4
  1306. * instructions of 4 bytes each
  1307. */
  1308. emit_instr(ctx, beq, MIPS_R_T8, MIPS_R_ZERO, -4 * 4);
  1309. emit_instr(ctx, nop);
  1310. break;
  1311. case BPF_DW:
  1312. if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
  1313. emit_instr(ctx, daddu, MIPS_R_AT, src, MIPS_R_ZERO);
  1314. emit_instr(ctx, dinsu, MIPS_R_AT, MIPS_R_ZERO, 32, 32);
  1315. src = MIPS_R_AT;
  1316. }
  1317. emit_instr(ctx, lld, MIPS_R_T8, mem_off, dst);
  1318. emit_instr(ctx, daddu, MIPS_R_T8, MIPS_R_T8, src);
  1319. emit_instr(ctx, scd, MIPS_R_T8, mem_off, dst);
  1320. emit_instr(ctx, beq, MIPS_R_T8, MIPS_R_ZERO, -4 * 4);
  1321. emit_instr(ctx, nop);
  1322. break;
  1323. }
  1324. } else { /* BPF_MEM */
  1325. switch (BPF_SIZE(insn->code)) {
  1326. case BPF_B:
  1327. emit_instr(ctx, sb, src, mem_off, dst);
  1328. break;
  1329. case BPF_H:
  1330. emit_instr(ctx, sh, src, mem_off, dst);
  1331. break;
  1332. case BPF_W:
  1333. emit_instr(ctx, sw, src, mem_off, dst);
  1334. break;
  1335. case BPF_DW:
  1336. if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
  1337. emit_instr(ctx, daddu, MIPS_R_AT, src, MIPS_R_ZERO);
  1338. emit_instr(ctx, dinsu, MIPS_R_AT, MIPS_R_ZERO, 32, 32);
  1339. src = MIPS_R_AT;
  1340. }
  1341. emit_instr(ctx, sd, src, mem_off, dst);
  1342. break;
  1343. }
  1344. }
  1345. break;
  1346. default:
  1347. pr_err("NOT HANDLED %d - (%02x)\n",
  1348. this_idx, (unsigned int)insn->code);
  1349. return -EINVAL;
  1350. }
  1351. return 1;
  1352. }
  1353. #define RVT_VISITED_MASK 0xc000000000000000ull
  1354. #define RVT_FALL_THROUGH 0x4000000000000000ull
  1355. #define RVT_BRANCH_TAKEN 0x8000000000000000ull
  1356. #define RVT_DONE (RVT_FALL_THROUGH | RVT_BRANCH_TAKEN)
  1357. static int build_int_body(struct jit_ctx *ctx)
  1358. {
  1359. const struct bpf_prog *prog = ctx->skf;
  1360. const struct bpf_insn *insn;
  1361. int i, r;
  1362. for (i = 0; i < prog->len; ) {
  1363. insn = prog->insnsi + i;
  1364. if ((ctx->reg_val_types[i] & RVT_VISITED_MASK) == 0) {
  1365. /* dead instruction, don't emit it. */
  1366. i++;
  1367. continue;
  1368. }
  1369. if (ctx->target == NULL)
  1370. ctx->offsets[i] = (ctx->offsets[i] & OFFSETS_B_CONV) | (ctx->idx * 4);
  1371. r = build_one_insn(insn, ctx, i, prog->len);
  1372. if (r < 0)
  1373. return r;
  1374. i += r;
  1375. }
  1376. /* epilogue offset */
  1377. if (ctx->target == NULL)
  1378. ctx->offsets[i] = ctx->idx * 4;
  1379. /*
  1380. * All exits have an offset of the epilogue, some offsets may
  1381. * not have been set due to banch-around threading, so set
  1382. * them now.
  1383. */
  1384. if (ctx->target == NULL)
  1385. for (i = 0; i < prog->len; i++) {
  1386. insn = prog->insnsi + i;
  1387. if (insn->code == (BPF_JMP | BPF_EXIT))
  1388. ctx->offsets[i] = ctx->idx * 4;
  1389. }
  1390. return 0;
  1391. }
  1392. /* return the last idx processed, or negative for error */
  1393. static int reg_val_propagate_range(struct jit_ctx *ctx, u64 initial_rvt,
  1394. int start_idx, bool follow_taken)
  1395. {
  1396. const struct bpf_prog *prog = ctx->skf;
  1397. const struct bpf_insn *insn;
  1398. u64 exit_rvt = initial_rvt;
  1399. u64 *rvt = ctx->reg_val_types;
  1400. int idx;
  1401. int reg;
  1402. for (idx = start_idx; idx < prog->len; idx++) {
  1403. rvt[idx] = (rvt[idx] & RVT_VISITED_MASK) | exit_rvt;
  1404. insn = prog->insnsi + idx;
  1405. switch (BPF_CLASS(insn->code)) {
  1406. case BPF_ALU:
  1407. switch (BPF_OP(insn->code)) {
  1408. case BPF_ADD:
  1409. case BPF_SUB:
  1410. case BPF_MUL:
  1411. case BPF_DIV:
  1412. case BPF_OR:
  1413. case BPF_AND:
  1414. case BPF_LSH:
  1415. case BPF_RSH:
  1416. case BPF_NEG:
  1417. case BPF_MOD:
  1418. case BPF_XOR:
  1419. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1420. break;
  1421. case BPF_MOV:
  1422. if (BPF_SRC(insn->code)) {
  1423. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1424. } else {
  1425. /* IMM to REG move*/
  1426. if (insn->imm >= 0)
  1427. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1428. else
  1429. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1430. }
  1431. break;
  1432. case BPF_END:
  1433. if (insn->imm == 64)
  1434. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1435. else if (insn->imm == 32)
  1436. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1437. else /* insn->imm == 16 */
  1438. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1439. break;
  1440. }
  1441. rvt[idx] |= RVT_DONE;
  1442. break;
  1443. case BPF_ALU64:
  1444. switch (BPF_OP(insn->code)) {
  1445. case BPF_MOV:
  1446. if (BPF_SRC(insn->code)) {
  1447. /* REG to REG move*/
  1448. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1449. } else {
  1450. /* IMM to REG move*/
  1451. if (insn->imm >= 0)
  1452. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1453. else
  1454. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT_32BIT);
  1455. }
  1456. break;
  1457. default:
  1458. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1459. }
  1460. rvt[idx] |= RVT_DONE;
  1461. break;
  1462. case BPF_LD:
  1463. switch (BPF_SIZE(insn->code)) {
  1464. case BPF_DW:
  1465. if (BPF_MODE(insn->code) == BPF_IMM) {
  1466. s64 val;
  1467. val = (s64)((u32)insn->imm | ((u64)(insn + 1)->imm << 32));
  1468. if (val > 0 && val <= S32_MAX)
  1469. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1470. else if (val >= S32_MIN && val <= S32_MAX)
  1471. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT_32BIT);
  1472. else
  1473. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1474. rvt[idx] |= RVT_DONE;
  1475. idx++;
  1476. } else {
  1477. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1478. }
  1479. break;
  1480. case BPF_B:
  1481. case BPF_H:
  1482. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1483. break;
  1484. case BPF_W:
  1485. if (BPF_MODE(insn->code) == BPF_IMM)
  1486. set_reg_val_type(&exit_rvt, insn->dst_reg,
  1487. insn->imm >= 0 ? REG_32BIT_POS : REG_32BIT);
  1488. else
  1489. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1490. break;
  1491. }
  1492. rvt[idx] |= RVT_DONE;
  1493. break;
  1494. case BPF_LDX:
  1495. switch (BPF_SIZE(insn->code)) {
  1496. case BPF_DW:
  1497. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1498. break;
  1499. case BPF_B:
  1500. case BPF_H:
  1501. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1502. break;
  1503. case BPF_W:
  1504. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1505. break;
  1506. }
  1507. rvt[idx] |= RVT_DONE;
  1508. break;
  1509. case BPF_JMP:
  1510. switch (BPF_OP(insn->code)) {
  1511. case BPF_EXIT:
  1512. rvt[idx] = RVT_DONE | exit_rvt;
  1513. rvt[prog->len] = exit_rvt;
  1514. return idx;
  1515. case BPF_JA:
  1516. rvt[idx] |= RVT_DONE;
  1517. idx += insn->off;
  1518. break;
  1519. case BPF_JEQ:
  1520. case BPF_JGT:
  1521. case BPF_JGE:
  1522. case BPF_JLT:
  1523. case BPF_JLE:
  1524. case BPF_JSET:
  1525. case BPF_JNE:
  1526. case BPF_JSGT:
  1527. case BPF_JSGE:
  1528. case BPF_JSLT:
  1529. case BPF_JSLE:
  1530. if (follow_taken) {
  1531. rvt[idx] |= RVT_BRANCH_TAKEN;
  1532. idx += insn->off;
  1533. follow_taken = false;
  1534. } else {
  1535. rvt[idx] |= RVT_FALL_THROUGH;
  1536. }
  1537. break;
  1538. case BPF_CALL:
  1539. set_reg_val_type(&exit_rvt, BPF_REG_0, REG_64BIT);
  1540. /* Upon call return, argument registers are clobbered. */
  1541. for (reg = BPF_REG_0; reg <= BPF_REG_5; reg++)
  1542. set_reg_val_type(&exit_rvt, reg, REG_64BIT);
  1543. rvt[idx] |= RVT_DONE;
  1544. break;
  1545. default:
  1546. WARN(1, "Unhandled BPF_JMP case.\n");
  1547. rvt[idx] |= RVT_DONE;
  1548. break;
  1549. }
  1550. break;
  1551. default:
  1552. rvt[idx] |= RVT_DONE;
  1553. break;
  1554. }
  1555. }
  1556. return idx;
  1557. }
  1558. /*
  1559. * Track the value range (i.e. 32-bit vs. 64-bit) of each register at
  1560. * each eBPF insn. This allows unneeded sign and zero extension
  1561. * operations to be omitted.
  1562. *
  1563. * Doesn't handle yet confluence of control paths with conflicting
  1564. * ranges, but it is good enough for most sane code.
  1565. */
  1566. static int reg_val_propagate(struct jit_ctx *ctx)
  1567. {
  1568. const struct bpf_prog *prog = ctx->skf;
  1569. u64 exit_rvt;
  1570. int reg;
  1571. int i;
  1572. /*
  1573. * 11 registers * 3 bits/reg leaves top bits free for other
  1574. * uses. Bit-62..63 used to see if we have visited an insn.
  1575. */
  1576. exit_rvt = 0;
  1577. /* Upon entry, argument registers are 64-bit. */
  1578. for (reg = BPF_REG_1; reg <= BPF_REG_5; reg++)
  1579. set_reg_val_type(&exit_rvt, reg, REG_64BIT);
  1580. /*
  1581. * First follow all conditional branches on the fall-through
  1582. * edge of control flow..
  1583. */
  1584. reg_val_propagate_range(ctx, exit_rvt, 0, false);
  1585. restart_search:
  1586. /*
  1587. * Then repeatedly find the first conditional branch where
  1588. * both edges of control flow have not been taken, and follow
  1589. * the branch taken edge. We will end up restarting the
  1590. * search once per conditional branch insn.
  1591. */
  1592. for (i = 0; i < prog->len; i++) {
  1593. u64 rvt = ctx->reg_val_types[i];
  1594. if ((rvt & RVT_VISITED_MASK) == RVT_DONE ||
  1595. (rvt & RVT_VISITED_MASK) == 0)
  1596. continue;
  1597. if ((rvt & RVT_VISITED_MASK) == RVT_FALL_THROUGH) {
  1598. reg_val_propagate_range(ctx, rvt & ~RVT_VISITED_MASK, i, true);
  1599. } else { /* RVT_BRANCH_TAKEN */
  1600. WARN(1, "Unexpected RVT_BRANCH_TAKEN case.\n");
  1601. reg_val_propagate_range(ctx, rvt & ~RVT_VISITED_MASK, i, false);
  1602. }
  1603. goto restart_search;
  1604. }
  1605. /*
  1606. * Eventually all conditional branches have been followed on
  1607. * both branches and we are done. Any insn that has not been
  1608. * visited at this point is dead.
  1609. */
  1610. return 0;
  1611. }
  1612. static void jit_fill_hole(void *area, unsigned int size)
  1613. {
  1614. u32 *p;
  1615. /* We are guaranteed to have aligned memory. */
  1616. for (p = area; size >= sizeof(u32); size -= sizeof(u32))
  1617. uasm_i_break(&p, BRK_BUG); /* Increments p */
  1618. }
  1619. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1620. {
  1621. struct bpf_prog *orig_prog = prog;
  1622. bool tmp_blinded = false;
  1623. struct bpf_prog *tmp;
  1624. struct bpf_binary_header *header = NULL;
  1625. struct jit_ctx ctx;
  1626. unsigned int image_size;
  1627. u8 *image_ptr;
  1628. if (!prog->jit_requested || !cpu_has_mips64r2)
  1629. return prog;
  1630. tmp = bpf_jit_blind_constants(prog);
  1631. /* If blinding was requested and we failed during blinding,
  1632. * we must fall back to the interpreter.
  1633. */
  1634. if (IS_ERR(tmp))
  1635. return orig_prog;
  1636. if (tmp != prog) {
  1637. tmp_blinded = true;
  1638. prog = tmp;
  1639. }
  1640. memset(&ctx, 0, sizeof(ctx));
  1641. preempt_disable();
  1642. switch (current_cpu_type()) {
  1643. case CPU_CAVIUM_OCTEON:
  1644. case CPU_CAVIUM_OCTEON_PLUS:
  1645. case CPU_CAVIUM_OCTEON2:
  1646. case CPU_CAVIUM_OCTEON3:
  1647. ctx.use_bbit_insns = 1;
  1648. break;
  1649. default:
  1650. ctx.use_bbit_insns = 0;
  1651. }
  1652. preempt_enable();
  1653. ctx.offsets = kcalloc(prog->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
  1654. if (ctx.offsets == NULL)
  1655. goto out_err;
  1656. ctx.reg_val_types = kcalloc(prog->len + 1, sizeof(*ctx.reg_val_types), GFP_KERNEL);
  1657. if (ctx.reg_val_types == NULL)
  1658. goto out_err;
  1659. ctx.skf = prog;
  1660. if (reg_val_propagate(&ctx))
  1661. goto out_err;
  1662. /*
  1663. * First pass discovers used resources and instruction offsets
  1664. * assuming short branches are used.
  1665. */
  1666. if (build_int_body(&ctx))
  1667. goto out_err;
  1668. /*
  1669. * If no calls are made (EBPF_SAVE_RA), then tail call count
  1670. * in $v1, else we must save in n$s4.
  1671. */
  1672. if (ctx.flags & EBPF_SEEN_TC) {
  1673. if (ctx.flags & EBPF_SAVE_RA)
  1674. ctx.flags |= EBPF_SAVE_S4;
  1675. else
  1676. ctx.flags |= EBPF_TCC_IN_V1;
  1677. }
  1678. /*
  1679. * Second pass generates offsets, if any branches are out of
  1680. * range a jump-around long sequence is generated, and we have
  1681. * to try again from the beginning to generate the new
  1682. * offsets. This is done until no additional conversions are
  1683. * necessary.
  1684. */
  1685. do {
  1686. ctx.idx = 0;
  1687. ctx.gen_b_offsets = 1;
  1688. ctx.long_b_conversion = 0;
  1689. if (gen_int_prologue(&ctx))
  1690. goto out_err;
  1691. if (build_int_body(&ctx))
  1692. goto out_err;
  1693. if (build_int_epilogue(&ctx, MIPS_R_RA))
  1694. goto out_err;
  1695. } while (ctx.long_b_conversion);
  1696. image_size = 4 * ctx.idx;
  1697. header = bpf_jit_binary_alloc(image_size, &image_ptr,
  1698. sizeof(u32), jit_fill_hole);
  1699. if (header == NULL)
  1700. goto out_err;
  1701. ctx.target = (u32 *)image_ptr;
  1702. /* Third pass generates the code */
  1703. ctx.idx = 0;
  1704. if (gen_int_prologue(&ctx))
  1705. goto out_err;
  1706. if (build_int_body(&ctx))
  1707. goto out_err;
  1708. if (build_int_epilogue(&ctx, MIPS_R_RA))
  1709. goto out_err;
  1710. /* Update the icache */
  1711. flush_icache_range((unsigned long)ctx.target,
  1712. (unsigned long)(ctx.target + ctx.idx * sizeof(u32)));
  1713. if (bpf_jit_enable > 1)
  1714. /* Dump JIT code */
  1715. bpf_jit_dump(prog->len, image_size, 2, ctx.target);
  1716. bpf_jit_binary_lock_ro(header);
  1717. prog->bpf_func = (void *)ctx.target;
  1718. prog->jited = 1;
  1719. prog->jited_len = image_size;
  1720. out_normal:
  1721. if (tmp_blinded)
  1722. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  1723. tmp : orig_prog);
  1724. kfree(ctx.offsets);
  1725. kfree(ctx.reg_val_types);
  1726. return prog;
  1727. out_err:
  1728. prog = orig_prog;
  1729. if (header)
  1730. bpf_jit_binary_free(header);
  1731. goto out_normal;
  1732. }