msr.h 6.4 KB

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