1#ifndef _ASM_X86_MSR_H
2#define _ASM_X86_MSR_H
3
4#include "msr-index.h"
5
6#ifndef __ASSEMBLY__
7
8#include <asm/asm.h>
9#include <asm/errno.h>
10#include <asm/cpumask.h>
11#include <uapi/asm/msr.h>
12
13struct msr {
14 union {
15 struct {
16 u32 l;
17 u32 h;
18 };
19 u64 q;
20 };
21};
22
23struct 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
35struct saved_msr {
36 bool valid;
37 struct msr_info info;
38};
39
40struct saved_msrs {
41 unsigned int num;
42 struct saved_msr *array;
43};
44
45
46
47
48
49
50
51#ifdef CONFIG_X86_64
52
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)
56#else
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)
60#endif
61
62#ifdef CONFIG_TRACEPOINTS
63
64
65
66#include <asm/atomic.h>
67#include <linux/tracepoint-defs.h>
68
69extern struct tracepoint __tracepoint_read_msr;
70extern struct tracepoint __tracepoint_write_msr;
71extern struct tracepoint __tracepoint_rdpmc;
72#define msr_tracepoint_active(t) static_key_false(&(t).key)
73extern void do_trace_write_msr(unsigned msr, u64 val, int failed);
74extern void do_trace_read_msr(unsigned msr, u64 val, int failed);
75extern void do_trace_rdpmc(unsigned msr, u64 val, int failed);
76#else
77#define msr_tracepoint_active(t) false
78static inline void do_trace_write_msr(unsigned msr, u64 val, int failed) {}
79static inline void do_trace_read_msr(unsigned msr, u64 val, int failed) {}
80static inline void do_trace_rdpmc(unsigned msr, u64 val, int failed) {}
81#endif
82
83static inline unsigned long long native_read_msr(unsigned int msr)
84{
85 DECLARE_ARGS(val, low, high);
86
87 asm volatile("1: rdmsr\n"
88 "2:\n"
89 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_rdmsr_unsafe)
90 : EAX_EDX_RET(val, low, high) : "c" (msr));
91 if (msr_tracepoint_active(__tracepoint_read_msr))
92 do_trace_read_msr(msr, EAX_EDX_VAL(val, low, high), 0);
93 return EAX_EDX_VAL(val, low, high);
94}
95
96static inline unsigned long long native_read_msr_safe(unsigned int msr,
97 int *err)
98{
99 DECLARE_ARGS(val, low, high);
100
101 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
102 "1:\n\t"
103 ".section .fixup,\"ax\"\n\t"
104 "3: mov %[fault],%[err]\n\t"
105 "xorl %%eax, %%eax\n\t"
106 "xorl %%edx, %%edx\n\t"
107 "jmp 1b\n\t"
108 ".previous\n\t"
109 _ASM_EXTABLE(2b, 3b)
110 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
111 : "c" (msr), [fault] "i" (-EIO));
112 if (msr_tracepoint_active(__tracepoint_read_msr))
113 do_trace_read_msr(msr, EAX_EDX_VAL(val, low, high), *err);
114 return EAX_EDX_VAL(val, low, high);
115}
116
117
118notrace static inline void native_write_msr(unsigned int msr,
119 unsigned low, unsigned high)
120{
121 asm volatile("1: wrmsr\n"
122 "2:\n"
123 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_wrmsr_unsafe)
124 : : "c" (msr), "a"(low), "d" (high) : "memory");
125 if (msr_tracepoint_active(__tracepoint_write_msr))
126 do_trace_write_msr(msr, ((u64)high << 32 | low), 0);
127}
128
129
130notrace static inline int native_write_msr_safe(unsigned int msr,
131 unsigned low, unsigned high)
132{
133 int err;
134 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
135 "1:\n\t"
136 ".section .fixup,\"ax\"\n\t"
137 "3: mov %[fault],%[err] ; jmp 1b\n\t"
138 ".previous\n\t"
139 _ASM_EXTABLE(2b, 3b)
140 : [err] "=a" (err)
141 : "c" (msr), "0" (low), "d" (high),
142 [fault] "i" (-EIO)
143 : "memory");
144 if (msr_tracepoint_active(__tracepoint_write_msr))
145 do_trace_write_msr(msr, ((u64)high << 32 | low), err);
146 return err;
147}
148
149extern int rdmsr_safe_regs(u32 regs[8]);
150extern int wrmsr_safe_regs(u32 regs[8]);
151
152
153
154
155
156
157
158
159
160
161static __always_inline unsigned long long rdtsc(void)
162{
163 DECLARE_ARGS(val, low, high);
164
165 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
166
167 return EAX_EDX_VAL(val, low, high);
168}
169
170
171
172
173
174
175
176
177
178static __always_inline unsigned long long rdtsc_ordered(void)
179{
180
181
182
183
184
185
186
187
188
189
190
191 alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC,
192 "lfence", X86_FEATURE_LFENCE_RDTSC);
193 return rdtsc();
194}
195
196
197#define rdtscll(now) do { (now) = rdtsc_ordered(); } while (0)
198
199static inline unsigned long long native_read_pmc(int counter)
200{
201 DECLARE_ARGS(val, low, high);
202
203 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
204 if (msr_tracepoint_active(__tracepoint_rdpmc))
205 do_trace_rdpmc(counter, EAX_EDX_VAL(val, low, high), 0);
206 return EAX_EDX_VAL(val, low, high);
207}
208
209#ifdef CONFIG_PARAVIRT
210#include <asm/paravirt.h>
211#else
212#include <linux/errno.h>
213
214
215
216
217
218
219#define rdmsr(msr, low, high) \
220do { \
221 u64 __val = native_read_msr((msr)); \
222 (void)((low) = (u32)__val); \
223 (void)((high) = (u32)(__val >> 32)); \
224} while (0)
225
226static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
227{
228 native_write_msr(msr, low, high);
229}
230
231#define rdmsrl(msr, val) \
232 ((val) = native_read_msr((msr)))
233
234static inline void wrmsrl(unsigned msr, u64 val)
235{
236 native_write_msr(msr, (u32)(val & 0xffffffffULL), (u32)(val >> 32));
237}
238
239
240static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
241{
242 return native_write_msr_safe(msr, low, high);
243}
244
245
246#define rdmsr_safe(msr, low, high) \
247({ \
248 int __err; \
249 u64 __val = native_read_msr_safe((msr), &__err); \
250 (*low) = (u32)__val; \
251 (*high) = (u32)(__val >> 32); \
252 __err; \
253})
254
255static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
256{
257 int err;
258
259 *p = native_read_msr_safe(msr, &err);
260 return err;
261}
262
263#define rdpmc(counter, low, high) \
264do { \
265 u64 _l = native_read_pmc((counter)); \
266 (low) = (u32)_l; \
267 (high) = (u32)(_l >> 32); \
268} while (0)
269
270#define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
271
272#endif
273
274
275
276
277static inline int wrmsrl_safe(u32 msr, u64 val)
278{
279 return wrmsr_safe(msr, (u32)val, (u32)(val >> 32));
280}
281
282#define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
283
284#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
285
286struct msr *msrs_alloc(void);
287void msrs_free(struct msr *msrs);
288int msr_set_bit(u32 msr, u8 bit);
289int msr_clear_bit(u32 msr, u8 bit);
290
291#ifdef CONFIG_SMP
292int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
293int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
294int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
295int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
296void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
297void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
298int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
299int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
300int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
301int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
302int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
303int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
304#else
305static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
306{
307 rdmsr(msr_no, *l, *h);
308 return 0;
309}
310static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
311{
312 wrmsr(msr_no, l, h);
313 return 0;
314}
315static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
316{
317 rdmsrl(msr_no, *q);
318 return 0;
319}
320static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
321{
322 wrmsrl(msr_no, q);
323 return 0;
324}
325static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
326 struct msr *msrs)
327{
328 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
329}
330static inline void wrmsr_on_cpus(const struct cpumask *m, u32 msr_no,
331 struct msr *msrs)
332{
333 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
334}
335static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
336 u32 *l, u32 *h)
337{
338 return rdmsr_safe(msr_no, l, h);
339}
340static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
341{
342 return wrmsr_safe(msr_no, l, h);
343}
344static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
345{
346 return rdmsrl_safe(msr_no, q);
347}
348static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
349{
350 return wrmsrl_safe(msr_no, q);
351}
352static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
353{
354 return rdmsr_safe_regs(regs);
355}
356static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
357{
358 return wrmsr_safe_regs(regs);
359}
360#endif
361#endif
362#endif
363