bpf_jit_comp.c 23 KB

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