2 * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _BPF_JIT_MACHDEP_H_
35 #define _BPF_JIT_MACHDEP_H_
63 /* A stream of native binary code.*/
64 typedef struct bpf_bin_stream {
65 /* Current native instruction pointer. */
69 * Current BPF instruction pointer, i.e. position in
70 * the BPF program reached by the jitter.
74 /* Instruction buffer, contains the generated native code. */
77 /* Jumps reference table. */
82 * Prototype of the emit functions.
84 * Different emit functions are used to create the reference table and
85 * to generate the actual filtering code. This allows to have simpler
87 * The first parameter is the stream that will receive the data.
88 * The second one is a variable containing the data.
89 * The third one is the length, that can be 1, 2, or 4 since it is possible
90 * to emit a byte, a short, or a word at a time.
92 typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
95 * native Instruction Macros
99 #define MOVid(i32, r32) do { \
100 emitm(&stream, (11 << 4) | (1 << 3) | (r32 & 0x7), 1); \
101 emitm(&stream, i32, 4); \
105 #define MOVrd(sr32, dr32) do { \
106 emitm(&stream, 0x89, 1); \
108 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
111 /* movl off(sr32),dr32 */
112 #define MOVodd(off, sr32, dr32) do { \
113 emitm(&stream, 0x8b, 1); \
115 (1 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
116 emitm(&stream, off, 1); \
119 /* movl (sr32,or32,1),dr32 */
120 #define MOVobd(sr32, or32, dr32) do { \
121 emitm(&stream, 0x8b, 1); \
122 emitm(&stream, ((dr32 & 0x7) << 3) | 4, 1); \
123 emitm(&stream, ((or32 & 0x7) << 3) | (sr32 & 0x7), 1); \
126 /* movw (sr32,or32,1),dr16 */
127 #define MOVobw(sr32, or32, dr16) do { \
128 emitm(&stream, 0x8b66, 2); \
129 emitm(&stream, ((dr16 & 0x7) << 3) | 4, 1); \
130 emitm(&stream, ((or32 & 0x7) << 3) | (sr32 & 0x7), 1); \
133 /* movb (sr32,or32,1),dr8 */
134 #define MOVobb(sr32, or32, dr8) do { \
135 emitm(&stream, 0x8a, 1); \
136 emitm(&stream, ((dr8 & 0x7) << 3) | 4, 1); \
137 emitm(&stream, ((or32 & 0x7) << 3) | (sr32 & 0x7), 1); \
140 /* movl sr32,(dr32,or32,1) */
141 #define MOVomd(sr32, dr32, or32) do { \
142 emitm(&stream, 0x89, 1); \
143 emitm(&stream, ((sr32 & 0x7) << 3) | 4, 1); \
144 emitm(&stream, ((or32 & 0x7) << 3) | (dr32 & 0x7), 1); \
148 #define BSWAP(dr32) do { \
149 emitm(&stream, 0xf, 1); \
150 emitm(&stream, (0x19 << 3) | dr32, 1); \
154 #define SWAP_AX() do { \
155 emitm(&stream, 0xc486, 2); \
159 #define PUSH(r32) do { \
160 emitm(&stream, (5 << 4) | (0 << 3) | (r32 & 0x7), 1); \
164 #define POP(r32) do { \
165 emitm(&stream, (5 << 4) | (1 << 3) | (r32 & 0x7), 1); \
169 #define LEAVE_RET() do { \
170 emitm(&stream, 0xc3c9, 2); \
174 #define ADDrd(sr32, dr32) do { \
175 emitm(&stream, 0x01, 1); \
177 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
181 #define ADD_EAXi(i32) do { \
182 emitm(&stream, 0x05, 1); \
183 emitm(&stream, i32, 4); \
187 #define ADDib(i8, r32) do { \
188 emitm(&stream, 0x83, 1); \
189 emitm(&stream, (24 << 3) | r32, 1); \
190 emitm(&stream, i8, 1); \
194 #define SUBrd(sr32, dr32) do { \
195 emitm(&stream, 0x29, 1); \
197 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
201 #define SUB_EAXi(i32) do { \
202 emitm(&stream, 0x2d, 1); \
203 emitm(&stream, i32, 4); \
207 #define MULrd(r32) do { \
208 emitm(&stream, 0xf7, 1); \
209 emitm(&stream, (7 << 5) | (r32 & 0x7), 1); \
213 #define DIVrd(r32) do { \
214 emitm(&stream, 0xf7, 1); \
215 emitm(&stream, (15 << 4) | (r32 & 0x7), 1); \
219 #define ANDib(i8, r8) do { \
221 emitm(&stream, 0x24, 1); \
223 emitm(&stream, 0x80, 1); \
224 emitm(&stream, (7 << 5) | r8, 1); \
226 emitm(&stream, i8, 1); \
230 #define ANDid(i32, r32) do { \
232 emitm(&stream, 0x25, 1); \
234 emitm(&stream, 0x81, 1); \
235 emitm(&stream, (7 << 5) | r32, 1); \
237 emitm(&stream, i32, 4); \
241 #define ANDrd(sr32, dr32) do { \
242 emitm(&stream, 0x21, 1); \
244 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
248 #define TESTid(i32, r32) do { \
250 emitm(&stream, 0xa9, 1); \
252 emitm(&stream, 0xf7, 1); \
253 emitm(&stream, (3 << 6) | r32, 1); \
255 emitm(&stream, i32, 4); \
258 /* testl sr32,dr32 */
259 #define TESTrd(sr32, dr32) do { \
260 emitm(&stream, 0x85, 1); \
262 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
266 #define ORrd(sr32, dr32) do { \
267 emitm(&stream, 0x09, 1); \
269 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
273 #define ORid(i32, r32) do { \
275 emitm(&stream, 0x0d, 1); \
277 emitm(&stream, 0x81, 1); \
278 emitm(&stream, (25 << 3) | r32, 1); \
280 emitm(&stream, i32, 4); \
284 #define SHLib(i8, r32) do { \
285 emitm(&stream, 0xc1, 1); \
286 emitm(&stream, (7 << 5) | (r32 & 0x7), 1); \
287 emitm(&stream, i8, 1); \
291 #define SHL_CLrb(dr32) do { \
292 emitm(&stream, 0xd3, 1); \
293 emitm(&stream, (7 << 5) | (dr32 & 0x7), 1); \
297 #define SHRib(i8, r32) do { \
298 emitm(&stream, 0xc1, 1); \
299 emitm(&stream, (29 << 3) | (r32 & 0x7), 1); \
300 emitm(&stream, i8, 1); \
304 #define SHR_CLrb(dr32) do { \
305 emitm(&stream, 0xd3, 1); \
306 emitm(&stream, (29 << 3) | (dr32 & 0x7), 1); \
310 #define NEGd(r32) do { \
311 emitm(&stream, 0xf7, 1); \
312 emitm(&stream, (27 << 3) | (r32 & 0x7), 1); \
316 #define CMPrd(sr32, dr32) do { \
317 emitm(&stream, 0x39, 1); \
319 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
323 #define CMPid(i32, dr32) do { \
325 emitm(&stream, 0x3d, 1); \
326 emitm(&stream, i32, 4); \
328 emitm(&stream, 0x81, 1); \
329 emitm(&stream, (0x1f << 3) | (dr32 & 0x7), 1); \
330 emitm(&stream, i32, 4); \
335 #define JBb(off8) do { \
336 emitm(&stream, 0x72, 1); \
337 emitm(&stream, off8, 1); \
341 #define JAEb(off8) do { \
342 emitm(&stream, 0x73, 1); \
343 emitm(&stream, off8, 1); \
347 #define JNEb(off8) do { \
348 emitm(&stream, 0x75, 1); \
349 emitm(&stream, off8, 1); \
353 #define JAb(off8) do { \
354 emitm(&stream, 0x77, 1); \
355 emitm(&stream, off8, 1); \
359 #define JMP(off32) do { \
360 emitm(&stream, 0xe9, 1); \
361 emitm(&stream, off32, 4); \
365 #define ZEROrd(r32) do { \
366 emitm(&stream, 0x31, 1); \
367 emitm(&stream, (3 << 6) | ((r32 & 0x7) << 3) | (r32 & 0x7), 1); \
371 * Conditional long jumps
380 #define JCC(t, f) do { \
381 if (ins->jt != 0 && ins->jf != 0) { \
382 /* 5 is the size of the following jmp */ \
383 emitm(&stream, ((t) << 8) | 0x0f, 2); \
384 emitm(&stream, stream.refs[stream.bpf_pc + ins->jt] - \
385 stream.refs[stream.bpf_pc] + 5, 4); \
386 JMP(stream.refs[stream.bpf_pc + ins->jf] - \
387 stream.refs[stream.bpf_pc]); \
388 } else if (ins->jt != 0) { \
389 emitm(&stream, ((t) << 8) | 0x0f, 2); \
390 emitm(&stream, stream.refs[stream.bpf_pc + ins->jt] - \
391 stream.refs[stream.bpf_pc], 4); \
393 emitm(&stream, ((f) << 8) | 0x0f, 2); \
394 emitm(&stream, stream.refs[stream.bpf_pc + ins->jf] - \
395 stream.refs[stream.bpf_pc], 4); \
399 #endif /* _BPF_JIT_MACHDEP_H_ */