xref: /freebsd/sys/amd64/linux32/linux.h (revision a50d73d5782a351ad83e8d1f84d11720a12e70d3)
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_ifconf {
308 	int	ifc_len;
309 	union {
310 		l_uintptr_t	ifcu_buf;
311 		l_uintptr_t	ifcu_req;
312 	} ifc_ifcu;
313 };
314 
315 #define	ifc_buf		ifc_ifcu.ifcu_buf
316 #define	ifc_req		ifc_ifcu.ifcu_req
317 
318 struct l_user_desc {
319 	l_uint		entry_number;
320 	l_uint		base_addr;
321 	l_uint		limit;
322 	l_uint		seg_32bit:1;
323 	l_uint		contents:2;
324 	l_uint		read_exec_only:1;
325 	l_uint		limit_in_pages:1;
326 	l_uint		seg_not_present:1;
327 	l_uint		useable:1;
328 };
329 
330 #define	LINUX_LOWERWORD	0x0000ffff
331 
332 /*
333  * Macros which does the same thing as those in Linux include/asm-um/ldt-i386.h.
334  * These convert Linux user space descriptor to machine one.
335  */
336 #define	LINUX_LDT_entry_a(info)					\
337 	((((info)->base_addr & LINUX_LOWERWORD) << 16) |	\
338 	((info)->limit & LINUX_LOWERWORD))
339 
340 #define	LINUX_ENTRY_B_READ_EXEC_ONLY	9
341 #define	LINUX_ENTRY_B_CONTENTS		10
342 #define	LINUX_ENTRY_B_SEG_NOT_PRESENT	15
343 #define	LINUX_ENTRY_B_BASE_ADDR		16
344 #define	LINUX_ENTRY_B_USEABLE		20
345 #define	LINUX_ENTRY_B_SEG32BIT		22
346 #define	LINUX_ENTRY_B_LIMIT		23
347 
348 #define	LINUX_LDT_entry_b(info)							\
349 	(((info)->base_addr & 0xff000000) |					\
350 	((info)->limit & 0xf0000) |						\
351 	((info)->contents << LINUX_ENTRY_B_CONTENTS) |				\
352 	(((info)->seg_not_present == 0) << LINUX_ENTRY_B_SEG_NOT_PRESENT) |	\
353 	(((info)->base_addr & 0x00ff0000) >> LINUX_ENTRY_B_BASE_ADDR) |		\
354 	(((info)->read_exec_only == 0) << LINUX_ENTRY_B_READ_EXEC_ONLY) |	\
355 	((info)->seg_32bit << LINUX_ENTRY_B_SEG32BIT) |				\
356 	((info)->useable << LINUX_ENTRY_B_USEABLE) |				\
357 	((info)->limit_in_pages << LINUX_ENTRY_B_LIMIT) | 0x7000)
358 
359 #define	LINUX_LDT_empty(info)		\
360 	((info)->base_addr == 0 &&	\
361 	(info)->limit == 0 &&		\
362 	(info)->contents == 0 &&	\
363 	(info)->seg_not_present == 1 &&	\
364 	(info)->read_exec_only == 1 &&	\
365 	(info)->seg_32bit == 0 &&	\
366 	(info)->limit_in_pages == 0 &&	\
367 	(info)->useable == 0)
368 
369 /*
370  * Macros for converting segments.
371  * They do the same as those in arch/i386/kernel/process.c in Linux.
372  */
373 #define	LINUX_GET_BASE(desc)				\
374 	((((desc)->a >> 16) & LINUX_LOWERWORD) |	\
375 	(((desc)->b << 16) & 0x00ff0000) |		\
376 	((desc)->b & 0xff000000))
377 
378 #define	LINUX_GET_LIMIT(desc)			\
379 	(((desc)->a & LINUX_LOWERWORD) |	\
380 	((desc)->b & 0xf0000))
381 
382 #define	LINUX_GET_32BIT(desc)		\
383 	(((desc)->b >> LINUX_ENTRY_B_SEG32BIT) & 1)
384 #define	LINUX_GET_CONTENTS(desc)	\
385 	(((desc)->b >> LINUX_ENTRY_B_CONTENTS) & 3)
386 #define	LINUX_GET_WRITABLE(desc)	\
387 	(((desc)->b >> LINUX_ENTRY_B_READ_EXEC_ONLY) & 1)
388 #define	LINUX_GET_LIMIT_PAGES(desc)	\
389 	(((desc)->b >> LINUX_ENTRY_B_LIMIT) & 1)
390 #define	LINUX_GET_PRESENT(desc)		\
391 	(((desc)->b >> LINUX_ENTRY_B_SEG_NOT_PRESENT) & 1)
392 #define	LINUX_GET_USEABLE(desc)		\
393 	(((desc)->b >> LINUX_ENTRY_B_USEABLE) & 1)
394 
395 #ifdef _KERNEL
396 struct iovec;
397 struct uio;
398 
399 struct l_iovec32 {
400 	uint32_t	iov_base;
401 	l_size_t	iov_len;
402 };
403 
404 int linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt,
405 			    struct iovec **iovp, int error);
406 int linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt,
407 			    struct uio **uiop);
408 int linux_copyout_rusage(struct rusage *ru, void *uaddr);
409 #endif /* _KERNEL */
410 
411 /* This corresponds to 'struct user_regs_struct32' in Linux. */
412 struct linux_pt_regset32 {
413 	l_uint ebx;
414 	l_uint ecx;
415 	l_uint edx;
416 	l_uint esi;
417 	l_uint edi;
418 	l_uint ebp;
419 	l_uint eax;
420 	l_uint ds;
421 	l_uint es;
422 	l_uint fs;
423 	l_uint gs;
424 	l_uint orig_eax;
425 	l_uint eip;
426 	l_uint cs;
427 	l_uint eflags;
428 	l_uint esp;
429 	l_uint ss;
430 };
431 
432 #ifdef _KERNEL
433 struct reg32;
434 
435 void	bsd_to_linux_regset32(const struct reg32 *b_reg,
436 	    struct linux_pt_regset32 *l_regset);
437 
438 extern bool linux32_emulate_i386;
439 #endif /* _KERNEL */
440 
441 #endif /* !_AMD64_LINUX_H_ */
442