xref: /illumos-gate/usr/src/cmd/rdmsr/rdmsr.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2019 Joyent, Inc.
14  */
15 
16 #include <sys/cpuid_drv.h>
17 
18 #include <libintl.h>
19 #include <string.h>
20 #include <locale.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <err.h>
27 
28 static const char dev_cpu_self_cpuid[] = "/dev/" CPUID_SELF_NAME;
29 
30 int
31 main(int argc, char *argv[])
32 {
33 	int ret = EXIT_SUCCESS;
34 	int errflg = 0;
35 	int fd;
36 	int c;
37 
38 	(void) setlocale(LC_ALL, "");
39 	(void) textdomain(TEXT_DOMAIN);
40 
41 	while ((c = getopt(argc, argv, "")) != EOF) {
42 		switch (c) {
43 		case '?':
44 		default:
45 			errflg++;
46 			break;
47 		}
48 	}
49 
50 	if (errflg != 0 || optind == argc) {
51 		fprintf(stderr, gettext("usage: rdmsr [0x<msrnr>...]\n"));
52 		return (EXIT_FAILURE);
53 	}
54 
55 	if ((fd = open(dev_cpu_self_cpuid, O_RDONLY)) == -1) {
56 		err(EXIT_FAILURE, gettext("failed to open %s"),
57 		    dev_cpu_self_cpuid);
58 	}
59 
60 	while (optind != argc) {
61 		struct cpuid_rdmsr crm;
62 		char *p;
63 
64 		errno = 0;
65 		crm.cr_msr_nr = strtoull(argv[optind], &p, 0);
66 
67 		if (errno != 0 || p == argv[optind] || *p != '\0') {
68 			fprintf(stderr,
69 			    gettext("rdmsr: invalid argument '%s'\n"),
70 			    argv[optind]);
71 			exit(EXIT_FAILURE);
72 		}
73 
74 		if (ioctl(fd, CPUID_RDMSR, &crm) != 0) {
75 			warn(gettext("rdmsr of 0x%lx failed"), crm.cr_msr_nr);
76 			ret = EXIT_FAILURE;
77 		} else {
78 			printf("0x%lx: 0x%lx\n", crm.cr_msr_nr, crm.cr_msr_val);
79 		}
80 
81 		optind++;
82 	}
83 
84 	return (ret);
85 }
86