bpf_jit.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common functionality for RV32 and RV64 BPF JIT compilers
  4. *
  5. * Copyright (c) 2019 Björn Töpel <bjorn.topel@gmail.com>
  6. *
  7. */
  8. #ifndef _BPF_JIT_H
  9. #define _BPF_JIT_H
  10. #include <linux/bpf.h>
  11. #include <linux/filter.h>
  12. #include <asm/cacheflush.h>
  13. static inline bool rvc_enabled(void)
  14. {
  15. return IS_ENABLED(CONFIG_RISCV_ISA_C);
  16. }
  17. static inline bool rvzba_enabled(void)
  18. {
  19. return IS_ENABLED(CONFIG_RISCV_ISA_ZBA) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBA);
  20. }
  21. static inline bool rvzbb_enabled(void)
  22. {
  23. return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);
  24. }
  25. enum {
  26. RV_REG_ZERO = 0, /* The constant value 0 */
  27. RV_REG_RA = 1, /* Return address */
  28. RV_REG_SP = 2, /* Stack pointer */
  29. RV_REG_GP = 3, /* Global pointer */
  30. RV_REG_TP = 4, /* Thread pointer */
  31. RV_REG_T0 = 5, /* Temporaries */
  32. RV_REG_T1 = 6,
  33. RV_REG_T2 = 7,
  34. RV_REG_FP = 8, /* Saved register/frame pointer */
  35. RV_REG_S1 = 9, /* Saved register */
  36. RV_REG_A0 = 10, /* Function argument/return values */
  37. RV_REG_A1 = 11, /* Function arguments */
  38. RV_REG_A2 = 12,
  39. RV_REG_A3 = 13,
  40. RV_REG_A4 = 14,
  41. RV_REG_A5 = 15,
  42. RV_REG_A6 = 16,
  43. RV_REG_A7 = 17,
  44. RV_REG_S2 = 18, /* Saved registers */
  45. RV_REG_S3 = 19,
  46. RV_REG_S4 = 20,
  47. RV_REG_S5 = 21,
  48. RV_REG_S6 = 22,
  49. RV_REG_S7 = 23,
  50. RV_REG_S8 = 24,
  51. RV_REG_S9 = 25,
  52. RV_REG_S10 = 26,
  53. RV_REG_S11 = 27,
  54. RV_REG_T3 = 28, /* Temporaries */
  55. RV_REG_T4 = 29,
  56. RV_REG_T5 = 30,
  57. RV_REG_T6 = 31,
  58. };
  59. static inline bool is_creg(u8 reg)
  60. {
  61. return (1 << reg) & (BIT(RV_REG_FP) |
  62. BIT(RV_REG_S1) |
  63. BIT(RV_REG_A0) |
  64. BIT(RV_REG_A1) |
  65. BIT(RV_REG_A2) |
  66. BIT(RV_REG_A3) |
  67. BIT(RV_REG_A4) |
  68. BIT(RV_REG_A5));
  69. }
  70. struct rv_jit_context {
  71. struct bpf_prog *prog;
  72. u16 *insns; /* RV insns */
  73. u16 *ro_insns;
  74. int ninsns;
  75. int prologue_len;
  76. int epilogue_offset;
  77. int *offset; /* BPF to RV */
  78. int nexentries;
  79. unsigned long flags;
  80. int stack_size;
  81. u64 arena_vm_start;
  82. u64 user_vm_start;
  83. };
  84. /* Convert from ninsns to bytes. */
  85. static inline int ninsns_rvoff(int ninsns)
  86. {
  87. return ninsns << 1;
  88. }
  89. struct rv_jit_data {
  90. struct bpf_binary_header *header;
  91. struct bpf_binary_header *ro_header;
  92. u8 *image;
  93. u8 *ro_image;
  94. struct rv_jit_context ctx;
  95. };
  96. static inline void bpf_fill_ill_insns(void *area, unsigned int size)
  97. {
  98. memset(area, 0, size);
  99. }
  100. static inline void bpf_flush_icache(void *start, void *end)
  101. {
  102. flush_icache_range((unsigned long)start, (unsigned long)end);
  103. }
  104. /* Emit a 4-byte riscv instruction. */
  105. static inline void emit(const u32 insn, struct rv_jit_context *ctx)
  106. {
  107. if (ctx->insns) {
  108. ctx->insns[ctx->ninsns] = insn;
  109. ctx->insns[ctx->ninsns + 1] = (insn >> 16);
  110. }
  111. ctx->ninsns += 2;
  112. }
  113. /* Emit a 2-byte riscv compressed instruction. */
  114. static inline void emitc(const u16 insn, struct rv_jit_context *ctx)
  115. {
  116. BUILD_BUG_ON(!rvc_enabled());
  117. if (ctx->insns)
  118. ctx->insns[ctx->ninsns] = insn;
  119. ctx->ninsns++;
  120. }
  121. static inline int epilogue_offset(struct rv_jit_context *ctx)
  122. {
  123. int to = ctx->epilogue_offset, from = ctx->ninsns;
  124. return ninsns_rvoff(to - from);
  125. }
  126. /* Return -1 or inverted cond. */
  127. static inline int invert_bpf_cond(u8 cond)
  128. {
  129. switch (cond) {
  130. case BPF_JEQ:
  131. return BPF_JNE;
  132. case BPF_JGT:
  133. return BPF_JLE;
  134. case BPF_JLT:
  135. return BPF_JGE;
  136. case BPF_JGE:
  137. return BPF_JLT;
  138. case BPF_JLE:
  139. return BPF_JGT;
  140. case BPF_JNE:
  141. return BPF_JEQ;
  142. case BPF_JSGT:
  143. return BPF_JSLE;
  144. case BPF_JSLT:
  145. return BPF_JSGE;
  146. case BPF_JSGE:
  147. return BPF_JSLT;
  148. case BPF_JSLE:
  149. return BPF_JSGT;
  150. }
  151. return -1;
  152. }
  153. static inline bool is_6b_int(long val)
  154. {
  155. return -(1L << 5) <= val && val < (1L << 5);
  156. }
  157. static inline bool is_7b_uint(unsigned long val)
  158. {
  159. return val < (1UL << 7);
  160. }
  161. static inline bool is_8b_uint(unsigned long val)
  162. {
  163. return val < (1UL << 8);
  164. }
  165. static inline bool is_9b_uint(unsigned long val)
  166. {
  167. return val < (1UL << 9);
  168. }
  169. static inline bool is_10b_int(long val)
  170. {
  171. return -(1L << 9) <= val && val < (1L << 9);
  172. }
  173. static inline bool is_10b_uint(unsigned long val)
  174. {
  175. return val < (1UL << 10);
  176. }
  177. static inline bool is_12b_int(long val)
  178. {
  179. return -(1L << 11) <= val && val < (1L << 11);
  180. }
  181. static inline int is_12b_check(int off, int insn)
  182. {
  183. if (!is_12b_int(off)) {
  184. pr_err("bpf-jit: insn=%d 12b < offset=%d not supported yet!\n",
  185. insn, (int)off);
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. static inline bool is_13b_int(long val)
  191. {
  192. return -(1L << 12) <= val && val < (1L << 12);
  193. }
  194. static inline bool is_21b_int(long val)
  195. {
  196. return -(1L << 20) <= val && val < (1L << 20);
  197. }
  198. static inline int rv_offset(int insn, int off, struct rv_jit_context *ctx)
  199. {
  200. int from, to;
  201. off++; /* BPF branch is from PC+1, RV is from PC */
  202. from = (insn > 0) ? ctx->offset[insn - 1] : ctx->prologue_len;
  203. to = (insn + off > 0) ? ctx->offset[insn + off - 1] : ctx->prologue_len;
  204. return ninsns_rvoff(to - from);
  205. }
  206. /* Instruction formats. */
  207. static inline u32 rv_r_insn(u8 funct7, u8 rs2, u8 rs1, u8 funct3, u8 rd,
  208. u8 opcode)
  209. {
  210. return (funct7 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
  211. (rd << 7) | opcode;
  212. }
  213. static inline u32 rv_i_insn(u16 imm11_0, u8 rs1, u8 funct3, u8 rd, u8 opcode)
  214. {
  215. return (imm11_0 << 20) | (rs1 << 15) | (funct3 << 12) | (rd << 7) |
  216. opcode;
  217. }
  218. static inline u32 rv_s_insn(u16 imm11_0, u8 rs2, u8 rs1, u8 funct3, u8 opcode)
  219. {
  220. u8 imm11_5 = imm11_0 >> 5, imm4_0 = imm11_0 & 0x1f;
  221. return (imm11_5 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
  222. (imm4_0 << 7) | opcode;
  223. }
  224. static inline u32 rv_b_insn(u16 imm12_1, u8 rs2, u8 rs1, u8 funct3, u8 opcode)
  225. {
  226. u8 imm12 = ((imm12_1 & 0x800) >> 5) | ((imm12_1 & 0x3f0) >> 4);
  227. u8 imm4_1 = ((imm12_1 & 0xf) << 1) | ((imm12_1 & 0x400) >> 10);
  228. return (imm12 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
  229. (imm4_1 << 7) | opcode;
  230. }
  231. static inline u32 rv_u_insn(u32 imm31_12, u8 rd, u8 opcode)
  232. {
  233. return (imm31_12 << 12) | (rd << 7) | opcode;
  234. }
  235. static inline u32 rv_j_insn(u32 imm20_1, u8 rd, u8 opcode)
  236. {
  237. u32 imm;
  238. imm = (imm20_1 & 0x80000) | ((imm20_1 & 0x3ff) << 9) |
  239. ((imm20_1 & 0x400) >> 2) | ((imm20_1 & 0x7f800) >> 11);
  240. return (imm << 12) | (rd << 7) | opcode;
  241. }
  242. static inline u32 rv_amo_insn(u8 funct5, u8 aq, u8 rl, u8 rs2, u8 rs1,
  243. u8 funct3, u8 rd, u8 opcode)
  244. {
  245. u8 funct7 = (funct5 << 2) | (aq << 1) | rl;
  246. return rv_r_insn(funct7, rs2, rs1, funct3, rd, opcode);
  247. }
  248. /* RISC-V compressed instruction formats. */
  249. static inline u16 rv_cr_insn(u8 funct4, u8 rd, u8 rs2, u8 op)
  250. {
  251. return (funct4 << 12) | (rd << 7) | (rs2 << 2) | op;
  252. }
  253. static inline u16 rv_ci_insn(u8 funct3, u32 imm6, u8 rd, u8 op)
  254. {
  255. u32 imm;
  256. imm = ((imm6 & 0x20) << 7) | ((imm6 & 0x1f) << 2);
  257. return (funct3 << 13) | (rd << 7) | op | imm;
  258. }
  259. static inline u16 rv_css_insn(u8 funct3, u32 uimm, u8 rs2, u8 op)
  260. {
  261. return (funct3 << 13) | (uimm << 7) | (rs2 << 2) | op;
  262. }
  263. static inline u16 rv_ciw_insn(u8 funct3, u32 uimm, u8 rd, u8 op)
  264. {
  265. return (funct3 << 13) | (uimm << 5) | ((rd & 0x7) << 2) | op;
  266. }
  267. static inline u16 rv_cl_insn(u8 funct3, u32 imm_hi, u8 rs1, u32 imm_lo, u8 rd,
  268. u8 op)
  269. {
  270. return (funct3 << 13) | (imm_hi << 10) | ((rs1 & 0x7) << 7) |
  271. (imm_lo << 5) | ((rd & 0x7) << 2) | op;
  272. }
  273. static inline u16 rv_cs_insn(u8 funct3, u32 imm_hi, u8 rs1, u32 imm_lo, u8 rs2,
  274. u8 op)
  275. {
  276. return (funct3 << 13) | (imm_hi << 10) | ((rs1 & 0x7) << 7) |
  277. (imm_lo << 5) | ((rs2 & 0x7) << 2) | op;
  278. }
  279. static inline u16 rv_ca_insn(u8 funct6, u8 rd, u8 funct2, u8 rs2, u8 op)
  280. {
  281. return (funct6 << 10) | ((rd & 0x7) << 7) | (funct2 << 5) |
  282. ((rs2 & 0x7) << 2) | op;
  283. }
  284. static inline u16 rv_cb_insn(u8 funct3, u32 imm6, u8 funct2, u8 rd, u8 op)
  285. {
  286. u32 imm;
  287. imm = ((imm6 & 0x20) << 7) | ((imm6 & 0x1f) << 2);
  288. return (funct3 << 13) | (funct2 << 10) | ((rd & 0x7) << 7) | op | imm;
  289. }
  290. /* Instructions shared by both RV32 and RV64. */
  291. static inline u32 rv_addi(u8 rd, u8 rs1, u16 imm11_0)
  292. {
  293. return rv_i_insn(imm11_0, rs1, 0, rd, 0x13);
  294. }
  295. static inline u32 rv_andi(u8 rd, u8 rs1, u16 imm11_0)
  296. {
  297. return rv_i_insn(imm11_0, rs1, 7, rd, 0x13);
  298. }
  299. static inline u32 rv_ori(u8 rd, u8 rs1, u16 imm11_0)
  300. {
  301. return rv_i_insn(imm11_0, rs1, 6, rd, 0x13);
  302. }
  303. static inline u32 rv_xori(u8 rd, u8 rs1, u16 imm11_0)
  304. {
  305. return rv_i_insn(imm11_0, rs1, 4, rd, 0x13);
  306. }
  307. static inline u32 rv_slli(u8 rd, u8 rs1, u16 imm11_0)
  308. {
  309. return rv_i_insn(imm11_0, rs1, 1, rd, 0x13);
  310. }
  311. static inline u32 rv_srli(u8 rd, u8 rs1, u16 imm11_0)
  312. {
  313. return rv_i_insn(imm11_0, rs1, 5, rd, 0x13);
  314. }
  315. static inline u32 rv_srai(u8 rd, u8 rs1, u16 imm11_0)
  316. {
  317. return rv_i_insn(0x400 | imm11_0, rs1, 5, rd, 0x13);
  318. }
  319. static inline u32 rv_lui(u8 rd, u32 imm31_12)
  320. {
  321. return rv_u_insn(imm31_12, rd, 0x37);
  322. }
  323. static inline u32 rv_auipc(u8 rd, u32 imm31_12)
  324. {
  325. return rv_u_insn(imm31_12, rd, 0x17);
  326. }
  327. static inline u32 rv_add(u8 rd, u8 rs1, u8 rs2)
  328. {
  329. return rv_r_insn(0, rs2, rs1, 0, rd, 0x33);
  330. }
  331. static inline u32 rv_sub(u8 rd, u8 rs1, u8 rs2)
  332. {
  333. return rv_r_insn(0x20, rs2, rs1, 0, rd, 0x33);
  334. }
  335. static inline u32 rv_sltu(u8 rd, u8 rs1, u8 rs2)
  336. {
  337. return rv_r_insn(0, rs2, rs1, 3, rd, 0x33);
  338. }
  339. static inline u32 rv_and(u8 rd, u8 rs1, u8 rs2)
  340. {
  341. return rv_r_insn(0, rs2, rs1, 7, rd, 0x33);
  342. }
  343. static inline u32 rv_or(u8 rd, u8 rs1, u8 rs2)
  344. {
  345. return rv_r_insn(0, rs2, rs1, 6, rd, 0x33);
  346. }
  347. static inline u32 rv_xor(u8 rd, u8 rs1, u8 rs2)
  348. {
  349. return rv_r_insn(0, rs2, rs1, 4, rd, 0x33);
  350. }
  351. static inline u32 rv_sll(u8 rd, u8 rs1, u8 rs2)
  352. {
  353. return rv_r_insn(0, rs2, rs1, 1, rd, 0x33);
  354. }
  355. static inline u32 rv_srl(u8 rd, u8 rs1, u8 rs2)
  356. {
  357. return rv_r_insn(0, rs2, rs1, 5, rd, 0x33);
  358. }
  359. static inline u32 rv_sra(u8 rd, u8 rs1, u8 rs2)
  360. {
  361. return rv_r_insn(0x20, rs2, rs1, 5, rd, 0x33);
  362. }
  363. static inline u32 rv_mul(u8 rd, u8 rs1, u8 rs2)
  364. {
  365. return rv_r_insn(1, rs2, rs1, 0, rd, 0x33);
  366. }
  367. static inline u32 rv_mulhu(u8 rd, u8 rs1, u8 rs2)
  368. {
  369. return rv_r_insn(1, rs2, rs1, 3, rd, 0x33);
  370. }
  371. static inline u32 rv_div(u8 rd, u8 rs1, u8 rs2)
  372. {
  373. return rv_r_insn(1, rs2, rs1, 4, rd, 0x33);
  374. }
  375. static inline u32 rv_divu(u8 rd, u8 rs1, u8 rs2)
  376. {
  377. return rv_r_insn(1, rs2, rs1, 5, rd, 0x33);
  378. }
  379. static inline u32 rv_rem(u8 rd, u8 rs1, u8 rs2)
  380. {
  381. return rv_r_insn(1, rs2, rs1, 6, rd, 0x33);
  382. }
  383. static inline u32 rv_remu(u8 rd, u8 rs1, u8 rs2)
  384. {
  385. return rv_r_insn(1, rs2, rs1, 7, rd, 0x33);
  386. }
  387. static inline u32 rv_jal(u8 rd, u32 imm20_1)
  388. {
  389. return rv_j_insn(imm20_1, rd, 0x6f);
  390. }
  391. static inline u32 rv_jalr(u8 rd, u8 rs1, u16 imm11_0)
  392. {
  393. return rv_i_insn(imm11_0, rs1, 0, rd, 0x67);
  394. }
  395. static inline u32 rv_beq(u8 rs1, u8 rs2, u16 imm12_1)
  396. {
  397. return rv_b_insn(imm12_1, rs2, rs1, 0, 0x63);
  398. }
  399. static inline u32 rv_bne(u8 rs1, u8 rs2, u16 imm12_1)
  400. {
  401. return rv_b_insn(imm12_1, rs2, rs1, 1, 0x63);
  402. }
  403. static inline u32 rv_bltu(u8 rs1, u8 rs2, u16 imm12_1)
  404. {
  405. return rv_b_insn(imm12_1, rs2, rs1, 6, 0x63);
  406. }
  407. static inline u32 rv_bgtu(u8 rs1, u8 rs2, u16 imm12_1)
  408. {
  409. return rv_bltu(rs2, rs1, imm12_1);
  410. }
  411. static inline u32 rv_bgeu(u8 rs1, u8 rs2, u16 imm12_1)
  412. {
  413. return rv_b_insn(imm12_1, rs2, rs1, 7, 0x63);
  414. }
  415. static inline u32 rv_bleu(u8 rs1, u8 rs2, u16 imm12_1)
  416. {
  417. return rv_bgeu(rs2, rs1, imm12_1);
  418. }
  419. static inline u32 rv_blt(u8 rs1, u8 rs2, u16 imm12_1)
  420. {
  421. return rv_b_insn(imm12_1, rs2, rs1, 4, 0x63);
  422. }
  423. static inline u32 rv_bgt(u8 rs1, u8 rs2, u16 imm12_1)
  424. {
  425. return rv_blt(rs2, rs1, imm12_1);
  426. }
  427. static inline u32 rv_bge(u8 rs1, u8 rs2, u16 imm12_1)
  428. {
  429. return rv_b_insn(imm12_1, rs2, rs1, 5, 0x63);
  430. }
  431. static inline u32 rv_ble(u8 rs1, u8 rs2, u16 imm12_1)
  432. {
  433. return rv_bge(rs2, rs1, imm12_1);
  434. }
  435. static inline u32 rv_lb(u8 rd, u16 imm11_0, u8 rs1)
  436. {
  437. return rv_i_insn(imm11_0, rs1, 0, rd, 0x03);
  438. }
  439. static inline u32 rv_lh(u8 rd, u16 imm11_0, u8 rs1)
  440. {
  441. return rv_i_insn(imm11_0, rs1, 1, rd, 0x03);
  442. }
  443. static inline u32 rv_lw(u8 rd, u16 imm11_0, u8 rs1)
  444. {
  445. return rv_i_insn(imm11_0, rs1, 2, rd, 0x03);
  446. }
  447. static inline u32 rv_lbu(u8 rd, u16 imm11_0, u8 rs1)
  448. {
  449. return rv_i_insn(imm11_0, rs1, 4, rd, 0x03);
  450. }
  451. static inline u32 rv_lhu(u8 rd, u16 imm11_0, u8 rs1)
  452. {
  453. return rv_i_insn(imm11_0, rs1, 5, rd, 0x03);
  454. }
  455. static inline u32 rv_sb(u8 rs1, u16 imm11_0, u8 rs2)
  456. {
  457. return rv_s_insn(imm11_0, rs2, rs1, 0, 0x23);
  458. }
  459. static inline u32 rv_sh(u8 rs1, u16 imm11_0, u8 rs2)
  460. {
  461. return rv_s_insn(imm11_0, rs2, rs1, 1, 0x23);
  462. }
  463. static inline u32 rv_sw(u8 rs1, u16 imm11_0, u8 rs2)
  464. {
  465. return rv_s_insn(imm11_0, rs2, rs1, 2, 0x23);
  466. }
  467. static inline u32 rv_amoadd_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  468. {
  469. return rv_amo_insn(0, aq, rl, rs2, rs1, 2, rd, 0x2f);
  470. }
  471. static inline u32 rv_amoand_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  472. {
  473. return rv_amo_insn(0xc, aq, rl, rs2, rs1, 2, rd, 0x2f);
  474. }
  475. static inline u32 rv_amoor_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  476. {
  477. return rv_amo_insn(0x8, aq, rl, rs2, rs1, 2, rd, 0x2f);
  478. }
  479. static inline u32 rv_amoxor_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  480. {
  481. return rv_amo_insn(0x4, aq, rl, rs2, rs1, 2, rd, 0x2f);
  482. }
  483. static inline u32 rv_amoswap_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  484. {
  485. return rv_amo_insn(0x1, aq, rl, rs2, rs1, 2, rd, 0x2f);
  486. }
  487. static inline u32 rv_lr_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  488. {
  489. return rv_amo_insn(0x2, aq, rl, rs2, rs1, 2, rd, 0x2f);
  490. }
  491. static inline u32 rv_sc_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  492. {
  493. return rv_amo_insn(0x3, aq, rl, rs2, rs1, 2, rd, 0x2f);
  494. }
  495. static inline u32 rv_fence(u8 pred, u8 succ)
  496. {
  497. u16 imm11_0 = pred << 4 | succ;
  498. return rv_i_insn(imm11_0, 0, 0, 0, 0xf);
  499. }
  500. static inline u32 rv_nop(void)
  501. {
  502. return rv_i_insn(0, 0, 0, 0, 0x13);
  503. }
  504. /* RVC instructions. */
  505. static inline u16 rvc_addi4spn(u8 rd, u32 imm10)
  506. {
  507. u32 imm;
  508. imm = ((imm10 & 0x30) << 2) | ((imm10 & 0x3c0) >> 4) |
  509. ((imm10 & 0x4) >> 1) | ((imm10 & 0x8) >> 3);
  510. return rv_ciw_insn(0x0, imm, rd, 0x0);
  511. }
  512. static inline u16 rvc_lw(u8 rd, u32 imm7, u8 rs1)
  513. {
  514. u32 imm_hi, imm_lo;
  515. imm_hi = (imm7 & 0x38) >> 3;
  516. imm_lo = ((imm7 & 0x4) >> 1) | ((imm7 & 0x40) >> 6);
  517. return rv_cl_insn(0x2, imm_hi, rs1, imm_lo, rd, 0x0);
  518. }
  519. static inline u16 rvc_sw(u8 rs1, u32 imm7, u8 rs2)
  520. {
  521. u32 imm_hi, imm_lo;
  522. imm_hi = (imm7 & 0x38) >> 3;
  523. imm_lo = ((imm7 & 0x4) >> 1) | ((imm7 & 0x40) >> 6);
  524. return rv_cs_insn(0x6, imm_hi, rs1, imm_lo, rs2, 0x0);
  525. }
  526. static inline u16 rvc_addi(u8 rd, u32 imm6)
  527. {
  528. return rv_ci_insn(0, imm6, rd, 0x1);
  529. }
  530. static inline u16 rvc_li(u8 rd, u32 imm6)
  531. {
  532. return rv_ci_insn(0x2, imm6, rd, 0x1);
  533. }
  534. static inline u16 rvc_addi16sp(u32 imm10)
  535. {
  536. u32 imm;
  537. imm = ((imm10 & 0x200) >> 4) | (imm10 & 0x10) | ((imm10 & 0x40) >> 3) |
  538. ((imm10 & 0x180) >> 6) | ((imm10 & 0x20) >> 5);
  539. return rv_ci_insn(0x3, imm, RV_REG_SP, 0x1);
  540. }
  541. static inline u16 rvc_lui(u8 rd, u32 imm6)
  542. {
  543. return rv_ci_insn(0x3, imm6, rd, 0x1);
  544. }
  545. static inline u16 rvc_srli(u8 rd, u32 imm6)
  546. {
  547. return rv_cb_insn(0x4, imm6, 0, rd, 0x1);
  548. }
  549. static inline u16 rvc_srai(u8 rd, u32 imm6)
  550. {
  551. return rv_cb_insn(0x4, imm6, 0x1, rd, 0x1);
  552. }
  553. static inline u16 rvc_andi(u8 rd, u32 imm6)
  554. {
  555. return rv_cb_insn(0x4, imm6, 0x2, rd, 0x1);
  556. }
  557. static inline u16 rvc_sub(u8 rd, u8 rs)
  558. {
  559. return rv_ca_insn(0x23, rd, 0, rs, 0x1);
  560. }
  561. static inline u16 rvc_xor(u8 rd, u8 rs)
  562. {
  563. return rv_ca_insn(0x23, rd, 0x1, rs, 0x1);
  564. }
  565. static inline u16 rvc_or(u8 rd, u8 rs)
  566. {
  567. return rv_ca_insn(0x23, rd, 0x2, rs, 0x1);
  568. }
  569. static inline u16 rvc_and(u8 rd, u8 rs)
  570. {
  571. return rv_ca_insn(0x23, rd, 0x3, rs, 0x1);
  572. }
  573. static inline u16 rvc_slli(u8 rd, u32 imm6)
  574. {
  575. return rv_ci_insn(0, imm6, rd, 0x2);
  576. }
  577. static inline u16 rvc_lwsp(u8 rd, u32 imm8)
  578. {
  579. u32 imm;
  580. imm = ((imm8 & 0xc0) >> 6) | (imm8 & 0x3c);
  581. return rv_ci_insn(0x2, imm, rd, 0x2);
  582. }
  583. static inline u16 rvc_jr(u8 rs1)
  584. {
  585. return rv_cr_insn(0x8, rs1, RV_REG_ZERO, 0x2);
  586. }
  587. static inline u16 rvc_mv(u8 rd, u8 rs)
  588. {
  589. return rv_cr_insn(0x8, rd, rs, 0x2);
  590. }
  591. static inline u16 rvc_jalr(u8 rs1)
  592. {
  593. return rv_cr_insn(0x9, rs1, RV_REG_ZERO, 0x2);
  594. }
  595. static inline u16 rvc_add(u8 rd, u8 rs)
  596. {
  597. return rv_cr_insn(0x9, rd, rs, 0x2);
  598. }
  599. static inline u16 rvc_swsp(u32 imm8, u8 rs2)
  600. {
  601. u32 imm;
  602. imm = (imm8 & 0x3c) | ((imm8 & 0xc0) >> 6);
  603. return rv_css_insn(0x6, imm, rs2, 0x2);
  604. }
  605. /* RVZBA instructions. */
  606. static inline u32 rvzba_sh2add(u8 rd, u8 rs1, u8 rs2)
  607. {
  608. return rv_r_insn(0x10, rs2, rs1, 0x4, rd, 0x33);
  609. }
  610. static inline u32 rvzba_sh3add(u8 rd, u8 rs1, u8 rs2)
  611. {
  612. return rv_r_insn(0x10, rs2, rs1, 0x6, rd, 0x33);
  613. }
  614. /* RVZBB instructions. */
  615. static inline u32 rvzbb_sextb(u8 rd, u8 rs1)
  616. {
  617. return rv_i_insn(0x604, rs1, 1, rd, 0x13);
  618. }
  619. static inline u32 rvzbb_sexth(u8 rd, u8 rs1)
  620. {
  621. return rv_i_insn(0x605, rs1, 1, rd, 0x13);
  622. }
  623. static inline u32 rvzbb_zexth(u8 rd, u8 rs)
  624. {
  625. if (IS_ENABLED(CONFIG_64BIT))
  626. return rv_i_insn(0x80, rs, 4, rd, 0x3b);
  627. return rv_i_insn(0x80, rs, 4, rd, 0x33);
  628. }
  629. static inline u32 rvzbb_rev8(u8 rd, u8 rs)
  630. {
  631. if (IS_ENABLED(CONFIG_64BIT))
  632. return rv_i_insn(0x6b8, rs, 5, rd, 0x13);
  633. return rv_i_insn(0x698, rs, 5, rd, 0x13);
  634. }
  635. /*
  636. * RV64-only instructions.
  637. *
  638. * These instructions are not available on RV32. Wrap them below a #if to
  639. * ensure that the RV32 JIT doesn't emit any of these instructions.
  640. */
  641. #if __riscv_xlen == 64
  642. static inline u32 rv_addiw(u8 rd, u8 rs1, u16 imm11_0)
  643. {
  644. return rv_i_insn(imm11_0, rs1, 0, rd, 0x1b);
  645. }
  646. static inline u32 rv_slliw(u8 rd, u8 rs1, u16 imm11_0)
  647. {
  648. return rv_i_insn(imm11_0, rs1, 1, rd, 0x1b);
  649. }
  650. static inline u32 rv_srliw(u8 rd, u8 rs1, u16 imm11_0)
  651. {
  652. return rv_i_insn(imm11_0, rs1, 5, rd, 0x1b);
  653. }
  654. static inline u32 rv_sraiw(u8 rd, u8 rs1, u16 imm11_0)
  655. {
  656. return rv_i_insn(0x400 | imm11_0, rs1, 5, rd, 0x1b);
  657. }
  658. static inline u32 rv_addw(u8 rd, u8 rs1, u8 rs2)
  659. {
  660. return rv_r_insn(0, rs2, rs1, 0, rd, 0x3b);
  661. }
  662. static inline u32 rv_subw(u8 rd, u8 rs1, u8 rs2)
  663. {
  664. return rv_r_insn(0x20, rs2, rs1, 0, rd, 0x3b);
  665. }
  666. static inline u32 rv_sllw(u8 rd, u8 rs1, u8 rs2)
  667. {
  668. return rv_r_insn(0, rs2, rs1, 1, rd, 0x3b);
  669. }
  670. static inline u32 rv_srlw(u8 rd, u8 rs1, u8 rs2)
  671. {
  672. return rv_r_insn(0, rs2, rs1, 5, rd, 0x3b);
  673. }
  674. static inline u32 rv_sraw(u8 rd, u8 rs1, u8 rs2)
  675. {
  676. return rv_r_insn(0x20, rs2, rs1, 5, rd, 0x3b);
  677. }
  678. static inline u32 rv_mulw(u8 rd, u8 rs1, u8 rs2)
  679. {
  680. return rv_r_insn(1, rs2, rs1, 0, rd, 0x3b);
  681. }
  682. static inline u32 rv_divw(u8 rd, u8 rs1, u8 rs2)
  683. {
  684. return rv_r_insn(1, rs2, rs1, 4, rd, 0x3b);
  685. }
  686. static inline u32 rv_divuw(u8 rd, u8 rs1, u8 rs2)
  687. {
  688. return rv_r_insn(1, rs2, rs1, 5, rd, 0x3b);
  689. }
  690. static inline u32 rv_remw(u8 rd, u8 rs1, u8 rs2)
  691. {
  692. return rv_r_insn(1, rs2, rs1, 6, rd, 0x3b);
  693. }
  694. static inline u32 rv_remuw(u8 rd, u8 rs1, u8 rs2)
  695. {
  696. return rv_r_insn(1, rs2, rs1, 7, rd, 0x3b);
  697. }
  698. static inline u32 rv_ld(u8 rd, u16 imm11_0, u8 rs1)
  699. {
  700. return rv_i_insn(imm11_0, rs1, 3, rd, 0x03);
  701. }
  702. static inline u32 rv_lwu(u8 rd, u16 imm11_0, u8 rs1)
  703. {
  704. return rv_i_insn(imm11_0, rs1, 6, rd, 0x03);
  705. }
  706. static inline u32 rv_sd(u8 rs1, u16 imm11_0, u8 rs2)
  707. {
  708. return rv_s_insn(imm11_0, rs2, rs1, 3, 0x23);
  709. }
  710. static inline u32 rv_amoadd_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  711. {
  712. return rv_amo_insn(0, aq, rl, rs2, rs1, 3, rd, 0x2f);
  713. }
  714. static inline u32 rv_amoand_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  715. {
  716. return rv_amo_insn(0xc, aq, rl, rs2, rs1, 3, rd, 0x2f);
  717. }
  718. static inline u32 rv_amoor_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  719. {
  720. return rv_amo_insn(0x8, aq, rl, rs2, rs1, 3, rd, 0x2f);
  721. }
  722. static inline u32 rv_amoxor_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  723. {
  724. return rv_amo_insn(0x4, aq, rl, rs2, rs1, 3, rd, 0x2f);
  725. }
  726. static inline u32 rv_amoswap_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  727. {
  728. return rv_amo_insn(0x1, aq, rl, rs2, rs1, 3, rd, 0x2f);
  729. }
  730. static inline u32 rv_lr_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  731. {
  732. return rv_amo_insn(0x2, aq, rl, rs2, rs1, 3, rd, 0x2f);
  733. }
  734. static inline u32 rv_sc_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
  735. {
  736. return rv_amo_insn(0x3, aq, rl, rs2, rs1, 3, rd, 0x2f);
  737. }
  738. /* RV64-only RVC instructions. */
  739. static inline u16 rvc_ld(u8 rd, u32 imm8, u8 rs1)
  740. {
  741. u32 imm_hi, imm_lo;
  742. imm_hi = (imm8 & 0x38) >> 3;
  743. imm_lo = (imm8 & 0xc0) >> 6;
  744. return rv_cl_insn(0x3, imm_hi, rs1, imm_lo, rd, 0x0);
  745. }
  746. static inline u16 rvc_sd(u8 rs1, u32 imm8, u8 rs2)
  747. {
  748. u32 imm_hi, imm_lo;
  749. imm_hi = (imm8 & 0x38) >> 3;
  750. imm_lo = (imm8 & 0xc0) >> 6;
  751. return rv_cs_insn(0x7, imm_hi, rs1, imm_lo, rs2, 0x0);
  752. }
  753. static inline u16 rvc_subw(u8 rd, u8 rs)
  754. {
  755. return rv_ca_insn(0x27, rd, 0, rs, 0x1);
  756. }
  757. static inline u16 rvc_addiw(u8 rd, u32 imm6)
  758. {
  759. return rv_ci_insn(0x1, imm6, rd, 0x1);
  760. }
  761. static inline u16 rvc_ldsp(u8 rd, u32 imm9)
  762. {
  763. u32 imm;
  764. imm = ((imm9 & 0x1c0) >> 6) | (imm9 & 0x38);
  765. return rv_ci_insn(0x3, imm, rd, 0x2);
  766. }
  767. static inline u16 rvc_sdsp(u32 imm9, u8 rs2)
  768. {
  769. u32 imm;
  770. imm = (imm9 & 0x38) | ((imm9 & 0x1c0) >> 6);
  771. return rv_css_insn(0x7, imm, rs2, 0x2);
  772. }
  773. /* RV64-only ZBA instructions. */
  774. static inline u32 rvzba_zextw(u8 rd, u8 rs1)
  775. {
  776. /* add.uw rd, rs1, ZERO */
  777. return rv_r_insn(0x04, RV_REG_ZERO, rs1, 0, rd, 0x3b);
  778. }
  779. #endif /* __riscv_xlen == 64 */
  780. /* Helper functions that emit RVC instructions when possible. */
  781. static inline void emit_jalr(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
  782. {
  783. if (rvc_enabled() && rd == RV_REG_RA && rs && !imm)
  784. emitc(rvc_jalr(rs), ctx);
  785. else if (rvc_enabled() && !rd && rs && !imm)
  786. emitc(rvc_jr(rs), ctx);
  787. else
  788. emit(rv_jalr(rd, rs, imm), ctx);
  789. }
  790. static inline void emit_mv(u8 rd, u8 rs, struct rv_jit_context *ctx)
  791. {
  792. if (rvc_enabled() && rd && rs)
  793. emitc(rvc_mv(rd, rs), ctx);
  794. else
  795. emit(rv_addi(rd, rs, 0), ctx);
  796. }
  797. static inline void emit_add(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
  798. {
  799. if (rvc_enabled() && rd && rd == rs1 && rs2)
  800. emitc(rvc_add(rd, rs2), ctx);
  801. else
  802. emit(rv_add(rd, rs1, rs2), ctx);
  803. }
  804. static inline void emit_addi(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
  805. {
  806. if (rvc_enabled() && rd == RV_REG_SP && rd == rs && is_10b_int(imm) && imm && !(imm & 0xf))
  807. emitc(rvc_addi16sp(imm), ctx);
  808. else if (rvc_enabled() && is_creg(rd) && rs == RV_REG_SP && is_10b_uint(imm) &&
  809. !(imm & 0x3) && imm)
  810. emitc(rvc_addi4spn(rd, imm), ctx);
  811. else if (rvc_enabled() && rd && rd == rs && imm && is_6b_int(imm))
  812. emitc(rvc_addi(rd, imm), ctx);
  813. else
  814. emit(rv_addi(rd, rs, imm), ctx);
  815. }
  816. static inline void emit_li(u8 rd, s32 imm, struct rv_jit_context *ctx)
  817. {
  818. if (rvc_enabled() && rd && is_6b_int(imm))
  819. emitc(rvc_li(rd, imm), ctx);
  820. else
  821. emit(rv_addi(rd, RV_REG_ZERO, imm), ctx);
  822. }
  823. static inline void emit_lui(u8 rd, s32 imm, struct rv_jit_context *ctx)
  824. {
  825. if (rvc_enabled() && rd && rd != RV_REG_SP && is_6b_int(imm) && imm)
  826. emitc(rvc_lui(rd, imm), ctx);
  827. else
  828. emit(rv_lui(rd, imm), ctx);
  829. }
  830. static inline void emit_slli(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
  831. {
  832. if (rvc_enabled() && rd && rd == rs && imm && (u32)imm < __riscv_xlen)
  833. emitc(rvc_slli(rd, imm), ctx);
  834. else
  835. emit(rv_slli(rd, rs, imm), ctx);
  836. }
  837. static inline void emit_andi(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
  838. {
  839. if (rvc_enabled() && is_creg(rd) && rd == rs && is_6b_int(imm))
  840. emitc(rvc_andi(rd, imm), ctx);
  841. else
  842. emit(rv_andi(rd, rs, imm), ctx);
  843. }
  844. static inline void emit_srli(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
  845. {
  846. if (rvc_enabled() && is_creg(rd) && rd == rs && imm && (u32)imm < __riscv_xlen)
  847. emitc(rvc_srli(rd, imm), ctx);
  848. else
  849. emit(rv_srli(rd, rs, imm), ctx);
  850. }
  851. static inline void emit_srai(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
  852. {
  853. if (rvc_enabled() && is_creg(rd) && rd == rs && imm && (u32)imm < __riscv_xlen)
  854. emitc(rvc_srai(rd, imm), ctx);
  855. else
  856. emit(rv_srai(rd, rs, imm), ctx);
  857. }
  858. static inline void emit_sub(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
  859. {
  860. if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
  861. emitc(rvc_sub(rd, rs2), ctx);
  862. else
  863. emit(rv_sub(rd, rs1, rs2), ctx);
  864. }
  865. static inline void emit_or(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
  866. {
  867. if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
  868. emitc(rvc_or(rd, rs2), ctx);
  869. else
  870. emit(rv_or(rd, rs1, rs2), ctx);
  871. }
  872. static inline void emit_and(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
  873. {
  874. if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
  875. emitc(rvc_and(rd, rs2), ctx);
  876. else
  877. emit(rv_and(rd, rs1, rs2), ctx);
  878. }
  879. static inline void emit_xor(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
  880. {
  881. if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
  882. emitc(rvc_xor(rd, rs2), ctx);
  883. else
  884. emit(rv_xor(rd, rs1, rs2), ctx);
  885. }
  886. static inline void emit_lw(u8 rd, s32 off, u8 rs1, struct rv_jit_context *ctx)
  887. {
  888. if (rvc_enabled() && rs1 == RV_REG_SP && rd && is_8b_uint(off) && !(off & 0x3))
  889. emitc(rvc_lwsp(rd, off), ctx);
  890. else if (rvc_enabled() && is_creg(rd) && is_creg(rs1) && is_7b_uint(off) && !(off & 0x3))
  891. emitc(rvc_lw(rd, off, rs1), ctx);
  892. else
  893. emit(rv_lw(rd, off, rs1), ctx);
  894. }
  895. static inline void emit_sw(u8 rs1, s32 off, u8 rs2, struct rv_jit_context *ctx)
  896. {
  897. if (rvc_enabled() && rs1 == RV_REG_SP && is_8b_uint(off) && !(off & 0x3))
  898. emitc(rvc_swsp(off, rs2), ctx);
  899. else if (rvc_enabled() && is_creg(rs1) && is_creg(rs2) && is_7b_uint(off) && !(off & 0x3))
  900. emitc(rvc_sw(rs1, off, rs2), ctx);
  901. else
  902. emit(rv_sw(rs1, off, rs2), ctx);
  903. }
  904. static inline void emit_sh2add(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
  905. {
  906. if (rvzba_enabled()) {
  907. emit(rvzba_sh2add(rd, rs1, rs2), ctx);
  908. return;
  909. }
  910. emit_slli(rd, rs1, 2, ctx);
  911. emit_add(rd, rd, rs2, ctx);
  912. }
  913. static inline void emit_sh3add(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
  914. {
  915. if (rvzba_enabled()) {
  916. emit(rvzba_sh3add(rd, rs1, rs2), ctx);
  917. return;
  918. }
  919. emit_slli(rd, rs1, 3, ctx);
  920. emit_add(rd, rd, rs2, ctx);
  921. }
  922. /* RV64-only helper functions. */
  923. #if __riscv_xlen == 64
  924. static inline void emit_addiw(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
  925. {
  926. if (rvc_enabled() && rd && rd == rs && is_6b_int(imm))
  927. emitc(rvc_addiw(rd, imm), ctx);
  928. else
  929. emit(rv_addiw(rd, rs, imm), ctx);
  930. }
  931. static inline void emit_ld(u8 rd, s32 off, u8 rs1, struct rv_jit_context *ctx)
  932. {
  933. if (rvc_enabled() && rs1 == RV_REG_SP && rd && is_9b_uint(off) && !(off & 0x7))
  934. emitc(rvc_ldsp(rd, off), ctx);
  935. else if (rvc_enabled() && is_creg(rd) && is_creg(rs1) && is_8b_uint(off) && !(off & 0x7))
  936. emitc(rvc_ld(rd, off, rs1), ctx);
  937. else
  938. emit(rv_ld(rd, off, rs1), ctx);
  939. }
  940. static inline void emit_sd(u8 rs1, s32 off, u8 rs2, struct rv_jit_context *ctx)
  941. {
  942. if (rvc_enabled() && rs1 == RV_REG_SP && is_9b_uint(off) && !(off & 0x7))
  943. emitc(rvc_sdsp(off, rs2), ctx);
  944. else if (rvc_enabled() && is_creg(rs1) && is_creg(rs2) && is_8b_uint(off) && !(off & 0x7))
  945. emitc(rvc_sd(rs1, off, rs2), ctx);
  946. else
  947. emit(rv_sd(rs1, off, rs2), ctx);
  948. }
  949. static inline void emit_subw(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
  950. {
  951. if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
  952. emitc(rvc_subw(rd, rs2), ctx);
  953. else
  954. emit(rv_subw(rd, rs1, rs2), ctx);
  955. }
  956. static inline void emit_sextb(u8 rd, u8 rs, struct rv_jit_context *ctx)
  957. {
  958. if (rvzbb_enabled()) {
  959. emit(rvzbb_sextb(rd, rs), ctx);
  960. return;
  961. }
  962. emit_slli(rd, rs, 56, ctx);
  963. emit_srai(rd, rd, 56, ctx);
  964. }
  965. static inline void emit_sexth(u8 rd, u8 rs, struct rv_jit_context *ctx)
  966. {
  967. if (rvzbb_enabled()) {
  968. emit(rvzbb_sexth(rd, rs), ctx);
  969. return;
  970. }
  971. emit_slli(rd, rs, 48, ctx);
  972. emit_srai(rd, rd, 48, ctx);
  973. }
  974. static inline void emit_sextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
  975. {
  976. emit_addiw(rd, rs, 0, ctx);
  977. }
  978. static inline void emit_zexth(u8 rd, u8 rs, struct rv_jit_context *ctx)
  979. {
  980. if (rvzbb_enabled()) {
  981. emit(rvzbb_zexth(rd, rs), ctx);
  982. return;
  983. }
  984. emit_slli(rd, rs, 48, ctx);
  985. emit_srli(rd, rd, 48, ctx);
  986. }
  987. static inline void emit_zextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
  988. {
  989. if (rvzba_enabled()) {
  990. emit(rvzba_zextw(rd, rs), ctx);
  991. return;
  992. }
  993. emit_slli(rd, rs, 32, ctx);
  994. emit_srli(rd, rd, 32, ctx);
  995. }
  996. static inline void emit_bswap(u8 rd, s32 imm, struct rv_jit_context *ctx)
  997. {
  998. if (rvzbb_enabled()) {
  999. int bits = 64 - imm;
  1000. emit(rvzbb_rev8(rd, rd), ctx);
  1001. if (bits)
  1002. emit_srli(rd, rd, bits, ctx);
  1003. return;
  1004. }
  1005. emit_li(RV_REG_T2, 0, ctx);
  1006. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  1007. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  1008. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  1009. emit_srli(rd, rd, 8, ctx);
  1010. if (imm == 16)
  1011. goto out_be;
  1012. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  1013. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  1014. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  1015. emit_srli(rd, rd, 8, ctx);
  1016. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  1017. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  1018. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  1019. emit_srli(rd, rd, 8, ctx);
  1020. if (imm == 32)
  1021. goto out_be;
  1022. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  1023. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  1024. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  1025. emit_srli(rd, rd, 8, ctx);
  1026. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  1027. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  1028. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  1029. emit_srli(rd, rd, 8, ctx);
  1030. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  1031. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  1032. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  1033. emit_srli(rd, rd, 8, ctx);
  1034. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  1035. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  1036. emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
  1037. emit_srli(rd, rd, 8, ctx);
  1038. out_be:
  1039. emit_andi(RV_REG_T1, rd, 0xff, ctx);
  1040. emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
  1041. emit_mv(rd, RV_REG_T2, ctx);
  1042. }
  1043. #endif /* __riscv_xlen == 64 */
  1044. void bpf_jit_build_prologue(struct rv_jit_context *ctx, bool is_subprog);
  1045. void bpf_jit_build_epilogue(struct rv_jit_context *ctx);
  1046. int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
  1047. bool extra_pass);
  1048. #endif /* _BPF_JIT_H */