1 /* $NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
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 "opt_compat.h"
42 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/ucred.h>
46 #include <sys/limits.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/resourcevar.h>
51 #include <sys/signal.h>
52 #include <sys/stdint.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
56 #include <sys/systm.h>
60 #include <machine/../linux32/linux.h>
61 #include <machine/../linux32/linux32_proto.h>
63 #include <machine/../linux/linux.h>
64 #include <machine/../linux/linux_proto.h>
67 #include <compat/linux/linux_dtrace.h>
68 #include <compat/linux/linux_misc.h>
69 #include <compat/linux/linux_timer.h>
70 #include <compat/linux/linux_util.h>
73 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
76 * DTrace probes in this module.
78 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
80 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
82 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_gettime, conversion_error, "int");
83 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
84 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
85 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
86 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime64, gettime_error, "int");
87 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime64, copyout_error, "int");
89 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
90 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_settime, settime_error, "int");
91 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_settime, conversion_error, "int");
92 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
93 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
94 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime64, conversion_error, "int");
95 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime64, copyin_error, "int");
97 LIN_SDT_PROBE_DEFINE0(time, linux_common_clock_getres, nullcall);
98 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_getres, conversion_error, "int");
99 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_getres, getres_error, "int");
100 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
101 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
102 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres_time64, copyout_error, "int");
104 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
105 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
106 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
107 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
108 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_nanosleep, unsupported_flags, "int");
109 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_nanosleep, unsupported_clockid, "int");
110 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
111 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep_time64, copyout_error, "int");
112 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep_time64, copyin_error, "int");
115 static int linux_common_clock_gettime(struct thread *, clockid_t,
117 static int linux_common_clock_settime(struct thread *, clockid_t,
119 static int linux_common_clock_getres(struct thread *, clockid_t,
121 static int linux_common_clock_nanosleep(struct thread *, clockid_t,
122 l_int, struct timespec *, struct timespec *);
125 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
128 #ifdef COMPAT_LINUX32
129 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
132 ltp->tv_sec = ntp->tv_sec;
133 ltp->tv_nsec = ntp->tv_nsec;
139 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
142 if (!timespecvalid_interval(ltp))
144 ntp->tv_sec = ltp->tv_sec;
145 ntp->tv_nsec = ltp->tv_nsec;
151 linux_put_timespec(struct timespec *ntp, struct l_timespec *ltp)
153 struct l_timespec lts;
156 error = native_to_linux_timespec(<s, ntp);
159 return (copyout(<s, ltp, sizeof(lts)));
163 linux_get_timespec(struct timespec *ntp, const struct l_timespec *ultp)
165 struct l_timespec lts;
168 error = copyin(ultp, <s, sizeof(lts));
171 return (linux_to_native_timespec(ntp, <s));
174 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
176 native_to_linux_timespec64(struct l_timespec64 *ltp64, struct timespec *ntp)
179 ltp64->tv_sec = ntp->tv_sec;
180 ltp64->tv_nsec = ntp->tv_nsec;
186 linux_to_native_timespec64(struct timespec *ntp, struct l_timespec64 *ltp64)
189 #if defined(__i386__)
190 /* i386 time_t is still 32-bit */
191 if (ltp64->tv_sec > INT_MAX || ltp64->tv_sec < INT_MIN)
194 /* Zero out the padding in compat mode. */
195 ntp->tv_nsec = ltp64->tv_nsec & 0xFFFFFFFFUL;
196 ntp->tv_sec = ltp64->tv_sec;
198 if (!timespecvalid_interval(ntp))
205 linux_put_timespec64(struct timespec *ntp, struct l_timespec64 *ltp)
207 struct l_timespec64 lts;
210 error = native_to_linux_timespec64(<s, ntp);
213 return (copyout(<s, ltp, sizeof(lts)));
217 linux_get_timespec64(struct timespec *ntp, const struct l_timespec64 *ultp)
219 struct l_timespec64 lts;
222 error = copyin(ultp, <s, sizeof(lts));
225 return (linux_to_native_timespec64(ntp, <s));
230 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
234 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
236 error = native_to_linux_timespec(<p->it_value, &ntp->it_value);
241 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
245 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
247 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
251 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
253 linux_to_native_itimerspec64(struct itimerspec *ntp, struct l_itimerspec64 *ltp)
257 error = linux_to_native_timespec64(&ntp->it_interval, <p->it_interval);
259 error = linux_to_native_timespec64(&ntp->it_value, <p->it_value);
264 native_to_linux_itimerspec64(struct l_itimerspec64 *ltp, struct itimerspec *ntp)
268 error = native_to_linux_timespec64(<p->it_interval, &ntp->it_interval);
270 error = native_to_linux_timespec64(<p->it_value, &ntp->it_value);
276 linux_to_native_clockid(clockid_t *n, clockid_t l)
281 if (LINUX_CPUCLOCK_WHICH(l) == LINUX_CLOCKFD) {
282 LIN_SDT_PROBE1(time, linux_to_native_clockid,
283 unsupported_clockid, l);
286 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD_MASK)
289 if (LINUX_CPUCLOCK_PERTHREAD(l))
290 *n = CLOCK_THREAD_CPUTIME_ID;
292 *n = CLOCK_PROCESS_CPUTIME_ID;
297 case LINUX_CLOCK_REALTIME:
300 case LINUX_CLOCK_MONOTONIC:
301 *n = CLOCK_MONOTONIC;
303 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
304 *n = CLOCK_PROCESS_CPUTIME_ID;
306 case LINUX_CLOCK_THREAD_CPUTIME_ID:
307 *n = CLOCK_THREAD_CPUTIME_ID;
309 case LINUX_CLOCK_REALTIME_COARSE:
310 *n = CLOCK_REALTIME_FAST;
312 case LINUX_CLOCK_MONOTONIC_COARSE:
313 case LINUX_CLOCK_MONOTONIC_RAW:
314 *n = CLOCK_MONOTONIC_FAST;
316 case LINUX_CLOCK_BOOTTIME:
319 case LINUX_CLOCK_REALTIME_ALARM:
320 case LINUX_CLOCK_BOOTTIME_ALARM:
321 case LINUX_CLOCK_SGI_CYCLE:
322 case LINUX_CLOCK_TAI:
323 LIN_SDT_PROBE1(time, linux_to_native_clockid,
324 unsupported_clockid, l);
327 LIN_SDT_PROBE1(time, linux_to_native_clockid,
336 linux_to_native_timerflags(int *nflags, int flags)
339 if (flags & ~LINUX_TIMER_ABSTIME)
342 if (flags & LINUX_TIMER_ABSTIME)
343 *nflags |= TIMER_ABSTIME;
348 linux_common_clock_gettime(struct thread *td, clockid_t which,
352 struct thread *targettd;
354 int error, clockwhich;
359 error = linux_to_native_clockid(&nwhich, which);
362 "unsupported clock_gettime clockid %d", which);
363 LIN_SDT_PROBE1(time, linux_common_clock_gettime,
364 conversion_error, error);
369 case CLOCK_PROCESS_CPUTIME_ID:
371 clockwhich = LINUX_CPUCLOCK_WHICH(which);
372 pid = LINUX_CPUCLOCK_ID(which);
374 clockwhich = LINUX_CPUCLOCK_SCHED;
381 error = pget(pid, PGET_CANSEE, &p);
385 switch (clockwhich) {
386 case LINUX_CPUCLOCK_PROF:
388 calcru(p, &ru.ru_utime, &ru.ru_stime);
391 timevaladd(&ru.ru_utime, &ru.ru_stime);
392 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
394 case LINUX_CPUCLOCK_VIRT:
396 calcru(p, &ru.ru_utime, &ru.ru_stime);
399 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
401 case LINUX_CPUCLOCK_SCHED:
402 kern_process_cputime(p, tp);
412 case CLOCK_THREAD_CPUTIME_ID:
414 clockwhich = LINUX_CPUCLOCK_WHICH(which);
415 tid = LINUX_CPUCLOCK_ID(which);
417 clockwhich = LINUX_CPUCLOCK_SCHED;
425 targettd = linux_tdfind(td, tid, p->p_pid);
426 if (targettd == NULL)
429 switch (clockwhich) {
430 case LINUX_CPUCLOCK_PROF:
432 thread_lock(targettd);
433 rufetchtd(targettd, &ru);
434 thread_unlock(targettd);
437 timevaladd(&ru.ru_utime, &ru.ru_stime);
438 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
440 case LINUX_CPUCLOCK_VIRT:
442 thread_lock(targettd);
443 rufetchtd(targettd, &ru);
444 thread_unlock(targettd);
447 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
449 case LINUX_CPUCLOCK_SCHED:
452 kern_thread_cputime(targettd, tp);
462 error = kern_clock_gettime(td, nwhich, tp);
470 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
475 error = linux_common_clock_gettime(td, args->which, &tp);
477 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
480 error = linux_put_timespec(&tp, args->tp);
482 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
487 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
489 linux_clock_gettime64(struct thread *td, struct linux_clock_gettime64_args *args)
494 error = linux_common_clock_gettime(td, args->which, &tp);
496 LIN_SDT_PROBE1(time, linux_clock_gettime64, gettime_error, error);
499 error = linux_put_timespec64(&tp, args->tp);
501 LIN_SDT_PROBE1(time, linux_clock_gettime64, copyout_error, error);
508 linux_common_clock_settime(struct thread *td, clockid_t which,
514 error = linux_to_native_clockid(&nwhich, which);
517 "unsupported clock_settime clockid %d", which);
518 LIN_SDT_PROBE1(time, linux_common_clock_settime, conversion_error,
523 error = kern_clock_settime(td, nwhich, ts);
525 LIN_SDT_PROBE1(time, linux_common_clock_settime,
526 settime_error, error);
532 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
537 error = linux_get_timespec(&ts, args->tp);
539 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
542 return (linux_common_clock_settime(td, args->which, &ts));
545 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
547 linux_clock_settime64(struct thread *td, struct linux_clock_settime64_args *args)
552 error = linux_get_timespec64(&ts, args->tp);
554 LIN_SDT_PROBE1(time, linux_clock_settime64, copyin_error, error);
557 return (linux_common_clock_settime(td, args->which, &ts));
562 linux_common_clock_getres(struct thread *td, clockid_t which,
566 int error, clockwhich;
571 error = linux_to_native_clockid(&nwhich, which);
574 "unsupported clock_getres clockid %d", which);
575 LIN_SDT_PROBE1(time, linux_common_clock_getres,
576 conversion_error, error);
581 * Check user supplied clock id in case of per-process
582 * or thread-specific cpu-time clock.
586 case CLOCK_THREAD_CPUTIME_ID:
587 tid = LINUX_CPUCLOCK_ID(which);
590 if (linux_tdfind(td, tid, p->p_pid) == NULL)
595 case CLOCK_PROCESS_CPUTIME_ID:
596 pid = LINUX_CPUCLOCK_ID(which);
598 error = pget(pid, PGET_CANSEE, &p);
608 LIN_SDT_PROBE0(time, linux_common_clock_getres, nullcall);
613 case CLOCK_THREAD_CPUTIME_ID:
614 case CLOCK_PROCESS_CPUTIME_ID:
615 clockwhich = LINUX_CPUCLOCK_WHICH(which);
617 * In both cases (when the clock id obtained by a call to
618 * clock_getcpuclockid() or using the clock
619 * ID CLOCK_PROCESS_CPUTIME_ID Linux hardcodes precision
620 * of clock. The same for the CLOCK_THREAD_CPUTIME_ID clock.
622 * See Linux posix_cpu_clock_getres() implementation.
624 if (which > 0 || clockwhich == LINUX_CPUCLOCK_SCHED) {
630 switch (clockwhich) {
631 case LINUX_CPUCLOCK_PROF:
634 case LINUX_CPUCLOCK_VIRT:
635 nwhich = CLOCK_VIRTUAL;
645 error = kern_clock_getres(td, nwhich, ts);
647 LIN_SDT_PROBE1(time, linux_common_clock_getres,
648 getres_error, error);
657 linux_clock_getres(struct thread *td,
658 struct linux_clock_getres_args *args)
663 error = linux_common_clock_getres(td, args->which, &ts);
664 if (error != 0 || args->tp == NULL)
666 error = linux_put_timespec(&ts, args->tp);
668 LIN_SDT_PROBE1(time, linux_clock_getres,
669 copyout_error, error);
673 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
675 linux_clock_getres_time64(struct thread *td,
676 struct linux_clock_getres_time64_args *args)
681 error = linux_common_clock_getres(td, args->which, &ts);
682 if (error != 0 || args->tp == NULL)
684 error = linux_put_timespec64(&ts, args->tp);
686 LIN_SDT_PROBE1(time, linux_clock_getres_time64,
687 copyout_error, error);
693 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
695 struct timespec *rmtp;
696 struct timespec rqts, rmts;
699 error = linux_get_timespec(&rqts, args->rqtp);
701 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
704 if (args->rmtp != NULL)
709 error = kern_nanosleep(td, &rqts, rmtp);
710 if (error == EINTR && args->rmtp != NULL) {
711 error2 = linux_put_timespec(rmtp, args->rmtp);
713 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
723 linux_common_clock_nanosleep(struct thread *td, clockid_t which,
724 l_int lflags, struct timespec *rqtp, struct timespec *rmtp)
729 error = linux_to_native_timerflags(&flags, lflags);
731 LIN_SDT_PROBE1(time, linux_common_clock_nanosleep,
732 unsupported_flags, lflags);
736 error = linux_to_native_clockid(&clockid, which);
739 "unsupported clock_nanosleep clockid %d", which);
740 LIN_SDT_PROBE1(time, linux_common_clock_nanosleep,
741 unsupported_clockid, which);
744 if (clockid == CLOCK_THREAD_CPUTIME_ID)
747 return (kern_clock_nanosleep(td, clockid, flags, rqtp, rmtp));
751 linux_clock_nanosleep(struct thread *td,
752 struct linux_clock_nanosleep_args *args)
754 struct timespec *rmtp;
755 struct timespec rqts, rmts;
758 error = linux_get_timespec(&rqts, args->rqtp);
760 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
764 if (args->rmtp != NULL)
769 error = linux_common_clock_nanosleep(td, args->which, args->flags,
771 if (error == EINTR && (args->flags & LINUX_TIMER_ABSTIME) == 0 &&
772 args->rmtp != NULL) {
773 error2 = linux_put_timespec(rmtp, args->rmtp);
775 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
776 copyout_error, error2);
783 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
785 linux_clock_nanosleep_time64(struct thread *td,
786 struct linux_clock_nanosleep_time64_args *args)
788 struct timespec *rmtp;
789 struct timespec rqts, rmts;
792 error = linux_get_timespec64(&rqts, args->rqtp);
794 LIN_SDT_PROBE1(time, linux_clock_nanosleep_time64,
795 copyin_error, error);
798 if (args->rmtp != NULL)
803 error = linux_common_clock_nanosleep(td, args->which, args->flags,
805 if (error == EINTR && (args->flags & LINUX_TIMER_ABSTIME) == 0 &&
806 args->rmtp != NULL) {
807 error2 = linux_put_timespec64(rmtp, args->rmtp);
809 LIN_SDT_PROBE1(time, linux_clock_nanosleep_time64,
810 copyout_error, error2);