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_BOOTTIME:
213 case LINUX_CLOCK_MONOTONIC_RAW:
214 case LINUX_CLOCK_REALTIME_ALARM:
215 case LINUX_CLOCK_BOOTTIME_ALARM:
216 case LINUX_CLOCK_SGI_CYCLE:
217 case LINUX_CLOCK_TAI:
218 LIN_SDT_PROBE1(time, linux_to_native_clockid,
219 unsupported_clockid, l);
220 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
223 LIN_SDT_PROBE1(time, linux_to_native_clockid,
225 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
229 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
234 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
236 struct l_timespec lts;
239 struct thread *targettd;
241 int error, clockwhich;
242 clockid_t nwhich = 0; /* XXX: GCC */
246 LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
248 error = linux_to_native_clockid(&nwhich, args->which);
250 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
252 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
257 case CLOCK_PROCESS_CPUTIME_ID:
258 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
259 pid = LINUX_CPUCLOCK_ID(args->which);
264 error = pget(pid, PGET_CANSEE, &p);
268 switch (clockwhich) {
269 case LINUX_CPUCLOCK_PROF:
271 calcru(p, &ru.ru_utime, &ru.ru_stime);
274 timevaladd(&ru.ru_utime, &ru.ru_stime);
275 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
277 case LINUX_CPUCLOCK_VIRT:
279 calcru(p, &ru.ru_utime, &ru.ru_stime);
282 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
284 case LINUX_CPUCLOCK_SCHED:
286 error = kern_clock_getcpuclockid2(td, pid,
287 CPUCLOCK_WHICH_PID, &nwhich);
290 error = kern_clock_gettime(td, nwhich, &tp);
299 case CLOCK_THREAD_CPUTIME_ID:
300 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
302 tid = LINUX_CPUCLOCK_ID(args->which);
307 targettd = tdfind(tid, p->p_pid);
308 if (targettd == NULL)
311 switch (clockwhich) {
312 case LINUX_CPUCLOCK_PROF:
314 thread_lock(targettd);
315 rufetchtd(targettd, &ru);
316 thread_unlock(targettd);
319 timevaladd(&ru.ru_utime, &ru.ru_stime);
320 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
322 case LINUX_CPUCLOCK_VIRT:
324 thread_lock(targettd);
325 rufetchtd(targettd, &ru);
326 thread_unlock(targettd);
329 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
331 case LINUX_CPUCLOCK_SCHED:
332 error = kern_clock_getcpuclockid2(td, tid,
333 CPUCLOCK_WHICH_TID, &nwhich);
337 error = kern_clock_gettime(td, nwhich, &tp);
346 error = kern_clock_gettime(td, nwhich, &tp);
350 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
351 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
354 error = native_to_linux_timespec(<s, &tp);
357 error = copyout(<s, args->tp, sizeof lts);
359 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
361 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
366 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
369 struct l_timespec lts;
371 clockid_t nwhich = 0; /* XXX: GCC */
373 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
375 error = linux_to_native_clockid(&nwhich, args->which);
377 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
379 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
382 error = copyin(args->tp, <s, sizeof lts);
384 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
385 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
388 error = linux_to_native_timespec(&ts, <s);
390 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
392 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
396 error = kern_clock_settime(td, nwhich, &ts);
398 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
400 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
405 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
409 struct l_timespec lts;
410 int error, clockwhich;
411 clockid_t nwhich = 0; /* XXX: GCC */
415 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
417 error = linux_to_native_clockid(&nwhich, args->which);
419 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
421 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
426 * Check user supplied clock id in case of per-process
427 * or thread-specific cpu-time clock.
430 case CLOCK_THREAD_CPUTIME_ID:
431 tid = LINUX_CPUCLOCK_ID(args->which);
434 if (tdfind(tid, p->p_pid) == NULL)
439 case CLOCK_PROCESS_CPUTIME_ID:
440 pid = LINUX_CPUCLOCK_ID(args->which);
442 error = pget(pid, PGET_CANSEE, &p);
450 if (args->tp == NULL) {
451 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
452 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
457 case CLOCK_THREAD_CPUTIME_ID:
458 case CLOCK_PROCESS_CPUTIME_ID:
459 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
460 switch (clockwhich) {
461 case LINUX_CPUCLOCK_PROF:
464 case LINUX_CPUCLOCK_VIRT:
465 nwhich = CLOCK_VIRTUAL;
467 case LINUX_CPUCLOCK_SCHED:
477 error = kern_clock_getres(td, nwhich, &ts);
479 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
480 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
483 error = native_to_linux_timespec(<s, &ts);
486 error = copyout(<s, args->tp, sizeof lts);
488 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
490 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
495 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
497 struct timespec *rmtp;
498 struct l_timespec lrqts, lrmts;
499 struct timespec rqts, rmts;
502 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
504 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
506 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
507 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
511 if (args->rmtp != NULL)
516 error = linux_to_native_timespec(&rqts, &lrqts);
518 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
519 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
522 error = kern_nanosleep(td, &rqts, rmtp);
523 if (error == EINTR && args->rmtp != NULL) {
524 error2 = native_to_linux_timespec(&lrmts, rmtp);
527 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
529 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
531 LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
536 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
541 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
543 struct timespec *rmtp;
544 struct l_timespec lrqts, lrmts;
545 struct timespec rqts, rmts;
548 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
549 args->flags, args->rqtp, args->rmtp);
551 if (args->flags != 0) {
552 /* XXX deal with TIMER_ABSTIME */
553 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
555 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
556 return (EINVAL); /* XXX deal with TIMER_ABSTIME */
559 if (args->which != LINUX_CLOCK_REALTIME) {
560 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
562 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
566 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
568 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
570 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
574 if (args->rmtp != NULL)
579 error = linux_to_native_timespec(&rqts, &lrqts);
581 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
583 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
586 error = kern_nanosleep(td, &rqts, rmtp);
587 if (error == EINTR && args->rmtp != NULL) {
588 /* XXX. Not for TIMER_ABSTIME */
589 error2 = native_to_linux_timespec(&lrmts, rmtp);
592 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
594 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
595 copyout_error, error2);
596 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
602 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);