2020-05-05 23:43:18 +02:00
|
|
|
/* This file is part of msrtool. */
|
2020-05-05 22:49:26 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2008-11-22 18:13:36 +01:00
|
|
|
|
|
|
|
#ifndef MSRTOOL_H
|
|
|
|
#define MSRTOOL_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2009-09-01 12:03:01 +02:00
|
|
|
#if (defined(__MACH__) && defined(__APPLE__))
|
2017-06-05 12:33:23 +02:00
|
|
|
/* DirectHW is available here: https://www.coreboot.org/DirectHW */
|
2009-09-01 12:03:01 +02:00
|
|
|
#define __DARWIN__
|
2011-03-14 10:08:27 +01:00
|
|
|
#include <DirectHW/DirectHW.h>
|
2009-09-01 12:03:01 +02:00
|
|
|
#endif
|
2009-11-28 06:21:42 +01:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/cpuctl.h>
|
|
|
|
#endif
|
2008-11-25 03:03:16 +01:00
|
|
|
#include <pci/pci.h>
|
2008-11-22 18:13:36 +01:00
|
|
|
|
|
|
|
#define HEXCHARS "0123456789abcdefABCDEF"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MSRTYPE_RDONLY,
|
|
|
|
MSRTYPE_RDWR,
|
|
|
|
MSRTYPE_WRONLY,
|
|
|
|
MSRTYPE_EOT
|
|
|
|
} MsrTypes;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PRESENT_RSVD,
|
|
|
|
PRESENT_DEC,
|
|
|
|
PRESENT_BIN,
|
|
|
|
PRESENT_OCT,
|
|
|
|
PRESENT_HEX,
|
2017-01-22 22:19:33 +01:00
|
|
|
PRESENT_HEXDEC,
|
|
|
|
PRESENT_STR,
|
2008-11-22 18:13:36 +01:00
|
|
|
} 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
|
|
|
|
|
2012-07-21 05:29:48 +02:00
|
|
|
typedef enum {
|
2017-01-22 22:19:21 +01:00
|
|
|
VENDOR_INTEL = 0x756e6547,
|
|
|
|
VENDOR_AMD = 0x68747541,
|
2017-01-22 22:20:04 +01:00
|
|
|
VENDOR_CENTAUR = 0x746e6543,
|
2012-07-21 05:29:48 +02:00
|
|
|
} 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;
|
|
|
|
};
|
|
|
|
|
2008-11-22 18:13:36 +01:00
|
|
|
struct targetdef {
|
|
|
|
const char *name;
|
|
|
|
const char *prettyname;
|
2012-07-21 05:29:48 +02:00
|
|
|
int (*probe)(const struct targetdef *target, const struct cpuid_t *id);
|
2008-11-22 18:13:36 +01:00
|
|
|
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;
|
|
|
|
|
2008-11-25 03:03:16 +01:00
|
|
|
extern struct pci_access *pacc;
|
|
|
|
|
2008-11-22 18:13:36 +01:00
|
|
|
#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);
|
2008-11-25 03:03:16 +01:00
|
|
|
struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
|
2008-11-22 18:13:36 +01:00
|
|
|
|
|
|
|
/* msrutils.c */
|
|
|
|
void hexprint(FILE *f, const struct msr val, const uint8_t bits);
|
2017-01-22 22:19:33 +01:00
|
|
|
void strprint(FILE *f, const struct msr val, const uint8_t bits);
|
2008-11-22 18:13:36 +01:00
|
|
|
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);
|
2009-03-08 05:37:39 +01:00
|
|
|
uint32_t msraddrbyname(const char *name);
|
2008-11-22 18:13:36 +01:00
|
|
|
void dumpmsrdefs(const struct targetdef *t);
|
|
|
|
int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
|
2010-01-17 19:33:53 +01:00
|
|
|
uint8_t str2msr(char *str, struct msr *msr, char **endptr);
|
2008-11-22 18:13:36 +01:00
|
|
|
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);
|
|
|
|
|
2009-09-01 12:03:01 +02:00
|
|
|
/* 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);
|
2008-11-22 18:13:36 +01:00
|
|
|
|
2009-11-28 06:21:42 +01:00
|
|
|
/* 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);
|
|
|
|
|
2008-11-22 18:13:36 +01:00
|
|
|
/** target externs **/
|
|
|
|
|
2010-01-15 11:06:39 +01:00
|
|
|
/* geodegx2.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int geodegx2_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2010-01-15 11:06:39 +01:00
|
|
|
extern const struct msrdef geodegx2_msrs[];
|
|
|
|
|
2008-11-22 18:13:36 +01:00
|
|
|
/* geodelx.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int geodelx_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2008-11-22 18:13:36 +01:00
|
|
|
extern const struct msrdef geodelx_msrs[];
|
|
|
|
|
|
|
|
/* cs5536.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int cs5536_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2008-11-22 18:13:36 +01:00
|
|
|
extern const struct msrdef cs5536_msrs[];
|
|
|
|
|
2009-03-08 05:37:39 +01:00
|
|
|
/* k8.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int k8_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2009-03-08 05:37:39 +01:00
|
|
|
extern const struct msrdef k8_msrs[];
|
|
|
|
|
2017-01-22 22:20:04 +01:00
|
|
|
/* via_c7.c */
|
|
|
|
extern int via_c7_probe(const struct targetdef *t, const struct cpuid_t *id);
|
|
|
|
extern const struct msrdef via_c7_msrs[];
|
|
|
|
|
2011-06-20 21:14:22 +02:00
|
|
|
/* intel_pentium3_early.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int intel_pentium3_early_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2011-06-20 21:14:22 +02:00
|
|
|
extern const struct msrdef intel_pentium3_early_msrs[];
|
|
|
|
|
|
|
|
/* intel_pentium3.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int intel_pentium3_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2011-06-20 21:14:22 +02:00
|
|
|
extern const struct msrdef intel_pentium3_msrs[];
|
|
|
|
|
|
|
|
/* intel_core1.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int intel_core1_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2011-06-20 21:14:22 +02:00
|
|
|
extern const struct msrdef intel_core1_msrs[];
|
|
|
|
|
|
|
|
/* intel_core2_early.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int intel_core2_early_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2011-06-20 21:14:22 +02:00
|
|
|
extern const struct msrdef intel_core2_early_msrs[];
|
|
|
|
|
|
|
|
/* intel_core2_later.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int intel_core2_later_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2011-06-20 21:14:22 +02:00
|
|
|
extern const struct msrdef intel_core2_later_msrs[];
|
|
|
|
|
|
|
|
/* intel_pentium4_early.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int intel_pentium4_early_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2011-06-20 21:14:22 +02:00
|
|
|
extern const struct msrdef intel_pentium4_early_msrs[];
|
|
|
|
|
|
|
|
/* intel_pentium4_later.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int intel_pentium4_later_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2011-06-20 21:14:22 +02:00
|
|
|
extern const struct msrdef intel_pentium4_later_msrs[];
|
|
|
|
|
2018-05-15 13:28:54 +02:00
|
|
|
/* 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[];
|
|
|
|
|
2012-07-04 05:35:45 +02:00
|
|
|
/* intel_nehalem.c */
|
2012-07-21 05:29:48 +02:00
|
|
|
extern int intel_nehalem_probe(const struct targetdef *t, const struct cpuid_t *id);
|
2012-07-04 05:35:45 +02:00
|
|
|
extern const struct msrdef intel_nehalem_msrs[];
|
|
|
|
|
2013-06-03 07:30:25 +02:00
|
|
|
/* intel_atom.c */
|
|
|
|
extern int intel_atom_probe(const struct targetdef *t, const struct cpuid_t *id);
|
|
|
|
extern const struct msrdef intel_atom_msrs[];
|
|
|
|
|
2008-11-22 18:13:36 +01:00
|
|
|
#endif /* MSRTOOL_H */
|