xref: /freebsd/sys/compat/linux/linux.h (revision 2b833162)
1 /*-
2  * Copyright (c) 2015 Dmitry Chagin <dchagin@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #ifndef _LINUX_MI_H_
29 #define _LINUX_MI_H_
30 
31 /*
32  * Private Brandinfo flags
33  */
34 #define	LINUX_BI_FUTEX_REQUEUE	0x01000000
35 
36 /*
37  * poll()
38  */
39 #define	LINUX_POLLIN		0x0001
40 #define	LINUX_POLLPRI		0x0002
41 #define	LINUX_POLLOUT		0x0004
42 #define	LINUX_POLLERR		0x0008
43 #define	LINUX_POLLHUP		0x0010
44 #define	LINUX_POLLNVAL		0x0020
45 #define	LINUX_POLLRDNORM	0x0040
46 #define	LINUX_POLLRDBAND	0x0080
47 #define	LINUX_POLLWRNORM	0x0100
48 #define	LINUX_POLLWRBAND	0x0200
49 #define	LINUX_POLLMSG		0x0400
50 #define	LINUX_POLLREMOVE	0x1000
51 #define	LINUX_POLLRDHUP		0x2000
52 
53 #define	LINUX_IFHWADDRLEN	6
54 #define	LINUX_IFNAMSIZ		16
55 
56 struct l_sockaddr {
57 	unsigned short	sa_family;
58 	char		sa_data[14];
59 };
60 
61 #define	LINUX_ARPHRD_ETHER	1
62 #define	LINUX_ARPHRD_LOOPBACK	772
63 
64 /*
65  * Supported address families
66  */
67 #define	LINUX_AF_UNSPEC		0
68 #define	LINUX_AF_UNIX		1
69 #define	LINUX_AF_INET		2
70 #define	LINUX_AF_AX25		3
71 #define	LINUX_AF_IPX		4
72 #define	LINUX_AF_APPLETALK	5
73 #define	LINUX_AF_INET6		10
74 #define	LINUX_AF_NETLINK	16
75 
76 #define	LINUX_NETLINK_ROUTE		0
77 #define	LINUX_NETLINK_SOCK_DIAG		4
78 #define	LINUX_NETLINK_NFLOG		5
79 #define	LINUX_NETLINK_SELINUX		7
80 #define	LINUX_NETLINK_AUDIT		9
81 #define	LINUX_NETLINK_FIB_LOOKUP	10
82 #define	LINUX_NETLINK_NETFILTER		12
83 #define	LINUX_NETLINK_KOBJECT_UEVENT	15
84 
85 /*
86  * net device flags
87  */
88 #define	LINUX_IFF_UP		0x0001
89 #define	LINUX_IFF_BROADCAST	0x0002
90 #define	LINUX_IFF_DEBUG		0x0004
91 #define	LINUX_IFF_LOOPBACK	0x0008
92 #define	LINUX_IFF_POINTOPOINT	0x0010
93 #define	LINUX_IFF_NOTRAILERS	0x0020
94 #define	LINUX_IFF_RUNNING	0x0040
95 #define	LINUX_IFF_NOARP		0x0080
96 #define	LINUX_IFF_PROMISC	0x0100
97 #define	LINUX_IFF_ALLMULTI	0x0200
98 #define	LINUX_IFF_MASTER	0x0400
99 #define	LINUX_IFF_SLAVE		0x0800
100 #define	LINUX_IFF_MULTICAST	0x1000
101 #define	LINUX_IFF_PORTSEL	0x2000
102 #define	LINUX_IFF_AUTOMEDIA	0x4000
103 #define	LINUX_IFF_DYNAMIC	0x8000
104 
105 /* sigaltstack */
106 #define	LINUX_SS_ONSTACK	1
107 #define	LINUX_SS_DISABLE	2
108 
109 int linux_to_bsd_sigaltstack(int lsa);
110 int bsd_to_linux_sigaltstack(int bsa);
111 
112 /* sigset */
113 typedef struct {
114 	uint64_t	__mask;
115 } l_sigset_t;
116 
117 /* primitives to manipulate sigset_t */
118 #define	LINUX_SIGEMPTYSET(set)		(set).__mask = 0
119 #define	LINUX_SIGISMEMBER(set, sig)	(1ULL & ((set).__mask >> _SIG_IDX(sig)))
120 #define	LINUX_SIGADDSET(set, sig)	(set).__mask |= 1ULL << _SIG_IDX(sig)
121 
122 void linux_to_bsd_sigset(l_sigset_t *, sigset_t *);
123 void bsd_to_linux_sigset(sigset_t *, l_sigset_t *);
124 
125 /* signaling */
126 #define	LINUX_SIGHUP		1
127 #define	LINUX_SIGINT		2
128 #define	LINUX_SIGQUIT		3
129 #define	LINUX_SIGILL		4
130 #define	LINUX_SIGTRAP		5
131 #define	LINUX_SIGABRT		6
132 #define	LINUX_SIGIOT		LINUX_SIGABRT
133 #define	LINUX_SIGBUS		7
134 #define	LINUX_SIGFPE		8
135 #define	LINUX_SIGKILL		9
136 #define	LINUX_SIGUSR1		10
137 #define	LINUX_SIGSEGV		11
138 #define	LINUX_SIGUSR2		12
139 #define	LINUX_SIGPIPE		13
140 #define	LINUX_SIGALRM		14
141 #define	LINUX_SIGTERM		15
142 #define	LINUX_SIGSTKFLT		16
143 #define	LINUX_SIGCHLD		17
144 #define	LINUX_SIGCONT		18
145 #define	LINUX_SIGSTOP		19
146 #define	LINUX_SIGTSTP		20
147 #define	LINUX_SIGTTIN		21
148 #define	LINUX_SIGTTOU		22
149 #define	LINUX_SIGURG		23
150 #define	LINUX_SIGXCPU		24
151 #define	LINUX_SIGXFSZ		25
152 #define	LINUX_SIGVTALRM		26
153 #define	LINUX_SIGPROF		27
154 #define	LINUX_SIGWINCH		28
155 #define	LINUX_SIGIO		29
156 #define	LINUX_SIGPOLL		LINUX_SIGIO
157 #define	LINUX_SIGPWR		30
158 #define	LINUX_SIGSYS		31
159 #define	LINUX_SIGTBLSZ		31
160 #define	LINUX_SIGRTMIN		32
161 #define	LINUX_SIGRTMAX		64
162 
163 #define LINUX_SIG_VALID(sig)	((sig) <= LINUX_SIGRTMAX && (sig) > 0)
164 
165 int linux_to_bsd_signal(int sig);
166 int bsd_to_linux_signal(int sig);
167 
168 /* sigprocmask actions */
169 #define	LINUX_SIG_BLOCK		0
170 #define	LINUX_SIG_UNBLOCK	1
171 #define	LINUX_SIG_SETMASK	2
172 
173 void linux_dev_shm_create(void);
174 void linux_dev_shm_destroy(void);
175 
176 /*
177  * mask=0 is not sensible for this application, so it will be taken to mean
178  * a mask equivalent to the value.  Otherwise, (word & mask) == value maps to
179  * (word & ~mask) | value in a bitfield for the platform we're converting to.
180  */
181 struct bsd_to_linux_bitmap {
182 	int	bsd_mask;
183 	int	bsd_value;
184 	int	linux_mask;
185 	int	linux_value;
186 };
187 
188 int bsd_to_linux_bits_(int value, struct bsd_to_linux_bitmap *bitmap,
189     size_t mapcnt, int no_value);
190 int linux_to_bsd_bits_(int value, struct bsd_to_linux_bitmap *bitmap,
191     size_t mapcnt, int no_value);
192 
193 /*
194  * These functions are used for simplification of BSD <-> Linux bit conversions.
195  * Given `value`, a bit field, these functions will walk the given bitmap table
196  * and set the appropriate bits for the target platform.  If any bits were
197  * successfully converted, then the return value is the equivalent of value
198  * represented with the bit values appropriate for the target platform.
199  * Otherwise, the value supplied as `no_value` is returned.
200  */
201 #define	bsd_to_linux_bits(_val, _bmap, _noval) \
202     bsd_to_linux_bits_((_val), (_bmap), nitems((_bmap)), (_noval))
203 #define	linux_to_bsd_bits(_val, _bmap, _noval) \
204     linux_to_bsd_bits_((_val), (_bmap), nitems((_bmap)), (_noval))
205 
206 /*
207  * Easy mapping helpers.  BITMAP_EASY_LINUX represents a single bit to be
208  * translated, and the FreeBSD and Linux values are supplied.  BITMAP_1t1_LINUX
209  * is the extreme version of this, where not only is it a single bit, but the
210  * name of the macro used to represent the Linux version of a bit literally has
211  * LINUX_ prepended to the normal name.
212  */
213 #define	BITMAP_EASY_LINUX(_name, _linux_name)	\
214 	{					\
215 		.bsd_value = (_name),		\
216 		.linux_value = (_linux_name),	\
217 	}
218 #define	BITMAP_1t1_LINUX(_name)	BITMAP_EASY_LINUX(_name, LINUX_##_name)
219 
220 int bsd_to_linux_errno(int error);
221 void linux_check_errtbl(void);
222 
223 #define STATX_BASIC_STATS		0x07ff
224 #define STATX_BTIME			0x0800
225 #define STATX_ALL			0x0fff
226 
227 #define STATX_ATTR_COMPRESSED		0x0004
228 #define STATX_ATTR_IMMUTABLE		0x0010
229 #define STATX_ATTR_APPEND		0x0020
230 #define STATX_ATTR_NODUMP		0x0040
231 #define STATX_ATTR_ENCRYPTED		0x0800
232 #define STATX_ATTR_AUTOMOUNT		0x1000
233 
234 struct l_statx_timestamp {
235 	int64_t tv_sec;
236 	int32_t tv_nsec;
237 	int32_t __spare0;
238 };
239 
240 struct l_statx {
241 	uint32_t stx_mask;
242 	uint32_t stx_blksize;
243 	uint64_t stx_attributes;
244 	uint32_t stx_nlink;
245 	uint32_t stx_uid;
246 	uint32_t stx_gid;
247 	uint16_t stx_mode;
248 	uint16_t __spare0[1];
249 	uint64_t stx_ino;
250 	uint64_t stx_size;
251 	uint64_t stx_blocks;
252 	uint64_t stx_attributes_mask;
253 	struct l_statx_timestamp stx_atime;
254 	struct l_statx_timestamp stx_btime;
255 	struct l_statx_timestamp stx_ctime;
256 	struct l_statx_timestamp stx_mtime;
257 	uint32_t stx_rdev_major;
258 	uint32_t stx_rdev_minor;
259 	uint32_t stx_dev_major;
260 	uint32_t stx_dev_minor;
261 	uint64_t stx_mnt_id;
262 	uint64_t __spare2[13];
263 };
264 
265 /*
266  * statfs f_flags
267  */
268 #define	LINUX_ST_RDONLY			0x0001
269 #define	LINUX_ST_NOSUID			0x0002
270 #define	LINUX_ST_NODEV			0x0004	/* No native analogue */
271 #define	LINUX_ST_NOEXEC			0x0008
272 #define	LINUX_ST_SYNCHRONOUS		0x0010
273 #define	LINUX_ST_VALID			0x0020
274 #define	LINUX_ST_MANDLOCK		0x0040	/* No native analogue */
275 #define	LINUX_ST_NOATIME		0x0400
276 #define	LINUX_ST_NODIRATIME		0x0800	/* No native analogue */
277 #define	LINUX_ST_RELATIME		0x1000	/* No native analogue */
278 #define	LINUX_ST_NOSYMFOLLOW		0x2000
279 
280 #define	lower_32_bits(n)	((uint32_t)((n) & 0xffffffff))
281 
282 #ifdef KTRACE
283 #define	linux_ktrsigset(s, l)	\
284 	ktrstruct("l_sigset_t", (s), l)
285 #endif
286 
287 /*
288  * Criteria for interface name translation
289  */
290 #define	IFP_IS_ETH(ifp)		(if_gettype(ifp) == IFT_ETHER)
291 #define	IFP_IS_LOOP(ifp)	(if_gettype(ifp) == IFT_LOOP)
292 
293 struct ifnet;
294 
295 bool linux_use_real_ifname(const struct ifnet *);
296 
297 void linux_netlink_register(void);
298 void linux_netlink_deregister(void);
299 
300 #endif /* _LINUX_MI_H_ */
301