blob: eabb5e0889ee422418040afcc18df0498541d8e2 [file] [log] [blame]
Markos Chandras266a88e2015-06-04 11:56:16 +01001/*
2 * bpf_jib_asm.S: Packet/header access helper functions for MIPS/MIPS64 BPF
3 * compiler.
4 *
5 * Copyright (C) 2015 Imagination Technologies Ltd.
6 * Author: Markos Chandras <markos.chandras@imgtec.com>
7 *
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.
11 */
12
13#include <asm/asm.h>
14#include <asm/regdef.h>
15#include "bpf_jit.h"
16
17/* ABI
18 *
19 * r_skb_hl skb header length
20 * r_skb_data skb data
21 * r_off(a1) offset register
22 * r_A BPF register A
23 * r_X PF register X
24 * r_skb(a0) *skb
25 * r_M *scratch memory
26 * r_skb_le skb length
27 * r_s0 Scratch register 0
28 * r_s1 Scratch register 1
29 *
30 * On entry:
31 * a0: *skb
32 * a1: offset (imm or imm + X)
33 *
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.
37 */
38
39#define skb a0
40#define offset a1
41#define SKF_LL_OFF (-0x200000) /* Can't include linux/filter.h in assembly */
42
43 /* We know better :) so prevent assembler reordering etc */
44 .set noreorder
45
46#define is_offset_negative(TYPE) \
47 /* If offset is negative we have more work to do */ \
48 slti t0, offset, 0; \
49 bgtz t0, bpf_slow_path_##TYPE##_neg; \
50 /* Be careful what follows in DS. */
51
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; \
57
58LEAF(sk_load_word)
59 is_offset_negative(word)
Ralf Baechle1e16a8f2015-10-01 15:45:44 +020060FEXPORT(sk_load_word_positive)
Markos Chandras266a88e2015-06-04 11:56:16 +010061 is_offset_in_header(4, word)
62 /* Offset within header boundaries */
63 PTR_ADDU t1, $r_skb_data, offset
64 lw $r_A, 0(t1)
65#ifdef CONFIG_CPU_LITTLE_ENDIAN
Aurelien Jarnob259e512015-09-05 18:46:57 +020066# if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
Markos Chandras266a88e2015-06-04 11:56:16 +010067 wsbh t0, $r_A
68 rotr $r_A, t0, 16
Aurelien Jarnob259e512015-09-05 18:46:57 +020069# else
70 sll t0, $r_A, 24
71 srl t1, $r_A, 24
72 srl t2, $r_A, 8
73 or t0, t0, t1
74 andi t2, t2, 0xff00
75 andi t1, $r_A, 0xff00
76 or t0, t0, t2
77 sll t1, t1, 8
78 or $r_A, t0, t1
79# endif
Markos Chandras266a88e2015-06-04 11:56:16 +010080#endif
81 jr $r_ra
82 move $r_ret, zero
83 END(sk_load_word)
84
85LEAF(sk_load_half)
86 is_offset_negative(half)
Ralf Baechle1e16a8f2015-10-01 15:45:44 +020087FEXPORT(sk_load_half_positive)
Markos Chandras266a88e2015-06-04 11:56:16 +010088 is_offset_in_header(2, half)
89 /* Offset within header boundaries */
90 PTR_ADDU t1, $r_skb_data, offset
91 lh $r_A, 0(t1)
92#ifdef CONFIG_CPU_LITTLE_ENDIAN
Aurelien Jarnob259e512015-09-05 18:46:57 +020093# if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
Markos Chandras266a88e2015-06-04 11:56:16 +010094 wsbh t0, $r_A
95 seh $r_A, t0
Aurelien Jarnob259e512015-09-05 18:46:57 +020096# else
97 sll t0, $r_A, 24
98 andi t1, $r_A, 0xff00
99 sra t0, t0, 16
100 srl t1, t1, 8
101 or $r_A, t0, t1
102# endif
Markos Chandras266a88e2015-06-04 11:56:16 +0100103#endif
104 jr $r_ra
105 move $r_ret, zero
106 END(sk_load_half)
107
108LEAF(sk_load_byte)
109 is_offset_negative(byte)
Ralf Baechle1e16a8f2015-10-01 15:45:44 +0200110FEXPORT(sk_load_byte_positive)
Markos Chandras266a88e2015-06-04 11:56:16 +0100111 is_offset_in_header(1, byte)
112 /* Offset within header boundaries */
113 PTR_ADDU t1, $r_skb_data, offset
114 lb $r_A, 0(t1)
115 jr $r_ra
116 move $r_ret, zero
117 END(sk_load_byte)
118
119/*
120 * call skb_copy_bits:
121 * (prototype in linux/skbuff.h)
122 *
123 * int skb_copy_bits(sk_buff *skb, int offset, void *to, int len)
124 *
125 * o32 mandates we leave 4 spaces for argument registers in case
126 * the callee needs to use them. Even though we don't care about
127 * the argument registers ourselves, we need to allocate that space
128 * to remain ABI compliant since the callee may want to use that space.
129 * We also allocate 2 more spaces for $r_ra and our return register (*to).
130 *
131 * n64 is a bit different. The *caller* will allocate the space to preserve
132 * the arguments. So in 64-bit kernels, we allocate the 4-arg space for no
133 * good reason but it does not matter that much really.
134 *
135 * (void *to) is returned in r_s0
136 *
137 */
138#define bpf_slow_path_common(SIZE) \
139 /* Quick check. Are we within reasonable boundaries? */ \
140 LONG_ADDIU $r_s1, $r_skb_len, -SIZE; \
141 sltu $r_s0, offset, $r_s1; \
142 beqz $r_s0, fault; \
143 /* Load 4th argument in DS */ \
144 LONG_ADDIU a3, zero, SIZE; \
145 PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
146 PTR_LA t0, skb_copy_bits; \
147 PTR_S $r_ra, (5 * SZREG)($r_sp); \
148 /* Assign low slot to a2 */ \
149 move a2, $r_sp; \
150 jalr t0; \
151 /* Reset our destination slot (DS but it's ok) */ \
152 INT_S zero, (4 * SZREG)($r_sp); \
153 /* \
154 * skb_copy_bits returns 0 on success and -EFAULT \
155 * on error. Our data live in a2. Do not bother with \
156 * our data if an error has been returned. \
157 */ \
158 /* Restore our frame */ \
159 PTR_L $r_ra, (5 * SZREG)($r_sp); \
160 INT_L $r_s0, (4 * SZREG)($r_sp); \
161 bltz v0, fault; \
162 PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
163 move $r_ret, zero; \
164
165NESTED(bpf_slow_path_word, (6 * SZREG), $r_sp)
166 bpf_slow_path_common(4)
167#ifdef CONFIG_CPU_LITTLE_ENDIAN
Aurelien Jarnob259e512015-09-05 18:46:57 +0200168# if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
Markos Chandras266a88e2015-06-04 11:56:16 +0100169 wsbh t0, $r_s0
170 jr $r_ra
171 rotr $r_A, t0, 16
Aurelien Jarnob259e512015-09-05 18:46:57 +0200172# else
173 sll t0, $r_s0, 24
174 srl t1, $r_s0, 24
175 srl t2, $r_s0, 8
176 or t0, t0, t1
177 andi t2, t2, 0xff00
178 andi t1, $r_s0, 0xff00
179 or t0, t0, t2
180 sll t1, t1, 8
181 jr $r_ra
182 or $r_A, t0, t1
183# endif
Aurelien Jarnofaa97242015-09-05 18:46:56 +0200184#else
Markos Chandras266a88e2015-06-04 11:56:16 +0100185 jr $r_ra
Aurelien Jarnofaa97242015-09-05 18:46:56 +0200186 move $r_A, $r_s0
187#endif
Markos Chandras266a88e2015-06-04 11:56:16 +0100188
189 END(bpf_slow_path_word)
190
191NESTED(bpf_slow_path_half, (6 * SZREG), $r_sp)
192 bpf_slow_path_common(2)
193#ifdef CONFIG_CPU_LITTLE_ENDIAN
Aurelien Jarnob259e512015-09-05 18:46:57 +0200194# if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
Markos Chandras266a88e2015-06-04 11:56:16 +0100195 jr $r_ra
196 wsbh $r_A, $r_s0
Aurelien Jarnob259e512015-09-05 18:46:57 +0200197# else
198 sll t0, $r_s0, 8
199 andi t1, $r_s0, 0xff00
200 andi t0, t0, 0xff00
201 srl t1, t1, 8
202 jr $r_ra
203 or $r_A, t0, t1
204# endif
Aurelien Jarnofaa97242015-09-05 18:46:56 +0200205#else
Markos Chandras266a88e2015-06-04 11:56:16 +0100206 jr $r_ra
207 move $r_A, $r_s0
Aurelien Jarnofaa97242015-09-05 18:46:56 +0200208#endif
Markos Chandras266a88e2015-06-04 11:56:16 +0100209
210 END(bpf_slow_path_half)
211
212NESTED(bpf_slow_path_byte, (6 * SZREG), $r_sp)
213 bpf_slow_path_common(1)
214 jr $r_ra
215 move $r_A, $r_s0
216
217 END(bpf_slow_path_byte)
218
219/*
220 * Negative entry points
221 */
222 .macro bpf_is_end_of_data
223 li t0, SKF_LL_OFF
224 /* Reading link layer data? */
225 slt t1, offset, t0
226 bgtz t1, fault
227 /* Be careful what follows in DS. */
228 .endm
229/*
230 * call skb_copy_bits:
231 * (prototype in linux/filter.h)
232 *
233 * void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
234 * int k, unsigned int size)
235 *
236 * see above (bpf_slow_path_common) for ABI restrictions
237 */
238#define bpf_negative_common(SIZE) \
239 PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
240 PTR_LA t0, bpf_internal_load_pointer_neg_helper; \
241 PTR_S $r_ra, (5 * SZREG)($r_sp); \
242 jalr t0; \
243 li a2, SIZE; \
244 PTR_L $r_ra, (5 * SZREG)($r_sp); \
245 /* Check return pointer */ \
246 beqz v0, fault; \
247 PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
248 /* Preserve our pointer */ \
249 move $r_s0, v0; \
250 /* Set return value */ \
251 move $r_ret, zero; \
252
253bpf_slow_path_word_neg:
254 bpf_is_end_of_data
255NESTED(sk_load_word_negative, (6 * SZREG), $r_sp)
256 bpf_negative_common(4)
257 jr $r_ra
258 lw $r_A, 0($r_s0)
259 END(sk_load_word_negative)
260
261bpf_slow_path_half_neg:
262 bpf_is_end_of_data
263NESTED(sk_load_half_negative, (6 * SZREG), $r_sp)
264 bpf_negative_common(2)
265 jr $r_ra
266 lhu $r_A, 0($r_s0)
267 END(sk_load_half_negative)
268
269bpf_slow_path_byte_neg:
270 bpf_is_end_of_data
271NESTED(sk_load_byte_negative, (6 * SZREG), $r_sp)
272 bpf_negative_common(1)
273 jr $r_ra
274 lbu $r_A, 0($r_s0)
275 END(sk_load_byte_negative)
276
277fault:
278 jr $r_ra
279 addiu $r_ret, zero, 1