xref: /openbsd/lib/libc/arch/mips64/gen/usertc.c (revision d415bd75)
1 /*	$OpenBSD: usertc.c,v 1.3 2021/07/25 22:58:39 jca Exp $	*/
2 /*
3  * Copyright (c) 2020 Visa Hankala
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
22 get_cp0_count(void)
23 {
24 	uint32_t count;
25 
26 	__asm volatile (
27 	"	.set	push\n"
28 	"	.set	mips64r2\n"
29 	"	rdhwr	%0, $2\n"
30 	"	.set	pop\n"
31 	: "=r" (count));
32 
33 	return count;
34 }
35 
36 static int
37 tc_get_timecount(struct timekeep *tk, u_int *tc)
38 {
39 	switch (tk->tk_user) {
40 	case TC_CP0_COUNT:
41 		*tc = get_cp0_count();
42 		return 0;
43 	}
44 
45 	return -1;
46 }
47 
48 int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount;
49