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"
70 #define HIGH(val) (uint32_t)(((val) >> 32) & 0xffffffff)
71 #define LOW(val) (uint32_t)((val) & 0xffffffff)
74 * Macros for freeing SLISTs, probably must be in /sys/queue.h
76 #define SLIST_FREE(head, field, freef) do { \
77 typeof(SLIST_FIRST(head)) __elm0; \
78 typeof(SLIST_FIRST(head)) __elm; \
79 SLIST_FOREACH_SAFE(__elm, (head), field, __elm0) \
80 (void)(freef)(__elm); \
85 SLIST_ENTRY(datadir) next;
87 static SLIST_HEAD(, datadir) datadirs = SLIST_HEAD_INITIALIZER(datadirs);
89 static struct ucode_handler {
91 ucode_update_t *update;
93 { intel_probe, intel_update },
94 { amd10h_probe, amd10h_update },
95 { amd_probe, amd_update },
96 { via_probe, via_update },
98 #define NHANDLERS (sizeof(handlers) / sizeof(*handlers))
100 static void usage(void);
101 static int isdir(const char *path);
102 static int do_cpuid(const char *cmdarg, const char *dev);
103 static int do_cpuid_count(const char *cmdarg, const char *dev);
104 static int do_msr(const char *cmdarg, const char *dev);
105 static int do_update(const char *dev);
106 static void datadir_add(const char *path);
113 name = getprogname();
116 fprintf(stderr, "Usage: %s [-vh] [-d datadir] [-m msr[=value] | "
117 "-i level | -i level,level_type | -u] device\n", name);
122 isdir(const char *path)
127 error = stat(path, &st);
129 WARN(0, "stat(%s)", path);
132 return (st.st_mode & S_IFDIR);
136 do_cpuid(const char *cmdarg, const char *dev)
139 cpuctl_cpuid_args_t args;
143 assert(cmdarg != NULL);
146 level = strtoul(cmdarg, &endptr, 16);
147 if (*cmdarg == '\0' || *endptr != '\0') {
148 WARNX(0, "incorrect operand: %s", cmdarg);
154 * Fill ioctl argument structure.
157 fd = open(dev, O_RDONLY);
159 WARN(0, "error opening %s for reading", dev);
162 error = ioctl(fd, CPUCTL_CPUID, &args);
164 WARN(0, "ioctl(%s, CPUCTL_CPUID)", dev);
168 fprintf(stdout, "cpuid level 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
169 level, args.data[0], args.data[1], args.data[2], args.data[3]);
175 do_cpuid_count(const char *cmdarg, const char *dev)
177 char *cmdarg1, *endptr, *endptr1;
178 unsigned int level, level_type;
179 cpuctl_cpuid_count_args_t args;
182 assert(cmdarg != NULL);
185 level = strtoul(cmdarg, &endptr, 16);
186 if (*cmdarg == '\0' || *endptr == '\0') {
187 WARNX(0, "incorrect or missing operand: %s", cmdarg);
191 /* Locate the comma... */
192 cmdarg1 = strstr(endptr, ",");
193 /* ... and skip past it */
195 level_type = strtoul(cmdarg1, &endptr1, 16);
196 if (*cmdarg1 == '\0' || *endptr1 != '\0') {
197 WARNX(0, "incorrect or missing operand: %s", cmdarg);
203 * Fill ioctl argument structure.
206 args.level_type = level_type;
207 fd = open(dev, O_RDONLY);
209 WARN(0, "error opening %s for reading", dev);
212 error = ioctl(fd, CPUCTL_CPUID_COUNT, &args);
214 WARN(0, "ioctl(%s, CPUCTL_CPUID_COUNT)", dev);
218 fprintf(stdout, "cpuid level 0x%x, level_type 0x%x: 0x%.8x 0x%.8x "
219 "0x%.8x 0x%.8x\n", level, level_type, args.data[0], args.data[1],
220 args.data[2], args.data[3]);
226 do_msr(const char *cmdarg, const char *dev)
229 cpuctl_msr_args_t args;
232 unsigned long command;
233 int do_invert = 0, op;
235 const char *command_name;
239 assert(cmdarg != NULL);
241 len = strlen(cmdarg);
243 WARNX(0, "MSR register expected");
249 * Parse command string.
251 msr = strtoul(cmdarg, &endptr, 16);
270 if (op != OP_READ) { /* Complex operation. */
279 data = strtoull(p, &endptr, 16);
280 if (*p == '\0' || *endptr != '\0') {
281 WARNX(0, "argument required: %s", cmdarg);
287 if (op == OP_INVAL) {
288 WARNX(0, "invalid operator: %s", cmdarg);
294 * Fill ioctl argument structure.
297 if ((do_invert != 0) ^ (op == OP_AND))
303 command = CPUCTL_RDMSR;
304 command_name = "RDMSR";
307 command = CPUCTL_WRMSR;
308 command_name = "WRMSR";
311 command = CPUCTL_MSRSBIT;
312 command_name = "MSRSBIT";
315 command = CPUCTL_MSRCBIT;
316 command_name = "MSRCBIT";
321 fd = open(dev, op == OP_READ ? O_RDONLY : O_WRONLY);
323 WARN(0, "error opening %s for %s", dev,
324 op == OP_READ ? "reading" : "writing");
327 error = ioctl(fd, command, &args);
329 WARN(0, "ioctl(%s, CPUCTL_%s (%lu))", dev, command_name, command);
334 fprintf(stdout, "MSR 0x%x: 0x%.8x 0x%.8x\n", msr,
335 HIGH(args.data), LOW(args.data));
341 do_update(const char *dev)
346 struct ucode_handler *handler;
349 struct dirent *direntry;
350 char buf[MAXPATHLEN];
352 fd = open(dev, O_RDONLY);
354 WARN(0, "error opening %s for reading", dev);
359 * Find the appropriate handler for device.
361 for (i = 0; i < NHANDLERS; i++)
362 if (handlers[i].probe(fd) == 0)
365 handler = &handlers[i];
367 WARNX(0, "cannot find the appropriate handler for device");
374 * Process every image in specified data directories.
376 SLIST_FOREACH(dir, &datadirs, next) {
377 dirp = opendir(dir->path);
379 WARNX(1, "skipping directory %s: not accessible", dir->path);
382 while ((direntry = readdir(dirp)) != NULL) {
383 if (direntry->d_namlen == 0)
385 error = snprintf(buf, sizeof(buf), "%s/%s", dir->path,
387 if ((unsigned)error >= sizeof(buf))
388 WARNX(0, "skipping %s, buffer too short",
390 if (isdir(buf) != 0) {
391 WARNX(2, "skipping %s: is a directory", buf);
394 handler->update(dev, buf);
396 error = closedir(dirp);
398 WARN(0, "closedir(%s)", dir->path);
404 * Add new data directory to the search list.
407 datadir_add(const char *path)
409 struct datadir *newdir;
411 newdir = (struct datadir *)malloc(sizeof(*newdir));
413 err(EX_OSERR, "cannot allocate memory");
415 SLIST_INSERT_HEAD(&datadirs, newdir, next);
419 main(int argc, char *argv[])
428 cmdarg = ""; /* To keep gcc3 happy. */
431 * Add all default data dirs to the list first.
433 datadir_add(DEFAULT_DATADIR);
434 while ((c = getopt(argc, argv, "d:hi:m:uv")) != -1) {
467 c = flags & (FLAG_I | FLAG_M | FLAG_U);
470 if (strstr(cmdarg, ",") != NULL)
471 error = do_cpuid_count(cmdarg, dev);
473 error = do_cpuid(cmdarg, dev);
476 error = do_msr(cmdarg, dev);
479 error = do_update(dev);
482 usage(); /* Only one command can be selected. */
484 SLIST_FREE(&datadirs, next, free);
485 return (error == 0 ? 0 : 1);