xref: /freebsd/sys/compat/linux/linux.h (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
1 /*-
2  * Copyright (c) 2015 Dmitry Chagin
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _LINUX_MI_H_
30 #define _LINUX_MI_H_
31 
32 #include <sys/queue.h>
33 
34 /*
35  * poll()
36  */
37 #define	LINUX_POLLIN		0x0001
38 #define	LINUX_POLLPRI		0x0002
39 #define	LINUX_POLLOUT		0x0004
40 #define	LINUX_POLLERR		0x0008
41 #define	LINUX_POLLHUP		0x0010
42 #define	LINUX_POLLNVAL		0x0020
43 #define	LINUX_POLLRDNORM	0x0040
44 #define	LINUX_POLLRDBAND	0x0080
45 #define	LINUX_POLLWRNORM	0x0100
46 #define	LINUX_POLLWRBAND	0x0200
47 #define	LINUX_POLLMSG		0x0400
48 #define	LINUX_POLLREMOVE	0x1000
49 #define	LINUX_POLLRDHUP		0x2000
50 
51 #define	LINUX_IFHWADDRLEN	6
52 #define	LINUX_IFNAMSIZ		16
53 
54 /*
55  * Criteria for interface name translation
56  */
57 #define	IFP_IS_ETH(ifp)		(ifp->if_type == IFT_ETHER)
58 #define	IFP_IS_LOOP(ifp)	(ifp->if_type == IFT_LOOP)
59 
60 struct l_sockaddr {
61 	unsigned short	sa_family;
62 	char		sa_data[14];
63 };
64 
65 #define	LINUX_ARPHRD_ETHER	1
66 #define	LINUX_ARPHRD_LOOPBACK	772
67 
68 /*
69  * Supported address families
70  */
71 #define	LINUX_AF_UNSPEC		0
72 #define	LINUX_AF_UNIX		1
73 #define	LINUX_AF_INET		2
74 #define	LINUX_AF_AX25		3
75 #define	LINUX_AF_IPX		4
76 #define	LINUX_AF_APPLETALK	5
77 #define	LINUX_AF_INET6		10
78 #define	LINUX_AF_NETLINK	16
79 
80 #define	LINUX_NETLINK_ROUTE		0
81 #define	LINUX_NETLINK_SOCK_DIAG		4
82 #define	LINUX_NETLINK_NFLOG		5
83 #define	LINUX_NETLINK_SELINUX		7
84 #define	LINUX_NETLINK_AUDIT		9
85 #define	LINUX_NETLINK_FIB_LOOKUP	10
86 #define	LINUX_NETLINK_NETFILTER		12
87 #define	LINUX_NETLINK_KOBJECT_UEVENT	15
88 
89 /*
90  * net device flags
91  */
92 #define	LINUX_IFF_UP		0x0001
93 #define	LINUX_IFF_BROADCAST	0x0002
94 #define	LINUX_IFF_DEBUG		0x0004
95 #define	LINUX_IFF_LOOPBACK	0x0008
96 #define	LINUX_IFF_POINTOPOINT	0x0010
97 #define	LINUX_IFF_NOTRAILERS	0x0020
98 #define	LINUX_IFF_RUNNING	0x0040
99 #define	LINUX_IFF_NOARP		0x0080
100 #define	LINUX_IFF_PROMISC	0x0100
101 #define	LINUX_IFF_ALLMULTI	0x0200
102 #define	LINUX_IFF_MASTER	0x0400
103 #define	LINUX_IFF_SLAVE		0x0800
104 #define	LINUX_IFF_MULTICAST	0x1000
105 #define	LINUX_IFF_PORTSEL	0x2000
106 #define	LINUX_IFF_AUTOMEDIA	0x4000
107 #define	LINUX_IFF_DYNAMIC	0x8000
108 
109 /* sigaltstack */
110 #define	LINUX_SS_ONSTACK	1
111 #define	LINUX_SS_DISABLE	2
112 
113 int linux_to_bsd_sigaltstack(int lsa);
114 int bsd_to_linux_sigaltstack(int bsa);
115 
116 /* sigset */
117 typedef struct {
118 	uint64_t	__mask;
119 } l_sigset_t;
120 
121 /* primitives to manipulate sigset_t */
122 #define	LINUX_SIGEMPTYSET(set)		(set).__mask = 0
123 #define	LINUX_SIGISMEMBER(set, sig)	(1UL & ((set).__mask >> _SIG_IDX(sig)))
124 #define	LINUX_SIGADDSET(set, sig)	(set).__mask |= 1UL << _SIG_IDX(sig)
125 
126 void linux_to_bsd_sigset(l_sigset_t *, sigset_t *);
127 void bsd_to_linux_sigset(sigset_t *, l_sigset_t *);
128 
129 /* signaling */
130 #define	LINUX_SIGHUP		1
131 #define	LINUX_SIGINT		2
132 #define	LINUX_SIGQUIT		3
133 #define	LINUX_SIGILL		4
134 #define	LINUX_SIGTRAP		5
135 #define	LINUX_SIGABRT		6
136 #define	LINUX_SIGIOT		LINUX_SIGABRT
137 #define	LINUX_SIGBUS		7
138 #define	LINUX_SIGFPE		8
139 #define	LINUX_SIGKILL		9
140 #define	LINUX_SIGUSR1		10
141 #define	LINUX_SIGSEGV		11
142 #define	LINUX_SIGUSR2		12
143 #define	LINUX_SIGPIPE		13
144 #define	LINUX_SIGALRM		14
145 #define	LINUX_SIGTERM		15
146 #define	LINUX_SIGSTKFLT		16
147 #define	LINUX_SIGCHLD		17
148 #define	LINUX_SIGCONT		18
149 #define	LINUX_SIGSTOP		19
150 #define	LINUX_SIGTSTP		20
151 #define	LINUX_SIGTTIN		21
152 #define	LINUX_SIGTTOU		22
153 #define	LINUX_SIGURG		23
154 #define	LINUX_SIGXCPU		24
155 #define	LINUX_SIGXFSZ		25
156 #define	LINUX_SIGVTALRM		26
157 #define	LINUX_SIGPROF		27
158 #define	LINUX_SIGWINCH		28
159 #define	LINUX_SIGIO		29
160 #define	LINUX_SIGPOLL		LINUX_SIGIO
161 #define	LINUX_SIGPWR		30
162 #define	LINUX_SIGSYS		31
163 #define	LINUX_SIGTBLSZ		31
164 #define	LINUX_SIGRTMIN		32
165 #define	LINUX_SIGRTMAX		64
166 
167 #define LINUX_SIG_VALID(sig)	((sig) <= LINUX_SIGRTMAX && (sig) > 0)
168 
169 int linux_to_bsd_signal(int sig);
170 int bsd_to_linux_signal(int sig);
171 
172 extern LIST_HEAD(futex_list, futex) futex_list;
173 extern struct mtx futex_mtx;
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 #endif /* _LINUX_MI_H_ */
268