1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2004 Tim J. Robbins
5 * Copyright (c) 2001 Doug Rabson
6 * Copyright (c) 1994-1996 S��ren Schmidt
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer
14 *    in this position and unchanged.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $FreeBSD$
33 */
34
35#ifndef _AMD64_LINUX_H_
36#define	_AMD64_LINUX_H_
37
38#include <sys/abi_compat.h>
39
40#include <compat/linux/linux.h>
41#include <amd64/linux32/linux32_syscall.h>
42
43#define	LINUX_LEGACY_SYSCALLS
44
45#define	LINUX_DTRACE	linuxulator32
46
47#define	LINUX32_MAXUSER		((1ul << 32) - PAGE_SIZE)
48#define	LINUX32_SHAREDPAGE	(LINUX32_MAXUSER - PAGE_SIZE)
49#define	LINUX32_USRSTACK	LINUX32_SHAREDPAGE
50
51/* XXX 16 = sizeof(linux32_ps_strings) */
52#define	LINUX32_PS_STRINGS	(LINUX32_USRSTACK - 16)
53#define	LINUX32_MAXDSIZ		(512 * 1024 * 1024)	/* 512MB */
54#define	LINUX32_MAXSSIZ		(64 * 1024 * 1024)	/* 64MB */
55#define	LINUX32_MAXVMEM		0			/* Unlimited */
56
57/*
58 * Provide a separate set of types for the Linux types.
59 */
60typedef int		l_int;
61typedef int32_t		l_long;
62typedef int64_t		l_longlong;
63typedef short		l_short;
64typedef unsigned int	l_uint;
65typedef uint32_t	l_ulong;
66typedef uint64_t	l_ulonglong;
67typedef unsigned short	l_ushort;
68
69typedef l_ulong		l_uintptr_t;
70typedef l_long		l_clock_t;
71typedef l_int		l_daddr_t;
72typedef l_ushort	l_dev_t;
73typedef l_uint		l_gid_t;
74typedef l_ushort	l_gid16_t;
75typedef l_ulong		l_ino_t;
76typedef l_int		l_key_t;
77typedef l_longlong	l_loff_t;
78typedef l_ushort	l_mode_t;
79typedef l_long		l_off_t;
80typedef l_int		l_pid_t;
81typedef l_uint		l_size_t;
82typedef l_long		l_suseconds_t;
83typedef l_long		l_time_t;
84typedef l_uint		l_uid_t;
85typedef l_ushort	l_uid16_t;
86typedef l_int		l_timer_t;
87typedef l_int		l_mqd_t;
88typedef	l_ulong		l_fd_mask;
89
90typedef struct {
91	l_int		val[2];
92} __packed l_fsid_t;
93
94typedef 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		20	/* Count of used aux entry types.
105					 * Keep this synchronized with
106					 * linux_fixup_elf() code.
107					 */
108struct 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} __packed;
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
133struct l_rlimit {
134	l_ulong rlim_cur;
135	l_ulong rlim_max;
136} __packed;
137
138struct l_rusage {
139	l_timeval ru_utime;
140	l_timeval ru_stime;
141	l_long	ru_maxrss;
142	l_long	ru_ixrss;
143	l_long	ru_idrss;
144	l_long	ru_isrss;
145	l_long	ru_minflt;
146	l_long	ru_majflt;
147	l_long	ru_nswap;
148	l_long	ru_inblock;
149	l_long	ru_oublock;
150	l_long	ru_msgsnd;
151	l_long	ru_msgrcv;
152	l_long	ru_nsignals;
153	l_long	ru_nvcsw;
154	l_long	ru_nivcsw;
155} __packed;
156
157struct l_mmap_argv {
158	l_uintptr_t	addr;
159	l_size_t	len;
160	l_int		prot;
161	l_int		flags;
162	l_int		fd;
163	l_ulong		pgoff;
164};
165
166/*
167 * stat family of syscalls
168 */
169struct l_timespec {
170	l_time_t	tv_sec;
171	l_long		tv_nsec;
172} __packed;
173
174struct l_newstat {
175	l_ushort	st_dev;
176	l_ushort	__pad1;
177	l_ulong		st_ino;
178	l_ushort	st_mode;
179	l_ushort	st_nlink;
180	l_ushort	st_uid;
181	l_ushort	st_gid;
182	l_ushort	st_rdev;
183	l_ushort	__pad2;
184	l_ulong		st_size;
185	l_ulong		st_blksize;
186	l_ulong		st_blocks;
187	struct l_timespec	st_atim;
188	struct l_timespec	st_mtim;
189	struct l_timespec	st_ctim;
190	l_ulong		__unused4;
191	l_ulong		__unused5;
192} __packed;
193
194struct l_stat {
195	l_ushort	st_dev;
196	l_ulong		st_ino;
197	l_ushort	st_mode;
198	l_ushort	st_nlink;
199	l_ushort	st_uid;
200	l_ushort	st_gid;
201	l_ushort	st_rdev;
202	l_long		st_size;
203	struct l_timespec	st_atim;
204	struct l_timespec	st_mtim;
205	struct l_timespec	st_ctim;
206	l_long		st_blksize;
207	l_long		st_blocks;
208	l_ulong		st_flags;
209	l_ulong		st_gen;
210};
211
212struct l_stat64 {
213	l_ushort	st_dev;
214	u_char		__pad0[10];
215	l_ulong		__st_ino;
216	l_uint		st_mode;
217	l_uint		st_nlink;
218	l_ulong		st_uid;
219	l_ulong		st_gid;
220	l_ushort	st_rdev;
221	u_char		__pad3[10];
222	l_longlong	st_size;
223	l_ulong		st_blksize;
224	l_ulong		st_blocks;
225	l_ulong		__pad4;
226	struct l_timespec	st_atim;
227	struct l_timespec	st_mtim;
228	struct l_timespec	st_ctim;
229	l_ulonglong	st_ino;
230} __packed;
231
232struct l_statfs64 {
233	l_int		f_type;
234	l_int		f_bsize;
235	uint64_t	f_blocks;
236	uint64_t	f_bfree;
237	uint64_t	f_bavail;
238	uint64_t	f_files;
239	uint64_t	f_ffree;
240	l_fsid_t	f_fsid;
241	l_int		f_namelen;
242	l_int		f_frsize;
243	l_int		f_flags;
244	l_int		f_spare[4];
245} __packed;
246
247/* sigaction flags */
248#define	LINUX_SA_NOCLDSTOP	0x00000001
249#define	LINUX_SA_NOCLDWAIT	0x00000002
250#define	LINUX_SA_SIGINFO	0x00000004
251#define	LINUX_SA_RESTORER	0x04000000
252#define	LINUX_SA_ONSTACK	0x08000000
253#define	LINUX_SA_RESTART	0x10000000
254#define	LINUX_SA_INTERRUPT	0x20000000
255#define	LINUX_SA_NOMASK		0x40000000
256#define	LINUX_SA_ONESHOT	0x80000000
257
258/* sigprocmask actions */
259#define	LINUX_SIG_BLOCK		0
260#define	LINUX_SIG_UNBLOCK	1
261#define	LINUX_SIG_SETMASK	2
262
263/* sigaltstack */
264#define	LINUX_MINSIGSTKSZ	2048
265
266typedef l_uintptr_t l_handler_t;
267typedef l_ulong	l_osigset_t;
268
269typedef struct {
270	l_handler_t	lsa_handler;
271	l_osigset_t	lsa_mask;
272	l_ulong		lsa_flags;
273	l_uintptr_t	lsa_restorer;
274} __packed l_osigaction_t;
275
276typedef struct {
277	l_handler_t	lsa_handler;
278	l_ulong		lsa_flags;
279	l_uintptr_t	lsa_restorer;
280	l_sigset_t	lsa_mask;
281} __packed l_sigaction_t;
282
283typedef struct {
284	l_uintptr_t	ss_sp;
285	l_int		ss_flags;
286	l_size_t	ss_size;
287} __packed l_stack_t;
288
289/* The Linux sigcontext, pretty much a standard 386 trapframe. */
290struct l_sigcontext {
291	l_uint		sc_gs;
292	l_uint		sc_fs;
293	l_uint		sc_es;
294	l_uint		sc_ds;
295	l_uint		sc_edi;
296	l_uint		sc_esi;
297	l_uint		sc_ebp;
298	l_uint		sc_esp;
299	l_uint		sc_ebx;
300	l_uint		sc_edx;
301	l_uint		sc_ecx;
302	l_uint		sc_eax;
303	l_uint		sc_trapno;
304	l_uint		sc_err;
305	l_uint		sc_eip;
306	l_uint		sc_cs;
307	l_uint		sc_eflags;
308	l_uint		sc_esp_at_signal;
309	l_uint		sc_ss;
310	l_uint		sc_387;
311	l_uint		sc_mask;
312	l_uint		sc_cr2;
313} __packed;
314
315struct l_ucontext {
316	l_ulong		uc_flags;
317	l_uintptr_t	uc_link;
318	l_stack_t	uc_stack;
319	struct l_sigcontext	uc_mcontext;
320	l_sigset_t	uc_sigmask;
321} __packed;
322
323#define	LINUX_SI_MAX_SIZE	128
324#define	LINUX_SI_PAD_SIZE	((LINUX_SI_MAX_SIZE/sizeof(l_int)) - 3)
325
326typedef union l_sigval {
327	l_int		sival_int;
328	l_uintptr_t	sival_ptr;
329} l_sigval_t;
330
331typedef struct l_siginfo {
332	l_int		lsi_signo;
333	l_int		lsi_errno;
334	l_int		lsi_code;
335	union {
336		l_int	_pad[LINUX_SI_PAD_SIZE];
337
338		struct {
339			l_pid_t		_pid;
340			l_uid_t		_uid;
341		} __packed _kill;
342
343		struct {
344			l_timer_t	_tid;
345			l_int		_overrun;
346			char		_pad[sizeof(l_uid_t) - sizeof(l_int)];
347			l_sigval_t	_sigval;
348			l_int		_sys_private;
349		} __packed _timer;
350
351		struct {
352			l_pid_t		_pid;		/* sender's pid */
353			l_uid_t		_uid;		/* sender's uid */
354			l_sigval_t	_sigval;
355		} __packed _rt;
356
357		struct {
358			l_pid_t		_pid;		/* which child */
359			l_uid_t		_uid;		/* sender's uid */
360			l_int		_status;	/* exit code */
361			l_clock_t	_utime;
362			l_clock_t	_stime;
363		} __packed _sigchld;
364
365		struct {
366			l_uintptr_t	_addr;	/* Faulting insn/memory ref. */
367		} __packed _sigfault;
368
369		struct {
370			l_long		_band;	/* POLL_IN,POLL_OUT,POLL_MSG */
371			l_int		_fd;
372		} __packed _sigpoll;
373	} _sifields;
374} __packed l_siginfo_t;
375
376#define	lsi_pid		_sifields._kill._pid
377#define	lsi_uid		_sifields._kill._uid
378#define	lsi_tid		_sifields._timer._tid
379#define	lsi_overrun	_sifields._timer._overrun
380#define	lsi_sys_private	_sifields._timer._sys_private
381#define	lsi_status	_sifields._sigchld._status
382#define	lsi_utime	_sifields._sigchld._utime
383#define	lsi_stime	_sifields._sigchld._stime
384#define	lsi_value	_sifields._rt._sigval
385#define	lsi_int		_sifields._rt._sigval.sival_int
386#define	lsi_ptr		_sifields._rt._sigval.sival_ptr
387#define	lsi_addr	_sifields._sigfault._addr
388#define	lsi_band	_sifields._sigpoll._band
389#define	lsi_fd		_sifields._sigpoll._fd
390
391struct l_fpreg {
392	u_int16_t	significand[4];
393	u_int16_t	exponent;
394} __packed;
395
396struct l_fpxreg {
397	u_int16_t	significand[4];
398	u_int16_t	exponent;
399	u_int16_t	padding[3];
400} __packed;
401
402struct l_xmmreg {
403	u_int32_t	element[4];
404} __packed;
405
406struct l_fpstate {
407	/* Regular FPU environment */
408	u_int32_t		cw;
409	u_int32_t		sw;
410	u_int32_t		tag;
411	u_int32_t		ipoff;
412	u_int32_t		cssel;
413	u_int32_t		dataoff;
414	u_int32_t		datasel;
415	struct l_fpreg		_st[8];
416	u_int16_t		status;
417	u_int16_t		magic;		/* 0xffff = regular FPU data */
418
419	/* FXSR FPU environment */
420	u_int32_t		_fxsr_env[6];	/* env is ignored. */
421	u_int32_t		mxcsr;
422	u_int32_t		reserved;
423	struct l_fpxreg		_fxsr_st[8];	/* reg data is ignored. */
424	struct l_xmmreg		_xmm[8];
425	u_int32_t		padding[56];
426} __packed;
427
428/*
429 * We make the stack look like Linux expects it when calling a signal
430 * handler, but use the BSD way of calling the handler and sigreturn().
431 * This means that we need to pass the pointer to the handler too.
432 * It is appended to the frame to not interfere with the rest of it.
433 */
434struct l_sigframe {
435	l_int			sf_sig;
436	struct l_sigcontext	sf_sc;
437	struct l_fpstate	sf_fpstate;
438	l_uint			sf_extramask[1];
439	l_handler_t		sf_handler;
440} __packed;
441
442struct l_rt_sigframe {
443	l_int			sf_sig;
444	l_uintptr_t		sf_siginfo;
445	l_uintptr_t		sf_ucontext;
446	l_siginfo_t		sf_si;
447	struct l_ucontext	sf_sc;
448	l_handler_t		sf_handler;
449} __packed;
450
451/*
452 * arch specific open/fcntl flags
453 */
454#define	LINUX_F_GETLK64		12
455#define	LINUX_F_SETLK64		13
456#define	LINUX_F_SETLKW64	14
457
458union l_semun {
459	l_int		val;
460	l_uintptr_t	buf;
461	l_uintptr_t	array;
462	l_uintptr_t	__buf;
463	l_uintptr_t	__pad;
464} __packed;
465
466struct l_ifmap {
467	l_ulong		mem_start;
468	l_ulong		mem_end;
469	l_ushort	base_addr;
470	u_char		irq;
471	u_char		dma;
472	u_char		port;
473} __packed;
474
475struct l_ifreq {
476	union {
477		char	ifrn_name[LINUX_IFNAMSIZ];
478	} ifr_ifrn;
479
480	union {
481		struct l_sockaddr	ifru_addr;
482		struct l_sockaddr	ifru_dstaddr;
483		struct l_sockaddr	ifru_broadaddr;
484		struct l_sockaddr	ifru_netmask;
485		struct l_sockaddr	ifru_hwaddr;
486		l_short		ifru_flags[1];
487		l_int		ifru_ivalue;
488		l_int		ifru_mtu;
489		struct l_ifmap	ifru_map;
490		char		ifru_slave[LINUX_IFNAMSIZ];
491		l_uintptr_t	ifru_data;
492	} ifr_ifru;
493} __packed;
494
495#define	ifr_name	ifr_ifrn.ifrn_name	/* Interface name */
496#define	ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address */
497#define	ifr_ifindex	ifr_ifru.ifru_ivalue	/* Interface index */
498
499struct l_ifconf {
500	int	ifc_len;
501	union {
502		l_uintptr_t	ifcu_buf;
503		l_uintptr_t	ifcu_req;
504	} ifc_ifcu;
505} __packed;
506
507#define	ifc_buf		ifc_ifcu.ifcu_buf
508#define	ifc_req		ifc_ifcu.ifcu_req
509
510/*
511 * poll()
512 */
513#define	LINUX_POLLIN		0x0001
514#define	LINUX_POLLPRI		0x0002
515#define	LINUX_POLLOUT		0x0004
516#define	LINUX_POLLERR		0x0008
517#define	LINUX_POLLHUP		0x0010
518#define	LINUX_POLLNVAL		0x0020
519#define	LINUX_POLLRDNORM	0x0040
520#define	LINUX_POLLRDBAND	0x0080
521#define	LINUX_POLLWRNORM	0x0100
522#define	LINUX_POLLWRBAND	0x0200
523#define	LINUX_POLLMSG		0x0400
524
525struct l_pollfd {
526	l_int		fd;
527	l_short		events;
528	l_short		revents;
529} __packed;
530
531struct l_user_desc {
532	l_uint		entry_number;
533	l_uint		base_addr;
534	l_uint		limit;
535	l_uint		seg_32bit:1;
536	l_uint		contents:2;
537	l_uint		read_exec_only:1;
538	l_uint		limit_in_pages:1;
539	l_uint		seg_not_present:1;
540	l_uint		useable:1;
541};
542
543#define	LINUX_LOWERWORD	0x0000ffff
544
545/*
546 * Macros which does the same thing as those in Linux include/asm-um/ldt-i386.h.
547 * These convert Linux user space descriptor to machine one.
548 */
549#define	LINUX_LDT_entry_a(info)					\
550	((((info)->base_addr & LINUX_LOWERWORD) << 16) |	\
551	((info)->limit & LINUX_LOWERWORD))
552
553#define	LINUX_ENTRY_B_READ_EXEC_ONLY	9
554#define	LINUX_ENTRY_B_CONTENTS		10
555#define	LINUX_ENTRY_B_SEG_NOT_PRESENT	15
556#define	LINUX_ENTRY_B_BASE_ADDR		16
557#define	LINUX_ENTRY_B_USEABLE		20
558#define	LINUX_ENTRY_B_SEG32BIT		22
559#define	LINUX_ENTRY_B_LIMIT		23
560
561#define	LINUX_LDT_entry_b(info)							\
562	(((info)->base_addr & 0xff000000) |					\
563	((info)->limit & 0xf0000) |						\
564	((info)->contents << LINUX_ENTRY_B_CONTENTS) |				\
565	(((info)->seg_not_present == 0) << LINUX_ENTRY_B_SEG_NOT_PRESENT) |	\
566	(((info)->base_addr & 0x00ff0000) >> LINUX_ENTRY_B_BASE_ADDR) |		\
567	(((info)->read_exec_only == 0) << LINUX_ENTRY_B_READ_EXEC_ONLY) |	\
568	((info)->seg_32bit << LINUX_ENTRY_B_SEG32BIT) |				\
569	((info)->useable << LINUX_ENTRY_B_USEABLE) |				\
570	((info)->limit_in_pages << LINUX_ENTRY_B_LIMIT) | 0x7000)
571
572#define	LINUX_LDT_empty(info)		\
573	((info)->base_addr == 0 &&	\
574	(info)->limit == 0 &&		\
575	(info)->contents == 0 &&	\
576	(info)->seg_not_present == 1 &&	\
577	(info)->read_exec_only == 1 &&	\
578	(info)->seg_32bit == 0 &&	\
579	(info)->limit_in_pages == 0 &&	\
580	(info)->useable == 0)
581
582/*
583 * Macros for converting segments.
584 * They do the same as those in arch/i386/kernel/process.c in Linux.
585 */
586#define	LINUX_GET_BASE(desc)				\
587	((((desc)->a >> 16) & LINUX_LOWERWORD) |	\
588	(((desc)->b << 16) & 0x00ff0000) |		\
589	((desc)->b & 0xff000000))
590
591#define	LINUX_GET_LIMIT(desc)			\
592	(((desc)->a & LINUX_LOWERWORD) |	\
593	((desc)->b & 0xf0000))
594
595#define	LINUX_GET_32BIT(desc)		\
596	(((desc)->b >> LINUX_ENTRY_B_SEG32BIT) & 1)
597#define	LINUX_GET_CONTENTS(desc)	\
598	(((desc)->b >> LINUX_ENTRY_B_CONTENTS) & 3)
599#define	LINUX_GET_WRITABLE(desc)	\
600	(((desc)->b >> LINUX_ENTRY_B_READ_EXEC_ONLY) & 1)
601#define	LINUX_GET_LIMIT_PAGES(desc)	\
602	(((desc)->b >> LINUX_ENTRY_B_LIMIT) & 1)
603#define	LINUX_GET_PRESENT(desc)		\
604	(((desc)->b >> LINUX_ENTRY_B_SEG_NOT_PRESENT) & 1)
605#define	LINUX_GET_USEABLE(desc)		\
606	(((desc)->b >> LINUX_ENTRY_B_USEABLE) & 1)
607
608struct iovec;
609struct uio;
610
611struct l_iovec32 {
612	uint32_t	iov_base;
613	l_size_t	iov_len;
614};
615
616int linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt,
617			    struct iovec **iovp, int error);
618int linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt,
619			    struct uio **uiop);
620int linux_copyout_rusage(struct rusage *ru, void *uaddr);
621
622/* robust futexes */
623struct linux_robust_list {
624	l_uintptr_t			next;
625};
626
627struct linux_robust_list_head {
628	struct linux_robust_list	list;
629	l_long				futex_offset;
630	l_uintptr_t			pending_list;
631};
632
633#endif /* !_AMD64_LINUX_H_ */
634