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