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