1// Copyright 2017 The Fuchsia Authors
2//
3// Use of this source code is governed by a MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT
6
7#pragma once
8
9#include <arch/hypervisor.h>
10
11// clang-format off
12
13#define X86_MSR_IA32_FEATURE_CONTROL        0x003a // Feature control
14#define X86_MSR_IA32_VMX_BASIC              0x0480 // Basic info
15#define X86_MSR_IA32_VMX_CR0_FIXED0         0x0486 // CR0 bits that must be 0 to enter VMX
16#define X86_MSR_IA32_VMX_CR0_FIXED1         0x0487 // CR0 bits that must be 1 to enter VMX
17#define X86_MSR_IA32_VMX_CR4_FIXED0         0x0488 // CR4 bits that must be 0 to enter VMX
18#define X86_MSR_IA32_VMX_CR4_FIXED1         0x0489 // CR4 bits that must be 1 to enter VMX
19#define X86_MSR_IA32_VMX_EPT_VPID_CAP       0x048c // VPID and EPT Capabilities
20#define X86_MSR_IA32_VMX_MISC               0x0485 // Miscellaneous info
21
22/* X86_MSR_IA32_VMX_BASIC flags */
23#define VMX_MEMORY_TYPE_WRITE_BACK          0x06 // Write back
24
25/* X86_MSR_IA32_FEATURE_CONTROL flags */
26#define X86_MSR_IA32_FEATURE_CONTROL_LOCK   (1u << 0) // Locked
27#define X86_MSR_IA32_FEATURE_CONTROL_VMXON  (1u << 2) // Enable VMXON
28
29#define VMX_ERR_CHECK(var)                  "setna %[" #var "];" // Check CF and ZF for error.
30
31// clang-format on
32
33/* Stores VMX info from the IA32_VMX_BASIC MSR. */
34struct VmxInfo {
35    uint32_t revision_id;
36    uint16_t region_size;
37    bool write_back;
38    bool io_exit_info;
39    bool vmx_controls;
40
41    VmxInfo();
42};
43
44/* Stores EPT info from the IA32_VMX_EPT_VPID_CAP MSR. */
45struct EptInfo {
46    bool page_walk_4;
47    bool write_back;
48    bool invept;
49
50    EptInfo();
51};
52
53/* VMX region to be used with both VMXON and VMCS. */
54struct VmxRegion {
55    uint32_t revision_id;
56};
57
58zx_status_t alloc_vmx_state();
59zx_status_t free_vmx_state();
60bool cr_is_invalid(uint64_t cr_value, uint32_t fixed0_msr, uint32_t fixed1_msr);
61