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