e25c336831dcbb5df0d5c2cb01d93ad2abbe1362
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 071-v5.16-06-mips-bpf-Remove-old-BPF-JIT-implementations.patch
1 From: Johan Almbladh <johan.almbladh@anyfinetworks.com>
2 Date: Tue, 5 Oct 2021 18:54:08 +0200
3 Subject: [PATCH] mips: bpf: Remove old BPF JIT implementations
4
5 This patch removes the old 32-bit cBPF and 64-bit eBPF JIT implementations.
6 They are replaced by a new eBPF implementation that supports both 32-bit
7 and 64-bit MIPS CPUs.
8
9 Signed-off-by: Johan Almbladh <johan.almbladh@anyfinetworks.com>
10 ---
11 delete mode 100644 arch/mips/net/bpf_jit.c
12 delete mode 100644 arch/mips/net/bpf_jit.h
13 delete mode 100644 arch/mips/net/bpf_jit_asm.S
14 delete mode 100644 arch/mips/net/ebpf_jit.c
15
16 --- a/arch/mips/net/bpf_jit.h
17 +++ /dev/null
18 @@ -1,81 +0,0 @@
19 -/* SPDX-License-Identifier: GPL-2.0-only */
20 -/*
21 - * Just-In-Time compiler for BPF filters on MIPS
22 - *
23 - * Copyright (c) 2014 Imagination Technologies Ltd.
24 - * Author: Markos Chandras <markos.chandras@imgtec.com>
25 - */
26 -
27 -#ifndef BPF_JIT_MIPS_OP_H
28 -#define BPF_JIT_MIPS_OP_H
29 -
30 -/* Registers used by JIT */
31 -#define MIPS_R_ZERO 0
32 -#define MIPS_R_V0 2
33 -#define MIPS_R_A0 4
34 -#define MIPS_R_A1 5
35 -#define MIPS_R_T4 12
36 -#define MIPS_R_T5 13
37 -#define MIPS_R_T6 14
38 -#define MIPS_R_T7 15
39 -#define MIPS_R_S0 16
40 -#define MIPS_R_S1 17
41 -#define MIPS_R_S2 18
42 -#define MIPS_R_S3 19
43 -#define MIPS_R_S4 20
44 -#define MIPS_R_S5 21
45 -#define MIPS_R_S6 22
46 -#define MIPS_R_S7 23
47 -#define MIPS_R_SP 29
48 -#define MIPS_R_RA 31
49 -
50 -/* Conditional codes */
51 -#define MIPS_COND_EQ 0x1
52 -#define MIPS_COND_GE (0x1 << 1)
53 -#define MIPS_COND_GT (0x1 << 2)
54 -#define MIPS_COND_NE (0x1 << 3)
55 -#define MIPS_COND_ALL (0x1 << 4)
56 -/* Conditionals on X register or K immediate */
57 -#define MIPS_COND_X (0x1 << 5)
58 -#define MIPS_COND_K (0x1 << 6)
59 -
60 -#define r_ret MIPS_R_V0
61 -
62 -/*
63 - * Use 2 scratch registers to avoid pipeline interlocks.
64 - * There is no overhead during epilogue and prologue since
65 - * any of the $s0-$s6 registers will only be preserved if
66 - * they are going to actually be used.
67 - */
68 -#define r_skb_hl MIPS_R_S0 /* skb header length */
69 -#define r_skb_data MIPS_R_S1 /* skb actual data */
70 -#define r_off MIPS_R_S2
71 -#define r_A MIPS_R_S3
72 -#define r_X MIPS_R_S4
73 -#define r_skb MIPS_R_S5
74 -#define r_M MIPS_R_S6
75 -#define r_skb_len MIPS_R_S7
76 -#define r_s0 MIPS_R_T4 /* scratch reg 1 */
77 -#define r_s1 MIPS_R_T5 /* scratch reg 2 */
78 -#define r_tmp_imm MIPS_R_T6 /* No need to preserve this */
79 -#define r_tmp MIPS_R_T7 /* No need to preserve this */
80 -#define r_zero MIPS_R_ZERO
81 -#define r_sp MIPS_R_SP
82 -#define r_ra MIPS_R_RA
83 -
84 -#ifndef __ASSEMBLY__
85 -
86 -/* Declare ASM helpers */
87 -
88 -#define DECLARE_LOAD_FUNC(func) \
89 - extern u8 func(unsigned long *skb, int offset); \
90 - extern u8 func##_negative(unsigned long *skb, int offset); \
91 - extern u8 func##_positive(unsigned long *skb, int offset)
92 -
93 -DECLARE_LOAD_FUNC(sk_load_word);
94 -DECLARE_LOAD_FUNC(sk_load_half);
95 -DECLARE_LOAD_FUNC(sk_load_byte);
96 -
97 -#endif
98 -
99 -#endif /* BPF_JIT_MIPS_OP_H */
100 --- a/arch/mips/net/bpf_jit_asm.S
101 +++ /dev/null
102 @@ -1,285 +0,0 @@
103 -/*
104 - * bpf_jib_asm.S: Packet/header access helper functions for MIPS/MIPS64 BPF
105 - * compiler.
106 - *
107 - * Copyright (C) 2015 Imagination Technologies Ltd.
108 - * Author: Markos Chandras <markos.chandras@imgtec.com>
109 - *
110 - * This program is free software; you can redistribute it and/or modify it
111 - * under the terms of the GNU General Public License as published by the
112 - * Free Software Foundation; version 2 of the License.
113 - */
114 -
115 -#include <asm/asm.h>
116 -#include <asm/isa-rev.h>
117 -#include <asm/regdef.h>
118 -#include "bpf_jit.h"
119 -
120 -/* ABI
121 - *
122 - * r_skb_hl skb header length
123 - * r_skb_data skb data
124 - * r_off(a1) offset register
125 - * r_A BPF register A
126 - * r_X PF register X
127 - * r_skb(a0) *skb
128 - * r_M *scratch memory
129 - * r_skb_le skb length
130 - * r_s0 Scratch register 0
131 - * r_s1 Scratch register 1
132 - *
133 - * On entry:
134 - * a0: *skb
135 - * a1: offset (imm or imm + X)
136 - *
137 - * All non-BPF-ABI registers are free for use. On return, we only
138 - * care about r_ret. The BPF-ABI registers are assumed to remain
139 - * unmodified during the entire filter operation.
140 - */
141 -
142 -#define skb a0
143 -#define offset a1
144 -#define SKF_LL_OFF (-0x200000) /* Can't include linux/filter.h in assembly */
145 -
146 - /* We know better :) so prevent assembler reordering etc */
147 - .set noreorder
148 -
149 -#define is_offset_negative(TYPE) \
150 - /* If offset is negative we have more work to do */ \
151 - slti t0, offset, 0; \
152 - bgtz t0, bpf_slow_path_##TYPE##_neg; \
153 - /* Be careful what follows in DS. */
154 -
155 -#define is_offset_in_header(SIZE, TYPE) \
156 - /* Reading from header? */ \
157 - addiu $r_s0, $r_skb_hl, -SIZE; \
158 - slt t0, $r_s0, offset; \
159 - bgtz t0, bpf_slow_path_##TYPE; \
160 -
161 -LEAF(sk_load_word)
162 - is_offset_negative(word)
163 -FEXPORT(sk_load_word_positive)
164 - is_offset_in_header(4, word)
165 - /* Offset within header boundaries */
166 - PTR_ADDU t1, $r_skb_data, offset
167 - .set reorder
168 - lw $r_A, 0(t1)
169 - .set noreorder
170 -#ifdef CONFIG_CPU_LITTLE_ENDIAN
171 -# if MIPS_ISA_REV >= 2
172 - wsbh t0, $r_A
173 - rotr $r_A, t0, 16
174 -# else
175 - sll t0, $r_A, 24
176 - srl t1, $r_A, 24
177 - srl t2, $r_A, 8
178 - or t0, t0, t1
179 - andi t2, t2, 0xff00
180 - andi t1, $r_A, 0xff00
181 - or t0, t0, t2
182 - sll t1, t1, 8
183 - or $r_A, t0, t1
184 -# endif
185 -#endif
186 - jr $r_ra
187 - move $r_ret, zero
188 - END(sk_load_word)
189 -
190 -LEAF(sk_load_half)
191 - is_offset_negative(half)
192 -FEXPORT(sk_load_half_positive)
193 - is_offset_in_header(2, half)
194 - /* Offset within header boundaries */
195 - PTR_ADDU t1, $r_skb_data, offset
196 - lhu $r_A, 0(t1)
197 -#ifdef CONFIG_CPU_LITTLE_ENDIAN
198 -# if MIPS_ISA_REV >= 2
199 - wsbh $r_A, $r_A
200 -# else
201 - sll t0, $r_A, 8
202 - srl t1, $r_A, 8
203 - andi t0, t0, 0xff00
204 - or $r_A, t0, t1
205 -# endif
206 -#endif
207 - jr $r_ra
208 - move $r_ret, zero
209 - END(sk_load_half)
210 -
211 -LEAF(sk_load_byte)
212 - is_offset_negative(byte)
213 -FEXPORT(sk_load_byte_positive)
214 - is_offset_in_header(1, byte)
215 - /* Offset within header boundaries */
216 - PTR_ADDU t1, $r_skb_data, offset
217 - lbu $r_A, 0(t1)
218 - jr $r_ra
219 - move $r_ret, zero
220 - END(sk_load_byte)
221 -
222 -/*
223 - * call skb_copy_bits:
224 - * (prototype in linux/skbuff.h)
225 - *
226 - * int skb_copy_bits(sk_buff *skb, int offset, void *to, int len)
227 - *
228 - * o32 mandates we leave 4 spaces for argument registers in case
229 - * the callee needs to use them. Even though we don't care about
230 - * the argument registers ourselves, we need to allocate that space
231 - * to remain ABI compliant since the callee may want to use that space.
232 - * We also allocate 2 more spaces for $r_ra and our return register (*to).
233 - *
234 - * n64 is a bit different. The *caller* will allocate the space to preserve
235 - * the arguments. So in 64-bit kernels, we allocate the 4-arg space for no
236 - * good reason but it does not matter that much really.
237 - *
238 - * (void *to) is returned in r_s0
239 - *
240 - */
241 -#ifdef CONFIG_CPU_LITTLE_ENDIAN
242 -#define DS_OFFSET(SIZE) (4 * SZREG)
243 -#else
244 -#define DS_OFFSET(SIZE) ((4 * SZREG) + (4 - SIZE))
245 -#endif
246 -#define bpf_slow_path_common(SIZE) \
247 - /* Quick check. Are we within reasonable boundaries? */ \
248 - LONG_ADDIU $r_s1, $r_skb_len, -SIZE; \
249 - sltu $r_s0, offset, $r_s1; \
250 - beqz $r_s0, fault; \
251 - /* Load 4th argument in DS */ \
252 - LONG_ADDIU a3, zero, SIZE; \
253 - PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
254 - PTR_LA t0, skb_copy_bits; \
255 - PTR_S $r_ra, (5 * SZREG)($r_sp); \
256 - /* Assign low slot to a2 */ \
257 - PTR_ADDIU a2, $r_sp, DS_OFFSET(SIZE); \
258 - jalr t0; \
259 - /* Reset our destination slot (DS but it's ok) */ \
260 - INT_S zero, (4 * SZREG)($r_sp); \
261 - /* \
262 - * skb_copy_bits returns 0 on success and -EFAULT \
263 - * on error. Our data live in a2. Do not bother with \
264 - * our data if an error has been returned. \
265 - */ \
266 - /* Restore our frame */ \
267 - PTR_L $r_ra, (5 * SZREG)($r_sp); \
268 - INT_L $r_s0, (4 * SZREG)($r_sp); \
269 - bltz v0, fault; \
270 - PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
271 - move $r_ret, zero; \
272 -
273 -NESTED(bpf_slow_path_word, (6 * SZREG), $r_sp)
274 - bpf_slow_path_common(4)
275 -#ifdef CONFIG_CPU_LITTLE_ENDIAN
276 -# if MIPS_ISA_REV >= 2
277 - wsbh t0, $r_s0
278 - jr $r_ra
279 - rotr $r_A, t0, 16
280 -# else
281 - sll t0, $r_s0, 24
282 - srl t1, $r_s0, 24
283 - srl t2, $r_s0, 8
284 - or t0, t0, t1
285 - andi t2, t2, 0xff00
286 - andi t1, $r_s0, 0xff00
287 - or t0, t0, t2
288 - sll t1, t1, 8
289 - jr $r_ra
290 - or $r_A, t0, t1
291 -# endif
292 -#else
293 - jr $r_ra
294 - move $r_A, $r_s0
295 -#endif
296 -
297 - END(bpf_slow_path_word)
298 -
299 -NESTED(bpf_slow_path_half, (6 * SZREG), $r_sp)
300 - bpf_slow_path_common(2)
301 -#ifdef CONFIG_CPU_LITTLE_ENDIAN
302 -# if MIPS_ISA_REV >= 2
303 - jr $r_ra
304 - wsbh $r_A, $r_s0
305 -# else
306 - sll t0, $r_s0, 8
307 - andi t1, $r_s0, 0xff00
308 - andi t0, t0, 0xff00
309 - srl t1, t1, 8
310 - jr $r_ra
311 - or $r_A, t0, t1
312 -# endif
313 -#else
314 - jr $r_ra
315 - move $r_A, $r_s0
316 -#endif
317 -
318 - END(bpf_slow_path_half)
319 -
320 -NESTED(bpf_slow_path_byte, (6 * SZREG), $r_sp)
321 - bpf_slow_path_common(1)
322 - jr $r_ra
323 - move $r_A, $r_s0
324 -
325 - END(bpf_slow_path_byte)
326 -
327 -/*
328 - * Negative entry points
329 - */
330 - .macro bpf_is_end_of_data
331 - li t0, SKF_LL_OFF
332 - /* Reading link layer data? */
333 - slt t1, offset, t0
334 - bgtz t1, fault
335 - /* Be careful what follows in DS. */
336 - .endm
337 -/*
338 - * call skb_copy_bits:
339 - * (prototype in linux/filter.h)
340 - *
341 - * void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
342 - * int k, unsigned int size)
343 - *
344 - * see above (bpf_slow_path_common) for ABI restrictions
345 - */
346 -#define bpf_negative_common(SIZE) \
347 - PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
348 - PTR_LA t0, bpf_internal_load_pointer_neg_helper; \
349 - PTR_S $r_ra, (5 * SZREG)($r_sp); \
350 - jalr t0; \
351 - li a2, SIZE; \
352 - PTR_L $r_ra, (5 * SZREG)($r_sp); \
353 - /* Check return pointer */ \
354 - beqz v0, fault; \
355 - PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
356 - /* Preserve our pointer */ \
357 - move $r_s0, v0; \
358 - /* Set return value */ \
359 - move $r_ret, zero; \
360 -
361 -bpf_slow_path_word_neg:
362 - bpf_is_end_of_data
363 -NESTED(sk_load_word_negative, (6 * SZREG), $r_sp)
364 - bpf_negative_common(4)
365 - jr $r_ra
366 - lw $r_A, 0($r_s0)
367 - END(sk_load_word_negative)
368 -
369 -bpf_slow_path_half_neg:
370 - bpf_is_end_of_data
371 -NESTED(sk_load_half_negative, (6 * SZREG), $r_sp)
372 - bpf_negative_common(2)
373 - jr $r_ra
374 - lhu $r_A, 0($r_s0)
375 - END(sk_load_half_negative)
376 -
377 -bpf_slow_path_byte_neg:
378 - bpf_is_end_of_data
379 -NESTED(sk_load_byte_negative, (6 * SZREG), $r_sp)
380 - bpf_negative_common(1)
381 - jr $r_ra
382 - lbu $r_A, 0($r_s0)
383 - END(sk_load_byte_negative)
384 -
385 -fault:
386 - jr $r_ra
387 - addiu $r_ret, zero, 1