2 * Copyright (c) 2009-2012 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef __HYPERV_PRIV_H__
32 #define __HYPERV_PRIV_H__
34 #include <sys/param.h>
36 #include <sys/mutex.h>
39 #include <dev/hyperv/include/hyperv.h>
43 * Status codes for hypervisor operations.
46 typedef uint16_t hv_vmbus_status;
48 #define HV_MESSAGE_SIZE (256)
49 #define HV_MESSAGE_PAYLOAD_BYTE_COUNT (240)
50 #define HV_MESSAGE_PAYLOAD_QWORD_COUNT (30)
51 #define HV_ANY_VP (0xFFFFFFFF)
54 * Synthetic interrupt controller flag constants.
57 #define HV_EVENT_FLAGS_COUNT (256 * 8)
58 #define HV_EVENT_FLAGS_BYTE_COUNT (256)
59 #define HV_EVENT_FLAGS_DWORD_COUNT (256 / sizeof(uint32_t))
62 * max channel count <== event_flags_dword_count * bit_of_dword
64 #define HV_CHANNEL_DWORD_LEN (32)
65 #define HV_CHANNEL_MAX_COUNT \
66 ((HV_EVENT_FLAGS_DWORD_COUNT) * HV_CHANNEL_DWORD_LEN)
68 * MessageId: HV_STATUS_INSUFFICIENT_BUFFERS
70 * You did not supply enough message buffers to send a message.
73 #define HV_STATUS_INSUFFICIENT_BUFFERS ((uint16_t)0x0013)
75 typedef void (*hv_vmbus_channel_callback)(void *context);
80 } hv_vmbus_sg_buffer_list;
83 uint32_t current_interrupt_mask;
84 uint32_t current_read_index;
85 uint32_t current_write_index;
86 uint32_t bytes_avail_to_read;
87 uint32_t bytes_avail_to_write;
88 } hv_vmbus_ring_buffer_debug_info;
92 hv_vmbus_channel_state state;
93 hv_guid interface_type;
94 hv_guid interface_instance;
96 uint32_t server_monitor_pending;
97 uint32_t server_monitor_latency;
98 uint32_t server_monitor_connection_id;
99 uint32_t client_monitor_pending;
100 uint32_t client_monitor_latency;
101 uint32_t client_monitor_connection_id;
102 hv_vmbus_ring_buffer_debug_info inbound;
103 hv_vmbus_ring_buffer_debug_info outbound;
104 } hv_vmbus_channel_debug_info;
107 hv_vmbus_channel_version_supported version_supported;
108 hv_vmbus_channel_open_result open_result;
109 hv_vmbus_channel_gpadl_torndown gpadl_torndown;
110 hv_vmbus_channel_gpadl_created gpadl_created;
111 hv_vmbus_channel_version_response version_response;
112 } hv_vmbus_channel_msg_response;
115 * Represents each channel msg on the vmbus connection
116 * This is a variable-size data structure depending on
117 * the msg type itself
119 typedef struct hv_vmbus_channel_msg_info {
123 TAILQ_ENTRY(hv_vmbus_channel_msg_info) msg_list_entry;
125 * So far, this is only used to handle
128 TAILQ_HEAD(, hv_vmbus_channel_msg_info) sub_msg_list_anchor;
130 * Synchronize the request/response if
132 * KYS: Use a semaphore for now.
135 struct sema wait_sema;
136 hv_vmbus_channel_msg_response response;
137 uint32_t message_size;
139 * The channel message that goes out on
140 * the "wire". It will contain at
142 * hv_vmbus_channel_msg_header
145 unsigned char msg[0];
146 } hv_vmbus_channel_msg_info;
149 * The format must be the same as hv_vm_data_gpa_direct
151 typedef struct hv_vmbus_channel_packet_page_buffer {
153 uint16_t data_offset8;
156 uint64_t transaction_id;
158 uint32_t range_count;
159 hv_vmbus_page_buffer range[HV_MAX_PAGE_BUFFER_COUNT];
160 } __packed hv_vmbus_channel_packet_page_buffer;
163 * The format must be the same as hv_vm_data_gpa_direct
165 typedef struct hv_vmbus_channel_packet_multipage_buffer {
167 uint16_t data_offset8;
170 uint64_t transaction_id;
172 uint32_t range_count; /* Always 1 in this case */
173 hv_vmbus_multipage_buffer range;
174 } __packed hv_vmbus_channel_packet_multipage_buffer;
177 HV_VMBUS_MESSAGE_CONNECTION_ID = 1,
178 HV_VMBUS_MESSAGE_PORT_ID = 1,
179 HV_VMBUS_EVENT_CONNECTION_ID = 2,
180 HV_VMBUS_EVENT_PORT_ID = 2,
181 HV_VMBUS_MONITOR_CONNECTION_ID = 3,
182 HV_VMBUS_MONITOR_PORT_ID = 3,
183 HV_VMBUS_MESSAGE_SINT = 2
186 #define HV_PRESENT_BIT 0x80000000
188 #define HV_HYPERCALL_PARAM_ALIGN sizeof(uint64_t)
192 void* hypercall_page;
193 hv_bool_uint8_t syn_ic_initialized;
195 hv_vmbus_handle syn_ic_msg_page[MAXCPU];
196 hv_vmbus_handle syn_ic_event_page[MAXCPU];
198 * For FreeBSD cpuid to Hyper-V vcpuid mapping.
200 uint32_t hv_vcpu_index[MAXCPU];
202 * Each cpu has its own software interrupt handler for channel
203 * event and msg handling.
205 struct taskqueue *hv_event_queue[MAXCPU];
206 struct intr_event *hv_msg_intr_event[MAXCPU];
207 void *msg_swintr[MAXCPU];
209 * Host use this vector to intrrupt guest for vmbus channel
212 unsigned int hv_cb_vector;
216 * Define hypervisor message types
220 HV_MESSAGE_TYPE_NONE = 0x00000000,
223 * Memory access messages
225 HV_MESSAGE_TYPE_UNMAPPED_GPA = 0x80000000,
226 HV_MESSAGE_TYPE_GPA_INTERCEPT = 0x80000001,
229 * Timer notification messages
231 HV_MESSAGE_TIMER_EXPIRED = 0x80000010,
236 HV_MESSAGE_TYPE_INVALID_VP_REGISTER_VALUE = 0x80000020,
237 HV_MESSAGE_TYPE_UNRECOVERABLE_EXCEPTION = 0x80000021,
238 HV_MESSAGE_TYPE_UNSUPPORTED_FEATURE = 0x80000022,
241 * Trace buffer complete messages
243 HV_MESSAGE_TYPE_EVENT_LOG_BUFFER_COMPLETE = 0x80000040,
246 * Platform-specific processor intercept messages
248 HV_MESSAGE_TYPE_X64_IO_PORT_INTERCEPT = 0x80010000,
249 HV_MESSAGE_TYPE_X64_MSR_INTERCEPT = 0x80010001,
250 HV_MESSAGE_TYPE_X64_CPU_INTERCEPT = 0x80010002,
251 HV_MESSAGE_TYPE_X64_EXCEPTION_INTERCEPT = 0x80010003,
252 HV_MESSAGE_TYPE_X64_APIC_EOI = 0x80010004,
253 HV_MESSAGE_TYPE_X64_LEGACY_FP_ERROR = 0x80010005
258 * Define port identifier type
260 typedef union _hv_vmbus_port_id {
261 uint32_t as_uint32_t;
269 * Define synthetic interrupt controller message flag
274 uint8_t message_pending:1;
277 } hv_vmbus_msg_flags;
279 typedef uint64_t hv_vmbus_partition_id;
282 * Define synthetic interrupt controller message header
285 hv_vmbus_msg_type message_type;
286 uint8_t payload_size;
287 hv_vmbus_msg_flags message_flags;
290 hv_vmbus_partition_id sender;
291 hv_vmbus_port_id port;
293 } hv_vmbus_msg_header;
296 * Define synthetic interrupt controller message format
299 hv_vmbus_msg_header header;
301 uint64_t payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
306 * Maximum channels is determined by the size of the interrupt
307 * page which is PAGE_SIZE. 1/2 of PAGE_SIZE is for
308 * send endpoint interrupt and the other is receive
309 * endpoint interrupt.
311 * Note: (PAGE_SIZE >> 1) << 3 allocates 16348 channels
313 #define HV_MAX_NUM_CHANNELS (PAGE_SIZE >> 1) << 3
316 * (The value here must be in multiple of 32)
318 #define HV_MAX_NUM_CHANNELS_SUPPORTED 256
321 * VM Bus connection states
328 } hv_vmbus_connect_state;
330 #define HV_MAX_SIZE_CHANNEL_MESSAGE HV_MESSAGE_PAYLOAD_BYTE_COUNT
334 hv_vmbus_connect_state connect_state;
335 uint32_t next_gpadl_handle;
337 * Represents channel interrupts. Each bit position
338 * represents a channel.
339 * When a channel sends an interrupt via VMBUS, it
340 * finds its bit in the send_interrupt_page, set it and
341 * calls Hv to generate a port event. The other end
342 * receives the port event and parse the
343 * recv_interrupt_page to see which bit is set
345 void *interrupt_page;
346 void *send_interrupt_page;
347 void *recv_interrupt_page;
349 * 2 pages - 1st page for parent->child
350 * notification and 2nd is child->parent
353 void *monitor_page_1;
354 void *monitor_page_2;
355 TAILQ_HEAD(, hv_vmbus_channel_msg_info) channel_msg_anchor;
356 struct mtx channel_msg_lock;
358 * List of primary channels. Sub channels will be linked
359 * under their primary channel.
361 TAILQ_HEAD(, hv_vmbus_channel) channel_anchor;
362 struct mtx channel_lock;
365 * channel table for fast lookup through id.
367 hv_vmbus_channel **channels;
368 } hv_vmbus_connection;
371 uint64_t as_uint64_t;
373 uint64_t build_number : 16;
374 uint64_t service_version : 8; /* Service Pack, etc. */
375 uint64_t minor_version : 8;
376 uint64_t major_version : 8;
378 * HV_GUEST_OS_MICROSOFT_IDS (If Vendor=MS)
382 uint64_t vendor_id : 16;
384 } hv_vmbus_x64_msr_guest_os_id_contents;
388 uint64_t as_uint64_t;
391 uint64_t reserved :11;
392 uint64_t guest_physical_address :52;
394 } hv_vmbus_x64_msr_hypercall_contents;
397 uint32_t as_uint32_t;
399 uint32_t group_enable :4;
402 } hv_vmbus_monitor_trigger_state;
405 uint64_t as_uint64_t;
410 } hv_vmbus_monitor_trigger_group;
413 hv_vmbus_connection_id connection_id;
414 uint16_t flag_number;
416 } hv_vmbus_monitor_parameter;
419 * hv_vmbus_monitor_page Layout
420 * ------------------------------------------------------
421 * | 0 | trigger_state (4 bytes) | Rsvd1 (4 bytes) |
422 * | 8 | trigger_group[0] |
423 * | 10 | trigger_group[1] |
424 * | 18 | trigger_group[2] |
425 * | 20 | trigger_group[3] |
429 * | 40 | next_check_time[0][0] | next_check_time[0][1] |
431 * | 240 | latency[0][0..3] |
433 * | 440 | parameter[0][0] |
434 * | 448 | parameter[0][1] |
437 * ------------------------------------------------------
441 hv_vmbus_monitor_trigger_state trigger_state;
444 hv_vmbus_monitor_trigger_group trigger_group[4];
447 int32_t next_check_time[4][32];
449 uint16_t latency[4][32];
450 uint64_t rsvd_z3[32];
452 hv_vmbus_monitor_parameter parameter[4][32];
454 uint8_t rsvd_z4[1984];
455 } hv_vmbus_monitor_page;
458 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
459 * is set by CPUID(HV_CPU_ID_FUNCTION_VERSION_AND_FEATURES).
462 HV_CPU_ID_FUNCTION_VERSION_AND_FEATURES = 0x00000001,
463 HV_CPU_ID_FUNCTION_HV_VENDOR_AND_MAX_FUNCTION = 0x40000000,
464 HV_CPU_ID_FUNCTION_HV_INTERFACE = 0x40000001,
466 * The remaining functions depend on the value
467 * of hv_cpu_id_function_interface
469 HV_CPU_ID_FUNCTION_MS_HV_VERSION = 0x40000002,
470 HV_CPU_ID_FUNCTION_MS_HV_FEATURES = 0x40000003,
471 HV_CPU_ID_FUNCTION_MS_HV_ENLIGHTENMENT_INFORMATION = 0x40000004,
472 HV_CPU_ID_FUNCTION_MS_HV_IMPLEMENTATION_LIMITS = 0x40000005
474 } hv_vmbus_cpuid_function;
477 * Define the format of the SIMP register
480 uint64_t as_uint64_t;
482 uint64_t simp_enabled : 1;
483 uint64_t preserved : 11;
484 uint64_t base_simp_gpa : 52;
486 } hv_vmbus_synic_simp;
489 * Define the format of the SIEFP register
492 uint64_t as_uint64_t;
494 uint64_t siefp_enabled : 1;
495 uint64_t preserved : 11;
496 uint64_t base_siefp_gpa : 52;
498 } hv_vmbus_synic_siefp;
501 * Define synthetic interrupt source
504 uint64_t as_uint64_t;
507 uint64_t reserved1 : 8;
509 uint64_t auto_eoi : 1;
510 uint64_t reserved2 : 46;
512 } hv_vmbus_synic_sint;
515 * Timer configuration register.
517 union hv_timer_config {
523 uint64_t auto_enable:1;
524 uint64_t reserved_z0:12;
526 uint64_t reserved_z1:44;
531 * Define syn_ic control register
533 typedef union _hv_vmbus_synic_scontrol {
534 uint64_t as_uint64_t;
537 uint64_t reserved : 63;
539 } hv_vmbus_synic_scontrol;
542 * Define the hv_vmbus_post_message hypercall input structure
545 hv_vmbus_connection_id connection_id;
547 hv_vmbus_msg_type message_type;
548 uint32_t payload_size;
549 uint64_t payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
550 } hv_vmbus_input_post_message;
553 * Define the synthetic interrupt controller event flags format
556 uint8_t flags8[HV_EVENT_FLAGS_BYTE_COUNT];
557 uint32_t flags32[HV_EVENT_FLAGS_DWORD_COUNT];
558 } hv_vmbus_synic_event_flags;
560 #define HV_X64_CPUID_MIN (0x40000005)
561 #define HV_X64_CPUID_MAX (0x4000ffff)
564 * Declare the MSR used to identify the guest OS
566 #define HV_X64_MSR_GUEST_OS_ID (0x40000000)
568 * Declare the MSR used to setup pages used to communicate with the hypervisor
570 #define HV_X64_MSR_HYPERCALL (0x40000001)
571 /* MSR used to provide vcpu index */
572 #define HV_X64_MSR_VP_INDEX (0x40000002)
574 #define HV_X64_MSR_TIME_REF_COUNT (0x40000020)
577 * Define synthetic interrupt controller model specific registers
579 #define HV_X64_MSR_SCONTROL (0x40000080)
580 #define HV_X64_MSR_SVERSION (0x40000081)
581 #define HV_X64_MSR_SIEFP (0x40000082)
582 #define HV_X64_MSR_SIMP (0x40000083)
583 #define HV_X64_MSR_EOM (0x40000084)
585 #define HV_X64_MSR_SINT0 (0x40000090)
586 #define HV_X64_MSR_SINT1 (0x40000091)
587 #define HV_X64_MSR_SINT2 (0x40000092)
588 #define HV_X64_MSR_SINT3 (0x40000093)
589 #define HV_X64_MSR_SINT4 (0x40000094)
590 #define HV_X64_MSR_SINT5 (0x40000095)
591 #define HV_X64_MSR_SINT6 (0x40000096)
592 #define HV_X64_MSR_SINT7 (0x40000097)
593 #define HV_X64_MSR_SINT8 (0x40000098)
594 #define HV_X64_MSR_SINT9 (0x40000099)
595 #define HV_X64_MSR_SINT10 (0x4000009A)
596 #define HV_X64_MSR_SINT11 (0x4000009B)
597 #define HV_X64_MSR_SINT12 (0x4000009C)
598 #define HV_X64_MSR_SINT13 (0x4000009D)
599 #define HV_X64_MSR_SINT14 (0x4000009E)
600 #define HV_X64_MSR_SINT15 (0x4000009F)
603 * Synthetic Timer MSRs. Four timers per vcpu.
605 #define HV_X64_MSR_STIMER0_CONFIG 0x400000B0
606 #define HV_X64_MSR_STIMER0_COUNT 0x400000B1
607 #define HV_X64_MSR_STIMER1_CONFIG 0x400000B2
608 #define HV_X64_MSR_STIMER1_COUNT 0x400000B3
609 #define HV_X64_MSR_STIMER2_CONFIG 0x400000B4
610 #define HV_X64_MSR_STIMER2_COUNT 0x400000B5
611 #define HV_X64_MSR_STIMER3_CONFIG 0x400000B6
612 #define HV_X64_MSR_STIMER3_COUNT 0x400000B7
615 * Declare the various hypercall operations
618 HV_CALL_POST_MESSAGE = 0x005c,
619 HV_CALL_SIGNAL_EVENT = 0x005d,
620 } hv_vmbus_call_code;
626 extern hv_vmbus_context hv_vmbus_g_context;
627 extern hv_vmbus_connection hv_vmbus_g_connection;
629 typedef void (*vmbus_msg_handler)(hv_vmbus_channel_msg_header *msg);
631 typedef struct hv_vmbus_channel_msg_table_entry {
632 hv_vmbus_channel_msg_type messageType;
634 vmbus_msg_handler messageHandler;
635 } hv_vmbus_channel_msg_table_entry;
637 extern hv_vmbus_channel_msg_table_entry g_channel_message_table[];
640 * Private, VM Bus functions
643 int hv_vmbus_ring_buffer_init(
644 hv_vmbus_ring_buffer_info *ring_info,
646 uint32_t buffer_len);
648 void hv_ring_buffer_cleanup(
649 hv_vmbus_ring_buffer_info *ring_info);
651 int hv_ring_buffer_write(
652 hv_vmbus_ring_buffer_info *ring_info,
653 hv_vmbus_sg_buffer_list sg_buffers[],
654 uint32_t sg_buff_count,
655 boolean_t *need_sig);
657 int hv_ring_buffer_peek(
658 hv_vmbus_ring_buffer_info *ring_info,
660 uint32_t buffer_len);
662 int hv_ring_buffer_read(
663 hv_vmbus_ring_buffer_info *ring_info,
668 uint32_t hv_vmbus_get_ring_buffer_interrupt_mask(
669 hv_vmbus_ring_buffer_info *ring_info);
671 void hv_vmbus_dump_ring_info(
672 hv_vmbus_ring_buffer_info *ring_info,
675 void hv_ring_buffer_read_begin(
676 hv_vmbus_ring_buffer_info *ring_info);
678 uint32_t hv_ring_buffer_read_end(
679 hv_vmbus_ring_buffer_info *ring_info);
681 hv_vmbus_channel* hv_vmbus_allocate_channel(void);
682 void hv_vmbus_free_vmbus_channel(hv_vmbus_channel *channel);
683 int hv_vmbus_request_channel_offers(void);
684 void hv_vmbus_release_unattached_channels(void);
685 int hv_vmbus_init(void);
686 void hv_vmbus_cleanup(void);
688 uint16_t hv_vmbus_post_msg_via_msg_ipc(
689 hv_vmbus_connection_id connection_id,
690 hv_vmbus_msg_type message_type,
692 size_t payload_size);
694 uint16_t hv_vmbus_signal_event(void *con_id);
695 void hv_vmbus_synic_init(void *irq_arg);
696 void hv_vmbus_synic_cleanup(void *arg);
697 int hv_vmbus_query_hypervisor_presence(void);
699 struct hv_device* hv_vmbus_child_device_create(
701 hv_guid device_instance,
702 hv_vmbus_channel *channel);
704 int hv_vmbus_child_device_register(
705 struct hv_device *child_dev);
706 int hv_vmbus_child_device_unregister(
707 struct hv_device *child_dev);
710 * Connection interfaces
712 int hv_vmbus_connect(void);
713 int hv_vmbus_disconnect(void);
714 int hv_vmbus_post_message(void *buffer, size_t buf_size);
715 int hv_vmbus_set_event(hv_vmbus_channel *channel);
716 void hv_vmbus_on_events(int cpu);
719 * Event Timer interfaces
721 void hv_et_init(void);
722 void hv_et_intr(struct trapframe*);
725 * The guest OS needs to register the guest ID with the hypervisor.
726 * The guest ID is a 64 bit entity and the structure of this ID is
727 * specified in the Hyper-V specification:
729 * http://msdn.microsoft.com/en-us/library/windows/
730 * hardware/ff542653%28v=vs.85%29.aspx
732 * While the current guideline does not specify how FreeBSD guest ID(s)
733 * need to be generated, our plan is to publish the guidelines for
734 * FreeBSD and other guest operating systems that currently are hosted
735 * on Hyper-V. The implementation here conforms to this yet
736 * unpublished guidelines.
739 * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
740 * 62:56 - Os Type; Linux is 0x100, FreeBSD is 0x200
741 * 55:48 - Distro specific identification
742 * 47:16 - FreeBSD kernel version number
743 * 15:0 - Distro specific identification
747 #define HV_FREEBSD_VENDOR_ID 0x8200
748 #define HV_FREEBSD_GUEST_ID hv_generate_guest_id(0,0)
750 static inline uint64_t hv_generate_guest_id(
751 uint8_t distro_id_part1,
752 uint16_t distro_id_part2)
755 guest_id = (((uint64_t)HV_FREEBSD_VENDOR_ID) << 48);
756 guest_id |= (((uint64_t)(distro_id_part1)) << 48);
757 guest_id |= (((uint64_t)(__FreeBSD_version)) << 16); /* in param.h */
758 guest_id |= ((uint64_t)(distro_id_part2));
764 void *page_buffers[2 * MAXCPU];
767 #endif /* __HYPERV_PRIV_H__ */