msr.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. * i386 calling convention returns 64-bit value in edx:eax, while
  20. * x86_64 returns at rax. Also, the "A" constraint does not really
  21. * mean rdx:rax in x86_64, so we need specialized behaviour for each
  22. * architecture
  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 void native_write_msr(unsigned int msr,
  56. unsigned low, unsigned high)
  57. {
  58. asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
  59. }
  60. static inline int native_write_msr_safe(unsigned int msr,
  61. unsigned low, unsigned high)
  62. {
  63. int err;
  64. asm volatile("2: wrmsr ; xor %[err],%[err]\n"
  65. "1:\n\t"
  66. ".section .fixup,\"ax\"\n\t"
  67. "3: mov %[fault],%[err] ; jmp 1b\n\t"
  68. ".previous\n\t"
  69. _ASM_EXTABLE(2b, 3b)
  70. : [err] "=a" (err)
  71. : "c" (msr), "0" (low), "d" (high),
  72. [fault] "i" (-EFAULT)
  73. : "memory");
  74. return err;
  75. }
  76. extern unsigned long long native_read_tsc(void);
  77. static __always_inline unsigned long long __native_read_tsc(void)
  78. {
  79. DECLARE_ARGS(val, low, high);
  80. rdtsc_barrier();
  81. asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
  82. rdtsc_barrier();
  83. return EAX_EDX_VAL(val, low, high);
  84. }
  85. static inline unsigned long long native_read_pmc(int counter)
  86. {
  87. DECLARE_ARGS(val, low, high);
  88. asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
  89. return EAX_EDX_VAL(val, low, high);
  90. }
  91. #ifdef CONFIG_PARAVIRT
  92. #include <asm/paravirt.h>
  93. #else
  94. #include <linux/errno.h>
  95. /*
  96. * Access to machine-specific registers (available on 586 and better only)
  97. * Note: the rd* operations modify the parameters directly (without using
  98. * pointer indirection), this allows gcc to optimize better
  99. */
  100. #define rdmsr(msr, val1, val2) \
  101. do { \
  102. u64 __val = native_read_msr((msr)); \
  103. (val1) = (u32)__val; \
  104. (val2) = (u32)(__val >> 32); \
  105. } while (0)
  106. static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
  107. {
  108. native_write_msr(msr, low, high);
  109. }
  110. #define rdmsrl(msr, val) \
  111. ((val) = native_read_msr((msr)))
  112. #define wrmsrl(msr, val) \
  113. native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
  114. /* wrmsr with exception handling */
  115. static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
  116. {
  117. return native_write_msr_safe(msr, low, high);
  118. }
  119. /* rdmsr with exception handling */
  120. #define rdmsr_safe(msr, p1, p2) \
  121. ({ \
  122. int __err; \
  123. u64 __val = native_read_msr_safe((msr), &__err); \
  124. (*p1) = (u32)__val; \
  125. (*p2) = (u32)(__val >> 32); \
  126. __err; \
  127. })
  128. static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
  129. {
  130. int err;
  131. *p = native_read_msr_safe(msr, &err);
  132. return err;
  133. }
  134. #define rdtscl(low) \
  135. ((low) = (u32)native_read_tsc())
  136. #define rdtscll(val) \
  137. ((val) = native_read_tsc())
  138. #define rdpmc(counter, low, high) \
  139. do { \
  140. u64 _l = native_read_pmc((counter)); \
  141. (low) = (u32)_l; \
  142. (high) = (u32)(_l >> 32); \
  143. } while (0)
  144. #define rdtscp(low, high, aux) \
  145. do { \
  146. unsigned long long _val = native_read_tscp(&(aux)); \
  147. (low) = (u32)_val; \
  148. (high) = (u32)(_val >> 32); \
  149. } while (0)
  150. #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
  151. #endif /* !CONFIG_PARAVIRT */
  152. #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
  153. (u32)((val) >> 32))
  154. #define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
  155. #define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
  156. #ifdef CONFIG_SMP
  157. int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  158. int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  159. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  160. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  161. #else /* CONFIG_SMP */
  162. static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  163. {
  164. rdmsr(msr_no, *l, *h);
  165. return 0;
  166. }
  167. static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  168. {
  169. wrmsr(msr_no, l, h);
  170. return 0;
  171. }
  172. static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
  173. u32 *l, u32 *h)
  174. {
  175. return rdmsr_safe(msr_no, l, h);
  176. }
  177. static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  178. {
  179. return wrmsr_safe(msr_no, l, h);
  180. }
  181. #endif /* CONFIG_SMP */
  182. #endif /* __ASSEMBLY__ */
  183. #endif /* __KERNEL__ */
  184. #endif