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