1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * bpf_jit.h: BPF JIT compiler for PPC
5 * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation
6 * 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
13 #include <asm/types.h>
14 #include <asm/ppc-opcode.h>
16 #ifdef PPC64_ELF_ABI_v1
17 #define FUNCTION_DESCR_SIZE 24
19 #define FUNCTION_DESCR_SIZE 0
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)
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)) & \
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) \
38 EMIT(PPC_RAW_LI(d, i)); \
40 EMIT(PPC_RAW_LIS(d, IMM_H(i))); \
42 EMIT(PPC_RAW_ORI(d, d, IMM_L(i))); \
45 #define PPC_LI64(d, i) do { \
46 if ((long)(i) >= -2147483648 && \
47 (long)(i) < 2147483648) \
50 if (!((uintptr_t)(i) & 0xffff800000000000ULL)) \
51 EMIT(PPC_RAW_LI(d, ((uintptr_t)(i) >> 32) & \
54 EMIT(PPC_RAW_LIS(d, ((uintptr_t)(i) >> 48))); \
55 if ((uintptr_t)(i) & 0x0000ffff00000000ULL) \
56 EMIT(PPC_RAW_ORI(d, d, \
57 ((uintptr_t)(i) >> 32) & 0xffff)); \
59 EMIT(PPC_RAW_SLDI(d, d, 32)); \
60 if ((uintptr_t)(i) & 0x00000000ffff0000ULL) \
61 EMIT(PPC_RAW_ORIS(d, d, \
62 ((uintptr_t)(i) >> 16) & 0xffff)); \
63 if ((uintptr_t)(i) & 0x000000000000ffffULL) \
64 EMIT(PPC_RAW_ORI(d, d, (uintptr_t)(i) & \
69 #define PPC_FUNC_ADDR(d,i) do { PPC_LI64(d, i); } while(0)
71 #define PPC_FUNC_ADDR(d,i) do { PPC_LI32(d, i); } while(0)
74 static inline bool is_nearbranch(int offset
)
76 return (offset
< 32768) && (offset
>= -32768);
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
86 #define PPC_BCC(cond, dest) do { \
87 if (is_nearbranch((dest) - (ctx->idx * 4))) { \
88 PPC_BCC_SHORT(cond, dest); \
89 EMIT(PPC_RAW_NOP()); \
91 /* Flip the 'T or F' bit to invert comparison */ \
92 PPC_BCC_SHORT(cond ^ COND_CMP_TRUE, (ctx->idx+2)*4); \
96 /* To create a branch condition, select a bit of cr0... */
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)
109 #define COND_LE (CR0_GT | COND_CMP_FALSE)