]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_timesync.c
MFC 295919,295958,295964
[FreeBSD/stable/10.git] / sys / dev / hyperv / utilities / hv_timesync.c
1 /*-
2  * Copyright (c) 2014 Microsoft Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * A common driver for all hyper-V util services.
31  */
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.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>
41
42 #include <dev/hyperv/include/hyperv.h>
43 #include "hv_util.h"
44
45 #define HV_WLTIMEDELTA              116444736000000000L     /* in 100ns unit */
46 #define HV_ICTIMESYNCFLAG_PROBE     0
47 #define HV_ICTIMESYNCFLAG_SYNC      1
48 #define HV_ICTIMESYNCFLAG_SAMPLE    2
49 #define HV_NANO_SEC_PER_SEC         1000000000
50
51 /* Time Sync data */
52 typedef struct {
53         uint64_t data;
54 } time_sync_data;
55
56         /* Time Synch Service */
57 static hv_guid service_guid = {.data =
58         {0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
59         0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf } };
60
61 struct hv_ictimesync_data {
62         uint64_t    parenttime;
63         uint64_t    childtime;
64         uint64_t    roundtriptime;
65         uint8_t     flags;
66 } __packed;
67
68 typedef struct hv_timesync_sc {
69         hv_util_sc      util_sc;
70         struct task     task;
71         time_sync_data  time_msg;
72 } hv_timesync_sc;
73
74 /**
75  * Set host time based on time sync message from host
76  */
77 static void
78 hv_set_host_time(void *context, int pending)
79 {
80         hv_timesync_sc *softc = (hv_timesync_sc*)context;
81         uint64_t hosttime = softc->time_msg.data;
82         struct timespec guest_ts, host_ts;
83         uint64_t host_tns;
84         int64_t diff;
85         int error;
86
87         host_tns = (hosttime - HV_WLTIMEDELTA) * 100;
88         host_ts.tv_sec = (time_t)(host_tns/HV_NANO_SEC_PER_SEC);
89         host_ts.tv_nsec = (long)(host_tns%HV_NANO_SEC_PER_SEC);
90
91         nanotime(&guest_ts);
92
93         diff = (int64_t)host_ts.tv_sec - (int64_t)guest_ts.tv_sec;
94
95         /*
96          * If host differs by 5 seconds then make the guest catch up
97          */
98         if (diff > 5 || diff < -5) {
99                 error = kern_clock_settime(curthread, CLOCK_REALTIME,
100                     &host_ts);
101         }
102 }
103
104 /**
105  * @brief Synchronize time with host after reboot, restore, etc.
106  *
107  * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
108  * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
109  * message after the timesync channel is opened. Since the hv_utils module is
110  * loaded after hv_vmbus, the first message is usually missed. The other
111  * thing is, systime is automatically set to emulated hardware clock which may
112  * not be UTC time or in the same time zone. So, to override these effects, we
113  * use the first 50 time samples for initial system time setting.
114  */
115 static inline
116 void hv_adj_guesttime(hv_timesync_sc *sc, uint64_t hosttime, uint8_t flags)
117 {
118         sc->time_msg.data = hosttime;
119
120         if (((flags & HV_ICTIMESYNCFLAG_SYNC) != 0) ||
121                 ((flags & HV_ICTIMESYNCFLAG_SAMPLE) != 0)) {
122                 taskqueue_enqueue(taskqueue_thread, &sc->task);
123         }
124 }
125
126 /**
127  * Time Sync Channel message handler
128  */
129 static void
130 hv_timesync_cb(void *context)
131 {
132         hv_vmbus_channel*       channel;
133         hv_vmbus_icmsg_hdr*     icmsghdrp;
134         uint32_t                recvlen;
135         uint64_t                requestId;
136         int                     ret;
137         uint8_t*                time_buf;
138         struct hv_ictimesync_data* timedatap;
139         hv_timesync_sc          *softc;
140
141         softc = (hv_timesync_sc*)context;
142         channel = softc->util_sc.hv_dev->channel;
143         time_buf = softc->util_sc.receive_buffer;
144
145         ret = hv_vmbus_channel_recv_packet(channel, time_buf,
146                 PAGE_SIZE, &recvlen, &requestId);
147
148         if ((ret == 0) && recvlen > 0) {
149             icmsghdrp = (struct hv_vmbus_icmsg_hdr *) &time_buf[
150                 sizeof(struct hv_vmbus_pipe_hdr)];
151
152             if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
153                 hv_negotiate_version(icmsghdrp, NULL, time_buf);
154             } else {
155                 timedatap = (struct hv_ictimesync_data *) &time_buf[
156                     sizeof(struct hv_vmbus_pipe_hdr) +
157                         sizeof(struct hv_vmbus_icmsg_hdr)];
158                 hv_adj_guesttime(softc, timedatap->parenttime, timedatap->flags);
159             }
160
161             icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION
162                 | HV_ICMSGHDRFLAG_RESPONSE;
163
164             hv_vmbus_channel_send_packet(channel, time_buf,
165                 recvlen, requestId,
166                 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
167         }
168 }
169
170 static int
171 hv_timesync_probe(device_t dev)
172 {
173         const char *p = vmbus_get_type(dev);
174         if (!memcmp(p, &service_guid, sizeof(hv_guid))) {
175                 device_set_desc(dev, "Hyper-V Time Synch Service");
176                 return BUS_PROBE_DEFAULT;
177         }
178
179         return ENXIO;
180 }
181
182 static int
183 hv_timesync_attach(device_t dev)
184 {
185         hv_timesync_sc *softc = device_get_softc(dev);
186
187         softc->util_sc.callback = hv_timesync_cb;
188         TASK_INIT(&softc->task, 1, hv_set_host_time, softc);
189
190         return hv_util_attach(dev);
191 }
192
193 static int
194 hv_timesync_detach(device_t dev)
195 {
196         hv_timesync_sc *softc = device_get_softc(dev);
197         taskqueue_drain(taskqueue_thread, &softc->task);
198
199         return hv_util_detach(dev);
200 }
201
202 static device_method_t timesync_methods[] = {
203         /* Device interface */
204         DEVMETHOD(device_probe, hv_timesync_probe),
205         DEVMETHOD(device_attach, hv_timesync_attach),
206         DEVMETHOD(device_detach, hv_timesync_detach),
207         { 0, 0 }
208 };
209
210 static driver_t timesync_driver = { "hvtimesync", timesync_methods, sizeof(hv_timesync_sc)};
211
212 static devclass_t timesync_devclass;
213
214 DRIVER_MODULE(hv_timesync, vmbus, timesync_driver, timesync_devclass, NULL, NULL);
215 MODULE_VERSION(hv_timesync, 1);
216 MODULE_DEPEND(hv_timesync, vmbus, 1, 1, 1);