bpf_jit.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * BPF JIT compiler for LoongArch
  4. *
  5. * Copyright (C) 2022 Loongson Technology Corporation Limited
  6. */
  7. #include "bpf_jit.h"
  8. #define REG_TCC LOONGARCH_GPR_A6
  9. #define TCC_SAVED LOONGARCH_GPR_S5
  10. #define SAVE_RA BIT(0)
  11. #define SAVE_TCC BIT(1)
  12. static const int regmap[] = {
  13. /* return value from in-kernel function, and exit value for eBPF program */
  14. [BPF_REG_0] = LOONGARCH_GPR_A5,
  15. /* arguments from eBPF program to in-kernel function */
  16. [BPF_REG_1] = LOONGARCH_GPR_A0,
  17. [BPF_REG_2] = LOONGARCH_GPR_A1,
  18. [BPF_REG_3] = LOONGARCH_GPR_A2,
  19. [BPF_REG_4] = LOONGARCH_GPR_A3,
  20. [BPF_REG_5] = LOONGARCH_GPR_A4,
  21. /* callee saved registers that in-kernel function will preserve */
  22. [BPF_REG_6] = LOONGARCH_GPR_S0,
  23. [BPF_REG_7] = LOONGARCH_GPR_S1,
  24. [BPF_REG_8] = LOONGARCH_GPR_S2,
  25. [BPF_REG_9] = LOONGARCH_GPR_S3,
  26. /* read-only frame pointer to access stack */
  27. [BPF_REG_FP] = LOONGARCH_GPR_S4,
  28. /* temporary register for blinding constants */
  29. [BPF_REG_AX] = LOONGARCH_GPR_T0,
  30. };
  31. static void mark_call(struct jit_ctx *ctx)
  32. {
  33. ctx->flags |= SAVE_RA;
  34. }
  35. static void mark_tail_call(struct jit_ctx *ctx)
  36. {
  37. ctx->flags |= SAVE_TCC;
  38. }
  39. static bool seen_call(struct jit_ctx *ctx)
  40. {
  41. return (ctx->flags & SAVE_RA);
  42. }
  43. static bool seen_tail_call(struct jit_ctx *ctx)
  44. {
  45. return (ctx->flags & SAVE_TCC);
  46. }
  47. static u8 tail_call_reg(struct jit_ctx *ctx)
  48. {
  49. if (seen_call(ctx))
  50. return TCC_SAVED;
  51. return REG_TCC;
  52. }
  53. /*
  54. * eBPF prog stack layout:
  55. *
  56. * high
  57. * original $sp ------------> +-------------------------+ <--LOONGARCH_GPR_FP
  58. * | $ra |
  59. * +-------------------------+
  60. * | $fp |
  61. * +-------------------------+
  62. * | $s0 |
  63. * +-------------------------+
  64. * | $s1 |
  65. * +-------------------------+
  66. * | $s2 |
  67. * +-------------------------+
  68. * | $s3 |
  69. * +-------------------------+
  70. * | $s4 |
  71. * +-------------------------+
  72. * | $s5 |
  73. * +-------------------------+ <--BPF_REG_FP
  74. * | prog->aux->stack_depth |
  75. * | (optional) |
  76. * current $sp -------------> +-------------------------+
  77. * low
  78. */
  79. static void build_prologue(struct jit_ctx *ctx)
  80. {
  81. int stack_adjust = 0, store_offset, bpf_stack_adjust;
  82. bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
  83. /* To store ra, fp, s0, s1, s2, s3, s4 and s5. */
  84. stack_adjust += sizeof(long) * 8;
  85. stack_adjust = round_up(stack_adjust, 16);
  86. stack_adjust += bpf_stack_adjust;
  87. /*
  88. * First instruction initializes the tail call count (TCC).
  89. * On tail call we skip this instruction, and the TCC is
  90. * passed in REG_TCC from the caller.
  91. */
  92. emit_insn(ctx, addid, REG_TCC, LOONGARCH_GPR_ZERO, MAX_TAIL_CALL_CNT);
  93. emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -stack_adjust);
  94. store_offset = stack_adjust - sizeof(long);
  95. emit_insn(ctx, std, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, store_offset);
  96. store_offset -= sizeof(long);
  97. emit_insn(ctx, std, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, store_offset);
  98. store_offset -= sizeof(long);
  99. emit_insn(ctx, std, LOONGARCH_GPR_S0, LOONGARCH_GPR_SP, store_offset);
  100. store_offset -= sizeof(long);
  101. emit_insn(ctx, std, LOONGARCH_GPR_S1, LOONGARCH_GPR_SP, store_offset);
  102. store_offset -= sizeof(long);
  103. emit_insn(ctx, std, LOONGARCH_GPR_S2, LOONGARCH_GPR_SP, store_offset);
  104. store_offset -= sizeof(long);
  105. emit_insn(ctx, std, LOONGARCH_GPR_S3, LOONGARCH_GPR_SP, store_offset);
  106. store_offset -= sizeof(long);
  107. emit_insn(ctx, std, LOONGARCH_GPR_S4, LOONGARCH_GPR_SP, store_offset);
  108. store_offset -= sizeof(long);
  109. emit_insn(ctx, std, LOONGARCH_GPR_S5, LOONGARCH_GPR_SP, store_offset);
  110. emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_adjust);
  111. if (bpf_stack_adjust)
  112. emit_insn(ctx, addid, regmap[BPF_REG_FP], LOONGARCH_GPR_SP, bpf_stack_adjust);
  113. /*
  114. * Program contains calls and tail calls, so REG_TCC need
  115. * to be saved across calls.
  116. */
  117. if (seen_tail_call(ctx) && seen_call(ctx))
  118. move_reg(ctx, TCC_SAVED, REG_TCC);
  119. ctx->stack_size = stack_adjust;
  120. }
  121. static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call)
  122. {
  123. int stack_adjust = ctx->stack_size;
  124. int load_offset;
  125. load_offset = stack_adjust - sizeof(long);
  126. emit_insn(ctx, ldd, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, load_offset);
  127. load_offset -= sizeof(long);
  128. emit_insn(ctx, ldd, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, load_offset);
  129. load_offset -= sizeof(long);
  130. emit_insn(ctx, ldd, LOONGARCH_GPR_S0, LOONGARCH_GPR_SP, load_offset);
  131. load_offset -= sizeof(long);
  132. emit_insn(ctx, ldd, LOONGARCH_GPR_S1, LOONGARCH_GPR_SP, load_offset);
  133. load_offset -= sizeof(long);
  134. emit_insn(ctx, ldd, LOONGARCH_GPR_S2, LOONGARCH_GPR_SP, load_offset);
  135. load_offset -= sizeof(long);
  136. emit_insn(ctx, ldd, LOONGARCH_GPR_S3, LOONGARCH_GPR_SP, load_offset);
  137. load_offset -= sizeof(long);
  138. emit_insn(ctx, ldd, LOONGARCH_GPR_S4, LOONGARCH_GPR_SP, load_offset);
  139. load_offset -= sizeof(long);
  140. emit_insn(ctx, ldd, LOONGARCH_GPR_S5, LOONGARCH_GPR_SP, load_offset);
  141. emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, stack_adjust);
  142. if (!is_tail_call) {
  143. /* Set return value */
  144. emit_insn(ctx, addiw, LOONGARCH_GPR_A0, regmap[BPF_REG_0], 0);
  145. /* Return to the caller */
  146. emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_RA, 0);
  147. } else {
  148. /*
  149. * Call the next bpf prog and skip the first instruction
  150. * of TCC initialization.
  151. */
  152. emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_T3, 1);
  153. }
  154. }
  155. static void build_epilogue(struct jit_ctx *ctx)
  156. {
  157. __build_epilogue(ctx, false);
  158. }
  159. bool bpf_jit_supports_kfunc_call(void)
  160. {
  161. return true;
  162. }
  163. bool bpf_jit_supports_far_kfunc_call(void)
  164. {
  165. return true;
  166. }
  167. /* initialized on the first pass of build_body() */
  168. static int out_offset = -1;
  169. static int emit_bpf_tail_call(struct jit_ctx *ctx)
  170. {
  171. int off;
  172. u8 tcc = tail_call_reg(ctx);
  173. u8 a1 = LOONGARCH_GPR_A1;
  174. u8 a2 = LOONGARCH_GPR_A2;
  175. u8 t1 = LOONGARCH_GPR_T1;
  176. u8 t2 = LOONGARCH_GPR_T2;
  177. u8 t3 = LOONGARCH_GPR_T3;
  178. const int idx0 = ctx->idx;
  179. #define cur_offset (ctx->idx - idx0)
  180. #define jmp_offset (out_offset - (cur_offset))
  181. /*
  182. * a0: &ctx
  183. * a1: &array
  184. * a2: index
  185. *
  186. * if (index >= array->map.max_entries)
  187. * goto out;
  188. */
  189. off = offsetof(struct bpf_array, map.max_entries);
  190. emit_insn(ctx, ldwu, t1, a1, off);
  191. /* bgeu $a2, $t1, jmp_offset */
  192. if (emit_tailcall_jmp(ctx, BPF_JGE, a2, t1, jmp_offset) < 0)
  193. goto toofar;
  194. /*
  195. * if (--TCC < 0)
  196. * goto out;
  197. */
  198. emit_insn(ctx, addid, REG_TCC, tcc, -1);
  199. if (emit_tailcall_jmp(ctx, BPF_JSLT, REG_TCC, LOONGARCH_GPR_ZERO, jmp_offset) < 0)
  200. goto toofar;
  201. /*
  202. * prog = array->ptrs[index];
  203. * if (!prog)
  204. * goto out;
  205. */
  206. emit_insn(ctx, alsld, t2, a2, a1, 2);
  207. off = offsetof(struct bpf_array, ptrs);
  208. emit_insn(ctx, ldd, t2, t2, off);
  209. /* beq $t2, $zero, jmp_offset */
  210. if (emit_tailcall_jmp(ctx, BPF_JEQ, t2, LOONGARCH_GPR_ZERO, jmp_offset) < 0)
  211. goto toofar;
  212. /* goto *(prog->bpf_func + 4); */
  213. off = offsetof(struct bpf_prog, bpf_func);
  214. emit_insn(ctx, ldd, t3, t2, off);
  215. __build_epilogue(ctx, true);
  216. /* out: */
  217. if (out_offset == -1)
  218. out_offset = cur_offset;
  219. if (cur_offset != out_offset) {
  220. pr_err_once("tail_call out_offset = %d, expected %d!\n",
  221. cur_offset, out_offset);
  222. return -1;
  223. }
  224. return 0;
  225. toofar:
  226. pr_info_once("tail_call: jump too far\n");
  227. return -1;
  228. #undef cur_offset
  229. #undef jmp_offset
  230. }
  231. static void emit_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  232. {
  233. const u8 t1 = LOONGARCH_GPR_T1;
  234. const u8 t2 = LOONGARCH_GPR_T2;
  235. const u8 t3 = LOONGARCH_GPR_T3;
  236. const u8 r0 = regmap[BPF_REG_0];
  237. const u8 src = regmap[insn->src_reg];
  238. const u8 dst = regmap[insn->dst_reg];
  239. const s16 off = insn->off;
  240. const s32 imm = insn->imm;
  241. const bool isdw = BPF_SIZE(insn->code) == BPF_DW;
  242. move_imm(ctx, t1, off, false);
  243. emit_insn(ctx, addd, t1, dst, t1);
  244. move_reg(ctx, t3, src);
  245. switch (imm) {
  246. /* lock *(size *)(dst + off) <op>= src */
  247. case BPF_ADD:
  248. if (isdw)
  249. emit_insn(ctx, amaddd, t2, t1, src);
  250. else
  251. emit_insn(ctx, amaddw, t2, t1, src);
  252. break;
  253. case BPF_AND:
  254. if (isdw)
  255. emit_insn(ctx, amandd, t2, t1, src);
  256. else
  257. emit_insn(ctx, amandw, t2, t1, src);
  258. break;
  259. case BPF_OR:
  260. if (isdw)
  261. emit_insn(ctx, amord, t2, t1, src);
  262. else
  263. emit_insn(ctx, amorw, t2, t1, src);
  264. break;
  265. case BPF_XOR:
  266. if (isdw)
  267. emit_insn(ctx, amxord, t2, t1, src);
  268. else
  269. emit_insn(ctx, amxorw, t2, t1, src);
  270. break;
  271. /* src = atomic_fetch_<op>(dst + off, src) */
  272. case BPF_ADD | BPF_FETCH:
  273. if (isdw) {
  274. emit_insn(ctx, amaddd, src, t1, t3);
  275. } else {
  276. emit_insn(ctx, amaddw, src, t1, t3);
  277. emit_zext_32(ctx, src, true);
  278. }
  279. break;
  280. case BPF_AND | BPF_FETCH:
  281. if (isdw) {
  282. emit_insn(ctx, amandd, src, t1, t3);
  283. } else {
  284. emit_insn(ctx, amandw, src, t1, t3);
  285. emit_zext_32(ctx, src, true);
  286. }
  287. break;
  288. case BPF_OR | BPF_FETCH:
  289. if (isdw) {
  290. emit_insn(ctx, amord, src, t1, t3);
  291. } else {
  292. emit_insn(ctx, amorw, src, t1, t3);
  293. emit_zext_32(ctx, src, true);
  294. }
  295. break;
  296. case BPF_XOR | BPF_FETCH:
  297. if (isdw) {
  298. emit_insn(ctx, amxord, src, t1, t3);
  299. } else {
  300. emit_insn(ctx, amxorw, src, t1, t3);
  301. emit_zext_32(ctx, src, true);
  302. }
  303. break;
  304. /* src = atomic_xchg(dst + off, src); */
  305. case BPF_XCHG:
  306. if (isdw) {
  307. emit_insn(ctx, amswapd, src, t1, t3);
  308. } else {
  309. emit_insn(ctx, amswapw, src, t1, t3);
  310. emit_zext_32(ctx, src, true);
  311. }
  312. break;
  313. /* r0 = atomic_cmpxchg(dst + off, r0, src); */
  314. case BPF_CMPXCHG:
  315. move_reg(ctx, t2, r0);
  316. if (isdw) {
  317. emit_insn(ctx, lld, r0, t1, 0);
  318. emit_insn(ctx, bne, t2, r0, 4);
  319. move_reg(ctx, t3, src);
  320. emit_insn(ctx, scd, t3, t1, 0);
  321. emit_insn(ctx, beq, t3, LOONGARCH_GPR_ZERO, -4);
  322. } else {
  323. emit_insn(ctx, llw, r0, t1, 0);
  324. emit_zext_32(ctx, t2, true);
  325. emit_zext_32(ctx, r0, true);
  326. emit_insn(ctx, bne, t2, r0, 4);
  327. move_reg(ctx, t3, src);
  328. emit_insn(ctx, scw, t3, t1, 0);
  329. emit_insn(ctx, beq, t3, LOONGARCH_GPR_ZERO, -6);
  330. emit_zext_32(ctx, r0, true);
  331. }
  332. break;
  333. }
  334. }
  335. static bool is_signed_bpf_cond(u8 cond)
  336. {
  337. return cond == BPF_JSGT || cond == BPF_JSLT ||
  338. cond == BPF_JSGE || cond == BPF_JSLE;
  339. }
  340. #define BPF_FIXUP_REG_MASK GENMASK(31, 27)
  341. #define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
  342. bool ex_handler_bpf(const struct exception_table_entry *ex,
  343. struct pt_regs *regs)
  344. {
  345. int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
  346. off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
  347. regs->regs[dst_reg] = 0;
  348. regs->csr_era = (unsigned long)&ex->fixup - offset;
  349. return true;
  350. }
  351. /* For accesses to BTF pointers, add an entry to the exception table */
  352. static int add_exception_handler(const struct bpf_insn *insn,
  353. struct jit_ctx *ctx,
  354. int dst_reg)
  355. {
  356. unsigned long pc;
  357. off_t offset;
  358. struct exception_table_entry *ex;
  359. if (!ctx->image || !ctx->prog->aux->extable)
  360. return 0;
  361. if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
  362. BPF_MODE(insn->code) != BPF_PROBE_MEMSX)
  363. return 0;
  364. if (WARN_ON_ONCE(ctx->num_exentries >= ctx->prog->aux->num_exentries))
  365. return -EINVAL;
  366. ex = &ctx->prog->aux->extable[ctx->num_exentries];
  367. pc = (unsigned long)&ctx->image[ctx->idx - 1];
  368. offset = pc - (long)&ex->insn;
  369. if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
  370. return -ERANGE;
  371. ex->insn = offset;
  372. /*
  373. * Since the extable follows the program, the fixup offset is always
  374. * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
  375. * to keep things simple, and put the destination register in the upper
  376. * bits. We don't need to worry about buildtime or runtime sort
  377. * modifying the upper bits because the table is already sorted, and
  378. * isn't part of the main exception table.
  379. */
  380. offset = (long)&ex->fixup - (pc + LOONGARCH_INSN_SIZE);
  381. if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, offset))
  382. return -ERANGE;
  383. ex->type = EX_TYPE_BPF;
  384. ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, offset) | FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
  385. ctx->num_exentries++;
  386. return 0;
  387. }
  388. static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extra_pass)
  389. {
  390. u8 tm = -1;
  391. u64 func_addr;
  392. bool func_addr_fixed, sign_extend;
  393. int i = insn - ctx->prog->insnsi;
  394. int ret, jmp_offset;
  395. const u8 code = insn->code;
  396. const u8 cond = BPF_OP(code);
  397. const u8 t1 = LOONGARCH_GPR_T1;
  398. const u8 t2 = LOONGARCH_GPR_T2;
  399. const u8 src = regmap[insn->src_reg];
  400. const u8 dst = regmap[insn->dst_reg];
  401. const s16 off = insn->off;
  402. const s32 imm = insn->imm;
  403. const bool is32 = BPF_CLASS(insn->code) == BPF_ALU || BPF_CLASS(insn->code) == BPF_JMP32;
  404. switch (code) {
  405. /* dst = src */
  406. case BPF_ALU | BPF_MOV | BPF_X:
  407. case BPF_ALU64 | BPF_MOV | BPF_X:
  408. switch (off) {
  409. case 0:
  410. move_reg(ctx, dst, src);
  411. emit_zext_32(ctx, dst, is32);
  412. break;
  413. case 8:
  414. move_reg(ctx, t1, src);
  415. emit_insn(ctx, extwb, dst, t1);
  416. emit_zext_32(ctx, dst, is32);
  417. break;
  418. case 16:
  419. move_reg(ctx, t1, src);
  420. emit_insn(ctx, extwh, dst, t1);
  421. emit_zext_32(ctx, dst, is32);
  422. break;
  423. case 32:
  424. emit_insn(ctx, addw, dst, src, LOONGARCH_GPR_ZERO);
  425. break;
  426. }
  427. break;
  428. /* dst = imm */
  429. case BPF_ALU | BPF_MOV | BPF_K:
  430. case BPF_ALU64 | BPF_MOV | BPF_K:
  431. move_imm(ctx, dst, imm, is32);
  432. break;
  433. /* dst = dst + src */
  434. case BPF_ALU | BPF_ADD | BPF_X:
  435. case BPF_ALU64 | BPF_ADD | BPF_X:
  436. emit_insn(ctx, addd, dst, dst, src);
  437. emit_zext_32(ctx, dst, is32);
  438. break;
  439. /* dst = dst + imm */
  440. case BPF_ALU | BPF_ADD | BPF_K:
  441. case BPF_ALU64 | BPF_ADD | BPF_K:
  442. if (is_signed_imm12(imm)) {
  443. emit_insn(ctx, addid, dst, dst, imm);
  444. } else {
  445. move_imm(ctx, t1, imm, is32);
  446. emit_insn(ctx, addd, dst, dst, t1);
  447. }
  448. emit_zext_32(ctx, dst, is32);
  449. break;
  450. /* dst = dst - src */
  451. case BPF_ALU | BPF_SUB | BPF_X:
  452. case BPF_ALU64 | BPF_SUB | BPF_X:
  453. emit_insn(ctx, subd, dst, dst, src);
  454. emit_zext_32(ctx, dst, is32);
  455. break;
  456. /* dst = dst - imm */
  457. case BPF_ALU | BPF_SUB | BPF_K:
  458. case BPF_ALU64 | BPF_SUB | BPF_K:
  459. if (is_signed_imm12(-imm)) {
  460. emit_insn(ctx, addid, dst, dst, -imm);
  461. } else {
  462. move_imm(ctx, t1, imm, is32);
  463. emit_insn(ctx, subd, dst, dst, t1);
  464. }
  465. emit_zext_32(ctx, dst, is32);
  466. break;
  467. /* dst = dst * src */
  468. case BPF_ALU | BPF_MUL | BPF_X:
  469. case BPF_ALU64 | BPF_MUL | BPF_X:
  470. emit_insn(ctx, muld, dst, dst, src);
  471. emit_zext_32(ctx, dst, is32);
  472. break;
  473. /* dst = dst * imm */
  474. case BPF_ALU | BPF_MUL | BPF_K:
  475. case BPF_ALU64 | BPF_MUL | BPF_K:
  476. move_imm(ctx, t1, imm, is32);
  477. emit_insn(ctx, muld, dst, dst, t1);
  478. emit_zext_32(ctx, dst, is32);
  479. break;
  480. /* dst = dst / src */
  481. case BPF_ALU | BPF_DIV | BPF_X:
  482. case BPF_ALU64 | BPF_DIV | BPF_X:
  483. if (!off) {
  484. emit_zext_32(ctx, dst, is32);
  485. move_reg(ctx, t1, src);
  486. emit_zext_32(ctx, t1, is32);
  487. emit_insn(ctx, divdu, dst, dst, t1);
  488. emit_zext_32(ctx, dst, is32);
  489. } else {
  490. emit_sext_32(ctx, dst, is32);
  491. move_reg(ctx, t1, src);
  492. emit_sext_32(ctx, t1, is32);
  493. emit_insn(ctx, divd, dst, dst, t1);
  494. emit_sext_32(ctx, dst, is32);
  495. }
  496. break;
  497. /* dst = dst / imm */
  498. case BPF_ALU | BPF_DIV | BPF_K:
  499. case BPF_ALU64 | BPF_DIV | BPF_K:
  500. if (!off) {
  501. move_imm(ctx, t1, imm, is32);
  502. emit_zext_32(ctx, dst, is32);
  503. emit_insn(ctx, divdu, dst, dst, t1);
  504. emit_zext_32(ctx, dst, is32);
  505. } else {
  506. move_imm(ctx, t1, imm, false);
  507. emit_sext_32(ctx, t1, is32);
  508. emit_sext_32(ctx, dst, is32);
  509. emit_insn(ctx, divd, dst, dst, t1);
  510. emit_sext_32(ctx, dst, is32);
  511. }
  512. break;
  513. /* dst = dst % src */
  514. case BPF_ALU | BPF_MOD | BPF_X:
  515. case BPF_ALU64 | BPF_MOD | BPF_X:
  516. if (!off) {
  517. emit_zext_32(ctx, dst, is32);
  518. move_reg(ctx, t1, src);
  519. emit_zext_32(ctx, t1, is32);
  520. emit_insn(ctx, moddu, dst, dst, t1);
  521. emit_zext_32(ctx, dst, is32);
  522. } else {
  523. emit_sext_32(ctx, dst, is32);
  524. move_reg(ctx, t1, src);
  525. emit_sext_32(ctx, t1, is32);
  526. emit_insn(ctx, modd, dst, dst, t1);
  527. emit_sext_32(ctx, dst, is32);
  528. }
  529. break;
  530. /* dst = dst % imm */
  531. case BPF_ALU | BPF_MOD | BPF_K:
  532. case BPF_ALU64 | BPF_MOD | BPF_K:
  533. if (!off) {
  534. move_imm(ctx, t1, imm, is32);
  535. emit_zext_32(ctx, dst, is32);
  536. emit_insn(ctx, moddu, dst, dst, t1);
  537. emit_zext_32(ctx, dst, is32);
  538. } else {
  539. move_imm(ctx, t1, imm, false);
  540. emit_sext_32(ctx, t1, is32);
  541. emit_sext_32(ctx, dst, is32);
  542. emit_insn(ctx, modd, dst, dst, t1);
  543. emit_sext_32(ctx, dst, is32);
  544. }
  545. break;
  546. /* dst = -dst */
  547. case BPF_ALU | BPF_NEG:
  548. case BPF_ALU64 | BPF_NEG:
  549. move_imm(ctx, t1, imm, is32);
  550. emit_insn(ctx, subd, dst, LOONGARCH_GPR_ZERO, dst);
  551. emit_zext_32(ctx, dst, is32);
  552. break;
  553. /* dst = dst & src */
  554. case BPF_ALU | BPF_AND | BPF_X:
  555. case BPF_ALU64 | BPF_AND | BPF_X:
  556. emit_insn(ctx, and, dst, dst, src);
  557. emit_zext_32(ctx, dst, is32);
  558. break;
  559. /* dst = dst & imm */
  560. case BPF_ALU | BPF_AND | BPF_K:
  561. case BPF_ALU64 | BPF_AND | BPF_K:
  562. if (is_unsigned_imm12(imm)) {
  563. emit_insn(ctx, andi, dst, dst, imm);
  564. } else {
  565. move_imm(ctx, t1, imm, is32);
  566. emit_insn(ctx, and, dst, dst, t1);
  567. }
  568. emit_zext_32(ctx, dst, is32);
  569. break;
  570. /* dst = dst | src */
  571. case BPF_ALU | BPF_OR | BPF_X:
  572. case BPF_ALU64 | BPF_OR | BPF_X:
  573. emit_insn(ctx, or, dst, dst, src);
  574. emit_zext_32(ctx, dst, is32);
  575. break;
  576. /* dst = dst | imm */
  577. case BPF_ALU | BPF_OR | BPF_K:
  578. case BPF_ALU64 | BPF_OR | BPF_K:
  579. if (is_unsigned_imm12(imm)) {
  580. emit_insn(ctx, ori, dst, dst, imm);
  581. } else {
  582. move_imm(ctx, t1, imm, is32);
  583. emit_insn(ctx, or, dst, dst, t1);
  584. }
  585. emit_zext_32(ctx, dst, is32);
  586. break;
  587. /* dst = dst ^ src */
  588. case BPF_ALU | BPF_XOR | BPF_X:
  589. case BPF_ALU64 | BPF_XOR | BPF_X:
  590. emit_insn(ctx, xor, dst, dst, src);
  591. emit_zext_32(ctx, dst, is32);
  592. break;
  593. /* dst = dst ^ imm */
  594. case BPF_ALU | BPF_XOR | BPF_K:
  595. case BPF_ALU64 | BPF_XOR | BPF_K:
  596. if (is_unsigned_imm12(imm)) {
  597. emit_insn(ctx, xori, dst, dst, imm);
  598. } else {
  599. move_imm(ctx, t1, imm, is32);
  600. emit_insn(ctx, xor, dst, dst, t1);
  601. }
  602. emit_zext_32(ctx, dst, is32);
  603. break;
  604. /* dst = dst << src (logical) */
  605. case BPF_ALU | BPF_LSH | BPF_X:
  606. emit_insn(ctx, sllw, dst, dst, src);
  607. emit_zext_32(ctx, dst, is32);
  608. break;
  609. case BPF_ALU64 | BPF_LSH | BPF_X:
  610. emit_insn(ctx, slld, dst, dst, src);
  611. break;
  612. /* dst = dst << imm (logical) */
  613. case BPF_ALU | BPF_LSH | BPF_K:
  614. emit_insn(ctx, slliw, dst, dst, imm);
  615. emit_zext_32(ctx, dst, is32);
  616. break;
  617. case BPF_ALU64 | BPF_LSH | BPF_K:
  618. emit_insn(ctx, sllid, dst, dst, imm);
  619. break;
  620. /* dst = dst >> src (logical) */
  621. case BPF_ALU | BPF_RSH | BPF_X:
  622. emit_insn(ctx, srlw, dst, dst, src);
  623. emit_zext_32(ctx, dst, is32);
  624. break;
  625. case BPF_ALU64 | BPF_RSH | BPF_X:
  626. emit_insn(ctx, srld, dst, dst, src);
  627. break;
  628. /* dst = dst >> imm (logical) */
  629. case BPF_ALU | BPF_RSH | BPF_K:
  630. emit_insn(ctx, srliw, dst, dst, imm);
  631. emit_zext_32(ctx, dst, is32);
  632. break;
  633. case BPF_ALU64 | BPF_RSH | BPF_K:
  634. emit_insn(ctx, srlid, dst, dst, imm);
  635. break;
  636. /* dst = dst >> src (arithmetic) */
  637. case BPF_ALU | BPF_ARSH | BPF_X:
  638. emit_insn(ctx, sraw, dst, dst, src);
  639. emit_zext_32(ctx, dst, is32);
  640. break;
  641. case BPF_ALU64 | BPF_ARSH | BPF_X:
  642. emit_insn(ctx, srad, dst, dst, src);
  643. break;
  644. /* dst = dst >> imm (arithmetic) */
  645. case BPF_ALU | BPF_ARSH | BPF_K:
  646. emit_insn(ctx, sraiw, dst, dst, imm);
  647. emit_zext_32(ctx, dst, is32);
  648. break;
  649. case BPF_ALU64 | BPF_ARSH | BPF_K:
  650. emit_insn(ctx, sraid, dst, dst, imm);
  651. break;
  652. /* dst = BSWAP##imm(dst) */
  653. case BPF_ALU | BPF_END | BPF_FROM_LE:
  654. switch (imm) {
  655. case 16:
  656. /* zero-extend 16 bits into 64 bits */
  657. emit_insn(ctx, bstrpickd, dst, dst, 15, 0);
  658. break;
  659. case 32:
  660. /* zero-extend 32 bits into 64 bits */
  661. emit_zext_32(ctx, dst, is32);
  662. break;
  663. case 64:
  664. /* do nothing */
  665. break;
  666. }
  667. break;
  668. case BPF_ALU | BPF_END | BPF_FROM_BE:
  669. case BPF_ALU64 | BPF_END | BPF_FROM_LE:
  670. switch (imm) {
  671. case 16:
  672. emit_insn(ctx, revb2h, dst, dst);
  673. /* zero-extend 16 bits into 64 bits */
  674. emit_insn(ctx, bstrpickd, dst, dst, 15, 0);
  675. break;
  676. case 32:
  677. emit_insn(ctx, revb2w, dst, dst);
  678. /* clear the upper 32 bits */
  679. emit_zext_32(ctx, dst, true);
  680. break;
  681. case 64:
  682. emit_insn(ctx, revbd, dst, dst);
  683. break;
  684. }
  685. break;
  686. /* PC += off if dst cond src */
  687. case BPF_JMP | BPF_JEQ | BPF_X:
  688. case BPF_JMP | BPF_JNE | BPF_X:
  689. case BPF_JMP | BPF_JGT | BPF_X:
  690. case BPF_JMP | BPF_JGE | BPF_X:
  691. case BPF_JMP | BPF_JLT | BPF_X:
  692. case BPF_JMP | BPF_JLE | BPF_X:
  693. case BPF_JMP | BPF_JSGT | BPF_X:
  694. case BPF_JMP | BPF_JSGE | BPF_X:
  695. case BPF_JMP | BPF_JSLT | BPF_X:
  696. case BPF_JMP | BPF_JSLE | BPF_X:
  697. case BPF_JMP32 | BPF_JEQ | BPF_X:
  698. case BPF_JMP32 | BPF_JNE | BPF_X:
  699. case BPF_JMP32 | BPF_JGT | BPF_X:
  700. case BPF_JMP32 | BPF_JGE | BPF_X:
  701. case BPF_JMP32 | BPF_JLT | BPF_X:
  702. case BPF_JMP32 | BPF_JLE | BPF_X:
  703. case BPF_JMP32 | BPF_JSGT | BPF_X:
  704. case BPF_JMP32 | BPF_JSGE | BPF_X:
  705. case BPF_JMP32 | BPF_JSLT | BPF_X:
  706. case BPF_JMP32 | BPF_JSLE | BPF_X:
  707. jmp_offset = bpf2la_offset(i, off, ctx);
  708. move_reg(ctx, t1, dst);
  709. move_reg(ctx, t2, src);
  710. if (is_signed_bpf_cond(BPF_OP(code))) {
  711. emit_sext_32(ctx, t1, is32);
  712. emit_sext_32(ctx, t2, is32);
  713. } else {
  714. emit_zext_32(ctx, t1, is32);
  715. emit_zext_32(ctx, t2, is32);
  716. }
  717. if (emit_cond_jmp(ctx, cond, t1, t2, jmp_offset) < 0)
  718. goto toofar;
  719. break;
  720. /* PC += off if dst cond imm */
  721. case BPF_JMP | BPF_JEQ | BPF_K:
  722. case BPF_JMP | BPF_JNE | BPF_K:
  723. case BPF_JMP | BPF_JGT | BPF_K:
  724. case BPF_JMP | BPF_JGE | BPF_K:
  725. case BPF_JMP | BPF_JLT | BPF_K:
  726. case BPF_JMP | BPF_JLE | BPF_K:
  727. case BPF_JMP | BPF_JSGT | BPF_K:
  728. case BPF_JMP | BPF_JSGE | BPF_K:
  729. case BPF_JMP | BPF_JSLT | BPF_K:
  730. case BPF_JMP | BPF_JSLE | BPF_K:
  731. case BPF_JMP32 | BPF_JEQ | BPF_K:
  732. case BPF_JMP32 | BPF_JNE | BPF_K:
  733. case BPF_JMP32 | BPF_JGT | BPF_K:
  734. case BPF_JMP32 | BPF_JGE | BPF_K:
  735. case BPF_JMP32 | BPF_JLT | BPF_K:
  736. case BPF_JMP32 | BPF_JLE | BPF_K:
  737. case BPF_JMP32 | BPF_JSGT | BPF_K:
  738. case BPF_JMP32 | BPF_JSGE | BPF_K:
  739. case BPF_JMP32 | BPF_JSLT | BPF_K:
  740. case BPF_JMP32 | BPF_JSLE | BPF_K:
  741. jmp_offset = bpf2la_offset(i, off, ctx);
  742. if (imm) {
  743. move_imm(ctx, t1, imm, false);
  744. tm = t1;
  745. } else {
  746. /* If imm is 0, simply use zero register. */
  747. tm = LOONGARCH_GPR_ZERO;
  748. }
  749. move_reg(ctx, t2, dst);
  750. if (is_signed_bpf_cond(BPF_OP(code))) {
  751. emit_sext_32(ctx, tm, is32);
  752. emit_sext_32(ctx, t2, is32);
  753. } else {
  754. emit_zext_32(ctx, tm, is32);
  755. emit_zext_32(ctx, t2, is32);
  756. }
  757. if (emit_cond_jmp(ctx, cond, t2, tm, jmp_offset) < 0)
  758. goto toofar;
  759. break;
  760. /* PC += off if dst & src */
  761. case BPF_JMP | BPF_JSET | BPF_X:
  762. case BPF_JMP32 | BPF_JSET | BPF_X:
  763. jmp_offset = bpf2la_offset(i, off, ctx);
  764. emit_insn(ctx, and, t1, dst, src);
  765. emit_zext_32(ctx, t1, is32);
  766. if (emit_cond_jmp(ctx, cond, t1, LOONGARCH_GPR_ZERO, jmp_offset) < 0)
  767. goto toofar;
  768. break;
  769. /* PC += off if dst & imm */
  770. case BPF_JMP | BPF_JSET | BPF_K:
  771. case BPF_JMP32 | BPF_JSET | BPF_K:
  772. jmp_offset = bpf2la_offset(i, off, ctx);
  773. move_imm(ctx, t1, imm, is32);
  774. emit_insn(ctx, and, t1, dst, t1);
  775. emit_zext_32(ctx, t1, is32);
  776. if (emit_cond_jmp(ctx, cond, t1, LOONGARCH_GPR_ZERO, jmp_offset) < 0)
  777. goto toofar;
  778. break;
  779. /* PC += off */
  780. case BPF_JMP | BPF_JA:
  781. case BPF_JMP32 | BPF_JA:
  782. if (BPF_CLASS(code) == BPF_JMP)
  783. jmp_offset = bpf2la_offset(i, off, ctx);
  784. else
  785. jmp_offset = bpf2la_offset(i, imm, ctx);
  786. if (emit_uncond_jmp(ctx, jmp_offset) < 0)
  787. goto toofar;
  788. break;
  789. /* function call */
  790. case BPF_JMP | BPF_CALL:
  791. mark_call(ctx);
  792. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
  793. &func_addr, &func_addr_fixed);
  794. if (ret < 0)
  795. return ret;
  796. move_addr(ctx, t1, func_addr);
  797. emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0);
  798. move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
  799. break;
  800. /* tail call */
  801. case BPF_JMP | BPF_TAIL_CALL:
  802. mark_tail_call(ctx);
  803. if (emit_bpf_tail_call(ctx) < 0)
  804. return -EINVAL;
  805. break;
  806. /* function return */
  807. case BPF_JMP | BPF_EXIT:
  808. if (i == ctx->prog->len - 1)
  809. break;
  810. jmp_offset = epilogue_offset(ctx);
  811. if (emit_uncond_jmp(ctx, jmp_offset) < 0)
  812. goto toofar;
  813. break;
  814. /* dst = imm64 */
  815. case BPF_LD | BPF_IMM | BPF_DW:
  816. {
  817. const u64 imm64 = (u64)(insn + 1)->imm << 32 | (u32)insn->imm;
  818. move_imm(ctx, dst, imm64, is32);
  819. return 1;
  820. }
  821. /* dst = *(size *)(src + off) */
  822. case BPF_LDX | BPF_MEM | BPF_B:
  823. case BPF_LDX | BPF_MEM | BPF_H:
  824. case BPF_LDX | BPF_MEM | BPF_W:
  825. case BPF_LDX | BPF_MEM | BPF_DW:
  826. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  827. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  828. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  829. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  830. /* dst_reg = (s64)*(signed size *)(src_reg + off) */
  831. case BPF_LDX | BPF_MEMSX | BPF_B:
  832. case BPF_LDX | BPF_MEMSX | BPF_H:
  833. case BPF_LDX | BPF_MEMSX | BPF_W:
  834. case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
  835. case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
  836. case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
  837. sign_extend = BPF_MODE(insn->code) == BPF_MEMSX ||
  838. BPF_MODE(insn->code) == BPF_PROBE_MEMSX;
  839. switch (BPF_SIZE(code)) {
  840. case BPF_B:
  841. if (is_signed_imm12(off)) {
  842. if (sign_extend)
  843. emit_insn(ctx, ldb, dst, src, off);
  844. else
  845. emit_insn(ctx, ldbu, dst, src, off);
  846. } else {
  847. move_imm(ctx, t1, off, is32);
  848. if (sign_extend)
  849. emit_insn(ctx, ldxb, dst, src, t1);
  850. else
  851. emit_insn(ctx, ldxbu, dst, src, t1);
  852. }
  853. break;
  854. case BPF_H:
  855. if (is_signed_imm12(off)) {
  856. if (sign_extend)
  857. emit_insn(ctx, ldh, dst, src, off);
  858. else
  859. emit_insn(ctx, ldhu, dst, src, off);
  860. } else {
  861. move_imm(ctx, t1, off, is32);
  862. if (sign_extend)
  863. emit_insn(ctx, ldxh, dst, src, t1);
  864. else
  865. emit_insn(ctx, ldxhu, dst, src, t1);
  866. }
  867. break;
  868. case BPF_W:
  869. if (is_signed_imm12(off)) {
  870. if (sign_extend)
  871. emit_insn(ctx, ldw, dst, src, off);
  872. else
  873. emit_insn(ctx, ldwu, dst, src, off);
  874. } else {
  875. move_imm(ctx, t1, off, is32);
  876. if (sign_extend)
  877. emit_insn(ctx, ldxw, dst, src, t1);
  878. else
  879. emit_insn(ctx, ldxwu, dst, src, t1);
  880. }
  881. break;
  882. case BPF_DW:
  883. move_imm(ctx, t1, off, is32);
  884. emit_insn(ctx, ldxd, dst, src, t1);
  885. break;
  886. }
  887. ret = add_exception_handler(insn, ctx, dst);
  888. if (ret)
  889. return ret;
  890. break;
  891. /* *(size *)(dst + off) = imm */
  892. case BPF_ST | BPF_MEM | BPF_B:
  893. case BPF_ST | BPF_MEM | BPF_H:
  894. case BPF_ST | BPF_MEM | BPF_W:
  895. case BPF_ST | BPF_MEM | BPF_DW:
  896. switch (BPF_SIZE(code)) {
  897. case BPF_B:
  898. move_imm(ctx, t1, imm, is32);
  899. if (is_signed_imm12(off)) {
  900. emit_insn(ctx, stb, t1, dst, off);
  901. } else {
  902. move_imm(ctx, t2, off, is32);
  903. emit_insn(ctx, stxb, t1, dst, t2);
  904. }
  905. break;
  906. case BPF_H:
  907. move_imm(ctx, t1, imm, is32);
  908. if (is_signed_imm12(off)) {
  909. emit_insn(ctx, sth, t1, dst, off);
  910. } else {
  911. move_imm(ctx, t2, off, is32);
  912. emit_insn(ctx, stxh, t1, dst, t2);
  913. }
  914. break;
  915. case BPF_W:
  916. move_imm(ctx, t1, imm, is32);
  917. if (is_signed_imm12(off)) {
  918. emit_insn(ctx, stw, t1, dst, off);
  919. } else if (is_signed_imm14(off)) {
  920. emit_insn(ctx, stptrw, t1, dst, off);
  921. } else {
  922. move_imm(ctx, t2, off, is32);
  923. emit_insn(ctx, stxw, t1, dst, t2);
  924. }
  925. break;
  926. case BPF_DW:
  927. move_imm(ctx, t1, imm, is32);
  928. if (is_signed_imm12(off)) {
  929. emit_insn(ctx, std, t1, dst, off);
  930. } else if (is_signed_imm14(off)) {
  931. emit_insn(ctx, stptrd, t1, dst, off);
  932. } else {
  933. move_imm(ctx, t2, off, is32);
  934. emit_insn(ctx, stxd, t1, dst, t2);
  935. }
  936. break;
  937. }
  938. break;
  939. /* *(size *)(dst + off) = src */
  940. case BPF_STX | BPF_MEM | BPF_B:
  941. case BPF_STX | BPF_MEM | BPF_H:
  942. case BPF_STX | BPF_MEM | BPF_W:
  943. case BPF_STX | BPF_MEM | BPF_DW:
  944. switch (BPF_SIZE(code)) {
  945. case BPF_B:
  946. if (is_signed_imm12(off)) {
  947. emit_insn(ctx, stb, src, dst, off);
  948. } else {
  949. move_imm(ctx, t1, off, is32);
  950. emit_insn(ctx, stxb, src, dst, t1);
  951. }
  952. break;
  953. case BPF_H:
  954. if (is_signed_imm12(off)) {
  955. emit_insn(ctx, sth, src, dst, off);
  956. } else {
  957. move_imm(ctx, t1, off, is32);
  958. emit_insn(ctx, stxh, src, dst, t1);
  959. }
  960. break;
  961. case BPF_W:
  962. if (is_signed_imm12(off)) {
  963. emit_insn(ctx, stw, src, dst, off);
  964. } else if (is_signed_imm14(off)) {
  965. emit_insn(ctx, stptrw, src, dst, off);
  966. } else {
  967. move_imm(ctx, t1, off, is32);
  968. emit_insn(ctx, stxw, src, dst, t1);
  969. }
  970. break;
  971. case BPF_DW:
  972. if (is_signed_imm12(off)) {
  973. emit_insn(ctx, std, src, dst, off);
  974. } else if (is_signed_imm14(off)) {
  975. emit_insn(ctx, stptrd, src, dst, off);
  976. } else {
  977. move_imm(ctx, t1, off, is32);
  978. emit_insn(ctx, stxd, src, dst, t1);
  979. }
  980. break;
  981. }
  982. break;
  983. case BPF_STX | BPF_ATOMIC | BPF_W:
  984. case BPF_STX | BPF_ATOMIC | BPF_DW:
  985. emit_atomic(insn, ctx);
  986. break;
  987. /* Speculation barrier */
  988. case BPF_ST | BPF_NOSPEC:
  989. break;
  990. default:
  991. pr_err("bpf_jit: unknown opcode %02x\n", code);
  992. return -EINVAL;
  993. }
  994. return 0;
  995. toofar:
  996. pr_info_once("bpf_jit: opcode %02x, jump too far\n", code);
  997. return -E2BIG;
  998. }
  999. static int build_body(struct jit_ctx *ctx, bool extra_pass)
  1000. {
  1001. int i;
  1002. const struct bpf_prog *prog = ctx->prog;
  1003. for (i = 0; i < prog->len; i++) {
  1004. const struct bpf_insn *insn = &prog->insnsi[i];
  1005. int ret;
  1006. if (ctx->image == NULL)
  1007. ctx->offset[i] = ctx->idx;
  1008. ret = build_insn(insn, ctx, extra_pass);
  1009. if (ret > 0) {
  1010. i++;
  1011. if (ctx->image == NULL)
  1012. ctx->offset[i] = ctx->idx;
  1013. continue;
  1014. }
  1015. if (ret)
  1016. return ret;
  1017. }
  1018. if (ctx->image == NULL)
  1019. ctx->offset[i] = ctx->idx;
  1020. return 0;
  1021. }
  1022. /* Fill space with break instructions */
  1023. static void jit_fill_hole(void *area, unsigned int size)
  1024. {
  1025. u32 *ptr;
  1026. /* We are guaranteed to have aligned memory */
  1027. for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
  1028. *ptr++ = INSN_BREAK;
  1029. }
  1030. static int validate_code(struct jit_ctx *ctx)
  1031. {
  1032. int i;
  1033. union loongarch_instruction insn;
  1034. for (i = 0; i < ctx->idx; i++) {
  1035. insn = ctx->image[i];
  1036. /* Check INSN_BREAK */
  1037. if (insn.word == INSN_BREAK)
  1038. return -1;
  1039. }
  1040. if (WARN_ON_ONCE(ctx->num_exentries != ctx->prog->aux->num_exentries))
  1041. return -1;
  1042. return 0;
  1043. }
  1044. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1045. {
  1046. bool tmp_blinded = false, extra_pass = false;
  1047. u8 *image_ptr;
  1048. int image_size, prog_size, extable_size;
  1049. struct jit_ctx ctx;
  1050. struct jit_data *jit_data;
  1051. struct bpf_binary_header *header;
  1052. struct bpf_prog *tmp, *orig_prog = prog;
  1053. /*
  1054. * If BPF JIT was not enabled then we must fall back to
  1055. * the interpreter.
  1056. */
  1057. if (!prog->jit_requested)
  1058. return orig_prog;
  1059. tmp = bpf_jit_blind_constants(prog);
  1060. /*
  1061. * If blinding was requested and we failed during blinding,
  1062. * we must fall back to the interpreter. Otherwise, we save
  1063. * the new JITed code.
  1064. */
  1065. if (IS_ERR(tmp))
  1066. return orig_prog;
  1067. if (tmp != prog) {
  1068. tmp_blinded = true;
  1069. prog = tmp;
  1070. }
  1071. jit_data = prog->aux->jit_data;
  1072. if (!jit_data) {
  1073. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  1074. if (!jit_data) {
  1075. prog = orig_prog;
  1076. goto out;
  1077. }
  1078. prog->aux->jit_data = jit_data;
  1079. }
  1080. if (jit_data->ctx.offset) {
  1081. ctx = jit_data->ctx;
  1082. image_ptr = jit_data->image;
  1083. header = jit_data->header;
  1084. extra_pass = true;
  1085. prog_size = sizeof(u32) * ctx.idx;
  1086. goto skip_init_ctx;
  1087. }
  1088. memset(&ctx, 0, sizeof(ctx));
  1089. ctx.prog = prog;
  1090. ctx.offset = kvcalloc(prog->len + 1, sizeof(u32), GFP_KERNEL);
  1091. if (ctx.offset == NULL) {
  1092. prog = orig_prog;
  1093. goto out_offset;
  1094. }
  1095. /* 1. Initial fake pass to compute ctx->idx and set ctx->flags */
  1096. build_prologue(&ctx);
  1097. if (build_body(&ctx, extra_pass)) {
  1098. prog = orig_prog;
  1099. goto out_offset;
  1100. }
  1101. ctx.epilogue_offset = ctx.idx;
  1102. build_epilogue(&ctx);
  1103. extable_size = prog->aux->num_exentries * sizeof(struct exception_table_entry);
  1104. /* Now we know the actual image size.
  1105. * As each LoongArch instruction is of length 32bit,
  1106. * we are translating number of JITed intructions into
  1107. * the size required to store these JITed code.
  1108. */
  1109. prog_size = sizeof(u32) * ctx.idx;
  1110. image_size = prog_size + extable_size;
  1111. /* Now we know the size of the structure to make */
  1112. header = bpf_jit_binary_alloc(image_size, &image_ptr,
  1113. sizeof(u32), jit_fill_hole);
  1114. if (header == NULL) {
  1115. prog = orig_prog;
  1116. goto out_offset;
  1117. }
  1118. /* 2. Now, the actual pass to generate final JIT code */
  1119. ctx.image = (union loongarch_instruction *)image_ptr;
  1120. if (extable_size)
  1121. prog->aux->extable = (void *)image_ptr + prog_size;
  1122. skip_init_ctx:
  1123. ctx.idx = 0;
  1124. ctx.num_exentries = 0;
  1125. build_prologue(&ctx);
  1126. if (build_body(&ctx, extra_pass)) {
  1127. bpf_jit_binary_free(header);
  1128. prog = orig_prog;
  1129. goto out_offset;
  1130. }
  1131. build_epilogue(&ctx);
  1132. /* 3. Extra pass to validate JITed code */
  1133. if (validate_code(&ctx)) {
  1134. bpf_jit_binary_free(header);
  1135. prog = orig_prog;
  1136. goto out_offset;
  1137. }
  1138. /* And we're done */
  1139. if (bpf_jit_enable > 1)
  1140. bpf_jit_dump(prog->len, prog_size, 2, ctx.image);
  1141. /* Update the icache */
  1142. flush_icache_range((unsigned long)header, (unsigned long)(ctx.image + ctx.idx));
  1143. if (!prog->is_func || extra_pass) {
  1144. int err;
  1145. if (extra_pass && ctx.idx != jit_data->ctx.idx) {
  1146. pr_err_once("multi-func JIT bug %d != %d\n",
  1147. ctx.idx, jit_data->ctx.idx);
  1148. goto out_free;
  1149. }
  1150. err = bpf_jit_binary_lock_ro(header);
  1151. if (err) {
  1152. pr_err_once("bpf_jit_binary_lock_ro() returned %d\n",
  1153. err);
  1154. goto out_free;
  1155. }
  1156. } else {
  1157. jit_data->ctx = ctx;
  1158. jit_data->image = image_ptr;
  1159. jit_data->header = header;
  1160. }
  1161. prog->jited = 1;
  1162. prog->jited_len = prog_size;
  1163. prog->bpf_func = (void *)ctx.image;
  1164. if (!prog->is_func || extra_pass) {
  1165. int i;
  1166. /* offset[prog->len] is the size of program */
  1167. for (i = 0; i <= prog->len; i++)
  1168. ctx.offset[i] *= LOONGARCH_INSN_SIZE;
  1169. bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
  1170. out_offset:
  1171. kvfree(ctx.offset);
  1172. kfree(jit_data);
  1173. prog->aux->jit_data = NULL;
  1174. }
  1175. out:
  1176. if (tmp_blinded)
  1177. bpf_jit_prog_release_other(prog, prog == orig_prog ? tmp : orig_prog);
  1178. out_offset = -1;
  1179. return prog;
  1180. out_free:
  1181. bpf_jit_binary_free(header);
  1182. prog->bpf_func = NULL;
  1183. prog->jited = 0;
  1184. prog->jited_len = 0;
  1185. goto out_offset;
  1186. }
  1187. /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
  1188. bool bpf_jit_supports_subprog_tailcalls(void)
  1189. {
  1190. return true;
  1191. }