bpf_jit_32.c 23 KB

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