4 #include <asm/msr-index.h>
9 #include <linux/types.h>
11 #include <asm/errno.h>
12 #include <asm/cpumask.h>
24 static inline unsigned long long native_read_tscp(unsigned int *aux)
26 unsigned long low, high;
27 asm volatile(".byte 0x0f,0x01,0xf9"
28 : "=a" (low), "=d" (high), "=c" (*aux));
29 return low | ((u64)high << 32);
33 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
34 * constraint has different meanings. For i386, "A" means exactly
35 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
36 * it means rax *or* rdx.
39 #define DECLARE_ARGS(val, low, high) unsigned low, high
40 #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
41 #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
42 #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
44 #define DECLARE_ARGS(val, low, high) unsigned long long val
45 #define EAX_EDX_VAL(val, low, high) (val)
46 #define EAX_EDX_ARGS(val, low, high) "A" (val)
47 #define EAX_EDX_RET(val, low, high) "=A" (val)
50 static inline unsigned long long native_read_msr(unsigned int msr)
52 DECLARE_ARGS(val, low, high);
54 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
55 return EAX_EDX_VAL(val, low, high);
58 static inline unsigned long long native_read_msr_safe(unsigned int msr,
61 DECLARE_ARGS(val, low, high);
63 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
65 ".section .fixup,\"ax\"\n\t"
66 "3: mov %[fault],%[err] ; jmp 1b\n\t"
69 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
70 : "c" (msr), [fault] "i" (-EFAULT));
71 return EAX_EDX_VAL(val, low, high);
74 static inline unsigned long long native_read_msr_amd_safe(unsigned int msr,
77 DECLARE_ARGS(val, low, high);
79 asm volatile("2: rdmsr ; xor %0,%0\n"
81 ".section .fixup,\"ax\"\n\t"
82 "3: mov %3,%0 ; jmp 1b\n\t"
85 : "=r" (*err), EAX_EDX_RET(val, low, high)
86 : "c" (msr), "D" (0x9c5a203a), "i" (-EFAULT));
87 return EAX_EDX_VAL(val, low, high);
90 static inline void native_write_msr(unsigned int msr,
91 unsigned low, unsigned high)
93 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
96 /* Can be uninlined because referenced by paravirt */
97 notrace static inline int native_write_msr_safe(unsigned int msr,
98 unsigned low, unsigned high)
101 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
103 ".section .fixup,\"ax\"\n\t"
104 "3: mov %[fault],%[err] ; jmp 1b\n\t"
108 : "c" (msr), "0" (low), "d" (high),
109 [fault] "i" (-EFAULT)
114 extern unsigned long long native_read_tsc(void);
116 static __always_inline unsigned long long __native_read_tsc(void)
118 DECLARE_ARGS(val, low, high);
120 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
122 return EAX_EDX_VAL(val, low, high);
125 static inline unsigned long long native_read_pmc(int counter)
127 DECLARE_ARGS(val, low, high);
129 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
130 return EAX_EDX_VAL(val, low, high);
133 #ifdef CONFIG_PARAVIRT
134 #include <asm/paravirt.h>
136 #include <linux/errno.h>
138 * Access to machine-specific registers (available on 586 and better only)
139 * Note: the rd* operations modify the parameters directly (without using
140 * pointer indirection), this allows gcc to optimize better
143 #define rdmsr(msr, val1, val2) \
145 u64 __val = native_read_msr((msr)); \
146 (val1) = (u32)__val; \
147 (val2) = (u32)(__val >> 32); \
150 static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
152 native_write_msr(msr, low, high);
155 #define rdmsrl(msr, val) \
156 ((val) = native_read_msr((msr)))
158 #define wrmsrl(msr, val) \
159 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
161 /* wrmsr with exception handling */
162 static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
164 return native_write_msr_safe(msr, low, high);
167 /* rdmsr with exception handling */
168 #define rdmsr_safe(msr, p1, p2) \
171 u64 __val = native_read_msr_safe((msr), &__err); \
172 (*p1) = (u32)__val; \
173 (*p2) = (u32)(__val >> 32); \
177 static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
181 *p = native_read_msr_safe(msr, &err);
184 static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
188 *p = native_read_msr_amd_safe(msr, &err);
192 #define rdtscl(low) \
193 ((low) = (u32)__native_read_tsc())
195 #define rdtscll(val) \
196 ((val) = __native_read_tsc())
198 #define rdpmc(counter, low, high) \
200 u64 _l = native_read_pmc((counter)); \
202 (high) = (u32)(_l >> 32); \
205 #define rdtscp(low, high, aux) \
207 unsigned long long _val = native_read_tscp(&(aux)); \
209 (high) = (u32)(_val >> 32); \
212 #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
214 #endif /* !CONFIG_PARAVIRT */
217 #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
220 #define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
222 #define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
225 int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
226 int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
227 void rdmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
228 void wrmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
229 int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
230 int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
231 #else /* CONFIG_SMP */
232 static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
234 rdmsr(msr_no, *l, *h);
237 static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
242 static inline void rdmsr_on_cpus(const cpumask_t *m, u32 msr_no,
245 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
247 static inline void wrmsr_on_cpus(const cpumask_t *m, u32 msr_no,
250 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
252 static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
255 return rdmsr_safe(msr_no, l, h);
257 static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
259 return wrmsr_safe(msr_no, l, h);
261 #endif /* CONFIG_SMP */
262 #endif /* __ASSEMBLY__ */
263 #endif /* __KERNEL__ */
264 #endif /* _ASM_X86_MSR_H */