Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Just-In-Time compiler for BPF filters on MIPS |
| 3 | * |
| 4 | * Copyright (c) 2014 Imagination Technologies Ltd. |
| 5 | * Author: Markos Chandras <markos.chandras@imgtec.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; version 2 of the License. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/bitops.h> |
| 13 | #include <linux/compiler.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/filter.h> |
| 16 | #include <linux/if_vlan.h> |
| 17 | #include <linux/kconfig.h> |
| 18 | #include <linux/moduleloader.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <asm/bitops.h> |
| 24 | #include <asm/cacheflush.h> |
| 25 | #include <asm/cpu-features.h> |
| 26 | #include <asm/uasm.h> |
| 27 | |
| 28 | #include "bpf_jit.h" |
| 29 | |
| 30 | /* ABI |
| 31 | * |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 32 | * s3 BPF register A |
| 33 | * s4 BPF register X |
| 34 | * s5 *skb |
| 35 | * s6 *scratch memory |
| 36 | * |
| 37 | * On entry (*bpf_func)(*skb, *filter) |
| 38 | * a0 = MIPS_R_A0 = skb; |
| 39 | * a1 = MIPS_R_A1 = filter; |
| 40 | * |
| 41 | * Stack |
| 42 | * ... |
| 43 | * M[15] |
| 44 | * M[14] |
| 45 | * M[13] |
| 46 | * ... |
| 47 | * M[0] <-- r_M |
| 48 | * saved reg k-1 |
| 49 | * saved reg k-2 |
| 50 | * ... |
| 51 | * saved reg 0 <-- r_sp |
| 52 | * <no argument area> |
| 53 | * |
| 54 | * Packet layout |
| 55 | * |
| 56 | * <--------------------- len ------------------------> |
| 57 | * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------> |
| 58 | * ---------------------------------------------------- |
| 59 | * | skb->data | |
| 60 | * ---------------------------------------------------- |
| 61 | */ |
| 62 | |
| 63 | #define RSIZE (sizeof(unsigned long)) |
| 64 | #define ptr typeof(unsigned long) |
| 65 | |
| 66 | /* ABI specific return values */ |
| 67 | #ifdef CONFIG_32BIT /* O32 */ |
| 68 | #ifdef CONFIG_CPU_LITTLE_ENDIAN |
| 69 | #define r_err MIPS_R_V1 |
| 70 | #define r_val MIPS_R_V0 |
| 71 | #else /* CONFIG_CPU_LITTLE_ENDIAN */ |
| 72 | #define r_err MIPS_R_V0 |
| 73 | #define r_val MIPS_R_V1 |
| 74 | #endif |
| 75 | #else /* N64 */ |
| 76 | #define r_err MIPS_R_V0 |
| 77 | #define r_val MIPS_R_V0 |
| 78 | #endif |
| 79 | |
| 80 | #define r_ret MIPS_R_V0 |
| 81 | |
| 82 | /* |
| 83 | * Use 2 scratch registers to avoid pipeline interlocks. |
| 84 | * There is no overhead during epilogue and prologue since |
| 85 | * any of the $s0-$s6 registers will only be preserved if |
| 86 | * they are going to actually be used. |
| 87 | */ |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 88 | #define r_off MIPS_R_S2 |
| 89 | #define r_A MIPS_R_S3 |
| 90 | #define r_X MIPS_R_S4 |
| 91 | #define r_skb MIPS_R_S5 |
| 92 | #define r_M MIPS_R_S6 |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 93 | #define r_s0 MIPS_R_T4 /* scratch reg 1 */ |
| 94 | #define r_s1 MIPS_R_T5 /* scratch reg 2 */ |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 95 | #define r_tmp_imm MIPS_R_T6 /* No need to preserve this */ |
| 96 | #define r_tmp MIPS_R_T7 /* No need to preserve this */ |
| 97 | #define r_zero MIPS_R_ZERO |
| 98 | #define r_sp MIPS_R_SP |
| 99 | #define r_ra MIPS_R_RA |
| 100 | |
| 101 | #define SCRATCH_OFF(k) (4 * (k)) |
| 102 | |
| 103 | /* JIT flags */ |
| 104 | #define SEEN_CALL (1 << BPF_MEMWORDS) |
| 105 | #define SEEN_SREG_SFT (BPF_MEMWORDS + 1) |
| 106 | #define SEEN_SREG_BASE (1 << SEEN_SREG_SFT) |
| 107 | #define SEEN_SREG(x) (SEEN_SREG_BASE << (x)) |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 108 | #define SEEN_OFF SEEN_SREG(2) |
| 109 | #define SEEN_A SEEN_SREG(3) |
| 110 | #define SEEN_X SEEN_SREG(4) |
| 111 | #define SEEN_SKB SEEN_SREG(5) |
| 112 | #define SEEN_MEM SEEN_SREG(6) |
| 113 | |
| 114 | /* Arguments used by JIT */ |
| 115 | #define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */ |
| 116 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 117 | #define SBIT(x) (1 << (x)) /* Signed version of BIT() */ |
| 118 | |
| 119 | /** |
| 120 | * struct jit_ctx - JIT context |
| 121 | * @skf: The sk_filter |
| 122 | * @prologue_bytes: Number of bytes for prologue |
| 123 | * @idx: Instruction index |
| 124 | * @flags: JIT flags |
| 125 | * @offsets: Instruction offsets |
| 126 | * @target: Memory location for the compiled filter |
| 127 | */ |
| 128 | struct jit_ctx { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 129 | const struct bpf_prog *skf; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 130 | unsigned int prologue_bytes; |
| 131 | u32 idx; |
| 132 | u32 flags; |
| 133 | u32 *offsets; |
| 134 | u32 *target; |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | static inline int optimize_div(u32 *k) |
| 139 | { |
| 140 | /* power of 2 divides can be implemented with right shift */ |
| 141 | if (!(*k & (*k-1))) { |
| 142 | *k = ilog2(*k); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Markos Chandras | 95782bf | 2014-06-25 09:37:21 +0100 | [diff] [blame] | 149 | static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx); |
| 150 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 151 | /* Simply emit the instruction if the JIT memory space has been allocated */ |
| 152 | #define emit_instr(ctx, func, ...) \ |
| 153 | do { \ |
| 154 | if ((ctx)->target != NULL) { \ |
| 155 | u32 *p = &(ctx)->target[ctx->idx]; \ |
| 156 | uasm_i_##func(&p, ##__VA_ARGS__); \ |
| 157 | } \ |
| 158 | (ctx)->idx++; \ |
| 159 | } while (0) |
| 160 | |
Markos Chandras | 39bcb79 | 2014-07-23 10:00:09 +0100 | [diff] [blame] | 161 | /* |
| 162 | * Similar to emit_instr but it must be used when we need to emit |
| 163 | * 32-bit or 64-bit instructions |
| 164 | */ |
| 165 | #define emit_long_instr(ctx, func, ...) \ |
| 166 | do { \ |
| 167 | if ((ctx)->target != NULL) { \ |
| 168 | u32 *p = &(ctx)->target[ctx->idx]; \ |
| 169 | UASM_i_##func(&p, ##__VA_ARGS__); \ |
| 170 | } \ |
| 171 | (ctx)->idx++; \ |
| 172 | } while (0) |
| 173 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 174 | /* Determine if immediate is within the 16-bit signed range */ |
| 175 | static inline bool is_range16(s32 imm) |
| 176 | { |
Markos Chandras | 10c4d61 | 2014-06-23 10:38:55 +0100 | [diff] [blame] | 177 | return !(imm >= SBIT(15) || imm < -SBIT(15)); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static inline void emit_addu(unsigned int dst, unsigned int src1, |
| 181 | unsigned int src2, struct jit_ctx *ctx) |
| 182 | { |
| 183 | emit_instr(ctx, addu, dst, src1, src2); |
| 184 | } |
| 185 | |
| 186 | static inline void emit_nop(struct jit_ctx *ctx) |
| 187 | { |
| 188 | emit_instr(ctx, nop); |
| 189 | } |
| 190 | |
| 191 | /* Load a u32 immediate to a register */ |
| 192 | static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx) |
| 193 | { |
| 194 | if (ctx->target != NULL) { |
| 195 | /* addiu can only handle s16 */ |
Markos Chandras | 10c4d61 | 2014-06-23 10:38:55 +0100 | [diff] [blame] | 196 | if (!is_range16(imm)) { |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 197 | u32 *p = &ctx->target[ctx->idx]; |
| 198 | uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16); |
| 199 | p = &ctx->target[ctx->idx + 1]; |
| 200 | uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff); |
| 201 | } else { |
| 202 | u32 *p = &ctx->target[ctx->idx]; |
| 203 | uasm_i_addiu(&p, dst, r_zero, imm); |
| 204 | } |
| 205 | } |
| 206 | ctx->idx++; |
| 207 | |
Markos Chandras | 10c4d61 | 2014-06-23 10:38:55 +0100 | [diff] [blame] | 208 | if (!is_range16(imm)) |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 209 | ctx->idx++; |
| 210 | } |
| 211 | |
| 212 | static inline void emit_or(unsigned int dst, unsigned int src1, |
| 213 | unsigned int src2, struct jit_ctx *ctx) |
| 214 | { |
| 215 | emit_instr(ctx, or, dst, src1, src2); |
| 216 | } |
| 217 | |
| 218 | static inline void emit_ori(unsigned int dst, unsigned src, u32 imm, |
| 219 | struct jit_ctx *ctx) |
| 220 | { |
| 221 | if (imm >= BIT(16)) { |
| 222 | emit_load_imm(r_tmp, imm, ctx); |
| 223 | emit_or(dst, src, r_tmp, ctx); |
| 224 | } else { |
| 225 | emit_instr(ctx, ori, dst, src, imm); |
| 226 | } |
| 227 | } |
| 228 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 229 | static inline void emit_daddiu(unsigned int dst, unsigned int src, |
| 230 | int imm, struct jit_ctx *ctx) |
| 231 | { |
| 232 | /* |
| 233 | * Only used for stack, so the imm is relatively small |
| 234 | * and it fits in 15-bits |
| 235 | */ |
| 236 | emit_instr(ctx, daddiu, dst, src, imm); |
| 237 | } |
| 238 | |
| 239 | static inline void emit_addiu(unsigned int dst, unsigned int src, |
| 240 | u32 imm, struct jit_ctx *ctx) |
| 241 | { |
Markos Chandras | 10c4d61 | 2014-06-23 10:38:55 +0100 | [diff] [blame] | 242 | if (!is_range16(imm)) { |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 243 | emit_load_imm(r_tmp, imm, ctx); |
| 244 | emit_addu(dst, r_tmp, src, ctx); |
| 245 | } else { |
| 246 | emit_instr(ctx, addiu, dst, src, imm); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | static inline void emit_and(unsigned int dst, unsigned int src1, |
| 251 | unsigned int src2, struct jit_ctx *ctx) |
| 252 | { |
| 253 | emit_instr(ctx, and, dst, src1, src2); |
| 254 | } |
| 255 | |
| 256 | static inline void emit_andi(unsigned int dst, unsigned int src, |
| 257 | u32 imm, struct jit_ctx *ctx) |
| 258 | { |
| 259 | /* If imm does not fit in u16 then load it to register */ |
| 260 | if (imm >= BIT(16)) { |
| 261 | emit_load_imm(r_tmp, imm, ctx); |
| 262 | emit_and(dst, src, r_tmp, ctx); |
| 263 | } else { |
| 264 | emit_instr(ctx, andi, dst, src, imm); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static inline void emit_xor(unsigned int dst, unsigned int src1, |
| 269 | unsigned int src2, struct jit_ctx *ctx) |
| 270 | { |
| 271 | emit_instr(ctx, xor, dst, src1, src2); |
| 272 | } |
| 273 | |
| 274 | static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx) |
| 275 | { |
| 276 | /* If imm does not fit in u16 then load it to register */ |
| 277 | if (imm >= BIT(16)) { |
| 278 | emit_load_imm(r_tmp, imm, ctx); |
| 279 | emit_xor(dst, src, r_tmp, ctx); |
| 280 | } else { |
| 281 | emit_instr(ctx, xori, dst, src, imm); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static inline void emit_stack_offset(int offset, struct jit_ctx *ctx) |
| 286 | { |
Markos Chandras | 39bcb79 | 2014-07-23 10:00:09 +0100 | [diff] [blame] | 287 | emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static inline void emit_subu(unsigned int dst, unsigned int src1, |
| 291 | unsigned int src2, struct jit_ctx *ctx) |
| 292 | { |
| 293 | emit_instr(ctx, subu, dst, src1, src2); |
| 294 | } |
| 295 | |
| 296 | static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx) |
| 297 | { |
| 298 | emit_subu(reg, r_zero, reg, ctx); |
| 299 | } |
| 300 | |
| 301 | static inline void emit_sllv(unsigned int dst, unsigned int src, |
| 302 | unsigned int sa, struct jit_ctx *ctx) |
| 303 | { |
| 304 | emit_instr(ctx, sllv, dst, src, sa); |
| 305 | } |
| 306 | |
| 307 | static inline void emit_sll(unsigned int dst, unsigned int src, |
| 308 | unsigned int sa, struct jit_ctx *ctx) |
| 309 | { |
| 310 | /* sa is 5-bits long */ |
Markos Chandras | 95782bf | 2014-06-25 09:37:21 +0100 | [diff] [blame] | 311 | if (sa >= BIT(5)) |
| 312 | /* Shifting >= 32 results in zero */ |
| 313 | emit_jit_reg_move(dst, r_zero, ctx); |
| 314 | else |
| 315 | emit_instr(ctx, sll, dst, src, sa); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static inline void emit_srlv(unsigned int dst, unsigned int src, |
| 319 | unsigned int sa, struct jit_ctx *ctx) |
| 320 | { |
| 321 | emit_instr(ctx, srlv, dst, src, sa); |
| 322 | } |
| 323 | |
| 324 | static inline void emit_srl(unsigned int dst, unsigned int src, |
| 325 | unsigned int sa, struct jit_ctx *ctx) |
| 326 | { |
| 327 | /* sa is 5-bits long */ |
Markos Chandras | 95782bf | 2014-06-25 09:37:21 +0100 | [diff] [blame] | 328 | if (sa >= BIT(5)) |
| 329 | /* Shifting >= 32 results in zero */ |
| 330 | emit_jit_reg_move(dst, r_zero, ctx); |
| 331 | else |
| 332 | emit_instr(ctx, srl, dst, src, sa); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Markos Chandras | 55393ee | 2014-06-23 10:38:48 +0100 | [diff] [blame] | 335 | static inline void emit_slt(unsigned int dst, unsigned int src1, |
| 336 | unsigned int src2, struct jit_ctx *ctx) |
| 337 | { |
| 338 | emit_instr(ctx, slt, dst, src1, src2); |
| 339 | } |
| 340 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 341 | static inline void emit_sltu(unsigned int dst, unsigned int src1, |
| 342 | unsigned int src2, struct jit_ctx *ctx) |
| 343 | { |
| 344 | emit_instr(ctx, sltu, dst, src1, src2); |
| 345 | } |
| 346 | |
| 347 | static inline void emit_sltiu(unsigned dst, unsigned int src, |
| 348 | unsigned int imm, struct jit_ctx *ctx) |
| 349 | { |
| 350 | /* 16 bit immediate */ |
Markos Chandras | 10c4d61 | 2014-06-23 10:38:55 +0100 | [diff] [blame] | 351 | if (!is_range16((s32)imm)) { |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 352 | emit_load_imm(r_tmp, imm, ctx); |
| 353 | emit_sltu(dst, src, r_tmp, ctx); |
| 354 | } else { |
| 355 | emit_instr(ctx, sltiu, dst, src, imm); |
| 356 | } |
| 357 | |
| 358 | } |
| 359 | |
| 360 | /* Store register on the stack */ |
| 361 | static inline void emit_store_stack_reg(ptr reg, ptr base, |
| 362 | unsigned int offset, |
| 363 | struct jit_ctx *ctx) |
| 364 | { |
Markos Chandras | 39bcb79 | 2014-07-23 10:00:09 +0100 | [diff] [blame] | 365 | emit_long_instr(ctx, SW, reg, offset, base); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static inline void emit_store(ptr reg, ptr base, unsigned int offset, |
| 369 | struct jit_ctx *ctx) |
| 370 | { |
| 371 | emit_instr(ctx, sw, reg, offset, base); |
| 372 | } |
| 373 | |
| 374 | static inline void emit_load_stack_reg(ptr reg, ptr base, |
| 375 | unsigned int offset, |
| 376 | struct jit_ctx *ctx) |
| 377 | { |
Markos Chandras | 39bcb79 | 2014-07-23 10:00:09 +0100 | [diff] [blame] | 378 | emit_long_instr(ctx, LW, reg, offset, base); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static inline void emit_load(unsigned int reg, unsigned int base, |
| 382 | unsigned int offset, struct jit_ctx *ctx) |
| 383 | { |
| 384 | emit_instr(ctx, lw, reg, offset, base); |
| 385 | } |
| 386 | |
| 387 | static inline void emit_load_byte(unsigned int reg, unsigned int base, |
| 388 | unsigned int offset, struct jit_ctx *ctx) |
| 389 | { |
| 390 | emit_instr(ctx, lb, reg, offset, base); |
| 391 | } |
| 392 | |
| 393 | static inline void emit_half_load(unsigned int reg, unsigned int base, |
| 394 | unsigned int offset, struct jit_ctx *ctx) |
| 395 | { |
| 396 | emit_instr(ctx, lh, reg, offset, base); |
| 397 | } |
| 398 | |
| 399 | static inline void emit_mul(unsigned int dst, unsigned int src1, |
| 400 | unsigned int src2, struct jit_ctx *ctx) |
| 401 | { |
| 402 | emit_instr(ctx, mul, dst, src1, src2); |
| 403 | } |
| 404 | |
| 405 | static inline void emit_div(unsigned int dst, unsigned int src, |
| 406 | struct jit_ctx *ctx) |
| 407 | { |
| 408 | if (ctx->target != NULL) { |
| 409 | u32 *p = &ctx->target[ctx->idx]; |
| 410 | uasm_i_divu(&p, dst, src); |
| 411 | p = &ctx->target[ctx->idx + 1]; |
Markos Chandras | 35a8e16 | 2014-06-23 10:38:47 +0100 | [diff] [blame] | 412 | uasm_i_mflo(&p, dst); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 413 | } |
| 414 | ctx->idx += 2; /* 2 insts */ |
| 415 | } |
| 416 | |
| 417 | static inline void emit_mod(unsigned int dst, unsigned int src, |
| 418 | struct jit_ctx *ctx) |
| 419 | { |
| 420 | if (ctx->target != NULL) { |
| 421 | u32 *p = &ctx->target[ctx->idx]; |
| 422 | uasm_i_divu(&p, dst, src); |
| 423 | p = &ctx->target[ctx->idx + 1]; |
Denis Kirjanov | 2e46477 | 2014-12-01 12:57:02 +0300 | [diff] [blame] | 424 | uasm_i_mfhi(&p, dst); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 425 | } |
| 426 | ctx->idx += 2; /* 2 insts */ |
| 427 | } |
| 428 | |
| 429 | static inline void emit_dsll(unsigned int dst, unsigned int src, |
| 430 | unsigned int sa, struct jit_ctx *ctx) |
| 431 | { |
| 432 | emit_instr(ctx, dsll, dst, src, sa); |
| 433 | } |
| 434 | |
| 435 | static inline void emit_dsrl32(unsigned int dst, unsigned int src, |
| 436 | unsigned int sa, struct jit_ctx *ctx) |
| 437 | { |
| 438 | emit_instr(ctx, dsrl32, dst, src, sa); |
| 439 | } |
| 440 | |
| 441 | static inline void emit_wsbh(unsigned int dst, unsigned int src, |
| 442 | struct jit_ctx *ctx) |
| 443 | { |
| 444 | emit_instr(ctx, wsbh, dst, src); |
| 445 | } |
| 446 | |
Markos Chandras | b6a14a9 | 2014-06-25 09:39:38 +0100 | [diff] [blame] | 447 | /* load pointer to register */ |
| 448 | static inline void emit_load_ptr(unsigned int dst, unsigned int src, |
| 449 | int imm, struct jit_ctx *ctx) |
| 450 | { |
| 451 | /* src contains the base addr of the 32/64-pointer */ |
Markos Chandras | 39bcb79 | 2014-07-23 10:00:09 +0100 | [diff] [blame] | 452 | emit_long_instr(ctx, LW, dst, imm, src); |
Markos Chandras | b6a14a9 | 2014-06-25 09:39:38 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 455 | /* load a function pointer to register */ |
| 456 | static inline void emit_load_func(unsigned int reg, ptr imm, |
| 457 | struct jit_ctx *ctx) |
| 458 | { |
| 459 | if (config_enabled(CONFIG_64BIT)) { |
| 460 | /* At this point imm is always 64-bit */ |
| 461 | emit_load_imm(r_tmp, (u64)imm >> 32, ctx); |
| 462 | emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */ |
| 463 | emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx); |
| 464 | emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */ |
| 465 | emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx); |
| 466 | } else { |
| 467 | emit_load_imm(reg, imm, ctx); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /* Move to real MIPS register */ |
| 472 | static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx) |
| 473 | { |
Markos Chandras | 39bcb79 | 2014-07-23 10:00:09 +0100 | [diff] [blame] | 474 | emit_long_instr(ctx, ADDU, dst, src, r_zero); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* Move to JIT (32-bit) register */ |
| 478 | static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx) |
| 479 | { |
| 480 | emit_addu(dst, src, r_zero, ctx); |
| 481 | } |
| 482 | |
| 483 | /* Compute the immediate value for PC-relative branches. */ |
| 484 | static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx) |
| 485 | { |
| 486 | if (ctx->target == NULL) |
| 487 | return 0; |
| 488 | |
| 489 | /* |
| 490 | * We want a pc-relative branch. We only do forward branches |
| 491 | * so tgt is always after pc. tgt is the instruction offset |
| 492 | * we want to jump to. |
| 493 | |
| 494 | * Branch on MIPS: |
| 495 | * I: target_offset <- sign_extend(offset) |
| 496 | * I+1: PC += target_offset (delay slot) |
| 497 | * |
| 498 | * ctx->idx currently points to the branch instruction |
| 499 | * but the offset is added to the delay slot so we need |
| 500 | * to subtract 4. |
| 501 | */ |
| 502 | return ctx->offsets[tgt] - |
| 503 | (ctx->idx * 4 - ctx->prologue_bytes) - 4; |
| 504 | } |
| 505 | |
| 506 | static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2, |
| 507 | unsigned int imm, struct jit_ctx *ctx) |
| 508 | { |
| 509 | if (ctx->target != NULL) { |
| 510 | u32 *p = &ctx->target[ctx->idx]; |
| 511 | |
| 512 | switch (cond) { |
| 513 | case MIPS_COND_EQ: |
| 514 | uasm_i_beq(&p, reg1, reg2, imm); |
| 515 | break; |
| 516 | case MIPS_COND_NE: |
| 517 | uasm_i_bne(&p, reg1, reg2, imm); |
| 518 | break; |
| 519 | case MIPS_COND_ALL: |
| 520 | uasm_i_b(&p, imm); |
| 521 | break; |
| 522 | default: |
| 523 | pr_warn("%s: Unhandled branch conditional: %d\n", |
| 524 | __func__, cond); |
| 525 | } |
| 526 | } |
| 527 | ctx->idx++; |
| 528 | } |
| 529 | |
| 530 | static inline void emit_b(unsigned int imm, struct jit_ctx *ctx) |
| 531 | { |
| 532 | emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx); |
| 533 | } |
| 534 | |
| 535 | static inline void emit_jalr(unsigned int link, unsigned int reg, |
| 536 | struct jit_ctx *ctx) |
| 537 | { |
| 538 | emit_instr(ctx, jalr, link, reg); |
| 539 | } |
| 540 | |
| 541 | static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx) |
| 542 | { |
| 543 | emit_instr(ctx, jr, reg); |
| 544 | } |
| 545 | |
| 546 | static inline u16 align_sp(unsigned int num) |
| 547 | { |
| 548 | /* Double word alignment for 32-bit, quadword for 64-bit */ |
| 549 | unsigned int align = config_enabled(CONFIG_64BIT) ? 16 : 8; |
| 550 | num = (num + (align - 1)) & -align; |
| 551 | return num; |
| 552 | } |
| 553 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 554 | static bool is_load_to_a(u16 inst) |
| 555 | { |
| 556 | switch (inst) { |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 557 | case BPF_LD | BPF_W | BPF_LEN: |
| 558 | case BPF_LD | BPF_W | BPF_ABS: |
| 559 | case BPF_LD | BPF_H | BPF_ABS: |
| 560 | case BPF_LD | BPF_B | BPF_ABS: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 561 | return true; |
| 562 | default: |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset) |
| 568 | { |
| 569 | int i = 0, real_off = 0; |
| 570 | u32 sflags, tmp_flags; |
| 571 | |
| 572 | /* Adjust the stack pointer */ |
| 573 | emit_stack_offset(-align_sp(offset), ctx); |
| 574 | |
| 575 | if (ctx->flags & SEEN_CALL) { |
| 576 | /* Argument save area */ |
| 577 | if (config_enabled(CONFIG_64BIT)) |
| 578 | /* Bottom of current frame */ |
| 579 | real_off = align_sp(offset) - RSIZE; |
| 580 | else |
| 581 | /* Top of previous frame */ |
| 582 | real_off = align_sp(offset) + RSIZE; |
| 583 | emit_store_stack_reg(MIPS_R_A0, r_sp, real_off, ctx); |
| 584 | emit_store_stack_reg(MIPS_R_A1, r_sp, real_off + RSIZE, ctx); |
| 585 | |
| 586 | real_off = 0; |
| 587 | } |
| 588 | |
| 589 | tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT; |
| 590 | /* sflags is essentially a bitmap */ |
| 591 | while (tmp_flags) { |
| 592 | if ((sflags >> i) & 0x1) { |
| 593 | emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off, |
| 594 | ctx); |
| 595 | real_off += RSIZE; |
| 596 | } |
| 597 | i++; |
| 598 | tmp_flags >>= 1; |
| 599 | } |
| 600 | |
| 601 | /* save return address */ |
| 602 | if (ctx->flags & SEEN_CALL) { |
| 603 | emit_store_stack_reg(r_ra, r_sp, real_off, ctx); |
| 604 | real_off += RSIZE; |
| 605 | } |
| 606 | |
| 607 | /* Setup r_M leaving the alignment gap if necessary */ |
| 608 | if (ctx->flags & SEEN_MEM) { |
| 609 | if (real_off % (RSIZE * 2)) |
| 610 | real_off += RSIZE; |
Markos Chandras | 39bcb79 | 2014-07-23 10:00:09 +0100 | [diff] [blame] | 611 | emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | static void restore_bpf_jit_regs(struct jit_ctx *ctx, |
| 616 | unsigned int offset) |
| 617 | { |
| 618 | int i, real_off = 0; |
| 619 | u32 sflags, tmp_flags; |
| 620 | |
| 621 | if (ctx->flags & SEEN_CALL) { |
| 622 | if (config_enabled(CONFIG_64BIT)) |
| 623 | /* Bottom of current frame */ |
| 624 | real_off = align_sp(offset) - RSIZE; |
| 625 | else |
| 626 | /* Top of previous frame */ |
| 627 | real_off = align_sp(offset) + RSIZE; |
| 628 | emit_load_stack_reg(MIPS_R_A0, r_sp, real_off, ctx); |
| 629 | emit_load_stack_reg(MIPS_R_A1, r_sp, real_off + RSIZE, ctx); |
| 630 | |
| 631 | real_off = 0; |
| 632 | } |
| 633 | |
| 634 | tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT; |
| 635 | /* sflags is a bitmap */ |
| 636 | i = 0; |
| 637 | while (tmp_flags) { |
| 638 | if ((sflags >> i) & 0x1) { |
| 639 | emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off, |
| 640 | ctx); |
| 641 | real_off += RSIZE; |
| 642 | } |
| 643 | i++; |
| 644 | tmp_flags >>= 1; |
| 645 | } |
| 646 | |
| 647 | /* restore return address */ |
| 648 | if (ctx->flags & SEEN_CALL) |
| 649 | emit_load_stack_reg(r_ra, r_sp, real_off, ctx); |
| 650 | |
| 651 | /* Restore the sp and discard the scrach memory */ |
| 652 | emit_stack_offset(align_sp(offset), ctx); |
| 653 | } |
| 654 | |
| 655 | static unsigned int get_stack_depth(struct jit_ctx *ctx) |
| 656 | { |
| 657 | int sp_off = 0; |
| 658 | |
| 659 | |
| 660 | /* How may s* regs do we need to preserved? */ |
| 661 | sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * RSIZE; |
| 662 | |
| 663 | if (ctx->flags & SEEN_MEM) |
| 664 | sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */ |
| 665 | |
| 666 | if (ctx->flags & SEEN_CALL) |
| 667 | /* |
| 668 | * The JIT code make calls to external functions using 2 |
| 669 | * arguments. Therefore, for o32 we don't need to allocate |
| 670 | * space because we don't care if the argumetns are lost |
| 671 | * across calls. We do need however to preserve incoming |
| 672 | * arguments but the space is already allocated for us by |
| 673 | * the caller. On the other hand, for n64, we need to allocate |
| 674 | * this space ourselves. We need to preserve $ra as well. |
| 675 | */ |
| 676 | sp_off += config_enabled(CONFIG_64BIT) ? |
| 677 | (ARGS_USED_BY_JIT + 1) * RSIZE : RSIZE; |
| 678 | |
Markos Chandras | 8833bc3 | 2015-06-04 11:56:13 +0100 | [diff] [blame] | 679 | return sp_off; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static void build_prologue(struct jit_ctx *ctx) |
| 683 | { |
| 684 | u16 first_inst = ctx->skf->insns[0].code; |
| 685 | int sp_off; |
| 686 | |
| 687 | /* Calculate the total offset for the stack pointer */ |
| 688 | sp_off = get_stack_depth(ctx); |
| 689 | save_bpf_jit_regs(ctx, sp_off); |
| 690 | |
| 691 | if (ctx->flags & SEEN_SKB) |
| 692 | emit_reg_move(r_skb, MIPS_R_A0, ctx); |
| 693 | |
Markos Chandras | e5bb48b | 2014-06-23 10:38:56 +0100 | [diff] [blame] | 694 | if (ctx->flags & SEEN_X) |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 695 | emit_jit_reg_move(r_X, r_zero, ctx); |
| 696 | |
| 697 | /* Do not leak kernel data to userspace */ |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 698 | if ((first_inst != (BPF_RET | BPF_K)) && !(is_load_to_a(first_inst))) |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 699 | emit_jit_reg_move(r_A, r_zero, ctx); |
| 700 | } |
| 701 | |
| 702 | static void build_epilogue(struct jit_ctx *ctx) |
| 703 | { |
| 704 | unsigned int sp_off; |
| 705 | |
| 706 | /* Calculate the total offset for the stack pointer */ |
| 707 | |
| 708 | sp_off = get_stack_depth(ctx); |
| 709 | restore_bpf_jit_regs(ctx, sp_off); |
| 710 | |
| 711 | /* Return */ |
| 712 | emit_jr(r_ra, ctx); |
| 713 | emit_nop(ctx); |
| 714 | } |
| 715 | |
| 716 | static u64 jit_get_skb_b(struct sk_buff *skb, unsigned offset) |
| 717 | { |
| 718 | u8 ret; |
| 719 | int err; |
| 720 | |
| 721 | err = skb_copy_bits(skb, offset, &ret, 1); |
| 722 | |
| 723 | return (u64)err << 32 | ret; |
| 724 | } |
| 725 | |
| 726 | static u64 jit_get_skb_h(struct sk_buff *skb, unsigned offset) |
| 727 | { |
| 728 | u16 ret; |
| 729 | int err; |
| 730 | |
| 731 | err = skb_copy_bits(skb, offset, &ret, 2); |
| 732 | |
| 733 | return (u64)err << 32 | ntohs(ret); |
| 734 | } |
| 735 | |
| 736 | static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset) |
| 737 | { |
| 738 | u32 ret; |
| 739 | int err; |
| 740 | |
| 741 | err = skb_copy_bits(skb, offset, &ret, 4); |
| 742 | |
| 743 | return (u64)err << 32 | ntohl(ret); |
| 744 | } |
| 745 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 746 | static int build_body(struct jit_ctx *ctx) |
| 747 | { |
| 748 | void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w}; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 749 | const struct bpf_prog *prog = ctx->skf; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 750 | const struct sock_filter *inst; |
| 751 | unsigned int i, off, load_order, condt; |
| 752 | u32 k, b_off __maybe_unused; |
| 753 | |
| 754 | for (i = 0; i < prog->len; i++) { |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 755 | u16 code; |
| 756 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 757 | inst = &(prog->insns[i]); |
| 758 | pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n", |
| 759 | __func__, inst->code, inst->jt, inst->jf, inst->k); |
| 760 | k = inst->k; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 761 | code = bpf_anc_helper(inst); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 762 | |
| 763 | if (ctx->target == NULL) |
| 764 | ctx->offsets[i] = ctx->idx * 4; |
| 765 | |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 766 | switch (code) { |
| 767 | case BPF_LD | BPF_IMM: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 768 | /* A <- k ==> li r_A, k */ |
| 769 | ctx->flags |= SEEN_A; |
| 770 | emit_load_imm(r_A, k, ctx); |
| 771 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 772 | case BPF_LD | BPF_W | BPF_LEN: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 773 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4); |
| 774 | /* A <- len ==> lw r_A, offset(skb) */ |
| 775 | ctx->flags |= SEEN_SKB | SEEN_A; |
| 776 | off = offsetof(struct sk_buff, len); |
| 777 | emit_load(r_A, r_skb, off, ctx); |
| 778 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 779 | case BPF_LD | BPF_MEM: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 780 | /* A <- M[k] ==> lw r_A, offset(M) */ |
| 781 | ctx->flags |= SEEN_MEM | SEEN_A; |
| 782 | emit_load(r_A, r_M, SCRATCH_OFF(k), ctx); |
| 783 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 784 | case BPF_LD | BPF_W | BPF_ABS: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 785 | /* A <- P[k:4] */ |
| 786 | load_order = 2; |
| 787 | goto load; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 788 | case BPF_LD | BPF_H | BPF_ABS: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 789 | /* A <- P[k:2] */ |
| 790 | load_order = 1; |
| 791 | goto load; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 792 | case BPF_LD | BPF_B | BPF_ABS: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 793 | /* A <- P[k:1] */ |
| 794 | load_order = 0; |
| 795 | load: |
Markos Chandras | 55393ee | 2014-06-23 10:38:48 +0100 | [diff] [blame] | 796 | /* the interpreter will deal with the negative K */ |
| 797 | if ((int)k < 0) |
| 798 | return -ENOTSUPP; |
| 799 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 800 | emit_load_imm(r_off, k, ctx); |
| 801 | load_common: |
Markos Chandras | 55393ee | 2014-06-23 10:38:48 +0100 | [diff] [blame] | 802 | /* |
| 803 | * We may got here from the indirect loads so |
| 804 | * return if offset is negative. |
| 805 | */ |
| 806 | emit_slt(r_s0, r_off, r_zero, ctx); |
| 807 | emit_bcond(MIPS_COND_NE, r_s0, r_zero, |
| 808 | b_imm(prog->len, ctx), ctx); |
| 809 | emit_reg_move(r_ret, r_zero, ctx); |
| 810 | |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 811 | ctx->flags |= SEEN_CALL | SEEN_OFF | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 812 | SEEN_SKB | SEEN_A; |
| 813 | |
| 814 | emit_load_func(r_s0, (ptr)load_func[load_order], |
| 815 | ctx); |
| 816 | emit_reg_move(MIPS_R_A0, r_skb, ctx); |
| 817 | emit_jalr(MIPS_R_RA, r_s0, ctx); |
| 818 | /* Load second argument to delay slot */ |
| 819 | emit_reg_move(MIPS_R_A1, r_off, ctx); |
| 820 | /* Check the error value */ |
| 821 | if (config_enabled(CONFIG_64BIT)) { |
| 822 | /* Get error code from the top 32-bits */ |
| 823 | emit_dsrl32(r_s0, r_val, 0, ctx); |
| 824 | /* Branch to 3 instructions ahead */ |
| 825 | emit_bcond(MIPS_COND_NE, r_s0, r_zero, 3 << 2, |
| 826 | ctx); |
| 827 | } else { |
| 828 | /* Branch to 3 instructions ahead */ |
| 829 | emit_bcond(MIPS_COND_NE, r_err, r_zero, 3 << 2, |
| 830 | ctx); |
| 831 | } |
| 832 | emit_nop(ctx); |
| 833 | /* We are good */ |
| 834 | emit_b(b_imm(i + 1, ctx), ctx); |
| 835 | emit_jit_reg_move(r_A, r_val, ctx); |
| 836 | /* Return with error */ |
| 837 | emit_b(b_imm(prog->len, ctx), ctx); |
| 838 | emit_reg_move(r_ret, r_zero, ctx); |
| 839 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 840 | case BPF_LD | BPF_W | BPF_IND: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 841 | /* A <- P[X + k:4] */ |
| 842 | load_order = 2; |
| 843 | goto load_ind; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 844 | case BPF_LD | BPF_H | BPF_IND: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 845 | /* A <- P[X + k:2] */ |
| 846 | load_order = 1; |
| 847 | goto load_ind; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 848 | case BPF_LD | BPF_B | BPF_IND: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 849 | /* A <- P[X + k:1] */ |
| 850 | load_order = 0; |
| 851 | load_ind: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 852 | ctx->flags |= SEEN_OFF | SEEN_X; |
| 853 | emit_addiu(r_off, r_X, k, ctx); |
| 854 | goto load_common; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 855 | case BPF_LDX | BPF_IMM: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 856 | /* X <- k */ |
| 857 | ctx->flags |= SEEN_X; |
| 858 | emit_load_imm(r_X, k, ctx); |
| 859 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 860 | case BPF_LDX | BPF_MEM: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 861 | /* X <- M[k] */ |
| 862 | ctx->flags |= SEEN_X | SEEN_MEM; |
| 863 | emit_load(r_X, r_M, SCRATCH_OFF(k), ctx); |
| 864 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 865 | case BPF_LDX | BPF_W | BPF_LEN: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 866 | /* X <- len */ |
| 867 | ctx->flags |= SEEN_X | SEEN_SKB; |
| 868 | off = offsetof(struct sk_buff, len); |
| 869 | emit_load(r_X, r_skb, off, ctx); |
| 870 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 871 | case BPF_LDX | BPF_B | BPF_MSH: |
Markos Chandras | 55393ee | 2014-06-23 10:38:48 +0100 | [diff] [blame] | 872 | /* the interpreter will deal with the negative K */ |
| 873 | if ((int)k < 0) |
| 874 | return -ENOTSUPP; |
| 875 | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 876 | /* X <- 4 * (P[k:1] & 0xf) */ |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 877 | ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 878 | /* Load offset to a1 */ |
| 879 | emit_load_func(r_s0, (ptr)jit_get_skb_b, ctx); |
| 880 | /* |
| 881 | * This may emit two instructions so it may not fit |
| 882 | * in the delay slot. So use a0 in the delay slot. |
| 883 | */ |
| 884 | emit_load_imm(MIPS_R_A1, k, ctx); |
| 885 | emit_jalr(MIPS_R_RA, r_s0, ctx); |
| 886 | emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */ |
| 887 | /* Check the error value */ |
| 888 | if (config_enabled(CONFIG_64BIT)) { |
| 889 | /* Top 32-bits of $v0 on 64-bit */ |
| 890 | emit_dsrl32(r_s0, r_val, 0, ctx); |
| 891 | emit_bcond(MIPS_COND_NE, r_s0, r_zero, |
| 892 | 3 << 2, ctx); |
| 893 | } else { |
| 894 | emit_bcond(MIPS_COND_NE, r_err, r_zero, |
| 895 | 3 << 2, ctx); |
| 896 | } |
| 897 | /* No need for delay slot */ |
| 898 | /* We are good */ |
| 899 | /* X <- P[1:K] & 0xf */ |
| 900 | emit_andi(r_X, r_val, 0xf, ctx); |
| 901 | /* X << 2 */ |
| 902 | emit_b(b_imm(i + 1, ctx), ctx); |
| 903 | emit_sll(r_X, r_X, 2, ctx); /* delay slot */ |
| 904 | /* Return with error */ |
| 905 | emit_b(b_imm(prog->len, ctx), ctx); |
| 906 | emit_load_imm(r_ret, 0, ctx); /* delay slot */ |
| 907 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 908 | case BPF_ST: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 909 | /* M[k] <- A */ |
| 910 | ctx->flags |= SEEN_MEM | SEEN_A; |
| 911 | emit_store(r_A, r_M, SCRATCH_OFF(k), ctx); |
| 912 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 913 | case BPF_STX: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 914 | /* M[k] <- X */ |
| 915 | ctx->flags |= SEEN_MEM | SEEN_X; |
| 916 | emit_store(r_X, r_M, SCRATCH_OFF(k), ctx); |
| 917 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 918 | case BPF_ALU | BPF_ADD | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 919 | /* A += K */ |
| 920 | ctx->flags |= SEEN_A; |
| 921 | emit_addiu(r_A, r_A, k, ctx); |
| 922 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 923 | case BPF_ALU | BPF_ADD | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 924 | /* A += X */ |
| 925 | ctx->flags |= SEEN_A | SEEN_X; |
| 926 | emit_addu(r_A, r_A, r_X, ctx); |
| 927 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 928 | case BPF_ALU | BPF_SUB | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 929 | /* A -= K */ |
| 930 | ctx->flags |= SEEN_A; |
| 931 | emit_addiu(r_A, r_A, -k, ctx); |
| 932 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 933 | case BPF_ALU | BPF_SUB | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 934 | /* A -= X */ |
| 935 | ctx->flags |= SEEN_A | SEEN_X; |
| 936 | emit_subu(r_A, r_A, r_X, ctx); |
| 937 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 938 | case BPF_ALU | BPF_MUL | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 939 | /* A *= K */ |
| 940 | /* Load K to scratch register before MUL */ |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 941 | ctx->flags |= SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 942 | emit_load_imm(r_s0, k, ctx); |
| 943 | emit_mul(r_A, r_A, r_s0, ctx); |
| 944 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 945 | case BPF_ALU | BPF_MUL | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 946 | /* A *= X */ |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 947 | ctx->flags |= SEEN_A | SEEN_X; |
| 948 | emit_mul(r_A, r_A, r_X, ctx); |
| 949 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 950 | case BPF_ALU | BPF_DIV | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 951 | /* A /= k */ |
| 952 | if (k == 1) |
| 953 | break; |
| 954 | if (optimize_div(&k)) { |
| 955 | ctx->flags |= SEEN_A; |
| 956 | emit_srl(r_A, r_A, k, ctx); |
| 957 | break; |
| 958 | } |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 959 | ctx->flags |= SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 960 | emit_load_imm(r_s0, k, ctx); |
| 961 | emit_div(r_A, r_s0, ctx); |
| 962 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 963 | case BPF_ALU | BPF_MOD | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 964 | /* A %= k */ |
Denis Kirjanov | 2e46477 | 2014-12-01 12:57:02 +0300 | [diff] [blame] | 965 | if (k == 1) { |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 966 | ctx->flags |= SEEN_A; |
| 967 | emit_jit_reg_move(r_A, r_zero, ctx); |
| 968 | } else { |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 969 | ctx->flags |= SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 970 | emit_load_imm(r_s0, k, ctx); |
| 971 | emit_mod(r_A, r_s0, ctx); |
| 972 | } |
| 973 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 974 | case BPF_ALU | BPF_DIV | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 975 | /* A /= X */ |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 976 | ctx->flags |= SEEN_X | SEEN_A; |
| 977 | /* Check if r_X is zero */ |
| 978 | emit_bcond(MIPS_COND_EQ, r_X, r_zero, |
| 979 | b_imm(prog->len, ctx), ctx); |
| 980 | emit_load_imm(r_val, 0, ctx); /* delay slot */ |
| 981 | emit_div(r_A, r_X, ctx); |
| 982 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 983 | case BPF_ALU | BPF_MOD | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 984 | /* A %= X */ |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 985 | ctx->flags |= SEEN_X | SEEN_A; |
| 986 | /* Check if r_X is zero */ |
| 987 | emit_bcond(MIPS_COND_EQ, r_X, r_zero, |
| 988 | b_imm(prog->len, ctx), ctx); |
| 989 | emit_load_imm(r_val, 0, ctx); /* delay slot */ |
| 990 | emit_mod(r_A, r_X, ctx); |
| 991 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 992 | case BPF_ALU | BPF_OR | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 993 | /* A |= K */ |
| 994 | ctx->flags |= SEEN_A; |
| 995 | emit_ori(r_A, r_A, k, ctx); |
| 996 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 997 | case BPF_ALU | BPF_OR | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 998 | /* A |= X */ |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 999 | ctx->flags |= SEEN_A; |
| 1000 | emit_ori(r_A, r_A, r_X, ctx); |
| 1001 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1002 | case BPF_ALU | BPF_XOR | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1003 | /* A ^= k */ |
| 1004 | ctx->flags |= SEEN_A; |
| 1005 | emit_xori(r_A, r_A, k, ctx); |
| 1006 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1007 | case BPF_ANC | SKF_AD_ALU_XOR_X: |
| 1008 | case BPF_ALU | BPF_XOR | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1009 | /* A ^= X */ |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1010 | ctx->flags |= SEEN_A; |
| 1011 | emit_xor(r_A, r_A, r_X, ctx); |
| 1012 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1013 | case BPF_ALU | BPF_AND | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1014 | /* A &= K */ |
| 1015 | ctx->flags |= SEEN_A; |
| 1016 | emit_andi(r_A, r_A, k, ctx); |
| 1017 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1018 | case BPF_ALU | BPF_AND | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1019 | /* A &= X */ |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1020 | ctx->flags |= SEEN_A | SEEN_X; |
| 1021 | emit_and(r_A, r_A, r_X, ctx); |
| 1022 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1023 | case BPF_ALU | BPF_LSH | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1024 | /* A <<= K */ |
| 1025 | ctx->flags |= SEEN_A; |
| 1026 | emit_sll(r_A, r_A, k, ctx); |
| 1027 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1028 | case BPF_ALU | BPF_LSH | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1029 | /* A <<= X */ |
| 1030 | ctx->flags |= SEEN_A | SEEN_X; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1031 | emit_sllv(r_A, r_A, r_X, ctx); |
| 1032 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1033 | case BPF_ALU | BPF_RSH | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1034 | /* A >>= K */ |
| 1035 | ctx->flags |= SEEN_A; |
| 1036 | emit_srl(r_A, r_A, k, ctx); |
| 1037 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1038 | case BPF_ALU | BPF_RSH | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1039 | ctx->flags |= SEEN_A | SEEN_X; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1040 | emit_srlv(r_A, r_A, r_X, ctx); |
| 1041 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1042 | case BPF_ALU | BPF_NEG: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1043 | /* A = -A */ |
| 1044 | ctx->flags |= SEEN_A; |
| 1045 | emit_neg(r_A, ctx); |
| 1046 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1047 | case BPF_JMP | BPF_JA: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1048 | /* pc += K */ |
| 1049 | emit_b(b_imm(i + k + 1, ctx), ctx); |
| 1050 | emit_nop(ctx); |
| 1051 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1052 | case BPF_JMP | BPF_JEQ | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1053 | /* pc += ( A == K ) ? pc->jt : pc->jf */ |
| 1054 | condt = MIPS_COND_EQ | MIPS_COND_K; |
| 1055 | goto jmp_cmp; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1056 | case BPF_JMP | BPF_JEQ | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1057 | ctx->flags |= SEEN_X; |
| 1058 | /* pc += ( A == X ) ? pc->jt : pc->jf */ |
| 1059 | condt = MIPS_COND_EQ | MIPS_COND_X; |
| 1060 | goto jmp_cmp; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1061 | case BPF_JMP | BPF_JGE | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1062 | /* pc += ( A >= K ) ? pc->jt : pc->jf */ |
| 1063 | condt = MIPS_COND_GE | MIPS_COND_K; |
| 1064 | goto jmp_cmp; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1065 | case BPF_JMP | BPF_JGE | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1066 | ctx->flags |= SEEN_X; |
| 1067 | /* pc += ( A >= X ) ? pc->jt : pc->jf */ |
| 1068 | condt = MIPS_COND_GE | MIPS_COND_X; |
| 1069 | goto jmp_cmp; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1070 | case BPF_JMP | BPF_JGT | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1071 | /* pc += ( A > K ) ? pc->jt : pc->jf */ |
| 1072 | condt = MIPS_COND_GT | MIPS_COND_K; |
| 1073 | goto jmp_cmp; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1074 | case BPF_JMP | BPF_JGT | BPF_X: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1075 | ctx->flags |= SEEN_X; |
| 1076 | /* pc += ( A > X ) ? pc->jt : pc->jf */ |
| 1077 | condt = MIPS_COND_GT | MIPS_COND_X; |
| 1078 | jmp_cmp: |
| 1079 | /* Greater or Equal */ |
| 1080 | if ((condt & MIPS_COND_GE) || |
| 1081 | (condt & MIPS_COND_GT)) { |
| 1082 | if (condt & MIPS_COND_K) { /* K */ |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 1083 | ctx->flags |= SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1084 | emit_sltiu(r_s0, r_A, k, ctx); |
| 1085 | } else { /* X */ |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 1086 | ctx->flags |= SEEN_A | |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1087 | SEEN_X; |
| 1088 | emit_sltu(r_s0, r_A, r_X, ctx); |
| 1089 | } |
| 1090 | /* A < (K|X) ? r_scrach = 1 */ |
| 1091 | b_off = b_imm(i + inst->jf + 1, ctx); |
Markos Chandras | 1ab24a4 | 2014-06-23 10:38:51 +0100 | [diff] [blame] | 1092 | emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1093 | ctx); |
| 1094 | emit_nop(ctx); |
| 1095 | /* A > (K|X) ? scratch = 0 */ |
| 1096 | if (condt & MIPS_COND_GT) { |
| 1097 | /* Checking for equality */ |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 1098 | ctx->flags |= SEEN_A | SEEN_X; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1099 | if (condt & MIPS_COND_K) |
| 1100 | emit_load_imm(r_s0, k, ctx); |
| 1101 | else |
| 1102 | emit_jit_reg_move(r_s0, r_X, |
| 1103 | ctx); |
| 1104 | b_off = b_imm(i + inst->jf + 1, ctx); |
| 1105 | emit_bcond(MIPS_COND_EQ, r_A, r_s0, |
| 1106 | b_off, ctx); |
| 1107 | emit_nop(ctx); |
| 1108 | /* Finally, A > K|X */ |
| 1109 | b_off = b_imm(i + inst->jt + 1, ctx); |
| 1110 | emit_b(b_off, ctx); |
| 1111 | emit_nop(ctx); |
| 1112 | } else { |
| 1113 | /* A >= (K|X) so jump */ |
| 1114 | b_off = b_imm(i + inst->jt + 1, ctx); |
| 1115 | emit_b(b_off, ctx); |
| 1116 | emit_nop(ctx); |
| 1117 | } |
| 1118 | } else { |
| 1119 | /* A == K|X */ |
| 1120 | if (condt & MIPS_COND_K) { /* K */ |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 1121 | ctx->flags |= SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1122 | emit_load_imm(r_s0, k, ctx); |
| 1123 | /* jump true */ |
| 1124 | b_off = b_imm(i + inst->jt + 1, ctx); |
| 1125 | emit_bcond(MIPS_COND_EQ, r_A, r_s0, |
| 1126 | b_off, ctx); |
| 1127 | emit_nop(ctx); |
| 1128 | /* jump false */ |
| 1129 | b_off = b_imm(i + inst->jf + 1, |
| 1130 | ctx); |
| 1131 | emit_bcond(MIPS_COND_NE, r_A, r_s0, |
| 1132 | b_off, ctx); |
| 1133 | emit_nop(ctx); |
| 1134 | } else { /* X */ |
| 1135 | /* jump true */ |
| 1136 | ctx->flags |= SEEN_A | SEEN_X; |
| 1137 | b_off = b_imm(i + inst->jt + 1, |
| 1138 | ctx); |
| 1139 | emit_bcond(MIPS_COND_EQ, r_A, r_X, |
| 1140 | b_off, ctx); |
| 1141 | emit_nop(ctx); |
| 1142 | /* jump false */ |
| 1143 | b_off = b_imm(i + inst->jf + 1, ctx); |
| 1144 | emit_bcond(MIPS_COND_NE, r_A, r_X, |
| 1145 | b_off, ctx); |
| 1146 | emit_nop(ctx); |
| 1147 | } |
| 1148 | } |
| 1149 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1150 | case BPF_JMP | BPF_JSET | BPF_K: |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 1151 | ctx->flags |= SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1152 | /* pc += (A & K) ? pc -> jt : pc -> jf */ |
| 1153 | emit_load_imm(r_s1, k, ctx); |
| 1154 | emit_and(r_s0, r_A, r_s1, ctx); |
| 1155 | /* jump true */ |
| 1156 | b_off = b_imm(i + inst->jt + 1, ctx); |
| 1157 | emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx); |
| 1158 | emit_nop(ctx); |
| 1159 | /* jump false */ |
| 1160 | b_off = b_imm(i + inst->jf + 1, ctx); |
| 1161 | emit_b(b_off, ctx); |
| 1162 | emit_nop(ctx); |
| 1163 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1164 | case BPF_JMP | BPF_JSET | BPF_X: |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 1165 | ctx->flags |= SEEN_X | SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1166 | /* pc += (A & X) ? pc -> jt : pc -> jf */ |
| 1167 | emit_and(r_s0, r_A, r_X, ctx); |
| 1168 | /* jump true */ |
| 1169 | b_off = b_imm(i + inst->jt + 1, ctx); |
| 1170 | emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx); |
| 1171 | emit_nop(ctx); |
| 1172 | /* jump false */ |
| 1173 | b_off = b_imm(i + inst->jf + 1, ctx); |
| 1174 | emit_b(b_off, ctx); |
| 1175 | emit_nop(ctx); |
| 1176 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1177 | case BPF_RET | BPF_A: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1178 | ctx->flags |= SEEN_A; |
| 1179 | if (i != prog->len - 1) |
| 1180 | /* |
| 1181 | * If this is not the last instruction |
| 1182 | * then jump to the epilogue |
| 1183 | */ |
| 1184 | emit_b(b_imm(prog->len, ctx), ctx); |
| 1185 | emit_reg_move(r_ret, r_A, ctx); /* delay slot */ |
| 1186 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1187 | case BPF_RET | BPF_K: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1188 | /* |
| 1189 | * It can emit two instructions so it does not fit on |
| 1190 | * the delay slot. |
| 1191 | */ |
| 1192 | emit_load_imm(r_ret, k, ctx); |
| 1193 | if (i != prog->len - 1) { |
| 1194 | /* |
| 1195 | * If this is not the last instruction |
| 1196 | * then jump to the epilogue |
| 1197 | */ |
| 1198 | emit_b(b_imm(prog->len, ctx), ctx); |
| 1199 | emit_nop(ctx); |
| 1200 | } |
| 1201 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1202 | case BPF_MISC | BPF_TAX: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1203 | /* X = A */ |
| 1204 | ctx->flags |= SEEN_X | SEEN_A; |
| 1205 | emit_jit_reg_move(r_X, r_A, ctx); |
| 1206 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1207 | case BPF_MISC | BPF_TXA: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1208 | /* A = X */ |
| 1209 | ctx->flags |= SEEN_A | SEEN_X; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1210 | emit_jit_reg_move(r_A, r_X, ctx); |
| 1211 | break; |
| 1212 | /* AUX */ |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1213 | case BPF_ANC | SKF_AD_PROTOCOL: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1214 | /* A = ntohs(skb->protocol */ |
| 1215 | ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A; |
| 1216 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, |
| 1217 | protocol) != 2); |
| 1218 | off = offsetof(struct sk_buff, protocol); |
| 1219 | emit_half_load(r_A, r_skb, off, ctx); |
| 1220 | #ifdef CONFIG_CPU_LITTLE_ENDIAN |
| 1221 | /* This needs little endian fixup */ |
Chen Jie | 3c09bae | 2014-08-15 16:56:58 +0800 | [diff] [blame] | 1222 | if (cpu_has_wsbh) { |
Ralf Baechle | b4f16c9 | 2014-06-03 13:05:55 +0200 | [diff] [blame] | 1223 | /* R2 and later have the wsbh instruction */ |
| 1224 | emit_wsbh(r_A, r_A, ctx); |
| 1225 | } else { |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1226 | /* Get first byte */ |
| 1227 | emit_andi(r_tmp_imm, r_A, 0xff, ctx); |
| 1228 | /* Shift it */ |
| 1229 | emit_sll(r_tmp, r_tmp_imm, 8, ctx); |
| 1230 | /* Get second byte */ |
| 1231 | emit_srl(r_tmp_imm, r_A, 8, ctx); |
| 1232 | emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx); |
| 1233 | /* Put everyting together in r_A */ |
| 1234 | emit_or(r_A, r_tmp, r_tmp_imm, ctx); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1235 | } |
| 1236 | #endif |
| 1237 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1238 | case BPF_ANC | SKF_AD_CPU: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1239 | ctx->flags |= SEEN_A | SEEN_OFF; |
| 1240 | /* A = current_thread_info()->cpu */ |
| 1241 | BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, |
| 1242 | cpu) != 4); |
| 1243 | off = offsetof(struct thread_info, cpu); |
| 1244 | /* $28/gp points to the thread_info struct */ |
| 1245 | emit_load(r_A, 28, off, ctx); |
| 1246 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1247 | case BPF_ANC | SKF_AD_IFINDEX: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1248 | /* A = skb->dev->ifindex */ |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 1249 | ctx->flags |= SEEN_SKB | SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1250 | off = offsetof(struct sk_buff, dev); |
Markos Chandras | b6a14a9 | 2014-06-25 09:39:38 +0100 | [diff] [blame] | 1251 | /* Load *dev pointer */ |
| 1252 | emit_load_ptr(r_s0, r_skb, off, ctx); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1253 | /* error (0) in the delay slot */ |
| 1254 | emit_bcond(MIPS_COND_EQ, r_s0, r_zero, |
| 1255 | b_imm(prog->len, ctx), ctx); |
| 1256 | emit_reg_move(r_ret, r_zero, ctx); |
| 1257 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, |
| 1258 | ifindex) != 4); |
| 1259 | off = offsetof(struct net_device, ifindex); |
| 1260 | emit_load(r_A, r_s0, off, ctx); |
| 1261 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1262 | case BPF_ANC | SKF_AD_MARK: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1263 | ctx->flags |= SEEN_SKB | SEEN_A; |
| 1264 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); |
| 1265 | off = offsetof(struct sk_buff, mark); |
| 1266 | emit_load(r_A, r_skb, off, ctx); |
| 1267 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1268 | case BPF_ANC | SKF_AD_RXHASH: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1269 | ctx->flags |= SEEN_SKB | SEEN_A; |
| 1270 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); |
| 1271 | off = offsetof(struct sk_buff, hash); |
| 1272 | emit_load(r_A, r_skb, off, ctx); |
| 1273 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1274 | case BPF_ANC | SKF_AD_VLAN_TAG: |
| 1275 | case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT: |
Markos Chandras | ad152bd | 2015-06-04 11:56:11 +0100 | [diff] [blame^] | 1276 | ctx->flags |= SEEN_SKB | SEEN_A; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1277 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, |
| 1278 | vlan_tci) != 2); |
| 1279 | off = offsetof(struct sk_buff, vlan_tci); |
| 1280 | emit_half_load(r_s0, r_skb, off, ctx); |
Markos Chandras | 91a41d7 | 2014-06-23 10:38:53 +0100 | [diff] [blame] | 1281 | if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) { |
Markos Chandras | 6e86c59 | 2014-06-23 10:38:52 +0100 | [diff] [blame] | 1282 | emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx); |
Markos Chandras | 91a41d7 | 2014-06-23 10:38:53 +0100 | [diff] [blame] | 1283 | } else { |
Markos Chandras | 9ee1606 | 2014-06-23 10:38:49 +0100 | [diff] [blame] | 1284 | emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx); |
Markos Chandras | 91a41d7 | 2014-06-23 10:38:53 +0100 | [diff] [blame] | 1285 | /* return 1 if present */ |
| 1286 | emit_sltu(r_A, r_zero, r_A, ctx); |
| 1287 | } |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1288 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1289 | case BPF_ANC | SKF_AD_PKTTYPE: |
Markos Chandras | 9eebfe4 | 2014-06-23 10:38:50 +0100 | [diff] [blame] | 1290 | ctx->flags |= SEEN_SKB; |
| 1291 | |
Hannes Frederic Sowa | 233577a | 2014-09-12 14:04:43 +0200 | [diff] [blame] | 1292 | emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1293 | /* Keep only the last 3 bits */ |
| 1294 | emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx); |
Markos Chandras | b4fe0ec | 2014-06-23 10:38:58 +0100 | [diff] [blame] | 1295 | #ifdef __BIG_ENDIAN_BITFIELD |
| 1296 | /* Get the actual packet type to the lower 3 bits */ |
| 1297 | emit_srl(r_A, r_A, 5, ctx); |
| 1298 | #endif |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1299 | break; |
Daniel Borkmann | a83d081 | 2014-06-17 12:16:18 +0200 | [diff] [blame] | 1300 | case BPF_ANC | SKF_AD_QUEUE: |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1301 | ctx->flags |= SEEN_SKB | SEEN_A; |
| 1302 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, |
| 1303 | queue_mapping) != 2); |
| 1304 | BUILD_BUG_ON(offsetof(struct sk_buff, |
| 1305 | queue_mapping) > 0xff); |
| 1306 | off = offsetof(struct sk_buff, queue_mapping); |
| 1307 | emit_half_load(r_A, r_skb, off, ctx); |
| 1308 | break; |
| 1309 | default: |
Markos Chandras | 78b95b6 | 2014-06-23 10:38:54 +0100 | [diff] [blame] | 1310 | pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__, |
| 1311 | inst->code); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1312 | return -1; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* compute offsets only during the first pass */ |
| 1317 | if (ctx->target == NULL) |
| 1318 | ctx->offsets[i] = ctx->idx * 4; |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | int bpf_jit_enable __read_mostly; |
| 1324 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1325 | void bpf_jit_compile(struct bpf_prog *fp) |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1326 | { |
| 1327 | struct jit_ctx ctx; |
| 1328 | unsigned int alloc_size, tmp_idx; |
| 1329 | |
| 1330 | if (!bpf_jit_enable) |
| 1331 | return; |
| 1332 | |
| 1333 | memset(&ctx, 0, sizeof(ctx)); |
| 1334 | |
| 1335 | ctx.offsets = kcalloc(fp->len, sizeof(*ctx.offsets), GFP_KERNEL); |
| 1336 | if (ctx.offsets == NULL) |
| 1337 | return; |
| 1338 | |
| 1339 | ctx.skf = fp; |
| 1340 | |
| 1341 | if (build_body(&ctx)) |
| 1342 | goto out; |
| 1343 | |
| 1344 | tmp_idx = ctx.idx; |
| 1345 | build_prologue(&ctx); |
| 1346 | ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4; |
| 1347 | /* just to complete the ctx.idx count */ |
| 1348 | build_epilogue(&ctx); |
| 1349 | |
| 1350 | alloc_size = 4 * ctx.idx; |
| 1351 | ctx.target = module_alloc(alloc_size); |
| 1352 | if (ctx.target == NULL) |
| 1353 | goto out; |
| 1354 | |
| 1355 | /* Clean it */ |
| 1356 | memset(ctx.target, 0, alloc_size); |
| 1357 | |
| 1358 | ctx.idx = 0; |
| 1359 | |
| 1360 | /* Generate the actual JIT code */ |
| 1361 | build_prologue(&ctx); |
| 1362 | build_body(&ctx); |
| 1363 | build_epilogue(&ctx); |
| 1364 | |
| 1365 | /* Update the icache */ |
| 1366 | flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx)); |
| 1367 | |
| 1368 | if (bpf_jit_enable > 1) |
| 1369 | /* Dump JIT code */ |
| 1370 | bpf_jit_dump(fp->len, alloc_size, 2, ctx.target); |
| 1371 | |
| 1372 | fp->bpf_func = (void *)ctx.target; |
Daniel Borkmann | 286aad3 | 2014-09-08 08:04:49 +0200 | [diff] [blame] | 1373 | fp->jited = true; |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1374 | |
| 1375 | out: |
| 1376 | kfree(ctx.offsets); |
| 1377 | } |
| 1378 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1379 | void bpf_jit_free(struct bpf_prog *fp) |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1380 | { |
| 1381 | if (fp->jited) |
Rusty Russell | be1f221 | 2015-01-20 09:07:05 +1030 | [diff] [blame] | 1382 | module_memfree(fp->bpf_func); |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1383 | |
| 1384 | bpf_prog_unlock_free(fp); |
Markos Chandras | c6610de | 2014-04-08 12:47:14 +0100 | [diff] [blame] | 1385 | } |