xref: /freebsd-13-stable/sys/amd64/linux32/linux.h (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
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_ifmap {
303 	l_ulong		mem_start;
304 	l_ulong		mem_end;
305 	l_ushort	base_addr;
306 	u_char		irq;
307 	u_char		dma;
308 	u_char		port;
309 	/* 3 bytes spare */
310 };
311 
312 struct l_ifreq {
313 	union {
314 		char	ifrn_name[LINUX_IFNAMSIZ];
315 	} ifr_ifrn;
316 
317 	union {
318 		struct l_sockaddr	ifru_addr;
319 		struct l_sockaddr	ifru_dstaddr;
320 		struct l_sockaddr	ifru_broadaddr;
321 		struct l_sockaddr	ifru_netmask;
322 		struct l_sockaddr	ifru_hwaddr;
323 		l_short		ifru_flags[1];
324 		l_int		ifru_ivalue;
325 		l_int		ifru_mtu;
326 		struct l_ifmap	ifru_map;
327 		char		ifru_slave[LINUX_IFNAMSIZ];
328 		l_uintptr_t	ifru_data;
329 	} ifr_ifru;
330 };
331 
332 #define	ifr_name	ifr_ifrn.ifrn_name	/* Interface name */
333 #define	ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address */
334 #define	ifr_ifindex	ifr_ifru.ifru_ivalue	/* Interface index */
335 
336 struct l_ifconf {
337 	int	ifc_len;
338 	union {
339 		l_uintptr_t	ifcu_buf;
340 		l_uintptr_t	ifcu_req;
341 	} ifc_ifcu;
342 };
343 
344 #define	ifc_buf		ifc_ifcu.ifcu_buf
345 #define	ifc_req		ifc_ifcu.ifcu_req
346 
347 struct l_user_desc {
348 	l_uint		entry_number;
349 	l_uint		base_addr;
350 	l_uint		limit;
351 	l_uint		seg_32bit:1;
352 	l_uint		contents:2;
353 	l_uint		read_exec_only:1;
354 	l_uint		limit_in_pages:1;
355 	l_uint		seg_not_present:1;
356 	l_uint		useable:1;
357 };
358 
359 #define	LINUX_LOWERWORD	0x0000ffff
360 
361 /*
362  * Macros which does the same thing as those in Linux include/asm-um/ldt-i386.h.
363  * These convert Linux user space descriptor to machine one.
364  */
365 #define	LINUX_LDT_entry_a(info)					\
366 	((((info)->base_addr & LINUX_LOWERWORD) << 16) |	\
367 	((info)->limit & LINUX_LOWERWORD))
368 
369 #define	LINUX_ENTRY_B_READ_EXEC_ONLY	9
370 #define	LINUX_ENTRY_B_CONTENTS		10
371 #define	LINUX_ENTRY_B_SEG_NOT_PRESENT	15
372 #define	LINUX_ENTRY_B_BASE_ADDR		16
373 #define	LINUX_ENTRY_B_USEABLE		20
374 #define	LINUX_ENTRY_B_SEG32BIT		22
375 #define	LINUX_ENTRY_B_LIMIT		23
376 
377 #define	LINUX_LDT_entry_b(info)							\
378 	(((info)->base_addr & 0xff000000) |					\
379 	((info)->limit & 0xf0000) |						\
380 	((info)->contents << LINUX_ENTRY_B_CONTENTS) |				\
381 	(((info)->seg_not_present == 0) << LINUX_ENTRY_B_SEG_NOT_PRESENT) |	\
382 	(((info)->base_addr & 0x00ff0000) >> LINUX_ENTRY_B_BASE_ADDR) |		\
383 	(((info)->read_exec_only == 0) << LINUX_ENTRY_B_READ_EXEC_ONLY) |	\
384 	((info)->seg_32bit << LINUX_ENTRY_B_SEG32BIT) |				\
385 	((info)->useable << LINUX_ENTRY_B_USEABLE) |				\
386 	((info)->limit_in_pages << LINUX_ENTRY_B_LIMIT) | 0x7000)
387 
388 #define	LINUX_LDT_empty(info)		\
389 	((info)->base_addr == 0 &&	\
390 	(info)->limit == 0 &&		\
391 	(info)->contents == 0 &&	\
392 	(info)->seg_not_present == 1 &&	\
393 	(info)->read_exec_only == 1 &&	\
394 	(info)->seg_32bit == 0 &&	\
395 	(info)->limit_in_pages == 0 &&	\
396 	(info)->useable == 0)
397 
398 /*
399  * Macros for converting segments.
400  * They do the same as those in arch/i386/kernel/process.c in Linux.
401  */
402 #define	LINUX_GET_BASE(desc)				\
403 	((((desc)->a >> 16) & LINUX_LOWERWORD) |	\
404 	(((desc)->b << 16) & 0x00ff0000) |		\
405 	((desc)->b & 0xff000000))
406 
407 #define	LINUX_GET_LIMIT(desc)			\
408 	(((desc)->a & LINUX_LOWERWORD) |	\
409 	((desc)->b & 0xf0000))
410 
411 #define	LINUX_GET_32BIT(desc)		\
412 	(((desc)->b >> LINUX_ENTRY_B_SEG32BIT) & 1)
413 #define	LINUX_GET_CONTENTS(desc)	\
414 	(((desc)->b >> LINUX_ENTRY_B_CONTENTS) & 3)
415 #define	LINUX_GET_WRITABLE(desc)	\
416 	(((desc)->b >> LINUX_ENTRY_B_READ_EXEC_ONLY) & 1)
417 #define	LINUX_GET_LIMIT_PAGES(desc)	\
418 	(((desc)->b >> LINUX_ENTRY_B_LIMIT) & 1)
419 #define	LINUX_GET_PRESENT(desc)		\
420 	(((desc)->b >> LINUX_ENTRY_B_SEG_NOT_PRESENT) & 1)
421 #define	LINUX_GET_USEABLE(desc)		\
422 	(((desc)->b >> LINUX_ENTRY_B_USEABLE) & 1)
423 
424 #ifdef _KERNEL
425 struct iovec;
426 struct uio;
427 
428 struct l_iovec32 {
429 	uint32_t	iov_base;
430 	l_size_t	iov_len;
431 };
432 
433 int linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt,
434 			    struct iovec **iovp, int error);
435 int linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt,
436 			    struct uio **uiop);
437 int linux_copyout_rusage(struct rusage *ru, void *uaddr);
438 #endif /* _KERNEL */
439 
440 /* This corresponds to 'struct user_regs_struct32' in Linux. */
441 struct linux_pt_regset32 {
442 	l_uint ebx;
443 	l_uint ecx;
444 	l_uint edx;
445 	l_uint esi;
446 	l_uint edi;
447 	l_uint ebp;
448 	l_uint eax;
449 	l_uint ds;
450 	l_uint es;
451 	l_uint fs;
452 	l_uint gs;
453 	l_uint orig_eax;
454 	l_uint eip;
455 	l_uint cs;
456 	l_uint eflags;
457 	l_uint esp;
458 	l_uint ss;
459 };
460 
461 #ifdef _KERNEL
462 struct reg32;
463 
464 void	bsd_to_linux_regset32(const struct reg32 *b_reg,
465 	    struct linux_pt_regset32 *l_regset);
466 int	linux_ptrace_peekuser(struct thread *td, pid_t pid,
467 	    void *addr, void *data);
468 int	linux_ptrace_pokeuser(struct thread *td, pid_t pid,
469 	    void *addr, void *data);
470 
471 extern bool linux32_emulate_i386;
472 #endif /* _KERNEL */
473 
474 #endif /* !_AMD64_LINUX_H_ */
475