msr.h 5.6 KB

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