bpf_jit_32.c 50 KB

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