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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
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.
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
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 * Solaris Porting Layer (SPL) Generic Implementation.
25 \*****************************************************************************/
27 #include <sys/sysmacros.h>
28 #include <sys/systeminfo.h>
29 #include <sys/vmsystm.h>
30 #include <sys/vnode.h>
32 #include <sys/mutex.h>
33 #include <sys/rwlock.h>
34 #include <sys/taskq.h>
35 #include <sys/debug.h>
37 #include <sys/kstat.h>
38 #include <sys/utsname.h>
40 #include <linux/kmod.h>
41 #include <linux/proc_compat.h>
42 #include <spl-debug.h>
44 #ifdef SS_DEBUG_SUBSYS
45 #undef SS_DEBUG_SUBSYS
48 #define SS_DEBUG_SUBSYS SS_GENERIC
50 char spl_version[16] = "SPL v" SPL_META_VERSION;
51 EXPORT_SYMBOL(spl_version);
54 EXPORT_SYMBOL(spl_hostid);
56 char hw_serial[HW_HOSTID_LEN] = "<none>";
57 EXPORT_SYMBOL(hw_serial);
62 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
63 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON;
67 highbit(unsigned long i)
74 #if BITS_PER_LONG == 64
75 if (i & 0xffffffff00000000ul) {
96 EXPORT_SYMBOL(highbit);
98 #if BITS_PER_LONG == 32
100 * Support 64/64 => 64 division on a 32-bit platform. While the kernel
101 * provides a div64_u64() function for this we do not use it because the
102 * implementation is flawed. There are cases which return incorrect
103 * results as late as linux-2.6.35. Until this is fixed upstream the
104 * spl must provide its own implementation.
106 * This implementation is a slightly modified version of the algorithm
107 * proposed by the book 'Hacker's Delight'. The original source can be
108 * found here and is available for use without restriction.
110 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
114 * Calculate number of leading of zeros for a 64-bit value.
123 if (x <= 0x00000000FFFFFFFFULL) {n = n + 32; x = x << 32;}
124 if (x <= 0x0000FFFFFFFFFFFFULL) {n = n + 16; x = x << 16;}
125 if (x <= 0x00FFFFFFFFFFFFFFULL) {n = n + 8; x = x << 8;}
126 if (x <= 0x0FFFFFFFFFFFFFFFULL) {n = n + 4; x = x << 4;}
127 if (x <= 0x3FFFFFFFFFFFFFFFULL) {n = n + 2; x = x << 2;}
128 if (x <= 0x7FFFFFFFFFFFFFFFULL) {n = n + 1;}
134 * Newer kernels have a div_u64() function but we define our own
135 * to simplify portibility between kernel versions.
137 static inline uint64_t
138 __div_u64(uint64_t u, uint32_t v)
145 * Implementation of 64-bit unsigned division for 32-bit machines.
147 * First the procedure takes care of the case in which the divisor is a
148 * 32-bit quantity. There are two subcases: (1) If the left half of the
149 * dividend is less than the divisor, one execution of do_div() is all that
150 * is required (overflow is not possible). (2) Otherwise it does two
151 * divisions, using the grade school method.
154 __udivdi3(uint64_t u, uint64_t v)
156 uint64_t u0, u1, v1, q0, q1, k;
159 if (v >> 32 == 0) { // If v < 2**32:
160 if (u >> 32 < v) { // If u/v cannot overflow,
161 return __div_u64(u, v); // just do one division.
162 } else { // If u/v would overflow:
163 u1 = u >> 32; // Break u into two halves.
165 q1 = __div_u64(u1, v); // First quotient digit.
166 k = u1 - q1 * v; // First remainder, < v.
168 q0 = __div_u64(u0, v); // Seconds quotient digit.
169 return (q1 << 32) + q0;
171 } else { // If v >= 2**32:
172 n = nlz64(v); // 0 <= n <= 31.
173 v1 = (v << n) >> 32; // Normalize divisor, MSB is 1.
174 u1 = u >> 1; // To ensure no overflow.
175 q1 = __div_u64(u1, v1); // Get quotient from
176 q0 = (q1 << n) >> 31; // Undo normalization and
177 // division of u by 2.
178 if (q0 != 0) // Make q0 correct or
179 q0 = q0 - 1; // too small by 1.
180 if ((u - q0 * v) >= v)
181 q0 = q0 + 1; // Now q0 is correct.
186 EXPORT_SYMBOL(__udivdi3);
189 * Implementation of 64-bit signed division for 32-bit machines.
192 __divdi3(int64_t u, int64_t v)
195 q = __udivdi3(abs64(u), abs64(v));
196 t = (u ^ v) >> 63; // If u, v have different
197 return (q ^ t) - t; // signs, negate q.
199 EXPORT_SYMBOL(__divdi3);
202 * Implementation of 64-bit unsigned modulo for 32-bit machines.
205 __umoddi3(uint64_t dividend, uint64_t divisor)
207 return (dividend - (divisor * __udivdi3(dividend, divisor)));
209 EXPORT_SYMBOL(__umoddi3);
211 #endif /* BITS_PER_LONG */
213 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
214 * ddi_strtol(9F) man page. I have not verified the behavior of these
215 * functions against their Solaris counterparts. It is possible that I
216 * may have misinterpreted the man page or the man page is incorrect.
218 int ddi_strtoul(const char *, char **, int, unsigned long *);
219 int ddi_strtol(const char *, char **, int, long *);
220 int ddi_strtoull(const char *, char **, int, unsigned long long *);
221 int ddi_strtoll(const char *, char **, int, long long *);
223 #define define_ddi_strtoux(type, valtype) \
224 int ddi_strtou##type(const char *str, char **endptr, \
225 int base, valtype *result) \
227 valtype last_value, value = 0; \
228 char *ptr = (char *)str; \
229 int flag = 1, digit; \
231 if (strlen(ptr) == 0) \
234 /* Auto-detect base based on prefix */ \
236 if (str[0] == '0') { \
237 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
238 base = 16; /* hex */ \
240 } else if (str[1] >= '0' && str[1] < 8) { \
241 base = 8; /* octal */ \
247 base = 10; /* decimal */ \
253 digit = *ptr - '0'; \
254 else if (isalpha(*ptr)) \
255 digit = tolower(*ptr) - 'a' + 10; \
262 last_value = value; \
263 value = value * base + digit; \
264 if (last_value > value) /* Overflow */ \
275 *endptr = (char *)(flag ? ptr : str); \
280 #define define_ddi_strtox(type, valtype) \
281 int ddi_strto##type(const char *str, char **endptr, \
282 int base, valtype *result) \
287 rc = ddi_strtou##type(str + 1, endptr, base, result); \
289 if (*endptr == str + 1) \
290 *endptr = (char *)str; \
292 *result = -*result; \
295 rc = ddi_strtou##type(str, endptr, base, result); \
301 define_ddi_strtoux(l, unsigned long)
302 define_ddi_strtox(l, long)
303 define_ddi_strtoux(ll, unsigned long long)
304 define_ddi_strtox(ll, long long)
306 EXPORT_SYMBOL(ddi_strtoul);
307 EXPORT_SYMBOL(ddi_strtol);
308 EXPORT_SYMBOL(ddi_strtoll);
309 EXPORT_SYMBOL(ddi_strtoull);
312 ddi_copyin(const void *from, void *to, size_t len, int flags)
314 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
315 if (flags & FKIOCTL) {
316 memcpy(to, from, len);
320 return copyin(from, to, len);
322 EXPORT_SYMBOL(ddi_copyin);
325 ddi_copyout(const void *from, void *to, size_t len, int flags)
327 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
328 if (flags & FKIOCTL) {
329 memcpy(to, from, len);
333 return copyout(from, to, len);
335 EXPORT_SYMBOL(ddi_copyout);
337 #ifndef HAVE_PUT_TASK_STRUCT
339 * This is only a stub function which should never be used. The SPL should
340 * never be putting away the last reference on a task structure so this will
341 * not be called. However, we still need to define it so the module does not
342 * have undefined symbol at load time. That all said if this impossible
343 * thing does somehow happen PANIC immediately so we know about it.
346 __put_task_struct(struct task_struct *t)
348 PANIC("Unexpectly put last reference on task %d\n", (int)t->pid);
350 EXPORT_SYMBOL(__put_task_struct);
351 #endif /* HAVE_PUT_TASK_STRUCT */
353 struct new_utsname *__utsname(void)
355 #ifdef HAVE_INIT_UTSNAME
356 return init_utsname();
358 return &system_utsname;
361 EXPORT_SYMBOL(__utsname);
366 char sh_path[] = "/bin/sh";
367 char *argv[] = { sh_path,
369 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
371 char *envp[] = { "HOME=/",
373 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
377 /* Doing address resolution in the kernel is tricky and just
378 * not a good idea in general. So to set the proper 'hw_serial'
379 * use the usermodehelper support to ask '/bin/sh' to run
380 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
381 * for us to use. It's a horrific solution but it will do for now.
383 rc = call_usermodehelper(sh_path, argv, envp, 1);
385 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
386 argv[0], argv[1], argv[2], rc);
392 zone_get_hostid(void *zone)
394 unsigned long hostid;
396 /* Only the global zone is supported */
397 ASSERT(zone == NULL);
399 if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0)
400 return HW_INVALID_HOSTID;
402 return (uint32_t)hostid;
404 EXPORT_SYMBOL(zone_get_hostid);
406 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
408 * Because kallsyms_lookup_name() is no longer exported in the
409 * mainline kernel we are forced to resort to somewhat drastic
410 * measures. This function replaces the functionality by performing
411 * an upcall to user space where /proc/kallsyms is consulted for
412 * the requested address.
414 #define GET_KALLSYMS_ADDR_CMD \
415 "gawk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
416 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
419 set_kallsyms_lookup_name(void)
421 char sh_path[] = "/bin/sh";
422 char *argv[] = { sh_path,
424 GET_KALLSYMS_ADDR_CMD,
426 char *envp[] = { "HOME=/",
428 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
432 rc = call_usermodehelper(sh_path, argv, envp, 1);
434 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
435 argv[0], argv[1], argv[2], rc);
442 __init spl_init(void)
446 if ((rc = debug_init()))
449 if ((rc = spl_kmem_init()))
452 if ((rc = spl_mutex_init()))
455 if ((rc = spl_rw_init()))
458 if ((rc = spl_taskq_init()))
461 if ((rc = vn_init()))
464 if ((rc = proc_init()))
467 if ((rc = kstat_init()))
470 if ((rc = set_hostid()))
471 SGOTO(out8, rc = -EADDRNOTAVAIL);
473 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
474 if ((rc = set_kallsyms_lookup_name()))
475 SGOTO(out8, rc = -EADDRNOTAVAIL);
476 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
478 if ((rc = spl_kmem_init_kallsyms_lookup()))
481 printk("SPL: Loaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
500 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
501 "rc = %d\n", SPL_META_VERSION, rc);
510 printk("SPL: Unloaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
521 /* Called when a dependent module is loaded */
528 * At module load time the pwd is set to '/' on a Solaris system.
529 * On a Linux system will be set to whatever directory the caller
530 * was in when executing insmod/modprobe.
532 rc = vn_set_pwd("/");
534 printk("SPL: Warning unable to set pwd to '/': %d\n", rc);
536 EXPORT_SYMBOL(spl_setup);
538 /* Called when a dependent module is unloaded */
543 EXPORT_SYMBOL(spl_cleanup);
545 module_init(spl_init);
546 module_exit(spl_fini);
548 MODULE_AUTHOR("Lawrence Livermore National Labs");
549 MODULE_DESCRIPTION("Solaris Porting Layer");
550 MODULE_LICENSE("GPL");