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 * A common driver for all hyper-V util services.
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/reboot.h>
40 #include <sys/timetc.h>
41 #include <sys/syscallsubr.h>
43 #include <dev/hyperv/include/hyperv.h>
51 static void hv_shutdown_cb(void *context);
52 static void hv_heartbeat_cb(void *context);
53 static void hv_timesync_cb(void *context);
55 static int hv_timesync_init(hv_vmbus_service *serv);
58 * Note: GUID codes below are predefined by the host hypervisor
59 * (Hyper-V and Azure)interface and required for correct operation.
61 hv_vmbus_service service_table[] = {
62 /* Shutdown Service */
63 { .guid.data = {0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
64 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB},
65 .name = "Hyper-V Shutdown Service\n",
67 .callback = hv_shutdown_cb,
70 /* Time Synch Service */
71 { .guid.data = {0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
72 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf},
73 .name = "Hyper-V Time Synch Service\n",
75 .init = hv_timesync_init,
76 .callback = hv_timesync_cb,
79 /* Heartbeat Service */
80 { .guid.data = {0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
81 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d},
82 .name = "Hyper-V Heartbeat Service\n",
84 .callback = hv_heartbeat_cb,
89 * Receive buffer pointers. There is one buffer per utility service. The
90 * buffer is allocated during attach().
92 uint8_t *receive_buffer[HV_MAX_UTIL_SERVICES];
94 struct hv_ictimesync_data {
97 uint64_t roundtriptime;
102 hv_timesync_init(hv_vmbus_service *serv)
105 serv->work_queue = hv_work_queue_create("Time Sync");
106 if (serv->work_queue == NULL)
112 hv_negotiate_version(
113 struct hv_vmbus_icmsg_hdr* icmsghdrp,
114 struct hv_vmbus_icmsg_negotiate* negop,
117 icmsghdrp->icmsgsize = 0x10;
119 negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
120 sizeof(struct hv_vmbus_pipe_hdr) +
121 sizeof(struct hv_vmbus_icmsg_hdr)];
123 if (negop->icframe_vercnt >= 2 &&
124 negop->icversion_data[1].major == 3) {
125 negop->icversion_data[0].major = 3;
126 negop->icversion_data[0].minor = 0;
127 negop->icversion_data[1].major = 3;
128 negop->icversion_data[1].minor = 0;
130 negop->icversion_data[0].major = 1;
131 negop->icversion_data[0].minor = 0;
132 negop->icversion_data[1].major = 1;
133 negop->icversion_data[1].minor = 0;
136 negop->icframe_vercnt = 1;
137 negop->icmsg_vercnt = 1;
142 * Set host time based on time sync message from host
145 hv_set_host_time(void *context)
147 time_sync_data *time_msg = context;
148 uint64_t hosttime = time_msg->data;
149 struct timespec guest_ts, host_ts;
154 host_tns = (hosttime - HV_WLTIMEDELTA) * 100;
155 host_ts.tv_sec = (time_t)(host_tns/HV_NANO_SEC_PER_SEC);
156 host_ts.tv_nsec = (long)(host_tns%HV_NANO_SEC_PER_SEC);
160 diff = (int64_t)host_ts.tv_sec - (int64_t)guest_ts.tv_sec;
163 * If host differs by 5 seconds then make the guest catch up
165 if (diff > 5 || diff < -5) {
166 error = kern_clock_settime(curthread, CLOCK_REALTIME,
171 * Free the hosttime that was allocated in hv_adj_guesttime()
173 free(time_msg, M_DEVBUF);
177 * @brief Synchronize time with host after reboot, restore, etc.
179 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
180 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
181 * message after the timesync channel is opened. Since the hv_utils module is
182 * loaded after hv_vmbus, the first message is usually missed. The other
183 * thing is, systime is automatically set to emulated hardware clock which may
184 * not be UTC time or in the same time zone. So, to override these effects, we
185 * use the first 50 time samples for initial system time setting.
188 void hv_adj_guesttime(uint64_t hosttime, uint8_t flags)
190 time_sync_data* time_msg;
192 time_msg = malloc(sizeof(time_sync_data), M_DEVBUF, M_NOWAIT);
194 if (time_msg == NULL)
197 time_msg->data = hosttime;
199 if ((flags & HV_ICTIMESYNCFLAG_SYNC) != 0) {
200 hv_queue_work_item(service_table[HV_TIME_SYNCH].work_queue,
201 hv_set_host_time, time_msg);
202 } else if ((flags & HV_ICTIMESYNCFLAG_SAMPLE) != 0) {
203 hv_queue_work_item(service_table[HV_TIME_SYNCH].work_queue,
204 hv_set_host_time, time_msg);
206 free(time_msg, M_DEVBUF);
211 * Time Sync Channel message handler
214 hv_timesync_cb(void *context)
216 hv_vmbus_channel* channel = context;
217 hv_vmbus_icmsg_hdr* icmsghdrp;
222 struct hv_ictimesync_data* timedatap;
224 time_buf = receive_buffer[HV_TIME_SYNCH];
226 ret = hv_vmbus_channel_recv_packet(channel, time_buf,
227 PAGE_SIZE, &recvlen, &requestId);
229 if ((ret == 0) && recvlen > 0) {
230 icmsghdrp = (struct hv_vmbus_icmsg_hdr *) &time_buf[
231 sizeof(struct hv_vmbus_pipe_hdr)];
233 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
234 hv_negotiate_version(icmsghdrp, NULL, time_buf);
236 timedatap = (struct hv_ictimesync_data *) &time_buf[
237 sizeof(struct hv_vmbus_pipe_hdr) +
238 sizeof(struct hv_vmbus_icmsg_hdr)];
239 hv_adj_guesttime(timedatap->parenttime, timedatap->flags);
242 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION
243 | HV_ICMSGHDRFLAG_RESPONSE;
245 hv_vmbus_channel_send_packet(channel, time_buf,
247 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
255 hv_shutdown_cb(void *context)
258 hv_vmbus_channel* channel = context;
259 uint8_t execute_shutdown = 0;
260 hv_vmbus_icmsg_hdr* icmsghdrp;
264 hv_vmbus_shutdown_msg_data* shutdown_msg;
266 buf = receive_buffer[HV_SHUT_DOWN];
268 ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE,
269 &recv_len, &request_id);
271 if ((ret == 0) && recv_len > 0) {
273 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
274 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
276 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
277 hv_negotiate_version(icmsghdrp, NULL, buf);
281 (struct hv_vmbus_shutdown_msg_data *)
282 &buf[sizeof(struct hv_vmbus_pipe_hdr) +
283 sizeof(struct hv_vmbus_icmsg_hdr)];
285 switch (shutdown_msg->flags) {
288 icmsghdrp->status = HV_S_OK;
289 execute_shutdown = 1;
291 printf("Shutdown request received -"
292 " graceful shutdown initiated\n");
295 icmsghdrp->status = HV_E_FAIL;
296 execute_shutdown = 0;
297 printf("Shutdown request received -"
298 " Invalid request\n");
303 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
304 HV_ICMSGHDRFLAG_RESPONSE;
306 hv_vmbus_channel_send_packet(channel, buf,
307 recv_len, request_id,
308 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
311 if (execute_shutdown)
312 shutdown_nice(RB_POWEROFF);
316 * Process heartbeat message
319 hv_heartbeat_cb(void *context)
322 hv_vmbus_channel* channel = context;
327 struct hv_vmbus_heartbeat_msg_data* heartbeat_msg;
328 struct hv_vmbus_icmsg_hdr* icmsghdrp;
330 buf = receive_buffer[HV_HEART_BEAT];
332 ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE, &recvlen,
335 if ((ret == 0) && recvlen > 0) {
337 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
338 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
340 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
341 hv_negotiate_version(icmsghdrp, NULL, buf);
345 (struct hv_vmbus_heartbeat_msg_data *)
346 &buf[sizeof(struct hv_vmbus_pipe_hdr) +
347 sizeof(struct hv_vmbus_icmsg_hdr)];
349 heartbeat_msg->seq_num += 1;
352 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
353 HV_ICMSGHDRFLAG_RESPONSE;
355 hv_vmbus_channel_send_packet(channel, buf, recvlen, requestid,
356 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
362 hv_util_probe(device_t dev)
365 int rtn_value = ENXIO;
367 for (i = 0; i < HV_MAX_UTIL_SERVICES; i++) {
368 const char *p = vmbus_get_type(dev);
369 if (service_table[i].enabled && !memcmp(p, &service_table[i].guid, sizeof(hv_guid))) {
370 device_set_softc(dev, (void *) (&service_table[i]));
379 hv_util_attach(device_t dev)
381 struct hv_device* hv_dev;
382 struct hv_vmbus_service* service;
384 size_t receive_buffer_offset;
386 hv_dev = vmbus_get_devctx(dev);
387 service = device_get_softc(dev);
388 receive_buffer_offset = service - &service_table[0];
389 device_printf(dev, "Hyper-V Service attaching: %s\n", service->name);
390 receive_buffer[receive_buffer_offset] =
391 malloc(4 * PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
393 if (service->init != NULL) {
394 ret = service->init(service);
401 ret = hv_vmbus_channel_open(hv_dev->channel, 4 * PAGE_SIZE,
402 4 * PAGE_SIZE, NULL, 0,
403 service->callback, hv_dev->channel);
412 free(receive_buffer[receive_buffer_offset], M_DEVBUF);
413 receive_buffer[receive_buffer_offset] = NULL;
419 hv_util_detach(device_t dev)
421 struct hv_device* hv_dev;
422 struct hv_vmbus_service* service;
423 size_t receive_buffer_offset;
425 hv_dev = vmbus_get_devctx(dev);
427 hv_vmbus_channel_close(hv_dev->channel);
428 service = device_get_softc(dev);
429 receive_buffer_offset = service - &service_table[0];
431 if (service->work_queue != NULL)
432 hv_work_queue_close(service->work_queue);
434 free(receive_buffer[receive_buffer_offset], M_DEVBUF);
435 receive_buffer[receive_buffer_offset] = NULL;
440 static void hv_util_init(void)
444 static int hv_util_modevent(module_t mod, int event, void *arg)
457 static device_method_t util_methods[] = {
458 /* Device interface */
459 DEVMETHOD(device_probe, hv_util_probe),
460 DEVMETHOD(device_attach, hv_util_attach),
461 DEVMETHOD(device_detach, hv_util_detach),
462 DEVMETHOD(device_shutdown, bus_generic_shutdown),
466 static driver_t util_driver = { "hyperv-utils", util_methods, 0 };
468 static devclass_t util_devclass;
470 DRIVER_MODULE(hv_utils, vmbus, util_driver, util_devclass, hv_util_modevent, 0);
471 MODULE_VERSION(hv_utils, 1);
472 MODULE_DEPEND(hv_utils, vmbus, 1, 1, 1);
474 SYSINIT(hv_util_initx, SI_SUB_KTHREAD_IDLE, SI_ORDER_MIDDLE + 1,