bpf_jit_comp.c 23 KB

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