1 /* $OpenBSD: usertc.c,v 1.2 2020/07/08 09:20:28 kettenis Exp $ */
2 /*
3 * Copyright (c) 2020 Mark Kettenis <kettenis@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <sys/types.h>
19 #include <sys/timetc.h>
20
21 static inline u_int
tick_get_timecount(struct timecounter * tc)22 tick_get_timecount(struct timecounter *tc)
23 {
24 u_int64_t tick;
25
26 __asm volatile("rd %%tick, %0" : "=r" (tick));
27
28 return (tick & ~0u);
29 }
30
31 static inline u_int
sys_tick_get_timecount(struct timecounter * tc)32 sys_tick_get_timecount(struct timecounter *tc)
33 {
34 u_int64_t tick;
35
36 __asm volatile("rd %%sys_tick, %0" : "=r" (tick));
37
38 return (tick & ~0u);
39 }
40
41 static int
tc_get_timecount(struct timekeep * tk,u_int * tc)42 tc_get_timecount(struct timekeep *tk, u_int *tc)
43 {
44 switch (tk->tk_user) {
45 case TC_TICK:
46 *tc = tick_get_timecount(NULL);
47 return 0;
48 case TC_SYS_TICK:
49 *tc = sys_tick_get_timecount(NULL);
50 return 0;
51 }
52
53 return -1;
54 }
55
56 int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount;
57