bpf_jit_32.c 24 KB

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