16ac0ba5aSNaveen N. Rao /* 26ac0ba5aSNaveen N. Rao * bpf_jit.h: BPF JIT compiler for PPC 30ca87f05SMatt Evans * 40ca87f05SMatt Evans * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation 5156d0e29SNaveen N. Rao * 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> 60ca87f05SMatt Evans * 70ca87f05SMatt Evans * This program is free software; you can redistribute it and/or 80ca87f05SMatt Evans * modify it under the terms of the GNU General Public License 90ca87f05SMatt Evans * as published by the Free Software Foundation; version 2 100ca87f05SMatt Evans * of the License. 110ca87f05SMatt Evans */ 120ca87f05SMatt Evans #ifndef _BPF_JIT_H 130ca87f05SMatt Evans #define _BPF_JIT_H 140ca87f05SMatt Evans 150ca87f05SMatt Evans #ifndef __ASSEMBLY__ 160ca87f05SMatt Evans 17156d0e29SNaveen N. Rao #include <asm/types.h> 18156d0e29SNaveen N. Rao 19156d0e29SNaveen N. Rao #ifdef PPC64_ELF_ABI_v1 200ca87f05SMatt Evans #define FUNCTION_DESCR_SIZE 24 2109ca5ab2SDenis Kirjanov #else 2209ca5ab2SDenis Kirjanov #define FUNCTION_DESCR_SIZE 0 2309ca5ab2SDenis Kirjanov #endif 240ca87f05SMatt Evans 250ca87f05SMatt Evans /* 260ca87f05SMatt Evans * 16-bit immediate helper macros: HA() is for use with sign-extending instrs 270ca87f05SMatt Evans * (e.g. LD, ADDI). If the bottom 16 bits is "-ve", add another bit into the 280ca87f05SMatt Evans * top half to negate the effect (i.e. 0xffff + 1 = 0x(1)0000). 290ca87f05SMatt Evans */ 300ca87f05SMatt Evans #define IMM_H(i) ((uintptr_t)(i)>>16) 310ca87f05SMatt Evans #define IMM_HA(i) (((uintptr_t)(i)>>16) + \ 320ca87f05SMatt Evans (((uintptr_t)(i) & 0x8000) >> 15)) 330ca87f05SMatt Evans #define IMM_L(i) ((uintptr_t)(i) & 0xffff) 340ca87f05SMatt Evans 350ca87f05SMatt Evans #define PLANT_INSTR(d, idx, instr) \ 360ca87f05SMatt Evans do { if (d) { (d)[idx] = instr; } idx++; } while (0) 370ca87f05SMatt Evans #define EMIT(instr) PLANT_INSTR(image, ctx->idx, instr) 380ca87f05SMatt Evans 390ca87f05SMatt Evans #define PPC_NOP() EMIT(PPC_INST_NOP) 400ca87f05SMatt Evans #define PPC_BLR() EMIT(PPC_INST_BLR) 410ca87f05SMatt Evans #define PPC_BLRL() EMIT(PPC_INST_BLRL) 42cdaade71SMichael Neuling #define PPC_MTLR(r) EMIT(PPC_INST_MTLR | ___PPC_RT(r)) 43ce076141SNaveen N. Rao #define PPC_BCTR() EMIT(PPC_INST_BCTR) 44ce076141SNaveen N. Rao #define PPC_MTCTR(r) EMIT(PPC_INST_MTCTR | ___PPC_RT(r)) 45cdaade71SMichael Neuling #define PPC_ADDI(d, a, i) EMIT(PPC_INST_ADDI | ___PPC_RT(d) | \ 46cdaade71SMichael Neuling ___PPC_RA(a) | IMM_L(i)) 470ca87f05SMatt Evans #define PPC_MR(d, a) PPC_OR(d, a, a) 480ca87f05SMatt Evans #define PPC_LI(r, i) PPC_ADDI(r, 0, i) 490ca87f05SMatt Evans #define PPC_ADDIS(d, a, i) EMIT(PPC_INST_ADDIS | \ 50cef1e8cdSNaveen N. Rao ___PPC_RT(d) | ___PPC_RA(a) | IMM_L(i)) 510ca87f05SMatt Evans #define PPC_LIS(r, i) PPC_ADDIS(r, 0, i) 52cdaade71SMichael Neuling #define PPC_STD(r, base, i) EMIT(PPC_INST_STD | ___PPC_RS(r) | \ 53cdaade71SMichael Neuling ___PPC_RA(base) | ((i) & 0xfffc)) 5486be36f6SNaveen N. Rao #define PPC_STDX(r, base, b) EMIT(PPC_INST_STDX | ___PPC_RS(r) | \ 5586be36f6SNaveen N. Rao ___PPC_RA(base) | ___PPC_RB(b)) 5609ca5ab2SDenis Kirjanov #define PPC_STDU(r, base, i) EMIT(PPC_INST_STDU | ___PPC_RS(r) | \ 5709ca5ab2SDenis Kirjanov ___PPC_RA(base) | ((i) & 0xfffc)) 5809ca5ab2SDenis Kirjanov #define PPC_STW(r, base, i) EMIT(PPC_INST_STW | ___PPC_RS(r) | \ 59cef1e8cdSNaveen N. Rao ___PPC_RA(base) | IMM_L(i)) 6009ca5ab2SDenis Kirjanov #define PPC_STWU(r, base, i) EMIT(PPC_INST_STWU | ___PPC_RS(r) | \ 61cef1e8cdSNaveen N. Rao ___PPC_RA(base) | IMM_L(i)) 62156d0e29SNaveen N. Rao #define PPC_STH(r, base, i) EMIT(PPC_INST_STH | ___PPC_RS(r) | \ 63156d0e29SNaveen N. Rao ___PPC_RA(base) | IMM_L(i)) 64156d0e29SNaveen N. Rao #define PPC_STB(r, base, i) EMIT(PPC_INST_STB | ___PPC_RS(r) | \ 65156d0e29SNaveen N. Rao ___PPC_RA(base) | IMM_L(i)) 664e235761SDenis Kirjanov 674e235761SDenis Kirjanov #define PPC_LBZ(r, base, i) EMIT(PPC_INST_LBZ | ___PPC_RT(r) | \ 684e235761SDenis Kirjanov ___PPC_RA(base) | IMM_L(i)) 69cdaade71SMichael Neuling #define PPC_LD(r, base, i) EMIT(PPC_INST_LD | ___PPC_RT(r) | \ 7086be36f6SNaveen N. Rao ___PPC_RA(base) | ((i) & 0xfffc)) 7186be36f6SNaveen N. Rao #define PPC_LDX(r, base, b) EMIT(PPC_INST_LDX | ___PPC_RT(r) | \ 7286be36f6SNaveen N. Rao ___PPC_RA(base) | ___PPC_RB(b)) 73cdaade71SMichael Neuling #define PPC_LWZ(r, base, i) EMIT(PPC_INST_LWZ | ___PPC_RT(r) | \ 74cdaade71SMichael Neuling ___PPC_RA(base) | IMM_L(i)) 75cdaade71SMichael Neuling #define PPC_LHZ(r, base, i) EMIT(PPC_INST_LHZ | ___PPC_RT(r) | \ 76cdaade71SMichael Neuling ___PPC_RA(base) | IMM_L(i)) 779c662cadSPhilippe Bergheaud #define PPC_LHBRX(r, base, b) EMIT(PPC_INST_LHBRX | ___PPC_RT(r) | \ 789c662cadSPhilippe Bergheaud ___PPC_RA(base) | ___PPC_RB(b)) 79156d0e29SNaveen N. Rao #define PPC_LDBRX(r, base, b) EMIT(PPC_INST_LDBRX | ___PPC_RT(r) | \ 80156d0e29SNaveen N. Rao ___PPC_RA(base) | ___PPC_RB(b)) 81156d0e29SNaveen N. Rao 82156d0e29SNaveen N. Rao #define PPC_BPF_LDARX(t, a, b, eh) EMIT(PPC_INST_LDARX | ___PPC_RT(t) | \ 83156d0e29SNaveen N. Rao ___PPC_RA(a) | ___PPC_RB(b) | \ 84156d0e29SNaveen N. Rao __PPC_EH(eh)) 85156d0e29SNaveen N. Rao #define PPC_BPF_LWARX(t, a, b, eh) EMIT(PPC_INST_LWARX | ___PPC_RT(t) | \ 86156d0e29SNaveen N. Rao ___PPC_RA(a) | ___PPC_RB(b) | \ 87156d0e29SNaveen N. Rao __PPC_EH(eh)) 88156d0e29SNaveen N. Rao #define PPC_BPF_STWCX(s, a, b) EMIT(PPC_INST_STWCX | ___PPC_RS(s) | \ 89156d0e29SNaveen N. Rao ___PPC_RA(a) | ___PPC_RB(b)) 90156d0e29SNaveen N. Rao #define PPC_BPF_STDCX(s, a, b) EMIT(PPC_INST_STDCX | ___PPC_RS(s) | \ 91156d0e29SNaveen N. Rao ___PPC_RA(a) | ___PPC_RB(b)) 92cdaade71SMichael Neuling #define PPC_CMPWI(a, i) EMIT(PPC_INST_CMPWI | ___PPC_RA(a) | IMM_L(i)) 93cdaade71SMichael Neuling #define PPC_CMPDI(a, i) EMIT(PPC_INST_CMPDI | ___PPC_RA(a) | IMM_L(i)) 94156d0e29SNaveen N. Rao #define PPC_CMPW(a, b) EMIT(PPC_INST_CMPW | ___PPC_RA(a) | \ 95156d0e29SNaveen N. Rao ___PPC_RB(b)) 96156d0e29SNaveen N. Rao #define PPC_CMPD(a, b) EMIT(PPC_INST_CMPD | ___PPC_RA(a) | \ 97156d0e29SNaveen N. Rao ___PPC_RB(b)) 98cdaade71SMichael Neuling #define PPC_CMPLWI(a, i) EMIT(PPC_INST_CMPLWI | ___PPC_RA(a) | IMM_L(i)) 99156d0e29SNaveen N. Rao #define PPC_CMPLDI(a, i) EMIT(PPC_INST_CMPLDI | ___PPC_RA(a) | IMM_L(i)) 100cef1e8cdSNaveen N. Rao #define PPC_CMPLW(a, b) EMIT(PPC_INST_CMPLW | ___PPC_RA(a) | \ 101cef1e8cdSNaveen N. Rao ___PPC_RB(b)) 102156d0e29SNaveen N. Rao #define PPC_CMPLD(a, b) EMIT(PPC_INST_CMPLD | ___PPC_RA(a) | \ 103156d0e29SNaveen N. Rao ___PPC_RB(b)) 1040ca87f05SMatt Evans 105cdaade71SMichael Neuling #define PPC_SUB(d, a, b) EMIT(PPC_INST_SUB | ___PPC_RT(d) | \ 106cdaade71SMichael Neuling ___PPC_RB(a) | ___PPC_RA(b)) 107cdaade71SMichael Neuling #define PPC_ADD(d, a, b) EMIT(PPC_INST_ADD | ___PPC_RT(d) | \ 108cdaade71SMichael Neuling ___PPC_RA(a) | ___PPC_RB(b)) 109156d0e29SNaveen N. Rao #define PPC_MULD(d, a, b) EMIT(PPC_INST_MULLD | ___PPC_RT(d) | \ 110156d0e29SNaveen N. Rao ___PPC_RA(a) | ___PPC_RB(b)) 111cef1e8cdSNaveen N. Rao #define PPC_MULW(d, a, b) EMIT(PPC_INST_MULLW | ___PPC_RT(d) | \ 112cdaade71SMichael Neuling ___PPC_RA(a) | ___PPC_RB(b)) 113cdaade71SMichael Neuling #define PPC_MULHWU(d, a, b) EMIT(PPC_INST_MULHWU | ___PPC_RT(d) | \ 114cdaade71SMichael Neuling ___PPC_RA(a) | ___PPC_RB(b)) 115cdaade71SMichael Neuling #define PPC_MULI(d, a, i) EMIT(PPC_INST_MULLI | ___PPC_RT(d) | \ 116cdaade71SMichael Neuling ___PPC_RA(a) | IMM_L(i)) 117cdaade71SMichael Neuling #define PPC_DIVWU(d, a, b) EMIT(PPC_INST_DIVWU | ___PPC_RT(d) | \ 118cdaade71SMichael Neuling ___PPC_RA(a) | ___PPC_RB(b)) 119156d0e29SNaveen N. Rao #define PPC_DIVD(d, a, b) EMIT(PPC_INST_DIVD | ___PPC_RT(d) | \ 120156d0e29SNaveen N. Rao ___PPC_RA(a) | ___PPC_RB(b)) 121cdaade71SMichael Neuling #define PPC_AND(d, a, b) EMIT(PPC_INST_AND | ___PPC_RA(d) | \ 122cdaade71SMichael Neuling ___PPC_RS(a) | ___PPC_RB(b)) 123cdaade71SMichael Neuling #define PPC_ANDI(d, a, i) EMIT(PPC_INST_ANDI | ___PPC_RA(d) | \ 124cdaade71SMichael Neuling ___PPC_RS(a) | IMM_L(i)) 125cdaade71SMichael Neuling #define PPC_AND_DOT(d, a, b) EMIT(PPC_INST_ANDDOT | ___PPC_RA(d) | \ 126cdaade71SMichael Neuling ___PPC_RS(a) | ___PPC_RB(b)) 127cdaade71SMichael Neuling #define PPC_OR(d, a, b) EMIT(PPC_INST_OR | ___PPC_RA(d) | \ 128cdaade71SMichael Neuling ___PPC_RS(a) | ___PPC_RB(b)) 129156d0e29SNaveen N. Rao #define PPC_MR(d, a) PPC_OR(d, a, a) 130cdaade71SMichael Neuling #define PPC_ORI(d, a, i) EMIT(PPC_INST_ORI | ___PPC_RA(d) | \ 131cdaade71SMichael Neuling ___PPC_RS(a) | IMM_L(i)) 132cdaade71SMichael Neuling #define PPC_ORIS(d, a, i) EMIT(PPC_INST_ORIS | ___PPC_RA(d) | \ 133cdaade71SMichael Neuling ___PPC_RS(a) | IMM_L(i)) 13402871903SDaniel Borkmann #define PPC_XOR(d, a, b) EMIT(PPC_INST_XOR | ___PPC_RA(d) | \ 13502871903SDaniel Borkmann ___PPC_RS(a) | ___PPC_RB(b)) 13602871903SDaniel Borkmann #define PPC_XORI(d, a, i) EMIT(PPC_INST_XORI | ___PPC_RA(d) | \ 13702871903SDaniel Borkmann ___PPC_RS(a) | IMM_L(i)) 13802871903SDaniel Borkmann #define PPC_XORIS(d, a, i) EMIT(PPC_INST_XORIS | ___PPC_RA(d) | \ 13902871903SDaniel Borkmann ___PPC_RS(a) | IMM_L(i)) 140156d0e29SNaveen N. Rao #define PPC_EXTSW(d, a) EMIT(PPC_INST_EXTSW | ___PPC_RA(d) | \ 141156d0e29SNaveen N. Rao ___PPC_RS(a)) 142cdaade71SMichael Neuling #define PPC_SLW(d, a, s) EMIT(PPC_INST_SLW | ___PPC_RA(d) | \ 143cdaade71SMichael Neuling ___PPC_RS(a) | ___PPC_RB(s)) 144156d0e29SNaveen N. Rao #define PPC_SLD(d, a, s) EMIT(PPC_INST_SLD | ___PPC_RA(d) | \ 145156d0e29SNaveen N. Rao ___PPC_RS(a) | ___PPC_RB(s)) 146cdaade71SMichael Neuling #define PPC_SRW(d, a, s) EMIT(PPC_INST_SRW | ___PPC_RA(d) | \ 147cdaade71SMichael Neuling ___PPC_RS(a) | ___PPC_RB(s)) 14844cf43c0SJiong Wang #define PPC_SRAW(d, a, s) EMIT(PPC_INST_SRAW | ___PPC_RA(d) | \ 14944cf43c0SJiong Wang ___PPC_RS(a) | ___PPC_RB(s)) 15044cf43c0SJiong Wang #define PPC_SRAWI(d, a, i) EMIT(PPC_INST_SRAWI | ___PPC_RA(d) | \ 15144cf43c0SJiong Wang ___PPC_RS(a) | __PPC_SH(i)) 152156d0e29SNaveen N. Rao #define PPC_SRD(d, a, s) EMIT(PPC_INST_SRD | ___PPC_RA(d) | \ 153156d0e29SNaveen N. Rao ___PPC_RS(a) | ___PPC_RB(s)) 154156d0e29SNaveen N. Rao #define PPC_SRAD(d, a, s) EMIT(PPC_INST_SRAD | ___PPC_RA(d) | \ 155156d0e29SNaveen N. Rao ___PPC_RS(a) | ___PPC_RB(s)) 156156d0e29SNaveen N. Rao #define PPC_SRADI(d, a, i) EMIT(PPC_INST_SRADI | ___PPC_RA(d) | \ 157c233f597SNaveen N. Rao ___PPC_RS(a) | __PPC_SH64(i)) 158277285b8SNaveen N. Rao #define PPC_RLWINM(d, a, i, mb, me) EMIT(PPC_INST_RLWINM | ___PPC_RA(d) | \ 159277285b8SNaveen N. Rao ___PPC_RS(a) | __PPC_SH(i) | \ 160277285b8SNaveen N. Rao __PPC_MB(mb) | __PPC_ME(me)) 1615f645996SJiong Wang #define PPC_RLWINM_DOT(d, a, i, mb, me) EMIT(PPC_INST_RLWINM_DOT | \ 1625f645996SJiong Wang ___PPC_RA(d) | ___PPC_RS(a) | \ 1635f645996SJiong Wang __PPC_SH(i) | __PPC_MB(mb) | \ 1645f645996SJiong Wang __PPC_ME(me)) 165156d0e29SNaveen N. Rao #define PPC_RLWIMI(d, a, i, mb, me) EMIT(PPC_INST_RLWIMI | ___PPC_RA(d) | \ 166156d0e29SNaveen N. Rao ___PPC_RS(a) | __PPC_SH(i) | \ 167156d0e29SNaveen N. Rao __PPC_MB(mb) | __PPC_ME(me)) 168156d0e29SNaveen N. Rao #define PPC_RLDICL(d, a, i, mb) EMIT(PPC_INST_RLDICL | ___PPC_RA(d) | \ 169c233f597SNaveen N. Rao ___PPC_RS(a) | __PPC_SH64(i) | \ 170c233f597SNaveen N. Rao __PPC_MB64(mb)) 171277285b8SNaveen N. Rao #define PPC_RLDICR(d, a, i, me) EMIT(PPC_INST_RLDICR | ___PPC_RA(d) | \ 172c233f597SNaveen N. Rao ___PPC_RS(a) | __PPC_SH64(i) | \ 173c233f597SNaveen N. Rao __PPC_ME64(me)) 174277285b8SNaveen N. Rao 1750ca87f05SMatt Evans /* slwi = rlwinm Rx, Ry, n, 0, 31-n */ 176277285b8SNaveen N. Rao #define PPC_SLWI(d, a, i) PPC_RLWINM(d, a, i, 0, 31-(i)) 1770ca87f05SMatt Evans /* srwi = rlwinm Rx, Ry, 32-n, n, 31 */ 178277285b8SNaveen N. Rao #define PPC_SRWI(d, a, i) PPC_RLWINM(d, a, 32-(i), i, 31) 1790ca87f05SMatt Evans /* sldi = rldicr Rx, Ry, n, 63-n */ 180277285b8SNaveen N. Rao #define PPC_SLDI(d, a, i) PPC_RLDICR(d, a, i, 63-(i)) 181156d0e29SNaveen N. Rao /* sldi = rldicl Rx, Ry, 64-n, n */ 182156d0e29SNaveen N. Rao #define PPC_SRDI(d, a, i) PPC_RLDICL(d, a, 64-(i), i) 183277285b8SNaveen N. Rao 184cdaade71SMichael Neuling #define PPC_NEG(d, a) EMIT(PPC_INST_NEG | ___PPC_RT(d) | ___PPC_RA(a)) 1850ca87f05SMatt Evans 1860ca87f05SMatt Evans /* Long jump; (unconditional 'branch') */ 1870ca87f05SMatt Evans #define PPC_JMP(dest) EMIT(PPC_INST_BRANCH | \ 1880ca87f05SMatt Evans (((dest) - (ctx->idx * 4)) & 0x03fffffc)) 1890ca87f05SMatt Evans /* "cond" here covers BO:BI fields. */ 1900ca87f05SMatt Evans #define PPC_BCC_SHORT(cond, dest) EMIT(PPC_INST_BRANCH_COND | \ 1910ca87f05SMatt Evans (((cond) & 0x3ff) << 16) | \ 1920ca87f05SMatt Evans (((dest) - (ctx->idx * 4)) & \ 1930ca87f05SMatt Evans 0xfffc)) 194aaf2f7e0SNaveen N. Rao /* Sign-extended 32-bit immediate load */ 195aaf2f7e0SNaveen N. Rao #define PPC_LI32(d, i) do { \ 196aaf2f7e0SNaveen N. Rao if ((int)(uintptr_t)(i) >= -32768 && \ 197aaf2f7e0SNaveen N. Rao (int)(uintptr_t)(i) < 32768) \ 198aaf2f7e0SNaveen N. Rao PPC_LI(d, i); \ 199aaf2f7e0SNaveen N. Rao else { \ 200aaf2f7e0SNaveen N. Rao PPC_LIS(d, IMM_H(i)); \ 201aaf2f7e0SNaveen N. Rao if (IMM_L(i)) \ 202aaf2f7e0SNaveen N. Rao PPC_ORI(d, d, IMM_L(i)); \ 2030ca87f05SMatt Evans } } while(0) 204aaf2f7e0SNaveen N. Rao 2050ca87f05SMatt Evans #define PPC_LI64(d, i) do { \ 206b1a05787SNaveen N. Rao if ((long)(i) >= -2147483648 && \ 207b1a05787SNaveen N. Rao (long)(i) < 2147483648) \ 2080ca87f05SMatt Evans PPC_LI32(d, i); \ 2090ca87f05SMatt Evans else { \ 210b1a05787SNaveen N. Rao if (!((uintptr_t)(i) & 0xffff800000000000ULL)) \ 211b1a05787SNaveen N. Rao PPC_LI(d, ((uintptr_t)(i) >> 32) & 0xffff); \ 212b1a05787SNaveen N. Rao else { \ 2130ca87f05SMatt Evans PPC_LIS(d, ((uintptr_t)(i) >> 48)); \ 2140ca87f05SMatt Evans if ((uintptr_t)(i) & 0x0000ffff00000000ULL) \ 2150ca87f05SMatt Evans PPC_ORI(d, d, \ 2160ca87f05SMatt Evans ((uintptr_t)(i) >> 32) & 0xffff); \ 217b1a05787SNaveen N. Rao } \ 2180ca87f05SMatt Evans PPC_SLDI(d, d, 32); \ 2190ca87f05SMatt Evans if ((uintptr_t)(i) & 0x00000000ffff0000ULL) \ 2200ca87f05SMatt Evans PPC_ORIS(d, d, \ 2210ca87f05SMatt Evans ((uintptr_t)(i) >> 16) & 0xffff); \ 2220ca87f05SMatt Evans if ((uintptr_t)(i) & 0x000000000000ffffULL) \ 2230ca87f05SMatt Evans PPC_ORI(d, d, (uintptr_t)(i) & 0xffff); \ 224b1a05787SNaveen N. Rao } } while (0) 2250ca87f05SMatt Evans 22609ca5ab2SDenis Kirjanov #ifdef CONFIG_PPC64 22709ca5ab2SDenis Kirjanov #define PPC_FUNC_ADDR(d,i) do { PPC_LI64(d, i); } while(0) 22809ca5ab2SDenis Kirjanov #else 22909ca5ab2SDenis Kirjanov #define PPC_FUNC_ADDR(d,i) do { PPC_LI32(d, i); } while(0) 23009ca5ab2SDenis Kirjanov #endif 23109ca5ab2SDenis Kirjanov 2320ca87f05SMatt Evans static inline bool is_nearbranch(int offset) 2330ca87f05SMatt Evans { 2340ca87f05SMatt Evans return (offset < 32768) && (offset >= -32768); 2350ca87f05SMatt Evans } 2360ca87f05SMatt Evans 2370ca87f05SMatt Evans /* 2380ca87f05SMatt Evans * The fly in the ointment of code size changing from pass to pass is 2390ca87f05SMatt Evans * avoided by padding the short branch case with a NOP. If code size differs 2400ca87f05SMatt Evans * with different branch reaches we will have the issue of code moving from 2410ca87f05SMatt Evans * one pass to the next and will need a few passes to converge on a stable 2420ca87f05SMatt Evans * state. 2430ca87f05SMatt Evans */ 2440ca87f05SMatt Evans #define PPC_BCC(cond, dest) do { \ 2450ca87f05SMatt Evans if (is_nearbranch((dest) - (ctx->idx * 4))) { \ 2460ca87f05SMatt Evans PPC_BCC_SHORT(cond, dest); \ 2470ca87f05SMatt Evans PPC_NOP(); \ 2480ca87f05SMatt Evans } else { \ 2490ca87f05SMatt Evans /* Flip the 'T or F' bit to invert comparison */ \ 2500ca87f05SMatt Evans PPC_BCC_SHORT(cond ^ COND_CMP_TRUE, (ctx->idx+2)*4); \ 2510ca87f05SMatt Evans PPC_JMP(dest); \ 2520ca87f05SMatt Evans } } while(0) 2530ca87f05SMatt Evans 2540ca87f05SMatt Evans /* To create a branch condition, select a bit of cr0... */ 2550ca87f05SMatt Evans #define CR0_LT 0 2560ca87f05SMatt Evans #define CR0_GT 1 2570ca87f05SMatt Evans #define CR0_EQ 2 2580ca87f05SMatt Evans /* ...and modify BO[3] */ 2590ca87f05SMatt Evans #define COND_CMP_TRUE 0x100 2600ca87f05SMatt Evans #define COND_CMP_FALSE 0x000 2610ca87f05SMatt Evans /* Together, they make all required comparisons: */ 2620ca87f05SMatt Evans #define COND_GT (CR0_GT | COND_CMP_TRUE) 2630ca87f05SMatt Evans #define COND_GE (CR0_LT | COND_CMP_FALSE) 2640ca87f05SMatt Evans #define COND_EQ (CR0_EQ | COND_CMP_TRUE) 2650ca87f05SMatt Evans #define COND_NE (CR0_EQ | COND_CMP_FALSE) 2660ca87f05SMatt Evans #define COND_LT (CR0_LT | COND_CMP_TRUE) 26720dbf5ccSDaniel Borkmann #define COND_LE (CR0_GT | COND_CMP_FALSE) 2680ca87f05SMatt Evans 2690ca87f05SMatt Evans #endif 2700ca87f05SMatt Evans 2710ca87f05SMatt Evans #endif 272