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, conversion_error, "int");
105 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
106 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
107 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, conversion_error, "int");
108 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
109 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
110 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
111 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "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,
121 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
124 #ifdef COMPAT_LINUX32
125 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
128 ltp->tv_sec = ntp->tv_sec;
129 ltp->tv_nsec = ntp->tv_nsec;
135 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
138 if (ltp->tv_sec < 0 || ltp->tv_nsec < 0 || ltp->tv_nsec > 999999999)
140 ntp->tv_sec = ltp->tv_sec;
141 ntp->tv_nsec = ltp->tv_nsec;
146 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
148 native_to_linux_timespec64(struct l_timespec64 *ltp64, struct timespec *ntp)
151 ltp64->tv_sec = ntp->tv_sec;
152 ltp64->tv_nsec = ntp->tv_nsec;
158 linux_to_native_timespec64(struct timespec *ntp, struct l_timespec64 *ltp64)
161 if (ltp64->tv_sec < 0 || ltp64->tv_nsec < 0 || ltp64->tv_nsec > 999999999)
163 ntp->tv_sec = ltp64->tv_sec;
164 ntp->tv_nsec = ltp64->tv_nsec;
171 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
175 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
177 error = native_to_linux_timespec(<p->it_value, &ntp->it_interval);
182 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
186 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
188 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
193 linux_to_native_clockid(clockid_t *n, clockid_t l)
198 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
200 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
203 if (LINUX_CPUCLOCK_PERTHREAD(l))
204 *n = CLOCK_THREAD_CPUTIME_ID;
206 *n = CLOCK_PROCESS_CPUTIME_ID;
211 case LINUX_CLOCK_REALTIME:
214 case LINUX_CLOCK_MONOTONIC:
215 *n = CLOCK_MONOTONIC;
217 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
218 *n = CLOCK_PROCESS_CPUTIME_ID;
220 case LINUX_CLOCK_THREAD_CPUTIME_ID:
221 *n = CLOCK_THREAD_CPUTIME_ID;
223 case LINUX_CLOCK_REALTIME_COARSE:
224 *n = CLOCK_REALTIME_FAST;
226 case LINUX_CLOCK_MONOTONIC_COARSE:
227 case LINUX_CLOCK_MONOTONIC_RAW:
228 *n = CLOCK_MONOTONIC_FAST;
230 case LINUX_CLOCK_BOOTTIME:
233 case LINUX_CLOCK_REALTIME_ALARM:
234 case LINUX_CLOCK_BOOTTIME_ALARM:
235 case LINUX_CLOCK_SGI_CYCLE:
236 case LINUX_CLOCK_TAI:
237 LIN_SDT_PROBE1(time, linux_to_native_clockid,
238 unsupported_clockid, l);
241 LIN_SDT_PROBE1(time, linux_to_native_clockid,
250 linux_to_native_timerflags(int *nflags, int flags)
253 if (flags & ~LINUX_TIMER_ABSTIME)
256 if (flags & LINUX_TIMER_ABSTIME)
257 *nflags |= TIMER_ABSTIME;
262 linux_common_clock_gettime(struct thread *td, clockid_t which,
266 struct thread *targettd;
268 int error, clockwhich;
273 error = linux_to_native_clockid(&nwhich, which);
276 "unsupported clock_gettime clockid %d", which);
277 LIN_SDT_PROBE1(time, linux_common_clock_gettime,
278 conversion_error, error);
283 case CLOCK_PROCESS_CPUTIME_ID:
285 clockwhich = LINUX_CPUCLOCK_WHICH(which);
286 pid = LINUX_CPUCLOCK_ID(which);
288 clockwhich = LINUX_CPUCLOCK_SCHED;
295 error = pget(pid, PGET_CANSEE, &p);
299 switch (clockwhich) {
300 case LINUX_CPUCLOCK_PROF:
302 calcru(p, &ru.ru_utime, &ru.ru_stime);
305 timevaladd(&ru.ru_utime, &ru.ru_stime);
306 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
308 case LINUX_CPUCLOCK_VIRT:
310 calcru(p, &ru.ru_utime, &ru.ru_stime);
313 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
315 case LINUX_CPUCLOCK_SCHED:
316 kern_process_cputime(p, tp);
326 case CLOCK_THREAD_CPUTIME_ID:
328 clockwhich = LINUX_CPUCLOCK_WHICH(which);
329 tid = LINUX_CPUCLOCK_ID(which);
331 clockwhich = LINUX_CPUCLOCK_SCHED;
339 targettd = linux_tdfind(td, tid, p->p_pid);
340 if (targettd == NULL)
343 switch (clockwhich) {
344 case LINUX_CPUCLOCK_PROF:
346 thread_lock(targettd);
347 rufetchtd(targettd, &ru);
348 thread_unlock(targettd);
351 timevaladd(&ru.ru_utime, &ru.ru_stime);
352 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
354 case LINUX_CPUCLOCK_VIRT:
356 thread_lock(targettd);
357 rufetchtd(targettd, &ru);
358 thread_unlock(targettd);
361 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
363 case LINUX_CPUCLOCK_SCHED:
366 kern_thread_cputime(targettd, tp);
376 error = kern_clock_gettime(td, nwhich, tp);
384 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
386 struct l_timespec lts;
390 error = linux_common_clock_gettime(td, args->which, &tp);
392 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
395 error = native_to_linux_timespec(<s, &tp);
398 error = copyout(<s, args->tp, sizeof(lts));
400 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
405 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
407 linux_clock_gettime64(struct thread *td, struct linux_clock_gettime64_args *args)
409 struct l_timespec64 lts;
413 error = linux_common_clock_gettime(td, args->which, &tp);
415 LIN_SDT_PROBE1(time, linux_clock_gettime64, gettime_error, error);
418 error = native_to_linux_timespec64(<s, &tp);
421 error = copyout(<s, args->tp, sizeof(lts));
423 LIN_SDT_PROBE1(time, linux_clock_gettime64, copyout_error, error);
430 linux_common_clock_settime(struct thread *td, clockid_t which,
436 error = linux_to_native_clockid(&nwhich, which);
439 "unsupported clock_settime clockid %d", which);
440 LIN_SDT_PROBE1(time, linux_common_clock_settime, conversion_error,
445 error = kern_clock_settime(td, nwhich, ts);
447 LIN_SDT_PROBE1(time, linux_common_clock_settime,
448 settime_error, error);
454 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
457 struct l_timespec lts;
460 error = copyin(args->tp, <s, sizeof(lts));
462 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
465 error = linux_to_native_timespec(&ts, <s);
467 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
470 return (linux_common_clock_settime(td, args->which, &ts));
473 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
475 linux_clock_settime64(struct thread *td, struct linux_clock_settime64_args *args)
478 struct l_timespec64 lts;
481 error = copyin(args->tp, <s, sizeof(lts));
483 LIN_SDT_PROBE1(time, linux_clock_settime64, copyin_error, error);
486 error = linux_to_native_timespec64(&ts, <s);
488 LIN_SDT_PROBE1(time, linux_clock_settime64, conversion_error,
490 return (linux_common_clock_settime(td, args->which, &ts));
495 linux_common_clock_getres(struct thread *td, clockid_t which,
499 int error, clockwhich;
504 error = linux_to_native_clockid(&nwhich, which);
507 "unsupported clock_getres clockid %d", which);
508 LIN_SDT_PROBE1(time, linux_common_clock_getres,
509 conversion_error, error);
514 * Check user supplied clock id in case of per-process
515 * or thread-specific cpu-time clock.
519 case CLOCK_THREAD_CPUTIME_ID:
520 tid = LINUX_CPUCLOCK_ID(which);
523 if (linux_tdfind(td, tid, p->p_pid) == NULL)
528 case CLOCK_PROCESS_CPUTIME_ID:
529 pid = LINUX_CPUCLOCK_ID(which);
531 error = pget(pid, PGET_CANSEE, &p);
541 LIN_SDT_PROBE0(time, linux_common_clock_getres, nullcall);
546 case CLOCK_THREAD_CPUTIME_ID:
547 case CLOCK_PROCESS_CPUTIME_ID:
548 clockwhich = LINUX_CPUCLOCK_WHICH(which);
550 * In both cases (when the clock id obtained by a call to
551 * clock_getcpuclockid() or using the clock
552 * ID CLOCK_PROCESS_CPUTIME_ID Linux hardcodes precision
553 * of clock. The same for the CLOCK_THREAD_CPUTIME_ID clock.
555 * See Linux posix_cpu_clock_getres() implementation.
557 if (which > 0 || clockwhich == LINUX_CPUCLOCK_SCHED) {
563 switch (clockwhich) {
564 case LINUX_CPUCLOCK_PROF:
567 case LINUX_CPUCLOCK_VIRT:
568 nwhich = CLOCK_VIRTUAL;
578 error = kern_clock_getres(td, nwhich, ts);
580 LIN_SDT_PROBE1(time, linux_common_clock_getres,
581 getres_error, error);
590 linux_clock_getres(struct thread *td,
591 struct linux_clock_getres_args *args)
594 struct l_timespec lts;
597 error = linux_common_clock_getres(td, args->which, &ts);
598 if (error != 0 || args->tp == NULL)
601 error = native_to_linux_timespec(<s, &ts);
604 error = copyout(<s, args->tp, sizeof(lts));
606 LIN_SDT_PROBE1(time, linux_clock_getres,
607 copyout_error, error);
611 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
613 linux_clock_getres_time64(struct thread *td,
614 struct linux_clock_getres_time64_args *args)
617 struct l_timespec64 lts;
620 error = linux_common_clock_getres(td, args->which, &ts);
621 if (error != 0 || args->tp == NULL)
624 error = native_to_linux_timespec64(<s, &ts);
627 error = copyout(<s, args->tp, sizeof(lts));
629 LIN_SDT_PROBE1(time, linux_clock_getres_time64,
630 copyout_error, error);
636 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
638 struct timespec *rmtp;
639 struct l_timespec lrqts, lrmts;
640 struct timespec rqts, rmts;
643 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
645 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
649 if (args->rmtp != NULL)
654 error = linux_to_native_timespec(&rqts, &lrqts);
656 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
659 error = kern_nanosleep(td, &rqts, rmtp);
660 if (error == EINTR && args->rmtp != NULL) {
661 error2 = native_to_linux_timespec(&lrmts, rmtp);
664 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
666 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
676 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
678 struct timespec *rmtp;
679 struct l_timespec lrqts, lrmts;
680 struct timespec rqts, rmts;
681 int error, error2, flags;
684 error = linux_to_native_timerflags(&flags, args->flags);
686 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
691 error = linux_to_native_clockid(&clockid, args->which);
694 "unsupported clock_nanosleep clockid %d", args->which);
695 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
700 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
702 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
707 if (args->rmtp != NULL)
712 error = linux_to_native_timespec(&rqts, &lrqts);
714 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
718 error = kern_clock_nanosleep(td, clockid, flags, &rqts, rmtp);
719 if (error == EINTR && (flags & TIMER_ABSTIME) == 0 &&
720 args->rmtp != NULL) {
721 error2 = native_to_linux_timespec(&lrmts, rmtp);
724 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
726 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
727 copyout_error, error2);