bpf_jit_32.c 22 KB

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