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>
36 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $");
39 #include <sys/param.h>
40 #include <sys/limits.h>
42 #include <sys/mutex.h>
44 #include <sys/resourcevar.h>
45 #include <sys/syscallsubr.h>
49 #include <machine/../linux32/linux.h>
50 #include <machine/../linux32/linux32_proto.h>
52 #include <machine/../linux/linux.h>
53 #include <machine/../linux/linux_proto.h>
56 #include <compat/linux/linux_dtrace.h>
57 #include <compat/linux/linux_misc.h>
58 #include <compat/linux/linux_time.h>
59 #include <compat/linux/linux_util.h>
62 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
65 * DTrace probes in this module.
67 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
69 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
71 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_gettime, conversion_error, "int");
72 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
73 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
74 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
75 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime64, gettime_error, "int");
76 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime64, copyout_error, "int");
78 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
79 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_settime, settime_error, "int");
80 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_settime, conversion_error, "int");
81 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
82 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
83 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime64, conversion_error, "int");
84 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime64, copyin_error, "int");
86 LIN_SDT_PROBE_DEFINE0(time, linux_common_clock_getres, nullcall);
87 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_getres, conversion_error, "int");
88 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_getres, getres_error, "int");
89 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
90 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
91 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres_time64, copyout_error, "int");
93 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
94 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
95 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
96 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
97 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_nanosleep, unsupported_flags, "int");
98 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_nanosleep, unsupported_clockid, "int");
99 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
100 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep_time64, copyout_error, "int");
101 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep_time64, copyin_error, "int");
104 static int linux_common_clock_gettime(struct thread *, clockid_t,
106 static int linux_common_clock_settime(struct thread *, clockid_t,
108 static int linux_common_clock_getres(struct thread *, clockid_t,
110 static int linux_common_clock_nanosleep(struct thread *, clockid_t,
111 l_int, struct timespec *, struct timespec *);
114 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
117 #ifdef COMPAT_LINUX32
118 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
121 ltp->tv_sec = ntp->tv_sec;
122 ltp->tv_nsec = ntp->tv_nsec;
128 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
131 if (!timespecvalid_interval(ltp))
133 ntp->tv_sec = ltp->tv_sec;
134 ntp->tv_nsec = ltp->tv_nsec;
140 linux_put_timespec(struct timespec *ntp, struct l_timespec *ltp)
142 struct l_timespec lts;
145 error = native_to_linux_timespec(<s, ntp);
148 return (copyout(<s, ltp, sizeof(lts)));
152 linux_get_timespec(struct timespec *ntp, const struct l_timespec *ultp)
154 struct l_timespec lts;
157 error = copyin(ultp, <s, sizeof(lts));
160 return (linux_to_native_timespec(ntp, <s));
163 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
165 native_to_linux_timespec64(struct l_timespec64 *ltp64, struct timespec *ntp)
168 ltp64->tv_sec = ntp->tv_sec;
169 ltp64->tv_nsec = ntp->tv_nsec;
175 linux_to_native_timespec64(struct timespec *ntp, struct l_timespec64 *ltp64)
178 #if defined(__i386__)
179 /* i386 time_t is still 32-bit */
180 if (ltp64->tv_sec > INT_MAX || ltp64->tv_sec < INT_MIN)
183 /* Zero out the padding in compat mode. */
184 ntp->tv_nsec = ltp64->tv_nsec & 0xFFFFFFFFUL;
185 ntp->tv_sec = ltp64->tv_sec;
187 if (!timespecvalid_interval(ntp))
194 linux_put_timespec64(struct timespec *ntp, struct l_timespec64 *ltp)
196 struct l_timespec64 lts;
199 error = native_to_linux_timespec64(<s, ntp);
202 return (copyout(<s, ltp, sizeof(lts)));
206 linux_get_timespec64(struct timespec *ntp, const struct l_timespec64 *ultp)
208 struct l_timespec64 lts;
211 error = copyin(ultp, <s, sizeof(lts));
214 return (linux_to_native_timespec64(ntp, <s));
219 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
223 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
225 error = native_to_linux_timespec(<p->it_value, &ntp->it_value);
230 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
234 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
236 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
240 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
242 linux_to_native_itimerspec64(struct itimerspec *ntp, struct l_itimerspec64 *ltp)
246 error = linux_to_native_timespec64(&ntp->it_interval, <p->it_interval);
248 error = linux_to_native_timespec64(&ntp->it_value, <p->it_value);
253 native_to_linux_itimerspec64(struct l_itimerspec64 *ltp, struct itimerspec *ntp)
257 error = native_to_linux_timespec64(<p->it_interval, &ntp->it_interval);
259 error = native_to_linux_timespec64(<p->it_value, &ntp->it_value);
265 linux_to_native_clockid(clockid_t *n, clockid_t l)
270 if (LINUX_CPUCLOCK_WHICH(l) == LINUX_CLOCKFD) {
271 LIN_SDT_PROBE1(time, linux_to_native_clockid,
272 unsupported_clockid, l);
275 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD_MASK)
278 if (LINUX_CPUCLOCK_PERTHREAD(l))
279 *n = CLOCK_THREAD_CPUTIME_ID;
281 *n = CLOCK_PROCESS_CPUTIME_ID;
286 case LINUX_CLOCK_REALTIME:
289 case LINUX_CLOCK_MONOTONIC:
290 *n = CLOCK_MONOTONIC;
292 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
293 *n = CLOCK_PROCESS_CPUTIME_ID;
295 case LINUX_CLOCK_THREAD_CPUTIME_ID:
296 *n = CLOCK_THREAD_CPUTIME_ID;
298 case LINUX_CLOCK_REALTIME_COARSE:
299 *n = CLOCK_REALTIME_FAST;
301 case LINUX_CLOCK_MONOTONIC_COARSE:
302 case LINUX_CLOCK_MONOTONIC_RAW:
303 *n = CLOCK_MONOTONIC_FAST;
305 case LINUX_CLOCK_BOOTTIME:
308 case LINUX_CLOCK_REALTIME_ALARM:
309 case LINUX_CLOCK_BOOTTIME_ALARM:
310 case LINUX_CLOCK_SGI_CYCLE:
311 case LINUX_CLOCK_TAI:
312 LIN_SDT_PROBE1(time, linux_to_native_clockid,
313 unsupported_clockid, l);
316 LIN_SDT_PROBE1(time, linux_to_native_clockid,
325 linux_to_native_timerflags(int *nflags, int flags)
328 if (flags & ~LINUX_TIMER_ABSTIME)
331 if (flags & LINUX_TIMER_ABSTIME)
332 *nflags |= TIMER_ABSTIME;
337 linux_common_clock_gettime(struct thread *td, clockid_t which,
341 struct thread *targettd;
343 int error, clockwhich;
348 error = linux_to_native_clockid(&nwhich, which);
351 "unsupported clock_gettime clockid %d", which);
352 LIN_SDT_PROBE1(time, linux_common_clock_gettime,
353 conversion_error, error);
358 case CLOCK_PROCESS_CPUTIME_ID:
360 clockwhich = LINUX_CPUCLOCK_WHICH(which);
361 pid = LINUX_CPUCLOCK_ID(which);
363 clockwhich = LINUX_CPUCLOCK_SCHED;
370 error = pget(pid, PGET_CANSEE, &p);
374 switch (clockwhich) {
375 case LINUX_CPUCLOCK_PROF:
377 calcru(p, &ru.ru_utime, &ru.ru_stime);
380 timevaladd(&ru.ru_utime, &ru.ru_stime);
381 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
383 case LINUX_CPUCLOCK_VIRT:
385 calcru(p, &ru.ru_utime, &ru.ru_stime);
388 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
390 case LINUX_CPUCLOCK_SCHED:
391 kern_process_cputime(p, tp);
401 case CLOCK_THREAD_CPUTIME_ID:
403 clockwhich = LINUX_CPUCLOCK_WHICH(which);
404 tid = LINUX_CPUCLOCK_ID(which);
406 clockwhich = LINUX_CPUCLOCK_SCHED;
414 targettd = linux_tdfind(td, tid, p->p_pid);
415 if (targettd == NULL)
418 switch (clockwhich) {
419 case LINUX_CPUCLOCK_PROF:
421 thread_lock(targettd);
422 rufetchtd(targettd, &ru);
423 thread_unlock(targettd);
426 timevaladd(&ru.ru_utime, &ru.ru_stime);
427 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
429 case LINUX_CPUCLOCK_VIRT:
431 thread_lock(targettd);
432 rufetchtd(targettd, &ru);
433 thread_unlock(targettd);
436 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
438 case LINUX_CPUCLOCK_SCHED:
441 kern_thread_cputime(targettd, tp);
451 error = kern_clock_gettime(td, nwhich, tp);
459 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
464 error = linux_common_clock_gettime(td, args->which, &tp);
466 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
469 error = linux_put_timespec(&tp, args->tp);
471 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
476 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
478 linux_clock_gettime64(struct thread *td, struct linux_clock_gettime64_args *args)
483 error = linux_common_clock_gettime(td, args->which, &tp);
485 LIN_SDT_PROBE1(time, linux_clock_gettime64, gettime_error, error);
488 error = linux_put_timespec64(&tp, args->tp);
490 LIN_SDT_PROBE1(time, linux_clock_gettime64, copyout_error, error);
497 linux_common_clock_settime(struct thread *td, clockid_t which,
503 error = linux_to_native_clockid(&nwhich, which);
506 "unsupported clock_settime clockid %d", which);
507 LIN_SDT_PROBE1(time, linux_common_clock_settime, conversion_error,
512 error = kern_clock_settime(td, nwhich, ts);
514 LIN_SDT_PROBE1(time, linux_common_clock_settime,
515 settime_error, error);
521 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
526 error = linux_get_timespec(&ts, args->tp);
528 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
531 return (linux_common_clock_settime(td, args->which, &ts));
534 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
536 linux_clock_settime64(struct thread *td, struct linux_clock_settime64_args *args)
541 error = linux_get_timespec64(&ts, args->tp);
543 LIN_SDT_PROBE1(time, linux_clock_settime64, copyin_error, error);
546 return (linux_common_clock_settime(td, args->which, &ts));
551 linux_common_clock_getres(struct thread *td, clockid_t which,
555 int error, clockwhich;
560 error = linux_to_native_clockid(&nwhich, which);
563 "unsupported clock_getres clockid %d", which);
564 LIN_SDT_PROBE1(time, linux_common_clock_getres,
565 conversion_error, error);
570 * Check user supplied clock id in case of per-process
571 * or thread-specific cpu-time clock.
575 case CLOCK_THREAD_CPUTIME_ID:
576 tid = LINUX_CPUCLOCK_ID(which);
579 if (linux_tdfind(td, tid, p->p_pid) == NULL)
584 case CLOCK_PROCESS_CPUTIME_ID:
585 pid = LINUX_CPUCLOCK_ID(which);
587 error = pget(pid, PGET_CANSEE, &p);
597 LIN_SDT_PROBE0(time, linux_common_clock_getres, nullcall);
602 case CLOCK_THREAD_CPUTIME_ID:
603 case CLOCK_PROCESS_CPUTIME_ID:
604 clockwhich = LINUX_CPUCLOCK_WHICH(which);
606 * In both cases (when the clock id obtained by a call to
607 * clock_getcpuclockid() or using the clock
608 * ID CLOCK_PROCESS_CPUTIME_ID Linux hardcodes precision
609 * of clock. The same for the CLOCK_THREAD_CPUTIME_ID clock.
611 * See Linux posix_cpu_clock_getres() implementation.
613 if (which > 0 || clockwhich == LINUX_CPUCLOCK_SCHED) {
619 switch (clockwhich) {
620 case LINUX_CPUCLOCK_PROF:
623 case LINUX_CPUCLOCK_VIRT:
624 nwhich = CLOCK_VIRTUAL;
634 error = kern_clock_getres(td, nwhich, ts);
636 LIN_SDT_PROBE1(time, linux_common_clock_getres,
637 getres_error, error);
646 linux_clock_getres(struct thread *td,
647 struct linux_clock_getres_args *args)
652 error = linux_common_clock_getres(td, args->which, &ts);
653 if (error != 0 || args->tp == NULL)
655 error = linux_put_timespec(&ts, args->tp);
657 LIN_SDT_PROBE1(time, linux_clock_getres,
658 copyout_error, error);
662 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
664 linux_clock_getres_time64(struct thread *td,
665 struct linux_clock_getres_time64_args *args)
670 error = linux_common_clock_getres(td, args->which, &ts);
671 if (error != 0 || args->tp == NULL)
673 error = linux_put_timespec64(&ts, args->tp);
675 LIN_SDT_PROBE1(time, linux_clock_getres_time64,
676 copyout_error, error);
682 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
684 struct timespec *rmtp;
685 struct timespec rqts, rmts;
688 error = linux_get_timespec(&rqts, args->rqtp);
690 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
693 if (args->rmtp != NULL)
698 error = kern_nanosleep(td, &rqts, rmtp);
699 if (error == EINTR && args->rmtp != NULL) {
700 error2 = linux_put_timespec(rmtp, args->rmtp);
702 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
712 linux_common_clock_nanosleep(struct thread *td, clockid_t which,
713 l_int lflags, struct timespec *rqtp, struct timespec *rmtp)
718 error = linux_to_native_timerflags(&flags, lflags);
720 LIN_SDT_PROBE1(time, linux_common_clock_nanosleep,
721 unsupported_flags, lflags);
725 error = linux_to_native_clockid(&clockid, which);
728 "unsupported clock_nanosleep clockid %d", which);
729 LIN_SDT_PROBE1(time, linux_common_clock_nanosleep,
730 unsupported_clockid, which);
733 if (clockid == CLOCK_THREAD_CPUTIME_ID)
736 return (kern_clock_nanosleep(td, clockid, flags, rqtp, rmtp));
740 linux_clock_nanosleep(struct thread *td,
741 struct linux_clock_nanosleep_args *args)
743 struct timespec *rmtp;
744 struct timespec rqts, rmts;
747 error = linux_get_timespec(&rqts, args->rqtp);
749 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
753 if (args->rmtp != NULL)
758 error = linux_common_clock_nanosleep(td, args->which, args->flags,
760 if (error == EINTR && (args->flags & LINUX_TIMER_ABSTIME) == 0 &&
761 args->rmtp != NULL) {
762 error2 = linux_put_timespec(rmtp, args->rmtp);
764 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
765 copyout_error, error2);
772 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
774 linux_clock_nanosleep_time64(struct thread *td,
775 struct linux_clock_nanosleep_time64_args *args)
777 struct timespec *rmtp;
778 struct timespec rqts, rmts;
781 error = linux_get_timespec64(&rqts, args->rqtp);
783 LIN_SDT_PROBE1(time, linux_clock_nanosleep_time64,
784 copyin_error, error);
787 if (args->rmtp != NULL)
792 error = linux_common_clock_nanosleep(td, args->which, args->flags,
794 if (error == EINTR && (args->flags & LINUX_TIMER_ABSTIME) == 0 &&
795 args->rmtp != NULL) {
796 error2 = linux_put_timespec64(rmtp, args->rmtp);
798 LIN_SDT_PROBE1(time, linux_clock_nanosleep_time64,
799 copyout_error, error2);