__vdso_gettc.c revision 237434
1230775Sjfv/*-
2230775Sjfv * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
3320897Serj *
4230775Sjfv * Redistribution and use in source and binary forms, with or without
5320897Serj * modification, are permitted provided that the following conditions
6320897Serj * are met:
7230775Sjfv * 1. Redistributions of source code must retain the above copyright
8320897Serj *    notice, this list of conditions and the following disclaimer.
9320897Serj * 2. Redistributions in binary form must reproduce the above copyright
10230775Sjfv *    notice, this list of conditions and the following disclaimer in the
11320897Serj *    documentation and/or other materials provided with the distribution.
12320897Serj *
13320897Serj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14230775Sjfv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15320897Serj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16320897Serj * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17320897Serj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18230775Sjfv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19320897Serj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20230775Sjfv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21320897Serj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22320897Serj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23320897Serj * SUCH DAMAGE.
24320897Serj */
25320897Serj
26320897Serj#include <sys/cdefs.h>
27320897Serj__FBSDID("$FreeBSD: head/lib/libc/amd64/sys/__vdso_gettc.c 237434 2012-06-22 07:13:30Z kib $");
28320897Serj
29230775Sjfv#include <sys/types.h>
30230775Sjfv#include <sys/time.h>
31230775Sjfv#include <sys/vdso.h>
32230775Sjfv#include <machine/cpufunc.h>
33230775Sjfv
34230775Sjfvstatic u_int
35230775Sjfv__vdso_gettc_low(const struct vdso_timehands *th)
36230775Sjfv{
37230775Sjfv	uint32_t rv;
38230775Sjfv
39230775Sjfv	__asm __volatile("rdtsc; shrd %%cl, %%edx, %0"
40230775Sjfv	    : "=a" (rv) : "c" (th->th_x86_shift) : "edx");
41230775Sjfv	return (rv);
42230775Sjfv}
43230775Sjfv
44280182Sjfvu_int
45280182Sjfv__vdso_gettc(const struct vdso_timehands *th)
46230775Sjfv{
47247822Sjfv
48230775Sjfv	return (th->th_x86_shift > 0 ? __vdso_gettc_low(th) : rdtsc32());
49230775Sjfv}
50230775Sjfv