1*78617c59Sjca /* $OpenBSD: usertc.c,v 1.3 2021/07/25 22:58:39 jca Exp $ */
2d82e6535Spirofti /*
3757e7193Svisa * Copyright (c) 2020 Visa Hankala
4d82e6535Spirofti *
5d82e6535Spirofti * Permission to use, copy, modify, and distribute this software for any
6d82e6535Spirofti * purpose with or without fee is hereby granted, provided that the above
7d82e6535Spirofti * copyright notice and this permission notice appear in all copies.
8d82e6535Spirofti *
9d82e6535Spirofti * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10d82e6535Spirofti * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11d82e6535Spirofti * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12d82e6535Spirofti * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13d82e6535Spirofti * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14d82e6535Spirofti * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15d82e6535Spirofti * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16d82e6535Spirofti */
17d82e6535Spirofti
18d82e6535Spirofti #include <sys/types.h>
19d82e6535Spirofti #include <sys/timetc.h>
20d82e6535Spirofti
21757e7193Svisa static inline u_int
get_cp0_count(void)22757e7193Svisa get_cp0_count(void)
23757e7193Svisa {
24757e7193Svisa uint32_t count;
25757e7193Svisa
26*78617c59Sjca __asm volatile (
27757e7193Svisa " .set push\n"
28757e7193Svisa " .set mips64r2\n"
29757e7193Svisa " rdhwr %0, $2\n"
30757e7193Svisa " .set pop\n"
31757e7193Svisa : "=r" (count));
32757e7193Svisa
33757e7193Svisa return count;
34757e7193Svisa }
35757e7193Svisa
36757e7193Svisa static int
tc_get_timecount(struct timekeep * tk,u_int * tc)37757e7193Svisa tc_get_timecount(struct timekeep *tk, u_int *tc)
38757e7193Svisa {
39757e7193Svisa switch (tk->tk_user) {
40757e7193Svisa case TC_CP0_COUNT:
41757e7193Svisa *tc = get_cp0_count();
42757e7193Svisa return 0;
43757e7193Svisa }
44757e7193Svisa
45757e7193Svisa return -1;
46757e7193Svisa }
47757e7193Svisa
48757e7193Svisa int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount;
49