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>
36 #include <machine/cpufunc.h>
37 #include <machine/cpu.h>
38 #include <machine/atomic.h>
39 #include <machine/pvclock.h>
42 * Last time; this guarantees a monotonically increasing clock for when
43 * a stable TSC is not provided.
45 static volatile uint64_t pvclock_last_cycles;
51 atomic_store_rel_64(&pvclock_last_cycles, 0);
55 pvclock_get_last_cycles(void)
58 return (atomic_load_acq_64(&pvclock_last_cycles));
62 pvclock_tsc_freq(struct pvclock_vcpu_time_info *ti)
66 freq = (1000000000ULL << 32) / ti->tsc_to_system_mul;
68 if (ti->tsc_shift < 0)
69 freq <<= -ti->tsc_shift;
71 freq >>= ti->tsc_shift;
77 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
78 * yielding a 64-bit result.
80 static inline uint64_t
81 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?"
130 pvclock_get_nsec_offset(struct pvclock_vcpu_time_info *ti)
134 delta = rdtsc() - ti->tsc_timestamp;
136 return (pvclock_scale_delta(delta, ti->tsc_to_system_mul,
141 pvclock_read_time_info(struct pvclock_vcpu_time_info *ti,
142 uint64_t *cycles, uint8_t *flags)
147 version = ti->version;
149 *cycles = ti->system_time + pvclock_get_nsec_offset(ti);
152 } while ((ti->version & 1) != 0 || ti->version != version);
156 pvclock_read_wall_clock(struct pvclock_wall_clock *wc, uint32_t *sec,
162 version = wc->version;
167 } while ((wc->version & 1) != 0 || wc->version != version);
171 pvclock_get_timecount(struct pvclock_vcpu_time_info *ti)
176 pvclock_read_time_info(ti, &now, &flags);
178 if (flags & PVCLOCK_FLAG_TSC_STABLE)
182 * Enforce a monotonically increasing clock time across all VCPUs.
183 * If our time is too old, use the last time and return. Otherwise,
184 * try to update the last time.
187 last = atomic_load_acq_64(&pvclock_last_cycles);
190 } while (!atomic_cmpset_64(&pvclock_last_cycles, last, now));
196 pvclock_get_wallclock(struct pvclock_wall_clock *wc, struct timespec *ts)
200 pvclock_read_wall_clock(wc, &sec, &nsec);