2 * Copyright (c) 2014,2016 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>
43 #include <dev/hyperv/include/vmbus.h>
47 #define HV_WLTIMEDELTA 116444736000000000L /* in 100ns unit */
48 #define HV_ICTIMESYNCFLAG_PROBE 0
49 #define HV_ICTIMESYNCFLAG_SYNC 1
50 #define HV_ICTIMESYNCFLAG_SAMPLE 2
51 #define HV_NANO_SEC_PER_SEC 1000000000
58 /* Time Synch Service */
59 static const struct hyperv_guid service_guid = {.hv_guid =
60 {0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
61 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf } };
63 struct hv_ictimesync_data {
66 uint64_t roundtriptime;
70 typedef struct hv_timesync_sc {
73 time_sync_data time_msg;
77 * Set host time based on time sync message from host
80 hv_set_host_time(void *context, int pending)
82 hv_timesync_sc *softc = (hv_timesync_sc*)context;
83 uint64_t hosttime = softc->time_msg.data;
84 struct timespec guest_ts, host_ts;
89 host_tns = (hosttime - HV_WLTIMEDELTA) * 100;
90 host_ts.tv_sec = (time_t)(host_tns/HV_NANO_SEC_PER_SEC);
91 host_ts.tv_nsec = (long)(host_tns%HV_NANO_SEC_PER_SEC);
95 diff = (int64_t)host_ts.tv_sec - (int64_t)guest_ts.tv_sec;
98 * If host differs by 5 seconds then make the guest catch up
100 if (diff > 5 || diff < -5) {
101 error = kern_clock_settime(curthread, CLOCK_REALTIME,
107 * @brief Synchronize time with host after reboot, restore, etc.
109 * ICTIMESYNCFLAG_SYNC flag bit indicates reboot, restore events of the VM.
110 * After reboot the flag ICTIMESYNCFLAG_SYNC is included in the first time
111 * message after the timesync channel is opened. Since the hv_utils module is
112 * loaded after hv_vmbus, the first message is usually missed. The other
113 * thing is, systime is automatically set to emulated hardware clock which may
114 * not be UTC time or in the same time zone. So, to override these effects, we
115 * use the first 50 time samples for initial system time setting.
118 void hv_adj_guesttime(hv_timesync_sc *sc, uint64_t hosttime, uint8_t flags)
120 sc->time_msg.data = hosttime;
122 if (((flags & HV_ICTIMESYNCFLAG_SYNC) != 0) ||
123 ((flags & HV_ICTIMESYNCFLAG_SAMPLE) != 0)) {
124 taskqueue_enqueue(taskqueue_thread, &sc->task);
129 * Time Sync Channel message handler
132 hv_timesync_cb(void *context)
134 hv_vmbus_channel* channel;
135 hv_vmbus_icmsg_hdr* icmsghdrp;
140 struct hv_ictimesync_data* timedatap;
141 hv_timesync_sc *softc;
143 softc = (hv_timesync_sc*)context;
144 channel = softc->util_sc.channel;
145 time_buf = softc->util_sc.receive_buffer;
147 ret = hv_vmbus_channel_recv_packet(channel, time_buf,
148 PAGE_SIZE, &recvlen, &requestId);
150 if ((ret == 0) && recvlen > 0) {
151 icmsghdrp = (struct hv_vmbus_icmsg_hdr *) &time_buf[
152 sizeof(struct hv_vmbus_pipe_hdr)];
154 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
155 hv_negotiate_version(icmsghdrp, NULL, time_buf);
157 timedatap = (struct hv_ictimesync_data *) &time_buf[
158 sizeof(struct hv_vmbus_pipe_hdr) +
159 sizeof(struct hv_vmbus_icmsg_hdr)];
160 hv_adj_guesttime(softc, timedatap->parenttime, timedatap->flags);
163 icmsghdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION
164 | HV_ICMSGHDRFLAG_RESPONSE;
166 vmbus_chan_send(channel, VMBUS_CHANPKT_TYPE_INBAND, 0,
167 time_buf, recvlen, requestId);
172 hv_timesync_probe(device_t dev)
174 if (resource_disabled("hvtimesync", 0))
177 if (VMBUS_PROBE_GUID(device_get_parent(dev), dev, &service_guid) == 0) {
178 device_set_desc(dev, "Hyper-V Time Synch Service");
179 return BUS_PROBE_DEFAULT;
185 hv_timesync_attach(device_t dev)
187 hv_timesync_sc *softc = device_get_softc(dev);
189 softc->util_sc.callback = hv_timesync_cb;
190 TASK_INIT(&softc->task, 1, hv_set_host_time, softc);
192 return hv_util_attach(dev);
196 hv_timesync_detach(device_t dev)
198 hv_timesync_sc *softc = device_get_softc(dev);
199 taskqueue_drain(taskqueue_thread, &softc->task);
201 return hv_util_detach(dev);
204 static device_method_t timesync_methods[] = {
205 /* Device interface */
206 DEVMETHOD(device_probe, hv_timesync_probe),
207 DEVMETHOD(device_attach, hv_timesync_attach),
208 DEVMETHOD(device_detach, hv_timesync_detach),
212 static driver_t timesync_driver = { "hvtimesync", timesync_methods, sizeof(hv_timesync_sc)};
214 static devclass_t timesync_devclass;
216 DRIVER_MODULE(hv_timesync, vmbus, timesync_driver, timesync_devclass, NULL, NULL);
217 MODULE_VERSION(hv_timesync, 1);
218 MODULE_DEPEND(hv_timesync, vmbus, 1, 1, 1);