xref: /freebsd/sys/compat/linux/linux.h (revision 95e33099c74d9fc7963968f052b7546ad3040317)
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 typedef uint32_t	l_dev_t;
32 
33 static __inline int
34 linux_decode_major(l_dev_t _dev)
35 {
36 
37 	return ((_dev & 0xfff00) >> 8);
38 }
39 
40 static __inline int
41 linux_decode_minor(l_dev_t _dev)
42 {
43 
44 	return ((_dev & 0xff) | ((_dev & 0xfff00000) >> 12));
45 }
46 
47 static __inline dev_t
48 linux_decode_dev(l_dev_t _dev)
49 {
50 
51 	return (makedev(linux_decode_major(_dev), linux_decode_minor(_dev)));
52 }
53 
54 /*
55  * Private Brandinfo flags
56  */
57 #define	LINUX_BI_FUTEX_REQUEUE	0x01000000
58 
59 /*
60  * poll()
61  */
62 #define	LINUX_POLLIN		0x0001
63 #define	LINUX_POLLPRI		0x0002
64 #define	LINUX_POLLOUT		0x0004
65 #define	LINUX_POLLERR		0x0008
66 #define	LINUX_POLLHUP		0x0010
67 #define	LINUX_POLLNVAL		0x0020
68 #define	LINUX_POLLRDNORM	0x0040
69 #define	LINUX_POLLRDBAND	0x0080
70 #define	LINUX_POLLWRNORM	0x0100
71 #define	LINUX_POLLWRBAND	0x0200
72 #define	LINUX_POLLMSG		0x0400
73 #define	LINUX_POLLREMOVE	0x1000
74 #define	LINUX_POLLRDHUP		0x2000
75 
76 #define	LINUX_IFHWADDRLEN	6
77 #define	LINUX_IFNAMSIZ		16
78 
79 struct l_sockaddr {
80 	unsigned short	sa_family;
81 	char		sa_data[14];
82 };
83 
84 #define	LINUX_ARPHRD_ETHER	1
85 #define	LINUX_ARPHRD_LOOPBACK	772
86 #define	LINUX_ARPHRD_SIT	776
87 
88 /*
89  * Supported address families
90  */
91 #define	LINUX_AF_UNSPEC		0
92 #define	LINUX_AF_UNIX		1
93 #define	LINUX_AF_INET		2
94 #define	LINUX_AF_AX25		3
95 #define	LINUX_AF_IPX		4
96 #define	LINUX_AF_APPLETALK	5
97 #define	LINUX_AF_INET6		10
98 #define	LINUX_AF_NETLINK	16
99 
100 #define	LINUX_NETLINK_ROUTE		0
101 #define	LINUX_NETLINK_SOCK_DIAG		4
102 #define	LINUX_NETLINK_NFLOG		5
103 #define	LINUX_NETLINK_SELINUX		7
104 #define	LINUX_NETLINK_AUDIT		9
105 #define	LINUX_NETLINK_FIB_LOOKUP	10
106 #define	LINUX_NETLINK_NETFILTER		12
107 #define	LINUX_NETLINK_KOBJECT_UEVENT	15
108 
109 /*
110  * net device flags
111  */
112 #define	LINUX_IFF_UP		0x0001
113 #define	LINUX_IFF_BROADCAST	0x0002
114 #define	LINUX_IFF_DEBUG		0x0004
115 #define	LINUX_IFF_LOOPBACK	0x0008
116 #define	LINUX_IFF_POINTOPOINT	0x0010
117 #define	LINUX_IFF_NOTRAILERS	0x0020
118 #define	LINUX_IFF_RUNNING	0x0040
119 #define	LINUX_IFF_NOARP		0x0080
120 #define	LINUX_IFF_PROMISC	0x0100
121 #define	LINUX_IFF_ALLMULTI	0x0200
122 #define	LINUX_IFF_MASTER	0x0400
123 #define	LINUX_IFF_SLAVE		0x0800
124 #define	LINUX_IFF_MULTICAST	0x1000
125 #define	LINUX_IFF_PORTSEL	0x2000
126 #define	LINUX_IFF_AUTOMEDIA	0x4000
127 #define	LINUX_IFF_DYNAMIC	0x8000
128 /* Volatile (netlink / ifinfomsg); not representable in ifr_flags short */
129 #define	LINUX_IFF_LOWER_UP	0x10000
130 #define	LINUX_IFF_DORMANT	0x20000
131 
132 /* sigaltstack */
133 #define	LINUX_SS_ONSTACK	1
134 #define	LINUX_SS_DISABLE	2
135 
136 int linux_to_bsd_sigaltstack(int lsa);
137 int bsd_to_linux_sigaltstack(int bsa);
138 
139 /* sigset */
140 typedef struct {
141 	uint64_t	__mask;
142 } l_sigset_t;
143 
144 /* primitives to manipulate sigset_t */
145 #define	LINUX_SIGEMPTYSET(set)		(set).__mask = 0
146 #define	LINUX_SIGISMEMBER(set, sig)	(1ULL & ((set).__mask >> _SIG_IDX(sig)))
147 #define	LINUX_SIGADDSET(set, sig)	(set).__mask |= 1ULL << _SIG_IDX(sig)
148 
149 void linux_to_bsd_sigset(l_sigset_t *, sigset_t *);
150 void bsd_to_linux_sigset(sigset_t *, l_sigset_t *);
151 
152 /* signaling */
153 #define	LINUX_SIGHUP		1
154 #define	LINUX_SIGINT		2
155 #define	LINUX_SIGQUIT		3
156 #define	LINUX_SIGILL		4
157 #define	LINUX_SIGTRAP		5
158 #define	LINUX_SIGABRT		6
159 #define	LINUX_SIGIOT		LINUX_SIGABRT
160 #define	LINUX_SIGBUS		7
161 #define	LINUX_SIGFPE		8
162 #define	LINUX_SIGKILL		9
163 #define	LINUX_SIGUSR1		10
164 #define	LINUX_SIGSEGV		11
165 #define	LINUX_SIGUSR2		12
166 #define	LINUX_SIGPIPE		13
167 #define	LINUX_SIGALRM		14
168 #define	LINUX_SIGTERM		15
169 #define	LINUX_SIGSTKFLT		16
170 #define	LINUX_SIGCHLD		17
171 #define	LINUX_SIGCONT		18
172 #define	LINUX_SIGSTOP		19
173 #define	LINUX_SIGTSTP		20
174 #define	LINUX_SIGTTIN		21
175 #define	LINUX_SIGTTOU		22
176 #define	LINUX_SIGURG		23
177 #define	LINUX_SIGXCPU		24
178 #define	LINUX_SIGXFSZ		25
179 #define	LINUX_SIGVTALRM		26
180 #define	LINUX_SIGPROF		27
181 #define	LINUX_SIGWINCH		28
182 #define	LINUX_SIGIO		29
183 #define	LINUX_SIGPOLL		LINUX_SIGIO
184 #define	LINUX_SIGPWR		30
185 #define	LINUX_SIGSYS		31
186 #define	LINUX_SIGTBLSZ		31
187 #define	LINUX_SIGRTMIN		32
188 #define	LINUX_SIGRTMAX		64
189 
190 #define LINUX_SIG_VALID(sig)	((sig) <= LINUX_SIGRTMAX && (sig) > 0)
191 
192 int linux_to_bsd_signal(int sig);
193 int bsd_to_linux_signal(int sig);
194 
195 /* sigprocmask actions */
196 #define	LINUX_SIG_BLOCK		0
197 #define	LINUX_SIG_UNBLOCK	1
198 #define	LINUX_SIG_SETMASK	2
199 
200 void linux_dev_shm_create(void);
201 void linux_dev_shm_destroy(void);
202 
203 /*
204  * mask=0 is not sensible for this application, so it will be taken to mean
205  * a mask equivalent to the value.  Otherwise, (word & mask) == value maps to
206  * (word & ~mask) | value in a bitfield for the platform we're converting to.
207  */
208 struct bsd_to_linux_bitmap {
209 	int	bsd_mask;
210 	int	bsd_value;
211 	int	linux_mask;
212 	int	linux_value;
213 };
214 
215 int bsd_to_linux_bits_(int value, struct bsd_to_linux_bitmap *bitmap,
216     size_t mapcnt, int no_value);
217 int linux_to_bsd_bits_(int value, struct bsd_to_linux_bitmap *bitmap,
218     size_t mapcnt, int no_value);
219 
220 /*
221  * These functions are used for simplification of BSD <-> Linux bit conversions.
222  * Given `value`, a bit field, these functions will walk the given bitmap table
223  * and set the appropriate bits for the target platform.  If any bits were
224  * successfully converted, then the return value is the equivalent of value
225  * represented with the bit values appropriate for the target platform.
226  * Otherwise, the value supplied as `no_value` is returned.
227  */
228 #define	bsd_to_linux_bits(_val, _bmap, _noval) \
229     bsd_to_linux_bits_((_val), (_bmap), nitems((_bmap)), (_noval))
230 #define	linux_to_bsd_bits(_val, _bmap, _noval) \
231     linux_to_bsd_bits_((_val), (_bmap), nitems((_bmap)), (_noval))
232 
233 /*
234  * Easy mapping helpers.  BITMAP_EASY_LINUX represents a single bit to be
235  * translated, and the FreeBSD and Linux values are supplied.  BITMAP_1t1_LINUX
236  * is the extreme version of this, where not only is it a single bit, but the
237  * name of the macro used to represent the Linux version of a bit literally has
238  * LINUX_ prepended to the normal name.
239  */
240 #define	BITMAP_EASY_LINUX(_name, _linux_name)	\
241 	{					\
242 		.bsd_value = (_name),		\
243 		.linux_value = (_linux_name),	\
244 	}
245 #define	BITMAP_1t1_LINUX(_name)	BITMAP_EASY_LINUX(_name, LINUX_##_name)
246 
247 int bsd_to_linux_errno(int error);
248 void linux_check_errtbl(void);
249 
250 #define STATX_BASIC_STATS		0x07ff
251 #define STATX_BTIME			0x0800
252 #define STATX_ALL			0x0fff
253 
254 #define STATX_ATTR_COMPRESSED		0x0004
255 #define STATX_ATTR_IMMUTABLE		0x0010
256 #define STATX_ATTR_APPEND		0x0020
257 #define STATX_ATTR_NODUMP		0x0040
258 #define STATX_ATTR_ENCRYPTED		0x0800
259 #define STATX_ATTR_AUTOMOUNT		0x1000
260 
261 struct l_statx_timestamp {
262 	int64_t tv_sec;
263 	int32_t tv_nsec;
264 	int32_t __spare0;
265 };
266 
267 struct l_statx {
268 	uint32_t stx_mask;
269 	uint32_t stx_blksize;
270 	uint64_t stx_attributes;
271 	uint32_t stx_nlink;
272 	uint32_t stx_uid;
273 	uint32_t stx_gid;
274 	uint16_t stx_mode;
275 	uint16_t __spare0[1];
276 	uint64_t stx_ino;
277 	uint64_t stx_size;
278 	uint64_t stx_blocks;
279 	uint64_t stx_attributes_mask;
280 	struct l_statx_timestamp stx_atime;
281 	struct l_statx_timestamp stx_btime;
282 	struct l_statx_timestamp stx_ctime;
283 	struct l_statx_timestamp stx_mtime;
284 	uint32_t stx_rdev_major;
285 	uint32_t stx_rdev_minor;
286 	uint32_t stx_dev_major;
287 	uint32_t stx_dev_minor;
288 	uint64_t stx_mnt_id;
289 	uint64_t __spare2[13];
290 };
291 
292 /*
293  * statfs f_flags
294  */
295 #define	LINUX_ST_RDONLY			0x0001
296 #define	LINUX_ST_NOSUID			0x0002
297 #define	LINUX_ST_NODEV			0x0004	/* No native analogue */
298 #define	LINUX_ST_NOEXEC			0x0008
299 #define	LINUX_ST_SYNCHRONOUS		0x0010
300 #define	LINUX_ST_VALID			0x0020
301 #define	LINUX_ST_MANDLOCK		0x0040	/* No native analogue */
302 #define	LINUX_ST_NOATIME		0x0400
303 #define	LINUX_ST_NODIRATIME		0x0800	/* No native analogue */
304 #define	LINUX_ST_RELATIME		0x1000	/* No native analogue */
305 #define	LINUX_ST_NOSYMFOLLOW		0x2000
306 
307 #ifndef lower_32_bits
308 #define	lower_32_bits(n)	((uint32_t)((n) & 0xffffffff))
309 #endif
310 
311 #ifdef KTRACE
312 #define	linux_ktrsigset(s, l)	\
313 	ktrstruct("l_sigset_t", (s), l)
314 #endif
315 
316 void linux_ifnet_init(void);
317 void linux_ifnet_uninit(void);
318 void linux_netlink_register(void);
319 void linux_netlink_deregister(void);
320 
321 #endif /* _LINUX_MI_H_ */
322