1 /* $NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2001 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Emmanuel Dreyfus.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $");
40 #include <sys/param.h>
41 #include <sys/limits.h>
43 #include <sys/mutex.h>
45 #include <sys/resourcevar.h>
46 #include <sys/syscallsubr.h>
50 #include <machine/../linux32/linux.h>
51 #include <machine/../linux32/linux32_proto.h>
53 #include <machine/../linux/linux.h>
54 #include <machine/../linux/linux_proto.h>
57 #include <compat/linux/linux_dtrace.h>
58 #include <compat/linux/linux_misc.h>
59 #include <compat/linux/linux_time.h>
60 #include <compat/linux/linux_util.h>
63 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
66 * DTrace probes in this module.
68 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
70 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
72 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_gettime, conversion_error, "int");
73 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
74 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
75 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
76 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime64, gettime_error, "int");
77 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime64, copyout_error, "int");
79 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
80 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_settime, settime_error, "int");
81 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_settime, conversion_error, "int");
82 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
83 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
84 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime64, conversion_error, "int");
85 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime64, copyin_error, "int");
87 LIN_SDT_PROBE_DEFINE0(time, linux_common_clock_getres, nullcall);
88 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_getres, conversion_error, "int");
89 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_getres, getres_error, "int");
90 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
91 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
92 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres_time64, copyout_error, "int");
94 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
95 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
96 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
97 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
98 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_nanosleep, unsupported_flags, "int");
99 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_nanosleep, unsupported_clockid, "int");
100 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
101 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep_time64, copyout_error, "int");
102 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep_time64, copyin_error, "int");
105 static int linux_common_clock_gettime(struct thread *, clockid_t,
107 static int linux_common_clock_settime(struct thread *, clockid_t,
109 static int linux_common_clock_getres(struct thread *, clockid_t,
111 static int linux_common_clock_nanosleep(struct thread *, clockid_t,
112 l_int, struct timespec *, struct timespec *);
115 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
118 #ifdef COMPAT_LINUX32
119 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
122 ltp->tv_sec = ntp->tv_sec;
123 ltp->tv_nsec = ntp->tv_nsec;
129 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
132 if (!timespecvalid_interval(ltp))
134 ntp->tv_sec = ltp->tv_sec;
135 ntp->tv_nsec = ltp->tv_nsec;
141 linux_put_timespec(struct timespec *ntp, struct l_timespec *ltp)
143 struct l_timespec lts;
146 error = native_to_linux_timespec(<s, ntp);
149 return (copyout(<s, ltp, sizeof(lts)));
153 linux_get_timespec(struct timespec *ntp, const struct l_timespec *ultp)
155 struct l_timespec lts;
158 error = copyin(ultp, <s, sizeof(lts));
161 return (linux_to_native_timespec(ntp, <s));
164 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
166 native_to_linux_timespec64(struct l_timespec64 *ltp64, struct timespec *ntp)
169 ltp64->tv_sec = ntp->tv_sec;
170 ltp64->tv_nsec = ntp->tv_nsec;
176 linux_to_native_timespec64(struct timespec *ntp, struct l_timespec64 *ltp64)
179 #if defined(__i386__)
180 /* i386 time_t is still 32-bit */
181 if (ltp64->tv_sec > INT_MAX || ltp64->tv_sec < INT_MIN)
184 /* Zero out the padding in compat mode. */
185 ntp->tv_nsec = ltp64->tv_nsec & 0xFFFFFFFFUL;
186 ntp->tv_sec = ltp64->tv_sec;
188 if (!timespecvalid_interval(ntp))
195 linux_put_timespec64(struct timespec *ntp, struct l_timespec64 *ltp)
197 struct l_timespec64 lts;
200 error = native_to_linux_timespec64(<s, ntp);
203 return (copyout(<s, ltp, sizeof(lts)));
207 linux_get_timespec64(struct timespec *ntp, const struct l_timespec64 *ultp)
209 struct l_timespec64 lts;
212 error = copyin(ultp, <s, sizeof(lts));
215 return (linux_to_native_timespec64(ntp, <s));
220 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
224 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
226 error = native_to_linux_timespec(<p->it_value, &ntp->it_value);
231 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
235 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
237 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
241 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
243 linux_to_native_itimerspec64(struct itimerspec *ntp, struct l_itimerspec64 *ltp)
247 error = linux_to_native_timespec64(&ntp->it_interval, <p->it_interval);
249 error = linux_to_native_timespec64(&ntp->it_value, <p->it_value);
254 native_to_linux_itimerspec64(struct l_itimerspec64 *ltp, struct itimerspec *ntp)
258 error = native_to_linux_timespec64(<p->it_interval, &ntp->it_interval);
260 error = native_to_linux_timespec64(<p->it_value, &ntp->it_value);
266 linux_to_native_clockid(clockid_t *n, clockid_t l)
271 if (LINUX_CPUCLOCK_WHICH(l) == LINUX_CLOCKFD) {
272 LIN_SDT_PROBE1(time, linux_to_native_clockid,
273 unsupported_clockid, l);
276 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD_MASK)
279 if (LINUX_CPUCLOCK_PERTHREAD(l))
280 *n = CLOCK_THREAD_CPUTIME_ID;
282 *n = CLOCK_PROCESS_CPUTIME_ID;
287 case LINUX_CLOCK_REALTIME:
290 case LINUX_CLOCK_MONOTONIC:
291 *n = CLOCK_MONOTONIC;
293 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
294 *n = CLOCK_PROCESS_CPUTIME_ID;
296 case LINUX_CLOCK_THREAD_CPUTIME_ID:
297 *n = CLOCK_THREAD_CPUTIME_ID;
299 case LINUX_CLOCK_REALTIME_COARSE:
300 *n = CLOCK_REALTIME_FAST;
302 case LINUX_CLOCK_MONOTONIC_COARSE:
303 case LINUX_CLOCK_MONOTONIC_RAW:
304 *n = CLOCK_MONOTONIC_FAST;
306 case LINUX_CLOCK_BOOTTIME:
309 case LINUX_CLOCK_REALTIME_ALARM:
310 case LINUX_CLOCK_BOOTTIME_ALARM:
311 case LINUX_CLOCK_SGI_CYCLE:
312 case LINUX_CLOCK_TAI:
313 LIN_SDT_PROBE1(time, linux_to_native_clockid,
314 unsupported_clockid, l);
317 LIN_SDT_PROBE1(time, linux_to_native_clockid,
326 linux_to_native_timerflags(int *nflags, int flags)
329 if (flags & ~LINUX_TIMER_ABSTIME)
332 if (flags & LINUX_TIMER_ABSTIME)
333 *nflags |= TIMER_ABSTIME;
338 linux_common_clock_gettime(struct thread *td, clockid_t which,
342 struct thread *targettd;
344 int error, clockwhich;
349 error = linux_to_native_clockid(&nwhich, which);
352 "unsupported clock_gettime clockid %d", which);
353 LIN_SDT_PROBE1(time, linux_common_clock_gettime,
354 conversion_error, error);
359 case CLOCK_PROCESS_CPUTIME_ID:
361 clockwhich = LINUX_CPUCLOCK_WHICH(which);
362 pid = LINUX_CPUCLOCK_ID(which);
364 clockwhich = LINUX_CPUCLOCK_SCHED;
371 error = pget(pid, PGET_CANSEE, &p);
375 switch (clockwhich) {
376 case LINUX_CPUCLOCK_PROF:
378 calcru(p, &ru.ru_utime, &ru.ru_stime);
381 timevaladd(&ru.ru_utime, &ru.ru_stime);
382 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
384 case LINUX_CPUCLOCK_VIRT:
386 calcru(p, &ru.ru_utime, &ru.ru_stime);
389 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
391 case LINUX_CPUCLOCK_SCHED:
392 kern_process_cputime(p, tp);
402 case CLOCK_THREAD_CPUTIME_ID:
404 clockwhich = LINUX_CPUCLOCK_WHICH(which);
405 tid = LINUX_CPUCLOCK_ID(which);
407 clockwhich = LINUX_CPUCLOCK_SCHED;
415 targettd = linux_tdfind(td, tid, p->p_pid);
416 if (targettd == NULL)
419 switch (clockwhich) {
420 case LINUX_CPUCLOCK_PROF:
422 thread_lock(targettd);
423 rufetchtd(targettd, &ru);
424 thread_unlock(targettd);
427 timevaladd(&ru.ru_utime, &ru.ru_stime);
428 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
430 case LINUX_CPUCLOCK_VIRT:
432 thread_lock(targettd);
433 rufetchtd(targettd, &ru);
434 thread_unlock(targettd);
437 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
439 case LINUX_CPUCLOCK_SCHED:
442 kern_thread_cputime(targettd, tp);
452 error = kern_clock_gettime(td, nwhich, tp);
460 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
465 error = linux_common_clock_gettime(td, args->which, &tp);
467 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
470 error = linux_put_timespec(&tp, args->tp);
472 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
477 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
479 linux_clock_gettime64(struct thread *td, struct linux_clock_gettime64_args *args)
484 error = linux_common_clock_gettime(td, args->which, &tp);
486 LIN_SDT_PROBE1(time, linux_clock_gettime64, gettime_error, error);
489 error = linux_put_timespec64(&tp, args->tp);
491 LIN_SDT_PROBE1(time, linux_clock_gettime64, copyout_error, error);
498 linux_common_clock_settime(struct thread *td, clockid_t which,
504 error = linux_to_native_clockid(&nwhich, which);
507 "unsupported clock_settime clockid %d", which);
508 LIN_SDT_PROBE1(time, linux_common_clock_settime, conversion_error,
513 error = kern_clock_settime(td, nwhich, ts);
515 LIN_SDT_PROBE1(time, linux_common_clock_settime,
516 settime_error, error);
522 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
527 error = linux_get_timespec(&ts, args->tp);
529 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
532 return (linux_common_clock_settime(td, args->which, &ts));
535 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
537 linux_clock_settime64(struct thread *td, struct linux_clock_settime64_args *args)
542 error = linux_get_timespec64(&ts, args->tp);
544 LIN_SDT_PROBE1(time, linux_clock_settime64, copyin_error, error);
547 return (linux_common_clock_settime(td, args->which, &ts));
552 linux_common_clock_getres(struct thread *td, clockid_t which,
556 int error, clockwhich;
561 error = linux_to_native_clockid(&nwhich, which);
564 "unsupported clock_getres clockid %d", which);
565 LIN_SDT_PROBE1(time, linux_common_clock_getres,
566 conversion_error, error);
571 * Check user supplied clock id in case of per-process
572 * or thread-specific cpu-time clock.
576 case CLOCK_THREAD_CPUTIME_ID:
577 tid = LINUX_CPUCLOCK_ID(which);
580 if (linux_tdfind(td, tid, p->p_pid) == NULL)
585 case CLOCK_PROCESS_CPUTIME_ID:
586 pid = LINUX_CPUCLOCK_ID(which);
588 error = pget(pid, PGET_CANSEE, &p);
598 LIN_SDT_PROBE0(time, linux_common_clock_getres, nullcall);
603 case CLOCK_THREAD_CPUTIME_ID:
604 case CLOCK_PROCESS_CPUTIME_ID:
605 clockwhich = LINUX_CPUCLOCK_WHICH(which);
607 * In both cases (when the clock id obtained by a call to
608 * clock_getcpuclockid() or using the clock
609 * ID CLOCK_PROCESS_CPUTIME_ID Linux hardcodes precision
610 * of clock. The same for the CLOCK_THREAD_CPUTIME_ID clock.
612 * See Linux posix_cpu_clock_getres() implementation.
614 if (which > 0 || clockwhich == LINUX_CPUCLOCK_SCHED) {
620 switch (clockwhich) {
621 case LINUX_CPUCLOCK_PROF:
624 case LINUX_CPUCLOCK_VIRT:
625 nwhich = CLOCK_VIRTUAL;
635 error = kern_clock_getres(td, nwhich, ts);
637 LIN_SDT_PROBE1(time, linux_common_clock_getres,
638 getres_error, error);
647 linux_clock_getres(struct thread *td,
648 struct linux_clock_getres_args *args)
653 error = linux_common_clock_getres(td, args->which, &ts);
654 if (error != 0 || args->tp == NULL)
656 error = linux_put_timespec(&ts, args->tp);
658 LIN_SDT_PROBE1(time, linux_clock_getres,
659 copyout_error, error);
663 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
665 linux_clock_getres_time64(struct thread *td,
666 struct linux_clock_getres_time64_args *args)
671 error = linux_common_clock_getres(td, args->which, &ts);
672 if (error != 0 || args->tp == NULL)
674 error = linux_put_timespec64(&ts, args->tp);
676 LIN_SDT_PROBE1(time, linux_clock_getres_time64,
677 copyout_error, error);
683 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
685 struct timespec *rmtp;
686 struct timespec rqts, rmts;
689 error = linux_get_timespec(&rqts, args->rqtp);
691 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
694 if (args->rmtp != NULL)
699 error = kern_nanosleep(td, &rqts, rmtp);
700 if (error == EINTR && args->rmtp != NULL) {
701 error2 = linux_put_timespec(rmtp, args->rmtp);
703 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
713 linux_common_clock_nanosleep(struct thread *td, clockid_t which,
714 l_int lflags, struct timespec *rqtp, struct timespec *rmtp)
719 error = linux_to_native_timerflags(&flags, lflags);
721 LIN_SDT_PROBE1(time, linux_common_clock_nanosleep,
722 unsupported_flags, lflags);
726 error = linux_to_native_clockid(&clockid, which);
729 "unsupported clock_nanosleep clockid %d", which);
730 LIN_SDT_PROBE1(time, linux_common_clock_nanosleep,
731 unsupported_clockid, which);
734 if (clockid == CLOCK_THREAD_CPUTIME_ID)
737 return (kern_clock_nanosleep(td, clockid, flags, rqtp, rmtp));
741 linux_clock_nanosleep(struct thread *td,
742 struct linux_clock_nanosleep_args *args)
744 struct timespec *rmtp;
745 struct timespec rqts, rmts;
748 error = linux_get_timespec(&rqts, args->rqtp);
750 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
754 if (args->rmtp != NULL)
759 error = linux_common_clock_nanosleep(td, args->which, args->flags,
761 if (error == EINTR && (args->flags & LINUX_TIMER_ABSTIME) == 0 &&
762 args->rmtp != NULL) {
763 error2 = linux_put_timespec(rmtp, args->rmtp);
765 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
766 copyout_error, error2);
773 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
775 linux_clock_nanosleep_time64(struct thread *td,
776 struct linux_clock_nanosleep_time64_args *args)
778 struct timespec *rmtp;
779 struct timespec rqts, rmts;
782 error = linux_get_timespec64(&rqts, args->rqtp);
784 LIN_SDT_PROBE1(time, linux_clock_nanosleep_time64,
785 copyin_error, error);
788 if (args->rmtp != NULL)
793 error = linux_common_clock_nanosleep(td, args->which, args->flags,
795 if (error == EINTR && (args->flags & LINUX_TIMER_ABSTIME) == 0 &&
796 args->rmtp != NULL) {
797 error2 = linux_put_timespec64(rmtp, args->rmtp);
799 LIN_SDT_PROBE1(time, linux_clock_nanosleep_time64,
800 copyout_error, error2);