xref: /freebsd/sys/contrib/openzfs/include/os/linux/spl/sys/sysmacros.h (revision fd141584cf89d7d24543ef5be83c6927a5d67ffa)
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	defclsyspri			(DEFAULT_PRIO)
96 /* Write issue taskq priority. */
97 #define	wtqclsyspri			(MAX_RT_PRIO + 1)
98 #define	maxclsyspri			(MAX_RT_PRIO)
99 
100 #ifndef NICE_TO_PRIO
101 #define	NICE_TO_PRIO(nice)		(MAX_RT_PRIO + (nice) + 20)
102 #endif
103 #ifndef PRIO_TO_NICE
104 #define	PRIO_TO_NICE(prio)		((prio) - MAX_RT_PRIO - 20)
105 #endif
106 
107 /*
108  * Missing macros
109  */
110 #ifndef PAGESIZE
111 #define	PAGESIZE			PAGE_SIZE
112 #endif
113 
114 #ifndef PAGESHIFT
115 #define	PAGESHIFT			PAGE_SHIFT
116 #endif
117 
118 /* Missing globals */
119 extern unsigned long spl_hostid;
120 
121 /* Missing misc functions */
122 extern uint32_t zone_get_hostid(void *zone);
123 extern void spl_setup(void);
124 extern void spl_cleanup(void);
125 
126 /*
127  * Only handles the first 4096 majors and first 256 minors. We don't have a
128  * libc for the kernel module so we define this inline.
129  */
130 static inline dev_t
makedev(unsigned int major,unsigned int minor)131 makedev(unsigned int major, unsigned int minor)
132 {
133 	return ((major & 0xFFF) << 8) | (minor & 0xFF);
134 }
135 
136 #define	highbit(x)		__fls(x)
137 #define	lowbit(x)		__ffs(x)
138 
139 #define	highbit64(x)		fls64(x)
140 #define	makedevice(maj, min)	makedev(maj, min)
141 
142 /* common macros */
143 #ifndef MIN
144 #define	MIN(a, b)		((a) < (b) ? (a) : (b))
145 #endif
146 #ifndef MAX
147 #define	MAX(a, b)		((a) < (b) ? (b) : (a))
148 #endif
149 #ifndef ABS
150 #define	ABS(a)			((a) < 0 ? -(a) : (a))
151 #endif
152 #ifndef DIV_ROUND_UP
153 #define	DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
154 #endif
155 #ifndef roundup
156 #define	roundup(x, y)		((((x) + ((y) - 1)) / (y)) * (y))
157 #endif
158 #ifndef howmany
159 #define	howmany(x, y)		(((x) + ((y) - 1)) / (y))
160 #endif
161 
162 /*
163  * Compatibility macros/typedefs needed for Solaris -> Linux port
164  */
165 // Deprecated. Use P2ALIGN_TYPED instead.
166 // #define	P2ALIGN(x, align)	((x) & -(align))
167 #define	P2CROSS(x, y, align)	(((x) ^ (y)) > (align) - 1)
168 #define	P2ROUNDUP(x, align)	((((x) - 1) | ((align) - 1)) + 1)
169 #define	P2PHASE(x, align)	((x) & ((align) - 1))
170 #define	P2NPHASE(x, align)	(-(x) & ((align) - 1))
171 #define	ISP2(x)			(((x) & ((x) - 1)) == 0)
172 #define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
173 #define	P2BOUNDARY(off, len, align) \
174 				(((off) ^ ((off) + (len) - 1)) > (align) - 1)
175 
176 /*
177  * Typed version of the P2* macros.  These macros should be used to ensure
178  * that the result is correctly calculated based on the data type of (x),
179  * which is passed in as the last argument, regardless of the data
180  * type of the alignment.  For example, if (x) is of type uint64_t,
181  * and we want to round it up to a page boundary using "PAGESIZE" as
182  * the alignment, we can do either
183  *
184  * P2ROUNDUP(x, (uint64_t)PAGESIZE)
185  * or
186  * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
187  */
188 #define	P2ALIGN_TYPED(x, align, type)   \
189 	((type)(x) & -(type)(align))
190 #define	P2PHASE_TYPED(x, align, type)   \
191 	((type)(x) & ((type)(align) - 1))
192 #define	P2NPHASE_TYPED(x, align, type)  \
193 	(-(type)(x) & ((type)(align) - 1))
194 #define	P2ROUNDUP_TYPED(x, align, type) \
195 	((((type)(x) - 1) | ((type)(align) - 1)) + 1)
196 #define	P2END_TYPED(x, align, type)     \
197 	(-(~(type)(x) & -(type)(align)))
198 #define	P2PHASEUP_TYPED(x, align, phase, type)  \
199 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
200 #define	P2CROSS_TYPED(x, y, align, type)	\
201 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
202 #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
203 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
204 
205 #define	SET_ERROR(err) \
206 	(__set_error(__FILE__, __func__, __LINE__, err), err)
207 
208 #include <linux/sort.h>
209 #define	qsort(base, num, size, cmp)		\
210 	sort(base, num, size, cmp, NULL)
211 
212 #if !defined(_KMEMUSER) && !defined(offsetof)
213 
214 /* avoid any possibility of clashing with <stddef.h> version */
215 
216 #define	offsetof(s, m)  ((size_t)(&(((s *)0)->m)))
217 #endif
218 
219 #endif  /* _SPL_SYSMACROS_H */
220