Home | History | Annotate | Line # | Download | only in cpuctl
cpuctl.c revision 1.26
      1 /*	$NetBSD: cpuctl.c,v 1.26 2015/11/16 02:02:41 mrg 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.26 2015/11/16 02:02:41 mrg 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 #include <stdbool.h>
     55 
     56 #include "cpuctl.h"
     57 
     58 static u_int	getcpuid(char **);
     59 __dead static void	usage(void);
     60 
     61 static void	cpu_identify(char **);
     62 static void	cpu_list(char **);
     63 static void	cpu_offline(char **);
     64 static void	cpu_online(char **);
     65 static void	cpu_intr(char **);
     66 static void	cpu_nointr(char **);
     67 static void	cpu_ucode(char **);
     68 
     69 static struct cmdtab {
     70 	const char	*label;
     71 	bool	takesargs;
     72 	bool	argsoptional;
     73 	void	(*func)(char **);
     74 } const cpu_cmdtab[] = {
     75 	{ "identify",	true,  false, cpu_identify },
     76 	{ "list",	false, false, cpu_list },
     77 	{ "offline",	true,  false, cpu_offline },
     78 	{ "online",	true,  false, cpu_online },
     79 	{ "intr",	true,  false, cpu_intr },
     80 	{ "nointr",	true,  false, cpu_nointr },
     81 	{ "ucode",	true,  true,  cpu_ucode },
     82 	{ NULL,		false, false, NULL },
     83 };
     84 
     85 static int	fd;
     86 int		verbose;
     87 
     88 int
     89 main(int argc, char **argv)
     90 {
     91 	const struct cmdtab *ct;
     92 	int ch;
     93 
     94 	while ((ch = getopt(argc, argv, "v")) != -1)
     95 		switch (ch) {
     96 		case 'v':
     97 			verbose = 1;
     98 			break;
     99 		default:
    100 			usage();
    101 		}
    102 	argc -= optind;
    103 	argv += optind;
    104 	if (argc < 1)
    105 		usage();
    106 
    107 	if ((fd = open(_PATH_CPUCTL, O_RDWR)) < 0)
    108 		err(EXIT_FAILURE, _PATH_CPUCTL);
    109 
    110 	for (ct = cpu_cmdtab; ct->label != NULL; ct++) {
    111 		if (strcmp(argv[0], ct->label) == 0) {
    112 			if (!ct->argsoptional &&
    113 			    ((ct->takesargs == 0) ^ (argv[1] == NULL)))
    114 			{
    115 				usage();
    116 			}
    117 			(*ct->func)(argv + 1);
    118 			break;
    119 		}
    120 	}
    121 
    122 	if (ct->label == NULL)
    123 		errx(EXIT_FAILURE, "unknown command ``%s''", argv[0]);
    124 
    125 	close(fd);
    126 	exit(EXIT_SUCCESS);
    127 	/* NOTREACHED */
    128 }
    129 
    130 static void
    131 usage(void)
    132 {
    133 	const char *progname = getprogname();
    134 
    135 	fprintf(stderr, "usage: %s identify cpuno\n", progname);
    136 	fprintf(stderr, "       %s list\n", progname);
    137 	fprintf(stderr, "       %s offline cpuno\n", progname);
    138 	fprintf(stderr, "       %s online cpuno\n", progname);
    139 	fprintf(stderr, "       %s intr cpuno\n", progname);
    140 	fprintf(stderr, "       %s nointr cpuno\n", progname);
    141 	fprintf(stderr, "       %s ucode [cpuno] [file]\n", progname);
    142 	exit(EXIT_FAILURE);
    143 	/* NOTREACHED */
    144 }
    145 
    146 static void
    147 cpu_online(char **argv)
    148 {
    149 	cpustate_t cs;
    150 
    151 	cs.cs_id = getcpuid(argv);
    152 	if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    153 		err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    154 	cs.cs_online = true;
    155 	if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
    156 		err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
    157 }
    158 
    159 static void
    160 cpu_offline(char **argv)
    161 {
    162 	cpustate_t cs;
    163 
    164 	cs.cs_id = getcpuid(argv);
    165 	if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    166 		err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    167 	cs.cs_online = false;
    168 	if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
    169 		err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
    170 }
    171 
    172 static void
    173 cpu_intr(char **argv)
    174 {
    175 	cpustate_t cs;
    176 
    177 	cs.cs_id = getcpuid(argv);
    178 	if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    179 		err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    180 	cs.cs_intr = true;
    181 	if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
    182 		err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
    183 }
    184 
    185 static void
    186 cpu_nointr(char **argv)
    187 {
    188 	cpustate_t cs;
    189 
    190 	cs.cs_id = getcpuid(argv);
    191 	if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    192 		err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    193 	cs.cs_intr = false;
    194 	if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0) {
    195 		if (errno == EOPNOTSUPP) {
    196 			warnx("interrupt control not supported on "
    197 			    "this platform");
    198 		} else
    199 			err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
    200 	}
    201 }
    202 
    203 static void
    204 cpu_ucode(char **argv)
    205 {
    206 	int error;
    207 	struct cpu_ucode uc;
    208 	unsigned long id = 0; /* gcc */
    209 	char *ep;
    210 	cpuset_t *cpuset;
    211 
    212 	uc.cpu_nr = -1;
    213 	if (argv[0] != NULL) {
    214 		id = strtoul(argv[0], &ep, 0);
    215 		if (id != ULONG_MAX && *ep == '\0') {
    216 			uc.cpu_nr = id;
    217 			argv++;
    218 		}
    219 	}
    220 	if (argv[0] != NULL)
    221 		strlcpy(uc.fwname, argv[0], sizeof(uc.fwname));
    222 	else
    223 		memset(uc.fwname, '\0', sizeof(uc.fwname));
    224 
    225 	error = ucodeupdate_check(fd, &uc);
    226 	if (error)
    227 		errx(EXIT_FAILURE, "unsupported");
    228 
    229 	if (uc.cpu_nr == CPU_UCODE_CURRENT_CPU) {
    230 		cpuset = cpuset_create();
    231 		if (cpuset == NULL)
    232 			err(EXIT_FAILURE, "cpuset_create");
    233 		cpuset_zero(cpuset);
    234 		cpuset_set(id, cpuset);
    235 		if (_sched_setaffinity(0, 0, cpuset_size(cpuset), cpuset) < 0) {
    236 			err(EXIT_FAILURE, "_sched_setaffinity");
    237 		}
    238 		cpuset_destroy(cpuset);
    239 	}
    240 	error = ioctl(fd, IOC_CPU_UCODE_APPLY, &uc);
    241 	if (error < 0) {
    242 		if (uc.fwname[0])
    243 			err(EXIT_FAILURE, "%s", uc.fwname);
    244 		else
    245 			err(EXIT_FAILURE, "IOC_CPU_UCODE_APPLY");
    246 	}
    247 }
    248 
    249 
    250 static void
    251 cpu_identify(char **argv)
    252 {
    253 	char name[32];
    254 	unsigned int id, np;
    255 	cpuset_t *cpuset;
    256 
    257 	np = sysconf(_SC_NPROCESSORS_CONF);
    258 	id = getcpuid(argv);
    259 	snprintf(name, sizeof(name), "cpu%u", id);
    260 
    261 	if (np != 1) {
    262 		cpuset = cpuset_create();
    263 		if (cpuset == NULL)
    264 			err(EXIT_FAILURE, "cpuset_create");
    265 		cpuset_zero(cpuset);
    266 		cpuset_set(id, cpuset);
    267 		if (_sched_setaffinity(0, 0, cpuset_size(cpuset), cpuset) < 0) {
    268 			if (errno == EPERM) {
    269 				printf("Cannot bind to target CPU.  Output "
    270 				    "may not accurately describe the target.\n"
    271 				    "Run as root to allow binding.\n\n");
    272 			} else {
    273 				err(EXIT_FAILURE, "_sched_setaffinity");
    274 			}
    275 		}
    276 		cpuset_destroy(cpuset);
    277 	}
    278 	identifycpu(fd, name);
    279 }
    280 
    281 static u_int
    282 getcpuid(char **argv)
    283 {
    284 	char *argp;
    285 	u_int id;
    286 	long np;
    287 
    288 	id = (u_int)strtoul(argv[0], &argp, 0);
    289 	if (*argp != '\0')
    290 		usage();
    291 
    292 	np = sysconf(_SC_NPROCESSORS_CONF);
    293 	if (id >= (u_long)np)
    294 		errx(EXIT_FAILURE, "Invalid CPU number");
    295 
    296 	return id;
    297 }
    298 
    299 static void
    300 cpu_list(char **argv)
    301 {
    302 	const char *state, *intr;
    303 	cpustate_t cs;
    304 	u_int cnt, i;
    305 	time_t lastmod;
    306 	char ibuf[16], *ts;
    307 
    308 	if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
    309 		err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
    310 
    311 	printf(
    312 "Num  HwId Unbound LWPs Interrupts Last change              #Intr\n"
    313 "---- ---- ------------ ---------- ------------------------ -----\n");
    314 
    315 	for (i = 0; i < cnt; i++) {
    316 		cs.cs_id = i;
    317 		if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    318 			err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    319 		if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
    320 			err(EXIT_FAILURE, "IOC_CPU_MAPID");
    321 		if (cs.cs_online)
    322 			state = "online";
    323 		else
    324 			state = "offline";
    325 		if (cs.cs_intr)
    326 			intr = "intr";
    327 		else
    328 			intr = "nointr";
    329 		if (cs.cs_intrcnt == 0)
    330 			strcpy(ibuf, "?");
    331 		else
    332 			snprintf(ibuf, sizeof(ibuf), "%d", cs.cs_intrcnt - 1);
    333 
    334 		lastmod = (time_t)cs.cs_lastmod |
    335 		    ((time_t)cs.cs_lastmodhi << 32);
    336 		ts = asctime(localtime(&lastmod));
    337 		ts[strlen(ts) - 1] = '\0';
    338 		printf("%-4d %-4x %-12s %-10s %s %-5s\n",
    339 		   i, cs.cs_hwid, state,
    340 		   intr, ts, ibuf);
    341 	}
    342 }
    343 
    344 int
    345 aprint_normal(const char *fmt, ...)
    346 {
    347 	va_list ap;
    348 	int rv;
    349 
    350 	va_start(ap, fmt);
    351 	rv = vfprintf(stdout, fmt, ap);
    352 	va_end(ap);
    353 
    354 	return rv;
    355 }
    356 __strong_alias(aprint_verbose,aprint_normal)
    357 __strong_alias(aprint_error,aprint_normal)
    358 
    359 int
    360 aprint_normal_dev(const char *dev, const char *fmt, ...)
    361 {
    362 	va_list ap;
    363 	int rv;
    364 
    365 	printf("%s: ", dev);
    366 	va_start(ap, fmt);
    367 	rv = vfprintf(stdout, fmt, ap);
    368 	va_end(ap);
    369 
    370 	return rv;
    371 }
    372 __strong_alias(aprint_verbose_dev,aprint_normal_dev)
    373 __strong_alias(aprint_error_dev,aprint_normal_dev)
    374