bpf_jit_comp32.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * BPF JIT compiler for RV32G
  4. *
  5. * Copyright (c) 2020 Luke Nelson <luke.r.nels@gmail.com>
  6. * Copyright (c) 2020 Xi Wang <xi.wang@gmail.com>
  7. *
  8. * The code is based on the BPF JIT compiler for RV64G by Björn Töpel and
  9. * the BPF JIT compiler for 32-bit ARM by Shubham Bansal and Mircea Gherzan.
  10. */
  11. #include <linux/bpf.h>
  12. #include <linux/filter.h>
  13. #include "bpf_jit.h"
  14. /*
  15. * Stack layout during BPF program execution:
  16. *
  17. * high
  18. * RV32 fp => +----------+
  19. * | saved ra |
  20. * | saved fp | RV32 callee-saved registers
  21. * | ... |
  22. * +----------+ <= (fp - 4 * NR_SAVED_REGISTERS)
  23. * | hi(R6) |
  24. * | lo(R6) |
  25. * | hi(R7) | JIT scratch space for BPF registers
  26. * | lo(R7) |
  27. * | ... |
  28. * BPF_REG_FP => +----------+ <= (fp - 4 * NR_SAVED_REGISTERS
  29. * | | - 4 * BPF_JIT_SCRATCH_REGS)
  30. * | |
  31. * | ... | BPF program stack
  32. * | |
  33. * RV32 sp => +----------+
  34. * | |
  35. * | ... | Function call stack
  36. * | |
  37. * +----------+
  38. * low
  39. */
  40. enum {
  41. /* Stack layout - these are offsets from top of JIT scratch space. */
  42. BPF_R6_HI,
  43. BPF_R6_LO,
  44. BPF_R7_HI,
  45. BPF_R7_LO,
  46. BPF_R8_HI,
  47. BPF_R8_LO,
  48. BPF_R9_HI,
  49. BPF_R9_LO,
  50. BPF_AX_HI,
  51. BPF_AX_LO,
  52. /* Stack space for BPF_REG_6 through BPF_REG_9 and BPF_REG_AX. */
  53. BPF_JIT_SCRATCH_REGS,
  54. };
  55. /* Number of callee-saved registers stored to stack: ra, fp, s1--s7. */
  56. #define NR_SAVED_REGISTERS 9
  57. /* Offset from fp for BPF registers stored on stack. */
  58. #define STACK_OFFSET(k) (-4 - (4 * NR_SAVED_REGISTERS) - (4 * (k)))
  59. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
  60. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
  61. #define RV_REG_TCC RV_REG_T6
  62. #define RV_REG_TCC_SAVED RV_REG_S7
  63. static const s8 bpf2rv32[][2] = {
  64. /* Return value from in-kernel function, and exit value from eBPF. */
  65. [BPF_REG_0] = {RV_REG_S2, RV_REG_S1},
  66. /* Arguments from eBPF program to in-kernel function. */
  67. [BPF_REG_1] = {RV_REG_A1, RV_REG_A0},
  68. [BPF_REG_2] = {RV_REG_A3, RV_REG_A2},
  69. [BPF_REG_3] = {RV_REG_A5, RV_REG_A4},
  70. [BPF_REG_4] = {RV_REG_A7, RV_REG_A6},
  71. [BPF_REG_5] = {RV_REG_S4, RV_REG_S3},
  72. /*
  73. * Callee-saved registers that in-kernel function will preserve.
  74. * Stored on the stack.
  75. */
  76. [BPF_REG_6] = {STACK_OFFSET(BPF_R6_HI), STACK_OFFSET(BPF_R6_LO)},
  77. [BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
  78. [BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
  79. [BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
  80. /* Read-only frame pointer to access BPF stack. */
  81. [BPF_REG_FP] = {RV_REG_S6, RV_REG_S5},
  82. /* Temporary register for blinding constants. Stored on the stack. */
  83. [BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
  84. /*
  85. * Temporary registers used by the JIT to operate on registers stored
  86. * on the stack. Save t0 and t1 to be used as temporaries in generated
  87. * code.
  88. */
  89. [TMP_REG_1] = {RV_REG_T3, RV_REG_T2},
  90. [TMP_REG_2] = {RV_REG_T5, RV_REG_T4},
  91. };
  92. static s8 hi(const s8 *r)
  93. {
  94. return r[0];
  95. }
  96. static s8 lo(const s8 *r)
  97. {
  98. return r[1];
  99. }
  100. static void emit_imm(const s8 rd, s32 imm, struct rv_jit_context *ctx)
  101. {
  102. u32 upper = (imm + (1 << 11)) >> 12;
  103. u32 lower = imm & 0xfff;
  104. if (upper) {
  105. emit(rv_lui(rd, upper), ctx);
  106. emit(rv_addi(rd, rd, lower), ctx);
  107. } else {
  108. emit(rv_addi(rd, RV_REG_ZERO, lower), ctx);
  109. }
  110. }
  111. static void emit_imm32(const s8 *rd, s32 imm, struct rv_jit_context *ctx)
  112. {
  113. /* Emit immediate into lower bits. */
  114. emit_imm(lo(rd), imm, ctx);
  115. /* Sign-extend into upper bits. */
  116. if (imm >= 0)
  117. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  118. else
  119. emit(rv_addi(hi(rd), RV_REG_ZERO, -1), ctx);
  120. }
  121. static void emit_imm64(const s8 *rd, s32 imm_hi, s32 imm_lo,
  122. struct rv_jit_context *ctx)
  123. {
  124. emit_imm(lo(rd), imm_lo, ctx);
  125. emit_imm(hi(rd), imm_hi, ctx);
  126. }
  127. static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
  128. {
  129. int stack_adjust = ctx->stack_size;
  130. const s8 *r0 = bpf2rv32[BPF_REG_0];
  131. /* Set return value if not tail call. */
  132. if (!is_tail_call) {
  133. emit(rv_addi(RV_REG_A0, lo(r0), 0), ctx);
  134. emit(rv_addi(RV_REG_A1, hi(r0), 0), ctx);
  135. }
  136. /* Restore callee-saved registers. */
  137. emit(rv_lw(RV_REG_RA, stack_adjust - 4, RV_REG_SP), ctx);
  138. emit(rv_lw(RV_REG_FP, stack_adjust - 8, RV_REG_SP), ctx);
  139. emit(rv_lw(RV_REG_S1, stack_adjust - 12, RV_REG_SP), ctx);
  140. emit(rv_lw(RV_REG_S2, stack_adjust - 16, RV_REG_SP), ctx);
  141. emit(rv_lw(RV_REG_S3, stack_adjust - 20, RV_REG_SP), ctx);
  142. emit(rv_lw(RV_REG_S4, stack_adjust - 24, RV_REG_SP), ctx);
  143. emit(rv_lw(RV_REG_S5, stack_adjust - 28, RV_REG_SP), ctx);
  144. emit(rv_lw(RV_REG_S6, stack_adjust - 32, RV_REG_SP), ctx);
  145. emit(rv_lw(RV_REG_S7, stack_adjust - 36, RV_REG_SP), ctx);
  146. emit(rv_addi(RV_REG_SP, RV_REG_SP, stack_adjust), ctx);
  147. if (is_tail_call) {
  148. /*
  149. * goto *(t0 + 4);
  150. * Skips first instruction of prologue which initializes tail
  151. * call counter. Assumes t0 contains address of target program,
  152. * see emit_bpf_tail_call.
  153. */
  154. emit(rv_jalr(RV_REG_ZERO, RV_REG_T0, 4), ctx);
  155. } else {
  156. emit(rv_jalr(RV_REG_ZERO, RV_REG_RA, 0), ctx);
  157. }
  158. }
  159. static bool is_stacked(s8 reg)
  160. {
  161. return reg < 0;
  162. }
  163. static const s8 *bpf_get_reg64(const s8 *reg, const s8 *tmp,
  164. struct rv_jit_context *ctx)
  165. {
  166. if (is_stacked(hi(reg))) {
  167. emit(rv_lw(hi(tmp), hi(reg), RV_REG_FP), ctx);
  168. emit(rv_lw(lo(tmp), lo(reg), RV_REG_FP), ctx);
  169. reg = tmp;
  170. }
  171. return reg;
  172. }
  173. static void bpf_put_reg64(const s8 *reg, const s8 *src,
  174. struct rv_jit_context *ctx)
  175. {
  176. if (is_stacked(hi(reg))) {
  177. emit(rv_sw(RV_REG_FP, hi(reg), hi(src)), ctx);
  178. emit(rv_sw(RV_REG_FP, lo(reg), lo(src)), ctx);
  179. }
  180. }
  181. static const s8 *bpf_get_reg32(const s8 *reg, const s8 *tmp,
  182. struct rv_jit_context *ctx)
  183. {
  184. if (is_stacked(lo(reg))) {
  185. emit(rv_lw(lo(tmp), lo(reg), RV_REG_FP), ctx);
  186. reg = tmp;
  187. }
  188. return reg;
  189. }
  190. static void bpf_put_reg32(const s8 *reg, const s8 *src,
  191. struct rv_jit_context *ctx)
  192. {
  193. if (is_stacked(lo(reg))) {
  194. emit(rv_sw(RV_REG_FP, lo(reg), lo(src)), ctx);
  195. if (!ctx->prog->aux->verifier_zext)
  196. emit(rv_sw(RV_REG_FP, hi(reg), RV_REG_ZERO), ctx);
  197. } else if (!ctx->prog->aux->verifier_zext) {
  198. emit(rv_addi(hi(reg), RV_REG_ZERO, 0), ctx);
  199. }
  200. }
  201. static void emit_jump_and_link(u8 rd, s32 rvoff, bool force_jalr,
  202. struct rv_jit_context *ctx)
  203. {
  204. s32 upper, lower;
  205. if (rvoff && is_21b_int(rvoff) && !force_jalr) {
  206. emit(rv_jal(rd, rvoff >> 1), ctx);
  207. return;
  208. }
  209. upper = (rvoff + (1 << 11)) >> 12;
  210. lower = rvoff & 0xfff;
  211. emit(rv_auipc(RV_REG_T1, upper), ctx);
  212. emit(rv_jalr(rd, RV_REG_T1, lower), ctx);
  213. }
  214. static void emit_alu_i64(const s8 *dst, s32 imm,
  215. struct rv_jit_context *ctx, const u8 op)
  216. {
  217. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  218. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  219. switch (op) {
  220. case BPF_MOV:
  221. emit_imm32(rd, imm, ctx);
  222. break;
  223. case BPF_AND:
  224. if (is_12b_int(imm)) {
  225. emit(rv_andi(lo(rd), lo(rd), imm), ctx);
  226. } else {
  227. emit_imm(RV_REG_T0, imm, ctx);
  228. emit(rv_and(lo(rd), lo(rd), RV_REG_T0), ctx);
  229. }
  230. if (imm >= 0)
  231. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  232. break;
  233. case BPF_OR:
  234. if (is_12b_int(imm)) {
  235. emit(rv_ori(lo(rd), lo(rd), imm), ctx);
  236. } else {
  237. emit_imm(RV_REG_T0, imm, ctx);
  238. emit(rv_or(lo(rd), lo(rd), RV_REG_T0), ctx);
  239. }
  240. if (imm < 0)
  241. emit(rv_ori(hi(rd), RV_REG_ZERO, -1), ctx);
  242. break;
  243. case BPF_XOR:
  244. if (is_12b_int(imm)) {
  245. emit(rv_xori(lo(rd), lo(rd), imm), ctx);
  246. } else {
  247. emit_imm(RV_REG_T0, imm, ctx);
  248. emit(rv_xor(lo(rd), lo(rd), RV_REG_T0), ctx);
  249. }
  250. if (imm < 0)
  251. emit(rv_xori(hi(rd), hi(rd), -1), ctx);
  252. break;
  253. case BPF_LSH:
  254. if (imm >= 32) {
  255. emit(rv_slli(hi(rd), lo(rd), imm - 32), ctx);
  256. emit(rv_addi(lo(rd), RV_REG_ZERO, 0), ctx);
  257. } else if (imm == 0) {
  258. /* Do nothing. */
  259. } else {
  260. emit(rv_srli(RV_REG_T0, lo(rd), 32 - imm), ctx);
  261. emit(rv_slli(hi(rd), hi(rd), imm), ctx);
  262. emit(rv_or(hi(rd), RV_REG_T0, hi(rd)), ctx);
  263. emit(rv_slli(lo(rd), lo(rd), imm), ctx);
  264. }
  265. break;
  266. case BPF_RSH:
  267. if (imm >= 32) {
  268. emit(rv_srli(lo(rd), hi(rd), imm - 32), ctx);
  269. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  270. } else if (imm == 0) {
  271. /* Do nothing. */
  272. } else {
  273. emit(rv_slli(RV_REG_T0, hi(rd), 32 - imm), ctx);
  274. emit(rv_srli(lo(rd), lo(rd), imm), ctx);
  275. emit(rv_or(lo(rd), RV_REG_T0, lo(rd)), ctx);
  276. emit(rv_srli(hi(rd), hi(rd), imm), ctx);
  277. }
  278. break;
  279. case BPF_ARSH:
  280. if (imm >= 32) {
  281. emit(rv_srai(lo(rd), hi(rd), imm - 32), ctx);
  282. emit(rv_srai(hi(rd), hi(rd), 31), ctx);
  283. } else if (imm == 0) {
  284. /* Do nothing. */
  285. } else {
  286. emit(rv_slli(RV_REG_T0, hi(rd), 32 - imm), ctx);
  287. emit(rv_srli(lo(rd), lo(rd), imm), ctx);
  288. emit(rv_or(lo(rd), RV_REG_T0, lo(rd)), ctx);
  289. emit(rv_srai(hi(rd), hi(rd), imm), ctx);
  290. }
  291. break;
  292. }
  293. bpf_put_reg64(dst, rd, ctx);
  294. }
  295. static void emit_alu_i32(const s8 *dst, s32 imm,
  296. struct rv_jit_context *ctx, const u8 op)
  297. {
  298. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  299. const s8 *rd = bpf_get_reg32(dst, tmp1, ctx);
  300. switch (op) {
  301. case BPF_MOV:
  302. emit_imm(lo(rd), imm, ctx);
  303. break;
  304. case BPF_ADD:
  305. if (is_12b_int(imm)) {
  306. emit(rv_addi(lo(rd), lo(rd), imm), ctx);
  307. } else {
  308. emit_imm(RV_REG_T0, imm, ctx);
  309. emit(rv_add(lo(rd), lo(rd), RV_REG_T0), ctx);
  310. }
  311. break;
  312. case BPF_SUB:
  313. if (is_12b_int(-imm)) {
  314. emit(rv_addi(lo(rd), lo(rd), -imm), ctx);
  315. } else {
  316. emit_imm(RV_REG_T0, imm, ctx);
  317. emit(rv_sub(lo(rd), lo(rd), RV_REG_T0), ctx);
  318. }
  319. break;
  320. case BPF_AND:
  321. if (is_12b_int(imm)) {
  322. emit(rv_andi(lo(rd), lo(rd), imm), ctx);
  323. } else {
  324. emit_imm(RV_REG_T0, imm, ctx);
  325. emit(rv_and(lo(rd), lo(rd), RV_REG_T0), ctx);
  326. }
  327. break;
  328. case BPF_OR:
  329. if (is_12b_int(imm)) {
  330. emit(rv_ori(lo(rd), lo(rd), imm), ctx);
  331. } else {
  332. emit_imm(RV_REG_T0, imm, ctx);
  333. emit(rv_or(lo(rd), lo(rd), RV_REG_T0), ctx);
  334. }
  335. break;
  336. case BPF_XOR:
  337. if (is_12b_int(imm)) {
  338. emit(rv_xori(lo(rd), lo(rd), imm), ctx);
  339. } else {
  340. emit_imm(RV_REG_T0, imm, ctx);
  341. emit(rv_xor(lo(rd), lo(rd), RV_REG_T0), ctx);
  342. }
  343. break;
  344. case BPF_LSH:
  345. if (is_12b_int(imm)) {
  346. emit(rv_slli(lo(rd), lo(rd), imm), ctx);
  347. } else {
  348. emit_imm(RV_REG_T0, imm, ctx);
  349. emit(rv_sll(lo(rd), lo(rd), RV_REG_T0), ctx);
  350. }
  351. break;
  352. case BPF_RSH:
  353. if (is_12b_int(imm)) {
  354. emit(rv_srli(lo(rd), lo(rd), imm), ctx);
  355. } else {
  356. emit_imm(RV_REG_T0, imm, ctx);
  357. emit(rv_srl(lo(rd), lo(rd), RV_REG_T0), ctx);
  358. }
  359. break;
  360. case BPF_ARSH:
  361. if (is_12b_int(imm)) {
  362. emit(rv_srai(lo(rd), lo(rd), imm), ctx);
  363. } else {
  364. emit_imm(RV_REG_T0, imm, ctx);
  365. emit(rv_sra(lo(rd), lo(rd), RV_REG_T0), ctx);
  366. }
  367. break;
  368. }
  369. bpf_put_reg32(dst, rd, ctx);
  370. }
  371. static void emit_alu_r64(const s8 *dst, const s8 *src,
  372. struct rv_jit_context *ctx, const u8 op)
  373. {
  374. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  375. const s8 *tmp2 = bpf2rv32[TMP_REG_2];
  376. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  377. const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
  378. switch (op) {
  379. case BPF_MOV:
  380. emit(rv_addi(lo(rd), lo(rs), 0), ctx);
  381. emit(rv_addi(hi(rd), hi(rs), 0), ctx);
  382. break;
  383. case BPF_ADD:
  384. if (rd == rs) {
  385. emit(rv_srli(RV_REG_T0, lo(rd), 31), ctx);
  386. emit(rv_slli(hi(rd), hi(rd), 1), ctx);
  387. emit(rv_or(hi(rd), RV_REG_T0, hi(rd)), ctx);
  388. emit(rv_slli(lo(rd), lo(rd), 1), ctx);
  389. } else {
  390. emit(rv_add(lo(rd), lo(rd), lo(rs)), ctx);
  391. emit(rv_sltu(RV_REG_T0, lo(rd), lo(rs)), ctx);
  392. emit(rv_add(hi(rd), hi(rd), hi(rs)), ctx);
  393. emit(rv_add(hi(rd), hi(rd), RV_REG_T0), ctx);
  394. }
  395. break;
  396. case BPF_SUB:
  397. emit(rv_sub(RV_REG_T1, hi(rd), hi(rs)), ctx);
  398. emit(rv_sltu(RV_REG_T0, lo(rd), lo(rs)), ctx);
  399. emit(rv_sub(hi(rd), RV_REG_T1, RV_REG_T0), ctx);
  400. emit(rv_sub(lo(rd), lo(rd), lo(rs)), ctx);
  401. break;
  402. case BPF_AND:
  403. emit(rv_and(lo(rd), lo(rd), lo(rs)), ctx);
  404. emit(rv_and(hi(rd), hi(rd), hi(rs)), ctx);
  405. break;
  406. case BPF_OR:
  407. emit(rv_or(lo(rd), lo(rd), lo(rs)), ctx);
  408. emit(rv_or(hi(rd), hi(rd), hi(rs)), ctx);
  409. break;
  410. case BPF_XOR:
  411. emit(rv_xor(lo(rd), lo(rd), lo(rs)), ctx);
  412. emit(rv_xor(hi(rd), hi(rd), hi(rs)), ctx);
  413. break;
  414. case BPF_MUL:
  415. emit(rv_mul(RV_REG_T0, hi(rs), lo(rd)), ctx);
  416. emit(rv_mul(hi(rd), hi(rd), lo(rs)), ctx);
  417. emit(rv_mulhu(RV_REG_T1, lo(rd), lo(rs)), ctx);
  418. emit(rv_add(hi(rd), hi(rd), RV_REG_T0), ctx);
  419. emit(rv_mul(lo(rd), lo(rd), lo(rs)), ctx);
  420. emit(rv_add(hi(rd), hi(rd), RV_REG_T1), ctx);
  421. break;
  422. case BPF_LSH:
  423. emit(rv_addi(RV_REG_T0, lo(rs), -32), ctx);
  424. emit(rv_blt(RV_REG_T0, RV_REG_ZERO, 8), ctx);
  425. emit(rv_sll(hi(rd), lo(rd), RV_REG_T0), ctx);
  426. emit(rv_addi(lo(rd), RV_REG_ZERO, 0), ctx);
  427. emit(rv_jal(RV_REG_ZERO, 16), ctx);
  428. emit(rv_addi(RV_REG_T1, RV_REG_ZERO, 31), ctx);
  429. emit(rv_srli(RV_REG_T0, lo(rd), 1), ctx);
  430. emit(rv_sub(RV_REG_T1, RV_REG_T1, lo(rs)), ctx);
  431. emit(rv_srl(RV_REG_T0, RV_REG_T0, RV_REG_T1), ctx);
  432. emit(rv_sll(hi(rd), hi(rd), lo(rs)), ctx);
  433. emit(rv_or(hi(rd), RV_REG_T0, hi(rd)), ctx);
  434. emit(rv_sll(lo(rd), lo(rd), lo(rs)), ctx);
  435. break;
  436. case BPF_RSH:
  437. emit(rv_addi(RV_REG_T0, lo(rs), -32), ctx);
  438. emit(rv_blt(RV_REG_T0, RV_REG_ZERO, 8), ctx);
  439. emit(rv_srl(lo(rd), hi(rd), RV_REG_T0), ctx);
  440. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  441. emit(rv_jal(RV_REG_ZERO, 16), ctx);
  442. emit(rv_addi(RV_REG_T1, RV_REG_ZERO, 31), ctx);
  443. emit(rv_slli(RV_REG_T0, hi(rd), 1), ctx);
  444. emit(rv_sub(RV_REG_T1, RV_REG_T1, lo(rs)), ctx);
  445. emit(rv_sll(RV_REG_T0, RV_REG_T0, RV_REG_T1), ctx);
  446. emit(rv_srl(lo(rd), lo(rd), lo(rs)), ctx);
  447. emit(rv_or(lo(rd), RV_REG_T0, lo(rd)), ctx);
  448. emit(rv_srl(hi(rd), hi(rd), lo(rs)), ctx);
  449. break;
  450. case BPF_ARSH:
  451. emit(rv_addi(RV_REG_T0, lo(rs), -32), ctx);
  452. emit(rv_blt(RV_REG_T0, RV_REG_ZERO, 8), ctx);
  453. emit(rv_sra(lo(rd), hi(rd), RV_REG_T0), ctx);
  454. emit(rv_srai(hi(rd), hi(rd), 31), ctx);
  455. emit(rv_jal(RV_REG_ZERO, 16), ctx);
  456. emit(rv_addi(RV_REG_T1, RV_REG_ZERO, 31), ctx);
  457. emit(rv_slli(RV_REG_T0, hi(rd), 1), ctx);
  458. emit(rv_sub(RV_REG_T1, RV_REG_T1, lo(rs)), ctx);
  459. emit(rv_sll(RV_REG_T0, RV_REG_T0, RV_REG_T1), ctx);
  460. emit(rv_srl(lo(rd), lo(rd), lo(rs)), ctx);
  461. emit(rv_or(lo(rd), RV_REG_T0, lo(rd)), ctx);
  462. emit(rv_sra(hi(rd), hi(rd), lo(rs)), ctx);
  463. break;
  464. case BPF_NEG:
  465. emit(rv_sub(lo(rd), RV_REG_ZERO, lo(rd)), ctx);
  466. emit(rv_sltu(RV_REG_T0, RV_REG_ZERO, lo(rd)), ctx);
  467. emit(rv_sub(hi(rd), RV_REG_ZERO, hi(rd)), ctx);
  468. emit(rv_sub(hi(rd), hi(rd), RV_REG_T0), ctx);
  469. break;
  470. }
  471. bpf_put_reg64(dst, rd, ctx);
  472. }
  473. static void emit_alu_r32(const s8 *dst, const s8 *src,
  474. struct rv_jit_context *ctx, const u8 op)
  475. {
  476. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  477. const s8 *tmp2 = bpf2rv32[TMP_REG_2];
  478. const s8 *rd = bpf_get_reg32(dst, tmp1, ctx);
  479. const s8 *rs = bpf_get_reg32(src, tmp2, ctx);
  480. switch (op) {
  481. case BPF_MOV:
  482. emit(rv_addi(lo(rd), lo(rs), 0), ctx);
  483. break;
  484. case BPF_ADD:
  485. emit(rv_add(lo(rd), lo(rd), lo(rs)), ctx);
  486. break;
  487. case BPF_SUB:
  488. emit(rv_sub(lo(rd), lo(rd), lo(rs)), ctx);
  489. break;
  490. case BPF_AND:
  491. emit(rv_and(lo(rd), lo(rd), lo(rs)), ctx);
  492. break;
  493. case BPF_OR:
  494. emit(rv_or(lo(rd), lo(rd), lo(rs)), ctx);
  495. break;
  496. case BPF_XOR:
  497. emit(rv_xor(lo(rd), lo(rd), lo(rs)), ctx);
  498. break;
  499. case BPF_MUL:
  500. emit(rv_mul(lo(rd), lo(rd), lo(rs)), ctx);
  501. break;
  502. case BPF_DIV:
  503. emit(rv_divu(lo(rd), lo(rd), lo(rs)), ctx);
  504. break;
  505. case BPF_MOD:
  506. emit(rv_remu(lo(rd), lo(rd), lo(rs)), ctx);
  507. break;
  508. case BPF_LSH:
  509. emit(rv_sll(lo(rd), lo(rd), lo(rs)), ctx);
  510. break;
  511. case BPF_RSH:
  512. emit(rv_srl(lo(rd), lo(rd), lo(rs)), ctx);
  513. break;
  514. case BPF_ARSH:
  515. emit(rv_sra(lo(rd), lo(rd), lo(rs)), ctx);
  516. break;
  517. case BPF_NEG:
  518. emit(rv_sub(lo(rd), RV_REG_ZERO, lo(rd)), ctx);
  519. break;
  520. }
  521. bpf_put_reg32(dst, rd, ctx);
  522. }
  523. static int emit_branch_r64(const s8 *src1, const s8 *src2, s32 rvoff,
  524. struct rv_jit_context *ctx, const u8 op)
  525. {
  526. int e, s = ctx->ninsns;
  527. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  528. const s8 *tmp2 = bpf2rv32[TMP_REG_2];
  529. const s8 *rs1 = bpf_get_reg64(src1, tmp1, ctx);
  530. const s8 *rs2 = bpf_get_reg64(src2, tmp2, ctx);
  531. /*
  532. * NO_JUMP skips over the rest of the instructions and the
  533. * emit_jump_and_link, meaning the BPF branch is not taken.
  534. * JUMP skips directly to the emit_jump_and_link, meaning
  535. * the BPF branch is taken.
  536. *
  537. * The fallthrough case results in the BPF branch being taken.
  538. */
  539. #define NO_JUMP(idx) (6 + (2 * (idx)))
  540. #define JUMP(idx) (2 + (2 * (idx)))
  541. switch (op) {
  542. case BPF_JEQ:
  543. emit(rv_bne(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  544. emit(rv_bne(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  545. break;
  546. case BPF_JGT:
  547. emit(rv_bgtu(hi(rs1), hi(rs2), JUMP(2)), ctx);
  548. emit(rv_bltu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  549. emit(rv_bleu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  550. break;
  551. case BPF_JLT:
  552. emit(rv_bltu(hi(rs1), hi(rs2), JUMP(2)), ctx);
  553. emit(rv_bgtu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  554. emit(rv_bgeu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  555. break;
  556. case BPF_JGE:
  557. emit(rv_bgtu(hi(rs1), hi(rs2), JUMP(2)), ctx);
  558. emit(rv_bltu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  559. emit(rv_bltu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  560. break;
  561. case BPF_JLE:
  562. emit(rv_bltu(hi(rs1), hi(rs2), JUMP(2)), ctx);
  563. emit(rv_bgtu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  564. emit(rv_bgtu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  565. break;
  566. case BPF_JNE:
  567. emit(rv_bne(hi(rs1), hi(rs2), JUMP(1)), ctx);
  568. emit(rv_beq(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  569. break;
  570. case BPF_JSGT:
  571. emit(rv_bgt(hi(rs1), hi(rs2), JUMP(2)), ctx);
  572. emit(rv_blt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  573. emit(rv_bleu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  574. break;
  575. case BPF_JSLT:
  576. emit(rv_blt(hi(rs1), hi(rs2), JUMP(2)), ctx);
  577. emit(rv_bgt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  578. emit(rv_bgeu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  579. break;
  580. case BPF_JSGE:
  581. emit(rv_bgt(hi(rs1), hi(rs2), JUMP(2)), ctx);
  582. emit(rv_blt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  583. emit(rv_bltu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  584. break;
  585. case BPF_JSLE:
  586. emit(rv_blt(hi(rs1), hi(rs2), JUMP(2)), ctx);
  587. emit(rv_bgt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  588. emit(rv_bgtu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  589. break;
  590. case BPF_JSET:
  591. emit(rv_and(RV_REG_T0, hi(rs1), hi(rs2)), ctx);
  592. emit(rv_bne(RV_REG_T0, RV_REG_ZERO, JUMP(2)), ctx);
  593. emit(rv_and(RV_REG_T0, lo(rs1), lo(rs2)), ctx);
  594. emit(rv_beq(RV_REG_T0, RV_REG_ZERO, NO_JUMP(0)), ctx);
  595. break;
  596. }
  597. #undef NO_JUMP
  598. #undef JUMP
  599. e = ctx->ninsns;
  600. /* Adjust for extra insns. */
  601. rvoff -= ninsns_rvoff(e - s);
  602. emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
  603. return 0;
  604. }
  605. static int emit_bcc(u8 op, u8 rd, u8 rs, int rvoff, struct rv_jit_context *ctx)
  606. {
  607. int e, s = ctx->ninsns;
  608. bool far = false;
  609. int off;
  610. if (op == BPF_JSET) {
  611. /*
  612. * BPF_JSET is a special case: it has no inverse so we always
  613. * treat it as a far branch.
  614. */
  615. far = true;
  616. } else if (!is_13b_int(rvoff)) {
  617. op = invert_bpf_cond(op);
  618. far = true;
  619. }
  620. /*
  621. * For a far branch, the condition is negated and we jump over the
  622. * branch itself, and the two instructions from emit_jump_and_link.
  623. * For a near branch, just use rvoff.
  624. */
  625. off = far ? 6 : (rvoff >> 1);
  626. switch (op) {
  627. case BPF_JEQ:
  628. emit(rv_beq(rd, rs, off), ctx);
  629. break;
  630. case BPF_JGT:
  631. emit(rv_bgtu(rd, rs, off), ctx);
  632. break;
  633. case BPF_JLT:
  634. emit(rv_bltu(rd, rs, off), ctx);
  635. break;
  636. case BPF_JGE:
  637. emit(rv_bgeu(rd, rs, off), ctx);
  638. break;
  639. case BPF_JLE:
  640. emit(rv_bleu(rd, rs, off), ctx);
  641. break;
  642. case BPF_JNE:
  643. emit(rv_bne(rd, rs, off), ctx);
  644. break;
  645. case BPF_JSGT:
  646. emit(rv_bgt(rd, rs, off), ctx);
  647. break;
  648. case BPF_JSLT:
  649. emit(rv_blt(rd, rs, off), ctx);
  650. break;
  651. case BPF_JSGE:
  652. emit(rv_bge(rd, rs, off), ctx);
  653. break;
  654. case BPF_JSLE:
  655. emit(rv_ble(rd, rs, off), ctx);
  656. break;
  657. case BPF_JSET:
  658. emit(rv_and(RV_REG_T0, rd, rs), ctx);
  659. emit(rv_beq(RV_REG_T0, RV_REG_ZERO, off), ctx);
  660. break;
  661. }
  662. if (far) {
  663. e = ctx->ninsns;
  664. /* Adjust for extra insns. */
  665. rvoff -= ninsns_rvoff(e - s);
  666. emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
  667. }
  668. return 0;
  669. }
  670. static int emit_branch_r32(const s8 *src1, const s8 *src2, s32 rvoff,
  671. struct rv_jit_context *ctx, const u8 op)
  672. {
  673. int e, s = ctx->ninsns;
  674. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  675. const s8 *tmp2 = bpf2rv32[TMP_REG_2];
  676. const s8 *rs1 = bpf_get_reg32(src1, tmp1, ctx);
  677. const s8 *rs2 = bpf_get_reg32(src2, tmp2, ctx);
  678. e = ctx->ninsns;
  679. /* Adjust for extra insns. */
  680. rvoff -= ninsns_rvoff(e - s);
  681. if (emit_bcc(op, lo(rs1), lo(rs2), rvoff, ctx))
  682. return -1;
  683. return 0;
  684. }
  685. static void emit_call(bool fixed, u64 addr, struct rv_jit_context *ctx)
  686. {
  687. const s8 *r0 = bpf2rv32[BPF_REG_0];
  688. const s8 *r5 = bpf2rv32[BPF_REG_5];
  689. u32 upper = ((u32)addr + (1 << 11)) >> 12;
  690. u32 lower = addr & 0xfff;
  691. /* R1-R4 already in correct registers---need to push R5 to stack. */
  692. emit(rv_addi(RV_REG_SP, RV_REG_SP, -16), ctx);
  693. emit(rv_sw(RV_REG_SP, 0, lo(r5)), ctx);
  694. emit(rv_sw(RV_REG_SP, 4, hi(r5)), ctx);
  695. /* Backup TCC. */
  696. emit(rv_addi(RV_REG_TCC_SAVED, RV_REG_TCC, 0), ctx);
  697. /*
  698. * Use lui/jalr pair to jump to absolute address. Don't use emit_imm as
  699. * the number of emitted instructions should not depend on the value of
  700. * addr.
  701. */
  702. emit(rv_lui(RV_REG_T1, upper), ctx);
  703. emit(rv_jalr(RV_REG_RA, RV_REG_T1, lower), ctx);
  704. /* Restore TCC. */
  705. emit(rv_addi(RV_REG_TCC, RV_REG_TCC_SAVED, 0), ctx);
  706. /* Set return value and restore stack. */
  707. emit(rv_addi(lo(r0), RV_REG_A0, 0), ctx);
  708. emit(rv_addi(hi(r0), RV_REG_A1, 0), ctx);
  709. emit(rv_addi(RV_REG_SP, RV_REG_SP, 16), ctx);
  710. }
  711. static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
  712. {
  713. /*
  714. * R1 -> &ctx
  715. * R2 -> &array
  716. * R3 -> index
  717. */
  718. int tc_ninsn, off, start_insn = ctx->ninsns;
  719. const s8 *arr_reg = bpf2rv32[BPF_REG_2];
  720. const s8 *idx_reg = bpf2rv32[BPF_REG_3];
  721. tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] :
  722. ctx->offset[0];
  723. /* max_entries = array->map.max_entries; */
  724. off = offsetof(struct bpf_array, map.max_entries);
  725. if (is_12b_check(off, insn))
  726. return -1;
  727. emit(rv_lw(RV_REG_T1, off, lo(arr_reg)), ctx);
  728. /*
  729. * if (index >= max_entries)
  730. * goto out;
  731. */
  732. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  733. emit_bcc(BPF_JGE, lo(idx_reg), RV_REG_T1, off, ctx);
  734. /*
  735. * if (--tcc < 0)
  736. * goto out;
  737. */
  738. emit(rv_addi(RV_REG_TCC, RV_REG_TCC, -1), ctx);
  739. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  740. emit_bcc(BPF_JSLT, RV_REG_TCC, RV_REG_ZERO, off, ctx);
  741. /*
  742. * prog = array->ptrs[index];
  743. * if (!prog)
  744. * goto out;
  745. */
  746. emit_sh2add(RV_REG_T0, lo(idx_reg), lo(arr_reg), ctx);
  747. off = offsetof(struct bpf_array, ptrs);
  748. if (is_12b_check(off, insn))
  749. return -1;
  750. emit(rv_lw(RV_REG_T0, off, RV_REG_T0), ctx);
  751. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  752. emit_bcc(BPF_JEQ, RV_REG_T0, RV_REG_ZERO, off, ctx);
  753. /*
  754. * tcc = temp_tcc;
  755. * goto *(prog->bpf_func + 4);
  756. */
  757. off = offsetof(struct bpf_prog, bpf_func);
  758. if (is_12b_check(off, insn))
  759. return -1;
  760. emit(rv_lw(RV_REG_T0, off, RV_REG_T0), ctx);
  761. /* Epilogue jumps to *(t0 + 4). */
  762. __build_epilogue(true, ctx);
  763. return 0;
  764. }
  765. static int emit_load_r64(const s8 *dst, const s8 *src, s16 off,
  766. struct rv_jit_context *ctx, const u8 size)
  767. {
  768. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  769. const s8 *tmp2 = bpf2rv32[TMP_REG_2];
  770. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  771. const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
  772. emit_imm(RV_REG_T0, off, ctx);
  773. emit(rv_add(RV_REG_T0, RV_REG_T0, lo(rs)), ctx);
  774. switch (size) {
  775. case BPF_B:
  776. emit(rv_lbu(lo(rd), 0, RV_REG_T0), ctx);
  777. if (!ctx->prog->aux->verifier_zext)
  778. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  779. break;
  780. case BPF_H:
  781. emit(rv_lhu(lo(rd), 0, RV_REG_T0), ctx);
  782. if (!ctx->prog->aux->verifier_zext)
  783. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  784. break;
  785. case BPF_W:
  786. emit(rv_lw(lo(rd), 0, RV_REG_T0), ctx);
  787. if (!ctx->prog->aux->verifier_zext)
  788. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  789. break;
  790. case BPF_DW:
  791. emit(rv_lw(lo(rd), 0, RV_REG_T0), ctx);
  792. emit(rv_lw(hi(rd), 4, RV_REG_T0), ctx);
  793. break;
  794. }
  795. bpf_put_reg64(dst, rd, ctx);
  796. return 0;
  797. }
  798. static int emit_store_r64(const s8 *dst, const s8 *src, s16 off,
  799. struct rv_jit_context *ctx, const u8 size,
  800. const u8 mode)
  801. {
  802. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  803. const s8 *tmp2 = bpf2rv32[TMP_REG_2];
  804. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  805. const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
  806. if (mode == BPF_ATOMIC && size != BPF_W)
  807. return -1;
  808. emit_imm(RV_REG_T0, off, ctx);
  809. emit(rv_add(RV_REG_T0, RV_REG_T0, lo(rd)), ctx);
  810. switch (size) {
  811. case BPF_B:
  812. emit(rv_sb(RV_REG_T0, 0, lo(rs)), ctx);
  813. break;
  814. case BPF_H:
  815. emit(rv_sh(RV_REG_T0, 0, lo(rs)), ctx);
  816. break;
  817. case BPF_W:
  818. switch (mode) {
  819. case BPF_MEM:
  820. emit(rv_sw(RV_REG_T0, 0, lo(rs)), ctx);
  821. break;
  822. case BPF_ATOMIC: /* Only BPF_ADD supported */
  823. emit(rv_amoadd_w(RV_REG_ZERO, lo(rs), RV_REG_T0, 0, 0),
  824. ctx);
  825. break;
  826. }
  827. break;
  828. case BPF_DW:
  829. emit(rv_sw(RV_REG_T0, 0, lo(rs)), ctx);
  830. emit(rv_sw(RV_REG_T0, 4, hi(rs)), ctx);
  831. break;
  832. }
  833. return 0;
  834. }
  835. static void emit_rev16(const s8 rd, struct rv_jit_context *ctx)
  836. {
  837. emit(rv_slli(rd, rd, 16), ctx);
  838. emit(rv_slli(RV_REG_T1, rd, 8), ctx);
  839. emit(rv_srli(rd, rd, 8), ctx);
  840. emit(rv_add(RV_REG_T1, rd, RV_REG_T1), ctx);
  841. emit(rv_srli(rd, RV_REG_T1, 16), ctx);
  842. }
  843. static void emit_rev32(const s8 rd, struct rv_jit_context *ctx)
  844. {
  845. emit(rv_addi(RV_REG_T1, RV_REG_ZERO, 0), ctx);
  846. emit(rv_andi(RV_REG_T0, rd, 255), ctx);
  847. emit(rv_add(RV_REG_T1, RV_REG_T1, RV_REG_T0), ctx);
  848. emit(rv_slli(RV_REG_T1, RV_REG_T1, 8), ctx);
  849. emit(rv_srli(rd, rd, 8), ctx);
  850. emit(rv_andi(RV_REG_T0, rd, 255), ctx);
  851. emit(rv_add(RV_REG_T1, RV_REG_T1, RV_REG_T0), ctx);
  852. emit(rv_slli(RV_REG_T1, RV_REG_T1, 8), ctx);
  853. emit(rv_srli(rd, rd, 8), ctx);
  854. emit(rv_andi(RV_REG_T0, rd, 255), ctx);
  855. emit(rv_add(RV_REG_T1, RV_REG_T1, RV_REG_T0), ctx);
  856. emit(rv_slli(RV_REG_T1, RV_REG_T1, 8), ctx);
  857. emit(rv_srli(rd, rd, 8), ctx);
  858. emit(rv_andi(RV_REG_T0, rd, 255), ctx);
  859. emit(rv_add(RV_REG_T1, RV_REG_T1, RV_REG_T0), ctx);
  860. emit(rv_addi(rd, RV_REG_T1, 0), ctx);
  861. }
  862. static void emit_zext64(const s8 *dst, struct rv_jit_context *ctx)
  863. {
  864. const s8 *rd;
  865. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  866. rd = bpf_get_reg64(dst, tmp1, ctx);
  867. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  868. bpf_put_reg64(dst, rd, ctx);
  869. }
  870. int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
  871. bool extra_pass)
  872. {
  873. bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
  874. BPF_CLASS(insn->code) == BPF_JMP;
  875. int s, e, rvoff, i = insn - ctx->prog->insnsi;
  876. u8 code = insn->code;
  877. s16 off = insn->off;
  878. s32 imm = insn->imm;
  879. const s8 *dst = bpf2rv32[insn->dst_reg];
  880. const s8 *src = bpf2rv32[insn->src_reg];
  881. const s8 *tmp1 = bpf2rv32[TMP_REG_1];
  882. const s8 *tmp2 = bpf2rv32[TMP_REG_2];
  883. switch (code) {
  884. case BPF_ALU64 | BPF_MOV | BPF_X:
  885. case BPF_ALU64 | BPF_ADD | BPF_X:
  886. case BPF_ALU64 | BPF_ADD | BPF_K:
  887. case BPF_ALU64 | BPF_SUB | BPF_X:
  888. case BPF_ALU64 | BPF_SUB | BPF_K:
  889. case BPF_ALU64 | BPF_AND | BPF_X:
  890. case BPF_ALU64 | BPF_OR | BPF_X:
  891. case BPF_ALU64 | BPF_XOR | BPF_X:
  892. case BPF_ALU64 | BPF_MUL | BPF_X:
  893. case BPF_ALU64 | BPF_MUL | BPF_K:
  894. case BPF_ALU64 | BPF_LSH | BPF_X:
  895. case BPF_ALU64 | BPF_RSH | BPF_X:
  896. case BPF_ALU64 | BPF_ARSH | BPF_X:
  897. if (BPF_SRC(code) == BPF_K) {
  898. emit_imm32(tmp2, imm, ctx);
  899. src = tmp2;
  900. }
  901. emit_alu_r64(dst, src, ctx, BPF_OP(code));
  902. break;
  903. case BPF_ALU64 | BPF_NEG:
  904. emit_alu_r64(dst, tmp2, ctx, BPF_OP(code));
  905. break;
  906. case BPF_ALU64 | BPF_DIV | BPF_X:
  907. case BPF_ALU64 | BPF_DIV | BPF_K:
  908. case BPF_ALU64 | BPF_MOD | BPF_X:
  909. case BPF_ALU64 | BPF_MOD | BPF_K:
  910. goto notsupported;
  911. case BPF_ALU64 | BPF_MOV | BPF_K:
  912. case BPF_ALU64 | BPF_AND | BPF_K:
  913. case BPF_ALU64 | BPF_OR | BPF_K:
  914. case BPF_ALU64 | BPF_XOR | BPF_K:
  915. case BPF_ALU64 | BPF_LSH | BPF_K:
  916. case BPF_ALU64 | BPF_RSH | BPF_K:
  917. case BPF_ALU64 | BPF_ARSH | BPF_K:
  918. emit_alu_i64(dst, imm, ctx, BPF_OP(code));
  919. break;
  920. case BPF_ALU | BPF_MOV | BPF_X:
  921. if (imm == 1) {
  922. /* Special mov32 for zext. */
  923. emit_zext64(dst, ctx);
  924. break;
  925. }
  926. fallthrough;
  927. case BPF_ALU | BPF_ADD | BPF_X:
  928. case BPF_ALU | BPF_SUB | BPF_X:
  929. case BPF_ALU | BPF_AND | BPF_X:
  930. case BPF_ALU | BPF_OR | BPF_X:
  931. case BPF_ALU | BPF_XOR | BPF_X:
  932. case BPF_ALU | BPF_MUL | BPF_X:
  933. case BPF_ALU | BPF_MUL | BPF_K:
  934. case BPF_ALU | BPF_DIV | BPF_X:
  935. case BPF_ALU | BPF_DIV | BPF_K:
  936. case BPF_ALU | BPF_MOD | BPF_X:
  937. case BPF_ALU | BPF_MOD | BPF_K:
  938. case BPF_ALU | BPF_LSH | BPF_X:
  939. case BPF_ALU | BPF_RSH | BPF_X:
  940. case BPF_ALU | BPF_ARSH | BPF_X:
  941. if (BPF_SRC(code) == BPF_K) {
  942. emit_imm32(tmp2, imm, ctx);
  943. src = tmp2;
  944. }
  945. emit_alu_r32(dst, src, ctx, BPF_OP(code));
  946. break;
  947. case BPF_ALU | BPF_MOV | BPF_K:
  948. case BPF_ALU | BPF_ADD | BPF_K:
  949. case BPF_ALU | BPF_SUB | BPF_K:
  950. case BPF_ALU | BPF_AND | BPF_K:
  951. case BPF_ALU | BPF_OR | BPF_K:
  952. case BPF_ALU | BPF_XOR | BPF_K:
  953. case BPF_ALU | BPF_LSH | BPF_K:
  954. case BPF_ALU | BPF_RSH | BPF_K:
  955. case BPF_ALU | BPF_ARSH | BPF_K:
  956. /*
  957. * mul,div,mod are handled in the BPF_X case since there are
  958. * no RISC-V I-type equivalents.
  959. */
  960. emit_alu_i32(dst, imm, ctx, BPF_OP(code));
  961. break;
  962. case BPF_ALU | BPF_NEG:
  963. /*
  964. * src is ignored---choose tmp2 as a dummy register since it
  965. * is not on the stack.
  966. */
  967. emit_alu_r32(dst, tmp2, ctx, BPF_OP(code));
  968. break;
  969. case BPF_ALU | BPF_END | BPF_FROM_LE:
  970. {
  971. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  972. switch (imm) {
  973. case 16:
  974. emit(rv_slli(lo(rd), lo(rd), 16), ctx);
  975. emit(rv_srli(lo(rd), lo(rd), 16), ctx);
  976. fallthrough;
  977. case 32:
  978. if (!ctx->prog->aux->verifier_zext)
  979. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  980. break;
  981. case 64:
  982. /* Do nothing. */
  983. break;
  984. default:
  985. pr_err("bpf-jit: BPF_END imm %d invalid\n", imm);
  986. return -1;
  987. }
  988. bpf_put_reg64(dst, rd, ctx);
  989. break;
  990. }
  991. case BPF_ALU | BPF_END | BPF_FROM_BE:
  992. {
  993. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  994. switch (imm) {
  995. case 16:
  996. emit_rev16(lo(rd), ctx);
  997. if (!ctx->prog->aux->verifier_zext)
  998. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  999. break;
  1000. case 32:
  1001. emit_rev32(lo(rd), ctx);
  1002. if (!ctx->prog->aux->verifier_zext)
  1003. emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
  1004. break;
  1005. case 64:
  1006. /* Swap upper and lower halves. */
  1007. emit(rv_addi(RV_REG_T0, lo(rd), 0), ctx);
  1008. emit(rv_addi(lo(rd), hi(rd), 0), ctx);
  1009. emit(rv_addi(hi(rd), RV_REG_T0, 0), ctx);
  1010. /* Swap each half. */
  1011. emit_rev32(lo(rd), ctx);
  1012. emit_rev32(hi(rd), ctx);
  1013. break;
  1014. default:
  1015. pr_err("bpf-jit: BPF_END imm %d invalid\n", imm);
  1016. return -1;
  1017. }
  1018. bpf_put_reg64(dst, rd, ctx);
  1019. break;
  1020. }
  1021. case BPF_JMP | BPF_JA:
  1022. rvoff = rv_offset(i, off, ctx);
  1023. emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
  1024. break;
  1025. case BPF_JMP | BPF_CALL:
  1026. {
  1027. bool fixed;
  1028. int ret;
  1029. u64 addr;
  1030. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, &addr,
  1031. &fixed);
  1032. if (ret < 0)
  1033. return ret;
  1034. emit_call(fixed, addr, ctx);
  1035. break;
  1036. }
  1037. case BPF_JMP | BPF_TAIL_CALL:
  1038. if (emit_bpf_tail_call(i, ctx))
  1039. return -1;
  1040. break;
  1041. case BPF_JMP | BPF_JEQ | BPF_X:
  1042. case BPF_JMP | BPF_JEQ | BPF_K:
  1043. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1044. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1045. case BPF_JMP | BPF_JNE | BPF_X:
  1046. case BPF_JMP | BPF_JNE | BPF_K:
  1047. case BPF_JMP32 | BPF_JNE | BPF_X:
  1048. case BPF_JMP32 | BPF_JNE | BPF_K:
  1049. case BPF_JMP | BPF_JLE | BPF_X:
  1050. case BPF_JMP | BPF_JLE | BPF_K:
  1051. case BPF_JMP32 | BPF_JLE | BPF_X:
  1052. case BPF_JMP32 | BPF_JLE | BPF_K:
  1053. case BPF_JMP | BPF_JLT | BPF_X:
  1054. case BPF_JMP | BPF_JLT | BPF_K:
  1055. case BPF_JMP32 | BPF_JLT | BPF_X:
  1056. case BPF_JMP32 | BPF_JLT | BPF_K:
  1057. case BPF_JMP | BPF_JGE | BPF_X:
  1058. case BPF_JMP | BPF_JGE | BPF_K:
  1059. case BPF_JMP32 | BPF_JGE | BPF_X:
  1060. case BPF_JMP32 | BPF_JGE | BPF_K:
  1061. case BPF_JMP | BPF_JGT | BPF_X:
  1062. case BPF_JMP | BPF_JGT | BPF_K:
  1063. case BPF_JMP32 | BPF_JGT | BPF_X:
  1064. case BPF_JMP32 | BPF_JGT | BPF_K:
  1065. case BPF_JMP | BPF_JSLE | BPF_X:
  1066. case BPF_JMP | BPF_JSLE | BPF_K:
  1067. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1068. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1069. case BPF_JMP | BPF_JSLT | BPF_X:
  1070. case BPF_JMP | BPF_JSLT | BPF_K:
  1071. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1072. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1073. case BPF_JMP | BPF_JSGE | BPF_X:
  1074. case BPF_JMP | BPF_JSGE | BPF_K:
  1075. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1076. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1077. case BPF_JMP | BPF_JSGT | BPF_X:
  1078. case BPF_JMP | BPF_JSGT | BPF_K:
  1079. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1080. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1081. case BPF_JMP | BPF_JSET | BPF_X:
  1082. case BPF_JMP | BPF_JSET | BPF_K:
  1083. case BPF_JMP32 | BPF_JSET | BPF_X:
  1084. case BPF_JMP32 | BPF_JSET | BPF_K:
  1085. rvoff = rv_offset(i, off, ctx);
  1086. if (BPF_SRC(code) == BPF_K) {
  1087. s = ctx->ninsns;
  1088. emit_imm32(tmp2, imm, ctx);
  1089. src = tmp2;
  1090. e = ctx->ninsns;
  1091. rvoff -= ninsns_rvoff(e - s);
  1092. }
  1093. if (is64)
  1094. emit_branch_r64(dst, src, rvoff, ctx, BPF_OP(code));
  1095. else
  1096. emit_branch_r32(dst, src, rvoff, ctx, BPF_OP(code));
  1097. break;
  1098. case BPF_JMP | BPF_EXIT:
  1099. if (i == ctx->prog->len - 1)
  1100. break;
  1101. rvoff = epilogue_offset(ctx);
  1102. emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
  1103. break;
  1104. case BPF_LD | BPF_IMM | BPF_DW:
  1105. {
  1106. struct bpf_insn insn1 = insn[1];
  1107. s32 imm_lo = imm;
  1108. s32 imm_hi = insn1.imm;
  1109. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  1110. emit_imm64(rd, imm_hi, imm_lo, ctx);
  1111. bpf_put_reg64(dst, rd, ctx);
  1112. return 1;
  1113. }
  1114. case BPF_LDX | BPF_MEM | BPF_B:
  1115. case BPF_LDX | BPF_MEM | BPF_H:
  1116. case BPF_LDX | BPF_MEM | BPF_W:
  1117. case BPF_LDX | BPF_MEM | BPF_DW:
  1118. if (emit_load_r64(dst, src, off, ctx, BPF_SIZE(code)))
  1119. return -1;
  1120. break;
  1121. /* speculation barrier */
  1122. case BPF_ST | BPF_NOSPEC:
  1123. break;
  1124. case BPF_ST | BPF_MEM | BPF_B:
  1125. case BPF_ST | BPF_MEM | BPF_H:
  1126. case BPF_ST | BPF_MEM | BPF_W:
  1127. case BPF_ST | BPF_MEM | BPF_DW:
  1128. case BPF_STX | BPF_MEM | BPF_B:
  1129. case BPF_STX | BPF_MEM | BPF_H:
  1130. case BPF_STX | BPF_MEM | BPF_W:
  1131. case BPF_STX | BPF_MEM | BPF_DW:
  1132. if (BPF_CLASS(code) == BPF_ST) {
  1133. emit_imm32(tmp2, imm, ctx);
  1134. src = tmp2;
  1135. }
  1136. if (emit_store_r64(dst, src, off, ctx, BPF_SIZE(code),
  1137. BPF_MODE(code)))
  1138. return -1;
  1139. break;
  1140. case BPF_STX | BPF_ATOMIC | BPF_W:
  1141. if (insn->imm != BPF_ADD) {
  1142. pr_info_once(
  1143. "bpf-jit: not supported: atomic operation %02x ***\n",
  1144. insn->imm);
  1145. return -EFAULT;
  1146. }
  1147. if (emit_store_r64(dst, src, off, ctx, BPF_SIZE(code),
  1148. BPF_MODE(code)))
  1149. return -1;
  1150. break;
  1151. /* No hardware support for 8-byte atomics in RV32. */
  1152. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1153. /* Fallthrough. */
  1154. notsupported:
  1155. pr_info_once("bpf-jit: not supported: opcode %02x ***\n", code);
  1156. return -EFAULT;
  1157. default:
  1158. pr_err("bpf-jit: unknown opcode %02x\n", code);
  1159. return -EINVAL;
  1160. }
  1161. return 0;
  1162. }
  1163. void bpf_jit_build_prologue(struct rv_jit_context *ctx, bool is_subprog)
  1164. {
  1165. const s8 *fp = bpf2rv32[BPF_REG_FP];
  1166. const s8 *r1 = bpf2rv32[BPF_REG_1];
  1167. int stack_adjust = 0;
  1168. int bpf_stack_adjust =
  1169. round_up(ctx->prog->aux->stack_depth, STACK_ALIGN);
  1170. /* Make space for callee-saved registers. */
  1171. stack_adjust += NR_SAVED_REGISTERS * sizeof(u32);
  1172. /* Make space for BPF registers on stack. */
  1173. stack_adjust += BPF_JIT_SCRATCH_REGS * sizeof(u32);
  1174. /* Make space for BPF stack. */
  1175. stack_adjust += bpf_stack_adjust;
  1176. /* Round up for stack alignment. */
  1177. stack_adjust = round_up(stack_adjust, STACK_ALIGN);
  1178. /*
  1179. * The first instruction sets the tail-call-counter (TCC) register.
  1180. * This instruction is skipped by tail calls.
  1181. */
  1182. emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
  1183. emit(rv_addi(RV_REG_SP, RV_REG_SP, -stack_adjust), ctx);
  1184. /* Save callee-save registers. */
  1185. emit(rv_sw(RV_REG_SP, stack_adjust - 4, RV_REG_RA), ctx);
  1186. emit(rv_sw(RV_REG_SP, stack_adjust - 8, RV_REG_FP), ctx);
  1187. emit(rv_sw(RV_REG_SP, stack_adjust - 12, RV_REG_S1), ctx);
  1188. emit(rv_sw(RV_REG_SP, stack_adjust - 16, RV_REG_S2), ctx);
  1189. emit(rv_sw(RV_REG_SP, stack_adjust - 20, RV_REG_S3), ctx);
  1190. emit(rv_sw(RV_REG_SP, stack_adjust - 24, RV_REG_S4), ctx);
  1191. emit(rv_sw(RV_REG_SP, stack_adjust - 28, RV_REG_S5), ctx);
  1192. emit(rv_sw(RV_REG_SP, stack_adjust - 32, RV_REG_S6), ctx);
  1193. emit(rv_sw(RV_REG_SP, stack_adjust - 36, RV_REG_S7), ctx);
  1194. /* Set fp: used as the base address for stacked BPF registers. */
  1195. emit(rv_addi(RV_REG_FP, RV_REG_SP, stack_adjust), ctx);
  1196. /* Set up BPF frame pointer. */
  1197. emit(rv_addi(lo(fp), RV_REG_SP, bpf_stack_adjust), ctx);
  1198. emit(rv_addi(hi(fp), RV_REG_ZERO, 0), ctx);
  1199. /* Set up BPF context pointer. */
  1200. emit(rv_addi(lo(r1), RV_REG_A0, 0), ctx);
  1201. emit(rv_addi(hi(r1), RV_REG_ZERO, 0), ctx);
  1202. ctx->stack_size = stack_adjust;
  1203. }
  1204. void bpf_jit_build_epilogue(struct rv_jit_context *ctx)
  1205. {
  1206. __build_epilogue(false, ctx);
  1207. }