2 * Copyright (c) 2009 Adrian Chadd
3 * Copyright (c) 2012 Spectra Logic Corporation
4 * Copyright (c) 2014 Bryan Venteicher
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, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/clock.h>
35 #include <sys/limits.h>
38 #include <machine/atomic.h>
39 #include <machine/md_var.h>
40 #include <machine/pvclock.h>
43 * Last system time. This is used to guarantee a monotonically non-decreasing
44 * clock for the kernel codepath and approximate the same for the vDSO codepath.
45 * In theory, this should be unnecessary absent hypervisor bug(s) and/or what
46 * should be rare cases where TSC jitter may still be visible despite the
47 * hypervisor's best efforts.
49 static volatile uint64_t pvclock_last_systime;
51 static uint64_t pvclock_getsystime(struct pvclock *pvc);
52 static void pvclock_read_time_info(
53 struct pvclock_vcpu_time_info *ti, uint64_t *ns, uint8_t *flags);
54 static void pvclock_read_wall_clock(struct pvclock_wall_clock *wc,
56 static u_int pvclock_tc_get_timecount(struct timecounter *tc);
61 atomic_store_rel_64(&pvclock_last_systime, 0);
65 pvclock_tsc_freq(struct pvclock_vcpu_time_info *ti)
69 freq = (1000000000ULL << 32) / ti->tsc_to_system_mul;
70 if (ti->tsc_shift < 0)
71 freq <<= -ti->tsc_shift;
73 freq >>= ti->tsc_shift;
78 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
79 * yielding a 64-bit result.
81 static inline uint64_t
82 pvclock_scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
95 * For i386, the formula looks like:
97 * lower = (mul_frac * (delta & UINT_MAX)) >> 32
98 * upper = mul_frac * (delta >> 32)
99 * product = lower + upper
109 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
110 : "a" ((uint32_t)delta), "1" ((uint32_t)(delta >> 32)),
113 #elif defined(__amd64__)
118 "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
119 : [lo]"=a" (product), [hi]"=d" (tmp)
120 : "0" (delta), [mul_frac]"rm"((uint64_t)mul_frac));
123 #error "pvclock: unsupported x86 architecture?"
129 pvclock_read_time_info(struct pvclock_vcpu_time_info *ti,
130 uint64_t *ns, uint8_t *flags)
136 version = atomic_load_acq_32(&ti->version);
137 delta = rdtsc_ordered() - ti->tsc_timestamp;
138 *ns = ti->system_time + pvclock_scale_delta(delta,
139 ti->tsc_to_system_mul, ti->tsc_shift);
141 atomic_thread_fence_acq();
142 } while ((ti->version & 1) != 0 || ti->version != version);
146 pvclock_read_wall_clock(struct pvclock_wall_clock *wc, struct timespec *ts)
151 version = atomic_load_acq_32(&wc->version);
152 ts->tv_sec = wc->sec;
153 ts->tv_nsec = wc->nsec;
154 atomic_thread_fence_acq();
155 } while ((wc->version & 1) != 0 || wc->version != version);
159 pvclock_getsystime(struct pvclock *pvc)
161 uint64_t now, last, ret;
165 pvclock_read_time_info(&pvc->timeinfos[curcpu], &now, &flags);
167 if ((flags & PVCLOCK_FLAG_TSC_STABLE) == 0) {
168 last = atomic_load_acq_64(&pvclock_last_systime);
174 } while (!atomic_fcmpset_rel_64(&pvclock_last_systime, &last,
182 * NOTE: Transitional-only; this should be removed after 'dev/xen/timer/timer.c'
183 * has been migrated to the 'struct pvclock' API.
186 pvclock_get_timecount(struct pvclock_vcpu_time_info *ti)
188 uint64_t now, last, ret;
191 pvclock_read_time_info(ti, &now, &flags);
193 if ((flags & PVCLOCK_FLAG_TSC_STABLE) == 0) {
194 last = atomic_load_acq_64(&pvclock_last_systime);
200 } while (!atomic_fcmpset_rel_64(&pvclock_last_systime, &last,
207 * NOTE: Transitional-only; this should be removed after 'dev/xen/timer/timer.c'
208 * has been migrated to the 'struct pvclock' API.
211 pvclock_get_wallclock(struct pvclock_wall_clock *wc, struct timespec *ts)
213 pvclock_read_wall_clock(wc, ts);
217 pvclock_tc_get_timecount(struct timecounter *tc)
219 struct pvclock *pvc = tc->tc_priv;
221 return (pvclock_getsystime(pvc) & UINT_MAX);
225 pvclock_gettime(struct pvclock *pvc, struct timespec *ts)
227 struct timespec system_ts;
230 pvclock_read_wall_clock(pvc->get_wallclock(pvc->get_wallclock_arg), ts);
231 system_ns = pvclock_getsystime(pvc);
232 system_ts.tv_sec = system_ns / 1000000000ULL;
233 system_ts.tv_nsec = system_ns % 1000000000ULL;
234 timespecadd(ts, &system_ts, ts);
238 pvclock_init(struct pvclock *pvc, device_t dev, const char *tc_name,
239 int tc_quality, u_int tc_flags)
241 KASSERT(((uintptr_t)pvc->timeinfos & PAGE_MASK) == 0,
242 ("Specified time info page(s) address is not page-aligned."));
244 /* Set up timecounter and timecounter-supporting members: */
245 pvc->tc.tc_get_timecount = pvclock_tc_get_timecount;
246 pvc->tc.tc_poll_pps = NULL;
247 pvc->tc.tc_counter_mask = ~0U;
248 pvc->tc.tc_frequency = 1000000000ULL;
249 pvc->tc.tc_name = tc_name;
250 pvc->tc.tc_quality = tc_quality;
251 pvc->tc.tc_flags = tc_flags;
252 pvc->tc.tc_priv = pvc;
253 pvc->tc.tc_fill_vdso_timehands = NULL;
254 #ifdef COMPAT_FREEBSD32
255 pvc->tc.tc_fill_vdso_timehands32 = NULL;
258 /* Register timecounter: */
262 * Register wallclock:
263 * The RTC registration API expects a resolution in microseconds;
264 * pvclock's 1ns resolution is rounded up to 1us.
266 clock_register(dev, 1);
270 pvclock_destroy(struct pvclock *pvc)
273 * Not currently possible since there is no teardown counterpart of