2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2011 Stanislav Sedov <stas@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This utility provides userland access to the cpuctl(4) pseudo-device
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
46 #include <sys/queue.h>
47 #include <sys/param.h>
48 #include <sys/types.h>
50 #include <sys/ioctl.h>
51 #include <sys/cpuctl.h>
53 #include "cpucontrol.h"
58 int verbosity_level = 0;
60 #define DEFAULT_DATADIR "/usr/local/share/cpucontrol"
73 #define HIGH(val) (uint32_t)(((val) >> 32) & 0xffffffff)
74 #define LOW(val) (uint32_t)((val) & 0xffffffff)
77 * Macros for freeing SLISTs, probably must be in /sys/queue.h
79 #define SLIST_FREE(head, field, freef) do { \
80 typeof(SLIST_FIRST(head)) __elm0; \
81 typeof(SLIST_FIRST(head)) __elm; \
82 SLIST_FOREACH_SAFE(__elm, (head), field, __elm0) \
83 (void)(freef)(__elm); \
88 SLIST_ENTRY(datadir) next;
90 static SLIST_HEAD(, datadir) datadirs = SLIST_HEAD_INITIALIZER(datadirs);
92 static struct ucode_handler {
94 ucode_update_t *update;
96 { intel_probe, intel_update },
97 { amd10h_probe, amd10h_update },
98 { amd_probe, amd_update },
99 { via_probe, via_update },
101 #define NHANDLERS (sizeof(handlers) / sizeof(*handlers))
103 static void usage(void);
104 static int isdir(const char *path);
105 static int do_cpuid(const char *cmdarg, const char *dev);
106 static int do_cpuid_count(const char *cmdarg, const char *dev);
107 static int do_msr(const char *cmdarg, const char *dev);
108 static int do_update(const char *dev);
109 static void datadir_add(const char *path);
116 name = getprogname();
119 fprintf(stderr, "Usage: %s [-vh] [-d datadir] [-m msr[=value] | "
120 "-i level | -i level,level_type | -u] device\n", name);
125 isdir(const char *path)
130 error = stat(path, &st);
132 WARN(0, "stat(%s)", path);
135 return (st.st_mode & S_IFDIR);
139 do_cpuid(const char *cmdarg, const char *dev)
142 cpuctl_cpuid_args_t args;
146 assert(cmdarg != NULL);
149 level = strtoul(cmdarg, &endptr, 16);
150 if (*cmdarg == '\0' || *endptr != '\0') {
151 WARNX(0, "incorrect operand: %s", cmdarg);
157 * Fill ioctl argument structure.
160 fd = open(dev, O_RDONLY);
162 WARN(0, "error opening %s for reading", dev);
165 error = ioctl(fd, CPUCTL_CPUID, &args);
167 WARN(0, "ioctl(%s, CPUCTL_CPUID)", dev);
171 fprintf(stdout, "cpuid level 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
172 level, args.data[0], args.data[1], args.data[2], args.data[3]);
178 do_cpuid_count(const char *cmdarg, const char *dev)
180 char *cmdarg1, *endptr, *endptr1;
181 unsigned int level, level_type;
182 cpuctl_cpuid_count_args_t args;
185 assert(cmdarg != NULL);
188 level = strtoul(cmdarg, &endptr, 16);
189 if (*cmdarg == '\0' || *endptr == '\0') {
190 WARNX(0, "incorrect or missing operand: %s", cmdarg);
194 /* Locate the comma... */
195 cmdarg1 = strstr(endptr, ",");
196 /* ... and skip past it */
198 level_type = strtoul(cmdarg1, &endptr1, 16);
199 if (*cmdarg1 == '\0' || *endptr1 != '\0') {
200 WARNX(0, "incorrect or missing operand: %s", cmdarg);
206 * Fill ioctl argument structure.
209 args.level_type = level_type;
210 fd = open(dev, O_RDONLY);
212 WARN(0, "error opening %s for reading", dev);
215 error = ioctl(fd, CPUCTL_CPUID_COUNT, &args);
217 WARN(0, "ioctl(%s, CPUCTL_CPUID_COUNT)", dev);
221 fprintf(stdout, "cpuid level 0x%x, level_type 0x%x: 0x%.8x 0x%.8x "
222 "0x%.8x 0x%.8x\n", level, level_type, args.data[0], args.data[1],
223 args.data[2], args.data[3]);
229 do_msr(const char *cmdarg, const char *dev)
232 cpuctl_msr_args_t args;
235 unsigned long command;
236 int do_invert = 0, op;
238 const char *command_name;
242 assert(cmdarg != NULL);
244 len = strlen(cmdarg);
246 WARNX(0, "MSR register expected");
252 * Parse command string.
254 msr = strtoul(cmdarg, &endptr, 16);
273 if (op != OP_READ) { /* Complex operation. */
282 data = strtoull(p, &endptr, 16);
283 if (*p == '\0' || *endptr != '\0') {
284 WARNX(0, "argument required: %s", cmdarg);
290 if (op == OP_INVAL) {
291 WARNX(0, "invalid operator: %s", cmdarg);
297 * Fill ioctl argument structure.
300 if ((do_invert != 0) ^ (op == OP_AND))
306 command = CPUCTL_RDMSR;
307 command_name = "RDMSR";
310 command = CPUCTL_WRMSR;
311 command_name = "WRMSR";
314 command = CPUCTL_MSRSBIT;
315 command_name = "MSRSBIT";
318 command = CPUCTL_MSRCBIT;
319 command_name = "MSRCBIT";
324 fd = open(dev, op == OP_READ ? O_RDONLY : O_WRONLY);
326 WARN(0, "error opening %s for %s", dev,
327 op == OP_READ ? "reading" : "writing");
330 error = ioctl(fd, command, &args);
332 WARN(0, "ioctl(%s, CPUCTL_%s (%lu))", dev, command_name, command);
337 fprintf(stdout, "MSR 0x%x: 0x%.8x 0x%.8x\n", msr,
338 HIGH(args.data), LOW(args.data));
344 do_update(const char *dev)
349 struct ucode_handler *handler;
352 struct dirent *direntry;
353 char buf[MAXPATHLEN];
355 fd = open(dev, O_RDONLY);
357 WARN(0, "error opening %s for reading", dev);
362 * Find the appropriate handler for device.
364 for (i = 0; i < NHANDLERS; i++)
365 if (handlers[i].probe(fd) == 0)
368 handler = &handlers[i];
370 WARNX(0, "cannot find the appropriate handler for device");
377 * Process every image in specified data directories.
379 SLIST_FOREACH(dir, &datadirs, next) {
380 dirp = opendir(dir->path);
382 WARNX(1, "skipping directory %s: not accessible", dir->path);
385 while ((direntry = readdir(dirp)) != NULL) {
386 if (direntry->d_namlen == 0)
388 error = snprintf(buf, sizeof(buf), "%s/%s", dir->path,
390 if ((unsigned)error >= sizeof(buf))
391 WARNX(0, "skipping %s, buffer too short",
393 if (isdir(buf) != 0) {
394 WARNX(2, "skipping %s: is a directory", buf);
397 handler->update(dev, buf);
399 error = closedir(dirp);
401 WARN(0, "closedir(%s)", dir->path);
407 * Add new data directory to the search list.
410 datadir_add(const char *path)
412 struct datadir *newdir;
414 newdir = (struct datadir *)malloc(sizeof(*newdir));
416 err(EX_OSERR, "cannot allocate memory");
418 SLIST_INSERT_HEAD(&datadirs, newdir, next);
422 main(int argc, char *argv[])
431 cmdarg = ""; /* To keep gcc3 happy. */
433 while ((c = getopt(argc, argv, "d:hi:m:nuv")) != -1) {
468 if ((flags & FLAG_N) == 0)
469 datadir_add(DEFAULT_DATADIR);
471 c = flags & (FLAG_I | FLAG_M | FLAG_U);
474 if (strstr(cmdarg, ",") != NULL)
475 error = do_cpuid_count(cmdarg, dev);
477 error = do_cpuid(cmdarg, dev);
480 error = do_msr(cmdarg, dev);
483 error = do_update(dev);
486 usage(); /* Only one command can be selected. */
488 SLIST_FREE(&datadirs, next, free);
489 return (error == 0 ? 0 : 1);