1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Type definitions for the Microsoft Hypervisor.
4  */
5 #ifndef _HV_HVGDK_H
6 #define _HV_HVGDK_H
7 
8 #include "hvgdk_mini.h"
9 #include "hvgdk_ext.h"
10 
11 /*
12  * The guest OS needs to register the guest ID with the hypervisor.
13  * The guest ID is a 64 bit entity and the structure of this ID is
14  * specified in the Hyper-V TLFS specification.
15  *
16  * While the current guideline does not specify how Linux guest ID(s)
17  * need to be generated, our plan is to publish the guidelines for
18  * Linux and other guest operating systems that currently are hosted
19  * on Hyper-V. The implementation here conforms to this yet
20  * unpublished guidelines.
21  *
22  * Bit(s)
23  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
24  * 62:56 - Os Type; Linux is 0x100
25  * 55:48 - Distro specific identification
26  * 47:16 - Linux kernel version number
27  * 15:0  - Distro specific identification
28  */
29 
30 #define HV_LINUX_VENDOR_ID              0x8100
31 
32 /* HV_VMX_ENLIGHTENED_VMCS */
33 struct hv_enlightened_vmcs {
34 	u32 revision_id;
35 	u32 abort;
36 
37 	u16 host_es_selector;
38 	u16 host_cs_selector;
39 	u16 host_ss_selector;
40 	u16 host_ds_selector;
41 	u16 host_fs_selector;
42 	u16 host_gs_selector;
43 	u16 host_tr_selector;
44 
45 	u16 padding16_1;
46 
47 	u64 host_ia32_pat;
48 	u64 host_ia32_efer;
49 
50 	u64 host_cr0;
51 	u64 host_cr3;
52 	u64 host_cr4;
53 
54 	u64 host_ia32_sysenter_esp;
55 	u64 host_ia32_sysenter_eip;
56 	u64 host_rip;
57 	u32 host_ia32_sysenter_cs;
58 
59 	u32 pin_based_vm_exec_control;
60 	u32 vm_exit_controls;
61 	u32 secondary_vm_exec_control;
62 
63 	u64 io_bitmap_a;
64 	u64 io_bitmap_b;
65 	u64 msr_bitmap;
66 
67 	u16 guest_es_selector;
68 	u16 guest_cs_selector;
69 	u16 guest_ss_selector;
70 	u16 guest_ds_selector;
71 	u16 guest_fs_selector;
72 	u16 guest_gs_selector;
73 	u16 guest_ldtr_selector;
74 	u16 guest_tr_selector;
75 
76 	u32 guest_es_limit;
77 	u32 guest_cs_limit;
78 	u32 guest_ss_limit;
79 	u32 guest_ds_limit;
80 	u32 guest_fs_limit;
81 	u32 guest_gs_limit;
82 	u32 guest_ldtr_limit;
83 	u32 guest_tr_limit;
84 	u32 guest_gdtr_limit;
85 	u32 guest_idtr_limit;
86 
87 	u32 guest_es_ar_bytes;
88 	u32 guest_cs_ar_bytes;
89 	u32 guest_ss_ar_bytes;
90 	u32 guest_ds_ar_bytes;
91 	u32 guest_fs_ar_bytes;
92 	u32 guest_gs_ar_bytes;
93 	u32 guest_ldtr_ar_bytes;
94 	u32 guest_tr_ar_bytes;
95 
96 	u64 guest_es_base;
97 	u64 guest_cs_base;
98 	u64 guest_ss_base;
99 	u64 guest_ds_base;
100 	u64 guest_fs_base;
101 	u64 guest_gs_base;
102 	u64 guest_ldtr_base;
103 	u64 guest_tr_base;
104 	u64 guest_gdtr_base;
105 	u64 guest_idtr_base;
106 
107 	u64 padding64_1[3];
108 
109 	u64 vm_exit_msr_store_addr;
110 	u64 vm_exit_msr_load_addr;
111 	u64 vm_entry_msr_load_addr;
112 
113 	u64 cr3_target_value0;
114 	u64 cr3_target_value1;
115 	u64 cr3_target_value2;
116 	u64 cr3_target_value3;
117 
118 	u32 page_fault_error_code_mask;
119 	u32 page_fault_error_code_match;
120 
121 	u32 cr3_target_count;
122 	u32 vm_exit_msr_store_count;
123 	u32 vm_exit_msr_load_count;
124 	u32 vm_entry_msr_load_count;
125 
126 	u64 tsc_offset;
127 	u64 virtual_apic_page_addr;
128 	u64 vmcs_link_pointer;
129 
130 	u64 guest_ia32_debugctl;
131 	u64 guest_ia32_pat;
132 	u64 guest_ia32_efer;
133 
134 	u64 guest_pdptr0;
135 	u64 guest_pdptr1;
136 	u64 guest_pdptr2;
137 	u64 guest_pdptr3;
138 
139 	u64 guest_pending_dbg_exceptions;
140 	u64 guest_sysenter_esp;
141 	u64 guest_sysenter_eip;
142 
143 	u32 guest_activity_state;
144 	u32 guest_sysenter_cs;
145 
146 	u64 cr0_guest_host_mask;
147 	u64 cr4_guest_host_mask;
148 	u64 cr0_read_shadow;
149 	u64 cr4_read_shadow;
150 	u64 guest_cr0;
151 	u64 guest_cr3;
152 	u64 guest_cr4;
153 	u64 guest_dr7;
154 
155 	u64 host_fs_base;
156 	u64 host_gs_base;
157 	u64 host_tr_base;
158 	u64 host_gdtr_base;
159 	u64 host_idtr_base;
160 	u64 host_rsp;
161 
162 	u64 ept_pointer;
163 
164 	u16 virtual_processor_id;
165 	u16 padding16_2[3];
166 
167 	u64 padding64_2[5];
168 	u64 guest_physical_address;
169 
170 	u32 vm_instruction_error;
171 	u32 vm_exit_reason;
172 	u32 vm_exit_intr_info;
173 	u32 vm_exit_intr_error_code;
174 	u32 idt_vectoring_info_field;
175 	u32 idt_vectoring_error_code;
176 	u32 vm_exit_instruction_len;
177 	u32 vmx_instruction_info;
178 
179 	u64 exit_qualification;
180 	u64 exit_io_instruction_ecx;
181 	u64 exit_io_instruction_esi;
182 	u64 exit_io_instruction_edi;
183 	u64 exit_io_instruction_eip;
184 
185 	u64 guest_linear_address;
186 	u64 guest_rsp;
187 	u64 guest_rflags;
188 
189 	u32 guest_interruptibility_info;
190 	u32 cpu_based_vm_exec_control;
191 	u32 exception_bitmap;
192 	u32 vm_entry_controls;
193 	u32 vm_entry_intr_info_field;
194 	u32 vm_entry_exception_error_code;
195 	u32 vm_entry_instruction_len;
196 	u32 tpr_threshold;
197 
198 	u64 guest_rip;
199 
200 	u32 hv_clean_fields;
201 	u32 padding32_1;
202 	u32 hv_synthetic_controls;
203 	struct {
204 		u32 nested_flush_hypercall:1;
205 		u32 msr_bitmap:1;
206 		u32 reserved:30;
207 	}  __packed hv_enlightenments_control;
208 	u32 hv_vp_id;
209 	u32 padding32_2;
210 	u64 hv_vm_id;
211 	u64 partition_assist_page;
212 	u64 padding64_4[4];
213 	u64 guest_bndcfgs;
214 	u64 guest_ia32_perf_global_ctrl;
215 	u64 guest_ia32_s_cet;
216 	u64 guest_ssp;
217 	u64 guest_ia32_int_ssp_table_addr;
218 	u64 guest_ia32_lbr_ctl;
219 	u64 padding64_5[2];
220 	u64 xss_exit_bitmap;
221 	u64 encls_exiting_bitmap;
222 	u64 host_ia32_perf_global_ctrl;
223 	u64 tsc_multiplier;
224 	u64 host_ia32_s_cet;
225 	u64 host_ssp;
226 	u64 host_ia32_int_ssp_table_addr;
227 	u64 padding64_6;
228 } __packed;
229 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE			0
230 
231 
232 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP		BIT(0)
233 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP		BIT(1)
234 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2		BIT(2)
235 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1		BIT(3)
236 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC		BIT(4)
237 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT		BIT(5)
238 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY		BIT(6)
239 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN		BIT(7)
240 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR			BIT(8)
241 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT		BIT(9)
242 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC		BIT(10)
243 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1		BIT(11)
244 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2		BIT(12)
245 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER		BIT(13)
246 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1		BIT(14)
247 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_ENLIGHTENMENTSCONTROL	BIT(15)
248 
249 #define HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL			0xFFFF
250 
251 /*
252  * Note, Hyper-V isn't actually stealing bit 28 from Intel, just abusing it by
253  * pairing it with architecturally impossible exit reasons.  Bit 28 is set only
254  * on SMI exits to a SMI transfer monitor (STM) and if and only if a MTF VM-Exit
255  * is pending.  I.e. it will never be set by hardware for non-SMI exits (there
256  * are only three), nor will it ever be set unless the VMM is an STM.
257  */
258 #define HV_VMX_SYNTHETIC_EXIT_REASON_TRAP_AFTER_FLUSH		0x10000031
259 
260 /*
261  * Hyper-V uses the software reserved 32 bytes in VMCB control area to expose
262  * SVM enlightenments to guests. This is documented in the TLFS doc.
263  * Note on naming: SVM_NESTED_ENLIGHTENED_VMCB_FIELDS
264  */
265 struct hv_vmcb_enlightenments {
266 	struct __packed hv_enlightenments_control {
267 		u32 nested_flush_hypercall : 1;
268 		u32 msr_bitmap : 1;
269 		u32 enlightened_npt_tlb: 1;
270 		u32 reserved : 29;
271 	} __packed hv_enlightenments_control;
272 	u32 hv_vp_id;
273 	u64 hv_vm_id;
274 	u64 partition_assist_page;
275 	u64 reserved;
276 } __packed;
277 
278 /*
279  * Hyper-V uses the software reserved clean bit in VMCB.
280  */
281 #define HV_VMCB_NESTED_ENLIGHTENMENTS		31
282 
283 /* Synthetic VM-Exit */
284 #define HV_SVM_EXITCODE_ENL			0xf0000000
285 #define HV_SVM_ENL_EXITCODE_TRAP_AFTER_FLUSH	(1)
286 
287 /* VM_PARTITION_ASSIST_PAGE */
288 struct hv_partition_assist_pg {
289 	u32 tlb_lock_count;
290 };
291 
292 /* Define connection identifier type. */
293 union hv_connection_id {
294 	u32 asu32;
295 	struct {
296 		u32 id : 24;
297 		u32 reserved : 8;
298 	} __packed u;
299 };
300 
301 struct hv_input_unmap_gpa_pages {
302 	u64 target_partition_id;
303 	u64 target_gpa_base;
304 	u32 unmap_flags;
305 	u32 padding;
306 } __packed;
307 
308 #endif /* #ifndef _HV_HVGDK_H */
309