2 * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (C) 2005-2009 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_
88 /* Optimization flags */
89 #define BPF_JIT_FRET 0x01
90 #define BPF_JIT_FPKT 0x02
91 #define BPF_JIT_FMEM 0x04
92 #define BPF_JIT_FJMP 0x08
93 #define BPF_JIT_FLEN 0x10
95 #define BPF_JIT_FLAG_ALL \
96 (BPF_JIT_FPKT | BPF_JIT_FMEM | BPF_JIT_FJMP | BPF_JIT_FLEN)
98 /* A stream of native binary code */
99 typedef struct bpf_bin_stream {
100 /* Current native instruction pointer. */
104 * Current BPF instruction pointer, i.e. position in
105 * the BPF program reached by the jitter.
109 /* Instruction buffer, contains the generated native code. */
112 /* Jumps reference table. */
117 * Prototype of the emit functions.
119 * Different emit functions are used to create the reference table and
120 * to generate the actual filtering code. This allows to have simpler
121 * instruction macros.
122 * The first parameter is the stream that will receive the data.
123 * The second one is a variable containing the data.
124 * The third one is the length, that can be 1, 2, or 4 since it is possible
125 * to emit a byte, a short, or a word at a time.
127 typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
130 * Native instruction macros
134 #define MOVid(i32, r32) do { \
135 emitm(&stream, (11 << 4) | (1 << 3) | (r32 & 0x7), 1); \
136 emitm(&stream, i32, 4); \
140 #define MOViq(i64, r64) do { \
141 emitm(&stream, 0x48, 1); \
142 emitm(&stream, (11 << 4) | (1 << 3) | (r64 & 0x7), 1); \
143 emitm(&stream, i64, 4); \
144 emitm(&stream, (i64 >> 32), 4); \
148 #define MOVrd(sr32, dr32) do { \
149 emitm(&stream, 0x89, 1); \
151 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
154 /* movl sr32,dr32 (dr32 = %r8-15d) */
155 #define MOVrd2(sr32, dr32) do { \
156 emitm(&stream, 0x8941, 2); \
158 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
161 /* movl sr32,dr32 (sr32 = %r8-15d) */
162 #define MOVrd3(sr32, dr32) do { \
163 emitm(&stream, 0x8944, 2); \
165 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
169 #define MOVrq(sr64, dr64) do { \
170 emitm(&stream, 0x8948, 2); \
172 (3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
175 /* movq sr64,dr64 (dr64 = %r8-15) */
176 #define MOVrq2(sr64, dr64) do { \
177 emitm(&stream, 0x8949, 2); \
179 (3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
182 /* movq sr64,dr64 (sr64 = %r8-15) */
183 #define MOVrq3(sr64, dr64) do { \
184 emitm(&stream, 0x894c, 2); \
186 (3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
189 /* movl (sr64,or64,1),dr32 */
190 #define MOVobd(sr64, or64, dr32) do { \
191 emitm(&stream, 0x8b, 1); \
192 emitm(&stream, ((dr32 & 0x7) << 3) | 4, 1); \
193 emitm(&stream, ((or64 & 0x7) << 3) | (sr64 & 0x7), 1); \
196 /* movw (sr64,or64,1),dr16 */
197 #define MOVobw(sr64, or64, dr16) do { \
198 emitm(&stream, 0x8b66, 2); \
199 emitm(&stream, ((dr16 & 0x7) << 3) | 4, 1); \
200 emitm(&stream, ((or64 & 0x7) << 3) | (sr64 & 0x7), 1); \
203 /* movb (sr64,or64,1),dr8 */
204 #define MOVobb(sr64, or64, dr8) do { \
205 emitm(&stream, 0x8a, 1); \
206 emitm(&stream, ((dr8 & 0x7) << 3) | 4, 1); \
207 emitm(&stream, ((or64 & 0x7) << 3) | (sr64 & 0x7), 1); \
210 /* movl sr32,(dr64,or64,1) */
211 #define MOVomd(sr32, dr64, or64) do { \
212 emitm(&stream, 0x89, 1); \
213 emitm(&stream, ((sr32 & 0x7) << 3) | 4, 1); \
214 emitm(&stream, ((or64 & 0x7) << 3) | (dr64 & 0x7), 1); \
218 #define BSWAP(dr32) do { \
219 emitm(&stream, 0xf, 1); \
220 emitm(&stream, (0x19 << 3) | dr32, 1); \
224 #define SWAP_AX() do { \
225 emitm(&stream, 0xc486, 2); \
229 #define PUSH(r64) do { \
230 emitm(&stream, (5 << 4) | (0 << 3) | (r64 & 0x7), 1); \
234 #define LEAVE() do { \
235 emitm(&stream, 0xc9, 1); \
240 emitm(&stream, 0xc3, 1); \
244 #define ADDrd(sr32, dr32) do { \
245 emitm(&stream, 0x01, 1); \
247 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
251 #define ADD_EAXi(i32) do { \
252 emitm(&stream, 0x05, 1); \
253 emitm(&stream, i32, 4); \
257 #define ADDib(i8, r32) do { \
258 emitm(&stream, 0x83, 1); \
259 emitm(&stream, (24 << 3) | r32, 1); \
260 emitm(&stream, i8, 1); \
264 #define SUBrd(sr32, dr32) do { \
265 emitm(&stream, 0x29, 1); \
267 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
271 #define SUB_EAXi(i32) do { \
272 emitm(&stream, 0x2d, 1); \
273 emitm(&stream, i32, 4); \
277 #define SUBib(i8, r64) do { \
278 emitm(&stream, 0x8348, 2); \
279 emitm(&stream, (29 << 3) | (r64 & 0x7), 1); \
280 emitm(&stream, i8, 1); \
284 #define MULrd(r32) do { \
285 emitm(&stream, 0xf7, 1); \
286 emitm(&stream, (7 << 5) | (r32 & 0x7), 1); \
290 #define DIVrd(r32) do { \
291 emitm(&stream, 0xf7, 1); \
292 emitm(&stream, (15 << 4) | (r32 & 0x7), 1); \
296 #define ANDib(i8, r8) do { \
298 emitm(&stream, 0x24, 1); \
300 emitm(&stream, 0x80, 1); \
301 emitm(&stream, (7 << 5) | r8, 1); \
303 emitm(&stream, i8, 1); \
307 #define ANDid(i32, r32) do { \
309 emitm(&stream, 0x25, 1); \
311 emitm(&stream, 0x81, 1); \
312 emitm(&stream, (7 << 5) | r32, 1); \
314 emitm(&stream, i32, 4); \
318 #define ANDrd(sr32, dr32) do { \
319 emitm(&stream, 0x21, 1); \
321 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
325 #define TESTid(i32, r32) do { \
327 emitm(&stream, 0xa9, 1); \
329 emitm(&stream, 0xf7, 1); \
330 emitm(&stream, (3 << 6) | r32, 1); \
332 emitm(&stream, i32, 4); \
335 /* testl sr32,dr32 */
336 #define TESTrd(sr32, dr32) do { \
337 emitm(&stream, 0x85, 1); \
339 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
343 #define ORrd(sr32, dr32) do { \
344 emitm(&stream, 0x09, 1); \
346 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
350 #define ORid(i32, r32) do { \
352 emitm(&stream, 0x0d, 1); \
354 emitm(&stream, 0x81, 1); \
355 emitm(&stream, (25 << 3) | r32, 1); \
357 emitm(&stream, i32, 4); \
361 #define SHLib(i8, r32) do { \
362 emitm(&stream, 0xc1, 1); \
363 emitm(&stream, (7 << 5) | (r32 & 0x7), 1); \
364 emitm(&stream, i8, 1); \
368 #define SHL_CLrb(dr32) do { \
369 emitm(&stream, 0xd3, 1); \
370 emitm(&stream, (7 << 5) | (dr32 & 0x7), 1); \
374 #define SHRib(i8, r32) do { \
375 emitm(&stream, 0xc1, 1); \
376 emitm(&stream, (29 << 3) | (r32 & 0x7), 1); \
377 emitm(&stream, i8, 1); \
381 #define SHR_CLrb(dr32) do { \
382 emitm(&stream, 0xd3, 1); \
383 emitm(&stream, (29 << 3) | (dr32 & 0x7), 1); \
387 #define NEGd(r32) do { \
388 emitm(&stream, 0xf7, 1); \
389 emitm(&stream, (27 << 3) | (r32 & 0x7), 1); \
393 #define CMPrd(sr32, dr32) do { \
394 emitm(&stream, 0x39, 1); \
396 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
400 #define CMPid(i32, dr32) do { \
402 emitm(&stream, 0x3d, 1); \
403 emitm(&stream, i32, 4); \
405 emitm(&stream, 0x81, 1); \
406 emitm(&stream, (0x1f << 3) | (dr32 & 0x7), 1); \
407 emitm(&stream, i32, 4); \
412 #define JBb(off8) do { \
413 emitm(&stream, 0x72, 1); \
414 emitm(&stream, off8, 1); \
418 #define JAEb(off8) do { \
419 emitm(&stream, 0x73, 1); \
420 emitm(&stream, off8, 1); \
424 #define JNEb(off8) do { \
425 emitm(&stream, 0x75, 1); \
426 emitm(&stream, off8, 1); \
430 #define JAb(off8) do { \
431 emitm(&stream, 0x77, 1); \
432 emitm(&stream, off8, 1); \
436 #define JMP(off32) do { \
437 emitm(&stream, 0xe9, 1); \
438 emitm(&stream, off32, 4); \
442 #define ZEROrd(r32) do { \
443 emitm(&stream, 0x31, 1); \
444 emitm(&stream, (3 << 6) | ((r32 & 0x7) << 3) | (r32 & 0x7), 1); \
448 * Conditional long jumps
457 #define JCC(t, f) do { \
458 if (ins->jt != 0 && ins->jf != 0) { \
459 /* 5 is the size of the following jmp */ \
460 emitm(&stream, ((t) << 8) | 0x0f, 2); \
461 emitm(&stream, stream.refs[stream.bpf_pc + ins->jt] - \
462 stream.refs[stream.bpf_pc] + 5, 4); \
463 JMP(stream.refs[stream.bpf_pc + ins->jf] - \
464 stream.refs[stream.bpf_pc]); \
465 } else if (ins->jt != 0) { \
466 emitm(&stream, ((t) << 8) | 0x0f, 2); \
467 emitm(&stream, stream.refs[stream.bpf_pc + ins->jt] - \
468 stream.refs[stream.bpf_pc], 4); \
470 emitm(&stream, ((f) << 8) | 0x0f, 2); \
471 emitm(&stream, stream.refs[stream.bpf_pc + ins->jf] - \
472 stream.refs[stream.bpf_pc], 4); \
476 #define JUMP(off) do { \
478 JMP(stream.refs[stream.bpf_pc + (off)] - \
479 stream.refs[stream.bpf_pc]); \
482 #endif /* _BPF_JIT_MACHDEP_H_ */