1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Taken from the linux kernel file of the same name
6 * Graeme Russ, <graeme.russ@gmail.com>
10 #define _ASM_X86_MSR_H
12 #include <asm/msr-index.h>
16 #include <linux/types.h>
17 #include <linux/ioctl.h>
19 #define X86_IOC_RDMSR_REGS _IOWR('c', 0xA0, __u32[8])
20 #define X86_IOC_WRMSR_REGS _IOWR('c', 0xA1, __u32[8])
24 #include <linux/errno.h>
43 struct msr_regs_info {
48 static inline unsigned long long native_read_tscp(unsigned int *aux)
50 unsigned long low, high;
51 asm volatile(".byte 0x0f,0x01,0xf9"
52 : "=a" (low), "=d" (high), "=c" (*aux));
53 return low | ((u64)high << 32);
57 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
58 * constraint has different meanings. For i386, "A" means exactly
59 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
60 * it means rax *or* rdx.
63 #define DECLARE_ARGS(val, low, high) unsigned low, high
64 #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
65 #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
66 #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
68 #define DECLARE_ARGS(val, low, high) unsigned long long val
69 #define EAX_EDX_VAL(val, low, high) (val)
70 #define EAX_EDX_ARGS(val, low, high) "A" (val)
71 #define EAX_EDX_RET(val, low, high) "=A" (val)
74 static inline __attribute__((no_instrument_function))
75 unsigned long long native_read_msr(unsigned int msr)
77 DECLARE_ARGS(val, low, high);
79 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
80 return EAX_EDX_VAL(val, low, high);
83 static inline void native_write_msr(unsigned int msr,
84 unsigned low, unsigned high)
86 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
89 extern unsigned long long native_read_tsc(void);
91 extern int native_rdmsr_safe_regs(u32 regs[8]);
92 extern int native_wrmsr_safe_regs(u32 regs[8]);
94 static inline unsigned long long native_read_pmc(int counter)
96 DECLARE_ARGS(val, low, high);
98 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
99 return EAX_EDX_VAL(val, low, high);
102 #ifdef CONFIG_PARAVIRT
103 #include <asm/paravirt.h>
107 * Access to machine-specific registers (available on 586 and better only)
108 * Note: the rd* operations modify the parameters directly (without using
109 * pointer indirection), this allows gcc to optimize better
112 #define rdmsr(msr, val1, val2) \
114 u64 __val = native_read_msr((msr)); \
115 (void)((val1) = (u32)__val); \
116 (void)((val2) = (u32)(__val >> 32)); \
119 static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
121 native_write_msr(msr, low, high);
124 #define rdmsrl(msr, val) \
125 ((val) = native_read_msr((msr)))
127 #define wrmsrl(msr, val) \
128 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
130 static inline void msr_clrsetbits_64(unsigned msr, u64 clear, u64 set)
134 val = native_read_msr(msr);
140 static inline void msr_setbits_64(unsigned msr, u64 set)
144 val = native_read_msr(msr);
149 static inline void msr_clrbits_64(unsigned msr, u64 clear)
153 val = native_read_msr(msr);
158 /* rdmsr with exception handling */
159 #define rdmsr_safe(msr, p1, p2) \
162 u64 __val = native_read_msr_safe((msr), &__err); \
163 (*p1) = (u32)__val; \
164 (*p2) = (u32)(__val >> 32); \
168 static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
174 gprs[7] = 0x9c5a203a;
176 err = native_rdmsr_safe_regs(gprs);
178 *p = gprs[0] | ((u64)gprs[2] << 32);
183 static inline int wrmsrl_amd_safe(unsigned msr, unsigned long long val)
190 gprs[7] = 0x9c5a203a;
192 return native_wrmsr_safe_regs(gprs);
195 static inline int rdmsr_safe_regs(u32 regs[8])
197 return native_rdmsr_safe_regs(regs);
200 static inline int wrmsr_safe_regs(u32 regs[8])
202 return native_wrmsr_safe_regs(regs);
205 typedef struct msr_t {
210 static inline struct msr_t msr_read(unsigned msr_num)
214 rdmsr(msr_num, msr.lo, msr.hi);
219 static inline void msr_write(unsigned msr_num, msr_t msr)
221 wrmsr(msr_num, msr.lo, msr.hi);
224 #define rdtscl(low) \
225 ((low) = (u32)__native_read_tsc())
227 #define rdtscll(val) \
228 ((val) = __native_read_tsc())
230 #define rdpmc(counter, low, high) \
232 u64 _l = native_read_pmc((counter)); \
234 (high) = (u32)(_l >> 32); \
237 #define rdtscp(low, high, aux) \
239 unsigned long long _val = native_read_tscp(&(aux)); \
241 (high) = (u32)(_val >> 32); \
244 #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
246 #endif /* !CONFIG_PARAVIRT */
249 #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
252 #define write_tsc(val1, val2) wrmsr(MSR_IA32_TSC, (val1), (val2))
254 #define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
256 struct msr *msrs_alloc(void);
257 void msrs_free(struct msr *msrs);
259 #endif /* __KERNEL__ */
260 #endif /* __ASSEMBLY__ */
261 #endif /* _ASM_X86_MSR_H */