blob: e6a707eb508167dea3eda93a440cee3ba15f39db [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_MSR_H
2#define _ASM_X86_MSR_H
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +02003
Borislav Petkovb72e7462015-06-04 18:55:26 +02004#include "msr-index.h"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +02005
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +01006#ifndef __ASSEMBLY__
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +01007
8#include <asm/asm.h>
9#include <asm/errno.h>
Borislav Petkov6bc10962009-05-22 12:12:01 +020010#include <asm/cpumask.h>
Borislav Petkovb72e7462015-06-04 18:55:26 +020011#include <uapi/asm/msr.h>
Borislav Petkov6bc10962009-05-22 12:12:01 +020012
13struct msr {
14 union {
15 struct {
16 u32 l;
17 u32 h;
18 };
19 u64 q;
20 };
21};
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010022
Borislav Petkov6ede31e2009-12-17 00:16:25 +010023struct msr_info {
24 u32 msr_no;
25 struct msr reg;
26 struct msr *msrs;
27 int err;
28};
29
30struct msr_regs_info {
31 u32 *regs;
32 int err;
33};
34
Andrew Morton1e160cc2008-01-30 13:31:17 +010035static inline unsigned long long native_read_tscp(unsigned int *aux)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010036{
37 unsigned long low, high;
Joe Perchesabb0ade2008-03-23 01:02:51 -070038 asm volatile(".byte 0x0f,0x01,0xf9"
39 : "=a" (low), "=d" (high), "=c" (*aux));
Max Asbock41aefdc2008-06-25 14:45:28 -070040 return low | ((u64)high << 32);
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010041}
42
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010043/*
Jike Songd4f1b102008-10-17 13:25:07 +080044 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
45 * constraint has different meanings. For i386, "A" means exactly
46 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
47 * it means rax *or* rdx.
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010048 */
49#ifdef CONFIG_X86_64
50#define DECLARE_ARGS(val, low, high) unsigned low, high
Joe Perchesabb0ade2008-03-23 01:02:51 -070051#define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010052#define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
53#define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
54#else
55#define DECLARE_ARGS(val, low, high) unsigned long long val
56#define EAX_EDX_VAL(val, low, high) (val)
57#define EAX_EDX_ARGS(val, low, high) "A" (val)
58#define EAX_EDX_RET(val, low, high) "=A" (val)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010059#endif
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020060
61static inline unsigned long long native_read_msr(unsigned int msr)
62{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010063 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020064
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010065 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
66 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020067}
68
69static inline unsigned long long native_read_msr_safe(unsigned int msr,
70 int *err)
71{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010072 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020073
H. Peter Anvin08970fc2008-08-25 22:39:15 -070074 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020075 "1:\n\t"
76 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -070077 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020078 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -070079 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -070080 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
H. Peter Anvin0cc02132009-08-31 14:23:29 -070081 : "c" (msr), [fault] "i" (-EIO));
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010082 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020083}
84
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010085static inline void native_write_msr(unsigned int msr,
86 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020087{
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -040088 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020089}
90
Frederic Weisbecker0ca59dd2008-12-24 23:30:02 +010091/* Can be uninlined because referenced by paravirt */
92notrace static inline int native_write_msr_safe(unsigned int msr,
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010093 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020094{
95 int err;
H. Peter Anvin08970fc2008-08-25 22:39:15 -070096 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020097 "1:\n\t"
98 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -070099 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200100 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -0700101 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700102 : [err] "=a" (err)
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100103 : "c" (msr), "0" (low), "d" (high),
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700104 [fault] "i" (-EIO)
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -0400105 : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200106 return err;
107}
108
Ingo Molnarcdc79572008-01-30 13:32:39 +0100109extern unsigned long long native_read_tsc(void);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200110
Andre Przywara1f975f72012-06-01 16:52:35 +0200111extern int rdmsr_safe_regs(u32 regs[8]);
112extern int wrmsr_safe_regs(u32 regs[8]);
Borislav Petkov132ec922009-08-31 09:50:09 +0200113
Ingo Molnar92767af2008-01-30 13:32:40 +0100114static __always_inline unsigned long long __native_read_tsc(void)
115{
116 DECLARE_ARGS(val, low, high);
117
Ingo Molnar92767af2008-01-30 13:32:40 +0100118 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
Ingo Molnar92767af2008-01-30 13:32:40 +0100119
120 return EAX_EDX_VAL(val, low, high);
121}
122
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100123static inline unsigned long long native_read_pmc(int counter)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200124{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100125 DECLARE_ARGS(val, low, high);
126
127 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
128 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200129}
130
131#ifdef CONFIG_PARAVIRT
132#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200133#else
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200134#include <linux/errno.h>
135/*
136 * Access to machine-specific registers (available on 586 and better only)
137 * Note: the rd* operations modify the parameters directly (without using
138 * pointer indirection), this allows gcc to optimize better
139 */
140
Borislav Petkov1423bed2013-03-04 21:16:19 +0100141#define rdmsr(msr, low, high) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700142do { \
143 u64 __val = native_read_msr((msr)); \
Borislav Petkov1423bed2013-03-04 21:16:19 +0100144 (void)((low) = (u32)__val); \
145 (void)((high) = (u32)(__val >> 32)); \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700146} while (0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200147
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100148static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200149{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100150 native_write_msr(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200151}
152
Joe Perchesabb0ade2008-03-23 01:02:51 -0700153#define rdmsrl(msr, val) \
154 ((val) = native_read_msr((msr)))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200155
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100156#define wrmsrl(msr, val) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700157 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200158
159/* wrmsr with exception handling */
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100160static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200161{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100162 return native_write_msr_safe(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200163}
164
H. Peter Anvin060feb62012-04-19 17:07:34 -0700165/* rdmsr with exception handling */
Borislav Petkov1423bed2013-03-04 21:16:19 +0100166#define rdmsr_safe(msr, low, high) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700167({ \
168 int __err; \
169 u64 __val = native_read_msr_safe((msr), &__err); \
Borislav Petkov1423bed2013-03-04 21:16:19 +0100170 (*low) = (u32)__val; \
171 (*high) = (u32)(__val >> 32); \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700172 __err; \
173})
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200174
Andi Kleen1de87bd2008-03-22 10:59:28 +0100175static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
176{
177 int err;
178
179 *p = native_read_msr_safe(msr, &err);
180 return err;
181}
Borislav Petkov177fed12009-08-31 09:50:10 +0200182
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200183#define rdtscl(low) \
Ken Chen205516c2008-12-16 00:32:21 -0800184 ((low) = (u32)__native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200185
186#define rdtscll(val) \
Ken Chen205516c2008-12-16 00:32:21 -0800187 ((val) = __native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200188
Joe Perchesabb0ade2008-03-23 01:02:51 -0700189#define rdpmc(counter, low, high) \
190do { \
191 u64 _l = native_read_pmc((counter)); \
192 (low) = (u32)_l; \
193 (high) = (u32)(_l >> 32); \
194} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100195
Andi Kleen1ff4d582012-06-05 17:56:50 -0700196#define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
197
Joe Perchesabb0ade2008-03-23 01:02:51 -0700198#define rdtscp(low, high, aux) \
199do { \
200 unsigned long long _val = native_read_tscp(&(aux)); \
201 (low) = (u32)_val; \
202 (high) = (u32)(_val >> 32); \
203} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100204
205#define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
206
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200207#endif /* !CONFIG_PARAVIRT */
208
Andy Lutomirskicf991de2015-06-04 17:13:44 -0700209/*
210 * 64-bit version of wrmsr_safe():
211 */
212static inline int wrmsrl_safe(u32 msr, u64 val)
213{
214 return wrmsr_safe(msr, (u32)val, (u32)(val >> 32));
215}
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200216
Borislav Petkov1423bed2013-03-04 21:16:19 +0100217#define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200218
Sheng Yang5df97402009-12-16 13:48:04 +0800219#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200220
Borislav Petkov50542252009-12-11 18:14:40 +0100221struct msr *msrs_alloc(void);
222void msrs_free(struct msr *msrs);
Borislav Petkov22085a62014-03-09 18:05:23 +0100223int msr_set_bit(u32 msr, u8 bit);
224int msr_clear_bit(u32 msr, u8 bit);
Borislav Petkov50542252009-12-11 18:14:40 +0100225
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200226#ifdef CONFIG_SMP
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700227int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
228int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
Jacob Pan1a6b9912013-10-11 16:54:58 -0700229int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
230int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
Borislav Petkovb8a47542009-07-30 11:10:02 +0200231void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
232void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200233int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
234int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
Jacob Pan1a6b9912013-10-11 16:54:58 -0700235int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
236int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
H. Peter Anvin8b956bf2009-08-31 14:13:48 -0700237int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
238int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200239#else /* CONFIG_SMP */
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700240static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200241{
242 rdmsr(msr_no, *l, *h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700243 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200244}
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700245static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200246{
247 wrmsr(msr_no, l, h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700248 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200249}
Jacob Pan1a6b9912013-10-11 16:54:58 -0700250static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
251{
252 rdmsrl(msr_no, *q);
253 return 0;
254}
255static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
256{
257 wrmsrl(msr_no, q);
258 return 0;
259}
Rusty Russell0d0fbbd2009-11-05 22:45:41 +1030260static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
Borislav Petkovb034c192009-05-22 13:52:19 +0200261 struct msr *msrs)
262{
263 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
264}
Rusty Russell0d0fbbd2009-11-05 22:45:41 +1030265static inline void wrmsr_on_cpus(const struct cpumask *m, u32 msr_no,
Borislav Petkovb034c192009-05-22 13:52:19 +0200266 struct msr *msrs)
267{
268 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
269}
Joe Perchesabb0ade2008-03-23 01:02:51 -0700270static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
271 u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200272{
273 return rdmsr_safe(msr_no, l, h);
274}
275static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
276{
277 return wrmsr_safe(msr_no, l, h);
278}
Jacob Pan1a6b9912013-10-11 16:54:58 -0700279static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
280{
281 return rdmsrl_safe(msr_no, q);
282}
283static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
284{
285 return wrmsrl_safe(msr_no, q);
286}
H. Peter Anvin8b956bf2009-08-31 14:13:48 -0700287static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
288{
289 return rdmsr_safe_regs(regs);
290}
291static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
292{
293 return wrmsr_safe_regs(regs);
294}
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200295#endif /* CONFIG_SMP */
H. Peter Anvinff55df52009-08-31 14:16:57 -0700296#endif /* __ASSEMBLY__ */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700297#endif /* _ASM_X86_MSR_H */