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>
36 #include <sys/debug.h>
38 #include <sys/kstat.h>
39 #include <sys/utsname.h>
41 #include <linux/kmod.h>
42 #include <linux/proc_compat.h>
43 #include <spl-debug.h>
45 #ifdef SS_DEBUG_SUBSYS
46 #undef SS_DEBUG_SUBSYS
49 #define SS_DEBUG_SUBSYS SS_GENERIC
51 char spl_version
[16] = "SPL v" SPL_META_VERSION
;
52 EXPORT_SYMBOL(spl_version
);
55 EXPORT_SYMBOL(spl_hostid
);
57 char hw_serial
[HW_HOSTID_LEN
] = "<none>";
58 EXPORT_SYMBOL(hw_serial
);
63 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
64 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn
= SYMBOL_POISON
;
68 highbit(unsigned long i
)
75 #if BITS_PER_LONG == 64
76 if (i
& 0xffffffff00000000ul
) {
97 EXPORT_SYMBOL(highbit
);
99 #if BITS_PER_LONG == 32
101 * Support 64/64 => 64 division on a 32-bit platform. While the kernel
102 * provides a div64_u64() function for this we do not use it because the
103 * implementation is flawed. There are cases which return incorrect
104 * results as late as linux-2.6.35. Until this is fixed upstream the
105 * spl must provide its own implementation.
107 * This implementation is a slightly modified version of the algorithm
108 * proposed by the book 'Hacker's Delight'. The original source can be
109 * found here and is available for use without restriction.
111 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
115 * Calculate number of leading of zeros for a 64-bit value.
124 if (x
<= 0x00000000FFFFFFFFULL
) {n
= n
+ 32; x
= x
<< 32;}
125 if (x
<= 0x0000FFFFFFFFFFFFULL
) {n
= n
+ 16; x
= x
<< 16;}
126 if (x
<= 0x00FFFFFFFFFFFFFFULL
) {n
= n
+ 8; x
= x
<< 8;}
127 if (x
<= 0x0FFFFFFFFFFFFFFFULL
) {n
= n
+ 4; x
= x
<< 4;}
128 if (x
<= 0x3FFFFFFFFFFFFFFFULL
) {n
= n
+ 2; x
= x
<< 2;}
129 if (x
<= 0x7FFFFFFFFFFFFFFFULL
) {n
= n
+ 1;}
135 * Newer kernels have a div_u64() function but we define our own
136 * to simplify portibility between kernel versions.
138 static inline uint64_t
139 __div_u64(uint64_t u
, uint32_t v
)
146 * Implementation of 64-bit unsigned division for 32-bit machines.
148 * First the procedure takes care of the case in which the divisor is a
149 * 32-bit quantity. There are two subcases: (1) If the left half of the
150 * dividend is less than the divisor, one execution of do_div() is all that
151 * is required (overflow is not possible). (2) Otherwise it does two
152 * divisions, using the grade school method.
155 __udivdi3(uint64_t u
, uint64_t v
)
157 uint64_t u0
, u1
, v1
, q0
, q1
, k
;
160 if (v
>> 32 == 0) { // If v < 2**32:
161 if (u
>> 32 < v
) { // If u/v cannot overflow,
162 return __div_u64(u
, v
); // just do one division.
163 } else { // If u/v would overflow:
164 u1
= u
>> 32; // Break u into two halves.
166 q1
= __div_u64(u1
, v
); // First quotient digit.
167 k
= u1
- q1
* v
; // First remainder, < v.
169 q0
= __div_u64(u0
, v
); // Seconds quotient digit.
170 return (q1
<< 32) + q0
;
172 } else { // If v >= 2**32:
173 n
= nlz64(v
); // 0 <= n <= 31.
174 v1
= (v
<< n
) >> 32; // Normalize divisor, MSB is 1.
175 u1
= u
>> 1; // To ensure no overflow.
176 q1
= __div_u64(u1
, v1
); // Get quotient from
177 q0
= (q1
<< n
) >> 31; // Undo normalization and
178 // division of u by 2.
179 if (q0
!= 0) // Make q0 correct or
180 q0
= q0
- 1; // too small by 1.
181 if ((u
- q0
* v
) >= v
)
182 q0
= q0
+ 1; // Now q0 is correct.
187 EXPORT_SYMBOL(__udivdi3
);
190 * Implementation of 64-bit signed division for 32-bit machines.
193 __divdi3(int64_t u
, int64_t v
)
196 q
= __udivdi3(abs64(u
), abs64(v
));
197 t
= (u
^ v
) >> 63; // If u, v have different
198 return (q
^ t
) - t
; // signs, negate q.
200 EXPORT_SYMBOL(__divdi3
);
203 * Implementation of 64-bit unsigned modulo for 32-bit machines.
206 __umoddi3(uint64_t dividend
, uint64_t divisor
)
208 return (dividend
- (divisor
* __udivdi3(dividend
, divisor
)));
210 EXPORT_SYMBOL(__umoddi3
);
212 #endif /* BITS_PER_LONG */
214 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
215 * ddi_strtol(9F) man page. I have not verified the behavior of these
216 * functions against their Solaris counterparts. It is possible that I
217 * may have misinterpreted the man page or the man page is incorrect.
219 int ddi_strtoul(const char *, char **, int, unsigned long *);
220 int ddi_strtol(const char *, char **, int, long *);
221 int ddi_strtoull(const char *, char **, int, unsigned long long *);
222 int ddi_strtoll(const char *, char **, int, long long *);
224 #define define_ddi_strtoux(type, valtype) \
225 int ddi_strtou##type(const char *str, char **endptr, \
226 int base, valtype *result) \
228 valtype last_value, value = 0; \
229 char *ptr = (char *)str; \
230 int flag = 1, digit; \
232 if (strlen(ptr) == 0) \
235 /* Auto-detect base based on prefix */ \
237 if (str[0] == '0') { \
238 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
239 base = 16; /* hex */ \
241 } else if (str[1] >= '0' && str[1] < 8) { \
242 base = 8; /* octal */ \
248 base = 10; /* decimal */ \
254 digit = *ptr - '0'; \
255 else if (isalpha(*ptr)) \
256 digit = tolower(*ptr) - 'a' + 10; \
263 last_value = value; \
264 value = value * base + digit; \
265 if (last_value > value) /* Overflow */ \
276 *endptr = (char *)(flag ? ptr : str); \
281 #define define_ddi_strtox(type, valtype) \
282 int ddi_strto##type(const char *str, char **endptr, \
283 int base, valtype *result) \
288 rc = ddi_strtou##type(str + 1, endptr, base, result); \
290 if (*endptr == str + 1) \
291 *endptr = (char *)str; \
293 *result = -*result; \
296 rc = ddi_strtou##type(str, endptr, base, result); \
302 define_ddi_strtoux(l
, unsigned long)
303 define_ddi_strtox(l
, long)
304 define_ddi_strtoux(ll
, unsigned long long)
305 define_ddi_strtox(ll
, long long)
307 EXPORT_SYMBOL(ddi_strtoul
);
308 EXPORT_SYMBOL(ddi_strtol
);
309 EXPORT_SYMBOL(ddi_strtoll
);
310 EXPORT_SYMBOL(ddi_strtoull
);
313 ddi_copyin(const void *from
, void *to
, size_t len
, int flags
)
315 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
316 if (flags
& FKIOCTL
) {
317 memcpy(to
, from
, len
);
321 return copyin(from
, to
, len
);
323 EXPORT_SYMBOL(ddi_copyin
);
326 ddi_copyout(const void *from
, void *to
, size_t len
, int flags
)
328 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
329 if (flags
& FKIOCTL
) {
330 memcpy(to
, from
, len
);
334 return copyout(from
, to
, len
);
336 EXPORT_SYMBOL(ddi_copyout
);
338 #ifndef HAVE_PUT_TASK_STRUCT
340 * This is only a stub function which should never be used. The SPL should
341 * never be putting away the last reference on a task structure so this will
342 * not be called. However, we still need to define it so the module does not
343 * have undefined symbol at load time. That all said if this impossible
344 * thing does somehow happen PANIC immediately so we know about it.
347 __put_task_struct(struct task_struct
*t
)
349 PANIC("Unexpectly put last reference on task %d\n", (int)t
->pid
);
351 EXPORT_SYMBOL(__put_task_struct
);
352 #endif /* HAVE_PUT_TASK_STRUCT */
354 struct new_utsname
*__utsname(void)
356 #ifdef HAVE_INIT_UTSNAME
357 return init_utsname();
359 return &system_utsname
;
362 EXPORT_SYMBOL(__utsname
);
367 char sh_path
[] = "/bin/sh";
368 char *argv
[] = { sh_path
,
370 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
372 char *envp
[] = { "HOME=/",
374 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
378 /* Doing address resolution in the kernel is tricky and just
379 * not a good idea in general. So to set the proper 'hw_serial'
380 * use the usermodehelper support to ask '/bin/sh' to run
381 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
382 * for us to use. It's a horrific solution but it will do for now.
384 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
386 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
387 argv
[0], argv
[1], argv
[2], rc
);
393 zone_get_hostid(void *zone
)
395 unsigned long hostid
;
397 /* Only the global zone is supported */
398 ASSERT(zone
== NULL
);
400 if (ddi_strtoul(hw_serial
, NULL
, HW_HOSTID_LEN
-1, &hostid
) != 0)
401 return HW_INVALID_HOSTID
;
403 return (uint32_t)hostid
;
405 EXPORT_SYMBOL(zone_get_hostid
);
407 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
409 * Because kallsyms_lookup_name() is no longer exported in the
410 * mainline kernel we are forced to resort to somewhat drastic
411 * measures. This function replaces the functionality by performing
412 * an upcall to user space where /proc/kallsyms is consulted for
413 * the requested address.
415 #define GET_KALLSYMS_ADDR_CMD \
416 "gawk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
417 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
420 set_kallsyms_lookup_name(void)
422 char sh_path
[] = "/bin/sh";
423 char *argv
[] = { sh_path
,
425 GET_KALLSYMS_ADDR_CMD
,
427 char *envp
[] = { "HOME=/",
429 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
433 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
435 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
436 argv
[0], argv
[1], argv
[2], rc
);
443 __init
spl_init(void)
447 if ((rc
= debug_init()))
450 if ((rc
= spl_kmem_init()))
453 if ((rc
= spl_mutex_init()))
456 if ((rc
= spl_rw_init()))
459 if ((rc
= spl_taskq_init()))
462 if ((rc
= vn_init()))
465 if ((rc
= proc_init()))
468 if ((rc
= kstat_init()))
471 if ((rc
= tsd_init()))
474 if ((rc
= set_hostid()))
475 SGOTO(out9
, rc
= -EADDRNOTAVAIL
);
477 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
478 if ((rc
= set_kallsyms_lookup_name()))
479 SGOTO(out9
, rc
= -EADDRNOTAVAIL
);
480 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
482 if ((rc
= spl_kmem_init_kallsyms_lookup()))
485 printk(KERN_NOTICE
"SPL: Loaded Solaris Porting Layer v%s%s\n",
486 SPL_META_VERSION
, SPL_DEBUG_STR
);
507 printk(KERN_NOTICE
"SPL: Failed to Load Solaris Porting Layer v%s%s"
508 ", rc = %d\n", SPL_META_VERSION
, SPL_DEBUG_STR
, rc
);
517 printk(KERN_NOTICE
"SPL: Unloaded Solaris Porting Layer v%s%s\n",
518 SPL_META_VERSION
, SPL_DEBUG_STR
);
530 /* Called when a dependent module is loaded */
537 * At module load time the pwd is set to '/' on a Solaris system.
538 * On a Linux system will be set to whatever directory the caller
539 * was in when executing insmod/modprobe.
541 rc
= vn_set_pwd("/");
543 printk("SPL: Warning unable to set pwd to '/': %d\n", rc
);
545 EXPORT_SYMBOL(spl_setup
);
547 /* Called when a dependent module is unloaded */
552 EXPORT_SYMBOL(spl_cleanup
);
554 module_init(spl_init
);
555 module_exit(spl_fini
);
557 MODULE_AUTHOR("Lawrence Livermore National Labs");
558 MODULE_DESCRIPTION("Solaris Porting Layer");
559 MODULE_LICENSE("GPL");