msr.h 8.0 KB

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