msr.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #ifndef _ASM_X86_MSR_H
  2. #define _ASM_X86_MSR_H
  3. #include <asm/msr-index.h>
  4. #ifdef __KERNEL__
  5. #ifndef __ASSEMBLY__
  6. #include <linux/types.h>
  7. #include <asm/asm.h>
  8. #include <asm/errno.h>
  9. #include <asm/cpumask.h>
  10. struct msr {
  11. union {
  12. struct {
  13. u32 l;
  14. u32 h;
  15. };
  16. u64 q;
  17. };
  18. };
  19. static inline unsigned long long native_read_tscp(unsigned int *aux)
  20. {
  21. unsigned long low, high;
  22. asm volatile(".byte 0x0f,0x01,0xf9"
  23. : "=a" (low), "=d" (high), "=c" (*aux));
  24. return low | ((u64)high << 32);
  25. }
  26. /*
  27. * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
  28. * constraint has different meanings. For i386, "A" means exactly
  29. * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
  30. * it means rax *or* rdx.
  31. */
  32. #ifdef CONFIG_X86_64
  33. #define DECLARE_ARGS(val, low, high) unsigned low, high
  34. #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
  35. #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
  36. #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
  37. #else
  38. #define DECLARE_ARGS(val, low, high) unsigned long long val
  39. #define EAX_EDX_VAL(val, low, high) (val)
  40. #define EAX_EDX_ARGS(val, low, high) "A" (val)
  41. #define EAX_EDX_RET(val, low, high) "=A" (val)
  42. #endif
  43. static inline unsigned long long native_read_msr(unsigned int msr)
  44. {
  45. DECLARE_ARGS(val, low, high);
  46. asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
  47. return EAX_EDX_VAL(val, low, high);
  48. }
  49. static inline unsigned long long native_read_msr_safe(unsigned int msr,
  50. int *err)
  51. {
  52. DECLARE_ARGS(val, low, high);
  53. asm volatile("2: rdmsr ; xor %[err],%[err]\n"
  54. "1:\n\t"
  55. ".section .fixup,\"ax\"\n\t"
  56. "3: mov %[fault],%[err] ; jmp 1b\n\t"
  57. ".previous\n\t"
  58. _ASM_EXTABLE(2b, 3b)
  59. : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
  60. : "c" (msr), [fault] "i" (-EFAULT));
  61. return EAX_EDX_VAL(val, low, high);
  62. }
  63. static inline unsigned long long native_read_msr_amd_safe(unsigned int msr,
  64. int *err)
  65. {
  66. DECLARE_ARGS(val, low, high);
  67. asm volatile("2: rdmsr ; xor %0,%0\n"
  68. "1:\n\t"
  69. ".section .fixup,\"ax\"\n\t"
  70. "3: mov %3,%0 ; jmp 1b\n\t"
  71. ".previous\n\t"
  72. _ASM_EXTABLE(2b, 3b)
  73. : "=r" (*err), EAX_EDX_RET(val, low, high)
  74. : "c" (msr), "D" (0x9c5a203a), "i" (-EFAULT));
  75. return EAX_EDX_VAL(val, low, high);
  76. }
  77. static inline void native_write_msr(unsigned int msr,
  78. unsigned low, unsigned high)
  79. {
  80. asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
  81. }
  82. /* Can be uninlined because referenced by paravirt */
  83. notrace static inline int native_write_msr_safe(unsigned int msr,
  84. unsigned low, unsigned high)
  85. {
  86. int err;
  87. asm volatile("2: wrmsr ; xor %[err],%[err]\n"
  88. "1:\n\t"
  89. ".section .fixup,\"ax\"\n\t"
  90. "3: mov %[fault],%[err] ; jmp 1b\n\t"
  91. ".previous\n\t"
  92. _ASM_EXTABLE(2b, 3b)
  93. : [err] "=a" (err)
  94. : "c" (msr), "0" (low), "d" (high),
  95. [fault] "i" (-EFAULT)
  96. : "memory");
  97. return err;
  98. }
  99. extern unsigned long long native_read_tsc(void);
  100. static __always_inline unsigned long long __native_read_tsc(void)
  101. {
  102. DECLARE_ARGS(val, low, high);
  103. asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
  104. return EAX_EDX_VAL(val, low, high);
  105. }
  106. static inline unsigned long long native_read_pmc(int counter)
  107. {
  108. DECLARE_ARGS(val, low, high);
  109. asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
  110. return EAX_EDX_VAL(val, low, high);
  111. }
  112. #ifdef CONFIG_PARAVIRT
  113. #include <asm/paravirt.h>
  114. #else
  115. #include <linux/errno.h>
  116. /*
  117. * Access to machine-specific registers (available on 586 and better only)
  118. * Note: the rd* operations modify the parameters directly (without using
  119. * pointer indirection), this allows gcc to optimize better
  120. */
  121. #define rdmsr(msr, val1, val2) \
  122. do { \
  123. u64 __val = native_read_msr((msr)); \
  124. (val1) = (u32)__val; \
  125. (val2) = (u32)(__val >> 32); \
  126. } while (0)
  127. static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
  128. {
  129. native_write_msr(msr, low, high);
  130. }
  131. #define rdmsrl(msr, val) \
  132. ((val) = native_read_msr((msr)))
  133. #define wrmsrl(msr, val) \
  134. native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
  135. /* wrmsr with exception handling */
  136. static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
  137. {
  138. return native_write_msr_safe(msr, low, high);
  139. }
  140. /* rdmsr with exception handling */
  141. #define rdmsr_safe(msr, p1, p2) \
  142. ({ \
  143. int __err; \
  144. u64 __val = native_read_msr_safe((msr), &__err); \
  145. (*p1) = (u32)__val; \
  146. (*p2) = (u32)(__val >> 32); \
  147. __err; \
  148. })
  149. static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
  150. {
  151. int err;
  152. *p = native_read_msr_safe(msr, &err);
  153. return err;
  154. }
  155. static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
  156. {
  157. int err;
  158. *p = native_read_msr_amd_safe(msr, &err);
  159. return err;
  160. }
  161. #define rdtscl(low) \
  162. ((low) = (u32)__native_read_tsc())
  163. #define rdtscll(val) \
  164. ((val) = __native_read_tsc())
  165. #define rdpmc(counter, low, high) \
  166. do { \
  167. u64 _l = native_read_pmc((counter)); \
  168. (low) = (u32)_l; \
  169. (high) = (u32)(_l >> 32); \
  170. } while (0)
  171. #define rdtscp(low, high, aux) \
  172. do { \
  173. unsigned long long _val = native_read_tscp(&(aux)); \
  174. (low) = (u32)_val; \
  175. (high) = (u32)(_val >> 32); \
  176. } while (0)
  177. #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
  178. #endif /* !CONFIG_PARAVIRT */
  179. #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
  180. (u32)((val) >> 32))
  181. #define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
  182. #define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
  183. #ifdef CONFIG_SMP
  184. int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  185. int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  186. void rdmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
  187. void wrmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
  188. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  189. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  190. #else /* CONFIG_SMP */
  191. static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  192. {
  193. rdmsr(msr_no, *l, *h);
  194. return 0;
  195. }
  196. static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  197. {
  198. wrmsr(msr_no, l, h);
  199. return 0;
  200. }
  201. static inline void rdmsr_on_cpus(const cpumask_t *m, u32 msr_no,
  202. struct msr *msrs)
  203. {
  204. rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
  205. }
  206. static inline void wrmsr_on_cpus(const cpumask_t *m, u32 msr_no,
  207. struct msr *msrs)
  208. {
  209. wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
  210. }
  211. static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
  212. u32 *l, u32 *h)
  213. {
  214. return rdmsr_safe(msr_no, l, h);
  215. }
  216. static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  217. {
  218. return wrmsr_safe(msr_no, l, h);
  219. }
  220. #endif /* CONFIG_SMP */
  221. #endif /* __ASSEMBLY__ */
  222. #endif /* __KERNEL__ */
  223. #endif /* _ASM_X86_MSR_H */