bpf_jit_comp.c 21 KB

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