bpf_jit_comp.c 22 KB

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