]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/pvclock.c
pvclock: Add 'struct pvclock' API
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / pvclock.c
1 /*-
2  * Copyright (c) 2009 Adrian Chadd
3  * Copyright (c) 2012 Spectra Logic Corporation
4  * Copyright (c) 2014 Bryan Venteicher
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/clock.h>
35 #include <sys/limits.h>
36 #include <sys/proc.h>
37
38 #include <machine/atomic.h>
39 #include <machine/md_var.h>
40 #include <machine/pvclock.h>
41
42 /*
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.
48  */
49 static volatile uint64_t pvclock_last_systime;
50
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,
55     struct timespec *ts);
56 static u_int             pvclock_tc_get_timecount(struct timecounter *tc);
57
58 void
59 pvclock_resume(void)
60 {
61         atomic_store_rel_64(&pvclock_last_systime, 0);
62 }
63
64 uint64_t
65 pvclock_tsc_freq(struct pvclock_vcpu_time_info *ti)
66 {
67         uint64_t freq;
68
69         freq = (1000000000ULL << 32) / ti->tsc_to_system_mul;
70         if (ti->tsc_shift < 0)
71                 freq <<= -ti->tsc_shift;
72         else
73                 freq >>= ti->tsc_shift;
74         return (freq);
75 }
76
77 /*
78  * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
79  * yielding a 64-bit result.
80  */
81 static inline uint64_t
82 pvclock_scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
83 {
84         uint64_t product;
85
86         if (shift < 0)
87                 delta >>= -shift;
88         else
89                 delta <<= shift;
90 #if defined(__i386__)
91         {
92                 uint32_t tmp1, tmp2;
93
94                 /**
95                  * For i386, the formula looks like:
96                  *
97                  *   lower = (mul_frac * (delta & UINT_MAX)) >> 32
98                  *   upper = mul_frac * (delta >> 32)
99                  *   product = lower + upper
100                  */
101                 __asm__ (
102                         "mul  %5       ; "
103                         "mov  %4,%%eax ; "
104                         "mov  %%edx,%4 ; "
105                         "mul  %5       ; "
106                         "xor  %5,%5    ; "
107                         "add  %4,%%eax ; "
108                         "adc  %5,%%edx ; "
109                         : "=A" (product), "=r" (tmp1), "=r" (tmp2)
110                         : "a" ((uint32_t)delta), "1" ((uint32_t)(delta >> 32)),
111                           "2" (mul_frac) );
112         }
113 #elif defined(__amd64__)
114         {
115                 unsigned long tmp;
116
117                 __asm__ (
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));
121         }
122 #else
123 #error "pvclock: unsupported x86 architecture?"
124 #endif
125         return (product);
126 }
127
128 static void
129 pvclock_read_time_info(struct pvclock_vcpu_time_info *ti,
130     uint64_t *ns, uint8_t *flags)
131 {
132         uint64_t delta;
133         uint32_t version;
134
135         do {
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);
140                 *flags = ti->flags;
141                 atomic_thread_fence_acq();
142         } while ((ti->version & 1) != 0 || ti->version != version);
143 }
144
145 static void
146 pvclock_read_wall_clock(struct pvclock_wall_clock *wc, struct timespec *ts)
147 {
148         uint32_t version;
149
150         do {
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);
156 }
157
158 static uint64_t
159 pvclock_getsystime(struct pvclock *pvc)
160 {
161         uint64_t now, last, ret;
162         uint8_t flags;
163
164         critical_enter();
165         pvclock_read_time_info(&pvc->timeinfos[curcpu], &now, &flags);
166         ret = now;
167         if ((flags & PVCLOCK_FLAG_TSC_STABLE) == 0) {
168                 last = atomic_load_acq_64(&pvclock_last_systime);
169                 do {
170                         if (last > now) {
171                                 ret = last;
172                                 break;
173                         }
174                 } while (!atomic_fcmpset_rel_64(&pvclock_last_systime, &last,
175                     now));
176         }
177         critical_exit();
178         return (ret);
179 }
180
181 /*
182  * NOTE: Transitional-only; this should be removed after 'dev/xen/timer/timer.c'
183  * has been migrated to the 'struct pvclock' API.
184  */
185 uint64_t
186 pvclock_get_timecount(struct pvclock_vcpu_time_info *ti)
187 {
188         uint64_t now, last, ret;
189         uint8_t flags;
190
191         pvclock_read_time_info(ti, &now, &flags);
192         ret = now;
193         if ((flags & PVCLOCK_FLAG_TSC_STABLE) == 0) {
194                 last = atomic_load_acq_64(&pvclock_last_systime);
195                 do {
196                         if (last > now) {
197                                 ret = last;
198                                 break;
199                         }
200                 } while (!atomic_fcmpset_rel_64(&pvclock_last_systime, &last,
201                     now));
202         }
203         return (ret);
204 }
205
206 /*
207  * NOTE: Transitional-only; this should be removed after 'dev/xen/timer/timer.c'
208  * has been migrated to the 'struct pvclock' API.
209  */
210 void
211 pvclock_get_wallclock(struct pvclock_wall_clock *wc, struct timespec *ts)
212 {
213         pvclock_read_wall_clock(wc, ts);
214 }
215
216 static u_int
217 pvclock_tc_get_timecount(struct timecounter *tc)
218 {
219         struct pvclock *pvc = tc->tc_priv;
220
221         return (pvclock_getsystime(pvc) & UINT_MAX);
222 }
223
224 void
225 pvclock_gettime(struct pvclock *pvc, struct timespec *ts)
226 {
227         struct timespec system_ts;
228         uint64_t system_ns;
229
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);
235 }
236
237 void
238 pvclock_init(struct pvclock *pvc, device_t dev, const char *tc_name,
239     int tc_quality, u_int tc_flags)
240 {
241         KASSERT(((uintptr_t)pvc->timeinfos & PAGE_MASK) == 0,
242             ("Specified time info page(s) address is not page-aligned."));
243
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;
256 #endif
257
258         /* Register timecounter: */
259         tc_init(&pvc->tc);
260
261         /*
262          * Register wallclock:
263          *     The RTC registration API expects a resolution in microseconds;
264          *     pvclock's 1ns resolution is rounded up to 1us.
265          */
266         clock_register(dev, 1);
267 }
268
269 int
270 pvclock_destroy(struct pvclock *pvc)
271 {
272         /*
273          * Not currently possible since there is no teardown counterpart of
274          * 'tc_init()'.
275          */
276         return (EBUSY);
277 }