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/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>
42 #ifdef DEBUG_SUBSYSTEM
43 #undef DEBUG_SUBSYSTEM
46 #define DEBUG_SUBSYSTEM S_GENERIC
48 char spl_version
[16] = "SPL v" SPL_META_VERSION
;
51 EXPORT_SYMBOL(spl_hostid
);
53 char hw_serial
[HW_HOSTID_LEN
] = "<none>";
54 EXPORT_SYMBOL(hw_serial
);
59 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
60 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn
= SYMBOL_POISON
;
64 highbit(unsigned long i
)
71 #if BITS_PER_LONG == 64
72 if (i
& 0xffffffff00000000ul
) {
93 EXPORT_SYMBOL(highbit
);
96 * Implementation of 64 bit division for 32-bit machines.
98 #if BITS_PER_LONG == 32
99 uint64_t __udivdi3(uint64_t dividend
, uint64_t divisor
)
101 #if defined(HAVE_DIV64_64) /* 2.6.22 - 2.6.25 API */
102 return div64_64(dividend
, divisor
);
103 #elif defined(HAVE_DIV64_U64) /* 2.6.26 - 2.6.x API */
104 return div64_u64(dividend
, divisor
);
106 /* Implementation from 2.6.30 kernel */
109 high
= divisor
>> 32;
111 unsigned int shift
= fls(high
);
113 d
= divisor
>> shift
;
121 #endif /* HAVE_DIV64_64, HAVE_DIV64_U64 */
123 EXPORT_SYMBOL(__udivdi3
);
126 * Implementation of 64 bit modulo for 32-bit machines.
128 uint64_t __umoddi3(uint64_t dividend
, uint64_t divisor
)
130 return dividend
- divisor
* (dividend
/ divisor
);
132 EXPORT_SYMBOL(__umoddi3
);
133 #endif /* BITS_PER_LONG */
135 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
136 * ddi_strtol(9F) man page. I have not verified the behavior of these
137 * functions against their Solaris counterparts. It is possible that I
138 * may have misinterpreted the man page or the man page is incorrect.
140 int ddi_strtoul(const char *, char **, int, unsigned long *);
141 int ddi_strtol(const char *, char **, int, long *);
142 int ddi_strtoull(const char *, char **, int, unsigned long long *);
143 int ddi_strtoll(const char *, char **, int, long long *);
145 #define define_ddi_strtoux(type, valtype) \
146 int ddi_strtou##type(const char *str, char **endptr, \
147 int base, valtype *result) \
149 valtype last_value, value = 0; \
150 char *ptr = (char *)str; \
151 int flag = 1, digit; \
153 if (strlen(ptr) == 0) \
156 /* Auto-detect base based on prefix */ \
158 if (str[0] == '0') { \
159 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
160 base = 16; /* hex */ \
162 } else if (str[1] >= '0' && str[1] < 8) { \
163 base = 8; /* octal */ \
169 base = 10; /* decimal */ \
175 digit = *ptr - '0'; \
176 else if (isalpha(*ptr)) \
177 digit = tolower(*ptr) - 'a' + 10; \
184 last_value = value; \
185 value = value * base + digit; \
186 if (last_value > value) /* Overflow */ \
197 *endptr = (char *)(flag ? ptr : str); \
202 #define define_ddi_strtox(type, valtype) \
203 int ddi_strto##type(const char *str, char **endptr, \
204 int base, valtype *result) \
209 rc = ddi_strtou##type(str + 1, endptr, base, result); \
211 if (*endptr == str + 1) \
212 *endptr = (char *)str; \
214 *result = -*result; \
217 rc = ddi_strtou##type(str, endptr, base, result); \
223 define_ddi_strtoux(l
, unsigned long)
224 define_ddi_strtox(l
, long)
225 define_ddi_strtoux(ll
, unsigned long long)
226 define_ddi_strtox(ll
, long long)
228 EXPORT_SYMBOL(ddi_strtoul
);
229 EXPORT_SYMBOL(ddi_strtol
);
230 EXPORT_SYMBOL(ddi_strtoll
);
231 EXPORT_SYMBOL(ddi_strtoull
);
234 ddi_copyin(const void *from
, void *to
, size_t len
, int flags
)
236 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
237 if (flags
& FKIOCTL
) {
238 memcpy(to
, from
, len
);
242 return copyin(from
, to
, len
);
244 EXPORT_SYMBOL(ddi_copyin
);
247 ddi_copyout(const void *from
, void *to
, size_t len
, int flags
)
249 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
250 if (flags
& FKIOCTL
) {
251 memcpy(to
, from
, len
);
255 return copyout(from
, to
, len
);
257 EXPORT_SYMBOL(ddi_copyout
);
259 #ifndef HAVE_PUT_TASK_STRUCT
261 * This is only a stub function which should never be used. The SPL should
262 * never be putting away the last reference on a task structure so this will
263 * not be called. However, we still need to define it so the module does not
264 * have undefined symbol at load time. That all said if this impossible
265 * thing does somehow happen SBUG() immediately so we know about it.
268 __put_task_struct(struct task_struct
*t
)
272 EXPORT_SYMBOL(__put_task_struct
);
273 #endif /* HAVE_PUT_TASK_STRUCT */
275 struct new_utsname
*__utsname(void)
277 #ifdef HAVE_INIT_UTSNAME
278 return init_utsname();
280 return &system_utsname
;
283 EXPORT_SYMBOL(__utsname
);
288 char sh_path
[] = "/bin/sh";
289 char *argv
[] = { sh_path
,
291 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
293 char *envp
[] = { "HOME=/",
295 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
299 /* Doing address resolution in the kernel is tricky and just
300 * not a good idea in general. So to set the proper 'hw_serial'
301 * use the usermodehelper support to ask '/bin/sh' to run
302 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
303 * for us to use. It's a horrific solution but it will do for now.
305 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
307 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
308 argv
[0], argv
[1], argv
[2], rc
);
314 zone_get_hostid(void *zone
)
316 unsigned long hostid
;
318 /* Only the global zone is supported */
319 ASSERT(zone
== NULL
);
321 if (ddi_strtoul(hw_serial
, NULL
, HW_HOSTID_LEN
-1, &hostid
) != 0)
322 return HW_INVALID_HOSTID
;
324 return (uint32_t)hostid
;
326 EXPORT_SYMBOL(zone_get_hostid
);
328 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
330 * Because kallsyms_lookup_name() is no longer exported in the
331 * mainline kernel we are forced to resort to somewhat drastic
332 * measures. This function replaces the functionality by performing
333 * an upcall to user space where /proc/kallsyms is consulted for
334 * the requested address.
336 #define GET_KALLSYMS_ADDR_CMD \
337 "awk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
338 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
341 set_kallsyms_lookup_name(void)
343 char sh_path
[] = "/bin/sh";
344 char *argv
[] = { sh_path
,
346 GET_KALLSYMS_ADDR_CMD
,
348 char *envp
[] = { "HOME=/",
350 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
354 rc
= call_usermodehelper(sh_path
, argv
, envp
, 1);
356 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
357 argv
[0], argv
[1], argv
[2], rc
);
364 __init
spl_init(void)
368 if ((rc
= debug_init()))
371 if ((rc
= spl_kmem_init()))
374 if ((rc
= spl_mutex_init()))
377 if ((rc
= spl_rw_init()))
380 if ((rc
= spl_taskq_init()))
383 if ((rc
= vn_init()))
386 if ((rc
= proc_init()))
389 if ((rc
= kstat_init()))
392 if ((rc
= set_hostid()))
393 GOTO(out8
, rc
= -EADDRNOTAVAIL
);
395 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
396 if ((rc
= set_kallsyms_lookup_name()))
397 GOTO(out8
, rc
= -EADDRNOTAVAIL
);
398 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
400 if ((rc
= spl_kmem_init_kallsyms_lookup()))
403 printk("SPL: Loaded Solaris Porting Layer v%s\n", SPL_META_VERSION
);
422 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
423 "rc = %d\n", SPL_META_VERSION
, rc
);
432 printk("SPL: Unloaded Solaris Porting Layer v%s\n", SPL_META_VERSION
);
443 /* Called when a dependent module is loaded */
450 * At module load time the pwd is set to '/' on a Solaris system.
451 * On a Linux system will be set to whatever directory the caller
452 * was in when executing insmod/modprobe.
454 rc
= vn_set_pwd("/");
456 printk("SPL: Warning unable to set pwd to '/': %d\n", rc
);
458 EXPORT_SYMBOL(spl_setup
);
460 /* Called when a dependent module is unloaded */
465 EXPORT_SYMBOL(spl_cleanup
);
467 module_init(spl_init
);
468 module_exit(spl_fini
);
470 MODULE_AUTHOR("Lawrence Livermore National Labs");
471 MODULE_DESCRIPTION("Solaris Porting Layer");
472 MODULE_LICENSE("GPL");