summaryrefslogtreecommitdiff
path: root/util/msrtool/msrtool.h
blob: 8cba4c74831e777c8bfc683b45972cc0c4231cf3 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * This file is part of msrtool.
 *
 * Copyright (c) 2008 Peter Stuge <peter@stuge.se>
 * Copyright (c) 2009 coresystems GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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.
 */

#ifndef MSRTOOL_H
#define MSRTOOL_H

#include <stdio.h>
#include <stdint.h>
#if (defined(__MACH__) && defined(__APPLE__))
/* DirectHW is available here: https://www.coreboot.org/DirectHW */
#define __DARWIN__
#include <DirectHW/DirectHW.h>
#endif
#if defined(__FreeBSD__)
#include <sys/ioctl.h>
#include <sys/cpuctl.h>
#endif
#include <pci/pci.h>

#define HEXCHARS "0123456789abcdefABCDEF"

enum {
	MSRTYPE_RDONLY,
	MSRTYPE_RDWR,
	MSRTYPE_WRONLY,
	MSRTYPE_EOT
} MsrTypes;

enum {
	PRESENT_RSVD,
	PRESENT_DEC,
	PRESENT_BIN,
	PRESENT_OCT,
	PRESENT_HEX,
	PRESENT_HEXDEC,
	PRESENT_STR,
} PresentTypes;

struct msr {
	uint32_t hi;
	uint32_t lo;
};

struct msrbitvalues {
	const struct msr value;
	const char *text;
};

struct msrbits {
	const uint8_t start;
	const uint8_t size;
	const char *name;
	const char *desc;
	const uint8_t present;
	const struct msrbitvalues bitval[32];
};

struct msrdef {
	const uint32_t addr;
	const uint8_t type;
	const struct msr resetval;
	const char *symbol;
	const char *desc;
	const struct msrbits bits[65];
};

#define MSR1(lo) { 0, (lo) }
#define MSR2(hi,lo) { (hi), (lo) }

#define BITVAL_EOT .text = NULL
#define BITVAL_ISEOT(bv) (NULL == (bv).text)

#define BITS_EOT .size = 0
#define BITS_ISEOT(b) (0 == (b).size)

#define MSR_EOT .type = MSRTYPE_EOT
#define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type)

#define NOBITS {{ BITVAL_EOT }}
#define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS

#define MAX_CORES 8

typedef enum {
	VENDOR_INTEL = 0x756e6547,
	VENDOR_AMD = 0x68747541,
	VENDOR_CENTAUR = 0x746e6543,
} vendor_t;

struct cpuid_t {
	uint8_t family;
	uint8_t model;
	uint8_t stepping;
	uint8_t ext_family;
	uint8_t ext_model;
	vendor_t vendor;
};

struct targetdef {
	const char *name;
	const char *prettyname;
	int (*probe)(const struct targetdef *target, const struct cpuid_t *id);
	const struct msrdef *msrs;
};

#define TARGET_EOT .name = NULL
#define TARGET_ISEOT(t) (NULL == (t).name)


enum SysModes {
	SYS_RDONLY = 0,
	SYS_WRONLY,
	SYS_RDWR
};

struct sysdef {
	const char *name;
	const char *prettyname;
	int (*probe)(const struct sysdef *system);
	int (*open)(uint8_t cpu, enum SysModes mode);
	int (*close)(uint8_t cpu);
	int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val);
};

#define SYSTEM_EOT .name = NULL
#define SYSTEM_ISEOT(s) (NULL == (s).name)

extern const struct sysdef *sys;

extern uint8_t targets_found;
extern const struct targetdef **targets;

extern uint8_t reserved, verbose, quiet;

extern struct pci_access *pacc;

#define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0)
#define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0)

#define SYSERROR(call, addr) do { \
	const struct msrdef *m = findmsrdef(addr); \
	if (m) \
		fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \
	else \
		fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \
} while (0);

/* sys.c */
struct cpuid_t *cpuid(void);
struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);

/* msrutils.c */
void hexprint(FILE *f, const struct msr val, const uint8_t bits);
void strprint(FILE *f, const struct msr val, const uint8_t bits);
int msr_eq(const struct msr a, const struct msr b);
struct msr msr_shl(const struct msr a, const uint8_t bits);
struct msr msr_shr(const struct msr a, const uint8_t bits);
void msr_and(struct msr *a, const struct msr b);
const struct msrdef *findmsrdef(const uint32_t addr);
uint32_t msraddrbyname(const char *name);
void dumpmsrdefs(const struct targetdef *t);
int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
uint8_t str2msr(char *str, struct msr *msr, char **endptr);
void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val);
uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b);



/** system externs **/

/* linux.c */
extern int linux_probe(const struct sysdef *system);
extern int linux_open(uint8_t cpu, enum SysModes mode);
extern int linux_close(uint8_t cpu);
extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);

/* darwin.c */
extern int darwin_probe(const struct sysdef *system);
extern int darwin_open(uint8_t cpu, enum SysModes mode);
extern int darwin_close(uint8_t cpu);
extern int darwin_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);

/* freebsd.c */
extern int freebsd_probe(const struct sysdef *system);
extern int freebsd_open(uint8_t cpu, enum SysModes mode);
extern int freebsd_close(uint8_t cpu);
extern int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);

/** target externs **/

/* geodegx2.c */
extern int geodegx2_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef geodegx2_msrs[];

/* geodelx.c */
extern int geodelx_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef geodelx_msrs[];

/* cs5536.c */
extern int cs5536_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef cs5536_msrs[];

/* k8.c */
extern int k8_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef k8_msrs[];

/* via_c7.c */
extern int via_c7_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef via_c7_msrs[];

/* intel_pentium3_early.c */
extern int intel_pentium3_early_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_pentium3_early_msrs[];

/* intel_pentium3.c */
extern int intel_pentium3_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_pentium3_msrs[];

/* intel_core1.c */
extern int intel_core1_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_core1_msrs[];

/* intel_core2_early.c */
extern int intel_core2_early_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_core2_early_msrs[];

/* intel_core2_later.c */
extern int intel_core2_later_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_core2_later_msrs[];

/* intel_pentium4_early.c */
extern int intel_pentium4_early_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_pentium4_early_msrs[];

/* intel_pentium4_later.c */
extern int intel_pentium4_later_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_pentium4_later_msrs[];

/* intel_pentium_d.c */
extern int intel_pentium_d_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_pentium_d_msrs[];

/* intel_nehalem.c */
extern int intel_nehalem_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_nehalem_msrs[];

/* intel_atom.c */
extern int intel_atom_probe(const struct targetdef *t, const struct cpuid_t *id);
extern const struct msrdef intel_atom_msrs[];

#endif /* MSRTOOL_H */