bpf_jit_32.c 27 KB

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