1fc55d94dchagin/*-
2fc55d94dchagin * Copyright (c) 2015 Dmitry Chagin
3fc55d94dchagin * All rights reserved.
4fc55d94dchagin *
5fc55d94dchagin * Redistribution and use in source and binary forms, with or without
6fc55d94dchagin * modification, are permitted provided that the following conditions
7fc55d94dchagin * are met:
8fc55d94dchagin * 1. Redistributions of source code must retain the above copyright
9fc55d94dchagin *    notice, this list of conditions and the following disclaimer.
10fc55d94dchagin * 2. Redistributions in binary form must reproduce the above copyright
11fc55d94dchagin *    notice, this list of conditions and the following disclaimer in the
12fc55d94dchagin *    documentation and/or other materials provided with the distribution.
13fc55d94dchagin *
14fc55d94dchagin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15fc55d94dchagin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16fc55d94dchagin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17fc55d94dchagin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18fc55d94dchagin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19fc55d94dchagin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20fc55d94dchagin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21fc55d94dchagin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22fc55d94dchagin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23fc55d94dchagin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24fc55d94dchagin * SUCH DAMAGE.
25fc55d94dchagin *
26fc55d94dchagin * $FreeBSD$
27fc55d94dchagin */
28fc55d94dchagin
29fc55d94dchagin#ifndef _LINUX_MI_H_
30fc55d94dchagin#define _LINUX_MI_H_
31fc55d94dchagin
32cdba6f9markj#include <sys/queue.h>
33cdba6f9markj
3490cdaa0dchagin#define	LINUX_IFHWADDRLEN	6
3590cdaa0dchagin#define	LINUX_IFNAMSIZ		16
3690cdaa0dchagin
3790cdaa0dchagin/*
3890cdaa0dchagin * Criteria for interface name translation
3990cdaa0dchagin */
4090cdaa0dchagin#define	IFP_IS_ETH(ifp)		(ifp->if_type == IFT_ETHER)
4190cdaa0dchagin#define	IFP_IS_LOOP(ifp)	(ifp->if_type == IFT_LOOP)
4290cdaa0dchagin
4390cdaa0dchaginstruct l_sockaddr {
4490cdaa0dchagin	unsigned short	sa_family;
4590cdaa0dchagin	char		sa_data[14];
4690cdaa0dchagin};
4790cdaa0dchagin
4890cdaa0dchagin#define	LINUX_ARPHRD_ETHER	1
4990cdaa0dchagin#define	LINUX_ARPHRD_LOOPBACK	772
5090cdaa0dchagin
51b83adbedchagin/*
5257102bcdchagin * Supported address families
5357102bcdchagin */
5457102bcdchagin#define	LINUX_AF_UNSPEC		0
5557102bcdchagin#define	LINUX_AF_UNIX		1
5657102bcdchagin#define	LINUX_AF_INET		2
5757102bcdchagin#define	LINUX_AF_AX25		3
5857102bcdchagin#define	LINUX_AF_IPX		4
5957102bcdchagin#define	LINUX_AF_APPLETALK	5
6057102bcdchagin#define	LINUX_AF_INET6		10
6157102bcdchagin
6257102bcdchagin/*
63b83adbedchagin * net device flags
64b83adbedchagin */
65b83adbedchagin#define	LINUX_IFF_UP		0x0001
66b83adbedchagin#define	LINUX_IFF_BROADCAST	0x0002
67b83adbedchagin#define	LINUX_IFF_DEBUG		0x0004
68b83adbedchagin#define	LINUX_IFF_LOOPBACK	0x0008
69b83adbedchagin#define	LINUX_IFF_POINTOPOINT	0x0010
70b83adbedchagin#define	LINUX_IFF_NOTRAILERS	0x0020
71b83adbedchagin#define	LINUX_IFF_RUNNING	0x0040
72b83adbedchagin#define	LINUX_IFF_NOARP		0x0080
73b83adbedchagin#define	LINUX_IFF_PROMISC	0x0100
74b83adbedchagin#define	LINUX_IFF_ALLMULTI	0x0200
75b83adbedchagin#define	LINUX_IFF_MASTER	0x0400
76b83adbedchagin#define	LINUX_IFF_SLAVE		0x0800
77b83adbedchagin#define	LINUX_IFF_MULTICAST	0x1000
78b83adbedchagin#define	LINUX_IFF_PORTSEL	0x2000
79b83adbedchagin#define	LINUX_IFF_AUTOMEDIA	0x4000
80b83adbedchagin#define	LINUX_IFF_DYNAMIC	0x8000
81b83adbedchagin
82fc55d94dchagin/* sigaltstack */
83fc55d94dchagin#define	LINUX_SS_ONSTACK	1
84fc55d94dchagin#define	LINUX_SS_DISABLE	2
85fc55d94dchagin
86fc55d94dchaginint linux_to_bsd_sigaltstack(int lsa);
87fc55d94dchaginint bsd_to_linux_sigaltstack(int bsa);
88fc55d94dchagin
89fc55d94dchagin/* sigset */
90fc55d94dchagintypedef struct {
91fc55d94dchagin	uint64_t	__mask;
92fc55d94dchagin} l_sigset_t;
93fc55d94dchagin
94fc55d94dchagin/* primitives to manipulate sigset_t */
95fc55d94dchagin#define	LINUX_SIGEMPTYSET(set)		(set).__mask = 0
96fc55d94dchagin#define	LINUX_SIGISMEMBER(set, sig)	(1UL & ((set).__mask >> _SIG_IDX(sig)))
97fc55d94dchagin#define	LINUX_SIGADDSET(set, sig)	(set).__mask |= 1UL << _SIG_IDX(sig)
98fc55d94dchagin
99fc55d94dchaginvoid linux_to_bsd_sigset(l_sigset_t *, sigset_t *);
100fc55d94dchaginvoid bsd_to_linux_sigset(sigset_t *, l_sigset_t *);
101fc55d94dchagin
102fc55d94dchagin/* signaling */
103fc55d94dchagin#define	LINUX_SIGHUP		1
104fc55d94dchagin#define	LINUX_SIGINT		2
105fc55d94dchagin#define	LINUX_SIGQUIT		3
106fc55d94dchagin#define	LINUX_SIGILL		4
107fc55d94dchagin#define	LINUX_SIGTRAP		5
108fc55d94dchagin#define	LINUX_SIGABRT		6
109fc55d94dchagin#define	LINUX_SIGIOT		LINUX_SIGABRT
110fc55d94dchagin#define	LINUX_SIGBUS		7
111fc55d94dchagin#define	LINUX_SIGFPE		8
112fc55d94dchagin#define	LINUX_SIGKILL		9
113fc55d94dchagin#define	LINUX_SIGUSR1		10
114fc55d94dchagin#define	LINUX_SIGSEGV		11
115fc55d94dchagin#define	LINUX_SIGUSR2		12
116fc55d94dchagin#define	LINUX_SIGPIPE		13
117fc55d94dchagin#define	LINUX_SIGALRM		14
118fc55d94dchagin#define	LINUX_SIGTERM		15
119fc55d94dchagin#define	LINUX_SIGSTKFLT		16
120fc55d94dchagin#define	LINUX_SIGCHLD		17
121fc55d94dchagin#define	LINUX_SIGCONT		18
122fc55d94dchagin#define	LINUX_SIGSTOP		19
123fc55d94dchagin#define	LINUX_SIGTSTP		20
124fc55d94dchagin#define	LINUX_SIGTTIN		21
125fc55d94dchagin#define	LINUX_SIGTTOU		22
126fc55d94dchagin#define	LINUX_SIGURG		23
127fc55d94dchagin#define	LINUX_SIGXCPU		24
128fc55d94dchagin#define	LINUX_SIGXFSZ		25
129fc55d94dchagin#define	LINUX_SIGVTALRM		26
130fc55d94dchagin#define	LINUX_SIGPROF		27
131fc55d94dchagin#define	LINUX_SIGWINCH		28
132fc55d94dchagin#define	LINUX_SIGIO		29
133fc55d94dchagin#define	LINUX_SIGPOLL		LINUX_SIGIO
134fc55d94dchagin#define	LINUX_SIGPWR		30
135fc55d94dchagin#define	LINUX_SIGSYS		31
136fc55d94dchagin#define	LINUX_SIGTBLSZ		31
137fc55d94dchagin#define	LINUX_SIGRTMIN		32
138fc55d94dchagin#define	LINUX_SIGRTMAX		64
139fc55d94dchagin
140fc55d94dchagin#define LINUX_SIG_VALID(sig)	((sig) <= LINUX_SIGRTMAX && (sig) > 0)
141fc55d94dchagin
142fc55d94dchaginint linux_to_bsd_signal(int sig);
143fc55d94dchaginint bsd_to_linux_signal(int sig);
144fc55d94dchagin
145fb90deayuripvextern LIST_HEAD(futex_list, futex) futex_list;
146ad16acfyuripvextern struct mtx futex_mtx;
147fb90deayuripv
1487b2d70etraszvoid linux_dev_shm_create(void);
1497b2d70etraszvoid linux_dev_shm_destroy(void);
1507b2d70etrasz
151145ddbakevans/*
152145ddbakevans * mask=0 is not sensible for this application, so it will be taken to mean
153145ddbakevans * a mask equivalent to the value.  Otherwise, (word & mask) == value maps to
154145ddbakevans * (word & ~mask) | value in a bitfield for the platform we're converting to.
155145ddbakevans */
156145ddbakevansstruct bsd_to_linux_bitmap {
157145ddbakevans	int	bsd_mask;
158145ddbakevans	int	bsd_value;
159145ddbakevans	int	linux_mask;
160145ddbakevans	int	linux_value;
161145ddbakevans};
162145ddbakevans
163145ddbakevansint bsd_to_linux_bits_(int value, struct bsd_to_linux_bitmap *bitmap,
164145ddbakevans    size_t mapcnt, int no_value);
165145ddbakevansint linux_to_bsd_bits_(int value, struct bsd_to_linux_bitmap *bitmap,
166145ddbakevans    size_t mapcnt, int no_value);
167145ddbakevans
168145ddbakevans/*
169145ddbakevans * These functions are used for simplification of BSD <-> Linux bit conversions.
170145ddbakevans * Given `value`, a bit field, these functions will walk the given bitmap table
171145ddbakevans * and set the appropriate bits for the target platform.  If any bits were
172145ddbakevans * successfully converted, then the return value is the equivalent of value
173145ddbakevans * represented with the bit values appropriate for the target platform.
174145ddbakevans * Otherwise, the value supplied as `no_value` is returned.
175145ddbakevans */
176283c1fekevans#define	bsd_to_linux_bits(_val, _bmap, _noval) \
177283c1fekevans    bsd_to_linux_bits_((_val), (_bmap), nitems((_bmap)), (_noval))
178145ddbakevans#define	linux_to_bsd_bits(_val, _bmap, _noval) \
179145ddbakevans    linux_to_bsd_bits_((_val), (_bmap), nitems((_bmap)), (_noval))
180145ddbakevans
181145ddbakevans/*
182145ddbakevans * Easy mapping helpers.  BITMAP_EASY_LINUX represents a single bit to be
183145ddbakevans * translated, and the FreeBSD and Linux values are supplied.  BITMAP_1t1_LINUX
184145ddbakevans * is the extreme version of this, where not only is it a single bit, but the
185145ddbakevans * name of the macro used to represent the Linux version of a bit literally has
186145ddbakevans * LINUX_ prepended to the normal name.
187145ddbakevans */
188145ddbakevans#define	BITMAP_EASY_LINUX(_name, _linux_name)	\
189145ddbakevans	{					\
190145ddbakevans		.bsd_value = (_name),		\
191145ddbakevans		.linux_value = (_linux_name),	\
192145ddbakevans	}
193145ddbakevans#define	BITMAP_1t1_LINUX(_name)	BITMAP_EASY_LINUX(_name, LINUX_##_name)
194145ddbakevans
195371726dtraszint linux_to_bsd_errno(int error);
196371726dtrasz
197fc55d94dchagin#endif /* _LINUX_MI_H_ */
198