1b886d83cSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 26ac0ba5aSNaveen N. Rao /* 36ac0ba5aSNaveen N. Rao * bpf_jit.h: BPF JIT compiler for PPC 40ca87f05SMatt Evans * 50ca87f05SMatt Evans * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation 6156d0e29SNaveen N. Rao * 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> 70ca87f05SMatt Evans */ 80ca87f05SMatt Evans #ifndef _BPF_JIT_H 90ca87f05SMatt Evans #define _BPF_JIT_H 100ca87f05SMatt Evans 110ca87f05SMatt Evans #ifndef __ASSEMBLY__ 120ca87f05SMatt Evans 13156d0e29SNaveen N. Rao #include <asm/types.h> 1406541865SBalamuruhan S #include <asm/ppc-opcode.h> 15156d0e29SNaveen N. Rao 16156d0e29SNaveen N. Rao #ifdef PPC64_ELF_ABI_v1 170ca87f05SMatt Evans #define FUNCTION_DESCR_SIZE 24 1809ca5ab2SDenis Kirjanov #else 1909ca5ab2SDenis Kirjanov #define FUNCTION_DESCR_SIZE 0 2009ca5ab2SDenis Kirjanov #endif 210ca87f05SMatt Evans 220ca87f05SMatt Evans #define PLANT_INSTR(d, idx, instr) \ 230ca87f05SMatt Evans do { if (d) { (d)[idx] = instr; } idx++; } while (0) 240ca87f05SMatt Evans #define EMIT(instr) PLANT_INSTR(image, ctx->idx, instr) 250ca87f05SMatt Evans 260ca87f05SMatt Evans /* Long jump; (unconditional 'branch') */ 270ca87f05SMatt Evans #define PPC_JMP(dest) EMIT(PPC_INST_BRANCH | \ 280ca87f05SMatt Evans (((dest) - (ctx->idx * 4)) & 0x03fffffc)) 290ca87f05SMatt Evans /* "cond" here covers BO:BI fields. */ 300ca87f05SMatt Evans #define PPC_BCC_SHORT(cond, dest) EMIT(PPC_INST_BRANCH_COND | \ 310ca87f05SMatt Evans (((cond) & 0x3ff) << 16) | \ 320ca87f05SMatt Evans (((dest) - (ctx->idx * 4)) & \ 330ca87f05SMatt Evans 0xfffc)) 34aaf2f7e0SNaveen N. Rao /* Sign-extended 32-bit immediate load */ 35aaf2f7e0SNaveen N. Rao #define PPC_LI32(d, i) do { \ 36aaf2f7e0SNaveen N. Rao if ((int)(uintptr_t)(i) >= -32768 && \ 37aaf2f7e0SNaveen N. Rao (int)(uintptr_t)(i) < 32768) \ 383a181237SBalamuruhan S EMIT(PPC_RAW_LI(d, i)); \ 39aaf2f7e0SNaveen N. Rao else { \ 403a181237SBalamuruhan S EMIT(PPC_RAW_LIS(d, IMM_H(i))); \ 41aaf2f7e0SNaveen N. Rao if (IMM_L(i)) \ 423a181237SBalamuruhan S EMIT(PPC_RAW_ORI(d, d, IMM_L(i))); \ 430ca87f05SMatt Evans } } while(0) 44aaf2f7e0SNaveen N. Rao 4551c66ad8SChristophe Leroy #ifdef CONFIG_PPC32 4651c66ad8SChristophe Leroy #define PPC_EX32(r, i) EMIT(PPC_RAW_LI((r), (i) < 0 ? -1 : 0)) 4751c66ad8SChristophe Leroy #endif 4851c66ad8SChristophe Leroy 490ca87f05SMatt Evans #define PPC_LI64(d, i) do { \ 50b1a05787SNaveen N. Rao if ((long)(i) >= -2147483648 && \ 51b1a05787SNaveen N. Rao (long)(i) < 2147483648) \ 520ca87f05SMatt Evans PPC_LI32(d, i); \ 530ca87f05SMatt Evans else { \ 54b1a05787SNaveen N. Rao if (!((uintptr_t)(i) & 0xffff800000000000ULL)) \ 553a181237SBalamuruhan S EMIT(PPC_RAW_LI(d, ((uintptr_t)(i) >> 32) & \ 563a181237SBalamuruhan S 0xffff)); \ 57b1a05787SNaveen N. Rao else { \ 583a181237SBalamuruhan S EMIT(PPC_RAW_LIS(d, ((uintptr_t)(i) >> 48))); \ 590ca87f05SMatt Evans if ((uintptr_t)(i) & 0x0000ffff00000000ULL) \ 603a181237SBalamuruhan S EMIT(PPC_RAW_ORI(d, d, \ 613a181237SBalamuruhan S ((uintptr_t)(i) >> 32) & 0xffff)); \ 62b1a05787SNaveen N. Rao } \ 633a181237SBalamuruhan S EMIT(PPC_RAW_SLDI(d, d, 32)); \ 640ca87f05SMatt Evans if ((uintptr_t)(i) & 0x00000000ffff0000ULL) \ 653a181237SBalamuruhan S EMIT(PPC_RAW_ORIS(d, d, \ 663a181237SBalamuruhan S ((uintptr_t)(i) >> 16) & 0xffff)); \ 670ca87f05SMatt Evans if ((uintptr_t)(i) & 0x000000000000ffffULL) \ 683a181237SBalamuruhan S EMIT(PPC_RAW_ORI(d, d, (uintptr_t)(i) & \ 693a181237SBalamuruhan S 0xffff)); \ 70b1a05787SNaveen N. Rao } } while (0) 710ca87f05SMatt Evans 7209ca5ab2SDenis Kirjanov #ifdef CONFIG_PPC64 7309ca5ab2SDenis Kirjanov #define PPC_FUNC_ADDR(d,i) do { PPC_LI64(d, i); } while(0) 7409ca5ab2SDenis Kirjanov #else 7509ca5ab2SDenis Kirjanov #define PPC_FUNC_ADDR(d,i) do { PPC_LI32(d, i); } while(0) 7609ca5ab2SDenis Kirjanov #endif 7709ca5ab2SDenis Kirjanov 780ca87f05SMatt Evans static inline bool is_nearbranch(int offset) 790ca87f05SMatt Evans { 800ca87f05SMatt Evans return (offset < 32768) && (offset >= -32768); 810ca87f05SMatt Evans } 820ca87f05SMatt Evans 830ca87f05SMatt Evans /* 840ca87f05SMatt Evans * The fly in the ointment of code size changing from pass to pass is 850ca87f05SMatt Evans * avoided by padding the short branch case with a NOP. If code size differs 860ca87f05SMatt Evans * with different branch reaches we will have the issue of code moving from 870ca87f05SMatt Evans * one pass to the next and will need a few passes to converge on a stable 880ca87f05SMatt Evans * state. 890ca87f05SMatt Evans */ 900ca87f05SMatt Evans #define PPC_BCC(cond, dest) do { \ 910ca87f05SMatt Evans if (is_nearbranch((dest) - (ctx->idx * 4))) { \ 920ca87f05SMatt Evans PPC_BCC_SHORT(cond, dest); \ 933a181237SBalamuruhan S EMIT(PPC_RAW_NOP()); \ 940ca87f05SMatt Evans } else { \ 950ca87f05SMatt Evans /* Flip the 'T or F' bit to invert comparison */ \ 960ca87f05SMatt Evans PPC_BCC_SHORT(cond ^ COND_CMP_TRUE, (ctx->idx+2)*4); \ 970ca87f05SMatt Evans PPC_JMP(dest); \ 980ca87f05SMatt Evans } } while(0) 990ca87f05SMatt Evans 1000ca87f05SMatt Evans /* To create a branch condition, select a bit of cr0... */ 1010ca87f05SMatt Evans #define CR0_LT 0 1020ca87f05SMatt Evans #define CR0_GT 1 1030ca87f05SMatt Evans #define CR0_EQ 2 1040ca87f05SMatt Evans /* ...and modify BO[3] */ 1050ca87f05SMatt Evans #define COND_CMP_TRUE 0x100 1060ca87f05SMatt Evans #define COND_CMP_FALSE 0x000 1070ca87f05SMatt Evans /* Together, they make all required comparisons: */ 1080ca87f05SMatt Evans #define COND_GT (CR0_GT | COND_CMP_TRUE) 1090ca87f05SMatt Evans #define COND_GE (CR0_LT | COND_CMP_FALSE) 1100ca87f05SMatt Evans #define COND_EQ (CR0_EQ | COND_CMP_TRUE) 1110ca87f05SMatt Evans #define COND_NE (CR0_EQ | COND_CMP_FALSE) 1120ca87f05SMatt Evans #define COND_LT (CR0_LT | COND_CMP_TRUE) 11320dbf5ccSDaniel Borkmann #define COND_LE (CR0_GT | COND_CMP_FALSE) 1140ca87f05SMatt Evans 115c426810fSChristophe Leroy #define SEEN_FUNC 0x20000000 /* might call external helpers */ 116c426810fSChristophe Leroy #define SEEN_STACK 0x40000000 /* uses BPF stack */ 117c426810fSChristophe Leroy #define SEEN_TAILCALL 0x80000000 /* uses tail calls */ 118f1b1583dSChristophe Leroy 119*40272035SChristophe Leroy #define SEEN_VREG_MASK 0x1ff80000 /* Volatile registers r3-r12 */ 120*40272035SChristophe Leroy #define SEEN_NVREG_MASK 0x0003ffff /* Non volatile registers r14-r31 */ 121*40272035SChristophe Leroy 122*40272035SChristophe Leroy #ifdef CONFIG_PPC64 123*40272035SChristophe Leroy extern const int b2p[MAX_BPF_JIT_REG + 2]; 124*40272035SChristophe Leroy #else 125*40272035SChristophe Leroy extern const int b2p[MAX_BPF_JIT_REG + 1]; 126*40272035SChristophe Leroy #endif 127*40272035SChristophe Leroy 128f1b1583dSChristophe Leroy struct codegen_context { 129f1b1583dSChristophe Leroy /* 130f1b1583dSChristophe Leroy * This is used to track register usage as well 131f1b1583dSChristophe Leroy * as calls to external helpers. 132f1b1583dSChristophe Leroy * - register usage is tracked with corresponding 133c426810fSChristophe Leroy * bits (r3-r31) 134f1b1583dSChristophe Leroy * - rest of the bits can be used to track other 135c426810fSChristophe Leroy * things -- for now, we use bits 0 to 2 136f1b1583dSChristophe Leroy * encoded in SEEN_* macros above 137f1b1583dSChristophe Leroy */ 138f1b1583dSChristophe Leroy unsigned int seen; 139f1b1583dSChristophe Leroy unsigned int idx; 140f1b1583dSChristophe Leroy unsigned int stack_size; 141*40272035SChristophe Leroy int b2p[ARRAY_SIZE(b2p)]; 142f1b1583dSChristophe Leroy }; 143f1b1583dSChristophe Leroy 144f1b1583dSChristophe Leroy static inline void bpf_flush_icache(void *start, void *end) 145f1b1583dSChristophe Leroy { 146f1b1583dSChristophe Leroy smp_wmb(); /* smp write barrier */ 147f1b1583dSChristophe Leroy flush_icache_range((unsigned long)start, (unsigned long)end); 148f1b1583dSChristophe Leroy } 149f1b1583dSChristophe Leroy 150f1b1583dSChristophe Leroy static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i) 151f1b1583dSChristophe Leroy { 152f1b1583dSChristophe Leroy return ctx->seen & (1 << (31 - i)); 153f1b1583dSChristophe Leroy } 154f1b1583dSChristophe Leroy 155f1b1583dSChristophe Leroy static inline void bpf_set_seen_register(struct codegen_context *ctx, int i) 156f1b1583dSChristophe Leroy { 157f1b1583dSChristophe Leroy ctx->seen |= 1 << (31 - i); 158f1b1583dSChristophe Leroy } 159f1b1583dSChristophe Leroy 160*40272035SChristophe Leroy static inline void bpf_clear_seen_register(struct codegen_context *ctx, int i) 161*40272035SChristophe Leroy { 162*40272035SChristophe Leroy ctx->seen &= ~(1 << (31 - i)); 163*40272035SChristophe Leroy } 164*40272035SChristophe Leroy 1654ea76e90SChristophe Leroy void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 func); 1664ea76e90SChristophe Leroy int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *ctx, 1674ea76e90SChristophe Leroy u32 *addrs, bool extra_pass); 1684ea76e90SChristophe Leroy void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx); 1694ea76e90SChristophe Leroy void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx); 170*40272035SChristophe Leroy void bpf_jit_realloc_regs(struct codegen_context *ctx); 1714ea76e90SChristophe Leroy 1720ca87f05SMatt Evans #endif 1730ca87f05SMatt Evans 1740ca87f05SMatt Evans #endif 175