Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 1 | /* bpf_jit.S : BPF JIT helper functions |
| 2 | * |
| 3 | * Copyright (C) 2011 Eric Dumazet (eric.dumazet@gmail.com) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; version 2 |
| 8 | * of the License. |
| 9 | */ |
| 10 | #include <linux/linkage.h> |
| 11 | #include <asm/dwarf2.h> |
| 12 | |
| 13 | /* |
| 14 | * Calling convention : |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 15 | * rbx : skb pointer (callee saved) |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 16 | * esi : offset of byte(s) to fetch in skb (can be scratched) |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 17 | * r10 : copy of skb->data |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 18 | * r9d : hlen = skb->len - skb->data_len |
| 19 | */ |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 20 | #define SKBDATA %r10 |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 21 | #define SKF_MAX_NEG_OFF $(-0x200000) /* SKF_LL_OFF from filter.h */ |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 22 | #define MAX_BPF_STACK (512 /* from filter.h */ + \ |
| 23 | 32 /* space for rbx,r13,r14,r15 */ + \ |
| 24 | 8 /* space for skb_copy_bits */) |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 25 | |
| 26 | sk_load_word: |
| 27 | .globl sk_load_word |
| 28 | |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 29 | test %esi,%esi |
| 30 | js bpf_slow_path_word_neg |
| 31 | |
| 32 | sk_load_word_positive_offset: |
| 33 | .globl sk_load_word_positive_offset |
| 34 | |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 35 | mov %r9d,%eax # hlen |
| 36 | sub %esi,%eax # hlen - offset |
| 37 | cmp $3,%eax |
| 38 | jle bpf_slow_path_word |
| 39 | mov (SKBDATA,%rsi),%eax |
| 40 | bswap %eax /* ntohl() */ |
| 41 | ret |
| 42 | |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 43 | sk_load_half: |
| 44 | .globl sk_load_half |
| 45 | |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 46 | test %esi,%esi |
| 47 | js bpf_slow_path_half_neg |
| 48 | |
| 49 | sk_load_half_positive_offset: |
| 50 | .globl sk_load_half_positive_offset |
| 51 | |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 52 | mov %r9d,%eax |
| 53 | sub %esi,%eax # hlen - offset |
| 54 | cmp $1,%eax |
| 55 | jle bpf_slow_path_half |
| 56 | movzwl (SKBDATA,%rsi),%eax |
| 57 | rol $8,%ax # ntohs() |
| 58 | ret |
| 59 | |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 60 | sk_load_byte: |
| 61 | .globl sk_load_byte |
| 62 | |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 63 | test %esi,%esi |
| 64 | js bpf_slow_path_byte_neg |
| 65 | |
| 66 | sk_load_byte_positive_offset: |
| 67 | .globl sk_load_byte_positive_offset |
| 68 | |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 69 | cmp %esi,%r9d /* if (offset >= hlen) goto bpf_slow_path_byte */ |
| 70 | jle bpf_slow_path_byte |
| 71 | movzbl (SKBDATA,%rsi),%eax |
| 72 | ret |
| 73 | |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 74 | /* rsi contains offset and can be scratched */ |
| 75 | #define bpf_slow_path_common(LEN) \ |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 76 | mov %rbx, %rdi; /* arg1 == skb */ \ |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 77 | push %r9; \ |
| 78 | push SKBDATA; \ |
| 79 | /* rsi already has offset */ \ |
| 80 | mov $LEN,%ecx; /* len */ \ |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 81 | lea - MAX_BPF_STACK + 32(%rbp),%rdx; \ |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 82 | call skb_copy_bits; \ |
| 83 | test %eax,%eax; \ |
| 84 | pop SKBDATA; \ |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 85 | pop %r9; |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | bpf_slow_path_word: |
| 89 | bpf_slow_path_common(4) |
| 90 | js bpf_error |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 91 | mov - MAX_BPF_STACK + 32(%rbp),%eax |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 92 | bswap %eax |
| 93 | ret |
| 94 | |
| 95 | bpf_slow_path_half: |
| 96 | bpf_slow_path_common(2) |
| 97 | js bpf_error |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 98 | mov - MAX_BPF_STACK + 32(%rbp),%ax |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 99 | rol $8,%ax |
| 100 | movzwl %ax,%eax |
| 101 | ret |
| 102 | |
| 103 | bpf_slow_path_byte: |
| 104 | bpf_slow_path_common(1) |
| 105 | js bpf_error |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 106 | movzbl - MAX_BPF_STACK + 32(%rbp),%eax |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 107 | ret |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 108 | |
| 109 | #define sk_negative_common(SIZE) \ |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 110 | mov %rbx, %rdi; /* arg1 == skb */ \ |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 111 | push %r9; \ |
| 112 | push SKBDATA; \ |
| 113 | /* rsi already has offset */ \ |
Alexei Starovoitov | fdfaf64 | 2014-03-10 15:56:51 -0700 | [diff] [blame] | 114 | mov $SIZE,%edx; /* size */ \ |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 115 | call bpf_internal_load_pointer_neg_helper; \ |
| 116 | test %rax,%rax; \ |
| 117 | pop SKBDATA; \ |
| 118 | pop %r9; \ |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 119 | jz bpf_error |
| 120 | |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 121 | bpf_slow_path_word_neg: |
| 122 | cmp SKF_MAX_NEG_OFF, %esi /* test range */ |
| 123 | jl bpf_error /* offset lower -> error */ |
| 124 | sk_load_word_negative_offset: |
| 125 | .globl sk_load_word_negative_offset |
| 126 | sk_negative_common(4) |
| 127 | mov (%rax), %eax |
| 128 | bswap %eax |
| 129 | ret |
| 130 | |
| 131 | bpf_slow_path_half_neg: |
| 132 | cmp SKF_MAX_NEG_OFF, %esi |
| 133 | jl bpf_error |
| 134 | sk_load_half_negative_offset: |
| 135 | .globl sk_load_half_negative_offset |
| 136 | sk_negative_common(2) |
| 137 | mov (%rax),%ax |
| 138 | rol $8,%ax |
| 139 | movzwl %ax,%eax |
| 140 | ret |
| 141 | |
| 142 | bpf_slow_path_byte_neg: |
| 143 | cmp SKF_MAX_NEG_OFF, %esi |
| 144 | jl bpf_error |
| 145 | sk_load_byte_negative_offset: |
| 146 | .globl sk_load_byte_negative_offset |
| 147 | sk_negative_common(1) |
| 148 | movzbl (%rax), %eax |
| 149 | ret |
| 150 | |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 151 | bpf_error: |
| 152 | # force a return 0 from jit handler |
Alexei Starovoitov | 6225827 | 2014-05-13 19:50:46 -0700 | [diff] [blame^] | 153 | xor %eax,%eax |
| 154 | mov - MAX_BPF_STACK(%rbp),%rbx |
| 155 | mov - MAX_BPF_STACK + 8(%rbp),%r13 |
| 156 | mov - MAX_BPF_STACK + 16(%rbp),%r14 |
| 157 | mov - MAX_BPF_STACK + 24(%rbp),%r15 |
Jan Seiffert | a998d43 | 2012-03-30 05:24:05 +0000 | [diff] [blame] | 158 | leaveq |
| 159 | ret |