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