blob: fb4656986fb940b9516f74f550d2670ee594876b [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Naveen N. Rao6ac0ba52016-06-22 21:55:06 +05302/*
3 * bpf_jit.h: BPF JIT compiler for PPC
Matt Evans0ca87f02011-07-20 15:51:00 +00004 *
5 * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation
Naveen N. Rao156d0e22016-06-22 21:55:07 +05306 * 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Matt Evans0ca87f02011-07-20 15:51:00 +00007 */
8#ifndef _BPF_JIT_H
9#define _BPF_JIT_H
10
Matt Evans0ca87f02011-07-20 15:51:00 +000011#ifndef __ASSEMBLY__
12
Naveen N. Rao156d0e22016-06-22 21:55:07 +053013#include <asm/types.h>
Balamuruhan S06541862020-06-24 17:00:35 +053014#include <asm/ppc-opcode.h>
Naveen N. Rao156d0e22016-06-22 21:55:07 +053015
16#ifdef PPC64_ELF_ABI_v1
Matt Evans0ca87f02011-07-20 15:51:00 +000017#define FUNCTION_DESCR_SIZE 24
Denis Kirjanov09ca5ab2015-02-17 10:04:40 +030018#else
19#define FUNCTION_DESCR_SIZE 0
20#endif
Matt Evans0ca87f02011-07-20 15:51:00 +000021
Matt Evans0ca87f02011-07-20 15:51:00 +000022#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 Evans0ca87f02011-07-20 15:51:00 +000026/* 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. Raoaaf2f7e2016-06-22 21:55:02 +053034/* 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 S3a181232020-06-24 17:00:36 +053038 EMIT(PPC_RAW_LI(d, i)); \
Naveen N. Raoaaf2f7e2016-06-22 21:55:02 +053039 else { \
Balamuruhan S3a181232020-06-24 17:00:36 +053040 EMIT(PPC_RAW_LIS(d, IMM_H(i))); \
Naveen N. Raoaaf2f7e2016-06-22 21:55:02 +053041 if (IMM_L(i)) \
Balamuruhan S3a181232020-06-24 17:00:36 +053042 EMIT(PPC_RAW_ORI(d, d, IMM_L(i))); \
Matt Evans0ca87f02011-07-20 15:51:00 +000043 } } while(0)
Naveen N. Raoaaf2f7e2016-06-22 21:55:02 +053044
Matt Evans0ca87f02011-07-20 15:51:00 +000045#define PPC_LI64(d, i) do { \
Naveen N. Raob1a05782016-06-22 21:55:03 +053046 if ((long)(i) >= -2147483648 && \
47 (long)(i) < 2147483648) \
Matt Evans0ca87f02011-07-20 15:51:00 +000048 PPC_LI32(d, i); \
49 else { \
Naveen N. Raob1a05782016-06-22 21:55:03 +053050 if (!((uintptr_t)(i) & 0xffff800000000000ULL)) \
Balamuruhan S3a181232020-06-24 17:00:36 +053051 EMIT(PPC_RAW_LI(d, ((uintptr_t)(i) >> 32) & \
52 0xffff)); \
Naveen N. Raob1a05782016-06-22 21:55:03 +053053 else { \
Balamuruhan S3a181232020-06-24 17:00:36 +053054 EMIT(PPC_RAW_LIS(d, ((uintptr_t)(i) >> 48))); \
Naveen N. Raob1a05782016-06-22 21:55:03 +053055 if ((uintptr_t)(i) & 0x0000ffff00000000ULL) \
Balamuruhan S3a181232020-06-24 17:00:36 +053056 EMIT(PPC_RAW_ORI(d, d, \
57 ((uintptr_t)(i) >> 32) & 0xffff)); \
Naveen N. Raob1a05782016-06-22 21:55:03 +053058 } \
Balamuruhan S3a181232020-06-24 17:00:36 +053059 EMIT(PPC_RAW_SLDI(d, d, 32)); \
Matt Evans0ca87f02011-07-20 15:51:00 +000060 if ((uintptr_t)(i) & 0x00000000ffff0000ULL) \
Balamuruhan S3a181232020-06-24 17:00:36 +053061 EMIT(PPC_RAW_ORIS(d, d, \
62 ((uintptr_t)(i) >> 16) & 0xffff)); \
Matt Evans0ca87f02011-07-20 15:51:00 +000063 if ((uintptr_t)(i) & 0x000000000000ffffULL) \
Balamuruhan S3a181232020-06-24 17:00:36 +053064 EMIT(PPC_RAW_ORI(d, d, (uintptr_t)(i) & \
65 0xffff)); \
Naveen N. Raob1a05782016-06-22 21:55:03 +053066 } } while (0)
Matt Evans0ca87f02011-07-20 15:51:00 +000067
Denis Kirjanov09ca5ab2015-02-17 10:04:40 +030068#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 Evans0ca87f02011-07-20 15:51:00 +000074static 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 S3a181232020-06-24 17:00:36 +053089 EMIT(PPC_RAW_NOP()); \
Matt Evans0ca87f02011-07-20 15:51:00 +000090 } 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 Borkmann20dbf5c2017-08-10 01:40:00 +0200109#define COND_LE (CR0_GT | COND_CMP_FALSE)
Matt Evans0ca87f02011-07-20 15:51:00 +0000110
Christophe Leroyc4268102021-03-22 16:37:50 +0000111#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 Leroyf1b15832021-03-22 16:37:48 +0000114
115struct 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 Leroyc4268102021-03-22 16:37:50 +0000120 * bits (r3-r31)
Christophe Leroyf1b15832021-03-22 16:37:48 +0000121 * - rest of the bits can be used to track other
Christophe Leroyc4268102021-03-22 16:37:50 +0000122 * things -- for now, we use bits 0 to 2
Christophe Leroyf1b15832021-03-22 16:37:48 +0000123 * encoded in SEEN_* macros above
124 */
125 unsigned int seen;
126 unsigned int idx;
127 unsigned int stack_size;
128};
129
130static 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
136static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i)
137{
138 return ctx->seen & (1 << (31 - i));
139}
140
141static inline void bpf_set_seen_register(struct codegen_context *ctx, int i)
142{
143 ctx->seen |= 1 << (31 - i);
144}
145
Christophe Leroy4ea76e92021-03-22 16:37:49 +0000146void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 func);
147int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *ctx,
148 u32 *addrs, bool extra_pass);
149void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx);
150void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx);
151
Matt Evans0ca87f02011-07-20 15:51:00 +0000152#endif
153
154#endif