xref: /freebsd/sys/amd64/linux/linux.h (revision 77a1348b3c1cfe8547be49a121b56299a1e18b69)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Dmitry Chagin
5  * Copyright (c) 1994-1996 Søren Schmidt
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _AMD64_LINUX_H_
33 #define	_AMD64_LINUX_H_
34 
35 #include <compat/linux/linux.h>
36 #include <amd64/linux/linux_syscall.h>
37 
38 #define	LINUX_LEGACY_SYSCALLS
39 
40 #define	LINUX_DTRACE	linuxulator
41 
42 #define	PTRIN(v)	(void *)(v)
43 #define	PTROUT(v)	(uintptr_t)(v)
44 
45 #define	CP(src,dst,fld) do { (dst).fld = (src).fld; } while (0)
46 #define	CP2(src,dst,sfld,dfld) do { (dst).dfld = (src).sfld; } while (0)
47 #define	PTRIN_CP(src,dst,fld) \
48 	do { (dst).fld = PTRIN((src).fld); } while (0)
49 
50 /*
51  * Provide a separate set of types for the Linux types.
52  */
53 typedef int32_t		l_int;
54 typedef int64_t		l_long;
55 typedef int16_t		l_short;
56 typedef uint32_t	l_uint;
57 typedef uint64_t	l_ulong;
58 typedef uint16_t	l_ushort;
59 
60 typedef l_ulong		l_uintptr_t;
61 typedef l_long		l_clock_t;
62 typedef l_int		l_daddr_t;
63 typedef l_ulong		l_dev_t;
64 typedef l_uint		l_gid_t;
65 typedef l_ushort	l_gid16_t;
66 typedef l_uint		l_uid_t;
67 typedef	l_ushort	l_uid16_t;
68 typedef l_ulong		l_ino_t;
69 typedef l_int		l_key_t;
70 typedef l_long		l_loff_t;
71 typedef l_uint		l_mode_t;
72 typedef l_long		l_off_t;
73 typedef l_int		l_pid_t;
74 typedef l_ulong		l_size_t;
75 typedef l_long		l_ssize_t;
76 typedef l_long		l_suseconds_t;
77 typedef l_long		l_time_t;
78 typedef l_int		l_timer_t;
79 typedef l_int		l_mqd_t;
80 typedef l_size_t	l_socklen_t;
81 typedef	l_ulong		l_fd_mask;
82 
83 typedef struct {
84 	l_int		val[2];
85 } l_fsid_t;
86 
87 typedef struct {
88 	l_time_t	tv_sec;
89 	l_suseconds_t	tv_usec;
90 } l_timeval;
91 
92 #define	l_fd_set	fd_set
93 
94 /*
95  * Miscellaneous
96  */
97 #define LINUX_AT_COUNT		19	/* Count of used aux entry types. */
98 
99 struct l___sysctl_args
100 {
101 	l_uintptr_t	name;
102 	l_int		nlen;
103 	l_uintptr_t	oldval;
104 	l_uintptr_t	oldlenp;
105 	l_uintptr_t	newval;
106 	l_size_t	newlen;
107 	l_ulong		__spare[4];
108 };
109 
110 /* Resource limits */
111 #define	LINUX_RLIMIT_CPU	0
112 #define	LINUX_RLIMIT_FSIZE	1
113 #define	LINUX_RLIMIT_DATA	2
114 #define	LINUX_RLIMIT_STACK	3
115 #define	LINUX_RLIMIT_CORE	4
116 #define	LINUX_RLIMIT_RSS	5
117 #define	LINUX_RLIMIT_NPROC	6
118 #define	LINUX_RLIMIT_NOFILE	7
119 #define	LINUX_RLIMIT_MEMLOCK	8
120 #define	LINUX_RLIMIT_AS		9	/* Address space limit */
121 
122 #define	LINUX_RLIM_NLIMITS	10
123 
124 struct l_rlimit {
125 	l_ulong		rlim_cur;
126 	l_ulong		rlim_max;
127 };
128 
129 /*
130  * stat family of syscalls
131  */
132 struct l_timespec {
133 	l_time_t	tv_sec;
134 	l_long		tv_nsec;
135 };
136 
137 struct l_newstat {
138 	l_dev_t		st_dev;
139 	l_ino_t		st_ino;
140 	l_ulong		st_nlink;
141 	l_uint		st_mode;
142 	l_uid_t		st_uid;
143 	l_gid_t		st_gid;
144 	l_uint		__st_pad1;
145 	l_dev_t		st_rdev;
146 	l_off_t		st_size;
147 	l_long		st_blksize;
148 	l_long		st_blocks;
149 	struct l_timespec	st_atim;
150 	struct l_timespec	st_mtim;
151 	struct l_timespec	st_ctim;
152 	l_long		__unused1;
153 	l_long		__unused2;
154 	l_long		__unused3;
155 };
156 
157 /* sigaction flags */
158 #define	LINUX_SA_NOCLDSTOP	0x00000001
159 #define	LINUX_SA_NOCLDWAIT	0x00000002
160 #define	LINUX_SA_SIGINFO	0x00000004
161 #define	LINUX_SA_RESTORER	0x04000000
162 #define	LINUX_SA_ONSTACK	0x08000000
163 #define	LINUX_SA_RESTART	0x10000000
164 #define	LINUX_SA_INTERRUPT	0x20000000
165 #define	LINUX_SA_NOMASK		0x40000000
166 #define	LINUX_SA_ONESHOT	0x80000000
167 
168 /* sigprocmask actions */
169 #define	LINUX_SIG_BLOCK		0
170 #define	LINUX_SIG_UNBLOCK	1
171 #define	LINUX_SIG_SETMASK	2
172 
173 /* sigaltstack */
174 #define	LINUX_MINSIGSTKSZ	2048
175 
176 typedef void	(*l_handler_t)(l_int);
177 
178 typedef struct {
179 	l_handler_t	lsa_handler;
180 	l_ulong		lsa_flags;
181 	l_uintptr_t	lsa_restorer;
182 	l_sigset_t	lsa_mask;
183 } l_sigaction_t;
184 
185 typedef struct {
186 	l_uintptr_t	ss_sp;
187 	l_int		ss_flags;
188 	l_size_t	ss_size;
189 } l_stack_t;
190 
191 struct l_fpstate {
192 	u_int16_t cwd;
193 	u_int16_t swd;
194 	u_int16_t twd;
195 	u_int16_t fop;
196 	u_int64_t rip;
197 	u_int64_t rdp;
198 	u_int32_t mxcsr;
199 	u_int32_t mxcsr_mask;
200 	u_int32_t st_space[32];
201 	u_int32_t xmm_space[64];
202 	u_int32_t reserved2[24];
203 };
204 
205 struct l_sigcontext {
206 	l_ulong		sc_r8;
207 	l_ulong		sc_r9;
208 	l_ulong		sc_r10;
209 	l_ulong		sc_r11;
210 	l_ulong		sc_r12;
211 	l_ulong		sc_r13;
212 	l_ulong		sc_r14;
213 	l_ulong		sc_r15;
214 	l_ulong		sc_rdi;
215 	l_ulong		sc_rsi;
216 	l_ulong		sc_rbp;
217 	l_ulong		sc_rbx;
218 	l_ulong		sc_rdx;
219 	l_ulong		sc_rax;
220 	l_ulong		sc_rcx;
221 	l_ulong		sc_rsp;
222 	l_ulong		sc_rip;
223 	l_ulong		sc_rflags;
224 	l_ushort	sc_cs;
225 	l_ushort	sc_gs;
226 	l_ushort	sc_fs;
227 	l_ushort	sc___pad0;
228 	l_ulong		sc_err;
229 	l_ulong		sc_trapno;
230 	l_sigset_t	sc_mask;
231 	l_ulong		sc_cr2;
232 	struct l_fpstate *sc_fpstate;
233 	l_ulong		sc_reserved1[8];
234 };
235 
236 struct l_ucontext {
237 	l_ulong		uc_flags;
238 	l_uintptr_t	uc_link;
239 	l_stack_t	uc_stack;
240 	struct l_sigcontext	uc_mcontext;
241 	l_sigset_t	uc_sigmask;
242 };
243 
244 #define LINUX_SI_PREAMBLE_SIZE	(4 * sizeof(int))
245 #define	LINUX_SI_MAX_SIZE	128
246 #define	LINUX_SI_PAD_SIZE	((LINUX_SI_MAX_SIZE - \
247 				    LINUX_SI_PREAMBLE_SIZE) / sizeof(l_int))
248 typedef union l_sigval {
249 	l_int		sival_int;
250 	l_uintptr_t	sival_ptr;
251 } l_sigval_t;
252 
253 typedef struct l_siginfo {
254 	l_int		lsi_signo;
255 	l_int		lsi_errno;
256 	l_int		lsi_code;
257 	union {
258 		l_int	_pad[LINUX_SI_PAD_SIZE];
259 
260 		struct {
261 			l_pid_t		_pid;
262 			l_uid_t		_uid;
263 		} _kill;
264 
265 		struct {
266 			l_timer_t	_tid;
267 			l_int		_overrun;
268 			char		_pad[sizeof(l_uid_t) - sizeof(int)];
269 			union l_sigval	_sigval;
270 			l_uint		_sys_private;
271 		} _timer;
272 
273 		struct {
274 			l_pid_t		_pid;		/* sender's pid */
275 			l_uid_t		_uid;		/* sender's uid */
276 			union l_sigval	_sigval;
277 		} _rt;
278 
279 		struct {
280 			l_pid_t		_pid;		/* which child */
281 			l_uid_t		_uid;		/* sender's uid */
282 			l_int		_status;	/* exit code */
283 			l_clock_t	_utime;
284 			l_clock_t	_stime;
285 		} _sigchld;
286 
287 		struct {
288 			l_uintptr_t	_addr;	/* Faulting insn/memory ref. */
289 		} _sigfault;
290 
291 		struct {
292 			l_long		_band;	/* POLL_IN,POLL_OUT,POLL_MSG */
293 			l_int		_fd;
294 		} _sigpoll;
295 	} _sifields;
296 } l_siginfo_t;
297 
298 #define	lsi_pid		_sifields._kill._pid
299 #define	lsi_uid		_sifields._kill._uid
300 #define	lsi_tid		_sifields._timer._tid
301 #define	lsi_overrun	_sifields._timer._overrun
302 #define	lsi_sys_private	_sifields._timer._sys_private
303 #define	lsi_status	_sifields._sigchld._status
304 #define	lsi_utime	_sifields._sigchld._utime
305 #define	lsi_stime	_sifields._sigchld._stime
306 #define	lsi_value	_sifields._rt._sigval
307 #define	lsi_int		_sifields._rt._sigval.sival_int
308 #define	lsi_ptr		_sifields._rt._sigval.sival_ptr
309 #define	lsi_addr	_sifields._sigfault._addr
310 #define	lsi_band	_sifields._sigpoll._band
311 #define	lsi_fd		_sifields._sigpoll._fd
312 
313 /*
314  * We make the stack look like Linux expects it when calling a signal
315  * handler, but use the BSD way of calling the handler and sigreturn().
316  * This means that we need to pass the pointer to the handler too.
317  * It is appended to the frame to not interfere with the rest of it.
318  */
319 
320 struct l_rt_sigframe {
321 	struct l_ucontext	sf_sc;
322 	struct l_siginfo	sf_si;
323 	l_handler_t		sf_handler;
324 };
325 
326 /*
327  * mount flags
328  */
329 #define	LINUX_MS_RDONLY		0x0001
330 #define	LINUX_MS_NOSUID		0x0002
331 #define	LINUX_MS_NODEV		0x0004
332 #define	LINUX_MS_NOEXEC		0x0008
333 #define	LINUX_MS_REMOUNT	0x0020
334 
335 /*
336  * SystemV IPC defines
337  */
338 #define	LINUX_IPC_RMID		0
339 #define	LINUX_IPC_SET		1
340 #define	LINUX_IPC_STAT		2
341 #define	LINUX_IPC_INFO		3
342 
343 #define	LINUX_SHM_LOCK		11
344 #define	LINUX_SHM_UNLOCK	12
345 #define	LINUX_SHM_STAT		13
346 #define	LINUX_SHM_INFO		14
347 
348 #define	LINUX_SHM_RDONLY	0x1000
349 #define	LINUX_SHM_RND		0x2000
350 #define	LINUX_SHM_REMAP		0x4000
351 
352 /* semctl commands */
353 #define	LINUX_GETPID		11
354 #define	LINUX_GETVAL		12
355 #define	LINUX_GETALL		13
356 #define	LINUX_GETNCNT		14
357 #define	LINUX_GETZCNT		15
358 #define	LINUX_SETVAL		16
359 #define	LINUX_SETALL		17
360 #define	LINUX_SEM_STAT		18
361 #define	LINUX_SEM_INFO		19
362 
363 union l_semun {
364 	l_int		val;
365 	l_uintptr_t	buf;
366 	l_uintptr_t	array;
367 	l_uintptr_t	__buf;
368 	l_uintptr_t	__pad;
369 };
370 
371 struct l_ifmap {
372 	l_ulong		mem_start;
373 	l_ulong		mem_end;
374 	l_ushort	base_addr;
375 	u_char		irq;
376 	u_char		dma;
377 	u_char		port;
378 } __packed;
379 
380 struct l_ifreq {
381 	union {
382 		char	ifrn_name[LINUX_IFNAMSIZ];
383 	} ifr_ifrn;
384 
385 	union {
386 		struct l_sockaddr	ifru_addr;
387 		struct l_sockaddr	ifru_dstaddr;
388 		struct l_sockaddr	ifru_broadaddr;
389 		struct l_sockaddr	ifru_netmask;
390 		struct l_sockaddr	ifru_hwaddr;
391 		l_short		ifru_flags[1];
392 		l_int		ifru_ivalue;
393 		l_int		ifru_mtu;
394 		struct l_ifmap	ifru_map;
395 		char		ifru_slave[LINUX_IFNAMSIZ];
396 		l_uintptr_t	ifru_data;
397 	} ifr_ifru;
398 } __packed;
399 
400 #define	ifr_name	ifr_ifrn.ifrn_name	/* Interface name */
401 #define	ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address */
402 #define	ifr_ifindex	ifr_ifru.ifru_ivalue	/* Interface index */
403 
404 struct l_ifconf {
405 	int	ifc_len;
406 	union {
407 		l_uintptr_t	ifcu_buf;
408 		l_uintptr_t	ifcu_req;
409 	} ifc_ifcu;
410 };
411 
412 #define	ifc_buf		ifc_ifcu.ifcu_buf
413 #define	ifc_req		ifc_ifcu.ifcu_req
414 
415 /*
416  * poll()
417  */
418 #define	LINUX_POLLIN		0x0001
419 #define	LINUX_POLLPRI		0x0002
420 #define	LINUX_POLLOUT		0x0004
421 #define	LINUX_POLLERR		0x0008
422 #define	LINUX_POLLHUP		0x0010
423 #define	LINUX_POLLNVAL		0x0020
424 #define	LINUX_POLLRDNORM	0x0040
425 #define	LINUX_POLLRDBAND	0x0080
426 #define	LINUX_POLLWRNORM	0x0100
427 #define	LINUX_POLLWRBAND	0x0200
428 #define	LINUX_POLLMSG		0x0400
429 
430 struct l_pollfd {
431 	l_int		fd;
432 	l_short		events;
433 	l_short		revents;
434 };
435 
436 #define LINUX_ARCH_SET_GS		0x1001
437 #define LINUX_ARCH_SET_FS		0x1002
438 #define LINUX_ARCH_GET_FS		0x1003
439 #define LINUX_ARCH_GET_GS		0x1004
440 
441 #define	linux_copyout_rusage(r, u)	copyout(r, u, sizeof(*r))
442 
443 /* robust futexes */
444 struct linux_robust_list {
445 	l_uintptr_t			next;
446 };
447 
448 struct linux_robust_list_head {
449 	struct linux_robust_list	list;
450 	l_long				futex_offset;
451 	l_uintptr_t			pending_list;
452 };
453 
454 #endif /* !_AMD64_LINUX_H_ */
455