1 /* $NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $");
38 #include "opt_compat.h"
40 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/ucred.h>
44 #include <sys/limits.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/resourcevar.h>
49 #include <sys/signal.h>
50 #include <sys/stdint.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysproto.h>
54 #include <sys/systm.h>
58 #include <machine/../linux32/linux.h>
59 #include <machine/../linux32/linux32_proto.h>
61 #include <machine/../linux/linux.h>
62 #include <machine/../linux/linux_proto.h>
65 #include <compat/linux/linux_dtrace.h>
66 #include <compat/linux/linux_timer.h>
69 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
72 * DTrace probes in this module.
74 LIN_SDT_PROBE_DEFINE2(time, native_to_linux_timespec, entry,
75 "struct l_timespec *", "struct timespec *");
76 LIN_SDT_PROBE_DEFINE0(time, native_to_linux_timespec, return);
77 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_timespec, entry,
78 "struct timespec *", "struct l_timespec *");
79 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_timespec, return, "int");
80 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_clockid, entry, "clockid_t *",
82 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
84 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
86 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, return, "int");
87 LIN_SDT_PROBE_DEFINE2(time, linux_clock_gettime, entry, "clockid_t",
88 "struct l_timespec *");
89 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, conversion_error, "int");
90 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
91 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
92 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, return, "int");
93 LIN_SDT_PROBE_DEFINE2(time, linux_clock_settime, entry, "clockid_t",
94 "struct l_timespec *");
95 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
96 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, settime_error, "int");
97 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
98 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, return, "int");
99 LIN_SDT_PROBE_DEFINE2(time, linux_clock_getres, entry, "clockid_t",
100 "struct l_timespec *");
101 LIN_SDT_PROBE_DEFINE0(time, linux_clock_getres, nullcall);
102 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, conversion_error, "int");
103 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, getres_error, "int");
104 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
105 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, return, "int");
106 LIN_SDT_PROBE_DEFINE2(time, linux_nanosleep, entry, "const struct l_timespec *",
107 "struct l_timespec *");
108 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, conversion_error, "int");
109 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
110 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
111 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, return, "int");
112 LIN_SDT_PROBE_DEFINE4(time, linux_clock_nanosleep, entry, "clockid_t", "int",
113 "struct l_timespec *", "struct l_timespec *");
114 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, conversion_error, "int");
115 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
116 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
117 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
118 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "int");
119 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, return, "int");
123 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
126 LIN_SDT_PROBE2(time, native_to_linux_timespec, entry, ltp, ntp);
127 #ifdef COMPAT_LINUX32
128 if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
131 ltp->tv_sec = ntp->tv_sec;
132 ltp->tv_nsec = ntp->tv_nsec;
134 LIN_SDT_PROBE0(time, native_to_linux_timespec, return);
139 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
142 LIN_SDT_PROBE2(time, linux_to_native_timespec, entry, ntp, ltp);
144 if (ltp->tv_sec < 0 || ltp->tv_nsec < 0 || ltp->tv_nsec > 999999999) {
145 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, EINVAL);
148 ntp->tv_sec = ltp->tv_sec;
149 ntp->tv_nsec = ltp->tv_nsec;
151 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, 0);
156 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
160 error = native_to_linux_timespec(<p->it_interval, &ntp->it_interval);
162 error = native_to_linux_timespec(<p->it_value, &ntp->it_interval);
167 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
171 error = linux_to_native_timespec(&ntp->it_interval, <p->it_interval);
173 error = linux_to_native_timespec(&ntp->it_value, <p->it_value);
178 linux_to_native_clockid(clockid_t *n, clockid_t l)
181 LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
185 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
187 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
190 if (LINUX_CPUCLOCK_PERTHREAD(l))
191 *n = CLOCK_THREAD_CPUTIME_ID;
193 *n = CLOCK_PROCESS_CPUTIME_ID;
198 case LINUX_CLOCK_REALTIME:
201 case LINUX_CLOCK_MONOTONIC:
202 *n = CLOCK_MONOTONIC;
204 case LINUX_CLOCK_REALTIME_COARSE:
205 *n = CLOCK_REALTIME_FAST;
207 case LINUX_CLOCK_MONOTONIC_COARSE:
208 *n = CLOCK_MONOTONIC_FAST;
210 case LINUX_CLOCK_MONOTONIC_RAW:
211 case LINUX_CLOCK_BOOTTIME:
212 case LINUX_CLOCK_REALTIME_ALARM:
213 case LINUX_CLOCK_BOOTTIME_ALARM:
214 case LINUX_CLOCK_SGI_CYCLE:
215 case LINUX_CLOCK_TAI:
216 LIN_SDT_PROBE1(time, linux_to_native_clockid,
217 unsupported_clockid, l);
218 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
221 LIN_SDT_PROBE1(time, linux_to_native_clockid,
223 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
227 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
232 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
234 struct l_timespec lts;
237 struct thread *targettd;
239 int error, clockwhich;
240 clockid_t nwhich = 0; /* XXX: GCC */
244 LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
246 error = linux_to_native_clockid(&nwhich, args->which);
248 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
250 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
255 case CLOCK_PROCESS_CPUTIME_ID:
256 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
257 pid = LINUX_CPUCLOCK_ID(args->which);
262 error = pget(pid, PGET_CANSEE, &p);
266 switch (clockwhich) {
267 case LINUX_CPUCLOCK_PROF:
269 calcru(p, &ru.ru_utime, &ru.ru_stime);
272 timevaladd(&ru.ru_utime, &ru.ru_stime);
273 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
275 case LINUX_CPUCLOCK_VIRT:
277 calcru(p, &ru.ru_utime, &ru.ru_stime);
280 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
282 case LINUX_CPUCLOCK_SCHED:
284 error = kern_clock_getcpuclockid2(td, pid,
285 CPUCLOCK_WHICH_PID, &nwhich);
288 error = kern_clock_gettime(td, nwhich, &tp);
297 case CLOCK_THREAD_CPUTIME_ID:
298 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
300 tid = LINUX_CPUCLOCK_ID(args->which);
305 targettd = tdfind(tid, p->p_pid);
306 if (targettd == NULL)
309 switch (clockwhich) {
310 case LINUX_CPUCLOCK_PROF:
312 thread_lock(targettd);
313 rufetchtd(targettd, &ru);
314 thread_unlock(targettd);
317 timevaladd(&ru.ru_utime, &ru.ru_stime);
318 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
320 case LINUX_CPUCLOCK_VIRT:
322 thread_lock(targettd);
323 rufetchtd(targettd, &ru);
324 thread_unlock(targettd);
327 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
329 case LINUX_CPUCLOCK_SCHED:
330 error = kern_clock_getcpuclockid2(td, tid,
331 CPUCLOCK_WHICH_TID, &nwhich);
335 error = kern_clock_gettime(td, nwhich, &tp);
344 error = kern_clock_gettime(td, nwhich, &tp);
348 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
349 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
352 error = native_to_linux_timespec(<s, &tp);
355 error = copyout(<s, args->tp, sizeof lts);
357 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
359 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
364 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
367 struct l_timespec lts;
369 clockid_t nwhich = 0; /* XXX: GCC */
371 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
373 error = linux_to_native_clockid(&nwhich, args->which);
375 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
377 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
380 error = copyin(args->tp, <s, sizeof lts);
382 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
383 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
386 error = linux_to_native_timespec(&ts, <s);
388 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
390 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
394 error = kern_clock_settime(td, nwhich, &ts);
396 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
398 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
403 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
407 struct l_timespec lts;
408 int error, clockwhich;
409 clockid_t nwhich = 0; /* XXX: GCC */
413 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
415 error = linux_to_native_clockid(&nwhich, args->which);
417 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
419 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
424 * Check user supplied clock id in case of per-process
425 * or thread-specific cpu-time clock.
428 case CLOCK_THREAD_CPUTIME_ID:
429 tid = LINUX_CPUCLOCK_ID(args->which);
432 if (tdfind(tid, p->p_pid) == NULL)
437 case CLOCK_PROCESS_CPUTIME_ID:
438 pid = LINUX_CPUCLOCK_ID(args->which);
440 error = pget(pid, PGET_CANSEE, &p);
448 if (args->tp == NULL) {
449 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
450 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
455 case CLOCK_THREAD_CPUTIME_ID:
456 case CLOCK_PROCESS_CPUTIME_ID:
457 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
458 switch (clockwhich) {
459 case LINUX_CPUCLOCK_PROF:
462 case LINUX_CPUCLOCK_VIRT:
463 nwhich = CLOCK_VIRTUAL;
465 case LINUX_CPUCLOCK_SCHED:
475 error = kern_clock_getres(td, nwhich, &ts);
477 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
478 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
481 error = native_to_linux_timespec(<s, &ts);
484 error = copyout(<s, args->tp, sizeof lts);
486 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
488 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
493 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
495 struct timespec *rmtp;
496 struct l_timespec lrqts, lrmts;
497 struct timespec rqts, rmts;
500 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
502 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
504 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
505 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
509 if (args->rmtp != NULL)
514 error = linux_to_native_timespec(&rqts, &lrqts);
516 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
517 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
520 error = kern_nanosleep(td, &rqts, rmtp);
521 if (error == EINTR && args->rmtp != NULL) {
522 error2 = native_to_linux_timespec(&lrmts, rmtp);
525 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
527 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
529 LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
534 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
539 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
541 struct timespec *rmtp;
542 struct l_timespec lrqts, lrmts;
543 struct timespec rqts, rmts;
546 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
547 args->flags, args->rqtp, args->rmtp);
549 if (args->flags != 0) {
550 /* XXX deal with TIMER_ABSTIME */
551 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
553 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
554 return (EINVAL); /* XXX deal with TIMER_ABSTIME */
557 if (args->which != LINUX_CLOCK_REALTIME) {
558 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
560 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
564 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
566 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
568 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
572 if (args->rmtp != NULL)
577 error = linux_to_native_timespec(&rqts, &lrqts);
579 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
581 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
584 error = kern_nanosleep(td, &rqts, rmtp);
585 if (error == EINTR && args->rmtp != NULL) {
586 /* XXX. Not for TIMER_ABSTIME */
587 error2 = native_to_linux_timespec(&lrmts, rmtp);
590 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
592 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
593 copyout_error, error2);
594 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
600 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);