bpf_jit_32.c 22 KB

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