4 #include <asm/msr-index.h>
8 #include <linux/types.h>
9 #include <linux/ioctl.h>
11 #define X86_IOC_RDMSR_REGS _IOWR('c', 0xA0, __u32[8])
12 #define X86_IOC_WRMSR_REGS _IOWR('c', 0xA1, __u32[8])
17 #include <asm/errno.h>
18 #include <asm/cpumask.h>
30 static inline unsigned long long native_read_tscp(unsigned int *aux
)
32 unsigned long low
, high
;
33 asm volatile(".byte 0x0f,0x01,0xf9"
34 : "=a" (low
), "=d" (high
), "=c" (*aux
));
35 return low
| ((u64
)high
<< 32);
39 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
40 * constraint has different meanings. For i386, "A" means exactly
41 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
42 * it means rax *or* rdx.
45 #define DECLARE_ARGS(val, low, high) unsigned low, high
46 #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
47 #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
48 #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
50 #define DECLARE_ARGS(val, low, high) unsigned long long val
51 #define EAX_EDX_VAL(val, low, high) (val)
52 #define EAX_EDX_ARGS(val, low, high) "A" (val)
53 #define EAX_EDX_RET(val, low, high) "=A" (val)
56 static inline unsigned long long native_read_msr(unsigned int msr
)
58 DECLARE_ARGS(val
, low
, high
);
60 asm volatile("rdmsr" : EAX_EDX_RET(val
, low
, high
) : "c" (msr
));
61 return EAX_EDX_VAL(val
, low
, high
);
64 static inline unsigned long long native_read_msr_safe(unsigned int msr
,
67 DECLARE_ARGS(val
, low
, high
);
69 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
71 ".section .fixup,\"ax\"\n\t"
72 "3: mov %[fault],%[err] ; jmp 1b\n\t"
75 : [err
] "=r" (*err
), EAX_EDX_RET(val
, low
, high
)
76 : "c" (msr
), [fault
] "i" (-EIO
));
77 return EAX_EDX_VAL(val
, low
, high
);
80 static inline void native_write_msr(unsigned int msr
,
81 unsigned low
, unsigned high
)
83 asm volatile("wrmsr" : : "c" (msr
), "a"(low
), "d" (high
) : "memory");
86 /* Can be uninlined because referenced by paravirt */
87 notrace
static inline int native_write_msr_safe(unsigned int msr
,
88 unsigned low
, unsigned high
)
91 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
93 ".section .fixup,\"ax\"\n\t"
94 "3: mov %[fault],%[err] ; jmp 1b\n\t"
98 : "c" (msr
), "0" (low
), "d" (high
),
104 extern unsigned long long native_read_tsc(void);
106 extern int native_rdmsr_safe_regs(u32 regs
[8]);
107 extern int native_wrmsr_safe_regs(u32 regs
[8]);
109 static __always_inline
unsigned long long __native_read_tsc(void)
111 DECLARE_ARGS(val
, low
, high
);
113 asm volatile("rdtsc" : EAX_EDX_RET(val
, low
, high
));
115 return EAX_EDX_VAL(val
, low
, high
);
118 static inline unsigned long long native_read_pmc(int counter
)
120 DECLARE_ARGS(val
, low
, high
);
122 asm volatile("rdpmc" : EAX_EDX_RET(val
, low
, high
) : "c" (counter
));
123 return EAX_EDX_VAL(val
, low
, high
);
126 #ifdef CONFIG_PARAVIRT
127 #include <asm/paravirt.h>
129 #include <linux/errno.h>
131 * Access to machine-specific registers (available on 586 and better only)
132 * Note: the rd* operations modify the parameters directly (without using
133 * pointer indirection), this allows gcc to optimize better
136 #define rdmsr(msr, val1, val2) \
138 u64 __val = native_read_msr((msr)); \
139 (val1) = (u32)__val; \
140 (val2) = (u32)(__val >> 32); \
143 static inline void wrmsr(unsigned msr
, unsigned low
, unsigned high
)
145 native_write_msr(msr
, low
, high
);
148 #define rdmsrl(msr, val) \
149 ((val) = native_read_msr((msr)))
151 #define wrmsrl(msr, val) \
152 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
154 /* wrmsr with exception handling */
155 static inline int wrmsr_safe(unsigned msr
, unsigned low
, unsigned high
)
157 return native_write_msr_safe(msr
, low
, high
);
160 /* rdmsr with exception handling */
161 #define rdmsr_safe(msr, p1, p2) \
164 u64 __val = native_read_msr_safe((msr), &__err); \
165 (*p1) = (u32)__val; \
166 (*p2) = (u32)(__val >> 32); \
170 static inline int rdmsrl_safe(unsigned msr
, unsigned long long *p
)
174 *p
= native_read_msr_safe(msr
, &err
);
178 static inline int rdmsrl_amd_safe(unsigned msr
, unsigned long long *p
)
184 gprs
[7] = 0x9c5a203a;
186 err
= native_rdmsr_safe_regs(gprs
);
188 *p
= gprs
[0] | ((u64
)gprs
[2] << 32);
193 static inline int wrmsrl_amd_safe(unsigned msr
, unsigned long long val
)
200 gprs
[7] = 0x9c5a203a;
202 return native_wrmsr_safe_regs(gprs
);
205 static inline int rdmsr_safe_regs(u32 regs
[8])
207 return native_rdmsr_safe_regs(regs
);
210 static inline int wrmsr_safe_regs(u32 regs
[8])
212 return native_wrmsr_safe_regs(regs
);
215 #define rdtscl(low) \
216 ((low) = (u32)__native_read_tsc())
218 #define rdtscll(val) \
219 ((val) = __native_read_tsc())
221 #define rdpmc(counter, low, high) \
223 u64 _l = native_read_pmc((counter)); \
225 (high) = (u32)(_l >> 32); \
228 #define rdtscp(low, high, aux) \
230 unsigned long long _val = native_read_tscp(&(aux)); \
232 (high) = (u32)(_val >> 32); \
235 #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
237 #endif /* !CONFIG_PARAVIRT */
240 #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
243 #define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
245 #define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
248 int rdmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
);
249 int wrmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
);
250 void rdmsr_on_cpus(const cpumask_t
*mask
, u32 msr_no
, struct msr
*msrs
);
251 void wrmsr_on_cpus(const cpumask_t
*mask
, u32 msr_no
, struct msr
*msrs
);
252 int rdmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
);
253 int wrmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
);
254 int rdmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8]);
255 int wrmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8]);
256 #else /* CONFIG_SMP */
257 static inline int rdmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
)
259 rdmsr(msr_no
, *l
, *h
);
262 static inline int wrmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
)
267 static inline void rdmsr_on_cpus(const cpumask_t
*m
, u32 msr_no
,
270 rdmsr_on_cpu(0, msr_no
, &(msrs
[0].l
), &(msrs
[0].h
));
272 static inline void wrmsr_on_cpus(const cpumask_t
*m
, u32 msr_no
,
275 wrmsr_on_cpu(0, msr_no
, msrs
[0].l
, msrs
[0].h
);
277 static inline int rdmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
,
280 return rdmsr_safe(msr_no
, l
, h
);
282 static inline int wrmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
)
284 return wrmsr_safe(msr_no
, l
, h
);
286 static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8])
288 return rdmsr_safe_regs(regs
);
290 static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu
, u32 regs
[8])
292 return wrmsr_safe_regs(regs
);
294 #endif /* CONFIG_SMP */
295 #endif /* __KERNEL__ */
296 #endif /* __ASSEMBLY__ */
297 #endif /* _ASM_X86_MSR_H */