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