10 #include <asm/cpumask.h>
11 #include <uapi/asm/msr.h>
30 struct msr_regs_info
{
42 struct saved_msr
*array
;
46 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
47 * constraint has different meanings. For i386, "A" means exactly
48 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
49 * it means rax *or* rdx.
52 /* Using 64-bit values saves one instruction clearing the high half of low */
53 #define DECLARE_ARGS(val, low, high) unsigned long low, high
54 #define EAX_EDX_VAL(val, low, high) ((low) | (high) << 32)
55 #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
57 #define DECLARE_ARGS(val, low, high) unsigned long long val
58 #define EAX_EDX_VAL(val, low, high) (val)
59 #define EAX_EDX_RET(val, low, high) "=A" (val)
62 #ifdef CONFIG_TRACEPOINTS
64 * Be very careful with includes. This header is prone to include loops.
66 #include <asm/atomic.h>
67 #include <linux/tracepoint-defs.h>
69 extern struct tracepoint __tracepoint_read_msr
;
70 extern struct tracepoint __tracepoint_write_msr
;
71 extern struct tracepoint __tracepoint_rdpmc
;
72 #define msr_tracepoint_active(t) static_key_false(&(t).key)
73 extern void do_trace_write_msr(unsigned msr
, u64 val
, int failed
);
74 extern void do_trace_read_msr(unsigned msr
, u64 val
, int failed
);
75 extern void do_trace_rdpmc(unsigned msr
, u64 val
, int failed
);
77 #define msr_tracepoint_active(t) false
78 static inline void do_trace_write_msr(unsigned msr
, u64 val
, int failed
) {}
79 static inline void do_trace_read_msr(unsigned msr
, u64 val
, int failed
) {}
80 static inline void do_trace_rdpmc(unsigned msr
, u64 val
, int failed
) {}
83 static inline unsigned long long native_read_msr(unsigned int msr
)
85 DECLARE_ARGS(val
, low
, high
);
87 asm volatile("rdmsr" : EAX_EDX_RET(val
, low
, high
) : "c" (msr
));
88 if (msr_tracepoint_active(__tracepoint_read_msr
))
89 do_trace_read_msr(msr
, EAX_EDX_VAL(val
, low
, high
), 0);
90 return EAX_EDX_VAL(val
, low
, high
);
93 static inline unsigned long long native_read_msr_safe(unsigned int msr
,
96 DECLARE_ARGS(val
, low
, high
);
98 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
100 ".section .fixup,\"ax\"\n\t"
101 "3: mov %[fault],%[err] ; jmp 1b\n\t"
104 : [err
] "=r" (*err
), EAX_EDX_RET(val
, low
, high
)
105 : "c" (msr
), [fault
] "i" (-EIO
));
106 if (msr_tracepoint_active(__tracepoint_read_msr
))
107 do_trace_read_msr(msr
, EAX_EDX_VAL(val
, low
, high
), *err
);
108 return EAX_EDX_VAL(val
, low
, high
);
111 static inline void native_write_msr(unsigned int msr
,
112 unsigned low
, unsigned high
)
114 asm volatile("wrmsr" : : "c" (msr
), "a"(low
), "d" (high
) : "memory");
115 if (msr_tracepoint_active(__tracepoint_read_msr
))
116 do_trace_write_msr(msr
, ((u64
)high
<< 32 | low
), 0);
119 /* Can be uninlined because referenced by paravirt */
120 notrace
static inline int native_write_msr_safe(unsigned int msr
,
121 unsigned low
, unsigned high
)
124 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
126 ".section .fixup,\"ax\"\n\t"
127 "3: mov %[fault],%[err] ; jmp 1b\n\t"
131 : "c" (msr
), "0" (low
), "d" (high
),
134 if (msr_tracepoint_active(__tracepoint_read_msr
))
135 do_trace_write_msr(msr
, ((u64
)high
<< 32 | low
), err
);
139 extern int rdmsr_safe_regs(u32 regs
[8]);
140 extern int wrmsr_safe_regs(u32 regs
[8]);
143 * rdtsc() - returns the current TSC without ordering constraints
145 * rdtsc() returns the result of RDTSC as a 64-bit integer. The
146 * only ordering constraint it supplies is the ordering implied by
147 * "asm volatile": it will put the RDTSC in the place you expect. The
148 * CPU can and will speculatively execute that RDTSC, though, so the
149 * results can be non-monotonic if compared on different CPUs.
151 static __always_inline
unsigned long long rdtsc(void)
153 DECLARE_ARGS(val
, low
, high
);
155 asm volatile("rdtsc" : EAX_EDX_RET(val
, low
, high
));
157 return EAX_EDX_VAL(val
, low
, high
);
161 * rdtsc_ordered() - read the current TSC in program order
163 * rdtsc_ordered() returns the result of RDTSC as a 64-bit integer.
164 * It is ordered like a load to a global in-memory counter. It should
165 * be impossible to observe non-monotonic rdtsc_unordered() behavior
166 * across multiple CPUs as long as the TSC is synced.
168 static __always_inline
unsigned long long rdtsc_ordered(void)
171 * The RDTSC instruction is not ordered relative to memory
172 * access. The Intel SDM and the AMD APM are both vague on this
173 * point, but empirically an RDTSC instruction can be
174 * speculatively executed before prior loads. An RDTSC
175 * immediately after an appropriate barrier appears to be
176 * ordered as a normal load, that is, it provides the same
177 * ordering guarantees as reading from a global memory location
178 * that some other imaginary CPU is updating continuously with a
181 alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC
,
182 "lfence", X86_FEATURE_LFENCE_RDTSC
);
186 /* Deprecated, keep it for a cycle for easier merging: */
187 #define rdtscll(now) do { (now) = rdtsc_ordered(); } while (0)
189 static inline unsigned long long native_read_pmc(int counter
)
191 DECLARE_ARGS(val
, low
, high
);
193 asm volatile("rdpmc" : EAX_EDX_RET(val
, low
, high
) : "c" (counter
));
194 if (msr_tracepoint_active(__tracepoint_rdpmc
))
195 do_trace_rdpmc(counter
, EAX_EDX_VAL(val
, low
, high
), 0);
196 return EAX_EDX_VAL(val
, low
, high
);
199 #ifdef CONFIG_PARAVIRT
200 #include <asm/paravirt.h>
202 #include <linux/errno.h>
204 * Access to machine-specific registers (available on 586 and better only)
205 * Note: the rd* operations modify the parameters directly (without using
206 * pointer indirection), this allows gcc to optimize better
209 #define rdmsr(msr, low, high) \
211 u64 __val = native_read_msr((msr)); \
212 (void)((low) = (u32)__val); \
213 (void)((high) = (u32)(__val >> 32)); \
216 static inline void wrmsr(unsigned msr
, unsigned low
, unsigned high
)
218 native_write_msr(msr
, low
, high
);
221 #define rdmsrl(msr, val) \
222 ((val) = native_read_msr((msr)))
224 static inline void wrmsrl(unsigned msr
, u64 val
)
226 native_write_msr(msr
, (u32
)(val
& 0xffffffffULL
), (u32
)(val
>> 32));
229 /* wrmsr with exception handling */
230 static inline int wrmsr_safe(unsigned msr
, unsigned low
, unsigned high
)
232 return native_write_msr_safe(msr
, low
, high
);
235 /* rdmsr with exception handling */
236 #define rdmsr_safe(msr, low, high) \
239 u64 __val = native_read_msr_safe((msr), &__err); \
240 (*low) = (u32)__val; \
241 (*high) = (u32)(__val >> 32); \
245 static inline int rdmsrl_safe(unsigned msr
, unsigned long long *p
)
249 *p
= native_read_msr_safe(msr
, &err
);
253 #define rdpmc(counter, low, high) \
255 u64 _l = native_read_pmc((counter)); \
257 (high) = (u32)(_l >> 32); \
260 #define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
262 #endif /* !CONFIG_PARAVIRT */
265 * 64-bit version of wrmsr_safe():
267 static inline int wrmsrl_safe(u32 msr
, u64 val
)
269 return wrmsr_safe(msr
, (u32
)val
, (u32
)(val
>> 32));
272 #define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
274 #define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
276 struct msr
*msrs_alloc(void);
277 void msrs_free(struct msr
*msrs
);
278 int msr_set_bit(u32 msr
, u8 bit
);
279 int msr_clear_bit(u32 msr
, u8 bit
);
282 int rdmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
);
283 int wrmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
);
284 int rdmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
);
285 int wrmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
);
286 void rdmsr_on_cpus(const struct cpumask
*mask
, u32 msr_no
, struct msr
*msrs
);
287 void wrmsr_on_cpus(const struct cpumask
*mask
, u32 msr_no
, struct msr
*msrs
);
288 int rdmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
);
289 int wrmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
);
290 int rdmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
);
291 int wrmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
);
292 int rdmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8]);
293 int wrmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8]);
294 #else /* CONFIG_SMP */
295 static inline int rdmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
)
297 rdmsr(msr_no
, *l
, *h
);
300 static inline int wrmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
)
305 static inline int rdmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
)
310 static inline int wrmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
)
315 static inline void rdmsr_on_cpus(const struct cpumask
*m
, u32 msr_no
,
318 rdmsr_on_cpu(0, msr_no
, &(msrs
[0].l
), &(msrs
[0].h
));
320 static inline void wrmsr_on_cpus(const struct cpumask
*m
, u32 msr_no
,
323 wrmsr_on_cpu(0, msr_no
, msrs
[0].l
, msrs
[0].h
);
325 static inline int rdmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
,
328 return rdmsr_safe(msr_no
, l
, h
);
330 static inline int wrmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
)
332 return wrmsr_safe(msr_no
, l
, h
);
334 static inline int rdmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
)
336 return rdmsrl_safe(msr_no
, q
);
338 static inline int wrmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
)
340 return wrmsrl_safe(msr_no
, q
);
342 static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8])
344 return rdmsr_safe_regs(regs
);
346 static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8])
348 return wrmsr_safe_regs(regs
);
350 #endif /* CONFIG_SMP */
351 #endif /* __ASSEMBLY__ */
352 #endif /* _ASM_X86_MSR_H */