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 <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/ucred.h>
44 #include <sys/limits.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/resourcevar.h>
49 #include <sys/signal.h>
50 #include <sys/stdint.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysproto.h>
54 #include <sys/systm.h>
58 #include <machine/../linux32/linux.h>
59 #include <machine/../linux32/linux32_proto.h>
61 #include <machine/../linux/linux.h>
62 #include <machine/../linux/linux_proto.h>
65 #include <compat/linux/linux_dtrace.h>
66 #include <compat/linux/linux_misc.h>
67 #include <compat/linux/linux_timer.h>
68 #include <compat/linux/linux_util.h>
71 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
74 * DTrace probes in this module.
76 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
78 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
80 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_gettime, conversion_error, "int");
81 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
82 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
83 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
84 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime64, gettime_error, "int");
85 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime64, copyout_error, "int");
87 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
88 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_settime, settime_error, "int");
89 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_settime, conversion_error, "int");
90 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
91 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
92 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime64, conversion_error, "int");
93 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime64, copyin_error, "int");
95 LIN_SDT_PROBE_DEFINE0(time, linux_common_clock_getres, nullcall);
96 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_getres, conversion_error, "int");
97 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_getres, getres_error, "int");
98 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
99 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
100 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres_time64, copyout_error, "int");
102 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
103 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
104 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
105 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
106 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_nanosleep, unsupported_flags, "int");
107 LIN_SDT_PROBE_DEFINE1(time, linux_common_clock_nanosleep, unsupported_clockid, "int");
108 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
109 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep_time64, copyout_error, "int");
110 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep_time64, copyin_error, "int");
113 static int linux_common_clock_gettime(struct thread *, clockid_t,
115 static int linux_common_clock_settime(struct thread *, clockid_t,
117 static int linux_common_clock_getres(struct thread *, clockid_t,
119 static int linux_common_clock_nanosleep(struct thread *, clockid_t,
120 l_int, struct timespec *, struct timespec *);
123 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
126 #ifdef COMPAT_LINUX32
127 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
130 ltp->tv_sec = ntp->tv_sec;
131 ltp->tv_nsec = ntp->tv_nsec;
137 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
140 if (!timespecvalid_interval(ltp))
142 ntp->tv_sec = ltp->tv_sec;
143 ntp->tv_nsec = ltp->tv_nsec;
149 linux_put_timespec(struct timespec *ntp, struct l_timespec *ltp)
151 struct l_timespec lts;
154 error = native_to_linux_timespec(<s, ntp);
157 return (copyout(<s, ltp, sizeof(lts)));
161 linux_get_timespec(struct timespec *ntp, const struct l_timespec *ultp)
163 struct l_timespec lts;
166 error = copyin(ultp, <s, sizeof(lts));
169 return (linux_to_native_timespec(ntp, <s));
172 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
174 native_to_linux_timespec64(struct l_timespec64 *ltp64, struct timespec *ntp)
177 ltp64->tv_sec = ntp->tv_sec;
178 ltp64->tv_nsec = ntp->tv_nsec;
184 linux_to_native_timespec64(struct timespec *ntp, struct l_timespec64 *ltp64)
187 #if defined(__i386__)
188 /* i386 time_t is still 32-bit */
189 if (ltp64->tv_sec > INT_MAX || ltp64->tv_sec < INT_MIN)
192 /* Zero out the padding in compat mode. */
193 ntp->tv_nsec = ltp64->tv_nsec & 0xFFFFFFFFUL;
194 ntp->tv_sec = ltp64->tv_sec;
196 if (!timespecvalid_interval(ntp))
203 linux_put_timespec64(struct timespec *ntp, struct l_timespec64 *ltp)
205 struct l_timespec64 lts;
208 error = native_to_linux_timespec64(<s, ntp);
211 return (copyout(<s, ltp, sizeof(lts)));
215 linux_get_timespec64(struct timespec *ntp, const struct l_timespec64 *ultp)
217 struct l_timespec64 lts;
220 error = copyin(ultp, <s, sizeof(lts));
223 return (linux_to_native_timespec64(ntp, <s));
228 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
232 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
234 error = native_to_linux_timespec(<p->it_value, &ntp->it_value);
239 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
243 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
245 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
249 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
251 linux_to_native_itimerspec64(struct itimerspec *ntp, struct l_itimerspec64 *ltp)
255 error = linux_to_native_timespec64(&ntp->it_interval, <p->it_interval);
257 error = linux_to_native_timespec64(&ntp->it_value, <p->it_value);
262 native_to_linux_itimerspec64(struct l_itimerspec64 *ltp, struct itimerspec *ntp)
266 error = native_to_linux_timespec64(<p->it_interval, &ntp->it_interval);
268 error = native_to_linux_timespec64(<p->it_value, &ntp->it_value);
274 linux_to_native_clockid(clockid_t *n, clockid_t l)
279 if (LINUX_CPUCLOCK_WHICH(l) == LINUX_CLOCKFD) {
280 LIN_SDT_PROBE1(time, linux_to_native_clockid,
281 unsupported_clockid, l);
284 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD_MASK)
287 if (LINUX_CPUCLOCK_PERTHREAD(l))
288 *n = CLOCK_THREAD_CPUTIME_ID;
290 *n = CLOCK_PROCESS_CPUTIME_ID;
295 case LINUX_CLOCK_REALTIME:
298 case LINUX_CLOCK_MONOTONIC:
299 *n = CLOCK_MONOTONIC;
301 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
302 *n = CLOCK_PROCESS_CPUTIME_ID;
304 case LINUX_CLOCK_THREAD_CPUTIME_ID:
305 *n = CLOCK_THREAD_CPUTIME_ID;
307 case LINUX_CLOCK_REALTIME_COARSE:
308 *n = CLOCK_REALTIME_FAST;
310 case LINUX_CLOCK_MONOTONIC_COARSE:
311 case LINUX_CLOCK_MONOTONIC_RAW:
312 *n = CLOCK_MONOTONIC_FAST;
314 case LINUX_CLOCK_BOOTTIME:
317 case LINUX_CLOCK_REALTIME_ALARM:
318 case LINUX_CLOCK_BOOTTIME_ALARM:
319 case LINUX_CLOCK_SGI_CYCLE:
320 case LINUX_CLOCK_TAI:
321 LIN_SDT_PROBE1(time, linux_to_native_clockid,
322 unsupported_clockid, l);
325 LIN_SDT_PROBE1(time, linux_to_native_clockid,
334 linux_to_native_timerflags(int *nflags, int flags)
337 if (flags & ~LINUX_TIMER_ABSTIME)
340 if (flags & LINUX_TIMER_ABSTIME)
341 *nflags |= TIMER_ABSTIME;
346 linux_common_clock_gettime(struct thread *td, clockid_t which,
350 struct thread *targettd;
352 int error, clockwhich;
357 error = linux_to_native_clockid(&nwhich, which);
360 "unsupported clock_gettime clockid %d", which);
361 LIN_SDT_PROBE1(time, linux_common_clock_gettime,
362 conversion_error, error);
367 case CLOCK_PROCESS_CPUTIME_ID:
369 clockwhich = LINUX_CPUCLOCK_WHICH(which);
370 pid = LINUX_CPUCLOCK_ID(which);
372 clockwhich = LINUX_CPUCLOCK_SCHED;
379 error = pget(pid, PGET_CANSEE, &p);
383 switch (clockwhich) {
384 case LINUX_CPUCLOCK_PROF:
386 calcru(p, &ru.ru_utime, &ru.ru_stime);
389 timevaladd(&ru.ru_utime, &ru.ru_stime);
390 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
392 case LINUX_CPUCLOCK_VIRT:
394 calcru(p, &ru.ru_utime, &ru.ru_stime);
397 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
399 case LINUX_CPUCLOCK_SCHED:
400 kern_process_cputime(p, tp);
410 case CLOCK_THREAD_CPUTIME_ID:
412 clockwhich = LINUX_CPUCLOCK_WHICH(which);
413 tid = LINUX_CPUCLOCK_ID(which);
415 clockwhich = LINUX_CPUCLOCK_SCHED;
423 targettd = linux_tdfind(td, tid, p->p_pid);
424 if (targettd == NULL)
427 switch (clockwhich) {
428 case LINUX_CPUCLOCK_PROF:
430 thread_lock(targettd);
431 rufetchtd(targettd, &ru);
432 thread_unlock(targettd);
435 timevaladd(&ru.ru_utime, &ru.ru_stime);
436 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
438 case LINUX_CPUCLOCK_VIRT:
440 thread_lock(targettd);
441 rufetchtd(targettd, &ru);
442 thread_unlock(targettd);
445 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
447 case LINUX_CPUCLOCK_SCHED:
450 kern_thread_cputime(targettd, tp);
460 error = kern_clock_gettime(td, nwhich, tp);
468 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
473 error = linux_common_clock_gettime(td, args->which, &tp);
475 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
478 error = linux_put_timespec(&tp, args->tp);
480 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
485 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
487 linux_clock_gettime64(struct thread *td, struct linux_clock_gettime64_args *args)
492 error = linux_common_clock_gettime(td, args->which, &tp);
494 LIN_SDT_PROBE1(time, linux_clock_gettime64, gettime_error, error);
497 error = linux_put_timespec64(&tp, args->tp);
499 LIN_SDT_PROBE1(time, linux_clock_gettime64, copyout_error, error);
506 linux_common_clock_settime(struct thread *td, clockid_t which,
512 error = linux_to_native_clockid(&nwhich, which);
515 "unsupported clock_settime clockid %d", which);
516 LIN_SDT_PROBE1(time, linux_common_clock_settime, conversion_error,
521 error = kern_clock_settime(td, nwhich, ts);
523 LIN_SDT_PROBE1(time, linux_common_clock_settime,
524 settime_error, error);
530 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
535 error = linux_get_timespec(&ts, args->tp);
537 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
540 return (linux_common_clock_settime(td, args->which, &ts));
543 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
545 linux_clock_settime64(struct thread *td, struct linux_clock_settime64_args *args)
550 error = linux_get_timespec64(&ts, args->tp);
552 LIN_SDT_PROBE1(time, linux_clock_settime64, copyin_error, error);
555 return (linux_common_clock_settime(td, args->which, &ts));
560 linux_common_clock_getres(struct thread *td, clockid_t which,
564 int error, clockwhich;
569 error = linux_to_native_clockid(&nwhich, which);
572 "unsupported clock_getres clockid %d", which);
573 LIN_SDT_PROBE1(time, linux_common_clock_getres,
574 conversion_error, error);
579 * Check user supplied clock id in case of per-process
580 * or thread-specific cpu-time clock.
584 case CLOCK_THREAD_CPUTIME_ID:
585 tid = LINUX_CPUCLOCK_ID(which);
588 if (linux_tdfind(td, tid, p->p_pid) == NULL)
593 case CLOCK_PROCESS_CPUTIME_ID:
594 pid = LINUX_CPUCLOCK_ID(which);
596 error = pget(pid, PGET_CANSEE, &p);
606 LIN_SDT_PROBE0(time, linux_common_clock_getres, nullcall);
611 case CLOCK_THREAD_CPUTIME_ID:
612 case CLOCK_PROCESS_CPUTIME_ID:
613 clockwhich = LINUX_CPUCLOCK_WHICH(which);
615 * In both cases (when the clock id obtained by a call to
616 * clock_getcpuclockid() or using the clock
617 * ID CLOCK_PROCESS_CPUTIME_ID Linux hardcodes precision
618 * of clock. The same for the CLOCK_THREAD_CPUTIME_ID clock.
620 * See Linux posix_cpu_clock_getres() implementation.
622 if (which > 0 || clockwhich == LINUX_CPUCLOCK_SCHED) {
628 switch (clockwhich) {
629 case LINUX_CPUCLOCK_PROF:
632 case LINUX_CPUCLOCK_VIRT:
633 nwhich = CLOCK_VIRTUAL;
643 error = kern_clock_getres(td, nwhich, ts);
645 LIN_SDT_PROBE1(time, linux_common_clock_getres,
646 getres_error, error);
655 linux_clock_getres(struct thread *td,
656 struct linux_clock_getres_args *args)
661 error = linux_common_clock_getres(td, args->which, &ts);
662 if (error != 0 || args->tp == NULL)
664 error = linux_put_timespec(&ts, args->tp);
666 LIN_SDT_PROBE1(time, linux_clock_getres,
667 copyout_error, error);
671 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
673 linux_clock_getres_time64(struct thread *td,
674 struct linux_clock_getres_time64_args *args)
679 error = linux_common_clock_getres(td, args->which, &ts);
680 if (error != 0 || args->tp == NULL)
682 error = linux_put_timespec64(&ts, args->tp);
684 LIN_SDT_PROBE1(time, linux_clock_getres_time64,
685 copyout_error, error);
691 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
693 struct timespec *rmtp;
694 struct timespec rqts, rmts;
697 error = linux_get_timespec(&rqts, args->rqtp);
699 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
702 if (args->rmtp != NULL)
707 error = kern_nanosleep(td, &rqts, rmtp);
708 if (error == EINTR && args->rmtp != NULL) {
709 error2 = linux_put_timespec(rmtp, args->rmtp);
711 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
721 linux_common_clock_nanosleep(struct thread *td, clockid_t which,
722 l_int lflags, struct timespec *rqtp, struct timespec *rmtp)
727 error = linux_to_native_timerflags(&flags, lflags);
729 LIN_SDT_PROBE1(time, linux_common_clock_nanosleep,
730 unsupported_flags, lflags);
734 error = linux_to_native_clockid(&clockid, which);
737 "unsupported clock_nanosleep clockid %d", which);
738 LIN_SDT_PROBE1(time, linux_common_clock_nanosleep,
739 unsupported_clockid, which);
742 if (clockid == CLOCK_THREAD_CPUTIME_ID)
745 return (kern_clock_nanosleep(td, clockid, flags, rqtp, rmtp));
749 linux_clock_nanosleep(struct thread *td,
750 struct linux_clock_nanosleep_args *args)
752 struct timespec *rmtp;
753 struct timespec rqts, rmts;
756 error = linux_get_timespec(&rqts, args->rqtp);
758 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
762 if (args->rmtp != NULL)
767 error = linux_common_clock_nanosleep(td, args->which, args->flags,
769 if (error == EINTR && (args->flags & LINUX_TIMER_ABSTIME) == 0 &&
770 args->rmtp != NULL) {
771 error2 = linux_put_timespec(rmtp, args->rmtp);
773 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
774 copyout_error, error2);
781 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
783 linux_clock_nanosleep_time64(struct thread *td,
784 struct linux_clock_nanosleep_time64_args *args)
786 struct timespec *rmtp;
787 struct timespec rqts, rmts;
790 error = linux_get_timespec64(&rqts, args->rqtp);
792 LIN_SDT_PROBE1(time, linux_clock_nanosleep_time64,
793 copyin_error, error);
796 if (args->rmtp != NULL)
801 error = linux_common_clock_nanosleep(td, args->which, args->flags,
803 if (error == EINTR && (args->flags & LINUX_TIMER_ABSTIME) == 0 &&
804 args->rmtp != NULL) {
805 error2 = linux_put_timespec64(rmtp, args->rmtp);
807 LIN_SDT_PROBE1(time, linux_clock_nanosleep_time64,
808 copyout_error, error2);