xref: /freebsd/sys/amd64/linux32/linux.h (revision 7eee0eaf1602765bdf20c8e56884069085812c27)
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_uint		l_gid_t;
67 typedef l_ushort	l_gid16_t;
68 typedef l_ulong		l_ino_t;
69 typedef l_int		l_key_t;
70 typedef l_longlong	l_loff_t;
71 typedef l_ushort	l_mode_t;
72 typedef l_long		l_off_t;
73 typedef l_int		l_pid_t;
74 typedef l_uint		l_size_t;
75 typedef l_long		l_suseconds_t;
76 typedef l_long		l_time_t;
77 typedef l_longlong	l_time64_t;
78 typedef l_uint		l_uid_t;
79 typedef l_ushort	l_uid16_t;
80 typedef l_int		l_timer_t;
81 typedef l_int		l_mqd_t;
82 typedef	l_ulong		l_fd_mask;
83 
84 #include <compat/linux/linux_siginfo.h>
85 
86 typedef struct {
87 	l_int		val[2];
88 } l_fsid_t;
89 
90 typedef struct {
91 	l_time_t	tv_sec;
92 	l_suseconds_t	tv_usec;
93 } l_timeval;
94 
95 typedef struct {
96 	l_time64_t	tv_sec;
97 	l_time64_t	tv_usec;
98 } l_sock_timeval;
99 
100 #define	l_fd_set	fd_set
101 
102 /*
103  * Miscellaneous
104  */
105 #define	LINUX_AT_COUNT		22	/* Count of used aux entry types.
106 					 * Keep this synchronized with
107 					 * linux_copyout_auxargs() code.
108 					 */
109 struct l___sysctl_args
110 {
111 	l_uintptr_t	name;
112 	l_int		nlen;
113 	l_uintptr_t	oldval;
114 	l_uintptr_t	oldlenp;
115 	l_uintptr_t	newval;
116 	l_size_t	newlen;
117 	l_ulong		__spare[4];
118 };
119 
120 /* Resource limits */
121 #define	LINUX_RLIMIT_CPU	0
122 #define	LINUX_RLIMIT_FSIZE	1
123 #define	LINUX_RLIMIT_DATA	2
124 #define	LINUX_RLIMIT_STACK	3
125 #define	LINUX_RLIMIT_CORE	4
126 #define	LINUX_RLIMIT_RSS	5
127 #define	LINUX_RLIMIT_NPROC	6
128 #define	LINUX_RLIMIT_NOFILE	7
129 #define	LINUX_RLIMIT_MEMLOCK	8
130 #define	LINUX_RLIMIT_AS		9	/* Address space limit */
131 
132 #define	LINUX_RLIM_NLIMITS	10
133 
134 struct l_rlimit {
135 	l_ulong rlim_cur;
136 	l_ulong rlim_max;
137 };
138 
139 struct l_rusage {
140 	l_timeval ru_utime;
141 	l_timeval ru_stime;
142 	l_long	ru_maxrss;
143 	l_long	ru_ixrss;
144 	l_long	ru_idrss;
145 	l_long	ru_isrss;
146 	l_long	ru_minflt;
147 	l_long	ru_majflt;
148 	l_long	ru_nswap;
149 	l_long	ru_inblock;
150 	l_long	ru_oublock;
151 	l_long	ru_msgsnd;
152 	l_long	ru_msgrcv;
153 	l_long	ru_nsignals;
154 	l_long	ru_nvcsw;
155 	l_long	ru_nivcsw;
156 };
157 
158 struct l_mmap_argv {
159 	l_uintptr_t	addr;
160 	l_size_t	len;
161 	l_int		prot;
162 	l_int		flags;
163 	l_int		fd;
164 	l_ulong		pgoff;
165 };
166 
167 /*
168  * stat family of syscalls
169  */
170 struct l_timespec {
171 	l_time_t	tv_sec;
172 	l_long		tv_nsec;
173 };
174 
175 /* __kernel_timespec */
176 struct l_timespec64 {
177 	l_time64_t	tv_sec;
178 	l_longlong	tv_nsec;
179 };
180 
181 struct l_newstat {
182 	l_ulong		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_ulong		st_rdev;
189 	l_ulong		st_size;
190 	l_ulong		st_blksize;
191 	l_ulong		st_blocks;
192 	struct l_timespec	st_atim;
193 	struct l_timespec	st_mtim;
194 	struct l_timespec	st_ctim;
195 	l_ulong		__unused4;
196 	l_ulong		__unused5;
197 };
198 
199 /* __old_kernel_stat now */
200 struct l_old_stat {
201 	l_ushort	st_dev;
202 	l_ulong		st_ino;
203 	l_ushort	st_mode;
204 	l_ushort	st_nlink;
205 	l_ushort	st_uid;
206 	l_ushort	st_gid;
207 	l_ushort	st_rdev;
208 	l_long		st_size;
209 	struct l_timespec	st_atim;
210 	struct l_timespec	st_mtim;
211 	struct l_timespec	st_ctim;
212 	l_long		st_blksize;
213 	l_long		st_blocks;
214 	l_ulong		st_flags;
215 	l_ulong		st_gen;
216 };
217 
218 struct l_stat64 {
219 	l_ulonglong	st_dev;
220 	u_char		__pad0[4];
221 	l_ulong		__st_ino;
222 	l_uint		st_mode;
223 	l_uint		st_nlink;
224 	l_ulong		st_uid;
225 	l_ulong		st_gid;
226 	l_ulonglong	st_rdev;
227 	u_char		__pad3[4];
228 	l_longlong	st_size;
229 	l_ulong		st_blksize;
230 	l_ulonglong	st_blocks;
231 	struct l_timespec	st_atim;
232 	struct l_timespec	st_mtim;
233 	struct l_timespec	st_ctim;
234 	l_ulonglong	st_ino;
235 } __packed;
236 
237 struct l_statfs64 {
238 	l_int		f_type;
239 	l_int		f_bsize;
240 	uint64_t	f_blocks;
241 	uint64_t	f_bfree;
242 	uint64_t	f_bavail;
243 	uint64_t	f_files;
244 	uint64_t	f_ffree;
245 	l_fsid_t	f_fsid;
246 	l_int		f_namelen;
247 	l_int		f_frsize;
248 	l_int		f_flags;
249 	l_int		f_spare[4];
250 } __packed;
251 
252 /* sigaction flags */
253 #define	LINUX_SA_NOCLDSTOP	0x00000001
254 #define	LINUX_SA_NOCLDWAIT	0x00000002
255 #define	LINUX_SA_SIGINFO	0x00000004
256 #define	LINUX_SA_RESTORER	0x04000000
257 #define	LINUX_SA_ONSTACK	0x08000000
258 #define	LINUX_SA_RESTART	0x10000000
259 #define	LINUX_SA_INTERRUPT	0x20000000
260 #define	LINUX_SA_NOMASK		0x40000000
261 #define	LINUX_SA_ONESHOT	0x80000000
262 
263 /* sigaltstack */
264 #define	LINUX_MINSIGSTKSZ	2048
265 
266 typedef l_uintptr_t l_handler_t;
267 typedef l_ulong	l_osigset_t;
268 
269 typedef struct {
270 	l_handler_t	lsa_handler;
271 	l_osigset_t	lsa_mask;
272 	l_ulong		lsa_flags;
273 	l_uintptr_t	lsa_restorer;
274 } l_osigaction_t;
275 
276 typedef struct {
277 	l_handler_t	lsa_handler;
278 	l_ulong		lsa_flags;
279 	l_uintptr_t	lsa_restorer;
280 	l_sigset_t	lsa_mask;
281 } __packed l_sigaction_t;
282 
283 typedef struct {
284 	l_uintptr_t	ss_sp;
285 	l_int		ss_flags;
286 	l_size_t	ss_size;
287 } l_stack_t;
288 
289 /*
290  * arch specific open/fcntl flags
291  */
292 #define	LINUX_F_GETLK64		12
293 #define	LINUX_F_SETLK64		13
294 #define	LINUX_F_SETLKW64	14
295 
296 union l_semun {
297 	l_int		val;
298 	l_uintptr_t	buf;
299 	l_uintptr_t	array;
300 	l_uintptr_t	__buf;
301 	l_uintptr_t	__pad;
302 };
303 
304 struct l_ifconf {
305 	int	ifc_len;
306 	union {
307 		l_uintptr_t	ifcu_buf;
308 		l_uintptr_t	ifcu_req;
309 	} ifc_ifcu;
310 };
311 
312 #define	ifc_buf		ifc_ifcu.ifcu_buf
313 #define	ifc_req		ifc_ifcu.ifcu_req
314 
315 struct l_user_desc {
316 	l_uint		entry_number;
317 	l_uint		base_addr;
318 	l_uint		limit;
319 	l_uint		seg_32bit:1;
320 	l_uint		contents:2;
321 	l_uint		read_exec_only:1;
322 	l_uint		limit_in_pages:1;
323 	l_uint		seg_not_present:1;
324 	l_uint		useable:1;
325 };
326 
327 #define	LINUX_LOWERWORD	0x0000ffff
328 
329 /*
330  * Macros which does the same thing as those in Linux include/asm-um/ldt-i386.h.
331  * These convert Linux user space descriptor to machine one.
332  */
333 #define	LINUX_LDT_entry_a(info)					\
334 	((((info)->base_addr & LINUX_LOWERWORD) << 16) |	\
335 	((info)->limit & LINUX_LOWERWORD))
336 
337 #define	LINUX_ENTRY_B_READ_EXEC_ONLY	9
338 #define	LINUX_ENTRY_B_CONTENTS		10
339 #define	LINUX_ENTRY_B_SEG_NOT_PRESENT	15
340 #define	LINUX_ENTRY_B_BASE_ADDR		16
341 #define	LINUX_ENTRY_B_USEABLE		20
342 #define	LINUX_ENTRY_B_SEG32BIT		22
343 #define	LINUX_ENTRY_B_LIMIT		23
344 
345 #define	LINUX_LDT_entry_b(info)							\
346 	(((info)->base_addr & 0xff000000) |					\
347 	((info)->limit & 0xf0000) |						\
348 	((info)->contents << LINUX_ENTRY_B_CONTENTS) |				\
349 	(((info)->seg_not_present == 0) << LINUX_ENTRY_B_SEG_NOT_PRESENT) |	\
350 	(((info)->base_addr & 0x00ff0000) >> LINUX_ENTRY_B_BASE_ADDR) |		\
351 	(((info)->read_exec_only == 0) << LINUX_ENTRY_B_READ_EXEC_ONLY) |	\
352 	((info)->seg_32bit << LINUX_ENTRY_B_SEG32BIT) |				\
353 	((info)->useable << LINUX_ENTRY_B_USEABLE) |				\
354 	((info)->limit_in_pages << LINUX_ENTRY_B_LIMIT) | 0x7000)
355 
356 #define	LINUX_LDT_empty(info)		\
357 	((info)->base_addr == 0 &&	\
358 	(info)->limit == 0 &&		\
359 	(info)->contents == 0 &&	\
360 	(info)->seg_not_present == 1 &&	\
361 	(info)->read_exec_only == 1 &&	\
362 	(info)->seg_32bit == 0 &&	\
363 	(info)->limit_in_pages == 0 &&	\
364 	(info)->useable == 0)
365 
366 /*
367  * Macros for converting segments.
368  * They do the same as those in arch/i386/kernel/process.c in Linux.
369  */
370 #define	LINUX_GET_BASE(desc)				\
371 	((((desc)->a >> 16) & LINUX_LOWERWORD) |	\
372 	(((desc)->b << 16) & 0x00ff0000) |		\
373 	((desc)->b & 0xff000000))
374 
375 #define	LINUX_GET_LIMIT(desc)			\
376 	(((desc)->a & LINUX_LOWERWORD) |	\
377 	((desc)->b & 0xf0000))
378 
379 #define	LINUX_GET_32BIT(desc)		\
380 	(((desc)->b >> LINUX_ENTRY_B_SEG32BIT) & 1)
381 #define	LINUX_GET_CONTENTS(desc)	\
382 	(((desc)->b >> LINUX_ENTRY_B_CONTENTS) & 3)
383 #define	LINUX_GET_WRITABLE(desc)	\
384 	(((desc)->b >> LINUX_ENTRY_B_READ_EXEC_ONLY) & 1)
385 #define	LINUX_GET_LIMIT_PAGES(desc)	\
386 	(((desc)->b >> LINUX_ENTRY_B_LIMIT) & 1)
387 #define	LINUX_GET_PRESENT(desc)		\
388 	(((desc)->b >> LINUX_ENTRY_B_SEG_NOT_PRESENT) & 1)
389 #define	LINUX_GET_USEABLE(desc)		\
390 	(((desc)->b >> LINUX_ENTRY_B_USEABLE) & 1)
391 
392 #ifdef _KERNEL
393 struct iovec;
394 struct uio;
395 
396 struct l_iovec32 {
397 	uint32_t	iov_base;
398 	l_size_t	iov_len;
399 };
400 
401 int linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt,
402 			    struct iovec **iovp, int error);
403 int linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt,
404 			    struct uio **uiop);
405 int linux_copyout_rusage(struct rusage *ru, void *uaddr);
406 #endif /* _KERNEL */
407 
408 /* This corresponds to 'struct user_regs_struct32' in Linux. */
409 struct linux_pt_regset32 {
410 	l_uint ebx;
411 	l_uint ecx;
412 	l_uint edx;
413 	l_uint esi;
414 	l_uint edi;
415 	l_uint ebp;
416 	l_uint eax;
417 	l_uint ds;
418 	l_uint es;
419 	l_uint fs;
420 	l_uint gs;
421 	l_uint orig_eax;
422 	l_uint eip;
423 	l_uint cs;
424 	l_uint eflags;
425 	l_uint esp;
426 	l_uint ss;
427 };
428 
429 #ifdef _KERNEL
430 struct reg32;
431 
432 void	bsd_to_linux_regset32(const struct reg32 *b_reg,
433 	    struct linux_pt_regset32 *l_regset);
434 int	linux_ptrace_peekuser(struct thread *td, pid_t pid,
435 	    void *addr, void *data);
436 int	linux_ptrace_pokeuser(struct thread *td, pid_t pid,
437 	    void *addr, void *data);
438 
439 extern bool linux32_emulate_i386;
440 #endif /* _KERNEL */
441 
442 #endif /* !_AMD64_LINUX_H_ */
443