xref: /illumos-gate/usr/src/lib/libfakekernel/common/kmisc.c (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
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 2017 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 	errno = 0;
99 	*res = strtoul(str, endp, base);
100 	if (*res == 0)
101 		return (errno);
102 	return (0);
103 }
104 
105 int
106 ddi_strtoull(const char *str, char **endp, int base, u_longlong_t *res)
107 {
108 	errno = 0;
109 	*res = strtoull(str, endp, base);
110 	if (*res == 0)
111 		return (errno);
112 	return (0);
113 }
114 
115 void
116 delay(clock_t ticks)
117 {
118 	int msec = ticks;  /* NB: hz==1000 */
119 	(void) poll(0, 0, msec);
120 }
121 
122 int
123 highbit(ulong_t i)
124 {
125 	return (fls(i));
126 }
127 
128 /* ARGSUSED */
129 int
130 issig(int why)
131 {
132 	return (0);
133 }
134 
135 /*
136  * This library does not really need an "init" function, but
137  * providing one the main program can call is an easy way to
138  * make sure this library is loaded into the debugger, and
139  * gives us a way to avoid elfcheck complaints in the build.
140  */
141 void
142 fakekernel_init(void)
143 {
144 }
145