-
Notifications
You must be signed in to change notification settings - Fork 22
/
msr.c
125 lines (99 loc) · 2.81 KB
/
msr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Copyright 2008 Google Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* Quick MSR access, requires linux msr driver
* Tim Hockin <[email protected]>
*/
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include "commands.h"
#include "platform.h"
#ifdef ARCH_X86
static int
open_and_seek(int cpu, unsigned long msr, int mode, int *fd)
{
char dev[512];
snprintf(dev, sizeof(dev), "/dev/cpu/%d/msr", cpu);
*fd = open(dev, mode);
if (*fd < 0) {
fprintf(stderr, "open(\"%s\"): %s\n", dev, strerror(errno));
return -1;
}
if (lseek(*fd, msr, SEEK_SET) == (off_t)-1) {
fprintf(stderr, "lseek(%lu): %s\n", msr, strerror(errno));
close(*fd);
return -1;
}
return 0;
}
static int
rd_msr(int argc, const char *argv[], const struct cmd_info *info)
{
unsigned long msr;
int cpu;
uint64_t data;
int fd;
cpu = strtol(argv[1], NULL, 0);
msr = strtoul(argv[2], NULL, 0);
if (open_and_seek(cpu, msr, O_RDONLY, &fd) < 0) {
return -1;
}
if (read(fd, &data, sizeof(data)) != sizeof(data)) {
fprintf(stderr, "read(): %s\n", strerror(errno));
close(fd);
return -1;
}
close(fd);
printf("0x%016" PRIx64 "\n", data);
return 0;
}
static int
wr_msr(int argc, const char *argv[], const struct cmd_info *info)
{
unsigned long msr;
int cpu;
uint64_t data;
int fd;
int ret = 0;
cpu = strtol(argv[1], NULL, 0);
msr = strtoul(argv[2], NULL, 0);
data = strtoull(argv[3], NULL, 0);
if (open_and_seek(cpu, msr, O_WRONLY, &fd) < 0) {
return -1;
}
if (write(fd, &data, sizeof(data)) != sizeof(data)) {
fprintf(stderr, "write(): %s\n", strerror(errno));
ret = -1;
}
close(fd);
return ret;
}
MAKE_PREREQ_PARAMS_FIXED_ARGS(rd_params, 3, "<cpu> <msr>", 0);
MAKE_PREREQ_PARAMS_FIXED_ARGS(wr_params, 4, "<cpu> <msr> <data>", 0);
static const struct cmd_info msr_cmds[] = {
MAKE_CMD_WITH_PARAMS(rdmsr, &rd_msr, NULL, &rd_params),
MAKE_CMD_WITH_PARAMS(wrmsr, &wr_msr, NULL, &wr_params),
};
MAKE_CMD_GROUP(MSR, "commands to access CPU model specific registers",
msr_cmds);
REGISTER_CMD_GROUP(MSR);
#endif /* #ifdef ARCH_X86 */