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