> Date: Tue, 7 Jul 2020 23:46:00 +0200 (CEST) > From: Mark Kettenis <mark.kette...@xs4all.nl> > > > Date: Tue, 7 Jul 2020 23:13:06 +0200 > > From: Christian Weisgerber <na...@mips.inka.de> > > > > Mark Kettenis: > > > > > > --- lib/libc/arch/alpha/gen/usertc.c 6 Jul 2020 13:33:05 -0000 > > > > 1.1 > > > > +++ lib/libc/arch/alpha/gen/usertc.c 7 Jul 2020 20:40:37 -0000 > > > > > > +int > > > > +tc_get_timecount(struct timekeep *tk, u_int *tc) > > > > > > Need to make this function static to avoid namespace pollution. > > > > Then this needs to be fixed in amd64/gen/usertc.c pronto, before > > everybody copies it from there. > > Yeah, maybe people should hold off a bit. I have a few cleanups to > that code so it is best not to copy what is there now.
So here is a diff that cleans things up and implements sparc64 support. Showing both together since some of the amd64 changes were inspired by the sparc64 code. * TC_LAST can be removed; it really doesn't serve any purpose * the functions in usertc.c need to be static to avoid namespace pollution in libc.a. * I use a switch statement to simplify tc_get_timecount(). Architectures with only one supported timecounter could use an if statement like naddy@ did for alpha. That would be fine with me as well. ok? Index: sys/arch/sparc64/sparc64/clock.c =================================================================== RCS file: /cvs/src/sys/arch/sparc64/sparc64/clock.c,v retrieving revision 1.62 diff -u -p -r1.62 clock.c --- sys/arch/sparc64/sparc64/clock.c 6 Jul 2020 13:33:08 -0000 1.62 +++ sys/arch/sparc64/sparc64/clock.c 7 Jul 2020 23:29:48 -0000 @@ -109,13 +109,14 @@ struct cfdriver clock_cd = { u_int tick_get_timecount(struct timecounter *); struct timecounter tick_timecounter = { - tick_get_timecount, NULL, ~0u, 0, "tick", 0, NULL, 0 + tick_get_timecount, NULL, ~0u, 0, "tick", 0, NULL, TC_TICK }; u_int sys_tick_get_timecount(struct timecounter *); struct timecounter sys_tick_timecounter = { - sys_tick_get_timecount, NULL, ~0u, 0, "sys_tick", 1000, NULL, 0 + sys_tick_get_timecount, NULL, ~0u, 0, "sys_tick", 1000, NULL, + TC_SYS_TICK }; /* @@ -940,7 +941,7 @@ tick_get_timecount(struct timecounter *t { u_int64_t tick; - __asm volatile("rd %%tick, %0" : "=r" (tick) :); + __asm volatile("rd %%tick, %0" : "=r" (tick)); return (tick & ~0u); } @@ -950,7 +951,7 @@ sys_tick_get_timecount(struct timecounte { u_int64_t tick; - __asm volatile("rd %%sys_tick, %0" : "=r" (tick) :); + __asm volatile("rd %%sys_tick, %0" : "=r" (tick)); return (tick & ~0u); } Index: lib/libc/arch/amd64/gen/usertc.c =================================================================== RCS file: /cvs/src/lib/libc/arch/amd64/gen/usertc.c,v retrieving revision 1.1 diff -u -p -r1.1 usertc.c --- lib/libc/arch/amd64/gen/usertc.c 6 Jul 2020 13:33:05 -0000 1.1 +++ lib/libc/arch/amd64/gen/usertc.c 7 Jul 2020 23:29:48 -0000 @@ -26,16 +26,16 @@ rdtsc(void) return ((uint64_t)lo)|(((uint64_t)hi)<<32); } -int +static int tc_get_timecount(struct timekeep *tk, u_int *tc) { - int tk_user = tk->tk_user; + switch (tk->tk_user) { + case TC_TSC: + *tc = rdtsc(); + return 0; + } - if (tk_user < 1 || tk_user >= TC_LAST) - return -1; - - *tc = rdtsc(); - return 0; + return -1; } -int (*const _tc_get_timecount)(struct timekeep *tk, u_int *tc) - = tc_get_timecount; + +int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount; Index: lib/libc/arch/sparc64/gen/Makefile.inc =================================================================== RCS file: /cvs/src/lib/libc/arch/sparc64/gen/Makefile.inc,v retrieving revision 1.15 diff -u -p -r1.15 Makefile.inc --- lib/libc/arch/sparc64/gen/Makefile.inc 6 Jul 2020 13:33:05 -0000 1.15 +++ lib/libc/arch/sparc64/gen/Makefile.inc 7 Jul 2020 23:29:48 -0000 @@ -5,3 +5,5 @@ SRCS+= _setjmp.S fabs.S fixunsdfsi.S flt fpsetround.c fpsetsticky.c infinity.c isfinitel.c \ isinfl.c isnanl.c isnormall.c ldexp.c usertc.c modf.S \ mul.S nan.c setjmp.S signbitl.c sigsetjmp.S umul.S + +CFLAGS += -Wa,-Av9b Index: lib/libc/arch/sparc64/gen/usertc.c =================================================================== RCS file: /cvs/src/lib/libc/arch/sparc64/gen/usertc.c,v retrieving revision 1.1 diff -u -p -r1.1 usertc.c --- lib/libc/arch/sparc64/gen/usertc.c 6 Jul 2020 13:33:05 -0000 1.1 +++ lib/libc/arch/sparc64/gen/usertc.c 7 Jul 2020 23:29:48 -0000 @@ -1,6 +1,6 @@ /* $OpenBSD: usertc.c,v 1.1 2020/07/06 13:33:05 pirofti Exp $ */ /* - * Copyright (c) 2020 Paul Irofti <p...@irofti.net> + * Copyright (c) 2020 Mark Kettenis <kette...@openbsd.org> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,4 +18,39 @@ #include <sys/types.h> #include <sys/timetc.h> -int (*const _tc_get_timecount)(struct timekeep *, u_int *) = NULL; +static inline u_int +tick_get_timecount(struct timecounter *tc) +{ + u_int64_t tick; + + __asm volatile("rd %%tick, %0" : "=r" (tick)); + + return (tick & ~0u); +} + +static inline u_int +sys_tick_get_timecount(struct timecounter *tc) +{ + u_int64_t tick; + + __asm volatile("rd %%sys_tick, %0" : "=r" (tick)); + + return (tick & ~0u); +} + +static int +tc_get_timecount(struct timekeep *tk, u_int *tc) +{ + switch (tk->tk_user) { + case TC_TICK: + *tc = tick_get_timecount(NULL); + return 0; + case TC_SYS_TICK: + *tc = sys_tick_get_timecount(NULL); + return 0; + } + + return -1; +} + +int (*const _tc_get_timecount)(struct timekeep *, u_int *) = tc_get_timecount; Index: sys/arch/amd64/include/timetc.h =================================================================== RCS file: /cvs/src/sys/arch/amd64/include/timetc.h,v retrieving revision 1.1 diff -u -p -r1.1 timetc.h --- sys/arch/amd64/include/timetc.h 6 Jul 2020 13:33:06 -0000 1.1 +++ sys/arch/amd64/include/timetc.h 7 Jul 2020 23:29:49 -0000 @@ -18,7 +18,6 @@ #ifndef _MACHINE_TIMETC_H_ #define _MACHINE_TIMETC_H_ -#define TC_TSC 1 -#define TC_LAST 2 +#define TC_TSC 1 #endif /* _MACHINE_TIMETC_H_ */ Index: sys/arch/sparc64/include/timetc.h =================================================================== RCS file: /cvs/src/sys/arch/sparc64/include/timetc.h,v retrieving revision 1.1 diff -u -p -r1.1 timetc.h --- sys/arch/sparc64/include/timetc.h 6 Jul 2020 13:33:08 -0000 1.1 +++ sys/arch/sparc64/include/timetc.h 7 Jul 2020 23:29:49 -0000 @@ -18,6 +18,7 @@ #ifndef _MACHINE_TIMETC_H_ #define _MACHINE_TIMETC_H_ -#define TC_LAST 0 +#define TC_TICK 1 +#define TC_SYS_TICK 2 #endif /* _MACHINE_TIMETC_H_ */