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