main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  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. #include <linux/bitops.h>
  11. #include <linux/compiler.h>
  12. #include <linux/errno.h>
  13. #include <linux/filter.h>
  14. #include <linux/moduleloader.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/string.h>
  17. #include <linux/slab.h>
  18. #include <linux/if_vlan.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/hwcap.h>
  21. #include "bpf_jit_32.h"
  22. /*
  23. * ABI:
  24. *
  25. * r0 scratch register
  26. * r4 BPF register A
  27. * r5 BPF register X
  28. * r6 pointer to the skb
  29. * r7 skb->data
  30. * r8 skb_headlen(skb)
  31. */
  32. #define r_scratch ARM_R0
  33. /* r1-r3 are (also) used for the unaligned loads on the non-ARMv7 slowpath */
  34. #define r_off ARM_R1
  35. #define r_A ARM_R4
  36. #define r_X ARM_R5
  37. #define r_skb ARM_R6
  38. #define r_skb_data ARM_R7
  39. #define r_skb_hl ARM_R8
  40. #define SCRATCH_SP_OFFSET 0
  41. #define SCRATCH_OFF(k) (SCRATCH_SP_OFFSET + 4 * (k))
  42. #define SEEN_MEM ((1 << BPF_MEMWORDS) - 1)
  43. #define SEEN_MEM_WORD(k) (1 << (k))
  44. #define SEEN_X (1 << BPF_MEMWORDS)
  45. #define SEEN_CALL (1 << (BPF_MEMWORDS + 1))
  46. #define SEEN_SKB (1 << (BPF_MEMWORDS + 2))
  47. #define SEEN_DATA (1 << (BPF_MEMWORDS + 3))
  48. #define FLAG_NEED_X_RESET (1 << 0)
  49. struct jit_ctx {
  50. const struct sk_filter *skf;
  51. unsigned idx;
  52. unsigned prologue_bytes;
  53. int ret0_fp_idx;
  54. u32 seen;
  55. u32 flags;
  56. u32 *offsets;
  57. u32 *target;
  58. #if __LINUX_ARM_ARCH__ < 7
  59. u16 epilogue_bytes;
  60. u16 imm_count;
  61. u32 *imms;
  62. #endif
  63. };
  64. int bpf_jit_enable __read_mostly;
  65. static u64 jit_get_skb_b(struct sk_buff *skb, unsigned offset)
  66. {
  67. u8 ret;
  68. int err;
  69. err = skb_copy_bits(skb, offset, &ret, 1);
  70. return (u64)err << 32 | ret;
  71. }
  72. static u64 jit_get_skb_h(struct sk_buff *skb, unsigned offset)
  73. {
  74. u16 ret;
  75. int err;
  76. err = skb_copy_bits(skb, offset, &ret, 2);
  77. return (u64)err << 32 | ntohs(ret);
  78. }
  79. static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
  80. {
  81. u32 ret;
  82. int err;
  83. err = skb_copy_bits(skb, offset, &ret, 4);
  84. return (u64)err << 32 | ntohl(ret);
  85. }
  86. /*
  87. * Wrapper that handles both OABI and EABI and assures Thumb2 interworking
  88. * (where the assembly routines like __aeabi_uidiv could cause problems).
  89. */
  90. static u32 jit_udiv(u32 dividend, u32 divisor)
  91. {
  92. return dividend / divisor;
  93. }
  94. static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)