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_DEFINE2(time, native_to_linux_timespec, entry,
79 "struct l_timespec *", "struct timespec *");
80 LIN_SDT_PROBE_DEFINE0(time, native_to_linux_timespec, return);
81 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_timespec, entry,
82 "struct timespec *", "struct l_timespec *");
83 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_timespec, return, "int");
84 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_clockid, entry, "clockid_t *",
86 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
88 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
90 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, return, "int");
91 LIN_SDT_PROBE_DEFINE2(time, linux_clock_gettime, entry, "clockid_t",
92 "struct l_timespec *");
93 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, conversion_error, "int");
94 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
95 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
96 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, return, "int");
97 LIN_SDT_PROBE_DEFINE2(time, linux_clock_settime, entry, "clockid_t",
98 "struct l_timespec *");
99 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
100 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, settime_error, "int");
101 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
102 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, return, "int");
103 LIN_SDT_PROBE_DEFINE2(time, linux_clock_getres, entry, "clockid_t",
104 "struct l_timespec *");
105 LIN_SDT_PROBE_DEFINE0(time, linux_clock_getres, nullcall);
106 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, conversion_error, "int");
107 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, getres_error, "int");
108 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
109 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, return, "int");
110 LIN_SDT_PROBE_DEFINE2(time, linux_nanosleep, entry, "const struct l_timespec *",
111 "struct l_timespec *");
112 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, conversion_error, "int");
113 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
114 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
115 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, return, "int");
116 LIN_SDT_PROBE_DEFINE4(time, linux_clock_nanosleep, entry, "clockid_t", "int",
117 "struct l_timespec *", "struct l_timespec *");
118 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, conversion_error, "int");
119 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
120 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
121 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
122 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "int");
123 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, return, "int");
127 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
130 LIN_SDT_PROBE2(time, native_to_linux_timespec, entry, ltp, ntp);
131 #ifdef COMPAT_LINUX32
132 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
135 ltp->tv_sec = ntp->tv_sec;
136 ltp->tv_nsec = ntp->tv_nsec;
138 LIN_SDT_PROBE0(time, native_to_linux_timespec, return);
143 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
146 LIN_SDT_PROBE2(time, linux_to_native_timespec, entry, ntp, ltp);
148 if (ltp->tv_sec < 0 || ltp->tv_nsec < 0 || ltp->tv_nsec > 999999999) {
149 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, EINVAL);
152 ntp->tv_sec = ltp->tv_sec;
153 ntp->tv_nsec = ltp->tv_nsec;
155 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, 0);
160 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
164 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
166 error = native_to_linux_timespec(<p->it_value, &ntp->it_interval);
171 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
175 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
177 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
182 linux_to_native_clockid(clockid_t *n, clockid_t l)
185 LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
189 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
191 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
194 if (LINUX_CPUCLOCK_PERTHREAD(l))
195 *n = CLOCK_THREAD_CPUTIME_ID;
197 *n = CLOCK_PROCESS_CPUTIME_ID;
202 case LINUX_CLOCK_REALTIME:
205 case LINUX_CLOCK_MONOTONIC:
206 *n = CLOCK_MONOTONIC;
208 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
209 *n = CLOCK_PROCESS_CPUTIME_ID;
211 case LINUX_CLOCK_THREAD_CPUTIME_ID:
212 *n = CLOCK_THREAD_CPUTIME_ID;
214 case LINUX_CLOCK_REALTIME_COARSE:
215 *n = CLOCK_REALTIME_FAST;
217 case LINUX_CLOCK_MONOTONIC_COARSE:
218 *n = CLOCK_MONOTONIC_FAST;
220 case LINUX_CLOCK_BOOTTIME:
223 case LINUX_CLOCK_MONOTONIC_RAW:
224 case LINUX_CLOCK_REALTIME_ALARM:
225 case LINUX_CLOCK_BOOTTIME_ALARM:
226 case LINUX_CLOCK_SGI_CYCLE:
227 case LINUX_CLOCK_TAI:
228 LIN_SDT_PROBE1(time, linux_to_native_clockid,
229 unsupported_clockid, l);
230 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
233 LIN_SDT_PROBE1(time, linux_to_native_clockid,
235 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
239 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
244 linux_to_native_timerflags(int *nflags, int flags)
247 if (flags & ~LINUX_TIMER_ABSTIME)
250 if (flags & LINUX_TIMER_ABSTIME)
251 *nflags |= TIMER_ABSTIME;
256 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
258 struct l_timespec lts;
261 struct thread *targettd;
263 int error, clockwhich;
268 LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
270 error = linux_to_native_clockid(&nwhich, args->which);
273 "unsupported clock_gettime clockid %d", args->which);
274 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
276 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
281 case CLOCK_PROCESS_CPUTIME_ID:
282 if (args->which < 0) {
283 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
284 pid = LINUX_CPUCLOCK_ID(args->which);
286 clockwhich = LINUX_CPUCLOCK_SCHED;
293 error = pget(pid, PGET_CANSEE, &p);
297 switch (clockwhich) {
298 case LINUX_CPUCLOCK_PROF:
300 calcru(p, &ru.ru_utime, &ru.ru_stime);
303 timevaladd(&ru.ru_utime, &ru.ru_stime);
304 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
306 case LINUX_CPUCLOCK_VIRT:
308 calcru(p, &ru.ru_utime, &ru.ru_stime);
311 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
313 case LINUX_CPUCLOCK_SCHED:
314 kern_process_cputime(p, &tp);
324 case CLOCK_THREAD_CPUTIME_ID:
325 if (args->which < 0) {
326 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
327 tid = LINUX_CPUCLOCK_ID(args->which);
329 clockwhich = LINUX_CPUCLOCK_SCHED;
337 targettd = linux_tdfind(td, tid, p->p_pid);
338 if (targettd == NULL)
341 switch (clockwhich) {
342 case LINUX_CPUCLOCK_PROF:
344 thread_lock(targettd);
345 rufetchtd(targettd, &ru);
346 thread_unlock(targettd);
349 timevaladd(&ru.ru_utime, &ru.ru_stime);
350 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
352 case LINUX_CPUCLOCK_VIRT:
354 thread_lock(targettd);
355 rufetchtd(targettd, &ru);
356 thread_unlock(targettd);
359 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
361 case LINUX_CPUCLOCK_SCHED:
364 kern_thread_cputime(targettd, &tp);
374 error = kern_clock_gettime(td, nwhich, &tp);
378 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
379 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
382 error = native_to_linux_timespec(<s, &tp);
385 error = copyout(<s, args->tp, sizeof lts);
387 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
389 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
394 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
397 struct l_timespec lts;
401 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
403 error = linux_to_native_clockid(&nwhich, args->which);
406 "unsupported clock_settime clockid %d", args->which);
407 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
409 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
412 error = copyin(args->tp, <s, sizeof lts);
414 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
415 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
418 error = linux_to_native_timespec(&ts, <s);
420 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
422 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
426 error = kern_clock_settime(td, nwhich, &ts);
428 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
430 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
435 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
439 struct l_timespec lts;
440 int error, clockwhich;
445 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
447 error = linux_to_native_clockid(&nwhich, args->which);
450 "unsupported clock_getres clockid %d", args->which);
451 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
453 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
458 * Check user supplied clock id in case of per-process
459 * or thread-specific cpu-time clock.
461 if (args->which < 0) {
463 case CLOCK_THREAD_CPUTIME_ID:
464 tid = LINUX_CPUCLOCK_ID(args->which);
467 if (linux_tdfind(td, tid, p->p_pid) == NULL)
472 case CLOCK_PROCESS_CPUTIME_ID:
473 pid = LINUX_CPUCLOCK_ID(args->which);
475 error = pget(pid, PGET_CANSEE, &p);
484 if (args->tp == NULL) {
485 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
486 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
491 case CLOCK_THREAD_CPUTIME_ID:
492 case CLOCK_PROCESS_CPUTIME_ID:
493 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
495 * In both cases (when the clock id obtained by a call to
496 * clock_getcpuclockid() or using the clock
497 * ID CLOCK_PROCESS_CPUTIME_ID Linux hardcodes precision
498 * of clock. The same for the CLOCK_THREAD_CPUTIME_ID clock.
500 * See Linux posix_cpu_clock_getres() implementation.
502 if (args->which > 0 || clockwhich == LINUX_CPUCLOCK_SCHED) {
508 switch (clockwhich) {
509 case LINUX_CPUCLOCK_PROF:
512 case LINUX_CPUCLOCK_VIRT:
513 nwhich = CLOCK_VIRTUAL;
523 error = kern_clock_getres(td, nwhich, &ts);
525 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
526 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
531 error = native_to_linux_timespec(<s, &ts);
534 error = copyout(<s, args->tp, sizeof lts);
536 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
538 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
543 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
545 struct timespec *rmtp;
546 struct l_timespec lrqts, lrmts;
547 struct timespec rqts, rmts;
550 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
552 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
554 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
555 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
559 if (args->rmtp != NULL)
564 error = linux_to_native_timespec(&rqts, &lrqts);
566 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
567 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
570 error = kern_nanosleep(td, &rqts, rmtp);
571 if (error == EINTR && args->rmtp != NULL) {
572 error2 = native_to_linux_timespec(&lrmts, rmtp);
575 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
577 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
579 LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
584 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
589 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
591 struct timespec *rmtp;
592 struct l_timespec lrqts, lrmts;
593 struct timespec rqts, rmts;
594 int error, error2, flags;
597 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
598 args->flags, args->rqtp, args->rmtp);
600 error = linux_to_native_timerflags(&flags, args->flags);
602 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
604 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
608 error = linux_to_native_clockid(&clockid, args->which);
611 "unsupported clock_nanosleep clockid %d", args->which);
612 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
614 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
618 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
620 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
622 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
626 if (args->rmtp != NULL)
631 error = linux_to_native_timespec(&rqts, &lrqts);
633 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
635 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
638 error = kern_clock_nanosleep(td, clockid, flags, &rqts, rmtp);
639 if (error == EINTR && (flags & TIMER_ABSTIME) == 0 &&
640 args->rmtp != NULL) {
641 error2 = native_to_linux_timespec(&lrmts, rmtp);
644 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
646 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
647 copyout_error, error2);
648 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
654 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);