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