xref: /freebsd/sys/i386/linux/linux.h (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994-1996 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _I386_LINUX_H_
30 #define	_I386_LINUX_H_
31 
32 #include <sys/abi_compat.h>
33 
34 #include <compat/linux/linux.h>
35 #include <i386/linux/linux_syscall.h>
36 
37 #define LINUX_LEGACY_SYSCALLS
38 
39 #define	LINUX_DTRACE	linuxulator
40 
41 /*
42  * Provide a separate set of types for the Linux types.
43  */
44 typedef int		l_int;
45 typedef int32_t		l_long;
46 typedef int64_t		l_longlong;
47 typedef short		l_short;
48 typedef unsigned int	l_uint;
49 typedef uint32_t	l_ulong;
50 typedef uint64_t	l_ulonglong;
51 typedef unsigned short	l_ushort;
52 
53 typedef char		*l_caddr_t;
54 typedef l_ulong		l_uintptr_t;
55 typedef l_long		l_clock_t;
56 typedef l_int		l_daddr_t;
57 typedef l_uint		l_gid_t;
58 typedef l_ushort	l_gid16_t;
59 typedef l_ulong		l_ino_t;
60 typedef l_int		l_key_t;
61 typedef l_longlong	l_loff_t;
62 typedef l_ushort	l_mode_t;
63 typedef l_long		l_off_t;
64 typedef l_int		l_pid_t;
65 typedef l_uint		l_size_t;
66 typedef l_long		l_suseconds_t;
67 typedef l_long		l_time_t;
68 typedef l_longlong	l_time64_t;
69 typedef l_uint		l_uid_t;
70 typedef l_ushort	l_uid16_t;
71 typedef l_int		l_timer_t;
72 typedef l_int		l_mqd_t;
73 typedef	l_ulong		l_fd_mask;
74 
75 #include <compat/linux/linux_siginfo.h>
76 
77 typedef struct {
78 	l_int		val[2];
79 } l_fsid_t;
80 
81 typedef struct {
82 	l_time_t	tv_sec;
83 	l_suseconds_t	tv_usec;
84 } l_timeval;
85 
86 typedef struct {
87 	l_time64_t	tv_sec;
88 	l_time64_t	tv_usec;
89 } l_sock_timeval;
90 
91 #define	l_fd_set	fd_set
92 
93 /*
94  * Miscellaneous
95  */
96 #define LINUX_AT_COUNT		22	/* Count of used aux entry types.
97 					 * Keep this synchronized with
98 					 * linux_copyout_auxargs() code.
99 					 */
100 struct l___sysctl_args
101 {
102 	l_int		*name;
103 	l_int		nlen;
104 	void		*oldval;
105 	l_size_t	*oldlenp;
106 	void		*newval;
107 	l_size_t	newlen;
108 	l_ulong		__spare[4];
109 };
110 
111 /* Resource limits */
112 #define	LINUX_RLIMIT_CPU	0
113 #define	LINUX_RLIMIT_FSIZE	1
114 #define	LINUX_RLIMIT_DATA	2
115 #define	LINUX_RLIMIT_STACK	3
116 #define	LINUX_RLIMIT_CORE	4
117 #define	LINUX_RLIMIT_RSS	5
118 #define	LINUX_RLIMIT_NPROC	6
119 #define	LINUX_RLIMIT_NOFILE	7
120 #define	LINUX_RLIMIT_MEMLOCK	8
121 #define	LINUX_RLIMIT_AS		9	/* Address space limit */
122 
123 #define	LINUX_RLIM_NLIMITS	10
124 
125 struct l_rlimit {
126 	l_ulong rlim_cur;
127 	l_ulong rlim_max;
128 };
129 
130 struct l_mmap_argv {
131 	l_uintptr_t	addr;
132 	l_size_t	len;
133 	l_int		prot;
134 	l_int		flags;
135 	l_int		fd;
136 	l_off_t		pgoff;
137 };
138 
139 /*
140  * stat family of syscalls
141  */
142 struct l_timespec {
143 	l_time_t	tv_sec;
144 	l_long		tv_nsec;
145 };
146 
147 /* __kernel_timespec */
148 struct l_timespec64 {
149 	l_time64_t	tv_sec;
150 	l_longlong	tv_nsec;
151 };
152 
153 struct l_newstat {
154 	l_ulong		st_dev;
155 	l_ulong		st_ino;
156 	l_ushort	st_mode;
157 	l_ushort	st_nlink;
158 	l_ushort	st_uid;
159 	l_ushort	st_gid;
160 	l_ulong		st_rdev;
161 	l_ulong		st_size;
162 	l_ulong		st_blksize;
163 	l_ulong		st_blocks;
164 	struct l_timespec	st_atim;
165 	struct l_timespec	st_mtim;
166 	struct l_timespec	st_ctim;
167 	l_ulong		__unused4;
168 	l_ulong		__unused5;
169 };
170 
171 /* __old_kernel_stat now */
172 struct l_old_stat {
173 	l_ushort	st_dev;
174 	l_ulong		st_ino;
175 	l_ushort	st_mode;
176 	l_ushort	st_nlink;
177 	l_ushort	st_uid;
178 	l_ushort	st_gid;
179 	l_ushort	st_rdev;
180 	l_long		st_size;
181 	struct l_timespec	st_atim;
182 	struct l_timespec	st_mtim;
183 	struct l_timespec	st_ctim;
184 	l_long		st_blksize;
185 	l_long		st_blocks;
186 	l_ulong		st_flags;
187 	l_ulong		st_gen;
188 };
189 
190 struct l_stat64 {
191 	l_ulonglong	st_dev;
192 	u_char		__pad0[4];
193 	l_ulong		__st_ino;
194 	l_uint		st_mode;
195 	l_uint		st_nlink;
196 	l_ulong		st_uid;
197 	l_ulong		st_gid;
198 	l_ulonglong	st_rdev;
199 	u_char		__pad3[4];
200 	l_longlong	st_size;
201 	l_ulong		st_blksize;
202 	l_ulonglong	st_blocks;
203 	struct l_timespec	st_atim;
204 	struct l_timespec	st_mtim;
205 	struct l_timespec	st_ctim;
206 	l_ulonglong	st_ino;
207 };
208 
209 struct l_statfs64 {
210 	l_int		f_type;
211 	l_int		f_bsize;
212 	uint64_t	f_blocks;
213 	uint64_t	f_bfree;
214 	uint64_t	f_bavail;
215 	uint64_t	f_files;
216 	uint64_t	f_ffree;
217 	l_fsid_t	f_fsid;
218 	l_int		f_namelen;
219 	l_int		f_frsize;
220 	l_int		f_flags;
221 	l_int		f_spare[4];
222 };
223 
224 /* sigaction flags */
225 #define	LINUX_SA_NOCLDSTOP	0x00000001
226 #define	LINUX_SA_NOCLDWAIT	0x00000002
227 #define	LINUX_SA_SIGINFO	0x00000004
228 #define	LINUX_SA_RESTORER	0x04000000
229 #define	LINUX_SA_ONSTACK	0x08000000
230 #define	LINUX_SA_RESTART	0x10000000
231 #define	LINUX_SA_INTERRUPT	0x20000000
232 #define	LINUX_SA_NOMASK		0x40000000
233 #define	LINUX_SA_ONESHOT	0x80000000
234 
235 /* sigaltstack */
236 #define	LINUX_MINSIGSTKSZ	2048
237 
238 typedef void	(*l_handler_t)(l_int);
239 typedef l_ulong	l_osigset_t;
240 
241 typedef struct {
242 	l_handler_t	lsa_handler;
243 	l_osigset_t	lsa_mask;
244 	l_ulong		lsa_flags;
245 	void	(*lsa_restorer)(void);
246 } l_osigaction_t;
247 
248 typedef struct {
249 	l_handler_t	lsa_handler;
250 	l_ulong		lsa_flags;
251 	void	(*lsa_restorer)(void);
252 	l_sigset_t	lsa_mask;
253 } l_sigaction_t;
254 
255 typedef struct {
256 	l_uintptr_t	ss_sp;
257 	l_int		ss_flags;
258 	l_size_t	ss_size;
259 } l_stack_t;
260 
261 extern struct sysentvec linux_sysvec;
262 
263 /*
264  * arch specific open/fcntl flags
265  */
266 #define	LINUX_F_GETLK64		12
267 #define	LINUX_F_SETLK64		13
268 #define	LINUX_F_SETLKW64	14
269 
270 union l_semun {
271 	l_int		val;
272 	l_uintptr_t	buf;
273 	l_ushort	*array;
274 	l_uintptr_t	__buf;
275 	l_uintptr_t	__pad;
276 };
277 
278 struct l_user_desc {
279 	l_uint		entry_number;
280 	l_uint		base_addr;
281 	l_uint		limit;
282 	l_uint		seg_32bit:1;
283 	l_uint		contents:2;
284 	l_uint		read_exec_only:1;
285 	l_uint		limit_in_pages:1;
286 	l_uint		seg_not_present:1;
287 	l_uint		useable:1;
288 };
289 
290 struct l_desc_struct {
291 	unsigned long	a, b;
292 };
293 
294 #define	LINUX_LOWERWORD	0x0000ffff
295 
296 /*
297  * Macros which does the same thing as those in Linux include/asm-um/ldt-i386.h.
298  * These convert Linux user space descriptor to machine one.
299  */
300 #define	LINUX_LDT_entry_a(info)					\
301 	((((info)->base_addr & LINUX_LOWERWORD) << 16) |	\
302 	((info)->limit & LINUX_LOWERWORD))
303 
304 #define	LINUX_ENTRY_B_READ_EXEC_ONLY	9
305 #define	LINUX_ENTRY_B_CONTENTS		10
306 #define	LINUX_ENTRY_B_SEG_NOT_PRESENT	15
307 #define	LINUX_ENTRY_B_BASE_ADDR		16
308 #define	LINUX_ENTRY_B_USEABLE		20
309 #define	LINUX_ENTRY_B_SEG32BIT		22
310 #define	LINUX_ENTRY_B_LIMIT		23
311 
312 #define	LINUX_LDT_entry_b(info)							\
313 	(((info)->base_addr & 0xff000000) |					\
314 	((info)->limit & 0xf0000) |						\
315 	((info)->contents << LINUX_ENTRY_B_CONTENTS) |				\
316 	(((info)->seg_not_present == 0) << LINUX_ENTRY_B_SEG_NOT_PRESENT) |	\
317 	(((info)->base_addr & 0x00ff0000) >> LINUX_ENTRY_B_BASE_ADDR) |		\
318 	(((info)->read_exec_only == 0) << LINUX_ENTRY_B_READ_EXEC_ONLY) |	\
319 	((info)->seg_32bit << LINUX_ENTRY_B_SEG32BIT) |				\
320 	((info)->useable << LINUX_ENTRY_B_USEABLE) |				\
321 	((info)->limit_in_pages << LINUX_ENTRY_B_LIMIT) | 0x7000)
322 
323 #define	LINUX_LDT_empty(info)		\
324 	((info)->base_addr == 0 &&	\
325 	(info)->limit == 0 &&		\
326 	(info)->contents == 0 &&	\
327 	(info)->seg_not_present == 1 &&	\
328 	(info)->read_exec_only == 1 &&	\
329 	(info)->seg_32bit == 0 &&	\
330 	(info)->limit_in_pages == 0 &&	\
331 	(info)->useable == 0)
332 
333 /*
334  * Macros for converting segments.
335  * They do the same as those in arch/i386/kernel/process.c in Linux.
336  */
337 #define	LINUX_GET_BASE(desc)				\
338 	((((desc)->a >> 16) & LINUX_LOWERWORD) |	\
339 	(((desc)->b << 16) & 0x00ff0000) |		\
340 	((desc)->b & 0xff000000))
341 
342 #define	LINUX_GET_LIMIT(desc)			\
343 	(((desc)->a & LINUX_LOWERWORD) |	\
344 	((desc)->b & 0xf0000))
345 
346 #define	LINUX_GET_32BIT(desc)		\
347 	(((desc)->b >> LINUX_ENTRY_B_SEG32BIT) & 1)
348 #define	LINUX_GET_CONTENTS(desc)	\
349 	(((desc)->b >> LINUX_ENTRY_B_CONTENTS) & 3)
350 #define	LINUX_GET_WRITABLE(desc)	\
351 	(((desc)->b >> LINUX_ENTRY_B_READ_EXEC_ONLY) & 1)
352 #define	LINUX_GET_LIMIT_PAGES(desc)	\
353 	(((desc)->b >> LINUX_ENTRY_B_LIMIT) & 1)
354 #define	LINUX_GET_PRESENT(desc)		\
355 	(((desc)->b >> LINUX_ENTRY_B_SEG_NOT_PRESENT) & 1)
356 #define	LINUX_GET_USEABLE(desc)		\
357 	(((desc)->b >> LINUX_ENTRY_B_USEABLE) & 1)
358 
359 #define	linux_copyout_rusage(r, u)	copyout(r, u, sizeof(*r))
360 
361 /* This corresponds to 'struct user_regs_struct' in Linux. */
362 struct linux_pt_regset {
363 	l_uint ebx;
364 	l_uint ecx;
365 	l_uint edx;
366 	l_uint esi;
367 	l_uint edi;
368 	l_uint ebp;
369 	l_uint eax;
370 	l_uint ds;
371 	l_uint es;
372 	l_uint fs;
373 	l_uint gs;
374 	l_uint orig_eax;
375 	l_uint eip;
376 	l_uint cs;
377 	l_uint eflags;
378 	l_uint esp;
379 	l_uint ss;
380 };
381 
382 #ifdef _KERNEL
383 struct reg;
384 
385 void	bsd_to_linux_regset(const struct reg *b_reg,
386 	    struct linux_pt_regset *l_regset);
387 #endif /* _KERNEL */
388 
389 #endif /* !_I386_LINUX_H_ */
390