msr.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Taken from the linux kernel file of the same name
  3. *
  4. * (C) Copyright 2012
  5. * Graeme Russ, <graeme.russ@gmail.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #ifndef _ASM_X86_MSR_H
  10. #define _ASM_X86_MSR_H
  11. #include <asm/msr-index.h>
  12. #ifndef __ASSEMBLY__
  13. #include <linux/types.h>
  14. #include <linux/ioctl.h>
  15. #define X86_IOC_RDMSR_REGS _IOWR('c', 0xA0, __u32[8])
  16. #define X86_IOC_WRMSR_REGS _IOWR('c', 0xA1, __u32[8])
  17. #ifdef __KERNEL__
  18. #include <linux/errno.h>
  19. struct msr {
  20. union {
  21. struct {
  22. u32 l;
  23. u32 h;
  24. };
  25. u64 q;
  26. };
  27. };
  28. struct msr_info {
  29. u32 msr_no;
  30. struct msr reg;
  31. struct msr *msrs;
  32. int err;
  33. };
  34. struct msr_regs_info {
  35. u32 *regs;
  36. int err;
  37. };
  38. static inline unsigned long long native_read_tscp(unsigned int *aux)
  39. {
  40. unsigned long low, high;
  41. asm volatile(".byte 0x0f,0x01,0xf9"
  42. : "=a" (low), "=d" (high), "=c" (*aux));
  43. return low | ((u64)high << 32);
  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. #define DECLARE_ARGS(val, low, high) unsigned low, high
  53. #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
  54. #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
  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_ARGS(val, low, high) "A" (val)
  60. #define EAX_EDX_RET(val, low, high) "=A" (val)
  61. #endif
  62. static inline __attribute__((no_instrument_function))
  63. unsigned long long native_read_msr(unsigned int msr)
  64. {
  65. DECLARE_ARGS(val, low, high);
  66. asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
  67. return EAX_EDX_VAL(val, low, high);
  68. }
  69. static inline void native_write_msr(unsigned int msr,
  70. unsigned low, unsigned high)
  71. {
  72. asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
  73. }
  74. extern unsigned long long native_read_tsc(void);
  75. extern int native_rdmsr_safe_regs(u32 regs[8]);
  76. extern int native_wrmsr_safe_regs(u32 regs[8]);
  77. static inline unsigned long long native_read_pmc(int counter)
  78. {
  79. DECLARE_ARGS(val, low, high);
  80. asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
  81. return EAX_EDX_VAL(val, low, high);
  82. }
  83. #ifdef CONFIG_PARAVIRT
  84. #include <asm/paravirt.h>
  85. #else
  86. #include <errno.h>
  87. /*
  88. * Access to machine-specific registers (available on 586 and better only)
  89. * Note: the rd* operations modify the parameters directly (without using
  90. * pointer indirection), this allows gcc to optimize better
  91. */
  92. #define rdmsr(msr, val1, val2) \
  93. do { \
  94. u64 __val = native_read_msr((msr)); \
  95. (void)((val1) = (u32)__val); \
  96. (void)((val2) = (u32)(__val >> 32)); \
  97. } while (0)
  98. static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
  99. {
  100. native_write_msr(msr, low, high);
  101. }
  102. #define rdmsrl(msr, val) \
  103. ((val) = native_read_msr((msr)))
  104. #define wrmsrl(msr, val) \
  105. native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
  106. static inline void msr_clrsetbits_64(unsigned msr, u64 clear, u64 set)
  107. {
  108. u64 val;
  109. val = native_read_msr(msr);
  110. val &= ~clear;
  111. val |= set;
  112. wrmsrl(msr, val);
  113. }
  114. static inline void msr_setbits_64(unsigned msr, u64 set)
  115. {
  116. u64 val;
  117. val = native_read_msr(msr);
  118. val |= set;
  119. wrmsrl(msr, val);
  120. }
  121. static inline void msr_clrbits_64(unsigned msr, u64 clear)
  122. {
  123. u64 val;
  124. val = native_read_msr(msr);
  125. val &= ~clear;
  126. wrmsrl(msr, val);
  127. }
  128. /* rdmsr with exception handling */
  129. #define rdmsr_safe(msr, p1, p2) \
  130. ({ \
  131. int __err; \
  132. u64 __val = native_read_msr_safe((msr), &__err); \
  133. (*p1) = (u32)__val; \
  134. (*p2) = (u32)(__val >> 32); \
  135. __err; \
  136. })
  137. static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
  138. {
  139. u32 gprs[8] = { 0 };
  140. int err;
  141. gprs[1] = msr;
  142. gprs[7] = 0x9c5a203a;
  143. err = native_rdmsr_safe_regs(gprs);
  144. *p = gprs[0] | ((u64)gprs[2] << 32);
  145. return err;
  146. }
  147. static inline int wrmsrl_amd_safe(unsigned msr, unsigned long long val)
  148. {
  149. u32 gprs[8] = { 0 };
  150. gprs[0] = (u32)val;
  151. gprs[1] = msr;
  152. gprs[2] = val >> 32;
  153. gprs[7] = 0x9c5a203a;
  154. return native_wrmsr_safe_regs(gprs);
  155. }
  156. static inline int rdmsr_safe_regs(u32 regs[8])
  157. {
  158. return native_rdmsr_safe_regs(regs);
  159. }
  160. static inline int wrmsr_safe_regs(u32 regs[8])
  161. {
  162. return native_wrmsr_safe_regs(regs);
  163. }
  164. typedef struct msr_t {
  165. uint32_t lo;
  166. uint32_t hi;
  167. } msr_t;
  168. static inline struct msr_t msr_read(unsigned msr_num)
  169. {
  170. struct msr_t msr;
  171. rdmsr(msr_num, msr.lo, msr.hi);
  172. return msr;
  173. }
  174. static inline void msr_write(unsigned msr_num, msr_t msr)
  175. {
  176. wrmsr(msr_num, msr.lo, msr.hi);
  177. }
  178. #define rdtscl(low) \
  179. ((low) = (u32)__native_read_tsc())
  180. #define rdtscll(val) \
  181. ((val) = __native_read_tsc())
  182. #define rdpmc(counter, low, high) \
  183. do { \
  184. u64 _l = native_read_pmc((counter)); \
  185. (low) = (u32)_l; \
  186. (high) = (u32)(_l >> 32); \
  187. } while (0)
  188. #define rdtscp(low, high, aux) \
  189. do { \
  190. unsigned long long _val = native_read_tscp(&(aux)); \
  191. (low) = (u32)_val; \
  192. (high) = (u32)(_val >> 32); \
  193. } while (0)
  194. #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
  195. #endif /* !CONFIG_PARAVIRT */
  196. #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
  197. (u32)((val) >> 32))
  198. #define write_tsc(val1, val2) wrmsr(MSR_IA32_TSC, (val1), (val2))
  199. #define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
  200. struct msr *msrs_alloc(void);
  201. void msrs_free(struct msr *msrs);
  202. #endif /* __KERNEL__ */
  203. #endif /* __ASSEMBLY__ */
  204. #endif /* _ASM_X86_MSR_H */