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>
38 #include <linux/kmod.h>
40 #ifdef DEBUG_SUBSYSTEM
41 #undef DEBUG_SUBSYSTEM
44 #define DEBUG_SUBSYSTEM S_GENERIC
46 char spl_version
[16] = "SPL v" VERSION
;
49 EXPORT_SYMBOL(spl_hostid
);
51 char hw_serial
[HW_HOSTID_LEN
] = "<none>";
52 EXPORT_SYMBOL(hw_serial
);
57 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
58 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn
= NULL
;
62 highbit(unsigned long i
)
69 #if BITS_PER_LONG == 64
70 if (i
& 0xffffffff00000000ul
) {
91 EXPORT_SYMBOL(highbit
);
94 * Implementation of 64 bit division for 32-bit machines.
96 #if BITS_PER_LONG == 32
97 uint64_t __udivdi3(uint64_t dividend
, uint64_t divisor
)
100 return div64_64(dividend
, divisor
);
102 /* Taken from a 2.6.24 kernel. */
105 high
= divisor
>> 32;
107 unsigned int shift
= fls(high
);
109 d
= divisor
>> shift
;
119 EXPORT_SYMBOL(__udivdi3
);
122 * Implementation of 64 bit modulo for 32-bit machines.
124 uint64_t __umoddi3(uint64_t dividend
, uint64_t divisor
)
126 return dividend
- divisor
* (dividend
/ divisor
);
128 EXPORT_SYMBOL(__umoddi3
);
131 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
132 * ddi_strtol(9F) man page. I have not verified the behavior of these
133 * functions against their Solaris counterparts. It is possible that I
134 * may have misinterpretted the man page or the man page is incorrect.
136 int ddi_strtoul(const char *, char **, int, unsigned long *);
137 int ddi_strtol(const char *, char **, int, long *);
138 int ddi_strtoull(const char *, char **, int, unsigned long long *);
139 int ddi_strtoll(const char *, char **, int, long long *);
141 #define define_ddi_strtoux(type, valtype) \
142 int ddi_strtou##type(const char *str, char **endptr, \
143 int base, valtype *result) \
145 valtype last_value, value = 0; \
146 char *ptr = (char *)str; \
147 int flag = 1, digit; \
149 if (strlen(ptr) == 0) \
152 /* Auto-detect base based on prefix */ \
154 if (str[0] == '0') { \
155 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
156 base = 16; /* hex */ \
158 } else if (str[1] >= '0' && str[1] < 8) { \
159 base = 8; /* octal */ \
165 base = 10; /* decimal */ \
171 digit = *ptr - '0'; \
172 else if (isalpha(*ptr)) \
173 digit = tolower(*ptr) - 'a' + 10; \
180 last_value = value; \
181 value = value * base + digit; \
182 if (last_value > value) /* Overflow */ \
193 *endptr = (char *)(flag ? ptr : str); \
198 #define define_ddi_strtox(type, valtype) \
199 int ddi_strto##type(const char *str, char **endptr, \
200 int base, valtype *result) \
205 rc = ddi_strtou##type(str + 1, endptr, base, result); \
207 if (*endptr == str + 1) \
208 *endptr = (char *)str; \
210 *result = -*result; \
213 rc = ddi_strtou##type(str, endptr, base, result); \
219 define_ddi_strtoux(l
, unsigned long)
220 define_ddi_strtox(l
, long)
221 define_ddi_strtoux(ll
, unsigned long long)
222 define_ddi_strtox(ll
, long long)
224 EXPORT_SYMBOL(ddi_strtoul
);
225 EXPORT_SYMBOL(ddi_strtol
);
226 EXPORT_SYMBOL(ddi_strtoll
);
227 EXPORT_SYMBOL(ddi_strtoull
);
229 struct new_utsname
*__utsname(void)
231 #ifdef HAVE_INIT_UTSNAME
232 return init_utsname();
234 return &system_utsname
;
237 EXPORT_SYMBOL(__utsname
);
242 char sh_path
[] = "/bin/sh";
243 char *argv
[] = { sh_path
,
245 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
247 char *envp
[] = { "HOME=/",
249 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
252 /* Doing address resolution in the kernel is tricky and just
253 * not a good idea in general. So to set the proper 'hw_serial'
254 * use the usermodehelper support to ask '/bin/sh' to run
255 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
256 * for us to use. It's a horific solution but it will do for now.
258 return call_usermodehelper(sh_path
, argv
, envp
, 1);
262 zone_get_hostid(void *zone
)
264 unsigned long hostid
;
266 /* Only the global zone is supported */
267 ASSERT(zone
== NULL
);
269 if (ddi_strtoul(hw_serial
, NULL
, HW_HOSTID_LEN
-1, &hostid
) != 0)
270 return HW_INVALID_HOSTID
;
272 return (uint32_t)hostid
;
274 EXPORT_SYMBOL(zone_get_hostid
);
276 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
277 #define set_kallsyms_lookup_name() (0)
280 * Because kallsyms_lookup_name() is no longer exported in the
281 * mainline kernel we are forced to resort to somewhat drastic
282 * measures. This function replaces the functionality by performing
283 * an upcall to user space where /proc/kallsyms is consulted for
284 * the requested address.
286 #define GET_KALLSYMS_ADDR_CMD \
287 "awk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
288 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
291 set_kallsyms_lookup_name(void)
293 char sh_path
[] = "/bin/sh";
294 char *argv
[] = { sh_path
,
296 GET_KALLSYMS_ADDR_CMD
,
298 char *envp
[] = { "HOME=/",
300 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
304 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
308 return spl_kmem_init_kallsyms_lookup();
312 static int __init
spl_init(void)
316 if ((rc
= debug_init()))
319 if ((rc
= spl_kmem_init()))
322 if ((rc
= spl_mutex_init()))
325 if ((rc
= spl_taskq_init()))
328 if ((rc
= vn_init()))
331 if ((rc
= proc_init()))
334 if ((rc
= kstat_init()))
337 if ((rc
= set_hostid()))
338 GOTO(out7
, rc
= -EADDRNOTAVAIL
);
340 if ((rc
= set_kallsyms_lookup_name()))
341 GOTO(out7
, rc
= -EADDRNOTAVAIL
);
343 printk("SPL: Loaded Solaris Porting Layer v%s\n", VERSION
);
360 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
361 "rc = %d\n", VERSION
, rc
);
365 static void spl_fini(void)
369 printk("SPL: Unloaded Solaris Porting Layer v%s\n", VERSION
);
379 module_init(spl_init
);
380 module_exit(spl_fini
);
382 MODULE_AUTHOR("Lawrence Livermore National Labs");
383 MODULE_DESCRIPTION("Solaris Porting Layer");
384 MODULE_LICENSE("GPL");