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 int msr
, u64 val
, int failed
);
74 extern void do_trace_read_msr(unsigned int msr
, u64 val
, int failed
);
75 extern void do_trace_rdpmc(unsigned int msr
, u64 val
, int failed
);
77 #define msr_tracepoint_active(t) false
78 static inline void do_trace_write_msr(unsigned int msr
, u64 val
, int failed
) {}
79 static inline void do_trace_read_msr(unsigned int msr
, u64 val
, int failed
) {}
80 static inline void do_trace_rdpmc(unsigned int msr
, u64 val
, int failed
) {}
84 * __rdmsr() and __wrmsr() are the two primitives which are the bare minimum MSR
85 * accessors and should not have any tracing or other functionality piggybacking
86 * on them - those are *purely* for accessing MSRs and nothing more. So don't even
87 * think of extending them - you will be slapped with a stinking trout or a frozen
88 * shark will reach you, wherever you are! You've been warned.
90 static inline unsigned long long notrace
__rdmsr(unsigned int msr
)
92 DECLARE_ARGS(val
, low
, high
);
94 asm volatile("1: rdmsr\n"
96 _ASM_EXTABLE_HANDLE(1b
, 2b
, ex_handler_rdmsr_unsafe
)
97 : EAX_EDX_RET(val
, low
, high
) : "c" (msr
));
99 return EAX_EDX_VAL(val
, low
, high
);
102 static inline void notrace
__wrmsr(unsigned int msr
, u32 low
, u32 high
)
104 asm volatile("1: wrmsr\n"
106 _ASM_EXTABLE_HANDLE(1b
, 2b
, ex_handler_wrmsr_unsafe
)
107 : : "c" (msr
), "a"(low
), "d" (high
) : "memory");
110 static inline unsigned long long native_read_msr(unsigned int msr
)
112 unsigned long long val
;
116 if (msr_tracepoint_active(__tracepoint_read_msr
))
117 do_trace_read_msr(msr
, val
, 0);
122 static inline unsigned long long native_read_msr_safe(unsigned int msr
,
125 DECLARE_ARGS(val
, low
, high
);
127 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
129 ".section .fixup,\"ax\"\n\t"
130 "3: mov %[fault],%[err]\n\t"
131 "xorl %%eax, %%eax\n\t"
132 "xorl %%edx, %%edx\n\t"
136 : [err
] "=r" (*err
), EAX_EDX_RET(val
, low
, high
)
137 : "c" (msr
), [fault
] "i" (-EIO
));
138 if (msr_tracepoint_active(__tracepoint_read_msr
))
139 do_trace_read_msr(msr
, EAX_EDX_VAL(val
, low
, high
), *err
);
140 return EAX_EDX_VAL(val
, low
, high
);
143 /* Can be uninlined because referenced by paravirt */
144 static inline void notrace
145 native_write_msr(unsigned int msr
, u32 low
, u32 high
)
147 __wrmsr(msr
, low
, high
);
149 if (msr_tracepoint_active(__tracepoint_write_msr
))
150 do_trace_write_msr(msr
, ((u64
)high
<< 32 | low
), 0);
153 /* Can be uninlined because referenced by paravirt */
154 static inline int notrace
155 native_write_msr_safe(unsigned int msr
, u32 low
, u32 high
)
159 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
161 ".section .fixup,\"ax\"\n\t"
162 "3: mov %[fault],%[err] ; jmp 1b\n\t"
166 : "c" (msr
), "0" (low
), "d" (high
),
169 if (msr_tracepoint_active(__tracepoint_write_msr
))
170 do_trace_write_msr(msr
, ((u64
)high
<< 32 | low
), err
);
174 extern int rdmsr_safe_regs(u32 regs
[8]);
175 extern int wrmsr_safe_regs(u32 regs
[8]);
178 * rdtsc() - returns the current TSC without ordering constraints
180 * rdtsc() returns the result of RDTSC as a 64-bit integer. The
181 * only ordering constraint it supplies is the ordering implied by
182 * "asm volatile": it will put the RDTSC in the place you expect. The
183 * CPU can and will speculatively execute that RDTSC, though, so the
184 * results can be non-monotonic if compared on different CPUs.
186 static __always_inline
unsigned long long rdtsc(void)
188 DECLARE_ARGS(val
, low
, high
);
190 asm volatile("rdtsc" : EAX_EDX_RET(val
, low
, high
));
192 return EAX_EDX_VAL(val
, low
, high
);
196 * rdtsc_ordered() - read the current TSC in program order
198 * rdtsc_ordered() returns the result of RDTSC as a 64-bit integer.
199 * It is ordered like a load to a global in-memory counter. It should
200 * be impossible to observe non-monotonic rdtsc_unordered() behavior
201 * across multiple CPUs as long as the TSC is synced.
203 static __always_inline
unsigned long long rdtsc_ordered(void)
206 * The RDTSC instruction is not ordered relative to memory
207 * access. The Intel SDM and the AMD APM are both vague on this
208 * point, but empirically an RDTSC instruction can be
209 * speculatively executed before prior loads. An RDTSC
210 * immediately after an appropriate barrier appears to be
211 * ordered as a normal load, that is, it provides the same
212 * ordering guarantees as reading from a global memory location
213 * that some other imaginary CPU is updating continuously with a
216 alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC
,
217 "lfence", X86_FEATURE_LFENCE_RDTSC
);
221 /* Deprecated, keep it for a cycle for easier merging: */
222 #define rdtscll(now) do { (now) = rdtsc_ordered(); } while (0)
224 static inline unsigned long long native_read_pmc(int counter
)
226 DECLARE_ARGS(val
, low
, high
);
228 asm volatile("rdpmc" : EAX_EDX_RET(val
, low
, high
) : "c" (counter
));
229 if (msr_tracepoint_active(__tracepoint_rdpmc
))
230 do_trace_rdpmc(counter
, EAX_EDX_VAL(val
, low
, high
), 0);
231 return EAX_EDX_VAL(val
, low
, high
);
234 #ifdef CONFIG_PARAVIRT
235 #include <asm/paravirt.h>
237 #include <linux/errno.h>
239 * Access to machine-specific registers (available on 586 and better only)
240 * Note: the rd* operations modify the parameters directly (without using
241 * pointer indirection), this allows gcc to optimize better
244 #define rdmsr(msr, low, high) \
246 u64 __val = native_read_msr((msr)); \
247 (void)((low) = (u32)__val); \
248 (void)((high) = (u32)(__val >> 32)); \
251 static inline void wrmsr(unsigned int msr
, u32 low
, u32 high
)
253 native_write_msr(msr
, low
, high
);
256 #define rdmsrl(msr, val) \
257 ((val) = native_read_msr((msr)))
259 static inline void wrmsrl(unsigned int msr
, u64 val
)
261 native_write_msr(msr
, (u32
)(val
& 0xffffffffULL
), (u32
)(val
>> 32));
264 /* wrmsr with exception handling */
265 static inline int wrmsr_safe(unsigned int msr
, u32 low
, u32 high
)
267 return native_write_msr_safe(msr
, low
, high
);
270 /* rdmsr with exception handling */
271 #define rdmsr_safe(msr, low, high) \
274 u64 __val = native_read_msr_safe((msr), &__err); \
275 (*low) = (u32)__val; \
276 (*high) = (u32)(__val >> 32); \
280 static inline int rdmsrl_safe(unsigned int msr
, unsigned long long *p
)
284 *p
= native_read_msr_safe(msr
, &err
);
288 #define rdpmc(counter, low, high) \
290 u64 _l = native_read_pmc((counter)); \
292 (high) = (u32)(_l >> 32); \
295 #define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
297 #endif /* !CONFIG_PARAVIRT */
300 * 64-bit version of wrmsr_safe():
302 static inline int wrmsrl_safe(u32 msr
, u64 val
)
304 return wrmsr_safe(msr
, (u32
)val
, (u32
)(val
>> 32));
307 #define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
309 #define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
311 struct msr
*msrs_alloc(void);
312 void msrs_free(struct msr
*msrs
);
313 int msr_set_bit(u32 msr
, u8 bit
);
314 int msr_clear_bit(u32 msr
, u8 bit
);
317 int rdmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
);
318 int wrmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
);
319 int rdmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
);
320 int wrmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
);
321 void rdmsr_on_cpus(const struct cpumask
*mask
, u32 msr_no
, struct msr
*msrs
);
322 void wrmsr_on_cpus(const struct cpumask
*mask
, u32 msr_no
, struct msr
*msrs
);
323 int rdmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
);
324 int wrmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
);
325 int rdmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
);
326 int wrmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
);
327 int rdmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8]);
328 int wrmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8]);
329 #else /* CONFIG_SMP */
330 static inline int rdmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
)
332 rdmsr(msr_no
, *l
, *h
);
335 static inline int wrmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
)
340 static inline int rdmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
)
345 static inline int wrmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
)
350 static inline void rdmsr_on_cpus(const struct cpumask
*m
, u32 msr_no
,
353 rdmsr_on_cpu(0, msr_no
, &(msrs
[0].l
), &(msrs
[0].h
));
355 static inline void wrmsr_on_cpus(const struct cpumask
*m
, u32 msr_no
,
358 wrmsr_on_cpu(0, msr_no
, msrs
[0].l
, msrs
[0].h
);
360 static inline int rdmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
,
363 return rdmsr_safe(msr_no
, l
, h
);
365 static inline int wrmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
)
367 return wrmsr_safe(msr_no
, l
, h
);
369 static inline int rdmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
)
371 return rdmsrl_safe(msr_no
, q
);
373 static inline int wrmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
)
375 return wrmsrl_safe(msr_no
, q
);
377 static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8])
379 return rdmsr_safe_regs(regs
);
381 static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8])
383 return wrmsr_safe_regs(regs
);
385 #endif /* CONFIG_SMP */
386 #endif /* __ASSEMBLY__ */
387 #endif /* _ASM_X86_MSR_H */