xref: /openbsd-src/lib/libc/arch/riscv64/gen/usertc.c (revision 82fa1b30dbd7fa64d08237720da3999618c34d6c)
1*82fa1b30Sjca /*	$OpenBSD: usertc.c,v 1.2 2021/07/24 22:41:09 jca Exp $	*/
27ecb72feSdrahn /*
37ecb72feSdrahn  * Copyright (c) 2020 Paul Irofti <paul@irofti.net>
4*82fa1b30Sjca  * Copyright (c) 2021 Jeremie Courreges-Anglas <jca@wxcvbn.org>
57ecb72feSdrahn  *
67ecb72feSdrahn  * Permission to use, copy, modify, and distribute this software for any
77ecb72feSdrahn  * purpose with or without fee is hereby granted, provided that the above
87ecb72feSdrahn  * copyright notice and this permission notice appear in all copies.
97ecb72feSdrahn  *
107ecb72feSdrahn  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
117ecb72feSdrahn  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
127ecb72feSdrahn  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
137ecb72feSdrahn  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
147ecb72feSdrahn  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
157ecb72feSdrahn  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
167ecb72feSdrahn  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
177ecb72feSdrahn  */
187ecb72feSdrahn 
197ecb72feSdrahn #include <sys/types.h>
207ecb72feSdrahn #include <sys/timetc.h>
217ecb72feSdrahn 
22*82fa1b30Sjca static inline u_int
rdtime(void)23*82fa1b30Sjca rdtime(void)
24*82fa1b30Sjca {
25*82fa1b30Sjca 	uint64_t ret;
26*82fa1b30Sjca 
27*82fa1b30Sjca 	__asm volatile("rdtime %0" : "=r"(ret));
28*82fa1b30Sjca 
29*82fa1b30Sjca 	return ret;
30*82fa1b30Sjca }
31*82fa1b30Sjca 
32*82fa1b30Sjca static int
tc_get_timecount(struct timekeep * tk,u_int * tc)33*82fa1b30Sjca tc_get_timecount(struct timekeep *tk, u_int *tc)
34*82fa1b30Sjca {
35*82fa1b30Sjca 	switch (tk->tk_user) {
36*82fa1b30Sjca 	case TC_TB:
37*82fa1b30Sjca 		*tc = rdtime();
38*82fa1b30Sjca 		return 0;
39*82fa1b30Sjca 	}
40*82fa1b30Sjca 
41*82fa1b30Sjca 	return -1;
42*82fa1b30Sjca }
43*82fa1b30Sjca 
44*82fa1b30Sjca int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount;
45