xref: /freebsd-head/sys/contrib/openzfs/include/os/linux/spl/sys/sysmacros.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
4  *  Copyright (C) 2007 The Regents of the University of California.
5  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
7  *  UCRL-CODE-235197
8  *
9  *  This file is part of the SPL, Solaris Porting Layer.
10  *
11  *  The SPL is free software; you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License as published by the
13  *  Free Software Foundation; either version 2 of the License, or (at your
14  *  option) any later version.
15  *
16  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
17  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19  *  for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef _SPL_SYSMACROS_H
26 #define	_SPL_SYSMACROS_H
27 
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/sched/rt.h>
31 #include <linux/cpumask.h>
32 #include <sys/debug.h>
33 #include <sys/zone.h>
34 #include <sys/signal.h>
35 #include <asm/page.h>
36 
37 
38 #ifndef _KERNEL
39 #define	_KERNEL				__KERNEL__
40 #endif
41 
42 #define	FALSE				0
43 #define	TRUE				1
44 
45 #define	INT8_MAX			(127)
46 #define	INT8_MIN			(-128)
47 #define	UINT8_MAX			(255)
48 #define	UINT8_MIN			(0)
49 
50 #define	INT16_MAX			(32767)
51 #define	INT16_MIN			(-32768)
52 #define	UINT16_MAX			(65535)
53 #define	UINT16_MIN			(0)
54 
55 #define	INT32_MAX			INT_MAX
56 #define	INT32_MIN			INT_MIN
57 #define	UINT32_MAX			UINT_MAX
58 #define	UINT32_MIN			UINT_MIN
59 
60 #define	INT64_MAX			LLONG_MAX
61 #define	INT64_MIN			LLONG_MIN
62 #define	UINT64_MAX			ULLONG_MAX
63 #define	UINT64_MIN			ULLONG_MIN
64 
65 #define	NBBY				8
66 
67 #define	MAXMSGLEN			256
68 #define	MAXNAMELEN			256
69 #define	MAXPATHLEN			4096
70 #define	MAXOFFSET_T			LLONG_MAX
71 #define	MAXBSIZE			8192
72 #define	DEV_BSIZE			512
73 #define	DEV_BSHIFT			9 /* log2(DEV_BSIZE) */
74 
75 #define	proc_pageout			NULL
76 #define	curproc				current
77 #define	max_ncpus			num_possible_cpus()
78 #define	boot_ncpus			num_online_cpus()
79 #define	CPU_SEQID			smp_processor_id()
80 #define	CPU_SEQID_UNSTABLE		raw_smp_processor_id()
81 #define	is_system_labeled()		0
82 
83 #ifndef RLIM64_INFINITY
84 #define	RLIM64_INFINITY			(~0ULL)
85 #endif
86 
87 /*
88  * 0..MAX_PRIO-1:		Process priority
89  * 0..MAX_RT_PRIO-1:		RT priority tasks
90  * MAX_RT_PRIO..MAX_PRIO-1:	SCHED_NORMAL tasks
91  *
92  * Treat shim tasks as SCHED_NORMAL tasks
93  */
94 #define	minclsyspri			(MAX_PRIO-1)
95 #define	maxclsyspri			(MAX_RT_PRIO)
96 #define	defclsyspri			(DEFAULT_PRIO)
97 
98 #ifndef NICE_TO_PRIO
99 #define	NICE_TO_PRIO(nice)		(MAX_RT_PRIO + (nice) + 20)
100 #endif
101 #ifndef PRIO_TO_NICE
102 #define	PRIO_TO_NICE(prio)		((prio) - MAX_RT_PRIO - 20)
103 #endif
104 
105 /*
106  * Missing macros
107  */
108 #ifndef PAGESIZE
109 #define	PAGESIZE			PAGE_SIZE
110 #endif
111 
112 #ifndef PAGESHIFT
113 #define	PAGESHIFT			PAGE_SHIFT
114 #endif
115 
116 /* Missing globals */
117 extern unsigned long spl_hostid;
118 
119 /* Missing misc functions */
120 extern uint32_t zone_get_hostid(void *zone);
121 extern void spl_setup(void);
122 extern void spl_cleanup(void);
123 
124 /*
125  * Only handles the first 4096 majors and first 256 minors. We don't have a
126  * libc for the kernel module so we define this inline.
127  */
128 static inline dev_t
makedev(unsigned int major,unsigned int minor)129 makedev(unsigned int major, unsigned int minor)
130 {
131 	return ((major & 0xFFF) << 8) | (minor & 0xFF);
132 }
133 
134 #define	highbit(x)		__fls(x)
135 #define	lowbit(x)		__ffs(x)
136 
137 #define	highbit64(x)		fls64(x)
138 #define	makedevice(maj, min)	makedev(maj, min)
139 
140 /* common macros */
141 #ifndef MIN
142 #define	MIN(a, b)		((a) < (b) ? (a) : (b))
143 #endif
144 #ifndef MAX
145 #define	MAX(a, b)		((a) < (b) ? (b) : (a))
146 #endif
147 #ifndef ABS
148 #define	ABS(a)			((a) < 0 ? -(a) : (a))
149 #endif
150 #ifndef DIV_ROUND_UP
151 #define	DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
152 #endif
153 #ifndef roundup
154 #define	roundup(x, y)		((((x) + ((y) - 1)) / (y)) * (y))
155 #endif
156 #ifndef howmany
157 #define	howmany(x, y)		(((x) + ((y) - 1)) / (y))
158 #endif
159 
160 /*
161  * Compatibility macros/typedefs needed for Solaris -> Linux port
162  */
163 // Deprecated. Use P2ALIGN_TYPED instead.
164 // #define	P2ALIGN(x, align)	((x) & -(align))
165 #define	P2CROSS(x, y, align)	(((x) ^ (y)) > (align) - 1)
166 #define	P2ROUNDUP(x, align)	((((x) - 1) | ((align) - 1)) + 1)
167 #define	P2PHASE(x, align)	((x) & ((align) - 1))
168 #define	P2NPHASE(x, align)	(-(x) & ((align) - 1))
169 #define	ISP2(x)			(((x) & ((x) - 1)) == 0)
170 #define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
171 #define	P2BOUNDARY(off, len, align) \
172 				(((off) ^ ((off) + (len) - 1)) > (align) - 1)
173 
174 /*
175  * Typed version of the P2* macros.  These macros should be used to ensure
176  * that the result is correctly calculated based on the data type of (x),
177  * which is passed in as the last argument, regardless of the data
178  * type of the alignment.  For example, if (x) is of type uint64_t,
179  * and we want to round it up to a page boundary using "PAGESIZE" as
180  * the alignment, we can do either
181  *
182  * P2ROUNDUP(x, (uint64_t)PAGESIZE)
183  * or
184  * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
185  */
186 #define	P2ALIGN_TYPED(x, align, type)   \
187 	((type)(x) & -(type)(align))
188 #define	P2PHASE_TYPED(x, align, type)   \
189 	((type)(x) & ((type)(align) - 1))
190 #define	P2NPHASE_TYPED(x, align, type)  \
191 	(-(type)(x) & ((type)(align) - 1))
192 #define	P2ROUNDUP_TYPED(x, align, type) \
193 	((((type)(x) - 1) | ((type)(align) - 1)) + 1)
194 #define	P2END_TYPED(x, align, type)     \
195 	(-(~(type)(x) & -(type)(align)))
196 #define	P2PHASEUP_TYPED(x, align, phase, type)  \
197 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
198 #define	P2CROSS_TYPED(x, y, align, type)	\
199 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
200 #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
201 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
202 
203 #define	SET_ERROR(err) \
204 	(__set_error(__FILE__, __func__, __LINE__, err), err)
205 
206 #include <linux/sort.h>
207 #define	qsort(base, num, size, cmp)		\
208 	sort(base, num, size, cmp, NULL)
209 
210 #if !defined(_KMEMUSER) && !defined(offsetof)
211 
212 /* avoid any possibility of clashing with <stddef.h> version */
213 
214 #define	offsetof(s, m)  ((size_t)(&(((s *)0)->m)))
215 #endif
216 
217 #endif  /* _SPL_SYSMACROS_H */
218