msr.h 7.8 KB
Newer Older
H
H. Peter Anvin 已提交
1 2
#ifndef _ASM_X86_MSR_H
#define _ASM_X86_MSR_H
T
Thomas Gleixner 已提交
3

4
#include "msr-index.h"
T
Thomas Gleixner 已提交
5

6
#ifndef __ASSEMBLY__
7 8 9

#include <asm/asm.h>
#include <asm/errno.h>
10
#include <asm/cpumask.h>
11
#include <uapi/asm/msr.h>
12 13 14 15 16 17 18 19 20 21

struct msr {
	union {
		struct {
			u32 l;
			u32 h;
		};
		u64 q;
	};
};
22

23 24 25 26 27 28 29 30 31 32 33 34
struct msr_info {
	u32 msr_no;
	struct msr reg;
	struct msr *msrs;
	int err;
};

struct msr_regs_info {
	u32 *regs;
	int err;
};

A
Andrew Morton 已提交
35
static inline unsigned long long native_read_tscp(unsigned int *aux)
36 37
{
	unsigned long low, high;
38 39
	asm volatile(".byte 0x0f,0x01,0xf9"
		     : "=a" (low), "=d" (high), "=c" (*aux));
40
	return low | ((u64)high << 32);
41 42
}

43
/*
44 45 46 47
 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
 * constraint has different meanings. For i386, "A" means exactly
 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
 * it means rax *or* rdx.
48 49 50
 */
#ifdef CONFIG_X86_64
#define DECLARE_ARGS(val, low, high)	unsigned low, high
51
#define EAX_EDX_VAL(val, low, high)	((low) | ((u64)(high) << 32))
52 53 54 55 56 57 58
#define EAX_EDX_ARGS(val, low, high)	"a" (low), "d" (high)
#define EAX_EDX_RET(val, low, high)	"=a" (low), "=d" (high)
#else
#define DECLARE_ARGS(val, low, high)	unsigned long long val
#define EAX_EDX_VAL(val, low, high)	(val)
#define EAX_EDX_ARGS(val, low, high)	"A" (val)
#define EAX_EDX_RET(val, low, high)	"=A" (val)
59 60
#endif

T
Thomas Gleixner 已提交
61 62
static inline unsigned long long native_read_msr(unsigned int msr)
{
63
	DECLARE_ARGS(val, low, high);
T
Thomas Gleixner 已提交
64

65 66
	asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
	return EAX_EDX_VAL(val, low, high);
T
Thomas Gleixner 已提交
67 68 69 70 71
}

static inline unsigned long long native_read_msr_safe(unsigned int msr,
						      int *err)
{
72
	DECLARE_ARGS(val, low, high);
T
Thomas Gleixner 已提交
73

74
	asm volatile("2: rdmsr ; xor %[err],%[err]\n"
T
Thomas Gleixner 已提交
75 76
		     "1:\n\t"
		     ".section .fixup,\"ax\"\n\t"
77
		     "3:  mov %[fault],%[err] ; jmp 1b\n\t"
T
Thomas Gleixner 已提交
78
		     ".previous\n\t"
79
		     _ASM_EXTABLE(2b, 3b)
80
		     : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
81
		     : "c" (msr), [fault] "i" (-EIO));
82
	return EAX_EDX_VAL(val, low, high);
T
Thomas Gleixner 已提交
83 84
}

85 86
static inline void native_write_msr(unsigned int msr,
				    unsigned low, unsigned high)
T
Thomas Gleixner 已提交
87
{
88
	asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
T
Thomas Gleixner 已提交
89 90
}

91 92
/* Can be uninlined because referenced by paravirt */
notrace static inline int native_write_msr_safe(unsigned int msr,
93
					unsigned low, unsigned high)
T
Thomas Gleixner 已提交
94 95
{
	int err;
96
	asm volatile("2: wrmsr ; xor %[err],%[err]\n"
T
Thomas Gleixner 已提交
97 98
		     "1:\n\t"
		     ".section .fixup,\"ax\"\n\t"
99
		     "3:  mov %[fault],%[err] ; jmp 1b\n\t"
T
Thomas Gleixner 已提交
100
		     ".previous\n\t"
101
		     _ASM_EXTABLE(2b, 3b)
102
		     : [err] "=a" (err)
103
		     : "c" (msr), "0" (low), "d" (high),
104
		       [fault] "i" (-EIO)
105
		     : "memory");
T
Thomas Gleixner 已提交
106 107 108
	return err;
}

109
extern unsigned long long native_read_tsc(void);
T
Thomas Gleixner 已提交
110

111 112
extern int rdmsr_safe_regs(u32 regs[8]);
extern int wrmsr_safe_regs(u32 regs[8]);
113

I
Ingo Molnar 已提交
114 115 116 117 118 119 120 121 122
static __always_inline unsigned long long __native_read_tsc(void)
{
	DECLARE_ARGS(val, low, high);

	asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));

	return EAX_EDX_VAL(val, low, high);
}

123
static inline unsigned long long native_read_pmc(int counter)
T
Thomas Gleixner 已提交
124
{
125 126 127 128
	DECLARE_ARGS(val, low, high);

	asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
	return EAX_EDX_VAL(val, low, high);
T
Thomas Gleixner 已提交
129 130 131 132
}

#ifdef CONFIG_PARAVIRT
#include <asm/paravirt.h>
133
#else
T
Thomas Gleixner 已提交
134 135 136 137 138 139 140
#include <linux/errno.h>
/*
 * Access to machine-specific registers (available on 586 and better only)
 * Note: the rd* operations modify the parameters directly (without using
 * pointer indirection), this allows gcc to optimize better
 */

B
Borislav Petkov 已提交
141
#define rdmsr(msr, low, high)					\
142 143
do {								\
	u64 __val = native_read_msr((msr));			\
B
Borislav Petkov 已提交
144 145
	(void)((low) = (u32)__val);				\
	(void)((high) = (u32)(__val >> 32));			\
146
} while (0)
T
Thomas Gleixner 已提交
147

148
static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
T
Thomas Gleixner 已提交
149
{
150
	native_write_msr(msr, low, high);
T
Thomas Gleixner 已提交
151 152
}

153 154
#define rdmsrl(msr, val)			\
	((val) = native_read_msr((msr)))
T
Thomas Gleixner 已提交
155

156
#define wrmsrl(msr, val)						\
157
	native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
T
Thomas Gleixner 已提交
158 159

/* wrmsr with exception handling */
160
static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
T
Thomas Gleixner 已提交
161
{
162
	return native_write_msr_safe(msr, low, high);
T
Thomas Gleixner 已提交
163 164
}

165
/* rdmsr with exception handling */
B
Borislav Petkov 已提交
166
#define rdmsr_safe(msr, low, high)				\
167 168 169
({								\
	int __err;						\
	u64 __val = native_read_msr_safe((msr), &__err);	\
B
Borislav Petkov 已提交
170 171
	(*low) = (u32)__val;					\
	(*high) = (u32)(__val >> 32);				\
172 173
	__err;							\
})
T
Thomas Gleixner 已提交
174

A
Andi Kleen 已提交
175 176 177 178 179 180 181
static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
{
	int err;

	*p = native_read_msr_safe(msr, &err);
	return err;
}
182

T
Thomas Gleixner 已提交
183
#define rdtscl(low)						\
184
	((low) = (u32)__native_read_tsc())
T
Thomas Gleixner 已提交
185 186

#define rdtscll(val)						\
187
	((val) = __native_read_tsc())
T
Thomas Gleixner 已提交
188

189 190 191 192 193 194
#define rdpmc(counter, low, high)			\
do {							\
	u64 _l = native_read_pmc((counter));		\
	(low)  = (u32)_l;				\
	(high) = (u32)(_l >> 32);			\
} while (0)
T
Thomas Gleixner 已提交
195

A
Andi Kleen 已提交
196 197
#define rdpmcl(counter, val) ((val) = native_read_pmc(counter))

198 199 200 201 202 203
#define rdtscp(low, high, aux)					\
do {                                                            \
	unsigned long long _val = native_read_tscp(&(aux));     \
	(low) = (u32)_val;                                      \
	(high) = (u32)(_val >> 32);                             \
} while (0)
T
Thomas Gleixner 已提交
204

205
#define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
T
Thomas Gleixner 已提交
206

207
#endif	/* !CONFIG_PARAVIRT */
T
Thomas Gleixner 已提交
208

209 210 211 212 213 214 215
/*
 * 64-bit version of wrmsr_safe():
 */
static inline int wrmsrl_safe(u32 msr, u64 val)
{
	return wrmsr_safe(msr, (u32)val,  (u32)(val >> 32));
}
T
Thomas Gleixner 已提交
216

B
Borislav Petkov 已提交
217
#define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
T
Thomas Gleixner 已提交
218

S
Sheng Yang 已提交
219
#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
T
Thomas Gleixner 已提交
220

221 222
struct msr *msrs_alloc(void);
void msrs_free(struct msr *msrs);
223 224
int msr_set_bit(u32 msr, u8 bit);
int msr_clear_bit(u32 msr, u8 bit);
225

T
Thomas Gleixner 已提交
226
#ifdef CONFIG_SMP
227 228
int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
229 230
int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
231 232
void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
T
Thomas Gleixner 已提交
233 234
int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
235 236
int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
237 238
int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
T
Thomas Gleixner 已提交
239
#else  /*  CONFIG_SMP  */
240
static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
T
Thomas Gleixner 已提交
241 242
{
	rdmsr(msr_no, *l, *h);
243
	return 0;
T
Thomas Gleixner 已提交
244
}
245
static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
T
Thomas Gleixner 已提交
246 247
{
	wrmsr(msr_no, l, h);
248
	return 0;
T
Thomas Gleixner 已提交
249
}
250 251 252 253 254 255 256 257 258 259
static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
{
	rdmsrl(msr_no, *q);
	return 0;
}
static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
{
	wrmsrl(msr_no, q);
	return 0;
}
260
static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
261 262 263 264
				struct msr *msrs)
{
       rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
}
265
static inline void wrmsr_on_cpus(const struct cpumask *m, u32 msr_no,
266 267 268 269
				struct msr *msrs)
{
       wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
}
270 271
static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
				    u32 *l, u32 *h)
T
Thomas Gleixner 已提交
272 273 274 275 276 277 278
{
	return rdmsr_safe(msr_no, l, h);
}
static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
{
	return wrmsr_safe(msr_no, l, h);
}
279 280 281 282 283 284 285 286
static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
{
	return rdmsrl_safe(msr_no, q);
}
static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
{
	return wrmsrl_safe(msr_no, q);
}
287 288 289 290 291 292 293 294
static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
{
	return rdmsr_safe_regs(regs);
}
static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
{
	return wrmsr_safe_regs(regs);
}
T
Thomas Gleixner 已提交
295
#endif  /* CONFIG_SMP */
296
#endif /* __ASSEMBLY__ */
H
H. Peter Anvin 已提交
297
#endif /* _ASM_X86_MSR_H */