4 #include <asm/msr-index.h>
7 # include <linux/types.h>
14 #include <asm/errno.h>
15 #include <asm/cpumask.h>
27 static inline unsigned long long native_read_tscp(unsigned int *aux)
29 unsigned long low, high;
30 asm volatile(".byte 0x0f,0x01,0xf9"
31 : "=a" (low), "=d" (high), "=c" (*aux));
32 return low | ((u64)high << 32);
36 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
37 * constraint has different meanings. For i386, "A" means exactly
38 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
39 * it means rax *or* rdx.
42 #define DECLARE_ARGS(val, low, high) unsigned low, high
43 #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
44 #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
45 #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
47 #define DECLARE_ARGS(val, low, high) unsigned long long val
48 #define EAX_EDX_VAL(val, low, high) (val)
49 #define EAX_EDX_ARGS(val, low, high) "A" (val)
50 #define EAX_EDX_RET(val, low, high) "=A" (val)
53 static inline unsigned long long native_read_msr(unsigned int msr)
55 DECLARE_ARGS(val, low, high);
57 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
58 return EAX_EDX_VAL(val, low, high);
61 static inline unsigned long long native_read_msr_safe(unsigned int msr,
64 DECLARE_ARGS(val, low, high);
66 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
68 ".section .fixup,\"ax\"\n\t"
69 "3: mov %[fault],%[err] ; jmp 1b\n\t"
72 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
73 : "c" (msr), [fault] "i" (-EFAULT));
74 return EAX_EDX_VAL(val, low, high);
77 static inline unsigned long long native_read_msr_amd_safe(unsigned int msr,
80 DECLARE_ARGS(val, low, high);
82 asm volatile("2: rdmsr ; xor %0,%0\n"
84 ".section .fixup,\"ax\"\n\t"
85 "3: mov %3,%0 ; jmp 1b\n\t"
88 : "=r" (*err), EAX_EDX_RET(val, low, high)
89 : "c" (msr), "D" (0x9c5a203a), "i" (-EFAULT));
90 return EAX_EDX_VAL(val, low, high);
93 static inline void native_write_msr(unsigned int msr,
94 unsigned low, unsigned high)
96 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
99 /* Can be uninlined because referenced by paravirt */
100 notrace static inline int native_write_msr_safe(unsigned int msr,
101 unsigned low, unsigned high)
104 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
106 ".section .fixup,\"ax\"\n\t"
107 "3: mov %[fault],%[err] ; jmp 1b\n\t"
111 : "c" (msr), "0" (low), "d" (high),
112 [fault] "i" (-EFAULT)
117 extern unsigned long long native_read_tsc(void);
119 static __always_inline unsigned long long __native_read_tsc(void)
121 DECLARE_ARGS(val, low, high);
123 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
125 return EAX_EDX_VAL(val, low, high);
128 static inline unsigned long long native_read_pmc(int counter)
130 DECLARE_ARGS(val, low, high);
132 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
133 return EAX_EDX_VAL(val, low, high);
136 #ifdef CONFIG_PARAVIRT
137 #include <asm/paravirt.h>
139 #include <linux/errno.h>
141 * Access to machine-specific registers (available on 586 and better only)
142 * Note: the rd* operations modify the parameters directly (without using
143 * pointer indirection), this allows gcc to optimize better
146 #define rdmsr(msr, val1, val2) \
148 u64 __val = native_read_msr((msr)); \
149 (val1) = (u32)__val; \
150 (val2) = (u32)(__val >> 32); \
153 static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
155 native_write_msr(msr, low, high);
158 #define rdmsrl(msr, val) \
159 ((val) = native_read_msr((msr)))
161 #define wrmsrl(msr, val) \
162 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
164 /* wrmsr with exception handling */
165 static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
167 return native_write_msr_safe(msr, low, high);
170 /* rdmsr with exception handling */
171 #define rdmsr_safe(msr, p1, p2) \
174 u64 __val = native_read_msr_safe((msr), &__err); \
175 (*p1) = (u32)__val; \
176 (*p2) = (u32)(__val >> 32); \
180 static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
184 *p = native_read_msr_safe(msr, &err);
187 static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
191 *p = native_read_msr_amd_safe(msr, &err);
195 #define rdtscl(low) \
196 ((low) = (u32)__native_read_tsc())
198 #define rdtscll(val) \
199 ((val) = __native_read_tsc())
201 #define rdpmc(counter, low, high) \
203 u64 _l = native_read_pmc((counter)); \
205 (high) = (u32)(_l >> 32); \
208 #define rdtscp(low, high, aux) \
210 unsigned long long _val = native_read_tscp(&(aux)); \
212 (high) = (u32)(_val >> 32); \
215 #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
217 #endif /* !CONFIG_PARAVIRT */
220 #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
223 #define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
225 #define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
228 int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
229 int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
230 void rdmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
231 void wrmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
232 int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
233 int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
234 #else /* CONFIG_SMP */
235 static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
237 rdmsr(msr_no, *l, *h);
240 static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
245 static inline void rdmsr_on_cpus(const cpumask_t *m, u32 msr_no,
248 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
250 static inline void wrmsr_on_cpus(const cpumask_t *m, u32 msr_no,
253 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
255 static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
258 return rdmsr_safe(msr_no, l, h);
260 static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
262 return wrmsr_safe(msr_no, l, h);
264 #endif /* CONFIG_SMP */
265 #endif /* __ASSEMBLY__ */
266 #endif /* __KERNEL__ */
269 #endif /* _ASM_X86_MSR_H */