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 <errno.h>
25 #include <time.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <fakekernel.h>
32
33 pri_t minclsyspri = 60;
34 extern zone_t zone0;
35
36 /* Some kernel code takes the address of this. */
37 proc_t p0 = {
38 .p_zone = &zone0, 0
39 };
40
41 proc_t *
42 _curproc(void)
43 {
44 return (&p0);
45 }
46
47 zone_t zone0 = {
48 .zone_name = "global",
49 .zone_zsched = &p0, 0
50 };
51
52 zone_t *
53 _curzone(void)
54 {
55 return (&zone0);
56 }
57
58 pid_t
59 ddi_get_pid(void)
60 {
61 return ((pid_t)getpid());
62 }
63
64 /*
65 * Find highest one bit set.
66 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
67 */
68 int
69 highbit64(uint64_t i)
70 {
71 int h = 1;
72
73 if (i == 0)
74 return (0);
75 if (i & 0xffffffff00000000ULL) {
76 h += 32; i >>= 32;
77 }
78 if (i & 0xffff0000) {
79 h += 16; i >>= 16;
80 }
81 if (i & 0xff00) {
82 h += 8; i >>= 8;
83 }
84 if (i & 0xf0) {
85 h += 4; i >>= 4;
86 }
87 if (i & 0xc) {
88 h += 2; i >>= 2;
89 }
90 if (i & 0x2) {
91 h += 1;
92 }
93 return (h);
94 }
95
96 int
97 ddi_strtoul(const char *str, char **endp, int base, unsigned long *res)
98 {
99 errno = 0;
100 *res = strtoul(str, endp, base);
101 return (errno);
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 }