Xen Test Framework
msr.h
Go to the documentation of this file.
1 
6 #ifndef XTF_X86_MSR_H
7 #define XTF_X86_MSR_H
8 
9 #include <xtf/extable.h>
10 #include <xtf/types.h>
11 
12 #include <xen/arch-x86/xen.h>
13 
14 #include <arch/msr-index.h>
15 
19 static inline uint64_t rdmsr(uint32_t idx)
20 {
21  uint32_t lo, hi;
22 
23  asm volatile ("rdmsr": "=a" (lo), "=d" (hi): "c" (idx));
24 
25  return (((uint64_t)hi) << 32) | lo;
26 }
27 
35 static inline bool rdmsr_safe(uint32_t idx, uint64_t *val)
36 {
37  uint32_t lo, hi, new_idx;
38 
39  asm volatile ("1: rdmsr; 2:"
40  _ASM_EXTABLE_HANDLER(1b, 2b, %P[hnd])
41  : "=a" (lo), "=d" (hi), "=c" (new_idx)
42  : "c" (idx), [hnd] "p" (ex_rdmsr_safe));
43 
44  bool fault = idx != new_idx;
45 
46  if ( !fault )
47  *val = (((uint64_t)hi) << 32) | lo;
48 
49  return fault;
50 }
51 
55 static inline void wrmsr(uint32_t idx, uint64_t val)
56 {
57  asm volatile ("wrmsr":
58  : "c" (idx), "a" ((uint32_t)val),
59  "d" ((uint32_t)(val >> 32)));
60 }
61 
69 static inline bool wrmsr_safe(uint32_t idx, uint64_t val)
70 {
71  uint32_t new_idx;
72 
73  asm volatile ("1: wrmsr; 2:"
74  _ASM_EXTABLE_HANDLER(1b, 2b, %P[hnd])
75  : "=c" (new_idx)
76  : "c" (idx), "a" ((uint32_t)val),
77  "d" ((uint32_t)(val >> 32)),
78  [hnd] "p" (ex_wrmsr_safe));
79 
80  return idx != new_idx;
81 }
82 
83 /*
84  * Types wrapping MSR content.
85  */
86 typedef union msr_feature_control {
88  struct {
89  bool lock:1,
92  };
94 
95 typedef union msr_vmx_basic {
97  struct {
99  bool mbz:1;
102  bool paddr_32bit:1;
103  bool smm_dual:1;
106  bool true_ctls:1;
107  };
109 
110 /*
111  * Library logic for MSRs.
112  */
114 {
116 
118  {
120  bool pred;
121  } *vals;
122 
123  size_t nr_vals;
124 };
125 
143 
144 #endif /* XTF_X86_MSR_H */
145 
146 /*
147  * Local variables:
148  * mode: C
149  * c-file-style: "BSD"
150  * c-basic-offset: 4
151  * tab-width: 4
152  * indent-tabs-mode: nil
153  * End:
154  */
static void wrmsr(uint32_t idx, uint64_t val)
Thin wrapper around an wrmsr instruction.
Definition: msr.h:55
bool ex_wrmsr_safe(struct cpu_regs *regs, const struct extable_entry *ex)
Fixup from a wrmsr fault.
Definition: extable.c:33
Common declarations for all tests.
static uint64_t rdmsr(uint32_t idx)
Thin wrapper around an rdmsr instruction.
Definition: msr.h:19
uint64_t raw
Definition: msr.h:87
Exception table support.
bool mbz
Definition: msr.h:99
void xtf_msr_consistency_test(const struct xtf_msr_consistency_test_data *t)
Run consistency tests as described by t.
Definition: msr.c:29
union msr_vmx_basic msr_vmx_basic_t
union msr_feature_control msr_feature_control_t
bool ex_rdmsr_safe(struct cpu_regs *regs, const struct extable_entry *ex)
Fixup from a rdmsr fault.
Definition: extable.c:24
uint32_t vmcs_mem_type
Definition: msr.h:104
bool smm_dual
Definition: msr.h:103
__UINT64_TYPE__ uint64_t
Definition: stdint.h:17
__UINT32_TYPE__ uint32_t
Definition: stdint.h:16
bool paddr_32bit
Definition: msr.h:102
bool vmxon_outside_smx
Definition: msr.h:89
uint64_t raw
Definition: msr.h:96
bool inouts_exit_info
Definition: msr.h:105
bool true_ctls
Definition: msr.h:106
uint32_t vmcs_size
Definition: msr.h:100
static bool rdmsr_safe(uint32_t idx, uint64_t *val)
Wrapper around rdmsr which safely catches #GP[0].
Definition: msr.h:35
static bool wrmsr_safe(uint32_t idx, uint64_t val)
Wrapper around wrmsr which safely catches #GP[0].
Definition: msr.h:69
bool vmxon_inside_smx
Definition: msr.h:89
#define _ASM_EXTABLE_HANDLER(fault, fixup, handler)
Create an exception table entry with custom handler.
Definition: extable.h:38
uint32_t vmcs_rev_id
Definition: msr.h:98