bpf_jit_comp64.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* BPF JIT compiler for RV64G
  3. *
  4. * Copyright(c) 2019 Björn Töpel <bjorn.topel@gmail.com>
  5. *
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bpf.h>
  9. #include <linux/filter.h>
  10. #include <linux/memory.h>
  11. #include <linux/stop_machine.h>
  12. #include <asm/patch.h>
  13. #include <asm/cfi.h>
  14. #include <asm/percpu.h>
  15. #include "bpf_jit.h"
  16. #define RV_MAX_REG_ARGS 8
  17. #define RV_FENTRY_NINSNS 2
  18. #define RV_FENTRY_NBYTES (RV_FENTRY_NINSNS * 4)
  19. #define RV_KCFI_NINSNS (IS_ENABLED(CONFIG_CFI_CLANG) ? 1 : 0)
  20. /* imm that allows emit_imm to emit max count insns */
  21. #define RV_MAX_COUNT_IMM 0x7FFF7FF7FF7FF7FF
  22. #define RV_REG_TCC RV_REG_A6
  23. #define RV_REG_TCC_SAVED RV_REG_S6 /* Store A6 in S6 if program do calls */
  24. #define RV_REG_ARENA RV_REG_S7 /* For storing arena_vm_start */
  25. static const int regmap[] = {
  26. [BPF_REG_0] = RV_REG_A5,
  27. [BPF_REG_1] = RV_REG_A0,
  28. [BPF_REG_2] = RV_REG_A1,
  29. [BPF_REG_3] = RV_REG_A2,
  30. [BPF_REG_4] = RV_REG_A3,
  31. [BPF_REG_5] = RV_REG_A4,
  32. [BPF_REG_6] = RV_REG_S1,
  33. [BPF_REG_7] = RV_REG_S2,
  34. [BPF_REG_8] = RV_REG_S3,
  35. [BPF_REG_9] = RV_REG_S4,
  36. [BPF_REG_FP] = RV_REG_S5,
  37. [BPF_REG_AX] = RV_REG_T0,
  38. };
  39. static const int pt_regmap[] = {
  40. [RV_REG_A0] = offsetof(struct pt_regs, a0),
  41. [RV_REG_A1] = offsetof(struct pt_regs, a1),
  42. [RV_REG_A2] = offsetof(struct pt_regs, a2),
  43. [RV_REG_A3] = offsetof(struct pt_regs, a3),
  44. [RV_REG_A4] = offsetof(struct pt_regs, a4),
  45. [RV_REG_A5] = offsetof(struct pt_regs, a5),
  46. [RV_REG_S1] = offsetof(struct pt_regs, s1),
  47. [RV_REG_S2] = offsetof(struct pt_regs, s2),
  48. [RV_REG_S3] = offsetof(struct pt_regs, s3),
  49. [RV_REG_S4] = offsetof(struct pt_regs, s4),
  50. [RV_REG_S5] = offsetof(struct pt_regs, s5),
  51. [RV_REG_T0] = offsetof(struct pt_regs, t0),
  52. };
  53. enum {
  54. RV_CTX_F_SEEN_TAIL_CALL = 0,
  55. RV_CTX_F_SEEN_CALL = RV_REG_RA,
  56. RV_CTX_F_SEEN_S1 = RV_REG_S1,
  57. RV_CTX_F_SEEN_S2 = RV_REG_S2,
  58. RV_CTX_F_SEEN_S3 = RV_REG_S3,
  59. RV_CTX_F_SEEN_S4 = RV_REG_S4,
  60. RV_CTX_F_SEEN_S5 = RV_REG_S5,
  61. RV_CTX_F_SEEN_S6 = RV_REG_S6,
  62. };
  63. static u8 bpf_to_rv_reg(int bpf_reg, struct rv_jit_context *ctx)
  64. {
  65. u8 reg = regmap[bpf_reg];
  66. switch (reg) {
  67. case RV_CTX_F_SEEN_S1:
  68. case RV_CTX_F_SEEN_S2:
  69. case RV_CTX_F_SEEN_S3:
  70. case RV_CTX_F_SEEN_S4:
  71. case RV_CTX_F_SEEN_S5:
  72. case RV_CTX_F_SEEN_S6:
  73. __set_bit(reg, &ctx->flags);
  74. }
  75. return reg;
  76. };
  77. static bool seen_reg(int reg, struct rv_jit_context *ctx)
  78. {
  79. switch (reg) {
  80. case RV_CTX_F_SEEN_CALL:
  81. case RV_CTX_F_SEEN_S1:
  82. case RV_CTX_F_SEEN_S2:
  83. case RV_CTX_F_SEEN_S3:
  84. case RV_CTX_F_SEEN_S4:
  85. case RV_CTX_F_SEEN_S5:
  86. case RV_CTX_F_SEEN_S6:
  87. return test_bit(reg, &ctx->flags);
  88. }
  89. return false;
  90. }
  91. static void mark_fp(struct rv_jit_context *ctx)
  92. {
  93. __set_bit(RV_CTX_F_SEEN_S5, &ctx->flags);
  94. }
  95. static void mark_call(struct rv_jit_context *ctx)
  96. {
  97. __set_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
  98. }
  99. static bool seen_call(struct rv_jit_context *ctx)
  100. {
  101. return test_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
  102. }
  103. static void mark_tail_call(struct rv_jit_context *ctx)
  104. {
  105. __set_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
  106. }
  107. static bool seen_tail_call(struct rv_jit_context *ctx)
  108. {
  109. return test_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
  110. }
  111. static u8 rv_tail_call_reg(struct rv_jit_context *ctx)
  112. {
  113. mark_tail_call(ctx);
  114. if (seen_call(ctx)) {
  115. __set_bit(RV_CTX_F_SEEN_S6, &ctx->flags);
  116. return RV_REG_S6;
  117. }
  118. return RV_REG_A6;
  119. }
  120. static bool is_32b_int(s64 val)
  121. {
  122. return -(1L << 31) <= val && val < (1L << 31);
  123. }
  124. static bool in_auipc_jalr_range(s64 val)
  125. {
  126. /*
  127. * auipc+jalr can reach any signed PC-relative offset in the range
  128. * [-2^31 - 2^11, 2^31 - 2^11).
  129. */
  130. return (-(1L << 31) - (1L << 11)) <= val &&
  131. val < ((1L << 31) - (1L << 11));
  132. }
  133. /* Modify rd pointer to alternate reg to avoid corrupting original reg */
  134. static void emit_sextw_alt(u8 *rd, u8 ra, struct rv_jit_context *ctx)
  135. {
  136. emit_sextw(ra, *rd, ctx);
  137. *rd = ra;
  138. }
  139. static void emit_zextw_alt(u8 *rd, u8 ra, struct rv_jit_context *ctx)
  140. {
  141. emit_zextw(ra, *rd, ctx);
  142. *rd = ra;
  143. }
  144. /* Emit fixed-length instructions for address */
  145. static int emit_addr(u8 rd, u64 addr, bool extra_pass, struct rv_jit_context *ctx)
  146. {
  147. /*
  148. * Use the ro_insns(RX) to calculate the offset as the BPF program will
  149. * finally run from this memory region.
  150. */
  151. u64 ip = (u64)(ctx->ro_insns + ctx->ninsns);
  152. s64 off = addr - ip;
  153. s64 upper = (off + (1 << 11)) >> 12;
  154. s64 lower = off & 0xfff;
  155. if (extra_pass && !in_auipc_jalr_range(off)) {
  156. pr_err("bpf-jit: target offset 0x%llx is out of range\n", off);
  157. return -ERANGE;
  158. }
  159. emit(rv_auipc(rd, upper), ctx);
  160. emit(rv_addi(rd, rd, lower), ctx);
  161. return 0;
  162. }
  163. /* Emit variable-length instructions for 32-bit and 64-bit imm */
  164. static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx)
  165. {
  166. /* Note that the immediate from the add is sign-extended,
  167. * which means that we need to compensate this by adding 2^12,
  168. * when the 12th bit is set. A simpler way of doing this, and
  169. * getting rid of the check, is to just add 2**11 before the
  170. * shift. The "Loading a 32-Bit constant" example from the
  171. * "Computer Organization and Design, RISC-V edition" book by
  172. * Patterson/Hennessy highlights this fact.
  173. *
  174. * This also means that we need to process LSB to MSB.
  175. */
  176. s64 upper = (val + (1 << 11)) >> 12;
  177. /* Sign-extend lower 12 bits to 64 bits since immediates for li, addiw,
  178. * and addi are signed and RVC checks will perform signed comparisons.
  179. */
  180. s64 lower = ((val & 0xfff) << 52) >> 52;
  181. int shift;
  182. if (is_32b_int(val)) {
  183. if (upper)
  184. emit_lui(rd, upper, ctx);
  185. if (!upper) {
  186. emit_li(rd, lower, ctx);
  187. return;
  188. }
  189. emit_addiw(rd, rd, lower, ctx);
  190. return;
  191. }
  192. shift = __ffs(upper);
  193. upper >>= shift;
  194. shift += 12;
  195. emit_imm(rd, upper, ctx);
  196. emit_slli(rd, rd, shift, ctx);
  197. if (lower)
  198. emit_addi(rd, rd, lower, ctx);
  199. }
  200. static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
  201. {
  202. int stack_adjust = ctx->stack_size, store_offset = stack_adjust - 8;
  203. if (seen_reg(RV_REG_RA, ctx)) {
  204. emit_ld(RV_REG_RA, store_offset, RV_REG_SP, ctx);
  205. store_offset -= 8;
  206. }
  207. emit_ld(RV_REG_FP, store_offset, RV_REG_SP, ctx);
  208. store_offset -= 8;
  209. if (seen_reg(RV_REG_S1, ctx)) {
  210. emit_ld(RV_REG_S1, store_offset, RV_REG_SP, ctx);
  211. store_offset -= 8;
  212. }
  213. if (seen_reg(RV_REG_S2, ctx)) {
  214. emit_ld(RV_REG_S2, store_offset, RV_REG_SP, ctx);
  215. store_offset -= 8;
  216. }
  217. if (seen_reg(RV_REG_S3, ctx)) {
  218. emit_ld(RV_REG_S3, store_offset, RV_REG_SP, ctx);
  219. store_offset -= 8;
  220. }
  221. if (seen_reg(RV_REG_S4, ctx)) {
  222. emit_ld(RV_REG_S4, store_offset, RV_REG_SP, ctx);
  223. store_offset -= 8;
  224. }
  225. if (seen_reg(RV_REG_S5, ctx)) {
  226. emit_ld(RV_REG_S5, store_offset, RV_REG_SP, ctx);
  227. store_offset -= 8;
  228. }
  229. if (seen_reg(RV_REG_S6, ctx)) {
  230. emit_ld(RV_REG_S6, store_offset, RV_REG_SP, ctx);
  231. store_offset -= 8;
  232. }
  233. if (ctx->arena_vm_start) {
  234. emit_ld(RV_REG_ARENA, store_offset, RV_REG_SP, ctx);
  235. store_offset -= 8;
  236. }
  237. emit_addi(RV_REG_SP, RV_REG_SP, stack_adjust, ctx);
  238. /* Set return value. */
  239. if (!is_tail_call)
  240. emit_addiw(RV_REG_A0, RV_REG_A5, 0, ctx);
  241. emit_jalr(RV_REG_ZERO, is_tail_call ? RV_REG_T3 : RV_REG_RA,
  242. /* kcfi, fentry and TCC init insns will be skipped on tailcall */
  243. is_tail_call ? (RV_KCFI_NINSNS + RV_FENTRY_NINSNS + 1) * 4 : 0,
  244. ctx);
  245. }
  246. static void emit_bcc(u8 cond, u8 rd, u8 rs, int rvoff,
  247. struct rv_jit_context *ctx)
  248. {
  249. switch (cond) {
  250. case BPF_JEQ:
  251. emit(rv_beq(rd, rs, rvoff >> 1), ctx);
  252. return;
  253. case BPF_JGT:
  254. emit(rv_bltu(rs, rd, rvoff >> 1), ctx);
  255. return;
  256. case BPF_JLT:
  257. emit(rv_bltu(rd, rs, rvoff >> 1), ctx);
  258. return;
  259. case BPF_JGE:
  260. emit(rv_bgeu(rd, rs, rvoff >> 1), ctx);
  261. return;
  262. case BPF_JLE:
  263. emit(rv_bgeu(rs, rd, rvoff >> 1), ctx);
  264. return;
  265. case BPF_JNE:
  266. emit(rv_bne(rd, rs, rvoff >> 1), ctx);
  267. return;
  268. case BPF_JSGT:
  269. emit(rv_blt(rs, rd, rvoff >> 1), ctx);
  270. return;
  271. case BPF_JSLT:
  272. emit(rv_blt(rd, rs, rvoff >> 1), ctx);
  273. return;
  274. case BPF_JSGE:
  275. emit(rv_bge(rd, rs, rvoff >> 1), ctx);
  276. return;
  277. case BPF_JSLE:
  278. emit(rv_bge(rs, rd, rvoff >> 1), ctx);
  279. }
  280. }
  281. static void emit_branch(u8 cond, u8 rd, u8 rs, int rvoff,
  282. struct rv_jit_context *ctx)
  283. {
  284. s64 upper, lower;
  285. if (is_13b_int(rvoff)) {
  286. emit_bcc(cond, rd, rs, rvoff, ctx);
  287. return;
  288. }
  289. /* Adjust for jal */
  290. rvoff -= 4;
  291. /* Transform, e.g.:
  292. * bne rd,rs,foo
  293. * to
  294. * beq rd,rs,<.L1>
  295. * (auipc foo)
  296. * jal(r) foo
  297. * .L1
  298. */
  299. cond = invert_bpf_cond(cond);
  300. if (is_21b_int(rvoff)) {
  301. emit_bcc(cond, rd, rs, 8, ctx);
  302. emit(rv_jal(RV_REG_ZERO, rvoff >> 1), ctx);
  303. return;
  304. }
  305. /* 32b No need for an additional rvoff adjustment, since we
  306. * get that from the auipc at PC', where PC = PC' + 4.
  307. */
  308. upper = (rvoff + (1 << 11)) >> 12;
  309. lower = rvoff & 0xfff;
  310. emit_bcc(cond, rd, rs, 12, ctx);
  311. emit(rv_auipc(RV_REG_T1, upper), ctx);
  312. emit(rv_jalr(RV_REG_ZERO, RV_REG_T1, lower), ctx);
  313. }
  314. static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
  315. {
  316. int tc_ninsn, off, start_insn = ctx->ninsns;
  317. u8 tcc = rv_tail_call_reg(ctx);
  318. /* a0: &ctx
  319. * a1: &array
  320. * a2: index
  321. *
  322. * if (index >= array->map.max_entries)
  323. * goto out;
  324. */
  325. tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] :
  326. ctx->offset[0];
  327. emit_zextw(RV_REG_A2, RV_REG_A2, ctx);
  328. off = offsetof(struct bpf_array, map.max_entries);
  329. if (is_12b_check(off, insn))
  330. return -1;
  331. emit(rv_lwu(RV_REG_T1, off, RV_REG_A1), ctx);
  332. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  333. emit_branch(BPF_JGE, RV_REG_A2, RV_REG_T1, off, ctx);
  334. /* if (--TCC < 0)
  335. * goto out;
  336. */
  337. emit_addi(RV_REG_TCC, tcc, -1, ctx);
  338. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  339. emit_branch(BPF_JSLT, RV_REG_TCC, RV_REG_ZERO, off, ctx);
  340. /* prog = array->ptrs[index];
  341. * if (!prog)
  342. * goto out;
  343. */
  344. emit_sh3add(RV_REG_T2, RV_REG_A2, RV_REG_A1, ctx);
  345. off = offsetof(struct bpf_array, ptrs);
  346. if (is_12b_check(off, insn))
  347. return -1;
  348. emit_ld(RV_REG_T2, off, RV_REG_T2, ctx);
  349. off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
  350. emit_branch(BPF_JEQ, RV_REG_T2, RV_REG_ZERO, off, ctx);
  351. /* goto *(prog->bpf_func + 4); */
  352. off = offsetof(struct bpf_prog, bpf_func);
  353. if (is_12b_check(off, insn))
  354. return -1;
  355. emit_ld(RV_REG_T3, off, RV_REG_T2, ctx);
  356. __build_epilogue(true, ctx);
  357. return 0;
  358. }
  359. static void init_regs(u8 *rd, u8 *rs, const struct bpf_insn *insn,
  360. struct rv_jit_context *ctx)
  361. {
  362. u8 code = insn->code;
  363. switch (code) {
  364. case BPF_JMP | BPF_JA:
  365. case BPF_JMP | BPF_CALL:
  366. case BPF_JMP | BPF_EXIT:
  367. case BPF_JMP | BPF_TAIL_CALL:
  368. break;
  369. default:
  370. *rd = bpf_to_rv_reg(insn->dst_reg, ctx);
  371. }
  372. if (code & (BPF_ALU | BPF_X) || code & (BPF_ALU64 | BPF_X) ||
  373. code & (BPF_JMP | BPF_X) || code & (BPF_JMP32 | BPF_X) ||
  374. code & BPF_LDX || code & BPF_STX)
  375. *rs = bpf_to_rv_reg(insn->src_reg, ctx);
  376. }
  377. static int emit_jump_and_link(u8 rd, s64 rvoff, bool fixed_addr,
  378. struct rv_jit_context *ctx)
  379. {
  380. s64 upper, lower;
  381. if (rvoff && fixed_addr && is_21b_int(rvoff)) {
  382. emit(rv_jal(rd, rvoff >> 1), ctx);
  383. return 0;
  384. } else if (in_auipc_jalr_range(rvoff)) {
  385. upper = (rvoff + (1 << 11)) >> 12;
  386. lower = rvoff & 0xfff;
  387. emit(rv_auipc(RV_REG_T1, upper), ctx);
  388. emit(rv_jalr(rd, RV_REG_T1, lower), ctx);
  389. return 0;
  390. }
  391. pr_err("bpf-jit: target offset 0x%llx is out of range\n", rvoff);
  392. return -ERANGE;
  393. }
  394. static bool is_signed_bpf_cond(u8 cond)
  395. {
  396. return cond == BPF_JSGT || cond == BPF_JSLT ||
  397. cond == BPF_JSGE || cond == BPF_JSLE;
  398. }
  399. static int emit_call(u64 addr, bool fixed_addr, struct rv_jit_context *ctx)
  400. {
  401. s64 off = 0;
  402. u64 ip;
  403. if (addr && ctx->insns && ctx->ro_insns) {
  404. /*
  405. * Use the ro_insns(RX) to calculate the offset as the BPF
  406. * program will finally run from this memory region.
  407. */
  408. ip = (u64)(long)(ctx->ro_insns + ctx->ninsns);
  409. off = addr - ip;
  410. }
  411. return emit_jump_and_link(RV_REG_RA, off, fixed_addr, ctx);
  412. }
  413. static inline void emit_kcfi(u32 hash, struct rv_jit_context *ctx)
  414. {
  415. if (IS_ENABLED(CONFIG_CFI_CLANG))
  416. emit(hash, ctx);
  417. }
  418. static void emit_atomic(u8 rd, u8 rs, s16 off, s32 imm, bool is64,
  419. struct rv_jit_context *ctx)
  420. {
  421. u8 r0;
  422. int jmp_offset;
  423. if (off) {
  424. if (is_12b_int(off)) {
  425. emit_addi(RV_REG_T1, rd, off, ctx);
  426. } else {
  427. emit_imm(RV_REG_T1, off, ctx);
  428. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  429. }
  430. rd = RV_REG_T1;
  431. }
  432. switch (imm) {
  433. /* lock *(u32/u64 *)(dst_reg + off16) <op>= src_reg */
  434. case BPF_ADD:
  435. emit(is64 ? rv_amoadd_d(RV_REG_ZERO, rs, rd, 0, 0) :
  436. rv_amoadd_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
  437. break;
  438. case BPF_AND:
  439. emit(is64 ? rv_amoand_d(RV_REG_ZERO, rs, rd, 0, 0) :
  440. rv_amoand_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
  441. break;
  442. case BPF_OR:
  443. emit(is64 ? rv_amoor_d(RV_REG_ZERO, rs, rd, 0, 0) :
  444. rv_amoor_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
  445. break;
  446. case BPF_XOR:
  447. emit(is64 ? rv_amoxor_d(RV_REG_ZERO, rs, rd, 0, 0) :
  448. rv_amoxor_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
  449. break;
  450. /* src_reg = atomic_fetch_<op>(dst_reg + off16, src_reg) */
  451. case BPF_ADD | BPF_FETCH:
  452. emit(is64 ? rv_amoadd_d(rs, rs, rd, 1, 1) :
  453. rv_amoadd_w(rs, rs, rd, 1, 1), ctx);
  454. if (!is64)
  455. emit_zextw(rs, rs, ctx);
  456. break;
  457. case BPF_AND | BPF_FETCH:
  458. emit(is64 ? rv_amoand_d(rs, rs, rd, 1, 1) :
  459. rv_amoand_w(rs, rs, rd, 1, 1), ctx);
  460. if (!is64)
  461. emit_zextw(rs, rs, ctx);
  462. break;
  463. case BPF_OR | BPF_FETCH:
  464. emit(is64 ? rv_amoor_d(rs, rs, rd, 1, 1) :
  465. rv_amoor_w(rs, rs, rd, 1, 1), ctx);
  466. if (!is64)
  467. emit_zextw(rs, rs, ctx);
  468. break;
  469. case BPF_XOR | BPF_FETCH:
  470. emit(is64 ? rv_amoxor_d(rs, rs, rd, 1, 1) :
  471. rv_amoxor_w(rs, rs, rd, 1, 1), ctx);
  472. if (!is64)
  473. emit_zextw(rs, rs, ctx);
  474. break;
  475. /* src_reg = atomic_xchg(dst_reg + off16, src_reg); */
  476. case BPF_XCHG:
  477. emit(is64 ? rv_amoswap_d(rs, rs, rd, 1, 1) :
  478. rv_amoswap_w(rs, rs, rd, 1, 1), ctx);
  479. if (!is64)
  480. emit_zextw(rs, rs, ctx);
  481. break;
  482. /* r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg); */
  483. case BPF_CMPXCHG:
  484. r0 = bpf_to_rv_reg(BPF_REG_0, ctx);
  485. if (is64)
  486. emit_mv(RV_REG_T2, r0, ctx);
  487. else
  488. emit_addiw(RV_REG_T2, r0, 0, ctx);
  489. emit(is64 ? rv_lr_d(r0, 0, rd, 0, 0) :
  490. rv_lr_w(r0, 0, rd, 0, 0), ctx);
  491. jmp_offset = ninsns_rvoff(8);
  492. emit(rv_bne(RV_REG_T2, r0, jmp_offset >> 1), ctx);
  493. emit(is64 ? rv_sc_d(RV_REG_T3, rs, rd, 0, 1) :
  494. rv_sc_w(RV_REG_T3, rs, rd, 0, 1), ctx);
  495. jmp_offset = ninsns_rvoff(-6);
  496. emit(rv_bne(RV_REG_T3, 0, jmp_offset >> 1), ctx);
  497. emit(rv_fence(0x3, 0x3), ctx);
  498. break;
  499. }
  500. }
  501. #define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
  502. #define BPF_FIXUP_REG_MASK GENMASK(31, 27)
  503. #define REG_DONT_CLEAR_MARKER 0 /* RV_REG_ZERO unused in pt_regmap */
  504. bool ex_handler_bpf(const struct exception_table_entry *ex,
  505. struct pt_regs *regs)
  506. {
  507. off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
  508. int regs_offset = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
  509. if (regs_offset != REG_DONT_CLEAR_MARKER)
  510. *(unsigned long *)((void *)regs + pt_regmap[regs_offset]) = 0;
  511. regs->epc = (unsigned long)&ex->fixup - offset;
  512. return true;
  513. }
  514. /* For accesses to BTF pointers, add an entry to the exception table */
  515. static int add_exception_handler(const struct bpf_insn *insn,
  516. struct rv_jit_context *ctx,
  517. int dst_reg, int insn_len)
  518. {
  519. struct exception_table_entry *ex;
  520. unsigned long pc;
  521. off_t ins_offset;
  522. off_t fixup_offset;
  523. if (!ctx->insns || !ctx->ro_insns || !ctx->prog->aux->extable ||
  524. (BPF_MODE(insn->code) != BPF_PROBE_MEM && BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
  525. BPF_MODE(insn->code) != BPF_PROBE_MEM32))
  526. return 0;
  527. if (WARN_ON_ONCE(ctx->nexentries >= ctx->prog->aux->num_exentries))
  528. return -EINVAL;
  529. if (WARN_ON_ONCE(insn_len > ctx->ninsns))
  530. return -EINVAL;
  531. if (WARN_ON_ONCE(!rvc_enabled() && insn_len == 1))
  532. return -EINVAL;
  533. ex = &ctx->prog->aux->extable[ctx->nexentries];
  534. pc = (unsigned long)&ctx->ro_insns[ctx->ninsns - insn_len];
  535. /*
  536. * This is the relative offset of the instruction that may fault from
  537. * the exception table itself. This will be written to the exception
  538. * table and if this instruction faults, the destination register will
  539. * be set to '0' and the execution will jump to the next instruction.
  540. */
  541. ins_offset = pc - (long)&ex->insn;
  542. if (WARN_ON_ONCE(ins_offset >= 0 || ins_offset < INT_MIN))
  543. return -ERANGE;
  544. /*
  545. * Since the extable follows the program, the fixup offset is always
  546. * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
  547. * to keep things simple, and put the destination register in the upper
  548. * bits. We don't need to worry about buildtime or runtime sort
  549. * modifying the upper bits because the table is already sorted, and
  550. * isn't part of the main exception table.
  551. *
  552. * The fixup_offset is set to the next instruction from the instruction
  553. * that may fault. The execution will jump to this after handling the
  554. * fault.
  555. */
  556. fixup_offset = (long)&ex->fixup - (pc + insn_len * sizeof(u16));
  557. if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, fixup_offset))
  558. return -ERANGE;
  559. /*
  560. * The offsets above have been calculated using the RO buffer but we
  561. * need to use the R/W buffer for writes.
  562. * switch ex to rw buffer for writing.
  563. */
  564. ex = (void *)ctx->insns + ((void *)ex - (void *)ctx->ro_insns);
  565. ex->insn = ins_offset;
  566. ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, fixup_offset) |
  567. FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
  568. ex->type = EX_TYPE_BPF;
  569. ctx->nexentries++;
  570. return 0;
  571. }
  572. static int gen_jump_or_nops(void *target, void *ip, u32 *insns, bool is_call)
  573. {
  574. s64 rvoff;
  575. struct rv_jit_context ctx;
  576. ctx.ninsns = 0;
  577. ctx.insns = (u16 *)insns;
  578. if (!target) {
  579. emit(rv_nop(), &ctx);
  580. emit(rv_nop(), &ctx);
  581. return 0;
  582. }
  583. rvoff = (s64)(target - ip);
  584. return emit_jump_and_link(is_call ? RV_REG_T0 : RV_REG_ZERO, rvoff, false, &ctx);
  585. }
  586. int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
  587. void *old_addr, void *new_addr)
  588. {
  589. u32 old_insns[RV_FENTRY_NINSNS], new_insns[RV_FENTRY_NINSNS];
  590. bool is_call = poke_type == BPF_MOD_CALL;
  591. int ret;
  592. if (!is_kernel_text((unsigned long)ip) &&
  593. !is_bpf_text_address((unsigned long)ip))
  594. return -ENOTSUPP;
  595. ret = gen_jump_or_nops(old_addr, ip, old_insns, is_call);
  596. if (ret)
  597. return ret;
  598. if (memcmp(ip, old_insns, RV_FENTRY_NBYTES))
  599. return -EFAULT;
  600. ret = gen_jump_or_nops(new_addr, ip, new_insns, is_call);
  601. if (ret)
  602. return ret;
  603. cpus_read_lock();
  604. mutex_lock(&text_mutex);
  605. if (memcmp(ip, new_insns, RV_FENTRY_NBYTES))
  606. ret = patch_text(ip, new_insns, RV_FENTRY_NBYTES);
  607. mutex_unlock(&text_mutex);
  608. cpus_read_unlock();
  609. return ret;
  610. }
  611. static void store_args(int nr_arg_slots, int args_off, struct rv_jit_context *ctx)
  612. {
  613. int i;
  614. for (i = 0; i < nr_arg_slots; i++) {
  615. if (i < RV_MAX_REG_ARGS) {
  616. emit_sd(RV_REG_FP, -args_off, RV_REG_A0 + i, ctx);
  617. } else {
  618. /* skip slots for T0 and FP of traced function */
  619. emit_ld(RV_REG_T1, 16 + (i - RV_MAX_REG_ARGS) * 8, RV_REG_FP, ctx);
  620. emit_sd(RV_REG_FP, -args_off, RV_REG_T1, ctx);
  621. }
  622. args_off -= 8;
  623. }
  624. }
  625. static void restore_args(int nr_reg_args, int args_off, struct rv_jit_context *ctx)
  626. {
  627. int i;
  628. for (i = 0; i < nr_reg_args; i++) {
  629. emit_ld(RV_REG_A0 + i, -args_off, RV_REG_FP, ctx);
  630. args_off -= 8;
  631. }
  632. }
  633. static void restore_stack_args(int nr_stack_args, int args_off, int stk_arg_off,
  634. struct rv_jit_context *ctx)
  635. {
  636. int i;
  637. for (i = 0; i < nr_stack_args; i++) {
  638. emit_ld(RV_REG_T1, -(args_off - RV_MAX_REG_ARGS * 8), RV_REG_FP, ctx);
  639. emit_sd(RV_REG_FP, -stk_arg_off, RV_REG_T1, ctx);
  640. args_off -= 8;
  641. stk_arg_off -= 8;
  642. }
  643. }
  644. static int invoke_bpf_prog(struct bpf_tramp_link *l, int args_off, int retval_off,
  645. int run_ctx_off, bool save_ret, struct rv_jit_context *ctx)
  646. {
  647. int ret, branch_off;
  648. struct bpf_prog *p = l->link.prog;
  649. int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
  650. if (l->cookie) {
  651. emit_imm(RV_REG_T1, l->cookie, ctx);
  652. emit_sd(RV_REG_FP, -run_ctx_off + cookie_off, RV_REG_T1, ctx);
  653. } else {
  654. emit_sd(RV_REG_FP, -run_ctx_off + cookie_off, RV_REG_ZERO, ctx);
  655. }
  656. /* arg1: prog */
  657. emit_imm(RV_REG_A0, (const s64)p, ctx);
  658. /* arg2: &run_ctx */
  659. emit_addi(RV_REG_A1, RV_REG_FP, -run_ctx_off, ctx);
  660. ret = emit_call((const u64)bpf_trampoline_enter(p), true, ctx);
  661. if (ret)
  662. return ret;
  663. /* store prog start time */
  664. emit_mv(RV_REG_S1, RV_REG_A0, ctx);
  665. /* if (__bpf_prog_enter(prog) == 0)
  666. * goto skip_exec_of_prog;
  667. */
  668. branch_off = ctx->ninsns;
  669. /* nop reserved for conditional jump */
  670. emit(rv_nop(), ctx);
  671. /* arg1: &args_off */
  672. emit_addi(RV_REG_A0, RV_REG_FP, -args_off, ctx);
  673. if (!p->jited)
  674. /* arg2: progs[i]->insnsi for interpreter */
  675. emit_imm(RV_REG_A1, (const s64)p->insnsi, ctx);
  676. ret = emit_call((const u64)p->bpf_func, true, ctx);
  677. if (ret)
  678. return ret;
  679. if (save_ret) {
  680. emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);
  681. emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);
  682. }
  683. /* update branch with beqz */
  684. if (ctx->insns) {
  685. int offset = ninsns_rvoff(ctx->ninsns - branch_off);
  686. u32 insn = rv_beq(RV_REG_A0, RV_REG_ZERO, offset >> 1);
  687. *(u32 *)(ctx->insns + branch_off) = insn;
  688. }
  689. /* arg1: prog */
  690. emit_imm(RV_REG_A0, (const s64)p, ctx);
  691. /* arg2: prog start time */
  692. emit_mv(RV_REG_A1, RV_REG_S1, ctx);
  693. /* arg3: &run_ctx */
  694. emit_addi(RV_REG_A2, RV_REG_FP, -run_ctx_off, ctx);
  695. ret = emit_call((const u64)bpf_trampoline_exit(p), true, ctx);
  696. return ret;
  697. }
  698. static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
  699. const struct btf_func_model *m,
  700. struct bpf_tramp_links *tlinks,
  701. void *func_addr, u32 flags,
  702. struct rv_jit_context *ctx)
  703. {
  704. int i, ret, offset;
  705. int *branches_off = NULL;
  706. int stack_size = 0, nr_arg_slots = 0;
  707. int retval_off, args_off, nregs_off, ip_off, run_ctx_off, sreg_off, stk_arg_off;
  708. struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
  709. struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
  710. struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
  711. bool is_struct_ops = flags & BPF_TRAMP_F_INDIRECT;
  712. void *orig_call = func_addr;
  713. bool save_ret;
  714. u32 insn;
  715. /* Two types of generated trampoline stack layout:
  716. *
  717. * 1. trampoline called from function entry
  718. * --------------------------------------
  719. * FP + 8 [ RA to parent func ] return address to parent
  720. * function
  721. * FP + 0 [ FP of parent func ] frame pointer of parent
  722. * function
  723. * FP - 8 [ T0 to traced func ] return address of traced
  724. * function
  725. * FP - 16 [ FP of traced func ] frame pointer of traced
  726. * function
  727. * --------------------------------------
  728. *
  729. * 2. trampoline called directly
  730. * --------------------------------------
  731. * FP - 8 [ RA to caller func ] return address to caller
  732. * function
  733. * FP - 16 [ FP of caller func ] frame pointer of caller
  734. * function
  735. * --------------------------------------
  736. *
  737. * FP - retval_off [ return value ] BPF_TRAMP_F_CALL_ORIG or
  738. * BPF_TRAMP_F_RET_FENTRY_RET
  739. * [ argN ]
  740. * [ ... ]
  741. * FP - args_off [ arg1 ]
  742. *
  743. * FP - nregs_off [ regs count ]
  744. *
  745. * FP - ip_off [ traced func ] BPF_TRAMP_F_IP_ARG
  746. *
  747. * FP - run_ctx_off [ bpf_tramp_run_ctx ]
  748. *
  749. * FP - sreg_off [ callee saved reg ]
  750. *
  751. * [ pads ] pads for 16 bytes alignment
  752. *
  753. * [ stack_argN ]
  754. * [ ... ]
  755. * FP - stk_arg_off [ stack_arg1 ] BPF_TRAMP_F_CALL_ORIG
  756. */
  757. if (flags & (BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SHARE_IPMODIFY))
  758. return -ENOTSUPP;
  759. if (m->nr_args > MAX_BPF_FUNC_ARGS)
  760. return -ENOTSUPP;
  761. for (i = 0; i < m->nr_args; i++)
  762. nr_arg_slots += round_up(m->arg_size[i], 8) / 8;
  763. /* room of trampoline frame to store return address and frame pointer */
  764. stack_size += 16;
  765. save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
  766. if (save_ret) {
  767. stack_size += 16; /* Save both A5 (BPF R0) and A0 */
  768. retval_off = stack_size;
  769. }
  770. stack_size += nr_arg_slots * 8;
  771. args_off = stack_size;
  772. stack_size += 8;
  773. nregs_off = stack_size;
  774. if (flags & BPF_TRAMP_F_IP_ARG) {
  775. stack_size += 8;
  776. ip_off = stack_size;
  777. }
  778. stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8);
  779. run_ctx_off = stack_size;
  780. stack_size += 8;
  781. sreg_off = stack_size;
  782. if ((flags & BPF_TRAMP_F_CALL_ORIG) && (nr_arg_slots - RV_MAX_REG_ARGS > 0))
  783. stack_size += (nr_arg_slots - RV_MAX_REG_ARGS) * 8;
  784. stack_size = round_up(stack_size, STACK_ALIGN);
  785. /* room for args on stack must be at the top of stack */
  786. stk_arg_off = stack_size;
  787. if (!is_struct_ops) {
  788. /* For the trampoline called from function entry,
  789. * the frame of traced function and the frame of
  790. * trampoline need to be considered.
  791. */
  792. emit_addi(RV_REG_SP, RV_REG_SP, -16, ctx);
  793. emit_sd(RV_REG_SP, 8, RV_REG_RA, ctx);
  794. emit_sd(RV_REG_SP, 0, RV_REG_FP, ctx);
  795. emit_addi(RV_REG_FP, RV_REG_SP, 16, ctx);
  796. emit_addi(RV_REG_SP, RV_REG_SP, -stack_size, ctx);
  797. emit_sd(RV_REG_SP, stack_size - 8, RV_REG_T0, ctx);
  798. emit_sd(RV_REG_SP, stack_size - 16, RV_REG_FP, ctx);
  799. emit_addi(RV_REG_FP, RV_REG_SP, stack_size, ctx);
  800. } else {
  801. /* emit kcfi hash */
  802. emit_kcfi(cfi_get_func_hash(func_addr), ctx);
  803. /* For the trampoline called directly, just handle
  804. * the frame of trampoline.
  805. */
  806. emit_addi(RV_REG_SP, RV_REG_SP, -stack_size, ctx);
  807. emit_sd(RV_REG_SP, stack_size - 8, RV_REG_RA, ctx);
  808. emit_sd(RV_REG_SP, stack_size - 16, RV_REG_FP, ctx);
  809. emit_addi(RV_REG_FP, RV_REG_SP, stack_size, ctx);
  810. }
  811. /* callee saved register S1 to pass start time */
  812. emit_sd(RV_REG_FP, -sreg_off, RV_REG_S1, ctx);
  813. /* store ip address of the traced function */
  814. if (flags & BPF_TRAMP_F_IP_ARG) {
  815. emit_imm(RV_REG_T1, (const s64)func_addr, ctx);
  816. emit_sd(RV_REG_FP, -ip_off, RV_REG_T1, ctx);
  817. }
  818. emit_li(RV_REG_T1, nr_arg_slots, ctx);
  819. emit_sd(RV_REG_FP, -nregs_off, RV_REG_T1, ctx);
  820. store_args(nr_arg_slots, args_off, ctx);
  821. /* skip to actual body of traced function */
  822. if (flags & BPF_TRAMP_F_SKIP_FRAME)
  823. orig_call += RV_FENTRY_NINSNS * 4;
  824. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  825. emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);
  826. ret = emit_call((const u64)__bpf_tramp_enter, true, ctx);
  827. if (ret)
  828. return ret;
  829. }
  830. for (i = 0; i < fentry->nr_links; i++) {
  831. ret = invoke_bpf_prog(fentry->links[i], args_off, retval_off, run_ctx_off,
  832. flags & BPF_TRAMP_F_RET_FENTRY_RET, ctx);
  833. if (ret)
  834. return ret;
  835. }
  836. if (fmod_ret->nr_links) {
  837. branches_off = kcalloc(fmod_ret->nr_links, sizeof(int), GFP_KERNEL);
  838. if (!branches_off)
  839. return -ENOMEM;
  840. /* cleanup to avoid garbage return value confusion */
  841. emit_sd(RV_REG_FP, -retval_off, RV_REG_ZERO, ctx);
  842. for (i = 0; i < fmod_ret->nr_links; i++) {
  843. ret = invoke_bpf_prog(fmod_ret->links[i], args_off, retval_off,
  844. run_ctx_off, true, ctx);
  845. if (ret)
  846. goto out;
  847. emit_ld(RV_REG_T1, -retval_off, RV_REG_FP, ctx);
  848. branches_off[i] = ctx->ninsns;
  849. /* nop reserved for conditional jump */
  850. emit(rv_nop(), ctx);
  851. }
  852. }
  853. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  854. restore_args(min_t(int, nr_arg_slots, RV_MAX_REG_ARGS), args_off, ctx);
  855. restore_stack_args(nr_arg_slots - RV_MAX_REG_ARGS, args_off, stk_arg_off, ctx);
  856. ret = emit_call((const u64)orig_call, true, ctx);
  857. if (ret)
  858. goto out;
  859. emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);
  860. emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);
  861. im->ip_after_call = ctx->ro_insns + ctx->ninsns;
  862. /* 2 nops reserved for auipc+jalr pair */
  863. emit(rv_nop(), ctx);
  864. emit(rv_nop(), ctx);
  865. }
  866. /* update branches saved in invoke_bpf_mod_ret with bnez */
  867. for (i = 0; ctx->insns && i < fmod_ret->nr_links; i++) {
  868. offset = ninsns_rvoff(ctx->ninsns - branches_off[i]);
  869. insn = rv_bne(RV_REG_T1, RV_REG_ZERO, offset >> 1);
  870. *(u32 *)(ctx->insns + branches_off[i]) = insn;
  871. }
  872. for (i = 0; i < fexit->nr_links; i++) {
  873. ret = invoke_bpf_prog(fexit->links[i], args_off, retval_off,
  874. run_ctx_off, false, ctx);
  875. if (ret)
  876. goto out;
  877. }
  878. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  879. im->ip_epilogue = ctx->ro_insns + ctx->ninsns;
  880. emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);
  881. ret = emit_call((const u64)__bpf_tramp_exit, true, ctx);
  882. if (ret)
  883. goto out;
  884. }
  885. if (flags & BPF_TRAMP_F_RESTORE_REGS)
  886. restore_args(min_t(int, nr_arg_slots, RV_MAX_REG_ARGS), args_off, ctx);
  887. if (save_ret) {
  888. emit_ld(RV_REG_A0, -retval_off, RV_REG_FP, ctx);
  889. emit_ld(regmap[BPF_REG_0], -(retval_off - 8), RV_REG_FP, ctx);
  890. }
  891. emit_ld(RV_REG_S1, -sreg_off, RV_REG_FP, ctx);
  892. if (!is_struct_ops) {
  893. /* trampoline called from function entry */
  894. emit_ld(RV_REG_T0, stack_size - 8, RV_REG_SP, ctx);
  895. emit_ld(RV_REG_FP, stack_size - 16, RV_REG_SP, ctx);
  896. emit_addi(RV_REG_SP, RV_REG_SP, stack_size, ctx);
  897. emit_ld(RV_REG_RA, 8, RV_REG_SP, ctx);
  898. emit_ld(RV_REG_FP, 0, RV_REG_SP, ctx);
  899. emit_addi(RV_REG_SP, RV_REG_SP, 16, ctx);
  900. if (flags & BPF_TRAMP_F_SKIP_FRAME)
  901. /* return to parent function */
  902. emit_jalr(RV_REG_ZERO, RV_REG_RA, 0, ctx);
  903. else
  904. /* return to traced function */
  905. emit_jalr(RV_REG_ZERO, RV_REG_T0, 0, ctx);
  906. } else {
  907. /* trampoline called directly */
  908. emit_ld(RV_REG_RA, stack_size - 8, RV_REG_SP, ctx);
  909. emit_ld(RV_REG_FP, stack_size - 16, RV_REG_SP, ctx);
  910. emit_addi(RV_REG_SP, RV_REG_SP, stack_size, ctx);
  911. emit_jalr(RV_REG_ZERO, RV_REG_RA, 0, ctx);
  912. }
  913. ret = ctx->ninsns;
  914. out:
  915. kfree(branches_off);
  916. return ret;
  917. }
  918. int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
  919. struct bpf_tramp_links *tlinks, void *func_addr)
  920. {
  921. struct bpf_tramp_image im;
  922. struct rv_jit_context ctx;
  923. int ret;
  924. ctx.ninsns = 0;
  925. ctx.insns = NULL;
  926. ctx.ro_insns = NULL;
  927. ret = __arch_prepare_bpf_trampoline(&im, m, tlinks, func_addr, flags, &ctx);
  928. return ret < 0 ? ret : ninsns_rvoff(ctx.ninsns);
  929. }
  930. void *arch_alloc_bpf_trampoline(unsigned int size)
  931. {
  932. return bpf_prog_pack_alloc(size, bpf_fill_ill_insns);
  933. }
  934. void arch_free_bpf_trampoline(void *image, unsigned int size)
  935. {
  936. bpf_prog_pack_free(image, size);
  937. }
  938. int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
  939. void *ro_image_end, const struct btf_func_model *m,
  940. u32 flags, struct bpf_tramp_links *tlinks,
  941. void *func_addr)
  942. {
  943. int ret;
  944. void *image, *res;
  945. struct rv_jit_context ctx;
  946. u32 size = ro_image_end - ro_image;
  947. image = kvmalloc(size, GFP_KERNEL);
  948. if (!image)
  949. return -ENOMEM;
  950. ctx.ninsns = 0;
  951. ctx.insns = image;
  952. ctx.ro_insns = ro_image;
  953. ret = __arch_prepare_bpf_trampoline(im, m, tlinks, func_addr, flags, &ctx);
  954. if (ret < 0)
  955. goto out;
  956. if (WARN_ON(size < ninsns_rvoff(ctx.ninsns))) {
  957. ret = -E2BIG;
  958. goto out;
  959. }
  960. res = bpf_arch_text_copy(ro_image, image, size);
  961. if (IS_ERR(res)) {
  962. ret = PTR_ERR(res);
  963. goto out;
  964. }
  965. bpf_flush_icache(ro_image, ro_image_end);
  966. out:
  967. kvfree(image);
  968. return ret < 0 ? ret : size;
  969. }
  970. int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
  971. bool extra_pass)
  972. {
  973. bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
  974. BPF_CLASS(insn->code) == BPF_JMP;
  975. int s, e, rvoff, ret, i = insn - ctx->prog->insnsi;
  976. struct bpf_prog_aux *aux = ctx->prog->aux;
  977. u8 rd = -1, rs = -1, code = insn->code;
  978. s16 off = insn->off;
  979. s32 imm = insn->imm;
  980. init_regs(&rd, &rs, insn, ctx);
  981. switch (code) {
  982. /* dst = src */
  983. case BPF_ALU | BPF_MOV | BPF_X:
  984. case BPF_ALU64 | BPF_MOV | BPF_X:
  985. if (insn_is_cast_user(insn)) {
  986. emit_mv(RV_REG_T1, rs, ctx);
  987. emit_zextw(RV_REG_T1, RV_REG_T1, ctx);
  988. emit_imm(rd, (ctx->user_vm_start >> 32) << 32, ctx);
  989. emit(rv_beq(RV_REG_T1, RV_REG_ZERO, 4), ctx);
  990. emit_or(RV_REG_T1, rd, RV_REG_T1, ctx);
  991. emit_mv(rd, RV_REG_T1, ctx);
  992. break;
  993. } else if (insn_is_mov_percpu_addr(insn)) {
  994. if (rd != rs)
  995. emit_mv(rd, rs, ctx);
  996. #ifdef CONFIG_SMP
  997. /* Load current CPU number in T1 */
  998. emit_ld(RV_REG_T1, offsetof(struct thread_info, cpu),
  999. RV_REG_TP, ctx);
  1000. /* Load address of __per_cpu_offset array in T2 */
  1001. emit_addr(RV_REG_T2, (u64)&__per_cpu_offset, extra_pass, ctx);
  1002. /* Get address of __per_cpu_offset[cpu] in T1 */
  1003. emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx);
  1004. /* Load __per_cpu_offset[cpu] in T1 */
  1005. emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx);
  1006. /* Add the offset to Rd */
  1007. emit_add(rd, rd, RV_REG_T1, ctx);
  1008. #endif
  1009. }
  1010. if (imm == 1) {
  1011. /* Special mov32 for zext */
  1012. emit_zextw(rd, rd, ctx);
  1013. break;
  1014. }
  1015. switch (insn->off) {
  1016. case 0:
  1017. emit_mv(rd, rs, ctx);
  1018. break;
  1019. case 8:
  1020. emit_sextb(rd, rs, ctx);
  1021. break;
  1022. case 16:
  1023. emit_sexth(rd, rs, ctx);
  1024. break;
  1025. case 32:
  1026. emit_sextw(rd, rs, ctx);
  1027. break;
  1028. }
  1029. if (!is64 && !aux->verifier_zext)
  1030. emit_zextw(rd, rd, ctx);
  1031. break;
  1032. /* dst = dst OP src */
  1033. case BPF_ALU | BPF_ADD | BPF_X:
  1034. case BPF_ALU64 | BPF_ADD | BPF_X:
  1035. emit_add(rd, rd, rs, ctx);
  1036. if (!is64 && !aux->verifier_zext)
  1037. emit_zextw(rd, rd, ctx);
  1038. break;
  1039. case BPF_ALU | BPF_SUB | BPF_X:
  1040. case BPF_ALU64 | BPF_SUB | BPF_X:
  1041. if (is64)
  1042. emit_sub(rd, rd, rs, ctx);
  1043. else
  1044. emit_subw(rd, rd, rs, ctx);
  1045. if (!is64 && !aux->verifier_zext)
  1046. emit_zextw(rd, rd, ctx);
  1047. break;
  1048. case BPF_ALU | BPF_AND | BPF_X:
  1049. case BPF_ALU64 | BPF_AND | BPF_X:
  1050. emit_and(rd, rd, rs, ctx);
  1051. if (!is64 && !aux->verifier_zext)
  1052. emit_zextw(rd, rd, ctx);
  1053. break;
  1054. case BPF_ALU | BPF_OR | BPF_X:
  1055. case BPF_ALU64 | BPF_OR | BPF_X:
  1056. emit_or(rd, rd, rs, ctx);
  1057. if (!is64 && !aux->verifier_zext)
  1058. emit_zextw(rd, rd, ctx);
  1059. break;
  1060. case BPF_ALU | BPF_XOR | BPF_X:
  1061. case BPF_ALU64 | BPF_XOR | BPF_X:
  1062. emit_xor(rd, rd, rs, ctx);
  1063. if (!is64 && !aux->verifier_zext)
  1064. emit_zextw(rd, rd, ctx);
  1065. break;
  1066. case BPF_ALU | BPF_MUL | BPF_X:
  1067. case BPF_ALU64 | BPF_MUL | BPF_X:
  1068. emit(is64 ? rv_mul(rd, rd, rs) : rv_mulw(rd, rd, rs), ctx);
  1069. if (!is64 && !aux->verifier_zext)
  1070. emit_zextw(rd, rd, ctx);
  1071. break;
  1072. case BPF_ALU | BPF_DIV | BPF_X:
  1073. case BPF_ALU64 | BPF_DIV | BPF_X:
  1074. if (off)
  1075. emit(is64 ? rv_div(rd, rd, rs) : rv_divw(rd, rd, rs), ctx);
  1076. else
  1077. emit(is64 ? rv_divu(rd, rd, rs) : rv_divuw(rd, rd, rs), ctx);
  1078. if (!is64 && !aux->verifier_zext)
  1079. emit_zextw(rd, rd, ctx);
  1080. break;
  1081. case BPF_ALU | BPF_MOD | BPF_X:
  1082. case BPF_ALU64 | BPF_MOD | BPF_X:
  1083. if (off)
  1084. emit(is64 ? rv_rem(rd, rd, rs) : rv_remw(rd, rd, rs), ctx);
  1085. else
  1086. emit(is64 ? rv_remu(rd, rd, rs) : rv_remuw(rd, rd, rs), ctx);
  1087. if (!is64 && !aux->verifier_zext)
  1088. emit_zextw(rd, rd, ctx);
  1089. break;
  1090. case BPF_ALU | BPF_LSH | BPF_X:
  1091. case BPF_ALU64 | BPF_LSH | BPF_X:
  1092. emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx);
  1093. if (!is64 && !aux->verifier_zext)
  1094. emit_zextw(rd, rd, ctx);
  1095. break;
  1096. case BPF_ALU | BPF_RSH | BPF_X:
  1097. case BPF_ALU64 | BPF_RSH | BPF_X:
  1098. emit(is64 ? rv_srl(rd, rd, rs) : rv_srlw(rd, rd, rs), ctx);
  1099. if (!is64 && !aux->verifier_zext)
  1100. emit_zextw(rd, rd, ctx);
  1101. break;
  1102. case BPF_ALU | BPF_ARSH | BPF_X:
  1103. case BPF_ALU64 | BPF_ARSH | BPF_X:
  1104. emit(is64 ? rv_sra(rd, rd, rs) : rv_sraw(rd, rd, rs), ctx);
  1105. if (!is64 && !aux->verifier_zext)
  1106. emit_zextw(rd, rd, ctx);
  1107. break;
  1108. /* dst = -dst */
  1109. case BPF_ALU | BPF_NEG:
  1110. case BPF_ALU64 | BPF_NEG:
  1111. emit_sub(rd, RV_REG_ZERO, rd, ctx);
  1112. if (!is64 && !aux->verifier_zext)
  1113. emit_zextw(rd, rd, ctx);
  1114. break;
  1115. /* dst = BSWAP##imm(dst) */
  1116. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1117. switch (imm) {
  1118. case 16:
  1119. emit_zexth(rd, rd, ctx);
  1120. break;
  1121. case 32:
  1122. if (!aux->verifier_zext)
  1123. emit_zextw(rd, rd, ctx);
  1124. break;
  1125. case 64:
  1126. /* Do nothing */
  1127. break;
  1128. }
  1129. break;
  1130. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1131. case BPF_ALU64 | BPF_END | BPF_FROM_LE:
  1132. emit_bswap(rd, imm, ctx);
  1133. break;
  1134. /* dst = imm */
  1135. case BPF_ALU | BPF_MOV | BPF_K:
  1136. case BPF_ALU64 | BPF_MOV | BPF_K:
  1137. emit_imm(rd, imm, ctx);
  1138. if (!is64 && !aux->verifier_zext)
  1139. emit_zextw(rd, rd, ctx);
  1140. break;
  1141. /* dst = dst OP imm */
  1142. case BPF_ALU | BPF_ADD | BPF_K:
  1143. case BPF_ALU64 | BPF_ADD | BPF_K:
  1144. if (is_12b_int(imm)) {
  1145. emit_addi(rd, rd, imm, ctx);
  1146. } else {
  1147. emit_imm(RV_REG_T1, imm, ctx);
  1148. emit_add(rd, rd, RV_REG_T1, ctx);
  1149. }
  1150. if (!is64 && !aux->verifier_zext)
  1151. emit_zextw(rd, rd, ctx);
  1152. break;
  1153. case BPF_ALU | BPF_SUB | BPF_K:
  1154. case BPF_ALU64 | BPF_SUB | BPF_K:
  1155. if (is_12b_int(-imm)) {
  1156. emit_addi(rd, rd, -imm, ctx);
  1157. } else {
  1158. emit_imm(RV_REG_T1, imm, ctx);
  1159. emit_sub(rd, rd, RV_REG_T1, ctx);
  1160. }
  1161. if (!is64 && !aux->verifier_zext)
  1162. emit_zextw(rd, rd, ctx);
  1163. break;
  1164. case BPF_ALU | BPF_AND | BPF_K:
  1165. case BPF_ALU64 | BPF_AND | BPF_K:
  1166. if (is_12b_int(imm)) {
  1167. emit_andi(rd, rd, imm, ctx);
  1168. } else {
  1169. emit_imm(RV_REG_T1, imm, ctx);
  1170. emit_and(rd, rd, RV_REG_T1, ctx);
  1171. }
  1172. if (!is64 && !aux->verifier_zext)
  1173. emit_zextw(rd, rd, ctx);
  1174. break;
  1175. case BPF_ALU | BPF_OR | BPF_K:
  1176. case BPF_ALU64 | BPF_OR | BPF_K:
  1177. if (is_12b_int(imm)) {
  1178. emit(rv_ori(rd, rd, imm), ctx);
  1179. } else {
  1180. emit_imm(RV_REG_T1, imm, ctx);
  1181. emit_or(rd, rd, RV_REG_T1, ctx);
  1182. }
  1183. if (!is64 && !aux->verifier_zext)
  1184. emit_zextw(rd, rd, ctx);
  1185. break;
  1186. case BPF_ALU | BPF_XOR | BPF_K:
  1187. case BPF_ALU64 | BPF_XOR | BPF_K:
  1188. if (is_12b_int(imm)) {
  1189. emit(rv_xori(rd, rd, imm), ctx);
  1190. } else {
  1191. emit_imm(RV_REG_T1, imm, ctx);
  1192. emit_xor(rd, rd, RV_REG_T1, ctx);
  1193. }
  1194. if (!is64 && !aux->verifier_zext)
  1195. emit_zextw(rd, rd, ctx);
  1196. break;
  1197. case BPF_ALU | BPF_MUL | BPF_K:
  1198. case BPF_ALU64 | BPF_MUL | BPF_K:
  1199. emit_imm(RV_REG_T1, imm, ctx);
  1200. emit(is64 ? rv_mul(rd, rd, RV_REG_T1) :
  1201. rv_mulw(rd, rd, RV_REG_T1), ctx);
  1202. if (!is64 && !aux->verifier_zext)
  1203. emit_zextw(rd, rd, ctx);
  1204. break;
  1205. case BPF_ALU | BPF_DIV | BPF_K:
  1206. case BPF_ALU64 | BPF_DIV | BPF_K:
  1207. emit_imm(RV_REG_T1, imm, ctx);
  1208. if (off)
  1209. emit(is64 ? rv_div(rd, rd, RV_REG_T1) :
  1210. rv_divw(rd, rd, RV_REG_T1), ctx);
  1211. else
  1212. emit(is64 ? rv_divu(rd, rd, RV_REG_T1) :
  1213. rv_divuw(rd, rd, RV_REG_T1), ctx);
  1214. if (!is64 && !aux->verifier_zext)
  1215. emit_zextw(rd, rd, ctx);
  1216. break;
  1217. case BPF_ALU | BPF_MOD | BPF_K:
  1218. case BPF_ALU64 | BPF_MOD | BPF_K:
  1219. emit_imm(RV_REG_T1, imm, ctx);
  1220. if (off)
  1221. emit(is64 ? rv_rem(rd, rd, RV_REG_T1) :
  1222. rv_remw(rd, rd, RV_REG_T1), ctx);
  1223. else
  1224. emit(is64 ? rv_remu(rd, rd, RV_REG_T1) :
  1225. rv_remuw(rd, rd, RV_REG_T1), ctx);
  1226. if (!is64 && !aux->verifier_zext)
  1227. emit_zextw(rd, rd, ctx);
  1228. break;
  1229. case BPF_ALU | BPF_LSH | BPF_K:
  1230. case BPF_ALU64 | BPF_LSH | BPF_K:
  1231. emit_slli(rd, rd, imm, ctx);
  1232. if (!is64 && !aux->verifier_zext)
  1233. emit_zextw(rd, rd, ctx);
  1234. break;
  1235. case BPF_ALU | BPF_RSH | BPF_K:
  1236. case BPF_ALU64 | BPF_RSH | BPF_K:
  1237. if (is64)
  1238. emit_srli(rd, rd, imm, ctx);
  1239. else
  1240. emit(rv_srliw(rd, rd, imm), ctx);
  1241. if (!is64 && !aux->verifier_zext)
  1242. emit_zextw(rd, rd, ctx);
  1243. break;
  1244. case BPF_ALU | BPF_ARSH | BPF_K:
  1245. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1246. if (is64)
  1247. emit_srai(rd, rd, imm, ctx);
  1248. else
  1249. emit(rv_sraiw(rd, rd, imm), ctx);
  1250. if (!is64 && !aux->verifier_zext)
  1251. emit_zextw(rd, rd, ctx);
  1252. break;
  1253. /* JUMP off */
  1254. case BPF_JMP | BPF_JA:
  1255. case BPF_JMP32 | BPF_JA:
  1256. if (BPF_CLASS(code) == BPF_JMP)
  1257. rvoff = rv_offset(i, off, ctx);
  1258. else
  1259. rvoff = rv_offset(i, imm, ctx);
  1260. ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
  1261. if (ret)
  1262. return ret;
  1263. break;
  1264. /* IF (dst COND src) JUMP off */
  1265. case BPF_JMP | BPF_JEQ | BPF_X:
  1266. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1267. case BPF_JMP | BPF_JGT | BPF_X:
  1268. case BPF_JMP32 | BPF_JGT | BPF_X:
  1269. case BPF_JMP | BPF_JLT | BPF_X:
  1270. case BPF_JMP32 | BPF_JLT | BPF_X:
  1271. case BPF_JMP | BPF_JGE | BPF_X:
  1272. case BPF_JMP32 | BPF_JGE | BPF_X:
  1273. case BPF_JMP | BPF_JLE | BPF_X:
  1274. case BPF_JMP32 | BPF_JLE | BPF_X:
  1275. case BPF_JMP | BPF_JNE | BPF_X:
  1276. case BPF_JMP32 | BPF_JNE | BPF_X:
  1277. case BPF_JMP | BPF_JSGT | BPF_X:
  1278. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1279. case BPF_JMP | BPF_JSLT | BPF_X:
  1280. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1281. case BPF_JMP | BPF_JSGE | BPF_X:
  1282. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1283. case BPF_JMP | BPF_JSLE | BPF_X:
  1284. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1285. case BPF_JMP | BPF_JSET | BPF_X:
  1286. case BPF_JMP32 | BPF_JSET | BPF_X:
  1287. rvoff = rv_offset(i, off, ctx);
  1288. if (!is64) {
  1289. s = ctx->ninsns;
  1290. if (is_signed_bpf_cond(BPF_OP(code))) {
  1291. emit_sextw_alt(&rs, RV_REG_T1, ctx);
  1292. emit_sextw_alt(&rd, RV_REG_T2, ctx);
  1293. } else {
  1294. emit_zextw_alt(&rs, RV_REG_T1, ctx);
  1295. emit_zextw_alt(&rd, RV_REG_T2, ctx);
  1296. }
  1297. e = ctx->ninsns;
  1298. /* Adjust for extra insns */
  1299. rvoff -= ninsns_rvoff(e - s);
  1300. }
  1301. if (BPF_OP(code) == BPF_JSET) {
  1302. /* Adjust for and */
  1303. rvoff -= 4;
  1304. emit_and(RV_REG_T1, rd, rs, ctx);
  1305. emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
  1306. } else {
  1307. emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
  1308. }
  1309. break;
  1310. /* IF (dst COND imm) JUMP off */
  1311. case BPF_JMP | BPF_JEQ | BPF_K:
  1312. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1313. case BPF_JMP | BPF_JGT | BPF_K:
  1314. case BPF_JMP32 | BPF_JGT | BPF_K:
  1315. case BPF_JMP | BPF_JLT | BPF_K:
  1316. case BPF_JMP32 | BPF_JLT | BPF_K:
  1317. case BPF_JMP | BPF_JGE | BPF_K:
  1318. case BPF_JMP32 | BPF_JGE | BPF_K:
  1319. case BPF_JMP | BPF_JLE | BPF_K:
  1320. case BPF_JMP32 | BPF_JLE | BPF_K:
  1321. case BPF_JMP | BPF_JNE | BPF_K:
  1322. case BPF_JMP32 | BPF_JNE | BPF_K:
  1323. case BPF_JMP | BPF_JSGT | BPF_K:
  1324. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1325. case BPF_JMP | BPF_JSLT | BPF_K:
  1326. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1327. case BPF_JMP | BPF_JSGE | BPF_K:
  1328. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1329. case BPF_JMP | BPF_JSLE | BPF_K:
  1330. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1331. rvoff = rv_offset(i, off, ctx);
  1332. s = ctx->ninsns;
  1333. if (imm)
  1334. emit_imm(RV_REG_T1, imm, ctx);
  1335. rs = imm ? RV_REG_T1 : RV_REG_ZERO;
  1336. if (!is64) {
  1337. if (is_signed_bpf_cond(BPF_OP(code))) {
  1338. emit_sextw_alt(&rd, RV_REG_T2, ctx);
  1339. /* rs has been sign extended */
  1340. } else {
  1341. emit_zextw_alt(&rd, RV_REG_T2, ctx);
  1342. if (imm)
  1343. emit_zextw(rs, rs, ctx);
  1344. }
  1345. }
  1346. e = ctx->ninsns;
  1347. /* Adjust for extra insns */
  1348. rvoff -= ninsns_rvoff(e - s);
  1349. emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
  1350. break;
  1351. case BPF_JMP | BPF_JSET | BPF_K:
  1352. case BPF_JMP32 | BPF_JSET | BPF_K:
  1353. rvoff = rv_offset(i, off, ctx);
  1354. s = ctx->ninsns;
  1355. if (is_12b_int(imm)) {
  1356. emit_andi(RV_REG_T1, rd, imm, ctx);
  1357. } else {
  1358. emit_imm(RV_REG_T1, imm, ctx);
  1359. emit_and(RV_REG_T1, rd, RV_REG_T1, ctx);
  1360. }
  1361. /* For jset32, we should clear the upper 32 bits of t1, but
  1362. * sign-extension is sufficient here and saves one instruction,
  1363. * as t1 is used only in comparison against zero.
  1364. */
  1365. if (!is64 && imm < 0)
  1366. emit_sextw(RV_REG_T1, RV_REG_T1, ctx);
  1367. e = ctx->ninsns;
  1368. rvoff -= ninsns_rvoff(e - s);
  1369. emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
  1370. break;
  1371. /* function call */
  1372. case BPF_JMP | BPF_CALL:
  1373. {
  1374. bool fixed_addr;
  1375. u64 addr;
  1376. /* Inline calls to bpf_get_smp_processor_id()
  1377. *
  1378. * RV_REG_TP holds the address of the current CPU's task_struct and thread_info is
  1379. * at offset 0 in task_struct.
  1380. * Load cpu from thread_info:
  1381. * Set R0 to ((struct thread_info *)(RV_REG_TP))->cpu
  1382. *
  1383. * This replicates the implementation of raw_smp_processor_id() on RISCV
  1384. */
  1385. if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
  1386. /* Load current CPU number in R0 */
  1387. emit_ld(bpf_to_rv_reg(BPF_REG_0, ctx), offsetof(struct thread_info, cpu),
  1388. RV_REG_TP, ctx);
  1389. break;
  1390. }
  1391. mark_call(ctx);
  1392. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
  1393. &addr, &fixed_addr);
  1394. if (ret < 0)
  1395. return ret;
  1396. if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
  1397. const struct btf_func_model *fm;
  1398. int idx;
  1399. fm = bpf_jit_find_kfunc_model(ctx->prog, insn);
  1400. if (!fm)
  1401. return -EINVAL;
  1402. for (idx = 0; idx < fm->nr_args; idx++) {
  1403. u8 reg = bpf_to_rv_reg(BPF_REG_1 + idx, ctx);
  1404. if (fm->arg_size[idx] == sizeof(int))
  1405. emit_sextw(reg, reg, ctx);
  1406. }
  1407. }
  1408. ret = emit_call(addr, fixed_addr, ctx);
  1409. if (ret)
  1410. return ret;
  1411. if (insn->src_reg != BPF_PSEUDO_CALL)
  1412. emit_mv(bpf_to_rv_reg(BPF_REG_0, ctx), RV_REG_A0, ctx);
  1413. break;
  1414. }
  1415. /* tail call */
  1416. case BPF_JMP | BPF_TAIL_CALL:
  1417. if (emit_bpf_tail_call(i, ctx))
  1418. return -1;
  1419. break;
  1420. /* function return */
  1421. case BPF_JMP | BPF_EXIT:
  1422. if (i == ctx->prog->len - 1)
  1423. break;
  1424. rvoff = epilogue_offset(ctx);
  1425. ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
  1426. if (ret)
  1427. return ret;
  1428. break;
  1429. /* dst = imm64 */
  1430. case BPF_LD | BPF_IMM | BPF_DW:
  1431. {
  1432. struct bpf_insn insn1 = insn[1];
  1433. u64 imm64;
  1434. imm64 = (u64)insn1.imm << 32 | (u32)imm;
  1435. if (bpf_pseudo_func(insn)) {
  1436. /* fixed-length insns for extra jit pass */
  1437. ret = emit_addr(rd, imm64, extra_pass, ctx);
  1438. if (ret)
  1439. return ret;
  1440. } else {
  1441. emit_imm(rd, imm64, ctx);
  1442. }
  1443. return 1;
  1444. }
  1445. /* LDX: dst = *(unsigned size *)(src + off) */
  1446. case BPF_LDX | BPF_MEM | BPF_B:
  1447. case BPF_LDX | BPF_MEM | BPF_H:
  1448. case BPF_LDX | BPF_MEM | BPF_W:
  1449. case BPF_LDX | BPF_MEM | BPF_DW:
  1450. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  1451. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  1452. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  1453. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  1454. /* LDSX: dst = *(signed size *)(src + off) */
  1455. case BPF_LDX | BPF_MEMSX | BPF_B:
  1456. case BPF_LDX | BPF_MEMSX | BPF_H:
  1457. case BPF_LDX | BPF_MEMSX | BPF_W:
  1458. case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
  1459. case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
  1460. case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
  1461. /* LDX | PROBE_MEM32: dst = *(unsigned size *)(src + RV_REG_ARENA + off) */
  1462. case BPF_LDX | BPF_PROBE_MEM32 | BPF_B:
  1463. case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
  1464. case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
  1465. case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
  1466. {
  1467. int insn_len, insns_start;
  1468. bool sign_ext;
  1469. sign_ext = BPF_MODE(insn->code) == BPF_MEMSX ||
  1470. BPF_MODE(insn->code) == BPF_PROBE_MEMSX;
  1471. if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
  1472. emit_add(RV_REG_T2, rs, RV_REG_ARENA, ctx);
  1473. rs = RV_REG_T2;
  1474. }
  1475. switch (BPF_SIZE(code)) {
  1476. case BPF_B:
  1477. if (is_12b_int(off)) {
  1478. insns_start = ctx->ninsns;
  1479. if (sign_ext)
  1480. emit(rv_lb(rd, off, rs), ctx);
  1481. else
  1482. emit(rv_lbu(rd, off, rs), ctx);
  1483. insn_len = ctx->ninsns - insns_start;
  1484. break;
  1485. }
  1486. emit_imm(RV_REG_T1, off, ctx);
  1487. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1488. insns_start = ctx->ninsns;
  1489. if (sign_ext)
  1490. emit(rv_lb(rd, 0, RV_REG_T1), ctx);
  1491. else
  1492. emit(rv_lbu(rd, 0, RV_REG_T1), ctx);
  1493. insn_len = ctx->ninsns - insns_start;
  1494. break;
  1495. case BPF_H:
  1496. if (is_12b_int(off)) {
  1497. insns_start = ctx->ninsns;
  1498. if (sign_ext)
  1499. emit(rv_lh(rd, off, rs), ctx);
  1500. else
  1501. emit(rv_lhu(rd, off, rs), ctx);
  1502. insn_len = ctx->ninsns - insns_start;
  1503. break;
  1504. }
  1505. emit_imm(RV_REG_T1, off, ctx);
  1506. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1507. insns_start = ctx->ninsns;
  1508. if (sign_ext)
  1509. emit(rv_lh(rd, 0, RV_REG_T1), ctx);
  1510. else
  1511. emit(rv_lhu(rd, 0, RV_REG_T1), ctx);
  1512. insn_len = ctx->ninsns - insns_start;
  1513. break;
  1514. case BPF_W:
  1515. if (is_12b_int(off)) {
  1516. insns_start = ctx->ninsns;
  1517. if (sign_ext)
  1518. emit(rv_lw(rd, off, rs), ctx);
  1519. else
  1520. emit(rv_lwu(rd, off, rs), ctx);
  1521. insn_len = ctx->ninsns - insns_start;
  1522. break;
  1523. }
  1524. emit_imm(RV_REG_T1, off, ctx);
  1525. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1526. insns_start = ctx->ninsns;
  1527. if (sign_ext)
  1528. emit(rv_lw(rd, 0, RV_REG_T1), ctx);
  1529. else
  1530. emit(rv_lwu(rd, 0, RV_REG_T1), ctx);
  1531. insn_len = ctx->ninsns - insns_start;
  1532. break;
  1533. case BPF_DW:
  1534. if (is_12b_int(off)) {
  1535. insns_start = ctx->ninsns;
  1536. emit_ld(rd, off, rs, ctx);
  1537. insn_len = ctx->ninsns - insns_start;
  1538. break;
  1539. }
  1540. emit_imm(RV_REG_T1, off, ctx);
  1541. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1542. insns_start = ctx->ninsns;
  1543. emit_ld(rd, 0, RV_REG_T1, ctx);
  1544. insn_len = ctx->ninsns - insns_start;
  1545. break;
  1546. }
  1547. ret = add_exception_handler(insn, ctx, rd, insn_len);
  1548. if (ret)
  1549. return ret;
  1550. if (BPF_SIZE(code) != BPF_DW && insn_is_zext(&insn[1]))
  1551. return 1;
  1552. break;
  1553. }
  1554. /* speculation barrier */
  1555. case BPF_ST | BPF_NOSPEC:
  1556. break;
  1557. /* ST: *(size *)(dst + off) = imm */
  1558. case BPF_ST | BPF_MEM | BPF_B:
  1559. emit_imm(RV_REG_T1, imm, ctx);
  1560. if (is_12b_int(off)) {
  1561. emit(rv_sb(rd, off, RV_REG_T1), ctx);
  1562. break;
  1563. }
  1564. emit_imm(RV_REG_T2, off, ctx);
  1565. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1566. emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx);
  1567. break;
  1568. case BPF_ST | BPF_MEM | BPF_H:
  1569. emit_imm(RV_REG_T1, imm, ctx);
  1570. if (is_12b_int(off)) {
  1571. emit(rv_sh(rd, off, RV_REG_T1), ctx);
  1572. break;
  1573. }
  1574. emit_imm(RV_REG_T2, off, ctx);
  1575. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1576. emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx);
  1577. break;
  1578. case BPF_ST | BPF_MEM | BPF_W:
  1579. emit_imm(RV_REG_T1, imm, ctx);
  1580. if (is_12b_int(off)) {
  1581. emit_sw(rd, off, RV_REG_T1, ctx);
  1582. break;
  1583. }
  1584. emit_imm(RV_REG_T2, off, ctx);
  1585. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1586. emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx);
  1587. break;
  1588. case BPF_ST | BPF_MEM | BPF_DW:
  1589. emit_imm(RV_REG_T1, imm, ctx);
  1590. if (is_12b_int(off)) {
  1591. emit_sd(rd, off, RV_REG_T1, ctx);
  1592. break;
  1593. }
  1594. emit_imm(RV_REG_T2, off, ctx);
  1595. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1596. emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx);
  1597. break;
  1598. case BPF_ST | BPF_PROBE_MEM32 | BPF_B:
  1599. case BPF_ST | BPF_PROBE_MEM32 | BPF_H:
  1600. case BPF_ST | BPF_PROBE_MEM32 | BPF_W:
  1601. case BPF_ST | BPF_PROBE_MEM32 | BPF_DW:
  1602. {
  1603. int insn_len, insns_start;
  1604. emit_add(RV_REG_T3, rd, RV_REG_ARENA, ctx);
  1605. rd = RV_REG_T3;
  1606. /* Load imm to a register then store it */
  1607. emit_imm(RV_REG_T1, imm, ctx);
  1608. switch (BPF_SIZE(code)) {
  1609. case BPF_B:
  1610. if (is_12b_int(off)) {
  1611. insns_start = ctx->ninsns;
  1612. emit(rv_sb(rd, off, RV_REG_T1), ctx);
  1613. insn_len = ctx->ninsns - insns_start;
  1614. break;
  1615. }
  1616. emit_imm(RV_REG_T2, off, ctx);
  1617. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1618. insns_start = ctx->ninsns;
  1619. emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx);
  1620. insn_len = ctx->ninsns - insns_start;
  1621. break;
  1622. case BPF_H:
  1623. if (is_12b_int(off)) {
  1624. insns_start = ctx->ninsns;
  1625. emit(rv_sh(rd, off, RV_REG_T1), ctx);
  1626. insn_len = ctx->ninsns - insns_start;
  1627. break;
  1628. }
  1629. emit_imm(RV_REG_T2, off, ctx);
  1630. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1631. insns_start = ctx->ninsns;
  1632. emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx);
  1633. insn_len = ctx->ninsns - insns_start;
  1634. break;
  1635. case BPF_W:
  1636. if (is_12b_int(off)) {
  1637. insns_start = ctx->ninsns;
  1638. emit_sw(rd, off, RV_REG_T1, ctx);
  1639. insn_len = ctx->ninsns - insns_start;
  1640. break;
  1641. }
  1642. emit_imm(RV_REG_T2, off, ctx);
  1643. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1644. insns_start = ctx->ninsns;
  1645. emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx);
  1646. insn_len = ctx->ninsns - insns_start;
  1647. break;
  1648. case BPF_DW:
  1649. if (is_12b_int(off)) {
  1650. insns_start = ctx->ninsns;
  1651. emit_sd(rd, off, RV_REG_T1, ctx);
  1652. insn_len = ctx->ninsns - insns_start;
  1653. break;
  1654. }
  1655. emit_imm(RV_REG_T2, off, ctx);
  1656. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1657. insns_start = ctx->ninsns;
  1658. emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx);
  1659. insn_len = ctx->ninsns - insns_start;
  1660. break;
  1661. }
  1662. ret = add_exception_handler(insn, ctx, REG_DONT_CLEAR_MARKER,
  1663. insn_len);
  1664. if (ret)
  1665. return ret;
  1666. break;
  1667. }
  1668. /* STX: *(size *)(dst + off) = src */
  1669. case BPF_STX | BPF_MEM | BPF_B:
  1670. if (is_12b_int(off)) {
  1671. emit(rv_sb(rd, off, rs), ctx);
  1672. break;
  1673. }
  1674. emit_imm(RV_REG_T1, off, ctx);
  1675. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1676. emit(rv_sb(RV_REG_T1, 0, rs), ctx);
  1677. break;
  1678. case BPF_STX | BPF_MEM | BPF_H:
  1679. if (is_12b_int(off)) {
  1680. emit(rv_sh(rd, off, rs), ctx);
  1681. break;
  1682. }
  1683. emit_imm(RV_REG_T1, off, ctx);
  1684. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1685. emit(rv_sh(RV_REG_T1, 0, rs), ctx);
  1686. break;
  1687. case BPF_STX | BPF_MEM | BPF_W:
  1688. if (is_12b_int(off)) {
  1689. emit_sw(rd, off, rs, ctx);
  1690. break;
  1691. }
  1692. emit_imm(RV_REG_T1, off, ctx);
  1693. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1694. emit_sw(RV_REG_T1, 0, rs, ctx);
  1695. break;
  1696. case BPF_STX | BPF_MEM | BPF_DW:
  1697. if (is_12b_int(off)) {
  1698. emit_sd(rd, off, rs, ctx);
  1699. break;
  1700. }
  1701. emit_imm(RV_REG_T1, off, ctx);
  1702. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1703. emit_sd(RV_REG_T1, 0, rs, ctx);
  1704. break;
  1705. case BPF_STX | BPF_ATOMIC | BPF_W:
  1706. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1707. emit_atomic(rd, rs, off, imm,
  1708. BPF_SIZE(code) == BPF_DW, ctx);
  1709. break;
  1710. case BPF_STX | BPF_PROBE_MEM32 | BPF_B:
  1711. case BPF_STX | BPF_PROBE_MEM32 | BPF_H:
  1712. case BPF_STX | BPF_PROBE_MEM32 | BPF_W:
  1713. case BPF_STX | BPF_PROBE_MEM32 | BPF_DW:
  1714. {
  1715. int insn_len, insns_start;
  1716. emit_add(RV_REG_T2, rd, RV_REG_ARENA, ctx);
  1717. rd = RV_REG_T2;
  1718. switch (BPF_SIZE(code)) {
  1719. case BPF_B:
  1720. if (is_12b_int(off)) {
  1721. insns_start = ctx->ninsns;
  1722. emit(rv_sb(rd, off, rs), ctx);
  1723. insn_len = ctx->ninsns - insns_start;
  1724. break;
  1725. }
  1726. emit_imm(RV_REG_T1, off, ctx);
  1727. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1728. insns_start = ctx->ninsns;
  1729. emit(rv_sb(RV_REG_T1, 0, rs), ctx);
  1730. insn_len = ctx->ninsns - insns_start;
  1731. break;
  1732. case BPF_H:
  1733. if (is_12b_int(off)) {
  1734. insns_start = ctx->ninsns;
  1735. emit(rv_sh(rd, off, rs), ctx);
  1736. insn_len = ctx->ninsns - insns_start;
  1737. break;
  1738. }
  1739. emit_imm(RV_REG_T1, off, ctx);
  1740. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1741. insns_start = ctx->ninsns;
  1742. emit(rv_sh(RV_REG_T1, 0, rs), ctx);
  1743. insn_len = ctx->ninsns - insns_start;
  1744. break;
  1745. case BPF_W:
  1746. if (is_12b_int(off)) {
  1747. insns_start = ctx->ninsns;
  1748. emit_sw(rd, off, rs, ctx);
  1749. insn_len = ctx->ninsns - insns_start;
  1750. break;
  1751. }
  1752. emit_imm(RV_REG_T1, off, ctx);
  1753. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1754. insns_start = ctx->ninsns;
  1755. emit_sw(RV_REG_T1, 0, rs, ctx);
  1756. insn_len = ctx->ninsns - insns_start;
  1757. break;
  1758. case BPF_DW:
  1759. if (is_12b_int(off)) {
  1760. insns_start = ctx->ninsns;
  1761. emit_sd(rd, off, rs, ctx);
  1762. insn_len = ctx->ninsns - insns_start;
  1763. break;
  1764. }
  1765. emit_imm(RV_REG_T1, off, ctx);
  1766. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1767. insns_start = ctx->ninsns;
  1768. emit_sd(RV_REG_T1, 0, rs, ctx);
  1769. insn_len = ctx->ninsns - insns_start;
  1770. break;
  1771. }
  1772. ret = add_exception_handler(insn, ctx, REG_DONT_CLEAR_MARKER,
  1773. insn_len);
  1774. if (ret)
  1775. return ret;
  1776. break;
  1777. }
  1778. default:
  1779. pr_err("bpf-jit: unknown opcode %02x\n", code);
  1780. return -EINVAL;
  1781. }
  1782. return 0;
  1783. }
  1784. void bpf_jit_build_prologue(struct rv_jit_context *ctx, bool is_subprog)
  1785. {
  1786. int i, stack_adjust = 0, store_offset, bpf_stack_adjust;
  1787. bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, STACK_ALIGN);
  1788. if (bpf_stack_adjust)
  1789. mark_fp(ctx);
  1790. if (seen_reg(RV_REG_RA, ctx))
  1791. stack_adjust += 8;
  1792. stack_adjust += 8; /* RV_REG_FP */
  1793. if (seen_reg(RV_REG_S1, ctx))
  1794. stack_adjust += 8;
  1795. if (seen_reg(RV_REG_S2, ctx))
  1796. stack_adjust += 8;
  1797. if (seen_reg(RV_REG_S3, ctx))
  1798. stack_adjust += 8;
  1799. if (seen_reg(RV_REG_S4, ctx))
  1800. stack_adjust += 8;
  1801. if (seen_reg(RV_REG_S5, ctx))
  1802. stack_adjust += 8;
  1803. if (seen_reg(RV_REG_S6, ctx))
  1804. stack_adjust += 8;
  1805. if (ctx->arena_vm_start)
  1806. stack_adjust += 8;
  1807. stack_adjust = round_up(stack_adjust, STACK_ALIGN);
  1808. stack_adjust += bpf_stack_adjust;
  1809. store_offset = stack_adjust - 8;
  1810. /* emit kcfi type preamble immediately before the first insn */
  1811. emit_kcfi(is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash, ctx);
  1812. /* nops reserved for auipc+jalr pair */
  1813. for (i = 0; i < RV_FENTRY_NINSNS; i++)
  1814. emit(rv_nop(), ctx);
  1815. /* First instruction is always setting the tail-call-counter
  1816. * (TCC) register. This instruction is skipped for tail calls.
  1817. * Force using a 4-byte (non-compressed) instruction.
  1818. */
  1819. emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
  1820. emit_addi(RV_REG_SP, RV_REG_SP, -stack_adjust, ctx);
  1821. if (seen_reg(RV_REG_RA, ctx)) {
  1822. emit_sd(RV_REG_SP, store_offset, RV_REG_RA, ctx);
  1823. store_offset -= 8;
  1824. }
  1825. emit_sd(RV_REG_SP, store_offset, RV_REG_FP, ctx);
  1826. store_offset -= 8;
  1827. if (seen_reg(RV_REG_S1, ctx)) {
  1828. emit_sd(RV_REG_SP, store_offset, RV_REG_S1, ctx);
  1829. store_offset -= 8;
  1830. }
  1831. if (seen_reg(RV_REG_S2, ctx)) {
  1832. emit_sd(RV_REG_SP, store_offset, RV_REG_S2, ctx);
  1833. store_offset -= 8;
  1834. }
  1835. if (seen_reg(RV_REG_S3, ctx)) {
  1836. emit_sd(RV_REG_SP, store_offset, RV_REG_S3, ctx);
  1837. store_offset -= 8;
  1838. }
  1839. if (seen_reg(RV_REG_S4, ctx)) {
  1840. emit_sd(RV_REG_SP, store_offset, RV_REG_S4, ctx);
  1841. store_offset -= 8;
  1842. }
  1843. if (seen_reg(RV_REG_S5, ctx)) {
  1844. emit_sd(RV_REG_SP, store_offset, RV_REG_S5, ctx);
  1845. store_offset -= 8;
  1846. }
  1847. if (seen_reg(RV_REG_S6, ctx)) {
  1848. emit_sd(RV_REG_SP, store_offset, RV_REG_S6, ctx);
  1849. store_offset -= 8;
  1850. }
  1851. if (ctx->arena_vm_start) {
  1852. emit_sd(RV_REG_SP, store_offset, RV_REG_ARENA, ctx);
  1853. store_offset -= 8;
  1854. }
  1855. emit_addi(RV_REG_FP, RV_REG_SP, stack_adjust, ctx);
  1856. if (bpf_stack_adjust)
  1857. emit_addi(RV_REG_S5, RV_REG_SP, bpf_stack_adjust, ctx);
  1858. /* Program contains calls and tail calls, so RV_REG_TCC need
  1859. * to be saved across calls.
  1860. */
  1861. if (seen_tail_call(ctx) && seen_call(ctx))
  1862. emit_mv(RV_REG_TCC_SAVED, RV_REG_TCC, ctx);
  1863. ctx->stack_size = stack_adjust;
  1864. if (ctx->arena_vm_start)
  1865. emit_imm(RV_REG_ARENA, ctx->arena_vm_start, ctx);
  1866. }
  1867. void bpf_jit_build_epilogue(struct rv_jit_context *ctx)
  1868. {
  1869. __build_epilogue(false, ctx);
  1870. }
  1871. bool bpf_jit_supports_kfunc_call(void)
  1872. {
  1873. return true;
  1874. }
  1875. bool bpf_jit_supports_ptr_xchg(void)
  1876. {
  1877. return true;
  1878. }
  1879. bool bpf_jit_supports_arena(void)
  1880. {
  1881. return true;
  1882. }
  1883. bool bpf_jit_supports_percpu_insn(void)
  1884. {
  1885. return true;
  1886. }
  1887. bool bpf_jit_inlines_helper_call(s32 imm)
  1888. {
  1889. switch (imm) {
  1890. case BPF_FUNC_get_smp_processor_id:
  1891. return true;
  1892. default:
  1893. return false;
  1894. }
  1895. }