bpf_jit_32.c 25 KB

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