msr.h 7.5 KB

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