Subversion Repositories Kolibri OS

Rev

Rev 5270 | Rev 6934 | Go to most recent revision | 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. static inline unsigned long long native_read_tscp(unsigned int *aux)
  36. {
  37.         unsigned long low, high;
  38.         asm volatile(".byte 0x0f,0x01,0xf9"
  39.                      : "=a" (low), "=d" (high), "=c" (*aux));
  40.         return low | ((u64)high << 32);
  41. }
  42.  
  43. /*
  44.  * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
  45.  * constraint has different meanings. For i386, "A" means exactly
  46.  * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
  47.  * it means rax *or* rdx.
  48.  */
  49. #ifdef CONFIG_X86_64
  50. /* Using 64-bit values saves one instruction clearing the high half of low */
  51. #define DECLARE_ARGS(val, low, high)    unsigned long low, high
  52. #define EAX_EDX_VAL(val, low, high)     ((low) | (high) << 32)
  53. #define EAX_EDX_RET(val, low, high)     "=a" (low), "=d" (high)
  54. #else
  55. #define DECLARE_ARGS(val, low, high)    unsigned long long val
  56. #define EAX_EDX_VAL(val, low, high)     (val)
  57. #define EAX_EDX_RET(val, low, high)     "=A" (val)
  58. #endif
  59.  
  60. static inline unsigned long long native_read_msr(unsigned int msr)
  61. {
  62.         DECLARE_ARGS(val, low, high);
  63.  
  64.         asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
  65.         return EAX_EDX_VAL(val, low, high);
  66. }
  67.  
  68. static inline unsigned long long native_read_msr_safe(unsigned int msr,
  69.                                                       int *err)
  70. {
  71.         DECLARE_ARGS(val, low, high);
  72.  
  73.         asm volatile("2: rdmsr ; xor %[err],%[err]\n"
  74.                      "1:\n\t"
  75.                      ".section .fixup,\"ax\"\n\t"
  76.                      "3:  mov %[fault],%[err] ; jmp 1b\n\t"
  77.                      ".previous\n\t"
  78.                      _ASM_EXTABLE(2b, 3b)
  79.                      : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
  80.                      : "c" (msr), [fault] "i" (-EIO));
  81.         return EAX_EDX_VAL(val, low, high);
  82. }
  83.  
  84. static inline void native_write_msr(unsigned int msr,
  85.                                     unsigned low, unsigned high)
  86. {
  87.         asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
  88. }
  89.  
  90. /* Can be uninlined because referenced by paravirt */
  91. notrace static inline int native_write_msr_safe(unsigned int msr,
  92.                                         unsigned low, unsigned high)
  93. {
  94.         int err;
  95.         asm volatile("2: wrmsr ; xor %[err],%[err]\n"
  96.                      "1:\n\t"
  97.                      ".section .fixup,\"ax\"\n\t"
  98.                      "3:  mov %[fault],%[err] ; jmp 1b\n\t"
  99.                      ".previous\n\t"
  100.                      _ASM_EXTABLE(2b, 3b)
  101.                      : [err] "=a" (err)
  102.                      : "c" (msr), "0" (low), "d" (high),
  103.                        [fault] "i" (-EIO)
  104.                      : "memory");
  105.         return err;
  106. }
  107.  
  108. extern int rdmsr_safe_regs(u32 regs[8]);
  109. extern int wrmsr_safe_regs(u32 regs[8]);
  110.  
  111. /**
  112.  * rdtsc() - returns the current TSC without ordering constraints
  113.  *
  114.  * rdtsc() returns the result of RDTSC as a 64-bit integer.  The
  115.  * only ordering constraint it supplies is the ordering implied by
  116.  * "asm volatile": it will put the RDTSC in the place you expect.  The
  117.  * CPU can and will speculatively execute that RDTSC, though, so the
  118.  * results can be non-monotonic if compared on different CPUs.
  119.  */
  120. static __always_inline unsigned long long rdtsc(void)
  121. {
  122.         DECLARE_ARGS(val, low, high);
  123.  
  124.         asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
  125.  
  126.         return EAX_EDX_VAL(val, low, high);
  127. }
  128.  
  129. static inline unsigned long long native_read_pmc(int counter)
  130. {
  131.         DECLARE_ARGS(val, low, high);
  132.  
  133.         asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
  134.         return EAX_EDX_VAL(val, low, high);
  135. }
  136.  
  137. #ifdef CONFIG_PARAVIRT
  138. #include <asm/paravirt.h>
  139. #else
  140. #include <linux/errno.h>
  141. /*
  142.  * Access to machine-specific registers (available on 586 and better only)
  143.  * Note: the rd* operations modify the parameters directly (without using
  144.  * pointer indirection), this allows gcc to optimize better
  145.  */
  146.  
  147. #define rdmsr(msr, low, high)                                   \
  148. do {                                                            \
  149.         u64 __val = native_read_msr((msr));                     \
  150.         (void)((low) = (u32)__val);                             \
  151.         (void)((high) = (u32)(__val >> 32));                    \
  152. } while (0)
  153.  
  154. static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
  155. {
  156.         native_write_msr(msr, low, high);
  157. }
  158.  
  159. #define rdmsrl(msr, val)                        \
  160.         ((val) = native_read_msr((msr)))
  161.  
  162. static inline void wrmsrl(unsigned msr, u64 val)
  163. {
  164.         native_write_msr(msr, (u32)val, (u32)(val >> 32));
  165. }
  166.  
  167. /* wrmsr with exception handling */
  168. static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
  169. {
  170.         return native_write_msr_safe(msr, low, high);
  171. }
  172.  
  173. /* rdmsr with exception handling */
  174. #define rdmsr_safe(msr, low, high)                              \
  175. ({                                                              \
  176.         int __err;                                              \
  177.         u64 __val = native_read_msr_safe((msr), &__err);        \
  178.         (*low) = (u32)__val;                                    \
  179.         (*high) = (u32)(__val >> 32);                           \
  180.         __err;                                                  \
  181. })
  182.  
  183. static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
  184. {
  185.         int err;
  186.  
  187.         *p = native_read_msr_safe(msr, &err);
  188.         return err;
  189. }
  190.  
  191. #define rdpmc(counter, low, high)                       \
  192. do {                                                    \
  193.         u64 _l = native_read_pmc((counter));            \
  194.         (low)  = (u32)_l;                               \
  195.         (high) = (u32)(_l >> 32);                       \
  196. } while (0)
  197.  
  198. #define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
  199.  
  200. #endif  /* !CONFIG_PARAVIRT */
  201.  
  202. /*
  203.  * 64-bit version of wrmsr_safe():
  204.  */
  205. static inline int wrmsrl_safe(u32 msr, u64 val)
  206. {
  207.         return wrmsr_safe(msr, (u32)val,  (u32)(val >> 32));
  208. }
  209.  
  210. #define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
  211.  
  212. #define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
  213.  
  214. struct msr *msrs_alloc(void);
  215. void msrs_free(struct msr *msrs);
  216. int msr_set_bit(u32 msr, u8 bit);
  217. int msr_clear_bit(u32 msr, u8 bit);
  218.  
  219. #ifdef CONFIG_SMP
  220. int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  221. int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  222. int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
  223. int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
  224. void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
  225. void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
  226. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
  227. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
  228. int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
  229. int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
  230. int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
  231. int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
  232. #else  /*  CONFIG_SMP  */
  233. static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  234. {
  235.         rdmsr(msr_no, *l, *h);
  236.         return 0;
  237. }
  238. static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  239. {
  240.         wrmsr(msr_no, l, h);
  241.         return 0;
  242. }
  243. static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
  244. {
  245.         rdmsrl(msr_no, *q);
  246.         return 0;
  247. }
  248. static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
  249. {
  250.         wrmsrl(msr_no, q);
  251.         return 0;
  252. }
  253. static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
  254.                                 struct msr *msrs)
  255. {
  256.        rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
  257. }
  258. static inline void wrmsr_on_cpus(const struct cpumask *m, u32 msr_no,
  259.                                 struct msr *msrs)
  260. {
  261.        wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
  262. }
  263. static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
  264.                                     u32 *l, u32 *h)
  265. {
  266.         return rdmsr_safe(msr_no, l, h);
  267. }
  268. static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  269. {
  270.         return wrmsr_safe(msr_no, l, h);
  271. }
  272. static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
  273. {
  274.         return rdmsrl_safe(msr_no, q);
  275. }
  276. static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
  277. {
  278.         return wrmsrl_safe(msr_no, q);
  279. }
  280. static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
  281. {
  282.         return rdmsr_safe_regs(regs);
  283. }
  284. static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
  285. {
  286.         return wrmsr_safe_regs(regs);
  287. }
  288. #endif  /* CONFIG_SMP */
  289. #endif /* __ASSEMBLY__ */
  290. #endif /* _ASM_X86_MSR_H */
  291.