A new eBPF JIT implementation for MIPS
Subsystems (5)
Johan Almbladh
maintainercover A new eBPF JIT implementation for MIPS
This is an implementation of an eBPF JIT for MIPS I-V and MIPS32/64 r1-r6. The new JIT is written from scratch, but uses the same overall structure as other eBPF JITs.
Before, the MIPS JIT situation looked like this.
- 32-bit: MIPS32, cBPF-only, tests fail
- 64-bit: MIPS64r2-r6, eBPF, tests fail, incomplete eBPF ISA support
The new JIT implementation raises the bar to the following level.
- 32/64-bit: all MIPS ISA, eBPF, all tests pass, full eBPF ISA support
Overview
========
The implementation supports all 32-bit and 64-bit eBPF instructions defined as of this writing, including the recently-added atomics. It is intended to provide good performance for native word size operations, while also being complete so the JIT never has to fall back to the interpreter. The new JIT replaces the current cBPF and eBPF JITs for MIPS.
The implementation is divided into separate files as follows. The source files contains comments describing internal mechanisms and details on things like eBPF-to-CPU register mappings, so I won't repeat that here.
- jit_comp.[ch] code shared between 32-bit and 64-bit JITs - jit_comp32.c 32-bit JIT implementation - jit_comp64.c 64-bit JIT implementation
Both the 32-bit and 64-bit versions map all eBPF registers to native MIPS CPU registers. There are also enough unmapped CPU registers available to allow all eBPF operations implemented natively by the JIT to use only CPU registers without having to resort to stack scratch space.
Some operations are deemed too complex to implement natively in the JIT. Those are instead implemented as a function call to a helper that performs the operation. This is done in the following cases.
- 64-bit div and mod on a 32-bit CPU
- 64-bit atomics on a 32-bit CPU
- 32-bit atomics on a 32-bit CPU that lacks ll/sc instructions
CPU errata workarounds
======================
The JIT implements workarounds for R10000, Loongson-2F and Loongson-3 CPU errata. For the Loongson workarounds, I have used the public information available on the matter.
Loongson people: could you please check if my understanding is correct,
especially for the Loongson-2F jump errata?
Testing
=======
During the development of the JIT, I have added a number of new test cases to the test_bpf.ko test suite to be able to verify correctness of JIT implementations in a more systematic way. The new additions increase the test suite roughly three-fold, with many of the new tests being very extensive and even exhaustive when feasible.
The JIT has been tested by running the test_bpf.ko test suite in QEMU with the following MIPS ISAs, in both big and little endian mode, with and without JIT hardening enabled.
MIPS32r2, MIPS32r6, MIPS64r2, MIPS64r6
For the QEMU r2 targets, the correctness of pre-r2 code emitted has been tested by manually overriding each of the following macros with 0.
cpu_has_llsc, cpu_has_mips_2, cpu_has_mips_r1, cpu_has_mips_r2
Similarly, CPU errata workaround code has been tested by enabling the each of the following configurations for the MIPS64r2 targets.
CONFIG_WAR_R10000
CONFIG_CPU_LOONGSON3_WORKAROUNDS
CONFIG_CPU_NOP_WORKAROUNDS
CONFIG_CPU_JUMP_WORKAROUNDS
The JIT passes all tests in all configurations. Below is the summary for MIPS32r2 in little endian mode.
test_bpf: Summary: 1006 PASSED, 0 FAILED, [994/994 JIT'ed]
test_bpf: test_tail_calls: Summary: 8 PASSED, 0 FAILED, [8/8 JIT'ed]
test_bpf: test_skb_segment: Summary: 2 PASSED, 0 FAILED
According to MIPS ISA reference documentation, the result of a 32-bit ALU arithmetic operation on a 64-bit CPU is unpredictable if an operand register value is not properly sign-extended to 64 bits. To verify the code emitted by the JIT, the code generation engine in QEMU was modifed to flip all low 32 bits if the above condition was not met. With this trip-wire installed, the kernel booted properly in qemu-system-mips64el and all test_bpf.ko tests passed.
Remaining features
==================
While the JIT is complete is terms of eBPF ISA support, this series does not include support for BPF-to-BPF calls and BPF trampolines. Those features are planned to be added in another patch series.
The BPF_ST | BPF_NOSPEC instruction currently emits nothing. This is consistent with the behavior if the MIPS interpreter and the existing eBPF JIT.
Why not build on the existing eBPF JIT?
=======================================
The existing eBPF JIT was originally written for MIPS64. An effort was made to add MIPS32 support to it in commit 716850ab104d ("MIPS: eBPF:
Initial eBPF support for MIPS32 architecture."). That turned out to contain a number of flaws, so eBPF support for MIPS32 was disabled in commit 36366e367ee9 ("MIPS: BPF: Restore MIPS32 cBPF JIT").
The current eBPF JIT for MIPS64 lacks a lot of functionality regarding ALU32, JMP32 and atomic operations. It also lacks 32-bit CPU support on a fundamental level, for example 32-bit CPU register mappings and o32 ABI calling conventions. For optimization purposes, it tracks register usage through the program control flow in order to do zero-extension and sign- extension only when necessary, a static analysis of sorts. In my opinion, having this kind of complexity in JITs, and for which there is not adequate test coverage, is a problem. Such analysis should be done by the verifier, if needed at all. Finally, when I run the BPF test suite test_bpf.ko on the current JIT, there are errors and warnings.
I believe that an eBPF JIT should strive to be correct, complete and optimized, and in that order. The JIT runs after the verifer has audited the program and given its approval. If the JIT then emits code that does something else, it will undermine the eBPF security model. A simple implementation is easier to get correct than a complex one. Furthermore, the real performance hit is not an extra CPU instruction here and there, but when the JIT bails on an unimplemented eBPF instruction and cause the whole program to fall back to the interpreter. My reasoning here boils down to the following.
* The JIT should not contain a static analyzer that tracks branches.
* It is acceptable to emit possibly superfluous sign-/zero-extensions for ALU32 and JMP32 operations on a 64-bit MIPS to guarantee correctness.
* The JIT should handle all eBPF instructions on all MIPS CPUs.
I conclude that the current eBPF MIPS JIT is complex, incomplete and incorrect. For the reasons stated above, I decided to not use the existing JIT implementation.
Johan
Tony Ambardar (1):
MIPS: uasm: Enable muhu opcode for MIPS R6
Johan Almbladh (6):
mips: uasm: Add workaround for Loongson-2F nop CPU errata
mips: bpf: Add eBPF JIT for 32-bit MIPS
mips: bpf: Add new eBPF JIT for 64-bit MIPS
mips: bpf: Add JIT workarounds for CPU errata
mips: bpf: Enable eBPF JITs
mips: bpf: Remove old BPF JIT implementations
MAINTAINERS | 1 + arch/mips/Kconfig | 6 +- arch/mips/include/asm/uasm.h | 5 + arch/mips/mm/uasm-mips.c | 4 +- arch/mips/mm/uasm.c | 3 +- arch/mips/net/Makefile | 8 +- arch/mips/net/bpf_jit.c | 1299 --------------------- arch/mips/net/bpf_jit.h | 81 -- arch/mips/net/bpf_jit_asm.S | 285 ----- arch/mips/net/bpf_jit_comp.c | 1034 +++++++++++++++++ arch/mips/net/bpf_jit_comp.h | 235 ++++ arch/mips/net/bpf_jit_comp32.c | 1899 +++++++++++++++++++++++++++++++ arch/mips/net/bpf_jit_comp64.c | 1054 +++++++++++++++++ arch/mips/net/ebpf_jit.c | 1938 -------------------------------- 14 files changed, 4244 insertions(+), 3608 deletions(-) delete mode 100644 arch/mips/net/bpf_jit.c delete mode 100644 arch/mips/net/bpf_jit.h delete mode 100644 arch/mips/net/bpf_jit_asm.S create mode 100644 arch/mips/net/bpf_jit_comp.c create mode 100644 arch/mips/net/bpf_jit_comp.h create mode 100644 arch/mips/net/bpf_jit_comp32.c create mode 100644 arch/mips/net/bpf_jit_comp64.c delete mode 100644 arch/mips/net/ebpf_jit.c
Signature · 1 line
2.30.2
Johan Almbladh
maintainerPATCH 1/7 MIPS: uasm: Enable muhu opcode for MIPS R6
Enable the 'muhu' instruction, complementing the existing 'mulu', needed to implement a MIPS32 BPF JIT.
Also fix a typo in the existing definition of 'dmulu'.
This patch is a dependency for my 32-bit MIPS eBPF JIT.
--- arch/mips/include/asm/uasm.h | 1 + arch/mips/mm/uasm-mips.c | 4 +++- arch/mips/mm/uasm.c | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-)
arch/mips/include/asm/uasm.h +1 -0
@@ -145,6 +145,7 @@ Ip_u1(_mtlo);Ip_u3u1u2 (_mul);Ip_u1u2 (_multu);Ip_u3u1u2 (_mulu); +Ip_u3u1u2 (_muhu);Ip_u3u1u2 (_nor);Ip_u3u1u2 (_or);Ip_u2u1u3 (_ori);
arch/mips/mm/uasm-mips.c +3 -1
@@ -90,7 +90,7 @@ static const struct insn insn_table[insn_invalid] = { RS | RT | RD}, [insn_dmtc0] = {M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET}, [insn_dmultu] = {M(spec_op, 0, 0, 0, 0, dmultu_op), RS | RT}, - [insn_dmulu] = {M(spec_op, 0, 0, 0, dmult_dmul_op, dmultu_op), + [insn_dmulu] = {M(spec_op, 0, 0, 0, dmultu_dmulu_op, dmultu_op), RS | RT | RD}, [insn_drotr] = {M(spec_op, 1, 0, 0, 0, dsrl_op), RT | RD | RE}, [insn_drotr32] = {M(spec_op, 1, 0, 0, 0, dsrl32_op), RT | RD | RE},
@@ -150,6 +150,8 @@ static const struct insn insn_table[insn_invalid] = { [insn_mtlo] = {M(spec_op, 0, 0, 0, 0, mtlo_op), RS}, [insn_mulu] = {M (spec_op,0 ,0 ,0 , multu_mulu_op, multu_op), RS | RT | RD}, + [insn_muhu] = {M (spec_op,0 ,0 ,0 , multu_muhu_op, multu_op), +RS |RT |RD }, #ifndef CONFIG_CPU_MIPSR6 [insn_mul] = {M(spec2_op, 0, 0, 0, 0, mul_op), RS | RT | RD}, #else
arch/mips/mm/uasm.c +2 -1
@@ -59,7 +59,7 @@ enum opcode { insn_lddir, insn_ldpte, insn_ldx, insn_lh, insn_lhu, insn_ll, insn_lld, insn_lui, insn_lw, insn_lwu, insn_lwx, insn_mfc0, insn_mfhc0, insn_mfhi, insn_mflo, insn_modu, insn_movn, insn_movz, insn_mtc0, insn_mthc0, - insn_mthi, insn_mtlo, insn_mul, insn_multu, insn_mulu, insn_nor, + insn_mthi, insn_mtlo, insn_mul, insn_multu, insn_mulu, insn_muhu, insn_nor, insn_or, insn_ori, insn_pref, insn_rfe, insn_rotr, insn_sb, insn_sc, insn_scd, insn_seleqz, insn_selnez, insn_sd, insn_sh, insn_sll, insn_sllv, insn_slt, insn_slti, insn_sltiu, insn_sltu, insn_sra,
@@ -344,6 +344,7 @@ I_u1(_mtlo) I_u3u1u2(_mul) I_u1u2(_multu) I_u3u1u2(_mulu) +I_u3u1u2(_muhu) I_u3u1u2(_nor) I_u3u1u2(_or) I_u2u1u3(_ori)
Signature · 1 line
2.30.2
Johan Almbladh
maintainerPATCH 2/7 mips: uasm: Add workaround for Loongson-2F nop CPU errata
In reply to Johan AlmbladhThis patch implements a workaround for the Loongson-2F nop in generated, code, if the existing option CONFIG_CPU_NOP_WORKAROUND is set. Before, the binutils option -mfix-loongson2f-nop was enabled, but no workaround was done when emitting MIPS code. Now, the nop pseudo instruction is emitted as "or ax,ax,zero" instead of the default "sll zero,zero,0". This is consistent with the workaround implemented by binutils.
--- arch/mips/include/asm/uasm.h | 4 ++++ 1 file changed, 4 insertions(+)
arch/mips/include/asm/uasm.h +4 -0
@@ -249,7 +249,11 @@ static inline void uasm_l##lb(struct uasm_label **lab, u32 *addr) \#define uasm_i_bnezl (buf, rs, off) uasm_i_bnel(buf, rs, 0, off)#define uasm_i_ehb (buf) uasm_i_sll(buf, 0, 0, 3)#define uasm_i_move (buf, a, b) UASM_i_ADDU(buf, a, 0, b) +#ifdef CONFIG_CPU_NOP_WORKAROUNDS +#define uasm_i_nop (buf) uasm_i_or(buf, 1, 1, 0) +#else #define uasm_i_nop (buf) uasm_i_sll(buf, 0, 0, 0) +#endif #define uasm_i_ssnop (buf) uasm_i_sll(buf, 0, 0, 1) static inline void uasm_i_drotr_safe(u32 **p, unsigned int a1,
Signature · 1 line
2.30.2
Re: [PATCH 2/7] mips: uasm: Add workaround for Loongson-2F nop CPU errata
Quoted 10 lines · Johan Almbladh
This patch implements a workaround for the Loongson-2F nop in generated, code, if the existing option
CONFIG_CPU_NOP_WORKAROUNDis set. Before, the binutils option -mfix-loongson2f-nop was enabled, but no workaround was done when emitting MIPS code. Now, the nop pseudo instruction is emitted as "or ax,ax,zero" instead of the default "sll zero,zero,0". This is consistent with the workaround implemented by binutils.Signed-off-by Johan Almbladh <[email protected]>
Thanks!
- Jiaxun
Re: [PATCH 2/7] mips: uasm: Add workaround for Loongson-2F nop CPU errata
In reply to Johan AlmbladhQuoted 5 lines · Johan Almbladh
This patch implements a workaround for the Loongson-2F nop in generated, code, if the existing option
CONFIG_CPU_NOP_WORKAROUNDis set. Before, the binutils option -mfix-loongson2f-nop was enabled, but no workaround was done when emitting MIPS code. Now, the nop pseudo instruction is emitted as "or ax,ax,zero" instead of the default "sll zero,zero,0". This
Confusing typo here, s/ax/at/.
Maciej
Johan Almbladh
maintainerRe: [PATCH 2/7] mips: uasm: Add workaround for Loongson-2F nop CPU errata
You are right, it should say "at" here instead of "ax". The code uses the MIPS "at" register. AX is a BPF register, so this is a typo in my commit message.
Thanks,
Johan
Quoted 12 lines · Maciej W. Rozycki
Quoted 5 lines · Johan Almbladh
This patch implements a workaround for the Loongson-2F nop in generated, code, if the existing option
CONFIG_CPU_NOP_WORKAROUNDis set. Before, the binutils option -mfix-loongson2f-nop was enabled, but no workaround was done when emitting MIPS code. Now, the nop pseudo instruction is emitted as "or ax,ax,zero" instead of the default "sll zero,zero,0". ThisConfusing typo here, s/ax/at/.
Maciej
Johan Almbladh
maintainerPATCH 3/7 mips: bpf: Add eBPF JIT for 32-bit MIPS
In reply to Johan AlmbladhThis is an implementation of an eBPF JIT for 32-bit MIPS I-V and MIPS32. The implementation supports all 32-bit and 64-bit ALU and JMP operations, including the recently-added atomics. 64-bit div/mod and 64-bit atomics are implemented using function calls to math64 and atomic64 functions, respectively. All 32-bit operations are implemented natively by the JIT, except if the CPU lacks ll/sc instructions.
Register mapping
================
All 64-bit eBPF registers are mapped to native 32-bit MIPS register pairs, and does not use any stack scratch space for register swapping. This means that all eBPF register data is kept in CPU registers all the time, and this simplifies the register management a lot. It also reduces the JIT's pressure on temporary registers since we do not have to move data around.
Native register pairs are ordered according to CPU endiannes, following the O32 calling convention for passing 64-bit arguments and return values. The eBPF return value, arguments and callee-saved registers are mapped to their native MIPS equivalents.
Since the 32 highest bits in the eBPF FP (frame pointer) register are always zero, only one general-purpose register is actually needed for the mapping. The MIPS fp register is used for this purpose. The high bits are mapped to MIPS register r0. This saves us one CPU register, which is much needed for temporaries, while still allowing us to treat the R10 (FP) register just like any other eBPF register in the JIT.
The MIPS gp (global pointer) and at (assembler temporary) registers are used as internal temporary registers for constant blinding. CPU registers t6-t9 are used internally by the JIT when constructing more complex 64-bit operations. This is precisely what is needed - two registers to store an operand value, and two more as scratch registers when performing the operation.
The register mapping is shown below.
R0 - $v1, $v0 return value
R1 - $a1, $a0 argument 1, passed in registers
R2 - $a3, $a2 argument 2, passed in registers
R3 - $t1, $t0 argument 3, passed on stack
R4 - $t3, $t2 argument 4, passed on stack
R5 - $t4, $t3 argument 5, passed on stack
R6 - $s1, $s0 callee-saved
R7 - $s3, $s2 callee-saved
R8 - $s5, $s4 callee-saved
R9 - $s7, $s6 callee-saved
FP - $r0, $fp 32-bit frame pointer
AX - $gp, $at constant-blinding
$t6 - $t9 unallocated, JIT temporariesJump offsets
============
The JIT tries to map all conditional JMP operations to MIPS conditional PC-relative branches. The MIPS branch offset field is 18 bits, in bytes, which is equivalent to the eBPF 16-bit instruction offset. However, since the JIT may emit more than one CPU instruction per eBPF instruction, the field width may overflow. If that happens, the JIT converts the long conditional jump to a short PC-relative branch with the condition inverted, jumping over a long unconditional absolute jmp (j).
This conversion will change the instruction offset mapping used for jumps, and may in turn result in more branch offset overflows. The JIT therefore dry-runs the translation until no more branches are converted and the offsets do not change anymore. There is an upper bound on this of course, and if the JIT hits that limit, the last two iterations are run with all branches being converted.
Tail call count
===============
The current tail call count is stored in the 16-byte area of the caller's stack frame that is reserved for the callee in the o32 ABI. The value is initialized in the prologue, and propagated to the tail-callee by skipping the initialization instructions when emitting the tail call.
--- arch/mips/net/Makefile | 7 +- arch/mips/net/bpf_jit_comp.c | 1032 +++++++++++++++++ arch/mips/net/bpf_jit_comp.h | 211 ++++ arch/mips/net/bpf_jit_comp32.c | 1899 ++++++++++++++++++++++++++++++++ 4 files changed, 3148 insertions(+), 1 deletion(-) create mode 100644 arch/mips/net/bpf_jit_comp.c create mode 100644 arch/mips/net/bpf_jit_comp.h create mode 100644 arch/mips/net/bpf_jit_comp32.c
arch/mips/net/Makefile +6 -1
@@ -2,4 +2,9 @@ # MIPS networking code obj-$(CONFIG_MIPS_CBPF_JIT) += bpf_jit.o bpf_jit_asm.o -obj-$(CONFIG_MIPS_EBPF_JIT) += ebpf_jit.o + +ifeq ($(CONFIG_32BIT),y) + obj-$(CONFIG_MIPS_EBPF_JIT) += bpf_jit_comp.o bpf_jit_comp32.o +else + obj-$(CONFIG_MIPS_EBPF_JIT) += ebpf_jit.o +endif
arch/mips/net/bpf_jit_comp.c new file +1032 -0
1,032 lines not on this page · raw message
arch/mips/net/bpf_jit_comp.h new file +211 -0
211 lines not on this page · raw message
arch/mips/net/bpf_jit_comp32.c new file +1899 -0
1,747 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.30.2
Part of the diff is not shown here. View the raw message.
Johan Almbladh
maintainerPATCH 4/7 mips: bpf: Add new eBPF JIT for 64-bit MIPS
In reply to Johan AlmbladhThis is an implementation on of an eBPF JIT for 64-bit MIPS III-V and MIPS64r1-r6. It uses the same framework introduced by the 32-bit JIT.
--- arch/mips/net/bpf_jit_comp64.c | 1052 ++++++++++++++++++++++++++++++++ 1 file changed, 1052 insertions(+) create mode 100644 arch/mips/net/bpf_jit_comp64.c
arch/mips/net/bpf_jit_comp64.c new file +1052 -0
@@ -0,0 +1,1052 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Just-In-Time compiler for eBPF bytecode on MIPS. +* Implementation of JIT functions for 64-bit CPUs. +* +* Copyright (c) 2021 Anyfi Networks AB. +* Author: Johan Almbladh <[email protected]> +* +* Based on code and ideas from +* Copyright (c) 2017 Cavium, Inc. +* Copyright (c) 2017 Shubham Bansal <[email protected]> +* Copyright (c) 2011 Mircea Gherzan <[email protected]> +*/ + +#include <linux/errno.h> +#include <linux/filter.h> +#include <linux/bpf.h> +#include <asm/cpu-features.h> +#include <asm/isa-rev.h> +#include <asm/uasm.h> + +#include "bpf_jit_comp.h" + +/* MIPS t0-t3 are not available in the n64 ABI */ +#undef MIPS_R_T0 +#undef MIPS_R_T1 +#undef MIPS_R_T2 +#undef MIPS_R_T3 + +/* Stack is 16-byte aligned in n64 ABI */ +#define MIPS_STACK_ALIGNMENT 16 + +/* Extra 64-bit eBPF registers used by JIT */ +#define JIT_REG_TC (MAX_BPF_JIT_REG + 0) +#define JIT_REG_ZX (MAX_BPF_JIT_REG + 1) + +/* Number of prologue bytes to skip when doing a tail call */ +#define JIT_TCALL_SKIP 4 + +/* Callee-saved CPU registers that the JIT must preserve */ +#define JIT_CALLEE_REGS \ + (BIT(MIPS_R_S0) | \ + BIT(MIPS_R_S1) | \ + BIT(MIPS_R_S2) | \ + BIT(MIPS_R_S3) | \ + BIT(MIPS_R_S4) | \ + BIT(MIPS_R_S5) | \ + BIT(MIPS_R_S6) | \ + BIT(MIPS_R_S7) | \ + BIT(MIPS_R_GP) | \ + BIT(MIPS_R_FP) | \ + BIT(MIPS_R_RA)) + +/* Caller-saved CPU registers available for JIT use */ +#define JIT_CALLER_REGS \ + (BIT(MIPS_R_A5) | \ + BIT(MIPS_R_A6) | \ + BIT(MIPS_R_A7)) +/* +* Mapping of 64-bit eBPF registers to 64-bit native MIPS registers. +* MIPS registers t4 - t7 may be used by the JIT as temporary registers. +* MIPS registers t8 - t9 are reserved for single-register common functions. +*/ +static const u8 bpf2mips64[] = { +/* Return value from in-kernel function, and exit value from eBPF */ + [BPF_REG_0 ] =MIPS_R_V0 , +/* Arguments from eBPF program to in-kernel function */ + [BPF_REG_1 ] =MIPS_R_A0 , + [BPF_REG_2 ] =MIPS_R_A1 , + [BPF_REG_3 ] =MIPS_R_A2 , + [BPF_REG_4 ] =MIPS_R_A3 , + [BPF_REG_5 ] =MIPS_R_A4 , +/* Callee-saved registers that in-kernel function will preserve */ + [BPF_REG_6 ] =MIPS_R_S0 , + [BPF_REG_7 ] =MIPS_R_S1 , + [BPF_REG_8 ] =MIPS_R_S2 , + [BPF_REG_9 ] =MIPS_R_S3 , +/* Read-only frame pointer to access the eBPF stack */ + [BPF_REG_FP ] =MIPS_R_FP , +/* Temporary register for blinding constants */ + [BPF_REG_AX ] =MIPS_R_AT , +/* Tail call count register, caller-saved */ + [JIT_REG_TC ] =MIPS_R_A5 , +/* Constant for register zero-extension */ + [JIT_REG_ZX ] =MIPS_R_V1 , +}; + +/* +* MIPS 32-bit operations on 64-bit registers generate a sign-extended +* result. However, the eBPF ISA mandates zero-extension, so we rely on the +* verifier to add that for us (emit_zext_ver). In addition, ALU arithmetic +* operations, right shift and byte swap require properly sign-extended +* operands or the result is unpredictable. We emit explicit sign-extensions +* in those cases. +*/ + +/* Sign extension */ +static void emit_sext (struct jit_context *ctx,u8 dst,u8 src) +{ +emit (ctx, sll, dst, src,0 ); +clobber_reg (ctx, dst); +} + +/* Zero extension */ +static void emit_zext (struct jit_context *ctx,u8 dst) +{ +if (cpu_has_mips64r2 || cpu_has_mips64r6) { +emit (ctx, dinsu, dst,MIPS_R_ZERO ,32 ,32 ); + }else { +emit (ctx, and, dst, dst, bpf2mips64[JIT_REG_ZX ]); +access_reg (ctx,JIT_REG_ZX );/* We need the ZX register */ + } +clobber_reg (ctx, dst); +} + +/* Zero extension, if verifier does not do it for us */ +static void emit_zext_ver (struct jit_context *ctx,u8 dst) +{ +if (!ctx->program->aux->verifier_zext) +emit_zext (ctx, dst); +} + +/* dst = imm (64-bit) */ +static void emit_mov_i64 (struct jit_context *ctx,u8 dst,u64 imm64) +{ +if (imm64 >=0xffffffffffff8000ULL || imm64 <0x8000ULL ) { +emit (ctx, daddiu, dst,MIPS_R_ZERO , (s16 )imm64); + }else if (imm64 >=0xffffffff80000000ULL || + (imm64 <0x80000000 && imm64 >0xffff )) { +emit (ctx, lui, dst, (s16)(imm64 >>16 )); +emit (ctx, ori, dst, dst, (u16 )imm64 &0xffff ); + }else { +u8 acc =MIPS_R_ZERO ; +int k; + +for (k =0 ; k <4 ; k++) { +u16 half = imm64 >> (48 -16 * k); + +if (acc == dst) +emit (ctx, dsll, dst, dst,16 ); + +if (half) { +emit (ctx, ori, dst, acc, half); + acc = dst; + } + } + } +clobber_reg (ctx, dst); +} + +/* ALU immediate operation (64-bit) */ +static void emit_alu_i64 (struct jit_context *ctx,u8 dst,s32 imm,u8 op) +{ +switch (BPF_OP (op)) { +/* dst = dst | imm */ +case BPF_OR : +emit (ctx, ori, dst, dst, (u16 )imm); +break ; +/* dst = dst ^ imm */ +case BPF_XOR : +emit (ctx, xori, dst, dst, (u16 )imm); +break ; +/* dst = -dst */ +case BPF_NEG : +emit (ctx, dsubu, dst,MIPS_R_ZERO , dst); +break ; +/* dst = dst << imm */ +case BPF_LSH : +emit (ctx, dsll_safe, dst, dst, imm); +break ; +/* dst = dst >> imm */ +case BPF_RSH : +emit (ctx, dsrl_safe, dst, dst, imm); +break ; +/* dst = dst >> imm (arithmetic) */ +case BPF_ARSH : +emit (ctx, dsra_safe, dst, dst, imm); +break ; +/* dst = dst + imm */ +case BPF_ADD : +emit (ctx, daddiu, dst, dst, imm); +break ; +/* dst = dst - imm */ +case BPF_SUB : +emit (ctx, daddiu, dst, dst, -imm); +break ; +default : +/* Width-generic operations */ +emit_alu_i (ctx, dst, imm, op); + } +clobber_reg (ctx, dst); +} + +/* ALU register operation (64-bit) */ +static void emit_alu_r64 (struct jit_context *ctx,u8 dst,u8 src,u8 op) +{ +switch (BPF_OP (op)) { +/* dst = dst << src */ +case BPF_LSH : +emit (ctx, dsllv, dst, dst, src); +break ; +/* dst = dst >> src */ +case BPF_RSH : +emit (ctx, dsrlv, dst, dst, src); +break ; +/* dst = dst >> src (arithmetic) */ +case BPF_ARSH : +emit (ctx, dsrav, dst, dst, src); +break ; +/* dst = dst + src */ +case BPF_ADD : +emit (ctx, daddu, dst, dst, src); +break ; +/* dst = dst - src */ +case BPF_SUB : +emit (ctx, dsubu, dst, dst, src); +break ; +/* dst = dst * src */ +case BPF_MUL : +if (cpu_has_mips64r6) { +emit (ctx, dmulu, dst, dst, src); + }else { +emit (ctx, dmultu, dst, src); +emit (ctx, mflo, dst); + } +break ; +/* dst = dst / src */ +case BPF_DIV : +if (cpu_has_mips64r6) { +emit (ctx, ddivu_r6, dst, dst, src); + }else { +emit (ctx, ddivu, dst, src); +emit (ctx, mflo, dst); + } +break ; +/* dst = dst % src */ +case BPF_MOD : +if (cpu_has_mips64r6) { +emit (ctx, dmodu, dst, dst, src); + }else { +emit (ctx, ddivu, dst, src); +emit (ctx, mfhi, dst); + } +break ; +default : +/* Width-generic operations */ +emit_alu_r (ctx, dst, src, op); + } +clobber_reg (ctx, dst); +} + +/* Swap sub words in a register double word */ +static void emit_swap_r64 (struct jit_context *ctx,u8 dst,u8 mask,u32 bits) +{ +u8 tmp =MIPS_R_T9 ; + +emit (ctx, and, tmp, dst, mask);/* tmp = dst & mask */ +emit (ctx, dsll, tmp, tmp, bits);/* tmp = tmp << bits */ +emit (ctx, dsrl, dst, dst, bits);/* dst = dst >> bits */ +emit (ctx, and, dst, dst, mask);/* dst = dst & mask */ +emit (ctx, or, dst, dst, tmp);/* dst = dst | tmp */ +} + +/* Swap bytes and truncate a register double word, word or half word */ +static void emit_bswap_r64 (struct jit_context *ctx,u8 dst,u32 width) +{ +switch (width) { +/* Swap bytes in a double word */ +case 64 : +if (cpu_has_mips64r2 || cpu_has_mips64r6) { +emit (ctx, dsbh, dst, dst); +emit (ctx, dshd, dst, dst); + }else { +u8 t1 =MIPS_R_T6 ; +u8 t2 =MIPS_R_T7 ; + +emit (ctx, dsll32, t2, dst,0 );/* t2 = dst << 32 */ +emit (ctx, dsrl32, dst, dst,0 );/* dst = dst >> 32 */ +emit (ctx, or, dst, dst, t2);/* dst = dst | t2 */ + +emit (ctx, ori, t2,MIPS_R_ZERO ,0xffff ); +emit (ctx, dsll32, t1, t2,0 );/* t1 = t2 << 32 */ +emit (ctx, or, t1, t1, t2);/* t1 = t1 | t2 */ +emit_swap_r64 (ctx, dst, t1,16 );/* dst = swap16(dst) */ + +emit (ctx, lui, t2,0xff );/* t2 = 0x00ff0000 */ +emit (ctx, ori, t2, t2,0xff );/* t2 = t2 | 0x00ff */ +emit (ctx, dsll32, t1, t2,0 );/* t1 = t2 << 32 */ +emit (ctx, or, t1, t1, t2);/* t1 = t1 | t2 */ +emit_swap_r64 (ctx, dst, t1,8 );/* dst = swap8(dst) */ + } +break ; +/* Swap bytes in a half word */ +/* Swap bytes in a word */ +case 32 : +case 16 : +emit_sext (ctx, dst, dst); +emit_bswap_r (ctx, dst, width); +if (cpu_has_mips64r2 || cpu_has_mips64r6) +emit_zext (ctx, dst); +break ; + } +clobber_reg (ctx, dst); +} + +/* Truncate a register double word, word or half word */ +static void emit_trunc_r64 (struct jit_context *ctx,u8 dst,u32 width) +{ +switch (width) { +case 64 : +break ; +/* Zero-extend a word */ +case 32 : +emit_zext (ctx, dst); +break ; +/* Zero-extend a half word */ +case 16 : +emit (ctx, andi, dst, dst,0xffff ); +break ; + } +clobber_reg (ctx, dst); +} + +/* Load operation: dst = *(size*)(src + off) */ +static void emit_ldx (struct jit_context *ctx,u8 dst,u8 src,s16 off,u8 size) +{ +switch (size) { +/* Load a byte */ +case BPF_B : +emit (ctx, lbu, dst, off, src); +break ; +/* Load a half word */ +case BPF_H : +emit (ctx, lhu, dst, off, src); +break ; +/* Load a word */ +case BPF_W : +emit (ctx, lwu, dst, off, src); +break ; +/* Load a double word */ +case BPF_DW : +emit (ctx, ld, dst, off, src); +break ; + } +clobber_reg (ctx, dst); +} + +/* Store operation: *(size *)(dst + off) = src */ +static void emit_stx (struct jit_context *ctx,u8 dst,u8 src,s16 off,u8 size) +{ +switch (size) { +/* Store a byte */ +case BPF_B : +emit (ctx, sb, src, off, dst); +break ; +/* Store a half word */ +case BPF_H : +emit (ctx, sh, src, off, dst); +break ; +/* Store a word */ +case BPF_W : +emit (ctx, sw, src, off, dst); +break ; +/* Store a double word */ +case BPF_DW : +emit (ctx, sd, src, off, dst); +break ; + } +} + +/* Atomic read-modify-write */ +static void emit_atomic_r64 (struct jit_context *ctx, +u8 dst,u8 src,s16 off,u8 code) +{ +u8 t1 =MIPS_R_T6 ; +u8 t2 =MIPS_R_T7 ; + +emit (ctx, lld, t1, off, dst); +switch (code) { +case BPF_ADD : +case BPF_ADD |BPF_FETCH : +emit (ctx, daddu, t2, t1, src); +break ; +case BPF_AND : +case BPF_AND |BPF_FETCH : +emit (ctx, and, t2, t1, src); +break ; +case BPF_OR : +case BPF_OR |BPF_FETCH : +emit (ctx, or, t2, t1, src); +break ; +case BPF_XOR : +case BPF_XOR |BPF_FETCH : +emit (ctx, xor, t2, t1, src); +break ; +case BPF_XCHG : +emit (ctx, move, t2, src); +break ; + } +emit (ctx, scd, t2, off, dst); +emit (ctx, beqz, t2,-16 ); +emit (ctx, nop);/* Delay slot */ + +if (code &BPF_FETCH ) { +emit (ctx, move, src, t1); +clobber_reg (ctx, src); + } +} + +/* Atomic compare-and-exchange */ +static void emit_cmpxchg_r64 (struct jit_context *ctx,u8 dst,u8 src,s16 off) +{ +u8 r0 = bpf2mips64[BPF_REG_0 ]; +u8 t1 =MIPS_R_T6 ; +u8 t2 =MIPS_R_T7 ; + +emit (ctx, lld, t1, off, dst); +emit (ctx, bne, t1, r0,12 ); +emit (ctx, move, t2, src);/* Delay slot */ +emit (ctx, scd, t2, off, dst); +emit (ctx, beqz, t2,-20 ); +emit (ctx, move, r0, t1);/* Delay slot */ + +clobber_reg (ctx, r0); +} + +/* Function call */ +static int emit_call (struct jit_context *ctx,const struct bpf_insn *insn) +{ +u8 zx = bpf2mips64[JIT_REG_ZX ]; +u8 tmp =MIPS_R_T6 ; +bool fixed; +u64 addr; + +/* Decode the call address */ +if (bpf_jit_get_func_addr (ctx->program, insn, false, + &addr, &fixed) <0 ) +return -1 ; +if (!fixed) +return -1 ; + +/* Push caller-saved registers on stack */ +push_regs (ctx, ctx->clobbered &JIT_CALLER_REGS ,0 ,0 ); + +/* Emit function call */ +emit_mov_i64 (ctx, tmp, addr); +emit (ctx, jalr,MIPS_R_RA , tmp); +emit (ctx, nop);/* Delay slot */ + +/* Restore caller-saved registers */ +pop_regs (ctx, ctx->clobbered &JIT_CALLER_REGS ,0 ,0 ); + +/* Re-initialize the JIT zero-extension register if accessed */ +if (ctx->accessed &BIT (JIT_REG_ZX )) { +emit (ctx, daddiu, zx,MIPS_R_ZERO ,-1 ); +emit (ctx, dsrl32, zx, zx,0 ); + } + +clobber_reg (ctx,MIPS_R_RA ); +clobber_reg (ctx,MIPS_R_V0 ); +clobber_reg (ctx,MIPS_R_V1 ); +return 0 ; +} + +/* Function tail call */ +static int emit_tail_call (struct jit_context *ctx) +{ +u8 ary = bpf2mips64[BPF_REG_2 ]; +u8 ind = bpf2mips64[BPF_REG_3 ]; +u8 tcc = bpf2mips64[JIT_REG_TC ]; +u8 tmp =MIPS_R_T6 ; +int off; + +/* +* Tail call: +* eBPF R1 - function argument (context ptr), passed in a0-a1 +* eBPF R2 - ptr to object with array of function entry points +* eBPF R3 - array index of function to be called +*/ + +/* if (ind >= ary->map.max_entries) goto out */ + off = offsetof(struct bpf_array, map.max_entries); +if (off >0x7fff ) +return -1 ; +emit (ctx, lwu, tmp, off, ary);/* tmp = ary->map.max_entrs*/ +emit (ctx, sltu, tmp, ind, tmp);/* tmp = ind < t1 */ +emit (ctx, beqz, tmp,get_offset (ctx,1 ));/* PC += off(1) if tmp == 0*/ + +/* if (--TCC < 0) goto out */ +emit (ctx, daddiu, tcc, tcc,-1 );/* tcc-- (delay slot) */ +emit (ctx, bltz, tcc,get_offset (ctx,1 ));/* PC += off(1) if tcc < 0 */ +/* (next insn delay slot) */ +/* prog = ary->ptrs[ind] */ + off = offsetof(struct bpf_array , ptrs); +if (off >0x7fff ) +return -1 ; +emit (ctx, dsll, tmp, ind,3 );/* tmp = ind << 3 */ +emit (ctx, daddu, tmp, tmp, ary);/* tmp += ary */ +emit (ctx, ld, tmp, off, tmp);/* tmp = *(tmp + off) */ + +/* if (prog == 0) goto out */ +emit (ctx, beqz, tmp,get_offset (ctx,1 ));/* PC += off(1) if tmp == 0*/ +emit (ctx, nop);/* Delay slot */ + +/* func = prog->bpf_func + 8 (prologue skip offset) */ + off = offsetof(struct bpf_prog , bpf_func); +if (off >0x7fff ) +return -1 ; +emit (ctx, ld, tmp, off, tmp);/* tmp = *(tmp + off) */ +emit (ctx, daddiu, tmp, tmp,JIT_TCALL_SKIP );/* tmp += skip (4) */ + +/* goto func */ +build_epilogue (ctx, tmp); +access_reg (ctx,JIT_REG_TC ); +return 0 ; +} + +/* +* Stack frame layout for a JITed program (stack grows down). +* +* Higher address : Previous stack frame : +* +===========================+ <--- MIPS sp before call +* | Callee-saved registers, | +* | including RA and FP | +* +---------------------------+ <--- eBPF FP (MIPS fp) +* | Local eBPF variables | +* | allocated by program | +* +---------------------------+ +* | Reserved for caller-saved | +* | registers | +* Lower address +===========================+ <--- MIPS sp +*/ + +/* Build program prologue to set up the stack and registers */ +void build_prologue (struct jit_context *ctx) +{ +u8 fp = bpf2mips64[BPF_REG_FP ]; +u8 tc = bpf2mips64[JIT_REG_TC ]; +u8 zx = bpf2mips64[JIT_REG_ZX ]; +int stack, saved, locals, reserved; + +/* +* The first instruction initializes the tail call count register. +* On a tail call, the calling function jumps into the prologue +* after this instruction. +*/ +emit (ctx, addiu, tc,MIPS_R_ZERO ,min (MAX_TAIL_CALL_CNT +1 ,0xffff )); + +/* === Entry-point for tail calls === */ + +/* +* If the eBPF frame pointer and tail call count registers were +* accessed they must be preserved. Mark them as clobbered here +* to save and restore them on the stack as needed. +*/ +if (ctx->accessed &BIT (BPF_REG_FP )) +clobber_reg (ctx, fp); +if (ctx->accessed &BIT (JIT_REG_TC )) +clobber_reg (ctx, tc); +if (ctx->accessed &BIT (JIT_REG_ZX )) +clobber_reg (ctx, zx); + +/* Compute the stack space needed for callee-saved registers */ + saved =hweight32 (ctx->clobbered &JIT_CALLEE_REGS ) *sizeof (u64); + saved =ALIGN (saved,MIPS_STACK_ALIGNMENT ); + +/* Stack space used by eBPF program local data */ + locals =ALIGN (ctx->program->aux->stack_depth,MIPS_STACK_ALIGNMENT ); + +/* +* If we are emitting function calls, reserve extra stack space for +* caller-saved registers needed by the JIT. The required space is +* computed automatically during resource usage discovery (pass 1). +*/ + reserved = ctx->stack_used; + +/* Allocate the stack frame */ + stack =ALIGN (saved + locals + reserved,MIPS_STACK_ALIGNMENT ); +if (stack) +emit (ctx, daddiu,MIPS_R_SP ,MIPS_R_SP , -stack); + +/* Store callee-saved registers on stack */ +push_regs (ctx, ctx->clobbered &JIT_CALLEE_REGS ,0 , stack - saved); + +/* Initialize the eBPF frame pointer if accessed */ +if (ctx->accessed &BIT (BPF_REG_FP )) +emit (ctx, daddiu, fp,MIPS_R_SP , stack - saved); + +/* Initialize the ePF JIT zero-extension register if accessed */ +if (ctx->accessed &BIT (JIT_REG_ZX )) { +emit (ctx, daddiu, zx,MIPS_R_ZERO ,-1 ); +emit (ctx, dsrl32, zx, zx,0 ); + } + + ctx->saved_size = saved; + ctx->stack_size = stack; +} + +/* Build the program epilogue to restore the stack and registers */ +void build_epilogue (struct jit_context *ctx,int dest_reg) +{ +/* Restore callee-saved registers from stack */ +pop_regs (ctx, ctx->clobbered &JIT_CALLEE_REGS ,0 , + ctx->stack_size - ctx->saved_size); + +/* Release the stack frame */ +if (ctx->stack_size) +emit (ctx, daddiu,MIPS_R_SP ,MIPS_R_SP , ctx->stack_size); + +/* Jump to return address and sign-extend the 32-bit return value */ +emit (ctx, jr, dest_reg); +emit (ctx, sll,MIPS_R_V0 ,MIPS_R_V0 ,0 );/* Delay slot */ +} + +/* Build one eBPF instruction */ +int build_insn (const struct bpf_insn *insn,struct jit_context *ctx) +{ +u8 dst = bpf2mips64[insn->dst_reg]; +u8 src = bpf2mips64[insn->src_reg]; +u8 res = bpf2mips64[BPF_REG_0 ]; +u8 code = insn->code; +s16 off = insn->off; +s32 imm = insn->imm; +s32 val, rel; +u8 alu, jmp; + +switch (code) { +/* ALU operations */ +/* dst = imm */ +case BPF_ALU |BPF_MOV |BPF_K : +emit_mov_i (ctx, dst, imm); +emit_zext_ver (ctx, dst); +break ; +/* dst = src */ +case BPF_ALU |BPF_MOV |BPF_X : +if (imm ==1 ) { +/* Special mov32 for zext */ +emit_zext (ctx, dst); + }else { +emit_mov_r (ctx, dst, src); +emit_zext_ver (ctx, dst); + } +break ; +/* dst = -dst */ +case BPF_ALU |BPF_NEG : +emit_sext (ctx, dst, dst); +emit_alu_i (ctx, dst,0 ,BPF_NEG ); +emit_zext_ver (ctx, dst); +break ; +/* dst = dst & imm */ +/* dst = dst | imm */ +/* dst = dst ^ imm */ +/* dst = dst << imm */ +case BPF_ALU |BPF_OR |BPF_K : +case BPF_ALU |BPF_AND |BPF_K : +case BPF_ALU |BPF_XOR |BPF_K : +case BPF_ALU |BPF_LSH |BPF_K : +if (!valid_alu_i (BPF_OP (code), imm)) { +emit_mov_i (ctx,MIPS_R_T4 , imm); +emit_alu_r (ctx, dst,MIPS_R_T4 ,BPF_OP (code)); + }else if (rewrite_alu_i (BPF_OP (code), imm, &alu, &val)) { +emit_alu_i (ctx, dst, val, alu); + } +emit_zext_ver (ctx, dst); +break ; +/* dst = dst >> imm */ +/* dst = dst >> imm (arithmetic) */ +/* dst = dst + imm */ +/* dst = dst - imm */ +/* dst = dst * imm */ +/* dst = dst / imm */ +/* dst = dst % imm */ +case BPF_ALU |BPF_RSH |BPF_K : +case BPF_ALU |BPF_ARSH |BPF_K : +case BPF_ALU |BPF_ADD |BPF_K : +case BPF_ALU |BPF_SUB |BPF_K : +case BPF_ALU |BPF_MUL |BPF_K : +case BPF_ALU |BPF_DIV |BPF_K : +case BPF_ALU |BPF_MOD |BPF_K : +if (!valid_alu_i (BPF_OP (code), imm)) { +emit_sext (ctx, dst, dst); +emit_mov_i (ctx,MIPS_R_T4 , imm); +emit_alu_r (ctx, dst,MIPS_R_T4 ,BPF_OP (code)); + }else if (rewrite_alu_i (BPF_OP (code), imm, &alu, &val)) { +emit_sext (ctx, dst, dst); +emit_alu_i (ctx, dst, val, alu); + } +emit_zext_ver (ctx, dst); +break ; +/* dst = dst & src */ +/* dst = dst | src */ +/* dst = dst ^ src */ +/* dst = dst << src */ +case BPF_ALU |BPF_AND |BPF_X : +case BPF_ALU |BPF_OR |BPF_X : +case BPF_ALU |BPF_XOR |BPF_X : +case BPF_ALU |BPF_LSH |BPF_X : +emit_alu_r (ctx, dst, src,BPF_OP (code)); +emit_zext_ver (ctx, dst); +break ; +/* dst = dst >> src */ +/* dst = dst >> src (arithmetic) */ +/* dst = dst + src */ +/* dst = dst - src */ +/* dst = dst * src */ +/* dst = dst / src */ +/* dst = dst % src */ +case BPF_ALU |BPF_RSH |BPF_X : +case BPF_ALU |BPF_ARSH |BPF_X : +case BPF_ALU |BPF_ADD |BPF_X : +case BPF_ALU |BPF_SUB |BPF_X : +case BPF_ALU |BPF_MUL |BPF_X : +case BPF_ALU |BPF_DIV |BPF_X : +case BPF_ALU |BPF_MOD |BPF_X : +emit_sext (ctx, dst, dst); +emit_sext (ctx,MIPS_R_T4 , src); +emit_alu_r (ctx, dst,MIPS_R_T4 ,BPF_OP (code)); +emit_zext_ver (ctx, dst); +break ; +/* dst = imm (64-bit) */ +case BPF_ALU64 |BPF_MOV |BPF_K : +emit_mov_i (ctx, dst, imm); +break ; +/* dst = src (64-bit) */ +case BPF_ALU64 |BPF_MOV |BPF_X : +emit_mov_r (ctx, dst, src); +break ; +/* dst = -dst (64-bit) */ +case BPF_ALU64 |BPF_NEG : +emit_alu_i64 (ctx, dst,0 ,BPF_NEG ); +break ; +/* dst = dst & imm (64-bit) */ +/* dst = dst | imm (64-bit) */ +/* dst = dst ^ imm (64-bit) */ +/* dst = dst << imm (64-bit) */ +/* dst = dst >> imm (64-bit) */ +/* dst = dst >> imm ((64-bit, arithmetic) */ +/* dst = dst + imm (64-bit) */ +/* dst = dst - imm (64-bit) */ +/* dst = dst * imm (64-bit) */ +/* dst = dst / imm (64-bit) */ +/* dst = dst % imm (64-bit) */ +case BPF_ALU64 |BPF_AND |BPF_K : +case BPF_ALU64 |BPF_OR |BPF_K : +case BPF_ALU64 |BPF_XOR |BPF_K : +case BPF_ALU64 |BPF_LSH |BPF_K : +case BPF_ALU64 |BPF_RSH |BPF_K : +case BPF_ALU64 |BPF_ARSH |BPF_K : +case BPF_ALU64 |BPF_ADD |BPF_K : +case BPF_ALU64 |BPF_SUB |BPF_K : +case BPF_ALU64 |BPF_MUL |BPF_K : +case BPF_ALU64 |BPF_DIV |BPF_K : +case BPF_ALU64 |BPF_MOD |BPF_K : +if (!valid_alu_i (BPF_OP (code), imm)) { +emit_mov_i (ctx,MIPS_R_T4 , imm); +emit_alu_r64 (ctx, dst,MIPS_R_T4 ,BPF_OP (code)); + }else if (rewrite_alu_i (BPF_OP (code), imm, &alu, &val)) { +emit_alu_i64 (ctx, dst, val, alu); + } +break ; +/* dst = dst & src (64-bit) */ +/* dst = dst | src (64-bit) */ +/* dst = dst ^ src (64-bit) */ +/* dst = dst << src (64-bit) */ +/* dst = dst >> src (64-bit) */ +/* dst = dst >> src (64-bit, arithmetic) */ +/* dst = dst + src (64-bit) */ +/* dst = dst - src (64-bit) */ +/* dst = dst * src (64-bit) */ +/* dst = dst / src (64-bit) */ +/* dst = dst % src (64-bit) */ +case BPF_ALU64 |BPF_AND |BPF_X : +case BPF_ALU64 |BPF_OR |BPF_X : +case BPF_ALU64 |BPF_XOR |BPF_X : +case BPF_ALU64 |BPF_LSH |BPF_X : +case BPF_ALU64 |BPF_RSH |BPF_X : +case BPF_ALU64 |BPF_ARSH |BPF_X : +case BPF_ALU64 |BPF_ADD |BPF_X : +case BPF_ALU64 |BPF_SUB |BPF_X : +case BPF_ALU64 |BPF_MUL |BPF_X : +case BPF_ALU64 |BPF_DIV |BPF_X : +case BPF_ALU64 |BPF_MOD |BPF_X : +emit_alu_r64 (ctx, dst, src,BPF_OP (code)); +break ; +/* dst = htole(dst) */ +/* dst = htobe(dst) */ +case BPF_ALU |BPF_END |BPF_FROM_LE : +case BPF_ALU |BPF_END |BPF_FROM_BE : + if (BPF_SRC(code) == +#ifdef __BIG_ENDIAN +BPF_FROM_LE +#else + BPF_FROM_BE +#endif + ) +emit_bswap_r64 (ctx ,dst ,imm ); + else +emit_trunc_r64 (ctx ,dst ,imm ); +break ; +/* dst = imm64 */ +case BPF_LD |BPF_IMM |BPF_DW : +emit_mov_i64 (ctx, dst, (u32 )imm | ((u64 )insn[1 ].imm <<32 )); +return 1 ; +/* LDX: dst = *(size *)(src + off) */ +case BPF_LDX |BPF_MEM |BPF_W : +case BPF_LDX |BPF_MEM |BPF_H : +case BPF_LDX |BPF_MEM |BPF_B : +case BPF_LDX |BPF_MEM |BPF_DW : +emit_ldx (ctx, dst, src, off,BPF_SIZE (code)); +break ; +/* ST: *(size *)(dst + off) = imm */ +case BPF_ST |BPF_MEM |BPF_W : +case BPF_ST |BPF_MEM |BPF_H : +case BPF_ST |BPF_MEM |BPF_B : +case BPF_ST |BPF_MEM |BPF_DW : +emit_mov_i (ctx,MIPS_R_T4 , imm); +emit_stx (ctx, dst,MIPS_R_T4 , off,BPF_SIZE (code)); +break ; +/* STX: *(size *)(dst + off) = src */ +case BPF_STX |BPF_MEM |BPF_W : +case BPF_STX |BPF_MEM |BPF_H : +case BPF_STX |BPF_MEM |BPF_B : +case BPF_STX |BPF_MEM |BPF_DW : +emit_stx (ctx, dst, src, off,BPF_SIZE (code)); +break ; +/* Speculation barrier */ +case BPF_ST |BPF_NOSPEC : +break ; +/* Atomics */ +case BPF_STX |BPF_ATOMIC |BPF_W : +case BPF_STX |BPF_ATOMIC |BPF_DW : +switch (imm) { +case BPF_ADD : +case BPF_ADD |BPF_FETCH : +case BPF_AND : +case BPF_AND |BPF_FETCH : +case BPF_OR : +case BPF_OR |BPF_FETCH : +case BPF_XOR : +case BPF_XOR |BPF_FETCH : +case BPF_XCHG : +if (BPF_SIZE (code) ==BPF_DW ) { +emit_atomic_r64 (ctx, dst, src, off, imm); + }else if (imm &BPF_FETCH ) { +u8 tmp = dst; + +if (src == dst) {/* Don't overwrite dst */ +emit_mov_r (ctx,MIPS_R_T4 , dst); + tmp =MIPS_R_T4 ; + } +emit_sext (ctx, src, src); +emit_atomic_r (ctx, tmp, src, off, imm); +emit_zext_ver (ctx, src); + }else {/* 32-bit, no fetch */ +emit_sext (ctx,MIPS_R_T4 , src); +emit_atomic_r (ctx, dst,MIPS_R_T4 , off, imm); + } +break ; +case BPF_CMPXCHG : +if (BPF_SIZE (code) ==BPF_DW ) { +emit_cmpxchg_r64 (ctx, dst, src, off); + }else { +u8 tmp = res; + +if (res == dst)/* Don't overwrite dst */ + tmp =MIPS_R_T4 ; +emit_sext (ctx, tmp, res); +emit_sext (ctx,MIPS_R_T5 , src); +emit_cmpxchg_r (ctx, dst,MIPS_R_T5 , tmp, off); +if (res == dst)/* Restore result */ +emit_mov_r (ctx, res,MIPS_R_T4 ); +/* Result zext inserted by verifier */ + } +break ; +default : + goto notyet; + } +break ; +/* PC += off if dst == src */ +/* PC += off if dst != src */ +/* PC += off if dst & src */ +/* PC += off if dst > src */ +/* PC += off if dst >= src */ +/* PC += off if dst < src */ +/* PC += off if dst <= src */ +/* PC += off if dst > src (signed) */ +/* PC += off if dst >= src (signed) */ +/* PC += off if dst < src (signed) */ +/* PC += off if dst <= src (signed) */ +case BPF_JMP32 |BPF_JEQ |BPF_X : +case BPF_JMP32 |BPF_JNE |BPF_X : +case BPF_JMP32 |BPF_JSET |BPF_X : +case BPF_JMP32 |BPF_JGT |BPF_X : +case BPF_JMP32 |BPF_JGE |BPF_X : +case BPF_JMP32 |BPF_JLT |BPF_X : +case BPF_JMP32 |BPF_JLE |BPF_X : +case BPF_JMP32 |BPF_JSGT |BPF_X : +case BPF_JMP32 |BPF_JSGE |BPF_X : +case BPF_JMP32 |BPF_JSLT |BPF_X : +case BPF_JMP32 |BPF_JSLE |BPF_X : +if (off ==0 ) +break ; +setup_jmp_r (ctx, dst == src,BPF_OP (code), off, &jmp, &rel); +emit_sext (ctx,MIPS_R_T4 , dst);/* Sign-extended dst */ +emit_sext (ctx,MIPS_R_T5 , src);/* Sign-extended src */ +emit_jmp_r (ctx,MIPS_R_T4 ,MIPS_R_T5 , rel, jmp); +if (finish_jmp (ctx, jmp, off) <0 ) + goto toofar; +break ; +/* PC += off if dst == imm */ +/* PC += off if dst != imm */ +/* PC += off if dst & imm */ +/* PC += off if dst > imm */ +/* PC += off if dst >= imm */ +/* PC += off if dst < imm */ +/* PC += off if dst <= imm */ +/* PC += off if dst > imm (signed) */ +/* PC += off if dst >= imm (signed) */ +/* PC += off if dst < imm (signed) */ +/* PC += off if dst <= imm (signed) */ +case BPF_JMP32 |BPF_JEQ |BPF_K : +case BPF_JMP32 |BPF_JNE |BPF_K : +case BPF_JMP32 |BPF_JSET |BPF_K : +case BPF_JMP32 |BPF_JGT |BPF_K : +case BPF_JMP32 |BPF_JGE |BPF_K : +case BPF_JMP32 |BPF_JLT |BPF_K : +case BPF_JMP32 |BPF_JLE |BPF_K : +case BPF_JMP32 |BPF_JSGT |BPF_K : +case BPF_JMP32 |BPF_JSGE |BPF_K : +case BPF_JMP32 |BPF_JSLT |BPF_K : +case BPF_JMP32 |BPF_JSLE |BPF_K : +if (off ==0 ) +break ; +setup_jmp_i (ctx, imm,32 ,BPF_OP (code), off, &jmp, &rel); +emit_sext (ctx,MIPS_R_T4 , dst);/* Sign-extended dst */ +if (valid_jmp_i (jmp, imm)) { +emit_jmp_i (ctx,MIPS_R_T4 , imm, rel, jmp); + }else { +/* Move large immediate to register, sign-extended */ +emit_mov_i (ctx,MIPS_R_T5 , imm); +emit_jmp_r (ctx,MIPS_R_T4 ,MIPS_R_T5 , rel, jmp); + } +if (finish_jmp (ctx, jmp, off) <0 ) + goto toofar; +break ; +/* PC += off if dst == src */ +/* PC += off if dst != src */ +/* PC += off if dst & src */ +/* PC += off if dst > src */ +/* PC += off if dst >= src */ +/* PC += off if dst < src */ +/* PC += off if dst <= src */ +/* PC += off if dst > src (signed) */ +/* PC += off if dst >= src (signed) */ +/* PC += off if dst < src (signed) */ +/* PC += off if dst <= src (signed) */ +case BPF_JMP |BPF_JEQ |BPF_X : +case BPF_JMP |BPF_JNE |BPF_X : +case BPF_JMP |BPF_JSET |BPF_X : +case BPF_JMP |BPF_JGT |BPF_X : +case BPF_JMP |BPF_JGE |BPF_X : +case BPF_JMP |BPF_JLT |BPF_X : +case BPF_JMP |BPF_JLE |BPF_X : +case BPF_JMP |BPF_JSGT |BPF_X : +case BPF_JMP |BPF_JSGE |BPF_X : +case BPF_JMP |BPF_JSLT |BPF_X : +case BPF_JMP |BPF_JSLE |BPF_X : +if (off ==0 ) +break ; +setup_jmp_r (ctx, dst == src,BPF_OP (code), off, &jmp, &rel); +emit_jmp_r (ctx, dst, src, rel, jmp); +if (finish_jmp (ctx, jmp, off) <0 ) + goto toofar; +break ; +/* PC += off if dst == imm */ +/* PC += off if dst != imm */ +/* PC += off if dst & imm */ +/* PC += off if dst > imm */ +/* PC += off if dst >= imm */ +/* PC += off if dst < imm */ +/* PC += off if dst <= imm */ +/* PC += off if dst > imm (signed) */ +/* PC += off if dst >= imm (signed) */ +/* PC += off if dst < imm (signed) */ +/* PC += off if dst <= imm (signed) */ +case BPF_JMP |BPF_JEQ |BPF_K : +case BPF_JMP |BPF_JNE |BPF_K : +case BPF_JMP |BPF_JSET |BPF_K : +case BPF_JMP |BPF_JGT |BPF_K : +case BPF_JMP |BPF_JGE |BPF_K : +case BPF_JMP |BPF_JLT |BPF_K : +case BPF_JMP |BPF_JLE |BPF_K : +case BPF_JMP |BPF_JSGT |BPF_K : +case BPF_JMP |BPF_JSGE |BPF_K : +case BPF_JMP |BPF_JSLT |BPF_K : +case BPF_JMP |BPF_JSLE |BPF_K : +if (off ==0 ) +break ; +setup_jmp_i (ctx, imm,64 ,BPF_OP (code), off, &jmp, &rel); +if (valid_jmp_i (jmp, imm)) { +emit_jmp_i (ctx, dst, imm, rel, jmp); + }else { +/* Move large immediate to register */ +emit_mov_i (ctx,MIPS_R_T4 , imm); +emit_jmp_r (ctx, dst,MIPS_R_T4 , rel, jmp); + } +if (finish_jmp (ctx, jmp, off) <0 ) + goto toofar; +break ; +/* PC += off */ +case BPF_JMP |BPF_JA : +if (off ==0 ) +break ; +if (emit_ja (ctx, off) <0 ) + goto toofar; +break ; +/* Tail call */ +case BPF_JMP |BPF_TAIL_CALL : +if (emit_tail_call (ctx) <0 ) + goto invalid; +break ; +/* Function call */ +case BPF_JMP |BPF_CALL : +if (emit_call (ctx, insn) <0 ) + goto invalid; +break ; +/* Function return */ +case BPF_JMP |BPF_EXIT : +/* +* Optimization: when last instruction is EXIT +* simply continue to epilogue. +*/ +if (ctx->bpf_index == ctx->program->len -1 ) +break ; +if (emit_exit (ctx) <0 ) + goto toofar; +break ; + +default : +invalid: +pr_err_once ("unknown opcode %02x\n" , code); +return -EINVAL ; +notyet: +pr_info_once ("*** NOT YET: opcode %02x ***\n" , code); +return -EFAULT ; +toofar: +pr_info_once ("*** TOO FAR: jump at %u opcode %02x ***\n" , + ctx->bpf_index, code); + return -E2BIG; + } +return 0 ; +}
Signature · 1 line
2.30.2
Johan Almbladh
maintainerPATCH 5/7 mips: bpf: Add JIT workarounds for CPU errata
In reply to Johan AlmbladhThis patch adds workarounds for the following CPU errata to the MIPS eBPF JIT, if enabled in the kernel configuration.
- R10000 ll/sc weak ordering
- Loongson-3 ll/sc weak ordering
- Loongson-2F jump hang
The Loongson-2F nop errata is implemented in uasm, which the JIT uses, so no additional mitigations are needed for that.
--- arch/mips/net/bpf_jit_comp.c | 6 ++++-- arch/mips/net/bpf_jit_comp.h | 26 +++++++++++++++++++++++++- arch/mips/net/bpf_jit_comp64.c | 10 ++++++---- 3 files changed, 35 insertions(+), 7 deletions(-)
arch/mips/net/bpf_jit_comp.c +4 -2
@@ -404,6 +404,7 @@ void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op)/* Atomic read-modify-write (32-bit) */ void emit_atomic_r (struct jit_context *ctx,u8 dst,u8 src,s16 off,u8 code) { +LLSC_sync (ctx);emit (ctx, ll,MIPS_R_T9 , off, dst);switch (code) {case BPF_ADD :
@@ -427,7 +428,7 @@ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)break ; }emit (ctx, sc,MIPS_R_T8 , off, dst); -emit (ctx, beqz,MIPS_R_T8 ,-16 ); +emit (ctx, LLSC_beqz,MIPS_R_T8 ,-16 - LLSC_offset);emit (ctx, nop);/* Delay slot */ if (code &BPF_FETCH ) {
@@ -439,11 +440,12 @@ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)/* Atomic compare-and-exchange (32-bit) */ void emit_cmpxchg_r (struct jit_context *ctx,u8 dst,u8 src,u8 res,s16 off) { +LLSC_sync (ctx);emit (ctx, ll,MIPS_R_T9 , off, dst);emit (ctx, bne,MIPS_R_T9 , res,12 );emit (ctx, move,MIPS_R_T8 , src);/* Delay slot */ emit (ctx, sc,MIPS_R_T8 , off, dst); -emit (ctx, beqz,MIPS_R_T8 ,-20 ); +emit (ctx, LLSC_beqz,MIPS_R_T8 ,-20 - LLSC_offset);emit (ctx, move, res,MIPS_R_T9 );/* Delay slot */ clobber_reg (ctx, res); }
arch/mips/net/bpf_jit_comp.h +25 -1
@@ -87,7 +87,7 @@ struct jit_context { };/* Emit the instruction if the JIT memory space has been allocated */ -#define emit (ctx, func, ...) \ +#define __emit (ctx, func, ...) \ do { \ if ((ctx)->target != NULL) { \ u32 *p = &(ctx)->target[ctx->jit_index]; \
@@ -95,6 +95,30 @@ do { \ } \ (ctx)->jit_index++; \ } while (0) +#define emit (...) __emit(__VA_ARGS__) + +/* Workaround for R10000 ll/sc errata */ +#ifdef CONFIG_WAR_R10000 +#define LLSC_beqz beqzl +#else +#define LLSC_beqz beqz +#endif + +/* Workaround for Loongson-3 ll/sc errata */ +#ifdef CONFIG_CPU_LOONGSON3_WORKAROUNDS +#define LLSC_sync (ctx) emit(ctx, sync, 0) +#define LLSC_offset 4 +#else +#define LLSC_sync (ctx) +#define LLSC_offset 0 +#endif + +/* Workaround for Loongson-2F jump errata */ +#ifdef CONFIG_CPU_JUMP_WORKAROUNDS +#define JALR_MASK 0xffffffffcfffffffULL +#else +#define JALR_MASK (~0ULL) +#endif /* * Mark a BPF register as accessed, it needs to be
arch/mips/net/bpf_jit_comp64.c +6 -4
@@ -375,6 +375,7 @@ static void emit_atomic_r64(struct jit_context *ctx,u8 t1 =MIPS_R_T6 ;u8 t2 =MIPS_R_T7 ; +LLSC_sync (ctx);emit (ctx, lld, t1, off, dst);switch (code) {case BPF_ADD :
@@ -398,7 +399,7 @@ static void emit_atomic_r64(struct jit_context *ctx,break ; }emit (ctx, scd, t2, off, dst); -emit (ctx, beqz, t2,-16 ); +emit (ctx, LLSC_beqz, t2,-16 - LLSC_offset);emit (ctx, nop);/* Delay slot */ if (code &BPF_FETCH ) {
@@ -414,12 +415,13 @@ static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off)u8 t1 =MIPS_R_T6 ;u8 t2 =MIPS_R_T7 ; +LLSC_sync (ctx);emit (ctx, lld, t1, off, dst);emit (ctx, bne, t1, r0,12 );emit (ctx, move, t2, src);/* Delay slot */ emit (ctx, scd, t2, off, dst); -emit (ctx, beqz, t2,-20 ); -emit (ctx, move, r0, t1);/* Delay slot */ +emit (ctx, LLSC_beqz, t2,-20 - LLSC_offset); +emit (ctx, move, r0, t1);/* Delay slot */ clobber_reg (ctx, r0); }
@@ -443,7 +445,7 @@ static int emit_call(struct jit_context *ctx, const struct bpf_insn *insn)push_regs (ctx, ctx->clobbered &JIT_CALLER_REGS ,0 ,0 );/* Emit function call */ -emit_mov_i64 (ctx, tmp, addr); +emit_mov_i64 (ctx, tmp, addr &JALR_MASK );emit (ctx, jalr,MIPS_R_RA , tmp);emit (ctx, nop);/* Delay slot */
Signature · 1 line
2.30.2
Re: [PATCH 5/7] mips: bpf: Add JIT workarounds for CPU errata
Quoted 11 lines · Johan Almbladh
This patch adds workarounds for the following CPU errata to the MIPS eBPF JIT, if enabled in the kernel configuration.
- R10000 ll/sc weak ordering
- Loongson-3 ll/sc weak ordering
- Loongson-2F jump hangThe Loongson-2F nop errata is implemented in uasm, which the JIT uses, so no additional mitigations are needed for that.
Signed-off-by Johan Almbladh <[email protected]>
Thanks!
- Jiaxun
Quoted 2 lines
---
Re: [PATCH 5/7] mips: bpf: Add JIT workarounds for CPU errata
In reply to Johan AlmbladhHi, for Loongson3 errata, we need another sycn for cmpxchg
@@ -414,12 +415,13 @@ static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off)
u8 t1 = MIPS_R_T6;
u8 t2 = MIPS_R_T7;
+ LLSC_sync(ctx);
emit(ctx, lld, t1, off, dst);
emit(ctx, bne, t1, r0, 12);
emit(ctx, move, t2, src); /* Delay slot */
emit(ctx, scd, t2, off, dst);
- emit(ctx, beqz, t2, -20);
- emit(ctx, move, r0, t1); /* Delay slot */
+ emit(ctx, LLSC_beqz, t2, -20 - LLSC_offset);
+ emit(ctx, move, r0, t1); /* Delay slot */
+ LLSC_sync(ctx);
Quoted 145 lines · Johan Almbladh
This patch adds workarounds for the following CPU errata to the MIPS eBPF JIT, if enabled in the kernel configuration.
- R10000 ll/sc weak ordering
- Loongson-3 ll/sc weak ordering
- Loongson-2F jump hangThe Loongson-2F nop errata is implemented in uasm, which the JIT uses, so no additional mitigations are needed for that.
Signed-off-by Johan Almbladh <[email protected]>--- arch/mips/net/bpf_jit_comp.c | 6 ++++-- arch/mips/net/bpf_jit_comp.h | 26 +++++++++++++++++++++++++- arch/mips/net/bpf_jit_comp64.c | 10 ++++++---- 3 files changed, 35 insertions(+), 7 deletions(-)arch/mips/net/bpf_jit_comp.c +4
-2@@ -404,6 +404,7 @@ void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op)/* Atomic read-modify-write (32-bit) */ void emit_atomic_r (struct jit_context *ctx,u8 dst,u8 src,s16 off,u8 code) { +LLSC_sync (ctx);emit (ctx, ll,MIPS_R_T9 , off, dst);switch (code) {case BPF_ADD :@@ -427,7 +428,7 @@ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)break ; }emit (ctx, sc,MIPS_R_T8 , off, dst); -emit (ctx, beqz,MIPS_R_T8 ,-16 ); +emit (ctx, LLSC_beqz,MIPS_R_T8 ,-16 - LLSC_offset);emit (ctx, nop);/* Delay slot */ if (code &BPF_FETCH ) {@@ -439,11 +440,12 @@ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)/* Atomic compare-and-exchange (32-bit) */ void emit_cmpxchg_r (struct jit_context *ctx,u8 dst,u8 src,u8 res,s16 off) { +LLSC_sync (ctx);emit (ctx, ll,MIPS_R_T9 , off, dst);emit (ctx, bne,MIPS_R_T9 , res,12 );emit (ctx, move,MIPS_R_T8 , src);/* Delay slot */ emit (ctx, sc,MIPS_R_T8 , off, dst); -emit (ctx, beqz,MIPS_R_T8 ,-20 ); +emit (ctx, LLSC_beqz,MIPS_R_T8 ,-20 - LLSC_offset);emit (ctx, move, res,MIPS_R_T9 );/* Delay slot */ clobber_reg (ctx, res); }arch/mips/net/bpf_jit_comp.h +25
-1@@ -87,7 +87,7 @@ struct jit_context { };/* Emit the instruction if the JIT memory space has been allocated */ -#define emit (ctx, func, ...) \ +#define __emit (ctx, func, ...) \ do { \ if ((ctx)->target != NULL) { \ u32 *p = &(ctx)->target[ctx->jit_index]; \@@ -95,6 +95,30 @@ do { \ } \ (ctx)->jit_index++; \ } while (0) +#define emit (...) __emit(__VA_ARGS__) + +/* Workaround for R10000 ll/sc errata */ +#ifdef CONFIG_WAR_R10000 +#define LLSC_beqz beqzl +#else +#define LLSC_beqz beqz +#endif + +/* Workaround for Loongson-3 ll/sc errata */ +#ifdef CONFIG_CPU_LOONGSON3_WORKAROUNDS +#define LLSC_sync (ctx) emit(ctx, sync, 0) +#define LLSC_offset 4 +#else +#define LLSC_sync (ctx) +#define LLSC_offset 0 +#endif + +/* Workaround for Loongson-2F jump errata */ +#ifdef CONFIG_CPU_JUMP_WORKAROUNDS +#define JALR_MASK 0xffffffffcfffffffULL +#else +#define JALR_MASK (~0ULL) +#endif /* * Mark a BPF register as accessed, it needs to be arch/mips/net/bpf_jit_comp64.c +6
-4@@ -375,6 +375,7 @@ static void emit_atomic_r64(struct jit_context *ctx,u8 t1 =MIPS_R_T6 ;u8 t2 =MIPS_R_T7 ; +LLSC_sync (ctx);emit (ctx, lld, t1, off, dst);switch (code) {case BPF_ADD :@@ -398,7 +399,7 @@ static void emit_atomic_r64(struct jit_context *ctx,break ; }emit (ctx, scd, t2, off, dst); -emit (ctx, beqz, t2,-16 ); +emit (ctx, LLSC_beqz, t2,-16 - LLSC_offset);emit (ctx, nop);/* Delay slot */ if (code &BPF_FETCH ) {@@ -414,12 +415,13 @@ static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off)u8 t1 =MIPS_R_T6 ;u8 t2 =MIPS_R_T7 ; +LLSC_sync (ctx);emit (ctx, lld, t1, off, dst);emit (ctx, bne, t1, r0,12 );emit (ctx, move, t2, src);/* Delay slot */ emit (ctx, scd, t2, off, dst); -emit (ctx, beqz, t2,-20 ); -emit (ctx, move, r0, t1);/* Delay slot */ +emit (ctx, LLSC_beqz, t2,-20 - LLSC_offset); +emit (ctx, move, r0, t1);/* Delay slot */ clobber_reg (ctx, r0); }@@ -443,7 +445,7 @@ static int emit_call(struct jit_context *ctx, const struct bpf_insn *insn)push_regs (ctx, ctx->clobbered &JIT_CALLER_REGS ,0 ,0 );/* Emit function call */ -emit_mov_i64 (ctx, tmp, addr); +emit_mov_i64 (ctx, tmp, addr &JALR_MASK );emit (ctx, jalr,MIPS_R_RA , tmp);emit (ctx, nop);/* Delay slot */ Signature · 1 line
2.30.2
Daniel Borkmann
maintainerRe: [PATCH 5/7] mips: bpf: Add JIT workarounds for CPU errata
Hi Huang,
Quoted 16 lines · Huang Pei
Hi, for Loongson3 errata, we need another sycn for cmpxchg
@@ -414,12 +415,13 @@ static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off) u8 t1 = MIPS_R_T6; u8 t2 = MIPS_R_T7; + LLSC_sync(ctx); emit(ctx, lld, t1, off, dst); emit(ctx, bne, t1, r0, 12); emit(ctx, move, t2, src); /* Delay slot */ emit(ctx, scd, t2, off, dst); - emit(ctx, beqz, t2, -20); - emit(ctx, move, r0, t1); /* Delay slot */ + emit(ctx, LLSC_beqz, t2, -20 - LLSC_offset); + emit(ctx, move, r0, t1); /* Delay slot */ + LLSC_sync(ctx);
Please craft an official fix for bpf tree and have Johan Almbladh in Cc for review / ack.
(Please also make sure that this passes the lib/test_bpf.ko suite.)
Thanks a lot,
Daniel
Quoted 147 lines · Johan Almbladh
Quoted 145 lines
This patch adds workarounds for the following CPU errata to the MIPS eBPF JIT, if enabled in the kernel configuration.
- R10000 ll/sc weak ordering
- Loongson-3 ll/sc weak ordering
- Loongson-2F jump hangThe Loongson-2F nop errata is implemented in uasm, which the JIT uses, so no additional mitigations are needed for that.
Signed-off-by Johan Almbladh <[email protected]>--- arch/mips/net/bpf_jit_comp.c | 6 ++++-- arch/mips/net/bpf_jit_comp.h | 26 +++++++++++++++++++++++++- arch/mips/net/bpf_jit_comp64.c | 10 ++++++---- 3 files changed, 35 insertions(+), 7 deletions(-)arch/mips/net/bpf_jit_comp.c +4
-2@@ -404,6 +404,7 @@ void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op)/* Atomic read-modify-write (32-bit) */ void emit_atomic_r (struct jit_context *ctx,u8 dst,u8 src,s16 off,u8 code) { +LLSC_sync (ctx);emit (ctx, ll,MIPS_R_T9 , off, dst);switch (code) {case BPF_ADD :@@ -427,7 +428,7 @@ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)break ; }emit (ctx, sc,MIPS_R_T8 , off, dst); -emit (ctx, beqz,MIPS_R_T8 ,-16 ); +emit (ctx, LLSC_beqz,MIPS_R_T8 ,-16 - LLSC_offset);emit (ctx, nop);/* Delay slot */ if (code &BPF_FETCH ) {@@ -439,11 +440,12 @@ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)/* Atomic compare-and-exchange (32-bit) */ void emit_cmpxchg_r (struct jit_context *ctx,u8 dst,u8 src,u8 res,s16 off) { +LLSC_sync (ctx);emit (ctx, ll,MIPS_R_T9 , off, dst);emit (ctx, bne,MIPS_R_T9 , res,12 );emit (ctx, move,MIPS_R_T8 , src);/* Delay slot */ emit (ctx, sc,MIPS_R_T8 , off, dst); -emit (ctx, beqz,MIPS_R_T8 ,-20 ); +emit (ctx, LLSC_beqz,MIPS_R_T8 ,-20 - LLSC_offset);emit (ctx, move, res,MIPS_R_T9 );/* Delay slot */ clobber_reg (ctx, res); }arch/mips/net/bpf_jit_comp.h +25
-1@@ -87,7 +87,7 @@ struct jit_context { };/* Emit the instruction if the JIT memory space has been allocated */ -#define emit (ctx, func, ...) \ +#define __emit (ctx, func, ...) \ do { \ if ((ctx)->target != NULL) { \ u32 *p = &(ctx)->target[ctx->jit_index]; \@@ -95,6 +95,30 @@ do { \ } \ (ctx)->jit_index++; \ } while (0) +#define emit (...) __emit(__VA_ARGS__) + +/* Workaround for R10000 ll/sc errata */ +#ifdef CONFIG_WAR_R10000 +#define LLSC_beqz beqzl +#else +#define LLSC_beqz beqz +#endif + +/* Workaround for Loongson-3 ll/sc errata */ +#ifdef CONFIG_CPU_LOONGSON3_WORKAROUNDS +#define LLSC_sync (ctx) emit(ctx, sync, 0) +#define LLSC_offset 4 +#else +#define LLSC_sync (ctx) +#define LLSC_offset 0 +#endif + +/* Workaround for Loongson-2F jump errata */ +#ifdef CONFIG_CPU_JUMP_WORKAROUNDS +#define JALR_MASK 0xffffffffcfffffffULL +#else +#define JALR_MASK (~0ULL) +#endif /* * Mark a BPF register as accessed, it needs to be arch/mips/net/bpf_jit_comp64.c +6
-4@@ -375,6 +375,7 @@ static void emit_atomic_r64(struct jit_context *ctx,u8 t1 =MIPS_R_T6 ;u8 t2 =MIPS_R_T7 ; +LLSC_sync (ctx);emit (ctx, lld, t1, off, dst);switch (code) {case BPF_ADD :@@ -398,7 +399,7 @@ static void emit_atomic_r64(struct jit_context *ctx,break ; }emit (ctx, scd, t2, off, dst); -emit (ctx, beqz, t2,-16 ); +emit (ctx, LLSC_beqz, t2,-16 - LLSC_offset);emit (ctx, nop);/* Delay slot */ if (code &BPF_FETCH ) {@@ -414,12 +415,13 @@ static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off)u8 t1 =MIPS_R_T6 ;u8 t2 =MIPS_R_T7 ; +LLSC_sync (ctx);emit (ctx, lld, t1, off, dst);emit (ctx, bne, t1, r0,12 );emit (ctx, move, t2, src);/* Delay slot */ emit (ctx, scd, t2, off, dst); -emit (ctx, beqz, t2,-20 ); -emit (ctx, move, r0, t1);/* Delay slot */ +emit (ctx, LLSC_beqz, t2,-20 - LLSC_offset); +emit (ctx, move, r0, t1);/* Delay slot */ clobber_reg (ctx, r0); }@@ -443,7 +445,7 @@ static int emit_call(struct jit_context *ctx, const struct bpf_insn *insn)push_regs (ctx, ctx->clobbered &JIT_CALLER_REGS ,0 ,0 );/* Emit function call */ -emit_mov_i64 (ctx, tmp, addr); +emit_mov_i64 (ctx, tmp, addr &JALR_MASK );emit (ctx, jalr,MIPS_R_RA , tmp);emit (ctx, nop);/* Delay slot */ Signature · 1 line
2.30.2
Johan Almbladh
maintainerPATCH 6/7 mips: bpf: Enable eBPF JITs
In reply to Johan AlmbladhThis patch enables the new eBPF JITs for 32-bit and 64-bit MIPS. It also disables the old cBPF JIT to so cBPF programs are converted to use the new JIT.
Workarounds for R4000 CPU errata are not implemented by the JIT, so the JIT is disabled if any of those workarounds are configured.
--- MAINTAINERS | 1 + arch/mips/Kconfig | 6 ++++-- arch/mips/net/Makefile | 5 +++-- 3 files changed, 8 insertions(+), 4 deletions(-)
MAINTAINERS +1 -0
@@ -3422,6 +3422,7 @@ S: Supported F: arch/arm64/net/ BPF JIT for MIPS (32-BIT AND 64-BIT) +M: Johan Almbladh <[email protected]> M: Paul Burton <[email protected]> L: [email protected] L: [email protected]
arch/mips/Kconfig +4 -2
@@ -57,7 +57,6 @@ config MIPS select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE if CPU_SUPPORTS_HUGEPAGES select HAVE_ASM_MODVERSIONS - select HAVE_CBPF_JIT if !64BIT && !CPU_MICROMIPS select HAVE_CONTEXT_TRACKING select HAVE_TIF_NOHZ select HAVE_C_RECORDMCOUNT
@@ -65,7 +64,10 @@ config MIPS select HAVE_DEBUG_STACKOVERFLOW select HAVE_DMA_CONTIGUOUS select HAVE_DYNAMIC_FTRACE - select HAVE_EBPF_JIT if 64BIT && !CPU_MICROMIPS && TARGET_ISA_REV >= 2 + select HAVE_EBPF_JIT if !CPU_MICROMIPS && \ + !CPU_DADDI_WORKAROUNDS && \ + !CPU_R4000_WORKAROUNDS && \ + !CPU_R4400_WORKAROUNDS select HAVE_EXIT_THREAD select HAVE_FAST_GUP select HAVE_FTRACE_MCOUNT_RECORD
arch/mips/net/Makefile +3 -2
@@ -2,9 +2,10 @@ # MIPS networking code obj-$(CONFIG_MIPS_CBPF_JIT) += bpf_jit.o bpf_jit_asm.o +obj-$(CONFIG_MIPS_EBPF_JIT) += bpf_jit_comp.o ifeq ($(CONFIG_32BIT),y) - obj-$(CONFIG_MIPS_EBPF_JIT) += bpf_jit_comp.o bpf_jit_comp32.o + obj-$(CONFIG_MIPS_EBPF_JIT) += bpf_jit_comp32.o else - obj-$(CONFIG_MIPS_EBPF_JIT) += ebpf_jit.o + obj-$(CONFIG_MIPS_EBPF_JIT) += bpf_jit_comp64.o endif
Signature · 1 line
2.30.2
Johan Almbladh
maintainerPATCH 7/7 mips: bpf: Remove old BPF JIT implementations
In reply to Johan AlmbladhThis patch removes the old 32-bit cBPF and 64-bit eBPF JIT implementations. They are replaced by a new eBPF implementation that supports both 32-bit and 64-bit MIPS CPUs.
--- arch/mips/net/bpf_jit.c | 1299 ----------------------- arch/mips/net/bpf_jit.h | 81 -- arch/mips/net/bpf_jit_asm.S | 285 ------ arch/mips/net/ebpf_jit.c | 1938 ----------------------------------- 4 files changed, 3603 deletions(-) delete mode 100644 arch/mips/net/bpf_jit.c delete mode 100644 arch/mips/net/bpf_jit.h delete mode 100644 arch/mips/net/bpf_jit_asm.S delete mode 100644 arch/mips/net/ebpf_jit.c
arch/mips/net/bpf_jit.c deleted +0 -1299
@@ -1,1299 +0,0 @@ -/* -* Just-In-Time compiler for BPF filters on MIPS -* -* Copyright (c) 2014 Imagination Technologies Ltd. -* Author: Markos Chandras <[email protected]> -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of the GNU General Public License as published by the -* Free Software Foundation; version 2 of the License. -*/ - -#include <linux/bitops.h> -#include <linux/compiler.h> -#include <linux/errno.h> -#include <linux/filter.h> -#include <linux/if_vlan.h> -#include <linux/moduleloader.h> -#include <linux/netdevice.h> -#include <linux/string.h> -#include <linux/slab.h> -#include <linux/types.h> -#include <asm/asm.h> -#include <asm/bitops.h> -#include <asm/cacheflush.h> -#include <asm/cpu-features.h> -#include <asm/uasm.h> - -#include "bpf_jit.h" - -/* ABI -* r_skb_hl SKB header length -* r_data SKB data pointer -* r_off Offset -* r_A BPF register A -* r_X BPF register X -* r_skb *skb -* r_M *scratch memory -* r_skb_len SKB length -* -* On entry (*bpf_func)(*skb, *filter) -* a0 = MIPS_R_A0 = skb; -* a1 = MIPS_R_A1 = filter; -* -* Stack -* ... -* M[15] -* M[14] -* M[13] -* ... -* M[0] <-- r_M -* saved reg k-1 -* saved reg k-2 -* ... -* saved reg 0 <-- r_sp -* <no argument area> -* -* Packet layout -* -* <--------------------- len ------------------------> -* <--skb-len(r_skb_hl)-->< ----- skb->data_len ------> -* ---------------------------------------------------- -* | skb->data | -* ---------------------------------------------------- -*/ - -#define ptr typeof(unsigned long) - -#define SCRATCH_OFF (k) (4 * (k)) - -/* JIT flags */ -#define SEEN_CALL (1 << BPF_MEMWORDS) -#define SEEN_SREG_SFT (BPF_MEMWORDS + 1) -#define SEEN_SREG_BASE (1 << SEEN_SREG_SFT) -#define SEEN_SREG (x) (SEEN_SREG_BASE << (x)) -#define SEEN_OFF SEEN_SREG(2) -#define SEEN_A SEEN_SREG(3) -#define SEEN_X SEEN_SREG(4) -#define SEEN_SKB SEEN_SREG(5) -#define SEEN_MEM SEEN_SREG(6) -/* SEEN_SK_DATA also implies skb_hl an skb_len */ -#define SEEN_SKB_DATA (SEEN_SREG(7) | SEEN_SREG(1) | SEEN_SREG(0)) - -/* Arguments used by JIT */ -#define ARGS_USED_BY_JIT 2/* only applicable to 64-bit */ - -#define SBIT (x) (1 << (x))/* Signed version of BIT() */ - -/** -* struct jit_ctx - JIT context -* @skf: The sk_filter -* @prologue_bytes: Number of bytes for prologue -* @idx: Instruction index -* @flags: JIT flags -* @offsets: Instruction offsets -* @target: Memory location for the compiled filter -*/ -struct jit_ctx { -const struct bpf_prog *skf; -unsigned int prologue_bytes; -u32 idx; -u32 flags; -u32 *offsets; -u32 *target; -}; - - -static inline int optimize_div (u32 *k) -{ -/* power of 2 divides can be implemented with right shift */ -if (!(*k & (*k-1 ))) { - *k =ilog2 (*k); -return 1 ; - } - -return 0 ; -} - -static inline void emit_jit_reg_move (ptr dst,ptr src,struct jit_ctx *ctx); - -/* Simply emit the instruction if the JIT memory space has been allocated */ -#define emit_instr (ctx, func, ...) \ -do { \ - if ((ctx)->target != NULL) { \ - u32 *p = &(ctx)->target[ctx->idx]; \ - uasm_i_##func(&p, ##__VA_ARGS__); \ - } \ - (ctx)->idx++; \ -} while (0) - -/* -* Similar to emit_instr but it must be used when we need to emit -* 32-bit or 64-bit instructions -*/ -#define emit_long_instr (ctx, func, ...) \ -do { \ - if ((ctx)->target != NULL) { \ - u32 *p = &(ctx)->target[ctx->idx]; \ - UASM_i_##func(&p, ##__VA_ARGS__); \ - } \ - (ctx)->idx++; \ -} while (0) - -/* Determine if immediate is within the 16-bit signed range */ -static inline bool is_range16 (s32 imm) -{ -return !(imm >=SBIT (15 ) || imm < -SBIT (15 )); -} - -static inline void emit_addu (unsigned int dst,unsigned int src1, -unsigned int src2,struct jit_ctx *ctx) -{ -emit_instr (ctx, addu, dst, src1, src2); -} - -static inline void emit_nop (struct jit_ctx *ctx) -{ -emit_instr (ctx, nop); -} - -/* Load a u32 immediate to a register */ -static inline void emit_load_imm (unsigned int dst,u32 imm,struct jit_ctx *ctx) -{ -if (ctx->target !=NULL ) { -/* addiu can only handle s16 */ -if (!is_range16 (imm)) { -u32 *p = &ctx->target[ctx->idx]; -uasm_i_lui (&p, r_tmp_imm, (s32 )imm >>16 ); - p = &ctx->target[ctx->idx +1 ]; -uasm_i_ori (&p, dst, r_tmp_imm, imm &0xffff ); - }else { -u32 *p = &ctx->target[ctx->idx]; -uasm_i_addiu (&p, dst, r_zero, imm); - } - } - ctx->idx++; - -if (!is_range16 (imm)) - ctx->idx++; -} - -static inline void emit_or (unsigned int dst,unsigned int src1, -unsigned int src2,struct jit_ctx *ctx) -{ -emit_instr (ctx, or, dst, src1, src2); -} - -static inline void emit_ori (unsigned int dst,unsigned src,u32 imm, -struct jit_ctx *ctx) -{ -if (imm >=BIT (16 )) { -emit_load_imm (r_tmp, imm, ctx); -emit_or (dst, src, r_tmp, ctx); - }else { -emit_instr (ctx, ori, dst, src, imm); - } -} - -static inline void emit_daddiu (unsigned int dst,unsigned int src, -int imm,struct jit_ctx *ctx) -{ -/* -* Only used for stack, so the imm is relatively small -* and it fits in 15-bits -*/ -emit_instr (ctx, daddiu, dst, src, imm); -} - -static inline void emit_addiu (unsigned int dst,unsigned int src, -u32 imm,struct jit_ctx *ctx) -{ -if (!is_range16 (imm)) { -emit_load_imm (r_tmp, imm, ctx); -emit_addu (dst, r_tmp, src, ctx); - }else { -emit_instr (ctx, addiu, dst, src, imm); - } -} - -static inline void emit_and (unsigned int dst,unsigned int src1, -unsigned int src2,struct jit_ctx *ctx) -{ -emit_instr (ctx, and, dst, src1, src2); -} - -static inline void emit_andi (unsigned int dst,unsigned int src, -u32 imm,struct jit_ctx *ctx) -{ -/* If imm does not fit in u16 then load it to register */ -if (imm >=BIT (16 )) { -emit_load_imm (r_tmp, imm, ctx); -emit_and (dst, src, r_tmp, ctx); - }else { -emit_instr (ctx, andi, dst, src, imm); - } -} - -static inline void emit_xor (unsigned int dst,unsigned int src1, -unsigned int src2,struct jit_ctx *ctx) -{ -emit_instr (ctx, xor, dst, src1, src2); -} - -static inline void emit_xori (ptr dst,ptr src,u32 imm,struct jit_ctx *ctx) -{ -/* If imm does not fit in u16 then load it to register */ -if (imm >=BIT (16 )) { -emit_load_imm (r_tmp, imm, ctx); -emit_xor (dst, src, r_tmp, ctx); - }else { -emit_instr (ctx, xori, dst, src, imm); - } -} - -static inline void emit_stack_offset (int offset,struct jit_ctx *ctx) -{ -emit_long_instr (ctx,ADDIU , r_sp, r_sp, offset); -} - -static inline void emit_subu (unsigned int dst,unsigned int src1, -unsigned int src2,struct jit_ctx *ctx) -{ -emit_instr (ctx, subu, dst, src1, src2); -} - -static inline void emit_neg (unsigned int reg,struct jit_ctx *ctx) -{ -emit_subu (reg, r_zero, reg, ctx); -} - -static inline void emit_sllv (unsigned int dst,unsigned int src, -unsigned int sa,struct jit_ctx *ctx) -{ -emit_instr (ctx, sllv, dst, src, sa); -} - -static inline void emit_sll (unsigned int dst,unsigned int src, -unsigned int sa,struct jit_ctx *ctx) -{ -/* sa is 5-bits long */ -if (sa >=BIT (5 )) -/* Shifting >= 32 results in zero */ -emit_jit_reg_move (dst, r_zero, ctx); -else -emit_instr (ctx, sll, dst, src, sa); -} - -static inline void emit_srlv (unsigned int dst,unsigned int src, -unsigned int sa,struct jit_ctx *ctx) -{ -emit_instr (ctx, srlv, dst, src, sa); -} - -static inline void emit_srl (unsigned int dst,unsigned int src, -unsigned int sa,struct jit_ctx *ctx) -{ -/* sa is 5-bits long */ -if (sa >=BIT (5 )) -/* Shifting >= 32 results in zero */ -emit_jit_reg_move (dst, r_zero, ctx); -else -emit_instr (ctx, srl, dst, src, sa); -} - -static inline void emit_slt (unsigned int dst,unsigned int src1, -unsigned int src2,struct jit_ctx *ctx) -{ -emit_instr (ctx, slt, dst, src1, src2); -} - -static inline void emit_sltu (unsigned int dst,unsigned int src1, -unsigned int src2,struct jit_ctx *ctx) -{ -emit_instr (ctx, sltu, dst, src1, src2); -} - -static inline void emit_sltiu (unsigned dst,unsigned int src, -unsigned int imm,struct jit_ctx *ctx) -{ -/* 16 bit immediate */ -if (!is_range16 ((s32 )imm)) { -emit_load_imm (r_tmp, imm, ctx); -emit_sltu (dst, src, r_tmp, ctx); - }else { -emit_instr (ctx, sltiu, dst, src, imm); - } - -} - -/* Store register on the stack */ -static inline void emit_store_stack_reg (ptr reg,ptr base, -unsigned int offset, -struct jit_ctx *ctx) -{ -emit_long_instr (ctx,SW , reg, offset, base); -} - -static inline void emit_store (ptr reg,ptr base,unsigned int offset, -struct jit_ctx *ctx) -{ -emit_instr (ctx, sw, reg, offset, base); -} - -static inline void emit_load_stack_reg (ptr reg,ptr base, -unsigned int offset, -struct jit_ctx *ctx) -{ -emit_long_instr (ctx,LW , reg, offset, base); -} - -static inline void emit_load (unsigned int reg,unsigned int base, -unsigned int offset,struct jit_ctx *ctx) -{ -emit_instr (ctx, lw, reg, offset, base); -} - -static inline void emit_load_byte (unsigned int reg,unsigned int base, -unsigned int offset,struct jit_ctx *ctx) -{ -emit_instr (ctx, lb, reg, offset, base); -} - -static inline void emit_half_load (unsigned int reg,unsigned int base, -unsigned int offset,struct jit_ctx *ctx) -{ -emit_instr (ctx, lh, reg, offset, base); -} - -static inline void emit_half_load_unsigned (unsigned int reg,unsigned int base, -unsigned int offset,struct jit_ctx *ctx) -{ -emit_instr (ctx, lhu, reg, offset, base); -} - -static inline void emit_mul (unsigned int dst,unsigned int src1, -unsigned int src2,struct jit_ctx *ctx) -{ -emit_instr (ctx, mul, dst, src1, src2); -} - -static inline void emit_div (unsigned int dst,unsigned int src, -struct jit_ctx *ctx) -{ -if (ctx->target !=NULL ) { -u32 *p = &ctx->target[ctx->idx]; -uasm_i_divu (&p, dst, src); - p = &ctx->target[ctx->idx +1 ]; -uasm_i_mflo (&p, dst); - } - ctx->idx +=2 ;/* 2 insts */ -} - -static inline void emit_mod (unsigned int dst,unsigned int src, -struct jit_ctx *ctx) -{ -if (ctx->target !=NULL ) { -u32 *p = &ctx->target[ctx->idx]; -uasm_i_divu (&p, dst, src); - p = &ctx->target[ctx->idx +1 ]; -uasm_i_mfhi (&p, dst); - } - ctx->idx +=2 ;/* 2 insts */ -} - -static inline void emit_dsll (unsigned int dst,unsigned int src, -unsigned int sa,struct jit_ctx *ctx) -{ -emit_instr (ctx, dsll, dst, src, sa); -} - -static inline void emit_dsrl32 (unsigned int dst,unsigned int src, -unsigned int sa,struct jit_ctx *ctx) -{ -emit_instr (ctx, dsrl32, dst, src, sa); -} - -static inline void emit_wsbh (unsigned int dst,unsigned int src, -struct jit_ctx *ctx) -{ -emit_instr (ctx, wsbh, dst, src); -} - -/* load pointer to register */ -static inline void emit_load_ptr (unsigned int dst,unsigned int src, -int imm,struct jit_ctx *ctx) -{ -/* src contains the base addr of the 32/64-pointer */ -emit_long_instr (ctx,LW , dst, imm, src); -} - -/* load a function pointer to register */ -static inline void emit_load_func (unsigned int reg,ptr imm, -struct jit_ctx *ctx) -{ -if (IS_ENABLED (CONFIG_64BIT )) { -/* At this point imm is always 64-bit */ -emit_load_imm (r_tmp, (u64 )imm >>32 , ctx); -emit_dsll (r_tmp_imm, r_tmp,16 , ctx);/* left shift by 16 */ -emit_ori (r_tmp, r_tmp_imm, (imm >>16 ) &0xffff , ctx); -emit_dsll (r_tmp_imm, r_tmp,16 , ctx);/* left shift by 16 */ -emit_ori (reg, r_tmp_imm, imm &0xffff , ctx); - }else { -emit_load_imm (reg, imm, ctx); - } -} - -/* Move to real MIPS register */ -static inline void emit_reg_move (ptr dst,ptr src,struct jit_ctx *ctx) -{ -emit_long_instr (ctx,ADDU , dst, src, r_zero); -} - -/* Move to JIT (32-bit) register */ -static inline void emit_jit_reg_move (ptr dst,ptr src,struct jit_ctx *ctx) -{ -emit_addu (dst, src, r_zero, ctx); -} - -/* Compute the immediate value for PC-relative branches. */ -static inline u32 b_imm (unsigned int tgt,struct jit_ctx *ctx) -{ -if (ctx->target ==NULL ) -return 0 ; - -/* -* We want a pc-relative branch. We only do forward branches -* so tgt is always after pc. tgt is the instruction offset -* we want to jump to. - -* Branch on MIPS: -* I: target_offset <- sign_extend(offset) -* I+1: PC += target_offset (delay slot) -* -* ctx->idx currently points to the branch instruction -* but the offset is added to the delay slot so we need -* to subtract 4. -*/ -return ctx->offsets[tgt] - - (ctx->idx *4 - ctx->prologue_bytes) -4 ; -} - -static inline void emit_bcond (int cond,unsigned int reg1,unsigned int reg2, -unsigned int imm,struct jit_ctx *ctx) -{ -if (ctx->target !=NULL ) { -u32 *p = &ctx->target[ctx->idx]; - -switch (cond) { -case MIPS_COND_EQ : -uasm_i_beq (&p, reg1, reg2, imm); -break ; -case MIPS_COND_NE : -uasm_i_bne (&p, reg1, reg2, imm); -break ; -case MIPS_COND_ALL : -uasm_i_b (&p, imm); -break ; -default : -pr_warn ("%s: Unhandled branch conditional: %d\n" , - __func__, cond); - } - } - ctx->idx++; -} - -static inline void emit_b (unsigned int imm,struct jit_ctx *ctx) -{ -emit_bcond (MIPS_COND_ALL , r_zero, r_zero, imm, ctx); -} - -static inline void emit_jalr (unsigned int link,unsigned int reg, -struct jit_ctx *ctx) -{ -emit_instr (ctx, jalr, link, reg); -} - -static inline void emit_jr (unsigned int reg,struct jit_ctx *ctx) -{ -emit_instr (ctx, jr, reg); -} - -static inline u16 align_sp (unsigned int num) -{ -/* Double word alignment for 32-bit, quadword for 64-bit */ -unsigned int align =IS_ENABLED (CONFIG_64BIT ) ?16 :8 ; - num = (num + (align -1 )) & -align; -return num; -} - -static void save_bpf_jit_regs (struct jit_ctx *ctx,unsigned offset) -{ -int i =0 , real_off =0 ; -u32 sflags, tmp_flags; - -/* Adjust the stack pointer */ -if (offset) -emit_stack_offset (-align_sp (offset), ctx); - - tmp_flags = sflags = ctx->flags >>SEEN_SREG_SFT ; -/* sflags is essentially a bitmap */ -while (tmp_flags) { -if ((sflags >> i) &0x1 ) { -emit_store_stack_reg (MIPS_R_S0 + i, r_sp, real_off, - ctx); - real_off +=SZREG ; - } - i++; - tmp_flags >>=1 ; - } - -/* save return address */ -if (ctx->flags &SEEN_CALL ) { -emit_store_stack_reg (r_ra, r_sp, real_off, ctx); - real_off +=SZREG ; - } - -/* Setup r_M leaving the alignment gap if necessary */ -if (ctx->flags &SEEN_MEM ) { -if (real_off % (SZREG *2 )) - real_off +=SZREG ; -emit_long_instr (ctx,ADDIU , r_M, r_sp, real_off); - } -} - -static void restore_bpf_jit_regs (struct jit_ctx *ctx, -unsigned int offset) -{ -int i, real_off =0 ; -u32 sflags, tmp_flags; - - tmp_flags = sflags = ctx->flags >>SEEN_SREG_SFT ; -/* sflags is a bitmap */ - i =0 ; -while (tmp_flags) { -if ((sflags >> i) &0x1 ) { -emit_load_stack_reg (MIPS_R_S0 + i, r_sp, real_off, - ctx); - real_off +=SZREG ; - } - i++; - tmp_flags >>=1 ; - } - -/* restore return address */ -if (ctx->flags &SEEN_CALL ) -emit_load_stack_reg (r_ra, r_sp, real_off, ctx); - -/* Restore the sp and discard the scrach memory */ -if (offset) -emit_stack_offset (align_sp (offset), ctx); -} - -static unsigned int get_stack_depth (struct jit_ctx *ctx) -{ -int sp_off =0 ; - - -/* How may s* regs do we need to preserved? */ - sp_off +=hweight32 (ctx->flags >>SEEN_SREG_SFT ) *SZREG ; - -if (ctx->flags &SEEN_MEM ) - sp_off +=4 *BPF_MEMWORDS ;/* BPF_MEMWORDS are 32-bit */ - -if (ctx->flags &SEEN_CALL ) - sp_off +=SZREG ;/* Space for our ra register */ - -return sp_off; -} - -static void build_prologue (struct jit_ctx *ctx) -{ -int sp_off; - -/* Calculate the total offset for the stack pointer */ - sp_off =get_stack_depth (ctx); -save_bpf_jit_regs (ctx, sp_off); - -if (ctx->flags &SEEN_SKB ) -emit_reg_move (r_skb,MIPS_R_A0 , ctx); - -if (ctx->flags &SEEN_SKB_DATA ) { -/* Load packet length */ -emit_load (r_skb_len, r_skb, offsetof(struct sk_buff , len), - ctx); -emit_load (r_tmp, r_skb, offsetof(struct sk_buff , data_len), - ctx); -/* Load the data pointer */ -emit_load_ptr (r_skb_data, r_skb, - offsetof(struct sk_buff , data), ctx); -/* Load the header length */ -emit_subu (r_skb_hl, r_skb_len, r_tmp, ctx); - } - -if (ctx->flags &SEEN_X ) -emit_jit_reg_move (r_X, r_zero, ctx); - -/* -* Do not leak kernel data to userspace, we only need to clear -* r_A if it is ever used. In fact if it is never used, we -* will not save/restore it, so clearing it in this case would -* corrupt the state of the caller. -*/ -if (bpf_needs_clear_a (&ctx->skf->insns[0 ]) && - (ctx->flags &SEEN_A )) -emit_jit_reg_move (r_A, r_zero, ctx); -} - -static void build_epilogue (struct jit_ctx *ctx) -{ -unsigned int sp_off; - -/* Calculate the total offset for the stack pointer */ - - sp_off =get_stack_depth (ctx); -restore_bpf_jit_regs (ctx, sp_off); - -/* Return */ -emit_jr (r_ra, ctx); -emit_nop (ctx); -} - -#define CHOOSE_LOAD_FUNC (K , func) \ - ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \ - func##_positive) - -static bool is_bad_offset (int b_off) -{ -return b_off >0x1ffff || b_off <-0x20000 ; -} - -static int build_body (struct jit_ctx *ctx) -{ -const struct bpf_prog *prog = ctx->skf; -const struct sock_filter *inst; -unsigned int i, off, condt; -u32 k, b_off__maybe_unused ; -u8 (*sk_load_func)(unsigned long *skb,int offset); - -for (i =0 ; i < prog->len; i++) { -u16 code; - - inst = &(prog->insns[i]); -pr_debug ("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n" , - __func__, inst->code, inst->jt, inst->jf, inst->k); - k = inst->k; - code =bpf_anc_helper (inst); - -if (ctx->target ==NULL ) - ctx->offsets[i] = ctx->idx *4 ; - -switch (code) { -case BPF_LD |BPF_IMM : -/* A <- k ==> li r_A, k */ - ctx->flags |=SEEN_A ; -emit_load_imm (r_A, k, ctx); -break ; -case BPF_LD |BPF_W |BPF_LEN : - BUILD_BUG_ON(sizeof_field(struct sk_buff, len) != 4); -/* A <- len ==> lw r_A, offset(skb) */ - ctx->flags |=SEEN_SKB |SEEN_A ; - off = offsetof(struct sk_buff , len); -emit_load (r_A, r_skb, off, ctx); -break ; -case BPF_LD |BPF_MEM : -/* A <- M[k] ==> lw r_A, offset(M) */ - ctx->flags |=SEEN_MEM |SEEN_A ; -emit_load (r_A, r_M,SCRATCH_OFF (k), ctx); -break ; -case BPF_LD |BPF_W |BPF_ABS : -/* A <- P[k:4] */ - sk_load_func =CHOOSE_LOAD_FUNC (k, sk_load_word); - goto load; -case BPF_LD |BPF_H |BPF_ABS : -/* A <- P[k:2] */ - sk_load_func =CHOOSE_LOAD_FUNC (k, sk_load_half); - goto load; -case BPF_LD |BPF_B |BPF_ABS : -/* A <- P[k:1] */ - sk_load_func =CHOOSE_LOAD_FUNC (k, sk_load_byte); -load: -emit_load_imm (r_off, k, ctx); -load_common: - ctx->flags |=SEEN_CALL |SEEN_OFF | -SEEN_SKB |SEEN_A |SEEN_SKB_DATA ; - -emit_load_func (r_s0, (ptr )sk_load_func, ctx); -emit_reg_move (MIPS_R_A0 , r_skb, ctx); -emit_jalr (MIPS_R_RA , r_s0, ctx); -/* Load second argument to delay slot */ -emit_reg_move (MIPS_R_A1 , r_off, ctx); -/* Check the error value */ -emit_bcond (MIPS_COND_EQ , r_ret,0 ,b_imm (i +1 , ctx), - ctx); -/* Load return register on DS for failures */ -emit_reg_move (r_ret, r_zero, ctx); -/* Return with error */ - b_off =b_imm (prog->len, ctx); -if (is_bad_offset (b_off)) -return -E2BIG ; -emit_b (b_off, ctx); -emit_nop (ctx); -break ; -case BPF_LD |BPF_W |BPF_IND : -/* A <- P[X + k:4] */ - sk_load_func = sk_load_word; - goto load_ind; -case BPF_LD |BPF_H |BPF_IND : -/* A <- P[X + k:2] */ - sk_load_func = sk_load_half; - goto load_ind; -case BPF_LD |BPF_B |BPF_IND : -/* A <- P[X + k:1] */ - sk_load_func = sk_load_byte; -load_ind: - ctx->flags |=SEEN_OFF |SEEN_X ; -emit_addiu (r_off, r_X, k, ctx); - goto load_common; -case BPF_LDX |BPF_IMM : -/* X <- k */ - ctx->flags |=SEEN_X ; -emit_load_imm (r_X, k, ctx); -break ; -case BPF_LDX |BPF_MEM : -/* X <- M[k] */ - ctx->flags |=SEEN_X |SEEN_MEM ; -emit_load (r_X, r_M,SCRATCH_OFF (k), ctx); -break ; -case BPF_LDX |BPF_W |BPF_LEN : -/* X <- len */ - ctx->flags |=SEEN_X |SEEN_SKB ; - off = offsetof(struct sk_buff , len); -emit_load (r_X, r_skb, off, ctx); -break ; -case BPF_LDX |BPF_B |BPF_MSH : -/* X <- 4 * (P[k:1] & 0xf) */ - ctx->flags |=SEEN_X |SEEN_CALL |SEEN_SKB ; -/* Load offset to a1 */ -emit_load_func (r_s0, (ptr )sk_load_byte, ctx); -/* -* This may emit two instructions so it may not fit -* in the delay slot. So use a0 in the delay slot. -*/ -emit_load_imm (MIPS_R_A1 , k, ctx); -emit_jalr (MIPS_R_RA , r_s0, ctx); -emit_reg_move (MIPS_R_A0 , r_skb, ctx);/* delay slot */ -/* Check the error value */ - b_off =b_imm (prog->len, ctx); -if (is_bad_offset (b_off)) -return -E2BIG ; -emit_bcond (MIPS_COND_NE , r_ret,0 , b_off, ctx); -emit_reg_move (r_ret, r_zero, ctx); -/* We are good */ -/* X <- P[1:K] & 0xf */ -emit_andi (r_X, r_A,0xf , ctx); -/* X << 2 */ -emit_b (b_imm (i +1 , ctx), ctx); -emit_sll (r_X, r_X,2 , ctx);/* delay slot */ -break ; -case BPF_ST : -/* M[k] <- A */ - ctx->flags |=SEEN_MEM |SEEN_A ; -emit_store (r_A, r_M,SCRATCH_OFF (k), ctx); -break ; -case BPF_STX : -/* M[k] <- X */ - ctx->flags |=SEEN_MEM |SEEN_X ; -emit_store (r_X, r_M,SCRATCH_OFF (k), ctx); -break ; -case BPF_ALU |BPF_ADD |BPF_K : -/* A += K */ - ctx->flags |=SEEN_A ; -emit_addiu (r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_ADD |BPF_X : -/* A += X */ - ctx->flags |=SEEN_A |SEEN_X ; -emit_addu (r_A, r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_SUB |BPF_K : -/* A -= K */ - ctx->flags |=SEEN_A ; -emit_addiu (r_A, r_A, -k, ctx); -break ; -case BPF_ALU |BPF_SUB |BPF_X : -/* A -= X */ - ctx->flags |=SEEN_A |SEEN_X ; -emit_subu (r_A, r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_MUL |BPF_K : -/* A *= K */ -/* Load K to scratch register before MUL */ - ctx->flags |=SEEN_A ; -emit_load_imm (r_s0, k, ctx); -emit_mul (r_A, r_A, r_s0, ctx); -break ; -case BPF_ALU |BPF_MUL |BPF_X : -/* A *= X */ - ctx->flags |=SEEN_A |SEEN_X ; -emit_mul (r_A, r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_DIV |BPF_K : -/* A /= k */ -if (k ==1 ) -break ; -if (optimize_div (&k)) { - ctx->flags |=SEEN_A ; -emit_srl (r_A, r_A, k, ctx); -break ; - } - ctx->flags |=SEEN_A ; -emit_load_imm (r_s0, k, ctx); -emit_div (r_A, r_s0, ctx); -break ; -case BPF_ALU |BPF_MOD |BPF_K : -/* A %= k */ -if (k ==1 ) { - ctx->flags |=SEEN_A ; -emit_jit_reg_move (r_A, r_zero, ctx); - }else { - ctx->flags |=SEEN_A ; -emit_load_imm (r_s0, k, ctx); -emit_mod (r_A, r_s0, ctx); - } -break ; -case BPF_ALU |BPF_DIV |BPF_X : -/* A /= X */ - ctx->flags |=SEEN_X |SEEN_A ; -/* Check if r_X is zero */ - b_off =b_imm (prog->len, ctx); -if (is_bad_offset (b_off)) -return -E2BIG ; -emit_bcond (MIPS_COND_EQ , r_X, r_zero, b_off, ctx); -emit_load_imm (r_ret,0 , ctx);/* delay slot */ -emit_div (r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_MOD |BPF_X : -/* A %= X */ - ctx->flags |=SEEN_X |SEEN_A ; -/* Check if r_X is zero */ - b_off =b_imm (prog->len, ctx); -if (is_bad_offset (b_off)) -return -E2BIG ; -emit_bcond (MIPS_COND_EQ , r_X, r_zero, b_off, ctx); -emit_load_imm (r_ret,0 , ctx);/* delay slot */ -emit_mod (r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_OR |BPF_K : -/* A |= K */ - ctx->flags |=SEEN_A ; -emit_ori (r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_OR |BPF_X : -/* A |= X */ - ctx->flags |=SEEN_A ; -emit_ori (r_A, r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_XOR |BPF_K : -/* A ^= k */ - ctx->flags |=SEEN_A ; -emit_xori (r_A, r_A, k, ctx); -break ; -case BPF_ANC |SKF_AD_ALU_XOR_X : -case BPF_ALU |BPF_XOR |BPF_X : -/* A ^= X */ - ctx->flags |=SEEN_A ; -emit_xor (r_A, r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_AND |BPF_K : -/* A &= K */ - ctx->flags |=SEEN_A ; -emit_andi (r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_AND |BPF_X : -/* A &= X */ - ctx->flags |=SEEN_A |SEEN_X ; -emit_and (r_A, r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_LSH |BPF_K : -/* A <<= K */ - ctx->flags |=SEEN_A ; -emit_sll (r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_LSH |BPF_X : -/* A <<= X */ - ctx->flags |=SEEN_A |SEEN_X ; -emit_sllv (r_A, r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_RSH |BPF_K : -/* A >>= K */ - ctx->flags |=SEEN_A ; -emit_srl (r_A, r_A, k, ctx); -break ; -case BPF_ALU |BPF_RSH |BPF_X : - ctx->flags |=SEEN_A |SEEN_X ; -emit_srlv (r_A, r_A, r_X, ctx); -break ; -case BPF_ALU |BPF_NEG : -/* A = -A */ - ctx->flags |=SEEN_A ; -emit_neg (r_A, ctx); -break ; -case BPF_JMP |BPF_JA : -/* pc += K */ - b_off =b_imm (i + k +1 , ctx); -if (is_bad_offset (b_off)) -return -E2BIG ; -emit_b (b_off, ctx); -emit_nop (ctx); -break ; -case BPF_JMP |BPF_JEQ |BPF_K : -/* pc += ( A == K ) ? pc->jt : pc->jf */ - condt =MIPS_COND_EQ |MIPS_COND_K ; - goto jmp_cmp; -case BPF_JMP |BPF_JEQ |BPF_X : - ctx->flags |=SEEN_X ; -/* pc += ( A == X ) ? pc->jt : pc->jf */ - condt =MIPS_COND_EQ |MIPS_COND_X ; - goto jmp_cmp; -case BPF_JMP |BPF_JGE |BPF_K : -/* pc += ( A >= K ) ? pc->jt : pc->jf */ - condt =MIPS_COND_GE |MIPS_COND_K ; - goto jmp_cmp; -case BPF_JMP |BPF_JGE |BPF_X : - ctx->flags |=SEEN_X ; -/* pc += ( A >= X ) ? pc->jt : pc->jf */ - condt =MIPS_COND_GE |MIPS_COND_X ; - goto jmp_cmp; -case BPF_JMP |BPF_JGT |BPF_K : -/* pc += ( A > K ) ? pc->jt : pc->jf */ - condt =MIPS_COND_GT |MIPS_COND_K ; - goto jmp_cmp; -case BPF_JMP |BPF_JGT |BPF_X : - ctx->flags |=SEEN_X ; -/* pc += ( A > X ) ? pc->jt : pc->jf */ - condt =MIPS_COND_GT |MIPS_COND_X ; -jmp_cmp: -/* Greater or Equal */ -if ((condt &MIPS_COND_GE ) || - (condt &MIPS_COND_GT )) { -if (condt &MIPS_COND_K ) {/* K */ - ctx->flags |=SEEN_A ; -emit_sltiu (r_s0, r_A, k, ctx); - }else {/* X */ - ctx->flags |=SEEN_A | -SEEN_X ; -emit_sltu (r_s0, r_A, r_X, ctx); - } -/* A < (K|X) ? r_scrach = 1 */ - b_off =b_imm (i + inst->jf +1 , ctx); -emit_bcond (MIPS_COND_NE , r_s0, r_zero, b_off, - ctx); -emit_nop (ctx); -/* A > (K|X) ? scratch = 0 */ -if (condt &MIPS_COND_GT ) { -/* Checking for equality */ - ctx->flags |=SEEN_A |SEEN_X ; -if (condt &MIPS_COND_K ) -emit_load_imm (r_s0, k, ctx); -else -emit_jit_reg_move (r_s0, r_X, - ctx); - b_off =b_imm (i + inst->jf +1 , ctx); -emit_bcond (MIPS_COND_EQ , r_A, r_s0, - b_off, ctx); -emit_nop (ctx); -/* Finally, A > K|X */ - b_off =b_imm (i + inst->jt +1 , ctx); -emit_b (b_off, ctx); -emit_nop (ctx); - }else { -/* A >= (K|X) so jump */ - b_off =b_imm (i + inst->jt +1 , ctx); -emit_b (b_off, ctx); -emit_nop (ctx); - } - }else { -/* A == K|X */ -if (condt &MIPS_COND_K ) {/* K */ - ctx->flags |=SEEN_A ; -emit_load_imm (r_s0, k, ctx); -/* jump true */ - b_off =b_imm (i + inst->jt +1 , ctx); -emit_bcond (MIPS_COND_EQ , r_A, r_s0, - b_off, ctx); -emit_nop (ctx); -/* jump false */ - b_off =b_imm (i + inst->jf +1 , - ctx); -emit_bcond (MIPS_COND_NE , r_A, r_s0, - b_off, ctx); -emit_nop (ctx); - }else {/* X */ -/* jump true */ - ctx->flags |=SEEN_A |SEEN_X ; - b_off =b_imm (i + inst->jt +1 , - ctx); -emit_bcond (MIPS_COND_EQ , r_A, r_X, - b_off, ctx); -emit_nop (ctx); -/* jump false */ - b_off =b_imm (i + inst->jf +1 , ctx); -emit_bcond (MIPS_COND_NE , r_A, r_X, - b_off, ctx); -emit_nop (ctx); - } - } -break ; -case BPF_JMP |BPF_JSET |BPF_K : - ctx->flags |=SEEN_A ; -/* pc += (A & K) ? pc -> jt : pc -> jf */ -emit_load_imm (r_s1, k, ctx); -emit_and (r_s0, r_A, r_s1, ctx); -/* jump true */ - b_off =b_imm (i + inst->jt +1 , ctx); -emit_bcond (MIPS_COND_NE , r_s0, r_zero, b_off, ctx); -emit_nop (ctx); -/* jump false */ - b_off =b_imm (i + inst->jf +1 , ctx); -emit_b (b_off, ctx); -emit_nop (ctx); -break ; -case BPF_JMP |BPF_JSET |BPF_X : - ctx->flags |=SEEN_X |SEEN_A ; -/* pc += (A & X) ? pc -> jt : pc -> jf */ -emit_and (r_s0, r_A, r_X, ctx); -/* jump true */ - b_off =b_imm (i + inst->jt +1 , ctx); -emit_bcond (MIPS_COND_NE , r_s0, r_zero, b_off, ctx); -emit_nop (ctx); -/* jump false */ - b_off =b_imm (i + inst->jf +1 , ctx); -emit_b (b_off, ctx); -emit_nop (ctx); -break ; -case BPF_RET |BPF_A : - ctx->flags |=SEEN_A ; -if (i != prog->len -1 ) { -/* -* If this is not the last instruction -* then jump to the epilogue -*/ - b_off =b_imm (prog->len, ctx); -if (is_bad_offset (b_off)) -return -E2BIG ; -emit_b (b_off, ctx); - } -emit_reg_move (r_ret, r_A, ctx);/* delay slot */ -break ; -case BPF_RET |BPF_K : -/* -* It can emit two instructions so it does not fit on -* the delay slot. -*/ -emit_load_imm (r_ret, k, ctx); -if (i != prog->len -1 ) { -/* -* If this is not the last instruction -* then jump to the epilogue -*/ - b_off =b_imm (prog->len, ctx); -if (is_bad_offset (b_off)) -return -E2BIG ; -emit_b (b_off, ctx); -emit_nop (ctx); - } -break ; -case BPF_MISC |BPF_TAX : -/* X = A */ - ctx->flags |=SEEN_X |SEEN_A ; -emit_jit_reg_move (r_X, r_A, ctx); -break ; -case BPF_MISC |BPF_TXA : -/* A = X */ - ctx->flags |=SEEN_A |SEEN_X ; -emit_jit_reg_move (r_A, r_X, ctx); -break ; -/* AUX */ -case BPF_ANC |SKF_AD_PROTOCOL : -/* A = ntohs(skb->protocol */ - ctx->flags |=SEEN_SKB |SEEN_OFF |SEEN_A ; -BUILD_BUG_ON (sizeof_field (struct sk_buff , - protocol) != 2); - off = offsetof(struct sk_buff , protocol); -emit_half_load (r_A, r_skb, off, ctx); -#ifdef CONFIG_CPU_LITTLE_ENDIAN -/* This needs little endian fixup */ -if (cpu_has_wsbh) { -/* R2 and later have the wsbh instruction */ -emit_wsbh (r_A, r_A, ctx); - }else { -/* Get first byte */ -emit_andi (r_tmp_imm, r_A,0xff , ctx); -/* Shift it */ -emit_sll (r_tmp, r_tmp_imm,8 , ctx); -/* Get second byte */ -emit_srl (r_tmp_imm, r_A,8 , ctx); -emit_andi (r_tmp_imm, r_tmp_imm,0xff , ctx); -/* Put everyting together in r_A */ -emit_or (r_A, r_tmp, r_tmp_imm, ctx); - } -#endif -break ; -case BPF_ANC |SKF_AD_CPU : - ctx->flags |=SEEN_A |SEEN_OFF ; -/* A = current_thread_info()->cpu */ -BUILD_BUG_ON (sizeof_field (struct thread_info , - cpu) != 4); - off = offsetof(struct thread_info , cpu); -/* $28/gp points to the thread_info struct */ -emit_load (r_A,28 , off, ctx); -break ; -case BPF_ANC |SKF_AD_IFINDEX : -/* A = skb->dev->ifindex */ -case BPF_ANC |SKF_AD_HATYPE : -/* A = skb->dev->type */ - ctx->flags |=SEEN_SKB |SEEN_A ; - off = offsetof(struct sk_buff , dev); -/* Load *dev pointer */ -emit_load_ptr (r_s0, r_skb, off, ctx); -/* error (0) in the delay slot */ - b_off =b_imm (prog->len, ctx); -if (is_bad_offset (b_off)) -return -E2BIG ; -emit_bcond (MIPS_COND_EQ , r_s0, r_zero, b_off, ctx); -emit_reg_move (r_ret, r_zero, ctx); -if (code == (BPF_ANC |SKF_AD_IFINDEX )) { - BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4); - off = offsetof(struct net_device , ifindex); -emit_load (r_A, r_s0, off, ctx); - }else {/* (code == (BPF_ANC | SKF_AD_HATYPE) */ - BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2); - off = offsetof(struct net_device , type); -emit_half_load_unsigned (r_A, r_s0, off, ctx); - } -break ; -case BPF_ANC |SKF_AD_MARK : - ctx->flags |=SEEN_SKB |SEEN_A ; - BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4); - off = offsetof(struct sk_buff , mark); -emit_load (r_A, r_skb, off, ctx); -break ; -case BPF_ANC |SKF_AD_RXHASH : - ctx->flags |=SEEN_SKB |SEEN_A ; - BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4); - off = offsetof(struct sk_buff , hash); -emit_load (r_A, r_skb, off, ctx); -break ; -case BPF_ANC |SKF_AD_VLAN_TAG : - ctx->flags |=SEEN_SKB |SEEN_A ; -BUILD_BUG_ON (sizeof_field (struct sk_buff , - vlan_tci) != 2); - off = offsetof(struct sk_buff , vlan_tci); -emit_half_load_unsigned (r_A, r_skb, off, ctx); -break ; -case BPF_ANC |SKF_AD_VLAN_TAG_PRESENT : - ctx->flags |=SEEN_SKB |SEEN_A ; -emit_load_byte (r_A, r_skb,PKT_VLAN_PRESENT_OFFSET (), ctx); -if (PKT_VLAN_PRESENT_BIT ) -emit_srl (r_A, r_A,PKT_VLAN_PRESENT_BIT , ctx); -if (PKT_VLAN_PRESENT_BIT <7 ) -emit_andi (r_A, r_A,1 , ctx); -break ; -case BPF_ANC |SKF_AD_PKTTYPE : - ctx->flags |=SEEN_SKB ; - -emit_load_byte (r_tmp, r_skb,PKT_TYPE_OFFSET (), ctx); -/* Keep only the last 3 bits */ -emit_andi (r_A, r_tmp,PKT_TYPE_MAX , ctx); -#ifdef __BIG_ENDIAN_BITFIELD -/* Get the actual packet type to the lower 3 bits */ -emit_srl (r_A, r_A,5 , ctx); -#endif -break ; -case BPF_ANC |SKF_AD_QUEUE : - ctx->flags |=SEEN_SKB |SEEN_A ; -BUILD_BUG_ON (sizeof_field (struct sk_buff , - queue_mapping) != 2); -BUILD_BUG_ON (offsetof(struct sk_buff , - queue_mapping) >0xff ); - off = offsetof(struct sk_buff , queue_mapping); -emit_half_load_unsigned (r_A, r_skb, off, ctx); -break ; -default : -pr_debug ("%s: Unhandled opcode: 0x%02x\n" , __FILE__, - inst->code); -return -1 ; - } - } - -/* compute offsets only during the first pass */ -if (ctx->target ==NULL ) - ctx->offsets[i] = ctx->idx *4 ; - -return 0 ; -} - -void bpf_jit_compile (struct bpf_prog *fp) -{ -struct jit_ctx ctx; -unsigned int alloc_size, tmp_idx; - -if (!bpf_jit_enable) -return ; - -memset (&ctx,0 ,sizeof (ctx)); - - ctx.offsets =kcalloc (fp->len +1 ,sizeof (*ctx.offsets),GFP_KERNEL ); -if (ctx.offsets ==NULL ) -return ; - - ctx.skf = fp; - -if (build_body (&ctx)) - goto out; - - tmp_idx = ctx.idx; -build_prologue (&ctx); - ctx.prologue_bytes = (ctx.idx - tmp_idx) *4 ; -/* just to complete the ctx.idx count */ -build_epilogue (&ctx); - - alloc_size =4 * ctx.idx; - ctx.target =module_alloc (alloc_size); -if (ctx.target ==NULL ) - goto out; - -/* Clean it */ -memset (ctx.target,0 , alloc_size); - - ctx.idx =0 ; - -/* Generate the actual JIT code */ -build_prologue (&ctx); -if (build_body (&ctx)) { -module_memfree (ctx.target); - goto out; - } -build_epilogue (&ctx); - -/* Update the icache */ -flush_icache_range ((ptr )ctx.target, (ptr)(ctx.target + ctx.idx)); - -if (bpf_jit_enable >1 ) -/* Dump JIT code */ -bpf_jit_dump (fp->len, alloc_size,2 , ctx.target); - - fp->bpf_func = (void *)ctx.target; - fp->jited =1 ; - -out: -kfree (ctx.offsets); -} - -void bpf_jit_free (struct bpf_prog *fp) -{ -if (fp->jited) -module_memfree (fp->bpf_func); - -bpf_prog_unlock_free (fp); -}
arch/mips/net/bpf_jit.h deleted +0 -81
81 lines not on this page · raw message
arch/mips/net/bpf_jit_asm.S deleted +0 -285
285 lines not on this page · raw message
arch/mips/net/ebpf_jit.c deleted +0 -1938
1,335 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.30.2
Part of the diff is not shown here. View the raw message.
This series was applied to bpf/bpf-next.git (refs/heads/master):
Hello:
This series was applied to bpf/bpf-next.git (refs/heads/master):
Quoted 10 lines · On Tue, 5 Oct 2021 18:54:01 +0200 you wrote:
This is an implementation of an eBPF JIT for MIPS I-V and MIPS32/64 r1-r6. The new JIT is written from scratch, but uses the same overall structure as other eBPF JITs.
Before, the MIPS JIT situation looked like this.
- 32-bit: MIPS32, cBPF-only, tests fail
- 64-bit: MIPS64r2-r6, eBPF, tests fail, incomplete eBPF ISA support[...]
Here is the summary with links:
- [1/7] MIPS: uasm: Enable muhu opcode for MIPS R6
https://git.kernel.org/bpf/bpf-next/c/52738ad51026 - [2/7] mips: uasm: Add workaround for Loongson-2F nop CPU errata https://git.kernel.org/bpf/bpf-next/c/be0f00d5a246 - [3/7] mips: bpf: Add eBPF JIT for 32-bit MIPS https://git.kernel.org/bpf/bpf-next/c/88dfe3f95766 - [4/7] mips: bpf: Add new eBPF JIT for 64-bit MIPS https://git.kernel.org/bpf/bpf-next/c/42fb8eacf86e - [5/7] mips: bpf: Add JIT workarounds for CPU errata https://git.kernel.org/bpf/bpf-next/c/a1db4f358142 - [6/7] mips: bpf: Enable eBPF JITs https://git.kernel.org/bpf/bpf-next/c/6675d4a60007 - [7/7] mips: bpf: Remove old BPF JIT implementations https://git.kernel.org/bpf/bpf-next/c/06b339fe5450
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html