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);
57 * Note: GUID codes below are predefined by the host hypervisor
58 * (Hyper-V and Azure)interface and required for correct operation.
60 hv_vmbus_service service_table[] = {
61 /* Shutdown Service */
62 { .guid.data = {0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
63 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB},
64 .name = "Hyper-V Shutdown Service\n",
66 .callback = hv_shutdown_cb,
69 /* Time Synch Service */
70 { .guid.data = {0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
71 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf},
72 .name = "Hyper-V Time Synch Service\n",
74 .init = hv_timesync_init,
75 .callback = hv_timesync_cb,
78 /* Heartbeat Service */
79 { .guid.data = {0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
80 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d},
81 .name = "Hyper-V Heartbeat Service\n",
83 .callback = hv_heartbeat_cb,
86 /* KVP (Key Value Pair) Service */
87 { .guid.data = {0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
88 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6},
89 .name = "Hyper-V KVP Service\n",
92 .callback = hv_kvp_callback,
97 * Receive buffer pointers. There is one buffer per utility service. The
98 * buffer is allocated during attach().
100 uint8_t *receive_buffer[HV_MAX_UTIL_SERVICES];
102 static boolean_t destroyed_kvp = FALSE;
104 struct hv_ictimesync_data {
107 uint64_t roundtriptime;
112 hv_timesync_init(hv_vmbus_service *serv)
115 serv->work_queue = hv_work_queue_create("Time Sync");
116 if (serv->work_queue == NULL)
122 hv_negotiate_version(
123 struct hv_vmbus_icmsg_hdr* icmsghdrp,
124 struct hv_vmbus_icmsg_negotiate* negop,
127 icmsghdrp->icmsgsize = 0x10;
129 negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
130 sizeof(struct hv_vmbus_pipe_hdr) +
131 sizeof(struct hv_vmbus_icmsg_hdr)];
133 if (negop->icframe_vercnt >= 2 &&
134 negop->icversion_data[1].major == 3) {
135 negop->icversion_data[0].major = 3;
136 negop->icversion_data[0].minor = 0;
137 negop->icversion_data[1].major = 3;
138 negop->icversion_data[1].minor = 0;
140 negop->icversion_data[0].major = 1;
141 negop->icversion_data[0].minor = 0;
142 negop->icversion_data[1].major = 1;
143 negop->icversion_data[1].minor = 0;
146 negop->icframe_vercnt = 1;
147 negop->icmsg_vercnt = 1;
152 * Set host time based on time sync message from host
155 hv_set_host_time(void *context)
157 time_sync_data* time_msg = (time_sync_data*) context;
158 uint64_t hosttime = time_msg->data;
159 struct timespec guest_ts, host_ts;
164 host_tns = (hosttime - HV_WLTIMEDELTA) * 100;
165 host_ts.tv_sec = (time_t)(host_tns/HV_NANO_SEC_PER_SEC);
166 host_ts.tv_nsec = (long)(host_tns%HV_NANO_SEC_PER_SEC);
170 diff = (int64_t)host_ts.tv_sec - (int64_t)guest_ts.tv_sec;
173 * If host differs by 5 seconds then make the guest catch up
175 if (diff > 5 || diff < -5) {
176 error = kern_clock_settime(curthread, CLOCK_REALTIME,
181 * Free the hosttime that was allocated in hv_adj_guesttime()
183 free(time_msg, M_DEVBUF);
187 * @brief Synchronize time with host after reboot, restore, etc.
189 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
190 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
191 * message after the timesync channel is opened. Since the hv_utils module is
192 * loaded after hv_vmbus, the first message is usually missed. The other
193 * thing is, systime is automatically set to emulated hardware clock which may
194 * not be UTC time or in the same time zone. So, to override these effects, we
195 * use the first 50 time samples for initial system time setting.
198 void hv_adj_guesttime(uint64_t hosttime, uint8_t flags)
200 time_sync_data* time_msg;
202 time_msg = malloc(sizeof(time_sync_data), M_DEVBUF, M_NOWAIT);
204 if (time_msg == NULL)
207 time_msg->data = hosttime;
209 if ((flags & HV_ICTIMESYNCFLAG_SYNC) != 0) {
210 hv_queue_work_item(service_table[HV_TIME_SYNCH].work_queue,
211 hv_set_host_time, time_msg);
212 } else if ((flags & HV_ICTIMESYNCFLAG_SAMPLE) != 0) {
213 hv_queue_work_item(service_table[HV_TIME_SYNCH].work_queue,
214 hv_set_host_time, time_msg);
216 free(time_msg, M_DEVBUF);
221 * Time Sync Channel message handler
224 hv_timesync_cb(void *context)
226 hv_vmbus_channel* channel = context;
227 hv_vmbus_icmsg_hdr* icmsghdrp;
232 struct hv_ictimesync_data* timedatap;
234 time_buf = receive_buffer[HV_TIME_SYNCH];
236 ret = hv_vmbus_channel_recv_packet(channel, time_buf,
237 PAGE_SIZE, &recvlen, &requestId);
239 if ((ret == 0) && recvlen > 0) {
240 icmsghdrp = (struct hv_vmbus_icmsg_hdr *) &time_buf[
241 sizeof(struct hv_vmbus_pipe_hdr)];
243 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
244 hv_negotiate_version(icmsghdrp, NULL, time_buf);
246 timedatap = (struct hv_ictimesync_data *) &time_buf[
247 sizeof(struct hv_vmbus_pipe_hdr) +
248 sizeof(struct hv_vmbus_icmsg_hdr)];
249 hv_adj_guesttime(timedatap->parenttime, timedatap->flags);
252 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION
253 | HV_ICMSGHDRFLAG_RESPONSE;
255 hv_vmbus_channel_send_packet(channel, time_buf,
257 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
265 hv_shutdown_cb(void *context)
268 hv_vmbus_channel* channel = context;
269 uint8_t execute_shutdown = 0;
270 hv_vmbus_icmsg_hdr* icmsghdrp;
274 hv_vmbus_shutdown_msg_data* shutdown_msg;
276 buf = receive_buffer[HV_SHUT_DOWN];
278 ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE,
279 &recv_len, &request_id);
281 if ((ret == 0) && recv_len > 0) {
283 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
284 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
286 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
287 hv_negotiate_version(icmsghdrp, NULL, buf);
291 (struct hv_vmbus_shutdown_msg_data *)
292 &buf[sizeof(struct hv_vmbus_pipe_hdr) +
293 sizeof(struct hv_vmbus_icmsg_hdr)];
295 switch (shutdown_msg->flags) {
298 icmsghdrp->status = HV_S_OK;
299 execute_shutdown = 1;
301 printf("Shutdown request received -"
302 " graceful shutdown initiated\n");
305 icmsghdrp->status = HV_E_FAIL;
306 execute_shutdown = 0;
307 printf("Shutdown request received -"
308 " Invalid request\n");
313 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
314 HV_ICMSGHDRFLAG_RESPONSE;
316 hv_vmbus_channel_send_packet(channel, buf,
317 recv_len, request_id,
318 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
321 if (execute_shutdown)
322 shutdown_nice(RB_POWEROFF);
326 * Process heartbeat message
329 hv_heartbeat_cb(void *context)
332 hv_vmbus_channel* channel = context;
337 struct hv_vmbus_heartbeat_msg_data* heartbeat_msg;
338 struct hv_vmbus_icmsg_hdr* icmsghdrp;
340 buf = receive_buffer[HV_HEART_BEAT];
342 ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE, &recvlen,
345 if ((ret == 0) && recvlen > 0) {
347 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
348 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
350 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
351 hv_negotiate_version(icmsghdrp, NULL, buf);
355 (struct hv_vmbus_heartbeat_msg_data *)
356 &buf[sizeof(struct hv_vmbus_pipe_hdr) +
357 sizeof(struct hv_vmbus_icmsg_hdr)];
359 heartbeat_msg->seq_num += 1;
362 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
363 HV_ICMSGHDRFLAG_RESPONSE;
365 hv_vmbus_channel_send_packet(channel, buf, recvlen, requestid,
366 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
372 hv_util_probe(device_t dev)
375 int rtn_value = ENXIO;
377 for (i = 0; i < HV_MAX_UTIL_SERVICES; i++) {
378 const char *p = vmbus_get_type(dev);
379 if (service_table[i].enabled && !memcmp(p, &service_table[i].guid, sizeof(hv_guid))) {
380 device_set_softc(dev, (void *) (&service_table[i]));
381 rtn_value = BUS_PROBE_DEFAULT;
389 hv_util_attach(device_t dev)
391 struct hv_device* hv_dev;
392 struct hv_vmbus_service* service;
394 size_t receive_buffer_offset;
396 hv_dev = vmbus_get_devctx(dev);
397 service = device_get_softc(dev);
398 receive_buffer_offset = service - &service_table[0];
399 device_printf(dev, "Hyper-V Service attaching: %s\n", service->name);
400 receive_buffer[receive_buffer_offset] =
401 malloc(4 * PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
403 if (service->init != NULL) {
404 ret = service->init(service);
411 ret = hv_vmbus_channel_open(hv_dev->channel, 4 * PAGE_SIZE,
412 4 * PAGE_SIZE, NULL, 0,
413 service->callback, hv_dev->channel);
422 free(receive_buffer[receive_buffer_offset], M_DEVBUF);
423 receive_buffer[receive_buffer_offset] = NULL;
429 hv_util_detach(device_t dev)
431 struct hv_device* hv_dev;
432 struct hv_vmbus_service* service;
433 size_t receive_buffer_offset;
435 if (!destroyed_kvp) {
437 destroyed_kvp = TRUE;
440 hv_dev = vmbus_get_devctx(dev);
442 hv_vmbus_channel_close(hv_dev->channel);
443 service = device_get_softc(dev);
444 receive_buffer_offset = service - &service_table[0];
446 if (service->work_queue != NULL)
447 hv_work_queue_close(service->work_queue);
449 free(receive_buffer[receive_buffer_offset], M_DEVBUF);
450 receive_buffer[receive_buffer_offset] = NULL;
460 hv_util_modevent(module_t mod, int event, void *arg)
473 static device_method_t util_methods[] = {
474 /* Device interface */
475 DEVMETHOD(device_probe, hv_util_probe),
476 DEVMETHOD(device_attach, hv_util_attach),
477 DEVMETHOD(device_detach, hv_util_detach),
478 DEVMETHOD(device_shutdown, bus_generic_shutdown),
482 static driver_t util_driver = { "hyperv-utils", util_methods, 0 };
484 static devclass_t util_devclass;
486 DRIVER_MODULE(hv_utils, vmbus, util_driver, util_devclass, hv_util_modevent, 0);
487 MODULE_VERSION(hv_utils, 1);
488 MODULE_DEPEND(hv_utils, vmbus, 1, 1, 1);
490 SYSINIT(hv_util_initx, SI_SUB_KTHREAD_IDLE, SI_ORDER_MIDDLE + 1,