2 * Copyright (c) 2008-2011 Stanislav Sedov <stas@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * This utility provides userland access to the cpuctl(4) pseudo-device
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
44 #include <sys/queue.h>
45 #include <sys/param.h>
46 #include <sys/types.h>
48 #include <sys/ioctl.h>
49 #include <sys/cpuctl.h>
51 #include "cpucontrol.h"
56 int verbosity_level = 0;
58 #define DEFAULT_DATADIR "/usr/local/share/cpucontrol"
71 #define HIGH(val) (uint32_t)(((val) >> 32) & 0xffffffff)
72 #define LOW(val) (uint32_t)((val) & 0xffffffff)
75 * Macros for freeing SLISTs, probably must be in /sys/queue.h
77 #define SLIST_FREE(head, field, freef) do { \
78 typeof(SLIST_FIRST(head)) __elm0; \
79 typeof(SLIST_FIRST(head)) __elm; \
80 SLIST_FOREACH_SAFE(__elm, (head), field, __elm0) \
81 (void)(freef)(__elm); \
86 SLIST_ENTRY(datadir) next;
88 static SLIST_HEAD(, datadir) datadirs = SLIST_HEAD_INITIALIZER(datadirs);
90 static struct ucode_handler {
92 ucode_update_t *update;
94 { intel_probe, intel_update },
95 { amd10h_probe, amd10h_update },
96 { amd_probe, amd_update },
97 { via_probe, via_update },
99 #define NHANDLERS (sizeof(handlers) / sizeof(*handlers))
101 static void usage(void);
102 static int isdir(const char *path);
103 static int do_cpuid(const char *cmdarg, const char *dev);
104 static int do_cpuid_count(const char *cmdarg, const char *dev);
105 static int do_msr(const char *cmdarg, const char *dev);
106 static int do_update(const char *dev);
107 static void datadir_add(const char *path);
114 name = getprogname();
117 fprintf(stderr, "Usage: %s [-vh] [-d datadir] [-m msr[=value] | "
118 "-i level | -i level,level_type | -u] device\n", name);
123 isdir(const char *path)
128 error = stat(path, &st);
130 WARN(0, "stat(%s)", path);
133 return (st.st_mode & S_IFDIR);
137 do_cpuid(const char *cmdarg, const char *dev)
140 cpuctl_cpuid_args_t args;
144 assert(cmdarg != NULL);
147 level = strtoul(cmdarg, &endptr, 16);
148 if (*cmdarg == '\0' || *endptr != '\0') {
149 WARNX(0, "incorrect operand: %s", cmdarg);
155 * Fill ioctl argument structure.
158 fd = open(dev, O_RDONLY);
160 WARN(0, "error opening %s for reading", dev);
163 error = ioctl(fd, CPUCTL_CPUID, &args);
165 WARN(0, "ioctl(%s, CPUCTL_CPUID)", dev);
169 fprintf(stdout, "cpuid level 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
170 level, args.data[0], args.data[1], args.data[2], args.data[3]);
176 do_cpuid_count(const char *cmdarg, const char *dev)
178 char *cmdarg1, *endptr, *endptr1;
179 unsigned int level, level_type;
180 cpuctl_cpuid_count_args_t args;
183 assert(cmdarg != NULL);
186 level = strtoul(cmdarg, &endptr, 16);
187 if (*cmdarg == '\0' || *endptr == '\0') {
188 WARNX(0, "incorrect or missing operand: %s", cmdarg);
192 /* Locate the comma... */
193 cmdarg1 = strstr(endptr, ",");
194 /* ... and skip past it */
196 level_type = strtoul(cmdarg1, &endptr1, 16);
197 if (*cmdarg1 == '\0' || *endptr1 != '\0') {
198 WARNX(0, "incorrect or missing operand: %s", cmdarg);
204 * Fill ioctl argument structure.
207 args.level_type = level_type;
208 fd = open(dev, O_RDONLY);
210 WARN(0, "error opening %s for reading", dev);
213 error = ioctl(fd, CPUCTL_CPUID_COUNT, &args);
215 WARN(0, "ioctl(%s, CPUCTL_CPUID_COUNT)", dev);
219 fprintf(stdout, "cpuid level 0x%x, level_type 0x%x: 0x%.8x 0x%.8x "
220 "0x%.8x 0x%.8x\n", level, level_type, args.data[0], args.data[1],
221 args.data[2], args.data[3]);
227 do_msr(const char *cmdarg, const char *dev)
230 cpuctl_msr_args_t args;
233 unsigned long command;
234 int do_invert = 0, op;
236 const char *command_name;
240 assert(cmdarg != NULL);
242 len = strlen(cmdarg);
244 WARNX(0, "MSR register expected");
250 * Parse command string.
252 msr = strtoul(cmdarg, &endptr, 16);
271 if (op != OP_READ) { /* Complex operation. */
280 data = strtoull(p, &endptr, 16);
281 if (*p == '\0' || *endptr != '\0') {
282 WARNX(0, "argument required: %s", cmdarg);
288 if (op == OP_INVAL) {
289 WARNX(0, "invalid operator: %s", cmdarg);
295 * Fill ioctl argument structure.
298 if ((do_invert != 0) ^ (op == OP_AND))
304 command = CPUCTL_RDMSR;
305 command_name = "RDMSR";
308 command = CPUCTL_WRMSR;
309 command_name = "WRMSR";
312 command = CPUCTL_MSRSBIT;
313 command_name = "MSRSBIT";
316 command = CPUCTL_MSRCBIT;
317 command_name = "MSRCBIT";
322 fd = open(dev, op == OP_READ ? O_RDONLY : O_WRONLY);
324 WARN(0, "error opening %s for %s", dev,
325 op == OP_READ ? "reading" : "writing");
328 error = ioctl(fd, command, &args);
330 WARN(0, "ioctl(%s, CPUCTL_%s (%lu))", dev, command_name, command);
335 fprintf(stdout, "MSR 0x%x: 0x%.8x 0x%.8x\n", msr,
336 HIGH(args.data), LOW(args.data));
342 do_update(const char *dev)
347 struct ucode_handler *handler;
350 struct dirent *direntry;
351 char buf[MAXPATHLEN];
353 fd = open(dev, O_RDONLY);
355 WARN(0, "error opening %s for reading", dev);
360 * Find the appropriate handler for device.
362 for (i = 0; i < NHANDLERS; i++)
363 if (handlers[i].probe(fd) == 0)
366 handler = &handlers[i];
368 WARNX(0, "cannot find the appropriate handler for device");
375 * Process every image in specified data directories.
377 SLIST_FOREACH(dir, &datadirs, next) {
378 dirp = opendir(dir->path);
380 WARNX(1, "skipping directory %s: not accessible", dir->path);
383 while ((direntry = readdir(dirp)) != NULL) {
384 if (direntry->d_namlen == 0)
386 error = snprintf(buf, sizeof(buf), "%s/%s", dir->path,
388 if ((unsigned)error >= sizeof(buf))
389 WARNX(0, "skipping %s, buffer too short",
391 if (isdir(buf) != 0) {
392 WARNX(2, "skipping %s: is a directory", buf);
395 handler->update(dev, buf);
397 error = closedir(dirp);
399 WARN(0, "closedir(%s)", dir->path);
405 * Add new data directory to the search list.
408 datadir_add(const char *path)
410 struct datadir *newdir;
412 newdir = (struct datadir *)malloc(sizeof(*newdir));
414 err(EX_OSERR, "cannot allocate memory");
416 SLIST_INSERT_HEAD(&datadirs, newdir, next);
420 main(int argc, char *argv[])
429 cmdarg = ""; /* To keep gcc3 happy. */
431 while ((c = getopt(argc, argv, "d:hi:m:nuv")) != -1) {
466 if ((flags & FLAG_N) == 0)
467 datadir_add(DEFAULT_DATADIR);
469 c = flags & (FLAG_I | FLAG_M | FLAG_U);
472 if (strstr(cmdarg, ",") != NULL)
473 error = do_cpuid_count(cmdarg, dev);
475 error = do_cpuid(cmdarg, dev);
478 error = do_msr(cmdarg, dev);
481 error = do_update(dev);
484 usage(); /* Only one command can be selected. */
486 SLIST_FREE(&datadirs, next, free);
487 return (error == 0 ? 0 : 1);