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$");
48 #include <sys/queue.h>
49 #include <sys/param.h>
50 #include <sys/types.h>
53 #include <sys/ioctl.h>
54 #include <sys/cpuctl.h>
56 #include "cpucontrol.h"
61 int verbosity_level = 0;
63 #define DEFAULT_DATADIR _PATH_LOCALBASE "/share/cpucontrol"
77 #define HIGH(val) (uint32_t)(((val) >> 32) & 0xffffffff)
78 #define LOW(val) (uint32_t)((val) & 0xffffffff)
82 SLIST_ENTRY(datadir) next;
84 static SLIST_HEAD(, datadir) datadirs = SLIST_HEAD_INITIALIZER(datadirs);
86 static struct ucode_handler {
88 ucode_update_t *update;
90 { intel_probe, intel_update },
91 { amd10h_probe, amd10h_update },
92 { amd_probe, amd_update },
93 { via_probe, via_update },
95 #define NHANDLERS (sizeof(handlers) / sizeof(*handlers))
97 static void usage(void);
98 static int do_cpuid(const char *cmdarg, const char *dev);
99 static int do_cpuid_count(const char *cmdarg, const char *dev);
100 static int do_msr(const char *cmdarg, const char *dev);
101 static int do_update(const char *dev);
102 static void datadir_add(const char *path);
109 name = getprogname();
112 fprintf(stderr, "Usage: %s [-vh] [-d datadir] [-m msr[=value] | "
113 "-i level | -i level,level_type | -e | -u] device\n", name);
118 do_cpuid(const char *cmdarg, const char *dev)
121 cpuctl_cpuid_args_t args;
125 assert(cmdarg != NULL);
128 level = strtoul(cmdarg, &endptr, 16);
129 if (*cmdarg == '\0' || *endptr != '\0') {
130 WARNX(0, "incorrect operand: %s", cmdarg);
136 * Fill ioctl argument structure.
139 fd = open(dev, O_RDONLY);
141 WARN(0, "error opening %s for reading", dev);
144 error = ioctl(fd, CPUCTL_CPUID, &args);
146 WARN(0, "ioctl(%s, CPUCTL_CPUID)", dev);
150 fprintf(stdout, "cpuid level 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
151 level, args.data[0], args.data[1], args.data[2], args.data[3]);
157 do_cpuid_count(const char *cmdarg, const char *dev)
159 char *cmdarg1, *endptr, *endptr1;
160 unsigned int level, level_type;
161 cpuctl_cpuid_count_args_t args;
164 assert(cmdarg != NULL);
167 level = strtoul(cmdarg, &endptr, 16);
168 if (*cmdarg == '\0' || *endptr == '\0') {
169 WARNX(0, "incorrect or missing operand: %s", cmdarg);
173 /* Locate the comma... */
174 cmdarg1 = strstr(endptr, ",");
175 /* ... and skip past it */
177 level_type = strtoul(cmdarg1, &endptr1, 16);
178 if (*cmdarg1 == '\0' || *endptr1 != '\0') {
179 WARNX(0, "incorrect or missing operand: %s", cmdarg);
185 * Fill ioctl argument structure.
188 args.level_type = level_type;
189 fd = open(dev, O_RDONLY);
191 WARN(0, "error opening %s for reading", dev);
194 error = ioctl(fd, CPUCTL_CPUID_COUNT, &args);
196 WARN(0, "ioctl(%s, CPUCTL_CPUID_COUNT)", dev);
200 fprintf(stdout, "cpuid level 0x%x, level_type 0x%x: 0x%.8x 0x%.8x "
201 "0x%.8x 0x%.8x\n", level, level_type, args.data[0], args.data[1],
202 args.data[2], args.data[3]);
208 do_msr(const char *cmdarg, const char *dev)
211 cpuctl_msr_args_t args;
214 unsigned long command;
215 int do_invert = 0, op;
217 const char *command_name;
221 assert(cmdarg != NULL);
223 len = strlen(cmdarg);
225 WARNX(0, "MSR register expected");
231 * Parse command string.
233 msr = strtoul(cmdarg, &endptr, 16);
252 if (op != OP_READ) { /* Complex operation. */
261 data = strtoull(p, &endptr, 16);
262 if (*p == '\0' || *endptr != '\0') {
263 WARNX(0, "argument required: %s", cmdarg);
269 if (op == OP_INVAL) {
270 WARNX(0, "invalid operator: %s", cmdarg);
276 * Fill ioctl argument structure.
279 if ((do_invert != 0) ^ (op == OP_AND))
285 command = CPUCTL_RDMSR;
286 command_name = "RDMSR";
289 command = CPUCTL_WRMSR;
290 command_name = "WRMSR";
293 command = CPUCTL_MSRSBIT;
294 command_name = "MSRSBIT";
297 command = CPUCTL_MSRCBIT;
298 command_name = "MSRCBIT";
303 fd = open(dev, op == OP_READ ? O_RDONLY : O_WRONLY);
305 WARN(0, "error opening %s for %s", dev,
306 op == OP_READ ? "reading" : "writing");
309 error = ioctl(fd, command, &args);
311 WARN(0, "ioctl(%s, CPUCTL_%s (%#x))", dev, command_name, msr);
316 fprintf(stdout, "MSR 0x%x: 0x%.8x 0x%.8x\n", msr,
317 HIGH(args.data), LOW(args.data));
323 do_eval_cpu_features(const char *dev)
329 fd = open(dev, O_RDWR);
331 WARN(0, "error opening %s for writing", dev);
334 error = ioctl(fd, CPUCTL_EVAL_CPU_FEATURES, NULL);
336 WARN(0, "ioctl(%s, CPUCTL_EVAL_CPU_FEATURES)", dev);
342 try_a_fw_image(const char *dev_path, int devfd, int fwdfd, const char *dpath,
343 const char *fname, struct ucode_handler *handler)
345 struct ucode_update_params parm;
354 fwfd = openat(fwdfd, fname, O_RDONLY);
356 WARN(0, "openat(%s, %s)", dpath, fname);
360 rc = asprintf(&fw_path, "%s/%s", dpath, fname);
362 WARNX(0, "out of memory");
367 rc = fstat(fwfd, &st);
369 WARN(0, "fstat(%s)", fw_path);
373 if (!S_ISREG(st.st_mode))
375 if (st.st_size <= 0) {
376 WARN(0, "%s: empty", fw_path);
380 fw_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fwfd, 0);
381 if (fw_map == MAP_FAILED) {
382 WARN(0, "mmap(%s)", fw_path);
387 memset(&parm, 0, sizeof(parm));
389 parm.fwimage = fw_map;
390 parm.fwsize = st.st_size;
391 parm.dev_path = dev_path;
392 parm.fw_path = fw_path;
394 handler->update(&parm);
397 if (fw_map != MAP_FAILED)
398 munmap(fw_map, st.st_size);
406 do_update(const char *dev)
411 struct ucode_handler *handler;
414 struct dirent *direntry;
416 fd = open(dev, O_RDONLY);
418 WARN(0, "error opening %s for reading", dev);
423 * Find the appropriate handler for CPU.
425 for (i = 0; i < NHANDLERS; i++)
426 if (handlers[i].probe(fd) == 0)
429 handler = &handlers[i];
431 WARNX(0, "cannot find the appropriate handler for %s", dev);
437 fd = open(dev, O_RDWR);
439 WARN(0, "error opening %s for writing", dev);
444 * Process every image in specified data directories.
446 SLIST_FOREACH(dir, &datadirs, next) {
447 fwdfd = open(dir->path, O_RDONLY);
449 WARN(1, "skipping directory %s: not accessible", dir->path);
452 dirp = fdopendir(fwdfd);
454 WARNX(0, "out of memory");
460 while ((direntry = readdir(dirp)) != NULL) {
461 if (direntry->d_namlen == 0)
463 if (direntry->d_type == DT_DIR)
466 error = try_a_fw_image(dev, fd, fwdfd, dir->path,
467 direntry->d_name, handler);
474 error = closedir(dirp);
476 WARN(0, "closedir(%s)", dir->path);
483 * Add new data directory to the search list.
486 datadir_add(const char *path)
488 struct datadir *newdir;
490 newdir = (struct datadir *)malloc(sizeof(*newdir));
492 err(EX_OSERR, "cannot allocate memory");
494 SLIST_INSERT_HEAD(&datadirs, newdir, next);
498 main(int argc, char *argv[])
508 cmdarg = ""; /* To keep gcc3 happy. */
510 while ((c = getopt(argc, argv, "d:ehi:m:nuv")) != -1) {
548 if ((flags & FLAG_N) == 0)
549 datadir_add(DEFAULT_DATADIR);
551 c = flags & (FLAG_E | FLAG_I | FLAG_M | FLAG_U);
554 if (strstr(cmdarg, ",") != NULL)
555 error = do_cpuid_count(cmdarg, dev);
557 error = do_cpuid(cmdarg, dev);
560 error = do_msr(cmdarg, dev);
563 error = do_update(dev);
566 error = do_eval_cpu_features(dev);
569 usage(); /* Only one command can be selected. */
571 while ((elm = SLIST_FIRST(&datadirs)) != NULL) {
572 SLIST_REMOVE_HEAD(&datadirs, next);
575 return (error == 0 ? 0 : 1);