xref: /freebsd/sys/i386/linux/linux.h (revision 5ab1c5846ff41be24b1f6beb0317bf8258cd4409)
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/signal.h>	/* for sigval union */
34 
35 #include <compat/linux/linux.h>
36 #include <i386/linux/linux_syscall.h>
37 
38 #define LINUX_LEGACY_SYSCALLS
39 
40 #define	LINUX_DTRACE	linuxulator
41 
42 #define	LINUX_SHAREDPAGE	(VM_MAXUSER_ADDRESS - PAGE_SIZE)
43 #define	LINUX_USRSTACK		LINUX_SHAREDPAGE
44 
45 #define	PTRIN(v)	(void *)(v)
46 #define	PTROUT(v)	(l_uintptr_t)(v)
47 
48 #define	CP(src,dst,fld) do { (dst).fld = (src).fld; } while (0)
49 #define	CP2(src,dst,sfld,dfld) do { (dst).dfld = (src).sfld; } while (0)
50 #define	PTRIN_CP(src,dst,fld) \
51 	do { (dst).fld = PTRIN((src).fld); } while (0)
52 
53 /*
54  * Provide a separate set of types for the Linux types.
55  */
56 typedef int		l_int;
57 typedef int32_t		l_long;
58 typedef int64_t		l_longlong;
59 typedef short		l_short;
60 typedef unsigned int	l_uint;
61 typedef uint32_t	l_ulong;
62 typedef uint64_t	l_ulonglong;
63 typedef unsigned short	l_ushort;
64 
65 typedef char		*l_caddr_t;
66 typedef l_ulong		l_uintptr_t;
67 typedef l_long		l_clock_t;
68 typedef l_int		l_daddr_t;
69 typedef l_ushort	l_dev_t;
70 typedef l_uint		l_gid_t;
71 typedef l_ushort	l_gid16_t;
72 typedef l_ulong		l_ino_t;
73 typedef l_int		l_key_t;
74 typedef l_longlong	l_loff_t;
75 typedef l_ushort	l_mode_t;
76 typedef l_long		l_off_t;
77 typedef l_int		l_pid_t;
78 typedef l_uint		l_size_t;
79 typedef l_long		l_suseconds_t;
80 typedef l_long		l_time_t;
81 typedef l_uint		l_uid_t;
82 typedef l_ushort	l_uid16_t;
83 typedef l_int		l_timer_t;
84 typedef l_int		l_mqd_t;
85 typedef	l_ulong		l_fd_mask;
86 
87 typedef struct {
88 	l_int		val[2];
89 } l_fsid_t;
90 
91 typedef struct {
92 	l_time_t	tv_sec;
93 	l_suseconds_t	tv_usec;
94 } l_timeval;
95 
96 #define	l_fd_set	fd_set
97 
98 /*
99  * Miscellaneous
100  */
101 #define LINUX_AT_COUNT		20	/* Count of used aux entry types.
102 					 * Keep this synchronized with
103 					 * linux_fixup_elf() code.
104 					 */
105 struct l___sysctl_args
106 {
107 	l_int		*name;
108 	l_int		nlen;
109 	void		*oldval;
110 	l_size_t	*oldlenp;
111 	void		*newval;
112 	l_size_t	newlen;
113 	l_ulong		__spare[4];
114 };
115 
116 /* Resource limits */
117 #define	LINUX_RLIMIT_CPU	0
118 #define	LINUX_RLIMIT_FSIZE	1
119 #define	LINUX_RLIMIT_DATA	2
120 #define	LINUX_RLIMIT_STACK	3
121 #define	LINUX_RLIMIT_CORE	4
122 #define	LINUX_RLIMIT_RSS	5
123 #define	LINUX_RLIMIT_NPROC	6
124 #define	LINUX_RLIMIT_NOFILE	7
125 #define	LINUX_RLIMIT_MEMLOCK	8
126 #define	LINUX_RLIMIT_AS		9	/* Address space limit */
127 
128 #define	LINUX_RLIM_NLIMITS	10
129 
130 struct l_rlimit {
131 	l_ulong rlim_cur;
132 	l_ulong rlim_max;
133 };
134 
135 struct l_mmap_argv {
136 	l_uintptr_t	addr;
137 	l_size_t	len;
138 	l_int		prot;
139 	l_int		flags;
140 	l_int		fd;
141 	l_off_t		pgoff;
142 } __packed;
143 
144 /*
145  * stat family of syscalls
146  */
147 struct l_timespec {
148 	l_time_t	tv_sec;
149 	l_long		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 
519 #define	LINUX_LOWERWORD	0x0000ffff
520 
521 /*
522  * Macros which does the same thing as those in Linux include/asm-um/ldt-i386.h.
523  * These convert Linux user space descriptor to machine one.
524  */
525 #define	LINUX_LDT_entry_a(info)					\
526 	((((info)->base_addr & LINUX_LOWERWORD) << 16) |	\
527 	((info)->limit & LINUX_LOWERWORD))
528 
529 #define	LINUX_ENTRY_B_READ_EXEC_ONLY	9
530 #define	LINUX_ENTRY_B_CONTENTS		10
531 #define	LINUX_ENTRY_B_SEG_NOT_PRESENT	15
532 #define	LINUX_ENTRY_B_BASE_ADDR		16
533 #define	LINUX_ENTRY_B_USEABLE		20
534 #define	LINUX_ENTRY_B_SEG32BIT		22
535 #define	LINUX_ENTRY_B_LIMIT		23
536 
537 #define	LINUX_LDT_entry_b(info)							\
538 	(((info)->base_addr & 0xff000000) |					\
539 	((info)->limit & 0xf0000) |						\
540 	((info)->contents << LINUX_ENTRY_B_CONTENTS) |				\
541 	(((info)->seg_not_present == 0) << LINUX_ENTRY_B_SEG_NOT_PRESENT) |	\
542 	(((info)->base_addr & 0x00ff0000) >> LINUX_ENTRY_B_BASE_ADDR) |		\
543 	(((info)->read_exec_only == 0) << LINUX_ENTRY_B_READ_EXEC_ONLY) |	\
544 	((info)->seg_32bit << LINUX_ENTRY_B_SEG32BIT) |				\
545 	((info)->useable << LINUX_ENTRY_B_USEABLE) |				\
546 	((info)->limit_in_pages << LINUX_ENTRY_B_LIMIT) | 0x7000)
547 
548 #define	LINUX_LDT_empty(info)		\
549 	((info)->base_addr == 0 &&	\
550 	(info)->limit == 0 &&		\
551 	(info)->contents == 0 &&	\
552 	(info)->seg_not_present == 1 &&	\
553 	(info)->read_exec_only == 1 &&	\
554 	(info)->seg_32bit == 0 &&	\
555 	(info)->limit_in_pages == 0 &&	\
556 	(info)->useable == 0)
557 
558 /*
559  * Macros for converting segments.
560  * They do the same as those in arch/i386/kernel/process.c in Linux.
561  */
562 #define	LINUX_GET_BASE(desc)				\
563 	((((desc)->a >> 16) & LINUX_LOWERWORD) |	\
564 	(((desc)->b << 16) & 0x00ff0000) |		\
565 	((desc)->b & 0xff000000))
566 
567 #define	LINUX_GET_LIMIT(desc)			\
568 	(((desc)->a & LINUX_LOWERWORD) |	\
569 	((desc)->b & 0xf0000))
570 
571 #define	LINUX_GET_32BIT(desc)		\
572 	(((desc)->b >> LINUX_ENTRY_B_SEG32BIT) & 1)
573 #define	LINUX_GET_CONTENTS(desc)	\
574 	(((desc)->b >> LINUX_ENTRY_B_CONTENTS) & 3)
575 #define	LINUX_GET_WRITABLE(desc)	\
576 	(((desc)->b >> LINUX_ENTRY_B_READ_EXEC_ONLY) & 1)
577 #define	LINUX_GET_LIMIT_PAGES(desc)	\
578 	(((desc)->b >> LINUX_ENTRY_B_LIMIT) & 1)
579 #define	LINUX_GET_PRESENT(desc)		\
580 	(((desc)->b >> LINUX_ENTRY_B_SEG_NOT_PRESENT) & 1)
581 #define	LINUX_GET_USEABLE(desc)		\
582 	(((desc)->b >> LINUX_ENTRY_B_USEABLE) & 1)
583 
584 #define	linux_copyout_rusage(r, u)	copyout(r, u, sizeof(*r))
585 
586 /* robust futexes */
587 struct linux_robust_list {
588 	struct linux_robust_list	*next;
589 };
590 
591 struct linux_robust_list_head {
592 	struct linux_robust_list	list;
593 	l_long				futex_offset;
594 	struct linux_robust_list	*pending_list;
595 };
596 
597 #endif /* !_I386_LINUX_H_ */
598