xref: /freebsd/sys/amd64/linux/linux.h (revision faf139cc5dd3396181c11922bc6685c0c59b7b24)
1 /*-
2  * Copyright (c) 2013 Dmitry Chagin
3  * Copyright (c) 1994-1996 Søren Schmidt
4  * All rights reserved.
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  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 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 /*
39  * debugging support
40  */
41 extern u_char linux_debug_map[];
42 #define	ldebug(name)	isclr(linux_debug_map, LINUX_SYS_linux_ ## name)
43 #define	ARGS(nm, fmt)	"linux(%ld/%ld): "#nm"("fmt")\n",			\
44 			(long)td->td_proc->p_pid, (long)td->td_tid
45 #define	LMSG(fmt)	"linux(%ld/%ld): "fmt"\n",				\
46 			(long)td->td_proc->p_pid, (long)td->td_tid
47 #define	LINUX_DTRACE	linuxulator
48 
49 #define	PTRIN(v)	(void *)(v)
50 #define	PTROUT(v)	(uintptr_t)(v)
51 
52 #define	CP(src,dst,fld) do { (dst).fld = (src).fld; } while (0)
53 #define	CP2(src,dst,sfld,dfld) do { (dst).dfld = (src).sfld; } while (0)
54 #define	PTRIN_CP(src,dst,fld) \
55 	do { (dst).fld = PTRIN((src).fld); } while (0)
56 
57 /*
58  * Provide a separate set of types for the Linux types.
59  */
60 typedef int32_t		l_int;
61 typedef int64_t		l_long;
62 typedef int16_t		l_short;
63 typedef uint32_t	l_uint;
64 typedef uint64_t	l_ulong;
65 typedef uint16_t	l_ushort;
66 
67 typedef l_ulong		l_uintptr_t;
68 typedef l_long		l_clock_t;
69 typedef l_int		l_daddr_t;
70 typedef l_ulong		l_dev_t;
71 typedef l_uint		l_gid_t;
72 typedef l_uint		l_uid_t;
73 typedef l_ulong		l_ino_t;
74 typedef l_int		l_key_t;
75 typedef l_long		l_loff_t;
76 typedef l_uint		l_mode_t;
77 typedef l_long		l_off_t;
78 typedef l_int		l_pid_t;
79 typedef l_ulong		l_size_t;
80 typedef l_long		l_ssize_t;
81 typedef l_long		l_suseconds_t;
82 typedef l_long		l_time_t;
83 typedef l_int		l_timer_t;
84 typedef l_int		l_mqd_t;
85 typedef l_size_t	l_socklen_t;
86 typedef	l_ulong		l_fd_mask;
87 
88 typedef struct {
89 	l_int		val[2];
90 } l_fsid_t;
91 
92 typedef struct {
93 	l_time_t	tv_sec;
94 	l_suseconds_t	tv_usec;
95 } l_timeval;
96 
97 #define	l_fd_set	fd_set
98 
99 /*
100  * Miscellaneous
101  */
102 #define	LINUX_NAME_MAX		255
103 #define	LINUX_CTL_MAXNAME	10
104 
105 #define LINUX_AT_COUNT		19	/* Count of used aux entry types. */
106 
107 struct l___sysctl_args
108 {
109 	l_uintptr_t	name;
110 	l_int		nlen;
111 	l_uintptr_t	oldval;
112 	l_uintptr_t	oldlenp;
113 	l_uintptr_t	newval;
114 	l_size_t	newlen;
115 	l_ulong		__spare[4];
116 };
117 
118 /* Scheduling policies */
119 #define	LINUX_SCHED_OTHER	0
120 #define	LINUX_SCHED_FIFO	1
121 #define	LINUX_SCHED_RR		2
122 
123 /* Resource limits */
124 #define	LINUX_RLIMIT_CPU	0
125 #define	LINUX_RLIMIT_FSIZE	1
126 #define	LINUX_RLIMIT_DATA	2
127 #define	LINUX_RLIMIT_STACK	3
128 #define	LINUX_RLIMIT_CORE	4
129 #define	LINUX_RLIMIT_RSS	5
130 #define	LINUX_RLIMIT_NPROC	6
131 #define	LINUX_RLIMIT_NOFILE	7
132 #define	LINUX_RLIMIT_MEMLOCK	8
133 #define	LINUX_RLIMIT_AS		9	/* Address space limit */
134 
135 #define	LINUX_RLIM_NLIMITS	10
136 
137 struct l_rlimit {
138 	l_ulong		rlim_cur;
139 	l_ulong		rlim_max;
140 };
141 
142 /* mmap options */
143 #define	LINUX_MAP_SHARED	0x0001
144 #define	LINUX_MAP_PRIVATE	0x0002
145 #define	LINUX_MAP_FIXED		0x0010
146 #define	LINUX_MAP_ANON		0x0020
147 #define	LINUX_MAP_GROWSDOWN	0x0100
148 
149 /*
150  * stat family of syscalls
151  */
152 struct l_timespec {
153 	l_time_t	tv_sec;
154 	l_long		tv_nsec;
155 };
156 
157 struct l_newstat {
158 	l_dev_t		st_dev;
159 	l_ino_t		st_ino;
160 	l_ulong		st_nlink;
161 	l_uint		st_mode;
162 	l_uid_t		st_uid;
163 	l_gid_t		st_gid;
164 	l_uint		__st_pad1;
165 	l_dev_t		st_rdev;
166 	l_off_t		st_size;
167 	l_long		st_blksize;
168 	l_long		st_blocks;
169 	struct l_timespec	st_atim;
170 	struct l_timespec	st_mtim;
171 	struct l_timespec	st_ctim;
172 	l_long		__unused1;
173 	l_long		__unused2;
174 	l_long		__unused3;
175 };
176 
177 /* sigaction flags */
178 #define	LINUX_SA_NOCLDSTOP	0x00000001
179 #define	LINUX_SA_NOCLDWAIT	0x00000002
180 #define	LINUX_SA_SIGINFO	0x00000004
181 #define	LINUX_SA_RESTORER	0x04000000
182 #define	LINUX_SA_ONSTACK	0x08000000
183 #define	LINUX_SA_RESTART	0x10000000
184 #define	LINUX_SA_INTERRUPT	0x20000000
185 #define	LINUX_SA_NOMASK		0x40000000
186 #define	LINUX_SA_ONESHOT	0x80000000
187 
188 /* sigprocmask actions */
189 #define	LINUX_SIG_BLOCK		0
190 #define	LINUX_SIG_UNBLOCK	1
191 #define	LINUX_SIG_SETMASK	2
192 
193 /* sigaltstack */
194 #define	LINUX_MINSIGSTKSZ	2048
195 
196 typedef void	(*l_handler_t)(l_int);
197 
198 typedef struct {
199 	l_handler_t	lsa_handler;
200 	l_ulong		lsa_flags;
201 	l_uintptr_t	lsa_restorer;
202 	l_sigset_t	lsa_mask;
203 } l_sigaction_t;
204 
205 typedef struct {
206 	l_uintptr_t	ss_sp;
207 	l_int		ss_flags;
208 	l_size_t	ss_size;
209 } l_stack_t;
210 
211 struct l_fpstate {
212 	u_int16_t cwd;
213 	u_int16_t swd;
214 	u_int16_t twd;
215 	u_int16_t fop;
216 	u_int64_t rip;
217 	u_int64_t rdp;
218 	u_int32_t mxcsr;
219 	u_int32_t mxcsr_mask;
220 	u_int32_t st_space[32];
221 	u_int32_t xmm_space[64];
222 	u_int32_t reserved2[24];
223 };
224 
225 struct l_sigcontext {
226 	l_ulong		sc_r8;
227 	l_ulong		sc_r9;
228 	l_ulong		sc_r10;
229 	l_ulong		sc_r11;
230 	l_ulong		sc_r12;
231 	l_ulong		sc_r13;
232 	l_ulong		sc_r14;
233 	l_ulong		sc_r15;
234 	l_ulong		sc_rdi;
235 	l_ulong		sc_rsi;
236 	l_ulong		sc_rbp;
237 	l_ulong		sc_rbx;
238 	l_ulong		sc_rdx;
239 	l_ulong		sc_rax;
240 	l_ulong		sc_rcx;
241 	l_ulong		sc_rsp;
242 	l_ulong		sc_rip;
243 	l_ulong		sc_rflags;
244 	l_ushort	sc_cs;
245 	l_ushort	sc_gs;
246 	l_ushort	sc_fs;
247 	l_ushort	sc___pad0;
248 	l_ulong		sc_err;
249 	l_ulong		sc_trapno;
250 	l_sigset_t	sc_mask;
251 	l_ulong		sc_cr2;
252 	struct l_fpstate *sc_fpstate;
253 	l_ulong		sc_reserved1[8];
254 };
255 
256 struct l_ucontext {
257 	l_ulong		uc_flags;
258 	l_uintptr_t	uc_link;
259 	l_stack_t	uc_stack;
260 	struct l_sigcontext	uc_mcontext;
261 	l_sigset_t	uc_sigmask;
262 };
263 
264 #define LINUX_SI_PREAMBLE_SIZE	(4 * sizeof(int))
265 #define	LINUX_SI_MAX_SIZE	128
266 #define	LINUX_SI_PAD_SIZE	((LINUX_SI_MAX_SIZE - \
267 				    LINUX_SI_PREAMBLE_SIZE) / sizeof(l_int))
268 typedef union l_sigval {
269 	l_int		sival_int;
270 	l_uintptr_t	sival_ptr;
271 } l_sigval_t;
272 
273 typedef struct l_siginfo {
274 	l_int		lsi_signo;
275 	l_int		lsi_errno;
276 	l_int		lsi_code;
277 	union {
278 		l_int	_pad[LINUX_SI_PAD_SIZE];
279 
280 		struct {
281 			l_pid_t		_pid;
282 			l_uid_t		_uid;
283 		} _kill;
284 
285 		struct {
286 			l_timer_t	_tid;
287 			l_int		_overrun;
288 			char		_pad[sizeof(l_uid_t) - sizeof(int)];
289 			union l_sigval	_sigval;
290 			l_uint		_sys_private;
291 		} _timer;
292 
293 		struct {
294 			l_pid_t		_pid;		/* sender's pid */
295 			l_uid_t		_uid;		/* sender's uid */
296 			union l_sigval	_sigval;
297 		} _rt;
298 
299 		struct {
300 			l_pid_t		_pid;		/* which child */
301 			l_uid_t		_uid;		/* sender's uid */
302 			l_int		_status;	/* exit code */
303 			l_clock_t	_utime;
304 			l_clock_t	_stime;
305 		} _sigchld;
306 
307 		struct {
308 			l_uintptr_t	_addr;	/* Faulting insn/memory ref. */
309 		} _sigfault;
310 
311 		struct {
312 			l_long		_band;	/* POLL_IN,POLL_OUT,POLL_MSG */
313 			l_int		_fd;
314 		} _sigpoll;
315 	} _sifields;
316 } l_siginfo_t;
317 
318 #define	lsi_pid		_sifields._kill._pid
319 #define	lsi_uid		_sifields._kill._uid
320 #define	lsi_tid		_sifields._timer._tid
321 #define	lsi_overrun	_sifields._timer._overrun
322 #define	lsi_sys_private	_sifields._timer._sys_private
323 #define	lsi_status	_sifields._sigchld._status
324 #define	lsi_utime	_sifields._sigchld._utime
325 #define	lsi_stime	_sifields._sigchld._stime
326 #define	lsi_value	_sifields._rt._sigval
327 #define	lsi_int		_sifields._rt._sigval.sival_int
328 #define	lsi_ptr		_sifields._rt._sigval.sival_ptr
329 #define	lsi_addr	_sifields._sigfault._addr
330 #define	lsi_band	_sifields._sigpoll._band
331 #define	lsi_fd		_sifields._sigpoll._fd
332 
333 /*
334  * We make the stack look like Linux expects it when calling a signal
335  * handler, but use the BSD way of calling the handler and sigreturn().
336  * This means that we need to pass the pointer to the handler too.
337  * It is appended to the frame to not interfere with the rest of it.
338  */
339 
340 struct l_rt_sigframe {
341 	struct l_ucontext	sf_sc;
342 	struct l_siginfo	sf_si;
343 	l_handler_t		sf_handler;
344 };
345 
346 /*
347  * mount flags
348  */
349 #define	LINUX_MS_RDONLY		0x0001
350 #define	LINUX_MS_NOSUID		0x0002
351 #define	LINUX_MS_NODEV		0x0004
352 #define	LINUX_MS_NOEXEC		0x0008
353 #define	LINUX_MS_REMOUNT	0x0020
354 
355 /*
356  * SystemV IPC defines
357  */
358 #define	LINUX_IPC_RMID		0
359 #define	LINUX_IPC_SET		1
360 #define	LINUX_IPC_STAT		2
361 #define	LINUX_IPC_INFO		3
362 
363 #define	LINUX_SHM_LOCK		11
364 #define	LINUX_SHM_UNLOCK	12
365 #define	LINUX_SHM_STAT		13
366 #define	LINUX_SHM_INFO		14
367 
368 #define	LINUX_SHM_RDONLY	0x1000
369 #define	LINUX_SHM_RND		0x2000
370 #define	LINUX_SHM_REMAP		0x4000
371 
372 /* semctl commands */
373 #define	LINUX_GETPID		11
374 #define	LINUX_GETVAL		12
375 #define	LINUX_GETALL		13
376 #define	LINUX_GETNCNT		14
377 #define	LINUX_GETZCNT		15
378 #define	LINUX_SETVAL		16
379 #define	LINUX_SETALL		17
380 #define	LINUX_SEM_STAT		18
381 #define	LINUX_SEM_INFO		19
382 
383 union l_semun {
384 	l_int		val;
385 	l_uintptr_t	buf;
386 	l_uintptr_t	array;
387 	l_uintptr_t	__buf;
388 	l_uintptr_t	__pad;
389 };
390 
391 struct l_ipc_perm {
392 	l_key_t		key;
393 	l_uid_t		uid;
394 	l_gid_t		gid;
395 	l_uid_t		cuid;
396 	l_gid_t		cgid;
397 	l_ushort	mode;
398 	l_ushort	seq;
399 };
400 
401 /*
402  * Socket defines
403  */
404 
405 #define	LINUX_SOL_SOCKET	1
406 #define	LINUX_SOL_IP		0
407 #define	LINUX_SOL_IPX		256
408 #define	LINUX_SOL_AX25		257
409 #define	LINUX_SOL_TCP		6
410 #define	LINUX_SOL_UDP		17
411 
412 #define	LINUX_SO_DEBUG		1
413 #define	LINUX_SO_REUSEADDR	2
414 #define	LINUX_SO_TYPE		3
415 #define	LINUX_SO_ERROR		4
416 #define	LINUX_SO_DONTROUTE	5
417 #define	LINUX_SO_BROADCAST	6
418 #define	LINUX_SO_SNDBUF		7
419 #define	LINUX_SO_RCVBUF		8
420 #define	LINUX_SO_KEEPALIVE	9
421 #define	LINUX_SO_OOBINLINE	10
422 #define	LINUX_SO_NO_CHECK	11
423 #define	LINUX_SO_PRIORITY	12
424 #define	LINUX_SO_LINGER		13
425 #define	LINUX_SO_PASSCRED	16
426 #define	LINUX_SO_PEERCRED	17
427 #define	LINUX_SO_RCVLOWAT	18
428 #define	LINUX_SO_SNDLOWAT	19
429 #define	LINUX_SO_RCVTIMEO	20
430 #define	LINUX_SO_SNDTIMEO	21
431 #define	LINUX_SO_TIMESTAMP	29
432 #define	LINUX_SO_ACCEPTCONN	30
433 
434 #define	LINUX_IP_TOS		1
435 #define	LINUX_IP_TTL		2
436 #define	LINUX_IP_HDRINCL	3
437 #define	LINUX_IP_OPTIONS	4
438 
439 #define	LINUX_IP_MULTICAST_IF		32
440 #define	LINUX_IP_MULTICAST_TTL		33
441 #define	LINUX_IP_MULTICAST_LOOP		34
442 #define	LINUX_IP_ADD_MEMBERSHIP		35
443 #define	LINUX_IP_DROP_MEMBERSHIP	36
444 
445 struct l_sockaddr {
446 	l_ushort	sa_family;
447 	char		sa_data[14];
448 };
449 
450 struct l_ifmap {
451 	l_ulong		mem_start;
452 	l_ulong		mem_end;
453 	l_ushort	base_addr;
454 	u_char		irq;
455 	u_char		dma;
456 	u_char		port;
457 } __packed;
458 
459 #define	LINUX_IFHWADDRLEN	6
460 #define	LINUX_IFNAMSIZ		16
461 
462 struct l_ifreq {
463 	union {
464 		char	ifrn_name[LINUX_IFNAMSIZ];
465 	} ifr_ifrn;
466 
467 	union {
468 		struct l_sockaddr	ifru_addr;
469 		struct l_sockaddr	ifru_dstaddr;
470 		struct l_sockaddr	ifru_broadaddr;
471 		struct l_sockaddr	ifru_netmask;
472 		struct l_sockaddr	ifru_hwaddr;
473 		l_short		ifru_flags[1];
474 		l_int		ifru_metric;
475 		l_int		ifru_mtu;
476 		struct l_ifmap	ifru_map;
477 		char		ifru_slave[LINUX_IFNAMSIZ];
478 		l_uintptr_t	ifru_data;
479 	} ifr_ifru;
480 } __packed;
481 
482 #define	ifr_name	ifr_ifrn.ifrn_name	/* Interface name */
483 #define	ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address */
484 
485 struct l_ifconf {
486 	int	ifc_len;
487 	union {
488 		l_uintptr_t	ifcu_buf;
489 		l_uintptr_t	ifcu_req;
490 	} ifc_ifcu;
491 };
492 
493 #define	ifc_buf		ifc_ifcu.ifcu_buf
494 #define	ifc_req		ifc_ifcu.ifcu_req
495 
496 /*
497  * poll()
498  */
499 #define	LINUX_POLLIN		0x0001
500 #define	LINUX_POLLPRI		0x0002
501 #define	LINUX_POLLOUT		0x0004
502 #define	LINUX_POLLERR		0x0008
503 #define	LINUX_POLLHUP		0x0010
504 #define	LINUX_POLLNVAL		0x0020
505 #define	LINUX_POLLRDNORM	0x0040
506 #define	LINUX_POLLRDBAND	0x0080
507 #define	LINUX_POLLWRNORM	0x0100
508 #define	LINUX_POLLWRBAND	0x0200
509 #define	LINUX_POLLMSG		0x0400
510 
511 struct l_pollfd {
512 	l_int		fd;
513 	l_short		events;
514 	l_short		revents;
515 };
516 
517 
518 #define	LINUX_CLONE_VM			0x00000100
519 #define	LINUX_CLONE_FS			0x00000200
520 #define	LINUX_CLONE_FILES		0x00000400
521 #define	LINUX_CLONE_SIGHAND		0x00000800
522 #define	LINUX_CLONE_PID			0x00001000	/* No longer exist in Linux */
523 #define	LINUX_CLONE_VFORK		0x00004000
524 #define	LINUX_CLONE_PARENT		0x00008000
525 #define	LINUX_CLONE_THREAD		0x00010000
526 #define	LINUX_CLONE_SETTLS		0x00080000
527 #define	LINUX_CLONE_PARENT_SETTID	0x00100000
528 #define	LINUX_CLONE_CHILD_CLEARTID	0x00200000
529 #define	LINUX_CLONE_CHILD_SETTID	0x01000000
530 
531 #define LINUX_ARCH_SET_GS		0x1001
532 #define LINUX_ARCH_SET_FS		0x1002
533 #define LINUX_ARCH_GET_FS		0x1003
534 #define LINUX_ARCH_GET_GS		0x1004
535 
536 #define	linux_copyout_rusage(r, u)	copyout(r, u, sizeof(*r))
537 
538 /* robust futexes */
539 struct linux_robust_list {
540 	l_uintptr_t			next;
541 };
542 
543 struct linux_robust_list_head {
544 	struct linux_robust_list	list;
545 	l_long				futex_offset;
546 	l_uintptr_t			pending_list;
547 };
548 
549 #endif /* !_AMD64_LINUX_H_ */
550