ebpf_jit.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. /*
  2. * Just-In-Time compiler for eBPF filters on MIPS
  3. *
  4. * Copyright (c) 2017 Cavium, Inc.
  5. *
  6. * Based on code from:
  7. *
  8. * Copyright (c) 2014 Imagination Technologies Ltd.
  9. * Author: Markos Chandras <markos.chandras@imgtec.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; version 2 of the License.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/errno.h>
  17. #include <linux/filter.h>
  18. #include <linux/bpf.h>
  19. #include <linux/slab.h>
  20. #include <asm/bitops.h>
  21. #include <asm/byteorder.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/cpu-features.h>
  24. #include <asm/uasm.h>
  25. /* Registers used by JIT */
  26. #define MIPS_R_ZERO 0
  27. #define MIPS_R_AT 1
  28. #define MIPS_R_V0 2 /* BPF_R0 */
  29. #define MIPS_R_V1 3
  30. #define MIPS_R_A0 4 /* BPF_R1 */
  31. #define MIPS_R_A1 5 /* BPF_R2 */
  32. #define MIPS_R_A2 6 /* BPF_R3 */
  33. #define MIPS_R_A3 7 /* BPF_R4 */
  34. #define MIPS_R_A4 8 /* BPF_R5 */
  35. #define MIPS_R_T4 12 /* BPF_AX */
  36. #define MIPS_R_T5 13
  37. #define MIPS_R_T6 14
  38. #define MIPS_R_T7 15
  39. #define MIPS_R_S0 16 /* BPF_R6 */
  40. #define MIPS_R_S1 17 /* BPF_R7 */
  41. #define MIPS_R_S2 18 /* BPF_R8 */
  42. #define MIPS_R_S3 19 /* BPF_R9 */
  43. #define MIPS_R_S4 20 /* BPF_TCC */
  44. #define MIPS_R_S5 21
  45. #define MIPS_R_S6 22
  46. #define MIPS_R_S7 23
  47. #define MIPS_R_T8 24
  48. #define MIPS_R_T9 25
  49. #define MIPS_R_SP 29
  50. #define MIPS_R_RA 31
  51. /* eBPF flags */
  52. #define EBPF_SAVE_S0 BIT(0)
  53. #define EBPF_SAVE_S1 BIT(1)
  54. #define EBPF_SAVE_S2 BIT(2)
  55. #define EBPF_SAVE_S3 BIT(3)
  56. #define EBPF_SAVE_S4 BIT(4)
  57. #define EBPF_SAVE_RA BIT(5)
  58. #define EBPF_SEEN_FP BIT(6)
  59. #define EBPF_SEEN_TC BIT(7)
  60. #define EBPF_TCC_IN_V1 BIT(8)
  61. /*
  62. * For the mips64 ISA, we need to track the value range or type for
  63. * each JIT register. The BPF machine requires zero extended 32-bit
  64. * values, but the mips64 ISA requires sign extended 32-bit values.
  65. * At each point in the BPF program we track the state of every
  66. * register so that we can zero extend or sign extend as the BPF
  67. * semantics require.
  68. */
  69. enum reg_val_type {
  70. /* uninitialized */
  71. REG_UNKNOWN,
  72. /* not known to be 32-bit compatible. */
  73. REG_64BIT,
  74. /* 32-bit compatible, no truncation needed for 64-bit ops. */
  75. REG_64BIT_32BIT,
  76. /* 32-bit compatible, need truncation for 64-bit ops. */
  77. REG_32BIT,
  78. /* 32-bit zero extended. */
  79. REG_32BIT_ZERO_EX,
  80. /* 32-bit no sign/zero extension needed. */
  81. REG_32BIT_POS
  82. };
  83. /*
  84. * high bit of offsets indicates if long branch conversion done at
  85. * this insn.
  86. */
  87. #define OFFSETS_B_CONV BIT(31)
  88. /**
  89. * struct jit_ctx - JIT context
  90. * @skf: The sk_filter
  91. * @stack_size: eBPF stack size
  92. * @idx: Instruction index
  93. * @flags: JIT flags
  94. * @offsets: Instruction offsets
  95. * @target: Memory location for the compiled filter
  96. * @reg_val_types Packed enum reg_val_type for each register.
  97. */
  98. struct jit_ctx {
  99. const struct bpf_prog *skf;
  100. int stack_size;
  101. u32 idx;
  102. u32 flags;
  103. u32 *offsets;
  104. u32 *target;
  105. u64 *reg_val_types;
  106. unsigned int long_b_conversion:1;
  107. unsigned int gen_b_offsets:1;
  108. unsigned int use_bbit_insns:1;
  109. };
  110. static void set_reg_val_type(u64 *rvt, int reg, enum reg_val_type type)
  111. {
  112. *rvt &= ~(7ull << (reg * 3));
  113. *rvt |= ((u64)type << (reg * 3));
  114. }
  115. static enum reg_val_type get_reg_val_type(const struct jit_ctx *ctx,
  116. int index, int reg)
  117. {
  118. return (ctx->reg_val_types[index] >> (reg * 3)) & 7;
  119. }
  120. /* Simply emit the instruction if the JIT memory space has been allocated */
  121. #define emit_instr(ctx, func, ...) \
  122. do { \
  123. if ((ctx)->target != NULL) { \
  124. u32 *p = &(ctx)->target[ctx->idx]; \
  125. uasm_i_##func(&p, ##__VA_ARGS__); \
  126. } \
  127. (ctx)->idx++; \
  128. } while (0)
  129. static unsigned int j_target(struct jit_ctx *ctx, int target_idx)
  130. {
  131. unsigned long target_va, base_va;
  132. unsigned int r;
  133. if (!ctx->target)
  134. return 0;
  135. base_va = (unsigned long)ctx->target;
  136. target_va = base_va + (ctx->offsets[target_idx] & ~OFFSETS_B_CONV);
  137. if ((base_va & ~0x0ffffffful) != (target_va & ~0x0ffffffful))
  138. return (unsigned int)-1;
  139. r = target_va & 0x0ffffffful;
  140. return r;
  141. }
  142. /* Compute the immediate value for PC-relative branches. */
  143. static u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
  144. {
  145. if (!ctx->gen_b_offsets)
  146. return 0;
  147. /*
  148. * We want a pc-relative branch. tgt is the instruction offset
  149. * we want to jump to.
  150. * Branch on MIPS:
  151. * I: target_offset <- sign_extend(offset)
  152. * I+1: PC += target_offset (delay slot)
  153. *
  154. * ctx->idx currently points to the branch instruction
  155. * but the offset is added to the delay slot so we need
  156. * to subtract 4.
  157. */
  158. return (ctx->offsets[tgt] & ~OFFSETS_B_CONV) -
  159. (ctx->idx * 4) - 4;
  160. }
  161. enum which_ebpf_reg {
  162. src_reg,
  163. src_reg_no_fp,
  164. dst_reg,
  165. dst_reg_fp_ok
  166. };
  167. /*
  168. * For eBPF, the register mapping naturally falls out of the
  169. * requirements of eBPF and the MIPS n64 ABI. We don't maintain a
  170. * separate frame pointer, so BPF_REG_10 relative accesses are
  171. * adjusted to be $sp relative.
  172. */
  173. int ebpf_to_mips_reg(struct jit_ctx *ctx, const struct bpf_insn *insn,
  174. enum which_ebpf_reg w)
  175. {
  176. int ebpf_reg = (w == src_reg || w == src_reg_no_fp) ?
  177. insn->src_reg : insn->dst_reg;
  178. switch (ebpf_reg) {
  179. case BPF_REG_0:
  180. return MIPS_R_V0;
  181. case BPF_REG_1:
  182. return MIPS_R_A0;
  183. case BPF_REG_2:
  184. return MIPS_R_A1;
  185. case BPF_REG_3:
  186. return MIPS_R_A2;
  187. case BPF_REG_4:
  188. return MIPS_R_A3;
  189. case BPF_REG_5:
  190. return MIPS_R_A4;
  191. case BPF_REG_6:
  192. ctx->flags |= EBPF_SAVE_S0;
  193. return MIPS_R_S0;
  194. case BPF_REG_7:
  195. ctx->flags |= EBPF_SAVE_S1;
  196. return MIPS_R_S1;
  197. case BPF_REG_8:
  198. ctx->flags |= EBPF_SAVE_S2;
  199. return MIPS_R_S2;
  200. case BPF_REG_9:
  201. ctx->flags |= EBPF_SAVE_S3;
  202. return MIPS_R_S3;
  203. case BPF_REG_10:
  204. if (w == dst_reg || w == src_reg_no_fp)
  205. goto bad_reg;
  206. ctx->flags |= EBPF_SEEN_FP;
  207. /*
  208. * Needs special handling, return something that
  209. * cannot be clobbered just in case.
  210. */
  211. return MIPS_R_ZERO;
  212. case BPF_REG_AX:
  213. return MIPS_R_T4;
  214. default:
  215. bad_reg:
  216. WARN(1, "Illegal bpf reg: %d\n", ebpf_reg);
  217. return -EINVAL;
  218. }
  219. }
  220. /*
  221. * eBPF stack frame will be something like:
  222. *
  223. * Entry $sp ------> +--------------------------------+
  224. * | $ra (optional) |
  225. * +--------------------------------+
  226. * | $s0 (optional) |
  227. * +--------------------------------+
  228. * | $s1 (optional) |
  229. * +--------------------------------+
  230. * | $s2 (optional) |
  231. * +--------------------------------+
  232. * | $s3 (optional) |
  233. * +--------------------------------+
  234. * | $s4 (optional) |
  235. * +--------------------------------+
  236. * | tmp-storage (if $ra saved) |
  237. * $sp + tmp_offset --> +--------------------------------+ <--BPF_REG_10
  238. * | BPF_REG_10 relative storage |
  239. * | MAX_BPF_STACK (optional) |
  240. * | . |
  241. * | . |
  242. * | . |
  243. * $sp --------> +--------------------------------+
  244. *
  245. * If BPF_REG_10 is never referenced, then the MAX_BPF_STACK sized
  246. * area is not allocated.
  247. */
  248. static int gen_int_prologue(struct jit_ctx *ctx)
  249. {
  250. int stack_adjust = 0;
  251. int store_offset;
  252. int locals_size;
  253. if (ctx->flags & EBPF_SAVE_RA)
  254. /*
  255. * If RA we are doing a function call and may need
  256. * extra 8-byte tmp area.
  257. */
  258. stack_adjust += 16;
  259. if (ctx->flags & EBPF_SAVE_S0)
  260. stack_adjust += 8;
  261. if (ctx->flags & EBPF_SAVE_S1)
  262. stack_adjust += 8;
  263. if (ctx->flags & EBPF_SAVE_S2)
  264. stack_adjust += 8;
  265. if (ctx->flags & EBPF_SAVE_S3)
  266. stack_adjust += 8;
  267. if (ctx->flags & EBPF_SAVE_S4)
  268. stack_adjust += 8;
  269. BUILD_BUG_ON(MAX_BPF_STACK & 7);
  270. locals_size = (ctx->flags & EBPF_SEEN_FP) ? MAX_BPF_STACK : 0;
  271. stack_adjust += locals_size;
  272. ctx->stack_size = stack_adjust;
  273. /*
  274. * First instruction initializes the tail call count (TCC).
  275. * On tail call we skip this instruction, and the TCC is
  276. * passed in $v1 from the caller.
  277. */
  278. emit_instr(ctx, daddiu, MIPS_R_V1, MIPS_R_ZERO, MAX_TAIL_CALL_CNT);
  279. if (stack_adjust)
  280. emit_instr(ctx, daddiu, MIPS_R_SP, MIPS_R_SP, -stack_adjust);
  281. else
  282. return 0;
  283. store_offset = stack_adjust - 8;
  284. if (ctx->flags & EBPF_SAVE_RA) {
  285. emit_instr(ctx, sd, MIPS_R_RA, store_offset, MIPS_R_SP);
  286. store_offset -= 8;
  287. }
  288. if (ctx->flags & EBPF_SAVE_S0) {
  289. emit_instr(ctx, sd, MIPS_R_S0, store_offset, MIPS_R_SP);
  290. store_offset -= 8;
  291. }
  292. if (ctx->flags & EBPF_SAVE_S1) {
  293. emit_instr(ctx, sd, MIPS_R_S1, store_offset, MIPS_R_SP);
  294. store_offset -= 8;
  295. }
  296. if (ctx->flags & EBPF_SAVE_S2) {
  297. emit_instr(ctx, sd, MIPS_R_S2, store_offset, MIPS_R_SP);
  298. store_offset -= 8;
  299. }
  300. if (ctx->flags & EBPF_SAVE_S3) {
  301. emit_instr(ctx, sd, MIPS_R_S3, store_offset, MIPS_R_SP);
  302. store_offset -= 8;
  303. }
  304. if (ctx->flags & EBPF_SAVE_S4) {
  305. emit_instr(ctx, sd, MIPS_R_S4, store_offset, MIPS_R_SP);
  306. store_offset -= 8;
  307. }
  308. if ((ctx->flags & EBPF_SEEN_TC) && !(ctx->flags & EBPF_TCC_IN_V1))
  309. emit_instr(ctx, daddu, MIPS_R_S4, MIPS_R_V1, MIPS_R_ZERO);
  310. return 0;
  311. }
  312. static int build_int_epilogue(struct jit_ctx *ctx, int dest_reg)
  313. {
  314. const struct bpf_prog *prog = ctx->skf;
  315. int stack_adjust = ctx->stack_size;
  316. int store_offset = stack_adjust - 8;
  317. enum reg_val_type td;
  318. int r0 = MIPS_R_V0;
  319. if (dest_reg == MIPS_R_RA) {
  320. /* Don't let zero extended value escape. */
  321. td = get_reg_val_type(ctx, prog->len, BPF_REG_0);
  322. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX)
  323. emit_instr(ctx, sll, r0, r0, 0);
  324. }
  325. if (ctx->flags & EBPF_SAVE_RA) {
  326. emit_instr(ctx, ld, MIPS_R_RA, store_offset, MIPS_R_SP);
  327. store_offset -= 8;
  328. }
  329. if (ctx->flags & EBPF_SAVE_S0) {
  330. emit_instr(ctx, ld, MIPS_R_S0, store_offset, MIPS_R_SP);
  331. store_offset -= 8;
  332. }
  333. if (ctx->flags & EBPF_SAVE_S1) {
  334. emit_instr(ctx, ld, MIPS_R_S1, store_offset, MIPS_R_SP);
  335. store_offset -= 8;
  336. }
  337. if (ctx->flags & EBPF_SAVE_S2) {
  338. emit_instr(ctx, ld, MIPS_R_S2, store_offset, MIPS_R_SP);
  339. store_offset -= 8;
  340. }
  341. if (ctx->flags & EBPF_SAVE_S3) {
  342. emit_instr(ctx, ld, MIPS_R_S3, store_offset, MIPS_R_SP);
  343. store_offset -= 8;
  344. }
  345. if (ctx->flags & EBPF_SAVE_S4) {
  346. emit_instr(ctx, ld, MIPS_R_S4, store_offset, MIPS_R_SP);
  347. store_offset -= 8;
  348. }
  349. emit_instr(ctx, jr, dest_reg);
  350. if (stack_adjust)
  351. emit_instr(ctx, daddiu, MIPS_R_SP, MIPS_R_SP, stack_adjust);
  352. else
  353. emit_instr(ctx, nop);
  354. return 0;
  355. }
  356. static void gen_imm_to_reg(const struct bpf_insn *insn, int reg,
  357. struct jit_ctx *ctx)
  358. {
  359. if (insn->imm >= S16_MIN && insn->imm <= S16_MAX) {
  360. emit_instr(ctx, addiu, reg, MIPS_R_ZERO, insn->imm);
  361. } else {
  362. int lower = (s16)(insn->imm & 0xffff);
  363. int upper = insn->imm - lower;
  364. emit_instr(ctx, lui, reg, upper >> 16);
  365. emit_instr(ctx, addiu, reg, reg, lower);
  366. }
  367. }
  368. static int gen_imm_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  369. int idx)
  370. {
  371. int upper_bound, lower_bound;
  372. int dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  373. if (dst < 0)
  374. return dst;
  375. switch (BPF_OP(insn->code)) {
  376. case BPF_MOV:
  377. case BPF_ADD:
  378. upper_bound = S16_MAX;
  379. lower_bound = S16_MIN;
  380. break;
  381. case BPF_SUB:
  382. upper_bound = -(int)S16_MIN;
  383. lower_bound = -(int)S16_MAX;
  384. break;
  385. case BPF_AND:
  386. case BPF_OR:
  387. case BPF_XOR:
  388. upper_bound = 0xffff;
  389. lower_bound = 0;
  390. break;
  391. case BPF_RSH:
  392. case BPF_LSH:
  393. case BPF_ARSH:
  394. /* Shift amounts are truncated, no need for bounds */
  395. upper_bound = S32_MAX;
  396. lower_bound = S32_MIN;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. /*
  402. * Immediate move clobbers the register, so no sign/zero
  403. * extension needed.
  404. */
  405. if (BPF_CLASS(insn->code) == BPF_ALU64 &&
  406. BPF_OP(insn->code) != BPF_MOV &&
  407. get_reg_val_type(ctx, idx, insn->dst_reg) == REG_32BIT)
  408. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  409. /* BPF_ALU | BPF_LSH doesn't need separate sign extension */
  410. if (BPF_CLASS(insn->code) == BPF_ALU &&
  411. BPF_OP(insn->code) != BPF_LSH &&
  412. BPF_OP(insn->code) != BPF_MOV &&
  413. get_reg_val_type(ctx, idx, insn->dst_reg) != REG_32BIT)
  414. emit_instr(ctx, sll, dst, dst, 0);
  415. if (insn->imm >= lower_bound && insn->imm <= upper_bound) {
  416. /* single insn immediate case */
  417. switch (BPF_OP(insn->code) | BPF_CLASS(insn->code)) {
  418. case BPF_ALU64 | BPF_MOV:
  419. emit_instr(ctx, daddiu, dst, MIPS_R_ZERO, insn->imm);
  420. break;
  421. case BPF_ALU64 | BPF_AND:
  422. case BPF_ALU | BPF_AND:
  423. emit_instr(ctx, andi, dst, dst, insn->imm);
  424. break;
  425. case BPF_ALU64 | BPF_OR:
  426. case BPF_ALU | BPF_OR:
  427. emit_instr(ctx, ori, dst, dst, insn->imm);
  428. break;
  429. case BPF_ALU64 | BPF_XOR:
  430. case BPF_ALU | BPF_XOR:
  431. emit_instr(ctx, xori, dst, dst, insn->imm);
  432. break;
  433. case BPF_ALU64 | BPF_ADD:
  434. emit_instr(ctx, daddiu, dst, dst, insn->imm);
  435. break;
  436. case BPF_ALU64 | BPF_SUB:
  437. emit_instr(ctx, daddiu, dst, dst, -insn->imm);
  438. break;
  439. case BPF_ALU64 | BPF_RSH:
  440. emit_instr(ctx, dsrl_safe, dst, dst, insn->imm & 0x3f);
  441. break;
  442. case BPF_ALU | BPF_RSH:
  443. emit_instr(ctx, srl, dst, dst, insn->imm & 0x1f);
  444. break;
  445. case BPF_ALU64 | BPF_LSH:
  446. emit_instr(ctx, dsll_safe, dst, dst, insn->imm & 0x3f);
  447. break;
  448. case BPF_ALU | BPF_LSH:
  449. emit_instr(ctx, sll, dst, dst, insn->imm & 0x1f);
  450. break;
  451. case BPF_ALU64 | BPF_ARSH:
  452. emit_instr(ctx, dsra_safe, dst, dst, insn->imm & 0x3f);
  453. break;
  454. case BPF_ALU | BPF_ARSH:
  455. emit_instr(ctx, sra, dst, dst, insn->imm & 0x1f);
  456. break;
  457. case BPF_ALU | BPF_MOV:
  458. emit_instr(ctx, addiu, dst, MIPS_R_ZERO, insn->imm);
  459. break;
  460. case BPF_ALU | BPF_ADD:
  461. emit_instr(ctx, addiu, dst, dst, insn->imm);
  462. break;
  463. case BPF_ALU | BPF_SUB:
  464. emit_instr(ctx, addiu, dst, dst, -insn->imm);
  465. break;
  466. default:
  467. return -EINVAL;
  468. }
  469. } else {
  470. /* multi insn immediate case */
  471. if (BPF_OP(insn->code) == BPF_MOV) {
  472. gen_imm_to_reg(insn, dst, ctx);
  473. } else {
  474. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  475. switch (BPF_OP(insn->code) | BPF_CLASS(insn->code)) {
  476. case BPF_ALU64 | BPF_AND:
  477. case BPF_ALU | BPF_AND:
  478. emit_instr(ctx, and, dst, dst, MIPS_R_AT);
  479. break;
  480. case BPF_ALU64 | BPF_OR:
  481. case BPF_ALU | BPF_OR:
  482. emit_instr(ctx, or, dst, dst, MIPS_R_AT);
  483. break;
  484. case BPF_ALU64 | BPF_XOR:
  485. case BPF_ALU | BPF_XOR:
  486. emit_instr(ctx, xor, dst, dst, MIPS_R_AT);
  487. break;
  488. case BPF_ALU64 | BPF_ADD:
  489. emit_instr(ctx, daddu, dst, dst, MIPS_R_AT);
  490. break;
  491. case BPF_ALU64 | BPF_SUB:
  492. emit_instr(ctx, dsubu, dst, dst, MIPS_R_AT);
  493. break;
  494. case BPF_ALU | BPF_ADD:
  495. emit_instr(ctx, addu, dst, dst, MIPS_R_AT);
  496. break;
  497. case BPF_ALU | BPF_SUB:
  498. emit_instr(ctx, subu, dst, dst, MIPS_R_AT);
  499. break;
  500. default:
  501. return -EINVAL;
  502. }
  503. }
  504. }
  505. return 0;
  506. }
  507. static void emit_const_to_reg(struct jit_ctx *ctx, int dst, u64 value)
  508. {
  509. if (value >= 0xffffffffffff8000ull || value < 0x8000ull) {
  510. emit_instr(ctx, daddiu, dst, MIPS_R_ZERO, (int)value);
  511. } else if (value >= 0xffffffff80000000ull ||
  512. (value < 0x80000000 && value > 0xffff)) {
  513. emit_instr(ctx, lui, dst, (s32)(s16)(value >> 16));
  514. emit_instr(ctx, ori, dst, dst, (unsigned int)(value & 0xffff));
  515. } else {
  516. int i;
  517. bool seen_part = false;
  518. int needed_shift = 0;
  519. for (i = 0; i < 4; i++) {
  520. u64 part = (value >> (16 * (3 - i))) & 0xffff;
  521. if (seen_part && needed_shift > 0 && (part || i == 3)) {
  522. emit_instr(ctx, dsll_safe, dst, dst, needed_shift);
  523. needed_shift = 0;
  524. }
  525. if (part) {
  526. if (i == 0 || (!seen_part && i < 3 && part < 0x8000)) {
  527. emit_instr(ctx, lui, dst, (s32)(s16)part);
  528. needed_shift = -16;
  529. } else {
  530. emit_instr(ctx, ori, dst,
  531. seen_part ? dst : MIPS_R_ZERO,
  532. (unsigned int)part);
  533. }
  534. seen_part = true;
  535. }
  536. if (seen_part)
  537. needed_shift += 16;
  538. }
  539. }
  540. }
  541. static int emit_bpf_tail_call(struct jit_ctx *ctx, int this_idx)
  542. {
  543. int off, b_off;
  544. int tcc_reg;
  545. ctx->flags |= EBPF_SEEN_TC;
  546. /*
  547. * if (index >= array->map.max_entries)
  548. * goto out;
  549. */
  550. off = offsetof(struct bpf_array, map.max_entries);
  551. emit_instr(ctx, lwu, MIPS_R_T5, off, MIPS_R_A1);
  552. emit_instr(ctx, sltu, MIPS_R_AT, MIPS_R_T5, MIPS_R_A2);
  553. b_off = b_imm(this_idx + 1, ctx);
  554. emit_instr(ctx, bne, MIPS_R_AT, MIPS_R_ZERO, b_off);
  555. /*
  556. * if (TCC-- < 0)
  557. * goto out;
  558. */
  559. /* Delay slot */
  560. tcc_reg = (ctx->flags & EBPF_TCC_IN_V1) ? MIPS_R_V1 : MIPS_R_S4;
  561. emit_instr(ctx, daddiu, MIPS_R_T5, tcc_reg, -1);
  562. b_off = b_imm(this_idx + 1, ctx);
  563. emit_instr(ctx, bltz, tcc_reg, b_off);
  564. /*
  565. * prog = array->ptrs[index];
  566. * if (prog == NULL)
  567. * goto out;
  568. */
  569. /* Delay slot */
  570. emit_instr(ctx, dsll, MIPS_R_T8, MIPS_R_A2, 3);
  571. emit_instr(ctx, daddu, MIPS_R_T8, MIPS_R_T8, MIPS_R_A1);
  572. off = offsetof(struct bpf_array, ptrs);
  573. emit_instr(ctx, ld, MIPS_R_AT, off, MIPS_R_T8);
  574. b_off = b_imm(this_idx + 1, ctx);
  575. emit_instr(ctx, beq, MIPS_R_AT, MIPS_R_ZERO, b_off);
  576. /* Delay slot */
  577. emit_instr(ctx, nop);
  578. /* goto *(prog->bpf_func + 4); */
  579. off = offsetof(struct bpf_prog, bpf_func);
  580. emit_instr(ctx, ld, MIPS_R_T9, off, MIPS_R_AT);
  581. /* All systems are go... propagate TCC */
  582. emit_instr(ctx, daddu, MIPS_R_V1, MIPS_R_T5, MIPS_R_ZERO);
  583. /* Skip first instruction (TCC initialization) */
  584. emit_instr(ctx, daddiu, MIPS_R_T9, MIPS_R_T9, 4);
  585. return build_int_epilogue(ctx, MIPS_R_T9);
  586. }
  587. static bool is_bad_offset(int b_off)
  588. {
  589. return b_off > 0x1ffff || b_off < -0x20000;
  590. }
  591. /* Returns the number of insn slots consumed. */
  592. static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  593. int this_idx, int exit_idx)
  594. {
  595. int src, dst, r, td, ts, mem_off, b_off;
  596. bool need_swap, did_move, cmp_eq;
  597. unsigned int target = 0;
  598. u64 t64;
  599. s64 t64s;
  600. int bpf_op = BPF_OP(insn->code);
  601. switch (insn->code) {
  602. case BPF_ALU64 | BPF_ADD | BPF_K: /* ALU64_IMM */
  603. case BPF_ALU64 | BPF_SUB | BPF_K: /* ALU64_IMM */
  604. case BPF_ALU64 | BPF_OR | BPF_K: /* ALU64_IMM */
  605. case BPF_ALU64 | BPF_AND | BPF_K: /* ALU64_IMM */
  606. case BPF_ALU64 | BPF_LSH | BPF_K: /* ALU64_IMM */
  607. case BPF_ALU64 | BPF_RSH | BPF_K: /* ALU64_IMM */
  608. case BPF_ALU64 | BPF_XOR | BPF_K: /* ALU64_IMM */
  609. case BPF_ALU64 | BPF_ARSH | BPF_K: /* ALU64_IMM */
  610. case BPF_ALU64 | BPF_MOV | BPF_K: /* ALU64_IMM */
  611. case BPF_ALU | BPF_MOV | BPF_K: /* ALU32_IMM */
  612. case BPF_ALU | BPF_ADD | BPF_K: /* ALU32_IMM */
  613. case BPF_ALU | BPF_SUB | BPF_K: /* ALU32_IMM */
  614. case BPF_ALU | BPF_OR | BPF_K: /* ALU64_IMM */
  615. case BPF_ALU | BPF_AND | BPF_K: /* ALU64_IMM */
  616. case BPF_ALU | BPF_LSH | BPF_K: /* ALU64_IMM */
  617. case BPF_ALU | BPF_RSH | BPF_K: /* ALU64_IMM */
  618. case BPF_ALU | BPF_XOR | BPF_K: /* ALU64_IMM */
  619. case BPF_ALU | BPF_ARSH | BPF_K: /* ALU64_IMM */
  620. r = gen_imm_insn(insn, ctx, this_idx);
  621. if (r < 0)
  622. return r;
  623. break;
  624. case BPF_ALU64 | BPF_MUL | BPF_K: /* ALU64_IMM */
  625. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  626. if (dst < 0)
  627. return dst;
  628. if (get_reg_val_type(ctx, this_idx, insn->dst_reg) == REG_32BIT)
  629. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  630. if (insn->imm == 1) /* Mult by 1 is a nop */
  631. break;
  632. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  633. emit_instr(ctx, dmultu, MIPS_R_AT, dst);
  634. emit_instr(ctx, mflo, dst);
  635. break;
  636. case BPF_ALU64 | BPF_NEG | BPF_K: /* ALU64_IMM */
  637. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  638. if (dst < 0)
  639. return dst;
  640. if (get_reg_val_type(ctx, this_idx, insn->dst_reg) == REG_32BIT)
  641. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  642. emit_instr(ctx, dsubu, dst, MIPS_R_ZERO, dst);
  643. break;
  644. case BPF_ALU | BPF_MUL | BPF_K: /* ALU_IMM */
  645. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  646. if (dst < 0)
  647. return dst;
  648. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  649. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX) {
  650. /* sign extend */
  651. emit_instr(ctx, sll, dst, dst, 0);
  652. }
  653. if (insn->imm == 1) /* Mult by 1 is a nop */
  654. break;
  655. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  656. emit_instr(ctx, multu, dst, MIPS_R_AT);
  657. emit_instr(ctx, mflo, dst);
  658. break;
  659. case BPF_ALU | BPF_NEG | BPF_K: /* ALU_IMM */
  660. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  661. if (dst < 0)
  662. return dst;
  663. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  664. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX) {
  665. /* sign extend */
  666. emit_instr(ctx, sll, dst, dst, 0);
  667. }
  668. emit_instr(ctx, subu, dst, MIPS_R_ZERO, dst);
  669. break;
  670. case BPF_ALU | BPF_DIV | BPF_K: /* ALU_IMM */
  671. case BPF_ALU | BPF_MOD | BPF_K: /* ALU_IMM */
  672. if (insn->imm == 0)
  673. return -EINVAL;
  674. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  675. if (dst < 0)
  676. return dst;
  677. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  678. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX)
  679. /* sign extend */
  680. emit_instr(ctx, sll, dst, dst, 0);
  681. if (insn->imm == 1) {
  682. /* div by 1 is a nop, mod by 1 is zero */
  683. if (bpf_op == BPF_MOD)
  684. emit_instr(ctx, addu, dst, MIPS_R_ZERO, MIPS_R_ZERO);
  685. break;
  686. }
  687. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  688. emit_instr(ctx, divu, dst, MIPS_R_AT);
  689. if (bpf_op == BPF_DIV)
  690. emit_instr(ctx, mflo, dst);
  691. else
  692. emit_instr(ctx, mfhi, dst);
  693. break;
  694. case BPF_ALU64 | BPF_DIV | BPF_K: /* ALU_IMM */
  695. case BPF_ALU64 | BPF_MOD | BPF_K: /* ALU_IMM */
  696. if (insn->imm == 0)
  697. return -EINVAL;
  698. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  699. if (dst < 0)
  700. return dst;
  701. if (get_reg_val_type(ctx, this_idx, insn->dst_reg) == REG_32BIT)
  702. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  703. if (insn->imm == 1) {
  704. /* div by 1 is a nop, mod by 1 is zero */
  705. if (bpf_op == BPF_MOD)
  706. emit_instr(ctx, addu, dst, MIPS_R_ZERO, MIPS_R_ZERO);
  707. break;
  708. }
  709. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  710. emit_instr(ctx, ddivu, dst, MIPS_R_AT);
  711. if (bpf_op == BPF_DIV)
  712. emit_instr(ctx, mflo, dst);
  713. else
  714. emit_instr(ctx, mfhi, dst);
  715. break;
  716. case BPF_ALU64 | BPF_MOV | BPF_X: /* ALU64_REG */
  717. case BPF_ALU64 | BPF_ADD | BPF_X: /* ALU64_REG */
  718. case BPF_ALU64 | BPF_SUB | BPF_X: /* ALU64_REG */
  719. case BPF_ALU64 | BPF_XOR | BPF_X: /* ALU64_REG */
  720. case BPF_ALU64 | BPF_OR | BPF_X: /* ALU64_REG */
  721. case BPF_ALU64 | BPF_AND | BPF_X: /* ALU64_REG */
  722. case BPF_ALU64 | BPF_MUL | BPF_X: /* ALU64_REG */
  723. case BPF_ALU64 | BPF_DIV | BPF_X: /* ALU64_REG */
  724. case BPF_ALU64 | BPF_MOD | BPF_X: /* ALU64_REG */
  725. case BPF_ALU64 | BPF_LSH | BPF_X: /* ALU64_REG */
  726. case BPF_ALU64 | BPF_RSH | BPF_X: /* ALU64_REG */
  727. case BPF_ALU64 | BPF_ARSH | BPF_X: /* ALU64_REG */
  728. src = ebpf_to_mips_reg(ctx, insn, src_reg);
  729. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  730. if (src < 0 || dst < 0)
  731. return -EINVAL;
  732. if (get_reg_val_type(ctx, this_idx, insn->dst_reg) == REG_32BIT)
  733. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  734. did_move = false;
  735. if (insn->src_reg == BPF_REG_10) {
  736. if (bpf_op == BPF_MOV) {
  737. emit_instr(ctx, daddiu, dst, MIPS_R_SP, MAX_BPF_STACK);
  738. did_move = true;
  739. } else {
  740. emit_instr(ctx, daddiu, MIPS_R_AT, MIPS_R_SP, MAX_BPF_STACK);
  741. src = MIPS_R_AT;
  742. }
  743. } else if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
  744. int tmp_reg = MIPS_R_AT;
  745. if (bpf_op == BPF_MOV) {
  746. tmp_reg = dst;
  747. did_move = true;
  748. }
  749. emit_instr(ctx, daddu, tmp_reg, src, MIPS_R_ZERO);
  750. emit_instr(ctx, dinsu, tmp_reg, MIPS_R_ZERO, 32, 32);
  751. src = MIPS_R_AT;
  752. }
  753. switch (bpf_op) {
  754. case BPF_MOV:
  755. if (!did_move)
  756. emit_instr(ctx, daddu, dst, src, MIPS_R_ZERO);
  757. break;
  758. case BPF_ADD:
  759. emit_instr(ctx, daddu, dst, dst, src);
  760. break;
  761. case BPF_SUB:
  762. emit_instr(ctx, dsubu, dst, dst, src);
  763. break;
  764. case BPF_XOR:
  765. emit_instr(ctx, xor, dst, dst, src);
  766. break;
  767. case BPF_OR:
  768. emit_instr(ctx, or, dst, dst, src);
  769. break;
  770. case BPF_AND:
  771. emit_instr(ctx, and, dst, dst, src);
  772. break;
  773. case BPF_MUL:
  774. emit_instr(ctx, dmultu, dst, src);
  775. emit_instr(ctx, mflo, dst);
  776. break;
  777. case BPF_DIV:
  778. case BPF_MOD:
  779. emit_instr(ctx, ddivu, dst, src);
  780. if (bpf_op == BPF_DIV)
  781. emit_instr(ctx, mflo, dst);
  782. else
  783. emit_instr(ctx, mfhi, dst);
  784. break;
  785. case BPF_LSH:
  786. emit_instr(ctx, dsllv, dst, dst, src);
  787. break;
  788. case BPF_RSH:
  789. emit_instr(ctx, dsrlv, dst, dst, src);
  790. break;
  791. case BPF_ARSH:
  792. emit_instr(ctx, dsrav, dst, dst, src);
  793. break;
  794. default:
  795. pr_err("ALU64_REG NOT HANDLED\n");
  796. return -EINVAL;
  797. }
  798. break;
  799. case BPF_ALU | BPF_MOV | BPF_X: /* ALU_REG */
  800. case BPF_ALU | BPF_ADD | BPF_X: /* ALU_REG */
  801. case BPF_ALU | BPF_SUB | BPF_X: /* ALU_REG */
  802. case BPF_ALU | BPF_XOR | BPF_X: /* ALU_REG */
  803. case BPF_ALU | BPF_OR | BPF_X: /* ALU_REG */
  804. case BPF_ALU | BPF_AND | BPF_X: /* ALU_REG */
  805. case BPF_ALU | BPF_MUL | BPF_X: /* ALU_REG */
  806. case BPF_ALU | BPF_DIV | BPF_X: /* ALU_REG */
  807. case BPF_ALU | BPF_MOD | BPF_X: /* ALU_REG */
  808. case BPF_ALU | BPF_LSH | BPF_X: /* ALU_REG */
  809. case BPF_ALU | BPF_RSH | BPF_X: /* ALU_REG */
  810. src = ebpf_to_mips_reg(ctx, insn, src_reg_no_fp);
  811. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  812. if (src < 0 || dst < 0)
  813. return -EINVAL;
  814. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  815. if (td == REG_64BIT || td == REG_32BIT_ZERO_EX) {
  816. /* sign extend */
  817. emit_instr(ctx, sll, dst, dst, 0);
  818. }
  819. did_move = false;
  820. ts = get_reg_val_type(ctx, this_idx, insn->src_reg);
  821. if (ts == REG_64BIT || ts == REG_32BIT_ZERO_EX) {
  822. int tmp_reg = MIPS_R_AT;
  823. if (bpf_op == BPF_MOV) {
  824. tmp_reg = dst;
  825. did_move = true;
  826. }
  827. /* sign extend */
  828. emit_instr(ctx, sll, tmp_reg, src, 0);
  829. src = MIPS_R_AT;
  830. }
  831. switch (bpf_op) {
  832. case BPF_MOV:
  833. if (!did_move)
  834. emit_instr(ctx, addu, dst, src, MIPS_R_ZERO);
  835. break;
  836. case BPF_ADD:
  837. emit_instr(ctx, addu, dst, dst, src);
  838. break;
  839. case BPF_SUB:
  840. emit_instr(ctx, subu, dst, dst, src);
  841. break;
  842. case BPF_XOR:
  843. emit_instr(ctx, xor, dst, dst, src);
  844. break;
  845. case BPF_OR:
  846. emit_instr(ctx, or, dst, dst, src);
  847. break;
  848. case BPF_AND:
  849. emit_instr(ctx, and, dst, dst, src);
  850. break;
  851. case BPF_MUL:
  852. emit_instr(ctx, mul, dst, dst, src);
  853. break;
  854. case BPF_DIV:
  855. case BPF_MOD:
  856. emit_instr(ctx, divu, dst, src);
  857. if (bpf_op == BPF_DIV)
  858. emit_instr(ctx, mflo, dst);
  859. else
  860. emit_instr(ctx, mfhi, dst);
  861. break;
  862. case BPF_LSH:
  863. emit_instr(ctx, sllv, dst, dst, src);
  864. break;
  865. case BPF_RSH:
  866. emit_instr(ctx, srlv, dst, dst, src);
  867. break;
  868. default:
  869. pr_err("ALU_REG NOT HANDLED\n");
  870. return -EINVAL;
  871. }
  872. break;
  873. case BPF_JMP | BPF_EXIT:
  874. if (this_idx + 1 < exit_idx) {
  875. b_off = b_imm(exit_idx, ctx);
  876. if (is_bad_offset(b_off))
  877. return -E2BIG;
  878. emit_instr(ctx, beq, MIPS_R_ZERO, MIPS_R_ZERO, b_off);
  879. emit_instr(ctx, nop);
  880. }
  881. break;
  882. case BPF_JMP | BPF_JEQ | BPF_K: /* JMP_IMM */
  883. case BPF_JMP | BPF_JNE | BPF_K: /* JMP_IMM */
  884. cmp_eq = (bpf_op == BPF_JEQ);
  885. dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
  886. if (dst < 0)
  887. return dst;
  888. if (insn->imm == 0) {
  889. src = MIPS_R_ZERO;
  890. } else {
  891. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  892. src = MIPS_R_AT;
  893. }
  894. goto jeq_common;
  895. case BPF_JMP | BPF_JEQ | BPF_X: /* JMP_REG */
  896. case BPF_JMP | BPF_JNE | BPF_X:
  897. case BPF_JMP | BPF_JSLT | BPF_X:
  898. case BPF_JMP | BPF_JSLE | BPF_X:
  899. case BPF_JMP | BPF_JSGT | BPF_X:
  900. case BPF_JMP | BPF_JSGE | BPF_X:
  901. case BPF_JMP | BPF_JLT | BPF_X:
  902. case BPF_JMP | BPF_JLE | BPF_X:
  903. case BPF_JMP | BPF_JGT | BPF_X:
  904. case BPF_JMP | BPF_JGE | BPF_X:
  905. case BPF_JMP | BPF_JSET | BPF_X:
  906. src = ebpf_to_mips_reg(ctx, insn, src_reg_no_fp);
  907. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  908. if (src < 0 || dst < 0)
  909. return -EINVAL;
  910. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  911. ts = get_reg_val_type(ctx, this_idx, insn->src_reg);
  912. if (td == REG_32BIT && ts != REG_32BIT) {
  913. emit_instr(ctx, sll, MIPS_R_AT, src, 0);
  914. src = MIPS_R_AT;
  915. } else if (ts == REG_32BIT && td != REG_32BIT) {
  916. emit_instr(ctx, sll, MIPS_R_AT, dst, 0);
  917. dst = MIPS_R_AT;
  918. }
  919. if (bpf_op == BPF_JSET) {
  920. emit_instr(ctx, and, MIPS_R_AT, dst, src);
  921. cmp_eq = false;
  922. dst = MIPS_R_AT;
  923. src = MIPS_R_ZERO;
  924. } else if (bpf_op == BPF_JSGT || bpf_op == BPF_JSLE) {
  925. emit_instr(ctx, dsubu, MIPS_R_AT, dst, src);
  926. if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
  927. b_off = b_imm(exit_idx, ctx);
  928. if (is_bad_offset(b_off))
  929. return -E2BIG;
  930. if (bpf_op == BPF_JSGT)
  931. emit_instr(ctx, blez, MIPS_R_AT, b_off);
  932. else
  933. emit_instr(ctx, bgtz, MIPS_R_AT, b_off);
  934. emit_instr(ctx, nop);
  935. return 2; /* We consumed the exit. */
  936. }
  937. b_off = b_imm(this_idx + insn->off + 1, ctx);
  938. if (is_bad_offset(b_off))
  939. return -E2BIG;
  940. if (bpf_op == BPF_JSGT)
  941. emit_instr(ctx, bgtz, MIPS_R_AT, b_off);
  942. else
  943. emit_instr(ctx, blez, MIPS_R_AT, b_off);
  944. emit_instr(ctx, nop);
  945. break;
  946. } else if (bpf_op == BPF_JSGE || bpf_op == BPF_JSLT) {
  947. emit_instr(ctx, slt, MIPS_R_AT, dst, src);
  948. cmp_eq = bpf_op == BPF_JSGE;
  949. dst = MIPS_R_AT;
  950. src = MIPS_R_ZERO;
  951. } else if (bpf_op == BPF_JGT || bpf_op == BPF_JLE) {
  952. /* dst or src could be AT */
  953. emit_instr(ctx, dsubu, MIPS_R_T8, dst, src);
  954. emit_instr(ctx, sltu, MIPS_R_AT, dst, src);
  955. /* SP known to be non-zero, movz becomes boolean not */
  956. emit_instr(ctx, movz, MIPS_R_T9, MIPS_R_SP, MIPS_R_T8);
  957. emit_instr(ctx, movn, MIPS_R_T9, MIPS_R_ZERO, MIPS_R_T8);
  958. emit_instr(ctx, or, MIPS_R_AT, MIPS_R_T9, MIPS_R_AT);
  959. cmp_eq = bpf_op == BPF_JGT;
  960. dst = MIPS_R_AT;
  961. src = MIPS_R_ZERO;
  962. } else if (bpf_op == BPF_JGE || bpf_op == BPF_JLT) {
  963. emit_instr(ctx, sltu, MIPS_R_AT, dst, src);
  964. cmp_eq = bpf_op == BPF_JGE;
  965. dst = MIPS_R_AT;
  966. src = MIPS_R_ZERO;
  967. } else { /* JNE/JEQ case */
  968. cmp_eq = (bpf_op == BPF_JEQ);
  969. }
  970. jeq_common:
  971. /*
  972. * If the next insn is EXIT and we are jumping arround
  973. * only it, invert the sense of the compare and
  974. * conditionally jump to the exit. Poor man's branch
  975. * chaining.
  976. */
  977. if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
  978. b_off = b_imm(exit_idx, ctx);
  979. if (is_bad_offset(b_off)) {
  980. target = j_target(ctx, exit_idx);
  981. if (target == (unsigned int)-1)
  982. return -E2BIG;
  983. cmp_eq = !cmp_eq;
  984. b_off = 4 * 3;
  985. if (!(ctx->offsets[this_idx] & OFFSETS_B_CONV)) {
  986. ctx->offsets[this_idx] |= OFFSETS_B_CONV;
  987. ctx->long_b_conversion = 1;
  988. }
  989. }
  990. if (cmp_eq)
  991. emit_instr(ctx, bne, dst, src, b_off);
  992. else
  993. emit_instr(ctx, beq, dst, src, b_off);
  994. emit_instr(ctx, nop);
  995. if (ctx->offsets[this_idx] & OFFSETS_B_CONV) {
  996. emit_instr(ctx, j, target);
  997. emit_instr(ctx, nop);
  998. }
  999. return 2; /* We consumed the exit. */
  1000. }
  1001. b_off = b_imm(this_idx + insn->off + 1, ctx);
  1002. if (is_bad_offset(b_off)) {
  1003. target = j_target(ctx, this_idx + insn->off + 1);
  1004. if (target == (unsigned int)-1)
  1005. return -E2BIG;
  1006. cmp_eq = !cmp_eq;
  1007. b_off = 4 * 3;
  1008. if (!(ctx->offsets[this_idx] & OFFSETS_B_CONV)) {
  1009. ctx->offsets[this_idx] |= OFFSETS_B_CONV;
  1010. ctx->long_b_conversion = 1;
  1011. }
  1012. }
  1013. if (cmp_eq)
  1014. emit_instr(ctx, beq, dst, src, b_off);
  1015. else
  1016. emit_instr(ctx, bne, dst, src, b_off);
  1017. emit_instr(ctx, nop);
  1018. if (ctx->offsets[this_idx] & OFFSETS_B_CONV) {
  1019. emit_instr(ctx, j, target);
  1020. emit_instr(ctx, nop);
  1021. }
  1022. break;
  1023. case BPF_JMP | BPF_JSGT | BPF_K: /* JMP_IMM */
  1024. case BPF_JMP | BPF_JSGE | BPF_K: /* JMP_IMM */
  1025. case BPF_JMP | BPF_JSLT | BPF_K: /* JMP_IMM */
  1026. case BPF_JMP | BPF_JSLE | BPF_K: /* JMP_IMM */
  1027. cmp_eq = (bpf_op == BPF_JSGE);
  1028. dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
  1029. if (dst < 0)
  1030. return dst;
  1031. if (insn->imm == 0) {
  1032. if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
  1033. b_off = b_imm(exit_idx, ctx);
  1034. if (is_bad_offset(b_off))
  1035. return -E2BIG;
  1036. switch (bpf_op) {
  1037. case BPF_JSGT:
  1038. emit_instr(ctx, blez, dst, b_off);
  1039. break;
  1040. case BPF_JSGE:
  1041. emit_instr(ctx, bltz, dst, b_off);
  1042. break;
  1043. case BPF_JSLT:
  1044. emit_instr(ctx, bgez, dst, b_off);
  1045. break;
  1046. case BPF_JSLE:
  1047. emit_instr(ctx, bgtz, dst, b_off);
  1048. break;
  1049. }
  1050. emit_instr(ctx, nop);
  1051. return 2; /* We consumed the exit. */
  1052. }
  1053. b_off = b_imm(this_idx + insn->off + 1, ctx);
  1054. if (is_bad_offset(b_off))
  1055. return -E2BIG;
  1056. switch (bpf_op) {
  1057. case BPF_JSGT:
  1058. emit_instr(ctx, bgtz, dst, b_off);
  1059. break;
  1060. case BPF_JSGE:
  1061. emit_instr(ctx, bgez, dst, b_off);
  1062. break;
  1063. case BPF_JSLT:
  1064. emit_instr(ctx, bltz, dst, b_off);
  1065. break;
  1066. case BPF_JSLE:
  1067. emit_instr(ctx, blez, dst, b_off);
  1068. break;
  1069. }
  1070. emit_instr(ctx, nop);
  1071. break;
  1072. }
  1073. /*
  1074. * only "LT" compare available, so we must use imm + 1
  1075. * to generate "GT" and imm -1 to generate LE
  1076. */
  1077. if (bpf_op == BPF_JSGT)
  1078. t64s = insn->imm + 1;
  1079. else if (bpf_op == BPF_JSLE)
  1080. t64s = insn->imm + 1;
  1081. else
  1082. t64s = insn->imm;
  1083. cmp_eq = bpf_op == BPF_JSGT || bpf_op == BPF_JSGE;
  1084. if (t64s >= S16_MIN && t64s <= S16_MAX) {
  1085. emit_instr(ctx, slti, MIPS_R_AT, dst, (int)t64s);
  1086. src = MIPS_R_AT;
  1087. dst = MIPS_R_ZERO;
  1088. goto jeq_common;
  1089. }
  1090. emit_const_to_reg(ctx, MIPS_R_AT, (u64)t64s);
  1091. emit_instr(ctx, slt, MIPS_R_AT, dst, MIPS_R_AT);
  1092. src = MIPS_R_AT;
  1093. dst = MIPS_R_ZERO;
  1094. goto jeq_common;
  1095. case BPF_JMP | BPF_JGT | BPF_K:
  1096. case BPF_JMP | BPF_JGE | BPF_K:
  1097. case BPF_JMP | BPF_JLT | BPF_K:
  1098. case BPF_JMP | BPF_JLE | BPF_K:
  1099. cmp_eq = (bpf_op == BPF_JGE);
  1100. dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
  1101. if (dst < 0)
  1102. return dst;
  1103. /*
  1104. * only "LT" compare available, so we must use imm + 1
  1105. * to generate "GT" and imm -1 to generate LE
  1106. */
  1107. if (bpf_op == BPF_JGT)
  1108. t64s = (u64)(u32)(insn->imm) + 1;
  1109. else if (bpf_op == BPF_JLE)
  1110. t64s = (u64)(u32)(insn->imm) + 1;
  1111. else
  1112. t64s = (u64)(u32)(insn->imm);
  1113. cmp_eq = bpf_op == BPF_JGT || bpf_op == BPF_JGE;
  1114. emit_const_to_reg(ctx, MIPS_R_AT, (u64)t64s);
  1115. emit_instr(ctx, sltu, MIPS_R_AT, dst, MIPS_R_AT);
  1116. src = MIPS_R_AT;
  1117. dst = MIPS_R_ZERO;
  1118. goto jeq_common;
  1119. case BPF_JMP | BPF_JSET | BPF_K: /* JMP_IMM */
  1120. dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
  1121. if (dst < 0)
  1122. return dst;
  1123. if (ctx->use_bbit_insns && hweight32((u32)insn->imm) == 1) {
  1124. if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
  1125. b_off = b_imm(exit_idx, ctx);
  1126. if (is_bad_offset(b_off))
  1127. return -E2BIG;
  1128. emit_instr(ctx, bbit0, dst, ffs((u32)insn->imm) - 1, b_off);
  1129. emit_instr(ctx, nop);
  1130. return 2; /* We consumed the exit. */
  1131. }
  1132. b_off = b_imm(this_idx + insn->off + 1, ctx);
  1133. if (is_bad_offset(b_off))
  1134. return -E2BIG;
  1135. emit_instr(ctx, bbit1, dst, ffs((u32)insn->imm) - 1, b_off);
  1136. emit_instr(ctx, nop);
  1137. break;
  1138. }
  1139. t64 = (u32)insn->imm;
  1140. emit_const_to_reg(ctx, MIPS_R_AT, t64);
  1141. emit_instr(ctx, and, MIPS_R_AT, dst, MIPS_R_AT);
  1142. src = MIPS_R_AT;
  1143. dst = MIPS_R_ZERO;
  1144. cmp_eq = false;
  1145. goto jeq_common;
  1146. case BPF_JMP | BPF_JA:
  1147. /*
  1148. * Prefer relative branch for easier debugging, but
  1149. * fall back if needed.
  1150. */
  1151. b_off = b_imm(this_idx + insn->off + 1, ctx);
  1152. if (is_bad_offset(b_off)) {
  1153. target = j_target(ctx, this_idx + insn->off + 1);
  1154. if (target == (unsigned int)-1)
  1155. return -E2BIG;
  1156. emit_instr(ctx, j, target);
  1157. } else {
  1158. emit_instr(ctx, b, b_off);
  1159. }
  1160. emit_instr(ctx, nop);
  1161. break;
  1162. case BPF_LD | BPF_DW | BPF_IMM:
  1163. if (insn->src_reg != 0)
  1164. return -EINVAL;
  1165. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1166. if (dst < 0)
  1167. return dst;
  1168. t64 = ((u64)(u32)insn->imm) | ((u64)(insn + 1)->imm << 32);
  1169. emit_const_to_reg(ctx, dst, t64);
  1170. return 2; /* Double slot insn */
  1171. case BPF_JMP | BPF_CALL:
  1172. ctx->flags |= EBPF_SAVE_RA;
  1173. t64s = (s64)insn->imm + (s64)__bpf_call_base;
  1174. emit_const_to_reg(ctx, MIPS_R_T9, (u64)t64s);
  1175. emit_instr(ctx, jalr, MIPS_R_RA, MIPS_R_T9);
  1176. /* delay slot */
  1177. emit_instr(ctx, nop);
  1178. break;
  1179. case BPF_JMP | BPF_TAIL_CALL:
  1180. if (emit_bpf_tail_call(ctx, this_idx))
  1181. return -EINVAL;
  1182. break;
  1183. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1184. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1185. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1186. if (dst < 0)
  1187. return dst;
  1188. td = get_reg_val_type(ctx, this_idx, insn->dst_reg);
  1189. if (insn->imm == 64 && td == REG_32BIT)
  1190. emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
  1191. if (insn->imm != 64 &&
  1192. (td == REG_64BIT || td == REG_32BIT_ZERO_EX)) {
  1193. /* sign extend */
  1194. emit_instr(ctx, sll, dst, dst, 0);
  1195. }
  1196. #ifdef __BIG_ENDIAN
  1197. need_swap = (BPF_SRC(insn->code) == BPF_FROM_LE);
  1198. #else
  1199. need_swap = (BPF_SRC(insn->code) == BPF_FROM_BE);
  1200. #endif
  1201. if (insn->imm == 16) {
  1202. if (need_swap)
  1203. emit_instr(ctx, wsbh, dst, dst);
  1204. emit_instr(ctx, andi, dst, dst, 0xffff);
  1205. } else if (insn->imm == 32) {
  1206. if (need_swap) {
  1207. emit_instr(ctx, wsbh, dst, dst);
  1208. emit_instr(ctx, rotr, dst, dst, 16);
  1209. }
  1210. } else { /* 64-bit*/
  1211. if (need_swap) {
  1212. emit_instr(ctx, dsbh, dst, dst);
  1213. emit_instr(ctx, dshd, dst, dst);
  1214. }
  1215. }
  1216. break;
  1217. case BPF_ST | BPF_B | BPF_MEM:
  1218. case BPF_ST | BPF_H | BPF_MEM:
  1219. case BPF_ST | BPF_W | BPF_MEM:
  1220. case BPF_ST | BPF_DW | BPF_MEM:
  1221. if (insn->dst_reg == BPF_REG_10) {
  1222. ctx->flags |= EBPF_SEEN_FP;
  1223. dst = MIPS_R_SP;
  1224. mem_off = insn->off + MAX_BPF_STACK;
  1225. } else {
  1226. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1227. if (dst < 0)
  1228. return dst;
  1229. mem_off = insn->off;
  1230. }
  1231. gen_imm_to_reg(insn, MIPS_R_AT, ctx);
  1232. switch (BPF_SIZE(insn->code)) {
  1233. case BPF_B:
  1234. emit_instr(ctx, sb, MIPS_R_AT, mem_off, dst);
  1235. break;
  1236. case BPF_H:
  1237. emit_instr(ctx, sh, MIPS_R_AT, mem_off, dst);
  1238. break;
  1239. case BPF_W:
  1240. emit_instr(ctx, sw, MIPS_R_AT, mem_off, dst);
  1241. break;
  1242. case BPF_DW:
  1243. emit_instr(ctx, sd, MIPS_R_AT, mem_off, dst);
  1244. break;
  1245. }
  1246. break;
  1247. case BPF_LDX | BPF_B | BPF_MEM:
  1248. case BPF_LDX | BPF_H | BPF_MEM:
  1249. case BPF_LDX | BPF_W | BPF_MEM:
  1250. case BPF_LDX | BPF_DW | BPF_MEM:
  1251. if (insn->src_reg == BPF_REG_10) {
  1252. ctx->flags |= EBPF_SEEN_FP;
  1253. src = MIPS_R_SP;
  1254. mem_off = insn->off + MAX_BPF_STACK;
  1255. } else {
  1256. src = ebpf_to_mips_reg(ctx, insn, src_reg_no_fp);
  1257. if (src < 0)
  1258. return src;
  1259. mem_off = insn->off;
  1260. }
  1261. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1262. if (dst < 0)
  1263. return dst;
  1264. switch (BPF_SIZE(insn->code)) {
  1265. case BPF_B:
  1266. emit_instr(ctx, lbu, dst, mem_off, src);
  1267. break;
  1268. case BPF_H:
  1269. emit_instr(ctx, lhu, dst, mem_off, src);
  1270. break;
  1271. case BPF_W:
  1272. emit_instr(ctx, lw, dst, mem_off, src);
  1273. break;
  1274. case BPF_DW:
  1275. emit_instr(ctx, ld, dst, mem_off, src);
  1276. break;
  1277. }
  1278. break;
  1279. case BPF_STX | BPF_B | BPF_MEM:
  1280. case BPF_STX | BPF_H | BPF_MEM:
  1281. case BPF_STX | BPF_W | BPF_MEM:
  1282. case BPF_STX | BPF_DW | BPF_MEM:
  1283. case BPF_STX | BPF_W | BPF_XADD:
  1284. case BPF_STX | BPF_DW | BPF_XADD:
  1285. if (insn->dst_reg == BPF_REG_10) {
  1286. ctx->flags |= EBPF_SEEN_FP;
  1287. dst = MIPS_R_SP;
  1288. mem_off = insn->off + MAX_BPF_STACK;
  1289. } else {
  1290. dst = ebpf_to_mips_reg(ctx, insn, dst_reg);
  1291. if (dst < 0)
  1292. return dst;
  1293. mem_off = insn->off;
  1294. }
  1295. src = ebpf_to_mips_reg(ctx, insn, src_reg_no_fp);
  1296. if (src < 0)
  1297. return src;
  1298. if (BPF_MODE(insn->code) == BPF_XADD) {
  1299. switch (BPF_SIZE(insn->code)) {
  1300. case BPF_W:
  1301. if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
  1302. emit_instr(ctx, sll, MIPS_R_AT, src, 0);
  1303. src = MIPS_R_AT;
  1304. }
  1305. emit_instr(ctx, ll, MIPS_R_T8, mem_off, dst);
  1306. emit_instr(ctx, addu, MIPS_R_T8, MIPS_R_T8, src);
  1307. emit_instr(ctx, sc, MIPS_R_T8, mem_off, dst);
  1308. /*
  1309. * On failure back up to LL (-4
  1310. * instructions of 4 bytes each
  1311. */
  1312. emit_instr(ctx, beq, MIPS_R_T8, MIPS_R_ZERO, -4 * 4);
  1313. emit_instr(ctx, nop);
  1314. break;
  1315. case BPF_DW:
  1316. if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
  1317. emit_instr(ctx, daddu, MIPS_R_AT, src, MIPS_R_ZERO);
  1318. emit_instr(ctx, dinsu, MIPS_R_AT, MIPS_R_ZERO, 32, 32);
  1319. src = MIPS_R_AT;
  1320. }
  1321. emit_instr(ctx, lld, MIPS_R_T8, mem_off, dst);
  1322. emit_instr(ctx, daddu, MIPS_R_T8, MIPS_R_T8, src);
  1323. emit_instr(ctx, scd, MIPS_R_T8, mem_off, dst);
  1324. emit_instr(ctx, beq, MIPS_R_T8, MIPS_R_ZERO, -4 * 4);
  1325. emit_instr(ctx, nop);
  1326. break;
  1327. }
  1328. } else { /* BPF_MEM */
  1329. switch (BPF_SIZE(insn->code)) {
  1330. case BPF_B:
  1331. emit_instr(ctx, sb, src, mem_off, dst);
  1332. break;
  1333. case BPF_H:
  1334. emit_instr(ctx, sh, src, mem_off, dst);
  1335. break;
  1336. case BPF_W:
  1337. emit_instr(ctx, sw, src, mem_off, dst);
  1338. break;
  1339. case BPF_DW:
  1340. if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
  1341. emit_instr(ctx, daddu, MIPS_R_AT, src, MIPS_R_ZERO);
  1342. emit_instr(ctx, dinsu, MIPS_R_AT, MIPS_R_ZERO, 32, 32);
  1343. src = MIPS_R_AT;
  1344. }
  1345. emit_instr(ctx, sd, src, mem_off, dst);
  1346. break;
  1347. }
  1348. }
  1349. break;
  1350. default:
  1351. pr_err("NOT HANDLED %d - (%02x)\n",
  1352. this_idx, (unsigned int)insn->code);
  1353. return -EINVAL;
  1354. }
  1355. return 1;
  1356. }
  1357. #define RVT_VISITED_MASK 0xc000000000000000ull
  1358. #define RVT_FALL_THROUGH 0x4000000000000000ull
  1359. #define RVT_BRANCH_TAKEN 0x8000000000000000ull
  1360. #define RVT_DONE (RVT_FALL_THROUGH | RVT_BRANCH_TAKEN)
  1361. static int build_int_body(struct jit_ctx *ctx)
  1362. {
  1363. const struct bpf_prog *prog = ctx->skf;
  1364. const struct bpf_insn *insn;
  1365. int i, r;
  1366. for (i = 0; i < prog->len; ) {
  1367. insn = prog->insnsi + i;
  1368. if ((ctx->reg_val_types[i] & RVT_VISITED_MASK) == 0) {
  1369. /* dead instruction, don't emit it. */
  1370. i++;
  1371. continue;
  1372. }
  1373. if (ctx->target == NULL)
  1374. ctx->offsets[i] = (ctx->offsets[i] & OFFSETS_B_CONV) | (ctx->idx * 4);
  1375. r = build_one_insn(insn, ctx, i, prog->len);
  1376. if (r < 0)
  1377. return r;
  1378. i += r;
  1379. }
  1380. /* epilogue offset */
  1381. if (ctx->target == NULL)
  1382. ctx->offsets[i] = ctx->idx * 4;
  1383. /*
  1384. * All exits have an offset of the epilogue, some offsets may
  1385. * not have been set due to banch-around threading, so set
  1386. * them now.
  1387. */
  1388. if (ctx->target == NULL)
  1389. for (i = 0; i < prog->len; i++) {
  1390. insn = prog->insnsi + i;
  1391. if (insn->code == (BPF_JMP | BPF_EXIT))
  1392. ctx->offsets[i] = ctx->idx * 4;
  1393. }
  1394. return 0;
  1395. }
  1396. /* return the last idx processed, or negative for error */
  1397. static int reg_val_propagate_range(struct jit_ctx *ctx, u64 initial_rvt,
  1398. int start_idx, bool follow_taken)
  1399. {
  1400. const struct bpf_prog *prog = ctx->skf;
  1401. const struct bpf_insn *insn;
  1402. u64 exit_rvt = initial_rvt;
  1403. u64 *rvt = ctx->reg_val_types;
  1404. int idx;
  1405. int reg;
  1406. for (idx = start_idx; idx < prog->len; idx++) {
  1407. rvt[idx] = (rvt[idx] & RVT_VISITED_MASK) | exit_rvt;
  1408. insn = prog->insnsi + idx;
  1409. switch (BPF_CLASS(insn->code)) {
  1410. case BPF_ALU:
  1411. switch (BPF_OP(insn->code)) {
  1412. case BPF_ADD:
  1413. case BPF_SUB:
  1414. case BPF_MUL:
  1415. case BPF_DIV:
  1416. case BPF_OR:
  1417. case BPF_AND:
  1418. case BPF_LSH:
  1419. case BPF_RSH:
  1420. case BPF_NEG:
  1421. case BPF_MOD:
  1422. case BPF_XOR:
  1423. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1424. break;
  1425. case BPF_MOV:
  1426. if (BPF_SRC(insn->code)) {
  1427. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1428. } else {
  1429. /* IMM to REG move*/
  1430. if (insn->imm >= 0)
  1431. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1432. else
  1433. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1434. }
  1435. break;
  1436. case BPF_END:
  1437. if (insn->imm == 64)
  1438. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1439. else if (insn->imm == 32)
  1440. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1441. else /* insn->imm == 16 */
  1442. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1443. break;
  1444. }
  1445. rvt[idx] |= RVT_DONE;
  1446. break;
  1447. case BPF_ALU64:
  1448. switch (BPF_OP(insn->code)) {
  1449. case BPF_MOV:
  1450. if (BPF_SRC(insn->code)) {
  1451. /* REG to REG move*/
  1452. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1453. } else {
  1454. /* IMM to REG move*/
  1455. if (insn->imm >= 0)
  1456. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1457. else
  1458. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT_32BIT);
  1459. }
  1460. break;
  1461. default:
  1462. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1463. }
  1464. rvt[idx] |= RVT_DONE;
  1465. break;
  1466. case BPF_LD:
  1467. switch (BPF_SIZE(insn->code)) {
  1468. case BPF_DW:
  1469. if (BPF_MODE(insn->code) == BPF_IMM) {
  1470. s64 val;
  1471. val = (s64)((u32)insn->imm | ((u64)(insn + 1)->imm << 32));
  1472. if (val > 0 && val <= S32_MAX)
  1473. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1474. else if (val >= S32_MIN && val <= S32_MAX)
  1475. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT_32BIT);
  1476. else
  1477. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1478. rvt[idx] |= RVT_DONE;
  1479. idx++;
  1480. } else {
  1481. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1482. }
  1483. break;
  1484. case BPF_B:
  1485. case BPF_H:
  1486. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1487. break;
  1488. case BPF_W:
  1489. if (BPF_MODE(insn->code) == BPF_IMM)
  1490. set_reg_val_type(&exit_rvt, insn->dst_reg,
  1491. insn->imm >= 0 ? REG_32BIT_POS : REG_32BIT);
  1492. else
  1493. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1494. break;
  1495. }
  1496. rvt[idx] |= RVT_DONE;
  1497. break;
  1498. case BPF_LDX:
  1499. switch (BPF_SIZE(insn->code)) {
  1500. case BPF_DW:
  1501. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_64BIT);
  1502. break;
  1503. case BPF_B:
  1504. case BPF_H:
  1505. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT_POS);
  1506. break;
  1507. case BPF_W:
  1508. set_reg_val_type(&exit_rvt, insn->dst_reg, REG_32BIT);
  1509. break;
  1510. }
  1511. rvt[idx] |= RVT_DONE;
  1512. break;
  1513. case BPF_JMP:
  1514. switch (BPF_OP(insn->code)) {
  1515. case BPF_EXIT:
  1516. rvt[idx] = RVT_DONE | exit_rvt;
  1517. rvt[prog->len] = exit_rvt;
  1518. return idx;
  1519. case BPF_JA:
  1520. rvt[idx] |= RVT_DONE;
  1521. idx += insn->off;
  1522. break;
  1523. case BPF_JEQ:
  1524. case BPF_JGT:
  1525. case BPF_JGE:
  1526. case BPF_JLT:
  1527. case BPF_JLE:
  1528. case BPF_JSET:
  1529. case BPF_JNE:
  1530. case BPF_JSGT:
  1531. case BPF_JSGE:
  1532. case BPF_JSLT:
  1533. case BPF_JSLE:
  1534. if (follow_taken) {
  1535. rvt[idx] |= RVT_BRANCH_TAKEN;
  1536. idx += insn->off;
  1537. follow_taken = false;
  1538. } else {
  1539. rvt[idx] |= RVT_FALL_THROUGH;
  1540. }
  1541. break;
  1542. case BPF_CALL:
  1543. set_reg_val_type(&exit_rvt, BPF_REG_0, REG_64BIT);
  1544. /* Upon call return, argument registers are clobbered. */
  1545. for (reg = BPF_REG_0; reg <= BPF_REG_5; reg++)
  1546. set_reg_val_type(&exit_rvt, reg, REG_64BIT);
  1547. rvt[idx] |= RVT_DONE;
  1548. break;
  1549. default:
  1550. WARN(1, "Unhandled BPF_JMP case.\n");
  1551. rvt[idx] |= RVT_DONE;
  1552. break;
  1553. }
  1554. break;
  1555. default:
  1556. rvt[idx] |= RVT_DONE;
  1557. break;
  1558. }
  1559. }
  1560. return idx;
  1561. }
  1562. /*
  1563. * Track the value range (i.e. 32-bit vs. 64-bit) of each register at
  1564. * each eBPF insn. This allows unneeded sign and zero extension
  1565. * operations to be omitted.
  1566. *
  1567. * Doesn't handle yet confluence of control paths with conflicting
  1568. * ranges, but it is good enough for most sane code.
  1569. */
  1570. static int reg_val_propagate(struct jit_ctx *ctx)
  1571. {
  1572. const struct bpf_prog *prog = ctx->skf;
  1573. u64 exit_rvt;
  1574. int reg;
  1575. int i;
  1576. /*
  1577. * 11 registers * 3 bits/reg leaves top bits free for other
  1578. * uses. Bit-62..63 used to see if we have visited an insn.
  1579. */
  1580. exit_rvt = 0;
  1581. /* Upon entry, argument registers are 64-bit. */
  1582. for (reg = BPF_REG_1; reg <= BPF_REG_5; reg++)
  1583. set_reg_val_type(&exit_rvt, reg, REG_64BIT);
  1584. /*
  1585. * First follow all conditional branches on the fall-through
  1586. * edge of control flow..
  1587. */
  1588. reg_val_propagate_range(ctx, exit_rvt, 0, false);
  1589. restart_search:
  1590. /*
  1591. * Then repeatedly find the first conditional branch where
  1592. * both edges of control flow have not been taken, and follow
  1593. * the branch taken edge. We will end up restarting the
  1594. * search once per conditional branch insn.
  1595. */
  1596. for (i = 0; i < prog->len; i++) {
  1597. u64 rvt = ctx->reg_val_types[i];
  1598. if ((rvt & RVT_VISITED_MASK) == RVT_DONE ||
  1599. (rvt & RVT_VISITED_MASK) == 0)
  1600. continue;
  1601. if ((rvt & RVT_VISITED_MASK) == RVT_FALL_THROUGH) {
  1602. reg_val_propagate_range(ctx, rvt & ~RVT_VISITED_MASK, i, true);
  1603. } else { /* RVT_BRANCH_TAKEN */
  1604. WARN(1, "Unexpected RVT_BRANCH_TAKEN case.\n");
  1605. reg_val_propagate_range(ctx, rvt & ~RVT_VISITED_MASK, i, false);
  1606. }
  1607. goto restart_search;
  1608. }
  1609. /*
  1610. * Eventually all conditional branches have been followed on
  1611. * both branches and we are done. Any insn that has not been
  1612. * visited at this point is dead.
  1613. */
  1614. return 0;
  1615. }
  1616. static void jit_fill_hole(void *area, unsigned int size)
  1617. {
  1618. u32 *p;
  1619. /* We are guaranteed to have aligned memory. */
  1620. for (p = area; size >= sizeof(u32); size -= sizeof(u32))
  1621. uasm_i_break(&p, BRK_BUG); /* Increments p */
  1622. }
  1623. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1624. {
  1625. struct bpf_prog *orig_prog = prog;
  1626. bool tmp_blinded = false;
  1627. struct bpf_prog *tmp;
  1628. struct bpf_binary_header *header = NULL;
  1629. struct jit_ctx ctx;
  1630. unsigned int image_size;
  1631. u8 *image_ptr;
  1632. if (!prog->jit_requested || !cpu_has_mips64r2)
  1633. return prog;
  1634. tmp = bpf_jit_blind_constants(prog);
  1635. /* If blinding was requested and we failed during blinding,
  1636. * we must fall back to the interpreter.
  1637. */
  1638. if (IS_ERR(tmp))
  1639. return orig_prog;
  1640. if (tmp != prog) {
  1641. tmp_blinded = true;
  1642. prog = tmp;
  1643. }
  1644. memset(&ctx, 0, sizeof(ctx));
  1645. preempt_disable();
  1646. switch (current_cpu_type()) {
  1647. case CPU_CAVIUM_OCTEON:
  1648. case CPU_CAVIUM_OCTEON_PLUS:
  1649. case CPU_CAVIUM_OCTEON2:
  1650. case CPU_CAVIUM_OCTEON3:
  1651. ctx.use_bbit_insns = 1;
  1652. break;
  1653. default:
  1654. ctx.use_bbit_insns = 0;
  1655. }
  1656. preempt_enable();
  1657. ctx.offsets = kcalloc(prog->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
  1658. if (ctx.offsets == NULL)
  1659. goto out_err;
  1660. ctx.reg_val_types = kcalloc(prog->len + 1, sizeof(*ctx.reg_val_types), GFP_KERNEL);
  1661. if (ctx.reg_val_types == NULL)
  1662. goto out_err;
  1663. ctx.skf = prog;
  1664. if (reg_val_propagate(&ctx))
  1665. goto out_err;
  1666. /*
  1667. * First pass discovers used resources and instruction offsets
  1668. * assuming short branches are used.
  1669. */
  1670. if (build_int_body(&ctx))
  1671. goto out_err;
  1672. /*
  1673. * If no calls are made (EBPF_SAVE_RA), then tail call count
  1674. * in $v1, else we must save in n$s4.
  1675. */
  1676. if (ctx.flags & EBPF_SEEN_TC) {
  1677. if (ctx.flags & EBPF_SAVE_RA)
  1678. ctx.flags |= EBPF_SAVE_S4;
  1679. else
  1680. ctx.flags |= EBPF_TCC_IN_V1;
  1681. }
  1682. /*
  1683. * Second pass generates offsets, if any branches are out of
  1684. * range a jump-around long sequence is generated, and we have
  1685. * to try again from the beginning to generate the new
  1686. * offsets. This is done until no additional conversions are
  1687. * necessary.
  1688. */
  1689. do {
  1690. ctx.idx = 0;
  1691. ctx.gen_b_offsets = 1;
  1692. ctx.long_b_conversion = 0;
  1693. if (gen_int_prologue(&ctx))
  1694. goto out_err;
  1695. if (build_int_body(&ctx))
  1696. goto out_err;
  1697. if (build_int_epilogue(&ctx, MIPS_R_RA))
  1698. goto out_err;
  1699. } while (ctx.long_b_conversion);
  1700. image_size = 4 * ctx.idx;
  1701. header = bpf_jit_binary_alloc(image_size, &image_ptr,
  1702. sizeof(u32), jit_fill_hole);
  1703. if (header == NULL)
  1704. goto out_err;
  1705. ctx.target = (u32 *)image_ptr;
  1706. /* Third pass generates the code */
  1707. ctx.idx = 0;
  1708. if (gen_int_prologue(&ctx))
  1709. goto out_err;
  1710. if (build_int_body(&ctx))
  1711. goto out_err;
  1712. if (build_int_epilogue(&ctx, MIPS_R_RA))
  1713. goto out_err;
  1714. /* Update the icache */
  1715. flush_icache_range((unsigned long)ctx.target,
  1716. (unsigned long)&ctx.target[ctx.idx]);
  1717. if (bpf_jit_enable > 1)
  1718. /* Dump JIT code */
  1719. bpf_jit_dump(prog->len, image_size, 2, ctx.target);
  1720. bpf_jit_binary_lock_ro(header);
  1721. prog->bpf_func = (void *)ctx.target;
  1722. prog->jited = 1;
  1723. prog->jited_len = image_size;
  1724. out_normal:
  1725. if (tmp_blinded)
  1726. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  1727. tmp : orig_prog);
  1728. kfree(ctx.offsets);
  1729. kfree(ctx.reg_val_types);
  1730. return prog;
  1731. out_err:
  1732. prog = orig_prog;
  1733. if (header)
  1734. bpf_jit_binary_free(header);
  1735. goto out_normal;
  1736. }