2 * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (C) 2005-2016 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 /* Optimization flags */
64 #define BPF_JIT_FRET 0x01
65 #define BPF_JIT_FPKT 0x02
66 #define BPF_JIT_FMEM 0x04
67 #define BPF_JIT_FJMP 0x08
68 #define BPF_JIT_FADK 0x10
70 #define BPF_JIT_FLAG_ALL \
71 (BPF_JIT_FPKT | BPF_JIT_FMEM | BPF_JIT_FJMP | BPF_JIT_FADK)
73 /* A stream of native binary code */
74 typedef struct bpf_bin_stream {
75 /* Current native instruction pointer. */
79 * Current BPF instruction pointer, i.e. position in
80 * the BPF program reached by the jitter.
84 /* Instruction buffer, contains the generated native code. */
87 /* Jumps reference table. */
92 * Prototype of the emit functions.
94 * Different emit functions are used to create the reference table and
95 * to generate the actual filtering code. This allows to have simpler
97 * The first parameter is the stream that will receive the data.
98 * The second one is a variable containing the data.
99 * The third one is the length, that can be 1, 2, or 4 since it is possible
100 * to emit a byte, a short, or a word at a time.
102 typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
105 * Native instruction macros
109 #define MOVid(i32, r32) do { \
110 emitm(&stream, (11 << 4) | (1 << 3) | (r32 & 0x7), 1); \
111 emitm(&stream, i32, 4); \
115 #define MOVrd(sr32, dr32) do { \
116 emitm(&stream, 0x89, 1); \
118 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
121 /* movl off(sr32),dr32 */
122 #define MOVodd(off, sr32, dr32) do { \
123 emitm(&stream, 0x8b, 1); \
125 (1 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
126 emitm(&stream, off, 1); \
129 /* movl (sr32,or32,1),dr32 */
130 #define MOVobd(sr32, or32, dr32) do { \
131 emitm(&stream, 0x8b, 1); \
132 emitm(&stream, ((dr32 & 0x7) << 3) | 4, 1); \
133 emitm(&stream, ((or32 & 0x7) << 3) | (sr32 & 0x7), 1); \
136 /* movw (sr32,or32,1),dr16 */
137 #define MOVobw(sr32, or32, dr16) do { \
138 emitm(&stream, 0x8b66, 2); \
139 emitm(&stream, ((dr16 & 0x7) << 3) | 4, 1); \
140 emitm(&stream, ((or32 & 0x7) << 3) | (sr32 & 0x7), 1); \
143 /* movb (sr32,or32,1),dr8 */
144 #define MOVobb(sr32, or32, dr8) do { \
145 emitm(&stream, 0x8a, 1); \
146 emitm(&stream, ((dr8 & 0x7) << 3) | 4, 1); \
147 emitm(&stream, ((or32 & 0x7) << 3) | (sr32 & 0x7), 1); \
150 /* movl sr32,(dr32,or32,1) */
151 #define MOVomd(sr32, dr32, or32) do { \
152 emitm(&stream, 0x89, 1); \
153 emitm(&stream, ((sr32 & 0x7) << 3) | 4, 1); \
154 emitm(&stream, ((or32 & 0x7) << 3) | (dr32 & 0x7), 1); \
158 #define BSWAP(dr32) do { \
159 emitm(&stream, 0xf, 1); \
160 emitm(&stream, (0x19 << 3) | dr32, 1); \
164 #define SWAP_AX() do { \
165 emitm(&stream, 0xc486, 2); \
169 #define PUSH(r32) do { \
170 emitm(&stream, (5 << 4) | (0 << 3) | (r32 & 0x7), 1); \
174 #define POP(r32) do { \
175 emitm(&stream, (5 << 4) | (1 << 3) | (r32 & 0x7), 1); \
179 #define LEAVE() do { \
180 emitm(&stream, 0xc9, 1); \
185 emitm(&stream, 0xc3, 1); \
189 #define ADDrd(sr32, dr32) do { \
190 emitm(&stream, 0x01, 1); \
192 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
196 #define ADD_EAXi(i32) do { \
197 emitm(&stream, 0x05, 1); \
198 emitm(&stream, i32, 4); \
202 #define ADDib(i8, r32) do { \
203 emitm(&stream, 0x83, 1); \
204 emitm(&stream, (24 << 3) | r32, 1); \
205 emitm(&stream, i8, 1); \
209 #define SUBrd(sr32, dr32) do { \
210 emitm(&stream, 0x29, 1); \
212 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
216 #define SUB_EAXi(i32) do { \
217 emitm(&stream, 0x2d, 1); \
218 emitm(&stream, i32, 4); \
222 #define SUBib(i8, r32) do { \
223 emitm(&stream, 0x83, 1); \
224 emitm(&stream, (29 << 3) | (r32 & 0x7), 1); \
225 emitm(&stream, i8, 1); \
229 #define MULrd(r32) do { \
230 emitm(&stream, 0xf7, 1); \
231 emitm(&stream, (7 << 5) | (r32 & 0x7), 1); \
235 #define DIVrd(r32) do { \
236 emitm(&stream, 0xf7, 1); \
237 emitm(&stream, (15 << 4) | (r32 & 0x7), 1); \
241 #define ANDib(i8, r8) do { \
243 emitm(&stream, 0x24, 1); \
245 emitm(&stream, 0x80, 1); \
246 emitm(&stream, (7 << 5) | r8, 1); \
248 emitm(&stream, i8, 1); \
252 #define ANDid(i32, r32) do { \
254 emitm(&stream, 0x25, 1); \
256 emitm(&stream, 0x81, 1); \
257 emitm(&stream, (7 << 5) | r32, 1); \
259 emitm(&stream, i32, 4); \
263 #define ANDrd(sr32, dr32) do { \
264 emitm(&stream, 0x21, 1); \
266 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
270 #define TESTid(i32, r32) do { \
272 emitm(&stream, 0xa9, 1); \
274 emitm(&stream, 0xf7, 1); \
275 emitm(&stream, (3 << 6) | r32, 1); \
277 emitm(&stream, i32, 4); \
280 /* testl sr32,dr32 */
281 #define TESTrd(sr32, dr32) do { \
282 emitm(&stream, 0x85, 1); \
284 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
288 #define ORrd(sr32, dr32) do { \
289 emitm(&stream, 0x09, 1); \
291 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
295 #define ORid(i32, r32) do { \
297 emitm(&stream, 0x0d, 1); \
299 emitm(&stream, 0x81, 1); \
300 emitm(&stream, (25 << 3) | r32, 1); \
302 emitm(&stream, i32, 4); \
306 #define XORrd(sr32, dr32) do { \
307 emitm(&stream, 0x31, 1); \
309 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
313 #define XORid(i32, r32) do { \
315 emitm(&stream, 0x35, 1); \
317 emitm(&stream, 0x81, 1); \
318 emitm(&stream, (25 << 3) | r32, 1); \
320 emitm(&stream, i32, 4); \
324 #define SHLib(i8, r32) do { \
325 emitm(&stream, 0xc1, 1); \
326 emitm(&stream, (7 << 5) | (r32 & 0x7), 1); \
327 emitm(&stream, i8, 1); \
331 #define SHL_CLrb(dr32) do { \
332 emitm(&stream, 0xd3, 1); \
333 emitm(&stream, (7 << 5) | (dr32 & 0x7), 1); \
337 #define SHRib(i8, r32) do { \
338 emitm(&stream, 0xc1, 1); \
339 emitm(&stream, (29 << 3) | (r32 & 0x7), 1); \
340 emitm(&stream, i8, 1); \
344 #define SHR_CLrb(dr32) do { \
345 emitm(&stream, 0xd3, 1); \
346 emitm(&stream, (29 << 3) | (dr32 & 0x7), 1); \
350 #define NEGd(r32) do { \
351 emitm(&stream, 0xf7, 1); \
352 emitm(&stream, (27 << 3) | (r32 & 0x7), 1); \
356 #define CMPrd(sr32, dr32) do { \
357 emitm(&stream, 0x39, 1); \
359 (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
363 #define CMPid(i32, dr32) do { \
365 emitm(&stream, 0x3d, 1); \
366 emitm(&stream, i32, 4); \
368 emitm(&stream, 0x81, 1); \
369 emitm(&stream, (0x1f << 3) | (dr32 & 0x7), 1); \
370 emitm(&stream, i32, 4); \
375 #define JBb(off8) do { \
376 emitm(&stream, 0x72, 1); \
377 emitm(&stream, off8, 1); \
381 #define JAEb(off8) do { \
382 emitm(&stream, 0x73, 1); \
383 emitm(&stream, off8, 1); \
387 #define JNEb(off8) do { \
388 emitm(&stream, 0x75, 1); \
389 emitm(&stream, off8, 1); \
393 #define JAb(off8) do { \
394 emitm(&stream, 0x77, 1); \
395 emitm(&stream, off8, 1); \
399 #define JMP(off32) do { \
400 emitm(&stream, 0xe9, 1); \
401 emitm(&stream, off32, 4); \
405 #define ZEROrd(r32) do { \
406 emitm(&stream, 0x31, 1); \
407 emitm(&stream, (3 << 6) | ((r32 & 0x7) << 3) | (r32 & 0x7), 1); \
411 * Conditional long jumps
420 #define JCC(t, f) do { \
421 if (ins->jt != 0 && ins->jf != 0) { \
422 /* 5 is the size of the following jmp */ \
423 emitm(&stream, ((t) << 8) | 0x0f, 2); \
424 emitm(&stream, stream.refs[stream.bpf_pc + ins->jt] - \
425 stream.refs[stream.bpf_pc] + 5, 4); \
426 JMP(stream.refs[stream.bpf_pc + ins->jf] - \
427 stream.refs[stream.bpf_pc]); \
428 } else if (ins->jt != 0) { \
429 emitm(&stream, ((t) << 8) | 0x0f, 2); \
430 emitm(&stream, stream.refs[stream.bpf_pc + ins->jt] - \
431 stream.refs[stream.bpf_pc], 4); \
433 emitm(&stream, ((f) << 8) | 0x0f, 2); \
434 emitm(&stream, stream.refs[stream.bpf_pc + ins->jf] - \
435 stream.refs[stream.bpf_pc], 4); \
439 #define JUMP(off) do { \
441 JMP(stream.refs[stream.bpf_pc + (off)] - \
442 stream.refs[stream.bpf_pc]); \
445 #endif /* _BPF_JIT_MACHDEP_H_ */