2 * Copyright (c) 2014 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * A common driver for all hyper-V util services.
33 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/reboot.h>
39 #include <sys/timetc.h>
40 #include <sys/syscallsubr.h>
42 #include <dev/hyperv/include/hyperv.h>
50 static void hv_shutdown_cb(void *context);
51 static void hv_heartbeat_cb(void *context);
52 static void hv_timesync_cb(void *context);
54 static int hv_timesync_init(hv_vmbus_service *serv);
55 static int hv_timesync_uninit(hv_vmbus_service *serv);
56 static void hv_set_host_time(void *context, int pending);
59 * Note: GUID codes below are predefined by the host hypervisor
60 * (Hyper-V and Azure)interface and required for correct operation.
62 hv_vmbus_service service_table[] = {
63 /* Shutdown Service */
64 { .guid.data = {0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
65 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB},
66 .name = "Hyper-V Shutdown Service\n",
68 .callback = hv_shutdown_cb,
71 /* Time Synch Service */
72 { .guid.data = {0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
73 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf},
74 .name = "Hyper-V Time Synch Service\n",
76 .init = hv_timesync_init,
77 .callback = hv_timesync_cb,
78 .uninit = hv_timesync_uninit,
81 /* Heartbeat Service */
82 { .guid.data = {0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
83 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d},
84 .name = "Hyper-V Heartbeat Service\n",
86 .callback = hv_heartbeat_cb,
89 /* KVP (Key Value Pair) Service */
90 { .guid.data = {0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
91 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6},
92 .name = "Hyper-V KVP Service\n",
95 .callback = hv_kvp_callback,
100 * Receive buffer pointers. There is one buffer per utility service. The
101 * buffer is allocated during attach().
103 uint8_t *receive_buffer[HV_MAX_UTIL_SERVICES];
105 static boolean_t destroyed_kvp = FALSE;
107 struct hv_ictimesync_data {
110 uint64_t roundtriptime;
115 hv_timesync_init(hv_vmbus_service *serv)
117 void *time_msg = malloc(sizeof(time_sync_data), M_DEVBUF, M_WAITOK);
118 TASK_INIT(&serv->task, 1, hv_set_host_time, time_msg);
123 hv_timesync_uninit(hv_vmbus_service *serv)
125 taskqueue_drain(taskqueue_thread, &serv->task);
126 free(serv->task.ta_context, M_DEVBUF);
131 hv_negotiate_version(
132 struct hv_vmbus_icmsg_hdr* icmsghdrp,
133 struct hv_vmbus_icmsg_negotiate* negop,
136 icmsghdrp->icmsgsize = 0x10;
138 negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
139 sizeof(struct hv_vmbus_pipe_hdr) +
140 sizeof(struct hv_vmbus_icmsg_hdr)];
142 if (negop->icframe_vercnt >= 2 &&
143 negop->icversion_data[1].major == 3) {
144 negop->icversion_data[0].major = 3;
145 negop->icversion_data[0].minor = 0;
146 negop->icversion_data[1].major = 3;
147 negop->icversion_data[1].minor = 0;
149 negop->icversion_data[0].major = 1;
150 negop->icversion_data[0].minor = 0;
151 negop->icversion_data[1].major = 1;
152 negop->icversion_data[1].minor = 0;
155 negop->icframe_vercnt = 1;
156 negop->icmsg_vercnt = 1;
161 * Set host time based on time sync message from host
164 hv_set_host_time(void *context, int pending)
166 time_sync_data* time_msg = (time_sync_data*) context;
167 uint64_t hosttime = time_msg->data;
168 struct timespec guest_ts, host_ts;
173 host_tns = (hosttime - HV_WLTIMEDELTA) * 100;
174 host_ts.tv_sec = (time_t)(host_tns/HV_NANO_SEC_PER_SEC);
175 host_ts.tv_nsec = (long)(host_tns%HV_NANO_SEC_PER_SEC);
179 diff = (int64_t)host_ts.tv_sec - (int64_t)guest_ts.tv_sec;
182 * If host differs by 5 seconds then make the guest catch up
184 if (diff > 5 || diff < -5) {
185 error = kern_clock_settime(curthread, CLOCK_REALTIME,
191 * @brief Synchronize time with host after reboot, restore, etc.
193 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
194 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
195 * message after the timesync channel is opened. Since the hv_utils module is
196 * loaded after hv_vmbus, the first message is usually missed. The other
197 * thing is, systime is automatically set to emulated hardware clock which may
198 * not be UTC time or in the same time zone. So, to override these effects, we
199 * use the first 50 time samples for initial system time setting.
202 void hv_adj_guesttime(uint64_t hosttime, uint8_t flags)
204 time_sync_data* time_msg = service_table[HV_TIME_SYNCH].task.ta_context;
206 time_msg->data = hosttime;
208 if (((flags & HV_ICTIMESYNCFLAG_SYNC) != 0) ||
209 ((flags & HV_ICTIMESYNCFLAG_SAMPLE) != 0)) {
210 taskqueue_enqueue(taskqueue_thread, &service_table[HV_TIME_SYNCH].task);
215 * Time Sync Channel message handler
218 hv_timesync_cb(void *context)
220 hv_vmbus_channel* channel = context;
221 hv_vmbus_icmsg_hdr* icmsghdrp;
226 struct hv_ictimesync_data* timedatap;
228 time_buf = receive_buffer[HV_TIME_SYNCH];
230 ret = hv_vmbus_channel_recv_packet(channel, time_buf,
231 PAGE_SIZE, &recvlen, &requestId);
233 if ((ret == 0) && recvlen > 0) {
234 icmsghdrp = (struct hv_vmbus_icmsg_hdr *) &time_buf[
235 sizeof(struct hv_vmbus_pipe_hdr)];
237 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
238 hv_negotiate_version(icmsghdrp, NULL, time_buf);
240 timedatap = (struct hv_ictimesync_data *) &time_buf[
241 sizeof(struct hv_vmbus_pipe_hdr) +
242 sizeof(struct hv_vmbus_icmsg_hdr)];
243 hv_adj_guesttime(timedatap->parenttime, timedatap->flags);
246 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION
247 | HV_ICMSGHDRFLAG_RESPONSE;
249 hv_vmbus_channel_send_packet(channel, time_buf,
251 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
259 hv_shutdown_cb(void *context)
262 hv_vmbus_channel* channel = context;
263 uint8_t execute_shutdown = 0;
264 hv_vmbus_icmsg_hdr* icmsghdrp;
268 hv_vmbus_shutdown_msg_data* shutdown_msg;
270 buf = receive_buffer[HV_SHUT_DOWN];
272 ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE,
273 &recv_len, &request_id);
275 if ((ret == 0) && recv_len > 0) {
277 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
278 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
280 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
281 hv_negotiate_version(icmsghdrp, NULL, buf);
285 (struct hv_vmbus_shutdown_msg_data *)
286 &buf[sizeof(struct hv_vmbus_pipe_hdr) +
287 sizeof(struct hv_vmbus_icmsg_hdr)];
289 switch (shutdown_msg->flags) {
292 icmsghdrp->status = HV_S_OK;
293 execute_shutdown = 1;
295 printf("Shutdown request received -"
296 " graceful shutdown initiated\n");
299 icmsghdrp->status = HV_E_FAIL;
300 execute_shutdown = 0;
301 printf("Shutdown request received -"
302 " Invalid request\n");
307 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
308 HV_ICMSGHDRFLAG_RESPONSE;
310 hv_vmbus_channel_send_packet(channel, buf,
311 recv_len, request_id,
312 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
315 if (execute_shutdown)
316 shutdown_nice(RB_POWEROFF);
320 * Process heartbeat message
323 hv_heartbeat_cb(void *context)
326 hv_vmbus_channel* channel = context;
331 struct hv_vmbus_heartbeat_msg_data* heartbeat_msg;
332 struct hv_vmbus_icmsg_hdr* icmsghdrp;
334 buf = receive_buffer[HV_HEART_BEAT];
336 ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE, &recvlen,
339 if ((ret == 0) && recvlen > 0) {
341 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
342 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
344 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
345 hv_negotiate_version(icmsghdrp, NULL, buf);
349 (struct hv_vmbus_heartbeat_msg_data *)
350 &buf[sizeof(struct hv_vmbus_pipe_hdr) +
351 sizeof(struct hv_vmbus_icmsg_hdr)];
353 heartbeat_msg->seq_num += 1;
356 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
357 HV_ICMSGHDRFLAG_RESPONSE;
359 hv_vmbus_channel_send_packet(channel, buf, recvlen, requestid,
360 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
366 hv_util_probe(device_t dev)
369 int rtn_value = ENXIO;
371 for (i = 0; i < HV_MAX_UTIL_SERVICES; i++) {
372 const char *p = vmbus_get_type(dev);
373 if (service_table[i].enabled && !memcmp(p, &service_table[i].guid, sizeof(hv_guid))) {
374 device_set_softc(dev, (void *) (&service_table[i]));
375 rtn_value = BUS_PROBE_DEFAULT;
383 hv_util_attach(device_t dev)
385 struct hv_device* hv_dev;
386 struct hv_vmbus_service* service;
388 size_t receive_buffer_offset;
390 hv_dev = vmbus_get_devctx(dev);
391 service = device_get_softc(dev);
392 receive_buffer_offset = service - &service_table[0];
393 device_printf(dev, "Hyper-V Service attaching: %s\n", service->name);
394 receive_buffer[receive_buffer_offset] =
395 malloc(4 * PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
397 if (service->init != NULL) {
398 ret = service->init(service);
406 * These services are not performance critical and do not need
407 * batched reading. Furthermore, some services such as KVP can
408 * only handle one message from the host at a time.
409 * Turn off batched reading for all util drivers before we open the
412 hv_set_channel_read_state(hv_dev->channel, FALSE);
414 ret = hv_vmbus_channel_open(hv_dev->channel, 4 * PAGE_SIZE,
415 4 * PAGE_SIZE, NULL, 0,
416 service->callback, hv_dev->channel);
425 free(receive_buffer[receive_buffer_offset], M_DEVBUF);
426 receive_buffer[receive_buffer_offset] = NULL;
432 hv_util_detach(device_t dev)
434 struct hv_device* hv_dev;
435 struct hv_vmbus_service* service;
436 size_t receive_buffer_offset;
438 if (!destroyed_kvp) {
440 destroyed_kvp = TRUE;
443 hv_dev = vmbus_get_devctx(dev);
445 hv_vmbus_channel_close(hv_dev->channel);
446 service = device_get_softc(dev);
447 receive_buffer_offset = service - &service_table[0];
449 if (service->uninit != NULL)
450 service->uninit(service);
452 free(receive_buffer[receive_buffer_offset], M_DEVBUF);
453 receive_buffer[receive_buffer_offset] = NULL;
458 hv_util_modevent(module_t mod, int event, void *arg)
471 static device_method_t util_methods[] = {
472 /* Device interface */
473 DEVMETHOD(device_probe, hv_util_probe),
474 DEVMETHOD(device_attach, hv_util_attach),
475 DEVMETHOD(device_detach, hv_util_detach),
476 DEVMETHOD(device_shutdown, bus_generic_shutdown),
480 static driver_t util_driver = { "hyperv-utils", util_methods, 0 };
482 static devclass_t util_devclass;
484 DRIVER_MODULE(hv_utils, vmbus, util_driver, util_devclass, hv_util_modevent, 0);
485 MODULE_VERSION(hv_utils, 1);
486 MODULE_DEPEND(hv_utils, vmbus, 1, 1, 1);