1 /* bpf_jit.S: Packet/header access helper functions
2 * for PPC64 BPF compiler.
4 * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; version 2
12 #include <asm/ppc_asm.h>
16 * All of these routines are called directly from generated code,
17 * whose register usage is:
21 * r6 *** address parameter to helper ***
29 * To consider: These helpers are so small it could be better to just
30 * generate them inline. Inline code can do the simple headlen check
31 * then branch directly to slow_path_XXX if required. (In fact, could
32 * load a spare GPR with the address of slow_path_generic and pass size
33 * as an argument, making the call site a mtlr, li and bllr.)
38 blt bpf_slow_path_word_neg
39 .globl sk_load_word_positive_offset
40 sk_load_word_positive_offset:
41 /* Are we accessing past headlen? */
42 subi r_scratch1, r_HL, 4
43 cmpd r_scratch1, r_addr
44 blt bpf_slow_path_word
45 /* Nope, just hitting the header. cr0 here is eq or gt! */
46 #ifdef __LITTLE_ENDIAN__
47 lwbrx r_A, r_D, r_addr
51 blr /* Return success, cr0 != LT */
56 blt bpf_slow_path_half_neg
57 .globl sk_load_half_positive_offset
58 sk_load_half_positive_offset:
59 subi r_scratch1, r_HL, 2
60 cmpd r_scratch1, r_addr
61 blt bpf_slow_path_half
62 #ifdef __LITTLE_ENDIAN__
63 lhbrx r_A, r_D, r_addr
72 blt bpf_slow_path_byte_neg
73 .globl sk_load_byte_positive_offset
74 sk_load_byte_positive_offset:
76 ble bpf_slow_path_byte
81 * BPF_S_LDX_B_MSH: ldxb 4*([offset]&0xf)
82 * r_addr is the offset value
84 .globl sk_load_byte_msh
87 blt bpf_slow_path_byte_msh_neg
88 .globl sk_load_byte_msh_positive_offset
89 sk_load_byte_msh_positive_offset:
91 ble bpf_slow_path_byte_msh
93 rlwinm r_X, r_X, 2, 32-4-2, 31-2
96 /* Call out to skb_copy_bits:
97 * We'll need to back up our volatile regs first; we have
98 * local variable space at r1+(BPF_PPC_STACK_BASIC).
99 * Allocate a new stack frame here to remain ABI-compliant in
102 #define bpf_slow_path_common(SIZE) \
105 /* R3 goes in parameter space of caller's frame */ \
106 std r_skb, (BPF_PPC_STACKFRAME+48)(r1); \
107 std r_A, (BPF_PPC_STACK_BASIC+(0*8))(r1); \
108 std r_X, (BPF_PPC_STACK_BASIC+(1*8))(r1); \
109 addi r5, r1, BPF_PPC_STACK_BASIC+(2*8); \
110 stdu r1, -BPF_PPC_SLOWPATH_FRAME(r1); \
111 /* R3 = r_skb, as passed */ \
116 /* R3 = 0 on success */ \
117 addi r1, r1, BPF_PPC_SLOWPATH_FRAME; \
119 ld r_A, (BPF_PPC_STACK_BASIC+(0*8))(r1); \
120 ld r_X, (BPF_PPC_STACK_BASIC+(1*8))(r1); \
123 blt bpf_error; /* cr0 = LT */ \
124 ld r_skb, (BPF_PPC_STACKFRAME+48)(r1); \
128 bpf_slow_path_common(4)
129 /* Data value is on stack, and cr0 != LT */
130 lwz r_A, BPF_PPC_STACK_BASIC+(2*8)(r1)
134 bpf_slow_path_common(2)
135 lhz r_A, BPF_PPC_STACK_BASIC+(2*8)(r1)
139 bpf_slow_path_common(1)
140 lbz r_A, BPF_PPC_STACK_BASIC+(2*8)(r1)
143 bpf_slow_path_byte_msh:
144 bpf_slow_path_common(1)
145 lbz r_X, BPF_PPC_STACK_BASIC+(2*8)(r1)
146 rlwinm r_X, r_X, 2, 32-4-2, 31-2
149 /* Call out to bpf_internal_load_pointer_neg_helper:
150 * We'll need to back up our volatile regs first; we have
151 * local variable space at r1+(BPF_PPC_STACK_BASIC).
152 * Allocate a new stack frame here to remain ABI-compliant in
155 #define sk_negative_common(SIZE) \
158 /* R3 goes in parameter space of caller's frame */ \
159 std r_skb, (BPF_PPC_STACKFRAME+48)(r1); \
160 std r_A, (BPF_PPC_STACK_BASIC+(0*8))(r1); \
161 std r_X, (BPF_PPC_STACK_BASIC+(1*8))(r1); \
162 stdu r1, -BPF_PPC_SLOWPATH_FRAME(r1); \
163 /* R3 = r_skb, as passed */ \
166 bl bpf_internal_load_pointer_neg_helper; \
168 /* R3 != 0 on success */ \
169 addi r1, r1, BPF_PPC_SLOWPATH_FRAME; \
171 ld r_A, (BPF_PPC_STACK_BASIC+(0*8))(r1); \
172 ld r_X, (BPF_PPC_STACK_BASIC+(1*8))(r1); \
175 beq bpf_error_slow; /* cr0 = EQ */ \
177 ld r_skb, (BPF_PPC_STACKFRAME+48)(r1); \
180 bpf_slow_path_word_neg:
181 lis r_scratch1,-32 /* SKF_LL_OFF */
182 cmpd r_addr, r_scratch1 /* addr < SKF_* */
183 blt bpf_error /* cr0 = LT */
184 .globl sk_load_word_negative_offset
185 sk_load_word_negative_offset:
186 sk_negative_common(4)
190 bpf_slow_path_half_neg:
191 lis r_scratch1,-32 /* SKF_LL_OFF */
192 cmpd r_addr, r_scratch1 /* addr < SKF_* */
193 blt bpf_error /* cr0 = LT */
194 .globl sk_load_half_negative_offset
195 sk_load_half_negative_offset:
196 sk_negative_common(2)
200 bpf_slow_path_byte_neg:
201 lis r_scratch1,-32 /* SKF_LL_OFF */
202 cmpd r_addr, r_scratch1 /* addr < SKF_* */
203 blt bpf_error /* cr0 = LT */
204 .globl sk_load_byte_negative_offset
205 sk_load_byte_negative_offset:
206 sk_negative_common(1)
210 bpf_slow_path_byte_msh_neg:
211 lis r_scratch1,-32 /* SKF_LL_OFF */
212 cmpd r_addr, r_scratch1 /* addr < SKF_* */
213 blt bpf_error /* cr0 = LT */
214 .globl sk_load_byte_msh_negative_offset
215 sk_load_byte_msh_negative_offset:
216 sk_negative_common(1)
218 rlwinm r_X, r_X, 2, 32-4-2, 31-2
222 /* fabricate a cr0 = lt */
226 /* Entered with cr0 = lt */
228 /* Generated code will 'blt epilogue', returning 0. */