Deleted Added
full compact
cpucontrol.c (241737) cpucontrol.c (267651)
1/*-
2 * Copyright (c) 2008-2011 Stanislav Sedov <stas@FreeBSD.org>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 */
25
26/*
27 * This utility provides userland access to the cpuctl(4) pseudo-device
28 * features.
29 */
30
31#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2008-2011 Stanislav Sedov <stas@FreeBSD.org>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 */
25
26/*
27 * This utility provides userland access to the cpuctl(4) pseudo-device
28 * features.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/usr.sbin/cpucontrol/cpucontrol.c 241737 2012-10-19 14:49:42Z ed $");
32__FBSDID("$FreeBSD: head/usr.sbin/cpucontrol/cpucontrol.c 267651 2014-06-19 21:54:41Z attilio $");
33
34#include <assert.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <err.h>

--- 53 unchanged lines hidden (view full) ---

94 { amd_probe, amd_update },
95 { via_probe, via_update },
96};
97#define NHANDLERS (sizeof(handlers) / sizeof(*handlers))
98
99static void usage(void);
100static int isdir(const char *path);
101static int do_cpuid(const char *cmdarg, const char *dev);
33
34#include <assert.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <err.h>

--- 53 unchanged lines hidden (view full) ---

94 { amd_probe, amd_update },
95 { via_probe, via_update },
96};
97#define NHANDLERS (sizeof(handlers) / sizeof(*handlers))
98
99static void usage(void);
100static int isdir(const char *path);
101static int do_cpuid(const char *cmdarg, const char *dev);
102static int do_cpuid_count(const char *cmdarg, const char *dev);
102static int do_msr(const char *cmdarg, const char *dev);
103static int do_update(const char *dev);
104static void datadir_add(const char *path);
105
106static void __dead2
107usage(void)
108{
109 const char *name;
110
111 name = getprogname();
112 if (name == NULL)
113 name = "cpuctl";
114 fprintf(stderr, "Usage: %s [-vh] [-d datadir] [-m msr[=value] | "
103static int do_msr(const char *cmdarg, const char *dev);
104static int do_update(const char *dev);
105static void datadir_add(const char *path);
106
107static void __dead2
108usage(void)
109{
110 const char *name;
111
112 name = getprogname();
113 if (name == NULL)
114 name = "cpuctl";
115 fprintf(stderr, "Usage: %s [-vh] [-d datadir] [-m msr[=value] | "
115 "-i level | -u] device\n", name);
116 "-i level | -i level,level_type | -u] device\n", name);
116 exit(EX_USAGE);
117}
118
119static int
120isdir(const char *path)
121{
122 int error;
123 struct stat st;

--- 41 unchanged lines hidden (view full) ---

165 }
166 fprintf(stdout, "cpuid level 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
167 level, args.data[0], args.data[1], args.data[2], args.data[3]);
168 close(fd);
169 return (0);
170}
171
172static int
117 exit(EX_USAGE);
118}
119
120static int
121isdir(const char *path)
122{
123 int error;
124 struct stat st;

--- 41 unchanged lines hidden (view full) ---

166 }
167 fprintf(stdout, "cpuid level 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
168 level, args.data[0], args.data[1], args.data[2], args.data[3]);
169 close(fd);
170 return (0);
171}
172
173static int
174do_cpuid_count(const char *cmdarg, const char *dev)
175{
176 char *cmdarg1, *endptr, *endptr1;
177 unsigned int level, level_type;
178 cpuctl_cpuid_args_t args;
179 int fd, error;
180
181 assert(cmdarg != NULL);
182 assert(dev != NULL);
183
184 level = strtoul(cmdarg, &endptr, 16);
185 if (*cmdarg == '\0' || *endptr == '\0') {
186 WARNX(0, "incorrect or missing operand: %s", cmdarg);
187 usage();
188 /* NOTREACHED */
189 }
190 /* Locate the comma... */
191 cmdarg1 = strstr(endptr, ",");
192 /* ... and skip past it */
193 cmdarg1 += 1;
194 level_type = strtoul(cmdarg1, &endptr1, 16);
195 if (*cmdarg1 == '\0' || *endptr1 != '\0') {
196 WARNX(0, "incorrect or missing operand: %s", cmdarg);
197 usage();
198 /* NOTREACHED */
199 }
200
201 /*
202 * Fill ioctl argument structure.
203 */
204 args.level = level;
205 args.level_type = level_type;
206 fd = open(dev, O_RDONLY);
207 if (fd < 0) {
208 WARN(0, "error opening %s for reading", dev);
209 return (1);
210 }
211 error = ioctl(fd, CPUCTL_CPUID_COUNT, &args);
212 if (error < 0) {
213 WARN(0, "ioctl(%s, CPUCTL_CPUID_COUNT)", dev);
214 close(fd);
215 return (error);
216 }
217 fprintf(stdout, "cpuid level 0x%x, level_type 0x%x: 0x%.8x 0x%.8x "
218 "0x%.8x 0x%.8x\n", level, level_type, args.data[0], args.data[1],
219 args.data[2], args.data[3]);
220 close(fd);
221 return (0);
222}
223
224static int
173do_msr(const char *cmdarg, const char *dev)
174{
175 unsigned int msr;
176 cpuctl_msr_args_t args;
177 size_t len;
178 uint64_t data = 0;
179 unsigned long command;
180 int do_invert = 0, op;

--- 228 unchanged lines hidden (view full) ---

409 if (argc < 1) {
410 usage();
411 /* NOTREACHED */
412 }
413 dev = argv[0];
414 c = flags & (FLAG_I | FLAG_M | FLAG_U);
415 switch (c) {
416 case FLAG_I:
225do_msr(const char *cmdarg, const char *dev)
226{
227 unsigned int msr;
228 cpuctl_msr_args_t args;
229 size_t len;
230 uint64_t data = 0;
231 unsigned long command;
232 int do_invert = 0, op;

--- 228 unchanged lines hidden (view full) ---

461 if (argc < 1) {
462 usage();
463 /* NOTREACHED */
464 }
465 dev = argv[0];
466 c = flags & (FLAG_I | FLAG_M | FLAG_U);
467 switch (c) {
468 case FLAG_I:
417 error = do_cpuid(cmdarg, dev);
469 if (strstr(cmdarg, ",") != NULL)
470 error = do_cpuid_count(cmdarg, dev);
471 else
472 error = do_cpuid(cmdarg, dev);
418 break;
419 case FLAG_M:
420 error = do_msr(cmdarg, dev);
421 break;
422 case FLAG_U:
423 error = do_update(dev);
424 break;
425 default:
426 usage(); /* Only one command can be selected. */
427 }
428 SLIST_FREE(&datadirs, next, free);
429 return (error);
430}
473 break;
474 case FLAG_M:
475 error = do_msr(cmdarg, dev);
476 break;
477 case FLAG_U:
478 error = do_update(dev);
479 break;
480 default:
481 usage(); /* Only one command can be selected. */
482 }
483 SLIST_FREE(&datadirs, next, free);
484 return (error);
485}