2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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/taskq.h>
34 #include <sys/debug.h>
36 #include <sys/kstat.h>
37 #include <sys/utsname.h>
39 #include <linux/kmod.h>
41 #ifdef DEBUG_SUBSYSTEM
42 #undef DEBUG_SUBSYSTEM
45 #define DEBUG_SUBSYSTEM S_GENERIC
47 char spl_version
[16] = "SPL v" SPL_META_VERSION
;
50 EXPORT_SYMBOL(spl_hostid
);
52 char hw_serial
[HW_HOSTID_LEN
] = "<none>";
53 EXPORT_SYMBOL(hw_serial
);
58 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
59 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn
= SYMBOL_POISON
;
63 highbit(unsigned long i
)
70 #if BITS_PER_LONG == 64
71 if (i
& 0xffffffff00000000ul
) {
92 EXPORT_SYMBOL(highbit
);
95 * Implementation of 64 bit division for 32-bit machines.
97 #if BITS_PER_LONG == 32
98 uint64_t __udivdi3(uint64_t dividend
, uint64_t divisor
)
100 #if defined(HAVE_DIV64_64) /* 2.6.22 - 2.6.25 API */
101 return div64_64(dividend
, divisor
);
102 #elif defined(HAVE_DIV64_U64) /* 2.6.26 - 2.6.x API */
103 return div64_u64(dividend
, divisor
);
105 /* Implementation from 2.6.30 kernel */
108 high
= divisor
>> 32;
110 unsigned int shift
= fls(high
);
112 d
= divisor
>> shift
;
117 return do_div(dividend
, d
);
118 #endif /* HAVE_DIV64_64, HAVE_DIV64_U64 */
120 EXPORT_SYMBOL(__udivdi3
);
123 * Implementation of 64 bit modulo for 32-bit machines.
125 uint64_t __umoddi3(uint64_t dividend
, uint64_t divisor
)
127 return dividend
- divisor
* (dividend
/ divisor
);
129 EXPORT_SYMBOL(__umoddi3
);
130 #endif /* BITS_PER_LONG */
132 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
133 * ddi_strtol(9F) man page. I have not verified the behavior of these
134 * functions against their Solaris counterparts. It is possible that I
135 * may have misinterpreted the man page or the man page is incorrect.
137 int ddi_strtoul(const char *, char **, int, unsigned long *);
138 int ddi_strtol(const char *, char **, int, long *);
139 int ddi_strtoull(const char *, char **, int, unsigned long long *);
140 int ddi_strtoll(const char *, char **, int, long long *);
142 #define define_ddi_strtoux(type, valtype) \
143 int ddi_strtou##type(const char *str, char **endptr, \
144 int base, valtype *result) \
146 valtype last_value, value = 0; \
147 char *ptr = (char *)str; \
148 int flag = 1, digit; \
150 if (strlen(ptr) == 0) \
153 /* Auto-detect base based on prefix */ \
155 if (str[0] == '0') { \
156 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
157 base = 16; /* hex */ \
159 } else if (str[1] >= '0' && str[1] < 8) { \
160 base = 8; /* octal */ \
166 base = 10; /* decimal */ \
172 digit = *ptr - '0'; \
173 else if (isalpha(*ptr)) \
174 digit = tolower(*ptr) - 'a' + 10; \
181 last_value = value; \
182 value = value * base + digit; \
183 if (last_value > value) /* Overflow */ \
194 *endptr = (char *)(flag ? ptr : str); \
199 #define define_ddi_strtox(type, valtype) \
200 int ddi_strto##type(const char *str, char **endptr, \
201 int base, valtype *result) \
206 rc = ddi_strtou##type(str + 1, endptr, base, result); \
208 if (*endptr == str + 1) \
209 *endptr = (char *)str; \
211 *result = -*result; \
214 rc = ddi_strtou##type(str, endptr, base, result); \
220 define_ddi_strtoux(l
, unsigned long)
221 define_ddi_strtox(l
, long)
222 define_ddi_strtoux(ll
, unsigned long long)
223 define_ddi_strtox(ll
, long long)
225 EXPORT_SYMBOL(ddi_strtoul
);
226 EXPORT_SYMBOL(ddi_strtol
);
227 EXPORT_SYMBOL(ddi_strtoll
);
228 EXPORT_SYMBOL(ddi_strtoull
);
231 ddi_copyin(const void *from
, void *to
, size_t len
, int flags
)
233 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
234 if (flags
& FKIOCTL
) {
235 memcpy(to
, from
, len
);
239 return copyin(from
, to
, len
);
241 EXPORT_SYMBOL(ddi_copyin
);
244 ddi_copyout(const void *from
, void *to
, size_t len
, int flags
)
246 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
247 if (flags
& FKIOCTL
) {
248 memcpy(to
, from
, len
);
252 return copyout(from
, to
, len
);
254 EXPORT_SYMBOL(ddi_copyout
);
256 struct new_utsname
*__utsname(void)
258 #ifdef HAVE_INIT_UTSNAME
259 return init_utsname();
261 return &system_utsname
;
264 EXPORT_SYMBOL(__utsname
);
269 char sh_path
[] = "/bin/sh";
270 char *argv
[] = { sh_path
,
272 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
274 char *envp
[] = { "HOME=/",
276 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
280 /* Doing address resolution in the kernel is tricky and just
281 * not a good idea in general. So to set the proper 'hw_serial'
282 * use the usermodehelper support to ask '/bin/sh' to run
283 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
284 * for us to use. It's a horrific solution but it will do for now.
286 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
288 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
289 argv
[0], argv
[1], argv
[2], rc
);
295 zone_get_hostid(void *zone
)
297 unsigned long hostid
;
299 /* Only the global zone is supported */
300 ASSERT(zone
== NULL
);
302 if (ddi_strtoul(hw_serial
, NULL
, HW_HOSTID_LEN
-1, &hostid
) != 0)
303 return HW_INVALID_HOSTID
;
305 return (uint32_t)hostid
;
307 EXPORT_SYMBOL(zone_get_hostid
);
309 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
311 * Because kallsyms_lookup_name() is no longer exported in the
312 * mainline kernel we are forced to resort to somewhat drastic
313 * measures. This function replaces the functionality by performing
314 * an upcall to user space where /proc/kallsyms is consulted for
315 * the requested address.
317 #define GET_KALLSYMS_ADDR_CMD \
318 "awk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
319 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
322 set_kallsyms_lookup_name(void)
324 char sh_path
[] = "/bin/sh";
325 char *argv
[] = { sh_path
,
327 GET_KALLSYMS_ADDR_CMD
,
329 char *envp
[] = { "HOME=/",
331 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
335 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
337 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
338 argv
[0], argv
[1], argv
[2], rc
);
344 static int __init
spl_init(void)
348 if ((rc
= debug_init()))
351 if ((rc
= spl_kmem_init()))
354 if ((rc
= spl_mutex_init()))
357 if ((rc
= spl_taskq_init()))
360 if ((rc
= vn_init()))
363 if ((rc
= proc_init()))
366 if ((rc
= kstat_init()))
369 if ((rc
= set_hostid()))
370 GOTO(out7
, rc
= -EADDRNOTAVAIL
);
372 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
373 if ((rc
= set_kallsyms_lookup_name()))
374 GOTO(out7
, rc
= -EADDRNOTAVAIL
);
375 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
377 if ((rc
= spl_kmem_init_kallsyms_lookup()))
380 printk("SPL: Loaded Solaris Porting Layer v%s\n", SPL_META_VERSION
);
397 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
398 "rc = %d\n", SPL_META_VERSION
, rc
);
402 static void spl_fini(void)
406 printk("SPL: Unloaded Solaris Porting Layer v%s\n", SPL_META_VERSION
);
416 module_init(spl_init
);
417 module_exit(spl_fini
);
419 MODULE_AUTHOR("Lawrence Livermore National Labs");
420 MODULE_DESCRIPTION("Solaris Porting Layer");
421 MODULE_LICENSE("GPL");