2 * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
33 #include <sys/timetc.h>
36 #define PVCLOCK_CDEVNAME "pvclock"
38 struct pvclock_vcpu_time_info {
41 uint64_t tsc_timestamp;
43 uint32_t tsc_to_system_mul;
49 #define PVCLOCK_FLAG_TSC_STABLE 0x01
50 #define PVCLOCK_FLAG_GUEST_PASUED 0x02
53 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
54 * yielding a 64-bit result.
56 static inline uint64_t
57 pvclock_scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
70 * For i386, the formula looks like:
72 * lower = (mul_frac * (delta & UINT_MAX)) >> 32
73 * upper = mul_frac * (delta >> 32)
74 * product = lower + upper
84 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
85 : "a" ((uint32_t)delta), "1" ((uint32_t)(delta >> 32)),
88 #elif defined(__amd64__)
93 "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
94 : [lo]"=a" (product), [hi]"=d" (tmp)
95 : "0" (delta), [mul_frac]"rm"((uint64_t)mul_frac));
98 #error "pvclock: unsupported x86 architecture?"
105 typedef struct pvclock_wall_clock *pvclock_get_wallclock_t(void *arg);
107 struct pvclock_wall_clock {
114 /* Public; initialized by the caller of 'pvclock_init()': */
115 pvclock_get_wallclock_t *get_wallclock;
116 void *get_wallclock_arg;
117 struct pvclock_vcpu_time_info *timeinfos;
118 bool stable_flag_supported;
120 /* Private; initialized by the 'pvclock' API: */
121 bool vdso_force_unstable;
122 bool vdso_enable_without_rdtscp;
123 struct timecounter tc;
128 * NOTE: 'pvclock_get_timecount()' and 'pvclock_get_wallclock()' are purely
129 * transitional; they should be removed after 'dev/xen/timer/timer.c' has been
130 * migrated to the 'struct pvclock' API.
132 void pvclock_resume(void);
133 uint64_t pvclock_tsc_freq(struct pvclock_vcpu_time_info *ti);
134 uint64_t pvclock_get_timecount(struct pvclock_vcpu_time_info *ti);
135 void pvclock_get_wallclock(struct pvclock_wall_clock *wc,
136 struct timespec *ts);
138 void pvclock_init(struct pvclock *pvc, device_t dev,
139 const char *tc_name, int tc_quality, u_int tc_flags);
140 void pvclock_gettime(struct pvclock *pvc, struct timespec *ts);
141 int pvclock_destroy(struct pvclock *pvc);