Subversion Repositories Kolibri OS

Rev

Rev 7143 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef _ASM_X86_MSR_H
  2. #define _ASM_X86_MSR_H
  3.  
  4. #include "msr-index.h"
  5.  
  6. #ifndef __ASSEMBLY__
  7.  
  8. #include <asm/asm.h>
  9. #include <asm/errno.h>
  10. #include <asm/cpumask.h>
  11. #include <uapi/asm/msr.h>
  12.  
  13. struct msr {
  14.         union {
  15.                 struct {
  16.                         u32 l;
  17.                         u32 h;
  18.                 };
  19.                 u64 q;
  20.         };
  21. };
  22.  
  23. struct msr_info {
  24.         u32 msr_no;
  25.         struct msr reg;
  26.         struct msr *msrs;
  27.         int err;
  28. };
  29.  
  30. struct msr_regs_info {
  31.         u32 *regs;
  32.         int err;
  33. };
  34.  
  35. struct saved_msr {
  36.         bool valid;
  37.         struct msr_info info;
  38. };
  39.  
  40. struct saved_msrs {
  41.         unsigned int num;
  42.         struct saved_msr *array;
  43. };
  44.  
  45. /*
  46.  * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
  47.  * constraint has different meanings. For i386, "A" means exactly
  48.  * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
  49.  * it means rax *or* rdx.
  50.  */
  51. #ifdef CONFIG_X86_64
  52. /* Using 64-bit values saves one instruction clearing the high half of low */
  53. #define DECLARE_ARGS(val, low, high)    unsigned long low, high
  54. #define EAX_EDX_VAL(val, low, high)     ((low) | (high) << 32)
  55. #define EAX_EDX_RET(val, low, high)     "=a" (low), "=d" (high)
  56. #else
  57. #define DECLARE_ARGS(val, low, high)    unsigned long long val
  58. #define EAX_EDX_VAL(val, low, high)     (val)
  59. #define EAX_EDX_RET(val, low, high)     "=A" (val)
  60. #endif
  61.  
  62. #ifdef CONFIG_TRACEPOINTS
  63. /*
  64.  * Be very careful with includes. This header is prone to include loops.
  65.  */
  66. #include <asm/atomic.h>
  67. #include <linux/tracepoint-defs.h>
  68.  
  69. extern struct tracepoint __tracepoint_read_msr;
  70. extern struct tracepoint __tracepoint_write_msr;
  71. extern struct tracepoint __tracepoint_rdpmc;
  72. #define msr_tracepoint_active(t) static_key_false(&(t).key)
  73. extern void do_trace_write_msr(unsigned msr, u64 val, int failed);
  74. extern void do_trace_read_msr(unsigned msr, u64 val, int failed);
  75. extern void do_trace_rdpmc(unsigned msr, u64 val, int failed);
  76. #else
  77. #define msr_tracepoint_active(t) false
  78. static inline void do_trace_write_msr(unsigned msr, u64 val, int failed) {}
  79. static inline void do_trace_read_msr(unsigned msr, u64 val, int failed) {}
  80. static inline void do_trace_rdpmc(unsigned msr, u64 val, int failed) {}
  81. #endif
  82.  
  83. static inline unsigned long long native_read_msr(unsigned int msr)
  84. {
  85.         DECLARE_ARGS(val, low, high);
  86.  
  87.         asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
  88.         if (msr_tracepoint_active(__tracepoint_read_msr))
  89.                 do_trace_read_msr(msr, EAX_EDX_VAL(val, low, high), 0);
  90.         return EAX_EDX_VAL(val, low, high);
  91. }
  92.  
  93. static inline unsigned long long native_read_msr_safe(unsigned int msr,
  94.                                                       int *err)
  95. {
  96. #warning "FIXME: native_read_msr_safe always return err = 0!"
  97.         *err = 0;
  98.         return native_read_msr(msr);
  99. }
  100.  
  101. static inline void native_write_msr(unsigned int msr,
  102.                                     unsigned low, unsigned high)
  103. {
  104.         asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
  105.         if (msr_tracepoint_active(__tracepoint_write_msr))
  106.                 do_trace_write_msr(msr, ((u64)high << 32 | low), 0);
  107. }
  108.  
  109. /* Can be uninlined because referenced by paravirt */
  110. notrace static inline int native_write_msr_safe(unsigned int msr,
  111.                                         unsigned low, unsigned high)
  112. {
  113. #warning "FIXME: native_write_msr_safe always return 0!"
  114.         native_write_msr(msr, low, high);
  115.         return 0;
  116. }
  117.  
  118. extern int rdmsr_safe_regs(u32 regs[8]);
  119. extern int wrmsr_safe_regs(u32 regs[8]);
  120.  
  121. /**
  122.  * rdtsc() - returns the current TSC without ordering constraints
  123.  *
  124.  * rdtsc() returns the result of RDTSC as a 64-bit integer.  The
  125.  * only ordering constraint it supplies is the ordering implied by
  126.  * "asm volatile": it will put the RDTSC in the place you expect.  The
  127.  * CPU can and will speculatively execute that RDTSC, though, so the
  128.  * results can be non-monotonic if compared on different CPUs.
  129.  */
  130. static __always_inline unsigned long long rdtsc(void)
  131. {
  132.         DECLARE_ARGS(val, low, high);
  133.  
  134.         asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
  135.  
  136.         return EAX_EDX_VAL(val, low, high);
  137. }
  138.  
  139. /**
  140.  * rdtsc_ordered() - read the current TSC in program order
  141.  *
  142.  * rdtsc_ordered() returns the result of RDTSC as a 64-bit integer.
  143.  * It is ordered like a load to a global in-memory counter.  It should
  144.  * be impossible to observe non-monotonic rdtsc_unordered() behavior
  145.  * across multiple CPUs as long as the TSC is synced.
  146.  */
  147. static __always_inline unsigned long long rdtsc_ordered(void)
  148. {
  149.         /*
  150.          * The RDTSC instruction is not ordered relative to memory
  151.          * access.  The Intel SDM and the AMD APM are both vague on this
  152.          * point, but empirically an RDTSC instruction can be
  153.          * speculatively executed before prior loads.  An RDTSC
  154.          * immediately after an appropriate barrier appears to be
  155.          * ordered as a normal load, that is, it provides the same
  156.          * ordering guarantees as reading from a global memory location
  157.          * that some other imaginary CPU is updating continuously with a
  158.          * time stamp.
  159.          */
  160.         alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC,
  161.                           "lfence", X86_FEATURE_LFENCE_RDTSC);
  162.         return rdtsc();
  163. }
  164.  
  165. /* Deprecated, keep it for a cycle for easier merging: */
  166. #define rdtscll(now)    do { (now) = rdtsc_ordered(); } while (0)
  167.  
  168. static inline unsigned long long native_read_pmc(int counter)
  169. {
  170.         DECLARE_ARGS(val, low, high);
  171.  
  172.         asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
  173.         if (msr_tracepoint_active(__tracepoint_rdpmc))
  174.                 do_trace_rdpmc(counter, EAX_EDX_VAL(val, low, high), 0);
  175.         return EAX_EDX_VAL(val, low, high);
  176. }
  177.  
  178. #ifdef CONFIG_PARAVIRT
  179. #include <asm/paravirt.h>
  180. #else
  181. #include <linux/errno.h>
  182. /*
  183.  * Access to machine-specific registers (available on 586 and better only)
  184.  * Note: the rd* operations modify the parameters directly (without using
  185.  * pointer indirection), this allows gcc to optimize better
  186.  */
  187.  
  188. #define rdmsr(msr, low, high)                                   \
  189. do {                                                            \
  190.         u64 __val = native_read_msr((msr));                     \
  191.         (void)((low) = (u32)__val);                             \
  192.         (void)((high) = (u32)(__val >> 32));                    \
  193. } while (0)
  194.  
  195. static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
  196. {
  197.         native_write_msr(msr, low, high);
  198. }
  199.  
  200. #define rdmsrl(msr, val)                        \
  201.         ((val) = native_read_msr((msr)))
  202.  
  203. static inline void wrmsrl(unsigned msr, u64 val)
  204. {
  205.         native_write_msr(msr, (u32)(val & 0xffffffffULL), (u32)(val >> 32));
  206. }
  207.  
  208. /* wrmsr with exception handling */
  209. static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
  210. {
  211.         return native_write_msr_safe(msr, low, high);
  212. }
  213.  
  214. /* rdmsr with exception handling */
  215. #define rdmsr_safe(msr, low, high)                              \
  216. ({                                                              \
  217.         int __err;                                              \
  218.         u64 __val = native_read_msr_safe((msr), &__err);        \
  219.         (*low) = (u32)__val;                                    \
  220.         (*high) = (u32)(__val >> 32);                           \
  221.         __err;                                                  \
  222. })
  223.  
  224. static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
  225. {
  226.         int err;
  227.  
  228.         *p = native_read_msr_safe(msr, &err);
  229.         return err;
  230. }
  231.  
  232. #define rdpmc(counter, low, high)                       \
  233. do {                                                    \
  234.         u64 _l = native_read_pmc((counter));            \
  235.         (low)  = (u32)_l;                               \
  236.         (high) = (u32)(_l >> 32);                       \
  237. } while (0)
  238.  
  239. #define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
  240.  
  241. #endif  /* !CONFIG_PARAVIRT */
  242.  
  243. /*
  244.  * 64-bit version of wrmsr_safe():
  245.  */
  246. static inline int wrmsrl_safe(u32 msr, u64 val)
  247. {
  248.         return wrmsr_safe(msr, (u32)val,  (u32)(val >> 32));
  249. }
  250.  
  251. #define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
  252.  
  253. #define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
  254.  
  255. struct msr *msrs_alloc(void);
  256. void msrs_free(struct msr *msrs);
  257. int msr_set_bit(u32 msr, u8 bit);
  258. int msr_clear_bit(u32 msr, u8 bit);
  259.  
  260. #ifdef CONFIG_SMP
  261. int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  262. int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  263. int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
  264. int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
  265. void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
  266. void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
  267. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  268. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  269. int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
  270. int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
  271. int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
  272. int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
  273. #else  /*  CONFIG_SMP  */
  274. static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  275. {
  276.         rdmsr(msr_no, *l, *h);
  277.         return 0;
  278. }
  279. static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  280. {
  281.         wrmsr(msr_no, l, h);
  282.         return 0;
  283. }
  284. static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
  285. {
  286.         rdmsrl(msr_no, *q);
  287.         return 0;
  288. }
  289. static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
  290. {
  291.         wrmsrl(msr_no, q);
  292.         return 0;
  293. }
  294. static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
  295.                                 struct msr *msrs)
  296. {
  297.        rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
  298. }
  299. static inline void wrmsr_on_cpus(const struct cpumask *m, u32 msr_no,
  300.                                 struct msr *msrs)
  301. {
  302.        wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
  303. }
  304. static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
  305.                                     u32 *l, u32 *h)
  306. {
  307.         return rdmsr_safe(msr_no, l, h);
  308. }
  309. static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  310. {
  311.         return wrmsr_safe(msr_no, l, h);
  312. }
  313. static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
  314. {
  315.         return rdmsrl_safe(msr_no, q);
  316. }
  317. static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
  318. {
  319.         return wrmsrl_safe(msr_no, q);
  320. }
  321. static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
  322. {
  323.         return rdmsr_safe_regs(regs);
  324. }
  325. static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
  326. {
  327.         return wrmsr_safe_regs(regs);
  328. }
  329. #endif  /* CONFIG_SMP */
  330. #endif /* __ASSEMBLY__ */
  331. #endif /* _ASM_X86_MSR_H */
  332.