bpf_jit_32.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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);
  189. if (ctx->seen & SEEN_MEM)
  190. emit(ARM_ADD_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
  191. reg_set &= ~(1 << ARM_LR);
  192. #ifdef CONFIG_FRAME_POINTER
  193. /* the first instruction of the prologue was: mov ip, sp */
  194. reg_set &= ~(1 << ARM_IP);
  195. reg_set |= (1 << ARM_SP);
  196. emit(ARM_LDM(ARM_SP, reg_set), ctx);
  197. #else
  198. if (reg_set) {
  199. if (ctx->seen & SEEN_CALL)
  200. reg_set |= 1 << ARM_PC;
  201. emit(ARM_POP(reg_set), ctx);
  202. }
  203. if (!(ctx->seen & SEEN_CALL))
  204. emit(ARM_BX(ARM_LR), ctx);
  205. #endif
  206. }
  207. static int16_t imm8m(u32 x)
  208. {
  209. u32 rot;
  210. for (rot = 0; rot < 16; rot++)
  211. if ((x & ~ror32(0xff, 2 * rot)) == 0)
  212. return rol32(x, 2 * rot) | (rot << 8);
  213. return -1;
  214. }
  215. #if __LINUX_ARM_ARCH__ < 7
  216. static u16 imm_offset(u32 k, struct jit_ctx *ctx)
  217. {
  218. unsigned i = 0, offset;
  219. u16 imm;
  220. /* on the "fake" run we just count them (duplicates included) */
  221. if (ctx->target == NULL) {
  222. ctx->imm_count++;
  223. return 0;
  224. }
  225. while ((i < ctx->imm_count) && ctx->imms[i]) {
  226. if (ctx->imms[i] == k)
  227. break;
  228. i++;
  229. }
  230. if (ctx->imms[i] == 0)
  231. ctx->imms[i] = k;
  232. /* constants go just after the epilogue */
  233. offset = ctx->offsets[ctx->skf->len];
  234. offset += ctx->prologue_bytes;
  235. offset += ctx->epilogue_bytes;
  236. offset += i * 4;
  237. ctx->target[offset / 4] = k;
  238. /* PC in ARM mode == address of the instruction + 8 */
  239. imm = offset - (8 + ctx->idx * 4);
  240. return imm;
  241. }
  242. #endif /* __LINUX_ARM_ARCH__ */
  243. /*
  244. * Move an immediate that's not an imm8m to a core register.
  245. */
  246. static inline void emit_mov_i_no8m(int rd, u32 val, struct jit_ctx *ctx)
  247. {
  248. #if __LINUX_ARM_ARCH__ < 7
  249. emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
  250. #else
  251. emit(ARM_MOVW(rd, val & 0xffff), ctx);
  252. if (val > 0xffff)
  253. emit(ARM_MOVT(rd, val >> 16), ctx);
  254. #endif
  255. }
  256. static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx)
  257. {
  258. int imm12 = imm8m(val);
  259. if (imm12 >= 0)
  260. emit(ARM_MOV_I(rd, imm12), ctx);
  261. else
  262. emit_mov_i_no8m(rd, val, ctx);
  263. }
  264. #if __LINUX_ARM_ARCH__ < 6
  265. static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
  266. {
  267. _emit(cond, ARM_LDRB_I(ARM_R3, r_addr, 1), ctx);
  268. _emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
  269. _emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 3), ctx);
  270. _emit(cond, ARM_LSL_I(ARM_R3, ARM_R3, 16), ctx);
  271. _emit(cond, ARM_LDRB_I(ARM_R0, r_addr, 2), ctx);
  272. _emit(cond, ARM_ORR_S(ARM_R3, ARM_R3, ARM_R1, SRTYPE_LSL, 24), ctx);
  273. _emit(cond, ARM_ORR_R(ARM_R3, ARM_R3, ARM_R2), ctx);
  274. _emit(cond, ARM_ORR_S(r_res, ARM_R3, ARM_R0, SRTYPE_LSL, 8), ctx);
  275. }
  276. static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
  277. {
  278. _emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
  279. _emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 1), ctx);
  280. _emit(cond, ARM_ORR_S(r_res, ARM_R2, ARM_R1, SRTYPE_LSL, 8), ctx);
  281. }
  282. static inline void emit_swap16(u8 r_dst, u8 r_src, struct jit_ctx *ctx)
  283. {
  284. /* r_dst = (r_src << 8) | (r_src >> 8) */
  285. emit(ARM_LSL_I(ARM_R1, r_src, 8), ctx);
  286. emit(ARM_ORR_S(r_dst, ARM_R1, r_src, SRTYPE_LSR, 8), ctx);
  287. /*
  288. * we need to mask out the bits set in r_dst[23:16] due to
  289. * the first shift instruction.
  290. *
  291. * note that 0x8ff is the encoded immediate 0x00ff0000.
  292. */
  293. emit(ARM_BIC_I(r_dst, r_dst, 0x8ff), ctx);
  294. }
  295. #else /* ARMv6+ */
  296. static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
  297. {
  298. _emit(cond, ARM_LDR_I(r_res, r_addr, 0), ctx);
  299. #ifdef __LITTLE_ENDIAN
  300. _emit(cond, ARM_REV(r_res, r_res), ctx);
  301. #endif
  302. }
  303. static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
  304. {
  305. _emit(cond, ARM_LDRH_I(r_res, r_addr, 0), ctx);
  306. #ifdef __LITTLE_ENDIAN
  307. _emit(cond, ARM_REV16(r_res, r_res), ctx);
  308. #endif
  309. }
  310. static inline void emit_swap16(u8 r_dst __maybe_unused,
  311. u8 r_src __maybe_unused,
  312. struct jit_ctx *ctx __maybe_unused)
  313. {
  314. #ifdef __LITTLE_ENDIAN
  315. emit(ARM_REV16(r_dst, r_src), ctx);
  316. #endif
  317. }
  318. #endif /* __LINUX_ARM_ARCH__ < 6 */
  319. /* Compute the immediate value for a PC-relative branch. */
  320. static inline u32 b_imm(unsigned tgt, struct jit_ctx *ctx)
  321. {
  322. u32 imm;
  323. if (ctx->target == NULL)
  324. return 0;
  325. /*
  326. * BPF allows only forward jumps and the offset of the target is
  327. * still the one computed during the first pass.
  328. */
  329. imm = ctx->offsets[tgt] + ctx->prologue_bytes - (ctx->idx * 4 + 8);
  330. return imm >> 2;
  331. }
  332. #define OP_IMM3(op, r1, r2, imm_val, ctx) \
  333. do { \
  334. imm12 = imm8m(imm_val); \
  335. if (imm12 < 0) { \
  336. emit_mov_i_no8m(r_scratch, imm_val, ctx); \
  337. emit(op ## _R((r1), (r2), r_scratch), ctx); \
  338. } else { \
  339. emit(op ## _I((r1), (r2), imm12), ctx); \
  340. } \
  341. } while (0)
  342. static inline void emit_err_ret(u8 cond, struct jit_ctx *ctx)
  343. {
  344. if (ctx->ret0_fp_idx >= 0) {
  345. _emit(cond, ARM_B(b_imm(ctx->ret0_fp_idx, ctx)), ctx);
  346. /* NOP to keep the size constant between passes */
  347. emit(ARM_MOV_R(ARM_R0, ARM_R0), ctx);
  348. } else {
  349. _emit(cond, ARM_MOV_I(ARM_R0, 0), ctx);
  350. _emit(cond, ARM_B(b_imm(ctx->skf->len, ctx)), ctx);
  351. }
  352. }
  353. static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
  354. {
  355. #if __LINUX_ARM_ARCH__ < 5
  356. emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
  357. if (elf_hwcap & HWCAP_THUMB)
  358. emit(ARM_BX(tgt_reg), ctx);
  359. else
  360. emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
  361. #else
  362. emit(ARM_BLX_R(tgt_reg), ctx);
  363. #endif
  364. }
  365. static inline void emit_udiv(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx)
  366. {
  367. #if __LINUX_ARM_ARCH__ == 7
  368. if (elf_hwcap & HWCAP_IDIVA) {
  369. emit(ARM_UDIV(rd, rm, rn), ctx);
  370. return;
  371. }
  372. #endif
  373. if (rm != ARM_R0)
  374. emit(ARM_MOV_R(ARM_R0, rm), ctx);
  375. if (rn != ARM_R1)
  376. emit(ARM_MOV_R(ARM_R1, rn), ctx);
  377. ctx->seen |= SEEN_CALL;
  378. emit_mov_i(ARM_R3, (u32)jit_udiv, ctx);
  379. emit_blx_r(ARM_R3, ctx);
  380. if (rd != ARM_R0)
  381. emit(ARM_MOV_R(rd, ARM_R0), ctx);
  382. }
  383. static inline void update_on_xread(struct jit_ctx *ctx)
  384. {
  385. if (!(ctx->seen & SEEN_X))
  386. ctx->flags |= FLAG_NEED_X_RESET;
  387. ctx->seen |= SEEN_X;
  388. }
  389. static int build_body(struct jit_ctx *ctx)
  390. {
  391. void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
  392. const struct sk_filter *prog = ctx->skf;
  393. const struct sock_filter *inst;
  394. unsigned i, load_order, off, condt;
  395. int imm12;
  396. u32 k;
  397. for (i = 0; i < prog->len; i++) {
  398. inst = &(prog->insns[i]);
  399. /* K as an immediate value operand */
  400. k = inst->k;
  401. /* compute offsets only in the fake pass */
  402. if (ctx->target == NULL)
  403. ctx->offsets[i] = ctx->idx * 4;
  404. switch (inst->code) {
  405. case BPF_S_LD_IMM:
  406. emit_mov_i(r_A, k, ctx);
  407. break;
  408. case BPF_S_LD_W_LEN:
  409. ctx->seen |= SEEN_SKB;
  410. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
  411. emit(ARM_LDR_I(r_A, r_skb,
  412. offsetof(struct sk_buff, len)), ctx);
  413. break;
  414. case BPF_S_LD_MEM:
  415. /* A = scratch[k] */
  416. ctx->seen |= SEEN_MEM_WORD(k);
  417. emit(ARM_LDR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
  418. break;
  419. case BPF_S_LD_W_ABS:
  420. load_order = 2;
  421. goto load;
  422. case BPF_S_LD_H_ABS:
  423. load_order = 1;
  424. goto load;
  425. case BPF_S_LD_B_ABS:
  426. load_order = 0;
  427. load:
  428. /* the interpreter will deal with the negative K */
  429. if ((int)k < 0)
  430. return -ENOTSUPP;
  431. emit_mov_i(r_off, k, ctx);
  432. load_common:
  433. ctx->seen |= SEEN_DATA | SEEN_CALL;
  434. if (load_order > 0) {
  435. emit(ARM_SUB_I(r_scratch, r_skb_hl,
  436. 1 << load_order), ctx);
  437. emit(ARM_CMP_R(r_scratch, r_off), ctx);
  438. condt = ARM_COND_HS;
  439. } else {
  440. emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
  441. condt = ARM_COND_HI;
  442. }
  443. _emit(condt, ARM_ADD_R(r_scratch, r_off, r_skb_data),
  444. ctx);
  445. if (load_order == 0)
  446. _emit(condt, ARM_LDRB_I(r_A, r_scratch, 0),
  447. ctx);
  448. else if (load_order == 1)
  449. emit_load_be16(condt, r_A, r_scratch, ctx);
  450. else if (load_order == 2)
  451. emit_load_be32(condt, r_A, r_scratch, ctx);
  452. _emit(condt, ARM_B(b_imm(i + 1, ctx)), ctx);
  453. /* the slowpath */
  454. emit_mov_i(ARM_R3, (u32)load_func[load_order], ctx);
  455. emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
  456. /* the offset is already in R1 */
  457. emit_blx_r(ARM_R3, ctx);
  458. /* check the result of skb_copy_bits */
  459. emit(ARM_CMP_I(ARM_R1, 0), ctx);
  460. emit_err_ret(ARM_COND_NE, ctx);
  461. emit(ARM_MOV_R(r_A, ARM_R0), ctx);
  462. break;
  463. case BPF_S_LD_W_IND:
  464. load_order = 2;
  465. goto load_ind;
  466. case BPF_S_LD_H_IND:
  467. load_order = 1;
  468. goto load_ind;
  469. case BPF_S_LD_B_IND:
  470. load_order = 0;
  471. load_ind:
  472. OP_IMM3(ARM_ADD, r_off, r_X, k, ctx);
  473. goto load_common;
  474. case BPF_S_LDX_IMM:
  475. ctx->seen |= SEEN_X;
  476. emit_mov_i(r_X, k, ctx);
  477. break;
  478. case BPF_S_LDX_W_LEN:
  479. ctx->seen |= SEEN_X | SEEN_SKB;
  480. emit(ARM_LDR_I(r_X, r_skb,
  481. offsetof(struct sk_buff, len)), ctx);
  482. break;
  483. case BPF_S_LDX_MEM:
  484. ctx->seen |= SEEN_X | SEEN_MEM_WORD(k);
  485. emit(ARM_LDR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
  486. break;
  487. case BPF_S_LDX_B_MSH:
  488. /* x = ((*(frame + k)) & 0xf) << 2; */
  489. ctx->seen |= SEEN_X | SEEN_DATA | SEEN_CALL;
  490. /* the interpreter should deal with the negative K */
  491. if ((int)k < 0)
  492. return -1;
  493. /* offset in r1: we might have to take the slow path */
  494. emit_mov_i(r_off, k, ctx);
  495. emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
  496. /* load in r0: common with the slowpath */
  497. _emit(ARM_COND_HI, ARM_LDRB_R(ARM_R0, r_skb_data,
  498. ARM_R1), ctx);
  499. /*
  500. * emit_mov_i() might generate one or two instructions,
  501. * the same holds for emit_blx_r()
  502. */
  503. _emit(ARM_COND_HI, ARM_B(b_imm(i + 1, ctx) - 2), ctx);
  504. emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
  505. /* r_off is r1 */
  506. emit_mov_i(ARM_R3, (u32)jit_get_skb_b, ctx);
  507. emit_blx_r(ARM_R3, ctx);
  508. /* check the return value of skb_copy_bits */
  509. emit(ARM_CMP_I(ARM_R1, 0), ctx);
  510. emit_err_ret(ARM_COND_NE, ctx);
  511. emit(ARM_AND_I(r_X, ARM_R0, 0x00f), ctx);
  512. emit(ARM_LSL_I(r_X, r_X, 2), ctx);
  513. break;
  514. case BPF_S_ST:
  515. ctx->seen |= SEEN_MEM_WORD(k);
  516. emit(ARM_STR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
  517. break;
  518. case BPF_S_STX:
  519. update_on_xread(ctx);
  520. ctx->seen |= SEEN_MEM_WORD(k);
  521. emit(ARM_STR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
  522. break;
  523. case BPF_S_ALU_ADD_K:
  524. /* A += K */
  525. OP_IMM3(ARM_ADD, r_A, r_A, k, ctx);
  526. break;
  527. case BPF_S_ALU_ADD_X:
  528. update_on_xread(ctx);
  529. emit(ARM_ADD_R(r_A, r_A, r_X), ctx);
  530. break;
  531. case BPF_S_ALU_SUB_K:
  532. /* A -= K */
  533. OP_IMM3(ARM_SUB, r_A, r_A, k, ctx);
  534. break;
  535. case BPF_S_ALU_SUB_X:
  536. update_on_xread(ctx);
  537. emit(ARM_SUB_R(r_A, r_A, r_X), ctx);
  538. break;
  539. case BPF_S_ALU_MUL_K:
  540. /* A *= K */
  541. emit_mov_i(r_scratch, k, ctx);
  542. emit(ARM_MUL(r_A, r_A, r_scratch), ctx);
  543. break;
  544. case BPF_S_ALU_MUL_X:
  545. update_on_xread(ctx);
  546. emit(ARM_MUL(r_A, r_A, r_X), ctx);
  547. break;
  548. case BPF_S_ALU_DIV_K:
  549. /* current k == reciprocal_value(userspace k) */
  550. emit_mov_i(r_scratch, k, ctx);
  551. /* A = top 32 bits of the product */
  552. emit(ARM_UMULL(r_scratch, r_A, r_A, r_scratch), ctx);
  553. break;
  554. case BPF_S_ALU_DIV_X:
  555. update_on_xread(ctx);
  556. emit(ARM_CMP_I(r_X, 0), ctx);
  557. emit_err_ret(ARM_COND_EQ, ctx);
  558. emit_udiv(r_A, r_A, r_X, ctx);
  559. break;
  560. case BPF_S_ALU_OR_K:
  561. /* A |= K */
  562. OP_IMM3(ARM_ORR, r_A, r_A, k, ctx);
  563. break;
  564. case BPF_S_ALU_OR_X:
  565. update_on_xread(ctx);
  566. emit(ARM_ORR_R(r_A, r_A, r_X), ctx);
  567. break;
  568. case BPF_S_ALU_XOR_K:
  569. /* A ^= K; */
  570. OP_IMM3(ARM_EOR, r_A, r_A, k, ctx);
  571. break;
  572. case BPF_S_ANC_ALU_XOR_X:
  573. case BPF_S_ALU_XOR_X:
  574. /* A ^= X */
  575. update_on_xread(ctx);
  576. emit(ARM_EOR_R(r_A, r_A, r_X), ctx);
  577. break;
  578. case BPF_S_ALU_AND_K:
  579. /* A &= K */
  580. OP_IMM3(ARM_AND, r_A, r_A, k, ctx);
  581. break;
  582. case BPF_S_ALU_AND_X:
  583. update_on_xread(ctx);
  584. emit(ARM_AND_R(r_A, r_A, r_X), ctx);
  585. break;
  586. case BPF_S_ALU_LSH_K:
  587. if (unlikely(k > 31))
  588. return -1;
  589. emit(ARM_LSL_I(r_A, r_A, k), ctx);
  590. break;
  591. case BPF_S_ALU_LSH_X:
  592. update_on_xread(ctx);
  593. emit(ARM_LSL_R(r_A, r_A, r_X), ctx);
  594. break;
  595. case BPF_S_ALU_RSH_K:
  596. if (unlikely(k > 31))
  597. return -1;
  598. emit(ARM_LSR_I(r_A, r_A, k), ctx);
  599. break;
  600. case BPF_S_ALU_RSH_X:
  601. update_on_xread(ctx);
  602. emit(ARM_LSR_R(r_A, r_A, r_X), ctx);
  603. break;
  604. case BPF_S_ALU_NEG:
  605. /* A = -A */
  606. emit(ARM_RSB_I(r_A, r_A, 0), ctx);
  607. break;
  608. case BPF_S_JMP_JA:
  609. /* pc += K */
  610. emit(ARM_B(b_imm(i + k + 1, ctx)), ctx);
  611. break;
  612. case BPF_S_JMP_JEQ_K:
  613. /* pc += (A == K) ? pc->jt : pc->jf */
  614. condt = ARM_COND_EQ;
  615. goto cmp_imm;
  616. case BPF_S_JMP_JGT_K:
  617. /* pc += (A > K) ? pc->jt : pc->jf */
  618. condt = ARM_COND_HI;
  619. goto cmp_imm;
  620. case BPF_S_JMP_JGE_K:
  621. /* pc += (A >= K) ? pc->jt : pc->jf */
  622. condt = ARM_COND_HS;
  623. cmp_imm:
  624. imm12 = imm8m(k);
  625. if (imm12 < 0) {
  626. emit_mov_i_no8m(r_scratch, k, ctx);
  627. emit(ARM_CMP_R(r_A, r_scratch), ctx);
  628. } else {
  629. emit(ARM_CMP_I(r_A, imm12), ctx);
  630. }
  631. cond_jump:
  632. if (inst->jt)
  633. _emit(condt, ARM_B(b_imm(i + inst->jt + 1,
  634. ctx)), ctx);
  635. if (inst->jf)
  636. _emit(condt ^ 1, ARM_B(b_imm(i + inst->jf + 1,
  637. ctx)), ctx);
  638. break;
  639. case BPF_S_JMP_JEQ_X:
  640. /* pc += (A == X) ? pc->jt : pc->jf */
  641. condt = ARM_COND_EQ;
  642. goto cmp_x;
  643. case BPF_S_JMP_JGT_X:
  644. /* pc += (A > X) ? pc->jt : pc->jf */
  645. condt = ARM_COND_HI;
  646. goto cmp_x;
  647. case BPF_S_JMP_JGE_X:
  648. /* pc += (A >= X) ? pc->jt : pc->jf */
  649. condt = ARM_COND_CS;
  650. cmp_x:
  651. update_on_xread(ctx);
  652. emit(ARM_CMP_R(r_A, r_X), ctx);
  653. goto cond_jump;
  654. case BPF_S_JMP_JSET_K:
  655. /* pc += (A & K) ? pc->jt : pc->jf */
  656. condt = ARM_COND_NE;
  657. /* not set iff all zeroes iff Z==1 iff EQ */
  658. imm12 = imm8m(k);
  659. if (imm12 < 0) {
  660. emit_mov_i_no8m(r_scratch, k, ctx);
  661. emit(ARM_TST_R(r_A, r_scratch), ctx);
  662. } else {
  663. emit(ARM_TST_I(r_A, imm12), ctx);
  664. }
  665. goto cond_jump;
  666. case BPF_S_JMP_JSET_X:
  667. /* pc += (A & X) ? pc->jt : pc->jf */
  668. update_on_xread(ctx);
  669. condt = ARM_COND_NE;
  670. emit(ARM_TST_R(r_A, r_X), ctx);
  671. goto cond_jump;
  672. case BPF_S_RET_A:
  673. emit(ARM_MOV_R(ARM_R0, r_A), ctx);
  674. goto b_epilogue;
  675. case BPF_S_RET_K:
  676. if ((k == 0) && (ctx->ret0_fp_idx < 0))
  677. ctx->ret0_fp_idx = i;
  678. emit_mov_i(ARM_R0, k, ctx);
  679. b_epilogue:
  680. if (i != ctx->skf->len - 1)
  681. emit(ARM_B(b_imm(prog->len, ctx)), ctx);
  682. break;
  683. case BPF_S_MISC_TAX:
  684. /* X = A */
  685. ctx->seen |= SEEN_X;
  686. emit(ARM_MOV_R(r_X, r_A), ctx);
  687. break;
  688. case BPF_S_MISC_TXA:
  689. /* A = X */
  690. update_on_xread(ctx);
  691. emit(ARM_MOV_R(r_A, r_X), ctx);
  692. break;
  693. case BPF_S_ANC_PROTOCOL:
  694. /* A = ntohs(skb->protocol) */
  695. ctx->seen |= SEEN_SKB;
  696. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  697. protocol) != 2);
  698. off = offsetof(struct sk_buff, protocol);
  699. emit(ARM_LDRH_I(r_scratch, r_skb, off), ctx);
  700. emit_swap16(r_A, r_scratch, ctx);
  701. break;
  702. case BPF_S_ANC_CPU:
  703. /* r_scratch = current_thread_info() */
  704. OP_IMM3(ARM_BIC, r_scratch, ARM_SP, THREAD_SIZE - 1, ctx);
  705. /* A = current_thread_info()->cpu */
  706. BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4);
  707. off = offsetof(struct thread_info, cpu);
  708. emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
  709. break;
  710. case BPF_S_ANC_IFINDEX:
  711. /* A = skb->dev->ifindex */
  712. ctx->seen |= SEEN_SKB;
  713. off = offsetof(struct sk_buff, dev);
  714. emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
  715. emit(ARM_CMP_I(r_scratch, 0), ctx);
  716. emit_err_ret(ARM_COND_EQ, ctx);
  717. BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
  718. ifindex) != 4);
  719. off = offsetof(struct net_device, ifindex);
  720. emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
  721. break;
  722. case BPF_S_ANC_MARK:
  723. ctx->seen |= SEEN_SKB;
  724. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
  725. off = offsetof(struct sk_buff, mark);
  726. emit(ARM_LDR_I(r_A, r_skb, off), ctx);
  727. break;
  728. case BPF_S_ANC_RXHASH:
  729. ctx->seen |= SEEN_SKB;
  730. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, rxhash) != 4);
  731. off = offsetof(struct sk_buff, rxhash);
  732. emit(ARM_LDR_I(r_A, r_skb, off), ctx);
  733. break;
  734. case BPF_S_ANC_VLAN_TAG:
  735. case BPF_S_ANC_VLAN_TAG_PRESENT:
  736. ctx->seen |= SEEN_SKB;
  737. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
  738. off = offsetof(struct sk_buff, vlan_tci);
  739. emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
  740. if (inst->code == BPF_S_ANC_VLAN_TAG)
  741. OP_IMM3(ARM_AND, r_A, r_A, VLAN_VID_MASK, ctx);
  742. else
  743. OP_IMM3(ARM_AND, r_A, r_A, VLAN_TAG_PRESENT, ctx);
  744. break;
  745. case BPF_S_ANC_QUEUE:
  746. ctx->seen |= SEEN_SKB;
  747. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  748. queue_mapping) != 2);
  749. BUILD_BUG_ON(offsetof(struct sk_buff,
  750. queue_mapping) > 0xff);
  751. off = offsetof(struct sk_buff, queue_mapping);
  752. emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
  753. break;
  754. default:
  755. return -1;
  756. }
  757. }
  758. /* compute offsets only during the first pass */
  759. if (ctx->target == NULL)
  760. ctx->offsets[i] = ctx->idx * 4;
  761. return 0;
  762. }
  763. void bpf_jit_compile(struct sk_filter *fp)
  764. {
  765. struct jit_ctx ctx;
  766. unsigned tmp_idx;
  767. unsigned alloc_size;
  768. if (!bpf_jit_enable)
  769. return;
  770. memset(&ctx, 0, sizeof(ctx));
  771. ctx.skf = fp;
  772. ctx.ret0_fp_idx = -1;
  773. ctx.offsets = kzalloc(4 * (ctx.skf->len + 1), GFP_KERNEL);
  774. if (ctx.offsets == NULL)
  775. return;
  776. /* fake pass to fill in the ctx->seen */
  777. if (unlikely(build_body(&ctx)))
  778. goto out;
  779. tmp_idx = ctx.idx;
  780. build_prologue(&ctx);
  781. ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
  782. #if __LINUX_ARM_ARCH__ < 7
  783. tmp_idx = ctx.idx;
  784. build_epilogue(&ctx);
  785. ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
  786. ctx.idx += ctx.imm_count;
  787. if (ctx.imm_count) {
  788. ctx.imms = kzalloc(4 * ctx.imm_count, GFP_KERNEL);
  789. if (ctx.imms == NULL)
  790. goto out;
  791. }
  792. #else
  793. /* there's nothing after the epilogue on ARMv7 */
  794. build_epilogue(&ctx);
  795. #endif
  796. alloc_size = 4 * ctx.idx;
  797. ctx.target = module_alloc(max(sizeof(struct work_struct),
  798. alloc_size));
  799. if (unlikely(ctx.target == NULL))
  800. goto out;
  801. ctx.idx = 0;
  802. build_prologue(&ctx);
  803. build_body(&ctx);
  804. build_epilogue(&ctx);
  805. flush_icache_range((u32)ctx.target, (u32)(ctx.target + ctx.idx));
  806. #if __LINUX_ARM_ARCH__ < 7
  807. if (ctx.imm_count)
  808. kfree(ctx.imms);
  809. #endif
  810. if (bpf_jit_enable > 1)
  811. print_hex_dump(KERN_INFO, "BPF JIT code: ",
  812. DUMP_PREFIX_ADDRESS, 16, 4, ctx.target,
  813. alloc_size, false);
  814. fp->bpf_func = (void *)ctx.target;
  815. out:
  816. kfree(ctx.offsets);
  817. return;
  818. }
  819. static void bpf_jit_free_worker(struct work_struct *work)
  820. {
  821. module_free(NULL, work);
  822. }
  823. void bpf_jit_free(struct sk_filter *fp)
  824. {
  825. struct work_struct *work;
  826. if (fp->bpf_func != sk_run_filter) {
  827. work = (struct work_struct *)fp->bpf_func;
  828. INIT_WORK(work, bpf_jit_free_worker);
  829. schedule_work(work);
  830. }
  831. }