1b819cea2SGordon Ross /*
2b819cea2SGordon Ross  * This file and its contents are supplied under the terms of the
3b819cea2SGordon Ross  * Common Development and Distribution License ("CDDL"), version 1.0.
4b819cea2SGordon Ross  * You may only use this file in accordance with the terms of version
5b819cea2SGordon Ross  * 1.0 of the CDDL.
6b819cea2SGordon Ross  *
7b819cea2SGordon Ross  * A full copy of the text of the CDDL should have accompanied this
8b819cea2SGordon Ross  * source.  A copy of the CDDL is also available via the Internet at
9b819cea2SGordon Ross  * http://www.illumos.org/license/CDDL.
10b819cea2SGordon Ross  */
11b819cea2SGordon Ross 
12b819cea2SGordon Ross /*
13*8d94f651SGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
14f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
15b819cea2SGordon Ross  */
16b819cea2SGordon Ross 
17b819cea2SGordon Ross #include <sys/types.h>
18b819cea2SGordon Ross #include <sys/time.h>
19b819cea2SGordon Ross #include <sys/thread.h>
20b819cea2SGordon Ross #include <sys/proc.h>
21b819cea2SGordon Ross #include <sys/zone.h>
22b819cea2SGordon Ross #include <sys/poll.h>
23b819cea2SGordon Ross 
24b819cea2SGordon Ross #include <time.h>
25b819cea2SGordon Ross #include <stdlib.h>
26b819cea2SGordon Ross #include <unistd.h>
27f06dce2cSAndrew Stormont #include <errno.h>
28811599a4SMatt Barden #include <string.h>
29b819cea2SGordon Ross 
30b819cea2SGordon Ross #include <fakekernel.h>
31b819cea2SGordon Ross 
32b819cea2SGordon Ross pri_t minclsyspri = 60;
338329232eSGordon Ross extern zone_t zone0;
34b819cea2SGordon Ross 
35b819cea2SGordon Ross /* Some kernel code takes the address of this. */
368329232eSGordon Ross proc_t p0 = {
378329232eSGordon Ross 	.p_zone = &zone0, 0
388329232eSGordon Ross };
39b819cea2SGordon Ross 
40b819cea2SGordon Ross proc_t *
_curproc(void)41b819cea2SGordon Ross _curproc(void)
42b819cea2SGordon Ross {
43b819cea2SGordon Ross 	return (&p0);
44b819cea2SGordon Ross }
45b819cea2SGordon Ross 
46b819cea2SGordon Ross zone_t zone0 = {
47b819cea2SGordon Ross 	.zone_name = "global",
48b819cea2SGordon Ross 	.zone_zsched = &p0, 0
49b819cea2SGordon Ross };
50b819cea2SGordon Ross 
51b819cea2SGordon Ross zone_t *
_curzone(void)52b819cea2SGordon Ross _curzone(void)
53b819cea2SGordon Ross {
54b819cea2SGordon Ross 	return (&zone0);
55b819cea2SGordon Ross }
56b819cea2SGordon Ross 
57b819cea2SGordon Ross pid_t
ddi_get_pid(void)58b819cea2SGordon Ross ddi_get_pid(void)
59b819cea2SGordon Ross {
60b819cea2SGordon Ross 	return ((pid_t)getpid());
61b819cea2SGordon Ross }
62b819cea2SGordon Ross 
63f06dce2cSAndrew Stormont /*
64f06dce2cSAndrew Stormont  * Find highest one bit set.
65f06dce2cSAndrew Stormont  *	Returns bit number + 1 of highest bit that is set, otherwise returns 0.
66f06dce2cSAndrew Stormont  */
67f06dce2cSAndrew Stormont int
highbit64(uint64_t i)68f06dce2cSAndrew Stormont highbit64(uint64_t i)
69f06dce2cSAndrew Stormont {
70f06dce2cSAndrew Stormont 	int h = 1;
71f06dce2cSAndrew Stormont 
72f06dce2cSAndrew Stormont 	if (i == 0)
73f06dce2cSAndrew Stormont 		return (0);
74f06dce2cSAndrew Stormont 	if (i & 0xffffffff00000000ULL) {
75f06dce2cSAndrew Stormont 		h += 32; i >>= 32;
76f06dce2cSAndrew Stormont 	}
77f06dce2cSAndrew Stormont 	if (i & 0xffff0000) {
78f06dce2cSAndrew Stormont 		h += 16; i >>= 16;
79f06dce2cSAndrew Stormont 	}
80f06dce2cSAndrew Stormont 	if (i & 0xff00) {
81f06dce2cSAndrew Stormont 		h += 8; i >>= 8;
82f06dce2cSAndrew Stormont 	}
83f06dce2cSAndrew Stormont 	if (i & 0xf0) {
84f06dce2cSAndrew Stormont 		h += 4; i >>= 4;
85f06dce2cSAndrew Stormont 	}
86f06dce2cSAndrew Stormont 	if (i & 0xc) {
87f06dce2cSAndrew Stormont 		h += 2; i >>= 2;
88f06dce2cSAndrew Stormont 	}
89f06dce2cSAndrew Stormont 	if (i & 0x2) {
90f06dce2cSAndrew Stormont 		h += 1;
91f06dce2cSAndrew Stormont 	}
92f06dce2cSAndrew Stormont 	return (h);
93f06dce2cSAndrew Stormont }
94f06dce2cSAndrew Stormont 
95b819cea2SGordon Ross int
ddi_strtoul(const char * str,char ** endp,int base,unsigned long * res)96b819cea2SGordon Ross ddi_strtoul(const char *str, char **endp, int base, unsigned long *res)
97b819cea2SGordon Ross {
98*8d94f651SGordon Ross 	errno = 0;
99b819cea2SGordon Ross 	*res = strtoul(str, endp, base);
100f06dce2cSAndrew Stormont 	if (*res == 0)
101f06dce2cSAndrew Stormont 		return (errno);
102f06dce2cSAndrew Stormont 	return (0);
103f06dce2cSAndrew Stormont }
104f06dce2cSAndrew Stormont 
105f06dce2cSAndrew Stormont int
ddi_strtoull(const char * str,char ** endp,int base,u_longlong_t * res)1068329232eSGordon Ross ddi_strtoull(const char *str, char **endp, int base, u_longlong_t *res)
107f06dce2cSAndrew Stormont {
1088329232eSGordon Ross 	errno = 0;
1098329232eSGordon Ross 	*res = strtoull(str, endp, base);
110f06dce2cSAndrew Stormont 	if (*res == 0)
111f06dce2cSAndrew Stormont 		return (errno);
112b819cea2SGordon Ross 	return (0);
113b819cea2SGordon Ross }
114b819cea2SGordon Ross 
115b819cea2SGordon Ross void
delay(clock_t ticks)116b819cea2SGordon Ross delay(clock_t ticks)
117b819cea2SGordon Ross {
118b819cea2SGordon Ross 	int msec = ticks;  /* NB: hz==1000 */
119b819cea2SGordon Ross 	(void) poll(0, 0, msec);
120b819cea2SGordon Ross }
121b819cea2SGordon Ross 
122811599a4SMatt Barden int
highbit(ulong_t i)123811599a4SMatt Barden highbit(ulong_t i)
124811599a4SMatt Barden {
125811599a4SMatt Barden 	return (fls(i));
126811599a4SMatt Barden }
127811599a4SMatt Barden 
1288329232eSGordon Ross /* ARGSUSED */
129f06dce2cSAndrew Stormont int
issig(int why)130f06dce2cSAndrew Stormont issig(int why)
131f06dce2cSAndrew Stormont {
132f06dce2cSAndrew Stormont 	return (0);
133f06dce2cSAndrew Stormont }
134f06dce2cSAndrew Stormont 
135b819cea2SGordon Ross /*
136b819cea2SGordon Ross  * This library does not really need an "init" function, but
137b819cea2SGordon Ross  * providing one the main program can call is an easy way to
138b819cea2SGordon Ross  * make sure this library is loaded into the debugger, and
139b819cea2SGordon Ross  * gives us a way to avoid elfcheck complaints in the build.
140b819cea2SGordon Ross  */
141b819cea2SGordon Ross void
fakekernel_init(void)142b819cea2SGordon Ross fakekernel_init(void)
143b819cea2SGordon Ross {
144b819cea2SGordon Ross }
145