xref: /freebsd/sys/amd64/linux32/linux.h (revision 8eb2bee6c0f4957c6c1cea826e59cda4d18a2a64)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 Tim J. Robbins
5  * Copyright (c) 2001 Doug Rabson
6  * Copyright (c) 1994-1996 Søren Schmidt
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 
35 #ifndef _AMD64_LINUX_H_
36 #define	_AMD64_LINUX_H_
37 
38 #include <sys/abi_compat.h>
39 
40 #include <compat/linux/linux.h>
41 #include <amd64/linux32/linux32_syscall.h>
42 
43 #define	LINUX_LEGACY_SYSCALLS
44 
45 #define	LINUX_DTRACE	linuxulator32
46 
47 #define	LINUX32_MAXDSIZ		(512 * 1024 * 1024)	/* 512MB */
48 #define	LINUX32_MAXSSIZ		(64 * 1024 * 1024)	/* 64MB */
49 #define	LINUX32_MAXVMEM		0			/* Unlimited */
50 
51 /*
52  * Provide a separate set of types for the Linux types.
53  */
54 typedef int		l_int;
55 typedef int32_t		l_long;
56 typedef int64_t		l_longlong;
57 typedef short		l_short;
58 typedef unsigned int	l_uint;
59 typedef uint32_t	l_ulong;
60 typedef uint64_t	l_ulonglong;
61 typedef unsigned short	l_ushort;
62 
63 typedef l_ulong		l_uintptr_t;
64 typedef l_long		l_clock_t;
65 typedef l_int		l_daddr_t;
66 typedef l_ushort	l_dev_t;
67 typedef l_uint		l_gid_t;
68 typedef l_ushort	l_gid16_t;
69 typedef l_ulong		l_ino_t;
70 typedef l_int		l_key_t;
71 typedef l_longlong	l_loff_t;
72 typedef l_ushort	l_mode_t;
73 typedef l_long		l_off_t;
74 typedef l_int		l_pid_t;
75 typedef l_uint		l_size_t;
76 typedef l_long		l_suseconds_t;
77 typedef l_long		l_time_t;
78 typedef l_longlong	l_time64_t;
79 typedef l_uint		l_uid_t;
80 typedef l_ushort	l_uid16_t;
81 typedef l_int		l_timer_t;
82 typedef l_int		l_mqd_t;
83 typedef	l_ulong		l_fd_mask;
84 
85 #include <compat/linux/linux_siginfo.h>
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 typedef struct {
97 	l_time64_t	tv_sec;
98 	l_time64_t	tv_usec;
99 } l_sock_timeval;
100 
101 #define	l_fd_set	fd_set
102 
103 /*
104  * Miscellaneous
105  */
106 #define	LINUX_AT_COUNT		21	/* Count of used aux entry types.
107 					 * Keep this synchronized with
108 					 * linux_fixup_elf() code.
109 					 */
110 struct l___sysctl_args
111 {
112 	l_uintptr_t	name;
113 	l_int		nlen;
114 	l_uintptr_t	oldval;
115 	l_uintptr_t	oldlenp;
116 	l_uintptr_t	newval;
117 	l_size_t	newlen;
118 	l_ulong		__spare[4];
119 };
120 
121 /* Resource limits */
122 #define	LINUX_RLIMIT_CPU	0
123 #define	LINUX_RLIMIT_FSIZE	1
124 #define	LINUX_RLIMIT_DATA	2
125 #define	LINUX_RLIMIT_STACK	3
126 #define	LINUX_RLIMIT_CORE	4
127 #define	LINUX_RLIMIT_RSS	5
128 #define	LINUX_RLIMIT_NPROC	6
129 #define	LINUX_RLIMIT_NOFILE	7
130 #define	LINUX_RLIMIT_MEMLOCK	8
131 #define	LINUX_RLIMIT_AS		9	/* Address space limit */
132 
133 #define	LINUX_RLIM_NLIMITS	10
134 
135 struct l_rlimit {
136 	l_ulong rlim_cur;
137 	l_ulong rlim_max;
138 };
139 
140 struct l_rusage {
141 	l_timeval ru_utime;
142 	l_timeval ru_stime;
143 	l_long	ru_maxrss;
144 	l_long	ru_ixrss;
145 	l_long	ru_idrss;
146 	l_long	ru_isrss;
147 	l_long	ru_minflt;
148 	l_long	ru_majflt;
149 	l_long	ru_nswap;
150 	l_long	ru_inblock;
151 	l_long	ru_oublock;
152 	l_long	ru_msgsnd;
153 	l_long	ru_msgrcv;
154 	l_long	ru_nsignals;
155 	l_long	ru_nvcsw;
156 	l_long	ru_nivcsw;
157 };
158 
159 struct l_mmap_argv {
160 	l_uintptr_t	addr;
161 	l_size_t	len;
162 	l_int		prot;
163 	l_int		flags;
164 	l_int		fd;
165 	l_ulong		pgoff;
166 };
167 
168 /*
169  * stat family of syscalls
170  */
171 struct l_timespec {
172 	l_time_t	tv_sec;
173 	l_long		tv_nsec;
174 };
175 
176 /* __kernel_timespec */
177 struct l_timespec64 {
178 	l_time64_t	tv_sec;
179 	l_longlong	tv_nsec;
180 };
181 
182 struct l_newstat {
183 	l_ushort	st_dev;
184 	l_ushort	__pad1;
185 	l_ulong		st_ino;
186 	l_ushort	st_mode;
187 	l_ushort	st_nlink;
188 	l_ushort	st_uid;
189 	l_ushort	st_gid;
190 	l_ushort	st_rdev;
191 	l_ushort	__pad2;
192 	l_ulong		st_size;
193 	l_ulong		st_blksize;
194 	l_ulong		st_blocks;
195 	struct l_timespec	st_atim;
196 	struct l_timespec	st_mtim;
197 	struct l_timespec	st_ctim;
198 	l_ulong		__unused4;
199 	l_ulong		__unused5;
200 };
201 
202 struct l_stat {
203 	l_ushort	st_dev;
204 	l_ulong		st_ino;
205 	l_ushort	st_mode;
206 	l_ushort	st_nlink;
207 	l_ushort	st_uid;
208 	l_ushort	st_gid;
209 	l_ushort	st_rdev;
210 	l_long		st_size;
211 	struct l_timespec	st_atim;
212 	struct l_timespec	st_mtim;
213 	struct l_timespec	st_ctim;
214 	l_long		st_blksize;
215 	l_long		st_blocks;
216 	l_ulong		st_flags;
217 	l_ulong		st_gen;
218 };
219 
220 struct l_stat64 {
221 	l_ushort	st_dev;
222 	u_char		__pad0[10];
223 	l_ulong		__st_ino;
224 	l_uint		st_mode;
225 	l_uint		st_nlink;
226 	l_ulong		st_uid;
227 	l_ulong		st_gid;
228 	l_ushort	st_rdev;
229 	u_char		__pad3[10];
230 	l_longlong	st_size;
231 	l_ulong		st_blksize;
232 	l_ulong		st_blocks;
233 	l_ulong		__pad4;
234 	struct l_timespec	st_atim;
235 	struct l_timespec	st_mtim;
236 	struct l_timespec	st_ctim;
237 	l_ulonglong	st_ino;
238 } __packed;
239 
240 struct l_statfs64 {
241 	l_int		f_type;
242 	l_int		f_bsize;
243 	uint64_t	f_blocks;
244 	uint64_t	f_bfree;
245 	uint64_t	f_bavail;
246 	uint64_t	f_files;
247 	uint64_t	f_ffree;
248 	l_fsid_t	f_fsid;
249 	l_int		f_namelen;
250 	l_int		f_frsize;
251 	l_int		f_flags;
252 	l_int		f_spare[4];
253 } __packed;
254 
255 /* sigaction flags */
256 #define	LINUX_SA_NOCLDSTOP	0x00000001
257 #define	LINUX_SA_NOCLDWAIT	0x00000002
258 #define	LINUX_SA_SIGINFO	0x00000004
259 #define	LINUX_SA_RESTORER	0x04000000
260 #define	LINUX_SA_ONSTACK	0x08000000
261 #define	LINUX_SA_RESTART	0x10000000
262 #define	LINUX_SA_INTERRUPT	0x20000000
263 #define	LINUX_SA_NOMASK		0x40000000
264 #define	LINUX_SA_ONESHOT	0x80000000
265 
266 /* sigaltstack */
267 #define	LINUX_MINSIGSTKSZ	2048
268 
269 typedef l_uintptr_t l_handler_t;
270 typedef l_ulong	l_osigset_t;
271 
272 typedef struct {
273 	l_handler_t	lsa_handler;
274 	l_osigset_t	lsa_mask;
275 	l_ulong		lsa_flags;
276 	l_uintptr_t	lsa_restorer;
277 } l_osigaction_t;
278 
279 typedef struct {
280 	l_handler_t	lsa_handler;
281 	l_ulong		lsa_flags;
282 	l_uintptr_t	lsa_restorer;
283 	l_sigset_t	lsa_mask;
284 } __packed l_sigaction_t;
285 
286 typedef struct {
287 	l_uintptr_t	ss_sp;
288 	l_int		ss_flags;
289 	l_size_t	ss_size;
290 } l_stack_t;
291 
292 /*
293  * arch specific open/fcntl flags
294  */
295 #define	LINUX_F_GETLK64		12
296 #define	LINUX_F_SETLK64		13
297 #define	LINUX_F_SETLKW64	14
298 
299 union l_semun {
300 	l_int		val;
301 	l_uintptr_t	buf;
302 	l_uintptr_t	array;
303 	l_uintptr_t	__buf;
304 	l_uintptr_t	__pad;
305 };
306 
307 struct l_ifmap {
308 	l_ulong		mem_start;
309 	l_ulong		mem_end;
310 	l_ushort	base_addr;
311 	u_char		irq;
312 	u_char		dma;
313 	u_char		port;
314 	/* 3 bytes spare */
315 };
316 
317 struct l_ifreq {
318 	union {
319 		char	ifrn_name[LINUX_IFNAMSIZ];
320 	} ifr_ifrn;
321 
322 	union {
323 		struct l_sockaddr	ifru_addr;
324 		struct l_sockaddr	ifru_dstaddr;
325 		struct l_sockaddr	ifru_broadaddr;
326 		struct l_sockaddr	ifru_netmask;
327 		struct l_sockaddr	ifru_hwaddr;
328 		l_short		ifru_flags[1];
329 		l_int		ifru_ivalue;
330 		l_int		ifru_mtu;
331 		struct l_ifmap	ifru_map;
332 		char		ifru_slave[LINUX_IFNAMSIZ];
333 		l_uintptr_t	ifru_data;
334 	} ifr_ifru;
335 };
336 
337 #define	ifr_name	ifr_ifrn.ifrn_name	/* Interface name */
338 #define	ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address */
339 #define	ifr_ifindex	ifr_ifru.ifru_ivalue	/* Interface index */
340 
341 struct l_ifconf {
342 	int	ifc_len;
343 	union {
344 		l_uintptr_t	ifcu_buf;
345 		l_uintptr_t	ifcu_req;
346 	} ifc_ifcu;
347 };
348 
349 #define	ifc_buf		ifc_ifcu.ifcu_buf
350 #define	ifc_req		ifc_ifcu.ifcu_req
351 
352 struct l_user_desc {
353 	l_uint		entry_number;
354 	l_uint		base_addr;
355 	l_uint		limit;
356 	l_uint		seg_32bit:1;
357 	l_uint		contents:2;
358 	l_uint		read_exec_only:1;
359 	l_uint		limit_in_pages:1;
360 	l_uint		seg_not_present:1;
361 	l_uint		useable:1;
362 };
363 
364 #define	LINUX_LOWERWORD	0x0000ffff
365 
366 /*
367  * Macros which does the same thing as those in Linux include/asm-um/ldt-i386.h.
368  * These convert Linux user space descriptor to machine one.
369  */
370 #define	LINUX_LDT_entry_a(info)					\
371 	((((info)->base_addr & LINUX_LOWERWORD) << 16) |	\
372 	((info)->limit & LINUX_LOWERWORD))
373 
374 #define	LINUX_ENTRY_B_READ_EXEC_ONLY	9
375 #define	LINUX_ENTRY_B_CONTENTS		10
376 #define	LINUX_ENTRY_B_SEG_NOT_PRESENT	15
377 #define	LINUX_ENTRY_B_BASE_ADDR		16
378 #define	LINUX_ENTRY_B_USEABLE		20
379 #define	LINUX_ENTRY_B_SEG32BIT		22
380 #define	LINUX_ENTRY_B_LIMIT		23
381 
382 #define	LINUX_LDT_entry_b(info)							\
383 	(((info)->base_addr & 0xff000000) |					\
384 	((info)->limit & 0xf0000) |						\
385 	((info)->contents << LINUX_ENTRY_B_CONTENTS) |				\
386 	(((info)->seg_not_present == 0) << LINUX_ENTRY_B_SEG_NOT_PRESENT) |	\
387 	(((info)->base_addr & 0x00ff0000) >> LINUX_ENTRY_B_BASE_ADDR) |		\
388 	(((info)->read_exec_only == 0) << LINUX_ENTRY_B_READ_EXEC_ONLY) |	\
389 	((info)->seg_32bit << LINUX_ENTRY_B_SEG32BIT) |				\
390 	((info)->useable << LINUX_ENTRY_B_USEABLE) |				\
391 	((info)->limit_in_pages << LINUX_ENTRY_B_LIMIT) | 0x7000)
392 
393 #define	LINUX_LDT_empty(info)		\
394 	((info)->base_addr == 0 &&	\
395 	(info)->limit == 0 &&		\
396 	(info)->contents == 0 &&	\
397 	(info)->seg_not_present == 1 &&	\
398 	(info)->read_exec_only == 1 &&	\
399 	(info)->seg_32bit == 0 &&	\
400 	(info)->limit_in_pages == 0 &&	\
401 	(info)->useable == 0)
402 
403 /*
404  * Macros for converting segments.
405  * They do the same as those in arch/i386/kernel/process.c in Linux.
406  */
407 #define	LINUX_GET_BASE(desc)				\
408 	((((desc)->a >> 16) & LINUX_LOWERWORD) |	\
409 	(((desc)->b << 16) & 0x00ff0000) |		\
410 	((desc)->b & 0xff000000))
411 
412 #define	LINUX_GET_LIMIT(desc)			\
413 	(((desc)->a & LINUX_LOWERWORD) |	\
414 	((desc)->b & 0xf0000))
415 
416 #define	LINUX_GET_32BIT(desc)		\
417 	(((desc)->b >> LINUX_ENTRY_B_SEG32BIT) & 1)
418 #define	LINUX_GET_CONTENTS(desc)	\
419 	(((desc)->b >> LINUX_ENTRY_B_CONTENTS) & 3)
420 #define	LINUX_GET_WRITABLE(desc)	\
421 	(((desc)->b >> LINUX_ENTRY_B_READ_EXEC_ONLY) & 1)
422 #define	LINUX_GET_LIMIT_PAGES(desc)	\
423 	(((desc)->b >> LINUX_ENTRY_B_LIMIT) & 1)
424 #define	LINUX_GET_PRESENT(desc)		\
425 	(((desc)->b >> LINUX_ENTRY_B_SEG_NOT_PRESENT) & 1)
426 #define	LINUX_GET_USEABLE(desc)		\
427 	(((desc)->b >> LINUX_ENTRY_B_USEABLE) & 1)
428 
429 #ifdef _KERNEL
430 struct iovec;
431 struct uio;
432 
433 struct l_iovec32 {
434 	uint32_t	iov_base;
435 	l_size_t	iov_len;
436 };
437 
438 int linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt,
439 			    struct iovec **iovp, int error);
440 int linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt,
441 			    struct uio **uiop);
442 int linux_copyout_rusage(struct rusage *ru, void *uaddr);
443 #endif /* _KERNEL */
444 
445 /* robust futexes */
446 struct linux_robust_list {
447 	l_uintptr_t			next;
448 };
449 
450 struct linux_robust_list_head {
451 	struct linux_robust_list	list;
452 	l_long				futex_offset;
453 	l_uintptr_t			pending_list;
454 };
455 
456 /* This corresponds to 'struct user_regs_struct32' in Linux. */
457 struct linux_pt_regset32 {
458 	l_uint ebx;
459 	l_uint ecx;
460 	l_uint edx;
461 	l_uint esi;
462 	l_uint edi;
463 	l_uint ebp;
464 	l_uint eax;
465 	l_uint ds;
466 	l_uint es;
467 	l_uint fs;
468 	l_uint gs;
469 	l_uint orig_eax;
470 	l_uint eip;
471 	l_uint cs;
472 	l_uint eflags;
473 	l_uint esp;
474 	l_uint ss;
475 };
476 
477 #ifdef _KERNEL
478 struct reg32;
479 
480 void	bsd_to_linux_regset32(const struct reg32 *b_reg,
481 	    struct linux_pt_regset32 *l_regset);
482 
483 extern bool linux32_emulate_i386;
484 #endif /* _KERNEL */
485 
486 #endif /* !_AMD64_LINUX_H_ */
487