On Sat, 30 May 2020 19:21:30 +0300
Paul Irofti <p...@irofti.net> wrote:

> Here is an updated diff with no libc bump.  Please use this one for
> further testing.

Your diff does amd64.
Here is a diff to add macppc.  Apply after your diff.

I have only tested clock_gettime(2) with CLOCK_REALTIME,
by doing loops in Ruby like, $ ruby27 -e '10000.times{p Time.now}'
The time increased steadily, and ktrace showed only a few system calls
to clock_gettime(2).

I copied ppc_mftb() from /sys/arch/powerpc/powerpc/cpu.h and renamed
it to tc_get_timecount_md(), because #include <machine/cpu.h> doesn't
provide ppc_mftb() if not _KERNEL.  It would be better to edit the
kernel headers to give ppc_mftb() if _LIBC, but I haven't done that.

I changed __amd64 to __amd64__ because I didn't find __powerpc.  I'm
not sure, but one might move the list of arches to dlfcn/Makefile.inc
and do -DTIMEKEEP, like how thread/Makefile.inc does -DFUTEX.  One
might drop the tc_get_timecount function pointer and just always call
the function #ifdef TIMEKEEP.

PowerPC Mac OS X had a userland gettimeofday(2) using the cpu's
timebase and a "common page" from the kernel.  Their common page also
had executable code for gettimeofday, memcpy, pthread_self, and a few
other functions.  --George

Index: lib/libc/arch/powerpc/gen/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/arch/powerpc/gen/Makefile.inc,v
retrieving revision 1.14
diff -u -p -r1.14 Makefile.inc
--- lib/libc/arch/powerpc/gen/Makefile.inc      12 Apr 2012 16:14:09 -0000      
1.14
+++ lib/libc/arch/powerpc/gen/Makefile.inc      31 May 2020 03:20:58 -0000
@@ -3,3 +3,4 @@ SRCS+= fabs.c
 SRCS+= fpgetmask.c fpsetmask.c
 SRCS+= fpgetround.c fpsetround.c
 SRCS+= fpgetsticky.c fpsetsticky.c
+SRCS+= usertc.c
--- /dev/null   Sat May 30 23:21:20 2020
+++ lib/libc/arch/powerpc/gen/usertc.c  Sat May 30 19:37:59 2020
@@ -0,0 +1,44 @@
+/*     $OpenBSD$ */
+/*
+ * Copyright (C) 1995, 1996 Wolfgang Solfrank.
+ * Copyright (C) 1995, 1996 TooLs GmbH.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by TooLs GmbH.
+ * 4. The name of TooLs GmbH may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+
+uint64_t
+tc_get_timecount_md(void)
+{
+       u_long scratch;
+       u_int64_t tb;
+
+       __asm volatile ("1: mftbu %0; mftb %L0; mftbu %1;"
+           " cmpw 0,%0,%1; bne 1b" : "=r"(tb), "=r"(scratch));
+       return tb;
+}
--- lib/libc/dlfcn/init.c.before        Sat May 30 23:26:35 2020
+++ lib/libc/dlfcn/init.c       Sat May 30 18:00:45 2020
@@ -70,7 +70,7 @@
 
 /* provide definitions for these */
 const dl_cb *_dl_cb __relro = NULL;
-#if defined(__amd64)
+#if defined(__amd64__) || defined(__powerpc__)
 uint64_t (*const tc_get_timecount)(void) = tc_get_timecount_md;
 #else
 uint64_t (*const tc_get_timecount)(void) = NULL;
--- sys/arch/macppc/macppc/clock.c.before       Sat May 30 23:28:00 2020
+++ sys/arch/macppc/macppc/clock.c      Sat May 30 20:35:47 2020
@@ -57,7 +57,7 @@
 static int32_t ticks_per_intr;
 
 static struct timecounter tb_timecounter = {
-       tb_get_timecount, NULL, 0x7fffffff, 0, "tb", 0, NULL, 0
+       tb_get_timecount, NULL, 0x7fffffff, 0, "tb", 0, NULL, 1
 };
 
 /* calibrate the timecounter frequency for the listed models */

Reply via email to