main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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)
  95. {
  96. if (ctx->target != NULL)
  97. ctx->target[ctx->idx] = inst | (cond << 28);
  98. ctx->idx++;
  99. }
  100. /*
  101. * Emit an instruction that will be executed unconditionally.
  102. */
  103. static inline void emit(u32 inst, struct jit_ctx *ctx)
  104. {
  105. _emit(ARM_COND_AL, inst, ctx);
  106. }
  107. static u16 saved_regs(struct jit_ctx *ctx)
  108. {
  109. u16 ret = 0;
  110. if ((ctx->skf->len > 1) ||
  111. (ctx->skf->insns[0].code == BPF_S_RET_A))
  112. ret |= 1 << r_A;
  113. #ifdef CONFIG_FRAME_POINTER
  114. ret |= (1 << ARM_FP) | (1 << ARM_IP) | (1 << ARM_LR) | (1 << ARM_PC);
  115. #else
  116. if (ctx->seen & SEEN_CALL)
  117. ret |= 1 << ARM_LR;
  118. #endif
  119. if (ctx->seen & (SEEN_DATA | SEEN_SKB))
  120. ret |= 1 << r_skb;
  121. if (ctx->seen & SEEN_DATA)
  122. ret |= (1 << r_skb_data) | (1 << r_skb_hl);
  123. if (ctx->seen & SEEN_X)
  124. ret |= 1 << r_X;
  125. return ret;
  126. }
  127. static inline int mem_words_used(struct jit_ctx *ctx)
  128. {
  129. /* yes, we do waste some stack space IF there are "holes" in the set" */
  130. return fls(ctx->seen & SEEN_MEM);
  131. }
  132. static inline bool is_load_to_a(u16 inst)
  133. {
  134. switch (inst) {
  135. case BPF_S_LD_W_LEN:
  136. case BPF_S_LD_W_ABS:
  137. case BPF_S_LD_H_ABS:
  138. case BPF_S_LD_B_ABS:
  139. case BPF_S_ANC_CPU:
  140. case BPF_S_ANC_IFINDEX:
  141. case BPF_S_ANC_MARK:
  142. case BPF_S_ANC_PROTOCOL:
  143. case BPF_S_ANC_RXHASH:
  144. case BPF_S_ANC_VLAN_TAG:
  145. case BPF_S_ANC_VLAN_TAG_PRESENT:
  146. case BPF_S_ANC_QUEUE:
  147. return true;
  148. default:
  149. return false;
  150. }
  151. }
  152. static void build_prologue(struct jit_ctx *ctx)
  153. {
  154. u16 reg_set = saved_regs(ctx);
  155. u16 first_inst = ctx->skf->insns[0].code;
  156. u16 off;
  157. #ifdef CONFIG_FRAME_POINTER
  158. emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
  159. emit(ARM_PUSH(reg_set), ctx);
  160. emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
  161. #else
  162. if (reg_set)
  163. emit(ARM_PUSH(reg_set), ctx);
  164. #endif
  165. if (ctx->seen & (SEEN_DATA | SEEN_SKB))
  166. emit(ARM_MOV_R(r_skb, ARM_R0), ctx);
  167. if (ctx->seen & SEEN_DATA) {
  168. off = offsetof(struct sk_buff, data);
  169. emit(ARM_LDR_I(r_skb_data, r_skb, off), ctx);
  170. /* headlen = len - data_len */
  171. off = offsetof(struct sk_buff, len);
  172. emit(ARM_LDR_I(r_skb_hl, r_skb, off), ctx);
  173. off = offsetof(struct sk_buff, data_len);
  174. emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
  175. emit(ARM_SUB_R(r_skb_hl, r_skb_hl, r_scratch), ctx);
  176. }
  177. if (ctx->flags & FLAG_NEED_X_RESET)
  178. emit(ARM_MOV_I(r_X, 0), ctx);
  179. /* do not leak kernel data to userspace */
  180. if ((first_inst != BPF_S_RET_K) && !(is_load_to_a(first_inst)))
  181. emit(ARM_MOV_I(r_A, 0), ctx);
  182. /* stack space for the BPF_MEM words */
  183. if (ctx->seen & SEEN_MEM)
  184. emit(ARM_SUB_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
  185. }
  186. static void build_epilogue(struct jit_ctx *ctx)
  187. {
  188. u16 reg_set = saved_regs(ctx);