bpf_jit_comp.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * BPF JIT compiler for ARM64
  4. *
  5. * Copyright (C) 2014-2016 Zi Shen Lim <zlim.lnx@gmail.com>
  6. */
  7. #define pr_fmt(fmt) "bpf_jit: " fmt
  8. #include <linux/bitfield.h>
  9. #include <linux/bpf.h>
  10. #include <linux/filter.h>
  11. #include <linux/memory.h>
  12. #include <linux/printk.h>
  13. #include <linux/slab.h>
  14. #include <asm/asm-extable.h>
  15. #include <asm/byteorder.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/debug-monitors.h>
  18. #include <asm/insn.h>
  19. #include <asm/patching.h>
  20. #include <asm/set_memory.h>
  21. #include "bpf_jit.h"
  22. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
  23. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
  24. #define TCCNT_PTR (MAX_BPF_JIT_REG + 2)
  25. #define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
  26. #define ARENA_VM_START (MAX_BPF_JIT_REG + 5)
  27. #define check_imm(bits, imm) do { \
  28. if ((((imm) > 0) && ((imm) >> (bits))) || \
  29. (((imm) < 0) && (~(imm) >> (bits)))) { \
  30. pr_info("[%2d] imm=%d(0x%x) out of range\n", \
  31. i, imm, imm); \
  32. return -EINVAL; \
  33. } \
  34. } while (0)
  35. #define check_imm19(imm) check_imm(19, imm)
  36. #define check_imm26(imm) check_imm(26, imm)
  37. /* Map BPF registers to A64 registers */
  38. static const int bpf2a64[] = {
  39. /* return value from in-kernel function, and exit value from eBPF */
  40. [BPF_REG_0] = A64_R(7),
  41. /* arguments from eBPF program to in-kernel function */
  42. [BPF_REG_1] = A64_R(0),
  43. [BPF_REG_2] = A64_R(1),
  44. [BPF_REG_3] = A64_R(2),
  45. [BPF_REG_4] = A64_R(3),
  46. [BPF_REG_5] = A64_R(4),
  47. /* callee saved registers that in-kernel function will preserve */
  48. [BPF_REG_6] = A64_R(19),
  49. [BPF_REG_7] = A64_R(20),
  50. [BPF_REG_8] = A64_R(21),
  51. [BPF_REG_9] = A64_R(22),
  52. /* read-only frame pointer to access stack */
  53. [BPF_REG_FP] = A64_R(25),
  54. /* temporary registers for BPF JIT */
  55. [TMP_REG_1] = A64_R(10),
  56. [TMP_REG_2] = A64_R(11),
  57. [TMP_REG_3] = A64_R(12),
  58. /* tail_call_cnt_ptr */
  59. [TCCNT_PTR] = A64_R(26),
  60. /* temporary register for blinding constants */
  61. [BPF_REG_AX] = A64_R(9),
  62. /* callee saved register for kern_vm_start address */
  63. [ARENA_VM_START] = A64_R(28),
  64. };
  65. struct jit_ctx {
  66. const struct bpf_prog *prog;
  67. int idx;
  68. int epilogue_offset;
  69. int *offset;
  70. int exentry_idx;
  71. int nr_used_callee_reg;
  72. u8 used_callee_reg[8]; /* r6~r9, fp, arena_vm_start */
  73. __le32 *image;
  74. __le32 *ro_image;
  75. u32 stack_size;
  76. u64 user_vm_start;
  77. u64 arena_vm_start;
  78. bool fp_used;
  79. bool write;
  80. };
  81. struct bpf_plt {
  82. u32 insn_ldr; /* load target */
  83. u32 insn_br; /* branch to target */
  84. u64 target; /* target value */
  85. };
  86. #define PLT_TARGET_SIZE sizeof_field(struct bpf_plt, target)
  87. #define PLT_TARGET_OFFSET offsetof(struct bpf_plt, target)
  88. static inline void emit(const u32 insn, struct jit_ctx *ctx)
  89. {
  90. if (ctx->image != NULL && ctx->write)
  91. ctx->image[ctx->idx] = cpu_to_le32(insn);
  92. ctx->idx++;
  93. }
  94. static inline void emit_a64_mov_i(const int is64, const int reg,
  95. const s32 val, struct jit_ctx *ctx)
  96. {
  97. u16 hi = val >> 16;
  98. u16 lo = val & 0xffff;
  99. if (hi & 0x8000) {
  100. if (hi == 0xffff) {
  101. emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
  102. } else {
  103. emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
  104. if (lo != 0xffff)
  105. emit(A64_MOVK(is64, reg, lo, 0), ctx);
  106. }
  107. } else {
  108. emit(A64_MOVZ(is64, reg, lo, 0), ctx);
  109. if (hi)
  110. emit(A64_MOVK(is64, reg, hi, 16), ctx);
  111. }
  112. }
  113. static int i64_i16_blocks(const u64 val, bool inverse)
  114. {
  115. return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  116. (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  117. (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  118. (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
  119. }
  120. static inline void emit_a64_mov_i64(const int reg, const u64 val,
  121. struct jit_ctx *ctx)
  122. {
  123. u64 nrm_tmp = val, rev_tmp = ~val;
  124. bool inverse;
  125. int shift;
  126. if (!(nrm_tmp >> 32))
  127. return emit_a64_mov_i(0, reg, (u32)val, ctx);
  128. inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
  129. shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
  130. (fls64(nrm_tmp) - 1)), 16), 0);
  131. if (inverse)
  132. emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
  133. else
  134. emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
  135. shift -= 16;
  136. while (shift >= 0) {
  137. if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
  138. emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
  139. shift -= 16;
  140. }
  141. }
  142. static inline void emit_bti(u32 insn, struct jit_ctx *ctx)
  143. {
  144. if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
  145. emit(insn, ctx);
  146. }
  147. /*
  148. * Kernel addresses in the vmalloc space use at most 48 bits, and the
  149. * remaining bits are guaranteed to be 0x1. So we can compose the address
  150. * with a fixed length movn/movk/movk sequence.
  151. */
  152. static inline void emit_addr_mov_i64(const int reg, const u64 val,
  153. struct jit_ctx *ctx)
  154. {
  155. u64 tmp = val;
  156. int shift = 0;
  157. emit(A64_MOVN(1, reg, ~tmp & 0xffff, shift), ctx);
  158. while (shift < 32) {
  159. tmp >>= 16;
  160. shift += 16;
  161. emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
  162. }
  163. }
  164. static bool should_emit_indirect_call(long target, const struct jit_ctx *ctx)
  165. {
  166. long offset;
  167. /* when ctx->ro_image is not allocated or the target is unknown,
  168. * emit indirect call
  169. */
  170. if (!ctx->ro_image || !target)
  171. return true;
  172. offset = target - (long)&ctx->ro_image[ctx->idx];
  173. return offset < -SZ_128M || offset >= SZ_128M;
  174. }
  175. static void emit_direct_call(u64 target, struct jit_ctx *ctx)
  176. {
  177. u32 insn;
  178. unsigned long pc;
  179. pc = (unsigned long)&ctx->ro_image[ctx->idx];
  180. insn = aarch64_insn_gen_branch_imm(pc, target, AARCH64_INSN_BRANCH_LINK);
  181. emit(insn, ctx);
  182. }
  183. static void emit_indirect_call(u64 target, struct jit_ctx *ctx)
  184. {
  185. u8 tmp;
  186. tmp = bpf2a64[TMP_REG_1];
  187. emit_addr_mov_i64(tmp, target, ctx);
  188. emit(A64_BLR(tmp), ctx);
  189. }
  190. static void emit_call(u64 target, struct jit_ctx *ctx)
  191. {
  192. if (should_emit_indirect_call((long)target, ctx))
  193. emit_indirect_call(target, ctx);
  194. else
  195. emit_direct_call(target, ctx);
  196. }
  197. static inline int bpf2a64_offset(int bpf_insn, int off,
  198. const struct jit_ctx *ctx)
  199. {
  200. /* BPF JMP offset is relative to the next instruction */
  201. bpf_insn++;
  202. /*
  203. * Whereas arm64 branch instructions encode the offset
  204. * from the branch itself, so we must subtract 1 from the
  205. * instruction offset.
  206. */
  207. return ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1);
  208. }
  209. static void jit_fill_hole(void *area, unsigned int size)
  210. {
  211. __le32 *ptr;
  212. /* We are guaranteed to have aligned memory. */
  213. for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
  214. *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
  215. }
  216. int bpf_arch_text_invalidate(void *dst, size_t len)
  217. {
  218. if (!aarch64_insn_set(dst, AARCH64_BREAK_FAULT, len))
  219. return -EINVAL;
  220. return 0;
  221. }
  222. static inline int epilogue_offset(const struct jit_ctx *ctx)
  223. {
  224. int to = ctx->epilogue_offset;
  225. int from = ctx->idx;
  226. return to - from;
  227. }
  228. static bool is_addsub_imm(u32 imm)
  229. {
  230. /* Either imm12 or shifted imm12. */
  231. return !(imm & ~0xfff) || !(imm & ~0xfff000);
  232. }
  233. /*
  234. * There are 3 types of AArch64 LDR/STR (immediate) instruction:
  235. * Post-index, Pre-index, Unsigned offset.
  236. *
  237. * For BPF ldr/str, the "unsigned offset" type is sufficient.
  238. *
  239. * "Unsigned offset" type LDR(immediate) format:
  240. *
  241. * 3 2 1 0
  242. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  243. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  244. * |x x|1 1 1 0 0 1 0 1| imm12 | Rn | Rt |
  245. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  246. * scale
  247. *
  248. * "Unsigned offset" type STR(immediate) format:
  249. * 3 2 1 0
  250. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  251. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  252. * |x x|1 1 1 0 0 1 0 0| imm12 | Rn | Rt |
  253. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  254. * scale
  255. *
  256. * The offset is calculated from imm12 and scale in the following way:
  257. *
  258. * offset = (u64)imm12 << scale
  259. */
  260. static bool is_lsi_offset(int offset, int scale)
  261. {
  262. if (offset < 0)
  263. return false;
  264. if (offset > (0xFFF << scale))
  265. return false;
  266. if (offset & ((1 << scale) - 1))
  267. return false;
  268. return true;
  269. }
  270. /* generated main prog prologue:
  271. * bti c // if CONFIG_ARM64_BTI_KERNEL
  272. * mov x9, lr
  273. * nop // POKE_OFFSET
  274. * paciasp // if CONFIG_ARM64_PTR_AUTH_KERNEL
  275. * stp x29, lr, [sp, #-16]!
  276. * mov x29, sp
  277. * stp xzr, x26, [sp, #-16]!
  278. * mov x26, sp
  279. * // PROLOGUE_OFFSET
  280. * // save callee-saved registers
  281. */
  282. static void prepare_bpf_tail_call_cnt(struct jit_ctx *ctx)
  283. {
  284. const bool is_main_prog = !bpf_is_subprog(ctx->prog);
  285. const u8 ptr = bpf2a64[TCCNT_PTR];
  286. if (is_main_prog) {
  287. /* Initialize tail_call_cnt. */
  288. emit(A64_PUSH(A64_ZR, ptr, A64_SP), ctx);
  289. emit(A64_MOV(1, ptr, A64_SP), ctx);
  290. } else
  291. emit(A64_PUSH(ptr, ptr, A64_SP), ctx);
  292. }
  293. static void find_used_callee_regs(struct jit_ctx *ctx)
  294. {
  295. int i;
  296. const struct bpf_prog *prog = ctx->prog;
  297. const struct bpf_insn *insn = &prog->insnsi[0];
  298. int reg_used = 0;
  299. for (i = 0; i < prog->len; i++, insn++) {
  300. if (insn->dst_reg == BPF_REG_6 || insn->src_reg == BPF_REG_6)
  301. reg_used |= 1;
  302. if (insn->dst_reg == BPF_REG_7 || insn->src_reg == BPF_REG_7)
  303. reg_used |= 2;
  304. if (insn->dst_reg == BPF_REG_8 || insn->src_reg == BPF_REG_8)
  305. reg_used |= 4;
  306. if (insn->dst_reg == BPF_REG_9 || insn->src_reg == BPF_REG_9)
  307. reg_used |= 8;
  308. if (insn->dst_reg == BPF_REG_FP || insn->src_reg == BPF_REG_FP) {
  309. ctx->fp_used = true;
  310. reg_used |= 16;
  311. }
  312. }
  313. i = 0;
  314. if (reg_used & 1)
  315. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_6];
  316. if (reg_used & 2)
  317. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_7];
  318. if (reg_used & 4)
  319. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_8];
  320. if (reg_used & 8)
  321. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_9];
  322. if (reg_used & 16)
  323. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_FP];
  324. if (ctx->arena_vm_start)
  325. ctx->used_callee_reg[i++] = bpf2a64[ARENA_VM_START];
  326. ctx->nr_used_callee_reg = i;
  327. }
  328. /* Save callee-saved registers */
  329. static void push_callee_regs(struct jit_ctx *ctx)
  330. {
  331. int reg1, reg2, i;
  332. /*
  333. * Program acting as exception boundary should save all ARM64
  334. * Callee-saved registers as the exception callback needs to recover
  335. * all ARM64 Callee-saved registers in its epilogue.
  336. */
  337. if (ctx->prog->aux->exception_boundary) {
  338. emit(A64_PUSH(A64_R(19), A64_R(20), A64_SP), ctx);
  339. emit(A64_PUSH(A64_R(21), A64_R(22), A64_SP), ctx);
  340. emit(A64_PUSH(A64_R(23), A64_R(24), A64_SP), ctx);
  341. emit(A64_PUSH(A64_R(25), A64_R(26), A64_SP), ctx);
  342. emit(A64_PUSH(A64_R(27), A64_R(28), A64_SP), ctx);
  343. } else {
  344. find_used_callee_regs(ctx);
  345. for (i = 0; i + 1 < ctx->nr_used_callee_reg; i += 2) {
  346. reg1 = ctx->used_callee_reg[i];
  347. reg2 = ctx->used_callee_reg[i + 1];
  348. emit(A64_PUSH(reg1, reg2, A64_SP), ctx);
  349. }
  350. if (i < ctx->nr_used_callee_reg) {
  351. reg1 = ctx->used_callee_reg[i];
  352. /* keep SP 16-byte aligned */
  353. emit(A64_PUSH(reg1, A64_ZR, A64_SP), ctx);
  354. }
  355. }
  356. }
  357. /* Restore callee-saved registers */
  358. static void pop_callee_regs(struct jit_ctx *ctx)
  359. {
  360. struct bpf_prog_aux *aux = ctx->prog->aux;
  361. int reg1, reg2, i;
  362. /*
  363. * Program acting as exception boundary pushes R23 and R24 in addition
  364. * to BPF callee-saved registers. Exception callback uses the boundary
  365. * program's stack frame, so recover these extra registers in the above
  366. * two cases.
  367. */
  368. if (aux->exception_boundary || aux->exception_cb) {
  369. emit(A64_POP(A64_R(27), A64_R(28), A64_SP), ctx);
  370. emit(A64_POP(A64_R(25), A64_R(26), A64_SP), ctx);
  371. emit(A64_POP(A64_R(23), A64_R(24), A64_SP), ctx);
  372. emit(A64_POP(A64_R(21), A64_R(22), A64_SP), ctx);
  373. emit(A64_POP(A64_R(19), A64_R(20), A64_SP), ctx);
  374. } else {
  375. i = ctx->nr_used_callee_reg - 1;
  376. if (ctx->nr_used_callee_reg % 2 != 0) {
  377. reg1 = ctx->used_callee_reg[i];
  378. emit(A64_POP(reg1, A64_ZR, A64_SP), ctx);
  379. i--;
  380. }
  381. while (i > 0) {
  382. reg1 = ctx->used_callee_reg[i - 1];
  383. reg2 = ctx->used_callee_reg[i];
  384. emit(A64_POP(reg1, reg2, A64_SP), ctx);
  385. i -= 2;
  386. }
  387. }
  388. }
  389. #define BTI_INSNS (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) ? 1 : 0)
  390. #define PAC_INSNS (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) ? 1 : 0)
  391. /* Offset of nop instruction in bpf prog entry to be poked */
  392. #define POKE_OFFSET (BTI_INSNS + 1)
  393. /* Tail call offset to jump into */
  394. #define PROLOGUE_OFFSET (BTI_INSNS + 2 + PAC_INSNS + 4)
  395. static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
  396. {
  397. const struct bpf_prog *prog = ctx->prog;
  398. const bool is_main_prog = !bpf_is_subprog(prog);
  399. const u8 fp = bpf2a64[BPF_REG_FP];
  400. const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
  401. const int idx0 = ctx->idx;
  402. int cur_offset;
  403. /*
  404. * BPF prog stack layout
  405. *
  406. * high
  407. * original A64_SP => 0:+-----+ BPF prologue
  408. * |FP/LR|
  409. * current A64_FP => -16:+-----+
  410. * | ... | callee saved registers
  411. * BPF fp register => -64:+-----+ <= (BPF_FP)
  412. * | |
  413. * | ... | BPF prog stack
  414. * | |
  415. * +-----+ <= (BPF_FP - prog->aux->stack_depth)
  416. * |RSVD | padding
  417. * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
  418. * | |
  419. * | ... | Function call stack
  420. * | |
  421. * +-----+
  422. * low
  423. *
  424. */
  425. /* bpf function may be invoked by 3 instruction types:
  426. * 1. bl, attached via freplace to bpf prog via short jump
  427. * 2. br, attached via freplace to bpf prog via long jump
  428. * 3. blr, working as a function pointer, used by emit_call.
  429. * So BTI_JC should used here to support both br and blr.
  430. */
  431. emit_bti(A64_BTI_JC, ctx);
  432. emit(A64_MOV(1, A64_R(9), A64_LR), ctx);
  433. emit(A64_NOP, ctx);
  434. if (!prog->aux->exception_cb) {
  435. /* Sign lr */
  436. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
  437. emit(A64_PACIASP, ctx);
  438. /* Save FP and LR registers to stay align with ARM64 AAPCS */
  439. emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
  440. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  441. prepare_bpf_tail_call_cnt(ctx);
  442. if (!ebpf_from_cbpf && is_main_prog) {
  443. cur_offset = ctx->idx - idx0;
  444. if (cur_offset != PROLOGUE_OFFSET) {
  445. pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
  446. cur_offset, PROLOGUE_OFFSET);
  447. return -1;
  448. }
  449. /* BTI landing pad for the tail call, done with a BR */
  450. emit_bti(A64_BTI_J, ctx);
  451. }
  452. push_callee_regs(ctx);
  453. } else {
  454. /*
  455. * Exception callback receives FP of Main Program as third
  456. * parameter
  457. */
  458. emit(A64_MOV(1, A64_FP, A64_R(2)), ctx);
  459. /*
  460. * Main Program already pushed the frame record and the
  461. * callee-saved registers. The exception callback will not push
  462. * anything and re-use the main program's stack.
  463. *
  464. * 12 registers are on the stack
  465. */
  466. emit(A64_SUB_I(1, A64_SP, A64_FP, 96), ctx);
  467. }
  468. if (ctx->fp_used)
  469. /* Set up BPF prog stack base register */
  470. emit(A64_MOV(1, fp, A64_SP), ctx);
  471. /* Stack must be multiples of 16B */
  472. ctx->stack_size = round_up(prog->aux->stack_depth, 16);
  473. /* Set up function call stack */
  474. if (ctx->stack_size)
  475. emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  476. if (ctx->arena_vm_start)
  477. emit_a64_mov_i64(arena_vm_base, ctx->arena_vm_start, ctx);
  478. return 0;
  479. }
  480. static int emit_bpf_tail_call(struct jit_ctx *ctx)
  481. {
  482. /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
  483. const u8 r2 = bpf2a64[BPF_REG_2];
  484. const u8 r3 = bpf2a64[BPF_REG_3];
  485. const u8 tmp = bpf2a64[TMP_REG_1];
  486. const u8 prg = bpf2a64[TMP_REG_2];
  487. const u8 tcc = bpf2a64[TMP_REG_3];
  488. const u8 ptr = bpf2a64[TCCNT_PTR];
  489. size_t off;
  490. __le32 *branch1 = NULL;
  491. __le32 *branch2 = NULL;
  492. __le32 *branch3 = NULL;
  493. /* if (index >= array->map.max_entries)
  494. * goto out;
  495. */
  496. off = offsetof(struct bpf_array, map.max_entries);
  497. emit_a64_mov_i64(tmp, off, ctx);
  498. emit(A64_LDR32(tmp, r2, tmp), ctx);
  499. emit(A64_MOV(0, r3, r3), ctx);
  500. emit(A64_CMP(0, r3, tmp), ctx);
  501. branch1 = ctx->image + ctx->idx;
  502. emit(A64_NOP, ctx);
  503. /*
  504. * if ((*tail_call_cnt_ptr) >= MAX_TAIL_CALL_CNT)
  505. * goto out;
  506. */
  507. emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx);
  508. emit(A64_LDR64I(tcc, ptr, 0), ctx);
  509. emit(A64_CMP(1, tcc, tmp), ctx);
  510. branch2 = ctx->image + ctx->idx;
  511. emit(A64_NOP, ctx);
  512. /* (*tail_call_cnt_ptr)++; */
  513. emit(A64_ADD_I(1, tcc, tcc, 1), ctx);
  514. /* prog = array->ptrs[index];
  515. * if (prog == NULL)
  516. * goto out;
  517. */
  518. off = offsetof(struct bpf_array, ptrs);
  519. emit_a64_mov_i64(tmp, off, ctx);
  520. emit(A64_ADD(1, tmp, r2, tmp), ctx);
  521. emit(A64_LSL(1, prg, r3, 3), ctx);
  522. emit(A64_LDR64(prg, tmp, prg), ctx);
  523. branch3 = ctx->image + ctx->idx;
  524. emit(A64_NOP, ctx);
  525. /* Update tail_call_cnt if the slot is populated. */
  526. emit(A64_STR64I(tcc, ptr, 0), ctx);
  527. /* restore SP */
  528. if (ctx->stack_size)
  529. emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  530. pop_callee_regs(ctx);
  531. /* goto *(prog->bpf_func + prologue_offset); */
  532. off = offsetof(struct bpf_prog, bpf_func);
  533. emit_a64_mov_i64(tmp, off, ctx);
  534. emit(A64_LDR64(tmp, prg, tmp), ctx);
  535. emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx);
  536. emit(A64_BR(tmp), ctx);
  537. if (ctx->image) {
  538. off = &ctx->image[ctx->idx] - branch1;
  539. *branch1 = cpu_to_le32(A64_B_(A64_COND_CS, off));
  540. off = &ctx->image[ctx->idx] - branch2;
  541. *branch2 = cpu_to_le32(A64_B_(A64_COND_CS, off));
  542. off = &ctx->image[ctx->idx] - branch3;
  543. *branch3 = cpu_to_le32(A64_CBZ(1, prg, off));
  544. }
  545. return 0;
  546. }
  547. #ifdef CONFIG_ARM64_LSE_ATOMICS
  548. static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  549. {
  550. const u8 code = insn->code;
  551. const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
  552. const u8 dst = bpf2a64[insn->dst_reg];
  553. const u8 src = bpf2a64[insn->src_reg];
  554. const u8 tmp = bpf2a64[TMP_REG_1];
  555. const u8 tmp2 = bpf2a64[TMP_REG_2];
  556. const bool isdw = BPF_SIZE(code) == BPF_DW;
  557. const bool arena = BPF_MODE(code) == BPF_PROBE_ATOMIC;
  558. const s16 off = insn->off;
  559. u8 reg = dst;
  560. if (off || arena) {
  561. if (off) {
  562. emit_a64_mov_i(1, tmp, off, ctx);
  563. emit(A64_ADD(1, tmp, tmp, dst), ctx);
  564. reg = tmp;
  565. }
  566. if (arena) {
  567. emit(A64_ADD(1, tmp, reg, arena_vm_base), ctx);
  568. reg = tmp;
  569. }
  570. }
  571. switch (insn->imm) {
  572. /* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */
  573. case BPF_ADD:
  574. emit(A64_STADD(isdw, reg, src), ctx);
  575. break;
  576. case BPF_AND:
  577. emit(A64_MVN(isdw, tmp2, src), ctx);
  578. emit(A64_STCLR(isdw, reg, tmp2), ctx);
  579. break;
  580. case BPF_OR:
  581. emit(A64_STSET(isdw, reg, src), ctx);
  582. break;
  583. case BPF_XOR:
  584. emit(A64_STEOR(isdw, reg, src), ctx);
  585. break;
  586. /* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */
  587. case BPF_ADD | BPF_FETCH:
  588. emit(A64_LDADDAL(isdw, src, reg, src), ctx);
  589. break;
  590. case BPF_AND | BPF_FETCH:
  591. emit(A64_MVN(isdw, tmp2, src), ctx);
  592. emit(A64_LDCLRAL(isdw, src, reg, tmp2), ctx);
  593. break;
  594. case BPF_OR | BPF_FETCH:
  595. emit(A64_LDSETAL(isdw, src, reg, src), ctx);
  596. break;
  597. case BPF_XOR | BPF_FETCH:
  598. emit(A64_LDEORAL(isdw, src, reg, src), ctx);
  599. break;
  600. /* src_reg = atomic_xchg(dst_reg + off, src_reg); */
  601. case BPF_XCHG:
  602. emit(A64_SWPAL(isdw, src, reg, src), ctx);
  603. break;
  604. /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
  605. case BPF_CMPXCHG:
  606. emit(A64_CASAL(isdw, src, reg, bpf2a64[BPF_REG_0]), ctx);
  607. break;
  608. default:
  609. pr_err_once("unknown atomic op code %02x\n", insn->imm);
  610. return -EINVAL;
  611. }
  612. return 0;
  613. }
  614. #else
  615. static inline int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  616. {
  617. return -EINVAL;
  618. }
  619. #endif
  620. static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  621. {
  622. const u8 code = insn->code;
  623. const u8 dst = bpf2a64[insn->dst_reg];
  624. const u8 src = bpf2a64[insn->src_reg];
  625. const u8 tmp = bpf2a64[TMP_REG_1];
  626. const u8 tmp2 = bpf2a64[TMP_REG_2];
  627. const u8 tmp3 = bpf2a64[TMP_REG_3];
  628. const int i = insn - ctx->prog->insnsi;
  629. const s32 imm = insn->imm;
  630. const s16 off = insn->off;
  631. const bool isdw = BPF_SIZE(code) == BPF_DW;
  632. u8 reg;
  633. s32 jmp_offset;
  634. if (BPF_MODE(code) == BPF_PROBE_ATOMIC) {
  635. /* ll_sc based atomics don't support unsafe pointers yet. */
  636. pr_err_once("unknown atomic opcode %02x\n", code);
  637. return -EINVAL;
  638. }
  639. if (!off) {
  640. reg = dst;
  641. } else {
  642. emit_a64_mov_i(1, tmp, off, ctx);
  643. emit(A64_ADD(1, tmp, tmp, dst), ctx);
  644. reg = tmp;
  645. }
  646. if (imm == BPF_ADD || imm == BPF_AND ||
  647. imm == BPF_OR || imm == BPF_XOR) {
  648. /* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */
  649. emit(A64_LDXR(isdw, tmp2, reg), ctx);
  650. if (imm == BPF_ADD)
  651. emit(A64_ADD(isdw, tmp2, tmp2, src), ctx);
  652. else if (imm == BPF_AND)
  653. emit(A64_AND(isdw, tmp2, tmp2, src), ctx);
  654. else if (imm == BPF_OR)
  655. emit(A64_ORR(isdw, tmp2, tmp2, src), ctx);
  656. else
  657. emit(A64_EOR(isdw, tmp2, tmp2, src), ctx);
  658. emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx);
  659. jmp_offset = -3;
  660. check_imm19(jmp_offset);
  661. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  662. } else if (imm == (BPF_ADD | BPF_FETCH) ||
  663. imm == (BPF_AND | BPF_FETCH) ||
  664. imm == (BPF_OR | BPF_FETCH) ||
  665. imm == (BPF_XOR | BPF_FETCH)) {
  666. /* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */
  667. const u8 ax = bpf2a64[BPF_REG_AX];
  668. emit(A64_MOV(isdw, ax, src), ctx);
  669. emit(A64_LDXR(isdw, src, reg), ctx);
  670. if (imm == (BPF_ADD | BPF_FETCH))
  671. emit(A64_ADD(isdw, tmp2, src, ax), ctx);
  672. else if (imm == (BPF_AND | BPF_FETCH))
  673. emit(A64_AND(isdw, tmp2, src, ax), ctx);
  674. else if (imm == (BPF_OR | BPF_FETCH))
  675. emit(A64_ORR(isdw, tmp2, src, ax), ctx);
  676. else
  677. emit(A64_EOR(isdw, tmp2, src, ax), ctx);
  678. emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
  679. jmp_offset = -3;
  680. check_imm19(jmp_offset);
  681. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  682. emit(A64_DMB_ISH, ctx);
  683. } else if (imm == BPF_XCHG) {
  684. /* src_reg = atomic_xchg(dst_reg + off, src_reg); */
  685. emit(A64_MOV(isdw, tmp2, src), ctx);
  686. emit(A64_LDXR(isdw, src, reg), ctx);
  687. emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
  688. jmp_offset = -2;
  689. check_imm19(jmp_offset);
  690. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  691. emit(A64_DMB_ISH, ctx);
  692. } else if (imm == BPF_CMPXCHG) {
  693. /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
  694. const u8 r0 = bpf2a64[BPF_REG_0];
  695. emit(A64_MOV(isdw, tmp2, r0), ctx);
  696. emit(A64_LDXR(isdw, r0, reg), ctx);
  697. emit(A64_EOR(isdw, tmp3, r0, tmp2), ctx);
  698. jmp_offset = 4;
  699. check_imm19(jmp_offset);
  700. emit(A64_CBNZ(isdw, tmp3, jmp_offset), ctx);
  701. emit(A64_STLXR(isdw, src, reg, tmp3), ctx);
  702. jmp_offset = -4;
  703. check_imm19(jmp_offset);
  704. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  705. emit(A64_DMB_ISH, ctx);
  706. } else {
  707. pr_err_once("unknown atomic op code %02x\n", imm);
  708. return -EINVAL;
  709. }
  710. return 0;
  711. }
  712. void dummy_tramp(void);
  713. asm (
  714. " .pushsection .text, \"ax\", @progbits\n"
  715. " .global dummy_tramp\n"
  716. " .type dummy_tramp, %function\n"
  717. "dummy_tramp:"
  718. #if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
  719. " bti j\n" /* dummy_tramp is called via "br x10" */
  720. #endif
  721. " mov x10, x30\n"
  722. " mov x30, x9\n"
  723. " ret x10\n"
  724. " .size dummy_tramp, .-dummy_tramp\n"
  725. " .popsection\n"
  726. );
  727. /* build a plt initialized like this:
  728. *
  729. * plt:
  730. * ldr tmp, target
  731. * br tmp
  732. * target:
  733. * .quad dummy_tramp
  734. *
  735. * when a long jump trampoline is attached, target is filled with the
  736. * trampoline address, and when the trampoline is removed, target is
  737. * restored to dummy_tramp address.
  738. */
  739. static void build_plt(struct jit_ctx *ctx)
  740. {
  741. const u8 tmp = bpf2a64[TMP_REG_1];
  742. struct bpf_plt *plt = NULL;
  743. /* make sure target is 64-bit aligned */
  744. if ((ctx->idx + PLT_TARGET_OFFSET / AARCH64_INSN_SIZE) % 2)
  745. emit(A64_NOP, ctx);
  746. plt = (struct bpf_plt *)(ctx->image + ctx->idx);
  747. /* plt is called via bl, no BTI needed here */
  748. emit(A64_LDR64LIT(tmp, 2 * AARCH64_INSN_SIZE), ctx);
  749. emit(A64_BR(tmp), ctx);
  750. if (ctx->image)
  751. plt->target = (u64)&dummy_tramp;
  752. }
  753. static void build_epilogue(struct jit_ctx *ctx)
  754. {
  755. const u8 r0 = bpf2a64[BPF_REG_0];
  756. const u8 ptr = bpf2a64[TCCNT_PTR];
  757. /* We're done with BPF stack */
  758. if (ctx->stack_size)
  759. emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  760. pop_callee_regs(ctx);
  761. emit(A64_POP(A64_ZR, ptr, A64_SP), ctx);
  762. /* Restore FP/LR registers */
  763. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  764. /* Set return value */
  765. emit(A64_MOV(1, A64_R(0), r0), ctx);
  766. /* Authenticate lr */
  767. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
  768. emit(A64_AUTIASP, ctx);
  769. emit(A64_RET(A64_LR), ctx);
  770. }
  771. #define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
  772. #define BPF_FIXUP_REG_MASK GENMASK(31, 27)
  773. #define DONT_CLEAR 5 /* Unused ARM64 register from BPF's POV */
  774. bool ex_handler_bpf(const struct exception_table_entry *ex,
  775. struct pt_regs *regs)
  776. {
  777. off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
  778. int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
  779. if (dst_reg != DONT_CLEAR)
  780. regs->regs[dst_reg] = 0;
  781. regs->pc = (unsigned long)&ex->fixup - offset;
  782. return true;
  783. }
  784. /* For accesses to BTF pointers, add an entry to the exception table */
  785. static int add_exception_handler(const struct bpf_insn *insn,
  786. struct jit_ctx *ctx,
  787. int dst_reg)
  788. {
  789. off_t ins_offset;
  790. off_t fixup_offset;
  791. unsigned long pc;
  792. struct exception_table_entry *ex;
  793. if (!ctx->image)
  794. /* First pass */
  795. return 0;
  796. if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
  797. BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
  798. BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
  799. BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
  800. return 0;
  801. if (!ctx->prog->aux->extable ||
  802. WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries))
  803. return -EINVAL;
  804. ex = &ctx->prog->aux->extable[ctx->exentry_idx];
  805. pc = (unsigned long)&ctx->ro_image[ctx->idx - 1];
  806. /*
  807. * This is the relative offset of the instruction that may fault from
  808. * the exception table itself. This will be written to the exception
  809. * table and if this instruction faults, the destination register will
  810. * be set to '0' and the execution will jump to the next instruction.
  811. */
  812. ins_offset = pc - (long)&ex->insn;
  813. if (WARN_ON_ONCE(ins_offset >= 0 || ins_offset < INT_MIN))
  814. return -ERANGE;
  815. /*
  816. * Since the extable follows the program, the fixup offset is always
  817. * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
  818. * to keep things simple, and put the destination register in the upper
  819. * bits. We don't need to worry about buildtime or runtime sort
  820. * modifying the upper bits because the table is already sorted, and
  821. * isn't part of the main exception table.
  822. *
  823. * The fixup_offset is set to the next instruction from the instruction
  824. * that may fault. The execution will jump to this after handling the
  825. * fault.
  826. */
  827. fixup_offset = (long)&ex->fixup - (pc + AARCH64_INSN_SIZE);
  828. if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, fixup_offset))
  829. return -ERANGE;
  830. /*
  831. * The offsets above have been calculated using the RO buffer but we
  832. * need to use the R/W buffer for writes.
  833. * switch ex to rw buffer for writing.
  834. */
  835. ex = (void *)ctx->image + ((void *)ex - (void *)ctx->ro_image);
  836. ex->insn = ins_offset;
  837. if (BPF_CLASS(insn->code) != BPF_LDX)
  838. dst_reg = DONT_CLEAR;
  839. ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, fixup_offset) |
  840. FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
  841. ex->type = EX_TYPE_BPF;
  842. ctx->exentry_idx++;
  843. return 0;
  844. }
  845. /* JITs an eBPF instruction.
  846. * Returns:
  847. * 0 - successfully JITed an 8-byte eBPF instruction.
  848. * >0 - successfully JITed a 16-byte eBPF instruction.
  849. * <0 - failed to JIT.
  850. */
  851. static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  852. bool extra_pass)
  853. {
  854. const u8 code = insn->code;
  855. u8 dst = bpf2a64[insn->dst_reg];
  856. u8 src = bpf2a64[insn->src_reg];
  857. const u8 tmp = bpf2a64[TMP_REG_1];
  858. const u8 tmp2 = bpf2a64[TMP_REG_2];
  859. const u8 fp = bpf2a64[BPF_REG_FP];
  860. const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
  861. const s16 off = insn->off;
  862. const s32 imm = insn->imm;
  863. const int i = insn - ctx->prog->insnsi;
  864. const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
  865. BPF_CLASS(code) == BPF_JMP;
  866. u8 jmp_cond;
  867. s32 jmp_offset;
  868. u32 a64_insn;
  869. u8 src_adj;
  870. u8 dst_adj;
  871. int off_adj;
  872. int ret;
  873. bool sign_extend;
  874. switch (code) {
  875. /* dst = src */
  876. case BPF_ALU | BPF_MOV | BPF_X:
  877. case BPF_ALU64 | BPF_MOV | BPF_X:
  878. if (insn_is_cast_user(insn)) {
  879. emit(A64_MOV(0, tmp, src), ctx); // 32-bit mov clears the upper 32 bits
  880. emit_a64_mov_i(0, dst, ctx->user_vm_start >> 32, ctx);
  881. emit(A64_LSL(1, dst, dst, 32), ctx);
  882. emit(A64_CBZ(1, tmp, 2), ctx);
  883. emit(A64_ORR(1, tmp, dst, tmp), ctx);
  884. emit(A64_MOV(1, dst, tmp), ctx);
  885. break;
  886. } else if (insn_is_mov_percpu_addr(insn)) {
  887. if (dst != src)
  888. emit(A64_MOV(1, dst, src), ctx);
  889. if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
  890. emit(A64_MRS_TPIDR_EL2(tmp), ctx);
  891. else
  892. emit(A64_MRS_TPIDR_EL1(tmp), ctx);
  893. emit(A64_ADD(1, dst, dst, tmp), ctx);
  894. break;
  895. }
  896. switch (insn->off) {
  897. case 0:
  898. emit(A64_MOV(is64, dst, src), ctx);
  899. break;
  900. case 8:
  901. emit(A64_SXTB(is64, dst, src), ctx);
  902. break;
  903. case 16:
  904. emit(A64_SXTH(is64, dst, src), ctx);
  905. break;
  906. case 32:
  907. emit(A64_SXTW(is64, dst, src), ctx);
  908. break;
  909. }
  910. break;
  911. /* dst = dst OP src */
  912. case BPF_ALU | BPF_ADD | BPF_X:
  913. case BPF_ALU64 | BPF_ADD | BPF_X:
  914. emit(A64_ADD(is64, dst, dst, src), ctx);
  915. break;
  916. case BPF_ALU | BPF_SUB | BPF_X:
  917. case BPF_ALU64 | BPF_SUB | BPF_X:
  918. emit(A64_SUB(is64, dst, dst, src), ctx);
  919. break;
  920. case BPF_ALU | BPF_AND | BPF_X:
  921. case BPF_ALU64 | BPF_AND | BPF_X:
  922. emit(A64_AND(is64, dst, dst, src), ctx);
  923. break;
  924. case BPF_ALU | BPF_OR | BPF_X:
  925. case BPF_ALU64 | BPF_OR | BPF_X:
  926. emit(A64_ORR(is64, dst, dst, src), ctx);
  927. break;
  928. case BPF_ALU | BPF_XOR | BPF_X:
  929. case BPF_ALU64 | BPF_XOR | BPF_X:
  930. emit(A64_EOR(is64, dst, dst, src), ctx);
  931. break;
  932. case BPF_ALU | BPF_MUL | BPF_X:
  933. case BPF_ALU64 | BPF_MUL | BPF_X:
  934. emit(A64_MUL(is64, dst, dst, src), ctx);
  935. break;
  936. case BPF_ALU | BPF_DIV | BPF_X:
  937. case BPF_ALU64 | BPF_DIV | BPF_X:
  938. if (!off)
  939. emit(A64_UDIV(is64, dst, dst, src), ctx);
  940. else
  941. emit(A64_SDIV(is64, dst, dst, src), ctx);
  942. break;
  943. case BPF_ALU | BPF_MOD | BPF_X:
  944. case BPF_ALU64 | BPF_MOD | BPF_X:
  945. if (!off)
  946. emit(A64_UDIV(is64, tmp, dst, src), ctx);
  947. else
  948. emit(A64_SDIV(is64, tmp, dst, src), ctx);
  949. emit(A64_MSUB(is64, dst, dst, tmp, src), ctx);
  950. break;
  951. case BPF_ALU | BPF_LSH | BPF_X:
  952. case BPF_ALU64 | BPF_LSH | BPF_X:
  953. emit(A64_LSLV(is64, dst, dst, src), ctx);
  954. break;
  955. case BPF_ALU | BPF_RSH | BPF_X:
  956. case BPF_ALU64 | BPF_RSH | BPF_X:
  957. emit(A64_LSRV(is64, dst, dst, src), ctx);
  958. break;
  959. case BPF_ALU | BPF_ARSH | BPF_X:
  960. case BPF_ALU64 | BPF_ARSH | BPF_X:
  961. emit(A64_ASRV(is64, dst, dst, src), ctx);
  962. break;
  963. /* dst = -dst */
  964. case BPF_ALU | BPF_NEG:
  965. case BPF_ALU64 | BPF_NEG:
  966. emit(A64_NEG(is64, dst, dst), ctx);
  967. break;
  968. /* dst = BSWAP##imm(dst) */
  969. case BPF_ALU | BPF_END | BPF_FROM_LE:
  970. case BPF_ALU | BPF_END | BPF_FROM_BE:
  971. case BPF_ALU64 | BPF_END | BPF_FROM_LE:
  972. #ifdef CONFIG_CPU_BIG_ENDIAN
  973. if (BPF_CLASS(code) == BPF_ALU && BPF_SRC(code) == BPF_FROM_BE)
  974. goto emit_bswap_uxt;
  975. #else /* !CONFIG_CPU_BIG_ENDIAN */
  976. if (BPF_CLASS(code) == BPF_ALU && BPF_SRC(code) == BPF_FROM_LE)
  977. goto emit_bswap_uxt;
  978. #endif
  979. switch (imm) {
  980. case 16:
  981. emit(A64_REV16(is64, dst, dst), ctx);
  982. /* zero-extend 16 bits into 64 bits */
  983. emit(A64_UXTH(is64, dst, dst), ctx);
  984. break;
  985. case 32:
  986. emit(A64_REV32(0, dst, dst), ctx);
  987. /* upper 32 bits already cleared */
  988. break;
  989. case 64:
  990. emit(A64_REV64(dst, dst), ctx);
  991. break;
  992. }
  993. break;
  994. emit_bswap_uxt:
  995. switch (imm) {
  996. case 16:
  997. /* zero-extend 16 bits into 64 bits */
  998. emit(A64_UXTH(is64, dst, dst), ctx);
  999. break;
  1000. case 32:
  1001. /* zero-extend 32 bits into 64 bits */
  1002. emit(A64_UXTW(is64, dst, dst), ctx);
  1003. break;
  1004. case 64:
  1005. /* nop */
  1006. break;
  1007. }
  1008. break;
  1009. /* dst = imm */
  1010. case BPF_ALU | BPF_MOV | BPF_K:
  1011. case BPF_ALU64 | BPF_MOV | BPF_K:
  1012. emit_a64_mov_i(is64, dst, imm, ctx);
  1013. break;
  1014. /* dst = dst OP imm */
  1015. case BPF_ALU | BPF_ADD | BPF_K:
  1016. case BPF_ALU64 | BPF_ADD | BPF_K:
  1017. if (is_addsub_imm(imm)) {
  1018. emit(A64_ADD_I(is64, dst, dst, imm), ctx);
  1019. } else if (is_addsub_imm(-imm)) {
  1020. emit(A64_SUB_I(is64, dst, dst, -imm), ctx);
  1021. } else {
  1022. emit_a64_mov_i(is64, tmp, imm, ctx);
  1023. emit(A64_ADD(is64, dst, dst, tmp), ctx);
  1024. }
  1025. break;
  1026. case BPF_ALU | BPF_SUB | BPF_K:
  1027. case BPF_ALU64 | BPF_SUB | BPF_K:
  1028. if (is_addsub_imm(imm)) {
  1029. emit(A64_SUB_I(is64, dst, dst, imm), ctx);
  1030. } else if (is_addsub_imm(-imm)) {
  1031. emit(A64_ADD_I(is64, dst, dst, -imm), ctx);
  1032. } else {
  1033. emit_a64_mov_i(is64, tmp, imm, ctx);
  1034. emit(A64_SUB(is64, dst, dst, tmp), ctx);
  1035. }
  1036. break;
  1037. case BPF_ALU | BPF_AND | BPF_K:
  1038. case BPF_ALU64 | BPF_AND | BPF_K:
  1039. a64_insn = A64_AND_I(is64, dst, dst, imm);
  1040. if (a64_insn != AARCH64_BREAK_FAULT) {
  1041. emit(a64_insn, ctx);
  1042. } else {
  1043. emit_a64_mov_i(is64, tmp, imm, ctx);
  1044. emit(A64_AND(is64, dst, dst, tmp), ctx);
  1045. }
  1046. break;
  1047. case BPF_ALU | BPF_OR | BPF_K:
  1048. case BPF_ALU64 | BPF_OR | BPF_K:
  1049. a64_insn = A64_ORR_I(is64, dst, dst, imm);
  1050. if (a64_insn != AARCH64_BREAK_FAULT) {
  1051. emit(a64_insn, ctx);
  1052. } else {
  1053. emit_a64_mov_i(is64, tmp, imm, ctx);
  1054. emit(A64_ORR(is64, dst, dst, tmp), ctx);
  1055. }
  1056. break;
  1057. case BPF_ALU | BPF_XOR | BPF_K:
  1058. case BPF_ALU64 | BPF_XOR | BPF_K:
  1059. a64_insn = A64_EOR_I(is64, dst, dst, imm);
  1060. if (a64_insn != AARCH64_BREAK_FAULT) {
  1061. emit(a64_insn, ctx);
  1062. } else {
  1063. emit_a64_mov_i(is64, tmp, imm, ctx);
  1064. emit(A64_EOR(is64, dst, dst, tmp), ctx);
  1065. }
  1066. break;
  1067. case BPF_ALU | BPF_MUL | BPF_K:
  1068. case BPF_ALU64 | BPF_MUL | BPF_K:
  1069. emit_a64_mov_i(is64, tmp, imm, ctx);
  1070. emit(A64_MUL(is64, dst, dst, tmp), ctx);
  1071. break;
  1072. case BPF_ALU | BPF_DIV | BPF_K:
  1073. case BPF_ALU64 | BPF_DIV | BPF_K:
  1074. emit_a64_mov_i(is64, tmp, imm, ctx);
  1075. if (!off)
  1076. emit(A64_UDIV(is64, dst, dst, tmp), ctx);
  1077. else
  1078. emit(A64_SDIV(is64, dst, dst, tmp), ctx);
  1079. break;
  1080. case BPF_ALU | BPF_MOD | BPF_K:
  1081. case BPF_ALU64 | BPF_MOD | BPF_K:
  1082. emit_a64_mov_i(is64, tmp2, imm, ctx);
  1083. if (!off)
  1084. emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
  1085. else
  1086. emit(A64_SDIV(is64, tmp, dst, tmp2), ctx);
  1087. emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx);
  1088. break;
  1089. case BPF_ALU | BPF_LSH | BPF_K:
  1090. case BPF_ALU64 | BPF_LSH | BPF_K:
  1091. emit(A64_LSL(is64, dst, dst, imm), ctx);
  1092. break;
  1093. case BPF_ALU | BPF_RSH | BPF_K:
  1094. case BPF_ALU64 | BPF_RSH | BPF_K:
  1095. emit(A64_LSR(is64, dst, dst, imm), ctx);
  1096. break;
  1097. case BPF_ALU | BPF_ARSH | BPF_K:
  1098. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1099. emit(A64_ASR(is64, dst, dst, imm), ctx);
  1100. break;
  1101. /* JUMP off */
  1102. case BPF_JMP | BPF_JA:
  1103. case BPF_JMP32 | BPF_JA:
  1104. if (BPF_CLASS(code) == BPF_JMP)
  1105. jmp_offset = bpf2a64_offset(i, off, ctx);
  1106. else
  1107. jmp_offset = bpf2a64_offset(i, imm, ctx);
  1108. check_imm26(jmp_offset);
  1109. emit(A64_B(jmp_offset), ctx);
  1110. break;
  1111. /* IF (dst COND src) JUMP off */
  1112. case BPF_JMP | BPF_JEQ | BPF_X:
  1113. case BPF_JMP | BPF_JGT | BPF_X:
  1114. case BPF_JMP | BPF_JLT | BPF_X:
  1115. case BPF_JMP | BPF_JGE | BPF_X:
  1116. case BPF_JMP | BPF_JLE | BPF_X:
  1117. case BPF_JMP | BPF_JNE | BPF_X:
  1118. case BPF_JMP | BPF_JSGT | BPF_X:
  1119. case BPF_JMP | BPF_JSLT | BPF_X:
  1120. case BPF_JMP | BPF_JSGE | BPF_X:
  1121. case BPF_JMP | BPF_JSLE | BPF_X:
  1122. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1123. case BPF_JMP32 | BPF_JGT | BPF_X:
  1124. case BPF_JMP32 | BPF_JLT | BPF_X:
  1125. case BPF_JMP32 | BPF_JGE | BPF_X:
  1126. case BPF_JMP32 | BPF_JLE | BPF_X:
  1127. case BPF_JMP32 | BPF_JNE | BPF_X:
  1128. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1129. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1130. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1131. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1132. emit(A64_CMP(is64, dst, src), ctx);
  1133. emit_cond_jmp:
  1134. jmp_offset = bpf2a64_offset(i, off, ctx);
  1135. check_imm19(jmp_offset);
  1136. switch (BPF_OP(code)) {
  1137. case BPF_JEQ:
  1138. jmp_cond = A64_COND_EQ;
  1139. break;
  1140. case BPF_JGT:
  1141. jmp_cond = A64_COND_HI;
  1142. break;
  1143. case BPF_JLT:
  1144. jmp_cond = A64_COND_CC;
  1145. break;
  1146. case BPF_JGE:
  1147. jmp_cond = A64_COND_CS;
  1148. break;
  1149. case BPF_JLE:
  1150. jmp_cond = A64_COND_LS;
  1151. break;
  1152. case BPF_JSET:
  1153. case BPF_JNE:
  1154. jmp_cond = A64_COND_NE;
  1155. break;
  1156. case BPF_JSGT:
  1157. jmp_cond = A64_COND_GT;
  1158. break;
  1159. case BPF_JSLT:
  1160. jmp_cond = A64_COND_LT;
  1161. break;
  1162. case BPF_JSGE:
  1163. jmp_cond = A64_COND_GE;
  1164. break;
  1165. case BPF_JSLE:
  1166. jmp_cond = A64_COND_LE;
  1167. break;
  1168. default:
  1169. return -EFAULT;
  1170. }
  1171. emit(A64_B_(jmp_cond, jmp_offset), ctx);
  1172. break;
  1173. case BPF_JMP | BPF_JSET | BPF_X:
  1174. case BPF_JMP32 | BPF_JSET | BPF_X:
  1175. emit(A64_TST(is64, dst, src), ctx);
  1176. goto emit_cond_jmp;
  1177. /* IF (dst COND imm) JUMP off */
  1178. case BPF_JMP | BPF_JEQ | BPF_K:
  1179. case BPF_JMP | BPF_JGT | BPF_K:
  1180. case BPF_JMP | BPF_JLT | BPF_K:
  1181. case BPF_JMP | BPF_JGE | BPF_K:
  1182. case BPF_JMP | BPF_JLE | BPF_K:
  1183. case BPF_JMP | BPF_JNE | BPF_K:
  1184. case BPF_JMP | BPF_JSGT | BPF_K:
  1185. case BPF_JMP | BPF_JSLT | BPF_K:
  1186. case BPF_JMP | BPF_JSGE | BPF_K:
  1187. case BPF_JMP | BPF_JSLE | BPF_K:
  1188. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1189. case BPF_JMP32 | BPF_JGT | BPF_K:
  1190. case BPF_JMP32 | BPF_JLT | BPF_K:
  1191. case BPF_JMP32 | BPF_JGE | BPF_K:
  1192. case BPF_JMP32 | BPF_JLE | BPF_K:
  1193. case BPF_JMP32 | BPF_JNE | BPF_K:
  1194. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1195. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1196. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1197. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1198. if (is_addsub_imm(imm)) {
  1199. emit(A64_CMP_I(is64, dst, imm), ctx);
  1200. } else if (is_addsub_imm(-imm)) {
  1201. emit(A64_CMN_I(is64, dst, -imm), ctx);
  1202. } else {
  1203. emit_a64_mov_i(is64, tmp, imm, ctx);
  1204. emit(A64_CMP(is64, dst, tmp), ctx);
  1205. }
  1206. goto emit_cond_jmp;
  1207. case BPF_JMP | BPF_JSET | BPF_K:
  1208. case BPF_JMP32 | BPF_JSET | BPF_K:
  1209. a64_insn = A64_TST_I(is64, dst, imm);
  1210. if (a64_insn != AARCH64_BREAK_FAULT) {
  1211. emit(a64_insn, ctx);
  1212. } else {
  1213. emit_a64_mov_i(is64, tmp, imm, ctx);
  1214. emit(A64_TST(is64, dst, tmp), ctx);
  1215. }
  1216. goto emit_cond_jmp;
  1217. /* function call */
  1218. case BPF_JMP | BPF_CALL:
  1219. {
  1220. const u8 r0 = bpf2a64[BPF_REG_0];
  1221. bool func_addr_fixed;
  1222. u64 func_addr;
  1223. u32 cpu_offset;
  1224. /* Implement helper call to bpf_get_smp_processor_id() inline */
  1225. if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
  1226. cpu_offset = offsetof(struct thread_info, cpu);
  1227. emit(A64_MRS_SP_EL0(tmp), ctx);
  1228. if (is_lsi_offset(cpu_offset, 2)) {
  1229. emit(A64_LDR32I(r0, tmp, cpu_offset), ctx);
  1230. } else {
  1231. emit_a64_mov_i(1, tmp2, cpu_offset, ctx);
  1232. emit(A64_LDR32(r0, tmp, tmp2), ctx);
  1233. }
  1234. break;
  1235. }
  1236. /* Implement helper call to bpf_get_current_task/_btf() inline */
  1237. if (insn->src_reg == 0 && (insn->imm == BPF_FUNC_get_current_task ||
  1238. insn->imm == BPF_FUNC_get_current_task_btf)) {
  1239. emit(A64_MRS_SP_EL0(r0), ctx);
  1240. break;
  1241. }
  1242. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
  1243. &func_addr, &func_addr_fixed);
  1244. if (ret < 0)
  1245. return ret;
  1246. emit_call(func_addr, ctx);
  1247. emit(A64_MOV(1, r0, A64_R(0)), ctx);
  1248. break;
  1249. }
  1250. /* tail call */
  1251. case BPF_JMP | BPF_TAIL_CALL:
  1252. if (emit_bpf_tail_call(ctx))
  1253. return -EFAULT;
  1254. break;
  1255. /* function return */
  1256. case BPF_JMP | BPF_EXIT:
  1257. /* Optimization: when last instruction is EXIT,
  1258. simply fallthrough to epilogue. */
  1259. if (i == ctx->prog->len - 1)
  1260. break;
  1261. jmp_offset = epilogue_offset(ctx);
  1262. check_imm26(jmp_offset);
  1263. emit(A64_B(jmp_offset), ctx);
  1264. break;
  1265. /* dst = imm64 */
  1266. case BPF_LD | BPF_IMM | BPF_DW:
  1267. {
  1268. const struct bpf_insn insn1 = insn[1];
  1269. u64 imm64;
  1270. imm64 = (u64)insn1.imm << 32 | (u32)imm;
  1271. if (bpf_pseudo_func(insn))
  1272. emit_addr_mov_i64(dst, imm64, ctx);
  1273. else
  1274. emit_a64_mov_i64(dst, imm64, ctx);
  1275. return 1;
  1276. }
  1277. /* LDX: dst = (u64)*(unsigned size *)(src + off) */
  1278. case BPF_LDX | BPF_MEM | BPF_W:
  1279. case BPF_LDX | BPF_MEM | BPF_H:
  1280. case BPF_LDX | BPF_MEM | BPF_B:
  1281. case BPF_LDX | BPF_MEM | BPF_DW:
  1282. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  1283. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  1284. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  1285. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  1286. /* LDXS: dst_reg = (s64)*(signed size *)(src_reg + off) */
  1287. case BPF_LDX | BPF_MEMSX | BPF_B:
  1288. case BPF_LDX | BPF_MEMSX | BPF_H:
  1289. case BPF_LDX | BPF_MEMSX | BPF_W:
  1290. case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
  1291. case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
  1292. case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
  1293. case BPF_LDX | BPF_PROBE_MEM32 | BPF_B:
  1294. case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
  1295. case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
  1296. case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
  1297. if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
  1298. emit(A64_ADD(1, tmp2, src, arena_vm_base), ctx);
  1299. src = tmp2;
  1300. }
  1301. if (src == fp) {
  1302. src_adj = A64_SP;
  1303. off_adj = off + ctx->stack_size;
  1304. } else {
  1305. src_adj = src;
  1306. off_adj = off;
  1307. }
  1308. sign_extend = (BPF_MODE(insn->code) == BPF_MEMSX ||
  1309. BPF_MODE(insn->code) == BPF_PROBE_MEMSX);
  1310. switch (BPF_SIZE(code)) {
  1311. case BPF_W:
  1312. if (is_lsi_offset(off_adj, 2)) {
  1313. if (sign_extend)
  1314. emit(A64_LDRSWI(dst, src_adj, off_adj), ctx);
  1315. else
  1316. emit(A64_LDR32I(dst, src_adj, off_adj), ctx);
  1317. } else {
  1318. emit_a64_mov_i(1, tmp, off, ctx);
  1319. if (sign_extend)
  1320. emit(A64_LDRSW(dst, src, tmp), ctx);
  1321. else
  1322. emit(A64_LDR32(dst, src, tmp), ctx);
  1323. }
  1324. break;
  1325. case BPF_H:
  1326. if (is_lsi_offset(off_adj, 1)) {
  1327. if (sign_extend)
  1328. emit(A64_LDRSHI(dst, src_adj, off_adj), ctx);
  1329. else
  1330. emit(A64_LDRHI(dst, src_adj, off_adj), ctx);
  1331. } else {
  1332. emit_a64_mov_i(1, tmp, off, ctx);
  1333. if (sign_extend)
  1334. emit(A64_LDRSH(dst, src, tmp), ctx);
  1335. else
  1336. emit(A64_LDRH(dst, src, tmp), ctx);
  1337. }
  1338. break;
  1339. case BPF_B:
  1340. if (is_lsi_offset(off_adj, 0)) {
  1341. if (sign_extend)
  1342. emit(A64_LDRSBI(dst, src_adj, off_adj), ctx);
  1343. else
  1344. emit(A64_LDRBI(dst, src_adj, off_adj), ctx);
  1345. } else {
  1346. emit_a64_mov_i(1, tmp, off, ctx);
  1347. if (sign_extend)
  1348. emit(A64_LDRSB(dst, src, tmp), ctx);
  1349. else
  1350. emit(A64_LDRB(dst, src, tmp), ctx);
  1351. }
  1352. break;
  1353. case BPF_DW:
  1354. if (is_lsi_offset(off_adj, 3)) {
  1355. emit(A64_LDR64I(dst, src_adj, off_adj), ctx);
  1356. } else {
  1357. emit_a64_mov_i(1, tmp, off, ctx);
  1358. emit(A64_LDR64(dst, src, tmp), ctx);
  1359. }
  1360. break;
  1361. }
  1362. ret = add_exception_handler(insn, ctx, dst);
  1363. if (ret)
  1364. return ret;
  1365. break;
  1366. /* speculation barrier */
  1367. case BPF_ST | BPF_NOSPEC:
  1368. /*
  1369. * Nothing required here.
  1370. *
  1371. * In case of arm64, we rely on the firmware mitigation of
  1372. * Speculative Store Bypass as controlled via the ssbd kernel
  1373. * parameter. Whenever the mitigation is enabled, it works
  1374. * for all of the kernel code with no need to provide any
  1375. * additional instructions.
  1376. */
  1377. break;
  1378. /* ST: *(size *)(dst + off) = imm */
  1379. case BPF_ST | BPF_MEM | BPF_W:
  1380. case BPF_ST | BPF_MEM | BPF_H:
  1381. case BPF_ST | BPF_MEM | BPF_B:
  1382. case BPF_ST | BPF_MEM | BPF_DW:
  1383. case BPF_ST | BPF_PROBE_MEM32 | BPF_B:
  1384. case BPF_ST | BPF_PROBE_MEM32 | BPF_H:
  1385. case BPF_ST | BPF_PROBE_MEM32 | BPF_W:
  1386. case BPF_ST | BPF_PROBE_MEM32 | BPF_DW:
  1387. if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
  1388. emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx);
  1389. dst = tmp2;
  1390. }
  1391. if (dst == fp) {
  1392. dst_adj = A64_SP;
  1393. off_adj = off + ctx->stack_size;
  1394. } else {
  1395. dst_adj = dst;
  1396. off_adj = off;
  1397. }
  1398. /* Load imm to a register then store it */
  1399. emit_a64_mov_i(1, tmp, imm, ctx);
  1400. switch (BPF_SIZE(code)) {
  1401. case BPF_W:
  1402. if (is_lsi_offset(off_adj, 2)) {
  1403. emit(A64_STR32I(tmp, dst_adj, off_adj), ctx);
  1404. } else {
  1405. emit_a64_mov_i(1, tmp2, off, ctx);
  1406. emit(A64_STR32(tmp, dst, tmp2), ctx);
  1407. }
  1408. break;
  1409. case BPF_H:
  1410. if (is_lsi_offset(off_adj, 1)) {
  1411. emit(A64_STRHI(tmp, dst_adj, off_adj), ctx);
  1412. } else {
  1413. emit_a64_mov_i(1, tmp2, off, ctx);
  1414. emit(A64_STRH(tmp, dst, tmp2), ctx);
  1415. }
  1416. break;
  1417. case BPF_B:
  1418. if (is_lsi_offset(off_adj, 0)) {
  1419. emit(A64_STRBI(tmp, dst_adj, off_adj), ctx);
  1420. } else {
  1421. emit_a64_mov_i(1, tmp2, off, ctx);
  1422. emit(A64_STRB(tmp, dst, tmp2), ctx);
  1423. }
  1424. break;
  1425. case BPF_DW:
  1426. if (is_lsi_offset(off_adj, 3)) {
  1427. emit(A64_STR64I(tmp, dst_adj, off_adj), ctx);
  1428. } else {
  1429. emit_a64_mov_i(1, tmp2, off, ctx);
  1430. emit(A64_STR64(tmp, dst, tmp2), ctx);
  1431. }
  1432. break;
  1433. }
  1434. ret = add_exception_handler(insn, ctx, dst);
  1435. if (ret)
  1436. return ret;
  1437. break;
  1438. /* STX: *(size *)(dst + off) = src */
  1439. case BPF_STX | BPF_MEM | BPF_W:
  1440. case BPF_STX | BPF_MEM | BPF_H:
  1441. case BPF_STX | BPF_MEM | BPF_B:
  1442. case BPF_STX | BPF_MEM | BPF_DW:
  1443. case BPF_STX | BPF_PROBE_MEM32 | BPF_B:
  1444. case BPF_STX | BPF_PROBE_MEM32 | BPF_H:
  1445. case BPF_STX | BPF_PROBE_MEM32 | BPF_W:
  1446. case BPF_STX | BPF_PROBE_MEM32 | BPF_DW:
  1447. if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
  1448. emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx);
  1449. dst = tmp2;
  1450. }
  1451. if (dst == fp) {
  1452. dst_adj = A64_SP;
  1453. off_adj = off + ctx->stack_size;
  1454. } else {
  1455. dst_adj = dst;
  1456. off_adj = off;
  1457. }
  1458. switch (BPF_SIZE(code)) {
  1459. case BPF_W:
  1460. if (is_lsi_offset(off_adj, 2)) {
  1461. emit(A64_STR32I(src, dst_adj, off_adj), ctx);
  1462. } else {
  1463. emit_a64_mov_i(1, tmp, off, ctx);
  1464. emit(A64_STR32(src, dst, tmp), ctx);
  1465. }
  1466. break;
  1467. case BPF_H:
  1468. if (is_lsi_offset(off_adj, 1)) {
  1469. emit(A64_STRHI(src, dst_adj, off_adj), ctx);
  1470. } else {
  1471. emit_a64_mov_i(1, tmp, off, ctx);
  1472. emit(A64_STRH(src, dst, tmp), ctx);
  1473. }
  1474. break;
  1475. case BPF_B:
  1476. if (is_lsi_offset(off_adj, 0)) {
  1477. emit(A64_STRBI(src, dst_adj, off_adj), ctx);
  1478. } else {
  1479. emit_a64_mov_i(1, tmp, off, ctx);
  1480. emit(A64_STRB(src, dst, tmp), ctx);
  1481. }
  1482. break;
  1483. case BPF_DW:
  1484. if (is_lsi_offset(off_adj, 3)) {
  1485. emit(A64_STR64I(src, dst_adj, off_adj), ctx);
  1486. } else {
  1487. emit_a64_mov_i(1, tmp, off, ctx);
  1488. emit(A64_STR64(src, dst, tmp), ctx);
  1489. }
  1490. break;
  1491. }
  1492. ret = add_exception_handler(insn, ctx, dst);
  1493. if (ret)
  1494. return ret;
  1495. break;
  1496. case BPF_STX | BPF_ATOMIC | BPF_W:
  1497. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1498. case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
  1499. case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
  1500. if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
  1501. ret = emit_lse_atomic(insn, ctx);
  1502. else
  1503. ret = emit_ll_sc_atomic(insn, ctx);
  1504. if (ret)
  1505. return ret;
  1506. ret = add_exception_handler(insn, ctx, dst);
  1507. if (ret)
  1508. return ret;
  1509. break;
  1510. default:
  1511. pr_err_once("unknown opcode %02x\n", code);
  1512. return -EINVAL;
  1513. }
  1514. return 0;
  1515. }
  1516. static int build_body(struct jit_ctx *ctx, bool extra_pass)
  1517. {
  1518. const struct bpf_prog *prog = ctx->prog;
  1519. int i;
  1520. /*
  1521. * - offset[0] offset of the end of prologue,
  1522. * start of the 1st instruction.
  1523. * - offset[1] - offset of the end of 1st instruction,
  1524. * start of the 2nd instruction
  1525. * [....]
  1526. * - offset[3] - offset of the end of 3rd instruction,
  1527. * start of 4th instruction
  1528. */
  1529. for (i = 0; i < prog->len; i++) {
  1530. const struct bpf_insn *insn = &prog->insnsi[i];
  1531. int ret;
  1532. ctx->offset[i] = ctx->idx;
  1533. ret = build_insn(insn, ctx, extra_pass);
  1534. if (ret > 0) {
  1535. i++;
  1536. ctx->offset[i] = ctx->idx;
  1537. continue;
  1538. }
  1539. if (ret)
  1540. return ret;
  1541. }
  1542. /*
  1543. * offset is allocated with prog->len + 1 so fill in
  1544. * the last element with the offset after the last
  1545. * instruction (end of program)
  1546. */
  1547. ctx->offset[i] = ctx->idx;
  1548. return 0;
  1549. }
  1550. static int validate_code(struct jit_ctx *ctx)
  1551. {
  1552. int i;
  1553. for (i = 0; i < ctx->idx; i++) {
  1554. u32 a64_insn = le32_to_cpu(ctx->image[i]);
  1555. if (a64_insn == AARCH64_BREAK_FAULT)
  1556. return -1;
  1557. }
  1558. return 0;
  1559. }
  1560. static int validate_ctx(struct jit_ctx *ctx)
  1561. {
  1562. if (validate_code(ctx))
  1563. return -1;
  1564. if (WARN_ON_ONCE(ctx->exentry_idx != ctx->prog->aux->num_exentries))
  1565. return -1;
  1566. return 0;
  1567. }
  1568. static inline void bpf_flush_icache(void *start, void *end)
  1569. {
  1570. flush_icache_range((unsigned long)start, (unsigned long)end);
  1571. }
  1572. struct arm64_jit_data {
  1573. struct bpf_binary_header *header;
  1574. u8 *ro_image;
  1575. struct bpf_binary_header *ro_header;
  1576. struct jit_ctx ctx;
  1577. };
  1578. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1579. {
  1580. int image_size, prog_size, extable_size, extable_align, extable_offset;
  1581. struct bpf_prog *tmp, *orig_prog = prog;
  1582. struct bpf_binary_header *header;
  1583. struct bpf_binary_header *ro_header;
  1584. struct arm64_jit_data *jit_data;
  1585. bool was_classic = bpf_prog_was_classic(prog);
  1586. bool tmp_blinded = false;
  1587. bool extra_pass = false;
  1588. struct jit_ctx ctx;
  1589. u8 *image_ptr;
  1590. u8 *ro_image_ptr;
  1591. int body_idx;
  1592. int exentry_idx;
  1593. if (!prog->jit_requested)
  1594. return orig_prog;
  1595. tmp = bpf_jit_blind_constants(prog);
  1596. /* If blinding was requested and we failed during blinding,
  1597. * we must fall back to the interpreter.
  1598. */
  1599. if (IS_ERR(tmp))
  1600. return orig_prog;
  1601. if (tmp != prog) {
  1602. tmp_blinded = true;
  1603. prog = tmp;
  1604. }
  1605. jit_data = prog->aux->jit_data;
  1606. if (!jit_data) {
  1607. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  1608. if (!jit_data) {
  1609. prog = orig_prog;
  1610. goto out;
  1611. }
  1612. prog->aux->jit_data = jit_data;
  1613. }
  1614. if (jit_data->ctx.offset) {
  1615. ctx = jit_data->ctx;
  1616. ro_image_ptr = jit_data->ro_image;
  1617. ro_header = jit_data->ro_header;
  1618. header = jit_data->header;
  1619. image_ptr = (void *)header + ((void *)ro_image_ptr
  1620. - (void *)ro_header);
  1621. extra_pass = true;
  1622. prog_size = sizeof(u32) * ctx.idx;
  1623. goto skip_init_ctx;
  1624. }
  1625. memset(&ctx, 0, sizeof(ctx));
  1626. ctx.prog = prog;
  1627. ctx.offset = kvcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
  1628. if (ctx.offset == NULL) {
  1629. prog = orig_prog;
  1630. goto out_off;
  1631. }
  1632. ctx.user_vm_start = bpf_arena_get_user_vm_start(prog->aux->arena);
  1633. ctx.arena_vm_start = bpf_arena_get_kern_vm_start(prog->aux->arena);
  1634. /* Pass 1: Estimate the maximum image size.
  1635. *
  1636. * BPF line info needs ctx->offset[i] to be the offset of
  1637. * instruction[i] in jited image, so build prologue first.
  1638. */
  1639. if (build_prologue(&ctx, was_classic)) {
  1640. prog = orig_prog;
  1641. goto out_off;
  1642. }
  1643. if (build_body(&ctx, extra_pass)) {
  1644. prog = orig_prog;
  1645. goto out_off;
  1646. }
  1647. ctx.epilogue_offset = ctx.idx;
  1648. build_epilogue(&ctx);
  1649. build_plt(&ctx);
  1650. extable_align = __alignof__(struct exception_table_entry);
  1651. extable_size = prog->aux->num_exentries *
  1652. sizeof(struct exception_table_entry);
  1653. /* Now we know the maximum image size. */
  1654. prog_size = sizeof(u32) * ctx.idx;
  1655. /* also allocate space for plt target */
  1656. extable_offset = round_up(prog_size + PLT_TARGET_SIZE, extable_align);
  1657. image_size = extable_offset + extable_size;
  1658. ro_header = bpf_jit_binary_pack_alloc(image_size, &ro_image_ptr,
  1659. sizeof(u32), &header, &image_ptr,
  1660. jit_fill_hole);
  1661. if (!ro_header) {
  1662. prog = orig_prog;
  1663. goto out_off;
  1664. }
  1665. /* Pass 2: Determine jited position and result for each instruction */
  1666. /*
  1667. * Use the image(RW) for writing the JITed instructions. But also save
  1668. * the ro_image(RX) for calculating the offsets in the image. The RW
  1669. * image will be later copied to the RX image from where the program
  1670. * will run. The bpf_jit_binary_pack_finalize() will do this copy in the
  1671. * final step.
  1672. */
  1673. ctx.image = (__le32 *)image_ptr;
  1674. ctx.ro_image = (__le32 *)ro_image_ptr;
  1675. if (extable_size)
  1676. prog->aux->extable = (void *)ro_image_ptr + extable_offset;
  1677. skip_init_ctx:
  1678. ctx.idx = 0;
  1679. ctx.exentry_idx = 0;
  1680. ctx.write = true;
  1681. build_prologue(&ctx, was_classic);
  1682. /* Record exentry_idx and body_idx before first build_body */
  1683. exentry_idx = ctx.exentry_idx;
  1684. body_idx = ctx.idx;
  1685. /* Dont write body instructions to memory for now */
  1686. ctx.write = false;
  1687. if (build_body(&ctx, extra_pass)) {
  1688. prog = orig_prog;
  1689. goto out_free_hdr;
  1690. }
  1691. ctx.epilogue_offset = ctx.idx;
  1692. ctx.exentry_idx = exentry_idx;
  1693. ctx.idx = body_idx;
  1694. ctx.write = true;
  1695. /* Pass 3: Adjust jump offset and write final image */
  1696. if (build_body(&ctx, extra_pass) ||
  1697. WARN_ON_ONCE(ctx.idx != ctx.epilogue_offset)) {
  1698. prog = orig_prog;
  1699. goto out_free_hdr;
  1700. }
  1701. build_epilogue(&ctx);
  1702. build_plt(&ctx);
  1703. /* Extra pass to validate JITed code. */
  1704. if (validate_ctx(&ctx)) {
  1705. prog = orig_prog;
  1706. goto out_free_hdr;
  1707. }
  1708. /* update the real prog size */
  1709. prog_size = sizeof(u32) * ctx.idx;
  1710. /* And we're done. */
  1711. if (bpf_jit_enable > 1)
  1712. bpf_jit_dump(prog->len, prog_size, 2, ctx.image);
  1713. if (!prog->is_func || extra_pass) {
  1714. /* The jited image may shrink since the jited result for
  1715. * BPF_CALL to subprog may be changed from indirect call
  1716. * to direct call.
  1717. */
  1718. if (extra_pass && ctx.idx > jit_data->ctx.idx) {
  1719. pr_err_once("multi-func JIT bug %d > %d\n",
  1720. ctx.idx, jit_data->ctx.idx);
  1721. prog->bpf_func = NULL;
  1722. prog->jited = 0;
  1723. prog->jited_len = 0;
  1724. goto out_free_hdr;
  1725. }
  1726. if (WARN_ON(bpf_jit_binary_pack_finalize(ro_header, header))) {
  1727. /* ro_header has been freed */
  1728. ro_header = NULL;
  1729. prog = orig_prog;
  1730. goto out_off;
  1731. }
  1732. /*
  1733. * The instructions have now been copied to the ROX region from
  1734. * where they will execute. Now the data cache has to be cleaned to
  1735. * the PoU and the I-cache has to be invalidated for the VAs.
  1736. */
  1737. bpf_flush_icache(ro_header, ctx.ro_image + ctx.idx);
  1738. } else {
  1739. jit_data->ctx = ctx;
  1740. jit_data->ro_image = ro_image_ptr;
  1741. jit_data->header = header;
  1742. jit_data->ro_header = ro_header;
  1743. }
  1744. prog->bpf_func = (void *)ctx.ro_image;
  1745. prog->jited = 1;
  1746. prog->jited_len = prog_size;
  1747. if (!prog->is_func || extra_pass) {
  1748. int i;
  1749. /* offset[prog->len] is the size of program */
  1750. for (i = 0; i <= prog->len; i++)
  1751. ctx.offset[i] *= AARCH64_INSN_SIZE;
  1752. bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
  1753. out_off:
  1754. kvfree(ctx.offset);
  1755. kfree(jit_data);
  1756. prog->aux->jit_data = NULL;
  1757. }
  1758. out:
  1759. if (tmp_blinded)
  1760. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  1761. tmp : orig_prog);
  1762. return prog;
  1763. out_free_hdr:
  1764. if (header) {
  1765. bpf_arch_text_copy(&ro_header->size, &header->size,
  1766. sizeof(header->size));
  1767. bpf_jit_binary_pack_free(ro_header, header);
  1768. }
  1769. goto out_off;
  1770. }
  1771. bool bpf_jit_supports_kfunc_call(void)
  1772. {
  1773. return true;
  1774. }
  1775. void *bpf_arch_text_copy(void *dst, void *src, size_t len)
  1776. {
  1777. if (!aarch64_insn_copy(dst, src, len))
  1778. return ERR_PTR(-EINVAL);
  1779. return dst;
  1780. }
  1781. u64 bpf_jit_alloc_exec_limit(void)
  1782. {
  1783. return VMALLOC_END - VMALLOC_START;
  1784. }
  1785. /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
  1786. bool bpf_jit_supports_subprog_tailcalls(void)
  1787. {
  1788. return true;
  1789. }
  1790. static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_link *l,
  1791. int args_off, int retval_off, int run_ctx_off,
  1792. bool save_ret)
  1793. {
  1794. __le32 *branch;
  1795. u64 enter_prog;
  1796. u64 exit_prog;
  1797. struct bpf_prog *p = l->link.prog;
  1798. int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
  1799. enter_prog = (u64)bpf_trampoline_enter(p);
  1800. exit_prog = (u64)bpf_trampoline_exit(p);
  1801. if (l->cookie == 0) {
  1802. /* if cookie is zero, one instruction is enough to store it */
  1803. emit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx);
  1804. } else {
  1805. emit_a64_mov_i64(A64_R(10), l->cookie, ctx);
  1806. emit(A64_STR64I(A64_R(10), A64_SP, run_ctx_off + cookie_off),
  1807. ctx);
  1808. }
  1809. /* save p to callee saved register x19 to avoid loading p with mov_i64
  1810. * each time.
  1811. */
  1812. emit_addr_mov_i64(A64_R(19), (const u64)p, ctx);
  1813. /* arg1: prog */
  1814. emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
  1815. /* arg2: &run_ctx */
  1816. emit(A64_ADD_I(1, A64_R(1), A64_SP, run_ctx_off), ctx);
  1817. emit_call(enter_prog, ctx);
  1818. /* save return value to callee saved register x20 */
  1819. emit(A64_MOV(1, A64_R(20), A64_R(0)), ctx);
  1820. /* if (__bpf_prog_enter(prog) == 0)
  1821. * goto skip_exec_of_prog;
  1822. */
  1823. branch = ctx->image + ctx->idx;
  1824. emit(A64_NOP, ctx);
  1825. emit(A64_ADD_I(1, A64_R(0), A64_SP, args_off), ctx);
  1826. if (!p->jited)
  1827. emit_addr_mov_i64(A64_R(1), (const u64)p->insnsi, ctx);
  1828. emit_call((const u64)p->bpf_func, ctx);
  1829. if (save_ret)
  1830. emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
  1831. if (ctx->image) {
  1832. int offset = &ctx->image[ctx->idx] - branch;
  1833. *branch = cpu_to_le32(A64_CBZ(1, A64_R(0), offset));
  1834. }
  1835. /* arg1: prog */
  1836. emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
  1837. /* arg2: start time */
  1838. emit(A64_MOV(1, A64_R(1), A64_R(20)), ctx);
  1839. /* arg3: &run_ctx */
  1840. emit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx);
  1841. emit_call(exit_prog, ctx);
  1842. }
  1843. static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_links *tl,
  1844. int args_off, int retval_off, int run_ctx_off,
  1845. __le32 **branches)
  1846. {
  1847. int i;
  1848. /* The first fmod_ret program will receive a garbage return value.
  1849. * Set this to 0 to avoid confusing the program.
  1850. */
  1851. emit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx);
  1852. for (i = 0; i < tl->nr_links; i++) {
  1853. invoke_bpf_prog(ctx, tl->links[i], args_off, retval_off,
  1854. run_ctx_off, true);
  1855. /* if (*(u64 *)(sp + retval_off) != 0)
  1856. * goto do_fexit;
  1857. */
  1858. emit(A64_LDR64I(A64_R(10), A64_SP, retval_off), ctx);
  1859. /* Save the location of branch, and generate a nop.
  1860. * This nop will be replaced with a cbnz later.
  1861. */
  1862. branches[i] = ctx->image + ctx->idx;
  1863. emit(A64_NOP, ctx);
  1864. }
  1865. }
  1866. static void save_args(struct jit_ctx *ctx, int args_off, int nregs)
  1867. {
  1868. int i;
  1869. for (i = 0; i < nregs; i++) {
  1870. emit(A64_STR64I(i, A64_SP, args_off), ctx);
  1871. args_off += 8;
  1872. }
  1873. }
  1874. static void restore_args(struct jit_ctx *ctx, int args_off, int nregs)
  1875. {
  1876. int i;
  1877. for (i = 0; i < nregs; i++) {
  1878. emit(A64_LDR64I(i, A64_SP, args_off), ctx);
  1879. args_off += 8;
  1880. }
  1881. }
  1882. static bool is_struct_ops_tramp(const struct bpf_tramp_links *fentry_links)
  1883. {
  1884. return fentry_links->nr_links == 1 &&
  1885. fentry_links->links[0]->link.type == BPF_LINK_TYPE_STRUCT_OPS;
  1886. }
  1887. /* Based on the x86's implementation of arch_prepare_bpf_trampoline().
  1888. *
  1889. * bpf prog and function entry before bpf trampoline hooked:
  1890. * mov x9, lr
  1891. * nop
  1892. *
  1893. * bpf prog and function entry after bpf trampoline hooked:
  1894. * mov x9, lr
  1895. * bl <bpf_trampoline or plt>
  1896. *
  1897. */
  1898. static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
  1899. struct bpf_tramp_links *tlinks, void *func_addr,
  1900. int nregs, u32 flags)
  1901. {
  1902. int i;
  1903. int stack_size;
  1904. int retaddr_off;
  1905. int regs_off;
  1906. int retval_off;
  1907. int args_off;
  1908. int nregs_off;
  1909. int ip_off;
  1910. int run_ctx_off;
  1911. struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
  1912. struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
  1913. struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
  1914. bool save_ret;
  1915. __le32 **branches = NULL;
  1916. bool is_struct_ops = is_struct_ops_tramp(fentry);
  1917. /* trampoline stack layout:
  1918. * [ parent ip ]
  1919. * [ FP ]
  1920. * SP + retaddr_off [ self ip ]
  1921. * [ FP ]
  1922. *
  1923. * [ padding ] align SP to multiples of 16
  1924. *
  1925. * [ x20 ] callee saved reg x20
  1926. * SP + regs_off [ x19 ] callee saved reg x19
  1927. *
  1928. * SP + retval_off [ return value ] BPF_TRAMP_F_CALL_ORIG or
  1929. * BPF_TRAMP_F_RET_FENTRY_RET
  1930. *
  1931. * [ arg reg N ]
  1932. * [ ... ]
  1933. * SP + args_off [ arg reg 1 ]
  1934. *
  1935. * SP + nregs_off [ arg regs count ]
  1936. *
  1937. * SP + ip_off [ traced function ] BPF_TRAMP_F_IP_ARG flag
  1938. *
  1939. * SP + run_ctx_off [ bpf_tramp_run_ctx ]
  1940. */
  1941. stack_size = 0;
  1942. run_ctx_off = stack_size;
  1943. /* room for bpf_tramp_run_ctx */
  1944. stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8);
  1945. ip_off = stack_size;
  1946. /* room for IP address argument */
  1947. if (flags & BPF_TRAMP_F_IP_ARG)
  1948. stack_size += 8;
  1949. nregs_off = stack_size;
  1950. /* room for args count */
  1951. stack_size += 8;
  1952. args_off = stack_size;
  1953. /* room for args */
  1954. stack_size += nregs * 8;
  1955. /* room for return value */
  1956. retval_off = stack_size;
  1957. save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
  1958. if (save_ret)
  1959. stack_size += 8;
  1960. /* room for callee saved registers, currently x19 and x20 are used */
  1961. regs_off = stack_size;
  1962. stack_size += 16;
  1963. /* round up to multiples of 16 to avoid SPAlignmentFault */
  1964. stack_size = round_up(stack_size, 16);
  1965. /* return address locates above FP */
  1966. retaddr_off = stack_size + 8;
  1967. /* bpf trampoline may be invoked by 3 instruction types:
  1968. * 1. bl, attached to bpf prog or kernel function via short jump
  1969. * 2. br, attached to bpf prog or kernel function via long jump
  1970. * 3. blr, working as a function pointer, used by struct_ops.
  1971. * So BTI_JC should used here to support both br and blr.
  1972. */
  1973. emit_bti(A64_BTI_JC, ctx);
  1974. /* x9 is not set for struct_ops */
  1975. if (!is_struct_ops) {
  1976. /* frame for parent function */
  1977. emit(A64_PUSH(A64_FP, A64_R(9), A64_SP), ctx);
  1978. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  1979. }
  1980. /* frame for patched function for tracing, or caller for struct_ops */
  1981. emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
  1982. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  1983. /* allocate stack space */
  1984. emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx);
  1985. if (flags & BPF_TRAMP_F_IP_ARG) {
  1986. /* save ip address of the traced function */
  1987. emit_addr_mov_i64(A64_R(10), (const u64)func_addr, ctx);
  1988. emit(A64_STR64I(A64_R(10), A64_SP, ip_off), ctx);
  1989. }
  1990. /* save arg regs count*/
  1991. emit(A64_MOVZ(1, A64_R(10), nregs, 0), ctx);
  1992. emit(A64_STR64I(A64_R(10), A64_SP, nregs_off), ctx);
  1993. /* save arg regs */
  1994. save_args(ctx, args_off, nregs);
  1995. /* save callee saved registers */
  1996. emit(A64_STR64I(A64_R(19), A64_SP, regs_off), ctx);
  1997. emit(A64_STR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
  1998. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  1999. /* for the first pass, assume the worst case */
  2000. if (!ctx->image)
  2001. ctx->idx += 4;
  2002. else
  2003. emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
  2004. emit_call((const u64)__bpf_tramp_enter, ctx);
  2005. }
  2006. for (i = 0; i < fentry->nr_links; i++)
  2007. invoke_bpf_prog(ctx, fentry->links[i], args_off,
  2008. retval_off, run_ctx_off,
  2009. flags & BPF_TRAMP_F_RET_FENTRY_RET);
  2010. if (fmod_ret->nr_links) {
  2011. branches = kcalloc(fmod_ret->nr_links, sizeof(__le32 *),
  2012. GFP_KERNEL);
  2013. if (!branches)
  2014. return -ENOMEM;
  2015. invoke_bpf_mod_ret(ctx, fmod_ret, args_off, retval_off,
  2016. run_ctx_off, branches);
  2017. }
  2018. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  2019. restore_args(ctx, args_off, nregs);
  2020. /* call original func */
  2021. emit(A64_LDR64I(A64_R(10), A64_SP, retaddr_off), ctx);
  2022. emit(A64_ADR(A64_LR, AARCH64_INSN_SIZE * 2), ctx);
  2023. emit(A64_RET(A64_R(10)), ctx);
  2024. /* store return value */
  2025. emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
  2026. /* reserve a nop for bpf_tramp_image_put */
  2027. im->ip_after_call = ctx->ro_image + ctx->idx;
  2028. emit(A64_NOP, ctx);
  2029. }
  2030. /* update the branches saved in invoke_bpf_mod_ret with cbnz */
  2031. for (i = 0; i < fmod_ret->nr_links && ctx->image != NULL; i++) {
  2032. int offset = &ctx->image[ctx->idx] - branches[i];
  2033. *branches[i] = cpu_to_le32(A64_CBNZ(1, A64_R(10), offset));
  2034. }
  2035. for (i = 0; i < fexit->nr_links; i++)
  2036. invoke_bpf_prog(ctx, fexit->links[i], args_off, retval_off,
  2037. run_ctx_off, false);
  2038. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  2039. im->ip_epilogue = ctx->ro_image + ctx->idx;
  2040. /* for the first pass, assume the worst case */
  2041. if (!ctx->image)
  2042. ctx->idx += 4;
  2043. else
  2044. emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
  2045. emit_call((const u64)__bpf_tramp_exit, ctx);
  2046. }
  2047. if (flags & BPF_TRAMP_F_RESTORE_REGS)
  2048. restore_args(ctx, args_off, nregs);
  2049. /* restore callee saved register x19 and x20 */
  2050. emit(A64_LDR64I(A64_R(19), A64_SP, regs_off), ctx);
  2051. emit(A64_LDR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
  2052. if (save_ret)
  2053. emit(A64_LDR64I(A64_R(0), A64_SP, retval_off), ctx);
  2054. /* reset SP */
  2055. emit(A64_MOV(1, A64_SP, A64_FP), ctx);
  2056. if (is_struct_ops) {
  2057. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  2058. emit(A64_RET(A64_LR), ctx);
  2059. } else {
  2060. /* pop frames */
  2061. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  2062. emit(A64_POP(A64_FP, A64_R(9), A64_SP), ctx);
  2063. if (flags & BPF_TRAMP_F_SKIP_FRAME) {
  2064. /* skip patched function, return to parent */
  2065. emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
  2066. emit(A64_RET(A64_R(9)), ctx);
  2067. } else {
  2068. /* return to patched function */
  2069. emit(A64_MOV(1, A64_R(10), A64_LR), ctx);
  2070. emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
  2071. emit(A64_RET(A64_R(10)), ctx);
  2072. }
  2073. }
  2074. kfree(branches);
  2075. return ctx->idx;
  2076. }
  2077. static int btf_func_model_nregs(const struct btf_func_model *m)
  2078. {
  2079. int nregs = m->nr_args;
  2080. int i;
  2081. /* extra registers needed for struct argument */
  2082. for (i = 0; i < MAX_BPF_FUNC_ARGS; i++) {
  2083. /* The arg_size is at most 16 bytes, enforced by the verifier. */
  2084. if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
  2085. nregs += (m->arg_size[i] + 7) / 8 - 1;
  2086. }
  2087. return nregs;
  2088. }
  2089. int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
  2090. struct bpf_tramp_links *tlinks, void *func_addr)
  2091. {
  2092. struct jit_ctx ctx = {
  2093. .image = NULL,
  2094. .idx = 0,
  2095. };
  2096. struct bpf_tramp_image im;
  2097. int nregs, ret;
  2098. nregs = btf_func_model_nregs(m);
  2099. /* the first 8 registers are used for arguments */
  2100. if (nregs > 8)
  2101. return -ENOTSUPP;
  2102. ret = prepare_trampoline(&ctx, &im, tlinks, func_addr, nregs, flags);
  2103. if (ret < 0)
  2104. return ret;
  2105. return ret < 0 ? ret : ret * AARCH64_INSN_SIZE;
  2106. }
  2107. void *arch_alloc_bpf_trampoline(unsigned int size)
  2108. {
  2109. return bpf_prog_pack_alloc(size, jit_fill_hole);
  2110. }
  2111. void arch_free_bpf_trampoline(void *image, unsigned int size)
  2112. {
  2113. bpf_prog_pack_free(image, size);
  2114. }
  2115. int arch_protect_bpf_trampoline(void *image, unsigned int size)
  2116. {
  2117. return 0;
  2118. }
  2119. int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
  2120. void *ro_image_end, const struct btf_func_model *m,
  2121. u32 flags, struct bpf_tramp_links *tlinks,
  2122. void *func_addr)
  2123. {
  2124. int ret, nregs;
  2125. void *image, *tmp;
  2126. u32 size = ro_image_end - ro_image;
  2127. /* image doesn't need to be in module memory range, so we can
  2128. * use kvmalloc.
  2129. */
  2130. image = kvmalloc(size, GFP_KERNEL);
  2131. if (!image)
  2132. return -ENOMEM;
  2133. struct jit_ctx ctx = {
  2134. .image = image,
  2135. .ro_image = ro_image,
  2136. .idx = 0,
  2137. .write = true,
  2138. };
  2139. nregs = btf_func_model_nregs(m);
  2140. /* the first 8 registers are used for arguments */
  2141. if (nregs > 8)
  2142. return -ENOTSUPP;
  2143. jit_fill_hole(image, (unsigned int)(ro_image_end - ro_image));
  2144. ret = prepare_trampoline(&ctx, im, tlinks, func_addr, nregs, flags);
  2145. if (ret > 0 && validate_code(&ctx) < 0) {
  2146. ret = -EINVAL;
  2147. goto out;
  2148. }
  2149. if (ret > 0)
  2150. ret *= AARCH64_INSN_SIZE;
  2151. tmp = bpf_arch_text_copy(ro_image, image, size);
  2152. if (IS_ERR(tmp)) {
  2153. ret = PTR_ERR(tmp);
  2154. goto out;
  2155. }
  2156. bpf_flush_icache(ro_image, ro_image + size);
  2157. out:
  2158. kvfree(image);
  2159. return ret;
  2160. }
  2161. static bool is_long_jump(void *ip, void *target)
  2162. {
  2163. long offset;
  2164. /* NULL target means this is a NOP */
  2165. if (!target)
  2166. return false;
  2167. offset = (long)target - (long)ip;
  2168. return offset < -SZ_128M || offset >= SZ_128M;
  2169. }
  2170. static int gen_branch_or_nop(enum aarch64_insn_branch_type type, void *ip,
  2171. void *addr, void *plt, u32 *insn)
  2172. {
  2173. void *target;
  2174. if (!addr) {
  2175. *insn = aarch64_insn_gen_nop();
  2176. return 0;
  2177. }
  2178. if (is_long_jump(ip, addr))
  2179. target = plt;
  2180. else
  2181. target = addr;
  2182. *insn = aarch64_insn_gen_branch_imm((unsigned long)ip,
  2183. (unsigned long)target,
  2184. type);
  2185. return *insn != AARCH64_BREAK_FAULT ? 0 : -EFAULT;
  2186. }
  2187. /* Replace the branch instruction from @ip to @old_addr in a bpf prog or a bpf
  2188. * trampoline with the branch instruction from @ip to @new_addr. If @old_addr
  2189. * or @new_addr is NULL, the old or new instruction is NOP.
  2190. *
  2191. * When @ip is the bpf prog entry, a bpf trampoline is being attached or
  2192. * detached. Since bpf trampoline and bpf prog are allocated separately with
  2193. * vmalloc, the address distance may exceed 128MB, the maximum branch range.
  2194. * So long jump should be handled.
  2195. *
  2196. * When a bpf prog is constructed, a plt pointing to empty trampoline
  2197. * dummy_tramp is placed at the end:
  2198. *
  2199. * bpf_prog:
  2200. * mov x9, lr
  2201. * nop // patchsite
  2202. * ...
  2203. * ret
  2204. *
  2205. * plt:
  2206. * ldr x10, target
  2207. * br x10
  2208. * target:
  2209. * .quad dummy_tramp // plt target
  2210. *
  2211. * This is also the state when no trampoline is attached.
  2212. *
  2213. * When a short-jump bpf trampoline is attached, the patchsite is patched
  2214. * to a bl instruction to the trampoline directly:
  2215. *
  2216. * bpf_prog:
  2217. * mov x9, lr
  2218. * bl <short-jump bpf trampoline address> // patchsite
  2219. * ...
  2220. * ret
  2221. *
  2222. * plt:
  2223. * ldr x10, target
  2224. * br x10
  2225. * target:
  2226. * .quad dummy_tramp // plt target
  2227. *
  2228. * When a long-jump bpf trampoline is attached, the plt target is filled with
  2229. * the trampoline address and the patchsite is patched to a bl instruction to
  2230. * the plt:
  2231. *
  2232. * bpf_prog:
  2233. * mov x9, lr
  2234. * bl plt // patchsite
  2235. * ...
  2236. * ret
  2237. *
  2238. * plt:
  2239. * ldr x10, target
  2240. * br x10
  2241. * target:
  2242. * .quad <long-jump bpf trampoline address> // plt target
  2243. *
  2244. * The dummy_tramp is used to prevent another CPU from jumping to unknown
  2245. * locations during the patching process, making the patching process easier.
  2246. */
  2247. int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
  2248. void *old_addr, void *new_addr)
  2249. {
  2250. int ret;
  2251. u32 old_insn;
  2252. u32 new_insn;
  2253. u32 replaced;
  2254. struct bpf_plt *plt = NULL;
  2255. unsigned long size = 0UL;
  2256. unsigned long offset = ~0UL;
  2257. enum aarch64_insn_branch_type branch_type;
  2258. char namebuf[KSYM_NAME_LEN];
  2259. void *image = NULL;
  2260. u64 plt_target = 0ULL;
  2261. bool poking_bpf_entry;
  2262. if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
  2263. /* Only poking bpf text is supported. Since kernel function
  2264. * entry is set up by ftrace, we reply on ftrace to poke kernel
  2265. * functions.
  2266. */
  2267. return -ENOTSUPP;
  2268. image = ip - offset;
  2269. /* zero offset means we're poking bpf prog entry */
  2270. poking_bpf_entry = (offset == 0UL);
  2271. /* bpf prog entry, find plt and the real patchsite */
  2272. if (poking_bpf_entry) {
  2273. /* plt locates at the end of bpf prog */
  2274. plt = image + size - PLT_TARGET_OFFSET;
  2275. /* skip to the nop instruction in bpf prog entry:
  2276. * bti c // if BTI enabled
  2277. * mov x9, x30
  2278. * nop
  2279. */
  2280. ip = image + POKE_OFFSET * AARCH64_INSN_SIZE;
  2281. }
  2282. /* long jump is only possible at bpf prog entry */
  2283. if (WARN_ON((is_long_jump(ip, new_addr) || is_long_jump(ip, old_addr)) &&
  2284. !poking_bpf_entry))
  2285. return -EINVAL;
  2286. if (poke_type == BPF_MOD_CALL)
  2287. branch_type = AARCH64_INSN_BRANCH_LINK;
  2288. else
  2289. branch_type = AARCH64_INSN_BRANCH_NOLINK;
  2290. if (gen_branch_or_nop(branch_type, ip, old_addr, plt, &old_insn) < 0)
  2291. return -EFAULT;
  2292. if (gen_branch_or_nop(branch_type, ip, new_addr, plt, &new_insn) < 0)
  2293. return -EFAULT;
  2294. if (is_long_jump(ip, new_addr))
  2295. plt_target = (u64)new_addr;
  2296. else if (is_long_jump(ip, old_addr))
  2297. /* if the old target is a long jump and the new target is not,
  2298. * restore the plt target to dummy_tramp, so there is always a
  2299. * legal and harmless address stored in plt target, and we'll
  2300. * never jump from plt to an unknown place.
  2301. */
  2302. plt_target = (u64)&dummy_tramp;
  2303. if (plt_target) {
  2304. /* non-zero plt_target indicates we're patching a bpf prog,
  2305. * which is read only.
  2306. */
  2307. if (set_memory_rw(PAGE_MASK & ((uintptr_t)&plt->target), 1))
  2308. return -EFAULT;
  2309. WRITE_ONCE(plt->target, plt_target);
  2310. set_memory_ro(PAGE_MASK & ((uintptr_t)&plt->target), 1);
  2311. /* since plt target points to either the new trampoline
  2312. * or dummy_tramp, even if another CPU reads the old plt
  2313. * target value before fetching the bl instruction to plt,
  2314. * it will be brought back by dummy_tramp, so no barrier is
  2315. * required here.
  2316. */
  2317. }
  2318. /* if the old target and the new target are both long jumps, no
  2319. * patching is required
  2320. */
  2321. if (old_insn == new_insn)
  2322. return 0;
  2323. mutex_lock(&text_mutex);
  2324. if (aarch64_insn_read(ip, &replaced)) {
  2325. ret = -EFAULT;
  2326. goto out;
  2327. }
  2328. if (replaced != old_insn) {
  2329. ret = -EFAULT;
  2330. goto out;
  2331. }
  2332. /* We call aarch64_insn_patch_text_nosync() to replace instruction
  2333. * atomically, so no other CPUs will fetch a half-new and half-old
  2334. * instruction. But there is chance that another CPU executes the
  2335. * old instruction after the patching operation finishes (e.g.,
  2336. * pipeline not flushed, or icache not synchronized yet).
  2337. *
  2338. * 1. when a new trampoline is attached, it is not a problem for
  2339. * different CPUs to jump to different trampolines temporarily.
  2340. *
  2341. * 2. when an old trampoline is freed, we should wait for all other
  2342. * CPUs to exit the trampoline and make sure the trampoline is no
  2343. * longer reachable, since bpf_tramp_image_put() function already
  2344. * uses percpu_ref and task-based rcu to do the sync, no need to call
  2345. * the sync version here, see bpf_tramp_image_put() for details.
  2346. */
  2347. ret = aarch64_insn_patch_text_nosync(ip, new_insn);
  2348. out:
  2349. mutex_unlock(&text_mutex);
  2350. return ret;
  2351. }
  2352. bool bpf_jit_supports_ptr_xchg(void)
  2353. {
  2354. return true;
  2355. }
  2356. bool bpf_jit_supports_exceptions(void)
  2357. {
  2358. /* We unwind through both kernel frames starting from within bpf_throw
  2359. * call and BPF frames. Therefore we require FP unwinder to be enabled
  2360. * to walk kernel frames and reach BPF frames in the stack trace.
  2361. * ARM64 kernel is aways compiled with CONFIG_FRAME_POINTER=y
  2362. */
  2363. return true;
  2364. }
  2365. bool bpf_jit_supports_arena(void)
  2366. {
  2367. return true;
  2368. }
  2369. bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
  2370. {
  2371. if (!in_arena)
  2372. return true;
  2373. switch (insn->code) {
  2374. case BPF_STX | BPF_ATOMIC | BPF_W:
  2375. case BPF_STX | BPF_ATOMIC | BPF_DW:
  2376. if (!cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
  2377. return false;
  2378. }
  2379. return true;
  2380. }
  2381. bool bpf_jit_supports_percpu_insn(void)
  2382. {
  2383. return true;
  2384. }
  2385. bool bpf_jit_inlines_helper_call(s32 imm)
  2386. {
  2387. switch (imm) {
  2388. case BPF_FUNC_get_smp_processor_id:
  2389. case BPF_FUNC_get_current_task:
  2390. case BPF_FUNC_get_current_task_btf:
  2391. return true;
  2392. default:
  2393. return false;
  2394. }
  2395. }
  2396. void bpf_jit_free(struct bpf_prog *prog)
  2397. {
  2398. if (prog->jited) {
  2399. struct arm64_jit_data *jit_data = prog->aux->jit_data;
  2400. struct bpf_binary_header *hdr;
  2401. /*
  2402. * If we fail the final pass of JIT (from jit_subprogs),
  2403. * the program may not be finalized yet. Call finalize here
  2404. * before freeing it.
  2405. */
  2406. if (jit_data) {
  2407. bpf_arch_text_copy(&jit_data->ro_header->size, &jit_data->header->size,
  2408. sizeof(jit_data->header->size));
  2409. kfree(jit_data);
  2410. }
  2411. hdr = bpf_jit_binary_pack_hdr(prog);
  2412. bpf_jit_binary_pack_free(hdr, NULL);
  2413. WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog));
  2414. }
  2415. bpf_prog_unlock_free(prog);
  2416. }