msr.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 __i386__
  8. #ifdef __KERNEL__
  9. #ifndef __ASSEMBLY__
  10. #include <asm/errno.h>
  11. static inline unsigned long long native_read_msr(unsigned int msr)
  12. {
  13. unsigned long long val;
  14. asm volatile("rdmsr" : "=A" (val) : "c" (msr));
  15. return val;
  16. }
  17. static inline unsigned long long native_read_msr_safe(unsigned int msr,
  18. int *err)
  19. {
  20. unsigned long long val;
  21. asm volatile("2: rdmsr ; xorl %0,%0\n"
  22. "1:\n\t"
  23. ".section .fixup,\"ax\"\n\t"
  24. "3: movl %3,%0 ; jmp 1b\n\t"
  25. ".previous\n\t"
  26. ".section __ex_table,\"a\"\n"
  27. " .align 4\n\t"
  28. " .long 2b,3b\n\t"
  29. ".previous"
  30. : "=r" (*err), "=A" (val)
  31. : "c" (msr), "i" (-EFAULT));
  32. return val;
  33. }
  34. static inline void native_write_msr(unsigned int msr, unsigned long long val)
  35. {
  36. asm volatile("wrmsr" : : "c" (msr), "A"(val));
  37. }
  38. static inline int native_write_msr_safe(unsigned int msr,
  39. unsigned long long val)
  40. {
  41. int err;
  42. asm volatile("2: wrmsr ; xorl %0,%0\n"
  43. "1:\n\t"
  44. ".section .fixup,\"ax\"\n\t"
  45. "3: movl %4,%0 ; jmp 1b\n\t"
  46. ".previous\n\t"
  47. ".section __ex_table,\"a\"\n"
  48. " .align 4\n\t"
  49. " .long 2b,3b\n\t"
  50. ".previous"
  51. : "=a" (err)
  52. : "c" (msr), "0" ((u32)val), "d" ((u32)(val>>32)),
  53. "i" (-EFAULT));
  54. return err;
  55. }
  56. static inline unsigned long long native_read_tsc(void)
  57. {
  58. unsigned long long val;
  59. asm volatile("rdtsc" : "=A" (val));
  60. return val;
  61. }
  62. static inline unsigned long long native_read_pmc(void)
  63. {
  64. unsigned long long val;
  65. asm volatile("rdpmc" : "=A" (val));
  66. return val;
  67. }
  68. #ifdef CONFIG_PARAVIRT
  69. #include <asm/paravirt.h>
  70. #else
  71. #include <linux/errno.h>
  72. /*
  73. * Access to machine-specific registers (available on 586 and better only)
  74. * Note: the rd* operations modify the parameters directly (without using
  75. * pointer indirection), this allows gcc to optimize better
  76. */
  77. #define rdmsr(msr,val1,val2) \
  78. do { \
  79. u64 __val = native_read_msr(msr); \
  80. (val1) = (u32)__val; \
  81. (val2) = (u32)(__val >> 32); \
  82. } while(0)
  83. static inline void wrmsr(u32 __msr, u32 __low, u32 __high)
  84. {
  85. native_write_msr(__msr, ((u64)__high << 32) | __low);
  86. }
  87. #define rdmsrl(msr,val) \
  88. ((val) = native_read_msr(msr))
  89. #define wrmsrl(msr,val) native_write_msr(msr, val)
  90. /* wrmsr with exception handling */
  91. static inline int wrmsr_safe(u32 __msr, u32 __low, u32 __high)
  92. {
  93. return native_write_msr_safe(__msr, ((u64)__high << 32) | __low);
  94. }
  95. /* rdmsr with exception handling */
  96. #define rdmsr_safe(msr,p1,p2) \
  97. ({ \
  98. int __err; \
  99. u64 __val = native_read_msr_safe(msr, &__err); \
  100. (*p1) = (u32)__val; \
  101. (*p2) = (u32)(__val >> 32); \
  102. __err; \
  103. })
  104. #define rdtscl(low) \
  105. ((low) = (u32)native_read_tsc())
  106. #define rdtscll(val) \
  107. ((val) = native_read_tsc())
  108. #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
  109. #define rdpmc(counter,low,high) \
  110. do { \
  111. u64 _l = native_read_pmc(); \
  112. (low) = (u32)_l; \
  113. (high) = (u32)(_l >> 32); \
  114. } while(0)
  115. #endif /* !CONFIG_PARAVIRT */
  116. #ifdef CONFIG_SMP
  117. void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  118. void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  119. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  120. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  121. #else /* CONFIG_SMP */
  122. static inline void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  123. {
  124. rdmsr(msr_no, *l, *h);
  125. }
  126. static inline void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  127. {
  128. wrmsr(msr_no, l, h);
  129. }
  130. static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  131. {
  132. return rdmsr_safe(msr_no, l, h);
  133. }
  134. static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  135. {
  136. return wrmsr_safe(msr_no, l, h);
  137. }
  138. #endif /* CONFIG_SMP */
  139. #endif /* ! __ASSEMBLY__ */
  140. #endif /* __KERNEL__ */
  141. #else /* __i386__ */
  142. #ifndef __ASSEMBLY__
  143. #include <linux/errno.h>
  144. /*
  145. * Access to machine-specific registers (available on 586 and better only)
  146. * Note: the rd* operations modify the parameters directly (without using
  147. * pointer indirection), this allows gcc to optimize better
  148. */
  149. #define rdmsr(msr,val1,val2) \
  150. __asm__ __volatile__("rdmsr" \
  151. : "=a" (val1), "=d" (val2) \
  152. : "c" (msr))
  153. #define rdmsrl(msr,val) do { unsigned long a__,b__; \
  154. __asm__ __volatile__("rdmsr" \
  155. : "=a" (a__), "=d" (b__) \
  156. : "c" (msr)); \
  157. val = a__ | (b__<<32); \
  158. } while(0)
  159. #define wrmsr(msr,val1,val2) \
  160. __asm__ __volatile__("wrmsr" \
  161. : /* no outputs */ \
  162. : "c" (msr), "a" (val1), "d" (val2))
  163. #define wrmsrl(msr,val) wrmsr(msr,(__u32)((__u64)(val)),((__u64)(val))>>32)
  164. #define rdtsc(low,high) \
  165. __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
  166. #define rdtscl(low) \
  167. __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
  168. #define rdtscp(low,high,aux) \
  169. __asm__ __volatile__ (".byte 0x0f,0x01,0xf9" : "=a" (low), "=d" (high), "=c" (aux))
  170. #define rdtscll(val) do { \
  171. unsigned int __a,__d; \
  172. __asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
  173. (val) = ((unsigned long)__a) | (((unsigned long)__d)<<32); \
  174. } while(0)
  175. #define rdtscpll(val, aux) do { \
  176. unsigned long __a, __d; \
  177. __asm__ __volatile__ (".byte 0x0f,0x01,0xf9" : "=a" (__a), "=d" (__d), "=c" (aux)); \
  178. (val) = (__d << 32) | __a; \
  179. } while (0)
  180. #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
  181. #define write_rdtscp_aux(val) wrmsr(0xc0000103, val, 0)
  182. #define rdpmc(counter,low,high) \
  183. __asm__ __volatile__("rdpmc" \
  184. : "=a" (low), "=d" (high) \
  185. : "c" (counter))
  186. static inline void cpuid(int op, unsigned int *eax, unsigned int *ebx,
  187. unsigned int *ecx, unsigned int *edx)
  188. {
  189. __asm__("cpuid"
  190. : "=a" (*eax),
  191. "=b" (*ebx),
  192. "=c" (*ecx),
  193. "=d" (*edx)
  194. : "0" (op));
  195. }
  196. /* Some CPUID calls want 'count' to be placed in ecx */
  197. static inline void cpuid_count(int op, int count, int *eax, int *ebx, int *ecx,
  198. int *edx)
  199. {
  200. __asm__("cpuid"
  201. : "=a" (*eax),
  202. "=b" (*ebx),
  203. "=c" (*ecx),
  204. "=d" (*edx)
  205. : "0" (op), "c" (count));
  206. }
  207. /*
  208. * CPUID functions returning a single datum
  209. */
  210. static inline unsigned int cpuid_eax(unsigned int op)
  211. {
  212. unsigned int eax;
  213. __asm__("cpuid"
  214. : "=a" (eax)
  215. : "0" (op)
  216. : "bx", "cx", "dx");
  217. return eax;
  218. }
  219. static inline unsigned int cpuid_ebx(unsigned int op)
  220. {
  221. unsigned int eax, ebx;
  222. __asm__("cpuid"
  223. : "=a" (eax), "=b" (ebx)
  224. : "0" (op)
  225. : "cx", "dx" );
  226. return ebx;
  227. }
  228. static inline unsigned int cpuid_ecx(unsigned int op)
  229. {
  230. unsigned int eax, ecx;
  231. __asm__("cpuid"
  232. : "=a" (eax), "=c" (ecx)
  233. : "0" (op)
  234. : "bx", "dx" );
  235. return ecx;
  236. }
  237. static inline unsigned int cpuid_edx(unsigned int op)
  238. {
  239. unsigned int eax, edx;
  240. __asm__("cpuid"
  241. : "=a" (eax), "=d" (edx)
  242. : "0" (op)
  243. : "bx", "cx");
  244. return edx;
  245. }
  246. #ifdef __KERNEL__
  247. /* wrmsr with exception handling */
  248. #define wrmsr_safe(msr,a,b) ({ int ret__; \
  249. asm volatile("2: wrmsr ; xorl %0,%0\n" \
  250. "1:\n\t" \
  251. ".section .fixup,\"ax\"\n\t" \
  252. "3: movl %4,%0 ; jmp 1b\n\t" \
  253. ".previous\n\t" \
  254. ".section __ex_table,\"a\"\n" \
  255. " .align 8\n\t" \
  256. " .quad 2b,3b\n\t" \
  257. ".previous" \
  258. : "=a" (ret__) \
  259. : "c" (msr), "0" (a), "d" (b), "i" (-EFAULT)); \
  260. ret__; })
  261. #define checking_wrmsrl(msr,val) wrmsr_safe(msr,(u32)(val),(u32)((val)>>32))
  262. #define rdmsr_safe(msr,a,b) \
  263. ({ int ret__; \
  264. asm volatile ("1: rdmsr\n" \
  265. "2:\n" \
  266. ".section .fixup,\"ax\"\n" \
  267. "3: movl %4,%0\n" \
  268. " jmp 2b\n" \
  269. ".previous\n" \
  270. ".section __ex_table,\"a\"\n" \
  271. " .align 8\n" \
  272. " .quad 1b,3b\n" \
  273. ".previous":"=&bDS" (ret__), "=a"(*(a)), "=d"(*(b)) \
  274. :"c"(msr), "i"(-EIO), "0"(0)); \
  275. ret__; })
  276. #ifdef CONFIG_SMP
  277. void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  278. void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  279. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  280. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  281. #else /* CONFIG_SMP */
  282. static inline void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  283. {
  284. rdmsr(msr_no, *l, *h);
  285. }
  286. static inline void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  287. {
  288. wrmsr(msr_no, l, h);
  289. }
  290. static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  291. {
  292. return rdmsr_safe(msr_no, l, h);
  293. }
  294. static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  295. {
  296. return wrmsr_safe(msr_no, l, h);
  297. }
  298. #endif /* CONFIG_SMP */
  299. #endif /* __KERNEL__ */
  300. #endif /* __ASSEMBLY__ */
  301. #endif /* !__i386__ */
  302. #endif