2 * Copyright (c) 2008 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"
55 int verbosity_level = 0;
57 #define DEFAULT_DATADIR "/usr/local/share/cpucontrol"
63 #define HIGH(val) (uint32_t)(((val) >> 32) & 0xffffffff)
64 #define LOW(val) (uint32_t)((val) & 0xffffffff)
67 * Macros for freeing SLISTs, probably must be in /sys/queue.h
69 #define SLIST_FREE(head, field, freef) do { \
70 typeof(SLIST_FIRST(head)) __elm0; \
71 typeof(SLIST_FIRST(head)) __elm; \
72 SLIST_FOREACH_SAFE(__elm, (head), field, __elm0) \
73 (void)(freef)(__elm); \
78 SLIST_ENTRY(datadir) next;
80 static SLIST_HEAD(, datadir) datadirs = SLIST_HEAD_INITIALIZER(&datadirs);
82 struct ucode_handler {
84 ucode_update_t *update;
86 { intel_probe, intel_update },
87 { amd_probe, amd_update },
89 #define NHANDLERS (sizeof(handlers) / sizeof(*handlers))
91 static void usage(void);
92 static int isdir(const char *path);
93 static int do_cpuid(const char *cmdarg, const char *dev);
94 static int do_msr(const char *cmdarg, const char *dev);
95 static int do_update(const char *dev);
96 static void datadir_add(const char *path);
103 name = getprogname();
106 fprintf(stderr, "Usage: %s [-vh] [-d datadir] [-m msr[=value] | "
107 "-i level | -u] device\n", name);
112 isdir(const char *path)
117 error = stat(path, &st);
119 WARN(0, "stat(%s)", path);
122 return (st.st_mode & S_IFDIR);
126 do_cpuid(const char *cmdarg, const char *dev)
129 cpuctl_cpuid_args_t args;
133 assert(cmdarg != NULL);
136 level = strtoul(cmdarg, &endptr, 16);
137 if (*cmdarg == '\0' || *endptr != '\0') {
138 WARNX(0, "incorrect operand: %s", cmdarg);
144 * Fill ioctl argument structure.
147 fd = open(dev, O_RDONLY);
149 WARN(0, "error opening %s for reading", dev);
152 error = ioctl(fd, CPUCTL_CPUID, &args);
154 WARN(0, "ioctl(%s, CPUCTL_CPUID)", dev);
158 fprintf(stdout, "cpuid level 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
159 level, args.data[0], args.data[1], args.data[2], args.data[3]);
165 do_msr(const char *cmdarg, const char *dev)
168 cpuctl_msr_args_t args;
174 assert(cmdarg != NULL);
177 p = strchr(cmdarg, '=');
181 args.data = strtoull(p, &endptr, 16);
182 if (*p == '\0' || *endptr != '\0') {
183 WARNX(0, "incorrect MSR value: %s", p);
188 msr = strtoul(cmdarg, &endptr, 16);
189 if (*cmdarg == '\0' || *endptr != '\0') {
190 WARNX(0, "incorrect MSR register: %s", cmdarg);
196 * Fill ioctl argument structure.
199 fd = open(dev, wr == 0 ? O_RDONLY : O_WRONLY);
201 WARN(0, "error opening %s for %s", dev,
202 wr == 0 ? "reading" : "writing");
205 error = ioctl(fd, wr == 0 ? CPUCTL_RDMSR : CPUCTL_WRMSR, &args);
207 WARN(0, "ioctl(%s, %s)", dev,
208 wr == 0 ? "CPUCTL_RDMSR" : "CPUCTL_WRMSR");
213 fprintf(stdout, "MSR 0x%x: 0x%.8x 0x%.8x\n", msr,
214 HIGH(args.data), LOW(args.data));
220 do_update(const char *dev)
225 struct ucode_handler *handler;
228 struct dirent *direntry;
229 char buf[MAXPATHLEN];
231 fd = open(dev, O_RDONLY);
233 WARN(0, "error opening %s for reading", dev);
238 * Find the appropriate handler for device.
240 for (i = 0; i < NHANDLERS; i++)
241 if (handlers[i].probe(fd) == 0)
244 handler = &handlers[i];
246 WARNX(0, "cannot find the appropriate handler for device");
253 * Process every image in specified data directories.
255 SLIST_FOREACH(dir, &datadirs, next) {
256 dirfd = opendir(dir->path);
258 WARNX(1, "skipping directory %s: not accessible", dir->path);
261 while ((direntry = readdir(dirfd)) != NULL) {
262 if (direntry->d_namlen == 0)
264 error = snprintf(buf, sizeof(buf), "%s/%s", dir->path,
266 if ((unsigned)error >= sizeof(buf))
267 WARNX(0, "skipping %s, buffer too short",
269 if (isdir(buf) != 0) {
270 WARNX(2, "skipping %s: is a directory", buf);
273 handler->update(dev, buf);
275 error = closedir(dirfd);
277 WARN(0, "closedir(%s)", dir->path);
283 * Add new data directory to the search list.
286 datadir_add(const char *path)
288 struct datadir *newdir;
290 newdir = (struct datadir *)malloc(sizeof(*newdir));
292 err(EX_OSERR, "cannot allocate memory");
294 SLIST_INSERT_HEAD(&datadirs, newdir, next);
298 main(int argc, char *argv[])
307 cmdarg = ""; /* To keep gcc3 happy. */
310 * Add all default data dirs to the list first.
312 datadir_add(DEFAULT_DATADIR);
313 while ((c = getopt(argc, argv, "d:hi:m:uv")) != -1) {
346 c = flags & (FLAG_I | FLAG_M | FLAG_U);
349 error = do_cpuid(cmdarg, dev);
352 error = do_msr(cmdarg, dev);
355 error = do_update(dev);
358 usage(); /* Only one command can be selected. */
360 SLIST_FREE(&datadirs, next, free);