bpf_jit_comp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * eBPF JIT compiler
  4. *
  5. * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
  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 <linux/kernel.h>
  17. #include <linux/memory.h>
  18. #include <linux/bpf.h>
  19. #include <asm/kprobes.h>
  20. #include <asm/code-patching.h>
  21. #include "bpf_jit.h"
  22. static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
  23. {
  24. memset32(area, BREAKPOINT_INSTRUCTION, size / 4);
  25. }
  26. int bpf_jit_emit_exit_insn(u32 *image, struct codegen_context *ctx, int tmp_reg, long exit_addr)
  27. {
  28. if (!exit_addr || is_offset_in_branch_range(exit_addr - (ctx->idx * 4))) {
  29. PPC_JMP(exit_addr);
  30. } else if (ctx->alt_exit_addr) {
  31. if (WARN_ON(!is_offset_in_branch_range((long)ctx->alt_exit_addr - (ctx->idx * 4))))
  32. return -1;
  33. PPC_JMP(ctx->alt_exit_addr);
  34. } else {
  35. ctx->alt_exit_addr = ctx->idx * 4;
  36. bpf_jit_build_epilogue(image, ctx);
  37. }
  38. return 0;
  39. }
  40. struct powerpc_jit_data {
  41. /* address of rw header */
  42. struct bpf_binary_header *hdr;
  43. /* address of ro final header */
  44. struct bpf_binary_header *fhdr;
  45. u32 *addrs;
  46. u8 *fimage;
  47. u32 proglen;
  48. struct codegen_context ctx;
  49. };
  50. bool bpf_jit_needs_zext(void)
  51. {
  52. return true;
  53. }
  54. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
  55. {
  56. u32 proglen;
  57. u32 alloclen;
  58. u8 *image = NULL;
  59. u32 *code_base;
  60. u32 *addrs;
  61. struct powerpc_jit_data *jit_data;
  62. struct codegen_context cgctx;
  63. int pass;
  64. int flen;
  65. struct bpf_binary_header *fhdr = NULL;
  66. struct bpf_binary_header *hdr = NULL;
  67. struct bpf_prog *org_fp = fp;
  68. struct bpf_prog *tmp_fp;
  69. bool bpf_blinded = false;
  70. bool extra_pass = false;
  71. u8 *fimage = NULL;
  72. u32 *fcode_base;
  73. u32 extable_len;
  74. u32 fixup_len;
  75. if (!fp->jit_requested)
  76. return org_fp;
  77. tmp_fp = bpf_jit_blind_constants(org_fp);
  78. if (IS_ERR(tmp_fp))
  79. return org_fp;
  80. if (tmp_fp != org_fp) {
  81. bpf_blinded = true;
  82. fp = tmp_fp;
  83. }
  84. jit_data = fp->aux->jit_data;
  85. if (!jit_data) {
  86. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  87. if (!jit_data) {
  88. fp = org_fp;
  89. goto out;
  90. }
  91. fp->aux->jit_data = jit_data;
  92. }
  93. flen = fp->len;
  94. addrs = jit_data->addrs;
  95. if (addrs) {
  96. cgctx = jit_data->ctx;
  97. /*
  98. * JIT compiled to a writable location (image/code_base) first.
  99. * It is then moved to the readonly final location (fimage/fcode_base)
  100. * using instruction patching.
  101. */
  102. fimage = jit_data->fimage;
  103. fhdr = jit_data->fhdr;
  104. proglen = jit_data->proglen;
  105. hdr = jit_data->hdr;
  106. image = (void *)hdr + ((void *)fimage - (void *)fhdr);
  107. extra_pass = true;
  108. /* During extra pass, ensure index is reset before repopulating extable entries */
  109. cgctx.exentry_idx = 0;
  110. goto skip_init_ctx;
  111. }
  112. addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
  113. if (addrs == NULL) {
  114. fp = org_fp;
  115. goto out_addrs;
  116. }
  117. memset(&cgctx, 0, sizeof(struct codegen_context));
  118. bpf_jit_init_reg_mapping(&cgctx);
  119. /* Make sure that the stack is quadword aligned. */
  120. cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
  121. /* Scouting faux-generate pass 0 */
  122. if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false)) {
  123. /* We hit something illegal or unsupported. */
  124. fp = org_fp;
  125. goto out_addrs;
  126. }
  127. /*
  128. * If we have seen a tail call, we need a second pass.
  129. * This is because bpf_jit_emit_common_epilogue() is called
  130. * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
  131. * We also need a second pass if we ended up with too large
  132. * a program so as to ensure BPF_EXIT branches are in range.
  133. */
  134. if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) {
  135. cgctx.idx = 0;
  136. if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false)) {
  137. fp = org_fp;
  138. goto out_addrs;
  139. }
  140. }
  141. bpf_jit_realloc_regs(&cgctx);
  142. /*
  143. * Pretend to build prologue, given the features we've seen. This will
  144. * update ctgtx.idx as it pretends to output instructions, then we can
  145. * calculate total size from idx.
  146. */
  147. bpf_jit_build_prologue(NULL, &cgctx);
  148. addrs[fp->len] = cgctx.idx * 4;
  149. bpf_jit_build_epilogue(NULL, &cgctx);
  150. fixup_len = fp->aux->num_exentries * BPF_FIXUP_LEN * 4;
  151. extable_len = fp->aux->num_exentries * sizeof(struct exception_table_entry);
  152. proglen = cgctx.idx * 4;
  153. alloclen = proglen + FUNCTION_DESCR_SIZE + fixup_len + extable_len;
  154. fhdr = bpf_jit_binary_pack_alloc(alloclen, &fimage, 4, &hdr, &image,
  155. bpf_jit_fill_ill_insns);
  156. if (!fhdr) {
  157. fp = org_fp;
  158. goto out_addrs;
  159. }
  160. if (extable_len)
  161. fp->aux->extable = (void *)fimage + FUNCTION_DESCR_SIZE + proglen + fixup_len;
  162. skip_init_ctx:
  163. code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
  164. fcode_base = (u32 *)(fimage + FUNCTION_DESCR_SIZE);
  165. /* Code generation passes 1-2 */
  166. for (pass = 1; pass < 3; pass++) {
  167. /* Now build the prologue, body code & epilogue for real. */
  168. cgctx.idx = 0;
  169. cgctx.alt_exit_addr = 0;
  170. bpf_jit_build_prologue(code_base, &cgctx);
  171. if (bpf_jit_build_body(fp, code_base, fcode_base, &cgctx, addrs, pass,
  172. extra_pass)) {
  173. bpf_arch_text_copy(&fhdr->size, &hdr->size, sizeof(hdr->size));
  174. bpf_jit_binary_pack_free(fhdr, hdr);
  175. fp = org_fp;
  176. goto out_addrs;
  177. }
  178. bpf_jit_build_epilogue(code_base, &cgctx);
  179. if (bpf_jit_enable > 1)
  180. pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
  181. proglen - (cgctx.idx * 4), cgctx.seen);
  182. }
  183. if (bpf_jit_enable > 1)
  184. /*
  185. * Note that we output the base address of the code_base
  186. * rather than image, since opcodes are in code_base.
  187. */
  188. bpf_jit_dump(flen, proglen, pass, code_base);
  189. #ifdef CONFIG_PPC64_ELF_ABI_V1
  190. /* Function descriptor nastiness: Address + TOC */
  191. ((u64 *)image)[0] = (u64)fcode_base;
  192. ((u64 *)image)[1] = local_paca->kernel_toc;
  193. #endif
  194. fp->bpf_func = (void *)fimage;
  195. fp->jited = 1;
  196. fp->jited_len = proglen + FUNCTION_DESCR_SIZE;
  197. if (!fp->is_func || extra_pass) {
  198. if (bpf_jit_binary_pack_finalize(fhdr, hdr)) {
  199. fp = org_fp;
  200. goto out_addrs;
  201. }
  202. bpf_prog_fill_jited_linfo(fp, addrs);
  203. out_addrs:
  204. kfree(addrs);
  205. kfree(jit_data);
  206. fp->aux->jit_data = NULL;
  207. } else {
  208. jit_data->addrs = addrs;
  209. jit_data->ctx = cgctx;
  210. jit_data->proglen = proglen;
  211. jit_data->fimage = fimage;
  212. jit_data->fhdr = fhdr;
  213. jit_data->hdr = hdr;
  214. }
  215. out:
  216. if (bpf_blinded)
  217. bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
  218. return fp;
  219. }
  220. /*
  221. * The caller should check for (BPF_MODE(code) == BPF_PROBE_MEM) before calling
  222. * this function, as this only applies to BPF_PROBE_MEM, for now.
  223. */
  224. int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, u32 *fimage, int pass,
  225. struct codegen_context *ctx, int insn_idx, int jmp_off,
  226. int dst_reg)
  227. {
  228. off_t offset;
  229. unsigned long pc;
  230. struct exception_table_entry *ex, *ex_entry;
  231. u32 *fixup;
  232. /* Populate extable entries only in the last pass */
  233. if (pass != 2)
  234. return 0;
  235. if (!fp->aux->extable ||
  236. WARN_ON_ONCE(ctx->exentry_idx >= fp->aux->num_exentries))
  237. return -EINVAL;
  238. /*
  239. * Program is first written to image before copying to the
  240. * final location (fimage). Accordingly, update in the image first.
  241. * As all offsets used are relative, copying as is to the
  242. * final location should be alright.
  243. */
  244. pc = (unsigned long)&image[insn_idx];
  245. ex = (void *)fp->aux->extable - (void *)fimage + (void *)image;
  246. fixup = (void *)ex -
  247. (fp->aux->num_exentries * BPF_FIXUP_LEN * 4) +
  248. (ctx->exentry_idx * BPF_FIXUP_LEN * 4);
  249. fixup[0] = PPC_RAW_LI(dst_reg, 0);
  250. if (IS_ENABLED(CONFIG_PPC32))
  251. fixup[1] = PPC_RAW_LI(dst_reg - 1, 0); /* clear higher 32-bit register too */
  252. fixup[BPF_FIXUP_LEN - 1] =
  253. PPC_RAW_BRANCH((long)(pc + jmp_off) - (long)&fixup[BPF_FIXUP_LEN - 1]);
  254. ex_entry = &ex[ctx->exentry_idx];
  255. offset = pc - (long)&ex_entry->insn;
  256. if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
  257. return -ERANGE;
  258. ex_entry->insn = offset;
  259. offset = (long)fixup - (long)&ex_entry->fixup;
  260. if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
  261. return -ERANGE;
  262. ex_entry->fixup = offset;
  263. ctx->exentry_idx++;
  264. return 0;
  265. }
  266. void *bpf_arch_text_copy(void *dst, void *src, size_t len)
  267. {
  268. int err;
  269. if (WARN_ON_ONCE(core_kernel_text((unsigned long)dst)))
  270. return ERR_PTR(-EINVAL);
  271. mutex_lock(&text_mutex);
  272. err = patch_instructions(dst, src, len, false);
  273. mutex_unlock(&text_mutex);
  274. return err ? ERR_PTR(err) : dst;
  275. }
  276. int bpf_arch_text_invalidate(void *dst, size_t len)
  277. {
  278. u32 insn = BREAKPOINT_INSTRUCTION;
  279. int ret;
  280. if (WARN_ON_ONCE(core_kernel_text((unsigned long)dst)))
  281. return -EINVAL;
  282. mutex_lock(&text_mutex);
  283. ret = patch_instructions(dst, &insn, len, true);
  284. mutex_unlock(&text_mutex);
  285. return ret;
  286. }
  287. void bpf_jit_free(struct bpf_prog *fp)
  288. {
  289. if (fp->jited) {
  290. struct powerpc_jit_data *jit_data = fp->aux->jit_data;
  291. struct bpf_binary_header *hdr;
  292. /*
  293. * If we fail the final pass of JIT (from jit_subprogs),
  294. * the program may not be finalized yet. Call finalize here
  295. * before freeing it.
  296. */
  297. if (jit_data) {
  298. bpf_jit_binary_pack_finalize(jit_data->fhdr, jit_data->hdr);
  299. kvfree(jit_data->addrs);
  300. kfree(jit_data);
  301. }
  302. hdr = bpf_jit_binary_pack_hdr(fp);
  303. bpf_jit_binary_pack_free(hdr, NULL);
  304. WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
  305. }
  306. bpf_prog_unlock_free(fp);
  307. }
  308. bool bpf_jit_supports_kfunc_call(void)
  309. {
  310. return true;
  311. }
  312. bool bpf_jit_supports_far_kfunc_call(void)
  313. {
  314. return IS_ENABLED(CONFIG_PPC64);
  315. }