blob: 591f9db3bf4048a4d727f195e4e2fb95e4ca06c4 [file] [log] [blame]
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001/*
2 * Just-In-Time compiler for BPF filters on 32bit ARM
3 *
4 * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License.
9 */
10
11#include <linux/bitops.h>
12#include <linux/compiler.h>
13#include <linux/errno.h>
14#include <linux/filter.h>
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010015#include <linux/netdevice.h>
16#include <linux/string.h>
17#include <linux/slab.h>
Daniel Borkmannbf0098f2012-11-07 15:31:02 +000018#include <linux/if_vlan.h>
Daniel Borkmanne8b56d52014-09-19 14:56:57 +020019
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010020#include <asm/cacheflush.h>
21#include <asm/hwcap.h>
Ben Dooks34607432013-07-24 15:44:56 +010022#include <asm/opcodes.h>
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010023
24#include "bpf_jit_32.h"
25
26/*
27 * ABI:
28 *
29 * r0 scratch register
30 * r4 BPF register A
31 * r5 BPF register X
32 * r6 pointer to the skb
33 * r7 skb->data
34 * r8 skb_headlen(skb)
35 */
36
37#define r_scratch ARM_R0
38/* r1-r3 are (also) used for the unaligned loads on the non-ARMv7 slowpath */
39#define r_off ARM_R1
40#define r_A ARM_R4
41#define r_X ARM_R5
42#define r_skb ARM_R6
43#define r_skb_data ARM_R7
44#define r_skb_hl ARM_R8
45
46#define SCRATCH_SP_OFFSET 0
Schichan Nicolasfe15f3f2012-12-10 14:49:40 +010047#define SCRATCH_OFF(k) (SCRATCH_SP_OFFSET + 4 * (k))
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010048
49#define SEEN_MEM ((1 << BPF_MEMWORDS) - 1)
50#define SEEN_MEM_WORD(k) (1 << (k))
51#define SEEN_X (1 << BPF_MEMWORDS)
52#define SEEN_CALL (1 << (BPF_MEMWORDS + 1))
53#define SEEN_SKB (1 << (BPF_MEMWORDS + 2))
54#define SEEN_DATA (1 << (BPF_MEMWORDS + 3))
55
56#define FLAG_NEED_X_RESET (1 << 0)
Nicolas Schichan0b59d882015-05-07 17:14:21 +020057#define FLAG_IMM_OVERFLOW (1 << 1)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010058
59struct jit_ctx {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070060 const struct bpf_prog *skf;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010061 unsigned idx;
62 unsigned prologue_bytes;
63 int ret0_fp_idx;
64 u32 seen;
65 u32 flags;
66 u32 *offsets;
67 u32 *target;
68#if __LINUX_ARM_ARCH__ < 7
69 u16 epilogue_bytes;
70 u16 imm_count;
71 u32 *imms;
72#endif
73};
74
75int bpf_jit_enable __read_mostly;
76
Nicolas Schichan6d715e32015-07-21 14:14:13 +020077static inline int call_neg_helper(struct sk_buff *skb, int offset, void *ret,
78 unsigned int size)
79{
80 void *ptr = bpf_internal_load_pointer_neg_helper(skb, offset, size);
81
82 if (!ptr)
83 return -EFAULT;
84 memcpy(ret, ptr, size);
85 return 0;
86}
87
88static u64 jit_get_skb_b(struct sk_buff *skb, int offset)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010089{
90 u8 ret;
91 int err;
92
Nicolas Schichan6d715e32015-07-21 14:14:13 +020093 if (offset < 0)
94 err = call_neg_helper(skb, offset, &ret, 1);
95 else
96 err = skb_copy_bits(skb, offset, &ret, 1);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010097
98 return (u64)err << 32 | ret;
99}
100
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200101static u64 jit_get_skb_h(struct sk_buff *skb, int offset)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100102{
103 u16 ret;
104 int err;
105
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200106 if (offset < 0)
107 err = call_neg_helper(skb, offset, &ret, 2);
108 else
109 err = skb_copy_bits(skb, offset, &ret, 2);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100110
111 return (u64)err << 32 | ntohs(ret);
112}
113
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200114static u64 jit_get_skb_w(struct sk_buff *skb, int offset)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100115{
116 u32 ret;
117 int err;
118
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200119 if (offset < 0)
120 err = call_neg_helper(skb, offset, &ret, 4);
121 else
122 err = skb_copy_bits(skb, offset, &ret, 4);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100123
124 return (u64)err << 32 | ntohl(ret);
125}
126
127/*
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200128 * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100129 * (where the assembly routines like __aeabi_uidiv could cause problems).
130 */
131static u32 jit_udiv(u32 dividend, u32 divisor)
132{
133 return dividend / divisor;
134}
135
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200136static u32 jit_mod(u32 dividend, u32 divisor)
137{
138 return dividend % divisor;
139}
140
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100141static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
142{
Ben Dooks34607432013-07-24 15:44:56 +0100143 inst |= (cond << 28);
144 inst = __opcode_to_mem_arm(inst);
145
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100146 if (ctx->target != NULL)
Ben Dooks34607432013-07-24 15:44:56 +0100147 ctx->target[ctx->idx] = inst;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100148
149 ctx->idx++;
150}
151
152/*
153 * Emit an instruction that will be executed unconditionally.
154 */
155static inline void emit(u32 inst, struct jit_ctx *ctx)
156{
157 _emit(ARM_COND_AL, inst, ctx);
158}
159
160static u16 saved_regs(struct jit_ctx *ctx)
161{
162 u16 ret = 0;
163
164 if ((ctx->skf->len > 1) ||
Daniel Borkmann34805932014-05-29 10:22:50 +0200165 (ctx->skf->insns[0].code == (BPF_RET | BPF_A)))
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100166 ret |= 1 << r_A;
167
168#ifdef CONFIG_FRAME_POINTER
169 ret |= (1 << ARM_FP) | (1 << ARM_IP) | (1 << ARM_LR) | (1 << ARM_PC);
170#else
171 if (ctx->seen & SEEN_CALL)
172 ret |= 1 << ARM_LR;
173#endif
174 if (ctx->seen & (SEEN_DATA | SEEN_SKB))
175 ret |= 1 << r_skb;
176 if (ctx->seen & SEEN_DATA)
177 ret |= (1 << r_skb_data) | (1 << r_skb_hl);
178 if (ctx->seen & SEEN_X)
179 ret |= 1 << r_X;
180
181 return ret;
182}
183
184static inline int mem_words_used(struct jit_ctx *ctx)
185{
186 /* yes, we do waste some stack space IF there are "holes" in the set" */
187 return fls(ctx->seen & SEEN_MEM);
188}
189
190static inline bool is_load_to_a(u16 inst)
191{
192 switch (inst) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200193 case BPF_LD | BPF_W | BPF_LEN:
194 case BPF_LD | BPF_W | BPF_ABS:
195 case BPF_LD | BPF_H | BPF_ABS:
196 case BPF_LD | BPF_B | BPF_ABS:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100197 return true;
198 default:
199 return false;
200 }
201}
202
Daniel Borkmann55309dd2014-09-08 08:04:48 +0200203static void jit_fill_hole(void *area, unsigned int size)
204{
Daniel Borkmanne8b56d52014-09-19 14:56:57 +0200205 u32 *ptr;
Daniel Borkmann55309dd2014-09-08 08:04:48 +0200206 /* We are guaranteed to have aligned memory. */
207 for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
Daniel Borkmanne8b56d52014-09-19 14:56:57 +0200208 *ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
Daniel Borkmann55309dd2014-09-08 08:04:48 +0200209}
210
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100211static void build_prologue(struct jit_ctx *ctx)
212{
213 u16 reg_set = saved_regs(ctx);
214 u16 first_inst = ctx->skf->insns[0].code;
215 u16 off;
216
217#ifdef CONFIG_FRAME_POINTER
218 emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
219 emit(ARM_PUSH(reg_set), ctx);
220 emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
221#else
222 if (reg_set)
223 emit(ARM_PUSH(reg_set), ctx);
224#endif
225
226 if (ctx->seen & (SEEN_DATA | SEEN_SKB))
227 emit(ARM_MOV_R(r_skb, ARM_R0), ctx);
228
229 if (ctx->seen & SEEN_DATA) {
230 off = offsetof(struct sk_buff, data);
231 emit(ARM_LDR_I(r_skb_data, r_skb, off), ctx);
232 /* headlen = len - data_len */
233 off = offsetof(struct sk_buff, len);
234 emit(ARM_LDR_I(r_skb_hl, r_skb, off), ctx);
235 off = offsetof(struct sk_buff, data_len);
236 emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
237 emit(ARM_SUB_R(r_skb_hl, r_skb_hl, r_scratch), ctx);
238 }
239
240 if (ctx->flags & FLAG_NEED_X_RESET)
241 emit(ARM_MOV_I(r_X, 0), ctx);
242
243 /* do not leak kernel data to userspace */
Daniel Borkmann34805932014-05-29 10:22:50 +0200244 if ((first_inst != (BPF_RET | BPF_K)) && !(is_load_to_a(first_inst)))
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100245 emit(ARM_MOV_I(r_A, 0), ctx);
246
247 /* stack space for the BPF_MEM words */
248 if (ctx->seen & SEEN_MEM)
249 emit(ARM_SUB_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
250}
251
252static void build_epilogue(struct jit_ctx *ctx)
253{
254 u16 reg_set = saved_regs(ctx);
255
256 if (ctx->seen & SEEN_MEM)
257 emit(ARM_ADD_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
258
259 reg_set &= ~(1 << ARM_LR);
260
261#ifdef CONFIG_FRAME_POINTER
262 /* the first instruction of the prologue was: mov ip, sp */
263 reg_set &= ~(1 << ARM_IP);
264 reg_set |= (1 << ARM_SP);
265 emit(ARM_LDM(ARM_SP, reg_set), ctx);
266#else
267 if (reg_set) {
268 if (ctx->seen & SEEN_CALL)
269 reg_set |= 1 << ARM_PC;
270 emit(ARM_POP(reg_set), ctx);
271 }
272
273 if (!(ctx->seen & SEEN_CALL))
274 emit(ARM_BX(ARM_LR), ctx);
275#endif
276}
277
278static int16_t imm8m(u32 x)
279{
280 u32 rot;
281
282 for (rot = 0; rot < 16; rot++)
283 if ((x & ~ror32(0xff, 2 * rot)) == 0)
284 return rol32(x, 2 * rot) | (rot << 8);
285
286 return -1;
287}
288
289#if __LINUX_ARM_ARCH__ < 7
290
291static u16 imm_offset(u32 k, struct jit_ctx *ctx)
292{
293 unsigned i = 0, offset;
294 u16 imm;
295
296 /* on the "fake" run we just count them (duplicates included) */
297 if (ctx->target == NULL) {
298 ctx->imm_count++;
299 return 0;
300 }
301
302 while ((i < ctx->imm_count) && ctx->imms[i]) {
303 if (ctx->imms[i] == k)
304 break;
305 i++;
306 }
307
308 if (ctx->imms[i] == 0)
309 ctx->imms[i] = k;
310
311 /* constants go just after the epilogue */
312 offset = ctx->offsets[ctx->skf->len];
313 offset += ctx->prologue_bytes;
314 offset += ctx->epilogue_bytes;
315 offset += i * 4;
316
317 ctx->target[offset / 4] = k;
318
319 /* PC in ARM mode == address of the instruction + 8 */
320 imm = offset - (8 + ctx->idx * 4);
321
Nicolas Schichan0b59d882015-05-07 17:14:21 +0200322 if (imm & ~0xfff) {
323 /*
324 * literal pool is too far, signal it into flags. we
325 * can only detect it on the second pass unfortunately.
326 */
327 ctx->flags |= FLAG_IMM_OVERFLOW;
328 return 0;
329 }
330
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100331 return imm;
332}
333
334#endif /* __LINUX_ARM_ARCH__ */
335
336/*
337 * Move an immediate that's not an imm8m to a core register.
338 */
339static inline void emit_mov_i_no8m(int rd, u32 val, struct jit_ctx *ctx)
340{
341#if __LINUX_ARM_ARCH__ < 7
342 emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
343#else
344 emit(ARM_MOVW(rd, val & 0xffff), ctx);
345 if (val > 0xffff)
346 emit(ARM_MOVT(rd, val >> 16), ctx);
347#endif
348}
349
350static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx)
351{
352 int imm12 = imm8m(val);
353
354 if (imm12 >= 0)
355 emit(ARM_MOV_I(rd, imm12), ctx);
356 else
357 emit_mov_i_no8m(rd, val, ctx);
358}
359
360#if __LINUX_ARM_ARCH__ < 6
361
362static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
363{
364 _emit(cond, ARM_LDRB_I(ARM_R3, r_addr, 1), ctx);
365 _emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
366 _emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 3), ctx);
367 _emit(cond, ARM_LSL_I(ARM_R3, ARM_R3, 16), ctx);
368 _emit(cond, ARM_LDRB_I(ARM_R0, r_addr, 2), ctx);
369 _emit(cond, ARM_ORR_S(ARM_R3, ARM_R3, ARM_R1, SRTYPE_LSL, 24), ctx);
370 _emit(cond, ARM_ORR_R(ARM_R3, ARM_R3, ARM_R2), ctx);
371 _emit(cond, ARM_ORR_S(r_res, ARM_R3, ARM_R0, SRTYPE_LSL, 8), ctx);
372}
373
374static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
375{
376 _emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
377 _emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 1), ctx);
378 _emit(cond, ARM_ORR_S(r_res, ARM_R2, ARM_R1, SRTYPE_LSL, 8), ctx);
379}
380
381static inline void emit_swap16(u8 r_dst, u8 r_src, struct jit_ctx *ctx)
382{
Nicolas Schichan462738f2013-02-13 17:30:39 +0000383 /* r_dst = (r_src << 8) | (r_src >> 8) */
384 emit(ARM_LSL_I(ARM_R1, r_src, 8), ctx);
385 emit(ARM_ORR_S(r_dst, ARM_R1, r_src, SRTYPE_LSR, 8), ctx);
386
387 /*
388 * we need to mask out the bits set in r_dst[23:16] due to
389 * the first shift instruction.
390 *
391 * note that 0x8ff is the encoded immediate 0x00ff0000.
392 */
393 emit(ARM_BIC_I(r_dst, r_dst, 0x8ff), ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100394}
395
396#else /* ARMv6+ */
397
398static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
399{
400 _emit(cond, ARM_LDR_I(r_res, r_addr, 0), ctx);
401#ifdef __LITTLE_ENDIAN
402 _emit(cond, ARM_REV(r_res, r_res), ctx);
403#endif
404}
405
406static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
407{
408 _emit(cond, ARM_LDRH_I(r_res, r_addr, 0), ctx);
409#ifdef __LITTLE_ENDIAN
410 _emit(cond, ARM_REV16(r_res, r_res), ctx);
411#endif
412}
413
414static inline void emit_swap16(u8 r_dst __maybe_unused,
415 u8 r_src __maybe_unused,
416 struct jit_ctx *ctx __maybe_unused)
417{
418#ifdef __LITTLE_ENDIAN
419 emit(ARM_REV16(r_dst, r_src), ctx);
420#endif
421}
422
423#endif /* __LINUX_ARM_ARCH__ < 6 */
424
425
426/* Compute the immediate value for a PC-relative branch. */
427static inline u32 b_imm(unsigned tgt, struct jit_ctx *ctx)
428{
429 u32 imm;
430
431 if (ctx->target == NULL)
432 return 0;
433 /*
434 * BPF allows only forward jumps and the offset of the target is
435 * still the one computed during the first pass.
436 */
437 imm = ctx->offsets[tgt] + ctx->prologue_bytes - (ctx->idx * 4 + 8);
438
439 return imm >> 2;
440}
441
442#define OP_IMM3(op, r1, r2, imm_val, ctx) \
443 do { \
444 imm12 = imm8m(imm_val); \
445 if (imm12 < 0) { \
446 emit_mov_i_no8m(r_scratch, imm_val, ctx); \
447 emit(op ## _R((r1), (r2), r_scratch), ctx); \
448 } else { \
449 emit(op ## _I((r1), (r2), imm12), ctx); \
450 } \
451 } while (0)
452
453static inline void emit_err_ret(u8 cond, struct jit_ctx *ctx)
454{
455 if (ctx->ret0_fp_idx >= 0) {
456 _emit(cond, ARM_B(b_imm(ctx->ret0_fp_idx, ctx)), ctx);
457 /* NOP to keep the size constant between passes */
458 emit(ARM_MOV_R(ARM_R0, ARM_R0), ctx);
459 } else {
460 _emit(cond, ARM_MOV_I(ARM_R0, 0), ctx);
461 _emit(cond, ARM_B(b_imm(ctx->skf->len, ctx)), ctx);
462 }
463}
464
465static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
466{
467#if __LINUX_ARM_ARCH__ < 5
468 emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
469
470 if (elf_hwcap & HWCAP_THUMB)
471 emit(ARM_BX(tgt_reg), ctx);
472 else
473 emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
474#else
475 emit(ARM_BLX_R(tgt_reg), ctx);
476#endif
477}
478
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200479static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx,
480 int bpf_op)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100481{
482#if __LINUX_ARM_ARCH__ == 7
483 if (elf_hwcap & HWCAP_IDIVA) {
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200484 if (bpf_op == BPF_DIV)
485 emit(ARM_UDIV(rd, rm, rn), ctx);
486 else {
487 emit(ARM_UDIV(ARM_R3, rm, rn), ctx);
488 emit(ARM_MLS(rd, rn, ARM_R3, rm), ctx);
489 }
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100490 return;
491 }
492#endif
Nicolas Schichan19fc99d2015-05-06 18:31:56 +0200493
494 /*
495 * For BPF_ALU | BPF_DIV | BPF_K instructions, rm is ARM_R4
496 * (r_A) and rn is ARM_R0 (r_scratch) so load rn first into
497 * ARM_R1 to avoid accidentally overwriting ARM_R0 with rm
498 * before using it as a source for ARM_R1.
499 *
500 * For BPF_ALU | BPF_DIV | BPF_X rm is ARM_R4 (r_A) and rn is
501 * ARM_R5 (r_X) so there is no particular register overlap
502 * issues.
503 */
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100504 if (rn != ARM_R1)
505 emit(ARM_MOV_R(ARM_R1, rn), ctx);
Nicolas Schichan19fc99d2015-05-06 18:31:56 +0200506 if (rm != ARM_R0)
507 emit(ARM_MOV_R(ARM_R0, rm), ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100508
509 ctx->seen |= SEEN_CALL;
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200510 emit_mov_i(ARM_R3, bpf_op == BPF_DIV ? (u32)jit_udiv : (u32)jit_mod,
511 ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100512 emit_blx_r(ARM_R3, ctx);
513
514 if (rd != ARM_R0)
515 emit(ARM_MOV_R(rd, ARM_R0), ctx);
516}
517
518static inline void update_on_xread(struct jit_ctx *ctx)
519{
520 if (!(ctx->seen & SEEN_X))
521 ctx->flags |= FLAG_NEED_X_RESET;
522
523 ctx->seen |= SEEN_X;
524}
525
526static int build_body(struct jit_ctx *ctx)
527{
528 void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700529 const struct bpf_prog *prog = ctx->skf;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100530 const struct sock_filter *inst;
531 unsigned i, load_order, off, condt;
532 int imm12;
533 u32 k;
534
535 for (i = 0; i < prog->len; i++) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200536 u16 code;
537
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100538 inst = &(prog->insns[i]);
539 /* K as an immediate value operand */
540 k = inst->k;
Daniel Borkmann34805932014-05-29 10:22:50 +0200541 code = bpf_anc_helper(inst);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100542
543 /* compute offsets only in the fake pass */
544 if (ctx->target == NULL)
545 ctx->offsets[i] = ctx->idx * 4;
546
Daniel Borkmann34805932014-05-29 10:22:50 +0200547 switch (code) {
548 case BPF_LD | BPF_IMM:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100549 emit_mov_i(r_A, k, ctx);
550 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200551 case BPF_LD | BPF_W | BPF_LEN:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100552 ctx->seen |= SEEN_SKB;
553 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
554 emit(ARM_LDR_I(r_A, r_skb,
555 offsetof(struct sk_buff, len)), ctx);
556 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200557 case BPF_LD | BPF_MEM:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100558 /* A = scratch[k] */
559 ctx->seen |= SEEN_MEM_WORD(k);
560 emit(ARM_LDR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
561 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200562 case BPF_LD | BPF_W | BPF_ABS:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100563 load_order = 2;
564 goto load;
Daniel Borkmann34805932014-05-29 10:22:50 +0200565 case BPF_LD | BPF_H | BPF_ABS:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100566 load_order = 1;
567 goto load;
Daniel Borkmann34805932014-05-29 10:22:50 +0200568 case BPF_LD | BPF_B | BPF_ABS:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100569 load_order = 0;
570load:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100571 emit_mov_i(r_off, k, ctx);
572load_common:
573 ctx->seen |= SEEN_DATA | SEEN_CALL;
574
575 if (load_order > 0) {
576 emit(ARM_SUB_I(r_scratch, r_skb_hl,
577 1 << load_order), ctx);
578 emit(ARM_CMP_R(r_scratch, r_off), ctx);
Nicolas Schichan7aed35c2015-07-21 14:14:12 +0200579 condt = ARM_COND_GE;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100580 } else {
581 emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
582 condt = ARM_COND_HI;
583 }
584
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200585 /*
586 * test for negative offset, only if we are
587 * currently scheduled to take the fast
588 * path. this will update the flags so that
589 * the slowpath instruction are ignored if the
590 * offset is negative.
591 *
592 * for loard_order == 0 the HI condition will
593 * make loads at offset 0 take the slow path too.
594 */
595 _emit(condt, ARM_CMP_I(r_off, 0), ctx);
596
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100597 _emit(condt, ARM_ADD_R(r_scratch, r_off, r_skb_data),
598 ctx);
599
600 if (load_order == 0)
601 _emit(condt, ARM_LDRB_I(r_A, r_scratch, 0),
602 ctx);
603 else if (load_order == 1)
604 emit_load_be16(condt, r_A, r_scratch, ctx);
605 else if (load_order == 2)
606 emit_load_be32(condt, r_A, r_scratch, ctx);
607
608 _emit(condt, ARM_B(b_imm(i + 1, ctx)), ctx);
609
610 /* the slowpath */
611 emit_mov_i(ARM_R3, (u32)load_func[load_order], ctx);
612 emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
613 /* the offset is already in R1 */
614 emit_blx_r(ARM_R3, ctx);
615 /* check the result of skb_copy_bits */
616 emit(ARM_CMP_I(ARM_R1, 0), ctx);
617 emit_err_ret(ARM_COND_NE, ctx);
618 emit(ARM_MOV_R(r_A, ARM_R0), ctx);
619 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200620 case BPF_LD | BPF_W | BPF_IND:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100621 load_order = 2;
622 goto load_ind;
Daniel Borkmann34805932014-05-29 10:22:50 +0200623 case BPF_LD | BPF_H | BPF_IND:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100624 load_order = 1;
625 goto load_ind;
Daniel Borkmann34805932014-05-29 10:22:50 +0200626 case BPF_LD | BPF_B | BPF_IND:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100627 load_order = 0;
628load_ind:
Nicolas Schichan8690f472015-10-02 15:39:12 +0200629 update_on_xread(ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100630 OP_IMM3(ARM_ADD, r_off, r_X, k, ctx);
631 goto load_common;
Daniel Borkmann34805932014-05-29 10:22:50 +0200632 case BPF_LDX | BPF_IMM:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100633 ctx->seen |= SEEN_X;
634 emit_mov_i(r_X, k, ctx);
635 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200636 case BPF_LDX | BPF_W | BPF_LEN:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100637 ctx->seen |= SEEN_X | SEEN_SKB;
638 emit(ARM_LDR_I(r_X, r_skb,
639 offsetof(struct sk_buff, len)), ctx);
640 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200641 case BPF_LDX | BPF_MEM:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100642 ctx->seen |= SEEN_X | SEEN_MEM_WORD(k);
643 emit(ARM_LDR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
644 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200645 case BPF_LDX | BPF_B | BPF_MSH:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100646 /* x = ((*(frame + k)) & 0xf) << 2; */
647 ctx->seen |= SEEN_X | SEEN_DATA | SEEN_CALL;
648 /* the interpreter should deal with the negative K */
Chen Gang45549a62013-03-10 10:15:54 +0800649 if ((int)k < 0)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100650 return -1;
651 /* offset in r1: we might have to take the slow path */
652 emit_mov_i(r_off, k, ctx);
653 emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
654
655 /* load in r0: common with the slowpath */
656 _emit(ARM_COND_HI, ARM_LDRB_R(ARM_R0, r_skb_data,
657 ARM_R1), ctx);
658 /*
659 * emit_mov_i() might generate one or two instructions,
660 * the same holds for emit_blx_r()
661 */
662 _emit(ARM_COND_HI, ARM_B(b_imm(i + 1, ctx) - 2), ctx);
663
664 emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
665 /* r_off is r1 */
666 emit_mov_i(ARM_R3, (u32)jit_get_skb_b, ctx);
667 emit_blx_r(ARM_R3, ctx);
668 /* check the return value of skb_copy_bits */
669 emit(ARM_CMP_I(ARM_R1, 0), ctx);
670 emit_err_ret(ARM_COND_NE, ctx);
671
672 emit(ARM_AND_I(r_X, ARM_R0, 0x00f), ctx);
673 emit(ARM_LSL_I(r_X, r_X, 2), ctx);
674 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200675 case BPF_ST:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100676 ctx->seen |= SEEN_MEM_WORD(k);
677 emit(ARM_STR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
678 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200679 case BPF_STX:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100680 update_on_xread(ctx);
681 ctx->seen |= SEEN_MEM_WORD(k);
682 emit(ARM_STR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
683 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200684 case BPF_ALU | BPF_ADD | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100685 /* A += K */
686 OP_IMM3(ARM_ADD, r_A, r_A, k, ctx);
687 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200688 case BPF_ALU | BPF_ADD | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100689 update_on_xread(ctx);
690 emit(ARM_ADD_R(r_A, r_A, r_X), ctx);
691 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200692 case BPF_ALU | BPF_SUB | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100693 /* A -= K */
694 OP_IMM3(ARM_SUB, r_A, r_A, k, ctx);
695 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200696 case BPF_ALU | BPF_SUB | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100697 update_on_xread(ctx);
698 emit(ARM_SUB_R(r_A, r_A, r_X), ctx);
699 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200700 case BPF_ALU | BPF_MUL | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100701 /* A *= K */
702 emit_mov_i(r_scratch, k, ctx);
703 emit(ARM_MUL(r_A, r_A, r_scratch), ctx);
704 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200705 case BPF_ALU | BPF_MUL | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100706 update_on_xread(ctx);
707 emit(ARM_MUL(r_A, r_A, r_X), ctx);
708 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200709 case BPF_ALU | BPF_DIV | BPF_K:
Eric Dumazetaee636c2014-01-15 06:50:07 -0800710 if (k == 1)
711 break;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100712 emit_mov_i(r_scratch, k, ctx);
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200713 emit_udivmod(r_A, r_A, r_scratch, ctx, BPF_DIV);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100714 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200715 case BPF_ALU | BPF_DIV | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100716 update_on_xread(ctx);
717 emit(ARM_CMP_I(r_X, 0), ctx);
718 emit_err_ret(ARM_COND_EQ, ctx);
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200719 emit_udivmod(r_A, r_A, r_X, ctx, BPF_DIV);
720 break;
721 case BPF_ALU | BPF_MOD | BPF_K:
722 if (k == 1) {
723 emit_mov_i(r_A, 0, ctx);
724 break;
725 }
726 emit_mov_i(r_scratch, k, ctx);
727 emit_udivmod(r_A, r_A, r_scratch, ctx, BPF_MOD);
728 break;
729 case BPF_ALU | BPF_MOD | BPF_X:
730 update_on_xread(ctx);
731 emit(ARM_CMP_I(r_X, 0), ctx);
732 emit_err_ret(ARM_COND_EQ, ctx);
733 emit_udivmod(r_A, r_A, r_X, ctx, BPF_MOD);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100734 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200735 case BPF_ALU | BPF_OR | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100736 /* A |= K */
737 OP_IMM3(ARM_ORR, r_A, r_A, k, ctx);
738 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200739 case BPF_ALU | BPF_OR | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100740 update_on_xread(ctx);
741 emit(ARM_ORR_R(r_A, r_A, r_X), ctx);
742 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200743 case BPF_ALU | BPF_XOR | BPF_K:
Daniel Borkmann3cbe2042012-11-07 15:28:28 +0000744 /* A ^= K; */
745 OP_IMM3(ARM_EOR, r_A, r_A, k, ctx);
746 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200747 case BPF_ANC | SKF_AD_ALU_XOR_X:
748 case BPF_ALU | BPF_XOR | BPF_X:
Daniel Borkmann3cbe2042012-11-07 15:28:28 +0000749 /* A ^= X */
750 update_on_xread(ctx);
751 emit(ARM_EOR_R(r_A, r_A, r_X), ctx);
752 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200753 case BPF_ALU | BPF_AND | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100754 /* A &= K */
755 OP_IMM3(ARM_AND, r_A, r_A, k, ctx);
756 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200757 case BPF_ALU | BPF_AND | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100758 update_on_xread(ctx);
759 emit(ARM_AND_R(r_A, r_A, r_X), ctx);
760 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200761 case BPF_ALU | BPF_LSH | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100762 if (unlikely(k > 31))
763 return -1;
764 emit(ARM_LSL_I(r_A, r_A, k), ctx);
765 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200766 case BPF_ALU | BPF_LSH | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100767 update_on_xread(ctx);
768 emit(ARM_LSL_R(r_A, r_A, r_X), ctx);
769 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200770 case BPF_ALU | BPF_RSH | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100771 if (unlikely(k > 31))
772 return -1;
773 emit(ARM_LSR_I(r_A, r_A, k), ctx);
774 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200775 case BPF_ALU | BPF_RSH | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100776 update_on_xread(ctx);
777 emit(ARM_LSR_R(r_A, r_A, r_X), ctx);
778 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200779 case BPF_ALU | BPF_NEG:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100780 /* A = -A */
781 emit(ARM_RSB_I(r_A, r_A, 0), ctx);
782 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200783 case BPF_JMP | BPF_JA:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100784 /* pc += K */
785 emit(ARM_B(b_imm(i + k + 1, ctx)), ctx);
786 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200787 case BPF_JMP | BPF_JEQ | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100788 /* pc += (A == K) ? pc->jt : pc->jf */
789 condt = ARM_COND_EQ;
790 goto cmp_imm;
Daniel Borkmann34805932014-05-29 10:22:50 +0200791 case BPF_JMP | BPF_JGT | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100792 /* pc += (A > K) ? pc->jt : pc->jf */
793 condt = ARM_COND_HI;
794 goto cmp_imm;
Daniel Borkmann34805932014-05-29 10:22:50 +0200795 case BPF_JMP | BPF_JGE | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100796 /* pc += (A >= K) ? pc->jt : pc->jf */
797 condt = ARM_COND_HS;
798cmp_imm:
799 imm12 = imm8m(k);
800 if (imm12 < 0) {
801 emit_mov_i_no8m(r_scratch, k, ctx);
802 emit(ARM_CMP_R(r_A, r_scratch), ctx);
803 } else {
804 emit(ARM_CMP_I(r_A, imm12), ctx);
805 }
806cond_jump:
807 if (inst->jt)
808 _emit(condt, ARM_B(b_imm(i + inst->jt + 1,
809 ctx)), ctx);
810 if (inst->jf)
811 _emit(condt ^ 1, ARM_B(b_imm(i + inst->jf + 1,
812 ctx)), ctx);
813 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200814 case BPF_JMP | BPF_JEQ | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100815 /* pc += (A == X) ? pc->jt : pc->jf */
816 condt = ARM_COND_EQ;
817 goto cmp_x;
Daniel Borkmann34805932014-05-29 10:22:50 +0200818 case BPF_JMP | BPF_JGT | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100819 /* pc += (A > X) ? pc->jt : pc->jf */
820 condt = ARM_COND_HI;
821 goto cmp_x;
Daniel Borkmann34805932014-05-29 10:22:50 +0200822 case BPF_JMP | BPF_JGE | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100823 /* pc += (A >= X) ? pc->jt : pc->jf */
824 condt = ARM_COND_CS;
825cmp_x:
826 update_on_xread(ctx);
827 emit(ARM_CMP_R(r_A, r_X), ctx);
828 goto cond_jump;
Daniel Borkmann34805932014-05-29 10:22:50 +0200829 case BPF_JMP | BPF_JSET | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100830 /* pc += (A & K) ? pc->jt : pc->jf */
831 condt = ARM_COND_NE;
832 /* not set iff all zeroes iff Z==1 iff EQ */
833
834 imm12 = imm8m(k);
835 if (imm12 < 0) {
836 emit_mov_i_no8m(r_scratch, k, ctx);
837 emit(ARM_TST_R(r_A, r_scratch), ctx);
838 } else {
839 emit(ARM_TST_I(r_A, imm12), ctx);
840 }
841 goto cond_jump;
Daniel Borkmann34805932014-05-29 10:22:50 +0200842 case BPF_JMP | BPF_JSET | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100843 /* pc += (A & X) ? pc->jt : pc->jf */
844 update_on_xread(ctx);
845 condt = ARM_COND_NE;
846 emit(ARM_TST_R(r_A, r_X), ctx);
847 goto cond_jump;
Daniel Borkmann34805932014-05-29 10:22:50 +0200848 case BPF_RET | BPF_A:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100849 emit(ARM_MOV_R(ARM_R0, r_A), ctx);
850 goto b_epilogue;
Daniel Borkmann34805932014-05-29 10:22:50 +0200851 case BPF_RET | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100852 if ((k == 0) && (ctx->ret0_fp_idx < 0))
853 ctx->ret0_fp_idx = i;
854 emit_mov_i(ARM_R0, k, ctx);
855b_epilogue:
856 if (i != ctx->skf->len - 1)
857 emit(ARM_B(b_imm(prog->len, ctx)), ctx);
858 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200859 case BPF_MISC | BPF_TAX:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100860 /* X = A */
861 ctx->seen |= SEEN_X;
862 emit(ARM_MOV_R(r_X, r_A), ctx);
863 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200864 case BPF_MISC | BPF_TXA:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100865 /* A = X */
866 update_on_xread(ctx);
867 emit(ARM_MOV_R(r_A, r_X), ctx);
868 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200869 case BPF_ANC | SKF_AD_PROTOCOL:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100870 /* A = ntohs(skb->protocol) */
871 ctx->seen |= SEEN_SKB;
872 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
873 protocol) != 2);
874 off = offsetof(struct sk_buff, protocol);
875 emit(ARM_LDRH_I(r_scratch, r_skb, off), ctx);
876 emit_swap16(r_A, r_scratch, ctx);
877 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200878 case BPF_ANC | SKF_AD_CPU:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100879 /* r_scratch = current_thread_info() */
880 OP_IMM3(ARM_BIC, r_scratch, ARM_SP, THREAD_SIZE - 1, ctx);
881 /* A = current_thread_info()->cpu */
882 BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4);
883 off = offsetof(struct thread_info, cpu);
884 emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
885 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200886 case BPF_ANC | SKF_AD_IFINDEX:
Nicolas Schichan5bf705b2015-07-27 15:06:51 +0200887 case BPF_ANC | SKF_AD_HATYPE:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100888 /* A = skb->dev->ifindex */
Nicolas Schichan5bf705b2015-07-27 15:06:51 +0200889 /* A = skb->dev->type */
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100890 ctx->seen |= SEEN_SKB;
891 off = offsetof(struct sk_buff, dev);
892 emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
893
894 emit(ARM_CMP_I(r_scratch, 0), ctx);
895 emit_err_ret(ARM_COND_EQ, ctx);
896
897 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
898 ifindex) != 4);
Nicolas Schichan5bf705b2015-07-27 15:06:51 +0200899 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
900 type) != 2);
901
902 if (code == (BPF_ANC | SKF_AD_IFINDEX)) {
903 off = offsetof(struct net_device, ifindex);
904 emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
905 } else {
906 /*
907 * offset of field "type" in "struct
908 * net_device" is above what can be
909 * used in the ldrh rd, [rn, #imm]
910 * instruction, so load the offset in
911 * a register and use ldrh rd, [rn, rm]
912 */
913 off = offsetof(struct net_device, type);
914 emit_mov_i(ARM_R3, off, ctx);
915 emit(ARM_LDRH_R(r_A, r_scratch, ARM_R3), ctx);
916 }
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100917 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200918 case BPF_ANC | SKF_AD_MARK:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100919 ctx->seen |= SEEN_SKB;
920 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
921 off = offsetof(struct sk_buff, mark);
922 emit(ARM_LDR_I(r_A, r_skb, off), ctx);
923 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200924 case BPF_ANC | SKF_AD_RXHASH:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100925 ctx->seen |= SEEN_SKB;
Tom Herbert61b905d2014-03-24 15:34:47 -0700926 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
927 off = offsetof(struct sk_buff, hash);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100928 emit(ARM_LDR_I(r_A, r_skb, off), ctx);
929 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200930 case BPF_ANC | SKF_AD_VLAN_TAG:
931 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
Daniel Borkmannbf0098f2012-11-07 15:31:02 +0000932 ctx->seen |= SEEN_SKB;
933 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
934 off = offsetof(struct sk_buff, vlan_tci);
935 emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
Daniel Borkmann34805932014-05-29 10:22:50 +0200936 if (code == (BPF_ANC | SKF_AD_VLAN_TAG))
Nicolas Schichanc18fe542015-07-21 14:14:14 +0200937 OP_IMM3(ARM_AND, r_A, r_A, ~VLAN_TAG_PRESENT, ctx);
938 else {
939 OP_IMM3(ARM_LSR, r_A, r_A, 12, ctx);
940 OP_IMM3(ARM_AND, r_A, r_A, 0x1, ctx);
941 }
Daniel Borkmannbf0098f2012-11-07 15:31:02 +0000942 break;
Nicolas Schichan1447f932015-07-27 15:06:49 +0200943 case BPF_ANC | SKF_AD_PKTTYPE:
944 ctx->seen |= SEEN_SKB;
945 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
946 __pkt_type_offset[0]) != 1);
947 off = PKT_TYPE_OFFSET();
948 emit(ARM_LDRB_I(r_A, r_skb, off), ctx);
949 emit(ARM_AND_I(r_A, r_A, PKT_TYPE_MAX), ctx);
950#ifdef __BIG_ENDIAN_BITFIELD
951 emit(ARM_LSR_I(r_A, r_A, 5), ctx);
952#endif
953 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200954 case BPF_ANC | SKF_AD_QUEUE:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100955 ctx->seen |= SEEN_SKB;
956 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
957 queue_mapping) != 2);
958 BUILD_BUG_ON(offsetof(struct sk_buff,
959 queue_mapping) > 0xff);
960 off = offsetof(struct sk_buff, queue_mapping);
961 emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
962 break;
Nicolas Schichan303249a2015-07-27 15:06:50 +0200963 case BPF_ANC | SKF_AD_PAY_OFFSET:
964 ctx->seen |= SEEN_SKB | SEEN_CALL;
965
966 emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
967 emit_mov_i(ARM_R3, (unsigned int)skb_get_poff, ctx);
968 emit_blx_r(ARM_R3, ctx);
969 emit(ARM_MOV_R(r_A, ARM_R0), ctx);
970 break;
Nicolas Schichan24e737c2015-05-07 15:00:13 +0200971 case BPF_LDX | BPF_W | BPF_ABS:
972 /*
973 * load a 32bit word from struct seccomp_data.
974 * seccomp_check_filter() will already have checked
975 * that k is 32bit aligned and lies within the
976 * struct seccomp_data.
977 */
978 ctx->seen |= SEEN_SKB;
979 emit(ARM_LDR_I(r_A, r_skb, k), ctx);
980 break;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100981 default:
982 return -1;
983 }
Nicolas Schichan0b59d882015-05-07 17:14:21 +0200984
985 if (ctx->flags & FLAG_IMM_OVERFLOW)
986 /*
987 * this instruction generated an overflow when
988 * trying to access the literal pool, so
989 * delegate this filter to the kernel interpreter.
990 */
991 return -1;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100992 }
993
994 /* compute offsets only during the first pass */
995 if (ctx->target == NULL)
996 ctx->offsets[i] = ctx->idx * 4;
997
998 return 0;
999}
1000
1001
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001002void bpf_jit_compile(struct bpf_prog *fp)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001003{
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001004 struct bpf_binary_header *header;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001005 struct jit_ctx ctx;
1006 unsigned tmp_idx;
1007 unsigned alloc_size;
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001008 u8 *target_ptr;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001009
1010 if (!bpf_jit_enable)
1011 return;
1012
1013 memset(&ctx, 0, sizeof(ctx));
1014 ctx.skf = fp;
1015 ctx.ret0_fp_idx = -1;
1016
Schichan Nicolas89c2e002012-12-10 14:49:39 +01001017 ctx.offsets = kzalloc(4 * (ctx.skf->len + 1), GFP_KERNEL);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001018 if (ctx.offsets == NULL)
1019 return;
1020
1021 /* fake pass to fill in the ctx->seen */
1022 if (unlikely(build_body(&ctx)))
1023 goto out;
1024
1025 tmp_idx = ctx.idx;
1026 build_prologue(&ctx);
1027 ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1028
1029#if __LINUX_ARM_ARCH__ < 7
1030 tmp_idx = ctx.idx;
1031 build_epilogue(&ctx);
1032 ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
1033
1034 ctx.idx += ctx.imm_count;
1035 if (ctx.imm_count) {
Schichan Nicolas89c2e002012-12-10 14:49:39 +01001036 ctx.imms = kzalloc(4 * ctx.imm_count, GFP_KERNEL);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001037 if (ctx.imms == NULL)
1038 goto out;
1039 }
1040#else
1041 /* there's nothing after the epilogue on ARMv7 */
1042 build_epilogue(&ctx);
1043#endif
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001044 alloc_size = 4 * ctx.idx;
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001045 header = bpf_jit_binary_alloc(alloc_size, &target_ptr,
1046 4, jit_fill_hole);
1047 if (header == NULL)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001048 goto out;
1049
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001050 ctx.target = (u32 *) target_ptr;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001051 ctx.idx = 0;
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001052
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001053 build_prologue(&ctx);
Nicolas Schichan0b59d882015-05-07 17:14:21 +02001054 if (build_body(&ctx) < 0) {
1055#if __LINUX_ARM_ARCH__ < 7
1056 if (ctx.imm_count)
1057 kfree(ctx.imms);
1058#endif
1059 bpf_jit_binary_free(header);
1060 goto out;
1061 }
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001062 build_epilogue(&ctx);
1063
Daniel Borkmannebaef642015-11-14 01:26:53 +01001064 flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001065
1066#if __LINUX_ARM_ARCH__ < 7
1067 if (ctx.imm_count)
1068 kfree(ctx.imms);
1069#endif
1070
1071 if (bpf_jit_enable > 1)
Daniel Borkmann79617802013-03-21 22:22:03 +01001072 /* there are 2 passes here */
1073 bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001074
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001075 set_memory_ro((unsigned long)header, header->pages);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001076 fp->bpf_func = (void *)ctx.target;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001077 fp->jited = 1;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001078out:
1079 kfree(ctx.offsets);
1080 return;
1081}
1082
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001083void bpf_jit_free(struct bpf_prog *fp)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001084{
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001085 unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
1086 struct bpf_binary_header *header = (void *)addr;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001087
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001088 if (!fp->jited)
1089 goto free_filter;
1090
1091 set_memory_rw(addr, header->pages);
1092 bpf_jit_binary_free(header);
1093
1094free_filter:
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001095 bpf_prog_unlock_free(fp);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001096}