msr.h 7.0 KB

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