bpf_jit_comp.c 23 KB

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