msr.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Taken from the linux kernel file of the same name
  3. *
  4. * (C) Copyright 2012
  5. * Graeme Russ, <graeme.russ@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #ifndef _ASM_X86_MSR_H
  23. #define _ASM_X86_MSR_H
  24. #include <asm/msr-index.h>
  25. #ifndef __ASSEMBLY__
  26. #include <linux/types.h>
  27. #include <linux/ioctl.h>
  28. #define X86_IOC_RDMSR_REGS _IOWR('c', 0xA0, __u32[8])
  29. #define X86_IOC_WRMSR_REGS _IOWR('c', 0xA1, __u32[8])
  30. #ifdef __KERNEL__
  31. #include <asm/errno.h>
  32. struct msr {
  33. union {
  34. struct {
  35. u32 l;
  36. u32 h;
  37. };
  38. u64 q;
  39. };
  40. };
  41. struct msr_info {
  42. u32 msr_no;
  43. struct msr reg;
  44. struct msr *msrs;
  45. int err;
  46. };
  47. struct msr_regs_info {
  48. u32 *regs;
  49. int err;
  50. };
  51. static inline unsigned long long native_read_tscp(unsigned int *aux)
  52. {
  53. unsigned long low, high;
  54. asm volatile(".byte 0x0f,0x01,0xf9"
  55. : "=a" (low), "=d" (high), "=c" (*aux));
  56. return low | ((u64)high << 32);
  57. }
  58. /*
  59. * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
  60. * constraint has different meanings. For i386, "A" means exactly
  61. * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
  62. * it means rax *or* rdx.
  63. */
  64. #ifdef CONFIG_X86_64
  65. #define DECLARE_ARGS(val, low, high) unsigned low, high
  66. #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
  67. #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
  68. #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
  69. #else
  70. #define DECLARE_ARGS(val, low, high) unsigned long long val
  71. #define EAX_EDX_VAL(val, low, high) (val)
  72. #define EAX_EDX_ARGS(val, low, high) "A" (val)
  73. #define EAX_EDX_RET(val, low, high) "=A" (val)
  74. #endif
  75. static inline unsigned long long native_read_msr(unsigned int msr)
  76. {
  77. DECLARE_ARGS(val, low, high);
  78. asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
  79. return EAX_EDX_VAL(val, low, high);
  80. }
  81. static inline void native_write_msr(unsigned int msr,
  82. unsigned low, unsigned high)
  83. {
  84. asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
  85. }
  86. extern unsigned long long native_read_tsc(void);
  87. extern int native_rdmsr_safe_regs(u32 regs[8]);
  88. extern int native_wrmsr_safe_regs(u32 regs[8]);
  89. static inline unsigned long long native_read_pmc(int counter)
  90. {
  91. DECLARE_ARGS(val, low, high);
  92. asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
  93. return EAX_EDX_VAL(val, low, high);
  94. }
  95. #ifdef CONFIG_PARAVIRT
  96. #include <asm/paravirt.h>
  97. #else
  98. #include <errno.h>
  99. /*
  100. * Access to machine-specific registers (available on 586 and better only)
  101. * Note: the rd* operations modify the parameters directly (without using
  102. * pointer indirection), this allows gcc to optimize better
  103. */
  104. #define rdmsr(msr, val1, val2) \
  105. do { \
  106. u64 __val = native_read_msr((msr)); \
  107. (void)((val1) = (u32)__val); \
  108. (void)((val2) = (u32)(__val >> 32)); \
  109. } while (0)
  110. static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
  111. {
  112. native_write_msr(msr, low, high);
  113. }
  114. #define rdmsrl(msr, val) \
  115. ((val) = native_read_msr((msr)))
  116. #define wrmsrl(msr, val) \
  117. native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
  118. /* rdmsr with exception handling */
  119. #define rdmsr_safe(msr, p1, p2) \
  120. ({ \
  121. int __err; \
  122. u64 __val = native_read_msr_safe((msr), &__err); \
  123. (*p1) = (u32)__val; \
  124. (*p2) = (u32)(__val >> 32); \
  125. __err; \
  126. })
  127. static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
  128. {
  129. u32 gprs[8] = { 0 };
  130. int err;
  131. gprs[1] = msr;
  132. gprs[7] = 0x9c5a203a;
  133. err = native_rdmsr_safe_regs(gprs);
  134. *p = gprs[0] | ((u64)gprs[2] << 32);
  135. return err;
  136. }
  137. static inline int wrmsrl_amd_safe(unsigned msr, unsigned long long val)
  138. {
  139. u32 gprs[8] = { 0 };
  140. gprs[0] = (u32)val;
  141. gprs[1] = msr;
  142. gprs[2] = val >> 32;
  143. gprs[7] = 0x9c5a203a;
  144. return native_wrmsr_safe_regs(gprs);
  145. }
  146. static inline int rdmsr_safe_regs(u32 regs[8])
  147. {
  148. return native_rdmsr_safe_regs(regs);
  149. }
  150. static inline int wrmsr_safe_regs(u32 regs[8])
  151. {
  152. return native_wrmsr_safe_regs(regs);
  153. }
  154. #define rdtscl(low) \
  155. ((low) = (u32)__native_read_tsc())
  156. #define rdtscll(val) \
  157. ((val) = __native_read_tsc())
  158. #define rdpmc(counter, low, high) \
  159. do { \
  160. u64 _l = native_read_pmc((counter)); \
  161. (low) = (u32)_l; \
  162. (high) = (u32)(_l >> 32); \
  163. } while (0)
  164. #define rdtscp(low, high, aux) \
  165. do { \
  166. unsigned long long _val = native_read_tscp(&(aux)); \
  167. (low) = (u32)_val; \
  168. (high) = (u32)(_val >> 32); \
  169. } while (0)
  170. #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
  171. #endif /* !CONFIG_PARAVIRT */
  172. #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
  173. (u32)((val) >> 32))
  174. #define write_tsc(val1, val2) wrmsr(MSR_IA32_TSC, (val1), (val2))
  175. #define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
  176. struct msr *msrs_alloc(void);
  177. void msrs_free(struct msr *msrs);
  178. #ifdef CONFIG_SMP
  179. int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  180. int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  181. void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
  182. void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
  183. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  184. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  185. int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
  186. int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
  187. #endif /* CONFIG_SMP */
  188. #endif /* __KERNEL__ */
  189. #endif /* __ASSEMBLY__ */
  190. #endif /* _ASM_X86_MSR_H */