summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpirofti <>2020-07-06 13:33:06 +0000
committerpirofti <>2020-07-06 13:33:06 +0000
commit8f3f3991023702d597728be7e37b1e46b3ad7e46 (patch)
treeef9ff1387be3a6d8c07e9b6b6f87416080f68262
parentc09004d5abb7461b1cd8e9ffd0624c1185603fba (diff)
downloadopenbsd-8f3f3991023702d597728be7e37b1e46b3ad7e46.tar.gz
openbsd-8f3f3991023702d597728be7e37b1e46b3ad7e46.tar.bz2
openbsd-8f3f3991023702d597728be7e37b1e46b3ad7e46.zip
Add support for timeconting in userland.
This diff exposes parts of clock_gettime(2) and gettimeofday(2) to userland via libc eliberating processes from the need for a context switch everytime they want to count the passage of time. If a timecounter clock can be exposed to userland than it needs to set its tc_user member to a non-zero value. Tested with one or multiple counters per architecture. The timing data is shared through a pointer found in the new ELF auxiliary vector AUX_openbsd_timekeep containing timehands information that is frequently updated by the kernel. Timing differences between the last kernel update and the current time are adjusted in userland by the tc_get_timecount() function inside the MD usertc.c file. This permits a much more responsive environment, quite visible in browsers, office programs and gaming (apparently one is are able to fly in Minecraft now). Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others! OK from at least kettenis@, cheloha@, naddy@, sthen@
-rw-r--r--src/lib/libc/crypt/bcrypt.c6
-rw-r--r--src/lib/libc/net/res_random.c6
-rw-r--r--src/regress/lib/libc/timekeep/Makefile5
-rw-r--r--src/regress/lib/libc/timekeep/test_clock_gettime.c43
-rw-r--r--src/regress/lib/libc/timekeep/test_gettimeofday.c37
-rw-r--r--src/regress/lib/libc/timekeep/test_time_skew.c55
6 files changed, 146 insertions, 6 deletions
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c
index 82de8fa33b..ba45b104ed 100644
--- a/src/lib/libc/crypt/bcrypt.c
+++ b/src/lib/libc/crypt/bcrypt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bcrypt.c,v 1.57 2016/08/26 08:25:02 guenther Exp $ */ 1/* $OpenBSD: bcrypt.c,v 1.58 2020/07/06 13:33:05 pirofti Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> 4 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
@@ -248,9 +248,9 @@ _bcrypt_autorounds(void)
248 char buf[_PASSWORD_LEN]; 248 char buf[_PASSWORD_LEN];
249 int duration; 249 int duration;
250 250
251 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &before); 251 WRAP(clock_gettime)(CLOCK_THREAD_CPUTIME_ID, &before);
252 bcrypt_newhash("testpassword", r, buf, sizeof(buf)); 252 bcrypt_newhash("testpassword", r, buf, sizeof(buf));
253 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &after); 253 WRAP(clock_gettime)(CLOCK_THREAD_CPUTIME_ID, &after);
254 254
255 duration = after.tv_sec - before.tv_sec; 255 duration = after.tv_sec - before.tv_sec;
256 duration *= 1000000; 256 duration *= 1000000;
diff --git a/src/lib/libc/net/res_random.c b/src/lib/libc/net/res_random.c
index 763e420bb8..b7036815c9 100644
--- a/src/lib/libc/net/res_random.c
+++ b/src/lib/libc/net/res_random.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: res_random.c,v 1.24 2016/04/05 04:29:21 guenther Exp $ */ 1/* $OpenBSD: res_random.c,v 1.25 2020/07/06 13:33:06 pirofti Exp $ */
2 2
3/* 3/*
4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
@@ -219,7 +219,7 @@ res_initid(void)
219 if (ru_prf != NULL) 219 if (ru_prf != NULL)
220 arc4random_buf(ru_prf, sizeof(*ru_prf)); 220 arc4random_buf(ru_prf, sizeof(*ru_prf));
221 221
222 clock_gettime(CLOCK_MONOTONIC, &ts); 222 WRAP(clock_gettime)(CLOCK_MONOTONIC, &ts);
223 ru_reseed = ts.tv_sec + RU_OUT; 223 ru_reseed = ts.tv_sec + RU_OUT;
224 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; 224 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
225} 225}
@@ -232,7 +232,7 @@ __res_randomid(void)
232 u_int r; 232 u_int r;
233 static void *randomid_mutex; 233 static void *randomid_mutex;
234 234
235 clock_gettime(CLOCK_MONOTONIC, &ts); 235 WRAP(clock_gettime)(CLOCK_MONOTONIC, &ts);
236 pid = getpid(); 236 pid = getpid();
237 237
238 _MUTEX_LOCK(&randomid_mutex); 238 _MUTEX_LOCK(&randomid_mutex);
diff --git a/src/regress/lib/libc/timekeep/Makefile b/src/regress/lib/libc/timekeep/Makefile
new file mode 100644
index 0000000000..ed7dc60379
--- /dev/null
+++ b/src/regress/lib/libc/timekeep/Makefile
@@ -0,0 +1,5 @@
1# $OpenBSD: Makefile,v 1.1 2020/07/06 13:33:06 pirofti Exp $
2
3PROGS= test_clock_gettime test_time_skew test_gettimeofday
4
5.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/timekeep/test_clock_gettime.c b/src/regress/lib/libc/timekeep/test_clock_gettime.c
new file mode 100644
index 0000000000..cc1d8e36e3
--- /dev/null
+++ b/src/regress/lib/libc/timekeep/test_clock_gettime.c
@@ -0,0 +1,43 @@
1/* $OpenBSD: test_clock_gettime.c,v 1.1 2020/07/06 13:33:06 pirofti Exp $ */
2/*
3 * Copyright (c) 2020 Paul Irofti <paul@irofti.net>
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 <assert.h>
19#include <time.h>
20
21#define ASSERT_EQ(a, b) assert((a) == (b))
22
23void
24check()
25{
26 struct timespec tp = {0};
27
28 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &tp));
29 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &tp));
30 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &tp));
31 ASSERT_EQ(0, clock_gettime(CLOCK_UPTIME, &tp));
32
33
34 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp));
35 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp));
36
37}
38
39int main()
40{
41 check();
42 return 0;
43}
diff --git a/src/regress/lib/libc/timekeep/test_gettimeofday.c b/src/regress/lib/libc/timekeep/test_gettimeofday.c
new file mode 100644
index 0000000000..914058505d
--- /dev/null
+++ b/src/regress/lib/libc/timekeep/test_gettimeofday.c
@@ -0,0 +1,37 @@
1/* $OpenBSD: test_gettimeofday.c,v 1.1 2020/07/06 13:33:06 pirofti Exp $ */
2/*
3 * Copyright (c) 2020 Paul Irofti <paul@irofti.net>
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 <assert.h>
19#include <sys/time.h>
20
21#define ASSERT_EQ(a, b) assert((a) == (b))
22
23void
24check()
25{
26 struct timeval tv = {0};
27 struct timezone tzp;
28
29 ASSERT_EQ(0, gettimeofday(&tv, NULL));
30 ASSERT_EQ(0, gettimeofday(&tv, &tzp));
31}
32
33int main()
34{
35 check();
36 return 0;
37}
diff --git a/src/regress/lib/libc/timekeep/test_time_skew.c b/src/regress/lib/libc/timekeep/test_time_skew.c
new file mode 100644
index 0000000000..7871795f06
--- /dev/null
+++ b/src/regress/lib/libc/timekeep/test_time_skew.c
@@ -0,0 +1,55 @@
1/* $OpenBSD: test_time_skew.c,v 1.1 2020/07/06 13:33:06 pirofti Exp $ */
2/*
3 * Copyright (c) 2020 Paul Irofti <paul@irofti.net>
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/time.h>
19
20#include <assert.h>
21#include <time.h>
22#include <stdlib.h>
23#include <stdio.h>
24
25#define ASSERT_EQ(a, b) assert((a) == (b))
26#define ASSERT_NE(a, b) assert((a) != (b))
27
28void
29check()
30{
31 struct timespec tp1, tp2, tout;
32
33 tout.tv_sec = 0;
34 tout.tv_nsec = 100000;
35
36 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &tp1));
37
38 nanosleep(&tout, NULL);
39
40 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &tp2));
41
42 /* tp1 should never be larger than tp2 */
43 ASSERT_NE(1, timespeccmp(&tp1, &tp2, >));
44}
45
46int
47main(void)
48{
49 int i;
50
51 for (i = 0; i < 1000; i++)
52 check();
53
54 return 0;
55}