bpf_jit_32.c 52 KB

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