• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 struct msr {
14 	union {
15 		struct {
16 			u32 l;
17 			u32 h;
18 		};
19 		u64 q;
20 	};
21 };
22 
23 struct msr_info {
24 	u32 msr_no;
25 	struct msr reg;
26 	struct msr *msrs;
27 	int err;
28 };
29 
30 struct msr_regs_info {
31 	u32 *regs;
32 	int err;
33 };
34 
35 struct saved_msr {
36 	bool valid;
37 	struct msr_info info;
38 };
39 
40 struct saved_msrs {
41 	unsigned int num;
42 	struct saved_msr *array;
43 };
44 
45 /*
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.
50  */
51 #ifdef CONFIG_X86_64
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)
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  * Be very careful with includes. This header is prone to include loops.
65  */
66 #include <asm/atomic.h>
67 #include <linux/tracepoint-defs.h>
68 
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 msr, u64 val, int failed);
74 extern void do_trace_read_msr(unsigned msr, u64 val, int failed);
75 extern void do_trace_rdpmc(unsigned msr, u64 val, int failed);
76 #else
77 #define msr_tracepoint_active(t) false
do_trace_write_msr(unsigned msr,u64 val,int failed)78 static inline void do_trace_write_msr(unsigned msr, u64 val, int failed) {}
do_trace_read_msr(unsigned msr,u64 val,int failed)79 static inline void do_trace_read_msr(unsigned msr, u64 val, int failed) {}
do_trace_rdpmc(unsigned msr,u64 val,int failed)80 static inline void do_trace_rdpmc(unsigned msr, u64 val, int failed) {}
81 #endif
82 
native_read_msr(unsigned int msr)83 static 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 
native_read_msr_safe(unsigned int msr,int * err)96 static 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 /* Can be uninlined because referenced by paravirt */
native_write_msr(unsigned int msr,unsigned low,unsigned high)118 notrace 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 /* Can be uninlined because referenced by paravirt */
native_write_msr_safe(unsigned int msr,unsigned low,unsigned high)130 notrace 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 
149 extern int rdmsr_safe_regs(u32 regs[8]);
150 extern int wrmsr_safe_regs(u32 regs[8]);
151 
152 /**
153  * rdtsc() - returns the current TSC without ordering constraints
154  *
155  * rdtsc() returns the result of RDTSC as a 64-bit integer.  The
156  * only ordering constraint it supplies is the ordering implied by
157  * "asm volatile": it will put the RDTSC in the place you expect.  The
158  * CPU can and will speculatively execute that RDTSC, though, so the
159  * results can be non-monotonic if compared on different CPUs.
160  */
rdtsc(void)161 static __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  * rdtsc_ordered() - read the current TSC in program order
172  *
173  * rdtsc_ordered() returns the result of RDTSC as a 64-bit integer.
174  * It is ordered like a load to a global in-memory counter.  It should
175  * be impossible to observe non-monotonic rdtsc_unordered() behavior
176  * across multiple CPUs as long as the TSC is synced.
177  */
rdtsc_ordered(void)178 static __always_inline unsigned long long rdtsc_ordered(void)
179 {
180 	/*
181 	 * The RDTSC instruction is not ordered relative to memory
182 	 * access.  The Intel SDM and the AMD APM are both vague on this
183 	 * point, but empirically an RDTSC instruction can be
184 	 * speculatively executed before prior loads.  An RDTSC
185 	 * immediately after an appropriate barrier appears to be
186 	 * ordered as a normal load, that is, it provides the same
187 	 * ordering guarantees as reading from a global memory location
188 	 * that some other imaginary CPU is updating continuously with a
189 	 * time stamp.
190 	 */
191 	barrier_nospec();
192 	return rdtsc();
193 }
194 
195 /* Deprecated, keep it for a cycle for easier merging: */
196 #define rdtscll(now)	do { (now) = rdtsc_ordered(); } while (0)
197 
native_read_pmc(int counter)198 static inline unsigned long long native_read_pmc(int counter)
199 {
200 	DECLARE_ARGS(val, low, high);
201 
202 	asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
203 	if (msr_tracepoint_active(__tracepoint_rdpmc))
204 		do_trace_rdpmc(counter, EAX_EDX_VAL(val, low, high), 0);
205 	return EAX_EDX_VAL(val, low, high);
206 }
207 
208 #ifdef CONFIG_PARAVIRT
209 #include <asm/paravirt.h>
210 #else
211 #include <linux/errno.h>
212 /*
213  * Access to machine-specific registers (available on 586 and better only)
214  * Note: the rd* operations modify the parameters directly (without using
215  * pointer indirection), this allows gcc to optimize better
216  */
217 
218 #define rdmsr(msr, low, high)					\
219 do {								\
220 	u64 __val = native_read_msr((msr));			\
221 	(void)((low) = (u32)__val);				\
222 	(void)((high) = (u32)(__val >> 32));			\
223 } while (0)
224 
wrmsr(unsigned msr,unsigned low,unsigned high)225 static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
226 {
227 	native_write_msr(msr, low, high);
228 }
229 
230 #define rdmsrl(msr, val)			\
231 	((val) = native_read_msr((msr)))
232 
wrmsrl(unsigned msr,u64 val)233 static inline void wrmsrl(unsigned msr, u64 val)
234 {
235 	native_write_msr(msr, (u32)(val & 0xffffffffULL), (u32)(val >> 32));
236 }
237 
238 /* wrmsr with exception handling */
wrmsr_safe(unsigned msr,unsigned low,unsigned high)239 static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
240 {
241 	return native_write_msr_safe(msr, low, high);
242 }
243 
244 /* rdmsr with exception handling */
245 #define rdmsr_safe(msr, low, high)				\
246 ({								\
247 	int __err;						\
248 	u64 __val = native_read_msr_safe((msr), &__err);	\
249 	(*low) = (u32)__val;					\
250 	(*high) = (u32)(__val >> 32);				\
251 	__err;							\
252 })
253 
rdmsrl_safe(unsigned msr,unsigned long long * p)254 static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
255 {
256 	int err;
257 
258 	*p = native_read_msr_safe(msr, &err);
259 	return err;
260 }
261 
262 #define rdpmc(counter, low, high)			\
263 do {							\
264 	u64 _l = native_read_pmc((counter));		\
265 	(low)  = (u32)_l;				\
266 	(high) = (u32)(_l >> 32);			\
267 } while (0)
268 
269 #define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
270 
271 #endif	/* !CONFIG_PARAVIRT */
272 
273 /*
274  * 64-bit version of wrmsr_safe():
275  */
wrmsrl_safe(u32 msr,u64 val)276 static inline int wrmsrl_safe(u32 msr, u64 val)
277 {
278 	return wrmsr_safe(msr, (u32)val,  (u32)(val >> 32));
279 }
280 
281 #define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
282 
283 #define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
284 
285 struct msr *msrs_alloc(void);
286 void msrs_free(struct msr *msrs);
287 int msr_set_bit(u32 msr, u8 bit);
288 int msr_clear_bit(u32 msr, u8 bit);
289 
290 #ifdef CONFIG_SMP
291 int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
292 int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
293 int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
294 int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
295 void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
296 void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
297 int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
298 int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
299 int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
300 int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
301 int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
302 int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
303 #else  /*  CONFIG_SMP  */
rdmsr_on_cpu(unsigned int cpu,u32 msr_no,u32 * l,u32 * h)304 static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
305 {
306 	rdmsr(msr_no, *l, *h);
307 	return 0;
308 }
wrmsr_on_cpu(unsigned int cpu,u32 msr_no,u32 l,u32 h)309 static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
310 {
311 	wrmsr(msr_no, l, h);
312 	return 0;
313 }
rdmsrl_on_cpu(unsigned int cpu,u32 msr_no,u64 * q)314 static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
315 {
316 	rdmsrl(msr_no, *q);
317 	return 0;
318 }
wrmsrl_on_cpu(unsigned int cpu,u32 msr_no,u64 q)319 static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
320 {
321 	wrmsrl(msr_no, q);
322 	return 0;
323 }
rdmsr_on_cpus(const struct cpumask * m,u32 msr_no,struct msr * msrs)324 static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
325 				struct msr *msrs)
326 {
327        rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
328 }
wrmsr_on_cpus(const struct cpumask * m,u32 msr_no,struct msr * msrs)329 static inline void wrmsr_on_cpus(const struct cpumask *m, u32 msr_no,
330 				struct msr *msrs)
331 {
332        wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
333 }
rdmsr_safe_on_cpu(unsigned int cpu,u32 msr_no,u32 * l,u32 * h)334 static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
335 				    u32 *l, u32 *h)
336 {
337 	return rdmsr_safe(msr_no, l, h);
338 }
wrmsr_safe_on_cpu(unsigned int cpu,u32 msr_no,u32 l,u32 h)339 static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
340 {
341 	return wrmsr_safe(msr_no, l, h);
342 }
rdmsrl_safe_on_cpu(unsigned int cpu,u32 msr_no,u64 * q)343 static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
344 {
345 	return rdmsrl_safe(msr_no, q);
346 }
wrmsrl_safe_on_cpu(unsigned int cpu,u32 msr_no,u64 q)347 static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
348 {
349 	return wrmsrl_safe(msr_no, q);
350 }
rdmsr_safe_regs_on_cpu(unsigned int cpu,u32 regs[8])351 static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
352 {
353 	return rdmsr_safe_regs(regs);
354 }
wrmsr_safe_regs_on_cpu(unsigned int cpu,u32 regs[8])355 static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
356 {
357 	return wrmsr_safe_regs(regs);
358 }
359 #endif  /* CONFIG_SMP */
360 #endif /* __ASSEMBLY__ */
361 #endif /* _ASM_X86_MSR_H */
362