bpf_jit_comp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * BPF JIT compiler for ARM64
  3. *
  4. * Copyright (C) 2014-2016 Zi Shen Lim <zlim.lnx@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) "bpf_jit: " fmt
  19. #include <linux/bpf.h>
  20. #include <linux/filter.h>
  21. #include <linux/printk.h>
  22. #include <linux/slab.h>
  23. #include <asm/byteorder.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/debug-monitors.h>
  26. #include <asm/set_memory.h>
  27. #include "bpf_jit.h"
  28. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
  29. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
  30. #define TCALL_CNT (MAX_BPF_JIT_REG + 2)
  31. #define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
  32. /* Map BPF registers to A64 registers */
  33. static const int bpf2a64[] = {
  34. /* return value from in-kernel function, and exit value from eBPF */
  35. [BPF_REG_0] = A64_R(7),
  36. /* arguments from eBPF program to in-kernel function */
  37. [BPF_REG_1] = A64_R(0),
  38. [BPF_REG_2] = A64_R(1),
  39. [BPF_REG_3] = A64_R(2),
  40. [BPF_REG_4] = A64_R(3),
  41. [BPF_REG_5] = A64_R(4),
  42. /* callee saved registers that in-kernel function will preserve */
  43. [BPF_REG_6] = A64_R(19),
  44. [BPF_REG_7] = A64_R(20),
  45. [BPF_REG_8] = A64_R(21),
  46. [BPF_REG_9] = A64_R(22),
  47. /* read-only frame pointer to access stack */
  48. [BPF_REG_FP] = A64_R(25),
  49. /* temporary registers for internal BPF JIT */
  50. [TMP_REG_1] = A64_R(10),
  51. [TMP_REG_2] = A64_R(11),
  52. [TMP_REG_3] = A64_R(12),
  53. /* tail_call_cnt */
  54. [TCALL_CNT] = A64_R(26),
  55. /* temporary register for blinding constants */
  56. [BPF_REG_AX] = A64_R(9),
  57. };
  58. struct jit_ctx {
  59. const struct bpf_prog *prog;
  60. int idx;
  61. int epilogue_offset;
  62. int *offset;
  63. __le32 *image;
  64. u32 stack_size;
  65. };
  66. static inline void emit(const u32 insn, struct jit_ctx *ctx)
  67. {
  68. if (ctx->image != NULL)
  69. ctx->image[ctx->idx] = cpu_to_le32(insn);
  70. ctx->idx++;
  71. }
  72. static inline void emit_a64_mov_i(const int is64, const int reg,
  73. const s32 val, struct jit_ctx *ctx)
  74. {
  75. u16 hi = val >> 16;
  76. u16 lo = val & 0xffff;
  77. if (hi & 0x8000) {
  78. if (hi == 0xffff) {
  79. emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
  80. } else {
  81. emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
  82. if (lo != 0xffff)
  83. emit(A64_MOVK(is64, reg, lo, 0), ctx);
  84. }
  85. } else {
  86. emit(A64_MOVZ(is64, reg, lo, 0), ctx);
  87. if (hi)
  88. emit(A64_MOVK(is64, reg, hi, 16), ctx);
  89. }
  90. }
  91. static int i64_i16_blocks(const u64 val, bool inverse)
  92. {
  93. return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  94. (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  95. (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  96. (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
  97. }
  98. static inline void emit_a64_mov_i64(const int reg, const u64 val,
  99. struct jit_ctx *ctx)
  100. {
  101. u64 nrm_tmp = val, rev_tmp = ~val;
  102. bool inverse;
  103. int shift;
  104. if (!(nrm_tmp >> 32))
  105. return emit_a64_mov_i(0, reg, (u32)val, ctx);
  106. inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
  107. shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
  108. (fls64(nrm_tmp) - 1)), 16), 0);
  109. if (inverse)
  110. emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
  111. else
  112. emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
  113. shift -= 16;
  114. while (shift >= 0) {
  115. if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
  116. emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
  117. shift -= 16;
  118. }
  119. }
  120. /*
  121. * This is an unoptimized 64 immediate emission used for BPF to BPF call
  122. * addresses. It will always do a full 64 bit decomposition as otherwise
  123. * more complexity in the last extra pass is required since we previously
  124. * reserved 4 instructions for the address.
  125. */
  126. static inline void emit_addr_mov_i64(const int reg, const u64 val,
  127. struct jit_ctx *ctx)
  128. {
  129. u64 tmp = val;
  130. int shift = 0;
  131. emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx);
  132. for (;shift < 48;) {
  133. tmp >>= 16;
  134. shift += 16;
  135. emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
  136. }
  137. }
  138. static inline int bpf2a64_offset(int bpf_to, int bpf_from,
  139. const struct jit_ctx *ctx)
  140. {
  141. int to = ctx->offset[bpf_to];
  142. /* -1 to account for the Branch instruction */
  143. int from = ctx->offset[bpf_from] - 1;
  144. return to - from;
  145. }
  146. static void jit_fill_hole(void *area, unsigned int size)
  147. {
  148. __le32 *ptr;
  149. /* We are guaranteed to have aligned memory. */
  150. for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
  151. *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
  152. }
  153. static inline int epilogue_offset(const struct jit_ctx *ctx)
  154. {
  155. int to = ctx->epilogue_offset;
  156. int from = ctx->idx;
  157. return to - from;
  158. }
  159. /* Stack must be multiples of 16B */
  160. #define STACK_ALIGN(sz) (((sz) + 15) & ~15)
  161. /* Tail call offset to jump into */
  162. #define PROLOGUE_OFFSET 7
  163. static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
  164. {
  165. const struct bpf_prog *prog = ctx->prog;
  166. const u8 r6 = bpf2a64[BPF_REG_6];
  167. const u8 r7 = bpf2a64[BPF_REG_7];
  168. const u8 r8 = bpf2a64[BPF_REG_8];
  169. const u8 r9 = bpf2a64[BPF_REG_9];
  170. const u8 fp = bpf2a64[BPF_REG_FP];
  171. const u8 tcc = bpf2a64[TCALL_CNT];
  172. const int idx0 = ctx->idx;
  173. int cur_offset;
  174. /*
  175. * BPF prog stack layout
  176. *
  177. * high
  178. * original A64_SP => 0:+-----+ BPF prologue
  179. * |FP/LR|
  180. * current A64_FP => -16:+-----+
  181. * | ... | callee saved registers
  182. * BPF fp register => -64:+-----+ <= (BPF_FP)
  183. * | |
  184. * | ... | BPF prog stack
  185. * | |
  186. * +-----+ <= (BPF_FP - prog->aux->stack_depth)
  187. * |RSVD | padding
  188. * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
  189. * | |
  190. * | ... | Function call stack
  191. * | |
  192. * +-----+
  193. * low
  194. *
  195. */
  196. /* Save FP and LR registers to stay align with ARM64 AAPCS */
  197. emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
  198. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  199. /* Save callee-saved registers */
  200. emit(A64_PUSH(r6, r7, A64_SP), ctx);
  201. emit(A64_PUSH(r8, r9, A64_SP), ctx);
  202. emit(A64_PUSH(fp, tcc, A64_SP), ctx);
  203. /* Set up BPF prog stack base register */
  204. emit(A64_MOV(1, fp, A64_SP), ctx);
  205. if (!ebpf_from_cbpf) {
  206. /* Initialize tail_call_cnt */
  207. emit(A64_MOVZ(1, tcc, 0, 0), ctx);
  208. cur_offset = ctx->idx - idx0;
  209. if (cur_offset != PROLOGUE_OFFSET) {
  210. pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
  211. cur_offset, PROLOGUE_OFFSET);
  212. return -1;
  213. }
  214. }
  215. ctx->stack_size = STACK_ALIGN(prog->aux->stack_depth);
  216. /* Set up function call stack */
  217. emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  218. return 0;
  219. }
  220. static int out_offset = -1; /* initialized on the first pass of build_body() */
  221. static int emit_bpf_tail_call(struct jit_ctx *ctx)
  222. {
  223. /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
  224. const u8 r2 = bpf2a64[BPF_REG_2];
  225. const u8 r3 = bpf2a64[BPF_REG_3];
  226. const u8 tmp = bpf2a64[TMP_REG_1];
  227. const u8 prg = bpf2a64[TMP_REG_2];
  228. const u8 tcc = bpf2a64[TCALL_CNT];
  229. const int idx0 = ctx->idx;
  230. #define cur_offset (ctx->idx - idx0)
  231. #define jmp_offset (out_offset - (cur_offset))
  232. size_t off;
  233. /* if (index >= array->map.max_entries)
  234. * goto out;
  235. */
  236. off = offsetof(struct bpf_array, map.max_entries);
  237. emit_a64_mov_i64(tmp, off, ctx);
  238. emit(A64_LDR32(tmp, r2, tmp), ctx);
  239. emit(A64_MOV(0, r3, r3), ctx);
  240. emit(A64_CMP(0, r3, tmp), ctx);
  241. emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
  242. /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
  243. * goto out;
  244. * tail_call_cnt++;
  245. */
  246. emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx);
  247. emit(A64_CMP(1, tcc, tmp), ctx);
  248. emit(A64_B_(A64_COND_HI, jmp_offset), ctx);
  249. emit(A64_ADD_I(1, tcc, tcc, 1), ctx);
  250. /* prog = array->ptrs[index];
  251. * if (prog == NULL)
  252. * goto out;
  253. */
  254. off = offsetof(struct bpf_array, ptrs);
  255. emit_a64_mov_i64(tmp, off, ctx);
  256. emit(A64_ADD(1, tmp, r2, tmp), ctx);
  257. emit(A64_LSL(1, prg, r3, 3), ctx);
  258. emit(A64_LDR64(prg, tmp, prg), ctx);
  259. emit(A64_CBZ(1, prg, jmp_offset), ctx);
  260. /* goto *(prog->bpf_func + prologue_offset); */
  261. off = offsetof(struct bpf_prog, bpf_func);
  262. emit_a64_mov_i64(tmp, off, ctx);
  263. emit(A64_LDR64(tmp, prg, tmp), ctx);
  264. emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx);
  265. emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  266. emit(A64_BR(tmp), ctx);
  267. /* out: */
  268. if (out_offset == -1)
  269. out_offset = cur_offset;
  270. if (cur_offset != out_offset) {
  271. pr_err_once("tail_call out_offset = %d, expected %d!\n",
  272. cur_offset, out_offset);
  273. return -1;
  274. }
  275. return 0;
  276. #undef cur_offset
  277. #undef jmp_offset
  278. }
  279. static void build_epilogue(struct jit_ctx *ctx)
  280. {
  281. const u8 r0 = bpf2a64[BPF_REG_0];
  282. const u8 r6 = bpf2a64[BPF_REG_6];
  283. const u8 r7 = bpf2a64[BPF_REG_7];
  284. const u8 r8 = bpf2a64[BPF_REG_8];
  285. const u8 r9 = bpf2a64[BPF_REG_9];
  286. const u8 fp = bpf2a64[BPF_REG_FP];
  287. /* We're done with BPF stack */
  288. emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  289. /* Restore fs (x25) and x26 */
  290. emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
  291. /* Restore callee-saved register */
  292. emit(A64_POP(r8, r9, A64_SP), ctx);
  293. emit(A64_POP(r6, r7, A64_SP), ctx);
  294. /* Restore FP/LR registers */
  295. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  296. /* Set return value */
  297. emit(A64_MOV(1, A64_R(0), r0), ctx);
  298. emit(A64_RET(A64_LR), ctx);
  299. }
  300. /* JITs an eBPF instruction.
  301. * Returns:
  302. * 0 - successfully JITed an 8-byte eBPF instruction.
  303. * >0 - successfully JITed a 16-byte eBPF instruction.
  304. * <0 - failed to JIT.
  305. */
  306. static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
  307. {
  308. const u8 code = insn->code;
  309. const u8 dst = bpf2a64[insn->dst_reg];
  310. const u8 src = bpf2a64[insn->src_reg];
  311. const u8 tmp = bpf2a64[TMP_REG_1];
  312. const u8 tmp2 = bpf2a64[TMP_REG_2];
  313. const u8 tmp3 = bpf2a64[TMP_REG_3];
  314. const s16 off = insn->off;
  315. const s32 imm = insn->imm;
  316. const int i = insn - ctx->prog->insnsi;
  317. const bool is64 = BPF_CLASS(code) == BPF_ALU64;
  318. const bool isdw = BPF_SIZE(code) == BPF_DW;
  319. u8 jmp_cond, reg;
  320. s32 jmp_offset;
  321. #define check_imm(bits, imm) do { \
  322. if ((((imm) > 0) && ((imm) >> (bits))) || \
  323. (((imm) < 0) && (~(imm) >> (bits)))) { \
  324. pr_info("[%2d] imm=%d(0x%x) out of range\n", \
  325. i, imm, imm); \
  326. return -EINVAL; \
  327. } \
  328. } while (0)
  329. #define check_imm19(imm) check_imm(19, imm)
  330. #define check_imm26(imm) check_imm(26, imm)
  331. switch (code) {
  332. /* dst = src */
  333. case BPF_ALU | BPF_MOV | BPF_X:
  334. case BPF_ALU64 | BPF_MOV | BPF_X:
  335. emit(A64_MOV(is64, dst, src), ctx);
  336. break;
  337. /* dst = dst OP src */
  338. case BPF_ALU | BPF_ADD | BPF_X:
  339. case BPF_ALU64 | BPF_ADD | BPF_X:
  340. emit(A64_ADD(is64, dst, dst, src), ctx);
  341. break;
  342. case BPF_ALU | BPF_SUB | BPF_X:
  343. case BPF_ALU64 | BPF_SUB | BPF_X:
  344. emit(A64_SUB(is64, dst, dst, src), ctx);
  345. break;
  346. case BPF_ALU | BPF_AND | BPF_X:
  347. case BPF_ALU64 | BPF_AND | BPF_X:
  348. emit(A64_AND(is64, dst, dst, src), ctx);
  349. break;
  350. case BPF_ALU | BPF_OR | BPF_X:
  351. case BPF_ALU64 | BPF_OR | BPF_X:
  352. emit(A64_ORR(is64, dst, dst, src), ctx);
  353. break;
  354. case BPF_ALU | BPF_XOR | BPF_X:
  355. case BPF_ALU64 | BPF_XOR | BPF_X:
  356. emit(A64_EOR(is64, dst, dst, src), ctx);
  357. break;
  358. case BPF_ALU | BPF_MUL | BPF_X:
  359. case BPF_ALU64 | BPF_MUL | BPF_X:
  360. emit(A64_MUL(is64, dst, dst, src), ctx);
  361. break;
  362. case BPF_ALU | BPF_DIV | BPF_X:
  363. case BPF_ALU64 | BPF_DIV | BPF_X:
  364. case BPF_ALU | BPF_MOD | BPF_X:
  365. case BPF_ALU64 | BPF_MOD | BPF_X:
  366. switch (BPF_OP(code)) {
  367. case BPF_DIV:
  368. emit(A64_UDIV(is64, dst, dst, src), ctx);
  369. break;
  370. case BPF_MOD:
  371. emit(A64_UDIV(is64, tmp, dst, src), ctx);
  372. emit(A64_MUL(is64, tmp, tmp, src), ctx);
  373. emit(A64_SUB(is64, dst, dst, tmp), ctx);
  374. break;
  375. }
  376. break;
  377. case BPF_ALU | BPF_LSH | BPF_X:
  378. case BPF_ALU64 | BPF_LSH | BPF_X:
  379. emit(A64_LSLV(is64, dst, dst, src), ctx);
  380. break;
  381. case BPF_ALU | BPF_RSH | BPF_X:
  382. case BPF_ALU64 | BPF_RSH | BPF_X:
  383. emit(A64_LSRV(is64, dst, dst, src), ctx);
  384. break;
  385. case BPF_ALU | BPF_ARSH | BPF_X:
  386. case BPF_ALU64 | BPF_ARSH | BPF_X:
  387. emit(A64_ASRV(is64, dst, dst, src), ctx);
  388. break;
  389. /* dst = -dst */
  390. case BPF_ALU | BPF_NEG:
  391. case BPF_ALU64 | BPF_NEG:
  392. emit(A64_NEG(is64, dst, dst), ctx);
  393. break;
  394. /* dst = BSWAP##imm(dst) */
  395. case BPF_ALU | BPF_END | BPF_FROM_LE:
  396. case BPF_ALU | BPF_END | BPF_FROM_BE:
  397. #ifdef CONFIG_CPU_BIG_ENDIAN
  398. if (BPF_SRC(code) == BPF_FROM_BE)
  399. goto emit_bswap_uxt;
  400. #else /* !CONFIG_CPU_BIG_ENDIAN */
  401. if (BPF_SRC(code) == BPF_FROM_LE)
  402. goto emit_bswap_uxt;
  403. #endif
  404. switch (imm) {
  405. case 16:
  406. emit(A64_REV16(is64, dst, dst), ctx);
  407. /* zero-extend 16 bits into 64 bits */
  408. emit(A64_UXTH(is64, dst, dst), ctx);
  409. break;
  410. case 32:
  411. emit(A64_REV32(is64, dst, dst), ctx);
  412. /* upper 32 bits already cleared */
  413. break;
  414. case 64:
  415. emit(A64_REV64(dst, dst), ctx);
  416. break;
  417. }
  418. break;
  419. emit_bswap_uxt:
  420. switch (imm) {
  421. case 16:
  422. /* zero-extend 16 bits into 64 bits */
  423. emit(A64_UXTH(is64, dst, dst), ctx);
  424. break;
  425. case 32:
  426. /* zero-extend 32 bits into 64 bits */
  427. emit(A64_UXTW(is64, dst, dst), ctx);
  428. break;
  429. case 64:
  430. /* nop */
  431. break;
  432. }
  433. break;
  434. /* dst = imm */
  435. case BPF_ALU | BPF_MOV | BPF_K:
  436. case BPF_ALU64 | BPF_MOV | BPF_K:
  437. emit_a64_mov_i(is64, dst, imm, ctx);
  438. break;
  439. /* dst = dst OP imm */
  440. case BPF_ALU | BPF_ADD | BPF_K:
  441. case BPF_ALU64 | BPF_ADD | BPF_K:
  442. emit_a64_mov_i(is64, tmp, imm, ctx);
  443. emit(A64_ADD(is64, dst, dst, tmp), ctx);
  444. break;
  445. case BPF_ALU | BPF_SUB | BPF_K:
  446. case BPF_ALU64 | BPF_SUB | BPF_K:
  447. emit_a64_mov_i(is64, tmp, imm, ctx);
  448. emit(A64_SUB(is64, dst, dst, tmp), ctx);
  449. break;
  450. case BPF_ALU | BPF_AND | BPF_K:
  451. case BPF_ALU64 | BPF_AND | BPF_K:
  452. emit_a64_mov_i(is64, tmp, imm, ctx);
  453. emit(A64_AND(is64, dst, dst, tmp), ctx);
  454. break;
  455. case BPF_ALU | BPF_OR | BPF_K:
  456. case BPF_ALU64 | BPF_OR | BPF_K:
  457. emit_a64_mov_i(is64, tmp, imm, ctx);
  458. emit(A64_ORR(is64, dst, dst, tmp), ctx);
  459. break;
  460. case BPF_ALU | BPF_XOR | BPF_K:
  461. case BPF_ALU64 | BPF_XOR | BPF_K:
  462. emit_a64_mov_i(is64, tmp, imm, ctx);
  463. emit(A64_EOR(is64, dst, dst, tmp), ctx);
  464. break;
  465. case BPF_ALU | BPF_MUL | BPF_K:
  466. case BPF_ALU64 | BPF_MUL | BPF_K:
  467. emit_a64_mov_i(is64, tmp, imm, ctx);
  468. emit(A64_MUL(is64, dst, dst, tmp), ctx);
  469. break;
  470. case BPF_ALU | BPF_DIV | BPF_K:
  471. case BPF_ALU64 | BPF_DIV | BPF_K:
  472. emit_a64_mov_i(is64, tmp, imm, ctx);
  473. emit(A64_UDIV(is64, dst, dst, tmp), ctx);
  474. break;
  475. case BPF_ALU | BPF_MOD | BPF_K:
  476. case BPF_ALU64 | BPF_MOD | BPF_K:
  477. emit_a64_mov_i(is64, tmp2, imm, ctx);
  478. emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
  479. emit(A64_MUL(is64, tmp, tmp, tmp2), ctx);
  480. emit(A64_SUB(is64, dst, dst, tmp), ctx);
  481. break;
  482. case BPF_ALU | BPF_LSH | BPF_K:
  483. case BPF_ALU64 | BPF_LSH | BPF_K:
  484. emit(A64_LSL(is64, dst, dst, imm), ctx);
  485. break;
  486. case BPF_ALU | BPF_RSH | BPF_K:
  487. case BPF_ALU64 | BPF_RSH | BPF_K:
  488. emit(A64_LSR(is64, dst, dst, imm), ctx);
  489. break;
  490. case BPF_ALU | BPF_ARSH | BPF_K:
  491. case BPF_ALU64 | BPF_ARSH | BPF_K:
  492. emit(A64_ASR(is64, dst, dst, imm), ctx);
  493. break;
  494. /* JUMP off */
  495. case BPF_JMP | BPF_JA:
  496. jmp_offset = bpf2a64_offset(i + off, i, ctx);
  497. check_imm26(jmp_offset);
  498. emit(A64_B(jmp_offset), ctx);
  499. break;
  500. /* IF (dst COND src) JUMP off */
  501. case BPF_JMP | BPF_JEQ | BPF_X:
  502. case BPF_JMP | BPF_JGT | BPF_X:
  503. case BPF_JMP | BPF_JLT | BPF_X:
  504. case BPF_JMP | BPF_JGE | BPF_X:
  505. case BPF_JMP | BPF_JLE | BPF_X:
  506. case BPF_JMP | BPF_JNE | BPF_X:
  507. case BPF_JMP | BPF_JSGT | BPF_X:
  508. case BPF_JMP | BPF_JSLT | BPF_X:
  509. case BPF_JMP | BPF_JSGE | BPF_X:
  510. case BPF_JMP | BPF_JSLE | BPF_X:
  511. emit(A64_CMP(1, dst, src), ctx);
  512. emit_cond_jmp:
  513. jmp_offset = bpf2a64_offset(i + off, i, ctx);
  514. check_imm19(jmp_offset);
  515. switch (BPF_OP(code)) {
  516. case BPF_JEQ:
  517. jmp_cond = A64_COND_EQ;
  518. break;
  519. case BPF_JGT:
  520. jmp_cond = A64_COND_HI;
  521. break;
  522. case BPF_JLT:
  523. jmp_cond = A64_COND_CC;
  524. break;
  525. case BPF_JGE:
  526. jmp_cond = A64_COND_CS;
  527. break;
  528. case BPF_JLE:
  529. jmp_cond = A64_COND_LS;
  530. break;
  531. case BPF_JSET:
  532. case BPF_JNE:
  533. jmp_cond = A64_COND_NE;
  534. break;
  535. case BPF_JSGT:
  536. jmp_cond = A64_COND_GT;
  537. break;
  538. case BPF_JSLT:
  539. jmp_cond = A64_COND_LT;
  540. break;
  541. case BPF_JSGE:
  542. jmp_cond = A64_COND_GE;
  543. break;
  544. case BPF_JSLE:
  545. jmp_cond = A64_COND_LE;
  546. break;
  547. default:
  548. return -EFAULT;
  549. }
  550. emit(A64_B_(jmp_cond, jmp_offset), ctx);
  551. break;
  552. case BPF_JMP | BPF_JSET | BPF_X:
  553. emit(A64_TST(1, dst, src), ctx);
  554. goto emit_cond_jmp;
  555. /* IF (dst COND imm) JUMP off */
  556. case BPF_JMP | BPF_JEQ | BPF_K:
  557. case BPF_JMP | BPF_JGT | BPF_K:
  558. case BPF_JMP | BPF_JLT | BPF_K:
  559. case BPF_JMP | BPF_JGE | BPF_K:
  560. case BPF_JMP | BPF_JLE | BPF_K:
  561. case BPF_JMP | BPF_JNE | BPF_K:
  562. case BPF_JMP | BPF_JSGT | BPF_K:
  563. case BPF_JMP | BPF_JSLT | BPF_K:
  564. case BPF_JMP | BPF_JSGE | BPF_K:
  565. case BPF_JMP | BPF_JSLE | BPF_K:
  566. emit_a64_mov_i(1, tmp, imm, ctx);
  567. emit(A64_CMP(1, dst, tmp), ctx);
  568. goto emit_cond_jmp;
  569. case BPF_JMP | BPF_JSET | BPF_K:
  570. emit_a64_mov_i(1, tmp, imm, ctx);
  571. emit(A64_TST(1, dst, tmp), ctx);
  572. goto emit_cond_jmp;
  573. /* function call */
  574. case BPF_JMP | BPF_CALL:
  575. {
  576. const u8 r0 = bpf2a64[BPF_REG_0];
  577. const u64 func = (u64)__bpf_call_base + imm;
  578. if (ctx->prog->is_func)
  579. emit_addr_mov_i64(tmp, func, ctx);
  580. else
  581. emit_a64_mov_i64(tmp, func, ctx);
  582. emit(A64_BLR(tmp), ctx);
  583. emit(A64_MOV(1, r0, A64_R(0)), ctx);
  584. break;
  585. }
  586. /* tail call */
  587. case BPF_JMP | BPF_TAIL_CALL:
  588. if (emit_bpf_tail_call(ctx))
  589. return -EFAULT;
  590. break;
  591. /* function return */
  592. case BPF_JMP | BPF_EXIT:
  593. /* Optimization: when last instruction is EXIT,
  594. simply fallthrough to epilogue. */
  595. if (i == ctx->prog->len - 1)
  596. break;
  597. jmp_offset = epilogue_offset(ctx);
  598. check_imm26(jmp_offset);
  599. emit(A64_B(jmp_offset), ctx);
  600. break;
  601. /* dst = imm64 */
  602. case BPF_LD | BPF_IMM | BPF_DW:
  603. {
  604. const struct bpf_insn insn1 = insn[1];
  605. u64 imm64;
  606. imm64 = (u64)insn1.imm << 32 | (u32)imm;
  607. emit_a64_mov_i64(dst, imm64, ctx);
  608. return 1;
  609. }
  610. /* LDX: dst = *(size *)(src + off) */
  611. case BPF_LDX | BPF_MEM | BPF_W:
  612. case BPF_LDX | BPF_MEM | BPF_H:
  613. case BPF_LDX | BPF_MEM | BPF_B:
  614. case BPF_LDX | BPF_MEM | BPF_DW:
  615. emit_a64_mov_i(1, tmp, off, ctx);
  616. switch (BPF_SIZE(code)) {
  617. case BPF_W:
  618. emit(A64_LDR32(dst, src, tmp), ctx);
  619. break;
  620. case BPF_H:
  621. emit(A64_LDRH(dst, src, tmp), ctx);
  622. break;
  623. case BPF_B:
  624. emit(A64_LDRB(dst, src, tmp), ctx);
  625. break;
  626. case BPF_DW:
  627. emit(A64_LDR64(dst, src, tmp), ctx);
  628. break;
  629. }
  630. break;
  631. /* ST: *(size *)(dst + off) = imm */
  632. case BPF_ST | BPF_MEM | BPF_W:
  633. case BPF_ST | BPF_MEM | BPF_H:
  634. case BPF_ST | BPF_MEM | BPF_B:
  635. case BPF_ST | BPF_MEM | BPF_DW:
  636. /* Load imm to a register then store it */
  637. emit_a64_mov_i(1, tmp2, off, ctx);
  638. emit_a64_mov_i(1, tmp, imm, ctx);
  639. switch (BPF_SIZE(code)) {
  640. case BPF_W:
  641. emit(A64_STR32(tmp, dst, tmp2), ctx);
  642. break;
  643. case BPF_H:
  644. emit(A64_STRH(tmp, dst, tmp2), ctx);
  645. break;
  646. case BPF_B:
  647. emit(A64_STRB(tmp, dst, tmp2), ctx);
  648. break;
  649. case BPF_DW:
  650. emit(A64_STR64(tmp, dst, tmp2), ctx);
  651. break;
  652. }
  653. break;
  654. /* STX: *(size *)(dst + off) = src */
  655. case BPF_STX | BPF_MEM | BPF_W:
  656. case BPF_STX | BPF_MEM | BPF_H:
  657. case BPF_STX | BPF_MEM | BPF_B:
  658. case BPF_STX | BPF_MEM | BPF_DW:
  659. emit_a64_mov_i(1, tmp, off, ctx);
  660. switch (BPF_SIZE(code)) {
  661. case BPF_W:
  662. emit(A64_STR32(src, dst, tmp), ctx);
  663. break;
  664. case BPF_H:
  665. emit(A64_STRH(src, dst, tmp), ctx);
  666. break;
  667. case BPF_B:
  668. emit(A64_STRB(src, dst, tmp), ctx);
  669. break;
  670. case BPF_DW:
  671. emit(A64_STR64(src, dst, tmp), ctx);
  672. break;
  673. }
  674. break;
  675. /* STX XADD: lock *(u32 *)(dst + off) += src */
  676. case BPF_STX | BPF_XADD | BPF_W:
  677. /* STX XADD: lock *(u64 *)(dst + off) += src */
  678. case BPF_STX | BPF_XADD | BPF_DW:
  679. if (!off) {
  680. reg = dst;
  681. } else {
  682. emit_a64_mov_i(1, tmp, off, ctx);
  683. emit(A64_ADD(1, tmp, tmp, dst), ctx);
  684. reg = tmp;
  685. }
  686. if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS)) {
  687. emit(A64_STADD(isdw, reg, src), ctx);
  688. } else {
  689. emit(A64_LDXR(isdw, tmp2, reg), ctx);
  690. emit(A64_ADD(isdw, tmp2, tmp2, src), ctx);
  691. emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx);
  692. jmp_offset = -3;
  693. check_imm19(jmp_offset);
  694. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  695. }
  696. break;
  697. default:
  698. pr_err_once("unknown opcode %02x\n", code);
  699. return -EINVAL;
  700. }
  701. return 0;
  702. }
  703. static int build_body(struct jit_ctx *ctx)
  704. {
  705. const struct bpf_prog *prog = ctx->prog;
  706. int i;
  707. for (i = 0; i < prog->len; i++) {
  708. const struct bpf_insn *insn = &prog->insnsi[i];
  709. int ret;
  710. ret = build_insn(insn, ctx);
  711. if (ret > 0) {
  712. i++;
  713. if (ctx->image == NULL)
  714. ctx->offset[i] = ctx->idx;
  715. continue;
  716. }
  717. if (ctx->image == NULL)
  718. ctx->offset[i] = ctx->idx;
  719. if (ret)
  720. return ret;
  721. }
  722. return 0;
  723. }
  724. static int validate_code(struct jit_ctx *ctx)
  725. {
  726. int i;
  727. for (i = 0; i < ctx->idx; i++) {
  728. u32 a64_insn = le32_to_cpu(ctx->image[i]);
  729. if (a64_insn == AARCH64_BREAK_FAULT)
  730. return -1;
  731. }
  732. return 0;
  733. }
  734. static inline void bpf_flush_icache(void *start, void *end)
  735. {
  736. flush_icache_range((unsigned long)start, (unsigned long)end);
  737. }
  738. struct arm64_jit_data {
  739. struct bpf_binary_header *header;
  740. u8 *image;
  741. struct jit_ctx ctx;
  742. };
  743. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  744. {
  745. struct bpf_prog *tmp, *orig_prog = prog;
  746. struct bpf_binary_header *header;
  747. struct arm64_jit_data *jit_data;
  748. bool was_classic = bpf_prog_was_classic(prog);
  749. bool tmp_blinded = false;
  750. bool extra_pass = false;
  751. struct jit_ctx ctx;
  752. int image_size;
  753. u8 *image_ptr;
  754. if (!prog->jit_requested)
  755. return orig_prog;
  756. tmp = bpf_jit_blind_constants(prog);
  757. /* If blinding was requested and we failed during blinding,
  758. * we must fall back to the interpreter.
  759. */
  760. if (IS_ERR(tmp))
  761. return orig_prog;
  762. if (tmp != prog) {
  763. tmp_blinded = true;
  764. prog = tmp;
  765. }
  766. jit_data = prog->aux->jit_data;
  767. if (!jit_data) {
  768. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  769. if (!jit_data) {
  770. prog = orig_prog;
  771. goto out;
  772. }
  773. prog->aux->jit_data = jit_data;
  774. }
  775. if (jit_data->ctx.offset) {
  776. ctx = jit_data->ctx;
  777. image_ptr = jit_data->image;
  778. header = jit_data->header;
  779. extra_pass = true;
  780. image_size = sizeof(u32) * ctx.idx;
  781. goto skip_init_ctx;
  782. }
  783. memset(&ctx, 0, sizeof(ctx));
  784. ctx.prog = prog;
  785. ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
  786. if (ctx.offset == NULL) {
  787. prog = orig_prog;
  788. goto out_off;
  789. }
  790. /* 1. Initial fake pass to compute ctx->idx. */
  791. /* Fake pass to fill in ctx->offset. */
  792. if (build_body(&ctx)) {
  793. prog = orig_prog;
  794. goto out_off;
  795. }
  796. if (build_prologue(&ctx, was_classic)) {
  797. prog = orig_prog;
  798. goto out_off;
  799. }
  800. ctx.epilogue_offset = ctx.idx;
  801. build_epilogue(&ctx);
  802. /* Now we know the actual image size. */
  803. image_size = sizeof(u32) * ctx.idx;
  804. header = bpf_jit_binary_alloc(image_size, &image_ptr,
  805. sizeof(u32), jit_fill_hole);
  806. if (header == NULL) {
  807. prog = orig_prog;
  808. goto out_off;
  809. }
  810. /* 2. Now, the actual pass. */
  811. ctx.image = (__le32 *)image_ptr;
  812. skip_init_ctx:
  813. ctx.idx = 0;
  814. build_prologue(&ctx, was_classic);
  815. if (build_body(&ctx)) {
  816. bpf_jit_binary_free(header);
  817. prog = orig_prog;
  818. goto out_off;
  819. }
  820. build_epilogue(&ctx);
  821. /* 3. Extra pass to validate JITed code. */
  822. if (validate_code(&ctx)) {
  823. bpf_jit_binary_free(header);
  824. prog = orig_prog;
  825. goto out_off;
  826. }
  827. /* And we're done. */
  828. if (bpf_jit_enable > 1)
  829. bpf_jit_dump(prog->len, image_size, 2, ctx.image);
  830. bpf_flush_icache(header, ctx.image + ctx.idx);
  831. if (!prog->is_func || extra_pass) {
  832. if (extra_pass && ctx.idx != jit_data->ctx.idx) {
  833. pr_err_once("multi-func JIT bug %d != %d\n",
  834. ctx.idx, jit_data->ctx.idx);
  835. bpf_jit_binary_free(header);
  836. prog->bpf_func = NULL;
  837. prog->jited = 0;
  838. goto out_off;
  839. }
  840. bpf_jit_binary_lock_ro(header);
  841. } else {
  842. jit_data->ctx = ctx;
  843. jit_data->image = image_ptr;
  844. jit_data->header = header;
  845. }
  846. prog->bpf_func = (void *)ctx.image;
  847. prog->jited = 1;
  848. prog->jited_len = image_size;
  849. if (!prog->is_func || extra_pass) {
  850. out_off:
  851. kfree(ctx.offset);
  852. kfree(jit_data);
  853. prog->aux->jit_data = NULL;
  854. }
  855. out:
  856. if (tmp_blinded)
  857. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  858. tmp : orig_prog);
  859. return prog;
  860. }