cpuctl.c revision 1.20.2.1 1 /* $NetBSD: cpuctl.c,v 1.20.2.1 2015/01/16 08:30:50 snj Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008, 2009, 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: cpuctl.c,v 1.20.2.1 2015/01/16 08:30:50 snj Exp $");
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/uio.h>
40 #include <sys/cpuio.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <paths.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <util.h>
52 #include <time.h>
53 #include <sched.h>
54
55 #include "cpuctl.h"
56
57 static u_int getcpuid(char **);
58 __dead static void usage(void);
59
60 static void cpu_identify(char **);
61 static void cpu_list(char **);
62 static void cpu_offline(char **);
63 static void cpu_online(char **);
64 static void cpu_intr(char **);
65 static void cpu_nointr(char **);
66 static void cpu_ucode(char **);
67
68 static struct cmdtab {
69 const char *label;
70 int takesargs;
71 int argsoptional;
72 void (*func)(char **);
73 } const cpu_cmdtab[] = {
74 { "identify", 1, 0, cpu_identify },
75 { "list", 0, 0, cpu_list },
76 { "offline", 1, 0, cpu_offline },
77 { "online", 1, 0, cpu_online },
78 { "intr", 1, 0, cpu_intr },
79 { "nointr", 1, 0, cpu_nointr },
80 { "ucode", 1, 1, cpu_ucode },
81 { NULL, 0, 0, NULL },
82 };
83
84 static int fd;
85 int verbose;
86
87 int
88 main(int argc, char **argv)
89 {
90 const struct cmdtab *ct;
91 int ch;
92
93 while ((ch = getopt(argc, argv, "v")) != -1)
94 switch (ch) {
95 case 'v':
96 verbose = 1;
97 break;
98 default:
99 usage();
100 }
101 argc -= optind;
102 argv += optind;
103 if (argc < 1)
104 usage();
105
106 if ((fd = open(_PATH_CPUCTL, O_RDWR)) < 0)
107 err(EXIT_FAILURE, _PATH_CPUCTL);
108
109 for (ct = cpu_cmdtab; ct->label != NULL; ct++) {
110 if (strcmp(argv[0], ct->label) == 0) {
111 if (!ct->argsoptional &&
112 ((ct->takesargs == 0) ^ (argv[1] == NULL)))
113 {
114 usage();
115 }
116 (*ct->func)(argv + 1);
117 break;
118 }
119 }
120
121 if (ct->label == NULL)
122 errx(EXIT_FAILURE, "unknown command ``%s''", argv[0]);
123
124 close(fd);
125 exit(EXIT_SUCCESS);
126 /* NOTREACHED */
127 }
128
129 static void
130 usage(void)
131 {
132 const char *progname = getprogname();
133
134 fprintf(stderr, "usage: %s identify cpuno\n", progname);
135 fprintf(stderr, " %s list\n", progname);
136 fprintf(stderr, " %s offline cpuno\n", progname);
137 fprintf(stderr, " %s online cpuno\n", progname);
138 fprintf(stderr, " %s intr cpuno\n", progname);
139 fprintf(stderr, " %s nointr cpuno\n", progname);
140 fprintf(stderr, " %s ucode [file]\n", progname);
141 exit(EXIT_FAILURE);
142 /* NOTREACHED */
143 }
144
145 static void
146 cpu_online(char **argv)
147 {
148 cpustate_t cs;
149
150 cs.cs_id = getcpuid(argv);
151 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
152 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
153 cs.cs_online = true;
154 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
155 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
156 }
157
158 static void
159 cpu_offline(char **argv)
160 {
161 cpustate_t cs;
162
163 cs.cs_id = getcpuid(argv);
164 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
165 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
166 cs.cs_online = false;
167 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
168 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
169 }
170
171 static void
172 cpu_intr(char **argv)
173 {
174 cpustate_t cs;
175
176 cs.cs_id = getcpuid(argv);
177 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
178 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
179 cs.cs_intr = true;
180 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
181 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
182 }
183
184 static void
185 cpu_nointr(char **argv)
186 {
187 cpustate_t cs;
188
189 cs.cs_id = getcpuid(argv);
190 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
191 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
192 cs.cs_intr = false;
193 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0) {
194 if (errno == EOPNOTSUPP) {
195 warnx("interrupt control not supported on "
196 "this platform");
197 } else
198 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
199 }
200 }
201
202 static void
203 cpu_ucode(char **argv)
204 {
205 int error;
206 struct cpu_ucode uc;
207
208 if (argv[0] != NULL)
209 strlcpy(uc.fwname, argv[0], sizeof(uc.fwname));
210 else
211 memset(uc.fwname, '\0', sizeof(uc.fwname));
212
213 error = ioctl(fd, IOC_CPU_UCODE_APPLY, &uc);
214 if (error < 0) {
215 if (uc.fwname[0])
216 err(EXIT_FAILURE, "%s", uc.fwname);
217 else
218 err(EXIT_FAILURE, "IOC_CPU_UCODE_APPLY");
219 }
220 }
221
222
223 static void
224 cpu_identify(char **argv)
225 {
226 char name[32];
227 unsigned int id, np;
228 cpuset_t *cpuset;
229 struct cpu_ucode ucode;
230 char ucbuf[16];
231
232 np = sysconf(_SC_NPROCESSORS_CONF);
233 id = getcpuid(argv);
234 snprintf(name, sizeof(name), "cpu%u", id);
235
236 if (np != 1) {
237 cpuset = cpuset_create();
238 if (cpuset == NULL)
239 err(EXIT_FAILURE, "cpuset_create");
240 cpuset_zero(cpuset);
241 cpuset_set(id, cpuset);
242 if (_sched_setaffinity(0, 0, cpuset_size(cpuset), cpuset) < 0) {
243 if (errno == EPERM) {
244 printf("Cannot bind to target CPU. Output "
245 "may not accurately describe the target.\n"
246 "Run as root to allow binding.\n\n");
247 } else {
248 err(EXIT_FAILURE, "_sched_setaffinity");
249 }
250 }
251 cpuset_destroy(cpuset);
252 }
253 identifycpu(name);
254
255 if (ioctl(fd, IOC_CPU_UCODE_GET_VERSION, &ucode) < 0)
256 ucode.version = (uint64_t)-1;
257 if (ucode.version == (uint64_t)-1)
258 strcpy(ucbuf, "?");
259 else
260 snprintf(ucbuf, sizeof(ucbuf), "0x%"PRIx64,
261 ucode.version);
262
263 printf("%s: UCode version: %s\n", name, ucbuf);
264 }
265
266 static u_int
267 getcpuid(char **argv)
268 {
269 char *argp;
270 u_int id;
271 long np;
272
273 id = (u_int)strtoul(argv[0], &argp, 0);
274 if (*argp != '\0')
275 usage();
276
277 np = sysconf(_SC_NPROCESSORS_CONF);
278 if (id >= (u_long)np)
279 errx(EXIT_FAILURE, "Invalid CPU number");
280
281 return id;
282 }
283
284 static void
285 cpu_list(char **argv)
286 {
287 const char *state, *intr;
288 cpustate_t cs;
289 u_int cnt, i;
290 time_t lastmod;
291 char ibuf[16], *ts;
292
293 if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
294 err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
295
296 printf(
297 "Num HwId Unbound LWPs Interrupts Last change #Intr\n"
298 "---- ---- ------------ ---------- ------------------------ -----\n");
299
300 for (i = 0; i < cnt; i++) {
301 cs.cs_id = i;
302 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
303 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
304 if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
305 err(EXIT_FAILURE, "IOC_CPU_MAPID");
306 if (cs.cs_online)
307 state = "online";
308 else
309 state = "offline";
310 if (cs.cs_intr)
311 intr = "intr";
312 else
313 intr = "nointr";
314 if (cs.cs_intrcnt == 0)
315 strcpy(ibuf, "?");
316 else
317 snprintf(ibuf, sizeof(ibuf), "%d", cs.cs_intrcnt - 1);
318
319 lastmod = (time_t)cs.cs_lastmod |
320 ((time_t)cs.cs_lastmodhi << 32);
321 ts = asctime(localtime(&lastmod));
322 ts[strlen(ts) - 1] = '\0';
323 printf("%-4d %-4x %-12s %-10s %s %-5s\n",
324 i, cs.cs_hwid, state,
325 intr, ts, ibuf);
326 }
327 }
328
329 int
330 aprint_normal(const char *fmt, ...)
331 {
332 va_list ap;
333 int rv;
334
335 va_start(ap, fmt);
336 rv = vfprintf(stdout, fmt, ap);
337 va_end(ap);
338
339 return rv;
340 }
341 __strong_alias(aprint_verbose,aprint_normal)
342 __strong_alias(aprint_error,aprint_normal)
343
344 int
345 aprint_normal_dev(const char *dev, const char *fmt, ...)
346 {
347 va_list ap;
348 int rv;
349
350 printf("%s: ", dev);
351 va_start(ap, fmt);
352 rv = vfprintf(stdout, fmt, ap);
353 va_end(ap);
354
355 return rv;
356 }
357 __strong_alias(aprint_verbose_dev,aprint_normal_dev)
358 __strong_alias(aprint_error_dev,aprint_normal_dev)
359