1eb63cfcdSJohan Almbladh // SPDX-License-Identifier: GPL-2.0-only
2eb63cfcdSJohan Almbladh /*
3eb63cfcdSJohan Almbladh * Just-In-Time compiler for eBPF bytecode on MIPS.
4eb63cfcdSJohan Almbladh * Implementation of JIT functions common to 32-bit and 64-bit CPUs.
5eb63cfcdSJohan Almbladh *
6eb63cfcdSJohan Almbladh * Copyright (c) 2021 Anyfi Networks AB.
7eb63cfcdSJohan Almbladh * Author: Johan Almbladh <johan.almbladh@gmail.com>
8eb63cfcdSJohan Almbladh *
9eb63cfcdSJohan Almbladh * Based on code and ideas from
10eb63cfcdSJohan Almbladh * Copyright (c) 2017 Cavium, Inc.
11eb63cfcdSJohan Almbladh * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
12eb63cfcdSJohan Almbladh * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
13eb63cfcdSJohan Almbladh */
14eb63cfcdSJohan Almbladh
15eb63cfcdSJohan Almbladh /*
16eb63cfcdSJohan Almbladh * Code overview
17eb63cfcdSJohan Almbladh * =============
18eb63cfcdSJohan Almbladh *
19eb63cfcdSJohan Almbladh * - bpf_jit_comp.h
20eb63cfcdSJohan Almbladh * Common definitions and utilities.
21eb63cfcdSJohan Almbladh *
22eb63cfcdSJohan Almbladh * - bpf_jit_comp.c
23eb63cfcdSJohan Almbladh * Implementation of JIT top-level logic and exported JIT API functions.
24eb63cfcdSJohan Almbladh * Implementation of internal operations shared by 32-bit and 64-bit code.
25eb63cfcdSJohan Almbladh * JMP and ALU JIT control code, register control code, shared ALU and
26eb63cfcdSJohan Almbladh * JMP/JMP32 JIT operations.
27eb63cfcdSJohan Almbladh *
28eb63cfcdSJohan Almbladh * - bpf_jit_comp32.c
29eb63cfcdSJohan Almbladh * Implementation of functions to JIT prologue, epilogue and a single eBPF
30eb63cfcdSJohan Almbladh * instruction for 32-bit MIPS CPUs. The functions use shared operations
31eb63cfcdSJohan Almbladh * where possible, and implement the rest for 32-bit MIPS such as ALU64
32eb63cfcdSJohan Almbladh * operations.
33eb63cfcdSJohan Almbladh *
34eb63cfcdSJohan Almbladh * - bpf_jit_comp64.c
35eb63cfcdSJohan Almbladh * Ditto, for 64-bit MIPS CPUs.
36eb63cfcdSJohan Almbladh *
37eb63cfcdSJohan Almbladh * Zero and sign extension
38eb63cfcdSJohan Almbladh * ========================
39eb63cfcdSJohan Almbladh * 32-bit MIPS instructions on 64-bit MIPS registers use sign extension,
40eb63cfcdSJohan Almbladh * but the eBPF instruction set mandates zero extension. We let the verifier
41eb63cfcdSJohan Almbladh * insert explicit zero-extensions after 32-bit ALU operations, both for
42eb63cfcdSJohan Almbladh * 32-bit and 64-bit MIPS JITs. Conditional JMP32 operations on 64-bit MIPs
43eb63cfcdSJohan Almbladh * are JITed with sign extensions inserted when so expected.
44eb63cfcdSJohan Almbladh *
45eb63cfcdSJohan Almbladh * ALU operations
46eb63cfcdSJohan Almbladh * ==============
47eb63cfcdSJohan Almbladh * ALU operations on 32/64-bit MIPS and ALU64 operations on 64-bit MIPS are
48eb63cfcdSJohan Almbladh * JITed in the following steps. ALU64 operations on 32-bit MIPS are more
49eb63cfcdSJohan Almbladh * complicated and therefore only processed by special implementations in
50eb63cfcdSJohan Almbladh * step (3).
51eb63cfcdSJohan Almbladh *
52eb63cfcdSJohan Almbladh * 1) valid_alu_i:
53eb63cfcdSJohan Almbladh * Determine if an immediate operation can be emitted as such, or if
54eb63cfcdSJohan Almbladh * we must fall back to the register version.
55eb63cfcdSJohan Almbladh *
56eb63cfcdSJohan Almbladh * 2) rewrite_alu_i:
57eb63cfcdSJohan Almbladh * Convert BPF operation and immediate value to a canonical form for
58eb63cfcdSJohan Almbladh * JITing. In some degenerate cases this form may be a no-op.
59eb63cfcdSJohan Almbladh *
60eb63cfcdSJohan Almbladh * 3) emit_alu_{i,i64,r,64}:
61eb63cfcdSJohan Almbladh * Emit instructions for an ALU or ALU64 immediate or register operation.
62eb63cfcdSJohan Almbladh *
63eb63cfcdSJohan Almbladh * JMP operations
64eb63cfcdSJohan Almbladh * ==============
65eb63cfcdSJohan Almbladh * JMP and JMP32 operations require an JIT instruction offset table for
66eb63cfcdSJohan Almbladh * translating the jump offset. This table is computed by dry-running the
67eb63cfcdSJohan Almbladh * JIT without actually emitting anything. However, the computed PC-relative
68eb63cfcdSJohan Almbladh * offset may overflow the 18-bit offset field width of the native MIPS
69eb63cfcdSJohan Almbladh * branch instruction. In such cases, the long jump is converted into the
70eb63cfcdSJohan Almbladh * following sequence.
71eb63cfcdSJohan Almbladh *
72eb63cfcdSJohan Almbladh * <branch> !<cond> +2 Inverted PC-relative branch
73eb63cfcdSJohan Almbladh * nop Delay slot
74eb63cfcdSJohan Almbladh * j <offset> Unconditional absolute long jump
75eb63cfcdSJohan Almbladh * nop Delay slot
76eb63cfcdSJohan Almbladh *
77eb63cfcdSJohan Almbladh * Since this converted sequence alters the offset table, all offsets must
78eb63cfcdSJohan Almbladh * be re-calculated. This may in turn trigger new branch conversions, so
79eb63cfcdSJohan Almbladh * the process is repeated until no further changes are made. Normally it
80eb63cfcdSJohan Almbladh * completes in 1-2 iterations. If JIT_MAX_ITERATIONS should reached, we
81eb63cfcdSJohan Almbladh * fall back to converting every remaining jump operation. The branch
82eb63cfcdSJohan Almbladh * conversion is independent of how the JMP or JMP32 condition is JITed.
83eb63cfcdSJohan Almbladh *
84eb63cfcdSJohan Almbladh * JMP32 and JMP operations are JITed as follows.
85eb63cfcdSJohan Almbladh *
86eb63cfcdSJohan Almbladh * 1) setup_jmp_{i,r}:
87eb63cfcdSJohan Almbladh * Convert jump conditional and offset into a form that can be JITed.
88eb63cfcdSJohan Almbladh * This form may be a no-op, a canonical form, or an inverted PC-relative
89eb63cfcdSJohan Almbladh * jump if branch conversion is necessary.
90eb63cfcdSJohan Almbladh *
91eb63cfcdSJohan Almbladh * 2) valid_jmp_i:
92eb63cfcdSJohan Almbladh * Determine if an immediate operations can be emitted as such, or if
93eb63cfcdSJohan Almbladh * we must fall back to the register version. Applies to JMP32 for 32-bit
94eb63cfcdSJohan Almbladh * MIPS, and both JMP and JMP32 for 64-bit MIPS.
95eb63cfcdSJohan Almbladh *
96eb63cfcdSJohan Almbladh * 3) emit_jmp_{i,i64,r,r64}:
97eb63cfcdSJohan Almbladh * Emit instructions for an JMP or JMP32 immediate or register operation.
98eb63cfcdSJohan Almbladh *
99eb63cfcdSJohan Almbladh * 4) finish_jmp_{i,r}:
100eb63cfcdSJohan Almbladh * Emit any instructions needed to finish the jump. This includes a nop
101eb63cfcdSJohan Almbladh * for the delay slot if a branch was emitted, and a long absolute jump
102eb63cfcdSJohan Almbladh * if the branch was converted.
103eb63cfcdSJohan Almbladh */
104eb63cfcdSJohan Almbladh
105eb63cfcdSJohan Almbladh #include <linux/limits.h>
106eb63cfcdSJohan Almbladh #include <linux/bitops.h>
107eb63cfcdSJohan Almbladh #include <linux/errno.h>
108eb63cfcdSJohan Almbladh #include <linux/filter.h>
109eb63cfcdSJohan Almbladh #include <linux/bpf.h>
110eb63cfcdSJohan Almbladh #include <linux/slab.h>
111eb63cfcdSJohan Almbladh #include <asm/bitops.h>
112eb63cfcdSJohan Almbladh #include <asm/cacheflush.h>
113eb63cfcdSJohan Almbladh #include <asm/cpu-features.h>
114eb63cfcdSJohan Almbladh #include <asm/isa-rev.h>
115eb63cfcdSJohan Almbladh #include <asm/uasm.h>
116eb63cfcdSJohan Almbladh
117eb63cfcdSJohan Almbladh #include "bpf_jit_comp.h"
118eb63cfcdSJohan Almbladh
119eb63cfcdSJohan Almbladh /* Convenience macros for descriptor access */
120eb63cfcdSJohan Almbladh #define CONVERTED(desc) ((desc) & JIT_DESC_CONVERT)
121eb63cfcdSJohan Almbladh #define INDEX(desc) ((desc) & ~JIT_DESC_CONVERT)
122eb63cfcdSJohan Almbladh
123eb63cfcdSJohan Almbladh /*
124eb63cfcdSJohan Almbladh * Push registers on the stack, starting at a given depth from the stack
125eb63cfcdSJohan Almbladh * pointer and increasing. The next depth to be written is returned.
126eb63cfcdSJohan Almbladh */
push_regs(struct jit_context * ctx,u32 mask,u32 excl,int depth)127eb63cfcdSJohan Almbladh int push_regs(struct jit_context *ctx, u32 mask, u32 excl, int depth)
128eb63cfcdSJohan Almbladh {
129eb63cfcdSJohan Almbladh int reg;
130eb63cfcdSJohan Almbladh
131eb63cfcdSJohan Almbladh for (reg = 0; reg < BITS_PER_BYTE * sizeof(mask); reg++)
132eb63cfcdSJohan Almbladh if (mask & BIT(reg)) {
133eb63cfcdSJohan Almbladh if ((excl & BIT(reg)) == 0) {
134eb63cfcdSJohan Almbladh if (sizeof(long) == 4)
135eb63cfcdSJohan Almbladh emit(ctx, sw, reg, depth, MIPS_R_SP);
136eb63cfcdSJohan Almbladh else /* sizeof(long) == 8 */
137eb63cfcdSJohan Almbladh emit(ctx, sd, reg, depth, MIPS_R_SP);
138eb63cfcdSJohan Almbladh }
139eb63cfcdSJohan Almbladh depth += sizeof(long);
140eb63cfcdSJohan Almbladh }
141eb63cfcdSJohan Almbladh
142eb63cfcdSJohan Almbladh ctx->stack_used = max((int)ctx->stack_used, depth);
143eb63cfcdSJohan Almbladh return depth;
144eb63cfcdSJohan Almbladh }
145eb63cfcdSJohan Almbladh
146eb63cfcdSJohan Almbladh /*
147eb63cfcdSJohan Almbladh * Pop registers from the stack, starting at a given depth from the stack
148eb63cfcdSJohan Almbladh * pointer and increasing. The next depth to be read is returned.
149eb63cfcdSJohan Almbladh */
pop_regs(struct jit_context * ctx,u32 mask,u32 excl,int depth)150eb63cfcdSJohan Almbladh int pop_regs(struct jit_context *ctx, u32 mask, u32 excl, int depth)
151eb63cfcdSJohan Almbladh {
152eb63cfcdSJohan Almbladh int reg;
153eb63cfcdSJohan Almbladh
154eb63cfcdSJohan Almbladh for (reg = 0; reg < BITS_PER_BYTE * sizeof(mask); reg++)
155eb63cfcdSJohan Almbladh if (mask & BIT(reg)) {
156eb63cfcdSJohan Almbladh if ((excl & BIT(reg)) == 0) {
157eb63cfcdSJohan Almbladh if (sizeof(long) == 4)
158eb63cfcdSJohan Almbladh emit(ctx, lw, reg, depth, MIPS_R_SP);
159eb63cfcdSJohan Almbladh else /* sizeof(long) == 8 */
160eb63cfcdSJohan Almbladh emit(ctx, ld, reg, depth, MIPS_R_SP);
161eb63cfcdSJohan Almbladh }
162eb63cfcdSJohan Almbladh depth += sizeof(long);
163eb63cfcdSJohan Almbladh }
164eb63cfcdSJohan Almbladh
165eb63cfcdSJohan Almbladh return depth;
166eb63cfcdSJohan Almbladh }
167eb63cfcdSJohan Almbladh
168eb63cfcdSJohan Almbladh /* Compute the 28-bit jump target address from a BPF program location */
get_target(struct jit_context * ctx,u32 loc)169eb63cfcdSJohan Almbladh int get_target(struct jit_context *ctx, u32 loc)
170eb63cfcdSJohan Almbladh {
171eb63cfcdSJohan Almbladh u32 index = INDEX(ctx->descriptors[loc]);
172eb63cfcdSJohan Almbladh unsigned long pc = (unsigned long)&ctx->target[ctx->jit_index];
173eb63cfcdSJohan Almbladh unsigned long addr = (unsigned long)&ctx->target[index];
174eb63cfcdSJohan Almbladh
175eb63cfcdSJohan Almbladh if (!ctx->target)
176eb63cfcdSJohan Almbladh return 0;
177eb63cfcdSJohan Almbladh
178eb63cfcdSJohan Almbladh if ((addr ^ pc) & ~MIPS_JMP_MASK)
179eb63cfcdSJohan Almbladh return -1;
180eb63cfcdSJohan Almbladh
181eb63cfcdSJohan Almbladh return addr & MIPS_JMP_MASK;
182eb63cfcdSJohan Almbladh }
183eb63cfcdSJohan Almbladh
184eb63cfcdSJohan Almbladh /* Compute the PC-relative offset to relative BPF program offset */
get_offset(const struct jit_context * ctx,int off)185eb63cfcdSJohan Almbladh int get_offset(const struct jit_context *ctx, int off)
186eb63cfcdSJohan Almbladh {
187eb63cfcdSJohan Almbladh return (INDEX(ctx->descriptors[ctx->bpf_index + off]) -
188eb63cfcdSJohan Almbladh ctx->jit_index - 1) * sizeof(u32);
189eb63cfcdSJohan Almbladh }
190eb63cfcdSJohan Almbladh
191eb63cfcdSJohan Almbladh /* dst = imm (register width) */
emit_mov_i(struct jit_context * ctx,u8 dst,s32 imm)192eb63cfcdSJohan Almbladh void emit_mov_i(struct jit_context *ctx, u8 dst, s32 imm)
193eb63cfcdSJohan Almbladh {
194eb63cfcdSJohan Almbladh if (imm >= -0x8000 && imm <= 0x7fff) {
195eb63cfcdSJohan Almbladh emit(ctx, addiu, dst, MIPS_R_ZERO, imm);
196eb63cfcdSJohan Almbladh } else {
197eb63cfcdSJohan Almbladh emit(ctx, lui, dst, (s16)((u32)imm >> 16));
198eb63cfcdSJohan Almbladh emit(ctx, ori, dst, dst, (u16)(imm & 0xffff));
199eb63cfcdSJohan Almbladh }
200eb63cfcdSJohan Almbladh clobber_reg(ctx, dst);
201eb63cfcdSJohan Almbladh }
202eb63cfcdSJohan Almbladh
203eb63cfcdSJohan Almbladh /* dst = src (register width) */
emit_mov_r(struct jit_context * ctx,u8 dst,u8 src)204eb63cfcdSJohan Almbladh void emit_mov_r(struct jit_context *ctx, u8 dst, u8 src)
205eb63cfcdSJohan Almbladh {
206eb63cfcdSJohan Almbladh emit(ctx, ori, dst, src, 0);
207eb63cfcdSJohan Almbladh clobber_reg(ctx, dst);
208eb63cfcdSJohan Almbladh }
209eb63cfcdSJohan Almbladh
210eb63cfcdSJohan Almbladh /* Validate ALU immediate range */
valid_alu_i(u8 op,s32 imm)211eb63cfcdSJohan Almbladh bool valid_alu_i(u8 op, s32 imm)
212eb63cfcdSJohan Almbladh {
213eb63cfcdSJohan Almbladh switch (BPF_OP(op)) {
214eb63cfcdSJohan Almbladh case BPF_NEG:
215eb63cfcdSJohan Almbladh case BPF_LSH:
216eb63cfcdSJohan Almbladh case BPF_RSH:
217eb63cfcdSJohan Almbladh case BPF_ARSH:
218eb63cfcdSJohan Almbladh /* All legal eBPF values are valid */
219eb63cfcdSJohan Almbladh return true;
220eb63cfcdSJohan Almbladh case BPF_ADD:
221bbefef2fSJiaxun Yang if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS))
222bbefef2fSJiaxun Yang return false;
223eb63cfcdSJohan Almbladh /* imm must be 16 bits */
224eb63cfcdSJohan Almbladh return imm >= -0x8000 && imm <= 0x7fff;
225eb63cfcdSJohan Almbladh case BPF_SUB:
226bbefef2fSJiaxun Yang if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS))
227bbefef2fSJiaxun Yang return false;
228eb63cfcdSJohan Almbladh /* -imm must be 16 bits */
229eb63cfcdSJohan Almbladh return imm >= -0x7fff && imm <= 0x8000;
230eb63cfcdSJohan Almbladh case BPF_AND:
231eb63cfcdSJohan Almbladh case BPF_OR:
232eb63cfcdSJohan Almbladh case BPF_XOR:
233eb63cfcdSJohan Almbladh /* imm must be 16 bits unsigned */
234eb63cfcdSJohan Almbladh return imm >= 0 && imm <= 0xffff;
235eb63cfcdSJohan Almbladh case BPF_MUL:
236eb63cfcdSJohan Almbladh /* imm must be zero or a positive power of two */
237eb63cfcdSJohan Almbladh return imm == 0 || (imm > 0 && is_power_of_2(imm));
238eb63cfcdSJohan Almbladh case BPF_DIV:
239eb63cfcdSJohan Almbladh case BPF_MOD:
240eb63cfcdSJohan Almbladh /* imm must be an 17-bit power of two */
241eb63cfcdSJohan Almbladh return (u32)imm <= 0x10000 && is_power_of_2((u32)imm);
242eb63cfcdSJohan Almbladh }
243eb63cfcdSJohan Almbladh return false;
244eb63cfcdSJohan Almbladh }
245eb63cfcdSJohan Almbladh
246eb63cfcdSJohan Almbladh /* Rewrite ALU immediate operation */
rewrite_alu_i(u8 op,s32 imm,u8 * alu,s32 * val)247eb63cfcdSJohan Almbladh bool rewrite_alu_i(u8 op, s32 imm, u8 *alu, s32 *val)
248eb63cfcdSJohan Almbladh {
249eb63cfcdSJohan Almbladh bool act = true;
250eb63cfcdSJohan Almbladh
251eb63cfcdSJohan Almbladh switch (BPF_OP(op)) {
252eb63cfcdSJohan Almbladh case BPF_LSH:
253eb63cfcdSJohan Almbladh case BPF_RSH:
254eb63cfcdSJohan Almbladh case BPF_ARSH:
255eb63cfcdSJohan Almbladh case BPF_ADD:
256eb63cfcdSJohan Almbladh case BPF_SUB:
257eb63cfcdSJohan Almbladh case BPF_OR:
258eb63cfcdSJohan Almbladh case BPF_XOR:
259eb63cfcdSJohan Almbladh /* imm == 0 is a no-op */
260eb63cfcdSJohan Almbladh act = imm != 0;
261eb63cfcdSJohan Almbladh break;
262eb63cfcdSJohan Almbladh case BPF_MUL:
263eb63cfcdSJohan Almbladh if (imm == 1) {
264eb63cfcdSJohan Almbladh /* dst * 1 is a no-op */
265eb63cfcdSJohan Almbladh act = false;
266eb63cfcdSJohan Almbladh } else if (imm == 0) {
267eb63cfcdSJohan Almbladh /* dst * 0 is dst & 0 */
268eb63cfcdSJohan Almbladh op = BPF_AND;
269eb63cfcdSJohan Almbladh } else {
270eb63cfcdSJohan Almbladh /* dst * (1 << n) is dst << n */
271eb63cfcdSJohan Almbladh op = BPF_LSH;
272eb63cfcdSJohan Almbladh imm = ilog2(abs(imm));
273eb63cfcdSJohan Almbladh }
274eb63cfcdSJohan Almbladh break;
275eb63cfcdSJohan Almbladh case BPF_DIV:
276eb63cfcdSJohan Almbladh if (imm == 1) {
277eb63cfcdSJohan Almbladh /* dst / 1 is a no-op */
278eb63cfcdSJohan Almbladh act = false;
279eb63cfcdSJohan Almbladh } else {
280eb63cfcdSJohan Almbladh /* dst / (1 << n) is dst >> n */
281eb63cfcdSJohan Almbladh op = BPF_RSH;
282eb63cfcdSJohan Almbladh imm = ilog2(imm);
283eb63cfcdSJohan Almbladh }
284eb63cfcdSJohan Almbladh break;
285eb63cfcdSJohan Almbladh case BPF_MOD:
286eb63cfcdSJohan Almbladh /* dst % (1 << n) is dst & ((1 << n) - 1) */
287eb63cfcdSJohan Almbladh op = BPF_AND;
288eb63cfcdSJohan Almbladh imm--;
289eb63cfcdSJohan Almbladh break;
290eb63cfcdSJohan Almbladh }
291eb63cfcdSJohan Almbladh
292eb63cfcdSJohan Almbladh *alu = op;
293eb63cfcdSJohan Almbladh *val = imm;
294eb63cfcdSJohan Almbladh return act;
295eb63cfcdSJohan Almbladh }
296eb63cfcdSJohan Almbladh
297eb63cfcdSJohan Almbladh /* ALU immediate operation (32-bit) */
emit_alu_i(struct jit_context * ctx,u8 dst,s32 imm,u8 op)298eb63cfcdSJohan Almbladh void emit_alu_i(struct jit_context *ctx, u8 dst, s32 imm, u8 op)
299eb63cfcdSJohan Almbladh {
300eb63cfcdSJohan Almbladh switch (BPF_OP(op)) {
301eb63cfcdSJohan Almbladh /* dst = -dst */
302eb63cfcdSJohan Almbladh case BPF_NEG:
303eb63cfcdSJohan Almbladh emit(ctx, subu, dst, MIPS_R_ZERO, dst);
304eb63cfcdSJohan Almbladh break;
305eb63cfcdSJohan Almbladh /* dst = dst & imm */
306eb63cfcdSJohan Almbladh case BPF_AND:
307eb63cfcdSJohan Almbladh emit(ctx, andi, dst, dst, (u16)imm);
308eb63cfcdSJohan Almbladh break;
309eb63cfcdSJohan Almbladh /* dst = dst | imm */
310eb63cfcdSJohan Almbladh case BPF_OR:
311eb63cfcdSJohan Almbladh emit(ctx, ori, dst, dst, (u16)imm);
312eb63cfcdSJohan Almbladh break;
313eb63cfcdSJohan Almbladh /* dst = dst ^ imm */
314eb63cfcdSJohan Almbladh case BPF_XOR:
315eb63cfcdSJohan Almbladh emit(ctx, xori, dst, dst, (u16)imm);
316eb63cfcdSJohan Almbladh break;
317eb63cfcdSJohan Almbladh /* dst = dst << imm */
318eb63cfcdSJohan Almbladh case BPF_LSH:
319eb63cfcdSJohan Almbladh emit(ctx, sll, dst, dst, imm);
320eb63cfcdSJohan Almbladh break;
321eb63cfcdSJohan Almbladh /* dst = dst >> imm */
322eb63cfcdSJohan Almbladh case BPF_RSH:
323eb63cfcdSJohan Almbladh emit(ctx, srl, dst, dst, imm);
324eb63cfcdSJohan Almbladh break;
325eb63cfcdSJohan Almbladh /* dst = dst >> imm (arithmetic) */
326eb63cfcdSJohan Almbladh case BPF_ARSH:
327eb63cfcdSJohan Almbladh emit(ctx, sra, dst, dst, imm);
328eb63cfcdSJohan Almbladh break;
329eb63cfcdSJohan Almbladh /* dst = dst + imm */
330eb63cfcdSJohan Almbladh case BPF_ADD:
331eb63cfcdSJohan Almbladh emit(ctx, addiu, dst, dst, imm);
332eb63cfcdSJohan Almbladh break;
333eb63cfcdSJohan Almbladh /* dst = dst - imm */
334eb63cfcdSJohan Almbladh case BPF_SUB:
335eb63cfcdSJohan Almbladh emit(ctx, addiu, dst, dst, -imm);
336eb63cfcdSJohan Almbladh break;
337eb63cfcdSJohan Almbladh }
338eb63cfcdSJohan Almbladh clobber_reg(ctx, dst);
339eb63cfcdSJohan Almbladh }
340eb63cfcdSJohan Almbladh
341eb63cfcdSJohan Almbladh /* ALU register operation (32-bit) */
emit_alu_r(struct jit_context * ctx,u8 dst,u8 src,u8 op)342eb63cfcdSJohan Almbladh void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op)
343eb63cfcdSJohan Almbladh {
344eb63cfcdSJohan Almbladh switch (BPF_OP(op)) {
345eb63cfcdSJohan Almbladh /* dst = dst & src */
346eb63cfcdSJohan Almbladh case BPF_AND:
347eb63cfcdSJohan Almbladh emit(ctx, and, dst, dst, src);
348eb63cfcdSJohan Almbladh break;
349eb63cfcdSJohan Almbladh /* dst = dst | src */
350eb63cfcdSJohan Almbladh case BPF_OR:
351eb63cfcdSJohan Almbladh emit(ctx, or, dst, dst, src);
352eb63cfcdSJohan Almbladh break;
353eb63cfcdSJohan Almbladh /* dst = dst ^ src */
354eb63cfcdSJohan Almbladh case BPF_XOR:
355eb63cfcdSJohan Almbladh emit(ctx, xor, dst, dst, src);
356eb63cfcdSJohan Almbladh break;
357eb63cfcdSJohan Almbladh /* dst = dst << src */
358eb63cfcdSJohan Almbladh case BPF_LSH:
359eb63cfcdSJohan Almbladh emit(ctx, sllv, dst, dst, src);
360eb63cfcdSJohan Almbladh break;
361eb63cfcdSJohan Almbladh /* dst = dst >> src */
362eb63cfcdSJohan Almbladh case BPF_RSH:
363eb63cfcdSJohan Almbladh emit(ctx, srlv, dst, dst, src);
364eb63cfcdSJohan Almbladh break;
365eb63cfcdSJohan Almbladh /* dst = dst >> src (arithmetic) */
366eb63cfcdSJohan Almbladh case BPF_ARSH:
367eb63cfcdSJohan Almbladh emit(ctx, srav, dst, dst, src);
368eb63cfcdSJohan Almbladh break;
369eb63cfcdSJohan Almbladh /* dst = dst + src */
370eb63cfcdSJohan Almbladh case BPF_ADD:
371eb63cfcdSJohan Almbladh emit(ctx, addu, dst, dst, src);
372eb63cfcdSJohan Almbladh break;
373eb63cfcdSJohan Almbladh /* dst = dst - src */
374eb63cfcdSJohan Almbladh case BPF_SUB:
375eb63cfcdSJohan Almbladh emit(ctx, subu, dst, dst, src);
376eb63cfcdSJohan Almbladh break;
377eb63cfcdSJohan Almbladh /* dst = dst * src */
378eb63cfcdSJohan Almbladh case BPF_MUL:
379eb63cfcdSJohan Almbladh if (cpu_has_mips32r1 || cpu_has_mips32r6) {
380eb63cfcdSJohan Almbladh emit(ctx, mul, dst, dst, src);
381eb63cfcdSJohan Almbladh } else {
382eb63cfcdSJohan Almbladh emit(ctx, multu, dst, src);
383eb63cfcdSJohan Almbladh emit(ctx, mflo, dst);
384eb63cfcdSJohan Almbladh }
385eb63cfcdSJohan Almbladh break;
386eb63cfcdSJohan Almbladh /* dst = dst / src */
387eb63cfcdSJohan Almbladh case BPF_DIV:
388eb63cfcdSJohan Almbladh if (cpu_has_mips32r6) {
389eb63cfcdSJohan Almbladh emit(ctx, divu_r6, dst, dst, src);
390eb63cfcdSJohan Almbladh } else {
391eb63cfcdSJohan Almbladh emit(ctx, divu, dst, src);
392eb63cfcdSJohan Almbladh emit(ctx, mflo, dst);
393eb63cfcdSJohan Almbladh }
394eb63cfcdSJohan Almbladh break;
395eb63cfcdSJohan Almbladh /* dst = dst % src */
396eb63cfcdSJohan Almbladh case BPF_MOD:
397eb63cfcdSJohan Almbladh if (cpu_has_mips32r6) {
398eb63cfcdSJohan Almbladh emit(ctx, modu, dst, dst, src);
399eb63cfcdSJohan Almbladh } else {
400eb63cfcdSJohan Almbladh emit(ctx, divu, dst, src);
401eb63cfcdSJohan Almbladh emit(ctx, mfhi, dst);
402eb63cfcdSJohan Almbladh }
403eb63cfcdSJohan Almbladh break;
404eb63cfcdSJohan Almbladh }
405eb63cfcdSJohan Almbladh clobber_reg(ctx, dst);
406eb63cfcdSJohan Almbladh }
407eb63cfcdSJohan Almbladh
408eb63cfcdSJohan Almbladh /* Atomic read-modify-write (32-bit) */
emit_atomic_r(struct jit_context * ctx,u8 dst,u8 src,s16 off,u8 code)409eb63cfcdSJohan Almbladh void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)
410eb63cfcdSJohan Almbladh {
41172570224SJohan Almbladh LLSC_sync(ctx);
412eb63cfcdSJohan Almbladh emit(ctx, ll, MIPS_R_T9, off, dst);
413eb63cfcdSJohan Almbladh switch (code) {
414eb63cfcdSJohan Almbladh case BPF_ADD:
415eb63cfcdSJohan Almbladh case BPF_ADD | BPF_FETCH:
416eb63cfcdSJohan Almbladh emit(ctx, addu, MIPS_R_T8, MIPS_R_T9, src);
417eb63cfcdSJohan Almbladh break;
418eb63cfcdSJohan Almbladh case BPF_AND:
419eb63cfcdSJohan Almbladh case BPF_AND | BPF_FETCH:
420eb63cfcdSJohan Almbladh emit(ctx, and, MIPS_R_T8, MIPS_R_T9, src);
421eb63cfcdSJohan Almbladh break;
422eb63cfcdSJohan Almbladh case BPF_OR:
423eb63cfcdSJohan Almbladh case BPF_OR | BPF_FETCH:
424eb63cfcdSJohan Almbladh emit(ctx, or, MIPS_R_T8, MIPS_R_T9, src);
425eb63cfcdSJohan Almbladh break;
426eb63cfcdSJohan Almbladh case BPF_XOR:
427eb63cfcdSJohan Almbladh case BPF_XOR | BPF_FETCH:
428eb63cfcdSJohan Almbladh emit(ctx, xor, MIPS_R_T8, MIPS_R_T9, src);
429eb63cfcdSJohan Almbladh break;
430eb63cfcdSJohan Almbladh case BPF_XCHG:
431eb63cfcdSJohan Almbladh emit(ctx, move, MIPS_R_T8, src);
432eb63cfcdSJohan Almbladh break;
433eb63cfcdSJohan Almbladh }
434eb63cfcdSJohan Almbladh emit(ctx, sc, MIPS_R_T8, off, dst);
43572570224SJohan Almbladh emit(ctx, LLSC_beqz, MIPS_R_T8, -16 - LLSC_offset);
436eb63cfcdSJohan Almbladh emit(ctx, nop); /* Delay slot */
437eb63cfcdSJohan Almbladh
438eb63cfcdSJohan Almbladh if (code & BPF_FETCH) {
439eb63cfcdSJohan Almbladh emit(ctx, move, src, MIPS_R_T9);
440eb63cfcdSJohan Almbladh clobber_reg(ctx, src);
441eb63cfcdSJohan Almbladh }
442eb63cfcdSJohan Almbladh }
443eb63cfcdSJohan Almbladh
444eb63cfcdSJohan Almbladh /* Atomic compare-and-exchange (32-bit) */
emit_cmpxchg_r(struct jit_context * ctx,u8 dst,u8 src,u8 res,s16 off)445eb63cfcdSJohan Almbladh void emit_cmpxchg_r(struct jit_context *ctx, u8 dst, u8 src, u8 res, s16 off)
446eb63cfcdSJohan Almbladh {
44772570224SJohan Almbladh LLSC_sync(ctx);
448eb63cfcdSJohan Almbladh emit(ctx, ll, MIPS_R_T9, off, dst);
449eb63cfcdSJohan Almbladh emit(ctx, bne, MIPS_R_T9, res, 12);
450eb63cfcdSJohan Almbladh emit(ctx, move, MIPS_R_T8, src); /* Delay slot */
451eb63cfcdSJohan Almbladh emit(ctx, sc, MIPS_R_T8, off, dst);
45272570224SJohan Almbladh emit(ctx, LLSC_beqz, MIPS_R_T8, -20 - LLSC_offset);
453eb63cfcdSJohan Almbladh emit(ctx, move, res, MIPS_R_T9); /* Delay slot */
454eb63cfcdSJohan Almbladh clobber_reg(ctx, res);
455eb63cfcdSJohan Almbladh }
456eb63cfcdSJohan Almbladh
457eb63cfcdSJohan Almbladh /* Swap bytes and truncate a register word or half word */
emit_bswap_r(struct jit_context * ctx,u8 dst,u32 width)458eb63cfcdSJohan Almbladh void emit_bswap_r(struct jit_context *ctx, u8 dst, u32 width)
459eb63cfcdSJohan Almbladh {
460eb63cfcdSJohan Almbladh u8 tmp = MIPS_R_T8;
461eb63cfcdSJohan Almbladh u8 msk = MIPS_R_T9;
462eb63cfcdSJohan Almbladh
463eb63cfcdSJohan Almbladh switch (width) {
464eb63cfcdSJohan Almbladh /* Swap bytes in a word */
465eb63cfcdSJohan Almbladh case 32:
466eb63cfcdSJohan Almbladh if (cpu_has_mips32r2 || cpu_has_mips32r6) {
467eb63cfcdSJohan Almbladh emit(ctx, wsbh, dst, dst);
468eb63cfcdSJohan Almbladh emit(ctx, rotr, dst, dst, 16);
469eb63cfcdSJohan Almbladh } else {
470eb63cfcdSJohan Almbladh emit(ctx, sll, tmp, dst, 16); /* tmp = dst << 16 */
471eb63cfcdSJohan Almbladh emit(ctx, srl, dst, dst, 16); /* dst = dst >> 16 */
472eb63cfcdSJohan Almbladh emit(ctx, or, dst, dst, tmp); /* dst = dst | tmp */
473eb63cfcdSJohan Almbladh
474eb63cfcdSJohan Almbladh emit(ctx, lui, msk, 0xff); /* msk = 0x00ff0000 */
475eb63cfcdSJohan Almbladh emit(ctx, ori, msk, msk, 0xff); /* msk = msk | 0xff */
476eb63cfcdSJohan Almbladh
477eb63cfcdSJohan Almbladh emit(ctx, and, tmp, dst, msk); /* tmp = dst & msk */
478eb63cfcdSJohan Almbladh emit(ctx, sll, tmp, tmp, 8); /* tmp = tmp << 8 */
479eb63cfcdSJohan Almbladh emit(ctx, srl, dst, dst, 8); /* dst = dst >> 8 */
480eb63cfcdSJohan Almbladh emit(ctx, and, dst, dst, msk); /* dst = dst & msk */
481eb63cfcdSJohan Almbladh emit(ctx, or, dst, dst, tmp); /* reg = dst | tmp */
482eb63cfcdSJohan Almbladh }
483eb63cfcdSJohan Almbladh break;
484eb63cfcdSJohan Almbladh /* Swap bytes in a half word */
485eb63cfcdSJohan Almbladh case 16:
486eb63cfcdSJohan Almbladh if (cpu_has_mips32r2 || cpu_has_mips32r6) {
487eb63cfcdSJohan Almbladh emit(ctx, wsbh, dst, dst);
488eb63cfcdSJohan Almbladh emit(ctx, andi, dst, dst, 0xffff);
489eb63cfcdSJohan Almbladh } else {
490eb63cfcdSJohan Almbladh emit(ctx, andi, tmp, dst, 0xff00); /* t = d & 0xff00 */
491eb63cfcdSJohan Almbladh emit(ctx, srl, tmp, tmp, 8); /* t = t >> 8 */
492eb63cfcdSJohan Almbladh emit(ctx, andi, dst, dst, 0x00ff); /* d = d & 0x00ff */
493eb63cfcdSJohan Almbladh emit(ctx, sll, dst, dst, 8); /* d = d << 8 */
494eb63cfcdSJohan Almbladh emit(ctx, or, dst, dst, tmp); /* d = d | t */
495eb63cfcdSJohan Almbladh }
496eb63cfcdSJohan Almbladh break;
497eb63cfcdSJohan Almbladh }
498eb63cfcdSJohan Almbladh clobber_reg(ctx, dst);
499eb63cfcdSJohan Almbladh }
500eb63cfcdSJohan Almbladh
501eb63cfcdSJohan Almbladh /* Validate jump immediate range */
valid_jmp_i(u8 op,s32 imm)502eb63cfcdSJohan Almbladh bool valid_jmp_i(u8 op, s32 imm)
503eb63cfcdSJohan Almbladh {
504eb63cfcdSJohan Almbladh switch (op) {
505eb63cfcdSJohan Almbladh case JIT_JNOP:
506eb63cfcdSJohan Almbladh /* Immediate value not used */
507eb63cfcdSJohan Almbladh return true;
508eb63cfcdSJohan Almbladh case BPF_JEQ:
509eb63cfcdSJohan Almbladh case BPF_JNE:
510eb63cfcdSJohan Almbladh /* No immediate operation */
511eb63cfcdSJohan Almbladh return false;
512eb63cfcdSJohan Almbladh case BPF_JSET:
513eb63cfcdSJohan Almbladh case JIT_JNSET:
514eb63cfcdSJohan Almbladh /* imm must be 16 bits unsigned */
515eb63cfcdSJohan Almbladh return imm >= 0 && imm <= 0xffff;
516eb63cfcdSJohan Almbladh case BPF_JGE:
517eb63cfcdSJohan Almbladh case BPF_JLT:
518eb63cfcdSJohan Almbladh case BPF_JSGE:
519eb63cfcdSJohan Almbladh case BPF_JSLT:
520eb63cfcdSJohan Almbladh /* imm must be 16 bits */
521eb63cfcdSJohan Almbladh return imm >= -0x8000 && imm <= 0x7fff;
522eb63cfcdSJohan Almbladh case BPF_JGT:
523eb63cfcdSJohan Almbladh case BPF_JLE:
524eb63cfcdSJohan Almbladh case BPF_JSGT:
525eb63cfcdSJohan Almbladh case BPF_JSLE:
526eb63cfcdSJohan Almbladh /* imm + 1 must be 16 bits */
527eb63cfcdSJohan Almbladh return imm >= -0x8001 && imm <= 0x7ffe;
528eb63cfcdSJohan Almbladh }
529eb63cfcdSJohan Almbladh return false;
530eb63cfcdSJohan Almbladh }
531eb63cfcdSJohan Almbladh
532eb63cfcdSJohan Almbladh /* Invert a conditional jump operation */
invert_jmp(u8 op)533eb63cfcdSJohan Almbladh static u8 invert_jmp(u8 op)
534eb63cfcdSJohan Almbladh {
535eb63cfcdSJohan Almbladh switch (op) {
536eb63cfcdSJohan Almbladh case BPF_JA: return JIT_JNOP;
537eb63cfcdSJohan Almbladh case BPF_JEQ: return BPF_JNE;
538eb63cfcdSJohan Almbladh case BPF_JNE: return BPF_JEQ;
539eb63cfcdSJohan Almbladh case BPF_JSET: return JIT_JNSET;
540eb63cfcdSJohan Almbladh case BPF_JGT: return BPF_JLE;
541eb63cfcdSJohan Almbladh case BPF_JGE: return BPF_JLT;
542eb63cfcdSJohan Almbladh case BPF_JLT: return BPF_JGE;
543eb63cfcdSJohan Almbladh case BPF_JLE: return BPF_JGT;
544eb63cfcdSJohan Almbladh case BPF_JSGT: return BPF_JSLE;
545eb63cfcdSJohan Almbladh case BPF_JSGE: return BPF_JSLT;
546eb63cfcdSJohan Almbladh case BPF_JSLT: return BPF_JSGE;
547eb63cfcdSJohan Almbladh case BPF_JSLE: return BPF_JSGT;
548eb63cfcdSJohan Almbladh }
549eb63cfcdSJohan Almbladh return 0;
550eb63cfcdSJohan Almbladh }
551eb63cfcdSJohan Almbladh
552eb63cfcdSJohan Almbladh /* Prepare a PC-relative jump operation */
setup_jmp(struct jit_context * ctx,u8 bpf_op,s16 bpf_off,u8 * jit_op,s32 * jit_off)553eb63cfcdSJohan Almbladh static void setup_jmp(struct jit_context *ctx, u8 bpf_op,
554eb63cfcdSJohan Almbladh s16 bpf_off, u8 *jit_op, s32 *jit_off)
555eb63cfcdSJohan Almbladh {
556eb63cfcdSJohan Almbladh u32 *descp = &ctx->descriptors[ctx->bpf_index];
557eb63cfcdSJohan Almbladh int op = bpf_op;
558eb63cfcdSJohan Almbladh int offset = 0;
559eb63cfcdSJohan Almbladh
560eb63cfcdSJohan Almbladh /* Do not compute offsets on the first pass */
561eb63cfcdSJohan Almbladh if (INDEX(*descp) == 0)
562eb63cfcdSJohan Almbladh goto done;
563eb63cfcdSJohan Almbladh
564eb63cfcdSJohan Almbladh /* Skip jumps never taken */
565eb63cfcdSJohan Almbladh if (bpf_op == JIT_JNOP)
566eb63cfcdSJohan Almbladh goto done;
567eb63cfcdSJohan Almbladh
568eb63cfcdSJohan Almbladh /* Convert jumps always taken */
569eb63cfcdSJohan Almbladh if (bpf_op == BPF_JA)
570eb63cfcdSJohan Almbladh *descp |= JIT_DESC_CONVERT;
571eb63cfcdSJohan Almbladh
572eb63cfcdSJohan Almbladh /*
573eb63cfcdSJohan Almbladh * Current ctx->jit_index points to the start of the branch preamble.
574eb63cfcdSJohan Almbladh * Since the preamble differs among different branch conditionals,
575eb63cfcdSJohan Almbladh * the current index cannot be used to compute the branch offset.
576eb63cfcdSJohan Almbladh * Instead, we use the offset table value for the next instruction,
577eb63cfcdSJohan Almbladh * which gives the index immediately after the branch delay slot.
578eb63cfcdSJohan Almbladh */
579eb63cfcdSJohan Almbladh if (!CONVERTED(*descp)) {
580eb63cfcdSJohan Almbladh int target = ctx->bpf_index + bpf_off + 1;
581eb63cfcdSJohan Almbladh int origin = ctx->bpf_index + 1;
582eb63cfcdSJohan Almbladh
583eb63cfcdSJohan Almbladh offset = (INDEX(ctx->descriptors[target]) -
584eb63cfcdSJohan Almbladh INDEX(ctx->descriptors[origin]) + 1) * sizeof(u32);
585eb63cfcdSJohan Almbladh }
586eb63cfcdSJohan Almbladh
587eb63cfcdSJohan Almbladh /*
588eb63cfcdSJohan Almbladh * The PC-relative branch offset field on MIPS is 18 bits signed,
589eb63cfcdSJohan Almbladh * so if the computed offset is larger than this we generate a an
590eb63cfcdSJohan Almbladh * absolute jump that we skip with an inverted conditional branch.
591eb63cfcdSJohan Almbladh */
592eb63cfcdSJohan Almbladh if (CONVERTED(*descp) || offset < -0x20000 || offset > 0x1ffff) {
593eb63cfcdSJohan Almbladh offset = 3 * sizeof(u32);
594eb63cfcdSJohan Almbladh op = invert_jmp(bpf_op);
595eb63cfcdSJohan Almbladh ctx->changes += !CONVERTED(*descp);
596eb63cfcdSJohan Almbladh *descp |= JIT_DESC_CONVERT;
597eb63cfcdSJohan Almbladh }
598eb63cfcdSJohan Almbladh
599eb63cfcdSJohan Almbladh done:
600eb63cfcdSJohan Almbladh *jit_off = offset;
601eb63cfcdSJohan Almbladh *jit_op = op;
602eb63cfcdSJohan Almbladh }
603eb63cfcdSJohan Almbladh
604eb63cfcdSJohan Almbladh /* Prepare a PC-relative jump operation with immediate conditional */
setup_jmp_i(struct jit_context * ctx,s32 imm,u8 width,u8 bpf_op,s16 bpf_off,u8 * jit_op,s32 * jit_off)605eb63cfcdSJohan Almbladh void setup_jmp_i(struct jit_context *ctx, s32 imm, u8 width,
606eb63cfcdSJohan Almbladh u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off)
607eb63cfcdSJohan Almbladh {
608eb63cfcdSJohan Almbladh bool always = false;
609eb63cfcdSJohan Almbladh bool never = false;
610eb63cfcdSJohan Almbladh
611eb63cfcdSJohan Almbladh switch (bpf_op) {
612eb63cfcdSJohan Almbladh case BPF_JEQ:
613eb63cfcdSJohan Almbladh case BPF_JNE:
614eb63cfcdSJohan Almbladh break;
615eb63cfcdSJohan Almbladh case BPF_JSET:
616eb63cfcdSJohan Almbladh case BPF_JLT:
617eb63cfcdSJohan Almbladh never = imm == 0;
618eb63cfcdSJohan Almbladh break;
619eb63cfcdSJohan Almbladh case BPF_JGE:
620eb63cfcdSJohan Almbladh always = imm == 0;
621eb63cfcdSJohan Almbladh break;
622eb63cfcdSJohan Almbladh case BPF_JGT:
623eb63cfcdSJohan Almbladh never = (u32)imm == U32_MAX;
624eb63cfcdSJohan Almbladh break;
625eb63cfcdSJohan Almbladh case BPF_JLE:
626eb63cfcdSJohan Almbladh always = (u32)imm == U32_MAX;
627eb63cfcdSJohan Almbladh break;
628eb63cfcdSJohan Almbladh case BPF_JSGT:
629eb63cfcdSJohan Almbladh never = imm == S32_MAX && width == 32;
630eb63cfcdSJohan Almbladh break;
631eb63cfcdSJohan Almbladh case BPF_JSGE:
632eb63cfcdSJohan Almbladh always = imm == S32_MIN && width == 32;
633eb63cfcdSJohan Almbladh break;
634eb63cfcdSJohan Almbladh case BPF_JSLT:
635eb63cfcdSJohan Almbladh never = imm == S32_MIN && width == 32;
636eb63cfcdSJohan Almbladh break;
637eb63cfcdSJohan Almbladh case BPF_JSLE:
638eb63cfcdSJohan Almbladh always = imm == S32_MAX && width == 32;
639eb63cfcdSJohan Almbladh break;
640eb63cfcdSJohan Almbladh }
641eb63cfcdSJohan Almbladh
642eb63cfcdSJohan Almbladh if (never)
643eb63cfcdSJohan Almbladh bpf_op = JIT_JNOP;
644eb63cfcdSJohan Almbladh if (always)
645eb63cfcdSJohan Almbladh bpf_op = BPF_JA;
646eb63cfcdSJohan Almbladh setup_jmp(ctx, bpf_op, bpf_off, jit_op, jit_off);
647eb63cfcdSJohan Almbladh }
648eb63cfcdSJohan Almbladh
649eb63cfcdSJohan Almbladh /* Prepare a PC-relative jump operation with register conditional */
setup_jmp_r(struct jit_context * ctx,bool same_reg,u8 bpf_op,s16 bpf_off,u8 * jit_op,s32 * jit_off)650eb63cfcdSJohan Almbladh void setup_jmp_r(struct jit_context *ctx, bool same_reg,
651eb63cfcdSJohan Almbladh u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off)
652eb63cfcdSJohan Almbladh {
653eb63cfcdSJohan Almbladh switch (bpf_op) {
654eb63cfcdSJohan Almbladh case BPF_JSET:
655eb63cfcdSJohan Almbladh break;
656eb63cfcdSJohan Almbladh case BPF_JEQ:
657eb63cfcdSJohan Almbladh case BPF_JGE:
658eb63cfcdSJohan Almbladh case BPF_JLE:
659eb63cfcdSJohan Almbladh case BPF_JSGE:
660eb63cfcdSJohan Almbladh case BPF_JSLE:
661eb63cfcdSJohan Almbladh if (same_reg)
662eb63cfcdSJohan Almbladh bpf_op = BPF_JA;
663eb63cfcdSJohan Almbladh break;
664eb63cfcdSJohan Almbladh case BPF_JNE:
665eb63cfcdSJohan Almbladh case BPF_JLT:
666eb63cfcdSJohan Almbladh case BPF_JGT:
667eb63cfcdSJohan Almbladh case BPF_JSGT:
668eb63cfcdSJohan Almbladh case BPF_JSLT:
669eb63cfcdSJohan Almbladh if (same_reg)
670eb63cfcdSJohan Almbladh bpf_op = JIT_JNOP;
671eb63cfcdSJohan Almbladh break;
672eb63cfcdSJohan Almbladh }
673eb63cfcdSJohan Almbladh setup_jmp(ctx, bpf_op, bpf_off, jit_op, jit_off);
674eb63cfcdSJohan Almbladh }
675eb63cfcdSJohan Almbladh
676eb63cfcdSJohan Almbladh /* Finish a PC-relative jump operation */
finish_jmp(struct jit_context * ctx,u8 jit_op,s16 bpf_off)677eb63cfcdSJohan Almbladh int finish_jmp(struct jit_context *ctx, u8 jit_op, s16 bpf_off)
678eb63cfcdSJohan Almbladh {
679eb63cfcdSJohan Almbladh /* Emit conditional branch delay slot */
680eb63cfcdSJohan Almbladh if (jit_op != JIT_JNOP)
681eb63cfcdSJohan Almbladh emit(ctx, nop);
682eb63cfcdSJohan Almbladh /*
683eb63cfcdSJohan Almbladh * Emit an absolute long jump with delay slot,
684eb63cfcdSJohan Almbladh * if the PC-relative branch was converted.
685eb63cfcdSJohan Almbladh */
686eb63cfcdSJohan Almbladh if (CONVERTED(ctx->descriptors[ctx->bpf_index])) {
687eb63cfcdSJohan Almbladh int target = get_target(ctx, ctx->bpf_index + bpf_off + 1);
688eb63cfcdSJohan Almbladh
689eb63cfcdSJohan Almbladh if (target < 0)
690eb63cfcdSJohan Almbladh return -1;
691eb63cfcdSJohan Almbladh emit(ctx, j, target);
692eb63cfcdSJohan Almbladh emit(ctx, nop);
693eb63cfcdSJohan Almbladh }
694eb63cfcdSJohan Almbladh return 0;
695eb63cfcdSJohan Almbladh }
696eb63cfcdSJohan Almbladh
697eb63cfcdSJohan Almbladh /* Jump immediate (32-bit) */
emit_jmp_i(struct jit_context * ctx,u8 dst,s32 imm,s32 off,u8 op)698eb63cfcdSJohan Almbladh void emit_jmp_i(struct jit_context *ctx, u8 dst, s32 imm, s32 off, u8 op)
699eb63cfcdSJohan Almbladh {
700eb63cfcdSJohan Almbladh switch (op) {
701eb63cfcdSJohan Almbladh /* No-op, used internally for branch optimization */
702eb63cfcdSJohan Almbladh case JIT_JNOP:
703eb63cfcdSJohan Almbladh break;
704eb63cfcdSJohan Almbladh /* PC += off if dst & imm */
705eb63cfcdSJohan Almbladh case BPF_JSET:
706eb63cfcdSJohan Almbladh emit(ctx, andi, MIPS_R_T9, dst, (u16)imm);
707eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
708eb63cfcdSJohan Almbladh break;
709eb63cfcdSJohan Almbladh /* PC += off if (dst & imm) == 0 (not in BPF, used for long jumps) */
710eb63cfcdSJohan Almbladh case JIT_JNSET:
711eb63cfcdSJohan Almbladh emit(ctx, andi, MIPS_R_T9, dst, (u16)imm);
712eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
713eb63cfcdSJohan Almbladh break;
714eb63cfcdSJohan Almbladh /* PC += off if dst > imm */
715eb63cfcdSJohan Almbladh case BPF_JGT:
716eb63cfcdSJohan Almbladh emit(ctx, sltiu, MIPS_R_T9, dst, imm + 1);
717eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
718eb63cfcdSJohan Almbladh break;
719eb63cfcdSJohan Almbladh /* PC += off if dst >= imm */
720eb63cfcdSJohan Almbladh case BPF_JGE:
721eb63cfcdSJohan Almbladh emit(ctx, sltiu, MIPS_R_T9, dst, imm);
722eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
723eb63cfcdSJohan Almbladh break;
724eb63cfcdSJohan Almbladh /* PC += off if dst < imm */
725eb63cfcdSJohan Almbladh case BPF_JLT:
726eb63cfcdSJohan Almbladh emit(ctx, sltiu, MIPS_R_T9, dst, imm);
727eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
728eb63cfcdSJohan Almbladh break;
729eb63cfcdSJohan Almbladh /* PC += off if dst <= imm */
730eb63cfcdSJohan Almbladh case BPF_JLE:
731eb63cfcdSJohan Almbladh emit(ctx, sltiu, MIPS_R_T9, dst, imm + 1);
732eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
733eb63cfcdSJohan Almbladh break;
734eb63cfcdSJohan Almbladh /* PC += off if dst > imm (signed) */
735eb63cfcdSJohan Almbladh case BPF_JSGT:
736eb63cfcdSJohan Almbladh emit(ctx, slti, MIPS_R_T9, dst, imm + 1);
737eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
738eb63cfcdSJohan Almbladh break;
739eb63cfcdSJohan Almbladh /* PC += off if dst >= imm (signed) */
740eb63cfcdSJohan Almbladh case BPF_JSGE:
741eb63cfcdSJohan Almbladh emit(ctx, slti, MIPS_R_T9, dst, imm);
742eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
743eb63cfcdSJohan Almbladh break;
744eb63cfcdSJohan Almbladh /* PC += off if dst < imm (signed) */
745eb63cfcdSJohan Almbladh case BPF_JSLT:
746eb63cfcdSJohan Almbladh emit(ctx, slti, MIPS_R_T9, dst, imm);
747eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
748eb63cfcdSJohan Almbladh break;
749eb63cfcdSJohan Almbladh /* PC += off if dst <= imm (signed) */
750eb63cfcdSJohan Almbladh case BPF_JSLE:
751eb63cfcdSJohan Almbladh emit(ctx, slti, MIPS_R_T9, dst, imm + 1);
752eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
753eb63cfcdSJohan Almbladh break;
754eb63cfcdSJohan Almbladh }
755eb63cfcdSJohan Almbladh }
756eb63cfcdSJohan Almbladh
757eb63cfcdSJohan Almbladh /* Jump register (32-bit) */
emit_jmp_r(struct jit_context * ctx,u8 dst,u8 src,s32 off,u8 op)758eb63cfcdSJohan Almbladh void emit_jmp_r(struct jit_context *ctx, u8 dst, u8 src, s32 off, u8 op)
759eb63cfcdSJohan Almbladh {
760eb63cfcdSJohan Almbladh switch (op) {
761eb63cfcdSJohan Almbladh /* No-op, used internally for branch optimization */
762eb63cfcdSJohan Almbladh case JIT_JNOP:
763eb63cfcdSJohan Almbladh break;
764eb63cfcdSJohan Almbladh /* PC += off if dst == src */
765eb63cfcdSJohan Almbladh case BPF_JEQ:
766eb63cfcdSJohan Almbladh emit(ctx, beq, dst, src, off);
767eb63cfcdSJohan Almbladh break;
768eb63cfcdSJohan Almbladh /* PC += off if dst != src */
769eb63cfcdSJohan Almbladh case BPF_JNE:
770eb63cfcdSJohan Almbladh emit(ctx, bne, dst, src, off);
771eb63cfcdSJohan Almbladh break;
772eb63cfcdSJohan Almbladh /* PC += off if dst & src */
773eb63cfcdSJohan Almbladh case BPF_JSET:
774eb63cfcdSJohan Almbladh emit(ctx, and, MIPS_R_T9, dst, src);
775eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
776eb63cfcdSJohan Almbladh break;
777eb63cfcdSJohan Almbladh /* PC += off if (dst & imm) == 0 (not in BPF, used for long jumps) */
778eb63cfcdSJohan Almbladh case JIT_JNSET:
779eb63cfcdSJohan Almbladh emit(ctx, and, MIPS_R_T9, dst, src);
780eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
781eb63cfcdSJohan Almbladh break;
782eb63cfcdSJohan Almbladh /* PC += off if dst > src */
783eb63cfcdSJohan Almbladh case BPF_JGT:
784eb63cfcdSJohan Almbladh emit(ctx, sltu, MIPS_R_T9, src, dst);
785eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
786eb63cfcdSJohan Almbladh break;
787eb63cfcdSJohan Almbladh /* PC += off if dst >= src */
788eb63cfcdSJohan Almbladh case BPF_JGE:
789eb63cfcdSJohan Almbladh emit(ctx, sltu, MIPS_R_T9, dst, src);
790eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
791eb63cfcdSJohan Almbladh break;
792eb63cfcdSJohan Almbladh /* PC += off if dst < src */
793eb63cfcdSJohan Almbladh case BPF_JLT:
794eb63cfcdSJohan Almbladh emit(ctx, sltu, MIPS_R_T9, dst, src);
795eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
796eb63cfcdSJohan Almbladh break;
797eb63cfcdSJohan Almbladh /* PC += off if dst <= src */
798eb63cfcdSJohan Almbladh case BPF_JLE:
799eb63cfcdSJohan Almbladh emit(ctx, sltu, MIPS_R_T9, src, dst);
800eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
801eb63cfcdSJohan Almbladh break;
802eb63cfcdSJohan Almbladh /* PC += off if dst > src (signed) */
803eb63cfcdSJohan Almbladh case BPF_JSGT:
804eb63cfcdSJohan Almbladh emit(ctx, slt, MIPS_R_T9, src, dst);
805eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
806eb63cfcdSJohan Almbladh break;
807eb63cfcdSJohan Almbladh /* PC += off if dst >= src (signed) */
808eb63cfcdSJohan Almbladh case BPF_JSGE:
809eb63cfcdSJohan Almbladh emit(ctx, slt, MIPS_R_T9, dst, src);
810eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
811eb63cfcdSJohan Almbladh break;
812eb63cfcdSJohan Almbladh /* PC += off if dst < src (signed) */
813eb63cfcdSJohan Almbladh case BPF_JSLT:
814eb63cfcdSJohan Almbladh emit(ctx, slt, MIPS_R_T9, dst, src);
815eb63cfcdSJohan Almbladh emit(ctx, bnez, MIPS_R_T9, off);
816eb63cfcdSJohan Almbladh break;
817eb63cfcdSJohan Almbladh /* PC += off if dst <= src (signed) */
818eb63cfcdSJohan Almbladh case BPF_JSLE:
819eb63cfcdSJohan Almbladh emit(ctx, slt, MIPS_R_T9, src, dst);
820eb63cfcdSJohan Almbladh emit(ctx, beqz, MIPS_R_T9, off);
821eb63cfcdSJohan Almbladh break;
822eb63cfcdSJohan Almbladh }
823eb63cfcdSJohan Almbladh }
824eb63cfcdSJohan Almbladh
825eb63cfcdSJohan Almbladh /* Jump always */
emit_ja(struct jit_context * ctx,s16 off)826eb63cfcdSJohan Almbladh int emit_ja(struct jit_context *ctx, s16 off)
827eb63cfcdSJohan Almbladh {
828eb63cfcdSJohan Almbladh int target = get_target(ctx, ctx->bpf_index + off + 1);
829eb63cfcdSJohan Almbladh
830eb63cfcdSJohan Almbladh if (target < 0)
831eb63cfcdSJohan Almbladh return -1;
832eb63cfcdSJohan Almbladh emit(ctx, j, target);
833eb63cfcdSJohan Almbladh emit(ctx, nop);
834eb63cfcdSJohan Almbladh return 0;
835eb63cfcdSJohan Almbladh }
836eb63cfcdSJohan Almbladh
837eb63cfcdSJohan Almbladh /* Jump to epilogue */
emit_exit(struct jit_context * ctx)838eb63cfcdSJohan Almbladh int emit_exit(struct jit_context *ctx)
839eb63cfcdSJohan Almbladh {
840eb63cfcdSJohan Almbladh int target = get_target(ctx, ctx->program->len);
841eb63cfcdSJohan Almbladh
842eb63cfcdSJohan Almbladh if (target < 0)
843eb63cfcdSJohan Almbladh return -1;
844eb63cfcdSJohan Almbladh emit(ctx, j, target);
845eb63cfcdSJohan Almbladh emit(ctx, nop);
846eb63cfcdSJohan Almbladh return 0;
847eb63cfcdSJohan Almbladh }
848eb63cfcdSJohan Almbladh
849eb63cfcdSJohan Almbladh /* Build the program body from eBPF bytecode */
build_body(struct jit_context * ctx)850eb63cfcdSJohan Almbladh static int build_body(struct jit_context *ctx)
851eb63cfcdSJohan Almbladh {
852eb63cfcdSJohan Almbladh const struct bpf_prog *prog = ctx->program;
853eb63cfcdSJohan Almbladh unsigned int i;
854eb63cfcdSJohan Almbladh
855eb63cfcdSJohan Almbladh ctx->stack_used = 0;
856eb63cfcdSJohan Almbladh for (i = 0; i < prog->len; i++) {
857eb63cfcdSJohan Almbladh const struct bpf_insn *insn = &prog->insnsi[i];
858eb63cfcdSJohan Almbladh u32 *descp = &ctx->descriptors[i];
859eb63cfcdSJohan Almbladh int ret;
860eb63cfcdSJohan Almbladh
861eb63cfcdSJohan Almbladh access_reg(ctx, insn->src_reg);
862eb63cfcdSJohan Almbladh access_reg(ctx, insn->dst_reg);
863eb63cfcdSJohan Almbladh
864eb63cfcdSJohan Almbladh ctx->bpf_index = i;
865eb63cfcdSJohan Almbladh if (ctx->target == NULL) {
866eb63cfcdSJohan Almbladh ctx->changes += INDEX(*descp) != ctx->jit_index;
867eb63cfcdSJohan Almbladh *descp &= JIT_DESC_CONVERT;
868eb63cfcdSJohan Almbladh *descp |= ctx->jit_index;
869eb63cfcdSJohan Almbladh }
870eb63cfcdSJohan Almbladh
871eb63cfcdSJohan Almbladh ret = build_insn(insn, ctx);
872eb63cfcdSJohan Almbladh if (ret < 0)
873eb63cfcdSJohan Almbladh return ret;
874eb63cfcdSJohan Almbladh
875eb63cfcdSJohan Almbladh if (ret > 0) {
876eb63cfcdSJohan Almbladh i++;
877eb63cfcdSJohan Almbladh if (ctx->target == NULL)
878eb63cfcdSJohan Almbladh descp[1] = ctx->jit_index;
879eb63cfcdSJohan Almbladh }
880eb63cfcdSJohan Almbladh }
881eb63cfcdSJohan Almbladh
882eb63cfcdSJohan Almbladh /* Store the end offset, where the epilogue begins */
883eb63cfcdSJohan Almbladh ctx->descriptors[prog->len] = ctx->jit_index;
884eb63cfcdSJohan Almbladh return 0;
885eb63cfcdSJohan Almbladh }
886eb63cfcdSJohan Almbladh
887eb63cfcdSJohan Almbladh /* Set the branch conversion flag on all instructions */
set_convert_flag(struct jit_context * ctx,bool enable)888eb63cfcdSJohan Almbladh static void set_convert_flag(struct jit_context *ctx, bool enable)
889eb63cfcdSJohan Almbladh {
890eb63cfcdSJohan Almbladh const struct bpf_prog *prog = ctx->program;
891eb63cfcdSJohan Almbladh u32 flag = enable ? JIT_DESC_CONVERT : 0;
892eb63cfcdSJohan Almbladh unsigned int i;
893eb63cfcdSJohan Almbladh
894eb63cfcdSJohan Almbladh for (i = 0; i <= prog->len; i++)
895eb63cfcdSJohan Almbladh ctx->descriptors[i] = INDEX(ctx->descriptors[i]) | flag;
896eb63cfcdSJohan Almbladh }
897eb63cfcdSJohan Almbladh
jit_fill_hole(void * area,unsigned int size)898eb63cfcdSJohan Almbladh static void jit_fill_hole(void *area, unsigned int size)
899eb63cfcdSJohan Almbladh {
900eb63cfcdSJohan Almbladh u32 *p;
901eb63cfcdSJohan Almbladh
902eb63cfcdSJohan Almbladh /* We are guaranteed to have aligned memory. */
903eb63cfcdSJohan Almbladh for (p = area; size >= sizeof(u32); size -= sizeof(u32))
904eb63cfcdSJohan Almbladh uasm_i_break(&p, BRK_BUG); /* Increments p */
905eb63cfcdSJohan Almbladh }
906eb63cfcdSJohan Almbladh
bpf_jit_needs_zext(void)907eb63cfcdSJohan Almbladh bool bpf_jit_needs_zext(void)
908eb63cfcdSJohan Almbladh {
909eb63cfcdSJohan Almbladh return true;
910eb63cfcdSJohan Almbladh }
911eb63cfcdSJohan Almbladh
bpf_int_jit_compile(struct bpf_prog * prog)912eb63cfcdSJohan Almbladh struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
913eb63cfcdSJohan Almbladh {
914eb63cfcdSJohan Almbladh struct bpf_prog *tmp, *orig_prog = prog;
915eb63cfcdSJohan Almbladh struct bpf_binary_header *header = NULL;
916eb63cfcdSJohan Almbladh struct jit_context ctx;
917eb63cfcdSJohan Almbladh bool tmp_blinded = false;
918eb63cfcdSJohan Almbladh unsigned int tmp_idx;
919eb63cfcdSJohan Almbladh unsigned int image_size;
920eb63cfcdSJohan Almbladh u8 *image_ptr;
921eb63cfcdSJohan Almbladh int tries;
922eb63cfcdSJohan Almbladh
923eb63cfcdSJohan Almbladh /*
924eb63cfcdSJohan Almbladh * If BPF JIT was not enabled then we must fall back to
925eb63cfcdSJohan Almbladh * the interpreter.
926eb63cfcdSJohan Almbladh */
927eb63cfcdSJohan Almbladh if (!prog->jit_requested)
928eb63cfcdSJohan Almbladh return orig_prog;
929eb63cfcdSJohan Almbladh /*
930eb63cfcdSJohan Almbladh * If constant blinding was enabled and we failed during blinding
931eb63cfcdSJohan Almbladh * then we must fall back to the interpreter. Otherwise, we save
932eb63cfcdSJohan Almbladh * the new JITed code.
933eb63cfcdSJohan Almbladh */
934eb63cfcdSJohan Almbladh tmp = bpf_jit_blind_constants(prog);
935eb63cfcdSJohan Almbladh if (IS_ERR(tmp))
936eb63cfcdSJohan Almbladh return orig_prog;
937eb63cfcdSJohan Almbladh if (tmp != prog) {
938eb63cfcdSJohan Almbladh tmp_blinded = true;
939eb63cfcdSJohan Almbladh prog = tmp;
940eb63cfcdSJohan Almbladh }
941eb63cfcdSJohan Almbladh
942eb63cfcdSJohan Almbladh memset(&ctx, 0, sizeof(ctx));
943eb63cfcdSJohan Almbladh ctx.program = prog;
944eb63cfcdSJohan Almbladh
945eb63cfcdSJohan Almbladh /*
946eb63cfcdSJohan Almbladh * Not able to allocate memory for descriptors[], then
947eb63cfcdSJohan Almbladh * we must fall back to the interpreter
948eb63cfcdSJohan Almbladh */
949eb63cfcdSJohan Almbladh ctx.descriptors = kcalloc(prog->len + 1, sizeof(*ctx.descriptors),
950eb63cfcdSJohan Almbladh GFP_KERNEL);
951eb63cfcdSJohan Almbladh if (ctx.descriptors == NULL)
952eb63cfcdSJohan Almbladh goto out_err;
953eb63cfcdSJohan Almbladh
954eb63cfcdSJohan Almbladh /* First pass discovers used resources */
955eb63cfcdSJohan Almbladh if (build_body(&ctx) < 0)
956eb63cfcdSJohan Almbladh goto out_err;
957eb63cfcdSJohan Almbladh /*
958eb63cfcdSJohan Almbladh * Second pass computes instruction offsets.
959eb63cfcdSJohan Almbladh * If any PC-relative branches are out of range, a sequence of
960eb63cfcdSJohan Almbladh * a PC-relative branch + a jump is generated, and we have to
961eb63cfcdSJohan Almbladh * try again from the beginning to generate the new offsets.
962eb63cfcdSJohan Almbladh * This is done until no additional conversions are necessary.
963eb63cfcdSJohan Almbladh * The last two iterations are done with all branches being
964eb63cfcdSJohan Almbladh * converted, to guarantee offset table convergence within a
965eb63cfcdSJohan Almbladh * fixed number of iterations.
966eb63cfcdSJohan Almbladh */
967eb63cfcdSJohan Almbladh ctx.jit_index = 0;
968eb63cfcdSJohan Almbladh build_prologue(&ctx);
969eb63cfcdSJohan Almbladh tmp_idx = ctx.jit_index;
970eb63cfcdSJohan Almbladh
971eb63cfcdSJohan Almbladh tries = JIT_MAX_ITERATIONS;
972eb63cfcdSJohan Almbladh do {
973eb63cfcdSJohan Almbladh ctx.jit_index = tmp_idx;
974eb63cfcdSJohan Almbladh ctx.changes = 0;
975eb63cfcdSJohan Almbladh if (tries == 2)
976eb63cfcdSJohan Almbladh set_convert_flag(&ctx, true);
977eb63cfcdSJohan Almbladh if (build_body(&ctx) < 0)
978eb63cfcdSJohan Almbladh goto out_err;
979eb63cfcdSJohan Almbladh } while (ctx.changes > 0 && --tries > 0);
980eb63cfcdSJohan Almbladh
981eb63cfcdSJohan Almbladh if (WARN_ONCE(ctx.changes > 0, "JIT offsets failed to converge"))
982eb63cfcdSJohan Almbladh goto out_err;
983eb63cfcdSJohan Almbladh
984eb63cfcdSJohan Almbladh build_epilogue(&ctx, MIPS_R_RA);
985eb63cfcdSJohan Almbladh
986eb63cfcdSJohan Almbladh /* Now we know the size of the structure to make */
987eb63cfcdSJohan Almbladh image_size = sizeof(u32) * ctx.jit_index;
988eb63cfcdSJohan Almbladh header = bpf_jit_binary_alloc(image_size, &image_ptr,
989eb63cfcdSJohan Almbladh sizeof(u32), jit_fill_hole);
990eb63cfcdSJohan Almbladh /*
991eb63cfcdSJohan Almbladh * Not able to allocate memory for the structure then
992eb63cfcdSJohan Almbladh * we must fall back to the interpretation
993eb63cfcdSJohan Almbladh */
994eb63cfcdSJohan Almbladh if (header == NULL)
995eb63cfcdSJohan Almbladh goto out_err;
996eb63cfcdSJohan Almbladh
997eb63cfcdSJohan Almbladh /* Actual pass to generate final JIT code */
998eb63cfcdSJohan Almbladh ctx.target = (u32 *)image_ptr;
999eb63cfcdSJohan Almbladh ctx.jit_index = 0;
1000eb63cfcdSJohan Almbladh
1001eb63cfcdSJohan Almbladh /*
1002eb63cfcdSJohan Almbladh * If building the JITed code fails somehow,
1003eb63cfcdSJohan Almbladh * we fall back to the interpretation.
1004eb63cfcdSJohan Almbladh */
1005eb63cfcdSJohan Almbladh build_prologue(&ctx);
1006eb63cfcdSJohan Almbladh if (build_body(&ctx) < 0)
1007eb63cfcdSJohan Almbladh goto out_err;
1008eb63cfcdSJohan Almbladh build_epilogue(&ctx, MIPS_R_RA);
1009eb63cfcdSJohan Almbladh
1010eb63cfcdSJohan Almbladh /* Populate line info meta data */
1011eb63cfcdSJohan Almbladh set_convert_flag(&ctx, false);
1012eb63cfcdSJohan Almbladh bpf_prog_fill_jited_linfo(prog, &ctx.descriptors[1]);
1013eb63cfcdSJohan Almbladh
1014eb63cfcdSJohan Almbladh /* Set as read-only exec and flush instruction cache */
1015*e60adf51SChristophe Leroy if (bpf_jit_binary_lock_ro(header))
1016*e60adf51SChristophe Leroy goto out_err;
1017eb63cfcdSJohan Almbladh flush_icache_range((unsigned long)header,
1018eb63cfcdSJohan Almbladh (unsigned long)&ctx.target[ctx.jit_index]);
1019eb63cfcdSJohan Almbladh
1020eb63cfcdSJohan Almbladh if (bpf_jit_enable > 1)
1021eb63cfcdSJohan Almbladh bpf_jit_dump(prog->len, image_size, 2, ctx.target);
1022eb63cfcdSJohan Almbladh
1023eb63cfcdSJohan Almbladh prog->bpf_func = (void *)ctx.target;
1024eb63cfcdSJohan Almbladh prog->jited = 1;
1025eb63cfcdSJohan Almbladh prog->jited_len = image_size;
1026eb63cfcdSJohan Almbladh
1027eb63cfcdSJohan Almbladh out:
1028eb63cfcdSJohan Almbladh if (tmp_blinded)
1029eb63cfcdSJohan Almbladh bpf_jit_prog_release_other(prog, prog == orig_prog ?
1030eb63cfcdSJohan Almbladh tmp : orig_prog);
1031eb63cfcdSJohan Almbladh kfree(ctx.descriptors);
1032eb63cfcdSJohan Almbladh return prog;
1033eb63cfcdSJohan Almbladh
1034eb63cfcdSJohan Almbladh out_err:
1035eb63cfcdSJohan Almbladh prog = orig_prog;
1036eb63cfcdSJohan Almbladh if (header)
1037eb63cfcdSJohan Almbladh bpf_jit_binary_free(header);
1038eb63cfcdSJohan Almbladh goto out;
1039eb63cfcdSJohan Almbladh }
1040