]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/include/asm/msr.h
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / include / asm / msr.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1965aae3
PA
2#ifndef _ASM_X86_MSR_H
3#define _ASM_X86_MSR_H
be7baf80 4
b72e7464 5#include "msr-index.h"
be7baf80 6
8f12dea6 7#ifndef __ASSEMBLY__
c210d249
GOC
8
9#include <asm/asm.h>
10#include <asm/errno.h>
6bc1096d 11#include <asm/cpumask.h>
b72e7464 12#include <uapi/asm/msr.h>
6bc1096d
BP
13
14struct msr {
15 union {
16 struct {
17 u32 l;
18 u32 h;
19 };
20 u64 q;
21 };
22};
c210d249 23
6ede31e0
BP
24struct msr_info {
25 u32 msr_no;
26 struct msr reg;
27 struct msr *msrs;
28 int err;
29};
30
31struct msr_regs_info {
32 u32 *regs;
33 int err;
34};
35
7a9c2dd0
CY
36struct saved_msr {
37 bool valid;
38 struct msr_info info;
39};
40
41struct saved_msrs {
42 unsigned int num;
43 struct saved_msr *array;
44};
45
c210d249 46/*
d4f1b103
JS
47 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
48 * constraint has different meanings. For i386, "A" means exactly
49 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
50 * it means rax *or* rdx.
c210d249
GOC
51 */
52#ifdef CONFIG_X86_64
5a33fcb8
GS
53/* Using 64-bit values saves one instruction clearing the high half of low */
54#define DECLARE_ARGS(val, low, high) unsigned long low, high
55#define EAX_EDX_VAL(val, low, high) ((low) | (high) << 32)
c210d249
GOC
56#define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
57#else
58#define DECLARE_ARGS(val, low, high) unsigned long long val
59#define EAX_EDX_VAL(val, low, high) (val)
c210d249 60#define EAX_EDX_RET(val, low, high) "=A" (val)
8f12dea6
GOC
61#endif
62
7f47d8cc
AK
63#ifdef CONFIG_TRACEPOINTS
64/*
65 * Be very careful with includes. This header is prone to include loops.
66 */
67#include <asm/atomic.h>
68#include <linux/tracepoint-defs.h>
69
70extern struct tracepoint __tracepoint_read_msr;
71extern struct tracepoint __tracepoint_write_msr;
72extern struct tracepoint __tracepoint_rdpmc;
73#define msr_tracepoint_active(t) static_key_false(&(t).key)
5d07c2cc
BP
74extern void do_trace_write_msr(unsigned int msr, u64 val, int failed);
75extern void do_trace_read_msr(unsigned int msr, u64 val, int failed);
76extern void do_trace_rdpmc(unsigned int msr, u64 val, int failed);
7f47d8cc
AK
77#else
78#define msr_tracepoint_active(t) false
5d07c2cc
BP
79static inline void do_trace_write_msr(unsigned int msr, u64 val, int failed) {}
80static inline void do_trace_read_msr(unsigned int msr, u64 val, int failed) {}
81static inline void do_trace_rdpmc(unsigned int msr, u64 val, int failed) {}
7f47d8cc
AK
82#endif
83
a585df8e
BP
84/*
85 * __rdmsr() and __wrmsr() are the two primitives which are the bare minimum MSR
86 * accessors and should not have any tracing or other functionality piggybacking
87 * on them - those are *purely* for accessing MSRs and nothing more. So don't even
88 * think of extending them - you will be slapped with a stinking trout or a frozen
89 * shark will reach you, wherever you are! You've been warned.
90 */
91static inline unsigned long long notrace __rdmsr(unsigned int msr)
be7baf80 92{
c210d249 93 DECLARE_ARGS(val, low, high);
be7baf80 94
fbd70437
AL
95 asm volatile("1: rdmsr\n"
96 "2:\n"
97 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_rdmsr_unsafe)
98 : EAX_EDX_RET(val, low, high) : "c" (msr));
a585df8e 99
c210d249 100 return EAX_EDX_VAL(val, low, high);
be7baf80
TG
101}
102
a585df8e
BP
103static inline void notrace __wrmsr(unsigned int msr, u32 low, u32 high)
104{
105 asm volatile("1: wrmsr\n"
106 "2:\n"
107 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_wrmsr_unsafe)
108 : : "c" (msr), "a"(low), "d" (high) : "memory");
109}
110
111static inline unsigned long long native_read_msr(unsigned int msr)
112{
113 unsigned long long val;
114
115 val = __rdmsr(msr);
116
117 if (msr_tracepoint_active(__tracepoint_read_msr))
118 do_trace_read_msr(msr, val, 0);
119
120 return val;
121}
122
be7baf80
TG
123static inline unsigned long long native_read_msr_safe(unsigned int msr,
124 int *err)
125{
c210d249 126 DECLARE_ARGS(val, low, high);
be7baf80 127
08970fc4 128 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
be7baf80
TG
129 "1:\n\t"
130 ".section .fixup,\"ax\"\n\t"
b828b79f
AL
131 "3: mov %[fault],%[err]\n\t"
132 "xorl %%eax, %%eax\n\t"
133 "xorl %%edx, %%edx\n\t"
134 "jmp 1b\n\t"
be7baf80 135 ".previous\n\t"
abb0ade0 136 _ASM_EXTABLE(2b, 3b)
08970fc4 137 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
0cc0213e 138 : "c" (msr), [fault] "i" (-EIO));
7f47d8cc
AK
139 if (msr_tracepoint_active(__tracepoint_read_msr))
140 do_trace_read_msr(msr, EAX_EDX_VAL(val, low, high), *err);
c210d249 141 return EAX_EDX_VAL(val, low, high);
be7baf80
TG
142}
143
b2c5ea4f 144/* Can be uninlined because referenced by paravirt */
5d07c2cc
BP
145static inline void notrace
146native_write_msr(unsigned int msr, u32 low, u32 high)
b2c5ea4f 147{
a585df8e
BP
148 __wrmsr(msr, low, high);
149
08dd8cd0 150 if (msr_tracepoint_active(__tracepoint_write_msr))
7f47d8cc 151 do_trace_write_msr(msr, ((u64)high << 32 | low), 0);
be7baf80
TG
152}
153
0ca59dd9 154/* Can be uninlined because referenced by paravirt */
5d07c2cc
BP
155static inline int notrace
156native_write_msr_safe(unsigned int msr, u32 low, u32 high)
be7baf80
TG
157{
158 int err;
5d07c2cc 159
08970fc4 160 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
be7baf80
TG
161 "1:\n\t"
162 ".section .fixup,\"ax\"\n\t"
08970fc4 163 "3: mov %[fault],%[err] ; jmp 1b\n\t"
be7baf80 164 ".previous\n\t"
abb0ade0 165 _ASM_EXTABLE(2b, 3b)
08970fc4 166 : [err] "=a" (err)
c9dcda5c 167 : "c" (msr), "0" (low), "d" (high),
0cc0213e 168 [fault] "i" (-EIO)
af2b1c60 169 : "memory");
08dd8cd0 170 if (msr_tracepoint_active(__tracepoint_write_msr))
7f47d8cc 171 do_trace_write_msr(msr, ((u64)high << 32 | low), err);
be7baf80
TG
172 return err;
173}
174
1f975f78
AP
175extern int rdmsr_safe_regs(u32 regs[8]);
176extern int wrmsr_safe_regs(u32 regs[8]);
132ec92f 177
4ea1636b
AL
178/**
179 * rdtsc() - returns the current TSC without ordering constraints
180 *
181 * rdtsc() returns the result of RDTSC as a 64-bit integer. The
182 * only ordering constraint it supplies is the ordering implied by
183 * "asm volatile": it will put the RDTSC in the place you expect. The
184 * CPU can and will speculatively execute that RDTSC, though, so the
185 * results can be non-monotonic if compared on different CPUs.
186 */
187static __always_inline unsigned long long rdtsc(void)
92767af0
IM
188{
189 DECLARE_ARGS(val, low, high);
190
92767af0 191 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
92767af0
IM
192
193 return EAX_EDX_VAL(val, low, high);
194}
195
03b9730b
AL
196/**
197 * rdtsc_ordered() - read the current TSC in program order
198 *
199 * rdtsc_ordered() returns the result of RDTSC as a 64-bit integer.
200 * It is ordered like a load to a global in-memory counter. It should
201 * be impossible to observe non-monotonic rdtsc_unordered() behavior
202 * across multiple CPUs as long as the TSC is synced.
203 */
204static __always_inline unsigned long long rdtsc_ordered(void)
205{
206 /*
207 * The RDTSC instruction is not ordered relative to memory
208 * access. The Intel SDM and the AMD APM are both vague on this
209 * point, but empirically an RDTSC instruction can be
210 * speculatively executed before prior loads. An RDTSC
211 * immediately after an appropriate barrier appears to be
212 * ordered as a normal load, that is, it provides the same
213 * ordering guarantees as reading from a global memory location
214 * that some other imaginary CPU is updating continuously with a
215 * time stamp.
216 */
217 alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC,
218 "lfence", X86_FEATURE_LFENCE_RDTSC);
219 return rdtsc();
220}
221
99770737
IM
222/* Deprecated, keep it for a cycle for easier merging: */
223#define rdtscll(now) do { (now) = rdtsc_ordered(); } while (0)
224
b8d1fae7 225static inline unsigned long long native_read_pmc(int counter)
be7baf80 226{
c210d249
GOC
227 DECLARE_ARGS(val, low, high);
228
229 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
7f47d8cc
AK
230 if (msr_tracepoint_active(__tracepoint_rdpmc))
231 do_trace_rdpmc(counter, EAX_EDX_VAL(val, low, high), 0);
c210d249 232 return EAX_EDX_VAL(val, low, high);
be7baf80
TG
233}
234
235#ifdef CONFIG_PARAVIRT
236#include <asm/paravirt.h>
96a388de 237#else
be7baf80
TG
238#include <linux/errno.h>
239/*
240 * Access to machine-specific registers (available on 586 and better only)
241 * Note: the rd* operations modify the parameters directly (without using
242 * pointer indirection), this allows gcc to optimize better
243 */
244
1423bed2 245#define rdmsr(msr, low, high) \
abb0ade0
JP
246do { \
247 u64 __val = native_read_msr((msr)); \
1423bed2
BP
248 (void)((low) = (u32)__val); \
249 (void)((high) = (u32)(__val >> 32)); \
abb0ade0 250} while (0)
be7baf80 251
5d07c2cc 252static inline void wrmsr(unsigned int msr, u32 low, u32 high)
be7baf80 253{
c9dcda5c 254 native_write_msr(msr, low, high);
be7baf80
TG
255}
256
abb0ade0
JP
257#define rdmsrl(msr, val) \
258 ((val) = native_read_msr((msr)))
be7baf80 259
5d07c2cc 260static inline void wrmsrl(unsigned int msr, u64 val)
47edb651 261{
679bcea8 262 native_write_msr(msr, (u32)(val & 0xffffffffULL), (u32)(val >> 32));
47edb651 263}
be7baf80
TG
264
265/* wrmsr with exception handling */
5d07c2cc 266static inline int wrmsr_safe(unsigned int msr, u32 low, u32 high)
be7baf80 267{
c9dcda5c 268 return native_write_msr_safe(msr, low, high);
be7baf80
TG
269}
270
060feb65 271/* rdmsr with exception handling */
1423bed2 272#define rdmsr_safe(msr, low, high) \
abb0ade0
JP
273({ \
274 int __err; \
275 u64 __val = native_read_msr_safe((msr), &__err); \
1423bed2
BP
276 (*low) = (u32)__val; \
277 (*high) = (u32)(__val >> 32); \
abb0ade0
JP
278 __err; \
279})
be7baf80 280
5d07c2cc 281static inline int rdmsrl_safe(unsigned int msr, unsigned long long *p)
1de87bd4
AK
282{
283 int err;
284
285 *p = native_read_msr_safe(msr, &err);
286 return err;
287}
177fed1e 288
abb0ade0
JP
289#define rdpmc(counter, low, high) \
290do { \
291 u64 _l = native_read_pmc((counter)); \
292 (low) = (u32)_l; \
293 (high) = (u32)(_l >> 32); \
294} while (0)
be7baf80 295
1ff4d58a
AK
296#define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
297
9261e050
AL
298#endif /* !CONFIG_PARAVIRT */
299
cf991de2
AL
300/*
301 * 64-bit version of wrmsr_safe():
302 */
303static inline int wrmsrl_safe(u32 msr, u64 val)
304{
305 return wrmsr_safe(msr, (u32)val, (u32)(val >> 32));
306}
be7baf80 307
1423bed2 308#define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
be7baf80 309
5df97400 310#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
be7baf80 311
50542251
BP
312struct msr *msrs_alloc(void);
313void msrs_free(struct msr *msrs);
22085a66
BP
314int msr_set_bit(u32 msr, u8 bit);
315int msr_clear_bit(u32 msr, u8 bit);
50542251 316
be7baf80 317#ifdef CONFIG_SMP
c6f31932
PA
318int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
319int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
1a6b991a
JP
320int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
321int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
b8a47541
BP
322void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
323void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
be7baf80
TG
324int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
325int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
1a6b991a
JP
326int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
327int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
8b956bf1
PA
328int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
329int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
be7baf80 330#else /* CONFIG_SMP */
c6f31932 331static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
be7baf80
TG
332{
333 rdmsr(msr_no, *l, *h);
c6f31932 334 return 0;
be7baf80 335}
c6f31932 336static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
be7baf80
TG
337{
338 wrmsr(msr_no, l, h);
c6f31932 339 return 0;
be7baf80 340}
1a6b991a
JP
341static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
342{
343 rdmsrl(msr_no, *q);
344 return 0;
345}
346static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
347{
348 wrmsrl(msr_no, q);
349 return 0;
350}
0d0fbbdd 351static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
b034c19f
BP
352 struct msr *msrs)
353{
5d07c2cc 354 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
b034c19f 355}
0d0fbbdd 356static inline void wrmsr_on_cpus(const struct cpumask *m, u32 msr_no,
b034c19f
BP
357 struct msr *msrs)
358{
5d07c2cc 359 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
b034c19f 360}
abb0ade0
JP
361static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
362 u32 *l, u32 *h)
be7baf80
TG
363{
364 return rdmsr_safe(msr_no, l, h);
365}
366static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
367{
368 return wrmsr_safe(msr_no, l, h);
369}
1a6b991a
JP
370static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
371{
372 return rdmsrl_safe(msr_no, q);
373}
374static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
375{
376 return wrmsrl_safe(msr_no, q);
377}
8b956bf1
PA
378static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
379{
380 return rdmsr_safe_regs(regs);
381}
382static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
383{
384 return wrmsr_safe_regs(regs);
385}
be7baf80 386#endif /* CONFIG_SMP */
ff55df53 387#endif /* __ASSEMBLY__ */
1965aae3 388#endif /* _ASM_X86_MSR_H */