bpf_jit_32.c 22 KB

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