Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Naveen N. Rao | 6ac0ba5 | 2016-06-22 21:55:06 +0530 | [diff] [blame] | 2 | /* |
| 3 | * bpf_jit.h: BPF JIT compiler for PPC |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation |
Naveen N. Rao | 156d0e2 | 2016-06-22 21:55:07 +0530 | [diff] [blame] | 6 | * 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 7 | */ |
| 8 | #ifndef _BPF_JIT_H |
| 9 | #define _BPF_JIT_H |
| 10 | |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 11 | #ifndef __ASSEMBLY__ |
| 12 | |
Naveen N. Rao | 156d0e2 | 2016-06-22 21:55:07 +0530 | [diff] [blame] | 13 | #include <asm/types.h> |
Balamuruhan S | 0654186 | 2020-06-24 17:00:35 +0530 | [diff] [blame] | 14 | #include <asm/ppc-opcode.h> |
Naveen N. Rao | 156d0e2 | 2016-06-22 21:55:07 +0530 | [diff] [blame] | 15 | |
| 16 | #ifdef PPC64_ELF_ABI_v1 |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 17 | #define FUNCTION_DESCR_SIZE 24 |
Denis Kirjanov | 09ca5ab | 2015-02-17 10:04:40 +0300 | [diff] [blame] | 18 | #else |
| 19 | #define FUNCTION_DESCR_SIZE 0 |
| 20 | #endif |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 21 | |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 22 | #define PLANT_INSTR(d, idx, instr) \ |
| 23 | do { if (d) { (d)[idx] = instr; } idx++; } while (0) |
| 24 | #define EMIT(instr) PLANT_INSTR(image, ctx->idx, instr) |
| 25 | |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 26 | /* Long jump; (unconditional 'branch') */ |
| 27 | #define PPC_JMP(dest) EMIT(PPC_INST_BRANCH | \ |
| 28 | (((dest) - (ctx->idx * 4)) & 0x03fffffc)) |
| 29 | /* "cond" here covers BO:BI fields. */ |
| 30 | #define PPC_BCC_SHORT(cond, dest) EMIT(PPC_INST_BRANCH_COND | \ |
| 31 | (((cond) & 0x3ff) << 16) | \ |
| 32 | (((dest) - (ctx->idx * 4)) & \ |
| 33 | 0xfffc)) |
Naveen N. Rao | aaf2f7e | 2016-06-22 21:55:02 +0530 | [diff] [blame] | 34 | /* Sign-extended 32-bit immediate load */ |
| 35 | #define PPC_LI32(d, i) do { \ |
| 36 | if ((int)(uintptr_t)(i) >= -32768 && \ |
| 37 | (int)(uintptr_t)(i) < 32768) \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 38 | EMIT(PPC_RAW_LI(d, i)); \ |
Naveen N. Rao | aaf2f7e | 2016-06-22 21:55:02 +0530 | [diff] [blame] | 39 | else { \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 40 | EMIT(PPC_RAW_LIS(d, IMM_H(i))); \ |
Naveen N. Rao | aaf2f7e | 2016-06-22 21:55:02 +0530 | [diff] [blame] | 41 | if (IMM_L(i)) \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 42 | EMIT(PPC_RAW_ORI(d, d, IMM_L(i))); \ |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 43 | } } while(0) |
Naveen N. Rao | aaf2f7e | 2016-06-22 21:55:02 +0530 | [diff] [blame] | 44 | |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 45 | #define PPC_LI64(d, i) do { \ |
Naveen N. Rao | b1a0578 | 2016-06-22 21:55:03 +0530 | [diff] [blame] | 46 | if ((long)(i) >= -2147483648 && \ |
| 47 | (long)(i) < 2147483648) \ |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 48 | PPC_LI32(d, i); \ |
| 49 | else { \ |
Naveen N. Rao | b1a0578 | 2016-06-22 21:55:03 +0530 | [diff] [blame] | 50 | if (!((uintptr_t)(i) & 0xffff800000000000ULL)) \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 51 | EMIT(PPC_RAW_LI(d, ((uintptr_t)(i) >> 32) & \ |
| 52 | 0xffff)); \ |
Naveen N. Rao | b1a0578 | 2016-06-22 21:55:03 +0530 | [diff] [blame] | 53 | else { \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 54 | EMIT(PPC_RAW_LIS(d, ((uintptr_t)(i) >> 48))); \ |
Naveen N. Rao | b1a0578 | 2016-06-22 21:55:03 +0530 | [diff] [blame] | 55 | if ((uintptr_t)(i) & 0x0000ffff00000000ULL) \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 56 | EMIT(PPC_RAW_ORI(d, d, \ |
| 57 | ((uintptr_t)(i) >> 32) & 0xffff)); \ |
Naveen N. Rao | b1a0578 | 2016-06-22 21:55:03 +0530 | [diff] [blame] | 58 | } \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 59 | EMIT(PPC_RAW_SLDI(d, d, 32)); \ |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 60 | if ((uintptr_t)(i) & 0x00000000ffff0000ULL) \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 61 | EMIT(PPC_RAW_ORIS(d, d, \ |
| 62 | ((uintptr_t)(i) >> 16) & 0xffff)); \ |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 63 | if ((uintptr_t)(i) & 0x000000000000ffffULL) \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 64 | EMIT(PPC_RAW_ORI(d, d, (uintptr_t)(i) & \ |
| 65 | 0xffff)); \ |
Naveen N. Rao | b1a0578 | 2016-06-22 21:55:03 +0530 | [diff] [blame] | 66 | } } while (0) |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 67 | |
Denis Kirjanov | 09ca5ab | 2015-02-17 10:04:40 +0300 | [diff] [blame] | 68 | #ifdef CONFIG_PPC64 |
| 69 | #define PPC_FUNC_ADDR(d,i) do { PPC_LI64(d, i); } while(0) |
| 70 | #else |
| 71 | #define PPC_FUNC_ADDR(d,i) do { PPC_LI32(d, i); } while(0) |
| 72 | #endif |
| 73 | |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 74 | static inline bool is_nearbranch(int offset) |
| 75 | { |
| 76 | return (offset < 32768) && (offset >= -32768); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * The fly in the ointment of code size changing from pass to pass is |
| 81 | * avoided by padding the short branch case with a NOP. If code size differs |
| 82 | * with different branch reaches we will have the issue of code moving from |
| 83 | * one pass to the next and will need a few passes to converge on a stable |
| 84 | * state. |
| 85 | */ |
| 86 | #define PPC_BCC(cond, dest) do { \ |
| 87 | if (is_nearbranch((dest) - (ctx->idx * 4))) { \ |
| 88 | PPC_BCC_SHORT(cond, dest); \ |
Balamuruhan S | 3a18123 | 2020-06-24 17:00:36 +0530 | [diff] [blame] | 89 | EMIT(PPC_RAW_NOP()); \ |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 90 | } else { \ |
| 91 | /* Flip the 'T or F' bit to invert comparison */ \ |
| 92 | PPC_BCC_SHORT(cond ^ COND_CMP_TRUE, (ctx->idx+2)*4); \ |
| 93 | PPC_JMP(dest); \ |
| 94 | } } while(0) |
| 95 | |
| 96 | /* To create a branch condition, select a bit of cr0... */ |
| 97 | #define CR0_LT 0 |
| 98 | #define CR0_GT 1 |
| 99 | #define CR0_EQ 2 |
| 100 | /* ...and modify BO[3] */ |
| 101 | #define COND_CMP_TRUE 0x100 |
| 102 | #define COND_CMP_FALSE 0x000 |
| 103 | /* Together, they make all required comparisons: */ |
| 104 | #define COND_GT (CR0_GT | COND_CMP_TRUE) |
| 105 | #define COND_GE (CR0_LT | COND_CMP_FALSE) |
| 106 | #define COND_EQ (CR0_EQ | COND_CMP_TRUE) |
| 107 | #define COND_NE (CR0_EQ | COND_CMP_FALSE) |
| 108 | #define COND_LT (CR0_LT | COND_CMP_TRUE) |
Daniel Borkmann | 20dbf5c | 2017-08-10 01:40:00 +0200 | [diff] [blame] | 109 | #define COND_LE (CR0_GT | COND_CMP_FALSE) |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 110 | |
Christophe Leroy | c426810 | 2021-03-22 16:37:50 +0000 | [diff] [blame^] | 111 | #define SEEN_FUNC 0x20000000 /* might call external helpers */ |
| 112 | #define SEEN_STACK 0x40000000 /* uses BPF stack */ |
| 113 | #define SEEN_TAILCALL 0x80000000 /* uses tail calls */ |
Christophe Leroy | f1b1583 | 2021-03-22 16:37:48 +0000 | [diff] [blame] | 114 | |
| 115 | struct codegen_context { |
| 116 | /* |
| 117 | * This is used to track register usage as well |
| 118 | * as calls to external helpers. |
| 119 | * - register usage is tracked with corresponding |
Christophe Leroy | c426810 | 2021-03-22 16:37:50 +0000 | [diff] [blame^] | 120 | * bits (r3-r31) |
Christophe Leroy | f1b1583 | 2021-03-22 16:37:48 +0000 | [diff] [blame] | 121 | * - rest of the bits can be used to track other |
Christophe Leroy | c426810 | 2021-03-22 16:37:50 +0000 | [diff] [blame^] | 122 | * things -- for now, we use bits 0 to 2 |
Christophe Leroy | f1b1583 | 2021-03-22 16:37:48 +0000 | [diff] [blame] | 123 | * encoded in SEEN_* macros above |
| 124 | */ |
| 125 | unsigned int seen; |
| 126 | unsigned int idx; |
| 127 | unsigned int stack_size; |
| 128 | }; |
| 129 | |
| 130 | static inline void bpf_flush_icache(void *start, void *end) |
| 131 | { |
| 132 | smp_wmb(); /* smp write barrier */ |
| 133 | flush_icache_range((unsigned long)start, (unsigned long)end); |
| 134 | } |
| 135 | |
| 136 | static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i) |
| 137 | { |
| 138 | return ctx->seen & (1 << (31 - i)); |
| 139 | } |
| 140 | |
| 141 | static inline void bpf_set_seen_register(struct codegen_context *ctx, int i) |
| 142 | { |
| 143 | ctx->seen |= 1 << (31 - i); |
| 144 | } |
| 145 | |
Christophe Leroy | 4ea76e9 | 2021-03-22 16:37:49 +0000 | [diff] [blame] | 146 | void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 func); |
| 147 | int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *ctx, |
| 148 | u32 *addrs, bool extra_pass); |
| 149 | void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx); |
| 150 | void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx); |
| 151 | |
Matt Evans | 0ca87f0 | 2011-07-20 15:51:00 +0000 | [diff] [blame] | 152 | #endif |
| 153 | |
| 154 | #endif |