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");
126 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
129 LIN_SDT_PROBE2(time, native_to_linux_timespec, entry, ltp, ntp);
130 #ifdef COMPAT_LINUX32
131 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
134 ltp->tv_sec = ntp->tv_sec;
135 ltp->tv_nsec = ntp->tv_nsec;
137 LIN_SDT_PROBE0(time, native_to_linux_timespec, return);
142 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
145 LIN_SDT_PROBE2(time, linux_to_native_timespec, entry, ntp, ltp);
147 if (ltp->tv_sec < 0 || ltp->tv_nsec < 0 || ltp->tv_nsec > 999999999) {
148 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, EINVAL);
151 ntp->tv_sec = ltp->tv_sec;
152 ntp->tv_nsec = ltp->tv_nsec;
154 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, 0);
159 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
163 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
165 error = native_to_linux_timespec(<p->it_value, &ntp->it_interval);
170 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
174 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
176 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
181 linux_to_native_clockid(clockid_t *n, clockid_t l)
184 LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
188 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
190 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
193 if (LINUX_CPUCLOCK_PERTHREAD(l))
194 *n = CLOCK_THREAD_CPUTIME_ID;
196 *n = CLOCK_PROCESS_CPUTIME_ID;
201 case LINUX_CLOCK_REALTIME:
204 case LINUX_CLOCK_MONOTONIC:
205 *n = CLOCK_MONOTONIC;
207 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
208 *n = CLOCK_PROCESS_CPUTIME_ID;
210 case LINUX_CLOCK_THREAD_CPUTIME_ID:
211 *n = CLOCK_THREAD_CPUTIME_ID;
213 case LINUX_CLOCK_REALTIME_COARSE:
214 *n = CLOCK_REALTIME_FAST;
216 case LINUX_CLOCK_MONOTONIC_COARSE:
217 case LINUX_CLOCK_MONOTONIC_RAW:
218 *n = CLOCK_MONOTONIC_FAST;
220 case LINUX_CLOCK_BOOTTIME:
223 case LINUX_CLOCK_REALTIME_ALARM:
224 case LINUX_CLOCK_BOOTTIME_ALARM:
225 case LINUX_CLOCK_SGI_CYCLE:
226 case LINUX_CLOCK_TAI:
227 LIN_SDT_PROBE1(time, linux_to_native_clockid,
228 unsupported_clockid, l);
229 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
232 LIN_SDT_PROBE1(time, linux_to_native_clockid,
234 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
238 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
243 linux_to_native_timerflags(int *nflags, int flags)
246 if (flags & ~LINUX_TIMER_ABSTIME)
249 if (flags & LINUX_TIMER_ABSTIME)
250 *nflags |= TIMER_ABSTIME;
255 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
257 struct l_timespec lts;
260 struct thread *targettd;
262 int error, clockwhich;
267 LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
269 error = linux_to_native_clockid(&nwhich, args->which);
272 "unsupported clock_gettime clockid %d", args->which);
273 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
275 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
280 case CLOCK_PROCESS_CPUTIME_ID:
281 if (args->which < 0) {
282 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
283 pid = LINUX_CPUCLOCK_ID(args->which);
285 clockwhich = LINUX_CPUCLOCK_SCHED;
292 error = pget(pid, PGET_CANSEE, &p);
296 switch (clockwhich) {
297 case LINUX_CPUCLOCK_PROF:
299 calcru(p, &ru.ru_utime, &ru.ru_stime);
302 timevaladd(&ru.ru_utime, &ru.ru_stime);
303 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
305 case LINUX_CPUCLOCK_VIRT:
307 calcru(p, &ru.ru_utime, &ru.ru_stime);
310 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
312 case LINUX_CPUCLOCK_SCHED:
313 kern_process_cputime(p, &tp);
323 case CLOCK_THREAD_CPUTIME_ID:
324 if (args->which < 0) {
325 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
326 tid = LINUX_CPUCLOCK_ID(args->which);
328 clockwhich = LINUX_CPUCLOCK_SCHED;
336 targettd = linux_tdfind(td, tid, p->p_pid);
337 if (targettd == NULL)
340 switch (clockwhich) {
341 case LINUX_CPUCLOCK_PROF:
343 thread_lock(targettd);
344 rufetchtd(targettd, &ru);
345 thread_unlock(targettd);
348 timevaladd(&ru.ru_utime, &ru.ru_stime);
349 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
351 case LINUX_CPUCLOCK_VIRT:
353 thread_lock(targettd);
354 rufetchtd(targettd, &ru);
355 thread_unlock(targettd);
358 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
360 case LINUX_CPUCLOCK_SCHED:
363 kern_thread_cputime(targettd, &tp);
373 error = kern_clock_gettime(td, nwhich, &tp);
377 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
378 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
381 error = native_to_linux_timespec(<s, &tp);
384 error = copyout(<s, args->tp, sizeof lts);
386 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
388 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
393 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
396 struct l_timespec lts;
400 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
402 error = linux_to_native_clockid(&nwhich, args->which);
405 "unsupported clock_settime clockid %d", args->which);
406 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
408 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
411 error = copyin(args->tp, <s, sizeof lts);
413 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
414 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
417 error = linux_to_native_timespec(&ts, <s);
419 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
421 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
425 error = kern_clock_settime(td, nwhich, &ts);
427 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
429 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
434 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
438 struct l_timespec lts;
439 int error, clockwhich;
444 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
446 error = linux_to_native_clockid(&nwhich, args->which);
449 "unsupported clock_getres clockid %d", args->which);
450 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
452 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
457 * Check user supplied clock id in case of per-process
458 * or thread-specific cpu-time clock.
460 if (args->which < 0) {
462 case CLOCK_THREAD_CPUTIME_ID:
463 tid = LINUX_CPUCLOCK_ID(args->which);
466 if (linux_tdfind(td, tid, p->p_pid) == NULL)
471 case CLOCK_PROCESS_CPUTIME_ID:
472 pid = LINUX_CPUCLOCK_ID(args->which);
474 error = pget(pid, PGET_CANSEE, &p);
483 if (args->tp == NULL) {
484 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
485 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
490 case CLOCK_THREAD_CPUTIME_ID:
491 case CLOCK_PROCESS_CPUTIME_ID:
492 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
494 * In both cases (when the clock id obtained by a call to
495 * clock_getcpuclockid() or using the clock
496 * ID CLOCK_PROCESS_CPUTIME_ID Linux hardcodes precision
497 * of clock. The same for the CLOCK_THREAD_CPUTIME_ID clock.
499 * See Linux posix_cpu_clock_getres() implementation.
501 if (args->which > 0 || clockwhich == LINUX_CPUCLOCK_SCHED) {
507 switch (clockwhich) {
508 case LINUX_CPUCLOCK_PROF:
511 case LINUX_CPUCLOCK_VIRT:
512 nwhich = CLOCK_VIRTUAL;
522 error = kern_clock_getres(td, nwhich, &ts);
524 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
525 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
530 error = native_to_linux_timespec(<s, &ts);
533 error = copyout(<s, args->tp, sizeof lts);
535 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
537 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
542 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
544 struct timespec *rmtp;
545 struct l_timespec lrqts, lrmts;
546 struct timespec rqts, rmts;
549 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
551 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
553 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
554 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
558 if (args->rmtp != NULL)
563 error = linux_to_native_timespec(&rqts, &lrqts);
565 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
566 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
569 error = kern_nanosleep(td, &rqts, rmtp);
570 if (error == EINTR && args->rmtp != NULL) {
571 error2 = native_to_linux_timespec(&lrmts, rmtp);
574 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
576 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
578 LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
583 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
588 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
590 struct timespec *rmtp;
591 struct l_timespec lrqts, lrmts;
592 struct timespec rqts, rmts;
593 int error, error2, flags;
596 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
597 args->flags, args->rqtp, args->rmtp);
599 error = linux_to_native_timerflags(&flags, args->flags);
601 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
603 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
607 error = linux_to_native_clockid(&clockid, args->which);
610 "unsupported clock_nanosleep clockid %d", args->which);
611 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
613 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
617 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
619 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
621 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
625 if (args->rmtp != NULL)
630 error = linux_to_native_timespec(&rqts, &lrqts);
632 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
634 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
637 error = kern_clock_nanosleep(td, clockid, flags, &rqts, rmtp);
638 if (error == EINTR && (flags & TIMER_ABSTIME) == 0 &&
639 args->rmtp != NULL) {
640 error2 = native_to_linux_timespec(&lrmts, rmtp);
643 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
645 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
646 copyout_error, error2);
647 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
653 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);