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