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 #define FALSE 0
38 #define TRUE 1
39
40 #define INT8_MAX (127)
41 #define INT8_MIN (-128)
42 #define UINT8_MAX (255)
43 #define UINT8_MIN (0)
44
45 #define INT16_MAX (32767)
46 #define INT16_MIN (-32768)
47 #define UINT16_MAX (65535)
48 #define UINT16_MIN (0)
49
50 #define INT32_MAX INT_MAX
51 #define INT32_MIN INT_MIN
52 #define UINT32_MAX UINT_MAX
53 #define UINT32_MIN UINT_MIN
54
55 #define INT64_MAX LLONG_MAX
56 #define INT64_MIN LLONG_MIN
57 #define UINT64_MAX ULLONG_MAX
58 #define UINT64_MIN ULLONG_MIN
59
60 #define NBBY 8
61
62 #define MAXMSGLEN 256
63 #define MAXNAMELEN 256
64 #define MAXPATHLEN 4096
65 #define MAXOFFSET_T LLONG_MAX
66 #define MAXBSIZE 8192
67 #define DEV_BSIZE 512
68 #define DEV_BSHIFT 9 /* log2(DEV_BSIZE) */
69
70 #define proc_pageout NULL
71 #define curproc current
72 #define max_ncpus num_possible_cpus()
73 #define boot_ncpus num_online_cpus()
74 #define CPU_SEQID smp_processor_id()
75 #define CPU_SEQID_UNSTABLE raw_smp_processor_id()
76 #define is_system_labeled() 0
77
78 #ifndef RLIM64_INFINITY
79 #define RLIM64_INFINITY (~0ULL)
80 #endif
81
82 /*
83 * 0..MAX_PRIO-1: Process priority
84 * 0..MAX_RT_PRIO-1: RT priority tasks
85 * MAX_RT_PRIO..MAX_PRIO-1: SCHED_NORMAL tasks
86 *
87 * Treat shim tasks as SCHED_NORMAL tasks
88 */
89 #define minclsyspri (MAX_PRIO-1)
90 #define defclsyspri (DEFAULT_PRIO)
91 /* Write issue taskq priority. */
92 #define wtqclsyspri (MAX_RT_PRIO + 1)
93 #define maxclsyspri (MAX_RT_PRIO)
94
95 #ifndef NICE_TO_PRIO
96 #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
97 #endif
98 #ifndef PRIO_TO_NICE
99 #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
100 #endif
101
102 /*
103 * Missing macros
104 */
105 #ifndef PAGESIZE
106 #define PAGESIZE PAGE_SIZE
107 #endif
108
109 #ifndef PAGESHIFT
110 #define PAGESHIFT PAGE_SHIFT
111 #endif
112
113 /* Missing globals */
114 extern unsigned long spl_hostid;
115
116 /* Missing misc functions */
117 extern uint32_t zone_get_hostid(void *zone);
118 extern void spl_setup(void);
119 extern void spl_cleanup(void);
120
121 /*
122 * Only handles the first 4096 majors and first 256 minors. We don't have a
123 * libc for the kernel module so we define this inline.
124 */
125 static inline dev_t
makedev(unsigned int major,unsigned int minor)126 makedev(unsigned int major, unsigned int minor)
127 {
128 return ((major & 0xFFF) << 8) | (minor & 0xFF);
129 }
130
131 #define highbit(x) __fls(x)
132 #define lowbit(x) __ffs(x)
133
134 #define highbit64(x) fls64(x)
135 #define makedevice(maj, min) makedev(maj, min)
136
137 /* common macros */
138 #ifndef MIN
139 #define MIN(a, b) ((a) < (b) ? (a) : (b))
140 #endif
141 #ifndef MAX
142 #define MAX(a, b) ((a) < (b) ? (b) : (a))
143 #endif
144 #ifndef ABS
145 #define ABS(a) ((a) < 0 ? -(a) : (a))
146 #endif
147 #ifndef DIV_ROUND_UP
148 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
149 #endif
150 #ifndef roundup
151 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
152 #endif
153 #ifndef howmany
154 #define howmany(x, y) (((x) + ((y) - 1)) / (y))
155 #endif
156
157 /*
158 * Compatibility macros/typedefs needed for Solaris -> Linux port
159 */
160 // Deprecated. Use P2ALIGN_TYPED instead.
161 // #define P2ALIGN(x, align) ((x) & -(align))
162 #define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
163 #define P2ROUNDUP(x, align) ((((x) - 1) | ((align) - 1)) + 1)
164 #define P2PHASE(x, align) ((x) & ((align) - 1))
165 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
166 #define ISP2(x) (((x) & ((x) - 1)) == 0)
167 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
168 #define P2BOUNDARY(off, len, align) \
169 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
170
171 /*
172 * Typed version of the P2* macros. These macros should be used to ensure
173 * that the result is correctly calculated based on the data type of (x),
174 * which is passed in as the last argument, regardless of the data
175 * type of the alignment. For example, if (x) is of type uint64_t,
176 * and we want to round it up to a page boundary using "PAGESIZE" as
177 * the alignment, we can do either
178 *
179 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
180 * or
181 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
182 */
183 #define P2ALIGN_TYPED(x, align, type) \
184 ((type)(x) & -(type)(align))
185 #define P2PHASE_TYPED(x, align, type) \
186 ((type)(x) & ((type)(align) - 1))
187 #define P2NPHASE_TYPED(x, align, type) \
188 (-(type)(x) & ((type)(align) - 1))
189 #define P2ROUNDUP_TYPED(x, align, type) \
190 ((((type)(x) - 1) | ((type)(align) - 1)) + 1)
191 #define P2END_TYPED(x, align, type) \
192 (-(~(type)(x) & -(type)(align)))
193 #define P2PHASEUP_TYPED(x, align, phase, type) \
194 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
195 #define P2CROSS_TYPED(x, y, align, type) \
196 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
197 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
198 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
199
200 #include <linux/sort.h>
201 #define qsort(base, num, size, cmp) \
202 sort(base, num, size, cmp, NULL)
203
204 #if !defined(_KMEMUSER) && !defined(offsetof)
205
206 /* avoid any possibility of clashing with <stddef.h> version */
207
208 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
209 #endif
210
211 #endif /* _SPL_SYSMACROS_H */
212