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_timer.h>
71 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
74 * DTrace probes in this module.
76 LIN_SDT_PROBE_DEFINE2(time, native_to_linux_timespec, entry,
77 "struct l_timespec *", "struct timespec *");
78 LIN_SDT_PROBE_DEFINE0(time, native_to_linux_timespec, return);
79 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_timespec, entry,
80 "struct timespec *", "struct l_timespec *");
81 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_timespec, return, "int");
82 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_clockid, entry, "clockid_t *",
84 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
86 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
88 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, return, "int");
89 LIN_SDT_PROBE_DEFINE2(time, linux_clock_gettime, entry, "clockid_t",
90 "struct l_timespec *");
91 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, conversion_error, "int");
92 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
93 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
94 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, return, "int");
95 LIN_SDT_PROBE_DEFINE2(time, linux_clock_settime, entry, "clockid_t",
96 "struct l_timespec *");
97 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
98 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, settime_error, "int");
99 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
100 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, return, "int");
101 LIN_SDT_PROBE_DEFINE2(time, linux_clock_getres, entry, "clockid_t",
102 "struct l_timespec *");
103 LIN_SDT_PROBE_DEFINE0(time, linux_clock_getres, nullcall);
104 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, conversion_error, "int");
105 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, getres_error, "int");
106 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
107 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, return, "int");
108 LIN_SDT_PROBE_DEFINE2(time, linux_nanosleep, entry, "const struct l_timespec *",
109 "struct l_timespec *");
110 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, conversion_error, "int");
111 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
112 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
113 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, return, "int");
114 LIN_SDT_PROBE_DEFINE4(time, linux_clock_nanosleep, entry, "clockid_t", "int",
115 "struct l_timespec *", "struct l_timespec *");
116 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, conversion_error, "int");
117 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
118 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
119 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
120 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "int");
121 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, return, "int");
125 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
128 LIN_SDT_PROBE2(time, native_to_linux_timespec, entry, ltp, ntp);
129 #ifdef COMPAT_LINUX32
130 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
133 ltp->tv_sec = ntp->tv_sec;
134 ltp->tv_nsec = ntp->tv_nsec;
136 LIN_SDT_PROBE0(time, native_to_linux_timespec, return);
141 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
144 LIN_SDT_PROBE2(time, linux_to_native_timespec, entry, ntp, ltp);
146 if (ltp->tv_sec < 0 || ltp->tv_nsec < 0 || ltp->tv_nsec > 999999999) {
147 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, EINVAL);
150 ntp->tv_sec = ltp->tv_sec;
151 ntp->tv_nsec = ltp->tv_nsec;
153 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, 0);
158 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
162 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
164 error = native_to_linux_timespec(<p->it_value, &ntp->it_interval);
169 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
173 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
175 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
180 linux_to_native_clockid(clockid_t *n, clockid_t l)
183 LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
187 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
189 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
192 if (LINUX_CPUCLOCK_PERTHREAD(l))
193 *n = CLOCK_THREAD_CPUTIME_ID;
195 *n = CLOCK_PROCESS_CPUTIME_ID;
200 case LINUX_CLOCK_REALTIME:
203 case LINUX_CLOCK_MONOTONIC:
204 *n = CLOCK_MONOTONIC;
206 case LINUX_CLOCK_REALTIME_COARSE:
207 *n = CLOCK_REALTIME_FAST;
209 case LINUX_CLOCK_MONOTONIC_COARSE:
210 *n = CLOCK_MONOTONIC_FAST;
212 case LINUX_CLOCK_BOOTTIME:
215 case LINUX_CLOCK_MONOTONIC_RAW:
216 case LINUX_CLOCK_REALTIME_ALARM:
217 case LINUX_CLOCK_BOOTTIME_ALARM:
218 case LINUX_CLOCK_SGI_CYCLE:
219 case LINUX_CLOCK_TAI:
220 LIN_SDT_PROBE1(time, linux_to_native_clockid,
221 unsupported_clockid, l);
222 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
225 LIN_SDT_PROBE1(time, linux_to_native_clockid,
227 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
231 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
236 linux_to_native_timerflags(int *nflags, int flags)
239 if (flags & ~LINUX_TIMER_ABSTIME)
242 if (flags & LINUX_TIMER_ABSTIME)
243 *nflags |= TIMER_ABSTIME;
248 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
250 struct l_timespec lts;
253 struct thread *targettd;
255 int error, clockwhich;
256 clockid_t nwhich = 0; /* XXX: GCC */
260 LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
262 error = linux_to_native_clockid(&nwhich, args->which);
264 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
266 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
271 case CLOCK_PROCESS_CPUTIME_ID:
272 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
273 pid = LINUX_CPUCLOCK_ID(args->which);
278 error = pget(pid, PGET_CANSEE, &p);
282 switch (clockwhich) {
283 case LINUX_CPUCLOCK_PROF:
285 calcru(p, &ru.ru_utime, &ru.ru_stime);
288 timevaladd(&ru.ru_utime, &ru.ru_stime);
289 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
291 case LINUX_CPUCLOCK_VIRT:
293 calcru(p, &ru.ru_utime, &ru.ru_stime);
296 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
298 case LINUX_CPUCLOCK_SCHED:
300 error = kern_clock_getcpuclockid2(td, pid,
301 CPUCLOCK_WHICH_PID, &nwhich);
304 error = kern_clock_gettime(td, nwhich, &tp);
313 case CLOCK_THREAD_CPUTIME_ID:
314 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
316 tid = LINUX_CPUCLOCK_ID(args->which);
321 targettd = tdfind(tid, p->p_pid);
322 if (targettd == NULL)
325 switch (clockwhich) {
326 case LINUX_CPUCLOCK_PROF:
328 thread_lock(targettd);
329 rufetchtd(targettd, &ru);
330 thread_unlock(targettd);
333 timevaladd(&ru.ru_utime, &ru.ru_stime);
334 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
336 case LINUX_CPUCLOCK_VIRT:
338 thread_lock(targettd);
339 rufetchtd(targettd, &ru);
340 thread_unlock(targettd);
343 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
345 case LINUX_CPUCLOCK_SCHED:
346 error = kern_clock_getcpuclockid2(td, tid,
347 CPUCLOCK_WHICH_TID, &nwhich);
351 error = kern_clock_gettime(td, nwhich, &tp);
360 error = kern_clock_gettime(td, nwhich, &tp);
364 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
365 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
368 error = native_to_linux_timespec(<s, &tp);
371 error = copyout(<s, args->tp, sizeof lts);
373 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
375 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
380 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
383 struct l_timespec lts;
385 clockid_t nwhich = 0; /* XXX: GCC */
387 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
389 error = linux_to_native_clockid(&nwhich, args->which);
391 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
393 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
396 error = copyin(args->tp, <s, sizeof lts);
398 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
399 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
402 error = linux_to_native_timespec(&ts, <s);
404 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
406 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
410 error = kern_clock_settime(td, nwhich, &ts);
412 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
414 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
419 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
423 struct l_timespec lts;
424 int error, clockwhich;
425 clockid_t nwhich = 0; /* XXX: GCC */
429 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
431 error = linux_to_native_clockid(&nwhich, args->which);
433 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
435 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
440 * Check user supplied clock id in case of per-process
441 * or thread-specific cpu-time clock.
444 case CLOCK_THREAD_CPUTIME_ID:
445 tid = LINUX_CPUCLOCK_ID(args->which);
448 if (tdfind(tid, p->p_pid) == NULL)
453 case CLOCK_PROCESS_CPUTIME_ID:
454 pid = LINUX_CPUCLOCK_ID(args->which);
456 error = pget(pid, PGET_CANSEE, &p);
464 if (args->tp == NULL) {
465 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
466 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
471 case CLOCK_THREAD_CPUTIME_ID:
472 case CLOCK_PROCESS_CPUTIME_ID:
473 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
474 switch (clockwhich) {
475 case LINUX_CPUCLOCK_PROF:
478 case LINUX_CPUCLOCK_VIRT:
479 nwhich = CLOCK_VIRTUAL;
481 case LINUX_CPUCLOCK_SCHED:
491 error = kern_clock_getres(td, nwhich, &ts);
493 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
494 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
497 error = native_to_linux_timespec(<s, &ts);
500 error = copyout(<s, args->tp, sizeof lts);
502 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
504 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
509 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
511 struct timespec *rmtp;
512 struct l_timespec lrqts, lrmts;
513 struct timespec rqts, rmts;
516 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
518 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
520 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
521 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
525 if (args->rmtp != NULL)
530 error = linux_to_native_timespec(&rqts, &lrqts);
532 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
533 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
536 error = kern_nanosleep(td, &rqts, rmtp);
537 if (error == EINTR && args->rmtp != NULL) {
538 error2 = native_to_linux_timespec(&lrmts, rmtp);
541 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
543 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
545 LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
550 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
555 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
557 struct timespec *rmtp;
558 struct l_timespec lrqts, lrmts;
559 struct timespec rqts, rmts;
560 int error, error2, flags;
563 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
564 args->flags, args->rqtp, args->rmtp);
566 error = linux_to_native_timerflags(&flags, args->flags);
568 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
570 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
574 error = linux_to_native_clockid(&clockid, args->which);
576 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
578 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
582 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
584 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
586 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
590 if (args->rmtp != NULL)
595 error = linux_to_native_timespec(&rqts, &lrqts);
597 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
599 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
602 error = kern_clock_nanosleep(td, clockid, flags, &rqts, rmtp);
603 if (error == EINTR && (flags & TIMER_ABSTIME) == 0 &&
604 args->rmtp != NULL) {
605 error2 = native_to_linux_timespec(&lrmts, rmtp);
608 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
610 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
611 copyout_error, error2);
612 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
618 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);