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