Sean Christopherson | cb1d474 | 2018-12-03 13:53:04 -0800 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef __KVM_X86_VMX_VMCS_H |
| 3 | #define __KVM_X86_VMX_VMCS_H |
| 4 | |
| 5 | #include <linux/list.h> |
| 6 | #include <linux/ktime.h> |
| 7 | |
| 8 | #include <asm/vmx.h> |
| 9 | |
| 10 | #include "capabilities.h" |
| 11 | |
| 12 | struct vmcs_hdr { |
| 13 | u32 revision_id:31; |
| 14 | u32 shadow_vmcs:1; |
| 15 | }; |
| 16 | |
| 17 | struct vmcs { |
| 18 | struct vmcs_hdr hdr; |
| 19 | u32 abort; |
| 20 | char data[0]; |
| 21 | }; |
| 22 | |
| 23 | /* |
| 24 | * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT |
| 25 | * and whose values change infrequently, but are not constant. I.e. this is |
| 26 | * used as a write-through cache of the corresponding VMCS fields. |
| 27 | */ |
| 28 | struct vmcs_host_state { |
| 29 | unsigned long cr3; /* May not match real cr3 */ |
| 30 | unsigned long cr4; /* May not match real cr4 */ |
| 31 | unsigned long gs_base; |
| 32 | unsigned long fs_base; |
| 33 | |
| 34 | u16 fs_sel, gs_sel, ldt_sel; |
| 35 | #ifdef CONFIG_X86_64 |
| 36 | u16 ds_sel, es_sel; |
| 37 | #endif |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 42 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 43 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 44 | */ |
| 45 | struct loaded_vmcs { |
| 46 | struct vmcs *vmcs; |
| 47 | struct vmcs *shadow_vmcs; |
| 48 | int cpu; |
| 49 | bool launched; |
| 50 | bool nmi_known_unmasked; |
| 51 | bool hv_timer_armed; |
| 52 | /* Support for vnmi-less CPUs */ |
| 53 | int soft_vnmi_blocked; |
| 54 | ktime_t entry_time; |
| 55 | s64 vnmi_blocked_time; |
| 56 | unsigned long *msr_bitmap; |
| 57 | struct list_head loaded_vmcss_on_cpu_link; |
| 58 | struct vmcs_host_state host_state; |
| 59 | }; |
| 60 | |
| 61 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
| 62 | { |
| 63 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 64 | INTR_INFO_VALID_MASK)) == |
| 65 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 66 | } |
| 67 | |
| 68 | static inline bool is_debug(u32 intr_info) |
| 69 | { |
| 70 | return is_exception_n(intr_info, DB_VECTOR); |
| 71 | } |
| 72 | |
| 73 | static inline bool is_breakpoint(u32 intr_info) |
| 74 | { |
| 75 | return is_exception_n(intr_info, BP_VECTOR); |
| 76 | } |
| 77 | |
| 78 | static inline bool is_page_fault(u32 intr_info) |
| 79 | { |
| 80 | return is_exception_n(intr_info, PF_VECTOR); |
| 81 | } |
| 82 | |
| 83 | static inline bool is_invalid_opcode(u32 intr_info) |
| 84 | { |
| 85 | return is_exception_n(intr_info, UD_VECTOR); |
| 86 | } |
| 87 | |
| 88 | static inline bool is_gp_fault(u32 intr_info) |
| 89 | { |
| 90 | return is_exception_n(intr_info, GP_VECTOR); |
| 91 | } |
| 92 | |
| 93 | static inline bool is_machine_check(u32 intr_info) |
| 94 | { |
| 95 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 96 | INTR_INFO_VALID_MASK)) == |
| 97 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 98 | } |
| 99 | |
| 100 | /* Undocumented: icebp/int1 */ |
| 101 | static inline bool is_icebp(u32 intr_info) |
| 102 | { |
| 103 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 104 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); |
| 105 | } |
| 106 | |
| 107 | static inline bool is_nmi(u32 intr_info) |
| 108 | { |
| 109 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 110 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
| 111 | } |
| 112 | |
| 113 | enum vmcs_field_width { |
| 114 | VMCS_FIELD_WIDTH_U16 = 0, |
| 115 | VMCS_FIELD_WIDTH_U64 = 1, |
| 116 | VMCS_FIELD_WIDTH_U32 = 2, |
| 117 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 |
| 118 | }; |
| 119 | |
| 120 | static inline int vmcs_field_width(unsigned long field) |
| 121 | { |
| 122 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 123 | return VMCS_FIELD_WIDTH_U32; |
| 124 | return (field >> 13) & 0x3; |
| 125 | } |
| 126 | |
| 127 | static inline int vmcs_field_readonly(unsigned long field) |
| 128 | { |
| 129 | return (((field >> 10) & 0x3) == 1); |
| 130 | } |
| 131 | |
| 132 | #endif /* __KVM_X86_VMX_VMCS_H */ |