xref: /freebsd-src/sys/contrib/openzfs/module/os/linux/spl/spl-generic.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3*eda14cbcSMatt Macy  *  Copyright (C) 2007 The Regents of the University of California.
4*eda14cbcSMatt Macy  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5*eda14cbcSMatt Macy  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6*eda14cbcSMatt Macy  *  UCRL-CODE-235197
7*eda14cbcSMatt Macy  *
8*eda14cbcSMatt Macy  *  This file is part of the SPL, Solaris Porting Layer.
9*eda14cbcSMatt Macy  *  For details, see <http://zfsonlinux.org/>.
10*eda14cbcSMatt Macy  *
11*eda14cbcSMatt Macy  *  The SPL is free software; you can redistribute it and/or modify it
12*eda14cbcSMatt Macy  *  under the terms of the GNU General Public License as published by the
13*eda14cbcSMatt Macy  *  Free Software Foundation; either version 2 of the License, or (at your
14*eda14cbcSMatt Macy  *  option) any later version.
15*eda14cbcSMatt Macy  *
16*eda14cbcSMatt Macy  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
17*eda14cbcSMatt Macy  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18*eda14cbcSMatt Macy  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19*eda14cbcSMatt Macy  *  for more details.
20*eda14cbcSMatt Macy  *
21*eda14cbcSMatt Macy  *  You should have received a copy of the GNU General Public License along
22*eda14cbcSMatt Macy  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
23*eda14cbcSMatt Macy  *
24*eda14cbcSMatt Macy  *  Solaris Porting Layer (SPL) Generic Implementation.
25*eda14cbcSMatt Macy  */
26*eda14cbcSMatt Macy 
27*eda14cbcSMatt Macy #include <sys/sysmacros.h>
28*eda14cbcSMatt Macy #include <sys/systeminfo.h>
29*eda14cbcSMatt Macy #include <sys/vmsystm.h>
30*eda14cbcSMatt Macy #include <sys/kmem.h>
31*eda14cbcSMatt Macy #include <sys/kmem_cache.h>
32*eda14cbcSMatt Macy #include <sys/vmem.h>
33*eda14cbcSMatt Macy #include <sys/mutex.h>
34*eda14cbcSMatt Macy #include <sys/rwlock.h>
35*eda14cbcSMatt Macy #include <sys/taskq.h>
36*eda14cbcSMatt Macy #include <sys/tsd.h>
37*eda14cbcSMatt Macy #include <sys/zmod.h>
38*eda14cbcSMatt Macy #include <sys/debug.h>
39*eda14cbcSMatt Macy #include <sys/proc.h>
40*eda14cbcSMatt Macy #include <sys/kstat.h>
41*eda14cbcSMatt Macy #include <sys/file.h>
42*eda14cbcSMatt Macy #include <sys/sunddi.h>
43*eda14cbcSMatt Macy #include <linux/ctype.h>
44*eda14cbcSMatt Macy #include <sys/disp.h>
45*eda14cbcSMatt Macy #include <sys/random.h>
46*eda14cbcSMatt Macy #include <sys/strings.h>
47*eda14cbcSMatt Macy #include <linux/kmod.h>
48*eda14cbcSMatt Macy #include "zfs_gitrev.h"
49*eda14cbcSMatt Macy #include <linux/mod_compat.h>
50*eda14cbcSMatt Macy #include <sys/cred.h>
51*eda14cbcSMatt Macy #include <sys/vnode.h>
52*eda14cbcSMatt Macy 
53*eda14cbcSMatt Macy char spl_gitrev[64] = ZFS_META_GITREV;
54*eda14cbcSMatt Macy 
55*eda14cbcSMatt Macy /* BEGIN CSTYLED */
56*eda14cbcSMatt Macy unsigned long spl_hostid = 0;
57*eda14cbcSMatt Macy EXPORT_SYMBOL(spl_hostid);
58*eda14cbcSMatt Macy /* BEGIN CSTYLED */
59*eda14cbcSMatt Macy module_param(spl_hostid, ulong, 0644);
60*eda14cbcSMatt Macy MODULE_PARM_DESC(spl_hostid, "The system hostid.");
61*eda14cbcSMatt Macy /* END CSTYLED */
62*eda14cbcSMatt Macy 
63*eda14cbcSMatt Macy proc_t p0;
64*eda14cbcSMatt Macy EXPORT_SYMBOL(p0);
65*eda14cbcSMatt Macy 
66*eda14cbcSMatt Macy /*
67*eda14cbcSMatt Macy  * Xorshift Pseudo Random Number Generator based on work by Sebastiano Vigna
68*eda14cbcSMatt Macy  *
69*eda14cbcSMatt Macy  * "Further scramblings of Marsaglia's xorshift generators"
70*eda14cbcSMatt Macy  * http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf
71*eda14cbcSMatt Macy  *
72*eda14cbcSMatt Macy  * random_get_pseudo_bytes() is an API function on Illumos whose sole purpose
73*eda14cbcSMatt Macy  * is to provide bytes containing random numbers. It is mapped to /dev/urandom
74*eda14cbcSMatt Macy  * on Illumos, which uses a "FIPS 186-2 algorithm". No user of the SPL's
75*eda14cbcSMatt Macy  * random_get_pseudo_bytes() needs bytes that are of cryptographic quality, so
76*eda14cbcSMatt Macy  * we can implement it using a fast PRNG that we seed using Linux' actual
77*eda14cbcSMatt Macy  * equivalent to random_get_pseudo_bytes(). We do this by providing each CPU
78*eda14cbcSMatt Macy  * with an independent seed so that all calls to random_get_pseudo_bytes() are
79*eda14cbcSMatt Macy  * free of atomic instructions.
80*eda14cbcSMatt Macy  *
81*eda14cbcSMatt Macy  * A consequence of using a fast PRNG is that using random_get_pseudo_bytes()
82*eda14cbcSMatt Macy  * to generate words larger than 128 bits will paradoxically be limited to
83*eda14cbcSMatt Macy  * `2^128 - 1` possibilities. This is because we have a sequence of `2^128 - 1`
84*eda14cbcSMatt Macy  * 128-bit words and selecting the first will implicitly select the second. If
85*eda14cbcSMatt Macy  * a caller finds this behavior undesirable, random_get_bytes() should be used
86*eda14cbcSMatt Macy  * instead.
87*eda14cbcSMatt Macy  *
88*eda14cbcSMatt Macy  * XXX: Linux interrupt handlers that trigger within the critical section
89*eda14cbcSMatt Macy  * formed by `s[1] = xp[1];` and `xp[0] = s[0];` and call this function will
90*eda14cbcSMatt Macy  * see the same numbers. Nothing in the code currently calls this in an
91*eda14cbcSMatt Macy  * interrupt handler, so this is considered to be okay. If that becomes a
92*eda14cbcSMatt Macy  * problem, we could create a set of per-cpu variables for interrupt handlers
93*eda14cbcSMatt Macy  * and use them when in_interrupt() from linux/preempt_mask.h evaluates to
94*eda14cbcSMatt Macy  * true.
95*eda14cbcSMatt Macy  */
96*eda14cbcSMatt Macy void __percpu *spl_pseudo_entropy;
97*eda14cbcSMatt Macy 
98*eda14cbcSMatt Macy /*
99*eda14cbcSMatt Macy  * spl_rand_next()/spl_rand_jump() are copied from the following CC-0 licensed
100*eda14cbcSMatt Macy  * file:
101*eda14cbcSMatt Macy  *
102*eda14cbcSMatt Macy  * http://xorshift.di.unimi.it/xorshift128plus.c
103*eda14cbcSMatt Macy  */
104*eda14cbcSMatt Macy 
105*eda14cbcSMatt Macy static inline uint64_t
106*eda14cbcSMatt Macy spl_rand_next(uint64_t *s)
107*eda14cbcSMatt Macy {
108*eda14cbcSMatt Macy 	uint64_t s1 = s[0];
109*eda14cbcSMatt Macy 	const uint64_t s0 = s[1];
110*eda14cbcSMatt Macy 	s[0] = s0;
111*eda14cbcSMatt Macy 	s1 ^= s1 << 23; // a
112*eda14cbcSMatt Macy 	s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); // b, c
113*eda14cbcSMatt Macy 	return (s[1] + s0);
114*eda14cbcSMatt Macy }
115*eda14cbcSMatt Macy 
116*eda14cbcSMatt Macy static inline void
117*eda14cbcSMatt Macy spl_rand_jump(uint64_t *s)
118*eda14cbcSMatt Macy {
119*eda14cbcSMatt Macy 	static const uint64_t JUMP[] =
120*eda14cbcSMatt Macy 	    { 0x8a5cd789635d2dff, 0x121fd2155c472f96 };
121*eda14cbcSMatt Macy 
122*eda14cbcSMatt Macy 	uint64_t s0 = 0;
123*eda14cbcSMatt Macy 	uint64_t s1 = 0;
124*eda14cbcSMatt Macy 	int i, b;
125*eda14cbcSMatt Macy 	for (i = 0; i < sizeof (JUMP) / sizeof (*JUMP); i++)
126*eda14cbcSMatt Macy 		for (b = 0; b < 64; b++) {
127*eda14cbcSMatt Macy 			if (JUMP[i] & 1ULL << b) {
128*eda14cbcSMatt Macy 				s0 ^= s[0];
129*eda14cbcSMatt Macy 				s1 ^= s[1];
130*eda14cbcSMatt Macy 			}
131*eda14cbcSMatt Macy 			(void) spl_rand_next(s);
132*eda14cbcSMatt Macy 		}
133*eda14cbcSMatt Macy 
134*eda14cbcSMatt Macy 	s[0] = s0;
135*eda14cbcSMatt Macy 	s[1] = s1;
136*eda14cbcSMatt Macy }
137*eda14cbcSMatt Macy 
138*eda14cbcSMatt Macy int
139*eda14cbcSMatt Macy random_get_pseudo_bytes(uint8_t *ptr, size_t len)
140*eda14cbcSMatt Macy {
141*eda14cbcSMatt Macy 	uint64_t *xp, s[2];
142*eda14cbcSMatt Macy 
143*eda14cbcSMatt Macy 	ASSERT(ptr);
144*eda14cbcSMatt Macy 
145*eda14cbcSMatt Macy 	xp = get_cpu_ptr(spl_pseudo_entropy);
146*eda14cbcSMatt Macy 
147*eda14cbcSMatt Macy 	s[0] = xp[0];
148*eda14cbcSMatt Macy 	s[1] = xp[1];
149*eda14cbcSMatt Macy 
150*eda14cbcSMatt Macy 	while (len) {
151*eda14cbcSMatt Macy 		union {
152*eda14cbcSMatt Macy 			uint64_t ui64;
153*eda14cbcSMatt Macy 			uint8_t byte[sizeof (uint64_t)];
154*eda14cbcSMatt Macy 		}entropy;
155*eda14cbcSMatt Macy 		int i = MIN(len, sizeof (uint64_t));
156*eda14cbcSMatt Macy 
157*eda14cbcSMatt Macy 		len -= i;
158*eda14cbcSMatt Macy 		entropy.ui64 = spl_rand_next(s);
159*eda14cbcSMatt Macy 
160*eda14cbcSMatt Macy 		while (i--)
161*eda14cbcSMatt Macy 			*ptr++ = entropy.byte[i];
162*eda14cbcSMatt Macy 	}
163*eda14cbcSMatt Macy 
164*eda14cbcSMatt Macy 	xp[0] = s[0];
165*eda14cbcSMatt Macy 	xp[1] = s[1];
166*eda14cbcSMatt Macy 
167*eda14cbcSMatt Macy 	put_cpu_ptr(spl_pseudo_entropy);
168*eda14cbcSMatt Macy 
169*eda14cbcSMatt Macy 	return (0);
170*eda14cbcSMatt Macy }
171*eda14cbcSMatt Macy 
172*eda14cbcSMatt Macy 
173*eda14cbcSMatt Macy EXPORT_SYMBOL(random_get_pseudo_bytes);
174*eda14cbcSMatt Macy 
175*eda14cbcSMatt Macy #if BITS_PER_LONG == 32
176*eda14cbcSMatt Macy 
177*eda14cbcSMatt Macy /*
178*eda14cbcSMatt Macy  * Support 64/64 => 64 division on a 32-bit platform.  While the kernel
179*eda14cbcSMatt Macy  * provides a div64_u64() function for this we do not use it because the
180*eda14cbcSMatt Macy  * implementation is flawed.  There are cases which return incorrect
181*eda14cbcSMatt Macy  * results as late as linux-2.6.35.  Until this is fixed upstream the
182*eda14cbcSMatt Macy  * spl must provide its own implementation.
183*eda14cbcSMatt Macy  *
184*eda14cbcSMatt Macy  * This implementation is a slightly modified version of the algorithm
185*eda14cbcSMatt Macy  * proposed by the book 'Hacker's Delight'.  The original source can be
186*eda14cbcSMatt Macy  * found here and is available for use without restriction.
187*eda14cbcSMatt Macy  *
188*eda14cbcSMatt Macy  * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
189*eda14cbcSMatt Macy  */
190*eda14cbcSMatt Macy 
191*eda14cbcSMatt Macy /*
192*eda14cbcSMatt Macy  * Calculate number of leading of zeros for a 64-bit value.
193*eda14cbcSMatt Macy  */
194*eda14cbcSMatt Macy static int
195*eda14cbcSMatt Macy nlz64(uint64_t x)
196*eda14cbcSMatt Macy {
197*eda14cbcSMatt Macy 	register int n = 0;
198*eda14cbcSMatt Macy 
199*eda14cbcSMatt Macy 	if (x == 0)
200*eda14cbcSMatt Macy 		return (64);
201*eda14cbcSMatt Macy 
202*eda14cbcSMatt Macy 	if (x <= 0x00000000FFFFFFFFULL) { n = n + 32; x = x << 32; }
203*eda14cbcSMatt Macy 	if (x <= 0x0000FFFFFFFFFFFFULL) { n = n + 16; x = x << 16; }
204*eda14cbcSMatt Macy 	if (x <= 0x00FFFFFFFFFFFFFFULL) { n = n +  8; x = x <<  8; }
205*eda14cbcSMatt Macy 	if (x <= 0x0FFFFFFFFFFFFFFFULL) { n = n +  4; x = x <<  4; }
206*eda14cbcSMatt Macy 	if (x <= 0x3FFFFFFFFFFFFFFFULL) { n = n +  2; x = x <<  2; }
207*eda14cbcSMatt Macy 	if (x <= 0x7FFFFFFFFFFFFFFFULL) { n = n +  1; }
208*eda14cbcSMatt Macy 
209*eda14cbcSMatt Macy 	return (n);
210*eda14cbcSMatt Macy }
211*eda14cbcSMatt Macy 
212*eda14cbcSMatt Macy /*
213*eda14cbcSMatt Macy  * Newer kernels have a div_u64() function but we define our own
214*eda14cbcSMatt Macy  * to simplify portability between kernel versions.
215*eda14cbcSMatt Macy  */
216*eda14cbcSMatt Macy static inline uint64_t
217*eda14cbcSMatt Macy __div_u64(uint64_t u, uint32_t v)
218*eda14cbcSMatt Macy {
219*eda14cbcSMatt Macy 	(void) do_div(u, v);
220*eda14cbcSMatt Macy 	return (u);
221*eda14cbcSMatt Macy }
222*eda14cbcSMatt Macy 
223*eda14cbcSMatt Macy /*
224*eda14cbcSMatt Macy  * Turn off missing prototypes warning for these functions. They are
225*eda14cbcSMatt Macy  * replacements for libgcc-provided functions and will never be called
226*eda14cbcSMatt Macy  * directly.
227*eda14cbcSMatt Macy  */
228*eda14cbcSMatt Macy #pragma GCC diagnostic push
229*eda14cbcSMatt Macy #pragma GCC diagnostic ignored "-Wmissing-prototypes"
230*eda14cbcSMatt Macy 
231*eda14cbcSMatt Macy /*
232*eda14cbcSMatt Macy  * Implementation of 64-bit unsigned division for 32-bit machines.
233*eda14cbcSMatt Macy  *
234*eda14cbcSMatt Macy  * First the procedure takes care of the case in which the divisor is a
235*eda14cbcSMatt Macy  * 32-bit quantity. There are two subcases: (1) If the left half of the
236*eda14cbcSMatt Macy  * dividend is less than the divisor, one execution of do_div() is all that
237*eda14cbcSMatt Macy  * is required (overflow is not possible). (2) Otherwise it does two
238*eda14cbcSMatt Macy  * divisions, using the grade school method.
239*eda14cbcSMatt Macy  */
240*eda14cbcSMatt Macy uint64_t
241*eda14cbcSMatt Macy __udivdi3(uint64_t u, uint64_t v)
242*eda14cbcSMatt Macy {
243*eda14cbcSMatt Macy 	uint64_t u0, u1, v1, q0, q1, k;
244*eda14cbcSMatt Macy 	int n;
245*eda14cbcSMatt Macy 
246*eda14cbcSMatt Macy 	if (v >> 32 == 0) {			// If v < 2**32:
247*eda14cbcSMatt Macy 		if (u >> 32 < v) {		// If u/v cannot overflow,
248*eda14cbcSMatt Macy 			return (__div_u64(u, v)); // just do one division.
249*eda14cbcSMatt Macy 		} else {			// If u/v would overflow:
250*eda14cbcSMatt Macy 			u1 = u >> 32;		// Break u into two halves.
251*eda14cbcSMatt Macy 			u0 = u & 0xFFFFFFFF;
252*eda14cbcSMatt Macy 			q1 = __div_u64(u1, v);	// First quotient digit.
253*eda14cbcSMatt Macy 			k  = u1 - q1 * v;	// First remainder, < v.
254*eda14cbcSMatt Macy 			u0 += (k << 32);
255*eda14cbcSMatt Macy 			q0 = __div_u64(u0, v);	// Seconds quotient digit.
256*eda14cbcSMatt Macy 			return ((q1 << 32) + q0);
257*eda14cbcSMatt Macy 		}
258*eda14cbcSMatt Macy 	} else {				// If v >= 2**32:
259*eda14cbcSMatt Macy 		n = nlz64(v);			// 0 <= n <= 31.
260*eda14cbcSMatt Macy 		v1 = (v << n) >> 32;		// Normalize divisor, MSB is 1.
261*eda14cbcSMatt Macy 		u1 = u >> 1;			// To ensure no overflow.
262*eda14cbcSMatt Macy 		q1 = __div_u64(u1, v1);		// Get quotient from
263*eda14cbcSMatt Macy 		q0 = (q1 << n) >> 31;		// Undo normalization and
264*eda14cbcSMatt Macy 						// division of u by 2.
265*eda14cbcSMatt Macy 		if (q0 != 0)			// Make q0 correct or
266*eda14cbcSMatt Macy 			q0 = q0 - 1;		// too small by 1.
267*eda14cbcSMatt Macy 		if ((u - q0 * v) >= v)
268*eda14cbcSMatt Macy 			q0 = q0 + 1;		// Now q0 is correct.
269*eda14cbcSMatt Macy 
270*eda14cbcSMatt Macy 		return (q0);
271*eda14cbcSMatt Macy 	}
272*eda14cbcSMatt Macy }
273*eda14cbcSMatt Macy EXPORT_SYMBOL(__udivdi3);
274*eda14cbcSMatt Macy 
275*eda14cbcSMatt Macy /* BEGIN CSTYLED */
276*eda14cbcSMatt Macy #ifndef abs64
277*eda14cbcSMatt Macy #define	abs64(x)	({ uint64_t t = (x) >> 63; ((x) ^ t) - t; })
278*eda14cbcSMatt Macy #endif
279*eda14cbcSMatt Macy /* END CSTYLED */
280*eda14cbcSMatt Macy 
281*eda14cbcSMatt Macy /*
282*eda14cbcSMatt Macy  * Implementation of 64-bit signed division for 32-bit machines.
283*eda14cbcSMatt Macy  */
284*eda14cbcSMatt Macy int64_t
285*eda14cbcSMatt Macy __divdi3(int64_t u, int64_t v)
286*eda14cbcSMatt Macy {
287*eda14cbcSMatt Macy 	int64_t q, t;
288*eda14cbcSMatt Macy 	// cppcheck-suppress shiftTooManyBitsSigned
289*eda14cbcSMatt Macy 	q = __udivdi3(abs64(u), abs64(v));
290*eda14cbcSMatt Macy 	// cppcheck-suppress shiftTooManyBitsSigned
291*eda14cbcSMatt Macy 	t = (u ^ v) >> 63;	// If u, v have different
292*eda14cbcSMatt Macy 	return ((q ^ t) - t);	// signs, negate q.
293*eda14cbcSMatt Macy }
294*eda14cbcSMatt Macy EXPORT_SYMBOL(__divdi3);
295*eda14cbcSMatt Macy 
296*eda14cbcSMatt Macy /*
297*eda14cbcSMatt Macy  * Implementation of 64-bit unsigned modulo for 32-bit machines.
298*eda14cbcSMatt Macy  */
299*eda14cbcSMatt Macy uint64_t
300*eda14cbcSMatt Macy __umoddi3(uint64_t dividend, uint64_t divisor)
301*eda14cbcSMatt Macy {
302*eda14cbcSMatt Macy 	return (dividend - (divisor * __udivdi3(dividend, divisor)));
303*eda14cbcSMatt Macy }
304*eda14cbcSMatt Macy EXPORT_SYMBOL(__umoddi3);
305*eda14cbcSMatt Macy 
306*eda14cbcSMatt Macy /* 64-bit signed modulo for 32-bit machines. */
307*eda14cbcSMatt Macy int64_t
308*eda14cbcSMatt Macy __moddi3(int64_t n, int64_t d)
309*eda14cbcSMatt Macy {
310*eda14cbcSMatt Macy 	int64_t q;
311*eda14cbcSMatt Macy 	boolean_t nn = B_FALSE;
312*eda14cbcSMatt Macy 
313*eda14cbcSMatt Macy 	if (n < 0) {
314*eda14cbcSMatt Macy 		nn = B_TRUE;
315*eda14cbcSMatt Macy 		n = -n;
316*eda14cbcSMatt Macy 	}
317*eda14cbcSMatt Macy 	if (d < 0)
318*eda14cbcSMatt Macy 		d = -d;
319*eda14cbcSMatt Macy 
320*eda14cbcSMatt Macy 	q = __umoddi3(n, d);
321*eda14cbcSMatt Macy 
322*eda14cbcSMatt Macy 	return (nn ? -q : q);
323*eda14cbcSMatt Macy }
324*eda14cbcSMatt Macy EXPORT_SYMBOL(__moddi3);
325*eda14cbcSMatt Macy 
326*eda14cbcSMatt Macy /*
327*eda14cbcSMatt Macy  * Implementation of 64-bit unsigned division/modulo for 32-bit machines.
328*eda14cbcSMatt Macy  */
329*eda14cbcSMatt Macy uint64_t
330*eda14cbcSMatt Macy __udivmoddi4(uint64_t n, uint64_t d, uint64_t *r)
331*eda14cbcSMatt Macy {
332*eda14cbcSMatt Macy 	uint64_t q = __udivdi3(n, d);
333*eda14cbcSMatt Macy 	if (r)
334*eda14cbcSMatt Macy 		*r = n - d * q;
335*eda14cbcSMatt Macy 	return (q);
336*eda14cbcSMatt Macy }
337*eda14cbcSMatt Macy EXPORT_SYMBOL(__udivmoddi4);
338*eda14cbcSMatt Macy 
339*eda14cbcSMatt Macy /*
340*eda14cbcSMatt Macy  * Implementation of 64-bit signed division/modulo for 32-bit machines.
341*eda14cbcSMatt Macy  */
342*eda14cbcSMatt Macy int64_t
343*eda14cbcSMatt Macy __divmoddi4(int64_t n, int64_t d, int64_t *r)
344*eda14cbcSMatt Macy {
345*eda14cbcSMatt Macy 	int64_t q, rr;
346*eda14cbcSMatt Macy 	boolean_t nn = B_FALSE;
347*eda14cbcSMatt Macy 	boolean_t nd = B_FALSE;
348*eda14cbcSMatt Macy 	if (n < 0) {
349*eda14cbcSMatt Macy 		nn = B_TRUE;
350*eda14cbcSMatt Macy 		n = -n;
351*eda14cbcSMatt Macy 	}
352*eda14cbcSMatt Macy 	if (d < 0) {
353*eda14cbcSMatt Macy 		nd = B_TRUE;
354*eda14cbcSMatt Macy 		d = -d;
355*eda14cbcSMatt Macy 	}
356*eda14cbcSMatt Macy 
357*eda14cbcSMatt Macy 	q = __udivmoddi4(n, d, (uint64_t *)&rr);
358*eda14cbcSMatt Macy 
359*eda14cbcSMatt Macy 	if (nn != nd)
360*eda14cbcSMatt Macy 		q = -q;
361*eda14cbcSMatt Macy 	if (nn)
362*eda14cbcSMatt Macy 		rr = -rr;
363*eda14cbcSMatt Macy 	if (r)
364*eda14cbcSMatt Macy 		*r = rr;
365*eda14cbcSMatt Macy 	return (q);
366*eda14cbcSMatt Macy }
367*eda14cbcSMatt Macy EXPORT_SYMBOL(__divmoddi4);
368*eda14cbcSMatt Macy 
369*eda14cbcSMatt Macy #if defined(__arm) || defined(__arm__)
370*eda14cbcSMatt Macy /*
371*eda14cbcSMatt Macy  * Implementation of 64-bit (un)signed division for 32-bit arm machines.
372*eda14cbcSMatt Macy  *
373*eda14cbcSMatt Macy  * Run-time ABI for the ARM Architecture (page 20).  A pair of (unsigned)
374*eda14cbcSMatt Macy  * long longs is returned in {{r0, r1}, {r2,r3}}, the quotient in {r0, r1},
375*eda14cbcSMatt Macy  * and the remainder in {r2, r3}.  The return type is specifically left
376*eda14cbcSMatt Macy  * set to 'void' to ensure the compiler does not overwrite these registers
377*eda14cbcSMatt Macy  * during the return.  All results are in registers as per ABI
378*eda14cbcSMatt Macy  */
379*eda14cbcSMatt Macy void
380*eda14cbcSMatt Macy __aeabi_uldivmod(uint64_t u, uint64_t v)
381*eda14cbcSMatt Macy {
382*eda14cbcSMatt Macy 	uint64_t res;
383*eda14cbcSMatt Macy 	uint64_t mod;
384*eda14cbcSMatt Macy 
385*eda14cbcSMatt Macy 	res = __udivdi3(u, v);
386*eda14cbcSMatt Macy 	mod = __umoddi3(u, v);
387*eda14cbcSMatt Macy 	{
388*eda14cbcSMatt Macy 		register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF);
389*eda14cbcSMatt Macy 		register uint32_t r1 asm("r1") = (res >> 32);
390*eda14cbcSMatt Macy 		register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF);
391*eda14cbcSMatt Macy 		register uint32_t r3 asm("r3") = (mod >> 32);
392*eda14cbcSMatt Macy 
393*eda14cbcSMatt Macy 		/* BEGIN CSTYLED */
394*eda14cbcSMatt Macy 		asm volatile(""
395*eda14cbcSMatt Macy 		    : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3)  /* output */
396*eda14cbcSMatt Macy 		    : "r"(r0), "r"(r1), "r"(r2), "r"(r3));   /* input */
397*eda14cbcSMatt Macy 		/* END CSTYLED */
398*eda14cbcSMatt Macy 
399*eda14cbcSMatt Macy 		return; /* r0; */
400*eda14cbcSMatt Macy 	}
401*eda14cbcSMatt Macy }
402*eda14cbcSMatt Macy EXPORT_SYMBOL(__aeabi_uldivmod);
403*eda14cbcSMatt Macy 
404*eda14cbcSMatt Macy void
405*eda14cbcSMatt Macy __aeabi_ldivmod(int64_t u, int64_t v)
406*eda14cbcSMatt Macy {
407*eda14cbcSMatt Macy 	int64_t res;
408*eda14cbcSMatt Macy 	uint64_t mod;
409*eda14cbcSMatt Macy 
410*eda14cbcSMatt Macy 	res =  __divdi3(u, v);
411*eda14cbcSMatt Macy 	mod = __umoddi3(u, v);
412*eda14cbcSMatt Macy 	{
413*eda14cbcSMatt Macy 		register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF);
414*eda14cbcSMatt Macy 		register uint32_t r1 asm("r1") = (res >> 32);
415*eda14cbcSMatt Macy 		register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF);
416*eda14cbcSMatt Macy 		register uint32_t r3 asm("r3") = (mod >> 32);
417*eda14cbcSMatt Macy 
418*eda14cbcSMatt Macy 		/* BEGIN CSTYLED */
419*eda14cbcSMatt Macy 		asm volatile(""
420*eda14cbcSMatt Macy 		    : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3)  /* output */
421*eda14cbcSMatt Macy 		    : "r"(r0), "r"(r1), "r"(r2), "r"(r3));   /* input */
422*eda14cbcSMatt Macy 		/* END CSTYLED */
423*eda14cbcSMatt Macy 
424*eda14cbcSMatt Macy 		return; /* r0; */
425*eda14cbcSMatt Macy 	}
426*eda14cbcSMatt Macy }
427*eda14cbcSMatt Macy EXPORT_SYMBOL(__aeabi_ldivmod);
428*eda14cbcSMatt Macy #endif /* __arm || __arm__ */
429*eda14cbcSMatt Macy 
430*eda14cbcSMatt Macy #pragma GCC diagnostic pop
431*eda14cbcSMatt Macy 
432*eda14cbcSMatt Macy #endif /* BITS_PER_LONG */
433*eda14cbcSMatt Macy 
434*eda14cbcSMatt Macy /*
435*eda14cbcSMatt Macy  * NOTE: The strtoxx behavior is solely based on my reading of the Solaris
436*eda14cbcSMatt Macy  * ddi_strtol(9F) man page.  I have not verified the behavior of these
437*eda14cbcSMatt Macy  * functions against their Solaris counterparts.  It is possible that I
438*eda14cbcSMatt Macy  * may have misinterpreted the man page or the man page is incorrect.
439*eda14cbcSMatt Macy  */
440*eda14cbcSMatt Macy int ddi_strtoul(const char *, char **, int, unsigned long *);
441*eda14cbcSMatt Macy int ddi_strtol(const char *, char **, int, long *);
442*eda14cbcSMatt Macy int ddi_strtoull(const char *, char **, int, unsigned long long *);
443*eda14cbcSMatt Macy int ddi_strtoll(const char *, char **, int, long long *);
444*eda14cbcSMatt Macy 
445*eda14cbcSMatt Macy #define	define_ddi_strtoux(type, valtype)				\
446*eda14cbcSMatt Macy int ddi_strtou##type(const char *str, char **endptr,			\
447*eda14cbcSMatt Macy     int base, valtype *result)						\
448*eda14cbcSMatt Macy {									\
449*eda14cbcSMatt Macy 	valtype last_value, value = 0;					\
450*eda14cbcSMatt Macy 	char *ptr = (char *)str;					\
451*eda14cbcSMatt Macy 	int flag = 1, digit;						\
452*eda14cbcSMatt Macy 									\
453*eda14cbcSMatt Macy 	if (strlen(ptr) == 0)						\
454*eda14cbcSMatt Macy 		return (EINVAL);					\
455*eda14cbcSMatt Macy 									\
456*eda14cbcSMatt Macy 	/* Auto-detect base based on prefix */				\
457*eda14cbcSMatt Macy 	if (!base) {							\
458*eda14cbcSMatt Macy 		if (str[0] == '0') {					\
459*eda14cbcSMatt Macy 			if (tolower(str[1]) == 'x' && isxdigit(str[2])) { \
460*eda14cbcSMatt Macy 				base = 16; /* hex */			\
461*eda14cbcSMatt Macy 				ptr += 2;				\
462*eda14cbcSMatt Macy 			} else if (str[1] >= '0' && str[1] < 8) {	\
463*eda14cbcSMatt Macy 				base = 8; /* octal */			\
464*eda14cbcSMatt Macy 				ptr += 1;				\
465*eda14cbcSMatt Macy 			} else {					\
466*eda14cbcSMatt Macy 				return (EINVAL);			\
467*eda14cbcSMatt Macy 			}						\
468*eda14cbcSMatt Macy 		} else {						\
469*eda14cbcSMatt Macy 			base = 10; /* decimal */			\
470*eda14cbcSMatt Macy 		}							\
471*eda14cbcSMatt Macy 	}								\
472*eda14cbcSMatt Macy 									\
473*eda14cbcSMatt Macy 	while (1) {							\
474*eda14cbcSMatt Macy 		if (isdigit(*ptr))					\
475*eda14cbcSMatt Macy 			digit = *ptr - '0';				\
476*eda14cbcSMatt Macy 		else if (isalpha(*ptr))					\
477*eda14cbcSMatt Macy 			digit = tolower(*ptr) - 'a' + 10;		\
478*eda14cbcSMatt Macy 		else							\
479*eda14cbcSMatt Macy 			break;						\
480*eda14cbcSMatt Macy 									\
481*eda14cbcSMatt Macy 		if (digit >= base)					\
482*eda14cbcSMatt Macy 			break;						\
483*eda14cbcSMatt Macy 									\
484*eda14cbcSMatt Macy 		last_value = value;					\
485*eda14cbcSMatt Macy 		value = value * base + digit;				\
486*eda14cbcSMatt Macy 		if (last_value > value) /* Overflow */			\
487*eda14cbcSMatt Macy 			return (ERANGE);				\
488*eda14cbcSMatt Macy 									\
489*eda14cbcSMatt Macy 		flag = 1;						\
490*eda14cbcSMatt Macy 		ptr++;							\
491*eda14cbcSMatt Macy 	}								\
492*eda14cbcSMatt Macy 									\
493*eda14cbcSMatt Macy 	if (flag)							\
494*eda14cbcSMatt Macy 		*result = value;					\
495*eda14cbcSMatt Macy 									\
496*eda14cbcSMatt Macy 	if (endptr)							\
497*eda14cbcSMatt Macy 		*endptr = (char *)(flag ? ptr : str);			\
498*eda14cbcSMatt Macy 									\
499*eda14cbcSMatt Macy 	return (0);							\
500*eda14cbcSMatt Macy }									\
501*eda14cbcSMatt Macy 
502*eda14cbcSMatt Macy #define	define_ddi_strtox(type, valtype)				\
503*eda14cbcSMatt Macy int ddi_strto##type(const char *str, char **endptr,			\
504*eda14cbcSMatt Macy     int base, valtype *result)						\
505*eda14cbcSMatt Macy {									\
506*eda14cbcSMatt Macy 	int rc;								\
507*eda14cbcSMatt Macy 									\
508*eda14cbcSMatt Macy 	if (*str == '-') {						\
509*eda14cbcSMatt Macy 		rc = ddi_strtou##type(str + 1, endptr, base, result);	\
510*eda14cbcSMatt Macy 		if (!rc) {						\
511*eda14cbcSMatt Macy 			if (*endptr == str + 1)				\
512*eda14cbcSMatt Macy 				*endptr = (char *)str;			\
513*eda14cbcSMatt Macy 			else						\
514*eda14cbcSMatt Macy 				*result = -*result;			\
515*eda14cbcSMatt Macy 		}							\
516*eda14cbcSMatt Macy 	} else {							\
517*eda14cbcSMatt Macy 		rc = ddi_strtou##type(str, endptr, base, result);	\
518*eda14cbcSMatt Macy 	}								\
519*eda14cbcSMatt Macy 									\
520*eda14cbcSMatt Macy 	return (rc);							\
521*eda14cbcSMatt Macy }
522*eda14cbcSMatt Macy 
523*eda14cbcSMatt Macy define_ddi_strtoux(l, unsigned long)
524*eda14cbcSMatt Macy define_ddi_strtox(l, long)
525*eda14cbcSMatt Macy define_ddi_strtoux(ll, unsigned long long)
526*eda14cbcSMatt Macy define_ddi_strtox(ll, long long)
527*eda14cbcSMatt Macy 
528*eda14cbcSMatt Macy EXPORT_SYMBOL(ddi_strtoul);
529*eda14cbcSMatt Macy EXPORT_SYMBOL(ddi_strtol);
530*eda14cbcSMatt Macy EXPORT_SYMBOL(ddi_strtoll);
531*eda14cbcSMatt Macy EXPORT_SYMBOL(ddi_strtoull);
532*eda14cbcSMatt Macy 
533*eda14cbcSMatt Macy int
534*eda14cbcSMatt Macy ddi_copyin(const void *from, void *to, size_t len, int flags)
535*eda14cbcSMatt Macy {
536*eda14cbcSMatt Macy 	/* Fake ioctl() issued by kernel, 'from' is a kernel address */
537*eda14cbcSMatt Macy 	if (flags & FKIOCTL) {
538*eda14cbcSMatt Macy 		memcpy(to, from, len);
539*eda14cbcSMatt Macy 		return (0);
540*eda14cbcSMatt Macy 	}
541*eda14cbcSMatt Macy 
542*eda14cbcSMatt Macy 	return (copyin(from, to, len));
543*eda14cbcSMatt Macy }
544*eda14cbcSMatt Macy EXPORT_SYMBOL(ddi_copyin);
545*eda14cbcSMatt Macy 
546*eda14cbcSMatt Macy int
547*eda14cbcSMatt Macy ddi_copyout(const void *from, void *to, size_t len, int flags)
548*eda14cbcSMatt Macy {
549*eda14cbcSMatt Macy 	/* Fake ioctl() issued by kernel, 'from' is a kernel address */
550*eda14cbcSMatt Macy 	if (flags & FKIOCTL) {
551*eda14cbcSMatt Macy 		memcpy(to, from, len);
552*eda14cbcSMatt Macy 		return (0);
553*eda14cbcSMatt Macy 	}
554*eda14cbcSMatt Macy 
555*eda14cbcSMatt Macy 	return (copyout(from, to, len));
556*eda14cbcSMatt Macy }
557*eda14cbcSMatt Macy EXPORT_SYMBOL(ddi_copyout);
558*eda14cbcSMatt Macy 
559*eda14cbcSMatt Macy static ssize_t
560*eda14cbcSMatt Macy spl_kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
561*eda14cbcSMatt Macy {
562*eda14cbcSMatt Macy #if defined(HAVE_KERNEL_READ_PPOS)
563*eda14cbcSMatt Macy 	return (kernel_read(file, buf, count, pos));
564*eda14cbcSMatt Macy #else
565*eda14cbcSMatt Macy 	mm_segment_t saved_fs;
566*eda14cbcSMatt Macy 	ssize_t ret;
567*eda14cbcSMatt Macy 
568*eda14cbcSMatt Macy 	saved_fs = get_fs();
569*eda14cbcSMatt Macy 	set_fs(KERNEL_DS);
570*eda14cbcSMatt Macy 
571*eda14cbcSMatt Macy 	ret = vfs_read(file, (void __user *)buf, count, pos);
572*eda14cbcSMatt Macy 
573*eda14cbcSMatt Macy 	set_fs(saved_fs);
574*eda14cbcSMatt Macy 
575*eda14cbcSMatt Macy 	return (ret);
576*eda14cbcSMatt Macy #endif
577*eda14cbcSMatt Macy }
578*eda14cbcSMatt Macy 
579*eda14cbcSMatt Macy static int
580*eda14cbcSMatt Macy spl_getattr(struct file *filp, struct kstat *stat)
581*eda14cbcSMatt Macy {
582*eda14cbcSMatt Macy 	int rc;
583*eda14cbcSMatt Macy 
584*eda14cbcSMatt Macy 	ASSERT(filp);
585*eda14cbcSMatt Macy 	ASSERT(stat);
586*eda14cbcSMatt Macy 
587*eda14cbcSMatt Macy #if defined(HAVE_4ARGS_VFS_GETATTR)
588*eda14cbcSMatt Macy 	rc = vfs_getattr(&filp->f_path, stat, STATX_BASIC_STATS,
589*eda14cbcSMatt Macy 	    AT_STATX_SYNC_AS_STAT);
590*eda14cbcSMatt Macy #elif defined(HAVE_2ARGS_VFS_GETATTR)
591*eda14cbcSMatt Macy 	rc = vfs_getattr(&filp->f_path, stat);
592*eda14cbcSMatt Macy #else
593*eda14cbcSMatt Macy 	rc = vfs_getattr(filp->f_path.mnt, filp->f_dentry, stat);
594*eda14cbcSMatt Macy #endif
595*eda14cbcSMatt Macy 	if (rc)
596*eda14cbcSMatt Macy 		return (-rc);
597*eda14cbcSMatt Macy 
598*eda14cbcSMatt Macy 	return (0);
599*eda14cbcSMatt Macy }
600*eda14cbcSMatt Macy 
601*eda14cbcSMatt Macy /*
602*eda14cbcSMatt Macy  * Read the unique system identifier from the /etc/hostid file.
603*eda14cbcSMatt Macy  *
604*eda14cbcSMatt Macy  * The behavior of /usr/bin/hostid on Linux systems with the
605*eda14cbcSMatt Macy  * regular eglibc and coreutils is:
606*eda14cbcSMatt Macy  *
607*eda14cbcSMatt Macy  *   1. Generate the value if the /etc/hostid file does not exist
608*eda14cbcSMatt Macy  *      or if the /etc/hostid file is less than four bytes in size.
609*eda14cbcSMatt Macy  *
610*eda14cbcSMatt Macy  *   2. If the /etc/hostid file is at least 4 bytes, then return
611*eda14cbcSMatt Macy  *      the first four bytes [0..3] in native endian order.
612*eda14cbcSMatt Macy  *
613*eda14cbcSMatt Macy  *   3. Always ignore bytes [4..] if they exist in the file.
614*eda14cbcSMatt Macy  *
615*eda14cbcSMatt Macy  * Only the first four bytes are significant, even on systems that
616*eda14cbcSMatt Macy  * have a 64-bit word size.
617*eda14cbcSMatt Macy  *
618*eda14cbcSMatt Macy  * See:
619*eda14cbcSMatt Macy  *
620*eda14cbcSMatt Macy  *   eglibc: sysdeps/unix/sysv/linux/gethostid.c
621*eda14cbcSMatt Macy  *   coreutils: src/hostid.c
622*eda14cbcSMatt Macy  *
623*eda14cbcSMatt Macy  * Notes:
624*eda14cbcSMatt Macy  *
625*eda14cbcSMatt Macy  * The /etc/hostid file on Solaris is a text file that often reads:
626*eda14cbcSMatt Macy  *
627*eda14cbcSMatt Macy  *   # DO NOT EDIT
628*eda14cbcSMatt Macy  *   "0123456789"
629*eda14cbcSMatt Macy  *
630*eda14cbcSMatt Macy  * Directly copying this file to Linux results in a constant
631*eda14cbcSMatt Macy  * hostid of 4f442023 because the default comment constitutes
632*eda14cbcSMatt Macy  * the first four bytes of the file.
633*eda14cbcSMatt Macy  *
634*eda14cbcSMatt Macy  */
635*eda14cbcSMatt Macy 
636*eda14cbcSMatt Macy char *spl_hostid_path = HW_HOSTID_PATH;
637*eda14cbcSMatt Macy module_param(spl_hostid_path, charp, 0444);
638*eda14cbcSMatt Macy MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)");
639*eda14cbcSMatt Macy 
640*eda14cbcSMatt Macy static int
641*eda14cbcSMatt Macy hostid_read(uint32_t *hostid)
642*eda14cbcSMatt Macy {
643*eda14cbcSMatt Macy 	uint64_t size;
644*eda14cbcSMatt Macy 	uint32_t value = 0;
645*eda14cbcSMatt Macy 	int error;
646*eda14cbcSMatt Macy 	loff_t off;
647*eda14cbcSMatt Macy 	struct file *filp;
648*eda14cbcSMatt Macy 	struct kstat stat;
649*eda14cbcSMatt Macy 
650*eda14cbcSMatt Macy 	filp = filp_open(spl_hostid_path, 0, 0);
651*eda14cbcSMatt Macy 
652*eda14cbcSMatt Macy 	if (IS_ERR(filp))
653*eda14cbcSMatt Macy 		return (ENOENT);
654*eda14cbcSMatt Macy 
655*eda14cbcSMatt Macy 	error = spl_getattr(filp, &stat);
656*eda14cbcSMatt Macy 	if (error) {
657*eda14cbcSMatt Macy 		filp_close(filp, 0);
658*eda14cbcSMatt Macy 		return (error);
659*eda14cbcSMatt Macy 	}
660*eda14cbcSMatt Macy 	size = stat.size;
661*eda14cbcSMatt Macy 	if (size < sizeof (HW_HOSTID_MASK)) {
662*eda14cbcSMatt Macy 		filp_close(filp, 0);
663*eda14cbcSMatt Macy 		return (EINVAL);
664*eda14cbcSMatt Macy 	}
665*eda14cbcSMatt Macy 
666*eda14cbcSMatt Macy 	off = 0;
667*eda14cbcSMatt Macy 	/*
668*eda14cbcSMatt Macy 	 * Read directly into the variable like eglibc does.
669*eda14cbcSMatt Macy 	 * Short reads are okay; native behavior is preserved.
670*eda14cbcSMatt Macy 	 */
671*eda14cbcSMatt Macy 	error = spl_kernel_read(filp, &value, sizeof (value), &off);
672*eda14cbcSMatt Macy 	if (error < 0) {
673*eda14cbcSMatt Macy 		filp_close(filp, 0);
674*eda14cbcSMatt Macy 		return (EIO);
675*eda14cbcSMatt Macy 	}
676*eda14cbcSMatt Macy 
677*eda14cbcSMatt Macy 	/* Mask down to 32 bits like coreutils does. */
678*eda14cbcSMatt Macy 	*hostid = (value & HW_HOSTID_MASK);
679*eda14cbcSMatt Macy 	filp_close(filp, 0);
680*eda14cbcSMatt Macy 
681*eda14cbcSMatt Macy 	return (0);
682*eda14cbcSMatt Macy }
683*eda14cbcSMatt Macy 
684*eda14cbcSMatt Macy /*
685*eda14cbcSMatt Macy  * Return the system hostid.  Preferentially use the spl_hostid module option
686*eda14cbcSMatt Macy  * when set, otherwise use the value in the /etc/hostid file.
687*eda14cbcSMatt Macy  */
688*eda14cbcSMatt Macy uint32_t
689*eda14cbcSMatt Macy zone_get_hostid(void *zone)
690*eda14cbcSMatt Macy {
691*eda14cbcSMatt Macy 	uint32_t hostid;
692*eda14cbcSMatt Macy 
693*eda14cbcSMatt Macy 	ASSERT3P(zone, ==, NULL);
694*eda14cbcSMatt Macy 
695*eda14cbcSMatt Macy 	if (spl_hostid != 0)
696*eda14cbcSMatt Macy 		return ((uint32_t)(spl_hostid & HW_HOSTID_MASK));
697*eda14cbcSMatt Macy 
698*eda14cbcSMatt Macy 	if (hostid_read(&hostid) == 0)
699*eda14cbcSMatt Macy 		return (hostid);
700*eda14cbcSMatt Macy 
701*eda14cbcSMatt Macy 	return (0);
702*eda14cbcSMatt Macy }
703*eda14cbcSMatt Macy EXPORT_SYMBOL(zone_get_hostid);
704*eda14cbcSMatt Macy 
705*eda14cbcSMatt Macy static int
706*eda14cbcSMatt Macy spl_kvmem_init(void)
707*eda14cbcSMatt Macy {
708*eda14cbcSMatt Macy 	int rc = 0;
709*eda14cbcSMatt Macy 
710*eda14cbcSMatt Macy 	rc = spl_kmem_init();
711*eda14cbcSMatt Macy 	if (rc)
712*eda14cbcSMatt Macy 		return (rc);
713*eda14cbcSMatt Macy 
714*eda14cbcSMatt Macy 	rc = spl_vmem_init();
715*eda14cbcSMatt Macy 	if (rc) {
716*eda14cbcSMatt Macy 		spl_kmem_fini();
717*eda14cbcSMatt Macy 		return (rc);
718*eda14cbcSMatt Macy 	}
719*eda14cbcSMatt Macy 
720*eda14cbcSMatt Macy 	return (rc);
721*eda14cbcSMatt Macy }
722*eda14cbcSMatt Macy 
723*eda14cbcSMatt Macy /*
724*eda14cbcSMatt Macy  * We initialize the random number generator with 128 bits of entropy from the
725*eda14cbcSMatt Macy  * system random number generator. In the improbable case that we have a zero
726*eda14cbcSMatt Macy  * seed, we fallback to the system jiffies, unless it is also zero, in which
727*eda14cbcSMatt Macy  * situation we use a preprogrammed seed. We step forward by 2^64 iterations to
728*eda14cbcSMatt Macy  * initialize each of the per-cpu seeds so that the sequences generated on each
729*eda14cbcSMatt Macy  * CPU are guaranteed to never overlap in practice.
730*eda14cbcSMatt Macy  */
731*eda14cbcSMatt Macy static void __init
732*eda14cbcSMatt Macy spl_random_init(void)
733*eda14cbcSMatt Macy {
734*eda14cbcSMatt Macy 	uint64_t s[2];
735*eda14cbcSMatt Macy 	int i = 0;
736*eda14cbcSMatt Macy 
737*eda14cbcSMatt Macy 	spl_pseudo_entropy = __alloc_percpu(2 * sizeof (uint64_t),
738*eda14cbcSMatt Macy 	    sizeof (uint64_t));
739*eda14cbcSMatt Macy 
740*eda14cbcSMatt Macy 	get_random_bytes(s, sizeof (s));
741*eda14cbcSMatt Macy 
742*eda14cbcSMatt Macy 	if (s[0] == 0 && s[1] == 0) {
743*eda14cbcSMatt Macy 		if (jiffies != 0) {
744*eda14cbcSMatt Macy 			s[0] = jiffies;
745*eda14cbcSMatt Macy 			s[1] = ~0 - jiffies;
746*eda14cbcSMatt Macy 		} else {
747*eda14cbcSMatt Macy 			(void) memcpy(s, "improbable seed", sizeof (s));
748*eda14cbcSMatt Macy 		}
749*eda14cbcSMatt Macy 		printk("SPL: get_random_bytes() returned 0 "
750*eda14cbcSMatt Macy 		    "when generating random seed. Setting initial seed to "
751*eda14cbcSMatt Macy 		    "0x%016llx%016llx.\n", cpu_to_be64(s[0]),
752*eda14cbcSMatt Macy 		    cpu_to_be64(s[1]));
753*eda14cbcSMatt Macy 	}
754*eda14cbcSMatt Macy 
755*eda14cbcSMatt Macy 	for_each_possible_cpu(i) {
756*eda14cbcSMatt Macy 		uint64_t *wordp = per_cpu_ptr(spl_pseudo_entropy, i);
757*eda14cbcSMatt Macy 
758*eda14cbcSMatt Macy 		spl_rand_jump(s);
759*eda14cbcSMatt Macy 
760*eda14cbcSMatt Macy 		wordp[0] = s[0];
761*eda14cbcSMatt Macy 		wordp[1] = s[1];
762*eda14cbcSMatt Macy 	}
763*eda14cbcSMatt Macy }
764*eda14cbcSMatt Macy 
765*eda14cbcSMatt Macy static void
766*eda14cbcSMatt Macy spl_random_fini(void)
767*eda14cbcSMatt Macy {
768*eda14cbcSMatt Macy 	free_percpu(spl_pseudo_entropy);
769*eda14cbcSMatt Macy }
770*eda14cbcSMatt Macy 
771*eda14cbcSMatt Macy static void
772*eda14cbcSMatt Macy spl_kvmem_fini(void)
773*eda14cbcSMatt Macy {
774*eda14cbcSMatt Macy 	spl_vmem_fini();
775*eda14cbcSMatt Macy 	spl_kmem_fini();
776*eda14cbcSMatt Macy }
777*eda14cbcSMatt Macy 
778*eda14cbcSMatt Macy static int __init
779*eda14cbcSMatt Macy spl_init(void)
780*eda14cbcSMatt Macy {
781*eda14cbcSMatt Macy 	int rc = 0;
782*eda14cbcSMatt Macy 
783*eda14cbcSMatt Macy 	bzero(&p0, sizeof (proc_t));
784*eda14cbcSMatt Macy 	spl_random_init();
785*eda14cbcSMatt Macy 
786*eda14cbcSMatt Macy 	if ((rc = spl_kvmem_init()))
787*eda14cbcSMatt Macy 		goto out1;
788*eda14cbcSMatt Macy 
789*eda14cbcSMatt Macy 	if ((rc = spl_tsd_init()))
790*eda14cbcSMatt Macy 		goto out2;
791*eda14cbcSMatt Macy 
792*eda14cbcSMatt Macy 	if ((rc = spl_taskq_init()))
793*eda14cbcSMatt Macy 		goto out3;
794*eda14cbcSMatt Macy 
795*eda14cbcSMatt Macy 	if ((rc = spl_kmem_cache_init()))
796*eda14cbcSMatt Macy 		goto out4;
797*eda14cbcSMatt Macy 
798*eda14cbcSMatt Macy 	if ((rc = spl_proc_init()))
799*eda14cbcSMatt Macy 		goto out5;
800*eda14cbcSMatt Macy 
801*eda14cbcSMatt Macy 	if ((rc = spl_kstat_init()))
802*eda14cbcSMatt Macy 		goto out6;
803*eda14cbcSMatt Macy 
804*eda14cbcSMatt Macy 	if ((rc = spl_zlib_init()))
805*eda14cbcSMatt Macy 		goto out7;
806*eda14cbcSMatt Macy 
807*eda14cbcSMatt Macy 	return (rc);
808*eda14cbcSMatt Macy 
809*eda14cbcSMatt Macy out7:
810*eda14cbcSMatt Macy 	spl_kstat_fini();
811*eda14cbcSMatt Macy out6:
812*eda14cbcSMatt Macy 	spl_proc_fini();
813*eda14cbcSMatt Macy out5:
814*eda14cbcSMatt Macy 	spl_kmem_cache_fini();
815*eda14cbcSMatt Macy out4:
816*eda14cbcSMatt Macy 	spl_taskq_fini();
817*eda14cbcSMatt Macy out3:
818*eda14cbcSMatt Macy 	spl_tsd_fini();
819*eda14cbcSMatt Macy out2:
820*eda14cbcSMatt Macy 	spl_kvmem_fini();
821*eda14cbcSMatt Macy out1:
822*eda14cbcSMatt Macy 	return (rc);
823*eda14cbcSMatt Macy }
824*eda14cbcSMatt Macy 
825*eda14cbcSMatt Macy static void __exit
826*eda14cbcSMatt Macy spl_fini(void)
827*eda14cbcSMatt Macy {
828*eda14cbcSMatt Macy 	spl_zlib_fini();
829*eda14cbcSMatt Macy 	spl_kstat_fini();
830*eda14cbcSMatt Macy 	spl_proc_fini();
831*eda14cbcSMatt Macy 	spl_kmem_cache_fini();
832*eda14cbcSMatt Macy 	spl_taskq_fini();
833*eda14cbcSMatt Macy 	spl_tsd_fini();
834*eda14cbcSMatt Macy 	spl_kvmem_fini();
835*eda14cbcSMatt Macy 	spl_random_fini();
836*eda14cbcSMatt Macy }
837*eda14cbcSMatt Macy 
838*eda14cbcSMatt Macy module_init(spl_init);
839*eda14cbcSMatt Macy module_exit(spl_fini);
840*eda14cbcSMatt Macy 
841*eda14cbcSMatt Macy ZFS_MODULE_DESCRIPTION("Solaris Porting Layer");
842*eda14cbcSMatt Macy ZFS_MODULE_AUTHOR(ZFS_META_AUTHOR);
843*eda14cbcSMatt Macy ZFS_MODULE_LICENSE("GPL");
844*eda14cbcSMatt Macy ZFS_MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
845