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>
37 #include <sys/debug.h>
39 #include <sys/kstat.h>
40 #include <sys/utsname.h>
42 #include <linux/kmod.h>
43 #include <linux/proc_compat.h>
44 #include <spl-debug.h>
46 #ifdef SS_DEBUG_SUBSYS
47 #undef SS_DEBUG_SUBSYS
50 #define SS_DEBUG_SUBSYS SS_GENERIC
52 char spl_version
[16] = "SPL v" SPL_META_VERSION
;
53 EXPORT_SYMBOL(spl_version
);
56 EXPORT_SYMBOL(spl_hostid
);
58 char hw_serial
[HW_HOSTID_LEN
] = "<none>";
59 EXPORT_SYMBOL(hw_serial
);
64 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
65 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn
= SYMBOL_POISON
;
69 highbit(unsigned long i
)
76 #if BITS_PER_LONG == 64
77 if (i
& 0xffffffff00000000ul
) {
98 EXPORT_SYMBOL(highbit
);
100 #if BITS_PER_LONG == 32
102 * Support 64/64 => 64 division on a 32-bit platform. While the kernel
103 * provides a div64_u64() function for this we do not use it because the
104 * implementation is flawed. There are cases which return incorrect
105 * results as late as linux-2.6.35. Until this is fixed upstream the
106 * spl must provide its own implementation.
108 * This implementation is a slightly modified version of the algorithm
109 * proposed by the book 'Hacker's Delight'. The original source can be
110 * found here and is available for use without restriction.
112 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
116 * Calculate number of leading of zeros for a 64-bit value.
125 if (x
<= 0x00000000FFFFFFFFULL
) {n
= n
+ 32; x
= x
<< 32;}
126 if (x
<= 0x0000FFFFFFFFFFFFULL
) {n
= n
+ 16; x
= x
<< 16;}
127 if (x
<= 0x00FFFFFFFFFFFFFFULL
) {n
= n
+ 8; x
= x
<< 8;}
128 if (x
<= 0x0FFFFFFFFFFFFFFFULL
) {n
= n
+ 4; x
= x
<< 4;}
129 if (x
<= 0x3FFFFFFFFFFFFFFFULL
) {n
= n
+ 2; x
= x
<< 2;}
130 if (x
<= 0x7FFFFFFFFFFFFFFFULL
) {n
= n
+ 1;}
136 * Newer kernels have a div_u64() function but we define our own
137 * to simplify portibility between kernel versions.
139 static inline uint64_t
140 __div_u64(uint64_t u
, uint32_t v
)
147 * Implementation of 64-bit unsigned division for 32-bit machines.
149 * First the procedure takes care of the case in which the divisor is a
150 * 32-bit quantity. There are two subcases: (1) If the left half of the
151 * dividend is less than the divisor, one execution of do_div() is all that
152 * is required (overflow is not possible). (2) Otherwise it does two
153 * divisions, using the grade school method.
156 __udivdi3(uint64_t u
, uint64_t v
)
158 uint64_t u0
, u1
, v1
, q0
, q1
, k
;
161 if (v
>> 32 == 0) { // If v < 2**32:
162 if (u
>> 32 < v
) { // If u/v cannot overflow,
163 return __div_u64(u
, v
); // just do one division.
164 } else { // If u/v would overflow:
165 u1
= u
>> 32; // Break u into two halves.
167 q1
= __div_u64(u1
, v
); // First quotient digit.
168 k
= u1
- q1
* v
; // First remainder, < v.
170 q0
= __div_u64(u0
, v
); // Seconds quotient digit.
171 return (q1
<< 32) + q0
;
173 } else { // If v >= 2**32:
174 n
= nlz64(v
); // 0 <= n <= 31.
175 v1
= (v
<< n
) >> 32; // Normalize divisor, MSB is 1.
176 u1
= u
>> 1; // To ensure no overflow.
177 q1
= __div_u64(u1
, v1
); // Get quotient from
178 q0
= (q1
<< n
) >> 31; // Undo normalization and
179 // division of u by 2.
180 if (q0
!= 0) // Make q0 correct or
181 q0
= q0
- 1; // too small by 1.
182 if ((u
- q0
* v
) >= v
)
183 q0
= q0
+ 1; // Now q0 is correct.
188 EXPORT_SYMBOL(__udivdi3
);
191 * Implementation of 64-bit signed division for 32-bit machines.
194 __divdi3(int64_t u
, int64_t v
)
197 q
= __udivdi3(abs64(u
), abs64(v
));
198 t
= (u
^ v
) >> 63; // If u, v have different
199 return (q
^ t
) - t
; // signs, negate q.
201 EXPORT_SYMBOL(__divdi3
);
204 * Implementation of 64-bit unsigned modulo for 32-bit machines.
207 __umoddi3(uint64_t dividend
, uint64_t divisor
)
209 return (dividend
- (divisor
* __udivdi3(dividend
, divisor
)));
211 EXPORT_SYMBOL(__umoddi3
);
213 #endif /* BITS_PER_LONG */
215 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
216 * ddi_strtol(9F) man page. I have not verified the behavior of these
217 * functions against their Solaris counterparts. It is possible that I
218 * may have misinterpreted the man page or the man page is incorrect.
220 int ddi_strtoul(const char *, char **, int, unsigned long *);
221 int ddi_strtol(const char *, char **, int, long *);
222 int ddi_strtoull(const char *, char **, int, unsigned long long *);
223 int ddi_strtoll(const char *, char **, int, long long *);
225 #define define_ddi_strtoux(type, valtype) \
226 int ddi_strtou##type(const char *str, char **endptr, \
227 int base, valtype *result) \
229 valtype last_value, value = 0; \
230 char *ptr = (char *)str; \
231 int flag = 1, digit; \
233 if (strlen(ptr) == 0) \
236 /* Auto-detect base based on prefix */ \
238 if (str[0] == '0') { \
239 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
240 base = 16; /* hex */ \
242 } else if (str[1] >= '0' && str[1] < 8) { \
243 base = 8; /* octal */ \
249 base = 10; /* decimal */ \
255 digit = *ptr - '0'; \
256 else if (isalpha(*ptr)) \
257 digit = tolower(*ptr) - 'a' + 10; \
264 last_value = value; \
265 value = value * base + digit; \
266 if (last_value > value) /* Overflow */ \
277 *endptr = (char *)(flag ? ptr : str); \
282 #define define_ddi_strtox(type, valtype) \
283 int ddi_strto##type(const char *str, char **endptr, \
284 int base, valtype *result) \
289 rc = ddi_strtou##type(str + 1, endptr, base, result); \
291 if (*endptr == str + 1) \
292 *endptr = (char *)str; \
294 *result = -*result; \
297 rc = ddi_strtou##type(str, endptr, base, result); \
303 define_ddi_strtoux(l
, unsigned long)
304 define_ddi_strtox(l
, long)
305 define_ddi_strtoux(ll
, unsigned long long)
306 define_ddi_strtox(ll
, long long)
308 EXPORT_SYMBOL(ddi_strtoul
);
309 EXPORT_SYMBOL(ddi_strtol
);
310 EXPORT_SYMBOL(ddi_strtoll
);
311 EXPORT_SYMBOL(ddi_strtoull
);
314 ddi_copyin(const void *from
, void *to
, size_t len
, int flags
)
316 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
317 if (flags
& FKIOCTL
) {
318 memcpy(to
, from
, len
);
322 return copyin(from
, to
, len
);
324 EXPORT_SYMBOL(ddi_copyin
);
327 ddi_copyout(const void *from
, void *to
, size_t len
, int flags
)
329 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
330 if (flags
& FKIOCTL
) {
331 memcpy(to
, from
, len
);
335 return copyout(from
, to
, len
);
337 EXPORT_SYMBOL(ddi_copyout
);
339 #ifndef HAVE_PUT_TASK_STRUCT
341 * This is only a stub function which should never be used. The SPL should
342 * never be putting away the last reference on a task structure so this will
343 * not be called. However, we still need to define it so the module does not
344 * have undefined symbol at load time. That all said if this impossible
345 * thing does somehow happen PANIC immediately so we know about it.
348 __put_task_struct(struct task_struct
*t
)
350 PANIC("Unexpectly put last reference on task %d\n", (int)t
->pid
);
352 EXPORT_SYMBOL(__put_task_struct
);
353 #endif /* HAVE_PUT_TASK_STRUCT */
355 struct new_utsname
*__utsname(void)
357 #ifdef HAVE_INIT_UTSNAME
358 return init_utsname();
360 return &system_utsname
;
363 EXPORT_SYMBOL(__utsname
);
368 char sh_path
[] = "/bin/sh";
369 char *argv
[] = { sh_path
,
371 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
373 char *envp
[] = { "HOME=/",
375 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
379 /* Doing address resolution in the kernel is tricky and just
380 * not a good idea in general. So to set the proper 'hw_serial'
381 * use the usermodehelper support to ask '/bin/sh' to run
382 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
383 * for us to use. It's a horrific solution but it will do for now.
385 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
387 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
388 argv
[0], argv
[1], argv
[2], rc
);
394 zone_get_hostid(void *zone
)
396 unsigned long hostid
;
398 /* Only the global zone is supported */
399 ASSERT(zone
== NULL
);
401 if (ddi_strtoul(hw_serial
, NULL
, HW_HOSTID_LEN
-1, &hostid
) != 0)
402 return HW_INVALID_HOSTID
;
404 return (uint32_t)hostid
;
406 EXPORT_SYMBOL(zone_get_hostid
);
408 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
410 * Because kallsyms_lookup_name() is no longer exported in the
411 * mainline kernel we are forced to resort to somewhat drastic
412 * measures. This function replaces the functionality by performing
413 * an upcall to user space where /proc/kallsyms is consulted for
414 * the requested address.
416 #define GET_KALLSYMS_ADDR_CMD \
417 "gawk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
418 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
421 set_kallsyms_lookup_name(void)
423 char sh_path
[] = "/bin/sh";
424 char *argv
[] = { sh_path
,
426 GET_KALLSYMS_ADDR_CMD
,
428 char *envp
[] = { "HOME=/",
430 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
434 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
436 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
437 argv
[0], argv
[1], argv
[2], rc
);
444 __init
spl_init(void)
448 if ((rc
= debug_init()))
451 if ((rc
= spl_kmem_init()))
454 if ((rc
= spl_mutex_init()))
457 if ((rc
= spl_rw_init()))
460 if ((rc
= spl_taskq_init()))
463 if ((rc
= vn_init()))
466 if ((rc
= proc_init()))
469 if ((rc
= kstat_init()))
472 if ((rc
= tsd_init()))
475 if ((rc
= zlib_init()))
478 if ((rc
= set_hostid()))
479 SGOTO(out10
, rc
= -EADDRNOTAVAIL
);
481 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
482 if ((rc
= set_kallsyms_lookup_name()))
483 SGOTO(out10
, rc
= -EADDRNOTAVAIL
);
484 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
486 if ((rc
= spl_kmem_init_kallsyms_lookup()))
489 printk(KERN_NOTICE
"SPL: Loaded Solaris Porting Layer v%s%s\n",
490 SPL_META_VERSION
, SPL_DEBUG_STR
);
513 printk(KERN_NOTICE
"SPL: Failed to Load Solaris Porting Layer v%s%s"
514 ", rc = %d\n", SPL_META_VERSION
, SPL_DEBUG_STR
, rc
);
523 printk(KERN_NOTICE
"SPL: Unloaded Solaris Porting Layer v%s%s\n",
524 SPL_META_VERSION
, SPL_DEBUG_STR
);
537 /* Called when a dependent module is loaded */
544 * At module load time the pwd is set to '/' on a Solaris system.
545 * On a Linux system will be set to whatever directory the caller
546 * was in when executing insmod/modprobe.
548 rc
= vn_set_pwd("/");
550 printk("SPL: Warning unable to set pwd to '/': %d\n", rc
);
552 EXPORT_SYMBOL(spl_setup
);
554 /* Called when a dependent module is unloaded */
559 EXPORT_SYMBOL(spl_cleanup
);
561 module_init(spl_init
);
562 module_exit(spl_fini
);
564 MODULE_AUTHOR("Lawrence Livermore National Labs");
565 MODULE_DESCRIPTION("Solaris Porting Layer");
566 MODULE_LICENSE("GPL");