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