msr.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. static inline unsigned long long native_read_tscp(int *aux)
  10. {
  11. unsigned long low, high;
  12. asm volatile (".byte 0x0f,0x01,0xf9"
  13. : "=a" (low), "=d" (high), "=c" (*aux));
  14. return low | ((u64)high >> 32);
  15. }
  16. #define rdtscp(low, high, aux) \
  17. do { \
  18. unsigned long long _val = native_read_tscp(&(aux)); \
  19. (low) = (u32)_val; \
  20. (high) = (u32)(_val >> 32); \
  21. } while (0)
  22. #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
  23. #endif
  24. #endif
  25. #ifdef __i386__
  26. #ifdef __KERNEL__
  27. #ifndef __ASSEMBLY__
  28. #include <asm/asm.h>
  29. #include <asm/errno.h>
  30. static inline unsigned long long native_read_msr(unsigned int msr)
  31. {
  32. unsigned long long val;
  33. asm volatile("rdmsr" : "=A" (val) : "c" (msr));
  34. return val;
  35. }
  36. static inline unsigned long long native_read_msr_safe(unsigned int msr,
  37. int *err)
  38. {
  39. unsigned long long val;
  40. asm volatile("2: rdmsr ; xor %0,%0\n"
  41. "1:\n\t"
  42. ".section .fixup,\"ax\"\n\t"
  43. "3: mov %3,%0 ; jmp 1b\n\t"
  44. ".previous\n\t"
  45. ".section __ex_table,\"a\"\n"
  46. _ASM_ALIGN "\n\t"
  47. _ASM_PTR " 2b,3b\n\t"
  48. ".previous"
  49. : "=r" (*err), "=A" (val)
  50. : "c" (msr), "i" (-EFAULT));
  51. return val;
  52. }
  53. static inline void native_write_msr(unsigned int msr,
  54. unsigned low, unsigned high)
  55. {
  56. asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high));
  57. }
  58. static inline int native_write_msr_safe(unsigned int msr,
  59. unsigned low, unsigned high)
  60. {
  61. int err;
  62. asm volatile("2: wrmsr ; xor %0,%0\n"
  63. "1:\n\t"
  64. ".section .fixup,\"ax\"\n\t"
  65. "3: mov %4,%0 ; jmp 1b\n\t"
  66. ".previous\n\t"
  67. ".section __ex_table,\"a\"\n"
  68. _ASM_ALIGN "\n\t"
  69. _ASM_PTR " 2b,3b\n\t"
  70. ".previous"
  71. : "=a" (err)
  72. : "c" (msr), "0" (low), "d" (high),
  73. "i" (-EFAULT));
  74. return err;
  75. }
  76. static inline unsigned long long native_read_tsc(void)
  77. {
  78. unsigned long long val;
  79. asm volatile("rdtsc" : "=A" (val));
  80. return val;
  81. }
  82. static inline unsigned long long native_read_pmc(int counter)
  83. {
  84. unsigned long long val;
  85. asm volatile("rdpmc" : "=A" (val) : "c" (counter));
  86. return val;
  87. }
  88. #ifdef CONFIG_PARAVIRT
  89. #include <asm/paravirt.h>
  90. #else
  91. #include <linux/errno.h>
  92. /*
  93. * Access to machine-specific registers (available on 586 and better only)
  94. * Note: the rd* operations modify the parameters directly (without using
  95. * pointer indirection), this allows gcc to optimize better
  96. */
  97. #define rdmsr(msr,val1,val2) \
  98. do { \
  99. u64 __val = native_read_msr(msr); \
  100. (val1) = (u32)__val; \
  101. (val2) = (u32)(__val >> 32); \
  102. } while(0)
  103. static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
  104. {
  105. native_write_msr(msr, low, high);
  106. }
  107. #define rdmsrl(msr,val) \
  108. ((val) = native_read_msr(msr))
  109. #define wrmsrl(msr, val) native_write_msr(msr, (u32)val, (u32)(val >> 32))
  110. /* wrmsr with exception handling */
  111. static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
  112. {
  113. return native_write_msr_safe(msr, low, high);
  114. }
  115. /* rdmsr with exception handling */
  116. #define rdmsr_safe(msr,p1,p2) \
  117. ({ \
  118. int __err; \
  119. u64 __val = native_read_msr_safe(msr, &__err); \
  120. (*p1) = (u32)__val; \
  121. (*p2) = (u32)(__val >> 32); \
  122. __err; \
  123. })
  124. #define rdtscl(low) \
  125. ((low) = (u32)native_read_tsc())
  126. #define rdtscll(val) \
  127. ((val) = native_read_tsc())
  128. #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
  129. #define rdpmc(counter,low,high) \
  130. do { \
  131. u64 _l = native_read_pmc(counter); \
  132. (low) = (u32)_l; \
  133. (high) = (u32)(_l >> 32); \
  134. } while(0)
  135. #endif /* !CONFIG_PARAVIRT */
  136. #endif /* ! __ASSEMBLY__ */
  137. #endif /* __KERNEL__ */
  138. #else /* __i386__ */
  139. #ifndef __ASSEMBLY__
  140. #include <linux/errno.h>
  141. /*
  142. * Access to machine-specific registers (available on 586 and better only)
  143. * Note: the rd* operations modify the parameters directly (without using
  144. * pointer indirection), this allows gcc to optimize better
  145. */
  146. #define rdmsr(msr,val1,val2) \
  147. __asm__ __volatile__("rdmsr" \
  148. : "=a" (val1), "=d" (val2) \
  149. : "c" (msr))
  150. #define rdmsrl(msr,val) do { unsigned long a__,b__; \
  151. __asm__ __volatile__("rdmsr" \
  152. : "=a" (a__), "=d" (b__) \
  153. : "c" (msr)); \
  154. val = a__ | (b__<<32); \
  155. } while(0)
  156. #define wrmsr(msr,val1,val2) \
  157. __asm__ __volatile__("wrmsr" \
  158. : /* no outputs */ \
  159. : "c" (msr), "a" (val1), "d" (val2))
  160. #define wrmsrl(msr,val) wrmsr(msr,(__u32)((__u64)(val)),((__u64)(val))>>32)
  161. #define rdtsc(low,high) \
  162. __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
  163. #define rdtscl(low) \
  164. __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
  165. #define rdtscll(val) do { \
  166. unsigned int __a,__d; \
  167. __asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
  168. (val) = ((unsigned long)__a) | (((unsigned long)__d)<<32); \
  169. } while(0)
  170. #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
  171. #define write_rdtscp_aux(val) wrmsr(0xc0000103, val, 0)
  172. #define rdpmc(counter,low,high) \
  173. __asm__ __volatile__("rdpmc" \
  174. : "=a" (low), "=d" (high) \
  175. : "c" (counter))
  176. #ifdef __KERNEL__
  177. /* wrmsr with exception handling */
  178. #define wrmsr_safe(msr,a,b) ({ int ret__; \
  179. asm volatile("2: wrmsr ; xorl %0,%0\n" \
  180. "1:\n\t" \
  181. ".section .fixup,\"ax\"\n\t" \
  182. "3: movl %4,%0 ; jmp 1b\n\t" \
  183. ".previous\n\t" \
  184. ".section __ex_table,\"a\"\n" \
  185. " .align 8\n\t" \
  186. " .quad 2b,3b\n\t" \
  187. ".previous" \
  188. : "=a" (ret__) \
  189. : "c" (msr), "0" (a), "d" (b), "i" (-EFAULT)); \
  190. ret__; })
  191. #define checking_wrmsrl(msr,val) wrmsr_safe(msr,(u32)(val),(u32)((val)>>32))
  192. #define rdmsr_safe(msr,a,b) \
  193. ({ int ret__; \
  194. asm volatile ("1: rdmsr\n" \
  195. "2:\n" \
  196. ".section .fixup,\"ax\"\n" \
  197. "3: movl %4,%0\n" \
  198. " jmp 2b\n" \
  199. ".previous\n" \
  200. ".section __ex_table,\"a\"\n" \
  201. " .align 8\n" \
  202. " .quad 1b,3b\n" \
  203. ".previous":"=&bDS" (ret__), "=a"(*(a)), "=d"(*(b)) \
  204. :"c"(msr), "i"(-EIO), "0"(0)); \
  205. ret__; })
  206. #endif /* __ASSEMBLY__ */
  207. #endif /* !__i386__ */
  208. #ifndef __ASSEMBLY__
  209. #ifdef CONFIG_SMP
  210. void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  211. void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  212. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  213. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  214. #else /* CONFIG_SMP */
  215. static inline void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  216. {
  217. rdmsr(msr_no, *l, *h);
  218. }
  219. static inline void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  220. {
  221. wrmsr(msr_no, l, h);
  222. }
  223. static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  224. {
  225. return rdmsr_safe(msr_no, l, h);
  226. }
  227. static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  228. {
  229. return wrmsr_safe(msr_no, l, h);
  230. }
  231. #endif /* CONFIG_SMP */
  232. #endif /* __KERNEL__ */
  233. #endif /* __ASSEMBLY__ */
  234. #endif