xref: /linux/arch/mips/net/bpf_jit_comp64.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Just-In-Time compiler for eBPF bytecode on MIPS.
4  * Implementation of JIT functions for 64-bit CPUs.
5  *
6  * Copyright (c) 2021 Anyfi Networks AB.
7  * Author: Johan Almbladh <johan.almbladh@gmail.com>
8  *
9  * Based on code and ideas from
10  * Copyright (c) 2017 Cavium, Inc.
11  * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
12  * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
13  */
14 
15 #include <linux/errno.h>
16 #include <linux/filter.h>
17 #include <linux/bpf.h>
18 #include <asm/cpu-features.h>
19 #include <asm/isa-rev.h>
20 #include <asm/uasm.h>
21 
22 #include "bpf_jit_comp.h"
23 
24 /* MIPS t0-t3 are not available in the n64 ABI */
25 #undef MIPS_R_T0
26 #undef MIPS_R_T1
27 #undef MIPS_R_T2
28 #undef MIPS_R_T3
29 
30 /* Stack is 16-byte aligned in n64 ABI */
31 #define MIPS_STACK_ALIGNMENT 16
32 
33 /* Extra 64-bit eBPF registers used by JIT */
34 #define JIT_REG_TC (MAX_BPF_JIT_REG + 0)
35 #define JIT_REG_ZX (MAX_BPF_JIT_REG + 1)
36 
37 /* Number of prologue bytes to skip when doing a tail call */
38 #define JIT_TCALL_SKIP 4
39 
40 /* Callee-saved CPU registers that the JIT must preserve */
41 #define JIT_CALLEE_REGS   \
42 	(BIT(MIPS_R_S0) | \
43 	 BIT(MIPS_R_S1) | \
44 	 BIT(MIPS_R_S2) | \
45 	 BIT(MIPS_R_S3) | \
46 	 BIT(MIPS_R_S4) | \
47 	 BIT(MIPS_R_S5) | \
48 	 BIT(MIPS_R_S6) | \
49 	 BIT(MIPS_R_S7) | \
50 	 BIT(MIPS_R_GP) | \
51 	 BIT(MIPS_R_FP) | \
52 	 BIT(MIPS_R_RA))
53 
54 /* Caller-saved CPU registers available for JIT use */
55 #define JIT_CALLER_REGS	  \
56 	(BIT(MIPS_R_A5) | \
57 	 BIT(MIPS_R_A6) | \
58 	 BIT(MIPS_R_A7))
59 /*
60  * Mapping of 64-bit eBPF registers to 64-bit native MIPS registers.
61  * MIPS registers t4 - t7 may be used by the JIT as temporary registers.
62  * MIPS registers t8 - t9 are reserved for single-register common functions.
63  */
64 static const u8 bpf2mips64[] = {
65 	/* Return value from in-kernel function, and exit value from eBPF */
66 	[BPF_REG_0] = MIPS_R_V0,
67 	/* Arguments from eBPF program to in-kernel function */
68 	[BPF_REG_1] = MIPS_R_A0,
69 	[BPF_REG_2] = MIPS_R_A1,
70 	[BPF_REG_3] = MIPS_R_A2,
71 	[BPF_REG_4] = MIPS_R_A3,
72 	[BPF_REG_5] = MIPS_R_A4,
73 	/* Callee-saved registers that in-kernel function will preserve */
74 	[BPF_REG_6] = MIPS_R_S0,
75 	[BPF_REG_7] = MIPS_R_S1,
76 	[BPF_REG_8] = MIPS_R_S2,
77 	[BPF_REG_9] = MIPS_R_S3,
78 	/* Read-only frame pointer to access the eBPF stack */
79 	[BPF_REG_FP] = MIPS_R_FP,
80 	/* Temporary register for blinding constants */
81 	[BPF_REG_AX] = MIPS_R_AT,
82 	/* Tail call count register, caller-saved */
83 	[JIT_REG_TC] = MIPS_R_A5,
84 	/* Constant for register zero-extension */
85 	[JIT_REG_ZX] = MIPS_R_V1,
86 };
87 
88 /*
89  * MIPS 32-bit operations on 64-bit registers generate a sign-extended
90  * result. However, the eBPF ISA mandates zero-extension, so we rely on the
91  * verifier to add that for us (emit_zext_ver). In addition, ALU arithmetic
92  * operations, right shift and byte swap require properly sign-extended
93  * operands or the result is unpredictable. We emit explicit sign-extensions
94  * in those cases.
95  */
96 
97 /* Sign extension */
98 static void emit_sext(struct jit_context *ctx, u8 dst, u8 src)
99 {
100 	emit(ctx, sll, dst, src, 0);
101 	clobber_reg(ctx, dst);
102 }
103 
104 /* Zero extension */
105 static void emit_zext(struct jit_context *ctx, u8 dst)
106 {
107 	if (cpu_has_mips64r2 || cpu_has_mips64r6) {
108 		emit(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
109 	} else {
110 		emit(ctx, and, dst, dst, bpf2mips64[JIT_REG_ZX]);
111 		access_reg(ctx, JIT_REG_ZX); /* We need the ZX register */
112 	}
113 	clobber_reg(ctx, dst);
114 }
115 
116 /* Zero extension, if verifier does not do it for us  */
117 static void emit_zext_ver(struct jit_context *ctx, u8 dst)
118 {
119 	if (!ctx->program->aux->verifier_zext)
120 		emit_zext(ctx, dst);
121 }
122 
123 /* Sign-extend an 8- or 16-bit field into a native register. */
124 static void emit_movsx_r(struct jit_context *ctx, u8 dst, u8 src, s16 off)
125 {
126 	/* seb/seh would need a sign-extended 32-bit source first. */
127 	int shift = 32 - off;
128 
129 	/* dsll32 and dsra32 add 32 to the shift argument. */
130 	emit(ctx, dsll32, dst, src, shift);
131 	emit(ctx, dsra32, dst, dst, shift);
132 }
133 
134 /* Register move operation (32-bit), optionally with sign extension */
135 static void emit_mov_r32(struct jit_context *ctx, u8 dst, u8 src, s16 off)
136 {
137 	switch (off) {
138 	case 8:
139 	case 16:
140 		emit_movsx_r(ctx, dst, src, off);
141 		break;
142 	default:
143 		/* off == 0 is MOV; the verifier rejects other offsets. */
144 		emit_mov_r(ctx, dst, src);
145 		break;
146 	}
147 	clobber_reg(ctx, dst);
148 	emit_zext_ver(ctx, dst);
149 }
150 
151 /* Register move operation (64-bit), optionally with sign extension */
152 static void emit_mov_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off)
153 {
154 	switch (off) {
155 	case 8:
156 	case 16:
157 		emit_movsx_r(ctx, dst, src, off);
158 		break;
159 	case 32:
160 		emit_sext(ctx, dst, src);
161 		break;
162 	default:
163 		/*
164 		 * off == 0 is ordinary MOV. The verifier rejects other
165 		 * offsets; defined exceptions require
166 		 * bpf_jit_supports_percpu_insn() or bpf_jit_supports_arena(),
167 		 * neither implemented by MIPS.
168 		 */
169 		emit_mov_r(ctx, dst, src);
170 		break;
171 	}
172 	clobber_reg(ctx, dst);
173 }
174 
175 /* dst = imm (64-bit) */
176 static void emit_mov_i64(struct jit_context *ctx, u8 dst, u64 imm64)
177 {
178 	if (imm64 >= 0xffffffffffff8000ULL || imm64 < 0x8000ULL) {
179 		emit(ctx, daddiu, dst, MIPS_R_ZERO, (s16)imm64);
180 	} else if (imm64 >= 0xffffffff80000000ULL ||
181 		   (imm64 < 0x80000000 && imm64 > 0xffff)) {
182 		emit(ctx, lui, dst, (s16)(imm64 >> 16));
183 		emit(ctx, ori, dst, dst, (u16)imm64 & 0xffff);
184 	} else {
185 		u8 acc = MIPS_R_ZERO;
186 		int shift = 0;
187 		int k;
188 
189 		for (k = 0; k < 4; k++) {
190 			u16 half = imm64 >> (48 - 16 * k);
191 
192 			if (acc == dst)
193 				shift += 16;
194 
195 			if (half) {
196 				if (shift)
197 					emit(ctx, dsll_safe, dst, dst, shift);
198 				emit(ctx, ori, dst, acc, half);
199 				acc = dst;
200 				shift = 0;
201 			}
202 		}
203 		if (shift)
204 			emit(ctx, dsll_safe, dst, dst, shift);
205 	}
206 	clobber_reg(ctx, dst);
207 }
208 
209 /* ALU immediate operation (64-bit) */
210 static void emit_alu_i64(struct jit_context *ctx, u8 dst, s32 imm, u8 op)
211 {
212 	switch (BPF_OP(op)) {
213 	/* dst = dst | imm */
214 	case BPF_OR:
215 		emit(ctx, ori, dst, dst, (u16)imm);
216 		break;
217 	/* dst = dst ^ imm */
218 	case BPF_XOR:
219 		emit(ctx, xori, dst, dst, (u16)imm);
220 		break;
221 	/* dst = -dst */
222 	case BPF_NEG:
223 		emit(ctx, dsubu, dst, MIPS_R_ZERO, dst);
224 		break;
225 	/* dst = dst << imm */
226 	case BPF_LSH:
227 		emit(ctx, dsll_safe, dst, dst, imm);
228 		break;
229 	/* dst = dst >> imm */
230 	case BPF_RSH:
231 		emit(ctx, dsrl_safe, dst, dst, imm);
232 		break;
233 	/* dst = dst >> imm (arithmetic) */
234 	case BPF_ARSH:
235 		emit(ctx, dsra_safe, dst, dst, imm);
236 		break;
237 	/* dst = dst + imm */
238 	case BPF_ADD:
239 		emit(ctx, daddiu, dst, dst, imm);
240 		break;
241 	/* dst = dst - imm */
242 	case BPF_SUB:
243 		emit(ctx, daddiu, dst, dst, -imm);
244 		break;
245 	default:
246 		/* Width-generic operations */
247 		emit_alu_i(ctx, dst, imm, op);
248 	}
249 	clobber_reg(ctx, dst);
250 }
251 
252 /* ALU division operation (64-bit) */
253 static void emit_div64(struct jit_context *ctx, u8 dst, u8 src, s16 off)
254 {
255 	if (cpu_has_mips64r6) {
256 		if (off == 1)
257 			emit(ctx, ddiv_r6, dst, dst, src);
258 		else
259 			emit(ctx, ddivu_r6, dst, dst, src);
260 	} else {
261 		if (off == 1)
262 			emit(ctx, ddiv, dst, src);
263 		else
264 			emit(ctx, ddivu, dst, src);
265 		emit(ctx, mflo, dst);
266 	}
267 }
268 
269 /* ALU modulo operation (64-bit) */
270 static void emit_mod64(struct jit_context *ctx, u8 dst, u8 src, s16 off)
271 {
272 	if (cpu_has_mips64r6) {
273 		if (off == 1)
274 			emit(ctx, dmod, dst, dst, src);
275 		else
276 			emit(ctx, dmodu, dst, dst, src);
277 	} else {
278 		if (off == 1)
279 			emit(ctx, ddiv, dst, src);
280 		else
281 			emit(ctx, ddivu, dst, src);
282 		emit(ctx, mfhi, dst);
283 	}
284 }
285 
286 /* ALU register operation (64-bit) */
287 static void emit_alu_r64(struct jit_context *ctx, u8 dst, u8 src, u8 op,
288 			 s16 off)
289 {
290 	switch (BPF_OP(op)) {
291 	/* dst = dst << src */
292 	case BPF_LSH:
293 		emit(ctx, dsllv, dst, dst, src);
294 		break;
295 	/* dst = dst >> src */
296 	case BPF_RSH:
297 		emit(ctx, dsrlv, dst, dst, src);
298 		break;
299 	/* dst = dst >> src (arithmetic) */
300 	case BPF_ARSH:
301 		emit(ctx, dsrav, dst, dst, src);
302 		break;
303 	/* dst = dst + src */
304 	case BPF_ADD:
305 		emit(ctx, daddu, dst, dst, src);
306 		break;
307 	/* dst = dst - src */
308 	case BPF_SUB:
309 		emit(ctx, dsubu, dst, dst, src);
310 		break;
311 	/* dst = dst * src */
312 	case BPF_MUL:
313 		if (cpu_has_mips64r6) {
314 			emit(ctx, dmulu, dst, dst, src);
315 		} else {
316 			emit(ctx, dmultu, dst, src);
317 			emit(ctx, mflo, dst);
318 			/* Ensure multiplication is completed */
319 			if (IS_ENABLED(CONFIG_CPU_R4000_WORKAROUNDS))
320 				emit(ctx, mfhi, MIPS_R_ZERO);
321 		}
322 		break;
323 	/* dst = dst / src */
324 	case BPF_DIV:
325 		emit_div64(ctx, dst, src, off);
326 		break;
327 	/* dst = dst % src */
328 	case BPF_MOD:
329 		emit_mod64(ctx, dst, src, off);
330 		break;
331 	default:
332 		/* Width-generic operations */
333 		emit_alu_r(ctx, dst, src, op, off);
334 	}
335 	clobber_reg(ctx, dst);
336 }
337 
338 /* Swap sub words in a register double word */
339 static void emit_swap_r64(struct jit_context *ctx, u8 dst, u8 mask, u32 bits)
340 {
341 	u8 tmp = MIPS_R_T9;
342 
343 	emit(ctx, and, tmp, dst, mask);  /* tmp = dst & mask  */
344 	emit(ctx, dsll, tmp, tmp, bits); /* tmp = tmp << bits */
345 	emit(ctx, dsrl, dst, dst, bits); /* dst = dst >> bits */
346 	emit(ctx, and, dst, dst, mask);  /* dst = dst & mask  */
347 	emit(ctx, or, dst, dst, tmp);    /* dst = dst | tmp   */
348 }
349 
350 /* Swap bytes and truncate a register double word, word or half word */
351 static void emit_bswap_r64(struct jit_context *ctx, u8 dst, u32 width)
352 {
353 	switch (width) {
354 	/* Swap bytes in a double word */
355 	case 64:
356 		if (cpu_has_mips64r2 || cpu_has_mips64r6) {
357 			emit(ctx, dsbh, dst, dst);
358 			emit(ctx, dshd, dst, dst);
359 		} else {
360 			u8 t1 = MIPS_R_T6;
361 			u8 t2 = MIPS_R_T7;
362 
363 			emit(ctx, dsll32, t2, dst, 0);  /* t2 = dst << 32    */
364 			emit(ctx, dsrl32, dst, dst, 0); /* dst = dst >> 32   */
365 			emit(ctx, or, dst, dst, t2);    /* dst = dst | t2    */
366 
367 			emit(ctx, ori, t2, MIPS_R_ZERO, 0xffff);
368 			emit(ctx, dsll32, t1, t2, 0);   /* t1 = t2 << 32     */
369 			emit(ctx, or, t1, t1, t2);      /* t1 = t1 | t2      */
370 			emit_swap_r64(ctx, dst, t1, 16);/* dst = swap16(dst) */
371 
372 			emit(ctx, lui, t2, 0xff);       /* t2 = 0x00ff0000   */
373 			emit(ctx, ori, t2, t2, 0xff);   /* t2 = t2 | 0x00ff  */
374 			emit(ctx, dsll32, t1, t2, 0);   /* t1 = t2 << 32     */
375 			emit(ctx, or, t1, t1, t2);      /* t1 = t1 | t2      */
376 			emit_swap_r64(ctx, dst, t1, 8); /* dst = swap8(dst)  */
377 		}
378 		break;
379 	/* Swap bytes in a half word */
380 	/* Swap bytes in a word */
381 	case 32:
382 	case 16:
383 		emit_sext(ctx, dst, dst);
384 		emit_bswap_r(ctx, dst, width);
385 		emit_zext(ctx, dst);
386 		break;
387 	}
388 	clobber_reg(ctx, dst);
389 }
390 
391 /* Truncate a register double word, word or half word */
392 static void emit_trunc_r64(struct jit_context *ctx, u8 dst, u32 width)
393 {
394 	switch (width) {
395 	case 64:
396 		break;
397 	/* Zero-extend a word */
398 	case 32:
399 		emit_zext(ctx, dst);
400 		break;
401 	/* Zero-extend a half word */
402 	case 16:
403 		emit(ctx, andi, dst, dst, 0xffff);
404 		break;
405 	}
406 	clobber_reg(ctx, dst);
407 }
408 
409 /* Load operation: dst = *(size*)(src + off) */
410 static void emit_ldx(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 size)
411 {
412 	switch (size) {
413 	/* Load a byte */
414 	case BPF_B:
415 		emit(ctx, lbu, dst, off, src);
416 		break;
417 	/* Load a half word */
418 	case BPF_H:
419 		emit(ctx, lhu, dst, off, src);
420 		break;
421 	/* Load a word */
422 	case BPF_W:
423 		emit(ctx, lwu, dst, off, src);
424 		break;
425 	/* Load a double word */
426 	case BPF_DW:
427 		emit(ctx, ld, dst, off, src);
428 		break;
429 	}
430 	clobber_reg(ctx, dst);
431 }
432 
433 /* Store operation: *(size *)(dst + off) = src */
434 static void emit_stx(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 size)
435 {
436 	switch (size) {
437 	/* Store a byte */
438 	case BPF_B:
439 		emit(ctx, sb, src, off, dst);
440 		break;
441 	/* Store a half word */
442 	case BPF_H:
443 		emit(ctx, sh, src, off, dst);
444 		break;
445 	/* Store a word */
446 	case BPF_W:
447 		emit(ctx, sw, src, off, dst);
448 		break;
449 	/* Store a double word */
450 	case BPF_DW:
451 		emit(ctx, sd, src, off, dst);
452 		break;
453 	}
454 }
455 
456 /* Atomic read-modify-write */
457 static void emit_atomic_r64(struct jit_context *ctx,
458 			    u8 dst, u8 src, s16 off, u8 code)
459 {
460 	u8 t1 = MIPS_R_T6;
461 	u8 t2 = MIPS_R_T7;
462 
463 	LLSC_sync(ctx);
464 	emit(ctx, lld, t1, off, dst);
465 	switch (code) {
466 	case BPF_ADD:
467 	case BPF_ADD | BPF_FETCH:
468 		emit(ctx, daddu, t2, t1, src);
469 		break;
470 	case BPF_AND:
471 	case BPF_AND | BPF_FETCH:
472 		emit(ctx, and, t2, t1, src);
473 		break;
474 	case BPF_OR:
475 	case BPF_OR | BPF_FETCH:
476 		emit(ctx, or, t2, t1, src);
477 		break;
478 	case BPF_XOR:
479 	case BPF_XOR | BPF_FETCH:
480 		emit(ctx, xor, t2, t1, src);
481 		break;
482 	case BPF_XCHG:
483 		emit(ctx, move, t2, src);
484 		break;
485 	}
486 	emit(ctx, scd, t2, off, dst);
487 	emit(ctx, LLSC_beqz, t2, -16 - LLSC_offset);
488 	emit(ctx, nop); /* Delay slot */
489 
490 	if (code & BPF_FETCH) {
491 		emit(ctx, move, src, t1);
492 		clobber_reg(ctx, src);
493 	}
494 }
495 
496 /* Atomic compare-and-exchange */
497 static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off)
498 {
499 	u8 r0 = bpf2mips64[BPF_REG_0];
500 	u8 t1 = MIPS_R_T6;
501 	u8 t2 = MIPS_R_T7;
502 
503 	LLSC_sync(ctx);
504 	emit(ctx, lld, t1, off, dst);
505 	emit(ctx, bne, t1, r0, 12);
506 	emit(ctx, move, t2, src);      /* Delay slot */
507 	emit(ctx, scd, t2, off, dst);
508 	emit(ctx, LLSC_beqz, t2, -20 - LLSC_offset);
509 	emit(ctx, move, r0, t1);       /* Delay slot */
510 
511 	clobber_reg(ctx, r0);
512 }
513 
514 /* Function call */
515 static int emit_call(struct jit_context *ctx, const struct bpf_insn *insn)
516 {
517 	u8 zx = bpf2mips64[JIT_REG_ZX];
518 	u8 tmp = MIPS_R_T6;
519 	bool fixed;
520 	u64 addr;
521 
522 	/* Decode the call address */
523 	if (bpf_jit_get_func_addr(ctx->program, insn, false,
524 				  &addr, &fixed) < 0)
525 		return -1;
526 	if (!fixed)
527 		return -1;
528 
529 	/* Push caller-saved registers on stack */
530 	push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, 0, 0);
531 
532 	/* Emit function call */
533 	emit_mov_i64(ctx, tmp, addr & JALR_MASK);
534 	emit(ctx, jalr, MIPS_R_RA, tmp);
535 	emit(ctx, nop); /* Delay slot */
536 
537 	/* Restore caller-saved registers */
538 	pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS, 0, 0);
539 
540 	/* Re-initialize the JIT zero-extension register if accessed */
541 	if (ctx->accessed & BIT(JIT_REG_ZX)) {
542 		emit(ctx, daddiu, zx, MIPS_R_ZERO, -1);
543 		emit(ctx, dsrl32, zx, zx, 0);
544 	}
545 
546 	clobber_reg(ctx, MIPS_R_RA);
547 	clobber_reg(ctx, MIPS_R_V0);
548 	clobber_reg(ctx, MIPS_R_V1);
549 	return 0;
550 }
551 
552 /* Function tail call */
553 static int emit_tail_call(struct jit_context *ctx)
554 {
555 	u8 ary = bpf2mips64[BPF_REG_2];
556 	u8 ind = bpf2mips64[BPF_REG_3];
557 	u8 tcc = bpf2mips64[JIT_REG_TC];
558 	u8 tmp = MIPS_R_T6;
559 	int off;
560 
561 	/*
562 	 * Tail call:
563 	 * eBPF R1 - function argument (context ptr), passed in a0-a1
564 	 * eBPF R2 - ptr to object with array of function entry points
565 	 * eBPF R3 - array index of function to be called
566 	 */
567 
568 	/* if (ind >= ary->map.max_entries) goto out */
569 	off = offsetof(struct bpf_array, map.max_entries);
570 	if (off > 0x7fff)
571 		return -1;
572 	emit(ctx, lwu, tmp, off, ary);            /* tmp = ary->map.max_entrs*/
573 	emit(ctx, sltu, tmp, ind, tmp);           /* tmp = ind < t1          */
574 	emit(ctx, beqz, tmp, get_offset(ctx, 1)); /* PC += off(1) if tmp == 0*/
575 
576 	/* if (--TCC < 0) goto out */
577 	emit(ctx, daddiu, tcc, tcc, -1);          /* tcc-- (delay slot)      */
578 	emit(ctx, bltz, tcc, get_offset(ctx, 1)); /* PC += off(1) if tcc < 0 */
579 						  /* (next insn delay slot)  */
580 	/* prog = ary->ptrs[ind] */
581 	off = offsetof(struct bpf_array, ptrs);
582 	if (off > 0x7fff)
583 		return -1;
584 	emit(ctx, dsll, tmp, ind, 3);             /* tmp = ind << 3          */
585 	emit(ctx, daddu, tmp, tmp, ary);          /* tmp += ary              */
586 	emit(ctx, ld, tmp, off, tmp);             /* tmp = *(tmp + off)      */
587 
588 	/* if (prog == 0) goto out */
589 	emit(ctx, beqz, tmp, get_offset(ctx, 1)); /* PC += off(1) if tmp == 0*/
590 	emit(ctx, nop);                           /* Delay slot              */
591 
592 	/* func = prog->bpf_func + 8 (prologue skip offset) */
593 	off = offsetof(struct bpf_prog, bpf_func);
594 	if (off > 0x7fff)
595 		return -1;
596 	emit(ctx, ld, tmp, off, tmp);                /* tmp = *(tmp + off)   */
597 	emit(ctx, daddiu, tmp, tmp, JIT_TCALL_SKIP); /* tmp += skip (4)      */
598 
599 	/* goto func */
600 	build_epilogue(ctx, tmp);
601 	access_reg(ctx, JIT_REG_TC);
602 	return 0;
603 }
604 
605 /*
606  * Stack frame layout for a JITed program (stack grows down).
607  *
608  * Higher address  : Previous stack frame      :
609  *                 +===========================+  <--- MIPS sp before call
610  *                 | Callee-saved registers,   |
611  *                 | including RA and FP       |
612  *                 +---------------------------+  <--- eBPF FP (MIPS fp)
613  *                 | Local eBPF variables      |
614  *                 | allocated by program      |
615  *                 +---------------------------+
616  *                 | Reserved for caller-saved |
617  *                 | registers                 |
618  * Lower address   +===========================+  <--- MIPS sp
619  */
620 
621 /* Build program prologue to set up the stack and registers */
622 void build_prologue(struct jit_context *ctx)
623 {
624 	u8 fp = bpf2mips64[BPF_REG_FP];
625 	u8 tc = bpf2mips64[JIT_REG_TC];
626 	u8 zx = bpf2mips64[JIT_REG_ZX];
627 	int stack, saved, locals, reserved;
628 
629 	/*
630 	 * In the unlikely event that the TCC limit is raised to more
631 	 * than 16 bits, it is clamped to the maximum value allowed for
632 	 * the generated code (0xffff). It is better fail to compile
633 	 * instead of degrading gracefully.
634 	 */
635 	BUILD_BUG_ON(MAX_TAIL_CALL_CNT > 0xffff);
636 
637 	/*
638 	 * The first instruction initializes the tail call count register.
639 	 * On a tail call, the calling function jumps into the prologue
640 	 * after this instruction.
641 	 */
642 	emit(ctx, ori, tc, MIPS_R_ZERO, MAX_TAIL_CALL_CNT);
643 
644 	/* === Entry-point for tail calls === */
645 
646 	/*
647 	 * If the eBPF frame pointer and tail call count registers were
648 	 * accessed they must be preserved. Mark them as clobbered here
649 	 * to save and restore them on the stack as needed.
650 	 */
651 	if (ctx->accessed & BIT(BPF_REG_FP))
652 		clobber_reg(ctx, fp);
653 	if (ctx->accessed & BIT(JIT_REG_TC))
654 		clobber_reg(ctx, tc);
655 	if (ctx->accessed & BIT(JIT_REG_ZX))
656 		clobber_reg(ctx, zx);
657 
658 	/* Compute the stack space needed for callee-saved registers */
659 	saved = hweight32(ctx->clobbered & JIT_CALLEE_REGS) * sizeof(u64);
660 	saved = ALIGN(saved, MIPS_STACK_ALIGNMENT);
661 
662 	/* Stack space used by eBPF program local data */
663 	locals = ALIGN(ctx->program->aux->stack_depth, MIPS_STACK_ALIGNMENT);
664 
665 	/*
666 	 * If we are emitting function calls, reserve extra stack space for
667 	 * caller-saved registers needed by the JIT. The required space is
668 	 * computed automatically during resource usage discovery (pass 1).
669 	 */
670 	reserved = ctx->stack_used;
671 
672 	/* Allocate the stack frame */
673 	stack = ALIGN(saved + locals + reserved, MIPS_STACK_ALIGNMENT);
674 	if (stack)
675 		emit(ctx, daddiu, MIPS_R_SP, MIPS_R_SP, -stack);
676 
677 	/* Store callee-saved registers on stack */
678 	push_regs(ctx, ctx->clobbered & JIT_CALLEE_REGS, 0, stack - saved);
679 
680 	/* Initialize the eBPF frame pointer if accessed */
681 	if (ctx->accessed & BIT(BPF_REG_FP))
682 		emit(ctx, daddiu, fp, MIPS_R_SP, stack - saved);
683 
684 	/* Initialize the ePF JIT zero-extension register if accessed */
685 	if (ctx->accessed & BIT(JIT_REG_ZX)) {
686 		emit(ctx, daddiu, zx, MIPS_R_ZERO, -1);
687 		emit(ctx, dsrl32, zx, zx, 0);
688 	}
689 
690 	ctx->saved_size = saved;
691 	ctx->stack_size = stack;
692 }
693 
694 /* Build the program epilogue to restore the stack and registers */
695 void build_epilogue(struct jit_context *ctx, int dest_reg)
696 {
697 	/* Restore callee-saved registers from stack */
698 	pop_regs(ctx, ctx->clobbered & JIT_CALLEE_REGS, 0,
699 		 ctx->stack_size - ctx->saved_size);
700 
701 	/* Release the stack frame */
702 	if (ctx->stack_size)
703 		emit(ctx, daddiu, MIPS_R_SP, MIPS_R_SP, ctx->stack_size);
704 
705 	/* Jump to return address and sign-extend the 32-bit return value */
706 	emit(ctx, jr, dest_reg);
707 	emit(ctx, sll, MIPS_R_V0, MIPS_R_V0, 0); /* Delay slot */
708 }
709 
710 /* Build one eBPF instruction */
711 int build_insn(const struct bpf_insn *insn, struct jit_context *ctx)
712 {
713 	u8 dst = bpf2mips64[insn->dst_reg];
714 	u8 src = bpf2mips64[insn->src_reg];
715 	u8 res = bpf2mips64[BPF_REG_0];
716 	u8 code = insn->code;
717 	s16 off = insn->off;
718 	s32 imm = insn->imm;
719 	s32 val, rel;
720 	u8 alu, jmp;
721 
722 	switch (code) {
723 	/* ALU operations */
724 	/* dst = imm */
725 	case BPF_ALU | BPF_MOV | BPF_K:
726 		emit_mov_i(ctx, dst, imm);
727 		emit_zext_ver(ctx, dst);
728 		break;
729 	/* dst = src */
730 	case BPF_ALU | BPF_MOV | BPF_X:
731 		if (imm == 1) {
732 			/* Special mov32 for zext */
733 			emit_zext(ctx, dst);
734 		} else {
735 			emit_mov_r32(ctx, dst, src, off);
736 		}
737 		break;
738 	/* dst = -dst */
739 	case BPF_ALU | BPF_NEG:
740 		emit_sext(ctx, dst, dst);
741 		emit_alu_i(ctx, dst, 0, BPF_NEG);
742 		emit_zext_ver(ctx, dst);
743 		break;
744 	/* dst = dst & imm */
745 	/* dst = dst | imm */
746 	/* dst = dst ^ imm */
747 	/* dst = dst << imm */
748 	case BPF_ALU | BPF_OR | BPF_K:
749 	case BPF_ALU | BPF_AND | BPF_K:
750 	case BPF_ALU | BPF_XOR | BPF_K:
751 	case BPF_ALU | BPF_LSH | BPF_K:
752 		if (!valid_alu_i(BPF_OP(code), imm, off)) {
753 			emit_mov_i(ctx, MIPS_R_T4, imm);
754 			emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code), off);
755 		} else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) {
756 			emit_alu_i(ctx, dst, val, alu);
757 		}
758 		emit_zext_ver(ctx, dst);
759 		break;
760 	/* dst = dst >> imm */
761 	/* dst = dst >> imm (arithmetic) */
762 	/* dst = dst + imm */
763 	/* dst = dst - imm */
764 	/* dst = dst * imm */
765 	/* dst = dst / imm */
766 	/* dst = dst % imm */
767 	case BPF_ALU | BPF_RSH | BPF_K:
768 	case BPF_ALU | BPF_ARSH | BPF_K:
769 	case BPF_ALU | BPF_ADD | BPF_K:
770 	case BPF_ALU | BPF_SUB | BPF_K:
771 	case BPF_ALU | BPF_MUL | BPF_K:
772 	case BPF_ALU | BPF_DIV | BPF_K:
773 	case BPF_ALU | BPF_MOD | BPF_K:
774 		if (!valid_alu_i(BPF_OP(code), imm, off)) {
775 			emit_sext(ctx, dst, dst);
776 			emit_mov_i(ctx, MIPS_R_T4, imm);
777 			emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code), off);
778 		} else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) {
779 			emit_sext(ctx, dst, dst);
780 			emit_alu_i(ctx, dst, val, alu);
781 		}
782 		emit_zext_ver(ctx, dst);
783 		break;
784 	/* dst = dst & src */
785 	/* dst = dst | src */
786 	/* dst = dst ^ src */
787 	/* dst = dst << src */
788 	case BPF_ALU | BPF_AND | BPF_X:
789 	case BPF_ALU | BPF_OR | BPF_X:
790 	case BPF_ALU | BPF_XOR | BPF_X:
791 	case BPF_ALU | BPF_LSH | BPF_X:
792 		emit_alu_r(ctx, dst, src, BPF_OP(code), off);
793 		emit_zext_ver(ctx, dst);
794 		break;
795 	/* dst = dst >> src */
796 	/* dst = dst >> src (arithmetic) */
797 	/* dst = dst + src */
798 	/* dst = dst - src */
799 	/* dst = dst * src */
800 	/* dst = dst / src */
801 	/* dst = dst % src */
802 	case BPF_ALU | BPF_RSH | BPF_X:
803 	case BPF_ALU | BPF_ARSH | BPF_X:
804 	case BPF_ALU | BPF_ADD | BPF_X:
805 	case BPF_ALU | BPF_SUB | BPF_X:
806 	case BPF_ALU | BPF_MUL | BPF_X:
807 	case BPF_ALU | BPF_DIV | BPF_X:
808 	case BPF_ALU | BPF_MOD | BPF_X:
809 		emit_sext(ctx, dst, dst);
810 		emit_sext(ctx, MIPS_R_T4, src);
811 		emit_alu_r(ctx, dst, MIPS_R_T4, BPF_OP(code), off);
812 		emit_zext_ver(ctx, dst);
813 		break;
814 	/* dst = imm (64-bit) */
815 	case BPF_ALU64 | BPF_MOV | BPF_K:
816 		emit_mov_i(ctx, dst, imm);
817 		break;
818 	/* dst = src (64-bit) */
819 	case BPF_ALU64 | BPF_MOV | BPF_X:
820 		emit_mov_r64(ctx, dst, src, off);
821 		break;
822 	/* dst = -dst (64-bit) */
823 	case BPF_ALU64 | BPF_NEG:
824 		emit_alu_i64(ctx, dst, 0, BPF_NEG);
825 		break;
826 	/* dst = dst & imm (64-bit) */
827 	/* dst = dst | imm (64-bit) */
828 	/* dst = dst ^ imm (64-bit) */
829 	/* dst = dst << imm (64-bit) */
830 	/* dst = dst >> imm (64-bit) */
831 	/* dst = dst >> imm ((64-bit, arithmetic) */
832 	/* dst = dst + imm (64-bit) */
833 	/* dst = dst - imm (64-bit) */
834 	/* dst = dst * imm (64-bit) */
835 	/* dst = dst / imm (64-bit) */
836 	/* dst = dst % imm (64-bit) */
837 	case BPF_ALU64 | BPF_AND | BPF_K:
838 	case BPF_ALU64 | BPF_OR | BPF_K:
839 	case BPF_ALU64 | BPF_XOR | BPF_K:
840 	case BPF_ALU64 | BPF_LSH | BPF_K:
841 	case BPF_ALU64 | BPF_RSH | BPF_K:
842 	case BPF_ALU64 | BPF_ARSH | BPF_K:
843 	case BPF_ALU64 | BPF_ADD | BPF_K:
844 	case BPF_ALU64 | BPF_SUB | BPF_K:
845 	case BPF_ALU64 | BPF_MUL | BPF_K:
846 	case BPF_ALU64 | BPF_DIV | BPF_K:
847 	case BPF_ALU64 | BPF_MOD | BPF_K:
848 		if (!valid_alu_i(BPF_OP(code), imm, off)) {
849 			emit_mov_i(ctx, MIPS_R_T4, imm);
850 			emit_alu_r64(ctx, dst, MIPS_R_T4, BPF_OP(code), off);
851 		} else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) {
852 			emit_alu_i64(ctx, dst, val, alu);
853 		}
854 		break;
855 	/* dst = dst & src (64-bit) */
856 	/* dst = dst | src (64-bit) */
857 	/* dst = dst ^ src (64-bit) */
858 	/* dst = dst << src (64-bit) */
859 	/* dst = dst >> src (64-bit) */
860 	/* dst = dst >> src (64-bit, arithmetic) */
861 	/* dst = dst + src (64-bit) */
862 	/* dst = dst - src (64-bit) */
863 	/* dst = dst * src (64-bit) */
864 	/* dst = dst / src (64-bit) */
865 	/* dst = dst % src (64-bit) */
866 	case BPF_ALU64 | BPF_AND | BPF_X:
867 	case BPF_ALU64 | BPF_OR | BPF_X:
868 	case BPF_ALU64 | BPF_XOR | BPF_X:
869 	case BPF_ALU64 | BPF_LSH | BPF_X:
870 	case BPF_ALU64 | BPF_RSH | BPF_X:
871 	case BPF_ALU64 | BPF_ARSH | BPF_X:
872 	case BPF_ALU64 | BPF_ADD | BPF_X:
873 	case BPF_ALU64 | BPF_SUB | BPF_X:
874 	case BPF_ALU64 | BPF_MUL | BPF_X:
875 	case BPF_ALU64 | BPF_DIV | BPF_X:
876 	case BPF_ALU64 | BPF_MOD | BPF_X:
877 		emit_alu_r64(ctx, dst, src, BPF_OP(code), off);
878 		break;
879 	/* dst = bswap(dst) */
880 	case BPF_ALU64 | BPF_END:
881 		emit_bswap_r64(ctx, dst, imm);
882 		break;
883 	/* dst = htole(dst) */
884 	/* dst = htobe(dst) */
885 	case BPF_ALU | BPF_END | BPF_FROM_LE:
886 	case BPF_ALU | BPF_END | BPF_FROM_BE:
887 		if (BPF_SRC(code) ==
888 #ifdef __BIG_ENDIAN
889 		    BPF_FROM_LE
890 #else
891 		    BPF_FROM_BE
892 #endif
893 		    )
894 			emit_bswap_r64(ctx, dst, imm);
895 		else
896 			emit_trunc_r64(ctx, dst, imm);
897 		break;
898 	/* dst = imm64 */
899 	case BPF_LD | BPF_IMM | BPF_DW:
900 		emit_mov_i64(ctx, dst, (u32)imm | ((u64)insn[1].imm << 32));
901 		return 1;
902 	/* LDX: dst = *(size *)(src + off) */
903 	case BPF_LDX | BPF_MEM | BPF_W:
904 	case BPF_LDX | BPF_MEM | BPF_H:
905 	case BPF_LDX | BPF_MEM | BPF_B:
906 	case BPF_LDX | BPF_MEM | BPF_DW:
907 		emit_ldx(ctx, dst, src, off, BPF_SIZE(code));
908 		break;
909 	/* ST: *(size *)(dst + off) = imm */
910 	case BPF_ST | BPF_MEM | BPF_W:
911 	case BPF_ST | BPF_MEM | BPF_H:
912 	case BPF_ST | BPF_MEM | BPF_B:
913 	case BPF_ST | BPF_MEM | BPF_DW:
914 		emit_mov_i(ctx, MIPS_R_T4, imm);
915 		emit_stx(ctx, dst, MIPS_R_T4, off, BPF_SIZE(code));
916 		break;
917 	/* STX: *(size *)(dst + off) = src */
918 	case BPF_STX | BPF_MEM | BPF_W:
919 	case BPF_STX | BPF_MEM | BPF_H:
920 	case BPF_STX | BPF_MEM | BPF_B:
921 	case BPF_STX | BPF_MEM | BPF_DW:
922 		emit_stx(ctx, dst, src, off, BPF_SIZE(code));
923 		break;
924 	/* Speculation barrier */
925 	case BPF_ST | BPF_NOSPEC:
926 		break;
927 	/* Atomics */
928 	case BPF_STX | BPF_ATOMIC | BPF_W:
929 	case BPF_STX | BPF_ATOMIC | BPF_DW:
930 		switch (imm) {
931 		case BPF_ADD:
932 		case BPF_ADD | BPF_FETCH:
933 		case BPF_AND:
934 		case BPF_AND | BPF_FETCH:
935 		case BPF_OR:
936 		case BPF_OR | BPF_FETCH:
937 		case BPF_XOR:
938 		case BPF_XOR | BPF_FETCH:
939 		case BPF_XCHG:
940 			if (BPF_SIZE(code) == BPF_DW) {
941 				emit_atomic_r64(ctx, dst, src, off, imm);
942 			} else if (imm & BPF_FETCH) {
943 				u8 tmp = dst;
944 
945 				if (src == dst) { /* Don't overwrite dst */
946 					emit_mov_r(ctx, MIPS_R_T4, dst);
947 					tmp = MIPS_R_T4;
948 				}
949 				emit_sext(ctx, src, src);
950 				emit_atomic_r(ctx, tmp, src, off, imm);
951 				emit_zext_ver(ctx, src);
952 			} else { /* 32-bit, no fetch */
953 				emit_sext(ctx, MIPS_R_T4, src);
954 				emit_atomic_r(ctx, dst, MIPS_R_T4, off, imm);
955 			}
956 			break;
957 		case BPF_CMPXCHG:
958 			if (BPF_SIZE(code) == BPF_DW) {
959 				emit_cmpxchg_r64(ctx, dst, src, off);
960 			} else {
961 				u8 tmp = res;
962 
963 				if (res == dst)   /* Don't overwrite dst */
964 					tmp = MIPS_R_T4;
965 				emit_sext(ctx, tmp, res);
966 				emit_sext(ctx, MIPS_R_T5, src);
967 				emit_cmpxchg_r(ctx, dst, MIPS_R_T5, tmp, off);
968 				if (res == dst)   /* Restore result */
969 					emit_mov_r(ctx, res, MIPS_R_T4);
970 				/* Result zext inserted by verifier */
971 			}
972 			break;
973 		default:
974 			goto notyet;
975 		}
976 		break;
977 	/* PC += off if dst == src */
978 	/* PC += off if dst != src */
979 	/* PC += off if dst & src */
980 	/* PC += off if dst > src */
981 	/* PC += off if dst >= src */
982 	/* PC += off if dst < src */
983 	/* PC += off if dst <= src */
984 	/* PC += off if dst > src (signed) */
985 	/* PC += off if dst >= src (signed) */
986 	/* PC += off if dst < src (signed) */
987 	/* PC += off if dst <= src (signed) */
988 	case BPF_JMP32 | BPF_JEQ | BPF_X:
989 	case BPF_JMP32 | BPF_JNE | BPF_X:
990 	case BPF_JMP32 | BPF_JSET | BPF_X:
991 	case BPF_JMP32 | BPF_JGT | BPF_X:
992 	case BPF_JMP32 | BPF_JGE | BPF_X:
993 	case BPF_JMP32 | BPF_JLT | BPF_X:
994 	case BPF_JMP32 | BPF_JLE | BPF_X:
995 	case BPF_JMP32 | BPF_JSGT | BPF_X:
996 	case BPF_JMP32 | BPF_JSGE | BPF_X:
997 	case BPF_JMP32 | BPF_JSLT | BPF_X:
998 	case BPF_JMP32 | BPF_JSLE | BPF_X:
999 		if (off == 0)
1000 			break;
1001 		setup_jmp_r(ctx, dst == src, BPF_OP(code), off, &jmp, &rel);
1002 		emit_sext(ctx, MIPS_R_T4, dst); /* Sign-extended dst */
1003 		emit_sext(ctx, MIPS_R_T5, src); /* Sign-extended src */
1004 		emit_jmp_r(ctx, MIPS_R_T4, MIPS_R_T5, rel, jmp);
1005 		if (finish_jmp(ctx, jmp, off) < 0)
1006 			goto toofar;
1007 		break;
1008 	/* PC += off if dst == imm */
1009 	/* PC += off if dst != imm */
1010 	/* PC += off if dst & imm */
1011 	/* PC += off if dst > imm */
1012 	/* PC += off if dst >= imm */
1013 	/* PC += off if dst < imm */
1014 	/* PC += off if dst <= imm */
1015 	/* PC += off if dst > imm (signed) */
1016 	/* PC += off if dst >= imm (signed) */
1017 	/* PC += off if dst < imm (signed) */
1018 	/* PC += off if dst <= imm (signed) */
1019 	case BPF_JMP32 | BPF_JEQ | BPF_K:
1020 	case BPF_JMP32 | BPF_JNE | BPF_K:
1021 	case BPF_JMP32 | BPF_JSET | BPF_K:
1022 	case BPF_JMP32 | BPF_JGT | BPF_K:
1023 	case BPF_JMP32 | BPF_JGE | BPF_K:
1024 	case BPF_JMP32 | BPF_JLT | BPF_K:
1025 	case BPF_JMP32 | BPF_JLE | BPF_K:
1026 	case BPF_JMP32 | BPF_JSGT | BPF_K:
1027 	case BPF_JMP32 | BPF_JSGE | BPF_K:
1028 	case BPF_JMP32 | BPF_JSLT | BPF_K:
1029 	case BPF_JMP32 | BPF_JSLE | BPF_K:
1030 		if (off == 0)
1031 			break;
1032 		setup_jmp_i(ctx, imm, 32, BPF_OP(code), off, &jmp, &rel);
1033 		emit_sext(ctx, MIPS_R_T4, dst); /* Sign-extended dst */
1034 		if (valid_jmp_i(jmp, imm)) {
1035 			emit_jmp_i(ctx, MIPS_R_T4, imm, rel, jmp);
1036 		} else {
1037 			/* Move large immediate to register, sign-extended */
1038 			emit_mov_i(ctx, MIPS_R_T5, imm);
1039 			emit_jmp_r(ctx, MIPS_R_T4, MIPS_R_T5, rel, jmp);
1040 		}
1041 		if (finish_jmp(ctx, jmp, off) < 0)
1042 			goto toofar;
1043 		break;
1044 	/* PC += imm */
1045 	case BPF_JMP32 | BPF_JA:
1046 		if (imm == 0)
1047 			break;
1048 		if (emit_ja(ctx, imm) < 0)
1049 			goto toofar;
1050 		break;
1051 	/* PC += off if dst == src */
1052 	/* PC += off if dst != src */
1053 	/* PC += off if dst & src */
1054 	/* PC += off if dst > src */
1055 	/* PC += off if dst >= src */
1056 	/* PC += off if dst < src */
1057 	/* PC += off if dst <= src */
1058 	/* PC += off if dst > src (signed) */
1059 	/* PC += off if dst >= src (signed) */
1060 	/* PC += off if dst < src (signed) */
1061 	/* PC += off if dst <= src (signed) */
1062 	case BPF_JMP | BPF_JEQ | BPF_X:
1063 	case BPF_JMP | BPF_JNE | BPF_X:
1064 	case BPF_JMP | BPF_JSET | BPF_X:
1065 	case BPF_JMP | BPF_JGT | BPF_X:
1066 	case BPF_JMP | BPF_JGE | BPF_X:
1067 	case BPF_JMP | BPF_JLT | BPF_X:
1068 	case BPF_JMP | BPF_JLE | BPF_X:
1069 	case BPF_JMP | BPF_JSGT | BPF_X:
1070 	case BPF_JMP | BPF_JSGE | BPF_X:
1071 	case BPF_JMP | BPF_JSLT | BPF_X:
1072 	case BPF_JMP | BPF_JSLE | BPF_X:
1073 		if (off == 0)
1074 			break;
1075 		setup_jmp_r(ctx, dst == src, BPF_OP(code), off, &jmp, &rel);
1076 		emit_jmp_r(ctx, dst, src, rel, jmp);
1077 		if (finish_jmp(ctx, jmp, off) < 0)
1078 			goto toofar;
1079 		break;
1080 	/* PC += off if dst == imm */
1081 	/* PC += off if dst != imm */
1082 	/* PC += off if dst & imm */
1083 	/* PC += off if dst > imm */
1084 	/* PC += off if dst >= imm */
1085 	/* PC += off if dst < imm */
1086 	/* PC += off if dst <= imm */
1087 	/* PC += off if dst > imm (signed) */
1088 	/* PC += off if dst >= imm (signed) */
1089 	/* PC += off if dst < imm (signed) */
1090 	/* PC += off if dst <= imm (signed) */
1091 	case BPF_JMP | BPF_JEQ | BPF_K:
1092 	case BPF_JMP | BPF_JNE | BPF_K:
1093 	case BPF_JMP | BPF_JSET | BPF_K:
1094 	case BPF_JMP | BPF_JGT | BPF_K:
1095 	case BPF_JMP | BPF_JGE | BPF_K:
1096 	case BPF_JMP | BPF_JLT | BPF_K:
1097 	case BPF_JMP | BPF_JLE | BPF_K:
1098 	case BPF_JMP | BPF_JSGT | BPF_K:
1099 	case BPF_JMP | BPF_JSGE | BPF_K:
1100 	case BPF_JMP | BPF_JSLT | BPF_K:
1101 	case BPF_JMP | BPF_JSLE | BPF_K:
1102 		if (off == 0)
1103 			break;
1104 		setup_jmp_i(ctx, imm, 64, BPF_OP(code), off, &jmp, &rel);
1105 		if (valid_jmp_i(jmp, imm)) {
1106 			emit_jmp_i(ctx, dst, imm, rel, jmp);
1107 		} else {
1108 			/* Move large immediate to register */
1109 			emit_mov_i(ctx, MIPS_R_T4, imm);
1110 			emit_jmp_r(ctx, dst, MIPS_R_T4, rel, jmp);
1111 		}
1112 		if (finish_jmp(ctx, jmp, off) < 0)
1113 			goto toofar;
1114 		break;
1115 	/* PC += off */
1116 	case BPF_JMP | BPF_JA:
1117 		if (off == 0)
1118 			break;
1119 		if (emit_ja(ctx, off) < 0)
1120 			goto toofar;
1121 		break;
1122 	/* Tail call */
1123 	case BPF_JMP | BPF_TAIL_CALL:
1124 		if (emit_tail_call(ctx) < 0)
1125 			goto invalid;
1126 		break;
1127 	/* Function call */
1128 	case BPF_JMP | BPF_CALL:
1129 		if (emit_call(ctx, insn) < 0)
1130 			goto invalid;
1131 		break;
1132 	/* Function return */
1133 	case BPF_JMP | BPF_EXIT:
1134 		/*
1135 		 * Optimization: when last instruction is EXIT
1136 		 * simply continue to epilogue.
1137 		 */
1138 		if (ctx->bpf_index == ctx->program->len - 1)
1139 			break;
1140 		if (emit_exit(ctx) < 0)
1141 			goto toofar;
1142 		break;
1143 
1144 	default:
1145 invalid:
1146 		pr_err_once("unknown opcode %02x\n", code);
1147 		return -EINVAL;
1148 notyet:
1149 		pr_info_once("*** NOT YET: opcode %02x ***\n", code);
1150 		return -EFAULT;
1151 toofar:
1152 		pr_info_once("*** TOO FAR: jump at %u opcode %02x ***\n",
1153 			     ctx->bpf_index, code);
1154 		return -E2BIG;
1155 	}
1156 	return 0;
1157 }
1158