]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_util.c
Revert r272128:
[FreeBSD/stable/10.git] / sys / dev / hyperv / utilities / hv_util.c
1 /*-
2  * Copyright (c) 2009-2012 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28  */
29
30 /**
31  * A common driver for all hyper-V util services.
32  */
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.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>
42
43 #include <dev/hyperv/include/hyperv.h>
44 #include "hv_kvp.h"
45
46 /* Time Sync data */
47 typedef struct {
48         uint64_t data;
49 } time_sync_data;
50
51 static void hv_shutdown_cb(void *context);
52 static void hv_heartbeat_cb(void *context);
53 static void hv_timesync_cb(void *context);
54
55 static int hv_timesync_init(hv_vmbus_service *serv);
56
57 /**
58  * Note: GUID codes below are predefined by the host hypervisor
59  * (Hyper-V and Azure)interface and required for correct operation.
60  */
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",
66           .enabled = TRUE,
67           .callback = hv_shutdown_cb,
68         },
69
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",
74           .enabled = TRUE,
75           .init = hv_timesync_init,
76           .callback = hv_timesync_cb,
77         },
78
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",
83           .enabled = TRUE,
84           .callback = hv_heartbeat_cb,
85         },
86 };
87
88 /*
89  * Receive buffer pointers. There is one buffer per utility service. The
90  * buffer is allocated during attach().
91  */
92 uint8_t *receive_buffer[HV_MAX_UTIL_SERVICES];
93
94 struct hv_ictimesync_data {
95         uint64_t    parenttime;
96         uint64_t    childtime;
97         uint64_t    roundtriptime;
98         uint8_t     flags;
99 } __packed;
100
101 static int
102 hv_timesync_init(hv_vmbus_service *serv)
103 {
104
105         serv->work_queue = hv_work_queue_create("Time Sync");
106         if (serv->work_queue == NULL)
107                 return (ENOMEM);
108         return (0);
109 }
110
111 static void
112 hv_negotiate_version(
113         struct hv_vmbus_icmsg_hdr*              icmsghdrp,
114         struct hv_vmbus_icmsg_negotiate*        negop,
115         uint8_t*                                buf)
116 {
117         icmsghdrp->icmsgsize = 0x10;
118
119         negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
120                 sizeof(struct hv_vmbus_pipe_hdr) +
121                 sizeof(struct hv_vmbus_icmsg_hdr)];
122
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;
129         } else {
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;
134         }
135
136         negop->icframe_vercnt = 1;
137         negop->icmsg_vercnt = 1;
138 }
139
140
141 /**
142  * Set host time based on time sync message from host
143  */
144 static void
145 hv_set_host_time(void *context)
146 {
147         time_sync_data *time_msg = context;     
148         uint64_t hosttime = time_msg->data;
149         struct timespec guest_ts, host_ts;
150         uint64_t host_tns;
151         int64_t diff;
152         int error;
153
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);
157
158         nanotime(&guest_ts);
159         
160         diff = (int64_t)host_ts.tv_sec - (int64_t)guest_ts.tv_sec;
161
162         /*
163          * If host differs by 5 seconds then make the guest catch up
164          */
165         if (diff > 5 || diff < -5) {
166                 error = kern_clock_settime(curthread, CLOCK_REALTIME,
167                     &host_ts);
168         } 
169
170         /*
171          * Free the hosttime that was allocated in hv_adj_guesttime()
172          */
173         free(time_msg, M_DEVBUF);
174 }
175
176 /**
177  * @brief Synchronize time with host after reboot, restore, etc.
178  *
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.
186  */
187 static inline
188 void hv_adj_guesttime(uint64_t hosttime, uint8_t flags)
189 {
190         time_sync_data* time_msg;
191
192         time_msg = malloc(sizeof(time_sync_data), M_DEVBUF, M_NOWAIT);
193
194         if (time_msg == NULL)
195                 return;
196         
197         time_msg->data = hosttime;
198
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);
205         } else {
206                 free(time_msg, M_DEVBUF);
207         }
208 }
209
210 /**
211  * Time Sync Channel message handler
212  */
213 static void
214 hv_timesync_cb(void *context)
215 {
216         hv_vmbus_channel*       channel = context;
217         hv_vmbus_icmsg_hdr*     icmsghdrp;
218         uint32_t                recvlen;
219         uint64_t                requestId;
220         int                     ret;
221         uint8_t*                time_buf;
222         struct hv_ictimesync_data* timedatap;
223
224         time_buf = receive_buffer[HV_TIME_SYNCH];
225
226         ret = hv_vmbus_channel_recv_packet(channel, time_buf,
227                                             PAGE_SIZE, &recvlen, &requestId);
228
229         if ((ret == 0) && recvlen > 0) {
230             icmsghdrp = (struct hv_vmbus_icmsg_hdr *) &time_buf[
231                 sizeof(struct hv_vmbus_pipe_hdr)];
232
233             if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
234                 hv_negotiate_version(icmsghdrp, NULL, time_buf);
235             } else {
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);
240             }
241
242             icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION
243                 | HV_ICMSGHDRFLAG_RESPONSE;
244
245             hv_vmbus_channel_send_packet(channel, time_buf,
246                 recvlen, requestId,
247                 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
248         }
249 }
250
251 /**
252  * Shutdown
253  */
254 static void
255 hv_shutdown_cb(void *context)
256 {
257         uint8_t*                        buf;
258         hv_vmbus_channel*               channel = context;
259         uint8_t                         execute_shutdown = 0;
260         hv_vmbus_icmsg_hdr*             icmsghdrp;
261         uint32_t                        recv_len;
262         uint64_t                        request_id;
263         int                             ret;
264         hv_vmbus_shutdown_msg_data*     shutdown_msg;
265
266         buf = receive_buffer[HV_SHUT_DOWN];
267
268         ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE,
269                                             &recv_len, &request_id);
270
271         if ((ret == 0) && recv_len > 0) {
272
273             icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
274                 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
275
276             if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
277                 hv_negotiate_version(icmsghdrp, NULL, buf);
278
279             } else {
280                 shutdown_msg =
281                     (struct hv_vmbus_shutdown_msg_data *)
282                     &buf[sizeof(struct hv_vmbus_pipe_hdr) +
283                         sizeof(struct hv_vmbus_icmsg_hdr)];
284
285                 switch (shutdown_msg->flags) {
286                     case 0:
287                     case 1:
288                         icmsghdrp->status = HV_S_OK;
289                         execute_shutdown = 1;
290                         if(bootverbose)
291                             printf("Shutdown request received -"
292                                     " graceful shutdown initiated\n");
293                         break;
294                     default:
295                         icmsghdrp->status = HV_E_FAIL;
296                         execute_shutdown = 0;
297                         printf("Shutdown request received -"
298                             " Invalid request\n");
299                         break;
300                     }
301             }
302
303             icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
304                                  HV_ICMSGHDRFLAG_RESPONSE;
305
306             hv_vmbus_channel_send_packet(channel, buf,
307                                         recv_len, request_id,
308                                         HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
309         }
310
311         if (execute_shutdown)
312             shutdown_nice(RB_POWEROFF);
313 }
314
315 /**
316  * Process heartbeat message
317  */
318 static void
319 hv_heartbeat_cb(void *context)
320 {
321         uint8_t*                buf;
322         hv_vmbus_channel*       channel = context;
323         uint32_t                recvlen;
324         uint64_t                requestid;
325         int                     ret;
326
327         struct hv_vmbus_heartbeat_msg_data*     heartbeat_msg;
328         struct hv_vmbus_icmsg_hdr*              icmsghdrp;
329
330         buf = receive_buffer[HV_HEART_BEAT];
331
332         ret = hv_vmbus_channel_recv_packet(channel, buf, PAGE_SIZE, &recvlen,
333                                             &requestid);
334
335         if ((ret == 0) && recvlen > 0) {
336
337             icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
338                 &buf[sizeof(struct hv_vmbus_pipe_hdr)];
339
340             if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
341                 hv_negotiate_version(icmsghdrp, NULL, buf);
342
343             } else {
344                 heartbeat_msg =
345                     (struct hv_vmbus_heartbeat_msg_data *)
346                         &buf[sizeof(struct hv_vmbus_pipe_hdr) +
347                              sizeof(struct hv_vmbus_icmsg_hdr)];
348
349                 heartbeat_msg->seq_num += 1;
350             }
351
352             icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
353                                  HV_ICMSGHDRFLAG_RESPONSE;
354
355             hv_vmbus_channel_send_packet(channel, buf, recvlen, requestid,
356                 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
357         }
358 }
359
360
361 static int
362 hv_util_probe(device_t dev)
363 {
364         int i;
365         int rtn_value = ENXIO;
366
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]));
371                 rtn_value = 0;
372             }
373         }
374
375         return rtn_value;
376 }
377
378 static int
379 hv_util_attach(device_t dev)
380 {
381         struct hv_device*               hv_dev;
382         struct hv_vmbus_service*        service;
383         int                             ret;
384         size_t                          receive_buffer_offset;
385
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);
392
393         if (service->init != NULL) {
394             ret = service->init(service);
395             if (ret) {
396                 ret = ENODEV;
397                 goto error0;
398             }
399         }
400
401         ret = hv_vmbus_channel_open(hv_dev->channel, 4 * PAGE_SIZE,
402                     4 * PAGE_SIZE, NULL, 0,
403                     service->callback, hv_dev->channel);
404
405         if (ret)
406             goto error0;
407
408         return (0);
409
410         error0:
411
412             free(receive_buffer[receive_buffer_offset], M_DEVBUF);
413             receive_buffer[receive_buffer_offset] = NULL;
414
415         return (ret);
416 }
417
418 static int
419 hv_util_detach(device_t dev)
420 {
421         struct hv_device*               hv_dev;
422         struct hv_vmbus_service*        service;
423         size_t                          receive_buffer_offset;
424
425         hv_dev = vmbus_get_devctx(dev);
426
427         hv_vmbus_channel_close(hv_dev->channel);
428         service = device_get_softc(dev);
429         receive_buffer_offset = service - &service_table[0];
430
431         if (service->work_queue != NULL)
432             hv_work_queue_close(service->work_queue);
433
434         free(receive_buffer[receive_buffer_offset], M_DEVBUF);
435         receive_buffer[receive_buffer_offset] = NULL;
436
437         return (0);
438 }
439
440 static void hv_util_init(void)
441 {
442 }
443
444 static int hv_util_modevent(module_t mod, int event, void *arg)
445 {
446         switch (event) {
447         case MOD_LOAD:
448                 break;
449         case MOD_UNLOAD:
450                 break;
451         default:
452                 break;
453         }
454         return (0);
455 }
456
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),
463         { 0, 0 } }
464 ;
465
466 static driver_t util_driver = { "hyperv-utils", util_methods, 0 };
467
468 static devclass_t util_devclass;
469
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);
473
474 SYSINIT(hv_util_initx, SI_SUB_KTHREAD_IDLE, SI_ORDER_MIDDLE + 1,
475         hv_util_init, NULL);