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_
88 /* A stream of native binary code.*/
89 typedef struct bpf_bin_stream {
90 /* Current native instruction pointer. */
94 * Current BPF instruction pointer, i.e. position in
95 * the BPF program reached by the jitter.
99 /* Instruction buffer, contains the generated native code. */
102 /* Jumps reference table. */
107 * Prototype of the emit functions.
109 * Different emit functions are used to create the reference table and
110 * to generate the actual filtering code. This allows to have simpler
111 * instruction macros.
112 * The first parameter is the stream that will receive the data.
113 * The second one is a variable containing the data.
114 * The third one is the length, that can be 1, 2, or 4 since it is possible
115 * to emit a byte, a short, or a word at a time.
117 typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
120 * native Instruction Macros
124 #define MOVid(i32, r32) do { \
125 emitm(&stream, (11 << 4) | (1 << 3) | (r32 & 0x7), 1); \
126 emitm(&stream, i32, 4); \
130 #define MOViq(i64, r64) do { \
131 emitm(&stream, 0x48, 1); \
132 emitm(&stream, (11 << 4) | (1 << 3) | (r64 & 0x7), 1); \
133 emitm(&stream, i64, 4); \
134 emitm(&stream, (i64 >> 32), 4); \
138 #define MOVrd(sr32, dr32) do { \
139 emitm(&stream, 0x89, 1); \
141 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
144 /* movl sr32,dr32 (dr32 = %r8-15d) */
145 #define MOVrd2(sr32, dr32) do { \
146 emitm(&stream, 0x8941, 2); \
148 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
151 /* movl sr32,dr32 (sr32 = %r8-15d) */
152 #define MOVrd3(sr32, dr32) do { \
153 emitm(&stream, 0x8944, 2); \
155 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
159 #define MOVrq(sr64, dr64) do { \
160 emitm(&stream, 0x8948, 2); \
162 (3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
165 /* movq sr64,dr64 (dr64 = %r8-15) */
166 #define MOVrq2(sr64, dr64) do { \
167 emitm(&stream, 0x8949, 2); \
169 (3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
172 /* movq sr64,dr64 (sr64 = %r8-15) */
173 #define MOVrq3(sr64, dr64) do { \
174 emitm(&stream, 0x894c, 2); \
176 (3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
179 /* movl (sr64,or64,1),dr32 */
180 #define MOVobd(sr64, or64, dr32) do { \
181 emitm(&stream, 0x8b, 1); \
182 emitm(&stream, ((dr32 & 0x7) << 3) | 4, 1); \
183 emitm(&stream, ((or64 & 0x7) << 3) | (sr64 & 0x7), 1); \
186 /* movw (sr64,or64,1),dr16 */
187 #define MOVobw(sr64, or64, dr16) do { \
188 emitm(&stream, 0x8b66, 2); \
189 emitm(&stream, ((dr16 & 0x7) << 3) | 4, 1); \
190 emitm(&stream, ((or64 & 0x7) << 3) | (sr64 & 0x7), 1); \
193 /* movb (sr64,or64,1),dr8 */
194 #define MOVobb(sr64, or64, dr8) do { \
195 emitm(&stream, 0x8a, 1); \
196 emitm(&stream, ((dr8 & 0x7) << 3) | 4, 1); \
197 emitm(&stream, ((or64 & 0x7) << 3) | (sr64 & 0x7), 1); \
200 /* movl sr32,(dr64,or64,1) */
201 #define MOVomd(sr32, dr64, or64) do { \
202 emitm(&stream, 0x89, 1); \
203 emitm(&stream, ((sr32 & 0x7) << 3) | 4, 1); \
204 emitm(&stream, ((or64 & 0x7) << 3) | (dr64 & 0x7), 1); \
208 #define BSWAP(dr32) do { \
209 emitm(&stream, 0xf, 1); \
210 emitm(&stream, (0x19 << 3) | dr32, 1); \
214 #define SWAP_AX() do { \
215 emitm(&stream, 0xc486, 2); \
220 emitm(&stream, 0xc3, 1); \
224 #define ADDrd(sr32, dr32) do { \
225 emitm(&stream, 0x01, 1); \
227 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
231 #define ADD_EAXi(i32) do { \
232 emitm(&stream, 0x05, 1); \
233 emitm(&stream, i32, 4); \
237 #define ADDib(i8, r32) do { \
238 emitm(&stream, 0x83, 1); \
239 emitm(&stream, (24 << 3) | r32, 1); \
240 emitm(&stream, i8, 1); \
244 #define SUBrd(sr32, dr32) do { \
245 emitm(&stream, 0x29, 1); \
247 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
251 #define SUB_EAXi(i32) do { \
252 emitm(&stream, 0x2d, 1); \
253 emitm(&stream, i32, 4); \
257 #define MULrd(r32) do { \
258 emitm(&stream, 0xf7, 1); \
259 emitm(&stream, (7 << 5) | (r32 & 0x7), 1); \
263 #define DIVrd(r32) do { \
264 emitm(&stream, 0xf7, 1); \
265 emitm(&stream, (15 << 4) | (r32 & 0x7), 1); \
269 #define ANDib(i8, r8) do { \
271 emitm(&stream, 0x24, 1); \
273 emitm(&stream, 0x80, 1); \
274 emitm(&stream, (7 << 5) | r8, 1); \
276 emitm(&stream, i8, 1); \
280 #define ANDid(i32, r32) do { \
282 emitm(&stream, 0x25, 1); \
284 emitm(&stream, 0x81, 1); \
285 emitm(&stream, (7 << 5) | r32, 1); \
287 emitm(&stream, i32, 4); \
291 #define ANDrd(sr32, dr32) do { \
292 emitm(&stream, 0x21, 1); \
294 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
298 #define TESTid(i32, r32) do { \
300 emitm(&stream, 0xa9, 1); \
302 emitm(&stream, 0xf7, 1); \
303 emitm(&stream, (3 << 6) | r32, 1); \
305 emitm(&stream, i32, 4); \
308 /* testl sr32,dr32 */
309 #define TESTrd(sr32, dr32) do { \
310 emitm(&stream, 0x85, 1); \
312 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
316 #define ORrd(sr32, dr32) do { \
317 emitm(&stream, 0x09, 1); \
319 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
323 #define ORid(i32, r32) do { \
325 emitm(&stream, 0x0d, 1); \
327 emitm(&stream, 0x81, 1); \
328 emitm(&stream, (25 << 3) | r32, 1); \
330 emitm(&stream, i32, 4); \
334 #define SHLib(i8, r32) do { \
335 emitm(&stream, 0xc1, 1); \
336 emitm(&stream, (7 << 5) | (r32 & 0x7), 1); \
337 emitm(&stream, i8, 1); \
341 #define SHL_CLrb(dr32) do { \
342 emitm(&stream, 0xd3, 1); \
343 emitm(&stream, (7 << 5) | (dr32 & 0x7), 1); \
347 #define SHRib(i8, r32) do { \
348 emitm(&stream, 0xc1, 1); \
349 emitm(&stream, (29 << 3) | (r32 & 0x7), 1); \
350 emitm(&stream, i8, 1); \
354 #define SHR_CLrb(dr32) do { \
355 emitm(&stream, 0xd3, 1); \
356 emitm(&stream, (29 << 3) | (dr32 & 0x7), 1); \
360 #define NEGd(r32) do { \
361 emitm(&stream, 0xf7, 1); \
362 emitm(&stream, (27 << 3) | (r32 & 0x7), 1); \
366 #define CMPrd(sr32, dr32) do { \
367 emitm(&stream, 0x39, 1); \
369 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
373 #define CMPid(i32, dr32) do { \
375 emitm(&stream, 0x3d, 1); \
376 emitm(&stream, i32, 4); \
378 emitm(&stream, 0x81, 1); \
379 emitm(&stream, (0x1f << 3) | (dr32 & 0x7), 1); \
380 emitm(&stream, i32, 4); \
385 #define JBb(off8) do { \
386 emitm(&stream, 0x72, 1); \
387 emitm(&stream, off8, 1); \
391 #define JAEb(off8) do { \
392 emitm(&stream, 0x73, 1); \
393 emitm(&stream, off8, 1); \
397 #define JNEb(off8) do { \
398 emitm(&stream, 0x75, 1); \
399 emitm(&stream, off8, 1); \
403 #define JAb(off8) do { \
404 emitm(&stream, 0x77, 1); \
405 emitm(&stream, off8, 1); \
409 #define JMP(off32) do { \
410 emitm(&stream, 0xe9, 1); \
411 emitm(&stream, off32, 4); \
415 #define ZEROrd(r32) do { \
416 emitm(&stream, 0x31, 1); \
417 emitm(&stream, (3 << 6) | ((r32 & 0x7) << 3) | (r32 & 0x7), 1); \
421 * Conditional long jumps
430 #define JCC(t, f) do { \
431 if (ins->jt != 0 && ins->jf != 0) { \
432 /* 5 is the size of the following jmp */ \
433 emitm(&stream, ((t) << 8) | 0x0f, 2); \
434 emitm(&stream, stream.refs[stream.bpf_pc + ins->jt] - \
435 stream.refs[stream.bpf_pc] + 5, 4); \
436 JMP(stream.refs[stream.bpf_pc + ins->jf] - \
437 stream.refs[stream.bpf_pc]); \
438 } else if (ins->jt != 0) { \
439 emitm(&stream, ((t) << 8) | 0x0f, 2); \
440 emitm(&stream, stream.refs[stream.bpf_pc + ins->jt] - \
441 stream.refs[stream.bpf_pc], 4); \
443 emitm(&stream, ((f) << 8) | 0x0f, 2); \
444 emitm(&stream, stream.refs[stream.bpf_pc + ins->jf] - \
445 stream.refs[stream.bpf_pc], 4); \
449 #endif /* _BPF_JIT_MACHDEP_H_ */