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