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