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>
72 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
75 * DTrace probes in this module.
77 LIN_SDT_PROBE_DEFINE2(time, native_to_linux_timespec, entry,
78 "struct l_timespec *", "struct timespec *");
79 LIN_SDT_PROBE_DEFINE0(time, native_to_linux_timespec, return);
80 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_timespec, entry,
81 "struct timespec *", "struct l_timespec *");
82 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_timespec, return, "int");
83 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_clockid, entry, "clockid_t *",
85 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
87 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
89 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, return, "int");
90 LIN_SDT_PROBE_DEFINE2(time, linux_clock_gettime, entry, "clockid_t",
91 "struct l_timespec *");
92 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, conversion_error, "int");
93 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
94 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
95 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, return, "int");
96 LIN_SDT_PROBE_DEFINE2(time, linux_clock_settime, entry, "clockid_t",
97 "struct l_timespec *");
98 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
99 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, settime_error, "int");
100 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
101 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, return, "int");
102 LIN_SDT_PROBE_DEFINE2(time, linux_clock_getres, entry, "clockid_t",
103 "struct l_timespec *");
104 LIN_SDT_PROBE_DEFINE0(time, linux_clock_getres, nullcall);
105 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, conversion_error, "int");
106 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, getres_error, "int");
107 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
108 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, return, "int");
109 LIN_SDT_PROBE_DEFINE2(time, linux_nanosleep, entry, "const struct l_timespec *",
110 "struct l_timespec *");
111 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, conversion_error, "int");
112 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
113 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
114 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, return, "int");
115 LIN_SDT_PROBE_DEFINE4(time, linux_clock_nanosleep, entry, "clockid_t", "int",
116 "struct l_timespec *", "struct l_timespec *");
117 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, conversion_error, "int");
118 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
119 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
120 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
121 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "int");
122 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 *n = CLOCK_MONOTONIC_FAST;
219 case LINUX_CLOCK_BOOTTIME:
222 case LINUX_CLOCK_MONOTONIC_RAW:
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);
271 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
273 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
278 case CLOCK_PROCESS_CPUTIME_ID:
279 if (args->which < 0) {
280 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
281 pid = LINUX_CPUCLOCK_ID(args->which);
283 clockwhich = LINUX_CPUCLOCK_SCHED;
290 error = pget(pid, PGET_CANSEE, &p);
294 switch (clockwhich) {
295 case LINUX_CPUCLOCK_PROF:
297 calcru(p, &ru.ru_utime, &ru.ru_stime);
300 timevaladd(&ru.ru_utime, &ru.ru_stime);
301 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
303 case LINUX_CPUCLOCK_VIRT:
305 calcru(p, &ru.ru_utime, &ru.ru_stime);
308 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
310 case LINUX_CPUCLOCK_SCHED:
311 kern_process_cputime(p, &tp);
321 case CLOCK_THREAD_CPUTIME_ID:
322 if (args->which < 0) {
323 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
324 tid = LINUX_CPUCLOCK_ID(args->which);
326 clockwhich = LINUX_CPUCLOCK_SCHED;
334 targettd = linux_tdfind(td, tid, p->p_pid);
335 if (targettd == NULL)
338 switch (clockwhich) {
339 case LINUX_CPUCLOCK_PROF:
341 thread_lock(targettd);
342 rufetchtd(targettd, &ru);
343 thread_unlock(targettd);
346 timevaladd(&ru.ru_utime, &ru.ru_stime);
347 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
349 case LINUX_CPUCLOCK_VIRT:
351 thread_lock(targettd);
352 rufetchtd(targettd, &ru);
353 thread_unlock(targettd);
356 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
358 case LINUX_CPUCLOCK_SCHED:
361 kern_thread_cputime(targettd, &tp);
371 error = kern_clock_gettime(td, nwhich, &tp);
375 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
376 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
379 error = native_to_linux_timespec(<s, &tp);
382 error = copyout(<s, args->tp, sizeof lts);
384 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
386 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
391 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
394 struct l_timespec lts;
398 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
400 error = linux_to_native_clockid(&nwhich, args->which);
402 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
404 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
407 error = copyin(args->tp, <s, sizeof lts);
409 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
410 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
413 error = linux_to_native_timespec(&ts, <s);
415 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
417 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
421 error = kern_clock_settime(td, nwhich, &ts);
423 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
425 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
430 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
434 struct l_timespec lts;
435 int error, clockwhich;
440 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
442 error = linux_to_native_clockid(&nwhich, args->which);
444 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
446 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
451 * Check user supplied clock id in case of per-process
452 * or thread-specific cpu-time clock.
454 if (args->which < 0) {
456 case CLOCK_THREAD_CPUTIME_ID:
457 tid = LINUX_CPUCLOCK_ID(args->which);
460 if (linux_tdfind(td, tid, p->p_pid) == NULL)
465 case CLOCK_PROCESS_CPUTIME_ID:
466 pid = LINUX_CPUCLOCK_ID(args->which);
468 error = pget(pid, PGET_CANSEE, &p);
477 if (args->tp == NULL) {
478 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
479 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
484 case CLOCK_THREAD_CPUTIME_ID:
485 case CLOCK_PROCESS_CPUTIME_ID:
486 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
488 * In both cases (when the clock id obtained by a call to
489 * clock_getcpuclockid() or using the clock
490 * ID CLOCK_PROCESS_CPUTIME_ID Linux hardcodes precision
491 * of clock. The same for the CLOCK_THREAD_CPUTIME_ID clock.
493 * See Linux posix_cpu_clock_getres() implementation.
495 if (args->which > 0 || clockwhich == LINUX_CPUCLOCK_SCHED) {
501 switch (clockwhich) {
502 case LINUX_CPUCLOCK_PROF:
505 case LINUX_CPUCLOCK_VIRT:
506 nwhich = CLOCK_VIRTUAL;
516 error = kern_clock_getres(td, nwhich, &ts);
518 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
519 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
524 error = native_to_linux_timespec(<s, &ts);
527 error = copyout(<s, args->tp, sizeof lts);
529 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
531 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
536 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
538 struct timespec *rmtp;
539 struct l_timespec lrqts, lrmts;
540 struct timespec rqts, rmts;
543 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
545 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
547 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
548 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
552 if (args->rmtp != NULL)
557 error = linux_to_native_timespec(&rqts, &lrqts);
559 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
560 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
563 error = kern_nanosleep(td, &rqts, rmtp);
564 if (error == EINTR && args->rmtp != NULL) {
565 error2 = native_to_linux_timespec(&lrmts, rmtp);
568 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
570 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
572 LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
577 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
582 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
584 struct timespec *rmtp;
585 struct l_timespec lrqts, lrmts;
586 struct timespec rqts, rmts;
587 int error, error2, flags;
590 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
591 args->flags, args->rqtp, args->rmtp);
593 error = linux_to_native_timerflags(&flags, args->flags);
595 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
597 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
601 error = linux_to_native_clockid(&clockid, args->which);
603 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
605 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
609 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
611 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
613 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
617 if (args->rmtp != NULL)
622 error = linux_to_native_timespec(&rqts, &lrqts);
624 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
626 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
629 error = kern_clock_nanosleep(td, clockid, flags, &rqts, rmtp);
630 if (error == EINTR && (flags & TIMER_ABSTIME) == 0 &&
631 args->rmtp != NULL) {
632 error2 = native_to_linux_timespec(&lrmts, rmtp);
635 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
637 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
638 copyout_error, error2);
639 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
645 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);