1*0d6cc87bSBrooks Davis /*-
2*0d6cc87bSBrooks Davis * Copyright (c) 2015 The FreeBSD Foundation
3*0d6cc87bSBrooks Davis *
4*0d6cc87bSBrooks Davis * This software was developed by Konstantin Belousov
5*0d6cc87bSBrooks Davis * under sponsorship from the FreeBSD Foundation.
6*0d6cc87bSBrooks Davis *
7*0d6cc87bSBrooks Davis * Redistribution and use in source and binary forms, with or without
8*0d6cc87bSBrooks Davis * modification, are permitted provided that the following conditions
9*0d6cc87bSBrooks Davis * are met:
10*0d6cc87bSBrooks Davis * 1. Redistributions of source code must retain the above copyright
11*0d6cc87bSBrooks Davis * notice, this list of conditions and the following disclaimer.
12*0d6cc87bSBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright
13*0d6cc87bSBrooks Davis * notice, this list of conditions and the following disclaimer in the
14*0d6cc87bSBrooks Davis * documentation and/or other materials provided with the distribution.
15*0d6cc87bSBrooks Davis *
16*0d6cc87bSBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0d6cc87bSBrooks Davis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0d6cc87bSBrooks Davis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0d6cc87bSBrooks Davis * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0d6cc87bSBrooks Davis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0d6cc87bSBrooks Davis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0d6cc87bSBrooks Davis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0d6cc87bSBrooks Davis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0d6cc87bSBrooks Davis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0d6cc87bSBrooks Davis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0d6cc87bSBrooks Davis * SUCH DAMAGE.
27*0d6cc87bSBrooks Davis */
28*0d6cc87bSBrooks Davis
29*0d6cc87bSBrooks Davis #include <sys/types.h>
30*0d6cc87bSBrooks Davis #include <sys/elf.h>
31*0d6cc87bSBrooks Davis #include <sys/time.h>
32*0d6cc87bSBrooks Davis #include <sys/vdso.h>
33*0d6cc87bSBrooks Davis #include <machine/cpufunc.h>
34*0d6cc87bSBrooks Davis #include <errno.h>
35*0d6cc87bSBrooks Davis #include "libc_private.h"
36*0d6cc87bSBrooks Davis
37*0d6cc87bSBrooks Davis static inline uint64_t
cp15_cntvct_get(void)38*0d6cc87bSBrooks Davis cp15_cntvct_get(void)
39*0d6cc87bSBrooks Davis {
40*0d6cc87bSBrooks Davis uint64_t reg;
41*0d6cc87bSBrooks Davis
42*0d6cc87bSBrooks Davis __asm __volatile("mrs %0, cntvct_el0" : "=r" (reg));
43*0d6cc87bSBrooks Davis return (reg);
44*0d6cc87bSBrooks Davis }
45*0d6cc87bSBrooks Davis
46*0d6cc87bSBrooks Davis static inline uint64_t
cp15_cntpct_get(void)47*0d6cc87bSBrooks Davis cp15_cntpct_get(void)
48*0d6cc87bSBrooks Davis {
49*0d6cc87bSBrooks Davis uint64_t reg;
50*0d6cc87bSBrooks Davis
51*0d6cc87bSBrooks Davis __asm __volatile("mrs %0, cntpct_el0" : "=r" (reg));
52*0d6cc87bSBrooks Davis return (reg);
53*0d6cc87bSBrooks Davis }
54*0d6cc87bSBrooks Davis
55*0d6cc87bSBrooks Davis #pragma weak __vdso_gettc
56*0d6cc87bSBrooks Davis int
__vdso_gettc(const struct vdso_timehands * th,u_int * tc)57*0d6cc87bSBrooks Davis __vdso_gettc(const struct vdso_timehands *th, u_int *tc)
58*0d6cc87bSBrooks Davis {
59*0d6cc87bSBrooks Davis
60*0d6cc87bSBrooks Davis if (th->th_algo != VDSO_TH_ALGO_ARM_GENTIM)
61*0d6cc87bSBrooks Davis return (ENOSYS);
62*0d6cc87bSBrooks Davis __asm __volatile("isb" : : : "memory");
63*0d6cc87bSBrooks Davis *tc = th->th_physical == 0 ? cp15_cntvct_get() : cp15_cntpct_get();
64*0d6cc87bSBrooks Davis return (0);
65*0d6cc87bSBrooks Davis }
66*0d6cc87bSBrooks Davis
67*0d6cc87bSBrooks Davis #pragma weak __vdso_gettimekeep
68*0d6cc87bSBrooks Davis int
__vdso_gettimekeep(struct vdso_timekeep ** tk)69*0d6cc87bSBrooks Davis __vdso_gettimekeep(struct vdso_timekeep **tk)
70*0d6cc87bSBrooks Davis {
71*0d6cc87bSBrooks Davis
72*0d6cc87bSBrooks Davis return (_elf_aux_info(AT_TIMEKEEP, tk, sizeof(*tk)));
73*0d6cc87bSBrooks Davis }
74