blob: 7fd448b23b9468c4940c399818dcac44d5c5d0ba [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
Nicolas Schichan6d715e32015-07-21 14:14:13 +020075static inline int call_neg_helper(struct sk_buff *skb, int offset, void *ret,
76 unsigned int size)
77{
78 void *ptr = bpf_internal_load_pointer_neg_helper(skb, offset, size);
79
80 if (!ptr)
81 return -EFAULT;
82 memcpy(ret, ptr, size);
83 return 0;
84}
85
86static u64 jit_get_skb_b(struct sk_buff *skb, int offset)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010087{
88 u8 ret;
89 int err;
90
Nicolas Schichan6d715e32015-07-21 14:14:13 +020091 if (offset < 0)
92 err = call_neg_helper(skb, offset, &ret, 1);
93 else
94 err = skb_copy_bits(skb, offset, &ret, 1);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +010095
96 return (u64)err << 32 | ret;
97}
98
Nicolas Schichan6d715e32015-07-21 14:14:13 +020099static u64 jit_get_skb_h(struct sk_buff *skb, int offset)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100100{
101 u16 ret;
102 int err;
103
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200104 if (offset < 0)
105 err = call_neg_helper(skb, offset, &ret, 2);
106 else
107 err = skb_copy_bits(skb, offset, &ret, 2);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100108
109 return (u64)err << 32 | ntohs(ret);
110}
111
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200112static u64 jit_get_skb_w(struct sk_buff *skb, int offset)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100113{
114 u32 ret;
115 int err;
116
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200117 if (offset < 0)
118 err = call_neg_helper(skb, offset, &ret, 4);
119 else
120 err = skb_copy_bits(skb, offset, &ret, 4);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100121
122 return (u64)err << 32 | ntohl(ret);
123}
124
125/*
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200126 * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100127 * (where the assembly routines like __aeabi_uidiv could cause problems).
128 */
129static u32 jit_udiv(u32 dividend, u32 divisor)
130{
131 return dividend / divisor;
132}
133
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200134static u32 jit_mod(u32 dividend, u32 divisor)
135{
136 return dividend % divisor;
137}
138
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100139static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
140{
Ben Dooks34607432013-07-24 15:44:56 +0100141 inst |= (cond << 28);
142 inst = __opcode_to_mem_arm(inst);
143
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100144 if (ctx->target != NULL)
Ben Dooks34607432013-07-24 15:44:56 +0100145 ctx->target[ctx->idx] = inst;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100146
147 ctx->idx++;
148}
149
150/*
151 * Emit an instruction that will be executed unconditionally.
152 */
153static inline void emit(u32 inst, struct jit_ctx *ctx)
154{
155 _emit(ARM_COND_AL, inst, ctx);
156}
157
158static u16 saved_regs(struct jit_ctx *ctx)
159{
160 u16 ret = 0;
161
162 if ((ctx->skf->len > 1) ||
Daniel Borkmann34805932014-05-29 10:22:50 +0200163 (ctx->skf->insns[0].code == (BPF_RET | BPF_A)))
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100164 ret |= 1 << r_A;
165
166#ifdef CONFIG_FRAME_POINTER
167 ret |= (1 << ARM_FP) | (1 << ARM_IP) | (1 << ARM_LR) | (1 << ARM_PC);
168#else
169 if (ctx->seen & SEEN_CALL)
170 ret |= 1 << ARM_LR;
171#endif
172 if (ctx->seen & (SEEN_DATA | SEEN_SKB))
173 ret |= 1 << r_skb;
174 if (ctx->seen & SEEN_DATA)
175 ret |= (1 << r_skb_data) | (1 << r_skb_hl);
176 if (ctx->seen & SEEN_X)
177 ret |= 1 << r_X;
178
179 return ret;
180}
181
182static inline int mem_words_used(struct jit_ctx *ctx)
183{
184 /* yes, we do waste some stack space IF there are "holes" in the set" */
185 return fls(ctx->seen & SEEN_MEM);
186}
187
Daniel Borkmann55309dd2014-09-08 08:04:48 +0200188static void jit_fill_hole(void *area, unsigned int size)
189{
Daniel Borkmanne8b56d52014-09-19 14:56:57 +0200190 u32 *ptr;
Daniel Borkmann55309dd2014-09-08 08:04:48 +0200191 /* We are guaranteed to have aligned memory. */
192 for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
Daniel Borkmanne8b56d52014-09-19 14:56:57 +0200193 *ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
Daniel Borkmann55309dd2014-09-08 08:04:48 +0200194}
195
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100196static void build_prologue(struct jit_ctx *ctx)
197{
198 u16 reg_set = saved_regs(ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100199 u16 off;
200
201#ifdef CONFIG_FRAME_POINTER
202 emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
203 emit(ARM_PUSH(reg_set), ctx);
204 emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
205#else
206 if (reg_set)
207 emit(ARM_PUSH(reg_set), ctx);
208#endif
209
210 if (ctx->seen & (SEEN_DATA | SEEN_SKB))
211 emit(ARM_MOV_R(r_skb, ARM_R0), ctx);
212
213 if (ctx->seen & SEEN_DATA) {
214 off = offsetof(struct sk_buff, data);
215 emit(ARM_LDR_I(r_skb_data, r_skb, off), ctx);
216 /* headlen = len - data_len */
217 off = offsetof(struct sk_buff, len);
218 emit(ARM_LDR_I(r_skb_hl, r_skb, off), ctx);
219 off = offsetof(struct sk_buff, data_len);
220 emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
221 emit(ARM_SUB_R(r_skb_hl, r_skb_hl, r_scratch), ctx);
222 }
223
224 if (ctx->flags & FLAG_NEED_X_RESET)
225 emit(ARM_MOV_I(r_X, 0), ctx);
226
227 /* do not leak kernel data to userspace */
Rabin Vincent55795ef2016-01-05 16:23:07 +0100228 if (bpf_needs_clear_a(&ctx->skf->insns[0]))
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100229 emit(ARM_MOV_I(r_A, 0), ctx);
230
231 /* stack space for the BPF_MEM words */
232 if (ctx->seen & SEEN_MEM)
233 emit(ARM_SUB_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
234}
235
236static void build_epilogue(struct jit_ctx *ctx)
237{
238 u16 reg_set = saved_regs(ctx);
239
240 if (ctx->seen & SEEN_MEM)
241 emit(ARM_ADD_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
242
243 reg_set &= ~(1 << ARM_LR);
244
245#ifdef CONFIG_FRAME_POINTER
246 /* the first instruction of the prologue was: mov ip, sp */
247 reg_set &= ~(1 << ARM_IP);
248 reg_set |= (1 << ARM_SP);
249 emit(ARM_LDM(ARM_SP, reg_set), ctx);
250#else
251 if (reg_set) {
252 if (ctx->seen & SEEN_CALL)
253 reg_set |= 1 << ARM_PC;
254 emit(ARM_POP(reg_set), ctx);
255 }
256
257 if (!(ctx->seen & SEEN_CALL))
258 emit(ARM_BX(ARM_LR), ctx);
259#endif
260}
261
262static int16_t imm8m(u32 x)
263{
264 u32 rot;
265
266 for (rot = 0; rot < 16; rot++)
267 if ((x & ~ror32(0xff, 2 * rot)) == 0)
268 return rol32(x, 2 * rot) | (rot << 8);
269
270 return -1;
271}
272
273#if __LINUX_ARM_ARCH__ < 7
274
275static u16 imm_offset(u32 k, struct jit_ctx *ctx)
276{
277 unsigned i = 0, offset;
278 u16 imm;
279
280 /* on the "fake" run we just count them (duplicates included) */
281 if (ctx->target == NULL) {
282 ctx->imm_count++;
283 return 0;
284 }
285
286 while ((i < ctx->imm_count) && ctx->imms[i]) {
287 if (ctx->imms[i] == k)
288 break;
289 i++;
290 }
291
292 if (ctx->imms[i] == 0)
293 ctx->imms[i] = k;
294
295 /* constants go just after the epilogue */
296 offset = ctx->offsets[ctx->skf->len];
297 offset += ctx->prologue_bytes;
298 offset += ctx->epilogue_bytes;
299 offset += i * 4;
300
301 ctx->target[offset / 4] = k;
302
303 /* PC in ARM mode == address of the instruction + 8 */
304 imm = offset - (8 + ctx->idx * 4);
305
Nicolas Schichan0b59d882015-05-07 17:14:21 +0200306 if (imm & ~0xfff) {
307 /*
308 * literal pool is too far, signal it into flags. we
309 * can only detect it on the second pass unfortunately.
310 */
311 ctx->flags |= FLAG_IMM_OVERFLOW;
312 return 0;
313 }
314
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100315 return imm;
316}
317
318#endif /* __LINUX_ARM_ARCH__ */
319
320/*
321 * Move an immediate that's not an imm8m to a core register.
322 */
323static inline void emit_mov_i_no8m(int rd, u32 val, struct jit_ctx *ctx)
324{
325#if __LINUX_ARM_ARCH__ < 7
326 emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
327#else
328 emit(ARM_MOVW(rd, val & 0xffff), ctx);
329 if (val > 0xffff)
330 emit(ARM_MOVT(rd, val >> 16), ctx);
331#endif
332}
333
334static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx)
335{
336 int imm12 = imm8m(val);
337
338 if (imm12 >= 0)
339 emit(ARM_MOV_I(rd, imm12), ctx);
340 else
341 emit_mov_i_no8m(rd, val, ctx);
342}
343
344#if __LINUX_ARM_ARCH__ < 6
345
346static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
347{
348 _emit(cond, ARM_LDRB_I(ARM_R3, r_addr, 1), ctx);
349 _emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
350 _emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 3), ctx);
351 _emit(cond, ARM_LSL_I(ARM_R3, ARM_R3, 16), ctx);
352 _emit(cond, ARM_LDRB_I(ARM_R0, r_addr, 2), ctx);
353 _emit(cond, ARM_ORR_S(ARM_R3, ARM_R3, ARM_R1, SRTYPE_LSL, 24), ctx);
354 _emit(cond, ARM_ORR_R(ARM_R3, ARM_R3, ARM_R2), ctx);
355 _emit(cond, ARM_ORR_S(r_res, ARM_R3, ARM_R0, SRTYPE_LSL, 8), ctx);
356}
357
358static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
359{
360 _emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
361 _emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 1), ctx);
362 _emit(cond, ARM_ORR_S(r_res, ARM_R2, ARM_R1, SRTYPE_LSL, 8), ctx);
363}
364
365static inline void emit_swap16(u8 r_dst, u8 r_src, struct jit_ctx *ctx)
366{
Nicolas Schichan462738f2013-02-13 17:30:39 +0000367 /* r_dst = (r_src << 8) | (r_src >> 8) */
368 emit(ARM_LSL_I(ARM_R1, r_src, 8), ctx);
369 emit(ARM_ORR_S(r_dst, ARM_R1, r_src, SRTYPE_LSR, 8), ctx);
370
371 /*
372 * we need to mask out the bits set in r_dst[23:16] due to
373 * the first shift instruction.
374 *
375 * note that 0x8ff is the encoded immediate 0x00ff0000.
376 */
377 emit(ARM_BIC_I(r_dst, r_dst, 0x8ff), ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100378}
379
380#else /* ARMv6+ */
381
382static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
383{
384 _emit(cond, ARM_LDR_I(r_res, r_addr, 0), ctx);
385#ifdef __LITTLE_ENDIAN
386 _emit(cond, ARM_REV(r_res, r_res), ctx);
387#endif
388}
389
390static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
391{
392 _emit(cond, ARM_LDRH_I(r_res, r_addr, 0), ctx);
393#ifdef __LITTLE_ENDIAN
394 _emit(cond, ARM_REV16(r_res, r_res), ctx);
395#endif
396}
397
398static inline void emit_swap16(u8 r_dst __maybe_unused,
399 u8 r_src __maybe_unused,
400 struct jit_ctx *ctx __maybe_unused)
401{
402#ifdef __LITTLE_ENDIAN
403 emit(ARM_REV16(r_dst, r_src), ctx);
404#endif
405}
406
407#endif /* __LINUX_ARM_ARCH__ < 6 */
408
409
410/* Compute the immediate value for a PC-relative branch. */
411static inline u32 b_imm(unsigned tgt, struct jit_ctx *ctx)
412{
413 u32 imm;
414
415 if (ctx->target == NULL)
416 return 0;
417 /*
418 * BPF allows only forward jumps and the offset of the target is
419 * still the one computed during the first pass.
420 */
421 imm = ctx->offsets[tgt] + ctx->prologue_bytes - (ctx->idx * 4 + 8);
422
423 return imm >> 2;
424}
425
426#define OP_IMM3(op, r1, r2, imm_val, ctx) \
427 do { \
428 imm12 = imm8m(imm_val); \
429 if (imm12 < 0) { \
430 emit_mov_i_no8m(r_scratch, imm_val, ctx); \
431 emit(op ## _R((r1), (r2), r_scratch), ctx); \
432 } else { \
433 emit(op ## _I((r1), (r2), imm12), ctx); \
434 } \
435 } while (0)
436
437static inline void emit_err_ret(u8 cond, struct jit_ctx *ctx)
438{
439 if (ctx->ret0_fp_idx >= 0) {
440 _emit(cond, ARM_B(b_imm(ctx->ret0_fp_idx, ctx)), ctx);
441 /* NOP to keep the size constant between passes */
442 emit(ARM_MOV_R(ARM_R0, ARM_R0), ctx);
443 } else {
444 _emit(cond, ARM_MOV_I(ARM_R0, 0), ctx);
445 _emit(cond, ARM_B(b_imm(ctx->skf->len, ctx)), ctx);
446 }
447}
448
449static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
450{
451#if __LINUX_ARM_ARCH__ < 5
452 emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
453
454 if (elf_hwcap & HWCAP_THUMB)
455 emit(ARM_BX(tgt_reg), ctx);
456 else
457 emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
458#else
459 emit(ARM_BLX_R(tgt_reg), ctx);
460#endif
461}
462
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200463static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx,
464 int bpf_op)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100465{
466#if __LINUX_ARM_ARCH__ == 7
467 if (elf_hwcap & HWCAP_IDIVA) {
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200468 if (bpf_op == BPF_DIV)
469 emit(ARM_UDIV(rd, rm, rn), ctx);
470 else {
471 emit(ARM_UDIV(ARM_R3, rm, rn), ctx);
472 emit(ARM_MLS(rd, rn, ARM_R3, rm), ctx);
473 }
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100474 return;
475 }
476#endif
Nicolas Schichan19fc99d2015-05-06 18:31:56 +0200477
478 /*
479 * For BPF_ALU | BPF_DIV | BPF_K instructions, rm is ARM_R4
480 * (r_A) and rn is ARM_R0 (r_scratch) so load rn first into
481 * ARM_R1 to avoid accidentally overwriting ARM_R0 with rm
482 * before using it as a source for ARM_R1.
483 *
484 * For BPF_ALU | BPF_DIV | BPF_X rm is ARM_R4 (r_A) and rn is
485 * ARM_R5 (r_X) so there is no particular register overlap
486 * issues.
487 */
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100488 if (rn != ARM_R1)
489 emit(ARM_MOV_R(ARM_R1, rn), ctx);
Nicolas Schichan19fc99d2015-05-06 18:31:56 +0200490 if (rm != ARM_R0)
491 emit(ARM_MOV_R(ARM_R0, rm), ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100492
493 ctx->seen |= SEEN_CALL;
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200494 emit_mov_i(ARM_R3, bpf_op == BPF_DIV ? (u32)jit_udiv : (u32)jit_mod,
495 ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100496 emit_blx_r(ARM_R3, ctx);
497
498 if (rd != ARM_R0)
499 emit(ARM_MOV_R(rd, ARM_R0), ctx);
500}
501
502static inline void update_on_xread(struct jit_ctx *ctx)
503{
504 if (!(ctx->seen & SEEN_X))
505 ctx->flags |= FLAG_NEED_X_RESET;
506
507 ctx->seen |= SEEN_X;
508}
509
510static int build_body(struct jit_ctx *ctx)
511{
512 void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700513 const struct bpf_prog *prog = ctx->skf;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100514 const struct sock_filter *inst;
515 unsigned i, load_order, off, condt;
516 int imm12;
517 u32 k;
518
519 for (i = 0; i < prog->len; i++) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200520 u16 code;
521
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100522 inst = &(prog->insns[i]);
523 /* K as an immediate value operand */
524 k = inst->k;
Daniel Borkmann34805932014-05-29 10:22:50 +0200525 code = bpf_anc_helper(inst);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100526
527 /* compute offsets only in the fake pass */
528 if (ctx->target == NULL)
529 ctx->offsets[i] = ctx->idx * 4;
530
Daniel Borkmann34805932014-05-29 10:22:50 +0200531 switch (code) {
532 case BPF_LD | BPF_IMM:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100533 emit_mov_i(r_A, k, ctx);
534 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200535 case BPF_LD | BPF_W | BPF_LEN:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100536 ctx->seen |= SEEN_SKB;
537 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
538 emit(ARM_LDR_I(r_A, r_skb,
539 offsetof(struct sk_buff, len)), ctx);
540 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200541 case BPF_LD | BPF_MEM:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100542 /* A = scratch[k] */
543 ctx->seen |= SEEN_MEM_WORD(k);
544 emit(ARM_LDR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
545 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200546 case BPF_LD | BPF_W | BPF_ABS:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100547 load_order = 2;
548 goto load;
Daniel Borkmann34805932014-05-29 10:22:50 +0200549 case BPF_LD | BPF_H | BPF_ABS:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100550 load_order = 1;
551 goto load;
Daniel Borkmann34805932014-05-29 10:22:50 +0200552 case BPF_LD | BPF_B | BPF_ABS:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100553 load_order = 0;
554load:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100555 emit_mov_i(r_off, k, ctx);
556load_common:
557 ctx->seen |= SEEN_DATA | SEEN_CALL;
558
559 if (load_order > 0) {
560 emit(ARM_SUB_I(r_scratch, r_skb_hl,
561 1 << load_order), ctx);
562 emit(ARM_CMP_R(r_scratch, r_off), ctx);
Nicolas Schichan7aed35c2015-07-21 14:14:12 +0200563 condt = ARM_COND_GE;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100564 } else {
565 emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
566 condt = ARM_COND_HI;
567 }
568
Nicolas Schichan6d715e32015-07-21 14:14:13 +0200569 /*
570 * test for negative offset, only if we are
571 * currently scheduled to take the fast
572 * path. this will update the flags so that
573 * the slowpath instruction are ignored if the
574 * offset is negative.
575 *
576 * for loard_order == 0 the HI condition will
577 * make loads at offset 0 take the slow path too.
578 */
579 _emit(condt, ARM_CMP_I(r_off, 0), ctx);
580
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100581 _emit(condt, ARM_ADD_R(r_scratch, r_off, r_skb_data),
582 ctx);
583
584 if (load_order == 0)
585 _emit(condt, ARM_LDRB_I(r_A, r_scratch, 0),
586 ctx);
587 else if (load_order == 1)
588 emit_load_be16(condt, r_A, r_scratch, ctx);
589 else if (load_order == 2)
590 emit_load_be32(condt, r_A, r_scratch, ctx);
591
592 _emit(condt, ARM_B(b_imm(i + 1, ctx)), ctx);
593
594 /* the slowpath */
595 emit_mov_i(ARM_R3, (u32)load_func[load_order], ctx);
596 emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
597 /* the offset is already in R1 */
598 emit_blx_r(ARM_R3, ctx);
599 /* check the result of skb_copy_bits */
600 emit(ARM_CMP_I(ARM_R1, 0), ctx);
601 emit_err_ret(ARM_COND_NE, ctx);
602 emit(ARM_MOV_R(r_A, ARM_R0), ctx);
603 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200604 case BPF_LD | BPF_W | BPF_IND:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100605 load_order = 2;
606 goto load_ind;
Daniel Borkmann34805932014-05-29 10:22:50 +0200607 case BPF_LD | BPF_H | BPF_IND:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100608 load_order = 1;
609 goto load_ind;
Daniel Borkmann34805932014-05-29 10:22:50 +0200610 case BPF_LD | BPF_B | BPF_IND:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100611 load_order = 0;
612load_ind:
Nicolas Schichan8690f472015-10-02 15:39:12 +0200613 update_on_xread(ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100614 OP_IMM3(ARM_ADD, r_off, r_X, k, ctx);
615 goto load_common;
Daniel Borkmann34805932014-05-29 10:22:50 +0200616 case BPF_LDX | BPF_IMM:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100617 ctx->seen |= SEEN_X;
618 emit_mov_i(r_X, k, ctx);
619 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200620 case BPF_LDX | BPF_W | BPF_LEN:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100621 ctx->seen |= SEEN_X | SEEN_SKB;
622 emit(ARM_LDR_I(r_X, r_skb,
623 offsetof(struct sk_buff, len)), ctx);
624 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200625 case BPF_LDX | BPF_MEM:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100626 ctx->seen |= SEEN_X | SEEN_MEM_WORD(k);
627 emit(ARM_LDR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
628 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200629 case BPF_LDX | BPF_B | BPF_MSH:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100630 /* x = ((*(frame + k)) & 0xf) << 2; */
631 ctx->seen |= SEEN_X | SEEN_DATA | SEEN_CALL;
632 /* the interpreter should deal with the negative K */
Chen Gang45549a62013-03-10 10:15:54 +0800633 if ((int)k < 0)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100634 return -1;
635 /* offset in r1: we might have to take the slow path */
636 emit_mov_i(r_off, k, ctx);
637 emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
638
639 /* load in r0: common with the slowpath */
640 _emit(ARM_COND_HI, ARM_LDRB_R(ARM_R0, r_skb_data,
641 ARM_R1), ctx);
642 /*
643 * emit_mov_i() might generate one or two instructions,
644 * the same holds for emit_blx_r()
645 */
646 _emit(ARM_COND_HI, ARM_B(b_imm(i + 1, ctx) - 2), ctx);
647
648 emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
649 /* r_off is r1 */
650 emit_mov_i(ARM_R3, (u32)jit_get_skb_b, ctx);
651 emit_blx_r(ARM_R3, ctx);
652 /* check the return value of skb_copy_bits */
653 emit(ARM_CMP_I(ARM_R1, 0), ctx);
654 emit_err_ret(ARM_COND_NE, ctx);
655
656 emit(ARM_AND_I(r_X, ARM_R0, 0x00f), ctx);
657 emit(ARM_LSL_I(r_X, r_X, 2), ctx);
658 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200659 case BPF_ST:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100660 ctx->seen |= SEEN_MEM_WORD(k);
661 emit(ARM_STR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
662 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200663 case BPF_STX:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100664 update_on_xread(ctx);
665 ctx->seen |= SEEN_MEM_WORD(k);
666 emit(ARM_STR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
667 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200668 case BPF_ALU | BPF_ADD | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100669 /* A += K */
670 OP_IMM3(ARM_ADD, r_A, r_A, k, ctx);
671 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200672 case BPF_ALU | BPF_ADD | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100673 update_on_xread(ctx);
674 emit(ARM_ADD_R(r_A, r_A, r_X), ctx);
675 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200676 case BPF_ALU | BPF_SUB | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100677 /* A -= K */
678 OP_IMM3(ARM_SUB, r_A, r_A, k, ctx);
679 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200680 case BPF_ALU | BPF_SUB | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100681 update_on_xread(ctx);
682 emit(ARM_SUB_R(r_A, r_A, r_X), ctx);
683 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200684 case BPF_ALU | BPF_MUL | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100685 /* A *= K */
686 emit_mov_i(r_scratch, k, ctx);
687 emit(ARM_MUL(r_A, r_A, r_scratch), ctx);
688 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200689 case BPF_ALU | BPF_MUL | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100690 update_on_xread(ctx);
691 emit(ARM_MUL(r_A, r_A, r_X), ctx);
692 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200693 case BPF_ALU | BPF_DIV | BPF_K:
Eric Dumazetaee636c2014-01-15 06:50:07 -0800694 if (k == 1)
695 break;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100696 emit_mov_i(r_scratch, k, ctx);
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200697 emit_udivmod(r_A, r_A, r_scratch, ctx, BPF_DIV);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100698 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200699 case BPF_ALU | BPF_DIV | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100700 update_on_xread(ctx);
701 emit(ARM_CMP_I(r_X, 0), ctx);
702 emit_err_ret(ARM_COND_EQ, ctx);
Nicolas Schichan4560cdf2015-10-02 17:06:47 +0200703 emit_udivmod(r_A, r_A, r_X, ctx, BPF_DIV);
704 break;
705 case BPF_ALU | BPF_MOD | BPF_K:
706 if (k == 1) {
707 emit_mov_i(r_A, 0, ctx);
708 break;
709 }
710 emit_mov_i(r_scratch, k, ctx);
711 emit_udivmod(r_A, r_A, r_scratch, ctx, BPF_MOD);
712 break;
713 case BPF_ALU | BPF_MOD | BPF_X:
714 update_on_xread(ctx);
715 emit(ARM_CMP_I(r_X, 0), ctx);
716 emit_err_ret(ARM_COND_EQ, ctx);
717 emit_udivmod(r_A, r_A, r_X, ctx, BPF_MOD);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100718 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200719 case BPF_ALU | BPF_OR | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100720 /* A |= K */
721 OP_IMM3(ARM_ORR, r_A, r_A, k, ctx);
722 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200723 case BPF_ALU | BPF_OR | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100724 update_on_xread(ctx);
725 emit(ARM_ORR_R(r_A, r_A, r_X), ctx);
726 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200727 case BPF_ALU | BPF_XOR | BPF_K:
Daniel Borkmann3cbe2042012-11-07 15:28:28 +0000728 /* A ^= K; */
729 OP_IMM3(ARM_EOR, r_A, r_A, k, ctx);
730 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200731 case BPF_ANC | SKF_AD_ALU_XOR_X:
732 case BPF_ALU | BPF_XOR | BPF_X:
Daniel Borkmann3cbe2042012-11-07 15:28:28 +0000733 /* A ^= X */
734 update_on_xread(ctx);
735 emit(ARM_EOR_R(r_A, r_A, r_X), ctx);
736 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200737 case BPF_ALU | BPF_AND | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100738 /* A &= K */
739 OP_IMM3(ARM_AND, r_A, r_A, k, ctx);
740 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200741 case BPF_ALU | BPF_AND | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100742 update_on_xread(ctx);
743 emit(ARM_AND_R(r_A, r_A, r_X), ctx);
744 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200745 case BPF_ALU | BPF_LSH | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100746 if (unlikely(k > 31))
747 return -1;
748 emit(ARM_LSL_I(r_A, r_A, k), ctx);
749 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200750 case BPF_ALU | BPF_LSH | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100751 update_on_xread(ctx);
752 emit(ARM_LSL_R(r_A, r_A, r_X), ctx);
753 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200754 case BPF_ALU | BPF_RSH | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100755 if (unlikely(k > 31))
756 return -1;
Rabin Vincentf9414612016-01-05 18:34:04 +0100757 if (k)
758 emit(ARM_LSR_I(r_A, r_A, k), ctx);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100759 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200760 case BPF_ALU | BPF_RSH | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100761 update_on_xread(ctx);
762 emit(ARM_LSR_R(r_A, r_A, r_X), ctx);
763 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200764 case BPF_ALU | BPF_NEG:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100765 /* A = -A */
766 emit(ARM_RSB_I(r_A, r_A, 0), ctx);
767 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200768 case BPF_JMP | BPF_JA:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100769 /* pc += K */
770 emit(ARM_B(b_imm(i + k + 1, ctx)), ctx);
771 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200772 case BPF_JMP | BPF_JEQ | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100773 /* pc += (A == K) ? pc->jt : pc->jf */
774 condt = ARM_COND_EQ;
775 goto cmp_imm;
Daniel Borkmann34805932014-05-29 10:22:50 +0200776 case BPF_JMP | BPF_JGT | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100777 /* pc += (A > K) ? pc->jt : pc->jf */
778 condt = ARM_COND_HI;
779 goto cmp_imm;
Daniel Borkmann34805932014-05-29 10:22:50 +0200780 case BPF_JMP | BPF_JGE | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100781 /* pc += (A >= K) ? pc->jt : pc->jf */
782 condt = ARM_COND_HS;
783cmp_imm:
784 imm12 = imm8m(k);
785 if (imm12 < 0) {
786 emit_mov_i_no8m(r_scratch, k, ctx);
787 emit(ARM_CMP_R(r_A, r_scratch), ctx);
788 } else {
789 emit(ARM_CMP_I(r_A, imm12), ctx);
790 }
791cond_jump:
792 if (inst->jt)
793 _emit(condt, ARM_B(b_imm(i + inst->jt + 1,
794 ctx)), ctx);
795 if (inst->jf)
796 _emit(condt ^ 1, ARM_B(b_imm(i + inst->jf + 1,
797 ctx)), ctx);
798 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200799 case BPF_JMP | BPF_JEQ | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100800 /* pc += (A == X) ? pc->jt : pc->jf */
801 condt = ARM_COND_EQ;
802 goto cmp_x;
Daniel Borkmann34805932014-05-29 10:22:50 +0200803 case BPF_JMP | BPF_JGT | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100804 /* pc += (A > X) ? pc->jt : pc->jf */
805 condt = ARM_COND_HI;
806 goto cmp_x;
Daniel Borkmann34805932014-05-29 10:22:50 +0200807 case BPF_JMP | BPF_JGE | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100808 /* pc += (A >= X) ? pc->jt : pc->jf */
809 condt = ARM_COND_CS;
810cmp_x:
811 update_on_xread(ctx);
812 emit(ARM_CMP_R(r_A, r_X), ctx);
813 goto cond_jump;
Daniel Borkmann34805932014-05-29 10:22:50 +0200814 case BPF_JMP | BPF_JSET | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100815 /* pc += (A & K) ? pc->jt : pc->jf */
816 condt = ARM_COND_NE;
817 /* not set iff all zeroes iff Z==1 iff EQ */
818
819 imm12 = imm8m(k);
820 if (imm12 < 0) {
821 emit_mov_i_no8m(r_scratch, k, ctx);
822 emit(ARM_TST_R(r_A, r_scratch), ctx);
823 } else {
824 emit(ARM_TST_I(r_A, imm12), ctx);
825 }
826 goto cond_jump;
Daniel Borkmann34805932014-05-29 10:22:50 +0200827 case BPF_JMP | BPF_JSET | BPF_X:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100828 /* pc += (A & X) ? pc->jt : pc->jf */
829 update_on_xread(ctx);
830 condt = ARM_COND_NE;
831 emit(ARM_TST_R(r_A, r_X), ctx);
832 goto cond_jump;
Daniel Borkmann34805932014-05-29 10:22:50 +0200833 case BPF_RET | BPF_A:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100834 emit(ARM_MOV_R(ARM_R0, r_A), ctx);
835 goto b_epilogue;
Daniel Borkmann34805932014-05-29 10:22:50 +0200836 case BPF_RET | BPF_K:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100837 if ((k == 0) && (ctx->ret0_fp_idx < 0))
838 ctx->ret0_fp_idx = i;
839 emit_mov_i(ARM_R0, k, ctx);
840b_epilogue:
841 if (i != ctx->skf->len - 1)
842 emit(ARM_B(b_imm(prog->len, ctx)), ctx);
843 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200844 case BPF_MISC | BPF_TAX:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100845 /* X = A */
846 ctx->seen |= SEEN_X;
847 emit(ARM_MOV_R(r_X, r_A), ctx);
848 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200849 case BPF_MISC | BPF_TXA:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100850 /* A = X */
851 update_on_xread(ctx);
852 emit(ARM_MOV_R(r_A, r_X), ctx);
853 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200854 case BPF_ANC | SKF_AD_PROTOCOL:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100855 /* A = ntohs(skb->protocol) */
856 ctx->seen |= SEEN_SKB;
857 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
858 protocol) != 2);
859 off = offsetof(struct sk_buff, protocol);
860 emit(ARM_LDRH_I(r_scratch, r_skb, off), ctx);
861 emit_swap16(r_A, r_scratch, ctx);
862 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200863 case BPF_ANC | SKF_AD_CPU:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100864 /* r_scratch = current_thread_info() */
865 OP_IMM3(ARM_BIC, r_scratch, ARM_SP, THREAD_SIZE - 1, ctx);
866 /* A = current_thread_info()->cpu */
867 BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4);
868 off = offsetof(struct thread_info, cpu);
869 emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
870 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200871 case BPF_ANC | SKF_AD_IFINDEX:
Nicolas Schichan5bf705b2015-07-27 15:06:51 +0200872 case BPF_ANC | SKF_AD_HATYPE:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100873 /* A = skb->dev->ifindex */
Nicolas Schichan5bf705b2015-07-27 15:06:51 +0200874 /* A = skb->dev->type */
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100875 ctx->seen |= SEEN_SKB;
876 off = offsetof(struct sk_buff, dev);
877 emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
878
879 emit(ARM_CMP_I(r_scratch, 0), ctx);
880 emit_err_ret(ARM_COND_EQ, ctx);
881
882 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
883 ifindex) != 4);
Nicolas Schichan5bf705b2015-07-27 15:06:51 +0200884 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
885 type) != 2);
886
887 if (code == (BPF_ANC | SKF_AD_IFINDEX)) {
888 off = offsetof(struct net_device, ifindex);
889 emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
890 } else {
891 /*
892 * offset of field "type" in "struct
893 * net_device" is above what can be
894 * used in the ldrh rd, [rn, #imm]
895 * instruction, so load the offset in
896 * a register and use ldrh rd, [rn, rm]
897 */
898 off = offsetof(struct net_device, type);
899 emit_mov_i(ARM_R3, off, ctx);
900 emit(ARM_LDRH_R(r_A, r_scratch, ARM_R3), ctx);
901 }
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100902 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200903 case BPF_ANC | SKF_AD_MARK:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100904 ctx->seen |= SEEN_SKB;
905 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
906 off = offsetof(struct sk_buff, mark);
907 emit(ARM_LDR_I(r_A, r_skb, off), ctx);
908 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200909 case BPF_ANC | SKF_AD_RXHASH:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100910 ctx->seen |= SEEN_SKB;
Tom Herbert61b905d2014-03-24 15:34:47 -0700911 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
912 off = offsetof(struct sk_buff, hash);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100913 emit(ARM_LDR_I(r_A, r_skb, off), ctx);
914 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200915 case BPF_ANC | SKF_AD_VLAN_TAG:
916 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
Daniel Borkmannbf0098f2012-11-07 15:31:02 +0000917 ctx->seen |= SEEN_SKB;
918 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
919 off = offsetof(struct sk_buff, vlan_tci);
920 emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
Daniel Borkmann34805932014-05-29 10:22:50 +0200921 if (code == (BPF_ANC | SKF_AD_VLAN_TAG))
Nicolas Schichanc18fe542015-07-21 14:14:14 +0200922 OP_IMM3(ARM_AND, r_A, r_A, ~VLAN_TAG_PRESENT, ctx);
923 else {
924 OP_IMM3(ARM_LSR, r_A, r_A, 12, ctx);
925 OP_IMM3(ARM_AND, r_A, r_A, 0x1, ctx);
926 }
Daniel Borkmannbf0098f2012-11-07 15:31:02 +0000927 break;
Nicolas Schichan1447f932015-07-27 15:06:49 +0200928 case BPF_ANC | SKF_AD_PKTTYPE:
929 ctx->seen |= SEEN_SKB;
930 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
931 __pkt_type_offset[0]) != 1);
932 off = PKT_TYPE_OFFSET();
933 emit(ARM_LDRB_I(r_A, r_skb, off), ctx);
934 emit(ARM_AND_I(r_A, r_A, PKT_TYPE_MAX), ctx);
935#ifdef __BIG_ENDIAN_BITFIELD
936 emit(ARM_LSR_I(r_A, r_A, 5), ctx);
937#endif
938 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200939 case BPF_ANC | SKF_AD_QUEUE:
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100940 ctx->seen |= SEEN_SKB;
941 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
942 queue_mapping) != 2);
943 BUILD_BUG_ON(offsetof(struct sk_buff,
944 queue_mapping) > 0xff);
945 off = offsetof(struct sk_buff, queue_mapping);
946 emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
947 break;
Nicolas Schichan303249a2015-07-27 15:06:50 +0200948 case BPF_ANC | SKF_AD_PAY_OFFSET:
949 ctx->seen |= SEEN_SKB | SEEN_CALL;
950
951 emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
952 emit_mov_i(ARM_R3, (unsigned int)skb_get_poff, ctx);
953 emit_blx_r(ARM_R3, ctx);
954 emit(ARM_MOV_R(r_A, ARM_R0), ctx);
955 break;
Nicolas Schichan24e737c2015-05-07 15:00:13 +0200956 case BPF_LDX | BPF_W | BPF_ABS:
957 /*
958 * load a 32bit word from struct seccomp_data.
959 * seccomp_check_filter() will already have checked
960 * that k is 32bit aligned and lies within the
961 * struct seccomp_data.
962 */
963 ctx->seen |= SEEN_SKB;
964 emit(ARM_LDR_I(r_A, r_skb, k), ctx);
965 break;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100966 default:
967 return -1;
968 }
Nicolas Schichan0b59d882015-05-07 17:14:21 +0200969
970 if (ctx->flags & FLAG_IMM_OVERFLOW)
971 /*
972 * this instruction generated an overflow when
973 * trying to access the literal pool, so
974 * delegate this filter to the kernel interpreter.
975 */
976 return -1;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100977 }
978
979 /* compute offsets only during the first pass */
980 if (ctx->target == NULL)
981 ctx->offsets[i] = ctx->idx * 4;
982
983 return 0;
984}
985
986
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700987void bpf_jit_compile(struct bpf_prog *fp)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100988{
Daniel Borkmann55309dd2014-09-08 08:04:48 +0200989 struct bpf_binary_header *header;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100990 struct jit_ctx ctx;
991 unsigned tmp_idx;
992 unsigned alloc_size;
Daniel Borkmann55309dd2014-09-08 08:04:48 +0200993 u8 *target_ptr;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +0100994
995 if (!bpf_jit_enable)
996 return;
997
998 memset(&ctx, 0, sizeof(ctx));
999 ctx.skf = fp;
1000 ctx.ret0_fp_idx = -1;
1001
Schichan Nicolas89c2e002012-12-10 14:49:39 +01001002 ctx.offsets = kzalloc(4 * (ctx.skf->len + 1), GFP_KERNEL);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001003 if (ctx.offsets == NULL)
1004 return;
1005
1006 /* fake pass to fill in the ctx->seen */
1007 if (unlikely(build_body(&ctx)))
1008 goto out;
1009
1010 tmp_idx = ctx.idx;
1011 build_prologue(&ctx);
1012 ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1013
1014#if __LINUX_ARM_ARCH__ < 7
1015 tmp_idx = ctx.idx;
1016 build_epilogue(&ctx);
1017 ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
1018
1019 ctx.idx += ctx.imm_count;
1020 if (ctx.imm_count) {
Schichan Nicolas89c2e002012-12-10 14:49:39 +01001021 ctx.imms = kzalloc(4 * ctx.imm_count, GFP_KERNEL);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001022 if (ctx.imms == NULL)
1023 goto out;
1024 }
1025#else
1026 /* there's nothing after the epilogue on ARMv7 */
1027 build_epilogue(&ctx);
1028#endif
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001029 alloc_size = 4 * ctx.idx;
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001030 header = bpf_jit_binary_alloc(alloc_size, &target_ptr,
1031 4, jit_fill_hole);
1032 if (header == NULL)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001033 goto out;
1034
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001035 ctx.target = (u32 *) target_ptr;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001036 ctx.idx = 0;
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001037
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001038 build_prologue(&ctx);
Nicolas Schichan0b59d882015-05-07 17:14:21 +02001039 if (build_body(&ctx) < 0) {
1040#if __LINUX_ARM_ARCH__ < 7
1041 if (ctx.imm_count)
1042 kfree(ctx.imms);
1043#endif
1044 bpf_jit_binary_free(header);
1045 goto out;
1046 }
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001047 build_epilogue(&ctx);
1048
Daniel Borkmannebaef642015-11-14 01:26:53 +01001049 flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001050
1051#if __LINUX_ARM_ARCH__ < 7
1052 if (ctx.imm_count)
1053 kfree(ctx.imms);
1054#endif
1055
1056 if (bpf_jit_enable > 1)
Daniel Borkmann79617802013-03-21 22:22:03 +01001057 /* there are 2 passes here */
1058 bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001059
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001060 set_memory_ro((unsigned long)header, header->pages);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001061 fp->bpf_func = (void *)ctx.target;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001062 fp->jited = 1;
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001063out:
1064 kfree(ctx.offsets);
1065 return;
1066}
1067
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001068void bpf_jit_free(struct bpf_prog *fp)
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001069{
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001070 unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
1071 struct bpf_binary_header *header = (void *)addr;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001072
Daniel Borkmann55309dd2014-09-08 08:04:48 +02001073 if (!fp->jited)
1074 goto free_filter;
1075
1076 set_memory_rw(addr, header->pages);
1077 bpf_jit_binary_free(header);
1078
1079free_filter:
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001080 bpf_prog_unlock_free(fp);
Mircea Gherzanddecdfc2012-03-16 13:37:12 +01001081}