1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1991, 1993, 1994
5 *	The Regents of the University of California.  All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)types.h	8.6 (Berkeley) 2/19/95
37 * $FreeBSD$
38 */
39
40#ifndef _SYS_TYPES_H_
41#define	_SYS_TYPES_H_
42
43#include <sys/cdefs.h>
44
45/* Machine type dependent parameters. */
46#include <machine/endian.h>
47#include <sys/_types.h>
48
49#include <sys/_pthreadtypes.h>
50
51#if __BSD_VISIBLE
52typedef	unsigned char	u_char;
53typedef	unsigned short	u_short;
54typedef	unsigned int	u_int;
55typedef	unsigned long	u_long;
56#ifndef _KERNEL
57typedef	unsigned short	ushort;		/* Sys V compatibility */
58typedef	unsigned int	uint;		/* Sys V compatibility */
59#endif
60#endif
61
62/*
63 * XXX POSIX sized integrals that should appear only in <sys/stdint.h>.
64 */
65#include <sys/_stdint.h>
66
67typedef __uint8_t	u_int8_t;	/* unsigned integrals (deprecated) */
68typedef __uint16_t	u_int16_t;
69typedef __uint32_t	u_int32_t;
70typedef __uint64_t	u_int64_t;
71
72typedef	__uint64_t	u_quad_t;	/* quads (deprecated) */
73typedef	__int64_t	quad_t;
74typedef	quad_t *	qaddr_t;
75
76typedef	char *		caddr_t;	/* core address */
77typedef	const char *	c_caddr_t;	/* core address, pointer to const */
78
79#ifndef _BLKSIZE_T_DECLARED
80typedef	__blksize_t	blksize_t;
81#define	_BLKSIZE_T_DECLARED
82#endif
83
84typedef	__cpuwhich_t	cpuwhich_t;
85typedef	__cpulevel_t	cpulevel_t;
86typedef	__cpusetid_t	cpusetid_t;
87
88#ifndef _BLKCNT_T_DECLARED
89typedef	__blkcnt_t	blkcnt_t;
90#define	_BLKCNT_T_DECLARED
91#endif
92
93#ifndef _CLOCK_T_DECLARED
94typedef	__clock_t	clock_t;
95#define	_CLOCK_T_DECLARED
96#endif
97
98#ifndef _CLOCKID_T_DECLARED
99typedef	__clockid_t	clockid_t;
100#define	_CLOCKID_T_DECLARED
101#endif
102
103typedef	__critical_t	critical_t;	/* Critical section value */
104typedef	__daddr_t	daddr_t;	/* disk address */
105
106#ifndef _DEV_T_DECLARED
107typedef	__dev_t		dev_t;		/* device number or struct cdev */
108#define	_DEV_T_DECLARED
109#endif
110
111#ifndef _FFLAGS_T_DECLARED
112typedef	__fflags_t	fflags_t;	/* file flags */
113#define	_FFLAGS_T_DECLARED
114#endif
115
116typedef	__fixpt_t	fixpt_t;	/* fixed point number */
117
118#ifndef _FSBLKCNT_T_DECLARED		/* for statvfs() */
119typedef	__fsblkcnt_t	fsblkcnt_t;
120typedef	__fsfilcnt_t	fsfilcnt_t;
121#define	_FSBLKCNT_T_DECLARED
122#endif
123
124#ifndef _GID_T_DECLARED
125typedef	__gid_t		gid_t;		/* group id */
126#define	_GID_T_DECLARED
127#endif
128
129#ifndef _IN_ADDR_T_DECLARED
130typedef	__uint32_t	in_addr_t;	/* base type for internet address */
131#define	_IN_ADDR_T_DECLARED
132#endif
133
134#ifndef _IN_PORT_T_DECLARED
135typedef	__uint16_t	in_port_t;
136#define	_IN_PORT_T_DECLARED
137#endif
138
139#ifndef _ID_T_DECLARED
140typedef	__id_t		id_t;		/* can hold a uid_t or pid_t */
141#define	_ID_T_DECLARED
142#endif
143
144#ifndef _INO_T_DECLARED
145typedef	__ino_t		ino_t;		/* inode number */
146#define	_INO_T_DECLARED
147#endif
148
149#ifndef _KEY_T_DECLARED
150typedef	__key_t		key_t;		/* IPC key (for Sys V IPC) */
151#define	_KEY_T_DECLARED
152#endif
153
154#ifndef _LWPID_T_DECLARED
155typedef	__lwpid_t	lwpid_t;	/* Thread ID (a.k.a. LWP) */
156#define	_LWPID_T_DECLARED
157#endif
158
159#ifndef _MODE_T_DECLARED
160typedef	__mode_t	mode_t;		/* permissions */
161#define	_MODE_T_DECLARED
162#endif
163
164#ifndef _ACCMODE_T_DECLARED
165typedef	__accmode_t	accmode_t;	/* access permissions */
166#define	_ACCMODE_T_DECLARED
167#endif
168
169#ifndef _NLINK_T_DECLARED
170typedef	__nlink_t	nlink_t;	/* link count */
171#define	_NLINK_T_DECLARED
172#endif
173
174#ifndef _OFF_T_DECLARED
175typedef	__off_t		off_t;		/* file offset */
176#define	_OFF_T_DECLARED
177#endif
178
179#ifndef _OFF64_T_DECLARED
180typedef	__off64_t	off64_t;	/* file offset (alias) */
181#define	_OFF64_T_DECLARED
182#endif
183
184#ifndef _PID_T_DECLARED
185typedef	__pid_t		pid_t;		/* process id */
186#define	_PID_T_DECLARED
187#endif
188
189typedef	__register_t	register_t;
190
191#ifndef _RLIM_T_DECLARED
192typedef	__rlim_t	rlim_t;		/* resource limit */
193#define	_RLIM_T_DECLARED
194#endif
195
196typedef	__int64_t	sbintime_t;
197
198typedef	__segsz_t	segsz_t;	/* segment size (in pages) */
199
200#ifndef _SIZE_T_DECLARED
201typedef	__size_t	size_t;
202#define	_SIZE_T_DECLARED
203#endif
204
205#ifndef _SSIZE_T_DECLARED
206typedef	__ssize_t	ssize_t;
207#define	_SSIZE_T_DECLARED
208#endif
209
210#ifndef _SUSECONDS_T_DECLARED
211typedef	__suseconds_t	suseconds_t;	/* microseconds (signed) */
212#define	_SUSECONDS_T_DECLARED
213#endif
214
215#ifndef _TIME_T_DECLARED
216typedef	__time_t	time_t;
217#define	_TIME_T_DECLARED
218#endif
219
220#ifndef _TIMER_T_DECLARED
221typedef	__timer_t	timer_t;
222#define	_TIMER_T_DECLARED
223#endif
224
225#ifndef _MQD_T_DECLARED
226typedef	__mqd_t	mqd_t;
227#define	_MQD_T_DECLARED
228#endif
229
230typedef	__u_register_t	u_register_t;
231
232#ifndef _UID_T_DECLARED
233typedef	__uid_t		uid_t;		/* user id */
234#define	_UID_T_DECLARED
235#endif
236
237#ifndef _USECONDS_T_DECLARED
238typedef	__useconds_t	useconds_t;	/* microseconds (unsigned) */
239#define	_USECONDS_T_DECLARED
240#endif
241
242#ifndef _CAP_IOCTL_T_DECLARED
243#define	_CAP_IOCTL_T_DECLARED
244typedef	unsigned long	cap_ioctl_t;
245#endif
246
247#ifndef _CAP_RIGHTS_T_DECLARED
248#define	_CAP_RIGHTS_T_DECLARED
249struct cap_rights;
250
251typedef	struct cap_rights	cap_rights_t;
252#endif
253
254/*
255 * Types suitable for exporting physical addresses, virtual addresses
256 * (pointers), and memory object sizes from the kernel independent of native
257 * word size.  These should be used in place of vm_paddr_t, (u)intptr_t, and
258 * size_t in structs which contain such types that are shared with userspace.
259 */
260typedef	__uint64_t	kpaddr_t;
261typedef	__uint64_t	kvaddr_t;
262typedef	__uint64_t	ksize_t;
263typedef	__int64_t	kssize_t;
264
265typedef	__vm_offset_t	vm_offset_t;
266typedef	__uint64_t	vm_ooffset_t;
267typedef	__vm_paddr_t	vm_paddr_t;
268typedef	__uint64_t	vm_pindex_t;
269typedef	__vm_size_t	vm_size_t;
270
271typedef __rman_res_t    rman_res_t;
272
273#ifdef _KERNEL
274typedef	int		boolean_t;
275typedef	struct device	*device_t;
276typedef	__intfptr_t	intfptr_t;
277
278/*
279 * XXX this is fixed width for historical reasons.  It should have had type
280 * __int_fast32_t.  Fixed-width types should not be used unless binary
281 * compatibility is essential.  Least-width types should be used even less
282 * since they provide smaller benefits.
283 *
284 * XXX should be MD.
285 *
286 * XXX this is bogus in -current, but still used for spl*().
287 */
288typedef	__uint32_t	intrmask_t;	/* Interrupt mask (spl, xxx_imask...) */
289
290typedef	__uintfptr_t	uintfptr_t;
291typedef	__uint64_t	uoff_t;
292typedef	char		vm_memattr_t;	/* memory attribute codes */
293typedef	struct vm_page	*vm_page_t;
294
295#define offsetof(type, field) __offsetof(type, field)
296#endif /* _KERNEL */
297
298#if	defined(_KERNEL) || defined(_STANDALONE)
299#if !defined(__bool_true_false_are_defined) && !defined(__cplusplus)
300#define	__bool_true_false_are_defined	1
301#define	false	0
302#define	true	1
303#if __STDC_VERSION__ < 199901L && __GNUC__ < 3 && !defined(__INTEL_COMPILER)
304typedef	int	_Bool;
305#endif
306typedef	_Bool	bool;
307#endif /* !__bool_true_false_are_defined && !__cplusplus */
308#endif /* KERNEL || _STANDALONE */
309
310/*
311 * The following are all things that really shouldn't exist in this header,
312 * since its purpose is to provide typedefs, not miscellaneous doodads.
313 */
314
315#ifdef __POPCNT__
316#define	__bitcount64(x)	__builtin_popcountll((__uint64_t)(x))
317#define	__bitcount32(x)	__builtin_popcount((__uint32_t)(x))
318#define	__bitcount16(x)	__builtin_popcount((__uint16_t)(x))
319#define	__bitcountl(x)	__builtin_popcountl((unsigned long)(x))
320#define	__bitcount(x)	__builtin_popcount((unsigned int)(x))
321#else
322/*
323 * Population count algorithm using SWAR approach
324 * - "SIMD Within A Register".
325 */
326static __inline __uint16_t
327__bitcount16(__uint16_t _x)
328{
329
330	_x = (_x & 0x5555) + ((_x & 0xaaaa) >> 1);
331	_x = (_x & 0x3333) + ((_x & 0xcccc) >> 2);
332	_x = (_x + (_x >> 4)) & 0x0f0f;
333	_x = (_x + (_x >> 8)) & 0x00ff;
334	return (_x);
335}
336
337static __inline __uint32_t
338__bitcount32(__uint32_t _x)
339{
340
341	_x = (_x & 0x55555555) + ((_x & 0xaaaaaaaa) >> 1);
342	_x = (_x & 0x33333333) + ((_x & 0xcccccccc) >> 2);
343	_x = (_x + (_x >> 4)) & 0x0f0f0f0f;
344	_x = (_x + (_x >> 8));
345	_x = (_x + (_x >> 16)) & 0x000000ff;
346	return (_x);
347}
348
349#ifdef __LP64__
350static __inline __uint64_t
351__bitcount64(__uint64_t _x)
352{
353
354	_x = (_x & 0x5555555555555555) + ((_x & 0xaaaaaaaaaaaaaaaa) >> 1);
355	_x = (_x & 0x3333333333333333) + ((_x & 0xcccccccccccccccc) >> 2);
356	_x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0f;
357	_x = (_x + (_x >> 8));
358	_x = (_x + (_x >> 16));
359	_x = (_x + (_x >> 32)) & 0x000000ff;
360	return (_x);
361}
362
363#define	__bitcountl(x)	__bitcount64((unsigned long)(x))
364#else
365static __inline __uint64_t
366__bitcount64(__uint64_t _x)
367{
368
369	return (__bitcount32(_x >> 32) + __bitcount32(_x));
370}
371
372#define	__bitcountl(x)	__bitcount32((unsigned long)(x))
373#endif
374#define	__bitcount(x)	__bitcount32((unsigned int)(x))
375#endif
376
377#if __BSD_VISIBLE
378
379#include <sys/select.h>
380
381/*
382 * The major and minor numbers are encoded in dev_t as MMMmmmMm (where
383 * letters correspond to bytes).  The encoding of the lower 4 bytes is
384 * constrained by compatibility with 16-bit and 32-bit dev_t's.  The
385 * encoding of of the upper 4 bytes is the least unnatural one consistent
386 * with this and other constraints.  Also, the decoding of the m bytes by
387 * minor() is unnatural to maximize compatibility subject to not discarding
388 * bits.  The upper m byte is shifted into the position of the lower M byte
389 * instead of shifting 3 upper m bytes to close the gap.  Compatibility for
390 * minor() is achieved iff the upper m byte is 0.
391 */
392#define	major(d)	__major(d)
393static __inline int
394__major(dev_t _d)
395{
396	return (((_d >> 32) & 0xffffff00) | ((_d >> 8) & 0xff));
397}
398#define	minor(d)	__minor(d)
399static __inline int
400__minor(dev_t _d)
401{
402	return (((_d >> 24) & 0xff00) | (_d & 0xffff00ff));
403}
404#define	makedev(M, m)	__makedev((M), (m))
405static __inline dev_t
406__makedev(int _Major, int _Minor)
407{
408	return (((dev_t)(_Major & 0xffffff00) << 32) | ((_Major & 0xff) << 8) |
409	    ((dev_t)(_Minor & 0xff00) << 24) | (_Minor & 0xffff00ff));
410}
411
412/*
413 * These declarations belong elsewhere, but are repeated here and in
414 * <stdio.h> to give broken programs a better chance of working with
415 * 64-bit off_t's.
416 */
417#ifndef _KERNEL
418__BEGIN_DECLS
419#ifndef _FTRUNCATE_DECLARED
420#define	_FTRUNCATE_DECLARED
421int	 ftruncate(int, off_t);
422#endif
423#ifndef _LSEEK_DECLARED
424#define	_LSEEK_DECLARED
425off_t	 lseek(int, off_t, int);
426#endif
427#ifndef _MMAP_DECLARED
428#define	_MMAP_DECLARED
429void *	 mmap(void *, size_t, int, int, int, off_t);
430#endif
431#ifndef _TRUNCATE_DECLARED
432#define	_TRUNCATE_DECLARED
433int	 truncate(const char *, off_t);
434#endif
435__END_DECLS
436#endif /* !_KERNEL */
437
438#endif /* __BSD_VISIBLE */
439
440#endif /* !_SYS_TYPES_H_ */
441