bpf_jit_32.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Just-In-Time compiler for eBPF filters on 32bit ARM
  4. *
  5. * Copyright (c) 2023 Puranjay Mohan <puranjay12@gmail.com>
  6. * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
  7. * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
  8. */
  9. #include <linux/bpf.h>
  10. #include <linux/bitops.h>
  11. #include <linux/compiler.h>
  12. #include <linux/errno.h>
  13. #include <linux/filter.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/string.h>
  16. #include <linux/slab.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/math64.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/hwcap.h>
  21. #include <asm/opcodes.h>
  22. #include <asm/system_info.h>
  23. #include "bpf_jit_32.h"
  24. /*
  25. * eBPF prog stack layout:
  26. *
  27. * high
  28. * original ARM_SP => +-----+
  29. * | | callee saved registers
  30. * +-----+ <= (BPF_FP + SCRATCH_SIZE)
  31. * | ... | eBPF JIT scratch space
  32. * eBPF fp register => +-----+
  33. * (BPF_FP) | ... | eBPF prog stack
  34. * +-----+
  35. * |RSVD | JIT scratchpad
  36. * current ARM_SP => +-----+ <= (BPF_FP - STACK_SIZE + SCRATCH_SIZE)
  37. * | ... | caller-saved registers
  38. * +-----+
  39. * | ... | arguments passed on stack
  40. * ARM_SP during call => +-----|
  41. * | |
  42. * | ... | Function call stack
  43. * | |
  44. * +-----+
  45. * low
  46. *
  47. * The callee saved registers depends on whether frame pointers are enabled.
  48. * With frame pointers (to be compliant with the ABI):
  49. *
  50. * high
  51. * original ARM_SP => +--------------+ \
  52. * | pc | |
  53. * current ARM_FP => +--------------+ } callee saved registers
  54. * |r4-r9,fp,ip,lr| |
  55. * +--------------+ /
  56. * low
  57. *
  58. * Without frame pointers:
  59. *
  60. * high
  61. * original ARM_SP => +--------------+
  62. * | r4-r9,fp,lr | callee saved registers
  63. * current ARM_FP => +--------------+
  64. * low
  65. *
  66. * When popping registers off the stack at the end of a BPF function, we
  67. * reference them via the current ARM_FP register.
  68. *
  69. * Some eBPF operations are implemented via a call to a helper function.
  70. * Such calls are "invisible" in the eBPF code, so it is up to the calling
  71. * program to preserve any caller-saved ARM registers during the call. The
  72. * JIT emits code to push and pop those registers onto the stack, immediately
  73. * above the callee stack frame.
  74. */
  75. #define CALLEE_MASK (1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \
  76. 1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R9 | \
  77. 1 << ARM_FP)
  78. #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR)
  79. #define CALLEE_POP_MASK (CALLEE_MASK | 1 << ARM_PC)
  80. #define CALLER_MASK (1 << ARM_R0 | 1 << ARM_R1 | 1 << ARM_R2 | 1 << ARM_R3)
  81. enum {
  82. /* Stack layout - these are offsets from (top of stack - 4) */
  83. BPF_R2_HI,
  84. BPF_R2_LO,
  85. BPF_R3_HI,
  86. BPF_R3_LO,
  87. BPF_R4_HI,
  88. BPF_R4_LO,
  89. BPF_R5_HI,
  90. BPF_R5_LO,
  91. BPF_R7_HI,
  92. BPF_R7_LO,
  93. BPF_R8_HI,
  94. BPF_R8_LO,
  95. BPF_R9_HI,
  96. BPF_R9_LO,
  97. BPF_FP_HI,
  98. BPF_FP_LO,
  99. BPF_TC_HI,
  100. BPF_TC_LO,
  101. BPF_AX_HI,
  102. BPF_AX_LO,
  103. /* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4,
  104. * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9,
  105. * BPF_REG_FP and Tail call counts.
  106. */
  107. BPF_JIT_SCRATCH_REGS,
  108. };
  109. /*
  110. * Negative "register" values indicate the register is stored on the stack
  111. * and are the offset from the top of the eBPF JIT scratch space.
  112. */
  113. #define STACK_OFFSET(k) (-4 - (k) * 4)
  114. #define SCRATCH_SIZE (BPF_JIT_SCRATCH_REGS * 4)
  115. #ifdef CONFIG_FRAME_POINTER
  116. #define EBPF_SCRATCH_TO_ARM_FP(x) ((x) - 4 * hweight16(CALLEE_PUSH_MASK) - 4)
  117. #else
  118. #define EBPF_SCRATCH_TO_ARM_FP(x) (x)
  119. #endif
  120. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0) /* TEMP Register 1 */
  121. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1) /* TEMP Register 2 */
  122. #define TCALL_CNT (MAX_BPF_JIT_REG + 2) /* Tail Call Count */
  123. #define FLAG_IMM_OVERFLOW (1 << 0)
  124. /*
  125. * Map eBPF registers to ARM 32bit registers or stack scratch space.
  126. *
  127. * 1. First argument is passed using the arm 32bit registers and rest of the
  128. * arguments are passed on stack scratch space.
  129. * 2. First callee-saved argument is mapped to arm 32 bit registers and rest
  130. * arguments are mapped to scratch space on stack.
  131. * 3. We need two 64 bit temp registers to do complex operations on eBPF
  132. * registers.
  133. *
  134. * As the eBPF registers are all 64 bit registers and arm has only 32 bit
  135. * registers, we have to map each eBPF registers with two arm 32 bit regs or
  136. * scratch memory space and we have to build eBPF 64 bit register from those.
  137. *
  138. */
  139. static const s8 bpf2a32[][2] = {
  140. /* return value from in-kernel function, and exit value from eBPF */
  141. [BPF_REG_0] = {ARM_R1, ARM_R0},
  142. /* arguments from eBPF program to in-kernel function */
  143. [BPF_REG_1] = {ARM_R3, ARM_R2},
  144. /* Stored on stack scratch space */
  145. [BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)},
  146. [BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)},
  147. [BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)},
  148. [BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)},
  149. /* callee saved registers that in-kernel function will preserve */
  150. [BPF_REG_6] = {ARM_R5, ARM_R4},
  151. /* Stored on stack scratch space */
  152. [BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
  153. [BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
  154. [BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
  155. /* Read only Frame Pointer to access Stack */
  156. [BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
  157. /* Temporary Register for BPF JIT, can be used
  158. * for constant blindings and others.
  159. */
  160. [TMP_REG_1] = {ARM_R7, ARM_R6},
  161. [TMP_REG_2] = {ARM_R9, ARM_R8},
  162. /* Tail call count. Stored on stack scratch space. */
  163. [TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)},
  164. /* temporary register for blinding constants.
  165. * Stored on stack scratch space.
  166. */
  167. [BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
  168. };
  169. #define dst_lo dst[1]
  170. #define dst_hi dst[0]
  171. #define src_lo src[1]
  172. #define src_hi src[0]
  173. /*
  174. * JIT Context:
  175. *
  176. * prog : bpf_prog
  177. * idx : index of current last JITed instruction.
  178. * prologue_bytes : bytes used in prologue.
  179. * epilogue_offset : offset of epilogue starting.
  180. * offsets : array of eBPF instruction offsets in
  181. * JITed code.
  182. * target : final JITed code.
  183. * epilogue_bytes : no of bytes used in epilogue.
  184. * imm_count : no of immediate counts used for global
  185. * variables.
  186. * imms : array of global variable addresses.
  187. */
  188. struct jit_ctx {
  189. const struct bpf_prog *prog;
  190. unsigned int idx;
  191. unsigned int prologue_bytes;
  192. unsigned int epilogue_offset;
  193. unsigned int cpu_architecture;
  194. u32 flags;
  195. u32 *offsets;
  196. u32 *target;
  197. u32 stack_size;
  198. #if __LINUX_ARM_ARCH__ < 7
  199. u16 epilogue_bytes;
  200. u16 imm_count;
  201. u32 *imms;
  202. #endif
  203. };
  204. /*
  205. * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
  206. * (where the assembly routines like __aeabi_uidiv could cause problems).
  207. */
  208. static u32 jit_udiv32(u32 dividend, u32 divisor)
  209. {
  210. return dividend / divisor;
  211. }
  212. static u32 jit_mod32(u32 dividend, u32 divisor)
  213. {
  214. return dividend % divisor;
  215. }
  216. static s32 jit_sdiv32(s32 dividend, s32 divisor)
  217. {
  218. return dividend / divisor;
  219. }
  220. static s32 jit_smod32(s32 dividend, s32 divisor)
  221. {
  222. return dividend % divisor;
  223. }
  224. /* Wrappers for 64-bit div/mod */
  225. static u64 jit_udiv64(u64 dividend, u64 divisor)
  226. {
  227. return div64_u64(dividend, divisor);
  228. }
  229. static u64 jit_mod64(u64 dividend, u64 divisor)
  230. {
  231. u64 rem;
  232. div64_u64_rem(dividend, divisor, &rem);
  233. return rem;
  234. }
  235. static s64 jit_sdiv64(s64 dividend, s64 divisor)
  236. {
  237. return div64_s64(dividend, divisor);
  238. }
  239. static s64 jit_smod64(s64 dividend, s64 divisor)
  240. {
  241. u64 q;
  242. q = div64_s64(dividend, divisor);
  243. return dividend - q * divisor;
  244. }
  245. static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
  246. {
  247. inst |= (cond << 28);
  248. inst = __opcode_to_mem_arm(inst);
  249. if (ctx->target != NULL)
  250. ctx->target[ctx->idx] = inst;
  251. ctx->idx++;
  252. }
  253. /*
  254. * Emit an instruction that will be executed unconditionally.
  255. */
  256. static inline void emit(u32 inst, struct jit_ctx *ctx)
  257. {
  258. _emit(ARM_COND_AL, inst, ctx);
  259. }
  260. /*
  261. * This is rather horrid, but necessary to convert an integer constant
  262. * to an immediate operand for the opcodes, and be able to detect at
  263. * build time whether the constant can't be converted (iow, usable in
  264. * BUILD_BUG_ON()).
  265. */
  266. #define imm12val(v, s) (rol32(v, (s)) | (s) << 7)
  267. #define const_imm8m(x) \
  268. ({ int r; \
  269. u32 v = (x); \
  270. if (!(v & ~0x000000ff)) \
  271. r = imm12val(v, 0); \
  272. else if (!(v & ~0xc000003f)) \
  273. r = imm12val(v, 2); \
  274. else if (!(v & ~0xf000000f)) \
  275. r = imm12val(v, 4); \
  276. else if (!(v & ~0xfc000003)) \
  277. r = imm12val(v, 6); \
  278. else if (!(v & ~0xff000000)) \
  279. r = imm12val(v, 8); \
  280. else if (!(v & ~0x3fc00000)) \
  281. r = imm12val(v, 10); \
  282. else if (!(v & ~0x0ff00000)) \
  283. r = imm12val(v, 12); \
  284. else if (!(v & ~0x03fc0000)) \
  285. r = imm12val(v, 14); \
  286. else if (!(v & ~0x00ff0000)) \
  287. r = imm12val(v, 16); \
  288. else if (!(v & ~0x003fc000)) \
  289. r = imm12val(v, 18); \
  290. else if (!(v & ~0x000ff000)) \
  291. r = imm12val(v, 20); \
  292. else if (!(v & ~0x0003fc00)) \
  293. r = imm12val(v, 22); \
  294. else if (!(v & ~0x0000ff00)) \
  295. r = imm12val(v, 24); \
  296. else if (!(v & ~0x00003fc0)) \
  297. r = imm12val(v, 26); \
  298. else if (!(v & ~0x00000ff0)) \
  299. r = imm12val(v, 28); \
  300. else if (!(v & ~0x000003fc)) \
  301. r = imm12val(v, 30); \
  302. else \
  303. r = -1; \
  304. r; })
  305. /*
  306. * Checks if immediate value can be converted to imm12(12 bits) value.
  307. */
  308. static int imm8m(u32 x)
  309. {
  310. u32 rot;
  311. for (rot = 0; rot < 16; rot++)
  312. if ((x & ~ror32(0xff, 2 * rot)) == 0)
  313. return rol32(x, 2 * rot) | (rot << 8);
  314. return -1;
  315. }
  316. #define imm8m(x) (__builtin_constant_p(x) ? const_imm8m(x) : imm8m(x))
  317. static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12)
  318. {
  319. op |= rt << 12 | rn << 16;
  320. if (imm12 >= 0)
  321. op |= ARM_INST_LDST__U;
  322. else
  323. imm12 = -imm12;
  324. return op | (imm12 & ARM_INST_LDST__IMM12);
  325. }
  326. static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8)
  327. {
  328. op |= rt << 12 | rn << 16;
  329. if (imm8 >= 0)
  330. op |= ARM_INST_LDST__U;
  331. else
  332. imm8 = -imm8;
  333. return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f);
  334. }
  335. #define ARM_LDR_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off)
  336. #define ARM_LDRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off)
  337. #define ARM_LDRD_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRD_I, rt, rn, off)
  338. #define ARM_LDRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off)
  339. #define ARM_LDRSH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRSH_I, rt, rn, off)
  340. #define ARM_LDRSB_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRSB_I, rt, rn, off)
  341. #define ARM_STR_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off)
  342. #define ARM_STRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off)
  343. #define ARM_STRD_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRD_I, rt, rn, off)
  344. #define ARM_STRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off)
  345. /*
  346. * Initializes the JIT space with undefined instructions.
  347. */
  348. static void jit_fill_hole(void *area, unsigned int size)
  349. {
  350. u32 *ptr;
  351. /* We are guaranteed to have aligned memory. */
  352. for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
  353. *ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
  354. }
  355. #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
  356. /* EABI requires the stack to be aligned to 64-bit boundaries */
  357. #define STACK_ALIGNMENT 8
  358. #else
  359. /* Stack must be aligned to 32-bit boundaries */
  360. #define STACK_ALIGNMENT 4
  361. #endif
  362. /* total stack size used in JITed code */
  363. #define _STACK_SIZE (ctx->prog->aux->stack_depth + SCRATCH_SIZE)
  364. #define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
  365. #if __LINUX_ARM_ARCH__ < 7
  366. static u16 imm_offset(u32 k, struct jit_ctx *ctx)
  367. {
  368. unsigned int i = 0, offset;
  369. u16 imm;
  370. /* on the "fake" run we just count them (duplicates included) */
  371. if (ctx->target == NULL) {
  372. ctx->imm_count++;
  373. return 0;
  374. }
  375. while ((i < ctx->imm_count) && ctx->imms[i]) {
  376. if (ctx->imms[i] == k)
  377. break;
  378. i++;
  379. }
  380. if (ctx->imms[i] == 0)
  381. ctx->imms[i] = k;
  382. /* constants go just after the epilogue */
  383. offset = ctx->offsets[ctx->prog->len - 1] * 4;
  384. offset += ctx->prologue_bytes;
  385. offset += ctx->epilogue_bytes;
  386. offset += i * 4;
  387. ctx->target[offset / 4] = k;
  388. /* PC in ARM mode == address of the instruction + 8 */
  389. imm = offset - (8 + ctx->idx * 4);
  390. if (imm & ~0xfff) {
  391. /*
  392. * literal pool is too far, signal it into flags. we
  393. * can only detect it on the second pass unfortunately.
  394. */
  395. ctx->flags |= FLAG_IMM_OVERFLOW;
  396. return 0;
  397. }
  398. return imm;
  399. }
  400. #endif /* __LINUX_ARM_ARCH__ */
  401. static inline int bpf2a32_offset(int bpf_to, int bpf_from,
  402. const struct jit_ctx *ctx) {
  403. int to, from;
  404. if (ctx->target == NULL)
  405. return 0;
  406. to = ctx->offsets[bpf_to];
  407. from = ctx->offsets[bpf_from];
  408. return to - from - 1;
  409. }
  410. /*
  411. * Move an immediate that's not an imm8m to a core register.
  412. */
  413. static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
  414. {
  415. #if __LINUX_ARM_ARCH__ < 7
  416. emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
  417. #else
  418. emit(ARM_MOVW(rd, val & 0xffff), ctx);
  419. if (val > 0xffff)
  420. emit(ARM_MOVT(rd, val >> 16), ctx);
  421. #endif
  422. }
  423. static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx)
  424. {
  425. int imm12 = imm8m(val);
  426. if (imm12 >= 0)
  427. emit(ARM_MOV_I(rd, imm12), ctx);
  428. else
  429. emit_mov_i_no8m(rd, val, ctx);
  430. }
  431. static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx)
  432. {
  433. if (elf_hwcap & HWCAP_THUMB)
  434. emit(ARM_BX(tgt_reg), ctx);
  435. else
  436. emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
  437. }
  438. static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
  439. {
  440. #if __LINUX_ARM_ARCH__ < 5
  441. emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
  442. emit_bx_r(tgt_reg, ctx);
  443. #else
  444. emit(ARM_BLX_R(tgt_reg), ctx);
  445. #endif
  446. }
  447. static inline int epilogue_offset(const struct jit_ctx *ctx)
  448. {
  449. int to, from;
  450. /* No need for 1st dummy run */
  451. if (ctx->target == NULL)
  452. return 0;
  453. to = ctx->epilogue_offset;
  454. from = ctx->idx;
  455. return to - from - 2;
  456. }
  457. static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op, u8 sign)
  458. {
  459. const int exclude_mask = BIT(ARM_R0) | BIT(ARM_R1);
  460. const s8 *tmp = bpf2a32[TMP_REG_1];
  461. u32 dst;
  462. #if __LINUX_ARM_ARCH__ == 7
  463. if (elf_hwcap & HWCAP_IDIVA) {
  464. if (op == BPF_DIV) {
  465. emit(sign ? ARM_SDIV(rd, rm, rn) : ARM_UDIV(rd, rm, rn), ctx);
  466. } else {
  467. emit(sign ? ARM_SDIV(ARM_IP, rm, rn) : ARM_UDIV(ARM_IP, rm, rn), ctx);
  468. emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx);
  469. }
  470. return;
  471. }
  472. #endif
  473. /*
  474. * For BPF_ALU | BPF_DIV | BPF_K instructions
  475. * As ARM_R1 and ARM_R0 contains 1st argument of bpf
  476. * function, we need to save it on caller side to save
  477. * it from getting destroyed within callee.
  478. * After the return from the callee, we restore ARM_R0
  479. * ARM_R1.
  480. */
  481. if (rn != ARM_R1) {
  482. emit(ARM_MOV_R(tmp[0], ARM_R1), ctx);
  483. emit(ARM_MOV_R(ARM_R1, rn), ctx);
  484. }
  485. if (rm != ARM_R0) {
  486. emit(ARM_MOV_R(tmp[1], ARM_R0), ctx);
  487. emit(ARM_MOV_R(ARM_R0, rm), ctx);
  488. }
  489. /* Push caller-saved registers on stack */
  490. emit(ARM_PUSH(CALLER_MASK & ~exclude_mask), ctx);
  491. /* Call appropriate function */
  492. if (sign) {
  493. if (op == BPF_DIV)
  494. dst = (u32)jit_sdiv32;
  495. else
  496. dst = (u32)jit_smod32;
  497. } else {
  498. if (op == BPF_DIV)
  499. dst = (u32)jit_udiv32;
  500. else
  501. dst = (u32)jit_mod32;
  502. }
  503. emit_mov_i(ARM_IP, dst, ctx);
  504. emit_blx_r(ARM_IP, ctx);
  505. /* Restore caller-saved registers from stack */
  506. emit(ARM_POP(CALLER_MASK & ~exclude_mask), ctx);
  507. /* Save return value */
  508. if (rd != ARM_R0)
  509. emit(ARM_MOV_R(rd, ARM_R0), ctx);
  510. /* Restore ARM_R0 and ARM_R1 */
  511. if (rn != ARM_R1)
  512. emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx);
  513. if (rm != ARM_R0)
  514. emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx);
  515. }
  516. static inline void emit_udivmod64(const s8 *rd, const s8 *rm, const s8 *rn, struct jit_ctx *ctx,
  517. u8 op, u8 sign)
  518. {
  519. u32 dst;
  520. /* Push caller-saved registers on stack */
  521. emit(ARM_PUSH(CALLER_MASK), ctx);
  522. /*
  523. * As we are implementing 64-bit div/mod as function calls, We need to put the dividend in
  524. * R0-R1 and the divisor in R2-R3. As we have already pushed these registers on the stack,
  525. * we can recover them later after returning from the function call.
  526. */
  527. if (rm[1] != ARM_R0 || rn[1] != ARM_R2) {
  528. /*
  529. * Move Rm to {R1, R0} if it is not already there.
  530. */
  531. if (rm[1] != ARM_R0) {
  532. if (rn[1] == ARM_R0)
  533. emit(ARM_PUSH(BIT(ARM_R0) | BIT(ARM_R1)), ctx);
  534. emit(ARM_MOV_R(ARM_R1, rm[0]), ctx);
  535. emit(ARM_MOV_R(ARM_R0, rm[1]), ctx);
  536. if (rn[1] == ARM_R0) {
  537. emit(ARM_POP(BIT(ARM_R2) | BIT(ARM_R3)), ctx);
  538. goto cont;
  539. }
  540. }
  541. /*
  542. * Move Rn to {R3, R2} if it is not already there.
  543. */
  544. if (rn[1] != ARM_R2) {
  545. emit(ARM_MOV_R(ARM_R3, rn[0]), ctx);
  546. emit(ARM_MOV_R(ARM_R2, rn[1]), ctx);
  547. }
  548. }
  549. cont:
  550. /* Call appropriate function */
  551. if (sign) {
  552. if (op == BPF_DIV)
  553. dst = (u32)jit_sdiv64;
  554. else
  555. dst = (u32)jit_smod64;
  556. } else {
  557. if (op == BPF_DIV)
  558. dst = (u32)jit_udiv64;
  559. else
  560. dst = (u32)jit_mod64;
  561. }
  562. emit_mov_i(ARM_IP, dst, ctx);
  563. emit_blx_r(ARM_IP, ctx);
  564. /* Save return value */
  565. if (rd[1] != ARM_R0) {
  566. emit(ARM_MOV_R(rd[0], ARM_R1), ctx);
  567. emit(ARM_MOV_R(rd[1], ARM_R0), ctx);
  568. }
  569. /* Recover {R3, R2} and {R1, R0} from stack if they are not Rd */
  570. if (rd[1] != ARM_R0 && rd[1] != ARM_R2) {
  571. emit(ARM_POP(CALLER_MASK), ctx);
  572. } else if (rd[1] != ARM_R0) {
  573. emit(ARM_POP(BIT(ARM_R0) | BIT(ARM_R1)), ctx);
  574. emit(ARM_ADD_I(ARM_SP, ARM_SP, 8), ctx);
  575. } else {
  576. emit(ARM_ADD_I(ARM_SP, ARM_SP, 8), ctx);
  577. emit(ARM_POP(BIT(ARM_R2) | BIT(ARM_R3)), ctx);
  578. }
  579. }
  580. /* Is the translated BPF register on stack? */
  581. static bool is_stacked(s8 reg)
  582. {
  583. return reg < 0;
  584. }
  585. /* If a BPF register is on the stack (stk is true), load it to the
  586. * supplied temporary register and return the temporary register
  587. * for subsequent operations, otherwise just use the CPU register.
  588. */
  589. static s8 arm_bpf_get_reg32(s8 reg, s8 tmp, struct jit_ctx *ctx)
  590. {
  591. if (is_stacked(reg)) {
  592. emit(ARM_LDR_I(tmp, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
  593. reg = tmp;
  594. }
  595. return reg;
  596. }
  597. static const s8 *arm_bpf_get_reg64(const s8 *reg, const s8 *tmp,
  598. struct jit_ctx *ctx)
  599. {
  600. if (is_stacked(reg[1])) {
  601. if (__LINUX_ARM_ARCH__ >= 6 ||
  602. ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
  603. emit(ARM_LDRD_I(tmp[1], ARM_FP,
  604. EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
  605. } else {
  606. emit(ARM_LDR_I(tmp[1], ARM_FP,
  607. EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
  608. emit(ARM_LDR_I(tmp[0], ARM_FP,
  609. EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
  610. }
  611. reg = tmp;
  612. }
  613. return reg;
  614. }
  615. /* If a BPF register is on the stack (stk is true), save the register
  616. * back to the stack. If the source register is not the same, then
  617. * move it into the correct register.
  618. */
  619. static void arm_bpf_put_reg32(s8 reg, s8 src, struct jit_ctx *ctx)
  620. {
  621. if (is_stacked(reg))
  622. emit(ARM_STR_I(src, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
  623. else if (reg != src)
  624. emit(ARM_MOV_R(reg, src), ctx);
  625. }
  626. static void arm_bpf_put_reg64(const s8 *reg, const s8 *src,
  627. struct jit_ctx *ctx)
  628. {
  629. if (is_stacked(reg[1])) {
  630. if (__LINUX_ARM_ARCH__ >= 6 ||
  631. ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
  632. emit(ARM_STRD_I(src[1], ARM_FP,
  633. EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
  634. } else {
  635. emit(ARM_STR_I(src[1], ARM_FP,
  636. EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
  637. emit(ARM_STR_I(src[0], ARM_FP,
  638. EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
  639. }
  640. } else {
  641. if (reg[1] != src[1])
  642. emit(ARM_MOV_R(reg[1], src[1]), ctx);
  643. if (reg[0] != src[0])
  644. emit(ARM_MOV_R(reg[0], src[0]), ctx);
  645. }
  646. }
  647. static inline void emit_a32_mov_i(const s8 dst, const u32 val,
  648. struct jit_ctx *ctx)
  649. {
  650. const s8 *tmp = bpf2a32[TMP_REG_1];
  651. if (is_stacked(dst)) {
  652. emit_mov_i(tmp[1], val, ctx);
  653. arm_bpf_put_reg32(dst, tmp[1], ctx);
  654. } else {
  655. emit_mov_i(dst, val, ctx);
  656. }
  657. }
  658. static void emit_a32_mov_i64(const s8 dst[], u64 val, struct jit_ctx *ctx)
  659. {
  660. const s8 *tmp = bpf2a32[TMP_REG_1];
  661. const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
  662. emit_mov_i(rd[1], (u32)val, ctx);
  663. emit_mov_i(rd[0], val >> 32, ctx);
  664. arm_bpf_put_reg64(dst, rd, ctx);
  665. }
  666. /* Sign extended move */
  667. static inline void emit_a32_mov_se_i64(const bool is64, const s8 dst[],
  668. const u32 val, struct jit_ctx *ctx) {
  669. u64 val64 = val;
  670. if (is64 && (val & (1<<31)))
  671. val64 |= 0xffffffff00000000ULL;
  672. emit_a32_mov_i64(dst, val64, ctx);
  673. }
  674. static inline void emit_a32_add_r(const u8 dst, const u8 src,
  675. const bool is64, const bool hi,
  676. struct jit_ctx *ctx) {
  677. /* 64 bit :
  678. * adds dst_lo, dst_lo, src_lo
  679. * adc dst_hi, dst_hi, src_hi
  680. * 32 bit :
  681. * add dst_lo, dst_lo, src_lo
  682. */
  683. if (!hi && is64)
  684. emit(ARM_ADDS_R(dst, dst, src), ctx);
  685. else if (hi && is64)
  686. emit(ARM_ADC_R(dst, dst, src), ctx);
  687. else
  688. emit(ARM_ADD_R(dst, dst, src), ctx);
  689. }
  690. static inline void emit_a32_sub_r(const u8 dst, const u8 src,
  691. const bool is64, const bool hi,
  692. struct jit_ctx *ctx) {
  693. /* 64 bit :
  694. * subs dst_lo, dst_lo, src_lo
  695. * sbc dst_hi, dst_hi, src_hi
  696. * 32 bit :
  697. * sub dst_lo, dst_lo, src_lo
  698. */
  699. if (!hi && is64)
  700. emit(ARM_SUBS_R(dst, dst, src), ctx);
  701. else if (hi && is64)
  702. emit(ARM_SBC_R(dst, dst, src), ctx);
  703. else
  704. emit(ARM_SUB_R(dst, dst, src), ctx);
  705. }
  706. static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64,
  707. const bool hi, const u8 op, struct jit_ctx *ctx){
  708. switch (BPF_OP(op)) {
  709. /* dst = dst + src */
  710. case BPF_ADD:
  711. emit_a32_add_r(dst, src, is64, hi, ctx);
  712. break;
  713. /* dst = dst - src */
  714. case BPF_SUB:
  715. emit_a32_sub_r(dst, src, is64, hi, ctx);
  716. break;
  717. /* dst = dst | src */
  718. case BPF_OR:
  719. emit(ARM_ORR_R(dst, dst, src), ctx);
  720. break;
  721. /* dst = dst & src */
  722. case BPF_AND:
  723. emit(ARM_AND_R(dst, dst, src), ctx);
  724. break;
  725. /* dst = dst ^ src */
  726. case BPF_XOR:
  727. emit(ARM_EOR_R(dst, dst, src), ctx);
  728. break;
  729. /* dst = dst * src */
  730. case BPF_MUL:
  731. emit(ARM_MUL(dst, dst, src), ctx);
  732. break;
  733. /* dst = dst << src */
  734. case BPF_LSH:
  735. emit(ARM_LSL_R(dst, dst, src), ctx);
  736. break;
  737. /* dst = dst >> src */
  738. case BPF_RSH:
  739. emit(ARM_LSR_R(dst, dst, src), ctx);
  740. break;
  741. /* dst = dst >> src (signed)*/
  742. case BPF_ARSH:
  743. emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx);
  744. break;
  745. }
  746. }
  747. /* ALU operation (64 bit) */
  748. static inline void emit_a32_alu_r64(const bool is64, const s8 dst[],
  749. const s8 src[], struct jit_ctx *ctx,
  750. const u8 op) {
  751. const s8 *tmp = bpf2a32[TMP_REG_1];
  752. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  753. const s8 *rd;
  754. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  755. if (is64) {
  756. const s8 *rs;
  757. rs = arm_bpf_get_reg64(src, tmp2, ctx);
  758. /* ALU operation */
  759. emit_alu_r(rd[1], rs[1], true, false, op, ctx);
  760. emit_alu_r(rd[0], rs[0], true, true, op, ctx);
  761. } else {
  762. s8 rs;
  763. rs = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  764. /* ALU operation */
  765. emit_alu_r(rd[1], rs, true, false, op, ctx);
  766. if (!ctx->prog->aux->verifier_zext)
  767. emit_a32_mov_i(rd[0], 0, ctx);
  768. }
  769. arm_bpf_put_reg64(dst, rd, ctx);
  770. }
  771. /* dst = src (4 bytes)*/
  772. static inline void emit_a32_mov_r(const s8 dst, const s8 src, struct jit_ctx *ctx) {
  773. const s8 *tmp = bpf2a32[TMP_REG_1];
  774. s8 rt;
  775. rt = arm_bpf_get_reg32(src, tmp[0], ctx);
  776. arm_bpf_put_reg32(dst, rt, ctx);
  777. }
  778. /* dst = src */
  779. static inline void emit_a32_mov_r64(const bool is64, const s8 dst[],
  780. const s8 src[],
  781. struct jit_ctx *ctx) {
  782. if (!is64) {
  783. emit_a32_mov_r(dst_lo, src_lo, ctx);
  784. if (!ctx->prog->aux->verifier_zext)
  785. /* Zero out high 4 bytes */
  786. emit_a32_mov_i(dst_hi, 0, ctx);
  787. } else if (__LINUX_ARM_ARCH__ < 6 &&
  788. ctx->cpu_architecture < CPU_ARCH_ARMv5TE) {
  789. /* complete 8 byte move */
  790. emit_a32_mov_r(dst_lo, src_lo, ctx);
  791. emit_a32_mov_r(dst_hi, src_hi, ctx);
  792. } else if (is_stacked(src_lo) && is_stacked(dst_lo)) {
  793. const u8 *tmp = bpf2a32[TMP_REG_1];
  794. emit(ARM_LDRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
  795. emit(ARM_STRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
  796. } else if (is_stacked(src_lo)) {
  797. emit(ARM_LDRD_I(dst[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
  798. } else if (is_stacked(dst_lo)) {
  799. emit(ARM_STRD_I(src[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
  800. } else {
  801. emit(ARM_MOV_R(dst[0], src[0]), ctx);
  802. emit(ARM_MOV_R(dst[1], src[1]), ctx);
  803. }
  804. }
  805. /* dst = (signed)src */
  806. static inline void emit_a32_movsx_r64(const bool is64, const u8 off, const s8 dst[], const s8 src[],
  807. struct jit_ctx *ctx) {
  808. const s8 *tmp = bpf2a32[TMP_REG_1];
  809. s8 rs;
  810. s8 rd;
  811. if (is_stacked(dst_lo))
  812. rd = tmp[1];
  813. else
  814. rd = dst_lo;
  815. rs = arm_bpf_get_reg32(src_lo, rd, ctx);
  816. /* rs may be one of src[1], dst[1], or tmp[1] */
  817. /* Sign extend rs if needed. If off == 32, lower 32-bits of src are moved to dst and sign
  818. * extension only happens in the upper 64 bits.
  819. */
  820. if (off != 32) {
  821. /* Sign extend rs into rd */
  822. emit(ARM_LSL_I(rd, rs, 32 - off), ctx);
  823. emit(ARM_ASR_I(rd, rd, 32 - off), ctx);
  824. } else {
  825. rd = rs;
  826. }
  827. /* Write rd to dst_lo
  828. *
  829. * Optimization:
  830. * Assume:
  831. * 1. dst == src and stacked.
  832. * 2. off == 32
  833. *
  834. * In this case src_lo was loaded into rd(tmp[1]) but rd was not sign extended as off==32.
  835. * So, we don't need to write rd back to dst_lo as they have the same value.
  836. * This saves us one str instruction.
  837. */
  838. if (dst_lo != src_lo || off != 32)
  839. arm_bpf_put_reg32(dst_lo, rd, ctx);
  840. if (!is64) {
  841. if (!ctx->prog->aux->verifier_zext)
  842. /* Zero out high 4 bytes */
  843. emit_a32_mov_i(dst_hi, 0, ctx);
  844. } else {
  845. if (is_stacked(dst_hi)) {
  846. emit(ARM_ASR_I(tmp[0], rd, 31), ctx);
  847. arm_bpf_put_reg32(dst_hi, tmp[0], ctx);
  848. } else {
  849. emit(ARM_ASR_I(dst_hi, rd, 31), ctx);
  850. }
  851. }
  852. }
  853. /* Shift operations */
  854. static inline void emit_a32_alu_i(const s8 dst, const u32 val,
  855. struct jit_ctx *ctx, const u8 op) {
  856. const s8 *tmp = bpf2a32[TMP_REG_1];
  857. s8 rd;
  858. rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
  859. /* Do shift operation */
  860. switch (op) {
  861. case BPF_LSH:
  862. emit(ARM_LSL_I(rd, rd, val), ctx);
  863. break;
  864. case BPF_RSH:
  865. emit(ARM_LSR_I(rd, rd, val), ctx);
  866. break;
  867. case BPF_ARSH:
  868. emit(ARM_ASR_I(rd, rd, val), ctx);
  869. break;
  870. case BPF_NEG:
  871. emit(ARM_RSB_I(rd, rd, val), ctx);
  872. break;
  873. }
  874. arm_bpf_put_reg32(dst, rd, ctx);
  875. }
  876. /* dst = ~dst (64 bit) */
  877. static inline void emit_a32_neg64(const s8 dst[],
  878. struct jit_ctx *ctx){
  879. const s8 *tmp = bpf2a32[TMP_REG_1];
  880. const s8 *rd;
  881. /* Setup Operand */
  882. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  883. /* Do Negate Operation */
  884. emit(ARM_RSBS_I(rd[1], rd[1], 0), ctx);
  885. emit(ARM_RSC_I(rd[0], rd[0], 0), ctx);
  886. arm_bpf_put_reg64(dst, rd, ctx);
  887. }
  888. /* dst = dst << src */
  889. static inline void emit_a32_lsh_r64(const s8 dst[], const s8 src[],
  890. struct jit_ctx *ctx) {
  891. const s8 *tmp = bpf2a32[TMP_REG_1];
  892. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  893. const s8 *rd;
  894. s8 rt;
  895. /* Setup Operands */
  896. rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  897. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  898. /* Do LSH operation */
  899. emit(ARM_SUB_I(ARM_IP, rt, 32), ctx);
  900. emit(ARM_RSB_I(tmp2[0], rt, 32), ctx);
  901. emit(ARM_MOV_SR(ARM_LR, rd[0], SRTYPE_ASL, rt), ctx);
  902. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[1], SRTYPE_ASL, ARM_IP), ctx);
  903. emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd[1], SRTYPE_LSR, tmp2[0]), ctx);
  904. emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_ASL, rt), ctx);
  905. arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
  906. arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
  907. }
  908. /* dst = dst >> src (signed)*/
  909. static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[],
  910. struct jit_ctx *ctx) {
  911. const s8 *tmp = bpf2a32[TMP_REG_1];
  912. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  913. const s8 *rd;
  914. s8 rt;
  915. /* Setup Operands */
  916. rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  917. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  918. /* Do the ARSH operation */
  919. emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
  920. emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
  921. emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
  922. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
  923. _emit(ARM_COND_PL,
  924. ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx);
  925. emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx);
  926. arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
  927. arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
  928. }
  929. /* dst = dst >> src */
  930. static inline void emit_a32_rsh_r64(const s8 dst[], const s8 src[],
  931. struct jit_ctx *ctx) {
  932. const s8 *tmp = bpf2a32[TMP_REG_1];
  933. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  934. const s8 *rd;
  935. s8 rt;
  936. /* Setup Operands */
  937. rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  938. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  939. /* Do RSH operation */
  940. emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
  941. emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
  942. emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
  943. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
  944. emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_LSR, tmp2[0]), ctx);
  945. emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_LSR, rt), ctx);
  946. arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
  947. arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
  948. }
  949. /* dst = dst << val */
  950. static inline void emit_a32_lsh_i64(const s8 dst[],
  951. const u32 val, struct jit_ctx *ctx){
  952. const s8 *tmp = bpf2a32[TMP_REG_1];
  953. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  954. const s8 *rd;
  955. /* Setup operands */
  956. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  957. /* Do LSH operation */
  958. if (val < 32) {
  959. emit(ARM_MOV_SI(tmp2[0], rd[0], SRTYPE_ASL, val), ctx);
  960. emit(ARM_ORR_SI(rd[0], tmp2[0], rd[1], SRTYPE_LSR, 32 - val), ctx);
  961. emit(ARM_MOV_SI(rd[1], rd[1], SRTYPE_ASL, val), ctx);
  962. } else {
  963. if (val == 32)
  964. emit(ARM_MOV_R(rd[0], rd[1]), ctx);
  965. else
  966. emit(ARM_MOV_SI(rd[0], rd[1], SRTYPE_ASL, val - 32), ctx);
  967. emit(ARM_EOR_R(rd[1], rd[1], rd[1]), ctx);
  968. }
  969. arm_bpf_put_reg64(dst, rd, ctx);
  970. }
  971. /* dst = dst >> val */
  972. static inline void emit_a32_rsh_i64(const s8 dst[],
  973. const u32 val, struct jit_ctx *ctx) {
  974. const s8 *tmp = bpf2a32[TMP_REG_1];
  975. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  976. const s8 *rd;
  977. /* Setup operands */
  978. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  979. /* Do LSR operation */
  980. if (val == 0) {
  981. /* An immediate value of 0 encodes a shift amount of 32
  982. * for LSR. To shift by 0, don't do anything.
  983. */
  984. } else if (val < 32) {
  985. emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
  986. emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
  987. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx);
  988. } else if (val == 32) {
  989. emit(ARM_MOV_R(rd[1], rd[0]), ctx);
  990. emit(ARM_MOV_I(rd[0], 0), ctx);
  991. } else {
  992. emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx);
  993. emit(ARM_MOV_I(rd[0], 0), ctx);
  994. }
  995. arm_bpf_put_reg64(dst, rd, ctx);
  996. }
  997. /* dst = dst >> val (signed) */
  998. static inline void emit_a32_arsh_i64(const s8 dst[],
  999. const u32 val, struct jit_ctx *ctx){
  1000. const s8 *tmp = bpf2a32[TMP_REG_1];
  1001. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1002. const s8 *rd;
  1003. /* Setup operands */
  1004. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  1005. /* Do ARSH operation */
  1006. if (val == 0) {
  1007. /* An immediate value of 0 encodes a shift amount of 32
  1008. * for ASR. To shift by 0, don't do anything.
  1009. */
  1010. } else if (val < 32) {
  1011. emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
  1012. emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
  1013. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx);
  1014. } else if (val == 32) {
  1015. emit(ARM_MOV_R(rd[1], rd[0]), ctx);
  1016. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
  1017. } else {
  1018. emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx);
  1019. emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
  1020. }
  1021. arm_bpf_put_reg64(dst, rd, ctx);
  1022. }
  1023. static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[],
  1024. struct jit_ctx *ctx) {
  1025. const s8 *tmp = bpf2a32[TMP_REG_1];
  1026. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1027. const s8 *rd, *rt;
  1028. /* Setup operands for multiplication */
  1029. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  1030. rt = arm_bpf_get_reg64(src, tmp2, ctx);
  1031. /* Do Multiplication */
  1032. emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx);
  1033. emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx);
  1034. emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
  1035. emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx);
  1036. emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx);
  1037. arm_bpf_put_reg32(dst_lo, ARM_IP, ctx);
  1038. arm_bpf_put_reg32(dst_hi, rd[0], ctx);
  1039. }
  1040. static bool is_ldst_imm(s16 off, const u8 size)
  1041. {
  1042. s16 off_max = 0;
  1043. switch (size) {
  1044. case BPF_B:
  1045. case BPF_W:
  1046. off_max = 0xfff;
  1047. break;
  1048. case BPF_H:
  1049. off_max = 0xff;
  1050. break;
  1051. case BPF_DW:
  1052. /* Need to make sure off+4 does not overflow. */
  1053. off_max = 0xfff - 4;
  1054. break;
  1055. }
  1056. return -off_max <= off && off <= off_max;
  1057. }
  1058. static bool is_ldst_imm8(s16 off, const u8 size)
  1059. {
  1060. s16 off_max = 0;
  1061. switch (size) {
  1062. case BPF_B:
  1063. off_max = 0xff;
  1064. break;
  1065. case BPF_W:
  1066. off_max = 0xfff;
  1067. break;
  1068. case BPF_H:
  1069. off_max = 0xff;
  1070. break;
  1071. }
  1072. return -off_max <= off && off <= off_max;
  1073. }
  1074. /* *(size *)(dst + off) = src */
  1075. static inline void emit_str_r(const s8 dst, const s8 src[],
  1076. s16 off, struct jit_ctx *ctx, const u8 sz){
  1077. const s8 *tmp = bpf2a32[TMP_REG_1];
  1078. s8 rd;
  1079. rd = arm_bpf_get_reg32(dst, tmp[1], ctx);
  1080. if (!is_ldst_imm(off, sz)) {
  1081. emit_a32_mov_i(tmp[0], off, ctx);
  1082. emit(ARM_ADD_R(tmp[0], tmp[0], rd), ctx);
  1083. rd = tmp[0];
  1084. off = 0;
  1085. }
  1086. switch (sz) {
  1087. case BPF_B:
  1088. /* Store a Byte */
  1089. emit(ARM_STRB_I(src_lo, rd, off), ctx);
  1090. break;
  1091. case BPF_H:
  1092. /* Store a HalfWord */
  1093. emit(ARM_STRH_I(src_lo, rd, off), ctx);
  1094. break;
  1095. case BPF_W:
  1096. /* Store a Word */
  1097. emit(ARM_STR_I(src_lo, rd, off), ctx);
  1098. break;
  1099. case BPF_DW:
  1100. /* Store a Double Word */
  1101. emit(ARM_STR_I(src_lo, rd, off), ctx);
  1102. emit(ARM_STR_I(src_hi, rd, off + 4), ctx);
  1103. break;
  1104. }
  1105. }
  1106. /* dst = *(size*)(src + off) */
  1107. static inline void emit_ldx_r(const s8 dst[], const s8 src,
  1108. s16 off, struct jit_ctx *ctx, const u8 sz){
  1109. const s8 *tmp = bpf2a32[TMP_REG_1];
  1110. const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
  1111. s8 rm = src;
  1112. if (!is_ldst_imm(off, sz)) {
  1113. emit_a32_mov_i(tmp[0], off, ctx);
  1114. emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
  1115. rm = tmp[0];
  1116. off = 0;
  1117. } else if (rd[1] == rm) {
  1118. emit(ARM_MOV_R(tmp[0], rm), ctx);
  1119. rm = tmp[0];
  1120. }
  1121. switch (sz) {
  1122. case BPF_B:
  1123. /* Load a Byte */
  1124. emit(ARM_LDRB_I(rd[1], rm, off), ctx);
  1125. if (!ctx->prog->aux->verifier_zext)
  1126. emit_a32_mov_i(rd[0], 0, ctx);
  1127. break;
  1128. case BPF_H:
  1129. /* Load a HalfWord */
  1130. emit(ARM_LDRH_I(rd[1], rm, off), ctx);
  1131. if (!ctx->prog->aux->verifier_zext)
  1132. emit_a32_mov_i(rd[0], 0, ctx);
  1133. break;
  1134. case BPF_W:
  1135. /* Load a Word */
  1136. emit(ARM_LDR_I(rd[1], rm, off), ctx);
  1137. if (!ctx->prog->aux->verifier_zext)
  1138. emit_a32_mov_i(rd[0], 0, ctx);
  1139. break;
  1140. case BPF_DW:
  1141. /* Load a Double Word */
  1142. emit(ARM_LDR_I(rd[1], rm, off), ctx);
  1143. emit(ARM_LDR_I(rd[0], rm, off + 4), ctx);
  1144. break;
  1145. }
  1146. arm_bpf_put_reg64(dst, rd, ctx);
  1147. }
  1148. /* dst = *(signed size*)(src + off) */
  1149. static inline void emit_ldsx_r(const s8 dst[], const s8 src,
  1150. s16 off, struct jit_ctx *ctx, const u8 sz){
  1151. const s8 *tmp = bpf2a32[TMP_REG_1];
  1152. const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
  1153. s8 rm = src;
  1154. int add_off;
  1155. if (!is_ldst_imm8(off, sz)) {
  1156. /*
  1157. * offset does not fit in the load/store immediate,
  1158. * construct an ADD instruction to apply the offset.
  1159. */
  1160. add_off = imm8m(off);
  1161. if (add_off > 0) {
  1162. emit(ARM_ADD_I(tmp[0], src, add_off), ctx);
  1163. rm = tmp[0];
  1164. } else {
  1165. emit_a32_mov_i(tmp[0], off, ctx);
  1166. emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
  1167. rm = tmp[0];
  1168. }
  1169. off = 0;
  1170. }
  1171. switch (sz) {
  1172. case BPF_B:
  1173. /* Load a Byte with sign extension*/
  1174. emit(ARM_LDRSB_I(rd[1], rm, off), ctx);
  1175. break;
  1176. case BPF_H:
  1177. /* Load a HalfWord with sign extension*/
  1178. emit(ARM_LDRSH_I(rd[1], rm, off), ctx);
  1179. break;
  1180. case BPF_W:
  1181. /* Load a Word*/
  1182. emit(ARM_LDR_I(rd[1], rm, off), ctx);
  1183. break;
  1184. }
  1185. /* Carry the sign extension to upper 32 bits */
  1186. emit(ARM_ASR_I(rd[0], rd[1], 31), ctx);
  1187. arm_bpf_put_reg64(dst, rd, ctx);
  1188. }
  1189. /* Arithmatic Operation */
  1190. static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
  1191. const u8 rn, struct jit_ctx *ctx, u8 op,
  1192. bool is_jmp64) {
  1193. switch (op) {
  1194. case BPF_JSET:
  1195. if (is_jmp64) {
  1196. emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
  1197. emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
  1198. emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
  1199. } else {
  1200. emit(ARM_ANDS_R(ARM_IP, rt, rn), ctx);
  1201. }
  1202. break;
  1203. case BPF_JEQ:
  1204. case BPF_JNE:
  1205. case BPF_JGT:
  1206. case BPF_JGE:
  1207. case BPF_JLE:
  1208. case BPF_JLT:
  1209. if (is_jmp64) {
  1210. emit(ARM_CMP_R(rd, rm), ctx);
  1211. /* Only compare low halve if high halve are equal. */
  1212. _emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
  1213. } else {
  1214. emit(ARM_CMP_R(rt, rn), ctx);
  1215. }
  1216. break;
  1217. case BPF_JSLE:
  1218. case BPF_JSGT:
  1219. emit(ARM_CMP_R(rn, rt), ctx);
  1220. if (is_jmp64)
  1221. emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
  1222. break;
  1223. case BPF_JSLT:
  1224. case BPF_JSGE:
  1225. emit(ARM_CMP_R(rt, rn), ctx);
  1226. if (is_jmp64)
  1227. emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
  1228. break;
  1229. }
  1230. }
  1231. static int out_offset = -1; /* initialized on the first pass of build_body() */
  1232. static int emit_bpf_tail_call(struct jit_ctx *ctx)
  1233. {
  1234. /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
  1235. const s8 *r2 = bpf2a32[BPF_REG_2];
  1236. const s8 *r3 = bpf2a32[BPF_REG_3];
  1237. const s8 *tmp = bpf2a32[TMP_REG_1];
  1238. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1239. const s8 *tcc = bpf2a32[TCALL_CNT];
  1240. const s8 *tc;
  1241. const int idx0 = ctx->idx;
  1242. #define cur_offset (ctx->idx - idx0)
  1243. #define jmp_offset (out_offset - (cur_offset) - 2)
  1244. u32 lo, hi;
  1245. s8 r_array, r_index;
  1246. int off;
  1247. /* if (index >= array->map.max_entries)
  1248. * goto out;
  1249. */
  1250. BUILD_BUG_ON(offsetof(struct bpf_array, map.max_entries) >
  1251. ARM_INST_LDST__IMM12);
  1252. off = offsetof(struct bpf_array, map.max_entries);
  1253. r_array = arm_bpf_get_reg32(r2[1], tmp2[0], ctx);
  1254. /* index is 32-bit for arrays */
  1255. r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx);
  1256. /* array->map.max_entries */
  1257. emit(ARM_LDR_I(tmp[1], r_array, off), ctx);
  1258. /* index >= array->map.max_entries */
  1259. emit(ARM_CMP_R(r_index, tmp[1]), ctx);
  1260. _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
  1261. /* tmp2[0] = array, tmp2[1] = index */
  1262. /*
  1263. * if (tail_call_cnt >= MAX_TAIL_CALL_CNT)
  1264. * goto out;
  1265. * tail_call_cnt++;
  1266. */
  1267. lo = (u32)MAX_TAIL_CALL_CNT;
  1268. hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
  1269. tc = arm_bpf_get_reg64(tcc, tmp, ctx);
  1270. emit(ARM_CMP_I(tc[0], hi), ctx);
  1271. _emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx);
  1272. _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
  1273. emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx);
  1274. emit(ARM_ADC_I(tc[0], tc[0], 0), ctx);
  1275. arm_bpf_put_reg64(tcc, tmp, ctx);
  1276. /* prog = array->ptrs[index]
  1277. * if (prog == NULL)
  1278. * goto out;
  1279. */
  1280. BUILD_BUG_ON(imm8m(offsetof(struct bpf_array, ptrs)) < 0);
  1281. off = imm8m(offsetof(struct bpf_array, ptrs));
  1282. emit(ARM_ADD_I(tmp[1], r_array, off), ctx);
  1283. emit(ARM_LDR_R_SI(tmp[1], tmp[1], r_index, SRTYPE_ASL, 2), ctx);
  1284. emit(ARM_CMP_I(tmp[1], 0), ctx);
  1285. _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
  1286. /* goto *(prog->bpf_func + prologue_size); */
  1287. BUILD_BUG_ON(offsetof(struct bpf_prog, bpf_func) >
  1288. ARM_INST_LDST__IMM12);
  1289. off = offsetof(struct bpf_prog, bpf_func);
  1290. emit(ARM_LDR_I(tmp[1], tmp[1], off), ctx);
  1291. emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
  1292. emit_bx_r(tmp[1], ctx);
  1293. /* out: */
  1294. if (out_offset == -1)
  1295. out_offset = cur_offset;
  1296. if (cur_offset != out_offset) {
  1297. pr_err_once("tail_call out_offset = %d, expected %d!\n",
  1298. cur_offset, out_offset);
  1299. return -1;
  1300. }
  1301. return 0;
  1302. #undef cur_offset
  1303. #undef jmp_offset
  1304. }
  1305. /* 0xabcd => 0xcdab */
  1306. static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
  1307. {
  1308. #if __LINUX_ARM_ARCH__ < 6
  1309. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1310. emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
  1311. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
  1312. emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
  1313. emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
  1314. #else /* ARMv6+ */
  1315. emit(ARM_REV16(rd, rn), ctx);
  1316. #endif
  1317. }
  1318. /* 0xabcdefgh => 0xghefcdab */
  1319. static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
  1320. {
  1321. #if __LINUX_ARM_ARCH__ < 6
  1322. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1323. emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
  1324. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
  1325. emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
  1326. emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
  1327. emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
  1328. emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
  1329. emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
  1330. emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
  1331. emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
  1332. emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
  1333. #else /* ARMv6+ */
  1334. emit(ARM_REV(rd, rn), ctx);
  1335. #endif
  1336. }
  1337. // push the scratch stack register on top of the stack
  1338. static inline void emit_push_r64(const s8 src[], struct jit_ctx *ctx)
  1339. {
  1340. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1341. const s8 *rt;
  1342. u16 reg_set = 0;
  1343. rt = arm_bpf_get_reg64(src, tmp2, ctx);
  1344. reg_set = (1 << rt[1]) | (1 << rt[0]);
  1345. emit(ARM_PUSH(reg_set), ctx);
  1346. }
  1347. static void build_prologue(struct jit_ctx *ctx)
  1348. {
  1349. const s8 arm_r0 = bpf2a32[BPF_REG_0][1];
  1350. const s8 *bpf_r1 = bpf2a32[BPF_REG_1];
  1351. const s8 *bpf_fp = bpf2a32[BPF_REG_FP];
  1352. const s8 *tcc = bpf2a32[TCALL_CNT];
  1353. /* Save callee saved registers. */
  1354. #ifdef CONFIG_FRAME_POINTER
  1355. u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC;
  1356. emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
  1357. emit(ARM_PUSH(reg_set), ctx);
  1358. emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
  1359. #else
  1360. emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx);
  1361. emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx);
  1362. #endif
  1363. /* mov r3, #0 */
  1364. /* sub r2, sp, #SCRATCH_SIZE */
  1365. emit(ARM_MOV_I(bpf_r1[0], 0), ctx);
  1366. emit(ARM_SUB_I(bpf_r1[1], ARM_SP, SCRATCH_SIZE), ctx);
  1367. ctx->stack_size = imm8m(STACK_SIZE);
  1368. /* Set up function call stack */
  1369. emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
  1370. /* Set up BPF prog stack base register */
  1371. emit_a32_mov_r64(true, bpf_fp, bpf_r1, ctx);
  1372. /* Initialize Tail Count */
  1373. emit(ARM_MOV_I(bpf_r1[1], 0), ctx);
  1374. emit_a32_mov_r64(true, tcc, bpf_r1, ctx);
  1375. /* Move BPF_CTX to BPF_R1 */
  1376. emit(ARM_MOV_R(bpf_r1[1], arm_r0), ctx);
  1377. /* end of prologue */
  1378. }
  1379. /* restore callee saved registers. */
  1380. static void build_epilogue(struct jit_ctx *ctx)
  1381. {
  1382. #ifdef CONFIG_FRAME_POINTER
  1383. /* When using frame pointers, some additional registers need to
  1384. * be loaded. */
  1385. u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP;
  1386. emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx);
  1387. emit(ARM_LDM(ARM_SP, reg_set), ctx);
  1388. #else
  1389. /* Restore callee saved registers. */
  1390. emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx);
  1391. emit(ARM_POP(CALLEE_POP_MASK), ctx);
  1392. #endif
  1393. }
  1394. /*
  1395. * Convert an eBPF instruction to native instruction, i.e
  1396. * JITs an eBPF instruction.
  1397. * Returns :
  1398. * 0 - Successfully JITed an 8-byte eBPF instruction
  1399. * >0 - Successfully JITed a 16-byte eBPF instruction
  1400. * <0 - Failed to JIT.
  1401. */
  1402. static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
  1403. {
  1404. const u8 code = insn->code;
  1405. const s8 *dst = bpf2a32[insn->dst_reg];
  1406. const s8 *src = bpf2a32[insn->src_reg];
  1407. const s8 *tmp = bpf2a32[TMP_REG_1];
  1408. const s8 *tmp2 = bpf2a32[TMP_REG_2];
  1409. const s16 off = insn->off;
  1410. const s32 imm = insn->imm;
  1411. const int i = insn - ctx->prog->insnsi;
  1412. const bool is64 = BPF_CLASS(code) == BPF_ALU64;
  1413. const s8 *rd, *rs;
  1414. s8 rd_lo, rt, rm, rn;
  1415. s32 jmp_offset;
  1416. #define check_imm(bits, imm) do { \
  1417. if ((imm) >= (1 << ((bits) - 1)) || \
  1418. (imm) < -(1 << ((bits) - 1))) { \
  1419. pr_info("[%2d] imm=%d(0x%x) out of range\n", \
  1420. i, imm, imm); \
  1421. return -EINVAL; \
  1422. } \
  1423. } while (0)
  1424. #define check_imm24(imm) check_imm(24, imm)
  1425. switch (code) {
  1426. /* ALU operations */
  1427. /* dst = src */
  1428. case BPF_ALU | BPF_MOV | BPF_K:
  1429. case BPF_ALU | BPF_MOV | BPF_X:
  1430. case BPF_ALU64 | BPF_MOV | BPF_K:
  1431. case BPF_ALU64 | BPF_MOV | BPF_X:
  1432. switch (BPF_SRC(code)) {
  1433. case BPF_X:
  1434. if (imm == 1) {
  1435. /* Special mov32 for zext */
  1436. emit_a32_mov_i(dst_hi, 0, ctx);
  1437. break;
  1438. }
  1439. if (insn->off)
  1440. emit_a32_movsx_r64(is64, insn->off, dst, src, ctx);
  1441. else
  1442. emit_a32_mov_r64(is64, dst, src, ctx);
  1443. break;
  1444. case BPF_K:
  1445. /* Sign-extend immediate value to destination reg */
  1446. emit_a32_mov_se_i64(is64, dst, imm, ctx);
  1447. break;
  1448. }
  1449. break;
  1450. /* dst = dst + src/imm */
  1451. /* dst = dst - src/imm */
  1452. /* dst = dst | src/imm */
  1453. /* dst = dst & src/imm */
  1454. /* dst = dst ^ src/imm */
  1455. /* dst = dst * src/imm */
  1456. /* dst = dst << src */
  1457. /* dst = dst >> src */
  1458. case BPF_ALU | BPF_ADD | BPF_K:
  1459. case BPF_ALU | BPF_ADD | BPF_X:
  1460. case BPF_ALU | BPF_SUB | BPF_K:
  1461. case BPF_ALU | BPF_SUB | BPF_X:
  1462. case BPF_ALU | BPF_OR | BPF_K:
  1463. case BPF_ALU | BPF_OR | BPF_X:
  1464. case BPF_ALU | BPF_AND | BPF_K:
  1465. case BPF_ALU | BPF_AND | BPF_X:
  1466. case BPF_ALU | BPF_XOR | BPF_K:
  1467. case BPF_ALU | BPF_XOR | BPF_X:
  1468. case BPF_ALU | BPF_MUL | BPF_K:
  1469. case BPF_ALU | BPF_MUL | BPF_X:
  1470. case BPF_ALU | BPF_LSH | BPF_X:
  1471. case BPF_ALU | BPF_RSH | BPF_X:
  1472. case BPF_ALU | BPF_ARSH | BPF_X:
  1473. case BPF_ALU64 | BPF_ADD | BPF_K:
  1474. case BPF_ALU64 | BPF_ADD | BPF_X:
  1475. case BPF_ALU64 | BPF_SUB | BPF_K:
  1476. case BPF_ALU64 | BPF_SUB | BPF_X:
  1477. case BPF_ALU64 | BPF_OR | BPF_K:
  1478. case BPF_ALU64 | BPF_OR | BPF_X:
  1479. case BPF_ALU64 | BPF_AND | BPF_K:
  1480. case BPF_ALU64 | BPF_AND | BPF_X:
  1481. case BPF_ALU64 | BPF_XOR | BPF_K:
  1482. case BPF_ALU64 | BPF_XOR | BPF_X:
  1483. switch (BPF_SRC(code)) {
  1484. case BPF_X:
  1485. emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code));
  1486. break;
  1487. case BPF_K:
  1488. /* Move immediate value to the temporary register
  1489. * and then do the ALU operation on the temporary
  1490. * register as this will sign-extend the immediate
  1491. * value into temporary reg and then it would be
  1492. * safe to do the operation on it.
  1493. */
  1494. emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
  1495. emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code));
  1496. break;
  1497. }
  1498. break;
  1499. /* dst = dst / src(imm) */
  1500. /* dst = dst % src(imm) */
  1501. case BPF_ALU | BPF_DIV | BPF_K:
  1502. case BPF_ALU | BPF_DIV | BPF_X:
  1503. case BPF_ALU | BPF_MOD | BPF_K:
  1504. case BPF_ALU | BPF_MOD | BPF_X:
  1505. rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx);
  1506. switch (BPF_SRC(code)) {
  1507. case BPF_X:
  1508. rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx);
  1509. break;
  1510. case BPF_K:
  1511. rt = tmp2[0];
  1512. emit_a32_mov_i(rt, imm, ctx);
  1513. break;
  1514. default:
  1515. rt = src_lo;
  1516. break;
  1517. }
  1518. emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code), off);
  1519. arm_bpf_put_reg32(dst_lo, rd_lo, ctx);
  1520. if (!ctx->prog->aux->verifier_zext)
  1521. emit_a32_mov_i(dst_hi, 0, ctx);
  1522. break;
  1523. case BPF_ALU64 | BPF_DIV | BPF_K:
  1524. case BPF_ALU64 | BPF_DIV | BPF_X:
  1525. case BPF_ALU64 | BPF_MOD | BPF_K:
  1526. case BPF_ALU64 | BPF_MOD | BPF_X:
  1527. rd = arm_bpf_get_reg64(dst, tmp2, ctx);
  1528. switch (BPF_SRC(code)) {
  1529. case BPF_X:
  1530. rs = arm_bpf_get_reg64(src, tmp, ctx);
  1531. break;
  1532. case BPF_K:
  1533. rs = tmp;
  1534. emit_a32_mov_se_i64(is64, rs, imm, ctx);
  1535. break;
  1536. }
  1537. emit_udivmod64(rd, rd, rs, ctx, BPF_OP(code), off);
  1538. arm_bpf_put_reg64(dst, rd, ctx);
  1539. break;
  1540. /* dst = dst << imm */
  1541. /* dst = dst >> imm */
  1542. /* dst = dst >> imm (signed) */
  1543. case BPF_ALU | BPF_LSH | BPF_K:
  1544. case BPF_ALU | BPF_RSH | BPF_K:
  1545. case BPF_ALU | BPF_ARSH | BPF_K:
  1546. if (unlikely(imm > 31))
  1547. return -EINVAL;
  1548. if (imm)
  1549. emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code));
  1550. if (!ctx->prog->aux->verifier_zext)
  1551. emit_a32_mov_i(dst_hi, 0, ctx);
  1552. break;
  1553. /* dst = dst << imm */
  1554. case BPF_ALU64 | BPF_LSH | BPF_K:
  1555. if (unlikely(imm > 63))
  1556. return -EINVAL;
  1557. emit_a32_lsh_i64(dst, imm, ctx);
  1558. break;
  1559. /* dst = dst >> imm */
  1560. case BPF_ALU64 | BPF_RSH | BPF_K:
  1561. if (unlikely(imm > 63))
  1562. return -EINVAL;
  1563. emit_a32_rsh_i64(dst, imm, ctx);
  1564. break;
  1565. /* dst = dst << src */
  1566. case BPF_ALU64 | BPF_LSH | BPF_X:
  1567. emit_a32_lsh_r64(dst, src, ctx);
  1568. break;
  1569. /* dst = dst >> src */
  1570. case BPF_ALU64 | BPF_RSH | BPF_X:
  1571. emit_a32_rsh_r64(dst, src, ctx);
  1572. break;
  1573. /* dst = dst >> src (signed) */
  1574. case BPF_ALU64 | BPF_ARSH | BPF_X:
  1575. emit_a32_arsh_r64(dst, src, ctx);
  1576. break;
  1577. /* dst = dst >> imm (signed) */
  1578. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1579. if (unlikely(imm > 63))
  1580. return -EINVAL;
  1581. emit_a32_arsh_i64(dst, imm, ctx);
  1582. break;
  1583. /* dst = ~dst */
  1584. case BPF_ALU | BPF_NEG:
  1585. emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code));
  1586. if (!ctx->prog->aux->verifier_zext)
  1587. emit_a32_mov_i(dst_hi, 0, ctx);
  1588. break;
  1589. /* dst = ~dst (64 bit) */
  1590. case BPF_ALU64 | BPF_NEG:
  1591. emit_a32_neg64(dst, ctx);
  1592. break;
  1593. /* dst = dst * src/imm */
  1594. case BPF_ALU64 | BPF_MUL | BPF_X:
  1595. case BPF_ALU64 | BPF_MUL | BPF_K:
  1596. switch (BPF_SRC(code)) {
  1597. case BPF_X:
  1598. emit_a32_mul_r64(dst, src, ctx);
  1599. break;
  1600. case BPF_K:
  1601. /* Move immediate value to the temporary register
  1602. * and then do the multiplication on it as this
  1603. * will sign-extend the immediate value into temp
  1604. * reg then it would be safe to do the operation
  1605. * on it.
  1606. */
  1607. emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
  1608. emit_a32_mul_r64(dst, tmp2, ctx);
  1609. break;
  1610. }
  1611. break;
  1612. /* dst = htole(dst) */
  1613. /* dst = htobe(dst) */
  1614. case BPF_ALU | BPF_END | BPF_FROM_LE: /* also BPF_TO_LE */
  1615. case BPF_ALU | BPF_END | BPF_FROM_BE: /* also BPF_TO_BE */
  1616. /* dst = bswap(dst) */
  1617. case BPF_ALU64 | BPF_END | BPF_FROM_LE: /* also BPF_TO_LE */
  1618. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  1619. if (BPF_SRC(code) == BPF_FROM_LE && BPF_CLASS(code) != BPF_ALU64)
  1620. goto emit_bswap_uxt;
  1621. switch (imm) {
  1622. case 16:
  1623. emit_rev16(rd[1], rd[1], ctx);
  1624. goto emit_bswap_uxt;
  1625. case 32:
  1626. emit_rev32(rd[1], rd[1], ctx);
  1627. goto emit_bswap_uxt;
  1628. case 64:
  1629. emit_rev32(ARM_LR, rd[1], ctx);
  1630. emit_rev32(rd[1], rd[0], ctx);
  1631. emit(ARM_MOV_R(rd[0], ARM_LR), ctx);
  1632. break;
  1633. }
  1634. goto exit;
  1635. emit_bswap_uxt:
  1636. switch (imm) {
  1637. case 16:
  1638. /* zero-extend 16 bits into 64 bits */
  1639. #if __LINUX_ARM_ARCH__ < 6
  1640. emit_a32_mov_i(tmp2[1], 0xffff, ctx);
  1641. emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx);
  1642. #else /* ARMv6+ */
  1643. emit(ARM_UXTH(rd[1], rd[1]), ctx);
  1644. #endif
  1645. if (!ctx->prog->aux->verifier_zext)
  1646. emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
  1647. break;
  1648. case 32:
  1649. /* zero-extend 32 bits into 64 bits */
  1650. if (!ctx->prog->aux->verifier_zext)
  1651. emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
  1652. break;
  1653. case 64:
  1654. /* nop */
  1655. break;
  1656. }
  1657. exit:
  1658. arm_bpf_put_reg64(dst, rd, ctx);
  1659. break;
  1660. /* dst = imm64 */
  1661. case BPF_LD | BPF_IMM | BPF_DW:
  1662. {
  1663. u64 val = (u32)imm | (u64)insn[1].imm << 32;
  1664. emit_a32_mov_i64(dst, val, ctx);
  1665. return 1;
  1666. }
  1667. /* LDX: dst = *(size *)(src + off) */
  1668. case BPF_LDX | BPF_MEM | BPF_W:
  1669. case BPF_LDX | BPF_MEM | BPF_H:
  1670. case BPF_LDX | BPF_MEM | BPF_B:
  1671. case BPF_LDX | BPF_MEM | BPF_DW:
  1672. /* LDSX: dst = *(signed size *)(src + off) */
  1673. case BPF_LDX | BPF_MEMSX | BPF_B:
  1674. case BPF_LDX | BPF_MEMSX | BPF_H:
  1675. case BPF_LDX | BPF_MEMSX | BPF_W:
  1676. rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  1677. if (BPF_MODE(insn->code) == BPF_MEMSX)
  1678. emit_ldsx_r(dst, rn, off, ctx, BPF_SIZE(code));
  1679. else
  1680. emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code));
  1681. break;
  1682. /* speculation barrier */
  1683. case BPF_ST | BPF_NOSPEC:
  1684. break;
  1685. /* ST: *(size *)(dst + off) = imm */
  1686. case BPF_ST | BPF_MEM | BPF_W:
  1687. case BPF_ST | BPF_MEM | BPF_H:
  1688. case BPF_ST | BPF_MEM | BPF_B:
  1689. case BPF_ST | BPF_MEM | BPF_DW:
  1690. switch (BPF_SIZE(code)) {
  1691. case BPF_DW:
  1692. /* Sign-extend immediate value into temp reg */
  1693. emit_a32_mov_se_i64(true, tmp2, imm, ctx);
  1694. break;
  1695. case BPF_W:
  1696. case BPF_H:
  1697. case BPF_B:
  1698. emit_a32_mov_i(tmp2[1], imm, ctx);
  1699. break;
  1700. }
  1701. emit_str_r(dst_lo, tmp2, off, ctx, BPF_SIZE(code));
  1702. break;
  1703. /* Atomic ops */
  1704. case BPF_STX | BPF_ATOMIC | BPF_W:
  1705. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1706. goto notyet;
  1707. /* STX: *(size *)(dst + off) = src */
  1708. case BPF_STX | BPF_MEM | BPF_W:
  1709. case BPF_STX | BPF_MEM | BPF_H:
  1710. case BPF_STX | BPF_MEM | BPF_B:
  1711. case BPF_STX | BPF_MEM | BPF_DW:
  1712. rs = arm_bpf_get_reg64(src, tmp2, ctx);
  1713. emit_str_r(dst_lo, rs, off, ctx, BPF_SIZE(code));
  1714. break;
  1715. /* PC += off if dst == src */
  1716. /* PC += off if dst > src */
  1717. /* PC += off if dst >= src */
  1718. /* PC += off if dst < src */
  1719. /* PC += off if dst <= src */
  1720. /* PC += off if dst != src */
  1721. /* PC += off if dst > src (signed) */
  1722. /* PC += off if dst >= src (signed) */
  1723. /* PC += off if dst < src (signed) */
  1724. /* PC += off if dst <= src (signed) */
  1725. /* PC += off if dst & src */
  1726. case BPF_JMP | BPF_JEQ | BPF_X:
  1727. case BPF_JMP | BPF_JGT | BPF_X:
  1728. case BPF_JMP | BPF_JGE | BPF_X:
  1729. case BPF_JMP | BPF_JNE | BPF_X:
  1730. case BPF_JMP | BPF_JSGT | BPF_X:
  1731. case BPF_JMP | BPF_JSGE | BPF_X:
  1732. case BPF_JMP | BPF_JSET | BPF_X:
  1733. case BPF_JMP | BPF_JLE | BPF_X:
  1734. case BPF_JMP | BPF_JLT | BPF_X:
  1735. case BPF_JMP | BPF_JSLT | BPF_X:
  1736. case BPF_JMP | BPF_JSLE | BPF_X:
  1737. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1738. case BPF_JMP32 | BPF_JGT | BPF_X:
  1739. case BPF_JMP32 | BPF_JGE | BPF_X:
  1740. case BPF_JMP32 | BPF_JNE | BPF_X:
  1741. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1742. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1743. case BPF_JMP32 | BPF_JSET | BPF_X:
  1744. case BPF_JMP32 | BPF_JLE | BPF_X:
  1745. case BPF_JMP32 | BPF_JLT | BPF_X:
  1746. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1747. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1748. /* Setup source registers */
  1749. rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx);
  1750. rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
  1751. goto go_jmp;
  1752. /* PC += off if dst == imm */
  1753. /* PC += off if dst > imm */
  1754. /* PC += off if dst >= imm */
  1755. /* PC += off if dst < imm */
  1756. /* PC += off if dst <= imm */
  1757. /* PC += off if dst != imm */
  1758. /* PC += off if dst > imm (signed) */
  1759. /* PC += off if dst >= imm (signed) */
  1760. /* PC += off if dst < imm (signed) */
  1761. /* PC += off if dst <= imm (signed) */
  1762. /* PC += off if dst & imm */
  1763. case BPF_JMP | BPF_JEQ | BPF_K:
  1764. case BPF_JMP | BPF_JGT | BPF_K:
  1765. case BPF_JMP | BPF_JGE | BPF_K:
  1766. case BPF_JMP | BPF_JNE | BPF_K:
  1767. case BPF_JMP | BPF_JSGT | BPF_K:
  1768. case BPF_JMP | BPF_JSGE | BPF_K:
  1769. case BPF_JMP | BPF_JSET | BPF_K:
  1770. case BPF_JMP | BPF_JLT | BPF_K:
  1771. case BPF_JMP | BPF_JLE | BPF_K:
  1772. case BPF_JMP | BPF_JSLT | BPF_K:
  1773. case BPF_JMP | BPF_JSLE | BPF_K:
  1774. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1775. case BPF_JMP32 | BPF_JGT | BPF_K:
  1776. case BPF_JMP32 | BPF_JGE | BPF_K:
  1777. case BPF_JMP32 | BPF_JNE | BPF_K:
  1778. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1779. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1780. case BPF_JMP32 | BPF_JSET | BPF_K:
  1781. case BPF_JMP32 | BPF_JLT | BPF_K:
  1782. case BPF_JMP32 | BPF_JLE | BPF_K:
  1783. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1784. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1785. if (off == 0)
  1786. break;
  1787. rm = tmp2[0];
  1788. rn = tmp2[1];
  1789. /* Sign-extend immediate value */
  1790. emit_a32_mov_se_i64(true, tmp2, imm, ctx);
  1791. go_jmp:
  1792. /* Setup destination register */
  1793. rd = arm_bpf_get_reg64(dst, tmp, ctx);
  1794. /* Check for the condition */
  1795. emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code),
  1796. BPF_CLASS(code) == BPF_JMP);
  1797. /* Setup JUMP instruction */
  1798. jmp_offset = bpf2a32_offset(i+off, i, ctx);
  1799. switch (BPF_OP(code)) {
  1800. case BPF_JNE:
  1801. case BPF_JSET:
  1802. _emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
  1803. break;
  1804. case BPF_JEQ:
  1805. _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
  1806. break;
  1807. case BPF_JGT:
  1808. _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
  1809. break;
  1810. case BPF_JGE:
  1811. _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
  1812. break;
  1813. case BPF_JSGT:
  1814. _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
  1815. break;
  1816. case BPF_JSGE:
  1817. _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
  1818. break;
  1819. case BPF_JLE:
  1820. _emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
  1821. break;
  1822. case BPF_JLT:
  1823. _emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
  1824. break;
  1825. case BPF_JSLT:
  1826. _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
  1827. break;
  1828. case BPF_JSLE:
  1829. _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
  1830. break;
  1831. }
  1832. break;
  1833. /* JMP OFF */
  1834. case BPF_JMP | BPF_JA:
  1835. case BPF_JMP32 | BPF_JA:
  1836. {
  1837. if (BPF_CLASS(code) == BPF_JMP32 && imm != 0)
  1838. jmp_offset = bpf2a32_offset(i + imm, i, ctx);
  1839. else if (BPF_CLASS(code) == BPF_JMP && off != 0)
  1840. jmp_offset = bpf2a32_offset(i + off, i, ctx);
  1841. else
  1842. break;
  1843. check_imm24(jmp_offset);
  1844. emit(ARM_B(jmp_offset), ctx);
  1845. break;
  1846. }
  1847. /* tail call */
  1848. case BPF_JMP | BPF_TAIL_CALL:
  1849. if (emit_bpf_tail_call(ctx))
  1850. return -EFAULT;
  1851. break;
  1852. /* function call */
  1853. case BPF_JMP | BPF_CALL:
  1854. {
  1855. const s8 *r0 = bpf2a32[BPF_REG_0];
  1856. const s8 *r1 = bpf2a32[BPF_REG_1];
  1857. const s8 *r2 = bpf2a32[BPF_REG_2];
  1858. const s8 *r3 = bpf2a32[BPF_REG_3];
  1859. const s8 *r4 = bpf2a32[BPF_REG_4];
  1860. const s8 *r5 = bpf2a32[BPF_REG_5];
  1861. const u32 func = (u32)__bpf_call_base + (u32)imm;
  1862. emit_a32_mov_r64(true, r0, r1, ctx);
  1863. emit_a32_mov_r64(true, r1, r2, ctx);
  1864. emit_push_r64(r5, ctx);
  1865. emit_push_r64(r4, ctx);
  1866. emit_push_r64(r3, ctx);
  1867. emit_a32_mov_i(tmp[1], func, ctx);
  1868. emit_blx_r(tmp[1], ctx);
  1869. emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
  1870. break;
  1871. }
  1872. /* function return */
  1873. case BPF_JMP | BPF_EXIT:
  1874. /* Optimization: when last instruction is EXIT
  1875. * simply fallthrough to epilogue.
  1876. */
  1877. if (i == ctx->prog->len - 1)
  1878. break;
  1879. jmp_offset = epilogue_offset(ctx);
  1880. check_imm24(jmp_offset);
  1881. emit(ARM_B(jmp_offset), ctx);
  1882. break;
  1883. notyet:
  1884. pr_info_once("*** NOT YET: opcode %02x ***\n", code);
  1885. return -EFAULT;
  1886. default:
  1887. pr_err_once("unknown opcode %02x\n", code);
  1888. return -EINVAL;
  1889. }
  1890. if (ctx->flags & FLAG_IMM_OVERFLOW)
  1891. /*
  1892. * this instruction generated an overflow when
  1893. * trying to access the literal pool, so
  1894. * delegate this filter to the kernel interpreter.
  1895. */
  1896. return -1;
  1897. return 0;
  1898. }
  1899. static int build_body(struct jit_ctx *ctx)
  1900. {
  1901. const struct bpf_prog *prog = ctx->prog;
  1902. unsigned int i;
  1903. for (i = 0; i < prog->len; i++) {
  1904. const struct bpf_insn *insn = &(prog->insnsi[i]);
  1905. int ret;
  1906. ret = build_insn(insn, ctx);
  1907. /* It's used with loading the 64 bit immediate value. */
  1908. if (ret > 0) {
  1909. i++;
  1910. if (ctx->target == NULL)
  1911. ctx->offsets[i] = ctx->idx;
  1912. continue;
  1913. }
  1914. if (ctx->target == NULL)
  1915. ctx->offsets[i] = ctx->idx;
  1916. /* If unsuccesful, return with error code */
  1917. if (ret)
  1918. return ret;
  1919. }
  1920. return 0;
  1921. }
  1922. static int validate_code(struct jit_ctx *ctx)
  1923. {
  1924. int i;
  1925. for (i = 0; i < ctx->idx; i++) {
  1926. if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
  1927. return -1;
  1928. }
  1929. return 0;
  1930. }
  1931. bool bpf_jit_needs_zext(void)
  1932. {
  1933. return true;
  1934. }
  1935. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1936. {
  1937. struct bpf_prog *tmp, *orig_prog = prog;
  1938. struct bpf_binary_header *header;
  1939. bool tmp_blinded = false;
  1940. struct jit_ctx ctx;
  1941. unsigned int tmp_idx;
  1942. unsigned int image_size;
  1943. u8 *image_ptr;
  1944. /* If BPF JIT was not enabled then we must fall back to
  1945. * the interpreter.
  1946. */
  1947. if (!prog->jit_requested)
  1948. return orig_prog;
  1949. /* If constant blinding was enabled and we failed during blinding
  1950. * then we must fall back to the interpreter. Otherwise, we save
  1951. * the new JITed code.
  1952. */
  1953. tmp = bpf_jit_blind_constants(prog);
  1954. if (IS_ERR(tmp))
  1955. return orig_prog;
  1956. if (tmp != prog) {
  1957. tmp_blinded = true;
  1958. prog = tmp;
  1959. }
  1960. memset(&ctx, 0, sizeof(ctx));
  1961. ctx.prog = prog;
  1962. ctx.cpu_architecture = cpu_architecture();
  1963. /* Not able to allocate memory for offsets[] , then
  1964. * we must fall back to the interpreter
  1965. */
  1966. ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
  1967. if (ctx.offsets == NULL) {
  1968. prog = orig_prog;
  1969. goto out;
  1970. }
  1971. /* 1) fake pass to find in the length of the JITed code,
  1972. * to compute ctx->offsets and other context variables
  1973. * needed to compute final JITed code.
  1974. * Also, calculate random starting pointer/start of JITed code
  1975. * which is prefixed by random number of fault instructions.
  1976. *
  1977. * If the first pass fails then there is no chance of it
  1978. * being successful in the second pass, so just fall back
  1979. * to the interpreter.
  1980. */
  1981. if (build_body(&ctx)) {
  1982. prog = orig_prog;
  1983. goto out_off;
  1984. }
  1985. tmp_idx = ctx.idx;
  1986. build_prologue(&ctx);
  1987. ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
  1988. ctx.epilogue_offset = ctx.idx;
  1989. #if __LINUX_ARM_ARCH__ < 7
  1990. tmp_idx = ctx.idx;
  1991. build_epilogue(&ctx);
  1992. ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
  1993. ctx.idx += ctx.imm_count;
  1994. if (ctx.imm_count) {
  1995. ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
  1996. if (ctx.imms == NULL) {
  1997. prog = orig_prog;
  1998. goto out_off;
  1999. }
  2000. }
  2001. #else
  2002. /* there's nothing about the epilogue on ARMv7 */
  2003. build_epilogue(&ctx);
  2004. #endif
  2005. /* Now we can get the actual image size of the JITed arm code.
  2006. * Currently, we are not considering the THUMB-2 instructions
  2007. * for jit, although it can decrease the size of the image.
  2008. *
  2009. * As each arm instruction is of length 32bit, we are translating
  2010. * number of JITed instructions into the size required to store these
  2011. * JITed code.
  2012. */
  2013. image_size = sizeof(u32) * ctx.idx;
  2014. /* Now we know the size of the structure to make */
  2015. header = bpf_jit_binary_alloc(image_size, &image_ptr,
  2016. sizeof(u32), jit_fill_hole);
  2017. /* Not able to allocate memory for the structure then
  2018. * we must fall back to the interpretation
  2019. */
  2020. if (header == NULL) {
  2021. prog = orig_prog;
  2022. goto out_imms;
  2023. }
  2024. /* 2.) Actual pass to generate final JIT code */
  2025. ctx.target = (u32 *) image_ptr;
  2026. ctx.idx = 0;
  2027. build_prologue(&ctx);
  2028. /* If building the body of the JITed code fails somehow,
  2029. * we fall back to the interpretation.
  2030. */
  2031. if (build_body(&ctx) < 0)
  2032. goto out_free;
  2033. build_epilogue(&ctx);
  2034. /* 3.) Extra pass to validate JITed Code */
  2035. if (validate_code(&ctx))
  2036. goto out_free;
  2037. flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
  2038. if (bpf_jit_enable > 1)
  2039. /* there are 2 passes here */
  2040. bpf_jit_dump(prog->len, image_size, 2, ctx.target);
  2041. if (bpf_jit_binary_lock_ro(header))
  2042. goto out_free;
  2043. prog->bpf_func = (void *)ctx.target;
  2044. prog->jited = 1;
  2045. prog->jited_len = image_size;
  2046. out_imms:
  2047. #if __LINUX_ARM_ARCH__ < 7
  2048. if (ctx.imm_count)
  2049. kfree(ctx.imms);
  2050. #endif
  2051. out_off:
  2052. kfree(ctx.offsets);
  2053. out:
  2054. if (tmp_blinded)
  2055. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  2056. tmp : orig_prog);
  2057. return prog;
  2058. out_free:
  2059. image_ptr = NULL;
  2060. bpf_jit_binary_free(header);
  2061. prog = orig_prog;
  2062. goto out_imms;
  2063. }