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>
43 #ifdef DEBUG_SUBSYSTEM
44 #undef DEBUG_SUBSYSTEM
47 #define DEBUG_SUBSYSTEM S_GENERIC
49 char spl_version[16] = "SPL v" SPL_META_VERSION;
50 EXPORT_SYMBOL(spl_version);
53 EXPORT_SYMBOL(spl_hostid);
55 char hw_serial[HW_HOSTID_LEN] = "<none>";
56 EXPORT_SYMBOL(hw_serial);
61 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
62 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON;
66 highbit(unsigned long i)
73 #if BITS_PER_LONG == 64
74 if (i & 0xffffffff00000000ul) {
95 EXPORT_SYMBOL(highbit);
98 * Implementation of 64 bit division for 32-bit machines.
100 #if BITS_PER_LONG == 32
102 __udivdi3(uint64_t dividend, uint64_t divisor)
104 #if defined(HAVE_DIV64_64) /* 2.6.22 - 2.6.25 API */
105 return div64_64(dividend, divisor);
106 #elif defined(HAVE_DIV64_U64) /* 2.6.26 - 2.6.x API */
107 return div64_u64(dividend, divisor);
109 /* Implementation from 2.6.30 kernel */
112 high = divisor >> 32;
114 unsigned int shift = fls(high);
116 d = divisor >> shift;
124 #endif /* HAVE_DIV64_64, HAVE_DIV64_U64 */
126 EXPORT_SYMBOL(__udivdi3);
129 * Implementation of 64 bit modulo for 32-bit machines.
132 __umoddi3(uint64_t dividend, uint64_t divisor)
134 return (dividend - (divisor * __udivdi3(dividend, divisor)));
136 EXPORT_SYMBOL(__umoddi3);
137 #endif /* BITS_PER_LONG */
139 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
140 * ddi_strtol(9F) man page. I have not verified the behavior of these
141 * functions against their Solaris counterparts. It is possible that I
142 * may have misinterpreted the man page or the man page is incorrect.
144 int ddi_strtoul(const char *, char **, int, unsigned long *);
145 int ddi_strtol(const char *, char **, int, long *);
146 int ddi_strtoull(const char *, char **, int, unsigned long long *);
147 int ddi_strtoll(const char *, char **, int, long long *);
149 #define define_ddi_strtoux(type, valtype) \
150 int ddi_strtou##type(const char *str, char **endptr, \
151 int base, valtype *result) \
153 valtype last_value, value = 0; \
154 char *ptr = (char *)str; \
155 int flag = 1, digit; \
157 if (strlen(ptr) == 0) \
160 /* Auto-detect base based on prefix */ \
162 if (str[0] == '0') { \
163 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
164 base = 16; /* hex */ \
166 } else if (str[1] >= '0' && str[1] < 8) { \
167 base = 8; /* octal */ \
173 base = 10; /* decimal */ \
179 digit = *ptr - '0'; \
180 else if (isalpha(*ptr)) \
181 digit = tolower(*ptr) - 'a' + 10; \
188 last_value = value; \
189 value = value * base + digit; \
190 if (last_value > value) /* Overflow */ \
201 *endptr = (char *)(flag ? ptr : str); \
206 #define define_ddi_strtox(type, valtype) \
207 int ddi_strto##type(const char *str, char **endptr, \
208 int base, valtype *result) \
213 rc = ddi_strtou##type(str + 1, endptr, base, result); \
215 if (*endptr == str + 1) \
216 *endptr = (char *)str; \
218 *result = -*result; \
221 rc = ddi_strtou##type(str, endptr, base, result); \
227 define_ddi_strtoux(l, unsigned long)
228 define_ddi_strtox(l, long)
229 define_ddi_strtoux(ll, unsigned long long)
230 define_ddi_strtox(ll, long long)
232 EXPORT_SYMBOL(ddi_strtoul);
233 EXPORT_SYMBOL(ddi_strtol);
234 EXPORT_SYMBOL(ddi_strtoll);
235 EXPORT_SYMBOL(ddi_strtoull);
238 ddi_copyin(const void *from, void *to, size_t len, int flags)
240 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
241 if (flags & FKIOCTL) {
242 memcpy(to, from, len);
246 return copyin(from, to, len);
248 EXPORT_SYMBOL(ddi_copyin);
251 ddi_copyout(const void *from, void *to, size_t len, int flags)
253 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
254 if (flags & FKIOCTL) {
255 memcpy(to, from, len);
259 return copyout(from, to, len);
261 EXPORT_SYMBOL(ddi_copyout);
263 #ifndef HAVE_PUT_TASK_STRUCT
265 * This is only a stub function which should never be used. The SPL should
266 * never be putting away the last reference on a task structure so this will
267 * not be called. However, we still need to define it so the module does not
268 * have undefined symbol at load time. That all said if this impossible
269 * thing does somehow happen SBUG() immediately so we know about it.
272 __put_task_struct(struct task_struct *t)
276 EXPORT_SYMBOL(__put_task_struct);
277 #endif /* HAVE_PUT_TASK_STRUCT */
279 struct new_utsname *__utsname(void)
281 #ifdef HAVE_INIT_UTSNAME
282 return init_utsname();
284 return &system_utsname;
287 EXPORT_SYMBOL(__utsname);
292 char sh_path[] = "/bin/sh";
293 char *argv[] = { sh_path,
295 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
297 char *envp[] = { "HOME=/",
299 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
303 /* Doing address resolution in the kernel is tricky and just
304 * not a good idea in general. So to set the proper 'hw_serial'
305 * use the usermodehelper support to ask '/bin/sh' to run
306 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
307 * for us to use. It's a horrific solution but it will do for now.
309 rc = call_usermodehelper(sh_path, argv, envp, 1);
311 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
312 argv[0], argv[1], argv[2], rc);
318 zone_get_hostid(void *zone)
320 unsigned long hostid;
322 /* Only the global zone is supported */
323 ASSERT(zone == NULL);
325 if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0)
326 return HW_INVALID_HOSTID;
328 return (uint32_t)hostid;
330 EXPORT_SYMBOL(zone_get_hostid);
332 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
334 * Because kallsyms_lookup_name() is no longer exported in the
335 * mainline kernel we are forced to resort to somewhat drastic
336 * measures. This function replaces the functionality by performing
337 * an upcall to user space where /proc/kallsyms is consulted for
338 * the requested address.
340 #define GET_KALLSYMS_ADDR_CMD \
341 "gawk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
342 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
345 set_kallsyms_lookup_name(void)
347 char sh_path[] = "/bin/sh";
348 char *argv[] = { sh_path,
350 GET_KALLSYMS_ADDR_CMD,
352 char *envp[] = { "HOME=/",
354 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
358 rc = call_usermodehelper(sh_path, argv, envp, 1);
360 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
361 argv[0], argv[1], argv[2], rc);
368 __init spl_init(void)
372 if ((rc = debug_init()))
375 if ((rc = spl_kmem_init()))
378 if ((rc = spl_mutex_init()))
381 if ((rc = spl_rw_init()))
384 if ((rc = spl_taskq_init()))
387 if ((rc = vn_init()))
390 if ((rc = proc_init()))
393 if ((rc = kstat_init()))
396 if ((rc = set_hostid()))
397 GOTO(out8, rc = -EADDRNOTAVAIL);
399 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
400 if ((rc = set_kallsyms_lookup_name()))
401 GOTO(out8, rc = -EADDRNOTAVAIL);
402 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
404 if ((rc = spl_kmem_init_kallsyms_lookup()))
407 printk("SPL: Loaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
426 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
427 "rc = %d\n", SPL_META_VERSION, rc);
436 printk("SPL: Unloaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
447 /* Called when a dependent module is loaded */
454 * At module load time the pwd is set to '/' on a Solaris system.
455 * On a Linux system will be set to whatever directory the caller
456 * was in when executing insmod/modprobe.
458 rc = vn_set_pwd("/");
460 printk("SPL: Warning unable to set pwd to '/': %d\n", rc);
462 EXPORT_SYMBOL(spl_setup);
464 /* Called when a dependent module is unloaded */
469 EXPORT_SYMBOL(spl_cleanup);
471 module_init(spl_init);
472 module_exit(spl_fini);
474 MODULE_AUTHOR("Lawrence Livermore National Labs");
475 MODULE_DESCRIPTION("Solaris Porting Layer");
476 MODULE_LICENSE("GPL");