ebpf_jit.c 54 KB

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