]> xenbits.xensource.com Git - xtf.git/commitdiff
Provide library {rdmsr,wrmsr}_safe() functions
authorAndrew Cooper <andrew.cooper3@citrix.com>
Mon, 17 Oct 2016 09:36:45 +0000 (10:36 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Mon, 17 Oct 2016 10:06:20 +0000 (11:06 +0100)
Replace a local implementation in the MSR utility.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
arch/x86/extable.c
include/arch/x86/lib.h
tests/msr/main.c

index f1387018f2e76b41a39299a31698ec36886c2771..f3bb461779a3b63f386dbc709c5e9e8d4483f4bd 100644 (file)
@@ -26,6 +26,33 @@ bool ex_record_fault_eax(struct cpu_regs *regs, const struct extable_entry *ex)
     return true;
 }
 
+/**
+ * Fixup from a rdmsr fault
+ *
+ * Clobber the MSR index to signify error, and zero output.
+ */
+bool ex_rdmsr_safe(struct cpu_regs *regs, const struct extable_entry *ex)
+{
+    regs->ax = regs->dx = 0;
+    regs->cx = -1u;
+    regs->ip = ex->fixup;
+
+    return true;
+}
+
+/**
+ * Fixup from a wrmsr fault
+ *
+ * Clobber the MSR index to signify error.
+ */
+bool ex_wrmsr_safe(struct cpu_regs *regs, const struct extable_entry *ex)
+{
+    regs->cx = -1u;
+    regs->ip = ex->fixup;
+
+    return true;
+}
+
 /*
  * Local variables:
  * mode: C
index f8bcb81270372355f2ca618abaf306c49933de2e..3a652b48bcbe5d1634f533d587b625777bdf50cc 100644 (file)
@@ -14,6 +14,23 @@ static inline uint64_t rdmsr(uint32_t idx)
     return (((uint64_t)hi) << 32) | lo;
 }
 
+static inline bool rdmsr_safe(uint32_t idx, uint64_t *val)
+{
+    uint32_t lo, hi, new_idx;
+
+    asm volatile("1: rdmsr; 2:"
+                 _ASM_EXTABLE_HANDLER(1b, 2b, ex_rdmsr_safe)
+                 : "=a" (lo), "=d" (hi), "=c" (new_idx)
+                 : "c" (idx));
+
+    bool fault = idx != new_idx;
+
+    if ( !fault )
+        *val = (((uint64_t)hi) << 32) | lo;
+
+    return fault;
+}
+
 static inline void wrmsr(uint32_t idx, uint64_t val)
 {
     asm volatile ("wrmsr":
@@ -21,6 +38,19 @@ static inline void wrmsr(uint32_t idx, uint64_t val)
                     "d" ((uint32_t)(val >> 32)));
 }
 
+static inline bool wrmsr_safe(uint32_t idx, uint64_t val)
+{
+    uint32_t new_idx;
+
+    asm volatile ("1: wrmsr; 2:"
+                  _ASM_EXTABLE_HANDLER(1b, 2b, ex_wrmsr_safe)
+                  : "=c" (new_idx)
+                  : "c" (idx), "a" ((uint32_t)val),
+                    "d" ((uint32_t)(val >> 32)));
+
+    return idx != new_idx;
+}
+
 static inline void cpuid(uint32_t leaf,
                          uint32_t *eax, uint32_t *ebx,
                          uint32_t *ecx, uint32_t *edx)
index 78643ea88fc8dd78244fadad21dc893b8ebf156c..e16f5707ba9982fca3791d999c6077d5636f2d6c 100644 (file)
  */
 #include <xtf.h>
 
-static int rdmsr_safe(uint32_t msr, uint64_t *val)
-{
-    uint32_t lo, hi;
-    int ret;
-
-    asm volatile ("1: rdmsr;"
-                  "jmp 3f; 2:"
-                  "xor %[lo], %[lo];"
-                  "xor %[hi], %[hi];"
-                  "mov %[err], %[res];"
-                  "3:"
-                  _ASM_EXTABLE(1b, 2b)
-                  : [lo] "=a" (lo), [hi] "=d" (hi), [res] "=&q" (ret)
-                  : "c" (msr), [err] "ir" (-EFAULT), "2" (0));
-
-    *val = ((uint64_t)hi) << 32 | lo;
-    return ret;
-}
-
 void test_main(void)
 {
     unsigned int idx = 0;