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