2 * bpf_jib_asm.S: Packet/header access helper functions for MIPS/MIPS64 BPF
5 * Copyright (C) 2015 Imagination Technologies Ltd.
6 * Author: Markos Chandras <markos.chandras@imgtec.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; version 2 of the License.
14 #include <asm/regdef.h>
19 * r_skb_hl skb header length
21 * r_off(a1) offset register
27 * r_s0 Scratch register 0
28 * r_s1 Scratch register 1
32 * a1: offset (imm or imm + X)
34 * All non-BPF-ABI registers are free for use. On return, we only
35 * care about r_ret. The BPF-ABI registers are assumed to remain
36 * unmodified during the entire filter operation.
41 #define SKF_LL_OFF (-0x200000) /* Can't include linux/filter.h in assembly */
43 /* We know better :) so prevent assembler reordering etc */
46 #define is_offset_negative(TYPE) \
47 /* If offset is negative we have more work to do */ \
49 bgtz t0, bpf_slow_path_##TYPE##_neg; \
50 /* Be careful what follows in DS. */
52 #define is_offset_in_header(SIZE, TYPE) \
53 /* Reading from header? */ \
54 addiu $r_s0, $r_skb_hl, -SIZE; \
55 slt t0, $r_s0, offset; \
56 bgtz t0, bpf_slow_path_##TYPE; \
59 is_offset_negative(word)
60 .globl sk_load_word_positive
61 sk_load_word_positive:
62 is_offset_in_header(4, word)
63 /* Offset within header boundaries */
64 PTR_ADDU t1, $r_skb_data, offset
66 #ifdef CONFIG_CPU_LITTLE_ENDIAN
67 # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
87 is_offset_negative(half)
88 .globl sk_load_half_positive
89 sk_load_half_positive:
90 is_offset_in_header(2, half)
91 /* Offset within header boundaries */
92 PTR_ADDU t1, $r_skb_data, offset
94 #ifdef CONFIG_CPU_LITTLE_ENDIAN
95 # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
100 andi t1, $r_A, 0xff00
111 is_offset_negative(byte)
112 .globl sk_load_byte_positive
113 sk_load_byte_positive:
114 is_offset_in_header(1, byte)
115 /* Offset within header boundaries */
116 PTR_ADDU t1, $r_skb_data, offset
123 * call skb_copy_bits:
124 * (prototype in linux/skbuff.h)
126 * int skb_copy_bits(sk_buff *skb, int offset, void *to, int len)
128 * o32 mandates we leave 4 spaces for argument registers in case
129 * the callee needs to use them. Even though we don't care about
130 * the argument registers ourselves, we need to allocate that space
131 * to remain ABI compliant since the callee may want to use that space.
132 * We also allocate 2 more spaces for $r_ra and our return register (*to).
134 * n64 is a bit different. The *caller* will allocate the space to preserve
135 * the arguments. So in 64-bit kernels, we allocate the 4-arg space for no
136 * good reason but it does not matter that much really.
138 * (void *to) is returned in r_s0
141 #define bpf_slow_path_common(SIZE) \
142 /* Quick check. Are we within reasonable boundaries? */ \
143 LONG_ADDIU $r_s1, $r_skb_len, -SIZE; \
144 sltu $r_s0, offset, $r_s1; \
146 /* Load 4th argument in DS */ \
147 LONG_ADDIU a3, zero, SIZE; \
148 PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
149 PTR_LA t0, skb_copy_bits; \
150 PTR_S $r_ra, (5 * SZREG)($r_sp); \
151 /* Assign low slot to a2 */ \
154 /* Reset our destination slot (DS but it's ok) */ \
155 INT_S zero, (4 * SZREG)($r_sp); \
157 * skb_copy_bits returns 0 on success and -EFAULT \
158 * on error. Our data live in a2. Do not bother with \
159 * our data if an error has been returned. \
161 /* Restore our frame */ \
162 PTR_L $r_ra, (5 * SZREG)($r_sp); \
163 INT_L $r_s0, (4 * SZREG)($r_sp); \
165 PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
168 NESTED(bpf_slow_path_word, (6 * SZREG), $r_sp)
169 bpf_slow_path_common(4)
170 #ifdef CONFIG_CPU_LITTLE_ENDIAN
171 # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
181 andi t1, $r_s0, 0xff00
192 END(bpf_slow_path_word)
194 NESTED(bpf_slow_path_half, (6 * SZREG), $r_sp)
195 bpf_slow_path_common(2)
196 #ifdef CONFIG_CPU_LITTLE_ENDIAN
197 # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
202 andi t1, $r_s0, 0xff00
213 END(bpf_slow_path_half)
215 NESTED(bpf_slow_path_byte, (6 * SZREG), $r_sp)
216 bpf_slow_path_common(1)
220 END(bpf_slow_path_byte)
223 * Negative entry points
225 .macro bpf_is_end_of_data
227 /* Reading link layer data? */
230 /* Be careful what follows in DS. */
233 * call skb_copy_bits:
234 * (prototype in linux/filter.h)
236 * void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
237 * int k, unsigned int size)
239 * see above (bpf_slow_path_common) for ABI restrictions
241 #define bpf_negative_common(SIZE) \
242 PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
243 PTR_LA t0, bpf_internal_load_pointer_neg_helper; \
244 PTR_S $r_ra, (5 * SZREG)($r_sp); \
247 PTR_L $r_ra, (5 * SZREG)($r_sp); \
248 /* Check return pointer */ \
250 PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
251 /* Preserve our pointer */ \
253 /* Set return value */ \
256 bpf_slow_path_word_neg:
258 NESTED(sk_load_word_negative, (6 * SZREG), $r_sp)
259 bpf_negative_common(4)
262 END(sk_load_word_negative)
264 bpf_slow_path_half_neg:
266 NESTED(sk_load_half_negative, (6 * SZREG), $r_sp)
267 bpf_negative_common(2)
270 END(sk_load_half_negative)
272 bpf_slow_path_byte_neg:
274 NESTED(sk_load_byte_negative, (6 * SZREG), $r_sp)
275 bpf_negative_common(1)
278 END(sk_load_byte_negative)
282 addiu $r_ret, zero, 1