2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
5 * Copyright (c) 2016, 2017, 2019 The FreeBSD Foundation
6 * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org>
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 rdtsc_low(const struct vdso_timehands *th)
38 __asm __volatile("rdtsc; shrd %%cl, %%edx, %0"
39 : "=a" (rv) : "c" (th->th_x86_shift) : "edx");
44 rdtscp_low(const struct vdso_timehands *th)
48 __asm __volatile("rdtscp; movl %%edi,%%ecx; shrd %%cl, %%edx, %0"
49 : "=a" (rv) : "D" (th->th_x86_shift) : "ecx", "edx");
54 rdtsc_low_mb_lfence(const struct vdso_timehands *th)
57 return (rdtsc_low(th));
61 rdtsc_low_mb_mfence(const struct vdso_timehands *th)
64 return (rdtsc_low(th));
68 rdtsc_low_mb_none(const struct vdso_timehands *th)
70 return (rdtsc_low(th));
74 rdtsc32_mb_lfence(void)
81 rdtsc32_mb_mfence(void)
99 struct tsc_selector_tag {
100 u_int (*ts_rdtsc32)(void);
101 u_int (*ts_rdtsc_low)(const struct vdso_timehands *);
104 static const struct tsc_selector_tag tsc_selector[] = {
105 [0] = { /* Intel, LFENCE */
106 .ts_rdtsc32 = rdtsc32_mb_lfence,
107 .ts_rdtsc_low = rdtsc_low_mb_lfence,
109 [1] = { /* AMD, MFENCE */
110 .ts_rdtsc32 = rdtsc32_mb_mfence,
111 .ts_rdtsc_low = rdtsc_low_mb_mfence,
113 [2] = { /* No SSE2 */
114 .ts_rdtsc32 = rdtsc32_mb_none,
115 .ts_rdtsc_low = rdtsc_low_mb_none,
118 .ts_rdtsc32 = rdtscp32_,
119 .ts_rdtsc_low = rdtscp_low,
124 __vdso_gettc_rdtsc_low(const struct vdso_timehands *th)
127 return (tsc_selector[kern_tsc_selector].ts_rdtsc_low(th));
131 __vdso_gettc_rdtsc32(void)
134 return (tsc_selector[kern_tsc_selector].ts_rdtsc32());
138 __vdso_gettc(const struct vdso_timehands *th, u_int *tc)
141 switch (th->th_algo) {
142 case VDSO_TH_ALGO_X86_TSC:
143 *tc = th->th_x86_shift > 0 ? __vdso_gettc_rdtsc_low(th) :
144 __vdso_gettc_rdtsc32();
146 case VDSO_TH_ALGO_X86_HPET: