bpf_jit_comp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * eBPF JIT compiler
  4. *
  5. * Copyright 2016 Naveen N. Rao <[email protected]>
  6. * IBM Corporation
  7. *
  8. * Based on the powerpc classic BPF JIT compiler by Matt Evans
  9. */
  10. #include <linux/moduleloader.h>
  11. #include <asm/cacheflush.h>
  12. #include <asm/asm-compat.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/filter.h>
  15. #include <linux/if_vlan.h>
  16. #include <asm/kprobes.h>
  17. #include <linux/bpf.h>
  18. #include "bpf_jit.h"
  19. static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
  20. {
  21. memset32(area, BREAKPOINT_INSTRUCTION, size / 4);
  22. }
  23. /* Fix updated addresses (for subprog calls, ldimm64, et al) during extra pass */
  24. static int bpf_jit_fixup_addresses(struct bpf_prog *fp, u32 *image,
  25. struct codegen_context *ctx, u32 *addrs)
  26. {
  27. const struct bpf_insn *insn = fp->insnsi;
  28. bool func_addr_fixed;
  29. u64 func_addr;
  30. u32 tmp_idx;
  31. int i, j, ret;
  32. for (i = 0; i < fp->len; i++) {
  33. /*
  34. * During the extra pass, only the branch target addresses for
  35. * the subprog calls need to be fixed. All other instructions
  36. * can left untouched.
  37. *
  38. * The JITed image length does not change because we already
  39. * ensure that the JITed instruction sequence for these calls
  40. * are of fixed length by padding them with NOPs.
  41. */
  42. if (insn[i].code == (BPF_JMP | BPF_CALL) &&
  43. insn[i].src_reg == BPF_PSEUDO_CALL) {
  44. ret = bpf_jit_get_func_addr(fp, &insn[i], true,
  45. &func_addr,
  46. &func_addr_fixed);
  47. if (ret < 0)
  48. return ret;
  49. /*
  50. * Save ctx->idx as this would currently point to the
  51. * end of the JITed image and set it to the offset of
  52. * the instruction sequence corresponding to the
  53. * subprog call temporarily.
  54. */
  55. tmp_idx = ctx->idx;
  56. ctx->idx = addrs[i] / 4;
  57. ret = bpf_jit_emit_func_call_rel(image, ctx, func_addr);
  58. if (ret)
  59. return ret;
  60. /*
  61. * Restore ctx->idx here. This is safe as the length
  62. * of the JITed sequence remains unchanged.
  63. */
  64. ctx->idx = tmp_idx;
  65. } else if (insn[i].code == (BPF_LD | BPF_IMM | BPF_DW)) {
  66. tmp_idx = ctx->idx;
  67. ctx->idx = addrs[i] / 4;
  68. #ifdef CONFIG_PPC32
  69. PPC_LI32(bpf_to_ppc(insn[i].dst_reg) - 1, (u32)insn[i + 1].imm);
  70. PPC_LI32(bpf_to_ppc(insn[i].dst_reg), (u32)insn[i].imm);
  71. for (j = ctx->idx - addrs[i] / 4; j < 4; j++)
  72. EMIT(PPC_RAW_NOP());
  73. #else
  74. func_addr = ((u64)(u32)insn[i].imm) | (((u64)(u32)insn[i + 1].imm) << 32);
  75. PPC_LI64(bpf_to_ppc(insn[i].dst_reg), func_addr);
  76. /* overwrite rest with nops */
  77. for (j = ctx->idx - addrs[i] / 4; j < 5; j++)
  78. EMIT(PPC_RAW_NOP());
  79. #endif
  80. ctx->idx = tmp_idx;
  81. i++;
  82. }
  83. }
  84. return 0;
  85. }
  86. int bpf_jit_emit_exit_insn(u32 *image, struct codegen_context *ctx, int tmp_reg, long exit_addr)
  87. {
  88. if (!exit_addr || is_offset_in_branch_range(exit_addr - (ctx->idx * 4))) {
  89. PPC_JMP(exit_addr);
  90. } else if (ctx->alt_exit_addr) {
  91. if (WARN_ON(!is_offset_in_branch_range((long)ctx->alt_exit_addr - (ctx->idx * 4))))
  92. return -1;
  93. PPC_JMP(ctx->alt_exit_addr);
  94. } else {
  95. ctx->alt_exit_addr = ctx->idx * 4;
  96. bpf_jit_build_epilogue(image, ctx);
  97. }
  98. return 0;
  99. }
  100. struct powerpc64_jit_data {
  101. struct bpf_binary_header *header;
  102. u32 *addrs;
  103. u8 *image;
  104. u32 proglen;
  105. struct codegen_context ctx;
  106. };
  107. bool bpf_jit_needs_zext(void)
  108. {
  109. return true;
  110. }
  111. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
  112. {
  113. u32 proglen;
  114. u32 alloclen;
  115. u8 *image = NULL;
  116. u32 *code_base;
  117. u32 *addrs;
  118. struct powerpc64_jit_data *jit_data;
  119. struct codegen_context cgctx;
  120. int pass;
  121. int flen;
  122. struct bpf_binary_header *bpf_hdr;
  123. struct bpf_prog *org_fp = fp;
  124. struct bpf_prog *tmp_fp;
  125. bool bpf_blinded = false;
  126. bool extra_pass = false;
  127. u32 extable_len;
  128. u32 fixup_len;
  129. if (!fp->jit_requested)
  130. return org_fp;
  131. tmp_fp = bpf_jit_blind_constants(org_fp);
  132. if (IS_ERR(tmp_fp))
  133. return org_fp;
  134. if (tmp_fp != org_fp) {
  135. bpf_blinded = true;
  136. fp = tmp_fp;
  137. }
  138. jit_data = fp->aux->jit_data;
  139. if (!jit_data) {
  140. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  141. if (!jit_data) {
  142. fp = org_fp;
  143. goto out;
  144. }
  145. fp->aux->jit_data = jit_data;
  146. }
  147. flen = fp->len;
  148. addrs = jit_data->addrs;
  149. if (addrs) {
  150. cgctx = jit_data->ctx;
  151. image = jit_data->image;
  152. bpf_hdr = jit_data->header;
  153. proglen = jit_data->proglen;
  154. extra_pass = true;
  155. goto skip_init_ctx;
  156. }
  157. addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
  158. if (addrs == NULL) {
  159. fp = org_fp;
  160. goto out_addrs;
  161. }
  162. memset(&cgctx, 0, sizeof(struct codegen_context));
  163. bpf_jit_init_reg_mapping(&cgctx);
  164. /* Make sure that the stack is quadword aligned. */
  165. cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
  166. /* Scouting faux-generate pass 0 */
  167. if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0)) {
  168. /* We hit something illegal or unsupported. */
  169. fp = org_fp;
  170. goto out_addrs;
  171. }
  172. /*
  173. * If we have seen a tail call, we need a second pass.
  174. * This is because bpf_jit_emit_common_epilogue() is called
  175. * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
  176. * We also need a second pass if we ended up with too large
  177. * a program so as to ensure BPF_EXIT branches are in range.
  178. */
  179. if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) {
  180. cgctx.idx = 0;
  181. if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0)) {
  182. fp = org_fp;
  183. goto out_addrs;
  184. }
  185. }
  186. bpf_jit_realloc_regs(&cgctx);
  187. /*
  188. * Pretend to build prologue, given the features we've seen. This will
  189. * update ctgtx.idx as it pretends to output instructions, then we can
  190. * calculate total size from idx.
  191. */
  192. bpf_jit_build_prologue(0, &cgctx);
  193. addrs[fp->len] = cgctx.idx * 4;
  194. bpf_jit_build_epilogue(0, &cgctx);
  195. fixup_len = fp->aux->num_exentries * BPF_FIXUP_LEN * 4;
  196. extable_len = fp->aux->num_exentries * sizeof(struct exception_table_entry);
  197. proglen = cgctx.idx * 4;
  198. alloclen = proglen + FUNCTION_DESCR_SIZE + fixup_len + extable_len;
  199. bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4, bpf_jit_fill_ill_insns);
  200. if (!bpf_hdr) {
  201. fp = org_fp;
  202. goto out_addrs;
  203. }
  204. if (extable_len)
  205. fp->aux->extable = (void *)image + FUNCTION_DESCR_SIZE + proglen + fixup_len;
  206. skip_init_ctx:
  207. code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
  208. if (extra_pass) {
  209. /*
  210. * Do not touch the prologue and epilogue as they will remain
  211. * unchanged. Only fix the branch target address for subprog
  212. * calls in the body, and ldimm64 instructions.
  213. *
  214. * This does not change the offsets and lengths of the subprog
  215. * call instruction sequences and hence, the size of the JITed
  216. * image as well.
  217. */
  218. bpf_jit_fixup_addresses(fp, code_base, &cgctx, addrs);
  219. /* There is no need to perform the usual passes. */
  220. goto skip_codegen_passes;
  221. }
  222. /* Code generation passes 1-2 */
  223. for (pass = 1; pass < 3; pass++) {
  224. /* Now build the prologue, body code & epilogue for real. */
  225. cgctx.idx = 0;
  226. cgctx.alt_exit_addr = 0;
  227. bpf_jit_build_prologue(code_base, &cgctx);
  228. if (bpf_jit_build_body(fp, code_base, &cgctx, addrs, pass)) {
  229. bpf_jit_binary_free(bpf_hdr);
  230. fp = org_fp;
  231. goto out_addrs;
  232. }
  233. bpf_jit_build_epilogue(code_base, &cgctx);
  234. if (bpf_jit_enable > 1)
  235. pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
  236. proglen - (cgctx.idx * 4), cgctx.seen);
  237. }
  238. skip_codegen_passes:
  239. if (bpf_jit_enable > 1)
  240. /*
  241. * Note that we output the base address of the code_base
  242. * rather than image, since opcodes are in code_base.
  243. */
  244. bpf_jit_dump(flen, proglen, pass, code_base);
  245. #ifdef CONFIG_PPC64_ELF_ABI_V1
  246. /* Function descriptor nastiness: Address + TOC */
  247. ((u64 *)image)[0] = (u64)code_base;
  248. ((u64 *)image)[1] = local_paca->kernel_toc;
  249. #endif
  250. fp->bpf_func = (void *)image;
  251. fp->jited = 1;
  252. fp->jited_len = proglen + FUNCTION_DESCR_SIZE;
  253. bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + bpf_hdr->size);
  254. if (!fp->is_func || extra_pass) {
  255. bpf_jit_binary_lock_ro(bpf_hdr);
  256. bpf_prog_fill_jited_linfo(fp, addrs);
  257. out_addrs:
  258. kfree(addrs);
  259. kfree(jit_data);
  260. fp->aux->jit_data = NULL;
  261. } else {
  262. jit_data->addrs = addrs;
  263. jit_data->ctx = cgctx;
  264. jit_data->proglen = proglen;
  265. jit_data->image = image;
  266. jit_data->header = bpf_hdr;
  267. }
  268. out:
  269. if (bpf_blinded)
  270. bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
  271. return fp;
  272. }
  273. /*
  274. * The caller should check for (BPF_MODE(code) == BPF_PROBE_MEM) before calling
  275. * this function, as this only applies to BPF_PROBE_MEM, for now.
  276. */
  277. int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, int pass, struct codegen_context *ctx,
  278. int insn_idx, int jmp_off, int dst_reg)
  279. {
  280. off_t offset;
  281. unsigned long pc;
  282. struct exception_table_entry *ex;
  283. u32 *fixup;
  284. /* Populate extable entries only in the last pass */
  285. if (pass != 2)
  286. return 0;
  287. if (!fp->aux->extable ||
  288. WARN_ON_ONCE(ctx->exentry_idx >= fp->aux->num_exentries))
  289. return -EINVAL;
  290. pc = (unsigned long)&image[insn_idx];
  291. fixup = (void *)fp->aux->extable -
  292. (fp->aux->num_exentries * BPF_FIXUP_LEN * 4) +
  293. (ctx->exentry_idx * BPF_FIXUP_LEN * 4);
  294. fixup[0] = PPC_RAW_LI(dst_reg, 0);
  295. if (IS_ENABLED(CONFIG_PPC32))
  296. fixup[1] = PPC_RAW_LI(dst_reg - 1, 0); /* clear higher 32-bit register too */
  297. fixup[BPF_FIXUP_LEN - 1] =
  298. PPC_RAW_BRANCH((long)(pc + jmp_off) - (long)&fixup[BPF_FIXUP_LEN - 1]);
  299. ex = &fp->aux->extable[ctx->exentry_idx];
  300. offset = pc - (long)&ex->insn;
  301. if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
  302. return -ERANGE;
  303. ex->insn = offset;
  304. offset = (long)fixup - (long)&ex->fixup;
  305. if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
  306. return -ERANGE;
  307. ex->fixup = offset;
  308. ctx->exentry_idx++;
  309. return 0;
  310. }