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