bpf_jit_comp64.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* BPF JIT compiler for RV64G
  3. *
  4. * Copyright(c) 2019 Björn Töpel <[email protected]>
  5. *
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bpf.h>
  9. #include <linux/filter.h>
  10. #include "bpf_jit.h"
  11. #define RV_REG_TCC RV_REG_A6
  12. #define RV_REG_TCC_SAVED RV_REG_S6 /* Store A6 in S6 if program do calls */
  13. static const int regmap[] = {
  14. [BPF_REG_0] = RV_REG_A5,
  15. [BPF_REG_1] = RV_REG_A0,
  16. [BPF_REG_2] = RV_REG_A1,
  17. [BPF_REG_3] = RV_REG_A2,
  18. [BPF_REG_4] = RV_REG_A3,
  19. [BPF_REG_5] = RV_REG_A4,
  20. [BPF_REG_6] = RV_REG_S1,
  21. [BPF_REG_7] = RV_REG_S2,
  22. [BPF_REG_8] = RV_REG_S3,
  23. [BPF_REG_9] = RV_REG_S4,
  24. [BPF_REG_FP] = RV_REG_S5,
  25. [BPF_REG_AX] = RV_REG_T0,
  26. };
  27. static const int pt_regmap[] = {
  28. [RV_REG_A0] = offsetof(struct pt_regs, a0),
  29. [RV_REG_A1] = offsetof(struct pt_regs, a1),
  30. [RV_REG_A2] = offsetof(struct pt_regs, a2),
  31. [RV_REG_A3] = offsetof(struct pt_regs, a3),
  32. [RV_REG_A4] = offsetof(struct pt_regs, a4),
  33. [RV_REG_A5] = offsetof(struct pt_regs, a5),
  34. [RV_REG_S1] = offsetof(struct pt_regs, s1),
  35. [RV_REG_S2] = offsetof(struct pt_regs, s2),
  36. [RV_REG_S3] = offsetof(struct pt_regs, s3),
  37. [RV_REG_S4] = offsetof(struct pt_regs, s4),
  38. [RV_REG_S5] = offsetof(struct pt_regs, s5),
  39. [RV_REG_T0] = offsetof(struct pt_regs, t0),
  40. };
  41. enum {
  42. RV_CTX_F_SEEN_TAIL_CALL = 0,
  43. RV_CTX_F_SEEN_CALL = RV_REG_RA,
  44. RV_CTX_F_SEEN_S1 = RV_REG_S1,
  45. RV_CTX_F_SEEN_S2 = RV_REG_S2,
  46. RV_CTX_F_SEEN_S3 = RV_REG_S3,
  47. RV_CTX_F_SEEN_S4 = RV_REG_S4,
  48. RV_CTX_F_SEEN_S5 = RV_REG_S5,
  49. RV_CTX_F_SEEN_S6 = RV_REG_S6,
  50. };
  51. static u8 bpf_to_rv_reg(int bpf_reg, struct rv_jit_context *ctx)
  52. {
  53. u8 reg = regmap[bpf_reg];
  54. switch (reg) {
  55. case RV_CTX_F_SEEN_S1:
  56. case RV_CTX_F_SEEN_S2:
  57. case RV_CTX_F_SEEN_S3:
  58. case RV_CTX_F_SEEN_S4:
  59. case RV_CTX_F_SEEN_S5:
  60. case RV_CTX_F_SEEN_S6:
  61. __set_bit(reg, &ctx->flags);
  62. }
  63. return reg;
  64. };
  65. static bool seen_reg(int reg, struct rv_jit_context *ctx)
  66. {
  67. switch (reg) {
  68. case RV_CTX_F_SEEN_CALL:
  69. case RV_CTX_F_SEEN_S1:
  70. case RV_CTX_F_SEEN_S2:
  71. case RV_CTX_F_SEEN_S3:
  72. case RV_CTX_F_SEEN_S4:
  73. case RV_CTX_F_SEEN_S5:
  74. case RV_CTX_F_SEEN_S6:
  75. return test_bit(reg, &ctx->flags);
  76. }
  77. return false;
  78. }
  79. static void mark_fp(struct rv_jit_context *ctx)
  80. {
  81. __set_bit(RV_CTX_F_SEEN_S5, &ctx->flags);
  82. }
  83. static void mark_call(struct rv_jit_context *ctx)
  84. {
  85. __set_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
  86. }
  87. static bool seen_call(struct rv_jit_context *ctx)
  88. {
  89. return test_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
  90. }
  91. static void mark_tail_call(struct rv_jit_context *ctx)
  92. {
  93. __set_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
  94. }
  95. static bool seen_tail_call(struct rv_jit_context *ctx)
  96. {
  97. return test_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
  98. }
  99. static u8 rv_tail_call_reg(struct rv_jit_context *ctx)
  100. {
  101. mark_tail_call(ctx);
  102. if (seen_call(ctx)) {
  103. __set_bit(RV_CTX_F_SEEN_S6, &ctx->flags);
  104. return RV_REG_S6;
  105. }
  106. return RV_REG_A6;
  107. }
  108. static bool is_32b_int(s64 val)
  109. {
  110. return -(1L << 31) <= val && val < (1L << 31);
  111. }
  112. static bool in_auipc_jalr_range(s64 val)
  113. {
  114. /*
  115. * auipc+jalr can reach any signed PC-relative offset in the range
  116. * [-2^31 - 2^11, 2^31 - 2^11).
  117. */
  118. return (-(1L << 31) - (1L << 11)) <= val &&
  119. val < ((1L << 31) - (1L << 11));
  120. }
  121. /* Emit fixed-length instructions for address */
  122. static int emit_addr(u8 rd, u64 addr, bool extra_pass, struct rv_jit_context *ctx)
  123. {
  124. u64 ip = (u64)(ctx->insns + ctx->ninsns);
  125. s64 off = addr - ip;
  126. s64 upper = (off + (1 << 11)) >> 12;
  127. s64 lower = off & 0xfff;
  128. if (extra_pass && !in_auipc_jalr_range(off)) {
  129. pr_err("bpf-jit: target offset 0x%llx is out of range\n", off);
  130. return -ERANGE;
  131. }
  132. emit(rv_auipc(rd, upper), ctx);
  133. emit(rv_addi(rd, rd, lower), ctx);
  134. return 0;
  135. }
  136. /* Emit variable-length instructions for 32-bit and 64-bit imm */
  137. static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx)
  138. {
  139. /* Note that the immediate from the add is sign-extended,
  140. * which means that we need to compensate this by adding 2^12,
  141. * when the 12th bit is set. A simpler way of doing this, and
  142. * getting rid of the check, is to just add 2**11 before the
  143. * shift. The "Loading a 32-Bit constant" example from the
  144. * "Computer Organization and Design, RISC-V edition" book by
  145. * Patterson/Hennessy highlights this fact.
  146. *
  147. * This also means that we need to process LSB to MSB.
  148. */
  149. s64 upper = (val + (1 << 11)) >> 12;
  150. /* Sign-extend lower 12 bits to 64 bits since immediates for li, addiw,
  151. * and addi are signed and RVC checks will perform signed comparisons.
  152. */
  153. s64 lower = ((val & 0xfff) << 52) >> 52;
  154. int shift;
  155. if (is_32b_int(val)) {
  156. if (upper)
  157. emit_lui(rd, upper, ctx);
  158. if (!upper) {
  159. emit_li(rd, lower, ctx);
  160. return;
  161. }
  162. emit_addiw(rd, rd, lower, ctx);
  163. return;
  164. }
  165. shift = __ffs(upper);
  166. upper >>= shift;
  167. shift += 12;
  168. emit_imm(rd, upper, ctx);
  169. emit_slli(rd, rd, shift, ctx);
  170. if (lower)
  171. emit_addi(rd, rd, lower, ctx);
  172. }
  173. static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
  174. {
  175. int stack_adjust = ctx->stack_size, store_offset = stack_adjust - 8;
  176. if (seen_reg(RV_REG_RA, ctx)) {
  177. emit_ld(RV_REG_RA, store_offset, RV_REG_SP, ctx);
  178. store_offset -= 8;
  179. }
  180. emit_ld(RV_REG_FP, store_offset, RV_REG_SP, ctx);
  181. store_offset -= 8;
  182. if (seen_reg(RV_REG_S1, ctx)) {
  183. emit_ld(RV_REG_S1, store_offset, RV_REG_SP, ctx);
  184. store_offset -= 8;
  185. }
  186. if (seen_reg(RV_REG_S2, ctx)) {
  187. emit_ld(RV_REG_S2, store_offset, RV_REG_SP, ctx);
  188. store_offset -= 8;
  189. }
  190. if (seen_reg(RV_REG_S3, ctx)) {
  191. emit_ld(RV_REG_S3, store_offset, RV_REG_SP, ctx);
  192. store_offset -= 8;
  193. }
  194. if (seen_reg(RV_REG_S4, ctx)) {
  195. emit_ld(RV_REG_S4, store_offset, RV_REG_SP, ctx);
  196. store_offset -= 8;
  197. }
  198. if (seen_reg(RV_REG_S5, ctx)) {
  199. emit_ld(RV_REG_S5, store_offset, RV_REG_SP, ctx);
  200. store_offset -= 8;
  201. }
  202. if (seen_reg(RV_REG_S6, ctx)) {
  203. emit_ld(RV_REG_S6, store_offset, RV_REG_SP, ctx);
  204. store_offset -= 8;
  205. }
  206. emit_addi(RV_REG_SP, RV_REG_SP, stack_adjust, ctx);
  207. /* Set return value. */
  208. if (!is_tail_call)
  209. emit_addiw(RV_REG_A0, RV_REG_A5, 0, ctx);
  210. emit_jalr(RV_REG_ZERO, is_tail_call ? RV_REG_T3 : RV_REG_RA,
  211. is_tail_call ? 4 : 0, /* skip TCC init */
  212. ctx);
  213. }
  214. static void emit_bcc(u8 cond, u8 rd, u8 rs, int rvoff,
  215. struct rv_jit_context *ctx)
  216. {
  217. switch (cond) {
  218. case BPF_JEQ:
  219. emit(rv_beq(rd, rs, rvoff >> 1), ctx);
  220. return;
  221. case BPF_JGT:
  222. emit(rv_bltu(rs, rd, rvoff >> 1), ctx);
  223. return;
  224. case BPF_JLT:
  225. emit(rv_bltu(rd, rs, rvoff >> 1), ctx);
  226. return;
  227. case BPF_JGE:
  228. emit(rv_bgeu(rd, rs, rvoff >> 1), ctx);
  229. return;
  230. case BPF_JLE:
  231. emit(rv_bgeu(rs, rd, rvoff >> 1), ctx);
  232. return;
  233. case BPF_JNE:
  234. emit(rv_bne(rd, rs, rvoff >> 1), ctx);
  235. return;
  236. case BPF_JSGT:
  237. emit(rv_blt(rs, rd, rvoff >> 1), ctx);
  238. return;
  239. case BPF_JSLT:
  240. emit(rv_blt(rd, rs, rvoff >> 1), ctx);
  241. return;
  242. case BPF_JSGE:
  243. emit(rv_bge(rd, rs, rvoff >> 1), ctx);
  244. return;
  245. case BPF_JSLE:
  246. emit(rv_bge(rs, rd, rvoff >> 1), ctx);
  247. }
  248. }
  249. static void emit_branch(u8 cond, u8 rd, u8 rs, int rvoff,
  250. struct rv_jit_context *ctx)
  251. {
  252. s64 upper, lower;
  253. if (is_13b_int(rvoff)) {
  254. emit_bcc(cond, rd, rs, rvoff, ctx);
  255. return;
  256. }
  257. /* Adjust for jal */
  258. rvoff -= 4;
  259. /* Transform, e.g.:
  260. * bne rd,rs,foo
  261. * to
  262. * beq rd,rs,<.L1>
  263. * (auipc foo)
  264. * jal(r) foo
  265. * .L1
  266. */
  267. cond = invert_bpf_cond(cond);
  268. if (is_21b_int(rvoff)) {
  269. emit_bcc(cond, rd, rs, 8, ctx);
  270. emit(rv_jal(RV_REG_ZERO, rvoff >> 1), ctx);
  271. return;
  272. }
  273. /* 32b No need for an additional rvoff adjustment, since we
  274. * get that from the auipc at PC', where PC = PC' + 4.
  275. */
  276. upper = (rvoff + (1 << 11)) >> 12;
  277. lower = rvoff & 0xfff;
  278. emit_bcc(cond, rd, rs, 12, ctx);
  279. emit(rv_auipc(RV_REG_T1, upper), ctx);
  280. emit(rv_jalr(RV_REG_ZERO, RV_REG_T1, lower), ctx);
  281. }
  282. static void emit_zext_32(u8 reg, struct rv_jit_context *ctx)
  283. {
  284. emit_slli(reg, reg, 32, ctx);
  285. emit_srli(reg, reg, 32, ctx);
  286. }
  287. static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
  288. {
  289. int tc_ninsn, off, start_insn = ctx->ninsns;
  290. u8 tcc = rv_tail_call_reg(ctx);
  291. /* a0: &ctx
  292. * a1: &array
  293. * a2: index
  294. *
  295. * if (index >= array->map.max_entries)
  296. * goto out;
  297. */
  298. tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] :
  299. ctx->offset[0];
  300. emit_zext_32(RV_REG_A2, ctx);
  301. off = offsetof(struct bpf_array, map.max_entries);
  302. if (is_12b_check(off, insn))
  303. return -1;
  304. emit(rv_lwu(RV_REG_T1, off, RV_REG_A1), ctx);
  305. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  306. emit_branch(BPF_JGE, RV_REG_A2, RV_REG_T1, off, ctx);
  307. /* if (--TCC < 0)
  308. * goto out;
  309. */
  310. emit_addi(RV_REG_TCC, tcc, -1, ctx);
  311. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  312. emit_branch(BPF_JSLT, RV_REG_TCC, RV_REG_ZERO, off, ctx);
  313. /* prog = array->ptrs[index];
  314. * if (!prog)
  315. * goto out;
  316. */
  317. emit_slli(RV_REG_T2, RV_REG_A2, 3, ctx);
  318. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_A1, ctx);
  319. off = offsetof(struct bpf_array, ptrs);
  320. if (is_12b_check(off, insn))
  321. return -1;
  322. emit_ld(RV_REG_T2, off, RV_REG_T2, ctx);
  323. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  324. emit_branch(BPF_JEQ, RV_REG_T2, RV_REG_ZERO, off, ctx);
  325. /* goto *(prog->bpf_func + 4); */
  326. off = offsetof(struct bpf_prog, bpf_func);
  327. if (is_12b_check(off, insn))
  328. return -1;
  329. emit_ld(RV_REG_T3, off, RV_REG_T2, ctx);
  330. __build_epilogue(true, ctx);
  331. return 0;
  332. }
  333. static void init_regs(u8 *rd, u8 *rs, const struct bpf_insn *insn,
  334. struct rv_jit_context *ctx)
  335. {
  336. u8 code = insn->code;
  337. switch (code) {
  338. case BPF_JMP | BPF_JA:
  339. case BPF_JMP | BPF_CALL:
  340. case BPF_JMP | BPF_EXIT:
  341. case BPF_JMP | BPF_TAIL_CALL:
  342. break;
  343. default:
  344. *rd = bpf_to_rv_reg(insn->dst_reg, ctx);
  345. }
  346. if (code & (BPF_ALU | BPF_X) || code & (BPF_ALU64 | BPF_X) ||
  347. code & (BPF_JMP | BPF_X) || code & (BPF_JMP32 | BPF_X) ||
  348. code & BPF_LDX || code & BPF_STX)
  349. *rs = bpf_to_rv_reg(insn->src_reg, ctx);
  350. }
  351. static void emit_zext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
  352. {
  353. emit_mv(RV_REG_T2, *rd, ctx);
  354. emit_zext_32(RV_REG_T2, ctx);
  355. emit_mv(RV_REG_T1, *rs, ctx);
  356. emit_zext_32(RV_REG_T1, ctx);
  357. *rd = RV_REG_T2;
  358. *rs = RV_REG_T1;
  359. }
  360. static void emit_sext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
  361. {
  362. emit_addiw(RV_REG_T2, *rd, 0, ctx);
  363. emit_addiw(RV_REG_T1, *rs, 0, ctx);
  364. *rd = RV_REG_T2;
  365. *rs = RV_REG_T1;
  366. }
  367. static void emit_zext_32_rd_t1(u8 *rd, struct rv_jit_context *ctx)
  368. {
  369. emit_mv(RV_REG_T2, *rd, ctx);
  370. emit_zext_32(RV_REG_T2, ctx);
  371. emit_zext_32(RV_REG_T1, ctx);
  372. *rd = RV_REG_T2;
  373. }
  374. static void emit_sext_32_rd(u8 *rd, struct rv_jit_context *ctx)
  375. {
  376. emit_addiw(RV_REG_T2, *rd, 0, ctx);
  377. *rd = RV_REG_T2;
  378. }
  379. static int emit_jump_and_link(u8 rd, s64 rvoff, bool fixed_addr,
  380. struct rv_jit_context *ctx)
  381. {
  382. s64 upper, lower;
  383. if (rvoff && fixed_addr && is_21b_int(rvoff)) {
  384. emit(rv_jal(rd, rvoff >> 1), ctx);
  385. return 0;
  386. } else if (in_auipc_jalr_range(rvoff)) {
  387. upper = (rvoff + (1 << 11)) >> 12;
  388. lower = rvoff & 0xfff;
  389. emit(rv_auipc(RV_REG_T1, upper), ctx);
  390. emit(rv_jalr(rd, RV_REG_T1, lower), ctx);
  391. return 0;
  392. }
  393. pr_err("bpf-jit: target offset 0x%llx is out of range\n", rvoff);
  394. return -ERANGE;
  395. }
  396. static bool is_signed_bpf_cond(u8 cond)
  397. {
  398. return cond == BPF_JSGT || cond == BPF_JSLT ||
  399. cond == BPF_JSGE || cond == BPF_JSLE;
  400. }
  401. static int emit_call(u64 addr, bool fixed_addr, struct rv_jit_context *ctx)
  402. {
  403. s64 off = 0;
  404. u64 ip;
  405. if (addr && ctx->insns) {
  406. ip = (u64)(long)(ctx->insns + ctx->ninsns);
  407. off = addr - ip;
  408. }
  409. return emit_jump_and_link(RV_REG_RA, off, fixed_addr, ctx);
  410. }
  411. static void emit_atomic(u8 rd, u8 rs, s16 off, s32 imm, bool is64,
  412. struct rv_jit_context *ctx)
  413. {
  414. u8 r0;
  415. int jmp_offset;
  416. if (off) {
  417. if (is_12b_int(off)) {
  418. emit_addi(RV_REG_T1, rd, off, ctx);
  419. } else {
  420. emit_imm(RV_REG_T1, off, ctx);
  421. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  422. }
  423. rd = RV_REG_T1;
  424. }
  425. switch (imm) {
  426. /* lock *(u32/u64 *)(dst_reg + off16) <op>= src_reg */
  427. case BPF_ADD:
  428. emit(is64 ? rv_amoadd_d(RV_REG_ZERO, rs, rd, 0, 0) :
  429. rv_amoadd_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
  430. break;
  431. case BPF_AND:
  432. emit(is64 ? rv_amoand_d(RV_REG_ZERO, rs, rd, 0, 0) :
  433. rv_amoand_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
  434. break;
  435. case BPF_OR:
  436. emit(is64 ? rv_amoor_d(RV_REG_ZERO, rs, rd, 0, 0) :
  437. rv_amoor_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
  438. break;
  439. case BPF_XOR:
  440. emit(is64 ? rv_amoxor_d(RV_REG_ZERO, rs, rd, 0, 0) :
  441. rv_amoxor_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
  442. break;
  443. /* src_reg = atomic_fetch_<op>(dst_reg + off16, src_reg) */
  444. case BPF_ADD | BPF_FETCH:
  445. emit(is64 ? rv_amoadd_d(rs, rs, rd, 0, 0) :
  446. rv_amoadd_w(rs, rs, rd, 0, 0), ctx);
  447. if (!is64)
  448. emit_zext_32(rs, ctx);
  449. break;
  450. case BPF_AND | BPF_FETCH:
  451. emit(is64 ? rv_amoand_d(rs, rs, rd, 0, 0) :
  452. rv_amoand_w(rs, rs, rd, 0, 0), ctx);
  453. if (!is64)
  454. emit_zext_32(rs, ctx);
  455. break;
  456. case BPF_OR | BPF_FETCH:
  457. emit(is64 ? rv_amoor_d(rs, rs, rd, 0, 0) :
  458. rv_amoor_w(rs, rs, rd, 0, 0), ctx);
  459. if (!is64)
  460. emit_zext_32(rs, ctx);
  461. break;
  462. case BPF_XOR | BPF_FETCH:
  463. emit(is64 ? rv_amoxor_d(rs, rs, rd, 0, 0) :
  464. rv_amoxor_w(rs, rs, rd, 0, 0), ctx);
  465. if (!is64)
  466. emit_zext_32(rs, ctx);
  467. break;
  468. /* src_reg = atomic_xchg(dst_reg + off16, src_reg); */
  469. case BPF_XCHG:
  470. emit(is64 ? rv_amoswap_d(rs, rs, rd, 0, 0) :
  471. rv_amoswap_w(rs, rs, rd, 0, 0), ctx);
  472. if (!is64)
  473. emit_zext_32(rs, ctx);
  474. break;
  475. /* r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg); */
  476. case BPF_CMPXCHG:
  477. r0 = bpf_to_rv_reg(BPF_REG_0, ctx);
  478. emit(is64 ? rv_addi(RV_REG_T2, r0, 0) :
  479. rv_addiw(RV_REG_T2, r0, 0), ctx);
  480. emit(is64 ? rv_lr_d(r0, 0, rd, 0, 0) :
  481. rv_lr_w(r0, 0, rd, 0, 0), ctx);
  482. jmp_offset = ninsns_rvoff(8);
  483. emit(rv_bne(RV_REG_T2, r0, jmp_offset >> 1), ctx);
  484. emit(is64 ? rv_sc_d(RV_REG_T3, rs, rd, 0, 0) :
  485. rv_sc_w(RV_REG_T3, rs, rd, 0, 0), ctx);
  486. jmp_offset = ninsns_rvoff(-6);
  487. emit(rv_bne(RV_REG_T3, 0, jmp_offset >> 1), ctx);
  488. emit(rv_fence(0x3, 0x3), ctx);
  489. break;
  490. }
  491. }
  492. #define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
  493. #define BPF_FIXUP_REG_MASK GENMASK(31, 27)
  494. bool ex_handler_bpf(const struct exception_table_entry *ex,
  495. struct pt_regs *regs)
  496. {
  497. off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
  498. int regs_offset = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
  499. *(unsigned long *)((void *)regs + pt_regmap[regs_offset]) = 0;
  500. regs->epc = (unsigned long)&ex->fixup - offset;
  501. return true;
  502. }
  503. /* For accesses to BTF pointers, add an entry to the exception table */
  504. static int add_exception_handler(const struct bpf_insn *insn,
  505. struct rv_jit_context *ctx,
  506. int dst_reg, int insn_len)
  507. {
  508. struct exception_table_entry *ex;
  509. unsigned long pc;
  510. off_t offset;
  511. if (!ctx->insns || !ctx->prog->aux->extable || BPF_MODE(insn->code) != BPF_PROBE_MEM)
  512. return 0;
  513. if (WARN_ON_ONCE(ctx->nexentries >= ctx->prog->aux->num_exentries))
  514. return -EINVAL;
  515. if (WARN_ON_ONCE(insn_len > ctx->ninsns))
  516. return -EINVAL;
  517. if (WARN_ON_ONCE(!rvc_enabled() && insn_len == 1))
  518. return -EINVAL;
  519. ex = &ctx->prog->aux->extable[ctx->nexentries];
  520. pc = (unsigned long)&ctx->insns[ctx->ninsns - insn_len];
  521. offset = pc - (long)&ex->insn;
  522. if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
  523. return -ERANGE;
  524. ex->insn = offset;
  525. /*
  526. * Since the extable follows the program, the fixup offset is always
  527. * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
  528. * to keep things simple, and put the destination register in the upper
  529. * bits. We don't need to worry about buildtime or runtime sort
  530. * modifying the upper bits because the table is already sorted, and
  531. * isn't part of the main exception table.
  532. */
  533. offset = (long)&ex->fixup - (pc + insn_len * sizeof(u16));
  534. if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, offset))
  535. return -ERANGE;
  536. ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, offset) |
  537. FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
  538. ex->type = EX_TYPE_BPF;
  539. ctx->nexentries++;
  540. return 0;
  541. }
  542. int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
  543. bool extra_pass)
  544. {
  545. bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
  546. BPF_CLASS(insn->code) == BPF_JMP;
  547. int s, e, rvoff, ret, i = insn - ctx->prog->insnsi;
  548. struct bpf_prog_aux *aux = ctx->prog->aux;
  549. u8 rd = -1, rs = -1, code = insn->code;
  550. s16 off = insn->off;
  551. s32 imm = insn->imm;
  552. init_regs(&rd, &rs, insn, ctx);
  553. switch (code) {
  554. /* dst = src */
  555. case BPF_ALU | BPF_MOV | BPF_X:
  556. case BPF_ALU64 | BPF_MOV | BPF_X:
  557. if (imm == 1) {
  558. /* Special mov32 for zext */
  559. emit_zext_32(rd, ctx);
  560. break;
  561. }
  562. emit_mv(rd, rs, ctx);
  563. if (!is64 && !aux->verifier_zext)
  564. emit_zext_32(rd, ctx);
  565. break;
  566. /* dst = dst OP src */
  567. case BPF_ALU | BPF_ADD | BPF_X:
  568. case BPF_ALU64 | BPF_ADD | BPF_X:
  569. emit_add(rd, rd, rs, ctx);
  570. if (!is64 && !aux->verifier_zext)
  571. emit_zext_32(rd, ctx);
  572. break;
  573. case BPF_ALU | BPF_SUB | BPF_X:
  574. case BPF_ALU64 | BPF_SUB | BPF_X:
  575. if (is64)
  576. emit_sub(rd, rd, rs, ctx);
  577. else
  578. emit_subw(rd, rd, rs, ctx);
  579. if (!is64 && !aux->verifier_zext)
  580. emit_zext_32(rd, ctx);
  581. break;
  582. case BPF_ALU | BPF_AND | BPF_X:
  583. case BPF_ALU64 | BPF_AND | BPF_X:
  584. emit_and(rd, rd, rs, ctx);
  585. if (!is64 && !aux->verifier_zext)
  586. emit_zext_32(rd, ctx);
  587. break;
  588. case BPF_ALU | BPF_OR | BPF_X:
  589. case BPF_ALU64 | BPF_OR | BPF_X:
  590. emit_or(rd, rd, rs, ctx);
  591. if (!is64 && !aux->verifier_zext)
  592. emit_zext_32(rd, ctx);
  593. break;
  594. case BPF_ALU | BPF_XOR | BPF_X:
  595. case BPF_ALU64 | BPF_XOR | BPF_X:
  596. emit_xor(rd, rd, rs, ctx);
  597. if (!is64 && !aux->verifier_zext)
  598. emit_zext_32(rd, ctx);
  599. break;
  600. case BPF_ALU | BPF_MUL | BPF_X:
  601. case BPF_ALU64 | BPF_MUL | BPF_X:
  602. emit(is64 ? rv_mul(rd, rd, rs) : rv_mulw(rd, rd, rs), ctx);
  603. if (!is64 && !aux->verifier_zext)
  604. emit_zext_32(rd, ctx);
  605. break;
  606. case BPF_ALU | BPF_DIV | BPF_X:
  607. case BPF_ALU64 | BPF_DIV | BPF_X:
  608. emit(is64 ? rv_divu(rd, rd, rs) : rv_divuw(rd, rd, rs), ctx);
  609. if (!is64 && !aux->verifier_zext)
  610. emit_zext_32(rd, ctx);
  611. break;
  612. case BPF_ALU | BPF_MOD | BPF_X:
  613. case BPF_ALU64 | BPF_MOD | BPF_X:
  614. emit(is64 ? rv_remu(rd, rd, rs) : rv_remuw(rd, rd, rs), ctx);
  615. if (!is64 && !aux->verifier_zext)
  616. emit_zext_32(rd, ctx);
  617. break;
  618. case BPF_ALU | BPF_LSH | BPF_X:
  619. case BPF_ALU64 | BPF_LSH | BPF_X:
  620. emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx);
  621. if (!is64 && !aux->verifier_zext)
  622. emit_zext_32(rd, ctx);
  623. break;
  624. case BPF_ALU | BPF_RSH | BPF_X:
  625. case BPF_ALU64 | BPF_RSH | BPF_X:
  626. emit(is64 ? rv_srl(rd, rd, rs) : rv_srlw(rd, rd, rs), ctx);
  627. if (!is64 && !aux->verifier_zext)
  628. emit_zext_32(rd, ctx);
  629. break;
  630. case BPF_ALU | BPF_ARSH | BPF_X:
  631. case BPF_ALU64 | BPF_ARSH | BPF_X:
  632. emit(is64 ? rv_sra(rd, rd, rs) : rv_sraw(rd, rd, rs), ctx);
  633. if (!is64 && !aux->verifier_zext)
  634. emit_zext_32(rd, ctx);
  635. break;
  636. /* dst = -dst */
  637. case BPF_ALU | BPF_NEG:
  638. case BPF_ALU64 | BPF_NEG:
  639. emit_sub(rd, RV_REG_ZERO, rd, ctx);
  640. if (!is64 && !aux->verifier_zext)
  641. emit_zext_32(rd, ctx);
  642. break;
  643. /* dst = BSWAP##imm(dst) */
  644. case BPF_ALU | BPF_END | BPF_FROM_LE:
  645. switch (imm) {
  646. case 16:
  647. emit_slli(rd, rd, 48, ctx);
  648. emit_srli(rd, rd, 48, ctx);
  649. break;
  650. case 32:
  651. if (!aux->verifier_zext)
  652. emit_zext_32(rd, ctx);
  653. break;
  654. case 64:
  655. /* Do nothing */
  656. break;
  657. }
  658. break;
  659. case BPF_ALU | BPF_END | BPF_FROM_BE:
  660. emit_li(RV_REG_T2, 0, ctx);
  661. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  662. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  663. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  664. emit_srli(rd, rd, 8, ctx);
  665. if (imm == 16)
  666. goto out_be;
  667. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  668. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  669. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  670. emit_srli(rd, rd, 8, ctx);
  671. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  672. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  673. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  674. emit_srli(rd, rd, 8, ctx);
  675. if (imm == 32)
  676. goto out_be;
  677. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  678. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  679. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  680. emit_srli(rd, rd, 8, ctx);
  681. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  682. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  683. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  684. emit_srli(rd, rd, 8, ctx);
  685. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  686. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  687. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  688. emit_srli(rd, rd, 8, ctx);
  689. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  690. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  691. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  692. emit_srli(rd, rd, 8, ctx);
  693. out_be:
  694. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  695. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  696. emit_mv(rd, RV_REG_T2, ctx);
  697. break;
  698. /* dst = imm */
  699. case BPF_ALU | BPF_MOV | BPF_K:
  700. case BPF_ALU64 | BPF_MOV | BPF_K:
  701. emit_imm(rd, imm, ctx);
  702. if (!is64 && !aux->verifier_zext)
  703. emit_zext_32(rd, ctx);
  704. break;
  705. /* dst = dst OP imm */
  706. case BPF_ALU | BPF_ADD | BPF_K:
  707. case BPF_ALU64 | BPF_ADD | BPF_K:
  708. if (is_12b_int(imm)) {
  709. emit_addi(rd, rd, imm, ctx);
  710. } else {
  711. emit_imm(RV_REG_T1, imm, ctx);
  712. emit_add(rd, rd, RV_REG_T1, ctx);
  713. }
  714. if (!is64 && !aux->verifier_zext)
  715. emit_zext_32(rd, ctx);
  716. break;
  717. case BPF_ALU | BPF_SUB | BPF_K:
  718. case BPF_ALU64 | BPF_SUB | BPF_K:
  719. if (is_12b_int(-imm)) {
  720. emit_addi(rd, rd, -imm, ctx);
  721. } else {
  722. emit_imm(RV_REG_T1, imm, ctx);
  723. emit_sub(rd, rd, RV_REG_T1, ctx);
  724. }
  725. if (!is64 && !aux->verifier_zext)
  726. emit_zext_32(rd, ctx);
  727. break;
  728. case BPF_ALU | BPF_AND | BPF_K:
  729. case BPF_ALU64 | BPF_AND | BPF_K:
  730. if (is_12b_int(imm)) {
  731. emit_andi(rd, rd, imm, ctx);
  732. } else {
  733. emit_imm(RV_REG_T1, imm, ctx);
  734. emit_and(rd, rd, RV_REG_T1, ctx);
  735. }
  736. if (!is64 && !aux->verifier_zext)
  737. emit_zext_32(rd, ctx);
  738. break;
  739. case BPF_ALU | BPF_OR | BPF_K:
  740. case BPF_ALU64 | BPF_OR | BPF_K:
  741. if (is_12b_int(imm)) {
  742. emit(rv_ori(rd, rd, imm), ctx);
  743. } else {
  744. emit_imm(RV_REG_T1, imm, ctx);
  745. emit_or(rd, rd, RV_REG_T1, ctx);
  746. }
  747. if (!is64 && !aux->verifier_zext)
  748. emit_zext_32(rd, ctx);
  749. break;
  750. case BPF_ALU | BPF_XOR | BPF_K:
  751. case BPF_ALU64 | BPF_XOR | BPF_K:
  752. if (is_12b_int(imm)) {
  753. emit(rv_xori(rd, rd, imm), ctx);
  754. } else {
  755. emit_imm(RV_REG_T1, imm, ctx);
  756. emit_xor(rd, rd, RV_REG_T1, ctx);
  757. }
  758. if (!is64 && !aux->verifier_zext)
  759. emit_zext_32(rd, ctx);
  760. break;
  761. case BPF_ALU | BPF_MUL | BPF_K:
  762. case BPF_ALU64 | BPF_MUL | BPF_K:
  763. emit_imm(RV_REG_T1, imm, ctx);
  764. emit(is64 ? rv_mul(rd, rd, RV_REG_T1) :
  765. rv_mulw(rd, rd, RV_REG_T1), ctx);
  766. if (!is64 && !aux->verifier_zext)
  767. emit_zext_32(rd, ctx);
  768. break;
  769. case BPF_ALU | BPF_DIV | BPF_K:
  770. case BPF_ALU64 | BPF_DIV | BPF_K:
  771. emit_imm(RV_REG_T1, imm, ctx);
  772. emit(is64 ? rv_divu(rd, rd, RV_REG_T1) :
  773. rv_divuw(rd, rd, RV_REG_T1), ctx);
  774. if (!is64 && !aux->verifier_zext)
  775. emit_zext_32(rd, ctx);
  776. break;
  777. case BPF_ALU | BPF_MOD | BPF_K:
  778. case BPF_ALU64 | BPF_MOD | BPF_K:
  779. emit_imm(RV_REG_T1, imm, ctx);
  780. emit(is64 ? rv_remu(rd, rd, RV_REG_T1) :
  781. rv_remuw(rd, rd, RV_REG_T1), ctx);
  782. if (!is64 && !aux->verifier_zext)
  783. emit_zext_32(rd, ctx);
  784. break;
  785. case BPF_ALU | BPF_LSH | BPF_K:
  786. case BPF_ALU64 | BPF_LSH | BPF_K:
  787. emit_slli(rd, rd, imm, ctx);
  788. if (!is64 && !aux->verifier_zext)
  789. emit_zext_32(rd, ctx);
  790. break;
  791. case BPF_ALU | BPF_RSH | BPF_K:
  792. case BPF_ALU64 | BPF_RSH | BPF_K:
  793. if (is64)
  794. emit_srli(rd, rd, imm, ctx);
  795. else
  796. emit(rv_srliw(rd, rd, imm), ctx);
  797. if (!is64 && !aux->verifier_zext)
  798. emit_zext_32(rd, ctx);
  799. break;
  800. case BPF_ALU | BPF_ARSH | BPF_K:
  801. case BPF_ALU64 | BPF_ARSH | BPF_K:
  802. if (is64)
  803. emit_srai(rd, rd, imm, ctx);
  804. else
  805. emit(rv_sraiw(rd, rd, imm), ctx);
  806. if (!is64 && !aux->verifier_zext)
  807. emit_zext_32(rd, ctx);
  808. break;
  809. /* JUMP off */
  810. case BPF_JMP | BPF_JA:
  811. rvoff = rv_offset(i, off, ctx);
  812. ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
  813. if (ret)
  814. return ret;
  815. break;
  816. /* IF (dst COND src) JUMP off */
  817. case BPF_JMP | BPF_JEQ | BPF_X:
  818. case BPF_JMP32 | BPF_JEQ | BPF_X:
  819. case BPF_JMP | BPF_JGT | BPF_X:
  820. case BPF_JMP32 | BPF_JGT | BPF_X:
  821. case BPF_JMP | BPF_JLT | BPF_X:
  822. case BPF_JMP32 | BPF_JLT | BPF_X:
  823. case BPF_JMP | BPF_JGE | BPF_X:
  824. case BPF_JMP32 | BPF_JGE | BPF_X:
  825. case BPF_JMP | BPF_JLE | BPF_X:
  826. case BPF_JMP32 | BPF_JLE | BPF_X:
  827. case BPF_JMP | BPF_JNE | BPF_X:
  828. case BPF_JMP32 | BPF_JNE | BPF_X:
  829. case BPF_JMP | BPF_JSGT | BPF_X:
  830. case BPF_JMP32 | BPF_JSGT | BPF_X:
  831. case BPF_JMP | BPF_JSLT | BPF_X:
  832. case BPF_JMP32 | BPF_JSLT | BPF_X:
  833. case BPF_JMP | BPF_JSGE | BPF_X:
  834. case BPF_JMP32 | BPF_JSGE | BPF_X:
  835. case BPF_JMP | BPF_JSLE | BPF_X:
  836. case BPF_JMP32 | BPF_JSLE | BPF_X:
  837. case BPF_JMP | BPF_JSET | BPF_X:
  838. case BPF_JMP32 | BPF_JSET | BPF_X:
  839. rvoff = rv_offset(i, off, ctx);
  840. if (!is64) {
  841. s = ctx->ninsns;
  842. if (is_signed_bpf_cond(BPF_OP(code)))
  843. emit_sext_32_rd_rs(&rd, &rs, ctx);
  844. else
  845. emit_zext_32_rd_rs(&rd, &rs, ctx);
  846. e = ctx->ninsns;
  847. /* Adjust for extra insns */
  848. rvoff -= ninsns_rvoff(e - s);
  849. }
  850. if (BPF_OP(code) == BPF_JSET) {
  851. /* Adjust for and */
  852. rvoff -= 4;
  853. emit_and(RV_REG_T1, rd, rs, ctx);
  854. emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff,
  855. ctx);
  856. } else {
  857. emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
  858. }
  859. break;
  860. /* IF (dst COND imm) JUMP off */
  861. case BPF_JMP | BPF_JEQ | BPF_K:
  862. case BPF_JMP32 | BPF_JEQ | BPF_K:
  863. case BPF_JMP | BPF_JGT | BPF_K:
  864. case BPF_JMP32 | BPF_JGT | BPF_K:
  865. case BPF_JMP | BPF_JLT | BPF_K:
  866. case BPF_JMP32 | BPF_JLT | BPF_K:
  867. case BPF_JMP | BPF_JGE | BPF_K:
  868. case BPF_JMP32 | BPF_JGE | BPF_K:
  869. case BPF_JMP | BPF_JLE | BPF_K:
  870. case BPF_JMP32 | BPF_JLE | BPF_K:
  871. case BPF_JMP | BPF_JNE | BPF_K:
  872. case BPF_JMP32 | BPF_JNE | BPF_K:
  873. case BPF_JMP | BPF_JSGT | BPF_K:
  874. case BPF_JMP32 | BPF_JSGT | BPF_K:
  875. case BPF_JMP | BPF_JSLT | BPF_K:
  876. case BPF_JMP32 | BPF_JSLT | BPF_K:
  877. case BPF_JMP | BPF_JSGE | BPF_K:
  878. case BPF_JMP32 | BPF_JSGE | BPF_K:
  879. case BPF_JMP | BPF_JSLE | BPF_K:
  880. case BPF_JMP32 | BPF_JSLE | BPF_K:
  881. rvoff = rv_offset(i, off, ctx);
  882. s = ctx->ninsns;
  883. if (imm) {
  884. emit_imm(RV_REG_T1, imm, ctx);
  885. rs = RV_REG_T1;
  886. } else {
  887. /* If imm is 0, simply use zero register. */
  888. rs = RV_REG_ZERO;
  889. }
  890. if (!is64) {
  891. if (is_signed_bpf_cond(BPF_OP(code)))
  892. emit_sext_32_rd(&rd, ctx);
  893. else
  894. emit_zext_32_rd_t1(&rd, ctx);
  895. }
  896. e = ctx->ninsns;
  897. /* Adjust for extra insns */
  898. rvoff -= ninsns_rvoff(e - s);
  899. emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
  900. break;
  901. case BPF_JMP | BPF_JSET | BPF_K:
  902. case BPF_JMP32 | BPF_JSET | BPF_K:
  903. rvoff = rv_offset(i, off, ctx);
  904. s = ctx->ninsns;
  905. if (is_12b_int(imm)) {
  906. emit_andi(RV_REG_T1, rd, imm, ctx);
  907. } else {
  908. emit_imm(RV_REG_T1, imm, ctx);
  909. emit_and(RV_REG_T1, rd, RV_REG_T1, ctx);
  910. }
  911. /* For jset32, we should clear the upper 32 bits of t1, but
  912. * sign-extension is sufficient here and saves one instruction,
  913. * as t1 is used only in comparison against zero.
  914. */
  915. if (!is64 && imm < 0)
  916. emit_addiw(RV_REG_T1, RV_REG_T1, 0, ctx);
  917. e = ctx->ninsns;
  918. rvoff -= ninsns_rvoff(e - s);
  919. emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
  920. break;
  921. /* function call */
  922. case BPF_JMP | BPF_CALL:
  923. {
  924. bool fixed_addr;
  925. u64 addr;
  926. mark_call(ctx);
  927. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
  928. &addr, &fixed_addr);
  929. if (ret < 0)
  930. return ret;
  931. ret = emit_call(addr, fixed_addr, ctx);
  932. if (ret)
  933. return ret;
  934. if (insn->src_reg != BPF_PSEUDO_CALL)
  935. emit_mv(bpf_to_rv_reg(BPF_REG_0, ctx), RV_REG_A0, ctx);
  936. break;
  937. }
  938. /* tail call */
  939. case BPF_JMP | BPF_TAIL_CALL:
  940. if (emit_bpf_tail_call(i, ctx))
  941. return -1;
  942. break;
  943. /* function return */
  944. case BPF_JMP | BPF_EXIT:
  945. if (i == ctx->prog->len - 1)
  946. break;
  947. rvoff = epilogue_offset(ctx);
  948. ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
  949. if (ret)
  950. return ret;
  951. break;
  952. /* dst = imm64 */
  953. case BPF_LD | BPF_IMM | BPF_DW:
  954. {
  955. struct bpf_insn insn1 = insn[1];
  956. u64 imm64;
  957. imm64 = (u64)insn1.imm << 32 | (u32)imm;
  958. if (bpf_pseudo_func(insn)) {
  959. /* fixed-length insns for extra jit pass */
  960. ret = emit_addr(rd, imm64, extra_pass, ctx);
  961. if (ret)
  962. return ret;
  963. } else {
  964. emit_imm(rd, imm64, ctx);
  965. }
  966. return 1;
  967. }
  968. /* LDX: dst = *(size *)(src + off) */
  969. case BPF_LDX | BPF_MEM | BPF_B:
  970. case BPF_LDX | BPF_MEM | BPF_H:
  971. case BPF_LDX | BPF_MEM | BPF_W:
  972. case BPF_LDX | BPF_MEM | BPF_DW:
  973. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  974. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  975. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  976. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  977. {
  978. int insn_len, insns_start;
  979. switch (BPF_SIZE(code)) {
  980. case BPF_B:
  981. if (is_12b_int(off)) {
  982. insns_start = ctx->ninsns;
  983. emit(rv_lbu(rd, off, rs), ctx);
  984. insn_len = ctx->ninsns - insns_start;
  985. break;
  986. }
  987. emit_imm(RV_REG_T1, off, ctx);
  988. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  989. insns_start = ctx->ninsns;
  990. emit(rv_lbu(rd, 0, RV_REG_T1), ctx);
  991. insn_len = ctx->ninsns - insns_start;
  992. if (insn_is_zext(&insn[1]))
  993. return 1;
  994. break;
  995. case BPF_H:
  996. if (is_12b_int(off)) {
  997. insns_start = ctx->ninsns;
  998. emit(rv_lhu(rd, off, rs), ctx);
  999. insn_len = ctx->ninsns - insns_start;
  1000. break;
  1001. }
  1002. emit_imm(RV_REG_T1, off, ctx);
  1003. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1004. insns_start = ctx->ninsns;
  1005. emit(rv_lhu(rd, 0, RV_REG_T1), ctx);
  1006. insn_len = ctx->ninsns - insns_start;
  1007. if (insn_is_zext(&insn[1]))
  1008. return 1;
  1009. break;
  1010. case BPF_W:
  1011. if (is_12b_int(off)) {
  1012. insns_start = ctx->ninsns;
  1013. emit(rv_lwu(rd, off, rs), ctx);
  1014. insn_len = ctx->ninsns - insns_start;
  1015. break;
  1016. }
  1017. emit_imm(RV_REG_T1, off, ctx);
  1018. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1019. insns_start = ctx->ninsns;
  1020. emit(rv_lwu(rd, 0, RV_REG_T1), ctx);
  1021. insn_len = ctx->ninsns - insns_start;
  1022. if (insn_is_zext(&insn[1]))
  1023. return 1;
  1024. break;
  1025. case BPF_DW:
  1026. if (is_12b_int(off)) {
  1027. insns_start = ctx->ninsns;
  1028. emit_ld(rd, off, rs, ctx);
  1029. insn_len = ctx->ninsns - insns_start;
  1030. break;
  1031. }
  1032. emit_imm(RV_REG_T1, off, ctx);
  1033. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1034. insns_start = ctx->ninsns;
  1035. emit_ld(rd, 0, RV_REG_T1, ctx);
  1036. insn_len = ctx->ninsns - insns_start;
  1037. break;
  1038. }
  1039. ret = add_exception_handler(insn, ctx, rd, insn_len);
  1040. if (ret)
  1041. return ret;
  1042. break;
  1043. }
  1044. /* speculation barrier */
  1045. case BPF_ST | BPF_NOSPEC:
  1046. break;
  1047. /* ST: *(size *)(dst + off) = imm */
  1048. case BPF_ST | BPF_MEM | BPF_B:
  1049. emit_imm(RV_REG_T1, imm, ctx);
  1050. if (is_12b_int(off)) {
  1051. emit(rv_sb(rd, off, RV_REG_T1), ctx);
  1052. break;
  1053. }
  1054. emit_imm(RV_REG_T2, off, ctx);
  1055. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1056. emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx);
  1057. break;
  1058. case BPF_ST | BPF_MEM | BPF_H:
  1059. emit_imm(RV_REG_T1, imm, ctx);
  1060. if (is_12b_int(off)) {
  1061. emit(rv_sh(rd, off, RV_REG_T1), ctx);
  1062. break;
  1063. }
  1064. emit_imm(RV_REG_T2, off, ctx);
  1065. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1066. emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx);
  1067. break;
  1068. case BPF_ST | BPF_MEM | BPF_W:
  1069. emit_imm(RV_REG_T1, imm, ctx);
  1070. if (is_12b_int(off)) {
  1071. emit_sw(rd, off, RV_REG_T1, ctx);
  1072. break;
  1073. }
  1074. emit_imm(RV_REG_T2, off, ctx);
  1075. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1076. emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx);
  1077. break;
  1078. case BPF_ST | BPF_MEM | BPF_DW:
  1079. emit_imm(RV_REG_T1, imm, ctx);
  1080. if (is_12b_int(off)) {
  1081. emit_sd(rd, off, RV_REG_T1, ctx);
  1082. break;
  1083. }
  1084. emit_imm(RV_REG_T2, off, ctx);
  1085. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1086. emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx);
  1087. break;
  1088. /* STX: *(size *)(dst + off) = src */
  1089. case BPF_STX | BPF_MEM | BPF_B:
  1090. if (is_12b_int(off)) {
  1091. emit(rv_sb(rd, off, rs), ctx);
  1092. break;
  1093. }
  1094. emit_imm(RV_REG_T1, off, ctx);
  1095. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1096. emit(rv_sb(RV_REG_T1, 0, rs), ctx);
  1097. break;
  1098. case BPF_STX | BPF_MEM | BPF_H:
  1099. if (is_12b_int(off)) {
  1100. emit(rv_sh(rd, off, rs), ctx);
  1101. break;
  1102. }
  1103. emit_imm(RV_REG_T1, off, ctx);
  1104. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1105. emit(rv_sh(RV_REG_T1, 0, rs), ctx);
  1106. break;
  1107. case BPF_STX | BPF_MEM | BPF_W:
  1108. if (is_12b_int(off)) {
  1109. emit_sw(rd, off, rs, ctx);
  1110. break;
  1111. }
  1112. emit_imm(RV_REG_T1, off, ctx);
  1113. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1114. emit_sw(RV_REG_T1, 0, rs, ctx);
  1115. break;
  1116. case BPF_STX | BPF_MEM | BPF_DW:
  1117. if (is_12b_int(off)) {
  1118. emit_sd(rd, off, rs, ctx);
  1119. break;
  1120. }
  1121. emit_imm(RV_REG_T1, off, ctx);
  1122. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1123. emit_sd(RV_REG_T1, 0, rs, ctx);
  1124. break;
  1125. case BPF_STX | BPF_ATOMIC | BPF_W:
  1126. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1127. emit_atomic(rd, rs, off, imm,
  1128. BPF_SIZE(code) == BPF_DW, ctx);
  1129. break;
  1130. default:
  1131. pr_err("bpf-jit: unknown opcode %02x\n", code);
  1132. return -EINVAL;
  1133. }
  1134. return 0;
  1135. }
  1136. void bpf_jit_build_prologue(struct rv_jit_context *ctx)
  1137. {
  1138. int stack_adjust = 0, store_offset, bpf_stack_adjust;
  1139. bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
  1140. if (bpf_stack_adjust)
  1141. mark_fp(ctx);
  1142. if (seen_reg(RV_REG_RA, ctx))
  1143. stack_adjust += 8;
  1144. stack_adjust += 8; /* RV_REG_FP */
  1145. if (seen_reg(RV_REG_S1, ctx))
  1146. stack_adjust += 8;
  1147. if (seen_reg(RV_REG_S2, ctx))
  1148. stack_adjust += 8;
  1149. if (seen_reg(RV_REG_S3, ctx))
  1150. stack_adjust += 8;
  1151. if (seen_reg(RV_REG_S4, ctx))
  1152. stack_adjust += 8;
  1153. if (seen_reg(RV_REG_S5, ctx))
  1154. stack_adjust += 8;
  1155. if (seen_reg(RV_REG_S6, ctx))
  1156. stack_adjust += 8;
  1157. stack_adjust = round_up(stack_adjust, 16);
  1158. stack_adjust += bpf_stack_adjust;
  1159. store_offset = stack_adjust - 8;
  1160. /* First instruction is always setting the tail-call-counter
  1161. * (TCC) register. This instruction is skipped for tail calls.
  1162. * Force using a 4-byte (non-compressed) instruction.
  1163. */
  1164. emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
  1165. emit_addi(RV_REG_SP, RV_REG_SP, -stack_adjust, ctx);
  1166. if (seen_reg(RV_REG_RA, ctx)) {
  1167. emit_sd(RV_REG_SP, store_offset, RV_REG_RA, ctx);
  1168. store_offset -= 8;
  1169. }
  1170. emit_sd(RV_REG_SP, store_offset, RV_REG_FP, ctx);
  1171. store_offset -= 8;
  1172. if (seen_reg(RV_REG_S1, ctx)) {
  1173. emit_sd(RV_REG_SP, store_offset, RV_REG_S1, ctx);
  1174. store_offset -= 8;
  1175. }
  1176. if (seen_reg(RV_REG_S2, ctx)) {
  1177. emit_sd(RV_REG_SP, store_offset, RV_REG_S2, ctx);
  1178. store_offset -= 8;
  1179. }
  1180. if (seen_reg(RV_REG_S3, ctx)) {
  1181. emit_sd(RV_REG_SP, store_offset, RV_REG_S3, ctx);
  1182. store_offset -= 8;
  1183. }
  1184. if (seen_reg(RV_REG_S4, ctx)) {
  1185. emit_sd(RV_REG_SP, store_offset, RV_REG_S4, ctx);
  1186. store_offset -= 8;
  1187. }
  1188. if (seen_reg(RV_REG_S5, ctx)) {
  1189. emit_sd(RV_REG_SP, store_offset, RV_REG_S5, ctx);
  1190. store_offset -= 8;
  1191. }
  1192. if (seen_reg(RV_REG_S6, ctx)) {
  1193. emit_sd(RV_REG_SP, store_offset, RV_REG_S6, ctx);
  1194. store_offset -= 8;
  1195. }
  1196. emit_addi(RV_REG_FP, RV_REG_SP, stack_adjust, ctx);
  1197. if (bpf_stack_adjust)
  1198. emit_addi(RV_REG_S5, RV_REG_SP, bpf_stack_adjust, ctx);
  1199. /* Program contains calls and tail calls, so RV_REG_TCC need
  1200. * to be saved across calls.
  1201. */
  1202. if (seen_tail_call(ctx) && seen_call(ctx))
  1203. emit_mv(RV_REG_TCC_SAVED, RV_REG_TCC, ctx);
  1204. ctx->stack_size = stack_adjust;
  1205. }
  1206. void bpf_jit_build_epilogue(struct rv_jit_context *ctx)
  1207. {
  1208. __build_epilogue(false, ctx);
  1209. }