/* bpf_jit.S : BPF JIT helper functions1*2* Copyright (C) 2011 Eric Dumazet ([email protected])3*4* This program is free software; you can redistribute it and/or5* modify it under the terms of the GNU General Public License6* as published by the Free Software Foundation; version 27* of the License.8*/9#include <linux/linkage.h>10#include <asm/dwarf2.h>1112/*13* Calling convention :14* rdi : skb pointer15* esi : offset of byte(s) to fetch in skb (can be scratched)16* r8 : copy of skb->data17* r9d : hlen = skb->len - skb->data_len18*/19#define SKBDATA %r82021sk_load_word_ind:22.globl sk_load_word_ind2324add %ebx,%esi /* offset += X */25# test %esi,%esi /* if (offset < 0) goto bpf_error; */26js bpf_error2728sk_load_word:29.globl sk_load_word3031mov %r9d,%eax # hlen32sub %esi,%eax # hlen - offset33cmp $3,%eax34jle bpf_slow_path_word35mov (SKBDATA,%rsi),%eax36bswap %eax /* ntohl() */37ret383940sk_load_half_ind:41.globl sk_load_half_ind4243add %ebx,%esi /* offset += X */44js bpf_error4546sk_load_half:47.globl sk_load_half4849mov %r9d,%eax50sub %esi,%eax # hlen - offset51cmp $1,%eax52jle bpf_slow_path_half53movzwl (SKBDATA,%rsi),%eax54rol $8,%ax # ntohs()55ret5657sk_load_byte_ind:58.globl sk_load_byte_ind59add %ebx,%esi /* offset += X */60js bpf_error6162sk_load_byte:63.globl sk_load_byte6465cmp %esi,%r9d /* if (offset >= hlen) goto bpf_slow_path_byte */66jle bpf_slow_path_byte67movzbl (SKBDATA,%rsi),%eax68ret6970/**71* sk_load_byte_msh - BPF_S_LDX_B_MSH helper72*73* Implements BPF_S_LDX_B_MSH : ldxb 4*([offset]&0xf)74* Must preserve A accumulator (%eax)75* Inputs : %esi is the offset value, already known positive76*/77ENTRY(sk_load_byte_msh)78CFI_STARTPROC79cmp %esi,%r9d /* if (offset >= hlen) goto bpf_slow_path_byte_msh */80jle bpf_slow_path_byte_msh81movzbl (SKBDATA,%rsi),%ebx82and $15,%bl83shl $2,%bl84ret85CFI_ENDPROC86ENDPROC(sk_load_byte_msh)8788bpf_error:89# force a return 0 from jit handler90xor %eax,%eax91mov -8(%rbp),%rbx92leaveq93ret9495/* rsi contains offset and can be scratched */96#define bpf_slow_path_common(LEN) \97push %rdi; /* save skb */ \98push %r9; \99push SKBDATA; \100/* rsi already has offset */ \101mov $LEN,%ecx; /* len */ \102lea -12(%rbp),%rdx; \103call skb_copy_bits; \104test %eax,%eax; \105pop SKBDATA; \106pop %r9; \107pop %rdi108109110bpf_slow_path_word:111bpf_slow_path_common(4)112js bpf_error113mov -12(%rbp),%eax114bswap %eax115ret116117bpf_slow_path_half:118bpf_slow_path_common(2)119js bpf_error120mov -12(%rbp),%ax121rol $8,%ax122movzwl %ax,%eax123ret124125bpf_slow_path_byte:126bpf_slow_path_common(1)127js bpf_error128movzbl -12(%rbp),%eax129ret130131bpf_slow_path_byte_msh:132xchg %eax,%ebx /* dont lose A , X is about to be scratched */133bpf_slow_path_common(1)134js bpf_error135movzbl -12(%rbp),%eax136and $15,%al137shl $2,%al138xchg %eax,%ebx139ret140141142