123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235 |
- #define pr_fmt(fmt) "bpf_jit: " fmt
- #include <linux/bitfield.h>
- #include <linux/bpf.h>
- #include <linux/filter.h>
- #include <linux/memory.h>
- #include <linux/printk.h>
- #include <linux/slab.h>
- #include <asm/asm-extable.h>
- #include <asm/byteorder.h>
- #include <asm/cacheflush.h>
- #include <asm/debug-monitors.h>
- #include <asm/insn.h>
- #include <asm/patching.h>
- #include <asm/set_memory.h>
- #include "bpf_jit.h"
- #ifdef CONFIG_RKP
- #include <linux/rkp.h>
- #endif
- #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
- #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
- #define TCALL_CNT (MAX_BPF_JIT_REG + 2)
- #define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
- #define FP_BOTTOM (MAX_BPF_JIT_REG + 4)
- #define check_imm(bits, imm) do { \
- if ((((imm) > 0) && ((imm) >> (bits))) || \
- (((imm) < 0) && (~(imm) >> (bits)))) { \
- pr_info("[%2d] imm=%d(0x%x) out of range\n", \
- i, imm, imm); \
- return -EINVAL; \
- } \
- } while (0)
- #define check_imm19(imm) check_imm(19, imm)
- #define check_imm26(imm) check_imm(26, imm)
- static const int bpf2a64[] = {
-
- [BPF_REG_0] = A64_R(7),
-
- [BPF_REG_1] = A64_R(0),
- [BPF_REG_2] = A64_R(1),
- [BPF_REG_3] = A64_R(2),
- [BPF_REG_4] = A64_R(3),
- [BPF_REG_5] = A64_R(4),
-
- [BPF_REG_6] = A64_R(19),
- [BPF_REG_7] = A64_R(20),
- [BPF_REG_8] = A64_R(21),
- [BPF_REG_9] = A64_R(22),
-
- [BPF_REG_FP] = A64_R(25),
-
- [TMP_REG_1] = A64_R(10),
- [TMP_REG_2] = A64_R(11),
- [TMP_REG_3] = A64_R(12),
-
- [TCALL_CNT] = A64_R(26),
-
- [BPF_REG_AX] = A64_R(9),
- [FP_BOTTOM] = A64_R(27),
- };
- struct jit_ctx {
- const struct bpf_prog *prog;
- int idx;
- int epilogue_offset;
- int *offset;
- int exentry_idx;
- __le32 *image;
- u32 stack_size;
- int fpb_offset;
- };
- struct bpf_plt {
- u32 insn_ldr;
- u32 insn_br;
- u64 target;
- };
- #define PLT_TARGET_SIZE sizeof_field(struct bpf_plt, target)
- #define PLT_TARGET_OFFSET offsetof(struct bpf_plt, target)
- static inline void emit(const u32 insn, struct jit_ctx *ctx)
- {
- if (ctx->image != NULL)
- ctx->image[ctx->idx] = cpu_to_le32(insn);
- ctx->idx++;
- }
- static inline void emit_a64_mov_i(const int is64, const int reg,
- const s32 val, struct jit_ctx *ctx)
- {
- u16 hi = val >> 16;
- u16 lo = val & 0xffff;
- if (hi & 0x8000) {
- if (hi == 0xffff) {
- emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
- } else {
- emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
- if (lo != 0xffff)
- emit(A64_MOVK(is64, reg, lo, 0), ctx);
- }
- } else {
- emit(A64_MOVZ(is64, reg, lo, 0), ctx);
- if (hi)
- emit(A64_MOVK(is64, reg, hi, 16), ctx);
- }
- }
- static int i64_i16_blocks(const u64 val, bool inverse)
- {
- return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
- (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
- (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
- (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
- }
- static inline void emit_a64_mov_i64(const int reg, const u64 val,
- struct jit_ctx *ctx)
- {
- u64 nrm_tmp = val, rev_tmp = ~val;
- bool inverse;
- int shift;
- if (!(nrm_tmp >> 32))
- return emit_a64_mov_i(0, reg, (u32)val, ctx);
- inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
- shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
- (fls64(nrm_tmp) - 1)), 16), 0);
- if (inverse)
- emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
- else
- emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
- shift -= 16;
- while (shift >= 0) {
- if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
- emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
- shift -= 16;
- }
- }
- static inline void emit_bti(u32 insn, struct jit_ctx *ctx)
- {
- if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
- emit(insn, ctx);
- }
- static inline void emit_addr_mov_i64(const int reg, const u64 val,
- struct jit_ctx *ctx)
- {
- u64 tmp = val;
- int shift = 0;
- emit(A64_MOVN(1, reg, ~tmp & 0xffff, shift), ctx);
- while (shift < 32) {
- tmp >>= 16;
- shift += 16;
- emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
- }
- }
- static inline void emit_call(u64 target, struct jit_ctx *ctx)
- {
- u8 tmp = bpf2a64[TMP_REG_1];
- emit_addr_mov_i64(tmp, target, ctx);
- emit(A64_BLR(tmp), ctx);
- }
- static inline int bpf2a64_offset(int bpf_insn, int off,
- const struct jit_ctx *ctx)
- {
-
- bpf_insn++;
-
- return ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1);
- }
- static void jit_fill_hole(void *area, unsigned int size)
- {
- __le32 *ptr;
-
- for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
- *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
- }
- static inline int epilogue_offset(const struct jit_ctx *ctx)
- {
- int to = ctx->epilogue_offset;
- int from = ctx->idx;
- return to - from;
- }
- static bool is_addsub_imm(u32 imm)
- {
-
- return !(imm & ~0xfff) || !(imm & ~0xfff000);
- }
- static bool is_lsi_offset(int offset, int scale)
- {
- if (offset < 0)
- return false;
- if (offset > (0xFFF << scale))
- return false;
- if (offset & ((1 << scale) - 1))
- return false;
- return true;
- }
- #define BTI_INSNS (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) ? 1 : 0)
- #define PAC_INSNS (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) ? 1 : 0)
- #define POKE_OFFSET (BTI_INSNS + 1)
- #define PROLOGUE_OFFSET (BTI_INSNS + 2 + PAC_INSNS + 8)
- static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
- {
- const struct bpf_prog *prog = ctx->prog;
- const bool is_main_prog = prog->aux->func_idx == 0;
- const u8 r6 = bpf2a64[BPF_REG_6];
- const u8 r7 = bpf2a64[BPF_REG_7];
- const u8 r8 = bpf2a64[BPF_REG_8];
- const u8 r9 = bpf2a64[BPF_REG_9];
- const u8 fp = bpf2a64[BPF_REG_FP];
- const u8 tcc = bpf2a64[TCALL_CNT];
- const u8 fpb = bpf2a64[FP_BOTTOM];
- const int idx0 = ctx->idx;
- int cur_offset;
-
-
- emit_bti(A64_BTI_JC, ctx);
- emit(A64_MOV(1, A64_R(9), A64_LR), ctx);
- emit(A64_NOP, ctx);
-
- if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
- emit(A64_PACIASP, ctx);
-
- emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
- emit(A64_MOV(1, A64_FP, A64_SP), ctx);
-
- emit(A64_PUSH(r6, r7, A64_SP), ctx);
- emit(A64_PUSH(r8, r9, A64_SP), ctx);
- emit(A64_PUSH(fp, tcc, A64_SP), ctx);
- emit(A64_PUSH(fpb, A64_R(28), A64_SP), ctx);
-
- emit(A64_MOV(1, fp, A64_SP), ctx);
- if (!ebpf_from_cbpf && is_main_prog) {
-
- emit(A64_MOVZ(1, tcc, 0, 0), ctx);
- cur_offset = ctx->idx - idx0;
- if (cur_offset != PROLOGUE_OFFSET) {
- pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
- cur_offset, PROLOGUE_OFFSET);
- return -1;
- }
-
- emit_bti(A64_BTI_J, ctx);
- }
- emit(A64_SUB_I(1, fpb, fp, ctx->fpb_offset), ctx);
-
- ctx->stack_size = round_up(prog->aux->stack_depth, 16);
-
- emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
- return 0;
- }
- static int out_offset = -1;
- static int emit_bpf_tail_call(struct jit_ctx *ctx)
- {
-
- const u8 r2 = bpf2a64[BPF_REG_2];
- const u8 r3 = bpf2a64[BPF_REG_3];
- const u8 tmp = bpf2a64[TMP_REG_1];
- const u8 prg = bpf2a64[TMP_REG_2];
- const u8 tcc = bpf2a64[TCALL_CNT];
- const int idx0 = ctx->idx;
- #define cur_offset (ctx->idx - idx0)
- #define jmp_offset (out_offset - (cur_offset))
- size_t off;
-
- off = offsetof(struct bpf_array, map.max_entries);
- emit_a64_mov_i64(tmp, off, ctx);
- emit(A64_LDR32(tmp, r2, tmp), ctx);
- emit(A64_MOV(0, r3, r3), ctx);
- emit(A64_CMP(0, r3, tmp), ctx);
- emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
-
- emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx);
- emit(A64_CMP(1, tcc, tmp), ctx);
- emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
- emit(A64_ADD_I(1, tcc, tcc, 1), ctx);
-
- off = offsetof(struct bpf_array, ptrs);
- emit_a64_mov_i64(tmp, off, ctx);
- emit(A64_ADD(1, tmp, r2, tmp), ctx);
- emit(A64_LSL(1, prg, r3, 3), ctx);
- emit(A64_LDR64(prg, tmp, prg), ctx);
- emit(A64_CBZ(1, prg, jmp_offset), ctx);
-
- off = offsetof(struct bpf_prog, bpf_func);
- emit_a64_mov_i64(tmp, off, ctx);
- emit(A64_LDR64(tmp, prg, tmp), ctx);
- emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx);
- emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
- emit(A64_BR(tmp), ctx);
-
- if (out_offset == -1)
- out_offset = cur_offset;
- if (cur_offset != out_offset) {
- pr_err_once("tail_call out_offset = %d, expected %d!\n",
- cur_offset, out_offset);
- return -1;
- }
- return 0;
- #undef cur_offset
- #undef jmp_offset
- }
- #ifdef CONFIG_ARM64_LSE_ATOMICS
- static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
- {
- const u8 code = insn->code;
- const u8 dst = bpf2a64[insn->dst_reg];
- const u8 src = bpf2a64[insn->src_reg];
- const u8 tmp = bpf2a64[TMP_REG_1];
- const u8 tmp2 = bpf2a64[TMP_REG_2];
- const bool isdw = BPF_SIZE(code) == BPF_DW;
- const s16 off = insn->off;
- u8 reg;
- if (!off) {
- reg = dst;
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_ADD(1, tmp, tmp, dst), ctx);
- reg = tmp;
- }
- switch (insn->imm) {
-
- case BPF_ADD:
- emit(A64_STADD(isdw, reg, src), ctx);
- break;
- case BPF_AND:
- emit(A64_MVN(isdw, tmp2, src), ctx);
- emit(A64_STCLR(isdw, reg, tmp2), ctx);
- break;
- case BPF_OR:
- emit(A64_STSET(isdw, reg, src), ctx);
- break;
- case BPF_XOR:
- emit(A64_STEOR(isdw, reg, src), ctx);
- break;
-
- case BPF_ADD | BPF_FETCH:
- emit(A64_LDADDAL(isdw, src, reg, src), ctx);
- break;
- case BPF_AND | BPF_FETCH:
- emit(A64_MVN(isdw, tmp2, src), ctx);
- emit(A64_LDCLRAL(isdw, src, reg, tmp2), ctx);
- break;
- case BPF_OR | BPF_FETCH:
- emit(A64_LDSETAL(isdw, src, reg, src), ctx);
- break;
- case BPF_XOR | BPF_FETCH:
- emit(A64_LDEORAL(isdw, src, reg, src), ctx);
- break;
-
- case BPF_XCHG:
- emit(A64_SWPAL(isdw, src, reg, src), ctx);
- break;
-
- case BPF_CMPXCHG:
- emit(A64_CASAL(isdw, src, reg, bpf2a64[BPF_REG_0]), ctx);
- break;
- default:
- pr_err_once("unknown atomic op code %02x\n", insn->imm);
- return -EINVAL;
- }
- return 0;
- }
- #else
- static inline int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
- {
- return -EINVAL;
- }
- #endif
- static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
- {
- const u8 code = insn->code;
- const u8 dst = bpf2a64[insn->dst_reg];
- const u8 src = bpf2a64[insn->src_reg];
- const u8 tmp = bpf2a64[TMP_REG_1];
- const u8 tmp2 = bpf2a64[TMP_REG_2];
- const u8 tmp3 = bpf2a64[TMP_REG_3];
- const int i = insn - ctx->prog->insnsi;
- const s32 imm = insn->imm;
- const s16 off = insn->off;
- const bool isdw = BPF_SIZE(code) == BPF_DW;
- u8 reg;
- s32 jmp_offset;
- if (!off) {
- reg = dst;
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_ADD(1, tmp, tmp, dst), ctx);
- reg = tmp;
- }
- if (imm == BPF_ADD || imm == BPF_AND ||
- imm == BPF_OR || imm == BPF_XOR) {
-
- emit(A64_LDXR(isdw, tmp2, reg), ctx);
- if (imm == BPF_ADD)
- emit(A64_ADD(isdw, tmp2, tmp2, src), ctx);
- else if (imm == BPF_AND)
- emit(A64_AND(isdw, tmp2, tmp2, src), ctx);
- else if (imm == BPF_OR)
- emit(A64_ORR(isdw, tmp2, tmp2, src), ctx);
- else
- emit(A64_EOR(isdw, tmp2, tmp2, src), ctx);
- emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx);
- jmp_offset = -3;
- check_imm19(jmp_offset);
- emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
- } else if (imm == (BPF_ADD | BPF_FETCH) ||
- imm == (BPF_AND | BPF_FETCH) ||
- imm == (BPF_OR | BPF_FETCH) ||
- imm == (BPF_XOR | BPF_FETCH)) {
-
- const u8 ax = bpf2a64[BPF_REG_AX];
- emit(A64_MOV(isdw, ax, src), ctx);
- emit(A64_LDXR(isdw, src, reg), ctx);
- if (imm == (BPF_ADD | BPF_FETCH))
- emit(A64_ADD(isdw, tmp2, src, ax), ctx);
- else if (imm == (BPF_AND | BPF_FETCH))
- emit(A64_AND(isdw, tmp2, src, ax), ctx);
- else if (imm == (BPF_OR | BPF_FETCH))
- emit(A64_ORR(isdw, tmp2, src, ax), ctx);
- else
- emit(A64_EOR(isdw, tmp2, src, ax), ctx);
- emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
- jmp_offset = -3;
- check_imm19(jmp_offset);
- emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
- emit(A64_DMB_ISH, ctx);
- } else if (imm == BPF_XCHG) {
-
- emit(A64_MOV(isdw, tmp2, src), ctx);
- emit(A64_LDXR(isdw, src, reg), ctx);
- emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
- jmp_offset = -2;
- check_imm19(jmp_offset);
- emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
- emit(A64_DMB_ISH, ctx);
- } else if (imm == BPF_CMPXCHG) {
-
- const u8 r0 = bpf2a64[BPF_REG_0];
- emit(A64_MOV(isdw, tmp2, r0), ctx);
- emit(A64_LDXR(isdw, r0, reg), ctx);
- emit(A64_EOR(isdw, tmp3, r0, tmp2), ctx);
- jmp_offset = 4;
- check_imm19(jmp_offset);
- emit(A64_CBNZ(isdw, tmp3, jmp_offset), ctx);
- emit(A64_STLXR(isdw, src, reg, tmp3), ctx);
- jmp_offset = -4;
- check_imm19(jmp_offset);
- emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
- emit(A64_DMB_ISH, ctx);
- } else {
- pr_err_once("unknown atomic op code %02x\n", imm);
- return -EINVAL;
- }
- return 0;
- }
- void dummy_tramp(void);
- asm (
- " .pushsection .text, \"ax\", @progbits\n"
- " .global dummy_tramp\n"
- " .type dummy_tramp, %function\n"
- "dummy_tramp:"
- #if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
- " bti j\n"
- #endif
- " mov x10, x30\n"
- " mov x30, x9\n"
- " ret x10\n"
- " .size dummy_tramp, .-dummy_tramp\n"
- " .popsection\n"
- );
- static void build_plt(struct jit_ctx *ctx)
- {
- const u8 tmp = bpf2a64[TMP_REG_1];
- struct bpf_plt *plt = NULL;
-
- if ((ctx->idx + PLT_TARGET_OFFSET / AARCH64_INSN_SIZE) % 2)
- emit(A64_NOP, ctx);
- plt = (struct bpf_plt *)(ctx->image + ctx->idx);
-
- emit(A64_LDR64LIT(tmp, 2 * AARCH64_INSN_SIZE), ctx);
- emit(A64_BR(tmp), ctx);
- if (ctx->image)
- plt->target = (u64)&dummy_tramp;
- }
- static void build_epilogue(struct jit_ctx *ctx)
- {
- const u8 r0 = bpf2a64[BPF_REG_0];
- const u8 r6 = bpf2a64[BPF_REG_6];
- const u8 r7 = bpf2a64[BPF_REG_7];
- const u8 r8 = bpf2a64[BPF_REG_8];
- const u8 r9 = bpf2a64[BPF_REG_9];
- const u8 fp = bpf2a64[BPF_REG_FP];
- const u8 fpb = bpf2a64[FP_BOTTOM];
-
- emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
-
- emit(A64_POP(fpb, A64_R(28), A64_SP), ctx);
-
- emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
-
- emit(A64_POP(r8, r9, A64_SP), ctx);
- emit(A64_POP(r6, r7, A64_SP), ctx);
-
- emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
-
- emit(A64_MOV(1, A64_R(0), r0), ctx);
-
- if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
- emit(A64_AUTIASP, ctx);
- emit(A64_RET(A64_LR), ctx);
- }
- #define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
- #define BPF_FIXUP_REG_MASK GENMASK(31, 27)
- bool ex_handler_bpf(const struct exception_table_entry *ex,
- struct pt_regs *regs)
- {
- off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
- int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
- regs->regs[dst_reg] = 0;
- regs->pc = (unsigned long)&ex->fixup - offset;
- return true;
- }
- static int add_exception_handler(const struct bpf_insn *insn,
- struct jit_ctx *ctx,
- int dst_reg)
- {
- off_t offset;
- unsigned long pc;
- struct exception_table_entry *ex;
- if (!ctx->image)
-
- return 0;
- if (BPF_MODE(insn->code) != BPF_PROBE_MEM)
- return 0;
- if (!ctx->prog->aux->extable ||
- WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries))
- return -EINVAL;
- ex = &ctx->prog->aux->extable[ctx->exentry_idx];
- pc = (unsigned long)&ctx->image[ctx->idx - 1];
- offset = pc - (long)&ex->insn;
- if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
- return -ERANGE;
- ex->insn = offset;
-
- offset = (long)&ex->fixup - (pc + AARCH64_INSN_SIZE);
- if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, offset))
- return -ERANGE;
- ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, offset) |
- FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
- ex->type = EX_TYPE_BPF;
- ctx->exentry_idx++;
- return 0;
- }
- static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
- bool extra_pass)
- {
- const u8 code = insn->code;
- const u8 dst = bpf2a64[insn->dst_reg];
- const u8 src = bpf2a64[insn->src_reg];
- const u8 tmp = bpf2a64[TMP_REG_1];
- const u8 tmp2 = bpf2a64[TMP_REG_2];
- const u8 fp = bpf2a64[BPF_REG_FP];
- const u8 fpb = bpf2a64[FP_BOTTOM];
- const s16 off = insn->off;
- const s32 imm = insn->imm;
- const int i = insn - ctx->prog->insnsi;
- const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
- BPF_CLASS(code) == BPF_JMP;
- u8 jmp_cond;
- s32 jmp_offset;
- u32 a64_insn;
- u8 src_adj;
- u8 dst_adj;
- int off_adj;
- int ret;
- switch (code) {
-
- case BPF_ALU | BPF_MOV | BPF_X:
- case BPF_ALU64 | BPF_MOV | BPF_X:
- emit(A64_MOV(is64, dst, src), ctx);
- break;
-
- case BPF_ALU | BPF_ADD | BPF_X:
- case BPF_ALU64 | BPF_ADD | BPF_X:
- emit(A64_ADD(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_SUB | BPF_X:
- case BPF_ALU64 | BPF_SUB | BPF_X:
- emit(A64_SUB(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_AND | BPF_X:
- case BPF_ALU64 | BPF_AND | BPF_X:
- emit(A64_AND(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_OR | BPF_X:
- case BPF_ALU64 | BPF_OR | BPF_X:
- emit(A64_ORR(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_XOR | BPF_X:
- case BPF_ALU64 | BPF_XOR | BPF_X:
- emit(A64_EOR(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_MUL | BPF_X:
- case BPF_ALU64 | BPF_MUL | BPF_X:
- emit(A64_MUL(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_DIV | BPF_X:
- case BPF_ALU64 | BPF_DIV | BPF_X:
- emit(A64_UDIV(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_MOD | BPF_X:
- case BPF_ALU64 | BPF_MOD | BPF_X:
- emit(A64_UDIV(is64, tmp, dst, src), ctx);
- emit(A64_MSUB(is64, dst, dst, tmp, src), ctx);
- break;
- case BPF_ALU | BPF_LSH | BPF_X:
- case BPF_ALU64 | BPF_LSH | BPF_X:
- emit(A64_LSLV(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_RSH | BPF_X:
- case BPF_ALU64 | BPF_RSH | BPF_X:
- emit(A64_LSRV(is64, dst, dst, src), ctx);
- break;
- case BPF_ALU | BPF_ARSH | BPF_X:
- case BPF_ALU64 | BPF_ARSH | BPF_X:
- emit(A64_ASRV(is64, dst, dst, src), ctx);
- break;
-
- case BPF_ALU | BPF_NEG:
- case BPF_ALU64 | BPF_NEG:
- emit(A64_NEG(is64, dst, dst), ctx);
- break;
-
- case BPF_ALU | BPF_END | BPF_FROM_LE:
- case BPF_ALU | BPF_END | BPF_FROM_BE:
- #ifdef CONFIG_CPU_BIG_ENDIAN
- if (BPF_SRC(code) == BPF_FROM_BE)
- goto emit_bswap_uxt;
- #else
- if (BPF_SRC(code) == BPF_FROM_LE)
- goto emit_bswap_uxt;
- #endif
- switch (imm) {
- case 16:
- emit(A64_REV16(is64, dst, dst), ctx);
-
- emit(A64_UXTH(is64, dst, dst), ctx);
- break;
- case 32:
- emit(A64_REV32(is64, dst, dst), ctx);
-
- break;
- case 64:
- emit(A64_REV64(dst, dst), ctx);
- break;
- }
- break;
- emit_bswap_uxt:
- switch (imm) {
- case 16:
-
- emit(A64_UXTH(is64, dst, dst), ctx);
- break;
- case 32:
-
- emit(A64_UXTW(is64, dst, dst), ctx);
- break;
- case 64:
-
- break;
- }
- break;
-
- case BPF_ALU | BPF_MOV | BPF_K:
- case BPF_ALU64 | BPF_MOV | BPF_K:
- emit_a64_mov_i(is64, dst, imm, ctx);
- break;
-
- case BPF_ALU | BPF_ADD | BPF_K:
- case BPF_ALU64 | BPF_ADD | BPF_K:
- if (is_addsub_imm(imm)) {
- emit(A64_ADD_I(is64, dst, dst, imm), ctx);
- } else if (is_addsub_imm(-imm)) {
- emit(A64_SUB_I(is64, dst, dst, -imm), ctx);
- } else {
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_ADD(is64, dst, dst, tmp), ctx);
- }
- break;
- case BPF_ALU | BPF_SUB | BPF_K:
- case BPF_ALU64 | BPF_SUB | BPF_K:
- if (is_addsub_imm(imm)) {
- emit(A64_SUB_I(is64, dst, dst, imm), ctx);
- } else if (is_addsub_imm(-imm)) {
- emit(A64_ADD_I(is64, dst, dst, -imm), ctx);
- } else {
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_SUB(is64, dst, dst, tmp), ctx);
- }
- break;
- case BPF_ALU | BPF_AND | BPF_K:
- case BPF_ALU64 | BPF_AND | BPF_K:
- a64_insn = A64_AND_I(is64, dst, dst, imm);
- if (a64_insn != AARCH64_BREAK_FAULT) {
- emit(a64_insn, ctx);
- } else {
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_AND(is64, dst, dst, tmp), ctx);
- }
- break;
- case BPF_ALU | BPF_OR | BPF_K:
- case BPF_ALU64 | BPF_OR | BPF_K:
- a64_insn = A64_ORR_I(is64, dst, dst, imm);
- if (a64_insn != AARCH64_BREAK_FAULT) {
- emit(a64_insn, ctx);
- } else {
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_ORR(is64, dst, dst, tmp), ctx);
- }
- break;
- case BPF_ALU | BPF_XOR | BPF_K:
- case BPF_ALU64 | BPF_XOR | BPF_K:
- a64_insn = A64_EOR_I(is64, dst, dst, imm);
- if (a64_insn != AARCH64_BREAK_FAULT) {
- emit(a64_insn, ctx);
- } else {
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_EOR(is64, dst, dst, tmp), ctx);
- }
- break;
- case BPF_ALU | BPF_MUL | BPF_K:
- case BPF_ALU64 | BPF_MUL | BPF_K:
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_MUL(is64, dst, dst, tmp), ctx);
- break;
- case BPF_ALU | BPF_DIV | BPF_K:
- case BPF_ALU64 | BPF_DIV | BPF_K:
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_UDIV(is64, dst, dst, tmp), ctx);
- break;
- case BPF_ALU | BPF_MOD | BPF_K:
- case BPF_ALU64 | BPF_MOD | BPF_K:
- emit_a64_mov_i(is64, tmp2, imm, ctx);
- emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
- emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx);
- break;
- case BPF_ALU | BPF_LSH | BPF_K:
- case BPF_ALU64 | BPF_LSH | BPF_K:
- emit(A64_LSL(is64, dst, dst, imm), ctx);
- break;
- case BPF_ALU | BPF_RSH | BPF_K:
- case BPF_ALU64 | BPF_RSH | BPF_K:
- emit(A64_LSR(is64, dst, dst, imm), ctx);
- break;
- case BPF_ALU | BPF_ARSH | BPF_K:
- case BPF_ALU64 | BPF_ARSH | BPF_K:
- emit(A64_ASR(is64, dst, dst, imm), ctx);
- break;
-
- case BPF_JMP | BPF_JA:
- jmp_offset = bpf2a64_offset(i, off, ctx);
- check_imm26(jmp_offset);
- emit(A64_B(jmp_offset), ctx);
- break;
-
- case BPF_JMP | BPF_JEQ | BPF_X:
- case BPF_JMP | BPF_JGT | BPF_X:
- case BPF_JMP | BPF_JLT | BPF_X:
- case BPF_JMP | BPF_JGE | BPF_X:
- case BPF_JMP | BPF_JLE | BPF_X:
- case BPF_JMP | BPF_JNE | BPF_X:
- case BPF_JMP | BPF_JSGT | BPF_X:
- case BPF_JMP | BPF_JSLT | BPF_X:
- case BPF_JMP | BPF_JSGE | BPF_X:
- case BPF_JMP | BPF_JSLE | BPF_X:
- case BPF_JMP32 | BPF_JEQ | BPF_X:
- case BPF_JMP32 | BPF_JGT | BPF_X:
- case BPF_JMP32 | BPF_JLT | BPF_X:
- case BPF_JMP32 | BPF_JGE | BPF_X:
- case BPF_JMP32 | BPF_JLE | BPF_X:
- case BPF_JMP32 | BPF_JNE | BPF_X:
- case BPF_JMP32 | BPF_JSGT | BPF_X:
- case BPF_JMP32 | BPF_JSLT | BPF_X:
- case BPF_JMP32 | BPF_JSGE | BPF_X:
- case BPF_JMP32 | BPF_JSLE | BPF_X:
- emit(A64_CMP(is64, dst, src), ctx);
- emit_cond_jmp:
- jmp_offset = bpf2a64_offset(i, off, ctx);
- check_imm19(jmp_offset);
- switch (BPF_OP(code)) {
- case BPF_JEQ:
- jmp_cond = A64_COND_EQ;
- break;
- case BPF_JGT:
- jmp_cond = A64_COND_HI;
- break;
- case BPF_JLT:
- jmp_cond = A64_COND_CC;
- break;
- case BPF_JGE:
- jmp_cond = A64_COND_CS;
- break;
- case BPF_JLE:
- jmp_cond = A64_COND_LS;
- break;
- case BPF_JSET:
- case BPF_JNE:
- jmp_cond = A64_COND_NE;
- break;
- case BPF_JSGT:
- jmp_cond = A64_COND_GT;
- break;
- case BPF_JSLT:
- jmp_cond = A64_COND_LT;
- break;
- case BPF_JSGE:
- jmp_cond = A64_COND_GE;
- break;
- case BPF_JSLE:
- jmp_cond = A64_COND_LE;
- break;
- default:
- return -EFAULT;
- }
- emit(A64_B_(jmp_cond, jmp_offset), ctx);
- break;
- case BPF_JMP | BPF_JSET | BPF_X:
- case BPF_JMP32 | BPF_JSET | BPF_X:
- emit(A64_TST(is64, dst, src), ctx);
- goto emit_cond_jmp;
-
- case BPF_JMP | BPF_JEQ | BPF_K:
- case BPF_JMP | BPF_JGT | BPF_K:
- case BPF_JMP | BPF_JLT | BPF_K:
- case BPF_JMP | BPF_JGE | BPF_K:
- case BPF_JMP | BPF_JLE | BPF_K:
- case BPF_JMP | BPF_JNE | BPF_K:
- case BPF_JMP | BPF_JSGT | BPF_K:
- case BPF_JMP | BPF_JSLT | BPF_K:
- case BPF_JMP | BPF_JSGE | BPF_K:
- case BPF_JMP | BPF_JSLE | BPF_K:
- case BPF_JMP32 | BPF_JEQ | BPF_K:
- case BPF_JMP32 | BPF_JGT | BPF_K:
- case BPF_JMP32 | BPF_JLT | BPF_K:
- case BPF_JMP32 | BPF_JGE | BPF_K:
- case BPF_JMP32 | BPF_JLE | BPF_K:
- case BPF_JMP32 | BPF_JNE | BPF_K:
- case BPF_JMP32 | BPF_JSGT | BPF_K:
- case BPF_JMP32 | BPF_JSLT | BPF_K:
- case BPF_JMP32 | BPF_JSGE | BPF_K:
- case BPF_JMP32 | BPF_JSLE | BPF_K:
- if (is_addsub_imm(imm)) {
- emit(A64_CMP_I(is64, dst, imm), ctx);
- } else if (is_addsub_imm(-imm)) {
- emit(A64_CMN_I(is64, dst, -imm), ctx);
- } else {
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_CMP(is64, dst, tmp), ctx);
- }
- goto emit_cond_jmp;
- case BPF_JMP | BPF_JSET | BPF_K:
- case BPF_JMP32 | BPF_JSET | BPF_K:
- a64_insn = A64_TST_I(is64, dst, imm);
- if (a64_insn != AARCH64_BREAK_FAULT) {
- emit(a64_insn, ctx);
- } else {
- emit_a64_mov_i(is64, tmp, imm, ctx);
- emit(A64_TST(is64, dst, tmp), ctx);
- }
- goto emit_cond_jmp;
-
- case BPF_JMP | BPF_CALL:
- {
- const u8 r0 = bpf2a64[BPF_REG_0];
- bool func_addr_fixed;
- u64 func_addr;
- ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
- &func_addr, &func_addr_fixed);
- if (ret < 0)
- return ret;
- emit_call(func_addr, ctx);
- emit(A64_MOV(1, r0, A64_R(0)), ctx);
- break;
- }
-
- case BPF_JMP | BPF_TAIL_CALL:
- if (emit_bpf_tail_call(ctx))
- return -EFAULT;
- break;
-
- case BPF_JMP | BPF_EXIT:
-
- if (i == ctx->prog->len - 1)
- break;
- jmp_offset = epilogue_offset(ctx);
- check_imm26(jmp_offset);
- emit(A64_B(jmp_offset), ctx);
- break;
-
- case BPF_LD | BPF_IMM | BPF_DW:
- {
- const struct bpf_insn insn1 = insn[1];
- u64 imm64;
- imm64 = (u64)insn1.imm << 32 | (u32)imm;
- if (bpf_pseudo_func(insn))
- emit_addr_mov_i64(dst, imm64, ctx);
- else
- emit_a64_mov_i64(dst, imm64, ctx);
- return 1;
- }
-
- case BPF_LDX | BPF_MEM | BPF_W:
- case BPF_LDX | BPF_MEM | BPF_H:
- case BPF_LDX | BPF_MEM | BPF_B:
- case BPF_LDX | BPF_MEM | BPF_DW:
- case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
- case BPF_LDX | BPF_PROBE_MEM | BPF_W:
- case BPF_LDX | BPF_PROBE_MEM | BPF_H:
- case BPF_LDX | BPF_PROBE_MEM | BPF_B:
- if (ctx->fpb_offset > 0 && src == fp) {
- src_adj = fpb;
- off_adj = off + ctx->fpb_offset;
- } else {
- src_adj = src;
- off_adj = off;
- }
- switch (BPF_SIZE(code)) {
- case BPF_W:
- if (is_lsi_offset(off_adj, 2)) {
- emit(A64_LDR32I(dst, src_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_LDR32(dst, src, tmp), ctx);
- }
- break;
- case BPF_H:
- if (is_lsi_offset(off_adj, 1)) {
- emit(A64_LDRHI(dst, src_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_LDRH(dst, src, tmp), ctx);
- }
- break;
- case BPF_B:
- if (is_lsi_offset(off_adj, 0)) {
- emit(A64_LDRBI(dst, src_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_LDRB(dst, src, tmp), ctx);
- }
- break;
- case BPF_DW:
- if (is_lsi_offset(off_adj, 3)) {
- emit(A64_LDR64I(dst, src_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_LDR64(dst, src, tmp), ctx);
- }
- break;
- }
- ret = add_exception_handler(insn, ctx, dst);
- if (ret)
- return ret;
- break;
-
- case BPF_ST | BPF_NOSPEC:
-
- break;
-
- case BPF_ST | BPF_MEM | BPF_W:
- case BPF_ST | BPF_MEM | BPF_H:
- case BPF_ST | BPF_MEM | BPF_B:
- case BPF_ST | BPF_MEM | BPF_DW:
- if (ctx->fpb_offset > 0 && dst == fp) {
- dst_adj = fpb;
- off_adj = off + ctx->fpb_offset;
- } else {
- dst_adj = dst;
- off_adj = off;
- }
-
- emit_a64_mov_i(1, tmp, imm, ctx);
- switch (BPF_SIZE(code)) {
- case BPF_W:
- if (is_lsi_offset(off_adj, 2)) {
- emit(A64_STR32I(tmp, dst_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp2, off, ctx);
- emit(A64_STR32(tmp, dst, tmp2), ctx);
- }
- break;
- case BPF_H:
- if (is_lsi_offset(off_adj, 1)) {
- emit(A64_STRHI(tmp, dst_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp2, off, ctx);
- emit(A64_STRH(tmp, dst, tmp2), ctx);
- }
- break;
- case BPF_B:
- if (is_lsi_offset(off_adj, 0)) {
- emit(A64_STRBI(tmp, dst_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp2, off, ctx);
- emit(A64_STRB(tmp, dst, tmp2), ctx);
- }
- break;
- case BPF_DW:
- if (is_lsi_offset(off_adj, 3)) {
- emit(A64_STR64I(tmp, dst_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp2, off, ctx);
- emit(A64_STR64(tmp, dst, tmp2), ctx);
- }
- break;
- }
- break;
-
- case BPF_STX | BPF_MEM | BPF_W:
- case BPF_STX | BPF_MEM | BPF_H:
- case BPF_STX | BPF_MEM | BPF_B:
- case BPF_STX | BPF_MEM | BPF_DW:
- if (ctx->fpb_offset > 0 && dst == fp) {
- dst_adj = fpb;
- off_adj = off + ctx->fpb_offset;
- } else {
- dst_adj = dst;
- off_adj = off;
- }
- switch (BPF_SIZE(code)) {
- case BPF_W:
- if (is_lsi_offset(off_adj, 2)) {
- emit(A64_STR32I(src, dst_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_STR32(src, dst, tmp), ctx);
- }
- break;
- case BPF_H:
- if (is_lsi_offset(off_adj, 1)) {
- emit(A64_STRHI(src, dst_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_STRH(src, dst, tmp), ctx);
- }
- break;
- case BPF_B:
- if (is_lsi_offset(off_adj, 0)) {
- emit(A64_STRBI(src, dst_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_STRB(src, dst, tmp), ctx);
- }
- break;
- case BPF_DW:
- if (is_lsi_offset(off_adj, 3)) {
- emit(A64_STR64I(src, dst_adj, off_adj), ctx);
- } else {
- emit_a64_mov_i(1, tmp, off, ctx);
- emit(A64_STR64(src, dst, tmp), ctx);
- }
- break;
- }
- break;
- case BPF_STX | BPF_ATOMIC | BPF_W:
- case BPF_STX | BPF_ATOMIC | BPF_DW:
- if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
- ret = emit_lse_atomic(insn, ctx);
- else
- ret = emit_ll_sc_atomic(insn, ctx);
- if (ret)
- return ret;
- break;
- default:
- pr_err_once("unknown opcode %02x\n", code);
- return -EINVAL;
- }
- return 0;
- }
- static int find_fpb_offset(struct bpf_prog *prog)
- {
- int i;
- int offset = 0;
- for (i = 0; i < prog->len; i++) {
- const struct bpf_insn *insn = &prog->insnsi[i];
- const u8 class = BPF_CLASS(insn->code);
- const u8 mode = BPF_MODE(insn->code);
- const u8 src = insn->src_reg;
- const u8 dst = insn->dst_reg;
- const s32 imm = insn->imm;
- const s16 off = insn->off;
- switch (class) {
- case BPF_STX:
- case BPF_ST:
-
- if (class == BPF_STX && mode == BPF_ATOMIC &&
- ((imm == BPF_XCHG ||
- imm == (BPF_FETCH | BPF_ADD) ||
- imm == (BPF_FETCH | BPF_AND) ||
- imm == (BPF_FETCH | BPF_XOR) ||
- imm == (BPF_FETCH | BPF_OR)) &&
- src == BPF_REG_FP))
- return 0;
- if (mode == BPF_MEM && dst == BPF_REG_FP &&
- off < offset)
- offset = insn->off;
- break;
- case BPF_JMP32:
- case BPF_JMP:
- break;
- case BPF_LDX:
- case BPF_LD:
-
- if (dst == BPF_REG_FP)
- return 0;
- if (class == BPF_LDX && mode == BPF_MEM &&
- src == BPF_REG_FP && off < offset)
- offset = off;
- break;
- case BPF_ALU:
- case BPF_ALU64:
- default:
-
- if (dst == BPF_REG_FP)
- return 0;
- }
- }
- if (offset < 0) {
-
- offset = -offset;
-
- offset = ALIGN_DOWN(offset, 8);
- }
- return offset;
- }
- static int build_body(struct jit_ctx *ctx, bool extra_pass)
- {
- const struct bpf_prog *prog = ctx->prog;
- int i;
-
- for (i = 0; i < prog->len; i++) {
- const struct bpf_insn *insn = &prog->insnsi[i];
- int ret;
- if (ctx->image == NULL)
- ctx->offset[i] = ctx->idx;
- ret = build_insn(insn, ctx, extra_pass);
- if (ret > 0) {
- i++;
- if (ctx->image == NULL)
- ctx->offset[i] = ctx->idx;
- continue;
- }
- if (ret)
- return ret;
- }
-
- if (ctx->image == NULL)
- ctx->offset[i] = ctx->idx;
- return 0;
- }
- static int validate_code(struct jit_ctx *ctx)
- {
- int i;
- for (i = 0; i < ctx->idx; i++) {
- u32 a64_insn = le32_to_cpu(ctx->image[i]);
- if (a64_insn == AARCH64_BREAK_FAULT)
- return -1;
- }
- return 0;
- }
- static int validate_ctx(struct jit_ctx *ctx)
- {
- if (validate_code(ctx))
- return -1;
- if (WARN_ON_ONCE(ctx->exentry_idx != ctx->prog->aux->num_exentries))
- return -1;
- return 0;
- }
- static inline void bpf_flush_icache(void *start, void *end)
- {
- flush_icache_range((unsigned long)start, (unsigned long)end);
- }
- struct arm64_jit_data {
- struct bpf_binary_header *header;
- u8 *image;
- struct jit_ctx ctx;
- };
- struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
- {
- int image_size, prog_size, extable_size, extable_align, extable_offset;
- struct bpf_prog *tmp, *orig_prog = prog;
- struct bpf_binary_header *header;
- struct arm64_jit_data *jit_data;
- bool was_classic = bpf_prog_was_classic(prog);
- bool tmp_blinded = false;
- bool extra_pass = false;
- struct jit_ctx ctx;
- u8 *image_ptr;
- if (!prog->jit_requested)
- return orig_prog;
- tmp = bpf_jit_blind_constants(prog);
-
- if (IS_ERR(tmp))
- return orig_prog;
- if (tmp != prog) {
- tmp_blinded = true;
- prog = tmp;
- }
- jit_data = prog->aux->jit_data;
- if (!jit_data) {
- jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
- if (!jit_data) {
- prog = orig_prog;
- goto out;
- }
- prog->aux->jit_data = jit_data;
- }
- if (jit_data->ctx.offset) {
- ctx = jit_data->ctx;
- image_ptr = jit_data->image;
- header = jit_data->header;
- extra_pass = true;
- prog_size = sizeof(u32) * ctx.idx;
- goto skip_init_ctx;
- }
- memset(&ctx, 0, sizeof(ctx));
- ctx.prog = prog;
- ctx.offset = kvcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
- if (ctx.offset == NULL) {
- prog = orig_prog;
- goto out_off;
- }
- ctx.fpb_offset = find_fpb_offset(prog);
-
- if (build_prologue(&ctx, was_classic)) {
- prog = orig_prog;
- goto out_off;
- }
- if (build_body(&ctx, extra_pass)) {
- prog = orig_prog;
- goto out_off;
- }
- ctx.epilogue_offset = ctx.idx;
- build_epilogue(&ctx);
- build_plt(&ctx);
- extable_align = __alignof__(struct exception_table_entry);
- extable_size = prog->aux->num_exentries *
- sizeof(struct exception_table_entry);
-
- prog_size = sizeof(u32) * ctx.idx;
-
- extable_offset = round_up(prog_size + PLT_TARGET_SIZE, extable_align);
- image_size = extable_offset + extable_size;
- header = bpf_jit_binary_alloc(image_size, &image_ptr,
- sizeof(u32), jit_fill_hole);
- if (header == NULL) {
- prog = orig_prog;
- goto out_off;
- }
-
- ctx.image = (__le32 *)image_ptr;
- if (extable_size)
- prog->aux->extable = (void *)image_ptr + extable_offset;
- skip_init_ctx:
- ctx.idx = 0;
- ctx.exentry_idx = 0;
- build_prologue(&ctx, was_classic);
- if (build_body(&ctx, extra_pass)) {
- bpf_jit_binary_free(header);
- prog = orig_prog;
- goto out_off;
- }
- build_epilogue(&ctx);
- build_plt(&ctx);
-
- if (validate_ctx(&ctx)) {
- bpf_jit_binary_free(header);
- prog = orig_prog;
- goto out_off;
- }
-
- if (bpf_jit_enable > 1)
- bpf_jit_dump(prog->len, prog_size, 2, ctx.image);
- bpf_flush_icache(header, ctx.image + ctx.idx);
- if (!prog->is_func || extra_pass) {
- if (extra_pass && ctx.idx != jit_data->ctx.idx) {
- pr_err_once("multi-func JIT bug %d != %d\n",
- ctx.idx, jit_data->ctx.idx);
- bpf_jit_binary_free(header);
- prog->bpf_func = NULL;
- prog->jited = 0;
- prog->jited_len = 0;
- goto out_off;
- }
- bpf_jit_binary_lock_ro(header);
- } else {
- jit_data->ctx = ctx;
- jit_data->image = image_ptr;
- jit_data->header = header;
- }
- prog->bpf_func = (void *)ctx.image;
- prog->jited = 1;
- prog->jited_len = prog_size;
- #ifdef CONFIG_RKP
- uh_call(UH_APP_RKP, RKP_BPF_LOAD, (u64)header, (u64)header->size, 0, 0);
- #endif
- if (!prog->is_func || extra_pass) {
- int i;
-
- for (i = 0; i <= prog->len; i++)
- ctx.offset[i] *= AARCH64_INSN_SIZE;
- bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
- out_off:
- kvfree(ctx.offset);
- kfree(jit_data);
- prog->aux->jit_data = NULL;
- }
- out:
- if (tmp_blinded)
- bpf_jit_prog_release_other(prog, prog == orig_prog ?
- tmp : orig_prog);
- return prog;
- }
- bool bpf_jit_supports_kfunc_call(void)
- {
- return true;
- }
- u64 bpf_jit_alloc_exec_limit(void)
- {
- return VMALLOC_END - VMALLOC_START;
- }
- void *bpf_jit_alloc_exec(unsigned long size)
- {
-
- return kasan_reset_tag(vmalloc(size));
- }
- void bpf_jit_free_exec(void *addr)
- {
- return vfree(addr);
- }
- bool bpf_jit_supports_subprog_tailcalls(void)
- {
- return true;
- }
- static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_link *l,
- int args_off, int retval_off, int run_ctx_off,
- bool save_ret)
- {
- __le32 *branch;
- u64 enter_prog;
- u64 exit_prog;
- struct bpf_prog *p = l->link.prog;
- int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
- enter_prog = (u64)bpf_trampoline_enter(p);
- exit_prog = (u64)bpf_trampoline_exit(p);
- if (l->cookie == 0) {
-
- emit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx);
- } else {
- emit_a64_mov_i64(A64_R(10), l->cookie, ctx);
- emit(A64_STR64I(A64_R(10), A64_SP, run_ctx_off + cookie_off),
- ctx);
- }
-
- emit_addr_mov_i64(A64_R(19), (const u64)p, ctx);
-
- emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
-
- emit(A64_ADD_I(1, A64_R(1), A64_SP, run_ctx_off), ctx);
- emit_call(enter_prog, ctx);
-
- branch = ctx->image + ctx->idx;
- emit(A64_NOP, ctx);
-
- emit(A64_MOV(1, A64_R(20), A64_R(0)), ctx);
- emit(A64_ADD_I(1, A64_R(0), A64_SP, args_off), ctx);
- if (!p->jited)
- emit_addr_mov_i64(A64_R(1), (const u64)p->insnsi, ctx);
- emit_call((const u64)p->bpf_func, ctx);
- if (save_ret)
- emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
- if (ctx->image) {
- int offset = &ctx->image[ctx->idx] - branch;
- *branch = cpu_to_le32(A64_CBZ(1, A64_R(0), offset));
- }
-
- emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
-
- emit(A64_MOV(1, A64_R(1), A64_R(20)), ctx);
-
- emit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx);
- emit_call(exit_prog, ctx);
- }
- static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_links *tl,
- int args_off, int retval_off, int run_ctx_off,
- __le32 **branches)
- {
- int i;
-
- emit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx);
- for (i = 0; i < tl->nr_links; i++) {
- invoke_bpf_prog(ctx, tl->links[i], args_off, retval_off,
- run_ctx_off, true);
-
- emit(A64_LDR64I(A64_R(10), A64_SP, retval_off), ctx);
-
- branches[i] = ctx->image + ctx->idx;
- emit(A64_NOP, ctx);
- }
- }
- static void save_args(struct jit_ctx *ctx, int args_off, int nargs)
- {
- int i;
- for (i = 0; i < nargs; i++) {
- emit(A64_STR64I(i, A64_SP, args_off), ctx);
- args_off += 8;
- }
- }
- static void restore_args(struct jit_ctx *ctx, int args_off, int nargs)
- {
- int i;
- for (i = 0; i < nargs; i++) {
- emit(A64_LDR64I(i, A64_SP, args_off), ctx);
- args_off += 8;
- }
- }
- static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
- struct bpf_tramp_links *tlinks, void *orig_call,
- int nargs, u32 flags)
- {
- int i;
- int stack_size;
- int retaddr_off;
- int regs_off;
- int retval_off;
- int args_off;
- int nargs_off;
- int ip_off;
- int run_ctx_off;
- struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
- struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
- struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
- bool save_ret;
- __le32 **branches = NULL;
-
- stack_size = 0;
- run_ctx_off = stack_size;
-
- stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8);
- ip_off = stack_size;
-
- if (flags & BPF_TRAMP_F_IP_ARG)
- stack_size += 8;
- nargs_off = stack_size;
-
- stack_size += 8;
- args_off = stack_size;
-
- stack_size += nargs * 8;
-
- retval_off = stack_size;
- save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
- if (save_ret)
- stack_size += 8;
-
- regs_off = stack_size;
- stack_size += 16;
-
- stack_size = round_up(stack_size, 16);
-
- retaddr_off = stack_size + 8;
-
- emit_bti(A64_BTI_JC, ctx);
-
- emit(A64_PUSH(A64_FP, A64_R(9), A64_SP), ctx);
- emit(A64_MOV(1, A64_FP, A64_SP), ctx);
-
- emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
- emit(A64_MOV(1, A64_FP, A64_SP), ctx);
-
- emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx);
- if (flags & BPF_TRAMP_F_IP_ARG) {
-
- emit_addr_mov_i64(A64_R(10), (const u64)orig_call, ctx);
- emit(A64_STR64I(A64_R(10), A64_SP, ip_off), ctx);
- }
-
- emit(A64_MOVZ(1, A64_R(10), nargs, 0), ctx);
- emit(A64_STR64I(A64_R(10), A64_SP, nargs_off), ctx);
-
- save_args(ctx, args_off, nargs);
-
- emit(A64_STR64I(A64_R(19), A64_SP, regs_off), ctx);
- emit(A64_STR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
- if (flags & BPF_TRAMP_F_CALL_ORIG) {
- emit_addr_mov_i64(A64_R(0), (const u64)im, ctx);
- emit_call((const u64)__bpf_tramp_enter, ctx);
- }
- for (i = 0; i < fentry->nr_links; i++)
- invoke_bpf_prog(ctx, fentry->links[i], args_off,
- retval_off, run_ctx_off,
- flags & BPF_TRAMP_F_RET_FENTRY_RET);
- if (fmod_ret->nr_links) {
- branches = kcalloc(fmod_ret->nr_links, sizeof(__le32 *),
- GFP_KERNEL);
- if (!branches)
- return -ENOMEM;
- invoke_bpf_mod_ret(ctx, fmod_ret, args_off, retval_off,
- run_ctx_off, branches);
- }
- if (flags & BPF_TRAMP_F_CALL_ORIG) {
- restore_args(ctx, args_off, nargs);
-
- emit(A64_LDR64I(A64_R(10), A64_SP, retaddr_off), ctx);
- emit(A64_ADR(A64_LR, AARCH64_INSN_SIZE * 2), ctx);
- emit(A64_RET(A64_R(10)), ctx);
-
- emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
-
- im->ip_after_call = ctx->image + ctx->idx;
- emit(A64_NOP, ctx);
- }
-
- for (i = 0; i < fmod_ret->nr_links && ctx->image != NULL; i++) {
- int offset = &ctx->image[ctx->idx] - branches[i];
- *branches[i] = cpu_to_le32(A64_CBNZ(1, A64_R(10), offset));
- }
- for (i = 0; i < fexit->nr_links; i++)
- invoke_bpf_prog(ctx, fexit->links[i], args_off, retval_off,
- run_ctx_off, false);
- if (flags & BPF_TRAMP_F_CALL_ORIG) {
- im->ip_epilogue = ctx->image + ctx->idx;
- emit_addr_mov_i64(A64_R(0), (const u64)im, ctx);
- emit_call((const u64)__bpf_tramp_exit, ctx);
- }
- if (flags & BPF_TRAMP_F_RESTORE_REGS)
- restore_args(ctx, args_off, nargs);
-
- emit(A64_LDR64I(A64_R(19), A64_SP, regs_off), ctx);
- emit(A64_LDR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
- if (save_ret)
- emit(A64_LDR64I(A64_R(0), A64_SP, retval_off), ctx);
-
- emit(A64_MOV(1, A64_SP, A64_FP), ctx);
-
- emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
- emit(A64_POP(A64_FP, A64_R(9), A64_SP), ctx);
- if (flags & BPF_TRAMP_F_SKIP_FRAME) {
-
- emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
- emit(A64_RET(A64_R(9)), ctx);
- } else {
-
- emit(A64_MOV(1, A64_R(10), A64_LR), ctx);
- emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
- emit(A64_RET(A64_R(10)), ctx);
- }
- if (ctx->image)
- bpf_flush_icache(ctx->image, ctx->image + ctx->idx);
- kfree(branches);
- return ctx->idx;
- }
- int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,
- void *image_end, const struct btf_func_model *m,
- u32 flags, struct bpf_tramp_links *tlinks,
- void *orig_call)
- {
- int i, ret;
- int nargs = m->nr_args;
- int max_insns = ((long)image_end - (long)image) / AARCH64_INSN_SIZE;
- struct jit_ctx ctx = {
- .image = NULL,
- .idx = 0,
- };
-
- if (nargs > 8)
- return -ENOTSUPP;
-
- for (i = 0; i < MAX_BPF_FUNC_ARGS; i++) {
- if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
- return -ENOTSUPP;
- }
- ret = prepare_trampoline(&ctx, im, tlinks, orig_call, nargs, flags);
- if (ret < 0)
- return ret;
- if (ret > max_insns)
- return -EFBIG;
- ctx.image = image;
- ctx.idx = 0;
- jit_fill_hole(image, (unsigned int)(image_end - image));
- ret = prepare_trampoline(&ctx, im, tlinks, orig_call, nargs, flags);
- if (ret > 0 && validate_code(&ctx) < 0)
- ret = -EINVAL;
- if (ret > 0)
- ret *= AARCH64_INSN_SIZE;
- return ret;
- }
- static bool is_long_jump(void *ip, void *target)
- {
- long offset;
-
- if (!target)
- return false;
- offset = (long)target - (long)ip;
- return offset < -SZ_128M || offset >= SZ_128M;
- }
- static int gen_branch_or_nop(enum aarch64_insn_branch_type type, void *ip,
- void *addr, void *plt, u32 *insn)
- {
- void *target;
- if (!addr) {
- *insn = aarch64_insn_gen_nop();
- return 0;
- }
- if (is_long_jump(ip, addr))
- target = plt;
- else
- target = addr;
- *insn = aarch64_insn_gen_branch_imm((unsigned long)ip,
- (unsigned long)target,
- type);
- return *insn != AARCH64_BREAK_FAULT ? 0 : -EFAULT;
- }
- int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
- void *old_addr, void *new_addr)
- {
- int ret;
- u32 old_insn;
- u32 new_insn;
- u32 replaced;
- struct bpf_plt *plt = NULL;
- unsigned long size = 0UL;
- unsigned long offset = ~0UL;
- enum aarch64_insn_branch_type branch_type;
- char namebuf[KSYM_NAME_LEN];
- void *image = NULL;
- u64 plt_target = 0ULL;
- bool poking_bpf_entry;
- if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
-
- return -ENOTSUPP;
- image = ip - offset;
-
- poking_bpf_entry = (offset == 0UL);
-
- if (poking_bpf_entry) {
-
- plt = image + size - PLT_TARGET_OFFSET;
-
- ip = image + POKE_OFFSET * AARCH64_INSN_SIZE;
- }
-
- if (WARN_ON((is_long_jump(ip, new_addr) || is_long_jump(ip, old_addr)) &&
- !poking_bpf_entry))
- return -EINVAL;
- if (poke_type == BPF_MOD_CALL)
- branch_type = AARCH64_INSN_BRANCH_LINK;
- else
- branch_type = AARCH64_INSN_BRANCH_NOLINK;
- if (gen_branch_or_nop(branch_type, ip, old_addr, plt, &old_insn) < 0)
- return -EFAULT;
- if (gen_branch_or_nop(branch_type, ip, new_addr, plt, &new_insn) < 0)
- return -EFAULT;
- if (is_long_jump(ip, new_addr))
- plt_target = (u64)new_addr;
- else if (is_long_jump(ip, old_addr))
-
- plt_target = (u64)&dummy_tramp;
- if (plt_target) {
-
- if (set_memory_rw(PAGE_MASK & ((uintptr_t)&plt->target), 1))
- return -EFAULT;
- WRITE_ONCE(plt->target, plt_target);
- set_memory_ro(PAGE_MASK & ((uintptr_t)&plt->target), 1);
-
- }
-
- if (old_insn == new_insn)
- return 0;
- mutex_lock(&text_mutex);
- if (aarch64_insn_read(ip, &replaced)) {
- ret = -EFAULT;
- goto out;
- }
- if (replaced != old_insn) {
- ret = -EFAULT;
- goto out;
- }
-
- ret = aarch64_insn_patch_text_nosync(ip, new_insn);
- out:
- mutex_unlock(&text_mutex);
- return ret;
- }
|