xref: /openbmc/qemu/scripts/kvm/vmxcap (revision 2e641870170e28df28c5d9914e76ea7cab141516)
1#!/usr/bin/env python3
2#
3# tool for querying VMX capabilities
4#
5# Copyright 2009-2010 Red Hat, Inc.
6#
7# Authors:
8#  Avi Kivity <avi@redhat.com>
9#
10# This work is licensed under the terms of the GNU GPL, version 2.  See
11# the COPYING file in the top-level directory.
12
13MSR_IA32_VMX_BASIC = 0x480
14MSR_IA32_VMX_PINBASED_CTLS = 0x481
15MSR_IA32_VMX_PROCBASED_CTLS = 0x482
16MSR_IA32_VMX_EXIT_CTLS = 0x483
17MSR_IA32_VMX_ENTRY_CTLS = 0x484
18MSR_IA32_VMX_MISC_CTLS = 0x485
19MSR_IA32_VMX_PROCBASED_CTLS2 = 0x48B
20MSR_IA32_VMX_EPT_VPID_CAP = 0x48C
21MSR_IA32_VMX_TRUE_PINBASED_CTLS = 0x48D
22MSR_IA32_VMX_TRUE_PROCBASED_CTLS = 0x48E
23MSR_IA32_VMX_TRUE_EXIT_CTLS = 0x48F
24MSR_IA32_VMX_TRUE_ENTRY_CTLS = 0x490
25MSR_IA32_VMX_VMFUNC = 0x491
26MSR_IA32_VMX_PROCBASED_CTLS3 = 0x492
27MSR_IA32_VMX_EXIT_CTLS2 = 0x493
28
29class msr(object):
30    def __init__(self):
31        try:
32            self.f = open('/dev/cpu/0/msr', 'rb', 0)
33        except:
34            self.f = open('/dev/msr0', 'rb', 0)
35    def read(self, index, default = None):
36        import struct
37        self.f.seek(index)
38        try:
39            return struct.unpack('Q', self.f.read(8))[0]
40        except:
41            return default
42
43class Control(object):
44    def __init__(self, name, bits, cap_msr, true_cap_msr = None):
45        self.name = name
46        self.bits = bits
47        self.cap_msr = cap_msr
48        self.true_cap_msr = true_cap_msr
49    def read2(self, nr):
50        m = msr()
51        val = m.read(nr, 0)
52        return (val & 0xffffffff, val >> 32)
53    def show(self):
54        print(self.name)
55        mb1, cb1 = self.read2(self.cap_msr)
56        tmb1, tcb1 = 0, 0
57        if self.true_cap_msr:
58            tmb1, tcb1 = self.read2(self.true_cap_msr)
59        for bit in sorted(self.bits.keys()):
60            zero = not (mb1 & (1 << bit))
61            one = cb1 & (1 << bit)
62            true_zero = not (tmb1 & (1 << bit))
63            true_one = tcb1 & (1 << bit)
64            s= '?'
65            if (self.true_cap_msr and true_zero and true_one
66                and one and not zero):
67                s = 'default'
68            elif zero and not one:
69                s = 'no'
70            elif one and not zero:
71                s = 'forced'
72            elif one and zero:
73                s = 'yes'
74            print('  %-40s %s' % (self.bits[bit], s))
75
76# All 64 bits in the tertiary controls MSR are allowed-1
77class Allowed1Control(Control):
78    def read2(self, nr):
79        m = msr()
80        val = m.read(nr, 0)
81        return (0, val)
82
83class Misc(object):
84    def __init__(self, name, bits, msr):
85        self.name = name
86        self.bits = bits
87        self.msr = msr
88    def show(self):
89        print(self.name)
90        value = msr().read(self.msr, 0)
91        print('  Hex: 0x%x' % (value))
92        def first_bit(key):
93            if type(key) is tuple:
94                return key[0]
95            else:
96                return key
97        for bits in sorted(self.bits.keys(), key = first_bit):
98            if type(bits) is tuple:
99                lo, hi = bits
100                fmt = int
101            else:
102                lo = hi = bits
103                def fmt(x):
104                    return { True: 'yes', False: 'no' }[x]
105            v = (value >> lo) & ((1 << (hi - lo + 1)) - 1)
106            print('  %-40s %s' % (self.bits[bits], fmt(v)))
107
108controls = [
109    Misc(
110        name = 'Basic VMX Information',
111        bits = {
112            (0, 30): 'Revision',
113            (32,44): 'VMCS size',
114            48: 'VMCS restricted to 32 bit addresses',
115            49: 'Dual-monitor support',
116            (50, 53): 'VMCS memory type',
117            54: 'INS/OUTS instruction information',
118            55: 'IA32_VMX_TRUE_*_CTLS support',
119            56: 'Skip checks on event error code',
120            },
121        msr = MSR_IA32_VMX_BASIC,
122        ),
123    Control(
124        name = 'pin-based controls',
125        bits = {
126            0: 'External interrupt exiting',
127            3: 'NMI exiting',
128            5: 'Virtual NMIs',
129            6: 'Activate VMX-preemption timer',
130            7: 'Process posted interrupts',
131            },
132        cap_msr = MSR_IA32_VMX_PINBASED_CTLS,
133        true_cap_msr = MSR_IA32_VMX_TRUE_PINBASED_CTLS,
134        ),
135
136    Control(
137        name = 'primary processor-based controls',
138        bits = {
139            2: 'Interrupt window exiting',
140            3: 'Use TSC offsetting',
141            7: 'HLT exiting',
142            9: 'INVLPG exiting',
143            10: 'MWAIT exiting',
144            11: 'RDPMC exiting',
145            12: 'RDTSC exiting',
146            15: 'CR3-load exiting',
147            16: 'CR3-store exiting',
148            17: 'Activate tertiary controls',
149            19: 'CR8-load exiting',
150            20: 'CR8-store exiting',
151            21: 'Use TPR shadow',
152            22: 'NMI-window exiting',
153            23: 'MOV-DR exiting',
154            24: 'Unconditional I/O exiting',
155            25: 'Use I/O bitmaps',
156            27: 'Monitor trap flag',
157            28: 'Use MSR bitmaps',
158            29: 'MONITOR exiting',
159            30: 'PAUSE exiting',
160            31: 'Activate secondary control',
161            },
162        cap_msr = MSR_IA32_VMX_PROCBASED_CTLS,
163        true_cap_msr = MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
164        ),
165
166    Control(
167        name = 'secondary processor-based controls',
168        bits = {
169            0: 'Virtualize APIC accesses',
170            1: 'Enable EPT',
171            2: 'Descriptor-table exiting',
172            3: 'Enable RDTSCP',
173            4: 'Virtualize x2APIC mode',
174            5: 'Enable VPID',
175            6: 'WBINVD exiting',
176            7: 'Unrestricted guest',
177            8: 'APIC register emulation',
178            9: 'Virtual interrupt delivery',
179            10: 'PAUSE-loop exiting',
180            11: 'RDRAND exiting',
181            12: 'Enable INVPCID',
182            13: 'Enable VM functions',
183            14: 'VMCS shadowing',
184            15: 'Enable ENCLS exiting',
185            16: 'RDSEED exiting',
186            17: 'Enable PML',
187            18: 'EPT-violation #VE',
188            19: 'Conceal non-root operation from PT',
189            20: 'Enable XSAVES/XRSTORS',
190            22: 'Mode-based execute control (XS/XU)',
191            23: 'Sub-page write permissions',
192            24: 'GPA translation for PT',
193            25: 'TSC scaling',
194            26: 'User wait and pause',
195            28: 'ENCLV exiting',
196            },
197        cap_msr = MSR_IA32_VMX_PROCBASED_CTLS2,
198        ),
199
200    Allowed1Control(
201        name = 'tertiary processor-based controls',
202        bits = {
203            4: 'Enable IPI virtualization'
204            },
205        cap_msr = MSR_IA32_VMX_PROCBASED_CTLS3,
206        ),
207
208    Control(
209        name = 'VM-Exit controls',
210        bits = {
211            2: 'Save debug controls',
212            9: 'Host address-space size',
213            12: 'Load IA32_PERF_GLOBAL_CTRL',
214            15: 'Acknowledge interrupt on exit',
215            18: 'Save IA32_PAT',
216            19: 'Load IA32_PAT',
217            20: 'Save IA32_EFER',
218            21: 'Load IA32_EFER',
219            22: 'Save VMX-preemption timer value',
220            23: 'Clear IA32_BNDCFGS',
221            24: 'Conceal VM exits from PT',
222            25: 'Clear IA32_RTIT_CTL',
223            31: 'Activate secondary VM-exit controls',
224            },
225        cap_msr = MSR_IA32_VMX_EXIT_CTLS,
226        true_cap_msr = MSR_IA32_VMX_TRUE_EXIT_CTLS,
227        ),
228
229    Allowed1Control(
230        name = 'secondary VM-Exit controls',
231        bits = {
232            0: 'Save IA32 FRED MSRs',
233            1: 'Load IA32 FRED MSRs',
234            },
235        cap_msr = MSR_IA32_VMX_EXIT_CTLS2,
236        ),
237
238    Control(
239        name = 'VM-Entry controls',
240        bits = {
241            2: 'Load debug controls',
242            9: 'IA-32e mode guest',
243            10: 'Entry to SMM',
244            11: 'Deactivate dual-monitor treatment',
245            13: 'Load IA32_PERF_GLOBAL_CTRL',
246            14: 'Load IA32_PAT',
247            15: 'Load IA32_EFER',
248            16: 'Load IA32_BNDCFGS',
249            17: 'Conceal VM entries from PT',
250            18: 'Load IA32_RTIT_CTL',
251            23: 'Load IA32 FRED MSRs',
252            },
253        cap_msr = MSR_IA32_VMX_ENTRY_CTLS,
254        true_cap_msr = MSR_IA32_VMX_TRUE_ENTRY_CTLS,
255        ),
256
257    Misc(
258        name = 'Miscellaneous data',
259        bits = {
260            (0,4): 'VMX-preemption timer scale (log2)',
261            5: 'Store EFER.LMA into IA-32e mode guest control',
262            6: 'HLT activity state',
263            7: 'Shutdown activity state',
264            8: 'Wait-for-SIPI activity state',
265            14: 'PT in VMX operation',
266            15: 'IA32_SMBASE support',
267            (16,24): 'Number of CR3-target values',
268            (25,27): 'MSR-load/store count recommendation',
269            28: 'IA32_SMM_MONITOR_CTL[2] can be set to 1',
270            29: 'VMWRITE to VM-exit information fields',
271            30: 'Inject event with insn length=0',
272            (32,63): 'MSEG revision identifier',
273            },
274        msr = MSR_IA32_VMX_MISC_CTLS,
275        ),
276
277    Misc(
278        name = 'VPID and EPT capabilities',
279        bits = {
280            0: 'Execute-only EPT translations',
281            6: 'Page-walk length 4',
282            7: 'Page-walk length 5',
283            8: 'Paging-structure memory type UC',
284            14: 'Paging-structure memory type WB',
285            16: '2MB EPT pages',
286            17: '1GB EPT pages',
287            20: 'INVEPT supported',
288            21: 'EPT accessed and dirty flags',
289            22: 'Advanced VM-exit information for EPT violations',
290            25: 'Single-context INVEPT',
291            26: 'All-context INVEPT',
292            32: 'INVVPID supported',
293            40: 'Individual-address INVVPID',
294            41: 'Single-context INVVPID',
295            42: 'All-context INVVPID',
296            43: 'Single-context-retaining-globals INVVPID',
297            },
298        msr = MSR_IA32_VMX_EPT_VPID_CAP,
299        ),
300    Misc(
301        name = 'VM Functions',
302        bits = {
303            0: 'EPTP Switching',
304            },
305        msr = MSR_IA32_VMX_VMFUNC,
306        ),
307    ]
308
309if __name__ == '__main__':
310    for c in controls:
311        c.show()
312