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