msr.h 7.0 KB

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