bpf_jit_32.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. /*
  2. * Just-In-Time compiler for eBPF filters on 32bit ARM
  3. *
  4. * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
  5. * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/bpf.h>
  12. #include <linux/bitops.h>
  13. #include <linux/compiler.h>
  14. #include <linux/errno.h>
  15. #include <linux/filter.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/string.h>
  18. #include <linux/slab.h>
  19. #include <linux/if_vlan.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/hwcap.h>
  22. #include <asm/opcodes.h>
  23. #include <asm/system_info.h>
  24. #include "bpf_jit_32.h"
  25. /*
  26. * eBPF prog stack layout:
  27. *
  28. * high
  29. * original ARM_SP => +-----+
  30. * | | callee saved registers
  31. * +-----+ <= (BPF_FP + SCRATCH_SIZE)
  32. * | ... | eBPF JIT scratch space
  33. * eBPF fp register => +-----+
  34. * (BPF_FP) | ... | eBPF prog stack
  35. * +-----+
  36. * |RSVD | JIT scratchpad
  37. * current ARM_SP => +-----+ <= (BPF_FP - STACK_SIZE + SCRATCH_SIZE)
  38. * | |
  39. * | ... | Function call stack
  40. * | |
  41. * +-----+
  42. * low
  43. *
  44. * The callee saved registers depends on whether frame pointers are enabled.
  45. * With frame pointers (to be compliant with the ABI):
  46. *
  47. * high
  48. * original ARM_SP => +--------------+ \
  49. * | pc | |
  50. * current ARM_FP => +--------------+ } callee saved registers
  51. * |r4-r9,fp,ip,lr| |
  52. * +--------------+ /
  53. * low
  54. *
  55. * Without frame pointers:
  56. *
  57. * high
  58. * original ARM_SP => +--------------+
  59. * | r4-r9,fp,lr | callee saved registers
  60. * current ARM_FP => +--------------+
  61. * low
  62. *
  63. * When popping registers off the stack at the end of a BPF function, we
  64. * reference them via the current ARM_FP register.
  65. */
  66. #define CALLEE_MASK (1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \
  67. 1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R9 | \
  68. 1 << ARM_FP)
  69. #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR)
  70. #define CALLEE_POP_MASK (CALLEE_MASK | 1 << ARM_PC)
  71. enum {
  72. /* Stack layout - these are offsets from (top of stack - 4) */
  73. BPF_R2_HI,
  74. BPF_R2_LO,
  75. BPF_R3_HI,
  76. BPF_R3_LO,
  77. BPF_R4_HI,
  78. BPF_R4_LO,
  79. BPF_R5_HI,
  80. BPF_R5_LO,
  81. BPF_R7_HI,
  82. BPF_R7_LO,
  83. BPF_R8_HI,
  84. BPF_R8_LO,
  85. BPF_R9_HI,
  86. BPF_R9_LO,
  87. BPF_FP_HI,
  88. BPF_FP_LO,
  89. BPF_TC_HI,
  90. BPF_TC_LO,
  91. BPF_AX_HI,
  92. BPF_AX_LO,
  93. /* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4,
  94. * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9,
  95. * BPF_REG_FP and Tail call counts.
  96. */
  97. BPF_JIT_SCRATCH_REGS,
  98. };
  99. /*
  100. * Negative "register" values indicate the register is stored on the stack
  101. * and are the offset from the top of the eBPF JIT scratch space.
  102. */
  103. #define STACK_OFFSET(k) (-4 - (k) * 4)
  104. #define SCRATCH_SIZE (BPF_JIT_SCRATCH_REGS * 4)
  105. #ifdef CONFIG_FRAME_POINTER
  106. #define EBPF_SCRATCH_TO_ARM_FP(x) ((x) - 4 * hweight16(CALLEE_PUSH_MASK) - 4)
  107. #else
  108. #define EBPF_SCRATCH_TO_ARM_FP(x) (x)
  109. #endif
  110. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0) /* TEMP Register 1 */
  111. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1) /* TEMP Register 2 */
  112. #define TCALL_CNT (MAX_BPF_JIT_REG + 2) /* Tail Call Count */
  113. #define FLAG_IMM_OVERFLOW (1 << 0)
  114. /*
  115. * Map eBPF registers to ARM 32bit registers or stack scratch space.
  116. *
  117. * 1. First argument is passed using the arm 32bit registers and rest of the
  118. * arguments are passed on stack scratch space.
  119. * 2. First callee-saved argument is mapped to arm 32 bit registers and rest
  120. * arguments are mapped to scratch space on stack.
  121. * 3. We need two 64 bit temp registers to do complex operations on eBPF
  122. * registers.
  123. *
  124. * As the eBPF registers are all 64 bit registers and arm has only 32 bit
  125. * registers, we have to map each eBPF registers with two arm 32 bit regs or
  126. * scratch memory space and we have to build eBPF 64 bit register from those.
  127. *
  128. */
  129. static const s8 bpf2a32[][2] = {
  130. /* return value from in-kernel function, and exit value from eBPF */
  131. [BPF_REG_0] = {ARM_R1, ARM_R0},
  132. /* arguments from eBPF program to in-kernel function */
  133. [BPF_REG_1] = {ARM_R3, ARM_R2},
  134. /* Stored on stack scratch space */
  135. [BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)},
  136. [BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)},
  137. [BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)},
  138. [BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)},
  139. /* callee saved registers that in-kernel function will preserve */
  140. [BPF_REG_6] = {ARM_R5, ARM_R4},
  141. /* Stored on stack scratch space */
  142. [BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
  143. [BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
  144. [BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
  145. /* Read only Frame Pointer to access Stack */
  146. [BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
  147. /* Temporary Register for internal BPF JIT, can be used
  148. * for constant blindings and others.
  149. */
  150. [TMP_REG_1] = {ARM_R7, ARM_R6},
  151. [TMP_REG_2] = {ARM_R9, ARM_R8},
  152. /* Tail call count. Stored on stack scratch space. */
  153. [TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)},
  154. /* temporary register for blinding constants.
  155. * Stored on stack scratch space.
  156. */
  157. [BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
  158. };
  159. #define dst_lo dst[1]
  160. #define dst_hi dst[0]
  161. #define src_lo src[1]
  162. #define src_hi src[0]
  163. /*
  164. * JIT Context:
  165. *
  166. * prog : bpf_prog
  167. * idx : index of current last JITed instruction.
  168. * prologue_bytes : bytes used in prologue.
  169. * epilogue_offset : offset of epilogue starting.
  170. * offsets : array of eBPF instruction offsets in
  171. * JITed code.
  172. * target : final JITed code.
  173. * epilogue_bytes : no of bytes used in epilogue.
  174. * imm_count : no of immediate counts used for global
  175. * variables.
  176. * imms : array of global variable addresses.
  177. */
  178. struct jit_ctx {
  179. const struct bpf_prog *prog;
  180. unsigned int idx;
  181. unsigned int prologue_bytes;
  182. unsigned int epilogue_offset;
  183. unsigned int cpu_architecture;
  184. u32 flags;
  185. u32 *offsets;
  186. u32 *target;
  187. u32 stack_size;
  188. #if __LINUX_ARM_ARCH__ < 7
  189. u16 epilogue_bytes;
  190. u16 imm_count;
  191. u32 *imms;
  192. #endif
  193. };
  194. /*
  195. * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
  196. * (where the assembly routines like __aeabi_uidiv could cause problems).
  197. */
  198. static u32 jit_udiv32(u32 dividend, u32 divisor)
  199. {
  200. return dividend / divisor;
  201. }
  202. static u32 jit_mod32(u32 dividend, u32 divisor)
  203. {
  204. return dividend % divisor;
  205. }
  206. static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
  207. {
  208. inst |= (cond << 28);
  209. inst = __opcode_to_mem_arm(inst);
  210. if (ctx->target != NULL)
  211. ctx->target[ctx->idx] = inst;
  212. ctx->idx++;
  213. }
  214. /*
  215. * Emit an instruction that will be executed unconditionally.
  216. */
  217. static inline void emit(u32 inst, struct jit_ctx *ctx)
  218. {
  219. _emit(ARM_COND_AL, inst, ctx);
  220. }
  221. /*
  222. * This is rather horrid, but necessary to convert an integer constant
  223. * to an immediate operand for the opcodes, and be able to detect at
  224. * build time whether the constant can't be converted (iow, usable in
  225. * BUILD_BUG_ON()).
  226. */
  227. #define imm12val(v, s) (rol32(v, (s)) | (s) << 7)
  228. #define const_imm8m(x) \
  229. ({ int r; \
  230. u32 v = (x); \
  231. if (!(v & ~0x000000ff)) \
  232. r = imm12val(v, 0); \
  233. else if (!(v & ~0xc000003f)) \
  234. r = imm12val(v, 2); \
  235. else if (!(v & ~0xf000000f)) \
  236. r = imm12val(v, 4); \
  237. else if (!(v & ~0xfc000003)) \
  238. r = imm12val(v, 6); \
  239. else if (!(v & ~0xff000000)) \
  240. r = imm12val(v, 8); \
  241. else if (!(v & ~0x3fc00000)) \
  242. r = imm12val(v, 10); \
  243. else if (!(v & ~0x0ff00000)) \
  244. r = imm12val(v, 12); \
  245. else if (!(v & ~0x03fc0000)) \
  246. r = imm12val(v, 14); \
  247. else if (!(v & ~0x00ff0000)) \
  248. r = imm12val(v, 16); \
  249. else if (!(v & ~0x003fc000)) \
  250. r = imm12val(v, 18); \
  251. else if (!(v & ~0x000ff000)) \
  252. r = imm12val(v, 20); \
  253. else if (!(v & ~0x0003fc00)) \
  254. r = imm12val(v, 22); \
  255. else if (!(v & ~0x0000ff00)) \
  256. r = imm12val(v, 24); \
  257. else if (!(v & ~0x00003fc0)) \
  258. r = imm12val(v, 26); \
  259. else if (!(v & ~0x00000ff0)) \
  260. r = imm12val(v, 28); \
  261. else if (!(v & ~0x000003fc)) \
  262. r = imm12val(v, 30); \
  263. else \
  264. r = -1; \
  265. r; })
  266. /*
  267. * Checks if immediate value can be converted to imm12(12 bits) value.
  268. */
  269. static int imm8m(u32 x)
  270. {
  271. u32 rot;
  272. for (rot = 0; rot < 16; rot++)
  273. if ((x & ~ror32(0xff, 2 * rot)) == 0)
  274. return rol32(x, 2 * rot) | (rot << 8);
  275. return -1;
  276. }
  277. #define imm8m(x) (__builtin_constant_p(x) ? const_imm8m(x) : imm8m(x))
  278. static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12)
  279. {
  280. op |= rt << 12 | rn << 16;
  281. if (imm12 >= 0)
  282. op |= ARM_INST_LDST__U;
  283. else
  284. imm12 = -imm12;
  285. return op | (imm12 & ARM_INST_LDST__IMM12);
  286. }
  287. static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8)
  288. {
  289. op |= rt << 12 | rn << 16;
  290. if (imm8 >= 0)
  291. op |= ARM_INST_LDST__U;
  292. else
  293. imm8 = -imm8;
  294. return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f);
  295. }
  296. #define ARM_LDR_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off)
  297. #define ARM_LDRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off)
  298. #define ARM_LDRD_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRD_I, rt, rn, off)
  299. #define ARM_LDRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off)
  300. #define ARM_STR_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off)
  301. #define ARM_STRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off)
  302. #define ARM_STRD_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRD_I, rt, rn, off)
  303. #define ARM_STRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off)
  304. /*
  305. * Initializes the JIT space with undefined instructions.
  306. */
  307. static void jit_fill_hole(void *area, unsigned int size)
  308. {
  309. u32 *ptr;
  310. /* We are guaranteed to have aligned memory. */
  311. for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
  312. *ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
  313. }
  314. #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
  315. /* EABI requires the stack to be aligned to 64-bit boundaries */
  316. #define STACK_ALIGNMENT 8
  317. #else
  318. /* Stack must be aligned to 32-bit boundaries */
  319. #define STACK_ALIGNMENT 4
  320. #endif
  321. /* total stack size used in JITed code */
  322. #define _STACK_SIZE (ctx->prog->aux->stack_depth + SCRATCH_SIZE)
  323. #define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
  324. #if __LINUX_ARM_ARCH__ < 7
  325. static u16 imm_offset(u32 k, struct jit_ctx *ctx)
  326. {
  327. unsigned int i = 0, offset;
  328. u16 imm;
  329. /* on the "fake" run we just count them (duplicates included) */
  330. if (ctx->target == NULL) {
  331. ctx->imm_count++;
  332. return 0;
  333. }
  334. while ((i < ctx->imm_count) && ctx->imms[i]) {
  335. if (ctx->imms[i] == k)
  336. break;
  337. i++;
  338. }
  339. if (ctx->imms[i] == 0)
  340. ctx->imms[i] = k;
  341. /* constants go just after the epilogue */
  342. offset = ctx->offsets[ctx->prog->len - 1] * 4;
  343. offset += ctx->prologue_bytes;
  344. offset += ctx->epilogue_bytes;
  345. offset += i * 4;
  346. ctx->target[offset / 4] = k;
  347. /* PC in ARM mode == address of the instruction + 8 */
  348. imm = offset - (8 + ctx->idx * 4);
  349. if (imm & ~0xfff) {
  350. /*
  351. * literal pool is too far, signal it into flags. we
  352. * can only detect it on the second pass unfortunately.
  353. */
  354. ctx->flags |= FLAG_IMM_OVERFLOW;
  355. return 0;
  356. }
  357. return imm;
  358. }
  359. #endif /* __LINUX_ARM_ARCH__ */
  360. static inline int bpf2a32_offset(int bpf_to, int bpf_from,
  361. const struct jit_ctx *ctx) {
  362. int to, from;
  363. if (ctx->target == NULL)
  364. return 0;
  365. to = ctx->offsets[bpf_to];
  366. from = ctx->offsets[bpf_from];
  367. return to - from - 1;
  368. }
  369. /*
  370. * Move an immediate that's not an imm8m to a core register.
  371. */
  372. static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
  373. {
  374. #if __LINUX_ARM_ARCH__ < 7
  375. emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
  376. #else
  377. emit(ARM_MOVW(rd, val & 0xffff), ctx);
  378. if (val > 0xffff)
  379. emit(ARM_MOVT(rd, val >> 16), ctx);
  380. #endif
  381. }
  382. static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx)
  383. {
  384. int imm12 = imm8m(val);
  385. if (imm12 >= 0)
  386. emit(ARM_MOV_I(rd, imm12), ctx);
  387. else
  388. emit_mov_i_no8m(rd, val, ctx);
  389. }
  390. static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx)
  391. {
  392. if (elf_hwcap & HWCAP_THUMB)
  393. emit(ARM_BX(tgt_reg), ctx);
  394. else
  395. emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
  396. }
  397. static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
  398. {
  399. #if __LINUX_ARM_ARCH__ < 5
  400. emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
  401. emit_bx_r(tgt_reg, ctx);
  402. #else
  403. emit(ARM_BLX_R(tgt_reg), ctx);
  404. #endif
  405. }
  406. static inline int epilogue_offset(const struct jit_ctx *ctx)
  407. {
  408. int to, from;
  409. /* No need for 1st dummy run */
  410. if (ctx->target == NULL)
  411. return 0;
  412. to = ctx->epilogue_offset;
  413. from = ctx->idx;
  414. return to - from - 2;
  415. }
  416. static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op)
  417. {
  418. const s8 *tmp = bpf2a32[TMP_REG_1];
  419. #if __LINUX_ARM_ARCH__ == 7
  420. if (elf_hwcap & HWCAP_IDIVA) {
  421. if (op == BPF_DIV)
  422. emit(ARM_UDIV(rd, rm, rn), ctx);
  423. else {
  424. emit(ARM_UDIV(ARM_IP, rm, rn), ctx);
  425. emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx);
  426. }
  427. return;
  428. }
  429. #endif
  430. /*
  431. * For BPF_ALU | BPF_DIV | BPF_K instructions
  432. * As ARM_R1 and ARM_R0 contains 1st argument of bpf
  433. * function, we need to save it on caller side to save
  434. * it from getting destroyed within callee.
  435. * After the return from the callee, we restore ARM_R0
  436. * ARM_R1.
  437. */
  438. if (rn != ARM_R1) {
  439. emit(ARM_MOV_R(tmp[0], ARM_R1), ctx);
  440. emit(ARM_MOV_R(ARM_R1, rn), ctx);
  441. }
  442. if (rm != ARM_R0) {
  443. emit(ARM_MOV_R(tmp[1], ARM_R0), ctx);
  444. emit(ARM_MOV_R(ARM_R0, rm), ctx);
  445. }
  446. /* Call appropriate function */
  447. emit_mov_i(ARM_IP, op == BPF_DIV ?
  448. (u32)jit_udiv32 : (u32)jit_mod32, ctx);
  449. emit_blx_r(ARM_IP, ctx);
  450. /* Save return value */
  451. if (rd != ARM_R0)
  452. emit(ARM_MOV_R(rd, ARM_R0), ctx);
  453. /* Restore ARM_R0 and ARM_R1 */
  454. if (rn != ARM_R1)
  455. emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx);
  456. if (rm != ARM_R0)
  457. emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx);
  458. }
  459. /* Is the translated BPF register on stack? */
  460. static bool is_stacked(s8 reg)
  461. {
  462. return reg < 0;
  463. }
  464. /* If a BPF register is on the stack (stk is true), load it to the
  465. * supplied temporary register and return the temporary register
  466. * for subsequent operations, otherwise just use the CPU register.
  467. */
  468. static s8 arm_bpf_get_reg32(s8 reg, s8 tmp, struct jit_ctx *ctx)
  469. {
  470. if (is_stacked(reg)) {
  471. emit(ARM_LDR_I(tmp, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
  472. reg = tmp;
  473. }
  474. return reg;
  475. }
  476. static const s8 *arm_bpf_get_reg64(const s8 *reg, const s8 *tmp,
  477. struct jit_ctx *ctx)
  478. {
  479. if (is_stacked(reg[1])) {
  480. if (__LINUX_ARM_ARCH__ >= 6 ||
  481. ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
  482. emit(ARM_LDRD_I(tmp[1], ARM_FP,
  483. EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
  484. } else {
  485. emit(ARM_LDR_I(tmp[1], ARM_FP,
  486. EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
  487. emit(ARM_LDR_I(tmp[0], ARM_FP,
  488. EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
  489. }
  490. reg = tmp;
  491. }
  492. return reg;
  493. }
  494. /* If a BPF register is on the stack (stk is true), save the register
  495. * back to the stack. If the source register is not the same, then
  496. * move it into the correct register.
  497. */
  498. static void arm_bpf_put_reg32(s8 reg, s8 src, struct jit_ctx *ctx)
  499. {
  500. if (is_stacked(reg))
  501. emit(ARM_STR_I(src, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
  502. else if (reg != src)
  503. emit(ARM_MOV_R(reg, src), ctx);
  504. }
  505. static void arm_bpf_put_reg64(const s8 *reg, const s8 *src,
  506. struct jit_ctx *ctx)
  507. {
  508. if (is_stacked(reg[1])) {
  509. if (__LINUX_ARM_ARCH__ >= 6 ||
  510. ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
  511. emit(ARM_STRD_I(src[1], ARM_FP,
  512. EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
  513. } else {
  514. emit(ARM_STR_I(src[1], ARM_FP,
  515. EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
  516. emit(ARM_STR_I(src[0], ARM_FP,
  517. EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
  518. }
  519. } else {
  520. if (reg[1] != src[1])
  521. emit(ARM_MOV_R(reg[1], src[1]), ctx);
  522. if (reg[0] != src[0])
  523. emit(ARM_MOV_R(reg[0], src[0]), ctx);
  524. }
  525. }
  526. static inline void emit_a32_mov_i(const s8 dst, const u32 val,
  527. struct jit_ctx *ctx)
  528. {
  529. const s8 *tmp = bpf2a32[TMP_REG_1];
  530. if (is_stacked(dst)) {
  531. emit_mov_i(tmp[1], val, ctx);
  532. arm_bpf_put_reg32(dst, tmp[1], ctx);
  533. } else {
  534. emit_mov_i(dst, val, ctx);
  535. }
  536. }
  537. static void emit_a32_mov_i64(const s8 dst[], u64 val, struct jit_ctx *ctx)
  538. {
  539. const s8 *tmp = bpf2a32[TMP_REG_1];
  540. const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
  541. emit_mov_i(rd[1], (u32)val, ctx);
  542. emit_mov_i(rd[0], val >> 32, ctx);
  543. arm_bpf_put_reg64(dst, rd, ctx);
  544. }
  545. /* Sign extended move */
  546. static inline void emit_a32_mov_se_i64(const bool is64, const s8 dst[],
  547. const u32 val, struct jit_ctx *ctx) {
  548. u64 val64 = val;
  549. if (is64 && (val & (1<<31)))
  550. val64 |= 0xffffffff00000000ULL;
  551. emit_a32_mov_i64(dst, val64, ctx);
  552. }
  553. static inline void emit_a32_add_r(const u8 dst, const u8 src,
  554. const bool is64, const bool hi,
  555. struct jit_ctx *ctx) {
  556. /* 64 bit :
  557. * adds dst_lo, dst_lo, src_lo
  558. * adc dst_hi, dst_hi, src_hi
  559. * 32 bit :
  560. * add dst_lo, dst_lo, src_lo
  561. */
  562. if (!hi && is64)
  563. emit(ARM_ADDS_R(dst, dst, src), ctx);
  564. else if (hi && is64)
  565. emit(ARM_ADC_R(dst, dst, src), ctx);
  566. else
  567. emit(ARM_ADD_R(dst, dst, src), ctx);
  568. }
  569. static inline void emit_a32_sub_r(const u8 dst, const u8 src,
  570. const bool is64, const bool hi,
  571. struct jit_ctx *ctx) {
  572. /* 64 bit :
  573. * subs dst_lo, dst_lo, src_lo
  574. * sbc dst_hi, dst_hi, src_hi
  575. * 32 bit :
  576. * sub dst_lo, dst_lo, src_lo
  577. */
  578. if (!hi && is64)
  579. emit(ARM_SUBS_R(dst, dst, src), ctx);
  580. else if (hi && is64)
  581. emit(ARM_SBC_R(dst, dst, src), ctx);
  582. else
  583. emit(ARM_SUB_R(dst, dst, src), ctx);
  584. }
  585. static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64,
  586. const bool hi, const u8 op, struct jit_ctx *ctx){
  587. switch (BPF_OP(op)) {
  588. /* dst = dst + src */
  589. case BPF_ADD:
  590. emit_a32_add_r(dst, src, is64, hi, ctx);
  591. break;
  592. /* dst = dst - src */
  593. case BPF_SUB:
  594. emit_a32_sub_r(dst, src, is64, hi, ctx);
  595. break;
  596. /* dst = dst | src */
  597. case BPF_OR:
  598. emit(ARM_ORR_R(dst, dst, src), ctx);
  599. break;
  600. /* dst = dst & src */
  601. case BPF_AND:
  602. emit(ARM_AND_R(dst, dst, src), ctx);
  603. break;
  604. /* dst = dst ^ src */
  605. case BPF_XOR:
  606. emit(ARM_EOR_R(dst, dst, src), ctx);
  607. break;
  608. /* dst = dst * src */
  609. case BPF_MUL:
  610. emit(ARM_MUL(dst, dst, src), ctx);
  611. break;
  612. /* dst = dst << src */
  613. case BPF_LSH:
  614. emit(ARM_LSL_R(dst, dst, src), ctx);
  615. break;
  616. /* dst = dst >> src */
  617. case BPF_RSH:
  618. emit(ARM_LSR_R(dst, dst, src), ctx);
  619. break;
  620. /* dst = dst >> src (signed)*/
  621. case BPF_ARSH:
  622. emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx);
  623. break;
  624. }
  625. }
  626. /* ALU operation (32 bit)
  627. * dst = dst (op) src
  628. */
  629. static inline void emit_a32_alu_r(const s8 dst, const s8 src,
  630. struct jit_ctx *ctx, const bool is64,
  631. const bool hi, const u8 op) {
  632. const s8 *tmp = bpf2a32[TMP_REG_1];
  633. s8 rn, rd;
  634. rn = arm_bpf_get_reg32(src, tmp[1], ctx);
  635. rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
  636. /* ALU operation */
  637. emit_alu_r(rd, rn, is64, hi, op, ctx);
  638. arm_bpf_put_reg32(dst, rd, ctx);
  639. }
  640. /* ALU operation (64 bit) */
  641. static inline void emit_a32_alu_r64(const bool is64, const s8 dst[],
  642. const s8 src[], struct jit_ctx *ctx,
  643. const u8 op) {
  644. const s8 *tmp = bpf2a32[TMP_REG_1];
  645. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  646. const s8 *rd;
  647. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  648. if (is64) {
  649. const s8 *rs;
  650. rs = arm_bpf_get_reg64(src, tmp2, ctx);
  651. /* ALU operation */
  652. emit_alu_r(rd[1], rs[1], true, false, op, ctx);
  653. emit_alu_r(rd[0], rs[0], true, true, op, ctx);
  654. } else {
  655. s8 rs;
  656. rs = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  657. /* ALU operation */
  658. emit_alu_r(rd[1], rs, true, false, op, ctx);
  659. emit_a32_mov_i(rd[0], 0, ctx);
  660. }
  661. arm_bpf_put_reg64(dst, rd, ctx);
  662. }
  663. /* dst = src (4 bytes)*/
  664. static inline void emit_a32_mov_r(const s8 dst, const s8 src,
  665. struct jit_ctx *ctx) {
  666. const s8 *tmp = bpf2a32[TMP_REG_1];
  667. s8 rt;
  668. rt = arm_bpf_get_reg32(src, tmp[0], ctx);
  669. arm_bpf_put_reg32(dst, rt, ctx);
  670. }
  671. /* dst = src */
  672. static inline void emit_a32_mov_r64(const bool is64, const s8 dst[],
  673. const s8 src[],
  674. struct jit_ctx *ctx) {
  675. if (!is64) {
  676. emit_a32_mov_r(dst_lo, src_lo, ctx);
  677. /* Zero out high 4 bytes */
  678. emit_a32_mov_i(dst_hi, 0, ctx);
  679. } else if (__LINUX_ARM_ARCH__ < 6 &&
  680. ctx->cpu_architecture < CPU_ARCH_ARMv5TE) {
  681. /* complete 8 byte move */
  682. emit_a32_mov_r(dst_lo, src_lo, ctx);
  683. emit_a32_mov_r(dst_hi, src_hi, ctx);
  684. } else if (is_stacked(src_lo) && is_stacked(dst_lo)) {
  685. const u8 *tmp = bpf2a32[TMP_REG_1];
  686. emit(ARM_LDRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
  687. emit(ARM_STRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
  688. } else if (is_stacked(src_lo)) {
  689. emit(ARM_LDRD_I(dst[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
  690. } else if (is_stacked(dst_lo)) {
  691. emit(ARM_STRD_I(src[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
  692. } else {
  693. emit(ARM_MOV_R(dst[0], src[0]), ctx);
  694. emit(ARM_MOV_R(dst[1], src[1]), ctx);
  695. }
  696. }
  697. /* Shift operations */
  698. static inline void emit_a32_alu_i(const s8 dst, const u32 val,
  699. struct jit_ctx *ctx, const u8 op) {
  700. const s8 *tmp = bpf2a32[TMP_REG_1];
  701. s8 rd;
  702. rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
  703. /* Do shift operation */
  704. switch (op) {
  705. case BPF_LSH:
  706. emit(ARM_LSL_I(rd, rd, val), ctx);
  707. break;
  708. case BPF_RSH:
  709. emit(ARM_LSR_I(rd, rd, val), ctx);
  710. break;
  711. case BPF_NEG:
  712. emit(ARM_RSB_I(rd, rd, val), ctx);
  713. break;
  714. }
  715. arm_bpf_put_reg32(dst, rd, ctx);
  716. }
  717. /* dst = ~dst (64 bit) */
  718. static inline void emit_a32_neg64(const s8 dst[],
  719. struct jit_ctx *ctx){
  720. const s8 *tmp = bpf2a32[TMP_REG_1];
  721. const s8 *rd;
  722. /* Setup Operand */
  723. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  724. /* Do Negate Operation */
  725. emit(ARM_RSBS_I(rd[1], rd[1], 0), ctx);
  726. emit(ARM_RSC_I(rd[0], rd[0], 0), ctx);
  727. arm_bpf_put_reg64(dst, rd, ctx);
  728. }
  729. /* dst = dst << src */
  730. static inline void emit_a32_lsh_r64(const s8 dst[], const s8 src[],
  731. struct jit_ctx *ctx) {
  732. const s8 *tmp = bpf2a32[TMP_REG_1];
  733. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  734. const s8 *rd;
  735. s8 rt;
  736. /* Setup Operands */
  737. rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  738. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  739. /* Do LSH operation */
  740. emit(ARM_SUB_I(ARM_IP, rt, 32), ctx);
  741. emit(ARM_RSB_I(tmp2[0], rt, 32), ctx);
  742. emit(ARM_MOV_SR(ARM_LR, rd[0], SRTYPE_ASL, rt), ctx);
  743. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[1], SRTYPE_ASL, ARM_IP), ctx);
  744. emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd[1], SRTYPE_LSR, tmp2[0]), ctx);
  745. emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_ASL, rt), ctx);
  746. arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
  747. arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
  748. }
  749. /* dst = dst >> src (signed)*/
  750. static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[],
  751. struct jit_ctx *ctx) {
  752. const s8 *tmp = bpf2a32[TMP_REG_1];
  753. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  754. const s8 *rd;
  755. s8 rt;
  756. /* Setup Operands */
  757. rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  758. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  759. /* Do the ARSH operation */
  760. emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
  761. emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
  762. emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
  763. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
  764. _emit(ARM_COND_MI, ARM_B(0), ctx);
  765. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx);
  766. emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx);
  767. arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
  768. arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
  769. }
  770. /* dst = dst >> src */
  771. static inline void emit_a32_rsh_r64(const s8 dst[], const s8 src[],
  772. struct jit_ctx *ctx) {
  773. const s8 *tmp = bpf2a32[TMP_REG_1];
  774. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  775. const s8 *rd;
  776. s8 rt;
  777. /* Setup Operands */
  778. rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  779. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  780. /* Do RSH operation */
  781. emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
  782. emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
  783. emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
  784. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
  785. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_LSR, tmp2[0]), ctx);
  786. emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_LSR, rt), ctx);
  787. arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
  788. arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
  789. }
  790. /* dst = dst << val */
  791. static inline void emit_a32_lsh_i64(const s8 dst[],
  792. const u32 val, struct jit_ctx *ctx){
  793. const s8 *tmp = bpf2a32[TMP_REG_1];
  794. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  795. const s8 *rd;
  796. /* Setup operands */
  797. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  798. /* Do LSH operation */
  799. if (val < 32) {
  800. emit(ARM_MOV_SI(tmp2[0], rd[0], SRTYPE_ASL, val), ctx);
  801. emit(ARM_ORR_SI(rd[0], tmp2[0], rd[1], SRTYPE_LSR, 32 - val), ctx);
  802. emit(ARM_MOV_SI(rd[1], rd[1], SRTYPE_ASL, val), ctx);
  803. } else {
  804. if (val == 32)
  805. emit(ARM_MOV_R(rd[0], rd[1]), ctx);
  806. else
  807. emit(ARM_MOV_SI(rd[0], rd[1], SRTYPE_ASL, val - 32), ctx);
  808. emit(ARM_EOR_R(rd[1], rd[1], rd[1]), ctx);
  809. }
  810. arm_bpf_put_reg64(dst, rd, ctx);
  811. }
  812. /* dst = dst >> val */
  813. static inline void emit_a32_rsh_i64(const s8 dst[],
  814. const u32 val, struct jit_ctx *ctx) {
  815. const s8 *tmp = bpf2a32[TMP_REG_1];
  816. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  817. const s8 *rd;
  818. /* Setup operands */
  819. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  820. /* Do LSR operation */
  821. if (val == 0) {
  822. /* An immediate value of 0 encodes a shift amount of 32
  823. * for LSR. To shift by 0, don't do anything.
  824. */
  825. } else if (val < 32) {
  826. emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
  827. emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
  828. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx);
  829. } else if (val == 32) {
  830. emit(ARM_MOV_R(rd[1], rd[0]), ctx);
  831. emit(ARM_MOV_I(rd[0], 0), ctx);
  832. } else {
  833. emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx);
  834. emit(ARM_MOV_I(rd[0], 0), ctx);
  835. }
  836. arm_bpf_put_reg64(dst, rd, ctx);
  837. }
  838. /* dst = dst >> val (signed) */
  839. static inline void emit_a32_arsh_i64(const s8 dst[],
  840. const u32 val, struct jit_ctx *ctx){
  841. const s8 *tmp = bpf2a32[TMP_REG_1];
  842. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  843. const s8 *rd;
  844. /* Setup operands */
  845. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  846. /* Do ARSH operation */
  847. if (val == 0) {
  848. /* An immediate value of 0 encodes a shift amount of 32
  849. * for ASR. To shift by 0, don't do anything.
  850. */
  851. } else if (val < 32) {
  852. emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
  853. emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
  854. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx);
  855. } else if (val == 32) {
  856. emit(ARM_MOV_R(rd[1], rd[0]), ctx);
  857. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
  858. } else {
  859. emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx);
  860. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
  861. }
  862. arm_bpf_put_reg64(dst, rd, ctx);
  863. }
  864. static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[],
  865. struct jit_ctx *ctx) {
  866. const s8 *tmp = bpf2a32[TMP_REG_1];
  867. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  868. const s8 *rd, *rt;
  869. /* Setup operands for multiplication */
  870. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  871. rt = arm_bpf_get_reg64(src, tmp2, ctx);
  872. /* Do Multiplication */
  873. emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx);
  874. emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx);
  875. emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
  876. emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx);
  877. emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx);
  878. arm_bpf_put_reg32(dst_lo, ARM_IP, ctx);
  879. arm_bpf_put_reg32(dst_hi, rd[0], ctx);
  880. }
  881. static bool is_ldst_imm(s16 off, const u8 size)
  882. {
  883. s16 off_max = 0;
  884. switch (size) {
  885. case BPF_B:
  886. case BPF_W:
  887. off_max = 0xfff;
  888. break;
  889. case BPF_H:
  890. off_max = 0xff;
  891. break;
  892. case BPF_DW:
  893. /* Need to make sure off+4 does not overflow. */
  894. off_max = 0xfff - 4;
  895. break;
  896. }
  897. return -off_max <= off && off <= off_max;
  898. }
  899. /* *(size *)(dst + off) = src */
  900. static inline void emit_str_r(const s8 dst, const s8 src[],
  901. s16 off, struct jit_ctx *ctx, const u8 sz){
  902. const s8 *tmp = bpf2a32[TMP_REG_1];
  903. s8 rd;
  904. rd = arm_bpf_get_reg32(dst, tmp[1], ctx);
  905. if (!is_ldst_imm(off, sz)) {
  906. emit_a32_mov_i(tmp[0], off, ctx);
  907. emit(ARM_ADD_R(tmp[0], tmp[0], rd), ctx);
  908. rd = tmp[0];
  909. off = 0;
  910. }
  911. switch (sz) {
  912. case BPF_B:
  913. /* Store a Byte */
  914. emit(ARM_STRB_I(src_lo, rd, off), ctx);
  915. break;
  916. case BPF_H:
  917. /* Store a HalfWord */
  918. emit(ARM_STRH_I(src_lo, rd, off), ctx);
  919. break;
  920. case BPF_W:
  921. /* Store a Word */
  922. emit(ARM_STR_I(src_lo, rd, off), ctx);
  923. break;
  924. case BPF_DW:
  925. /* Store a Double Word */
  926. emit(ARM_STR_I(src_lo, rd, off), ctx);
  927. emit(ARM_STR_I(src_hi, rd, off + 4), ctx);
  928. break;
  929. }
  930. }
  931. /* dst = *(size*)(src + off) */
  932. static inline void emit_ldx_r(const s8 dst[], const s8 src,
  933. s16 off, struct jit_ctx *ctx, const u8 sz){
  934. const s8 *tmp = bpf2a32[TMP_REG_1];
  935. const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
  936. s8 rm = src;
  937. if (!is_ldst_imm(off, sz)) {
  938. emit_a32_mov_i(tmp[0], off, ctx);
  939. emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
  940. rm = tmp[0];
  941. off = 0;
  942. } else if (rd[1] == rm) {
  943. emit(ARM_MOV_R(tmp[0], rm), ctx);
  944. rm = tmp[0];
  945. }
  946. switch (sz) {
  947. case BPF_B:
  948. /* Load a Byte */
  949. emit(ARM_LDRB_I(rd[1], rm, off), ctx);
  950. emit_a32_mov_i(rd[0], 0, ctx);
  951. break;
  952. case BPF_H:
  953. /* Load a HalfWord */
  954. emit(ARM_LDRH_I(rd[1], rm, off), ctx);
  955. emit_a32_mov_i(rd[0], 0, ctx);
  956. break;
  957. case BPF_W:
  958. /* Load a Word */
  959. emit(ARM_LDR_I(rd[1], rm, off), ctx);
  960. emit_a32_mov_i(rd[0], 0, ctx);
  961. break;
  962. case BPF_DW:
  963. /* Load a Double Word */
  964. emit(ARM_LDR_I(rd[1], rm, off), ctx);
  965. emit(ARM_LDR_I(rd[0], rm, off + 4), ctx);
  966. break;
  967. }
  968. arm_bpf_put_reg64(dst, rd, ctx);
  969. }
  970. /* Arithmatic Operation */
  971. static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
  972. const u8 rn, struct jit_ctx *ctx, u8 op) {
  973. switch (op) {
  974. case BPF_JSET:
  975. emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
  976. emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
  977. emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
  978. break;
  979. case BPF_JEQ:
  980. case BPF_JNE:
  981. case BPF_JGT:
  982. case BPF_JGE:
  983. case BPF_JLE:
  984. case BPF_JLT:
  985. emit(ARM_CMP_R(rd, rm), ctx);
  986. _emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
  987. break;
  988. case BPF_JSLE:
  989. case BPF_JSGT:
  990. emit(ARM_CMP_R(rn, rt), ctx);
  991. emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
  992. break;
  993. case BPF_JSLT:
  994. case BPF_JSGE:
  995. emit(ARM_CMP_R(rt, rn), ctx);
  996. emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
  997. break;
  998. }
  999. }
  1000. static int out_offset = -1; /* initialized on the first pass of build_body() */
  1001. static int emit_bpf_tail_call(struct jit_ctx *ctx)
  1002. {
  1003. /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
  1004. const s8 *r2 = bpf2a32[BPF_REG_2];
  1005. const s8 *r3 = bpf2a32[BPF_REG_3];
  1006. const s8 *tmp = bpf2a32[TMP_REG_1];
  1007. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1008. const s8 *tcc = bpf2a32[TCALL_CNT];
  1009. const s8 *tc;
  1010. const int idx0 = ctx->idx;
  1011. #define cur_offset (ctx->idx - idx0)
  1012. #define jmp_offset (out_offset - (cur_offset) - 2)
  1013. u32 lo, hi;
  1014. s8 r_array, r_index;
  1015. int off;
  1016. /* if (index >= array->map.max_entries)
  1017. * goto out;
  1018. */
  1019. BUILD_BUG_ON(offsetof(struct bpf_array, map.max_entries) >
  1020. ARM_INST_LDST__IMM12);
  1021. off = offsetof(struct bpf_array, map.max_entries);
  1022. r_array = arm_bpf_get_reg32(r2[1], tmp2[0], ctx);
  1023. /* index is 32-bit for arrays */
  1024. r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx);
  1025. /* array->map.max_entries */
  1026. emit(ARM_LDR_I(tmp[1], r_array, off), ctx);
  1027. /* index >= array->map.max_entries */
  1028. emit(ARM_CMP_R(r_index, tmp[1]), ctx);
  1029. _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
  1030. /* tmp2[0] = array, tmp2[1] = index */
  1031. /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
  1032. * goto out;
  1033. * tail_call_cnt++;
  1034. */
  1035. lo = (u32)MAX_TAIL_CALL_CNT;
  1036. hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
  1037. tc = arm_bpf_get_reg64(tcc, tmp, ctx);
  1038. emit(ARM_CMP_I(tc[0], hi), ctx);
  1039. _emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx);
  1040. _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
  1041. emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx);
  1042. emit(ARM_ADC_I(tc[0], tc[0], 0), ctx);
  1043. arm_bpf_put_reg64(tcc, tmp, ctx);
  1044. /* prog = array->ptrs[index]
  1045. * if (prog == NULL)
  1046. * goto out;
  1047. */
  1048. BUILD_BUG_ON(imm8m(offsetof(struct bpf_array, ptrs)) < 0);
  1049. off = imm8m(offsetof(struct bpf_array, ptrs));
  1050. emit(ARM_ADD_I(tmp[1], r_array, off), ctx);
  1051. emit(ARM_LDR_R_SI(tmp[1], tmp[1], r_index, SRTYPE_ASL, 2), ctx);
  1052. emit(ARM_CMP_I(tmp[1], 0), ctx);
  1053. _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
  1054. /* goto *(prog->bpf_func + prologue_size); */
  1055. BUILD_BUG_ON(offsetof(struct bpf_prog, bpf_func) >
  1056. ARM_INST_LDST__IMM12);
  1057. off = offsetof(struct bpf_prog, bpf_func);
  1058. emit(ARM_LDR_I(tmp[1], tmp[1], off), ctx);
  1059. emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
  1060. emit_bx_r(tmp[1], ctx);
  1061. /* out: */
  1062. if (out_offset == -1)
  1063. out_offset = cur_offset;
  1064. if (cur_offset != out_offset) {
  1065. pr_err_once("tail_call out_offset = %d, expected %d!\n",
  1066. cur_offset, out_offset);
  1067. return -1;
  1068. }
  1069. return 0;
  1070. #undef cur_offset
  1071. #undef jmp_offset
  1072. }
  1073. /* 0xabcd => 0xcdab */
  1074. static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
  1075. {
  1076. #if __LINUX_ARM_ARCH__ < 6
  1077. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1078. emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
  1079. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
  1080. emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
  1081. emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
  1082. #else /* ARMv6+ */
  1083. emit(ARM_REV16(rd, rn), ctx);
  1084. #endif
  1085. }
  1086. /* 0xabcdefgh => 0xghefcdab */
  1087. static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
  1088. {
  1089. #if __LINUX_ARM_ARCH__ < 6
  1090. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1091. emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
  1092. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
  1093. emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
  1094. emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
  1095. emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
  1096. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
  1097. emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
  1098. emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
  1099. emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
  1100. emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
  1101. #else /* ARMv6+ */
  1102. emit(ARM_REV(rd, rn), ctx);
  1103. #endif
  1104. }
  1105. // push the scratch stack register on top of the stack
  1106. static inline void emit_push_r64(const s8 src[], struct jit_ctx *ctx)
  1107. {
  1108. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1109. const s8 *rt;
  1110. u16 reg_set = 0;
  1111. rt = arm_bpf_get_reg64(src, tmp2, ctx);
  1112. reg_set = (1 << rt[1]) | (1 << rt[0]);
  1113. emit(ARM_PUSH(reg_set), ctx);
  1114. }
  1115. static void build_prologue(struct jit_ctx *ctx)
  1116. {
  1117. const s8 r0 = bpf2a32[BPF_REG_0][1];
  1118. const s8 r2 = bpf2a32[BPF_REG_1][1];
  1119. const s8 r3 = bpf2a32[BPF_REG_1][0];
  1120. const s8 r4 = bpf2a32[BPF_REG_6][1];
  1121. const s8 fplo = bpf2a32[BPF_REG_FP][1];
  1122. const s8 fphi = bpf2a32[BPF_REG_FP][0];
  1123. const s8 *tcc = bpf2a32[TCALL_CNT];
  1124. /* Save callee saved registers. */
  1125. #ifdef CONFIG_FRAME_POINTER
  1126. u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC;
  1127. emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
  1128. emit(ARM_PUSH(reg_set), ctx);
  1129. emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
  1130. #else
  1131. emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx);
  1132. emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx);
  1133. #endif
  1134. /* Save frame pointer for later */
  1135. emit(ARM_SUB_I(ARM_IP, ARM_SP, SCRATCH_SIZE), ctx);
  1136. ctx->stack_size = imm8m(STACK_SIZE);
  1137. /* Set up function call stack */
  1138. emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
  1139. /* Set up BPF prog stack base register */
  1140. emit_a32_mov_r(fplo, ARM_IP, ctx);
  1141. emit_a32_mov_i(fphi, 0, ctx);
  1142. /* mov r4, 0 */
  1143. emit(ARM_MOV_I(r4, 0), ctx);
  1144. /* Move BPF_CTX to BPF_R1 */
  1145. emit(ARM_MOV_R(r3, r4), ctx);
  1146. emit(ARM_MOV_R(r2, r0), ctx);
  1147. /* Initialize Tail Count */
  1148. emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[0])), ctx);
  1149. emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[1])), ctx);
  1150. /* end of prologue */
  1151. }
  1152. /* restore callee saved registers. */
  1153. static void build_epilogue(struct jit_ctx *ctx)
  1154. {
  1155. #ifdef CONFIG_FRAME_POINTER
  1156. /* When using frame pointers, some additional registers need to
  1157. * be loaded. */
  1158. u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP;
  1159. emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx);
  1160. emit(ARM_LDM(ARM_SP, reg_set), ctx);
  1161. #else
  1162. /* Restore callee saved registers. */
  1163. emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx);
  1164. emit(ARM_POP(CALLEE_POP_MASK), ctx);
  1165. #endif
  1166. }
  1167. /*
  1168. * Convert an eBPF instruction to native instruction, i.e
  1169. * JITs an eBPF instruction.
  1170. * Returns :
  1171. * 0 - Successfully JITed an 8-byte eBPF instruction
  1172. * >0 - Successfully JITed a 16-byte eBPF instruction
  1173. * <0 - Failed to JIT.
  1174. */
  1175. static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
  1176. {
  1177. const u8 code = insn->code;
  1178. const s8 *dst = bpf2a32[insn->dst_reg];
  1179. const s8 *src = bpf2a32[insn->src_reg];
  1180. const s8 *tmp = bpf2a32[TMP_REG_1];
  1181. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1182. const s16 off = insn->off;
  1183. const s32 imm = insn->imm;
  1184. const int i = insn - ctx->prog->insnsi;
  1185. const bool is64 = BPF_CLASS(code) == BPF_ALU64;
  1186. const s8 *rd, *rs;
  1187. s8 rd_lo, rt, rm, rn;
  1188. s32 jmp_offset;
  1189. #define check_imm(bits, imm) do { \
  1190. if ((imm) >= (1 << ((bits) - 1)) || \
  1191. (imm) < -(1 << ((bits) - 1))) { \
  1192. pr_info("[%2d] imm=%d(0x%x) out of range\n", \
  1193. i, imm, imm); \
  1194. return -EINVAL; \
  1195. } \
  1196. } while (0)
  1197. #define check_imm24(imm) check_imm(24, imm)
  1198. switch (code) {
  1199. /* ALU operations */
  1200. /* dst = src */
  1201. case BPF_ALU | BPF_MOV | BPF_K:
  1202. case BPF_ALU | BPF_MOV | BPF_X:
  1203. case BPF_ALU64 | BPF_MOV | BPF_K:
  1204. case BPF_ALU64 | BPF_MOV | BPF_X:
  1205. switch (BPF_SRC(code)) {
  1206. case BPF_X:
  1207. emit_a32_mov_r64(is64, dst, src, ctx);
  1208. break;
  1209. case BPF_K:
  1210. /* Sign-extend immediate value to destination reg */
  1211. emit_a32_mov_se_i64(is64, dst, imm, ctx);
  1212. break;
  1213. }
  1214. break;
  1215. /* dst = dst + src/imm */
  1216. /* dst = dst - src/imm */
  1217. /* dst = dst | src/imm */
  1218. /* dst = dst & src/imm */
  1219. /* dst = dst ^ src/imm */
  1220. /* dst = dst * src/imm */
  1221. /* dst = dst << src */
  1222. /* dst = dst >> src */
  1223. case BPF_ALU | BPF_ADD | BPF_K:
  1224. case BPF_ALU | BPF_ADD | BPF_X:
  1225. case BPF_ALU | BPF_SUB | BPF_K:
  1226. case BPF_ALU | BPF_SUB | BPF_X:
  1227. case BPF_ALU | BPF_OR | BPF_K:
  1228. case BPF_ALU | BPF_OR | BPF_X:
  1229. case BPF_ALU | BPF_AND | BPF_K:
  1230. case BPF_ALU | BPF_AND | BPF_X:
  1231. case BPF_ALU | BPF_XOR | BPF_K:
  1232. case BPF_ALU | BPF_XOR | BPF_X:
  1233. case BPF_ALU | BPF_MUL | BPF_K:
  1234. case BPF_ALU | BPF_MUL | BPF_X:
  1235. case BPF_ALU | BPF_LSH | BPF_X:
  1236. case BPF_ALU | BPF_RSH | BPF_X:
  1237. case BPF_ALU | BPF_ARSH | BPF_K:
  1238. case BPF_ALU | BPF_ARSH | BPF_X:
  1239. case BPF_ALU64 | BPF_ADD | BPF_K:
  1240. case BPF_ALU64 | BPF_ADD | BPF_X:
  1241. case BPF_ALU64 | BPF_SUB | BPF_K:
  1242. case BPF_ALU64 | BPF_SUB | BPF_X:
  1243. case BPF_ALU64 | BPF_OR | BPF_K:
  1244. case BPF_ALU64 | BPF_OR | BPF_X:
  1245. case BPF_ALU64 | BPF_AND | BPF_K:
  1246. case BPF_ALU64 | BPF_AND | BPF_X:
  1247. case BPF_ALU64 | BPF_XOR | BPF_K:
  1248. case BPF_ALU64 | BPF_XOR | BPF_X:
  1249. switch (BPF_SRC(code)) {
  1250. case BPF_X:
  1251. emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code));
  1252. break;
  1253. case BPF_K:
  1254. /* Move immediate value to the temporary register
  1255. * and then do the ALU operation on the temporary
  1256. * register as this will sign-extend the immediate
  1257. * value into temporary reg and then it would be
  1258. * safe to do the operation on it.
  1259. */
  1260. emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
  1261. emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code));
  1262. break;
  1263. }
  1264. break;
  1265. /* dst = dst / src(imm) */
  1266. /* dst = dst % src(imm) */
  1267. case BPF_ALU | BPF_DIV | BPF_K:
  1268. case BPF_ALU | BPF_DIV | BPF_X:
  1269. case BPF_ALU | BPF_MOD | BPF_K:
  1270. case BPF_ALU | BPF_MOD | BPF_X:
  1271. rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx);
  1272. switch (BPF_SRC(code)) {
  1273. case BPF_X:
  1274. rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx);
  1275. break;
  1276. case BPF_K:
  1277. rt = tmp2[0];
  1278. emit_a32_mov_i(rt, imm, ctx);
  1279. break;
  1280. default:
  1281. rt = src_lo;
  1282. break;
  1283. }
  1284. emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code));
  1285. arm_bpf_put_reg32(dst_lo, rd_lo, ctx);
  1286. emit_a32_mov_i(dst_hi, 0, ctx);
  1287. break;
  1288. case BPF_ALU64 | BPF_DIV | BPF_K:
  1289. case BPF_ALU64 | BPF_DIV | BPF_X:
  1290. case BPF_ALU64 | BPF_MOD | BPF_K:
  1291. case BPF_ALU64 | BPF_MOD | BPF_X:
  1292. goto notyet;
  1293. /* dst = dst >> imm */
  1294. /* dst = dst << imm */
  1295. case BPF_ALU | BPF_RSH | BPF_K:
  1296. case BPF_ALU | BPF_LSH | BPF_K:
  1297. if (unlikely(imm > 31))
  1298. return -EINVAL;
  1299. if (imm)
  1300. emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code));
  1301. emit_a32_mov_i(dst_hi, 0, ctx);
  1302. break;
  1303. /* dst = dst << imm */
  1304. case BPF_ALU64 | BPF_LSH | BPF_K:
  1305. if (unlikely(imm > 63))
  1306. return -EINVAL;
  1307. emit_a32_lsh_i64(dst, imm, ctx);
  1308. break;
  1309. /* dst = dst >> imm */
  1310. case BPF_ALU64 | BPF_RSH | BPF_K:
  1311. if (unlikely(imm > 63))
  1312. return -EINVAL;
  1313. emit_a32_rsh_i64(dst, imm, ctx);
  1314. break;
  1315. /* dst = dst << src */
  1316. case BPF_ALU64 | BPF_LSH | BPF_X:
  1317. emit_a32_lsh_r64(dst, src, ctx);
  1318. break;
  1319. /* dst = dst >> src */
  1320. case BPF_ALU64 | BPF_RSH | BPF_X:
  1321. emit_a32_rsh_r64(dst, src, ctx);
  1322. break;
  1323. /* dst = dst >> src (signed) */
  1324. case BPF_ALU64 | BPF_ARSH | BPF_X:
  1325. emit_a32_arsh_r64(dst, src, ctx);
  1326. break;
  1327. /* dst = dst >> imm (signed) */
  1328. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1329. if (unlikely(imm > 63))
  1330. return -EINVAL;
  1331. emit_a32_arsh_i64(dst, imm, ctx);
  1332. break;
  1333. /* dst = ~dst */
  1334. case BPF_ALU | BPF_NEG:
  1335. emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code));
  1336. emit_a32_mov_i(dst_hi, 0, ctx);
  1337. break;
  1338. /* dst = ~dst (64 bit) */
  1339. case BPF_ALU64 | BPF_NEG:
  1340. emit_a32_neg64(dst, ctx);
  1341. break;
  1342. /* dst = dst * src/imm */
  1343. case BPF_ALU64 | BPF_MUL | BPF_X:
  1344. case BPF_ALU64 | BPF_MUL | BPF_K:
  1345. switch (BPF_SRC(code)) {
  1346. case BPF_X:
  1347. emit_a32_mul_r64(dst, src, ctx);
  1348. break;
  1349. case BPF_K:
  1350. /* Move immediate value to the temporary register
  1351. * and then do the multiplication on it as this
  1352. * will sign-extend the immediate value into temp
  1353. * reg then it would be safe to do the operation
  1354. * on it.
  1355. */
  1356. emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
  1357. emit_a32_mul_r64(dst, tmp2, ctx);
  1358. break;
  1359. }
  1360. break;
  1361. /* dst = htole(dst) */
  1362. /* dst = htobe(dst) */
  1363. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1364. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1365. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  1366. if (BPF_SRC(code) == BPF_FROM_LE)
  1367. goto emit_bswap_uxt;
  1368. switch (imm) {
  1369. case 16:
  1370. emit_rev16(rd[1], rd[1], ctx);
  1371. goto emit_bswap_uxt;
  1372. case 32:
  1373. emit_rev32(rd[1], rd[1], ctx);
  1374. goto emit_bswap_uxt;
  1375. case 64:
  1376. emit_rev32(ARM_LR, rd[1], ctx);
  1377. emit_rev32(rd[1], rd[0], ctx);
  1378. emit(ARM_MOV_R(rd[0], ARM_LR), ctx);
  1379. break;
  1380. }
  1381. goto exit;
  1382. emit_bswap_uxt:
  1383. switch (imm) {
  1384. case 16:
  1385. /* zero-extend 16 bits into 64 bits */
  1386. #if __LINUX_ARM_ARCH__ < 6
  1387. emit_a32_mov_i(tmp2[1], 0xffff, ctx);
  1388. emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx);
  1389. #else /* ARMv6+ */
  1390. emit(ARM_UXTH(rd[1], rd[1]), ctx);
  1391. #endif
  1392. emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
  1393. break;
  1394. case 32:
  1395. /* zero-extend 32 bits into 64 bits */
  1396. emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
  1397. break;
  1398. case 64:
  1399. /* nop */
  1400. break;
  1401. }
  1402. exit:
  1403. arm_bpf_put_reg64(dst, rd, ctx);
  1404. break;
  1405. /* dst = imm64 */
  1406. case BPF_LD | BPF_IMM | BPF_DW:
  1407. {
  1408. u64 val = (u32)imm | (u64)insn[1].imm << 32;
  1409. emit_a32_mov_i64(dst, val, ctx);
  1410. return 1;
  1411. }
  1412. /* LDX: dst = *(size *)(src + off) */
  1413. case BPF_LDX | BPF_MEM | BPF_W:
  1414. case BPF_LDX | BPF_MEM | BPF_H:
  1415. case BPF_LDX | BPF_MEM | BPF_B:
  1416. case BPF_LDX | BPF_MEM | BPF_DW:
  1417. rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  1418. emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code));
  1419. break;
  1420. /* ST: *(size *)(dst + off) = imm */
  1421. case BPF_ST | BPF_MEM | BPF_W:
  1422. case BPF_ST | BPF_MEM | BPF_H:
  1423. case BPF_ST | BPF_MEM | BPF_B:
  1424. case BPF_ST | BPF_MEM | BPF_DW:
  1425. switch (BPF_SIZE(code)) {
  1426. case BPF_DW:
  1427. /* Sign-extend immediate value into temp reg */
  1428. emit_a32_mov_se_i64(true, tmp2, imm, ctx);
  1429. break;
  1430. case BPF_W:
  1431. case BPF_H:
  1432. case BPF_B:
  1433. emit_a32_mov_i(tmp2[1], imm, ctx);
  1434. break;
  1435. }
  1436. emit_str_r(dst_lo, tmp2, off, ctx, BPF_SIZE(code));
  1437. break;
  1438. /* STX XADD: lock *(u32 *)(dst + off) += src */
  1439. case BPF_STX | BPF_XADD | BPF_W:
  1440. /* STX XADD: lock *(u64 *)(dst + off) += src */
  1441. case BPF_STX | BPF_XADD | BPF_DW:
  1442. goto notyet;
  1443. /* STX: *(size *)(dst + off) = src */
  1444. case BPF_STX | BPF_MEM | BPF_W:
  1445. case BPF_STX | BPF_MEM | BPF_H:
  1446. case BPF_STX | BPF_MEM | BPF_B:
  1447. case BPF_STX | BPF_MEM | BPF_DW:
  1448. rs = arm_bpf_get_reg64(src, tmp2, ctx);
  1449. emit_str_r(dst_lo, rs, off, ctx, BPF_SIZE(code));
  1450. break;
  1451. /* PC += off if dst == src */
  1452. /* PC += off if dst > src */
  1453. /* PC += off if dst >= src */
  1454. /* PC += off if dst < src */
  1455. /* PC += off if dst <= src */
  1456. /* PC += off if dst != src */
  1457. /* PC += off if dst > src (signed) */
  1458. /* PC += off if dst >= src (signed) */
  1459. /* PC += off if dst < src (signed) */
  1460. /* PC += off if dst <= src (signed) */
  1461. /* PC += off if dst & src */
  1462. case BPF_JMP | BPF_JEQ | BPF_X:
  1463. case BPF_JMP | BPF_JGT | BPF_X:
  1464. case BPF_JMP | BPF_JGE | BPF_X:
  1465. case BPF_JMP | BPF_JNE | BPF_X:
  1466. case BPF_JMP | BPF_JSGT | BPF_X:
  1467. case BPF_JMP | BPF_JSGE | BPF_X:
  1468. case BPF_JMP | BPF_JSET | BPF_X:
  1469. case BPF_JMP | BPF_JLE | BPF_X:
  1470. case BPF_JMP | BPF_JLT | BPF_X:
  1471. case BPF_JMP | BPF_JSLT | BPF_X:
  1472. case BPF_JMP | BPF_JSLE | BPF_X:
  1473. /* Setup source registers */
  1474. rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx);
  1475. rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  1476. goto go_jmp;
  1477. /* PC += off if dst == imm */
  1478. /* PC += off if dst > imm */
  1479. /* PC += off if dst >= imm */
  1480. /* PC += off if dst < imm */
  1481. /* PC += off if dst <= imm */
  1482. /* PC += off if dst != imm */
  1483. /* PC += off if dst > imm (signed) */
  1484. /* PC += off if dst >= imm (signed) */
  1485. /* PC += off if dst < imm (signed) */
  1486. /* PC += off if dst <= imm (signed) */
  1487. /* PC += off if dst & imm */
  1488. case BPF_JMP | BPF_JEQ | BPF_K:
  1489. case BPF_JMP | BPF_JGT | BPF_K:
  1490. case BPF_JMP | BPF_JGE | BPF_K:
  1491. case BPF_JMP | BPF_JNE | BPF_K:
  1492. case BPF_JMP | BPF_JSGT | BPF_K:
  1493. case BPF_JMP | BPF_JSGE | BPF_K:
  1494. case BPF_JMP | BPF_JSET | BPF_K:
  1495. case BPF_JMP | BPF_JLT | BPF_K:
  1496. case BPF_JMP | BPF_JLE | BPF_K:
  1497. case BPF_JMP | BPF_JSLT | BPF_K:
  1498. case BPF_JMP | BPF_JSLE | BPF_K:
  1499. if (off == 0)
  1500. break;
  1501. rm = tmp2[0];
  1502. rn = tmp2[1];
  1503. /* Sign-extend immediate value */
  1504. emit_a32_mov_se_i64(true, tmp2, imm, ctx);
  1505. go_jmp:
  1506. /* Setup destination register */
  1507. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  1508. /* Check for the condition */
  1509. emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code));
  1510. /* Setup JUMP instruction */
  1511. jmp_offset = bpf2a32_offset(i+off, i, ctx);
  1512. switch (BPF_OP(code)) {
  1513. case BPF_JNE:
  1514. case BPF_JSET:
  1515. _emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
  1516. break;
  1517. case BPF_JEQ:
  1518. _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
  1519. break;
  1520. case BPF_JGT:
  1521. _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
  1522. break;
  1523. case BPF_JGE:
  1524. _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
  1525. break;
  1526. case BPF_JSGT:
  1527. _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
  1528. break;
  1529. case BPF_JSGE:
  1530. _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
  1531. break;
  1532. case BPF_JLE:
  1533. _emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
  1534. break;
  1535. case BPF_JLT:
  1536. _emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
  1537. break;
  1538. case BPF_JSLT:
  1539. _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
  1540. break;
  1541. case BPF_JSLE:
  1542. _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
  1543. break;
  1544. }
  1545. break;
  1546. /* JMP OFF */
  1547. case BPF_JMP | BPF_JA:
  1548. {
  1549. if (off == 0)
  1550. break;
  1551. jmp_offset = bpf2a32_offset(i+off, i, ctx);
  1552. check_imm24(jmp_offset);
  1553. emit(ARM_B(jmp_offset), ctx);
  1554. break;
  1555. }
  1556. /* tail call */
  1557. case BPF_JMP | BPF_TAIL_CALL:
  1558. if (emit_bpf_tail_call(ctx))
  1559. return -EFAULT;
  1560. break;
  1561. /* function call */
  1562. case BPF_JMP | BPF_CALL:
  1563. {
  1564. const s8 *r0 = bpf2a32[BPF_REG_0];
  1565. const s8 *r1 = bpf2a32[BPF_REG_1];
  1566. const s8 *r2 = bpf2a32[BPF_REG_2];
  1567. const s8 *r3 = bpf2a32[BPF_REG_3];
  1568. const s8 *r4 = bpf2a32[BPF_REG_4];
  1569. const s8 *r5 = bpf2a32[BPF_REG_5];
  1570. const u32 func = (u32)__bpf_call_base + (u32)imm;
  1571. emit_a32_mov_r64(true, r0, r1, ctx);
  1572. emit_a32_mov_r64(true, r1, r2, ctx);
  1573. emit_push_r64(r5, ctx);
  1574. emit_push_r64(r4, ctx);
  1575. emit_push_r64(r3, ctx);
  1576. emit_a32_mov_i(tmp[1], func, ctx);
  1577. emit_blx_r(tmp[1], ctx);
  1578. emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
  1579. break;
  1580. }
  1581. /* function return */
  1582. case BPF_JMP | BPF_EXIT:
  1583. /* Optimization: when last instruction is EXIT
  1584. * simply fallthrough to epilogue.
  1585. */
  1586. if (i == ctx->prog->len - 1)
  1587. break;
  1588. jmp_offset = epilogue_offset(ctx);
  1589. check_imm24(jmp_offset);
  1590. emit(ARM_B(jmp_offset), ctx);
  1591. break;
  1592. notyet:
  1593. pr_info_once("*** NOT YET: opcode %02x ***\n", code);
  1594. return -EFAULT;
  1595. default:
  1596. pr_err_once("unknown opcode %02x\n", code);
  1597. return -EINVAL;
  1598. }
  1599. if (ctx->flags & FLAG_IMM_OVERFLOW)
  1600. /*
  1601. * this instruction generated an overflow when
  1602. * trying to access the literal pool, so
  1603. * delegate this filter to the kernel interpreter.
  1604. */
  1605. return -1;
  1606. return 0;
  1607. }
  1608. static int build_body(struct jit_ctx *ctx)
  1609. {
  1610. const struct bpf_prog *prog = ctx->prog;
  1611. unsigned int i;
  1612. for (i = 0; i < prog->len; i++) {
  1613. const struct bpf_insn *insn = &(prog->insnsi[i]);
  1614. int ret;
  1615. ret = build_insn(insn, ctx);
  1616. /* It's used with loading the 64 bit immediate value. */
  1617. if (ret > 0) {
  1618. i++;
  1619. if (ctx->target == NULL)
  1620. ctx->offsets[i] = ctx->idx;
  1621. continue;
  1622. }
  1623. if (ctx->target == NULL)
  1624. ctx->offsets[i] = ctx->idx;
  1625. /* If unsuccesfull, return with error code */
  1626. if (ret)
  1627. return ret;
  1628. }
  1629. return 0;
  1630. }
  1631. static int validate_code(struct jit_ctx *ctx)
  1632. {
  1633. int i;
  1634. for (i = 0; i < ctx->idx; i++) {
  1635. if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
  1636. return -1;
  1637. }
  1638. return 0;
  1639. }
  1640. void bpf_jit_compile(struct bpf_prog *prog)
  1641. {
  1642. /* Nothing to do here. We support Internal BPF. */
  1643. }
  1644. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1645. {
  1646. struct bpf_prog *tmp, *orig_prog = prog;
  1647. struct bpf_binary_header *header;
  1648. bool tmp_blinded = false;
  1649. struct jit_ctx ctx;
  1650. unsigned int tmp_idx;
  1651. unsigned int image_size;
  1652. u8 *image_ptr;
  1653. /* If BPF JIT was not enabled then we must fall back to
  1654. * the interpreter.
  1655. */
  1656. if (!prog->jit_requested)
  1657. return orig_prog;
  1658. /* If constant blinding was enabled and we failed during blinding
  1659. * then we must fall back to the interpreter. Otherwise, we save
  1660. * the new JITed code.
  1661. */
  1662. tmp = bpf_jit_blind_constants(prog);
  1663. if (IS_ERR(tmp))
  1664. return orig_prog;
  1665. if (tmp != prog) {
  1666. tmp_blinded = true;
  1667. prog = tmp;
  1668. }
  1669. memset(&ctx, 0, sizeof(ctx));
  1670. ctx.prog = prog;
  1671. ctx.cpu_architecture = cpu_architecture();
  1672. /* Not able to allocate memory for offsets[] , then
  1673. * we must fall back to the interpreter
  1674. */
  1675. ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
  1676. if (ctx.offsets == NULL) {
  1677. prog = orig_prog;
  1678. goto out;
  1679. }
  1680. /* 1) fake pass to find in the length of the JITed code,
  1681. * to compute ctx->offsets and other context variables
  1682. * needed to compute final JITed code.
  1683. * Also, calculate random starting pointer/start of JITed code
  1684. * which is prefixed by random number of fault instructions.
  1685. *
  1686. * If the first pass fails then there is no chance of it
  1687. * being successful in the second pass, so just fall back
  1688. * to the interpreter.
  1689. */
  1690. if (build_body(&ctx)) {
  1691. prog = orig_prog;
  1692. goto out_off;
  1693. }
  1694. tmp_idx = ctx.idx;
  1695. build_prologue(&ctx);
  1696. ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
  1697. ctx.epilogue_offset = ctx.idx;
  1698. #if __LINUX_ARM_ARCH__ < 7
  1699. tmp_idx = ctx.idx;
  1700. build_epilogue(&ctx);
  1701. ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
  1702. ctx.idx += ctx.imm_count;
  1703. if (ctx.imm_count) {
  1704. ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
  1705. if (ctx.imms == NULL) {
  1706. prog = orig_prog;
  1707. goto out_off;
  1708. }
  1709. }
  1710. #else
  1711. /* there's nothing about the epilogue on ARMv7 */
  1712. build_epilogue(&ctx);
  1713. #endif
  1714. /* Now we can get the actual image size of the JITed arm code.
  1715. * Currently, we are not considering the THUMB-2 instructions
  1716. * for jit, although it can decrease the size of the image.
  1717. *
  1718. * As each arm instruction is of length 32bit, we are translating
  1719. * number of JITed intructions into the size required to store these
  1720. * JITed code.
  1721. */
  1722. image_size = sizeof(u32) * ctx.idx;
  1723. /* Now we know the size of the structure to make */
  1724. header = bpf_jit_binary_alloc(image_size, &image_ptr,
  1725. sizeof(u32), jit_fill_hole);
  1726. /* Not able to allocate memory for the structure then
  1727. * we must fall back to the interpretation
  1728. */
  1729. if (header == NULL) {
  1730. prog = orig_prog;
  1731. goto out_imms;
  1732. }
  1733. /* 2.) Actual pass to generate final JIT code */
  1734. ctx.target = (u32 *) image_ptr;
  1735. ctx.idx = 0;
  1736. build_prologue(&ctx);
  1737. /* If building the body of the JITed code fails somehow,
  1738. * we fall back to the interpretation.
  1739. */
  1740. if (build_body(&ctx) < 0) {
  1741. image_ptr = NULL;
  1742. bpf_jit_binary_free(header);
  1743. prog = orig_prog;
  1744. goto out_imms;
  1745. }
  1746. build_epilogue(&ctx);
  1747. /* 3.) Extra pass to validate JITed Code */
  1748. if (validate_code(&ctx)) {
  1749. image_ptr = NULL;
  1750. bpf_jit_binary_free(header);
  1751. prog = orig_prog;
  1752. goto out_imms;
  1753. }
  1754. flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
  1755. if (bpf_jit_enable > 1)
  1756. /* there are 2 passes here */
  1757. bpf_jit_dump(prog->len, image_size, 2, ctx.target);
  1758. bpf_jit_binary_lock_ro(header);
  1759. prog->bpf_func = (void *)ctx.target;
  1760. prog->jited = 1;
  1761. prog->jited_len = image_size;
  1762. out_imms:
  1763. #if __LINUX_ARM_ARCH__ < 7
  1764. if (ctx.imm_count)
  1765. kfree(ctx.imms);
  1766. #endif
  1767. out_off:
  1768. kfree(ctx.offsets);
  1769. out:
  1770. if (tmp_blinded)
  1771. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  1772. tmp : orig_prog);
  1773. return prog;
  1774. }