1 /* $OpenBSD: usertc.c,v 1.2 2021/07/24 22:41:09 jca Exp $ */
2 /*
3 * Copyright (c) 2020 Paul Irofti <paul@irofti.net>
4 * Copyright (c) 2021 Jeremie Courreges-Anglas <jca@wxcvbn.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <sys/timetc.h>
21
22 static inline u_int
rdtime(void)23 rdtime(void)
24 {
25 uint64_t ret;
26
27 __asm volatile("rdtime %0" : "=r"(ret));
28
29 return ret;
30 }
31
32 static int
tc_get_timecount(struct timekeep * tk,u_int * tc)33 tc_get_timecount(struct timekeep *tk, u_int *tc)
34 {
35 switch (tk->tk_user) {
36 case TC_TB:
37 *tc = rdtime();
38 return 0;
39 }
40
41 return -1;
42 }
43
44 int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount;
45