bpf_jit_32.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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 < 32) {
  822. emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
  823. emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
  824. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx);
  825. } else if (val == 32) {
  826. emit(ARM_MOV_R(rd[1], rd[0]), ctx);
  827. emit(ARM_MOV_I(rd[0], 0), ctx);
  828. } else {
  829. emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx);
  830. emit(ARM_MOV_I(rd[0], 0), ctx);
  831. }
  832. arm_bpf_put_reg64(dst, rd, ctx);
  833. }
  834. /* dst = dst >> val (signed) */
  835. static inline void emit_a32_arsh_i64(const s8 dst[],
  836. const u32 val, struct jit_ctx *ctx){
  837. const s8 *tmp = bpf2a32[TMP_REG_1];
  838. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  839. const s8 *rd;
  840. /* Setup operands */
  841. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  842. /* Do ARSH operation */
  843. if (val < 32) {
  844. emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
  845. emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
  846. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx);
  847. } else if (val == 32) {
  848. emit(ARM_MOV_R(rd[1], rd[0]), ctx);
  849. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
  850. } else {
  851. emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx);
  852. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
  853. }
  854. arm_bpf_put_reg64(dst, rd, ctx);
  855. }
  856. static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[],
  857. struct jit_ctx *ctx) {
  858. const s8 *tmp = bpf2a32[TMP_REG_1];
  859. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  860. const s8 *rd, *rt;
  861. /* Setup operands for multiplication */
  862. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  863. rt = arm_bpf_get_reg64(src, tmp2, ctx);
  864. /* Do Multiplication */
  865. emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx);
  866. emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx);
  867. emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
  868. emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx);
  869. emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx);
  870. arm_bpf_put_reg32(dst_lo, ARM_IP, ctx);
  871. arm_bpf_put_reg32(dst_hi, rd[0], ctx);
  872. }
  873. /* *(size *)(dst + off) = src */
  874. static inline void emit_str_r(const s8 dst, const s8 src[],
  875. s32 off, struct jit_ctx *ctx, const u8 sz){
  876. const s8 *tmp = bpf2a32[TMP_REG_1];
  877. s32 off_max;
  878. s8 rd;
  879. rd = arm_bpf_get_reg32(dst, tmp[1], ctx);
  880. if (sz == BPF_H)
  881. off_max = 0xff;
  882. else
  883. off_max = 0xfff;
  884. if (off < 0 || off > off_max) {
  885. emit_a32_mov_i(tmp[0], off, ctx);
  886. emit(ARM_ADD_R(tmp[0], tmp[0], rd), ctx);
  887. rd = tmp[0];
  888. off = 0;
  889. }
  890. switch (sz) {
  891. case BPF_B:
  892. /* Store a Byte */
  893. emit(ARM_STRB_I(src_lo, rd, off), ctx);
  894. break;
  895. case BPF_H:
  896. /* Store a HalfWord */
  897. emit(ARM_STRH_I(src_lo, rd, off), ctx);
  898. break;
  899. case BPF_W:
  900. /* Store a Word */
  901. emit(ARM_STR_I(src_lo, rd, off), ctx);
  902. break;
  903. case BPF_DW:
  904. /* Store a Double Word */
  905. emit(ARM_STR_I(src_lo, rd, off), ctx);
  906. emit(ARM_STR_I(src_hi, rd, off + 4), ctx);
  907. break;
  908. }
  909. }
  910. /* dst = *(size*)(src + off) */
  911. static inline void emit_ldx_r(const s8 dst[], const s8 src,
  912. s32 off, struct jit_ctx *ctx, const u8 sz){
  913. const s8 *tmp = bpf2a32[TMP_REG_1];
  914. const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
  915. s8 rm = src;
  916. s32 off_max;
  917. if (sz == BPF_H)
  918. off_max = 0xff;
  919. else
  920. off_max = 0xfff;
  921. if (off < 0 || off > off_max) {
  922. emit_a32_mov_i(tmp[0], off, ctx);
  923. emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
  924. rm = tmp[0];
  925. off = 0;
  926. } else if (rd[1] == rm) {
  927. emit(ARM_MOV_R(tmp[0], rm), ctx);
  928. rm = tmp[0];
  929. }
  930. switch (sz) {
  931. case BPF_B:
  932. /* Load a Byte */
  933. emit(ARM_LDRB_I(rd[1], rm, off), ctx);
  934. emit_a32_mov_i(rd[0], 0, ctx);
  935. break;
  936. case BPF_H:
  937. /* Load a HalfWord */
  938. emit(ARM_LDRH_I(rd[1], rm, off), ctx);
  939. emit_a32_mov_i(rd[0], 0, ctx);
  940. break;
  941. case BPF_W:
  942. /* Load a Word */
  943. emit(ARM_LDR_I(rd[1], rm, off), ctx);
  944. emit_a32_mov_i(rd[0], 0, ctx);
  945. break;
  946. case BPF_DW:
  947. /* Load a Double Word */
  948. emit(ARM_LDR_I(rd[1], rm, off), ctx);
  949. emit(ARM_LDR_I(rd[0], rm, off + 4), ctx);
  950. break;
  951. }
  952. arm_bpf_put_reg64(dst, rd, ctx);
  953. }
  954. /* Arithmatic Operation */
  955. static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
  956. const u8 rn, struct jit_ctx *ctx, u8 op) {
  957. switch (op) {
  958. case BPF_JSET:
  959. emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
  960. emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
  961. emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
  962. break;
  963. case BPF_JEQ:
  964. case BPF_JNE:
  965. case BPF_JGT:
  966. case BPF_JGE:
  967. case BPF_JLE:
  968. case BPF_JLT:
  969. emit(ARM_CMP_R(rd, rm), ctx);
  970. _emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
  971. break;
  972. case BPF_JSLE:
  973. case BPF_JSGT:
  974. emit(ARM_CMP_R(rn, rt), ctx);
  975. emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
  976. break;
  977. case BPF_JSLT:
  978. case BPF_JSGE:
  979. emit(ARM_CMP_R(rt, rn), ctx);
  980. emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
  981. break;
  982. }
  983. }
  984. static int out_offset = -1; /* initialized on the first pass of build_body() */
  985. static int emit_bpf_tail_call(struct jit_ctx *ctx)
  986. {
  987. /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
  988. const s8 *r2 = bpf2a32[BPF_REG_2];
  989. const s8 *r3 = bpf2a32[BPF_REG_3];
  990. const s8 *tmp = bpf2a32[TMP_REG_1];
  991. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  992. const s8 *tcc = bpf2a32[TCALL_CNT];
  993. const s8 *tc;
  994. const int idx0 = ctx->idx;
  995. #define cur_offset (ctx->idx - idx0)
  996. #define jmp_offset (out_offset - (cur_offset) - 2)
  997. u32 lo, hi;
  998. s8 r_array, r_index;
  999. int off;
  1000. /* if (index >= array->map.max_entries)
  1001. * goto out;
  1002. */
  1003. BUILD_BUG_ON(offsetof(struct bpf_array, map.max_entries) >
  1004. ARM_INST_LDST__IMM12);
  1005. off = offsetof(struct bpf_array, map.max_entries);
  1006. r_array = arm_bpf_get_reg32(r2[1], tmp2[0], ctx);
  1007. /* index is 32-bit for arrays */
  1008. r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx);
  1009. /* array->map.max_entries */
  1010. emit(ARM_LDR_I(tmp[1], r_array, off), ctx);
  1011. /* index >= array->map.max_entries */
  1012. emit(ARM_CMP_R(r_index, tmp[1]), ctx);
  1013. _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
  1014. /* tmp2[0] = array, tmp2[1] = index */
  1015. /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
  1016. * goto out;
  1017. * tail_call_cnt++;
  1018. */
  1019. lo = (u32)MAX_TAIL_CALL_CNT;
  1020. hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
  1021. tc = arm_bpf_get_reg64(tcc, tmp, ctx);
  1022. emit(ARM_CMP_I(tc[0], hi), ctx);
  1023. _emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx);
  1024. _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
  1025. emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx);
  1026. emit(ARM_ADC_I(tc[0], tc[0], 0), ctx);
  1027. arm_bpf_put_reg64(tcc, tmp, ctx);
  1028. /* prog = array->ptrs[index]
  1029. * if (prog == NULL)
  1030. * goto out;
  1031. */
  1032. BUILD_BUG_ON(imm8m(offsetof(struct bpf_array, ptrs)) < 0);
  1033. off = imm8m(offsetof(struct bpf_array, ptrs));
  1034. emit(ARM_ADD_I(tmp[1], r_array, off), ctx);
  1035. emit(ARM_LDR_R_SI(tmp[1], tmp[1], r_index, SRTYPE_ASL, 2), ctx);
  1036. emit(ARM_CMP_I(tmp[1], 0), ctx);
  1037. _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
  1038. /* goto *(prog->bpf_func + prologue_size); */
  1039. BUILD_BUG_ON(offsetof(struct bpf_prog, bpf_func) >
  1040. ARM_INST_LDST__IMM12);
  1041. off = offsetof(struct bpf_prog, bpf_func);
  1042. emit(ARM_LDR_I(tmp[1], tmp[1], off), ctx);
  1043. emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
  1044. emit_bx_r(tmp[1], ctx);
  1045. /* out: */
  1046. if (out_offset == -1)
  1047. out_offset = cur_offset;
  1048. if (cur_offset != out_offset) {
  1049. pr_err_once("tail_call out_offset = %d, expected %d!\n",
  1050. cur_offset, out_offset);
  1051. return -1;
  1052. }
  1053. return 0;
  1054. #undef cur_offset
  1055. #undef jmp_offset
  1056. }
  1057. /* 0xabcd => 0xcdab */
  1058. static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
  1059. {
  1060. #if __LINUX_ARM_ARCH__ < 6
  1061. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1062. emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
  1063. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
  1064. emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
  1065. emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
  1066. #else /* ARMv6+ */
  1067. emit(ARM_REV16(rd, rn), ctx);
  1068. #endif
  1069. }
  1070. /* 0xabcdefgh => 0xghefcdab */
  1071. static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
  1072. {
  1073. #if __LINUX_ARM_ARCH__ < 6
  1074. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1075. emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
  1076. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
  1077. emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
  1078. emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
  1079. emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
  1080. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
  1081. emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
  1082. emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
  1083. emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
  1084. emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
  1085. #else /* ARMv6+ */
  1086. emit(ARM_REV(rd, rn), ctx);
  1087. #endif
  1088. }
  1089. // push the scratch stack register on top of the stack
  1090. static inline void emit_push_r64(const s8 src[], struct jit_ctx *ctx)
  1091. {
  1092. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1093. const s8 *rt;
  1094. u16 reg_set = 0;
  1095. rt = arm_bpf_get_reg64(src, tmp2, ctx);
  1096. reg_set = (1 << rt[1]) | (1 << rt[0]);
  1097. emit(ARM_PUSH(reg_set), ctx);
  1098. }
  1099. static void build_prologue(struct jit_ctx *ctx)
  1100. {
  1101. const s8 r0 = bpf2a32[BPF_REG_0][1];
  1102. const s8 r2 = bpf2a32[BPF_REG_1][1];
  1103. const s8 r3 = bpf2a32[BPF_REG_1][0];
  1104. const s8 r4 = bpf2a32[BPF_REG_6][1];
  1105. const s8 fplo = bpf2a32[BPF_REG_FP][1];
  1106. const s8 fphi = bpf2a32[BPF_REG_FP][0];
  1107. const s8 *tcc = bpf2a32[TCALL_CNT];
  1108. /* Save callee saved registers. */
  1109. #ifdef CONFIG_FRAME_POINTER
  1110. u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC;
  1111. emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
  1112. emit(ARM_PUSH(reg_set), ctx);
  1113. emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
  1114. #else
  1115. emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx);
  1116. emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx);
  1117. #endif
  1118. /* Save frame pointer for later */
  1119. emit(ARM_SUB_I(ARM_IP, ARM_SP, SCRATCH_SIZE), ctx);
  1120. ctx->stack_size = imm8m(STACK_SIZE);
  1121. /* Set up function call stack */
  1122. emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
  1123. /* Set up BPF prog stack base register */
  1124. emit_a32_mov_r(fplo, ARM_IP, ctx);
  1125. emit_a32_mov_i(fphi, 0, ctx);
  1126. /* mov r4, 0 */
  1127. emit(ARM_MOV_I(r4, 0), ctx);
  1128. /* Move BPF_CTX to BPF_R1 */
  1129. emit(ARM_MOV_R(r3, r4), ctx);
  1130. emit(ARM_MOV_R(r2, r0), ctx);
  1131. /* Initialize Tail Count */
  1132. emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[0])), ctx);
  1133. emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[1])), ctx);
  1134. /* end of prologue */
  1135. }
  1136. /* restore callee saved registers. */
  1137. static void build_epilogue(struct jit_ctx *ctx)
  1138. {
  1139. #ifdef CONFIG_FRAME_POINTER
  1140. /* When using frame pointers, some additional registers need to
  1141. * be loaded. */
  1142. u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP;
  1143. emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx);
  1144. emit(ARM_LDM(ARM_SP, reg_set), ctx);
  1145. #else
  1146. /* Restore callee saved registers. */
  1147. emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx);
  1148. emit(ARM_POP(CALLEE_POP_MASK), ctx);
  1149. #endif
  1150. }
  1151. /*
  1152. * Convert an eBPF instruction to native instruction, i.e
  1153. * JITs an eBPF instruction.
  1154. * Returns :
  1155. * 0 - Successfully JITed an 8-byte eBPF instruction
  1156. * >0 - Successfully JITed a 16-byte eBPF instruction
  1157. * <0 - Failed to JIT.
  1158. */
  1159. static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
  1160. {
  1161. const u8 code = insn->code;
  1162. const s8 *dst = bpf2a32[insn->dst_reg];
  1163. const s8 *src = bpf2a32[insn->src_reg];
  1164. const s8 *tmp = bpf2a32[TMP_REG_1];
  1165. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1166. const s16 off = insn->off;
  1167. const s32 imm = insn->imm;
  1168. const int i = insn - ctx->prog->insnsi;
  1169. const bool is64 = BPF_CLASS(code) == BPF_ALU64;
  1170. const s8 *rd, *rs;
  1171. s8 rd_lo, rt, rm, rn;
  1172. s32 jmp_offset;
  1173. #define check_imm(bits, imm) do { \
  1174. if ((imm) >= (1 << ((bits) - 1)) || \
  1175. (imm) < -(1 << ((bits) - 1))) { \
  1176. pr_info("[%2d] imm=%d(0x%x) out of range\n", \
  1177. i, imm, imm); \
  1178. return -EINVAL; \
  1179. } \
  1180. } while (0)
  1181. #define check_imm24(imm) check_imm(24, imm)
  1182. switch (code) {
  1183. /* ALU operations */
  1184. /* dst = src */
  1185. case BPF_ALU | BPF_MOV | BPF_K:
  1186. case BPF_ALU | BPF_MOV | BPF_X:
  1187. case BPF_ALU64 | BPF_MOV | BPF_K:
  1188. case BPF_ALU64 | BPF_MOV | BPF_X:
  1189. switch (BPF_SRC(code)) {
  1190. case BPF_X:
  1191. emit_a32_mov_r64(is64, dst, src, ctx);
  1192. break;
  1193. case BPF_K:
  1194. /* Sign-extend immediate value to destination reg */
  1195. emit_a32_mov_se_i64(is64, dst, imm, ctx);
  1196. break;
  1197. }
  1198. break;
  1199. /* dst = dst + src/imm */
  1200. /* dst = dst - src/imm */
  1201. /* dst = dst | src/imm */
  1202. /* dst = dst & src/imm */
  1203. /* dst = dst ^ src/imm */
  1204. /* dst = dst * src/imm */
  1205. /* dst = dst << src */
  1206. /* dst = dst >> src */
  1207. case BPF_ALU | BPF_ADD | BPF_K:
  1208. case BPF_ALU | BPF_ADD | BPF_X:
  1209. case BPF_ALU | BPF_SUB | BPF_K:
  1210. case BPF_ALU | BPF_SUB | BPF_X:
  1211. case BPF_ALU | BPF_OR | BPF_K:
  1212. case BPF_ALU | BPF_OR | BPF_X:
  1213. case BPF_ALU | BPF_AND | BPF_K:
  1214. case BPF_ALU | BPF_AND | BPF_X:
  1215. case BPF_ALU | BPF_XOR | BPF_K:
  1216. case BPF_ALU | BPF_XOR | BPF_X:
  1217. case BPF_ALU | BPF_MUL | BPF_K:
  1218. case BPF_ALU | BPF_MUL | BPF_X:
  1219. case BPF_ALU | BPF_LSH | BPF_X:
  1220. case BPF_ALU | BPF_RSH | BPF_X:
  1221. case BPF_ALU | BPF_ARSH | BPF_K:
  1222. case BPF_ALU | BPF_ARSH | BPF_X:
  1223. case BPF_ALU64 | BPF_ADD | BPF_K:
  1224. case BPF_ALU64 | BPF_ADD | BPF_X:
  1225. case BPF_ALU64 | BPF_SUB | BPF_K:
  1226. case BPF_ALU64 | BPF_SUB | BPF_X:
  1227. case BPF_ALU64 | BPF_OR | BPF_K:
  1228. case BPF_ALU64 | BPF_OR | BPF_X:
  1229. case BPF_ALU64 | BPF_AND | BPF_K:
  1230. case BPF_ALU64 | BPF_AND | BPF_X:
  1231. case BPF_ALU64 | BPF_XOR | BPF_K:
  1232. case BPF_ALU64 | BPF_XOR | BPF_X:
  1233. switch (BPF_SRC(code)) {
  1234. case BPF_X:
  1235. emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code));
  1236. break;
  1237. case BPF_K:
  1238. /* Move immediate value to the temporary register
  1239. * and then do the ALU operation on the temporary
  1240. * register as this will sign-extend the immediate
  1241. * value into temporary reg and then it would be
  1242. * safe to do the operation on it.
  1243. */
  1244. emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
  1245. emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code));
  1246. break;
  1247. }
  1248. break;
  1249. /* dst = dst / src(imm) */
  1250. /* dst = dst % src(imm) */
  1251. case BPF_ALU | BPF_DIV | BPF_K:
  1252. case BPF_ALU | BPF_DIV | BPF_X:
  1253. case BPF_ALU | BPF_MOD | BPF_K:
  1254. case BPF_ALU | BPF_MOD | BPF_X:
  1255. rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx);
  1256. switch (BPF_SRC(code)) {
  1257. case BPF_X:
  1258. rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx);
  1259. break;
  1260. case BPF_K:
  1261. rt = tmp2[0];
  1262. emit_a32_mov_i(rt, imm, ctx);
  1263. break;
  1264. default:
  1265. rt = src_lo;
  1266. break;
  1267. }
  1268. emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code));
  1269. arm_bpf_put_reg32(dst_lo, rd_lo, ctx);
  1270. emit_a32_mov_i(dst_hi, 0, ctx);
  1271. break;
  1272. case BPF_ALU64 | BPF_DIV | BPF_K:
  1273. case BPF_ALU64 | BPF_DIV | BPF_X:
  1274. case BPF_ALU64 | BPF_MOD | BPF_K:
  1275. case BPF_ALU64 | BPF_MOD | BPF_X:
  1276. goto notyet;
  1277. /* dst = dst >> imm */
  1278. /* dst = dst << imm */
  1279. case BPF_ALU | BPF_RSH | BPF_K:
  1280. case BPF_ALU | BPF_LSH | BPF_K:
  1281. if (unlikely(imm > 31))
  1282. return -EINVAL;
  1283. if (imm)
  1284. emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code));
  1285. emit_a32_mov_i(dst_hi, 0, ctx);
  1286. break;
  1287. /* dst = dst << imm */
  1288. case BPF_ALU64 | BPF_LSH | BPF_K:
  1289. if (unlikely(imm > 63))
  1290. return -EINVAL;
  1291. emit_a32_lsh_i64(dst, imm, ctx);
  1292. break;
  1293. /* dst = dst >> imm */
  1294. case BPF_ALU64 | BPF_RSH | BPF_K:
  1295. if (unlikely(imm > 63))
  1296. return -EINVAL;
  1297. emit_a32_rsh_i64(dst, imm, ctx);
  1298. break;
  1299. /* dst = dst << src */
  1300. case BPF_ALU64 | BPF_LSH | BPF_X:
  1301. emit_a32_lsh_r64(dst, src, ctx);
  1302. break;
  1303. /* dst = dst >> src */
  1304. case BPF_ALU64 | BPF_RSH | BPF_X:
  1305. emit_a32_rsh_r64(dst, src, ctx);
  1306. break;
  1307. /* dst = dst >> src (signed) */
  1308. case BPF_ALU64 | BPF_ARSH | BPF_X:
  1309. emit_a32_arsh_r64(dst, src, ctx);
  1310. break;
  1311. /* dst = dst >> imm (signed) */
  1312. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1313. if (unlikely(imm > 63))
  1314. return -EINVAL;
  1315. emit_a32_arsh_i64(dst, imm, ctx);
  1316. break;
  1317. /* dst = ~dst */
  1318. case BPF_ALU | BPF_NEG:
  1319. emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code));
  1320. emit_a32_mov_i(dst_hi, 0, ctx);
  1321. break;
  1322. /* dst = ~dst (64 bit) */
  1323. case BPF_ALU64 | BPF_NEG:
  1324. emit_a32_neg64(dst, ctx);
  1325. break;
  1326. /* dst = dst * src/imm */
  1327. case BPF_ALU64 | BPF_MUL | BPF_X:
  1328. case BPF_ALU64 | BPF_MUL | BPF_K:
  1329. switch (BPF_SRC(code)) {
  1330. case BPF_X:
  1331. emit_a32_mul_r64(dst, src, ctx);
  1332. break;
  1333. case BPF_K:
  1334. /* Move immediate value to the temporary register
  1335. * and then do the multiplication on it as this
  1336. * will sign-extend the immediate value into temp
  1337. * reg then it would be safe to do the operation
  1338. * on it.
  1339. */
  1340. emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
  1341. emit_a32_mul_r64(dst, tmp2, ctx);
  1342. break;
  1343. }
  1344. break;
  1345. /* dst = htole(dst) */
  1346. /* dst = htobe(dst) */
  1347. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1348. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1349. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  1350. if (BPF_SRC(code) == BPF_FROM_LE)
  1351. goto emit_bswap_uxt;
  1352. switch (imm) {
  1353. case 16:
  1354. emit_rev16(rd[1], rd[1], ctx);
  1355. goto emit_bswap_uxt;
  1356. case 32:
  1357. emit_rev32(rd[1], rd[1], ctx);
  1358. goto emit_bswap_uxt;
  1359. case 64:
  1360. emit_rev32(ARM_LR, rd[1], ctx);
  1361. emit_rev32(rd[1], rd[0], ctx);
  1362. emit(ARM_MOV_R(rd[0], ARM_LR), ctx);
  1363. break;
  1364. }
  1365. goto exit;
  1366. emit_bswap_uxt:
  1367. switch (imm) {
  1368. case 16:
  1369. /* zero-extend 16 bits into 64 bits */
  1370. #if __LINUX_ARM_ARCH__ < 6
  1371. emit_a32_mov_i(tmp2[1], 0xffff, ctx);
  1372. emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx);
  1373. #else /* ARMv6+ */
  1374. emit(ARM_UXTH(rd[1], rd[1]), ctx);
  1375. #endif
  1376. emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
  1377. break;
  1378. case 32:
  1379. /* zero-extend 32 bits into 64 bits */
  1380. emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
  1381. break;
  1382. case 64:
  1383. /* nop */
  1384. break;
  1385. }
  1386. exit:
  1387. arm_bpf_put_reg64(dst, rd, ctx);
  1388. break;
  1389. /* dst = imm64 */
  1390. case BPF_LD | BPF_IMM | BPF_DW:
  1391. {
  1392. u64 val = (u32)imm | (u64)insn[1].imm << 32;
  1393. emit_a32_mov_i64(dst, val, ctx);
  1394. return 1;
  1395. }
  1396. /* LDX: dst = *(size *)(src + off) */
  1397. case BPF_LDX | BPF_MEM | BPF_W:
  1398. case BPF_LDX | BPF_MEM | BPF_H:
  1399. case BPF_LDX | BPF_MEM | BPF_B:
  1400. case BPF_LDX | BPF_MEM | BPF_DW:
  1401. rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  1402. emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code));
  1403. break;
  1404. /* ST: *(size *)(dst + off) = imm */
  1405. case BPF_ST | BPF_MEM | BPF_W:
  1406. case BPF_ST | BPF_MEM | BPF_H:
  1407. case BPF_ST | BPF_MEM | BPF_B:
  1408. case BPF_ST | BPF_MEM | BPF_DW:
  1409. switch (BPF_SIZE(code)) {
  1410. case BPF_DW:
  1411. /* Sign-extend immediate value into temp reg */
  1412. emit_a32_mov_se_i64(true, tmp2, imm, ctx);
  1413. break;
  1414. case BPF_W:
  1415. case BPF_H:
  1416. case BPF_B:
  1417. emit_a32_mov_i(tmp2[1], imm, ctx);
  1418. break;
  1419. }
  1420. emit_str_r(dst_lo, tmp2, off, ctx, BPF_SIZE(code));
  1421. break;
  1422. /* STX XADD: lock *(u32 *)(dst + off) += src */
  1423. case BPF_STX | BPF_XADD | BPF_W:
  1424. /* STX XADD: lock *(u64 *)(dst + off) += src */
  1425. case BPF_STX | BPF_XADD | BPF_DW:
  1426. goto notyet;
  1427. /* STX: *(size *)(dst + off) = src */
  1428. case BPF_STX | BPF_MEM | BPF_W:
  1429. case BPF_STX | BPF_MEM | BPF_H:
  1430. case BPF_STX | BPF_MEM | BPF_B:
  1431. case BPF_STX | BPF_MEM | BPF_DW:
  1432. rs = arm_bpf_get_reg64(src, tmp2, ctx);
  1433. emit_str_r(dst_lo, rs, off, ctx, BPF_SIZE(code));
  1434. break;
  1435. /* PC += off if dst == src */
  1436. /* PC += off if dst > src */
  1437. /* PC += off if dst >= src */
  1438. /* PC += off if dst < src */
  1439. /* PC += off if dst <= src */
  1440. /* PC += off if dst != src */
  1441. /* PC += off if dst > src (signed) */
  1442. /* PC += off if dst >= src (signed) */
  1443. /* PC += off if dst < src (signed) */
  1444. /* PC += off if dst <= src (signed) */
  1445. /* PC += off if dst & src */
  1446. case BPF_JMP | BPF_JEQ | BPF_X:
  1447. case BPF_JMP | BPF_JGT | BPF_X:
  1448. case BPF_JMP | BPF_JGE | BPF_X:
  1449. case BPF_JMP | BPF_JNE | BPF_X:
  1450. case BPF_JMP | BPF_JSGT | BPF_X:
  1451. case BPF_JMP | BPF_JSGE | BPF_X:
  1452. case BPF_JMP | BPF_JSET | BPF_X:
  1453. case BPF_JMP | BPF_JLE | BPF_X:
  1454. case BPF_JMP | BPF_JLT | BPF_X:
  1455. case BPF_JMP | BPF_JSLT | BPF_X:
  1456. case BPF_JMP | BPF_JSLE | BPF_X:
  1457. /* Setup source registers */
  1458. rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx);
  1459. rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  1460. goto go_jmp;
  1461. /* PC += off if dst == imm */
  1462. /* PC += off if dst > imm */
  1463. /* PC += off if dst >= imm */
  1464. /* PC += off if dst < imm */
  1465. /* PC += off if dst <= imm */
  1466. /* PC += off if dst != imm */
  1467. /* PC += off if dst > imm (signed) */
  1468. /* PC += off if dst >= imm (signed) */
  1469. /* PC += off if dst < imm (signed) */
  1470. /* PC += off if dst <= imm (signed) */
  1471. /* PC += off if dst & imm */
  1472. case BPF_JMP | BPF_JEQ | BPF_K:
  1473. case BPF_JMP | BPF_JGT | BPF_K:
  1474. case BPF_JMP | BPF_JGE | BPF_K:
  1475. case BPF_JMP | BPF_JNE | BPF_K:
  1476. case BPF_JMP | BPF_JSGT | BPF_K:
  1477. case BPF_JMP | BPF_JSGE | BPF_K:
  1478. case BPF_JMP | BPF_JSET | BPF_K:
  1479. case BPF_JMP | BPF_JLT | BPF_K:
  1480. case BPF_JMP | BPF_JLE | BPF_K:
  1481. case BPF_JMP | BPF_JSLT | BPF_K:
  1482. case BPF_JMP | BPF_JSLE | BPF_K:
  1483. if (off == 0)
  1484. break;
  1485. rm = tmp2[0];
  1486. rn = tmp2[1];
  1487. /* Sign-extend immediate value */
  1488. emit_a32_mov_se_i64(true, tmp2, imm, ctx);
  1489. go_jmp:
  1490. /* Setup destination register */
  1491. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  1492. /* Check for the condition */
  1493. emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code));
  1494. /* Setup JUMP instruction */
  1495. jmp_offset = bpf2a32_offset(i+off, i, ctx);
  1496. switch (BPF_OP(code)) {
  1497. case BPF_JNE:
  1498. case BPF_JSET:
  1499. _emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
  1500. break;
  1501. case BPF_JEQ:
  1502. _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
  1503. break;
  1504. case BPF_JGT:
  1505. _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
  1506. break;
  1507. case BPF_JGE:
  1508. _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
  1509. break;
  1510. case BPF_JSGT:
  1511. _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
  1512. break;
  1513. case BPF_JSGE:
  1514. _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
  1515. break;
  1516. case BPF_JLE:
  1517. _emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
  1518. break;
  1519. case BPF_JLT:
  1520. _emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
  1521. break;
  1522. case BPF_JSLT:
  1523. _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
  1524. break;
  1525. case BPF_JSLE:
  1526. _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
  1527. break;
  1528. }
  1529. break;
  1530. /* JMP OFF */
  1531. case BPF_JMP | BPF_JA:
  1532. {
  1533. if (off == 0)
  1534. break;
  1535. jmp_offset = bpf2a32_offset(i+off, i, ctx);
  1536. check_imm24(jmp_offset);
  1537. emit(ARM_B(jmp_offset), ctx);
  1538. break;
  1539. }
  1540. /* tail call */
  1541. case BPF_JMP | BPF_TAIL_CALL:
  1542. if (emit_bpf_tail_call(ctx))
  1543. return -EFAULT;
  1544. break;
  1545. /* function call */
  1546. case BPF_JMP | BPF_CALL:
  1547. {
  1548. const s8 *r0 = bpf2a32[BPF_REG_0];
  1549. const s8 *r1 = bpf2a32[BPF_REG_1];
  1550. const s8 *r2 = bpf2a32[BPF_REG_2];
  1551. const s8 *r3 = bpf2a32[BPF_REG_3];
  1552. const s8 *r4 = bpf2a32[BPF_REG_4];
  1553. const s8 *r5 = bpf2a32[BPF_REG_5];
  1554. const u32 func = (u32)__bpf_call_base + (u32)imm;
  1555. emit_a32_mov_r64(true, r0, r1, ctx);
  1556. emit_a32_mov_r64(true, r1, r2, ctx);
  1557. emit_push_r64(r5, ctx);
  1558. emit_push_r64(r4, ctx);
  1559. emit_push_r64(r3, ctx);
  1560. emit_a32_mov_i(tmp[1], func, ctx);
  1561. emit_blx_r(tmp[1], ctx);
  1562. emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
  1563. break;
  1564. }
  1565. /* function return */
  1566. case BPF_JMP | BPF_EXIT:
  1567. /* Optimization: when last instruction is EXIT
  1568. * simply fallthrough to epilogue.
  1569. */
  1570. if (i == ctx->prog->len - 1)
  1571. break;
  1572. jmp_offset = epilogue_offset(ctx);
  1573. check_imm24(jmp_offset);
  1574. emit(ARM_B(jmp_offset), ctx);
  1575. break;
  1576. notyet:
  1577. pr_info_once("*** NOT YET: opcode %02x ***\n", code);
  1578. return -EFAULT;
  1579. default:
  1580. pr_err_once("unknown opcode %02x\n", code);
  1581. return -EINVAL;
  1582. }
  1583. if (ctx->flags & FLAG_IMM_OVERFLOW)
  1584. /*
  1585. * this instruction generated an overflow when
  1586. * trying to access the literal pool, so
  1587. * delegate this filter to the kernel interpreter.
  1588. */
  1589. return -1;
  1590. return 0;
  1591. }
  1592. static int build_body(struct jit_ctx *ctx)
  1593. {
  1594. const struct bpf_prog *prog = ctx->prog;
  1595. unsigned int i;
  1596. for (i = 0; i < prog->len; i++) {
  1597. const struct bpf_insn *insn = &(prog->insnsi[i]);
  1598. int ret;
  1599. ret = build_insn(insn, ctx);
  1600. /* It's used with loading the 64 bit immediate value. */
  1601. if (ret > 0) {
  1602. i++;
  1603. if (ctx->target == NULL)
  1604. ctx->offsets[i] = ctx->idx;
  1605. continue;
  1606. }
  1607. if (ctx->target == NULL)
  1608. ctx->offsets[i] = ctx->idx;
  1609. /* If unsuccesfull, return with error code */
  1610. if (ret)
  1611. return ret;
  1612. }
  1613. return 0;
  1614. }
  1615. static int validate_code(struct jit_ctx *ctx)
  1616. {
  1617. int i;
  1618. for (i = 0; i < ctx->idx; i++) {
  1619. if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
  1620. return -1;
  1621. }
  1622. return 0;
  1623. }
  1624. void bpf_jit_compile(struct bpf_prog *prog)
  1625. {
  1626. /* Nothing to do here. We support Internal BPF. */
  1627. }
  1628. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1629. {
  1630. struct bpf_prog *tmp, *orig_prog = prog;
  1631. struct bpf_binary_header *header;
  1632. bool tmp_blinded = false;
  1633. struct jit_ctx ctx;
  1634. unsigned int tmp_idx;
  1635. unsigned int image_size;
  1636. u8 *image_ptr;
  1637. /* If BPF JIT was not enabled then we must fall back to
  1638. * the interpreter.
  1639. */
  1640. if (!prog->jit_requested)
  1641. return orig_prog;
  1642. /* If constant blinding was enabled and we failed during blinding
  1643. * then we must fall back to the interpreter. Otherwise, we save
  1644. * the new JITed code.
  1645. */
  1646. tmp = bpf_jit_blind_constants(prog);
  1647. if (IS_ERR(tmp))
  1648. return orig_prog;
  1649. if (tmp != prog) {
  1650. tmp_blinded = true;
  1651. prog = tmp;
  1652. }
  1653. memset(&ctx, 0, sizeof(ctx));
  1654. ctx.prog = prog;
  1655. ctx.cpu_architecture = cpu_architecture();
  1656. /* Not able to allocate memory for offsets[] , then
  1657. * we must fall back to the interpreter
  1658. */
  1659. ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
  1660. if (ctx.offsets == NULL) {
  1661. prog = orig_prog;
  1662. goto out;
  1663. }
  1664. /* 1) fake pass to find in the length of the JITed code,
  1665. * to compute ctx->offsets and other context variables
  1666. * needed to compute final JITed code.
  1667. * Also, calculate random starting pointer/start of JITed code
  1668. * which is prefixed by random number of fault instructions.
  1669. *
  1670. * If the first pass fails then there is no chance of it
  1671. * being successful in the second pass, so just fall back
  1672. * to the interpreter.
  1673. */
  1674. if (build_body(&ctx)) {
  1675. prog = orig_prog;
  1676. goto out_off;
  1677. }
  1678. tmp_idx = ctx.idx;
  1679. build_prologue(&ctx);
  1680. ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
  1681. ctx.epilogue_offset = ctx.idx;
  1682. #if __LINUX_ARM_ARCH__ < 7
  1683. tmp_idx = ctx.idx;
  1684. build_epilogue(&ctx);
  1685. ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
  1686. ctx.idx += ctx.imm_count;
  1687. if (ctx.imm_count) {
  1688. ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
  1689. if (ctx.imms == NULL) {
  1690. prog = orig_prog;
  1691. goto out_off;
  1692. }
  1693. }
  1694. #else
  1695. /* there's nothing about the epilogue on ARMv7 */
  1696. build_epilogue(&ctx);
  1697. #endif
  1698. /* Now we can get the actual image size of the JITed arm code.
  1699. * Currently, we are not considering the THUMB-2 instructions
  1700. * for jit, although it can decrease the size of the image.
  1701. *
  1702. * As each arm instruction is of length 32bit, we are translating
  1703. * number of JITed intructions into the size required to store these
  1704. * JITed code.
  1705. */
  1706. image_size = sizeof(u32) * ctx.idx;
  1707. /* Now we know the size of the structure to make */
  1708. header = bpf_jit_binary_alloc(image_size, &image_ptr,
  1709. sizeof(u32), jit_fill_hole);
  1710. /* Not able to allocate memory for the structure then
  1711. * we must fall back to the interpretation
  1712. */
  1713. if (header == NULL) {
  1714. prog = orig_prog;
  1715. goto out_imms;
  1716. }
  1717. /* 2.) Actual pass to generate final JIT code */
  1718. ctx.target = (u32 *) image_ptr;
  1719. ctx.idx = 0;
  1720. build_prologue(&ctx);
  1721. /* If building the body of the JITed code fails somehow,
  1722. * we fall back to the interpretation.
  1723. */
  1724. if (build_body(&ctx) < 0) {
  1725. image_ptr = NULL;
  1726. bpf_jit_binary_free(header);
  1727. prog = orig_prog;
  1728. goto out_imms;
  1729. }
  1730. build_epilogue(&ctx);
  1731. /* 3.) Extra pass to validate JITed Code */
  1732. if (validate_code(&ctx)) {
  1733. image_ptr = NULL;
  1734. bpf_jit_binary_free(header);
  1735. prog = orig_prog;
  1736. goto out_imms;
  1737. }
  1738. flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
  1739. if (bpf_jit_enable > 1)
  1740. /* there are 2 passes here */
  1741. bpf_jit_dump(prog->len, image_size, 2, ctx.target);
  1742. bpf_jit_binary_lock_ro(header);
  1743. prog->bpf_func = (void *)ctx.target;
  1744. prog->jited = 1;
  1745. prog->jited_len = image_size;
  1746. out_imms:
  1747. #if __LINUX_ARM_ARCH__ < 7
  1748. if (ctx.imm_count)
  1749. kfree(ctx.imms);
  1750. #endif
  1751. out_off:
  1752. kfree(ctx.offsets);
  1753. out:
  1754. if (tmp_blinded)
  1755. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  1756. tmp : orig_prog);
  1757. return prog;
  1758. }