bpf_jit_comp64.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  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. /*
  502. * Sign-extend the register if necessary
  503. */
  504. static int sign_extend(u8 rd, u8 rs, u8 sz, bool sign, struct rv_jit_context *ctx)
  505. {
  506. if (!sign && (sz == 1 || sz == 2)) {
  507. if (rd != rs)
  508. emit_mv(rd, rs, ctx);
  509. return 0;
  510. }
  511. switch (sz) {
  512. case 1:
  513. emit_sextb(rd, rs, ctx);
  514. break;
  515. case 2:
  516. emit_sexth(rd, rs, ctx);
  517. break;
  518. case 4:
  519. emit_sextw(rd, rs, ctx);
  520. break;
  521. case 8:
  522. if (rd != rs)
  523. emit_mv(rd, rs, ctx);
  524. break;
  525. default:
  526. pr_err("bpf-jit: invalid size %d for sign_extend\n", sz);
  527. return -EINVAL;
  528. }
  529. return 0;
  530. }
  531. #define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
  532. #define BPF_FIXUP_REG_MASK GENMASK(31, 27)
  533. #define REG_DONT_CLEAR_MARKER 0 /* RV_REG_ZERO unused in pt_regmap */
  534. bool ex_handler_bpf(const struct exception_table_entry *ex,
  535. struct pt_regs *regs)
  536. {
  537. off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
  538. int regs_offset = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
  539. if (regs_offset != REG_DONT_CLEAR_MARKER)
  540. *(unsigned long *)((void *)regs + pt_regmap[regs_offset]) = 0;
  541. regs->epc = (unsigned long)&ex->fixup - offset;
  542. return true;
  543. }
  544. /* For accesses to BTF pointers, add an entry to the exception table */
  545. static int add_exception_handler(const struct bpf_insn *insn,
  546. struct rv_jit_context *ctx,
  547. int dst_reg, int insn_len)
  548. {
  549. struct exception_table_entry *ex;
  550. unsigned long pc;
  551. off_t ins_offset;
  552. off_t fixup_offset;
  553. if (!ctx->insns || !ctx->ro_insns || !ctx->prog->aux->extable ||
  554. (BPF_MODE(insn->code) != BPF_PROBE_MEM && BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
  555. BPF_MODE(insn->code) != BPF_PROBE_MEM32))
  556. return 0;
  557. if (WARN_ON_ONCE(ctx->nexentries >= ctx->prog->aux->num_exentries))
  558. return -EINVAL;
  559. if (WARN_ON_ONCE(insn_len > ctx->ninsns))
  560. return -EINVAL;
  561. if (WARN_ON_ONCE(!rvc_enabled() && insn_len == 1))
  562. return -EINVAL;
  563. ex = &ctx->prog->aux->extable[ctx->nexentries];
  564. pc = (unsigned long)&ctx->ro_insns[ctx->ninsns - insn_len];
  565. /*
  566. * This is the relative offset of the instruction that may fault from
  567. * the exception table itself. This will be written to the exception
  568. * table and if this instruction faults, the destination register will
  569. * be set to '0' and the execution will jump to the next instruction.
  570. */
  571. ins_offset = pc - (long)&ex->insn;
  572. if (WARN_ON_ONCE(ins_offset >= 0 || ins_offset < INT_MIN))
  573. return -ERANGE;
  574. /*
  575. * Since the extable follows the program, the fixup offset is always
  576. * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
  577. * to keep things simple, and put the destination register in the upper
  578. * bits. We don't need to worry about buildtime or runtime sort
  579. * modifying the upper bits because the table is already sorted, and
  580. * isn't part of the main exception table.
  581. *
  582. * The fixup_offset is set to the next instruction from the instruction
  583. * that may fault. The execution will jump to this after handling the
  584. * fault.
  585. */
  586. fixup_offset = (long)&ex->fixup - (pc + insn_len * sizeof(u16));
  587. if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, fixup_offset))
  588. return -ERANGE;
  589. /*
  590. * The offsets above have been calculated using the RO buffer but we
  591. * need to use the R/W buffer for writes.
  592. * switch ex to rw buffer for writing.
  593. */
  594. ex = (void *)ctx->insns + ((void *)ex - (void *)ctx->ro_insns);
  595. ex->insn = ins_offset;
  596. ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, fixup_offset) |
  597. FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
  598. ex->type = EX_TYPE_BPF;
  599. ctx->nexentries++;
  600. return 0;
  601. }
  602. static int gen_jump_or_nops(void *target, void *ip, u32 *insns, bool is_call)
  603. {
  604. s64 rvoff;
  605. struct rv_jit_context ctx;
  606. ctx.ninsns = 0;
  607. ctx.insns = (u16 *)insns;
  608. if (!target) {
  609. emit(rv_nop(), &ctx);
  610. emit(rv_nop(), &ctx);
  611. return 0;
  612. }
  613. rvoff = (s64)(target - ip);
  614. return emit_jump_and_link(is_call ? RV_REG_T0 : RV_REG_ZERO, rvoff, false, &ctx);
  615. }
  616. int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
  617. void *old_addr, void *new_addr)
  618. {
  619. u32 old_insns[RV_FENTRY_NINSNS], new_insns[RV_FENTRY_NINSNS];
  620. bool is_call = poke_type == BPF_MOD_CALL;
  621. int ret;
  622. if (!is_kernel_text((unsigned long)ip) &&
  623. !is_bpf_text_address((unsigned long)ip))
  624. return -ENOTSUPP;
  625. ret = gen_jump_or_nops(old_addr, ip, old_insns, is_call);
  626. if (ret)
  627. return ret;
  628. if (memcmp(ip, old_insns, RV_FENTRY_NBYTES))
  629. return -EFAULT;
  630. ret = gen_jump_or_nops(new_addr, ip, new_insns, is_call);
  631. if (ret)
  632. return ret;
  633. cpus_read_lock();
  634. mutex_lock(&text_mutex);
  635. if (memcmp(ip, new_insns, RV_FENTRY_NBYTES))
  636. ret = patch_text(ip, new_insns, RV_FENTRY_NBYTES);
  637. mutex_unlock(&text_mutex);
  638. cpus_read_unlock();
  639. return ret;
  640. }
  641. static void store_args(int nr_arg_slots, int args_off, struct rv_jit_context *ctx)
  642. {
  643. int i;
  644. for (i = 0; i < nr_arg_slots; i++) {
  645. if (i < RV_MAX_REG_ARGS) {
  646. emit_sd(RV_REG_FP, -args_off, RV_REG_A0 + i, ctx);
  647. } else {
  648. /* skip slots for T0 and FP of traced function */
  649. emit_ld(RV_REG_T1, 16 + (i - RV_MAX_REG_ARGS) * 8, RV_REG_FP, ctx);
  650. emit_sd(RV_REG_FP, -args_off, RV_REG_T1, ctx);
  651. }
  652. args_off -= 8;
  653. }
  654. }
  655. static void restore_args(int nr_reg_args, int args_off, struct rv_jit_context *ctx)
  656. {
  657. int i;
  658. for (i = 0; i < nr_reg_args; i++) {
  659. emit_ld(RV_REG_A0 + i, -args_off, RV_REG_FP, ctx);
  660. args_off -= 8;
  661. }
  662. }
  663. static void restore_stack_args(int nr_stack_args, int args_off, int stk_arg_off,
  664. struct rv_jit_context *ctx)
  665. {
  666. int i;
  667. for (i = 0; i < nr_stack_args; i++) {
  668. emit_ld(RV_REG_T1, -(args_off - RV_MAX_REG_ARGS * 8), RV_REG_FP, ctx);
  669. emit_sd(RV_REG_FP, -stk_arg_off, RV_REG_T1, ctx);
  670. args_off -= 8;
  671. stk_arg_off -= 8;
  672. }
  673. }
  674. static int invoke_bpf_prog(struct bpf_tramp_link *l, int args_off, int retval_off,
  675. int run_ctx_off, bool save_ret, struct rv_jit_context *ctx)
  676. {
  677. int ret, branch_off;
  678. struct bpf_prog *p = l->link.prog;
  679. int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
  680. if (l->cookie) {
  681. emit_imm(RV_REG_T1, l->cookie, ctx);
  682. emit_sd(RV_REG_FP, -run_ctx_off + cookie_off, RV_REG_T1, ctx);
  683. } else {
  684. emit_sd(RV_REG_FP, -run_ctx_off + cookie_off, RV_REG_ZERO, ctx);
  685. }
  686. /* arg1: prog */
  687. emit_imm(RV_REG_A0, (const s64)p, ctx);
  688. /* arg2: &run_ctx */
  689. emit_addi(RV_REG_A1, RV_REG_FP, -run_ctx_off, ctx);
  690. ret = emit_call((const u64)bpf_trampoline_enter(p), true, ctx);
  691. if (ret)
  692. return ret;
  693. /* store prog start time */
  694. emit_mv(RV_REG_S1, RV_REG_A0, ctx);
  695. /* if (__bpf_prog_enter(prog) == 0)
  696. * goto skip_exec_of_prog;
  697. */
  698. branch_off = ctx->ninsns;
  699. /* nop reserved for conditional jump */
  700. emit(rv_nop(), ctx);
  701. /* arg1: &args_off */
  702. emit_addi(RV_REG_A0, RV_REG_FP, -args_off, ctx);
  703. if (!p->jited)
  704. /* arg2: progs[i]->insnsi for interpreter */
  705. emit_imm(RV_REG_A1, (const s64)p->insnsi, ctx);
  706. ret = emit_call((const u64)p->bpf_func, true, ctx);
  707. if (ret)
  708. return ret;
  709. if (save_ret) {
  710. emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);
  711. emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);
  712. }
  713. /* update branch with beqz */
  714. if (ctx->insns) {
  715. int offset = ninsns_rvoff(ctx->ninsns - branch_off);
  716. u32 insn = rv_beq(RV_REG_A0, RV_REG_ZERO, offset >> 1);
  717. *(u32 *)(ctx->insns + branch_off) = insn;
  718. }
  719. /* arg1: prog */
  720. emit_imm(RV_REG_A0, (const s64)p, ctx);
  721. /* arg2: prog start time */
  722. emit_mv(RV_REG_A1, RV_REG_S1, ctx);
  723. /* arg3: &run_ctx */
  724. emit_addi(RV_REG_A2, RV_REG_FP, -run_ctx_off, ctx);
  725. ret = emit_call((const u64)bpf_trampoline_exit(p), true, ctx);
  726. return ret;
  727. }
  728. static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
  729. const struct btf_func_model *m,
  730. struct bpf_tramp_links *tlinks,
  731. void *func_addr, u32 flags,
  732. struct rv_jit_context *ctx)
  733. {
  734. int i, ret, offset;
  735. int *branches_off = NULL;
  736. int stack_size = 0, nr_arg_slots = 0;
  737. int retval_off, args_off, nregs_off, ip_off, run_ctx_off, sreg_off, stk_arg_off;
  738. struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
  739. struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
  740. struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
  741. bool is_struct_ops = flags & BPF_TRAMP_F_INDIRECT;
  742. void *orig_call = func_addr;
  743. bool save_ret;
  744. u32 insn;
  745. /* Two types of generated trampoline stack layout:
  746. *
  747. * 1. trampoline called from function entry
  748. * --------------------------------------
  749. * FP + 8 [ RA to parent func ] return address to parent
  750. * function
  751. * FP + 0 [ FP of parent func ] frame pointer of parent
  752. * function
  753. * FP - 8 [ T0 to traced func ] return address of traced
  754. * function
  755. * FP - 16 [ FP of traced func ] frame pointer of traced
  756. * function
  757. * --------------------------------------
  758. *
  759. * 2. trampoline called directly
  760. * --------------------------------------
  761. * FP - 8 [ RA to caller func ] return address to caller
  762. * function
  763. * FP - 16 [ FP of caller func ] frame pointer of caller
  764. * function
  765. * --------------------------------------
  766. *
  767. * FP - retval_off [ return value ] BPF_TRAMP_F_CALL_ORIG or
  768. * BPF_TRAMP_F_RET_FENTRY_RET
  769. * [ argN ]
  770. * [ ... ]
  771. * FP - args_off [ arg1 ]
  772. *
  773. * FP - nregs_off [ regs count ]
  774. *
  775. * FP - ip_off [ traced func ] BPF_TRAMP_F_IP_ARG
  776. *
  777. * FP - run_ctx_off [ bpf_tramp_run_ctx ]
  778. *
  779. * FP - sreg_off [ callee saved reg ]
  780. *
  781. * [ pads ] pads for 16 bytes alignment
  782. *
  783. * [ stack_argN ]
  784. * [ ... ]
  785. * FP - stk_arg_off [ stack_arg1 ] BPF_TRAMP_F_CALL_ORIG
  786. */
  787. if (flags & (BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SHARE_IPMODIFY))
  788. return -ENOTSUPP;
  789. if (m->nr_args > MAX_BPF_FUNC_ARGS)
  790. return -ENOTSUPP;
  791. for (i = 0; i < m->nr_args; i++)
  792. nr_arg_slots += round_up(m->arg_size[i], 8) / 8;
  793. /* room of trampoline frame to store return address and frame pointer */
  794. stack_size += 16;
  795. save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
  796. if (save_ret) {
  797. stack_size += 16; /* Save both A5 (BPF R0) and A0 */
  798. retval_off = stack_size;
  799. }
  800. stack_size += nr_arg_slots * 8;
  801. args_off = stack_size;
  802. stack_size += 8;
  803. nregs_off = stack_size;
  804. if (flags & BPF_TRAMP_F_IP_ARG) {
  805. stack_size += 8;
  806. ip_off = stack_size;
  807. }
  808. stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8);
  809. run_ctx_off = stack_size;
  810. stack_size += 8;
  811. sreg_off = stack_size;
  812. if ((flags & BPF_TRAMP_F_CALL_ORIG) && (nr_arg_slots - RV_MAX_REG_ARGS > 0))
  813. stack_size += (nr_arg_slots - RV_MAX_REG_ARGS) * 8;
  814. stack_size = round_up(stack_size, STACK_ALIGN);
  815. /* room for args on stack must be at the top of stack */
  816. stk_arg_off = stack_size;
  817. if (!is_struct_ops) {
  818. /* For the trampoline called from function entry,
  819. * the frame of traced function and the frame of
  820. * trampoline need to be considered.
  821. */
  822. emit_addi(RV_REG_SP, RV_REG_SP, -16, ctx);
  823. emit_sd(RV_REG_SP, 8, RV_REG_RA, ctx);
  824. emit_sd(RV_REG_SP, 0, RV_REG_FP, ctx);
  825. emit_addi(RV_REG_FP, RV_REG_SP, 16, ctx);
  826. emit_addi(RV_REG_SP, RV_REG_SP, -stack_size, ctx);
  827. emit_sd(RV_REG_SP, stack_size - 8, RV_REG_T0, ctx);
  828. emit_sd(RV_REG_SP, stack_size - 16, RV_REG_FP, ctx);
  829. emit_addi(RV_REG_FP, RV_REG_SP, stack_size, ctx);
  830. } else {
  831. /* emit kcfi hash */
  832. emit_kcfi(cfi_get_func_hash(func_addr), ctx);
  833. /* For the trampoline called directly, just handle
  834. * the frame of trampoline.
  835. */
  836. emit_addi(RV_REG_SP, RV_REG_SP, -stack_size, ctx);
  837. emit_sd(RV_REG_SP, stack_size - 8, RV_REG_RA, ctx);
  838. emit_sd(RV_REG_SP, stack_size - 16, RV_REG_FP, ctx);
  839. emit_addi(RV_REG_FP, RV_REG_SP, stack_size, ctx);
  840. }
  841. /* callee saved register S1 to pass start time */
  842. emit_sd(RV_REG_FP, -sreg_off, RV_REG_S1, ctx);
  843. /* store ip address of the traced function */
  844. if (flags & BPF_TRAMP_F_IP_ARG) {
  845. emit_imm(RV_REG_T1, (const s64)func_addr, ctx);
  846. emit_sd(RV_REG_FP, -ip_off, RV_REG_T1, ctx);
  847. }
  848. emit_li(RV_REG_T1, nr_arg_slots, ctx);
  849. emit_sd(RV_REG_FP, -nregs_off, RV_REG_T1, ctx);
  850. store_args(nr_arg_slots, args_off, ctx);
  851. /* skip to actual body of traced function */
  852. if (flags & BPF_TRAMP_F_SKIP_FRAME)
  853. orig_call += RV_FENTRY_NINSNS * 4;
  854. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  855. emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);
  856. ret = emit_call((const u64)__bpf_tramp_enter, true, ctx);
  857. if (ret)
  858. return ret;
  859. }
  860. for (i = 0; i < fentry->nr_links; i++) {
  861. ret = invoke_bpf_prog(fentry->links[i], args_off, retval_off, run_ctx_off,
  862. flags & BPF_TRAMP_F_RET_FENTRY_RET, ctx);
  863. if (ret)
  864. return ret;
  865. }
  866. if (fmod_ret->nr_links) {
  867. branches_off = kcalloc(fmod_ret->nr_links, sizeof(int), GFP_KERNEL);
  868. if (!branches_off)
  869. return -ENOMEM;
  870. /* cleanup to avoid garbage return value confusion */
  871. emit_sd(RV_REG_FP, -retval_off, RV_REG_ZERO, ctx);
  872. for (i = 0; i < fmod_ret->nr_links; i++) {
  873. ret = invoke_bpf_prog(fmod_ret->links[i], args_off, retval_off,
  874. run_ctx_off, true, ctx);
  875. if (ret)
  876. goto out;
  877. emit_ld(RV_REG_T1, -retval_off, RV_REG_FP, ctx);
  878. branches_off[i] = ctx->ninsns;
  879. /* nop reserved for conditional jump */
  880. emit(rv_nop(), ctx);
  881. }
  882. }
  883. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  884. restore_args(min_t(int, nr_arg_slots, RV_MAX_REG_ARGS), args_off, ctx);
  885. restore_stack_args(nr_arg_slots - RV_MAX_REG_ARGS, args_off, stk_arg_off, ctx);
  886. ret = emit_call((const u64)orig_call, true, ctx);
  887. if (ret)
  888. goto out;
  889. emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);
  890. emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);
  891. im->ip_after_call = ctx->ro_insns + ctx->ninsns;
  892. /* 2 nops reserved for auipc+jalr pair */
  893. emit(rv_nop(), ctx);
  894. emit(rv_nop(), ctx);
  895. }
  896. /* update branches saved in invoke_bpf_mod_ret with bnez */
  897. for (i = 0; ctx->insns && i < fmod_ret->nr_links; i++) {
  898. offset = ninsns_rvoff(ctx->ninsns - branches_off[i]);
  899. insn = rv_bne(RV_REG_T1, RV_REG_ZERO, offset >> 1);
  900. *(u32 *)(ctx->insns + branches_off[i]) = insn;
  901. }
  902. for (i = 0; i < fexit->nr_links; i++) {
  903. ret = invoke_bpf_prog(fexit->links[i], args_off, retval_off,
  904. run_ctx_off, false, ctx);
  905. if (ret)
  906. goto out;
  907. }
  908. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  909. im->ip_epilogue = ctx->ro_insns + ctx->ninsns;
  910. emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);
  911. ret = emit_call((const u64)__bpf_tramp_exit, true, ctx);
  912. if (ret)
  913. goto out;
  914. }
  915. if (flags & BPF_TRAMP_F_RESTORE_REGS)
  916. restore_args(min_t(int, nr_arg_slots, RV_MAX_REG_ARGS), args_off, ctx);
  917. if (save_ret) {
  918. emit_ld(regmap[BPF_REG_0], -(retval_off - 8), RV_REG_FP, ctx);
  919. if (is_struct_ops) {
  920. ret = sign_extend(RV_REG_A0, regmap[BPF_REG_0], m->ret_size,
  921. m->ret_flags & BTF_FMODEL_SIGNED_ARG, ctx);
  922. if (ret)
  923. goto out;
  924. } else {
  925. emit_ld(RV_REG_A0, -retval_off, RV_REG_FP, ctx);
  926. }
  927. }
  928. emit_ld(RV_REG_S1, -sreg_off, RV_REG_FP, ctx);
  929. if (!is_struct_ops) {
  930. /* trampoline called from function entry */
  931. emit_ld(RV_REG_T0, stack_size - 8, RV_REG_SP, ctx);
  932. emit_ld(RV_REG_FP, stack_size - 16, RV_REG_SP, ctx);
  933. emit_addi(RV_REG_SP, RV_REG_SP, stack_size, ctx);
  934. emit_ld(RV_REG_RA, 8, RV_REG_SP, ctx);
  935. emit_ld(RV_REG_FP, 0, RV_REG_SP, ctx);
  936. emit_addi(RV_REG_SP, RV_REG_SP, 16, ctx);
  937. if (flags & BPF_TRAMP_F_SKIP_FRAME)
  938. /* return to parent function */
  939. emit_jalr(RV_REG_ZERO, RV_REG_RA, 0, ctx);
  940. else
  941. /* return to traced function */
  942. emit_jalr(RV_REG_ZERO, RV_REG_T0, 0, ctx);
  943. } else {
  944. /* trampoline called directly */
  945. emit_ld(RV_REG_RA, stack_size - 8, RV_REG_SP, ctx);
  946. emit_ld(RV_REG_FP, stack_size - 16, RV_REG_SP, ctx);
  947. emit_addi(RV_REG_SP, RV_REG_SP, stack_size, ctx);
  948. emit_jalr(RV_REG_ZERO, RV_REG_RA, 0, ctx);
  949. }
  950. ret = ctx->ninsns;
  951. out:
  952. kfree(branches_off);
  953. return ret;
  954. }
  955. int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
  956. struct bpf_tramp_links *tlinks, void *func_addr)
  957. {
  958. struct bpf_tramp_image im;
  959. struct rv_jit_context ctx;
  960. int ret;
  961. ctx.ninsns = 0;
  962. ctx.insns = NULL;
  963. ctx.ro_insns = NULL;
  964. ret = __arch_prepare_bpf_trampoline(&im, m, tlinks, func_addr, flags, &ctx);
  965. return ret < 0 ? ret : ninsns_rvoff(ctx.ninsns);
  966. }
  967. void *arch_alloc_bpf_trampoline(unsigned int size)
  968. {
  969. return bpf_prog_pack_alloc(size, bpf_fill_ill_insns);
  970. }
  971. void arch_free_bpf_trampoline(void *image, unsigned int size)
  972. {
  973. bpf_prog_pack_free(image, size);
  974. }
  975. int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
  976. void *ro_image_end, const struct btf_func_model *m,
  977. u32 flags, struct bpf_tramp_links *tlinks,
  978. void *func_addr)
  979. {
  980. int ret;
  981. void *image, *res;
  982. struct rv_jit_context ctx;
  983. u32 size = ro_image_end - ro_image;
  984. image = kvmalloc(size, GFP_KERNEL);
  985. if (!image)
  986. return -ENOMEM;
  987. ctx.ninsns = 0;
  988. ctx.insns = image;
  989. ctx.ro_insns = ro_image;
  990. ret = __arch_prepare_bpf_trampoline(im, m, tlinks, func_addr, flags, &ctx);
  991. if (ret < 0)
  992. goto out;
  993. if (WARN_ON(size < ninsns_rvoff(ctx.ninsns))) {
  994. ret = -E2BIG;
  995. goto out;
  996. }
  997. res = bpf_arch_text_copy(ro_image, image, size);
  998. if (IS_ERR(res)) {
  999. ret = PTR_ERR(res);
  1000. goto out;
  1001. }
  1002. bpf_flush_icache(ro_image, ro_image_end);
  1003. out:
  1004. kvfree(image);
  1005. return ret < 0 ? ret : size;
  1006. }
  1007. int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
  1008. bool extra_pass)
  1009. {
  1010. bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
  1011. BPF_CLASS(insn->code) == BPF_JMP;
  1012. int s, e, rvoff, ret, i = insn - ctx->prog->insnsi;
  1013. struct bpf_prog_aux *aux = ctx->prog->aux;
  1014. u8 rd = -1, rs = -1, code = insn->code;
  1015. s16 off = insn->off;
  1016. s32 imm = insn->imm;
  1017. init_regs(&rd, &rs, insn, ctx);
  1018. switch (code) {
  1019. /* dst = src */
  1020. case BPF_ALU | BPF_MOV | BPF_X:
  1021. case BPF_ALU64 | BPF_MOV | BPF_X:
  1022. if (insn_is_cast_user(insn)) {
  1023. emit_mv(RV_REG_T1, rs, ctx);
  1024. emit_zextw(RV_REG_T1, RV_REG_T1, ctx);
  1025. emit_imm(rd, (ctx->user_vm_start >> 32) << 32, ctx);
  1026. emit(rv_beq(RV_REG_T1, RV_REG_ZERO, 4), ctx);
  1027. emit_or(RV_REG_T1, rd, RV_REG_T1, ctx);
  1028. emit_mv(rd, RV_REG_T1, ctx);
  1029. break;
  1030. } else if (insn_is_mov_percpu_addr(insn)) {
  1031. if (rd != rs)
  1032. emit_mv(rd, rs, ctx);
  1033. #ifdef CONFIG_SMP
  1034. /* Load current CPU number in T1 */
  1035. emit_lw(RV_REG_T1, offsetof(struct thread_info, cpu),
  1036. RV_REG_TP, ctx);
  1037. /* Load address of __per_cpu_offset array in T2 */
  1038. emit_addr(RV_REG_T2, (u64)&__per_cpu_offset, extra_pass, ctx);
  1039. /* Get address of __per_cpu_offset[cpu] in T1 */
  1040. emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx);
  1041. /* Load __per_cpu_offset[cpu] in T1 */
  1042. emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx);
  1043. /* Add the offset to Rd */
  1044. emit_add(rd, rd, RV_REG_T1, ctx);
  1045. #endif
  1046. }
  1047. if (imm == 1) {
  1048. /* Special mov32 for zext */
  1049. emit_zextw(rd, rd, ctx);
  1050. break;
  1051. }
  1052. switch (insn->off) {
  1053. case 0:
  1054. emit_mv(rd, rs, ctx);
  1055. break;
  1056. case 8:
  1057. emit_sextb(rd, rs, ctx);
  1058. break;
  1059. case 16:
  1060. emit_sexth(rd, rs, ctx);
  1061. break;
  1062. case 32:
  1063. emit_sextw(rd, rs, ctx);
  1064. break;
  1065. }
  1066. if (!is64 && !aux->verifier_zext)
  1067. emit_zextw(rd, rd, ctx);
  1068. break;
  1069. /* dst = dst OP src */
  1070. case BPF_ALU | BPF_ADD | BPF_X:
  1071. case BPF_ALU64 | BPF_ADD | BPF_X:
  1072. emit_add(rd, rd, rs, ctx);
  1073. if (!is64 && !aux->verifier_zext)
  1074. emit_zextw(rd, rd, ctx);
  1075. break;
  1076. case BPF_ALU | BPF_SUB | BPF_X:
  1077. case BPF_ALU64 | BPF_SUB | BPF_X:
  1078. if (is64)
  1079. emit_sub(rd, rd, rs, ctx);
  1080. else
  1081. emit_subw(rd, rd, rs, ctx);
  1082. if (!is64 && !aux->verifier_zext)
  1083. emit_zextw(rd, rd, ctx);
  1084. break;
  1085. case BPF_ALU | BPF_AND | BPF_X:
  1086. case BPF_ALU64 | BPF_AND | BPF_X:
  1087. emit_and(rd, rd, rs, ctx);
  1088. if (!is64 && !aux->verifier_zext)
  1089. emit_zextw(rd, rd, ctx);
  1090. break;
  1091. case BPF_ALU | BPF_OR | BPF_X:
  1092. case BPF_ALU64 | BPF_OR | BPF_X:
  1093. emit_or(rd, rd, rs, ctx);
  1094. if (!is64 && !aux->verifier_zext)
  1095. emit_zextw(rd, rd, ctx);
  1096. break;
  1097. case BPF_ALU | BPF_XOR | BPF_X:
  1098. case BPF_ALU64 | BPF_XOR | BPF_X:
  1099. emit_xor(rd, rd, rs, ctx);
  1100. if (!is64 && !aux->verifier_zext)
  1101. emit_zextw(rd, rd, ctx);
  1102. break;
  1103. case BPF_ALU | BPF_MUL | BPF_X:
  1104. case BPF_ALU64 | BPF_MUL | BPF_X:
  1105. emit(is64 ? rv_mul(rd, rd, rs) : rv_mulw(rd, rd, rs), ctx);
  1106. if (!is64 && !aux->verifier_zext)
  1107. emit_zextw(rd, rd, ctx);
  1108. break;
  1109. case BPF_ALU | BPF_DIV | BPF_X:
  1110. case BPF_ALU64 | BPF_DIV | BPF_X:
  1111. if (off)
  1112. emit(is64 ? rv_div(rd, rd, rs) : rv_divw(rd, rd, rs), ctx);
  1113. else
  1114. emit(is64 ? rv_divu(rd, rd, rs) : rv_divuw(rd, rd, rs), ctx);
  1115. if (!is64 && !aux->verifier_zext)
  1116. emit_zextw(rd, rd, ctx);
  1117. break;
  1118. case BPF_ALU | BPF_MOD | BPF_X:
  1119. case BPF_ALU64 | BPF_MOD | BPF_X:
  1120. if (off)
  1121. emit(is64 ? rv_rem(rd, rd, rs) : rv_remw(rd, rd, rs), ctx);
  1122. else
  1123. emit(is64 ? rv_remu(rd, rd, rs) : rv_remuw(rd, rd, rs), ctx);
  1124. if (!is64 && !aux->verifier_zext)
  1125. emit_zextw(rd, rd, ctx);
  1126. break;
  1127. case BPF_ALU | BPF_LSH | BPF_X:
  1128. case BPF_ALU64 | BPF_LSH | BPF_X:
  1129. emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx);
  1130. if (!is64 && !aux->verifier_zext)
  1131. emit_zextw(rd, rd, ctx);
  1132. break;
  1133. case BPF_ALU | BPF_RSH | BPF_X:
  1134. case BPF_ALU64 | BPF_RSH | BPF_X:
  1135. emit(is64 ? rv_srl(rd, rd, rs) : rv_srlw(rd, rd, rs), ctx);
  1136. if (!is64 && !aux->verifier_zext)
  1137. emit_zextw(rd, rd, ctx);
  1138. break;
  1139. case BPF_ALU | BPF_ARSH | BPF_X:
  1140. case BPF_ALU64 | BPF_ARSH | BPF_X:
  1141. emit(is64 ? rv_sra(rd, rd, rs) : rv_sraw(rd, rd, rs), ctx);
  1142. if (!is64 && !aux->verifier_zext)
  1143. emit_zextw(rd, rd, ctx);
  1144. break;
  1145. /* dst = -dst */
  1146. case BPF_ALU | BPF_NEG:
  1147. case BPF_ALU64 | BPF_NEG:
  1148. emit_sub(rd, RV_REG_ZERO, rd, ctx);
  1149. if (!is64 && !aux->verifier_zext)
  1150. emit_zextw(rd, rd, ctx);
  1151. break;
  1152. /* dst = BSWAP##imm(dst) */
  1153. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1154. switch (imm) {
  1155. case 16:
  1156. emit_zexth(rd, rd, ctx);
  1157. break;
  1158. case 32:
  1159. if (!aux->verifier_zext)
  1160. emit_zextw(rd, rd, ctx);
  1161. break;
  1162. case 64:
  1163. /* Do nothing */
  1164. break;
  1165. }
  1166. break;
  1167. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1168. case BPF_ALU64 | BPF_END | BPF_FROM_LE:
  1169. emit_bswap(rd, imm, ctx);
  1170. break;
  1171. /* dst = imm */
  1172. case BPF_ALU | BPF_MOV | BPF_K:
  1173. case BPF_ALU64 | BPF_MOV | BPF_K:
  1174. emit_imm(rd, imm, ctx);
  1175. if (!is64 && !aux->verifier_zext)
  1176. emit_zextw(rd, rd, ctx);
  1177. break;
  1178. /* dst = dst OP imm */
  1179. case BPF_ALU | BPF_ADD | BPF_K:
  1180. case BPF_ALU64 | BPF_ADD | BPF_K:
  1181. if (is_12b_int(imm)) {
  1182. emit_addi(rd, rd, imm, ctx);
  1183. } else {
  1184. emit_imm(RV_REG_T1, imm, ctx);
  1185. emit_add(rd, rd, RV_REG_T1, ctx);
  1186. }
  1187. if (!is64 && !aux->verifier_zext)
  1188. emit_zextw(rd, rd, ctx);
  1189. break;
  1190. case BPF_ALU | BPF_SUB | BPF_K:
  1191. case BPF_ALU64 | BPF_SUB | BPF_K:
  1192. if (is_12b_int(-imm)) {
  1193. emit_addi(rd, rd, -imm, ctx);
  1194. } else {
  1195. emit_imm(RV_REG_T1, imm, ctx);
  1196. emit_sub(rd, rd, RV_REG_T1, ctx);
  1197. }
  1198. if (!is64 && !aux->verifier_zext)
  1199. emit_zextw(rd, rd, ctx);
  1200. break;
  1201. case BPF_ALU | BPF_AND | BPF_K:
  1202. case BPF_ALU64 | BPF_AND | BPF_K:
  1203. if (is_12b_int(imm)) {
  1204. emit_andi(rd, rd, imm, ctx);
  1205. } else {
  1206. emit_imm(RV_REG_T1, imm, ctx);
  1207. emit_and(rd, rd, RV_REG_T1, ctx);
  1208. }
  1209. if (!is64 && !aux->verifier_zext)
  1210. emit_zextw(rd, rd, ctx);
  1211. break;
  1212. case BPF_ALU | BPF_OR | BPF_K:
  1213. case BPF_ALU64 | BPF_OR | BPF_K:
  1214. if (is_12b_int(imm)) {
  1215. emit(rv_ori(rd, rd, imm), ctx);
  1216. } else {
  1217. emit_imm(RV_REG_T1, imm, ctx);
  1218. emit_or(rd, rd, RV_REG_T1, ctx);
  1219. }
  1220. if (!is64 && !aux->verifier_zext)
  1221. emit_zextw(rd, rd, ctx);
  1222. break;
  1223. case BPF_ALU | BPF_XOR | BPF_K:
  1224. case BPF_ALU64 | BPF_XOR | BPF_K:
  1225. if (is_12b_int(imm)) {
  1226. emit(rv_xori(rd, rd, imm), ctx);
  1227. } else {
  1228. emit_imm(RV_REG_T1, imm, ctx);
  1229. emit_xor(rd, rd, RV_REG_T1, ctx);
  1230. }
  1231. if (!is64 && !aux->verifier_zext)
  1232. emit_zextw(rd, rd, ctx);
  1233. break;
  1234. case BPF_ALU | BPF_MUL | BPF_K:
  1235. case BPF_ALU64 | BPF_MUL | BPF_K:
  1236. emit_imm(RV_REG_T1, imm, ctx);
  1237. emit(is64 ? rv_mul(rd, rd, RV_REG_T1) :
  1238. rv_mulw(rd, rd, RV_REG_T1), ctx);
  1239. if (!is64 && !aux->verifier_zext)
  1240. emit_zextw(rd, rd, ctx);
  1241. break;
  1242. case BPF_ALU | BPF_DIV | BPF_K:
  1243. case BPF_ALU64 | BPF_DIV | BPF_K:
  1244. emit_imm(RV_REG_T1, imm, ctx);
  1245. if (off)
  1246. emit(is64 ? rv_div(rd, rd, RV_REG_T1) :
  1247. rv_divw(rd, rd, RV_REG_T1), ctx);
  1248. else
  1249. emit(is64 ? rv_divu(rd, rd, RV_REG_T1) :
  1250. rv_divuw(rd, rd, RV_REG_T1), ctx);
  1251. if (!is64 && !aux->verifier_zext)
  1252. emit_zextw(rd, rd, ctx);
  1253. break;
  1254. case BPF_ALU | BPF_MOD | BPF_K:
  1255. case BPF_ALU64 | BPF_MOD | BPF_K:
  1256. emit_imm(RV_REG_T1, imm, ctx);
  1257. if (off)
  1258. emit(is64 ? rv_rem(rd, rd, RV_REG_T1) :
  1259. rv_remw(rd, rd, RV_REG_T1), ctx);
  1260. else
  1261. emit(is64 ? rv_remu(rd, rd, RV_REG_T1) :
  1262. rv_remuw(rd, rd, RV_REG_T1), ctx);
  1263. if (!is64 && !aux->verifier_zext)
  1264. emit_zextw(rd, rd, ctx);
  1265. break;
  1266. case BPF_ALU | BPF_LSH | BPF_K:
  1267. case BPF_ALU64 | BPF_LSH | BPF_K:
  1268. emit_slli(rd, rd, imm, ctx);
  1269. if (!is64 && !aux->verifier_zext)
  1270. emit_zextw(rd, rd, ctx);
  1271. break;
  1272. case BPF_ALU | BPF_RSH | BPF_K:
  1273. case BPF_ALU64 | BPF_RSH | BPF_K:
  1274. if (is64)
  1275. emit_srli(rd, rd, imm, ctx);
  1276. else
  1277. emit(rv_srliw(rd, rd, imm), ctx);
  1278. if (!is64 && !aux->verifier_zext)
  1279. emit_zextw(rd, rd, ctx);
  1280. break;
  1281. case BPF_ALU | BPF_ARSH | BPF_K:
  1282. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1283. if (is64)
  1284. emit_srai(rd, rd, imm, ctx);
  1285. else
  1286. emit(rv_sraiw(rd, rd, imm), ctx);
  1287. if (!is64 && !aux->verifier_zext)
  1288. emit_zextw(rd, rd, ctx);
  1289. break;
  1290. /* JUMP off */
  1291. case BPF_JMP | BPF_JA:
  1292. case BPF_JMP32 | BPF_JA:
  1293. if (BPF_CLASS(code) == BPF_JMP)
  1294. rvoff = rv_offset(i, off, ctx);
  1295. else
  1296. rvoff = rv_offset(i, imm, ctx);
  1297. ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
  1298. if (ret)
  1299. return ret;
  1300. break;
  1301. /* IF (dst COND src) JUMP off */
  1302. case BPF_JMP | BPF_JEQ | BPF_X:
  1303. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1304. case BPF_JMP | BPF_JGT | BPF_X:
  1305. case BPF_JMP32 | BPF_JGT | BPF_X:
  1306. case BPF_JMP | BPF_JLT | BPF_X:
  1307. case BPF_JMP32 | BPF_JLT | BPF_X:
  1308. case BPF_JMP | BPF_JGE | BPF_X:
  1309. case BPF_JMP32 | BPF_JGE | BPF_X:
  1310. case BPF_JMP | BPF_JLE | BPF_X:
  1311. case BPF_JMP32 | BPF_JLE | BPF_X:
  1312. case BPF_JMP | BPF_JNE | BPF_X:
  1313. case BPF_JMP32 | BPF_JNE | BPF_X:
  1314. case BPF_JMP | BPF_JSGT | BPF_X:
  1315. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1316. case BPF_JMP | BPF_JSLT | BPF_X:
  1317. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1318. case BPF_JMP | BPF_JSGE | BPF_X:
  1319. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1320. case BPF_JMP | BPF_JSLE | BPF_X:
  1321. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1322. case BPF_JMP | BPF_JSET | BPF_X:
  1323. case BPF_JMP32 | BPF_JSET | BPF_X:
  1324. rvoff = rv_offset(i, off, ctx);
  1325. if (!is64) {
  1326. s = ctx->ninsns;
  1327. if (is_signed_bpf_cond(BPF_OP(code))) {
  1328. emit_sextw_alt(&rs, RV_REG_T1, ctx);
  1329. emit_sextw_alt(&rd, RV_REG_T2, ctx);
  1330. } else {
  1331. emit_zextw_alt(&rs, RV_REG_T1, ctx);
  1332. emit_zextw_alt(&rd, RV_REG_T2, ctx);
  1333. }
  1334. e = ctx->ninsns;
  1335. /* Adjust for extra insns */
  1336. rvoff -= ninsns_rvoff(e - s);
  1337. }
  1338. if (BPF_OP(code) == BPF_JSET) {
  1339. /* Adjust for and */
  1340. rvoff -= 4;
  1341. emit_and(RV_REG_T1, rd, rs, ctx);
  1342. emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
  1343. } else {
  1344. emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
  1345. }
  1346. break;
  1347. /* IF (dst COND imm) JUMP off */
  1348. case BPF_JMP | BPF_JEQ | BPF_K:
  1349. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1350. case BPF_JMP | BPF_JGT | BPF_K:
  1351. case BPF_JMP32 | BPF_JGT | BPF_K:
  1352. case BPF_JMP | BPF_JLT | BPF_K:
  1353. case BPF_JMP32 | BPF_JLT | BPF_K:
  1354. case BPF_JMP | BPF_JGE | BPF_K:
  1355. case BPF_JMP32 | BPF_JGE | BPF_K:
  1356. case BPF_JMP | BPF_JLE | BPF_K:
  1357. case BPF_JMP32 | BPF_JLE | BPF_K:
  1358. case BPF_JMP | BPF_JNE | BPF_K:
  1359. case BPF_JMP32 | BPF_JNE | BPF_K:
  1360. case BPF_JMP | BPF_JSGT | BPF_K:
  1361. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1362. case BPF_JMP | BPF_JSLT | BPF_K:
  1363. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1364. case BPF_JMP | BPF_JSGE | BPF_K:
  1365. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1366. case BPF_JMP | BPF_JSLE | BPF_K:
  1367. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1368. rvoff = rv_offset(i, off, ctx);
  1369. s = ctx->ninsns;
  1370. if (imm)
  1371. emit_imm(RV_REG_T1, imm, ctx);
  1372. rs = imm ? RV_REG_T1 : RV_REG_ZERO;
  1373. if (!is64) {
  1374. if (is_signed_bpf_cond(BPF_OP(code))) {
  1375. emit_sextw_alt(&rd, RV_REG_T2, ctx);
  1376. /* rs has been sign extended */
  1377. } else {
  1378. emit_zextw_alt(&rd, RV_REG_T2, ctx);
  1379. if (imm)
  1380. emit_zextw(rs, rs, ctx);
  1381. }
  1382. }
  1383. e = ctx->ninsns;
  1384. /* Adjust for extra insns */
  1385. rvoff -= ninsns_rvoff(e - s);
  1386. emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
  1387. break;
  1388. case BPF_JMP | BPF_JSET | BPF_K:
  1389. case BPF_JMP32 | BPF_JSET | BPF_K:
  1390. rvoff = rv_offset(i, off, ctx);
  1391. s = ctx->ninsns;
  1392. if (is_12b_int(imm)) {
  1393. emit_andi(RV_REG_T1, rd, imm, ctx);
  1394. } else {
  1395. emit_imm(RV_REG_T1, imm, ctx);
  1396. emit_and(RV_REG_T1, rd, RV_REG_T1, ctx);
  1397. }
  1398. /* For jset32, we should clear the upper 32 bits of t1, but
  1399. * sign-extension is sufficient here and saves one instruction,
  1400. * as t1 is used only in comparison against zero.
  1401. */
  1402. if (!is64 && imm < 0)
  1403. emit_sextw(RV_REG_T1, RV_REG_T1, ctx);
  1404. e = ctx->ninsns;
  1405. rvoff -= ninsns_rvoff(e - s);
  1406. emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
  1407. break;
  1408. /* function call */
  1409. case BPF_JMP | BPF_CALL:
  1410. {
  1411. bool fixed_addr;
  1412. u64 addr;
  1413. /* Inline calls to bpf_get_smp_processor_id()
  1414. *
  1415. * RV_REG_TP holds the address of the current CPU's task_struct and thread_info is
  1416. * at offset 0 in task_struct.
  1417. * Load cpu from thread_info:
  1418. * Set R0 to ((struct thread_info *)(RV_REG_TP))->cpu
  1419. *
  1420. * This replicates the implementation of raw_smp_processor_id() on RISCV
  1421. */
  1422. if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
  1423. /* Load current CPU number in R0 */
  1424. emit_lw(bpf_to_rv_reg(BPF_REG_0, ctx), offsetof(struct thread_info, cpu),
  1425. RV_REG_TP, ctx);
  1426. break;
  1427. }
  1428. mark_call(ctx);
  1429. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
  1430. &addr, &fixed_addr);
  1431. if (ret < 0)
  1432. return ret;
  1433. if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
  1434. const struct btf_func_model *fm;
  1435. int idx;
  1436. fm = bpf_jit_find_kfunc_model(ctx->prog, insn);
  1437. if (!fm)
  1438. return -EINVAL;
  1439. for (idx = 0; idx < fm->nr_args; idx++) {
  1440. u8 reg = bpf_to_rv_reg(BPF_REG_1 + idx, ctx);
  1441. if (fm->arg_size[idx] == sizeof(int))
  1442. emit_sextw(reg, reg, ctx);
  1443. }
  1444. }
  1445. ret = emit_call(addr, fixed_addr, ctx);
  1446. if (ret)
  1447. return ret;
  1448. if (insn->src_reg != BPF_PSEUDO_CALL)
  1449. emit_mv(bpf_to_rv_reg(BPF_REG_0, ctx), RV_REG_A0, ctx);
  1450. break;
  1451. }
  1452. /* tail call */
  1453. case BPF_JMP | BPF_TAIL_CALL:
  1454. if (emit_bpf_tail_call(i, ctx))
  1455. return -1;
  1456. break;
  1457. /* function return */
  1458. case BPF_JMP | BPF_EXIT:
  1459. if (i == ctx->prog->len - 1)
  1460. break;
  1461. rvoff = epilogue_offset(ctx);
  1462. ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
  1463. if (ret)
  1464. return ret;
  1465. break;
  1466. /* dst = imm64 */
  1467. case BPF_LD | BPF_IMM | BPF_DW:
  1468. {
  1469. struct bpf_insn insn1 = insn[1];
  1470. u64 imm64;
  1471. imm64 = (u64)insn1.imm << 32 | (u32)imm;
  1472. if (bpf_pseudo_func(insn)) {
  1473. /* fixed-length insns for extra jit pass */
  1474. ret = emit_addr(rd, imm64, extra_pass, ctx);
  1475. if (ret)
  1476. return ret;
  1477. } else {
  1478. emit_imm(rd, imm64, ctx);
  1479. }
  1480. return 1;
  1481. }
  1482. /* LDX: dst = *(unsigned size *)(src + off) */
  1483. case BPF_LDX | BPF_MEM | BPF_B:
  1484. case BPF_LDX | BPF_MEM | BPF_H:
  1485. case BPF_LDX | BPF_MEM | BPF_W:
  1486. case BPF_LDX | BPF_MEM | BPF_DW:
  1487. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  1488. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  1489. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  1490. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  1491. /* LDSX: dst = *(signed size *)(src + off) */
  1492. case BPF_LDX | BPF_MEMSX | BPF_B:
  1493. case BPF_LDX | BPF_MEMSX | BPF_H:
  1494. case BPF_LDX | BPF_MEMSX | BPF_W:
  1495. case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
  1496. case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
  1497. case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
  1498. /* LDX | PROBE_MEM32: dst = *(unsigned size *)(src + RV_REG_ARENA + off) */
  1499. case BPF_LDX | BPF_PROBE_MEM32 | BPF_B:
  1500. case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
  1501. case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
  1502. case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
  1503. {
  1504. int insn_len, insns_start;
  1505. bool sign_ext;
  1506. sign_ext = BPF_MODE(insn->code) == BPF_MEMSX ||
  1507. BPF_MODE(insn->code) == BPF_PROBE_MEMSX;
  1508. if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
  1509. emit_add(RV_REG_T2, rs, RV_REG_ARENA, ctx);
  1510. rs = RV_REG_T2;
  1511. }
  1512. switch (BPF_SIZE(code)) {
  1513. case BPF_B:
  1514. if (is_12b_int(off)) {
  1515. insns_start = ctx->ninsns;
  1516. if (sign_ext)
  1517. emit(rv_lb(rd, off, rs), ctx);
  1518. else
  1519. emit(rv_lbu(rd, off, rs), ctx);
  1520. insn_len = ctx->ninsns - insns_start;
  1521. break;
  1522. }
  1523. emit_imm(RV_REG_T1, off, ctx);
  1524. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1525. insns_start = ctx->ninsns;
  1526. if (sign_ext)
  1527. emit(rv_lb(rd, 0, RV_REG_T1), ctx);
  1528. else
  1529. emit(rv_lbu(rd, 0, RV_REG_T1), ctx);
  1530. insn_len = ctx->ninsns - insns_start;
  1531. break;
  1532. case BPF_H:
  1533. if (is_12b_int(off)) {
  1534. insns_start = ctx->ninsns;
  1535. if (sign_ext)
  1536. emit(rv_lh(rd, off, rs), ctx);
  1537. else
  1538. emit(rv_lhu(rd, off, rs), ctx);
  1539. insn_len = ctx->ninsns - insns_start;
  1540. break;
  1541. }
  1542. emit_imm(RV_REG_T1, off, ctx);
  1543. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1544. insns_start = ctx->ninsns;
  1545. if (sign_ext)
  1546. emit(rv_lh(rd, 0, RV_REG_T1), ctx);
  1547. else
  1548. emit(rv_lhu(rd, 0, RV_REG_T1), ctx);
  1549. insn_len = ctx->ninsns - insns_start;
  1550. break;
  1551. case BPF_W:
  1552. if (is_12b_int(off)) {
  1553. insns_start = ctx->ninsns;
  1554. if (sign_ext)
  1555. emit(rv_lw(rd, off, rs), ctx);
  1556. else
  1557. emit(rv_lwu(rd, off, rs), ctx);
  1558. insn_len = ctx->ninsns - insns_start;
  1559. break;
  1560. }
  1561. emit_imm(RV_REG_T1, off, ctx);
  1562. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1563. insns_start = ctx->ninsns;
  1564. if (sign_ext)
  1565. emit(rv_lw(rd, 0, RV_REG_T1), ctx);
  1566. else
  1567. emit(rv_lwu(rd, 0, RV_REG_T1), ctx);
  1568. insn_len = ctx->ninsns - insns_start;
  1569. break;
  1570. case BPF_DW:
  1571. if (is_12b_int(off)) {
  1572. insns_start = ctx->ninsns;
  1573. emit_ld(rd, off, rs, ctx);
  1574. insn_len = ctx->ninsns - insns_start;
  1575. break;
  1576. }
  1577. emit_imm(RV_REG_T1, off, ctx);
  1578. emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
  1579. insns_start = ctx->ninsns;
  1580. emit_ld(rd, 0, RV_REG_T1, ctx);
  1581. insn_len = ctx->ninsns - insns_start;
  1582. break;
  1583. }
  1584. ret = add_exception_handler(insn, ctx, rd, insn_len);
  1585. if (ret)
  1586. return ret;
  1587. if (BPF_SIZE(code) != BPF_DW && insn_is_zext(&insn[1]))
  1588. return 1;
  1589. break;
  1590. }
  1591. /* speculation barrier */
  1592. case BPF_ST | BPF_NOSPEC:
  1593. break;
  1594. /* ST: *(size *)(dst + off) = imm */
  1595. case BPF_ST | BPF_MEM | BPF_B:
  1596. emit_imm(RV_REG_T1, imm, ctx);
  1597. if (is_12b_int(off)) {
  1598. emit(rv_sb(rd, off, RV_REG_T1), ctx);
  1599. break;
  1600. }
  1601. emit_imm(RV_REG_T2, off, ctx);
  1602. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1603. emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx);
  1604. break;
  1605. case BPF_ST | BPF_MEM | BPF_H:
  1606. emit_imm(RV_REG_T1, imm, ctx);
  1607. if (is_12b_int(off)) {
  1608. emit(rv_sh(rd, off, RV_REG_T1), ctx);
  1609. break;
  1610. }
  1611. emit_imm(RV_REG_T2, off, ctx);
  1612. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1613. emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx);
  1614. break;
  1615. case BPF_ST | BPF_MEM | BPF_W:
  1616. emit_imm(RV_REG_T1, imm, ctx);
  1617. if (is_12b_int(off)) {
  1618. emit_sw(rd, off, RV_REG_T1, ctx);
  1619. break;
  1620. }
  1621. emit_imm(RV_REG_T2, off, ctx);
  1622. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1623. emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx);
  1624. break;
  1625. case BPF_ST | BPF_MEM | BPF_DW:
  1626. emit_imm(RV_REG_T1, imm, ctx);
  1627. if (is_12b_int(off)) {
  1628. emit_sd(rd, off, RV_REG_T1, ctx);
  1629. break;
  1630. }
  1631. emit_imm(RV_REG_T2, off, ctx);
  1632. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1633. emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx);
  1634. break;
  1635. case BPF_ST | BPF_PROBE_MEM32 | BPF_B:
  1636. case BPF_ST | BPF_PROBE_MEM32 | BPF_H:
  1637. case BPF_ST | BPF_PROBE_MEM32 | BPF_W:
  1638. case BPF_ST | BPF_PROBE_MEM32 | BPF_DW:
  1639. {
  1640. int insn_len, insns_start;
  1641. emit_add(RV_REG_T3, rd, RV_REG_ARENA, ctx);
  1642. rd = RV_REG_T3;
  1643. /* Load imm to a register then store it */
  1644. emit_imm(RV_REG_T1, imm, ctx);
  1645. switch (BPF_SIZE(code)) {
  1646. case BPF_B:
  1647. if (is_12b_int(off)) {
  1648. insns_start = ctx->ninsns;
  1649. emit(rv_sb(rd, off, RV_REG_T1), ctx);
  1650. insn_len = ctx->ninsns - insns_start;
  1651. break;
  1652. }
  1653. emit_imm(RV_REG_T2, off, ctx);
  1654. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1655. insns_start = ctx->ninsns;
  1656. emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx);
  1657. insn_len = ctx->ninsns - insns_start;
  1658. break;
  1659. case BPF_H:
  1660. if (is_12b_int(off)) {
  1661. insns_start = ctx->ninsns;
  1662. emit(rv_sh(rd, off, RV_REG_T1), ctx);
  1663. insn_len = ctx->ninsns - insns_start;
  1664. break;
  1665. }
  1666. emit_imm(RV_REG_T2, off, ctx);
  1667. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1668. insns_start = ctx->ninsns;
  1669. emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx);
  1670. insn_len = ctx->ninsns - insns_start;
  1671. break;
  1672. case BPF_W:
  1673. if (is_12b_int(off)) {
  1674. insns_start = ctx->ninsns;
  1675. emit_sw(rd, off, RV_REG_T1, ctx);
  1676. insn_len = ctx->ninsns - insns_start;
  1677. break;
  1678. }
  1679. emit_imm(RV_REG_T2, off, ctx);
  1680. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1681. insns_start = ctx->ninsns;
  1682. emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx);
  1683. insn_len = ctx->ninsns - insns_start;
  1684. break;
  1685. case BPF_DW:
  1686. if (is_12b_int(off)) {
  1687. insns_start = ctx->ninsns;
  1688. emit_sd(rd, off, RV_REG_T1, ctx);
  1689. insn_len = ctx->ninsns - insns_start;
  1690. break;
  1691. }
  1692. emit_imm(RV_REG_T2, off, ctx);
  1693. emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
  1694. insns_start = ctx->ninsns;
  1695. emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx);
  1696. insn_len = ctx->ninsns - insns_start;
  1697. break;
  1698. }
  1699. ret = add_exception_handler(insn, ctx, REG_DONT_CLEAR_MARKER,
  1700. insn_len);
  1701. if (ret)
  1702. return ret;
  1703. break;
  1704. }
  1705. /* STX: *(size *)(dst + off) = src */
  1706. case BPF_STX | BPF_MEM | BPF_B:
  1707. if (is_12b_int(off)) {
  1708. emit(rv_sb(rd, off, rs), ctx);
  1709. break;
  1710. }
  1711. emit_imm(RV_REG_T1, off, ctx);
  1712. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1713. emit(rv_sb(RV_REG_T1, 0, rs), ctx);
  1714. break;
  1715. case BPF_STX | BPF_MEM | BPF_H:
  1716. if (is_12b_int(off)) {
  1717. emit(rv_sh(rd, off, rs), ctx);
  1718. break;
  1719. }
  1720. emit_imm(RV_REG_T1, off, ctx);
  1721. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1722. emit(rv_sh(RV_REG_T1, 0, rs), ctx);
  1723. break;
  1724. case BPF_STX | BPF_MEM | BPF_W:
  1725. if (is_12b_int(off)) {
  1726. emit_sw(rd, off, rs, ctx);
  1727. break;
  1728. }
  1729. emit_imm(RV_REG_T1, off, ctx);
  1730. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1731. emit_sw(RV_REG_T1, 0, rs, ctx);
  1732. break;
  1733. case BPF_STX | BPF_MEM | BPF_DW:
  1734. if (is_12b_int(off)) {
  1735. emit_sd(rd, off, rs, ctx);
  1736. break;
  1737. }
  1738. emit_imm(RV_REG_T1, off, ctx);
  1739. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1740. emit_sd(RV_REG_T1, 0, rs, ctx);
  1741. break;
  1742. case BPF_STX | BPF_ATOMIC | BPF_W:
  1743. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1744. emit_atomic(rd, rs, off, imm,
  1745. BPF_SIZE(code) == BPF_DW, ctx);
  1746. break;
  1747. case BPF_STX | BPF_PROBE_MEM32 | BPF_B:
  1748. case BPF_STX | BPF_PROBE_MEM32 | BPF_H:
  1749. case BPF_STX | BPF_PROBE_MEM32 | BPF_W:
  1750. case BPF_STX | BPF_PROBE_MEM32 | BPF_DW:
  1751. {
  1752. int insn_len, insns_start;
  1753. emit_add(RV_REG_T2, rd, RV_REG_ARENA, ctx);
  1754. rd = RV_REG_T2;
  1755. switch (BPF_SIZE(code)) {
  1756. case BPF_B:
  1757. if (is_12b_int(off)) {
  1758. insns_start = ctx->ninsns;
  1759. emit(rv_sb(rd, off, rs), ctx);
  1760. insn_len = ctx->ninsns - insns_start;
  1761. break;
  1762. }
  1763. emit_imm(RV_REG_T1, off, ctx);
  1764. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1765. insns_start = ctx->ninsns;
  1766. emit(rv_sb(RV_REG_T1, 0, rs), ctx);
  1767. insn_len = ctx->ninsns - insns_start;
  1768. break;
  1769. case BPF_H:
  1770. if (is_12b_int(off)) {
  1771. insns_start = ctx->ninsns;
  1772. emit(rv_sh(rd, off, rs), ctx);
  1773. insn_len = ctx->ninsns - insns_start;
  1774. break;
  1775. }
  1776. emit_imm(RV_REG_T1, off, ctx);
  1777. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1778. insns_start = ctx->ninsns;
  1779. emit(rv_sh(RV_REG_T1, 0, rs), ctx);
  1780. insn_len = ctx->ninsns - insns_start;
  1781. break;
  1782. case BPF_W:
  1783. if (is_12b_int(off)) {
  1784. insns_start = ctx->ninsns;
  1785. emit_sw(rd, off, rs, ctx);
  1786. insn_len = ctx->ninsns - insns_start;
  1787. break;
  1788. }
  1789. emit_imm(RV_REG_T1, off, ctx);
  1790. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1791. insns_start = ctx->ninsns;
  1792. emit_sw(RV_REG_T1, 0, rs, ctx);
  1793. insn_len = ctx->ninsns - insns_start;
  1794. break;
  1795. case BPF_DW:
  1796. if (is_12b_int(off)) {
  1797. insns_start = ctx->ninsns;
  1798. emit_sd(rd, off, rs, ctx);
  1799. insn_len = ctx->ninsns - insns_start;
  1800. break;
  1801. }
  1802. emit_imm(RV_REG_T1, off, ctx);
  1803. emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
  1804. insns_start = ctx->ninsns;
  1805. emit_sd(RV_REG_T1, 0, rs, ctx);
  1806. insn_len = ctx->ninsns - insns_start;
  1807. break;
  1808. }
  1809. ret = add_exception_handler(insn, ctx, REG_DONT_CLEAR_MARKER,
  1810. insn_len);
  1811. if (ret)
  1812. return ret;
  1813. break;
  1814. }
  1815. default:
  1816. pr_err("bpf-jit: unknown opcode %02x\n", code);
  1817. return -EINVAL;
  1818. }
  1819. return 0;
  1820. }
  1821. void bpf_jit_build_prologue(struct rv_jit_context *ctx, bool is_subprog)
  1822. {
  1823. int i, stack_adjust = 0, store_offset, bpf_stack_adjust;
  1824. bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, STACK_ALIGN);
  1825. if (bpf_stack_adjust)
  1826. mark_fp(ctx);
  1827. if (seen_reg(RV_REG_RA, ctx))
  1828. stack_adjust += 8;
  1829. stack_adjust += 8; /* RV_REG_FP */
  1830. if (seen_reg(RV_REG_S1, ctx))
  1831. stack_adjust += 8;
  1832. if (seen_reg(RV_REG_S2, ctx))
  1833. stack_adjust += 8;
  1834. if (seen_reg(RV_REG_S3, ctx))
  1835. stack_adjust += 8;
  1836. if (seen_reg(RV_REG_S4, ctx))
  1837. stack_adjust += 8;
  1838. if (seen_reg(RV_REG_S5, ctx))
  1839. stack_adjust += 8;
  1840. if (seen_reg(RV_REG_S6, ctx))
  1841. stack_adjust += 8;
  1842. if (ctx->arena_vm_start)
  1843. stack_adjust += 8;
  1844. stack_adjust = round_up(stack_adjust, STACK_ALIGN);
  1845. stack_adjust += bpf_stack_adjust;
  1846. store_offset = stack_adjust - 8;
  1847. /* emit kcfi type preamble immediately before the first insn */
  1848. emit_kcfi(is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash, ctx);
  1849. /* nops reserved for auipc+jalr pair */
  1850. for (i = 0; i < RV_FENTRY_NINSNS; i++)
  1851. emit(rv_nop(), ctx);
  1852. /* First instruction is always setting the tail-call-counter
  1853. * (TCC) register. This instruction is skipped for tail calls.
  1854. * Force using a 4-byte (non-compressed) instruction.
  1855. */
  1856. emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
  1857. emit_addi(RV_REG_SP, RV_REG_SP, -stack_adjust, ctx);
  1858. if (seen_reg(RV_REG_RA, ctx)) {
  1859. emit_sd(RV_REG_SP, store_offset, RV_REG_RA, ctx);
  1860. store_offset -= 8;
  1861. }
  1862. emit_sd(RV_REG_SP, store_offset, RV_REG_FP, ctx);
  1863. store_offset -= 8;
  1864. if (seen_reg(RV_REG_S1, ctx)) {
  1865. emit_sd(RV_REG_SP, store_offset, RV_REG_S1, ctx);
  1866. store_offset -= 8;
  1867. }
  1868. if (seen_reg(RV_REG_S2, ctx)) {
  1869. emit_sd(RV_REG_SP, store_offset, RV_REG_S2, ctx);
  1870. store_offset -= 8;
  1871. }
  1872. if (seen_reg(RV_REG_S3, ctx)) {
  1873. emit_sd(RV_REG_SP, store_offset, RV_REG_S3, ctx);
  1874. store_offset -= 8;
  1875. }
  1876. if (seen_reg(RV_REG_S4, ctx)) {
  1877. emit_sd(RV_REG_SP, store_offset, RV_REG_S4, ctx);
  1878. store_offset -= 8;
  1879. }
  1880. if (seen_reg(RV_REG_S5, ctx)) {
  1881. emit_sd(RV_REG_SP, store_offset, RV_REG_S5, ctx);
  1882. store_offset -= 8;
  1883. }
  1884. if (seen_reg(RV_REG_S6, ctx)) {
  1885. emit_sd(RV_REG_SP, store_offset, RV_REG_S6, ctx);
  1886. store_offset -= 8;
  1887. }
  1888. if (ctx->arena_vm_start) {
  1889. emit_sd(RV_REG_SP, store_offset, RV_REG_ARENA, ctx);
  1890. store_offset -= 8;
  1891. }
  1892. emit_addi(RV_REG_FP, RV_REG_SP, stack_adjust, ctx);
  1893. if (bpf_stack_adjust)
  1894. emit_addi(RV_REG_S5, RV_REG_SP, bpf_stack_adjust, ctx);
  1895. /* Program contains calls and tail calls, so RV_REG_TCC need
  1896. * to be saved across calls.
  1897. */
  1898. if (seen_tail_call(ctx) && seen_call(ctx))
  1899. emit_mv(RV_REG_TCC_SAVED, RV_REG_TCC, ctx);
  1900. ctx->stack_size = stack_adjust;
  1901. if (ctx->arena_vm_start)
  1902. emit_imm(RV_REG_ARENA, ctx->arena_vm_start, ctx);
  1903. }
  1904. void bpf_jit_build_epilogue(struct rv_jit_context *ctx)
  1905. {
  1906. __build_epilogue(false, ctx);
  1907. }
  1908. bool bpf_jit_supports_kfunc_call(void)
  1909. {
  1910. return true;
  1911. }
  1912. bool bpf_jit_supports_ptr_xchg(void)
  1913. {
  1914. return true;
  1915. }
  1916. bool bpf_jit_supports_arena(void)
  1917. {
  1918. return true;
  1919. }
  1920. bool bpf_jit_supports_percpu_insn(void)
  1921. {
  1922. return true;
  1923. }
  1924. bool bpf_jit_inlines_helper_call(s32 imm)
  1925. {
  1926. switch (imm) {
  1927. case BPF_FUNC_get_smp_processor_id:
  1928. return true;
  1929. default:
  1930. return false;
  1931. }
  1932. }