ebpf_jit.c 50 KB

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