1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /*
4  * This file contains definitions from Hyper-V Hypervisor Top-Level Functional
5  * Specification (TLFS):
6  * https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
7  */
8 
9 #ifndef _ASM_GENERIC_HYPERV_TLFS_H
10 #define _ASM_GENERIC_HYPERV_TLFS_H
11 
12 #include <linux/types.h>
13 #include <linux/bits.h>
14 #include <linux/time64.h>
15 
16 /*
17  * While not explicitly listed in the TLFS, Hyper-V always runs with a page size
18  * of 4096. These definitions are used when communicating with Hyper-V using
19  * guest physical pages and guest physical page addresses, since the guest page
20  * size may not be 4096 on all architectures.
21  */
22 #define HV_HYP_PAGE_SHIFT      12
23 #define HV_HYP_PAGE_SIZE       BIT(HV_HYP_PAGE_SHIFT)
24 #define HV_HYP_PAGE_MASK       (~(HV_HYP_PAGE_SIZE - 1))
25 
26 /*
27  * Hyper-V provides two categories of flags relevant to guest VMs.  The
28  * "Features" category indicates specific functionality that is available
29  * to guests on this particular instance of Hyper-V. The "Features"
30  * are presented in four groups, each of which is 32 bits. The group A
31  * and B definitions are common across architectures and are listed here.
32  * However, not all flags are relevant on all architectures.
33  *
34  * Groups C and D vary across architectures and are listed in the
35  * architecture specific portion of hyperv-tlfs.h. Some of these flags exist
36  * on multiple architectures, but the bit positions are different so they
37  * cannot appear in the generic portion of hyperv-tlfs.h.
38  *
39  * The "Enlightenments" category provides recommendations on whether to use
40  * specific enlightenments that are available. The Enlighenments are a single
41  * group of 32 bits, but they vary across architectures and are listed in
42  * the architecture specific portion of hyperv-tlfs.h.
43  */
44 
45 /*
46  * Group A Features.
47  */
48 
49 /* VP Runtime register available */
50 #define HV_MSR_VP_RUNTIME_AVAILABLE		BIT(0)
51 /* Partition Reference Counter available*/
52 #define HV_MSR_TIME_REF_COUNT_AVAILABLE		BIT(1)
53 /* Basic SynIC register available */
54 #define HV_MSR_SYNIC_AVAILABLE			BIT(2)
55 /* Synthetic Timer registers available */
56 #define HV_MSR_SYNTIMER_AVAILABLE		BIT(3)
57 /* Virtual APIC assist and VP assist page registers available */
58 #define HV_MSR_APIC_ACCESS_AVAILABLE		BIT(4)
59 /* Hypercall and Guest OS ID registers available*/
60 #define HV_MSR_HYPERCALL_AVAILABLE		BIT(5)
61 /* Access virtual processor index register available*/
62 #define HV_MSR_VP_INDEX_AVAILABLE		BIT(6)
63 /* Virtual system reset register available*/
64 #define HV_MSR_RESET_AVAILABLE			BIT(7)
65 /* Access statistics page registers available */
66 #define HV_MSR_STAT_PAGES_AVAILABLE		BIT(8)
67 /* Partition reference TSC register is available */
68 #define HV_MSR_REFERENCE_TSC_AVAILABLE		BIT(9)
69 /* Partition Guest IDLE register is available */
70 #define HV_MSR_GUEST_IDLE_AVAILABLE		BIT(10)
71 /* Partition local APIC and TSC frequency registers available */
72 #define HV_ACCESS_FREQUENCY_MSRS		BIT(11)
73 /* AccessReenlightenmentControls privilege */
74 #define HV_ACCESS_REENLIGHTENMENT		BIT(13)
75 /* AccessTscInvariantControls privilege */
76 #define HV_ACCESS_TSC_INVARIANT			BIT(15)
77 
78 /*
79  * Group B features.
80  */
81 #define HV_CREATE_PARTITIONS			BIT(0)
82 #define HV_ACCESS_PARTITION_ID			BIT(1)
83 #define HV_ACCESS_MEMORY_POOL			BIT(2)
84 #define HV_ADJUST_MESSAGE_BUFFERS		BIT(3)
85 #define HV_POST_MESSAGES			BIT(4)
86 #define HV_SIGNAL_EVENTS			BIT(5)
87 #define HV_CREATE_PORT				BIT(6)
88 #define HV_CONNECT_PORT				BIT(7)
89 #define HV_ACCESS_STATS				BIT(8)
90 #define HV_DEBUGGING				BIT(11)
91 #define HV_CPU_MANAGEMENT			BIT(12)
92 #define HV_ISOLATION				BIT(22)
93 
94 
95 /*
96  * TSC page layout.
97  */
98 struct ms_hyperv_tsc_page {
99 	volatile u32 tsc_sequence;
100 	u32 reserved1;
101 	volatile u64 tsc_scale;
102 	volatile s64 tsc_offset;
103 } __packed;
104 
105 /*
106  * The guest OS needs to register the guest ID with the hypervisor.
107  * The guest ID is a 64 bit entity and the structure of this ID is
108  * specified in the Hyper-V specification:
109  *
110  * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
111  *
112  * While the current guideline does not specify how Linux guest ID(s)
113  * need to be generated, our plan is to publish the guidelines for
114  * Linux and other guest operating systems that currently are hosted
115  * on Hyper-V. The implementation here conforms to this yet
116  * unpublished guidelines.
117  *
118  *
119  * Bit(s)
120  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
121  * 62:56 - Os Type; Linux is 0x100
122  * 55:48 - Distro specific identification
123  * 47:16 - Linux kernel version number
124  * 15:0  - Distro specific identification
125  *
126  *
127  */
128 
129 #define HV_LINUX_VENDOR_ID              0x8100
130 
131 /*
132  * Crash notification flags.
133  */
134 #define HV_CRASH_CTL_CRASH_NOTIFY_MSG		BIT_ULL(62)
135 #define HV_CRASH_CTL_CRASH_NOTIFY		BIT_ULL(63)
136 
137 /* Declare the various hypercall operations. */
138 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE	0x0002
139 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST	0x0003
140 #define HVCALL_NOTIFY_LONG_SPIN_WAIT		0x0008
141 #define HVCALL_SEND_IPI				0x000b
142 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX	0x0013
143 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX	0x0014
144 #define HVCALL_SEND_IPI_EX			0x0015
145 #define HVCALL_GET_PARTITION_ID			0x0046
146 #define HVCALL_DEPOSIT_MEMORY			0x0048
147 #define HVCALL_CREATE_VP			0x004e
148 #define HVCALL_GET_VP_REGISTERS			0x0050
149 #define HVCALL_SET_VP_REGISTERS			0x0051
150 #define HVCALL_POST_MESSAGE			0x005c
151 #define HVCALL_SIGNAL_EVENT			0x005d
152 #define HVCALL_POST_DEBUG_DATA			0x0069
153 #define HVCALL_RETRIEVE_DEBUG_DATA		0x006a
154 #define HVCALL_RESET_DEBUG_SESSION		0x006b
155 #define HVCALL_ADD_LOGICAL_PROCESSOR		0x0076
156 #define HVCALL_RETARGET_INTERRUPT		0x007e
157 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
158 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0
159 
160 #define HV_FLUSH_ALL_PROCESSORS			BIT(0)
161 #define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES	BIT(1)
162 #define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY	BIT(2)
163 #define HV_FLUSH_USE_EXTENDED_RANGE_FORMAT	BIT(3)
164 
165 enum HV_GENERIC_SET_FORMAT {
166 	HV_GENERIC_SET_SPARSE_4K,
167 	HV_GENERIC_SET_ALL,
168 };
169 
170 #define HV_PARTITION_ID_SELF		((u64)-1)
171 #define HV_VP_INDEX_SELF		((u32)-2)
172 
173 #define HV_HYPERCALL_RESULT_MASK	GENMASK_ULL(15, 0)
174 #define HV_HYPERCALL_FAST_BIT		BIT(16)
175 #define HV_HYPERCALL_VARHEAD_OFFSET	17
176 #define HV_HYPERCALL_REP_COMP_OFFSET	32
177 #define HV_HYPERCALL_REP_COMP_1		BIT_ULL(32)
178 #define HV_HYPERCALL_REP_COMP_MASK	GENMASK_ULL(43, 32)
179 #define HV_HYPERCALL_REP_START_OFFSET	48
180 #define HV_HYPERCALL_REP_START_MASK	GENMASK_ULL(59, 48)
181 
182 /* hypercall status code */
183 #define HV_STATUS_SUCCESS			0
184 #define HV_STATUS_INVALID_HYPERCALL_CODE	2
185 #define HV_STATUS_INVALID_HYPERCALL_INPUT	3
186 #define HV_STATUS_INVALID_ALIGNMENT		4
187 #define HV_STATUS_INVALID_PARAMETER		5
188 #define HV_STATUS_OPERATION_DENIED		8
189 #define HV_STATUS_INSUFFICIENT_MEMORY		11
190 #define HV_STATUS_INVALID_PORT_ID		17
191 #define HV_STATUS_INVALID_CONNECTION_ID		18
192 #define HV_STATUS_INSUFFICIENT_BUFFERS		19
193 
194 /*
195  * The Hyper-V TimeRefCount register and the TSC
196  * page provide a guest VM clock with 100ns tick rate
197  */
198 #define HV_CLOCK_HZ (NSEC_PER_SEC/100)
199 
200 /* Define the number of synthetic interrupt sources. */
201 #define HV_SYNIC_SINT_COUNT		(16)
202 /* Define the expected SynIC version. */
203 #define HV_SYNIC_VERSION_1		(0x1)
204 /* Valid SynIC vectors are 16-255. */
205 #define HV_SYNIC_FIRST_VALID_VECTOR	(16)
206 
207 #define HV_SYNIC_CONTROL_ENABLE		(1ULL << 0)
208 #define HV_SYNIC_SIMP_ENABLE		(1ULL << 0)
209 #define HV_SYNIC_SIEFP_ENABLE		(1ULL << 0)
210 #define HV_SYNIC_SINT_MASKED		(1ULL << 16)
211 #define HV_SYNIC_SINT_AUTO_EOI		(1ULL << 17)
212 #define HV_SYNIC_SINT_VECTOR_MASK	(0xFF)
213 
214 #define HV_SYNIC_STIMER_COUNT		(4)
215 
216 /* Define synthetic interrupt controller message constants. */
217 #define HV_MESSAGE_SIZE			(256)
218 #define HV_MESSAGE_PAYLOAD_BYTE_COUNT	(240)
219 #define HV_MESSAGE_PAYLOAD_QWORD_COUNT	(30)
220 
221 /* Define synthetic interrupt controller message flags. */
222 union hv_message_flags {
223 	__u8 asu8;
224 	struct {
225 		__u8 msg_pending:1;
226 		__u8 reserved:7;
227 	} __packed;
228 };
229 
230 /* Define port identifier type. */
231 union hv_port_id {
232 	__u32 asu32;
233 	struct {
234 		__u32 id:24;
235 		__u32 reserved:8;
236 	} __packed u;
237 };
238 
239 /* Define synthetic interrupt controller message header. */
240 struct hv_message_header {
241 	__u32 message_type;
242 	__u8 payload_size;
243 	union hv_message_flags message_flags;
244 	__u8 reserved[2];
245 	union {
246 		__u64 sender;
247 		union hv_port_id port;
248 	};
249 } __packed;
250 
251 /* Define synthetic interrupt controller message format. */
252 struct hv_message {
253 	struct hv_message_header header;
254 	union {
255 		__u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
256 	} u;
257 } __packed;
258 
259 /* Define the synthetic interrupt message page layout. */
260 struct hv_message_page {
261 	struct hv_message sint_message[HV_SYNIC_SINT_COUNT];
262 } __packed;
263 
264 /* Define timer message payload structure. */
265 struct hv_timer_message_payload {
266 	__u32 timer_index;
267 	__u32 reserved;
268 	__u64 expiration_time;	/* When the timer expired */
269 	__u64 delivery_time;	/* When the message was delivered */
270 } __packed;
271 
272 
273 /* Define synthetic interrupt controller flag constants. */
274 #define HV_EVENT_FLAGS_COUNT		(256 * 8)
275 #define HV_EVENT_FLAGS_LONG_COUNT	(256 / sizeof(unsigned long))
276 
277 /*
278  * Synthetic timer configuration.
279  */
280 union hv_stimer_config {
281 	u64 as_uint64;
282 	struct {
283 		u64 enable:1;
284 		u64 periodic:1;
285 		u64 lazy:1;
286 		u64 auto_enable:1;
287 		u64 apic_vector:8;
288 		u64 direct_mode:1;
289 		u64 reserved_z0:3;
290 		u64 sintx:4;
291 		u64 reserved_z1:44;
292 	} __packed;
293 };
294 
295 
296 /* Define the synthetic interrupt controller event flags format. */
297 union hv_synic_event_flags {
298 	unsigned long flags[HV_EVENT_FLAGS_LONG_COUNT];
299 };
300 
301 /* Define SynIC control register. */
302 union hv_synic_scontrol {
303 	u64 as_uint64;
304 	struct {
305 		u64 enable:1;
306 		u64 reserved:63;
307 	} __packed;
308 };
309 
310 /* Define synthetic interrupt source. */
311 union hv_synic_sint {
312 	u64 as_uint64;
313 	struct {
314 		u64 vector:8;
315 		u64 reserved1:8;
316 		u64 masked:1;
317 		u64 auto_eoi:1;
318 		u64 polling:1;
319 		u64 reserved2:45;
320 	} __packed;
321 };
322 
323 /* Define the format of the SIMP register */
324 union hv_synic_simp {
325 	u64 as_uint64;
326 	struct {
327 		u64 simp_enabled:1;
328 		u64 preserved:11;
329 		u64 base_simp_gpa:52;
330 	} __packed;
331 };
332 
333 /* Define the format of the SIEFP register */
334 union hv_synic_siefp {
335 	u64 as_uint64;
336 	struct {
337 		u64 siefp_enabled:1;
338 		u64 preserved:11;
339 		u64 base_siefp_gpa:52;
340 	} __packed;
341 };
342 
343 struct hv_vpset {
344 	u64 format;
345 	u64 valid_bank_mask;
346 	u64 bank_contents[];
347 } __packed;
348 
349 /* HvCallSendSyntheticClusterIpi hypercall */
350 struct hv_send_ipi {
351 	u32 vector;
352 	u32 reserved;
353 	u64 cpu_mask;
354 } __packed;
355 
356 /* HvCallSendSyntheticClusterIpiEx hypercall */
357 struct hv_send_ipi_ex {
358 	u32 vector;
359 	u32 reserved;
360 	struct hv_vpset vp_set;
361 } __packed;
362 
363 /* HvFlushGuestPhysicalAddressSpace hypercalls */
364 struct hv_guest_mapping_flush {
365 	u64 address_space;
366 	u64 flags;
367 } __packed;
368 
369 /*
370  *  HV_MAX_FLUSH_PAGES = "additional_pages" + 1. It's limited
371  *  by the bitwidth of "additional_pages" in union hv_gpa_page_range.
372  */
373 #define HV_MAX_FLUSH_PAGES (2048)
374 
375 /* HvFlushGuestPhysicalAddressList hypercall */
376 union hv_gpa_page_range {
377 	u64 address_space;
378 	struct {
379 		u64 additional_pages:11;
380 		u64 largepage:1;
381 		u64 basepfn:52;
382 	} page;
383 };
384 
385 /*
386  * All input flush parameters should be in single page. The max flush
387  * count is equal with how many entries of union hv_gpa_page_range can
388  * be populated into the input parameter page.
389  */
390 #define HV_MAX_FLUSH_REP_COUNT ((HV_HYP_PAGE_SIZE - 2 * sizeof(u64)) /	\
391 				sizeof(union hv_gpa_page_range))
392 
393 struct hv_guest_mapping_flush_list {
394 	u64 address_space;
395 	u64 flags;
396 	union hv_gpa_page_range gpa_list[HV_MAX_FLUSH_REP_COUNT];
397 };
398 
399 /* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */
400 struct hv_tlb_flush {
401 	u64 address_space;
402 	u64 flags;
403 	u64 processor_mask;
404 	u64 gva_list[];
405 } __packed;
406 
407 /* HvFlushVirtualAddressSpaceEx, HvFlushVirtualAddressListEx hypercalls */
408 struct hv_tlb_flush_ex {
409 	u64 address_space;
410 	u64 flags;
411 	struct hv_vpset hv_vp_set;
412 	u64 gva_list[];
413 } __packed;
414 
415 /* HvGetPartitionId hypercall (output only) */
416 struct hv_get_partition_id {
417 	u64 partition_id;
418 } __packed;
419 
420 /* HvDepositMemory hypercall */
421 struct hv_deposit_memory {
422 	u64 partition_id;
423 	u64 gpa_page_list[];
424 } __packed;
425 
426 struct hv_proximity_domain_flags {
427 	u32 proximity_preferred : 1;
428 	u32 reserved : 30;
429 	u32 proximity_info_valid : 1;
430 } __packed;
431 
432 /* Not a union in windows but useful for zeroing */
433 union hv_proximity_domain_info {
434 	struct {
435 		u32 domain_id;
436 		struct hv_proximity_domain_flags flags;
437 	};
438 	u64 as_uint64;
439 } __packed;
440 
441 struct hv_lp_startup_status {
442 	u64 hv_status;
443 	u64 substatus1;
444 	u64 substatus2;
445 	u64 substatus3;
446 	u64 substatus4;
447 	u64 substatus5;
448 	u64 substatus6;
449 } __packed;
450 
451 /* HvAddLogicalProcessor hypercall */
452 struct hv_add_logical_processor_in {
453 	u32 lp_index;
454 	u32 apic_id;
455 	union hv_proximity_domain_info proximity_domain_info;
456 	u64 flags;
457 } __packed;
458 
459 struct hv_add_logical_processor_out {
460 	struct hv_lp_startup_status startup_status;
461 } __packed;
462 
463 enum HV_SUBNODE_TYPE
464 {
465     HvSubnodeAny = 0,
466     HvSubnodeSocket = 1,
467     HvSubnodeAmdNode = 2,
468     HvSubnodeL3 = 3,
469     HvSubnodeCount = 4,
470     HvSubnodeInvalid = -1
471 };
472 
473 /* HvCreateVp hypercall */
474 struct hv_create_vp {
475 	u64 partition_id;
476 	u32 vp_index;
477 	u8 padding[3];
478 	u8 subnode_type;
479 	u64 subnode_id;
480 	union hv_proximity_domain_info proximity_domain_info;
481 	u64 flags;
482 } __packed;
483 
484 enum hv_interrupt_source {
485 	HV_INTERRUPT_SOURCE_MSI = 1, /* MSI and MSI-X */
486 	HV_INTERRUPT_SOURCE_IOAPIC,
487 };
488 
489 union hv_msi_address_register {
490 	u32 as_uint32;
491 	struct {
492 		u32 reserved1:2;
493 		u32 destination_mode:1;
494 		u32 redirection_hint:1;
495 		u32 reserved2:8;
496 		u32 destination_id:8;
497 		u32 msi_base:12;
498 	};
499 } __packed;
500 
501 union hv_msi_data_register {
502 	u32 as_uint32;
503 	struct {
504 		u32 vector:8;
505 		u32 delivery_mode:3;
506 		u32 reserved1:3;
507 		u32 level_assert:1;
508 		u32 trigger_mode:1;
509 		u32 reserved2:16;
510 	};
511 } __packed;
512 
513 /* HvRetargetDeviceInterrupt hypercall */
514 union hv_msi_entry {
515 	u64 as_uint64;
516 	struct {
517 		union hv_msi_address_register address;
518 		union hv_msi_data_register data;
519 	} __packed;
520 };
521 
522 union hv_ioapic_rte {
523 	u64 as_uint64;
524 
525 	struct {
526 		u32 vector:8;
527 		u32 delivery_mode:3;
528 		u32 destination_mode:1;
529 		u32 delivery_status:1;
530 		u32 interrupt_polarity:1;
531 		u32 remote_irr:1;
532 		u32 trigger_mode:1;
533 		u32 interrupt_mask:1;
534 		u32 reserved1:15;
535 
536 		u32 reserved2:24;
537 		u32 destination_id:8;
538 	};
539 
540 	struct {
541 		u32 low_uint32;
542 		u32 high_uint32;
543 	};
544 } __packed;
545 
546 struct hv_interrupt_entry {
547 	u32 source;
548 	u32 reserved1;
549 	union {
550 		union hv_msi_entry msi_entry;
551 		union hv_ioapic_rte ioapic_rte;
552 	};
553 } __packed;
554 
555 /*
556  * flags for hv_device_interrupt_target.flags
557  */
558 #define HV_DEVICE_INTERRUPT_TARGET_MULTICAST		1
559 #define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET	2
560 
561 struct hv_device_interrupt_target {
562 	u32 vector;
563 	u32 flags;
564 	union {
565 		u64 vp_mask;
566 		struct hv_vpset vp_set;
567 	};
568 } __packed;
569 
570 struct hv_retarget_device_interrupt {
571 	u64 partition_id;		/* use "self" */
572 	u64 device_id;
573 	struct hv_interrupt_entry int_entry;
574 	u64 reserved2;
575 	struct hv_device_interrupt_target int_target;
576 } __packed __aligned(8);
577 
578 
579 /* HvGetVpRegisters hypercall input with variable size reg name list*/
580 struct hv_get_vp_registers_input {
581 	struct {
582 		u64 partitionid;
583 		u32 vpindex;
584 		u8  inputvtl;
585 		u8  padding[3];
586 	} header;
587 	struct input {
588 		u32 name0;
589 		u32 name1;
590 	} element[];
591 } __packed;
592 
593 
594 /* HvGetVpRegisters returns an array of these output elements */
595 struct hv_get_vp_registers_output {
596 	union {
597 		struct {
598 			u32 a;
599 			u32 b;
600 			u32 c;
601 			u32 d;
602 		} as32 __packed;
603 		struct {
604 			u64 low;
605 			u64 high;
606 		} as64 __packed;
607 	};
608 };
609 
610 /* HvSetVpRegisters hypercall with variable size reg name/value list*/
611 struct hv_set_vp_registers_input {
612 	struct {
613 		u64 partitionid;
614 		u32 vpindex;
615 		u8  inputvtl;
616 		u8  padding[3];
617 	} header;
618 	struct {
619 		u32 name;
620 		u32 padding1;
621 		u64 padding2;
622 		u64 valuelow;
623 		u64 valuehigh;
624 	} element[];
625 } __packed;
626 
627 #endif
628