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"
39 #include "opt_kdtrace.h"
41 #include <sys/param.h>
42 #include <sys/kernel.h>
44 #include <sys/ucred.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);
128 ltp->tv_sec = ntp->tv_sec;
129 ltp->tv_nsec = ntp->tv_nsec;
131 LIN_SDT_PROBE0(time, native_to_linux_timespec, return);
135 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
138 LIN_SDT_PROBE2(time, linux_to_native_timespec, entry, ntp, ltp);
140 if (ltp->tv_sec < 0 || ltp->tv_nsec > (l_long)999999999L) {
141 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, EINVAL);
144 ntp->tv_sec = ltp->tv_sec;
145 ntp->tv_nsec = ltp->tv_nsec;
147 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, 0);
152 linux_to_native_clockid(clockid_t *n, clockid_t l)
155 LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
159 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
161 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
164 if (LINUX_CPUCLOCK_PERTHREAD(l))
165 *n = CLOCK_THREAD_CPUTIME_ID;
167 *n = CLOCK_PROCESS_CPUTIME_ID;
172 case LINUX_CLOCK_REALTIME:
175 case LINUX_CLOCK_MONOTONIC:
176 *n = CLOCK_MONOTONIC;
178 case LINUX_CLOCK_REALTIME_COARSE:
179 *n = CLOCK_REALTIME_FAST;
181 case LINUX_CLOCK_MONOTONIC_COARSE:
182 *n = CLOCK_MONOTONIC_FAST;
184 case LINUX_CLOCK_MONOTONIC_RAW:
185 case LINUX_CLOCK_BOOTTIME:
186 case LINUX_CLOCK_REALTIME_ALARM:
187 case LINUX_CLOCK_BOOTTIME_ALARM:
188 case LINUX_CLOCK_SGI_CYCLE:
189 case LINUX_CLOCK_TAI:
190 LIN_SDT_PROBE1(time, linux_to_native_clockid,
191 unsupported_clockid, l);
192 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
195 LIN_SDT_PROBE1(time, linux_to_native_clockid,
197 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
201 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
206 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
208 struct l_timespec lts;
211 struct thread *targettd;
213 int error, clockwhich;
214 clockid_t nwhich = 0; /* XXX: GCC */
218 LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
220 error = linux_to_native_clockid(&nwhich, args->which);
222 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
224 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
229 case CLOCK_PROCESS_CPUTIME_ID:
230 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
231 pid = LINUX_CPUCLOCK_ID(args->which);
236 error = pget(pid, PGET_CANSEE, &p);
240 switch (clockwhich) {
241 case LINUX_CPUCLOCK_PROF:
243 calcru(p, &ru.ru_utime, &ru.ru_stime);
246 timevaladd(&ru.ru_utime, &ru.ru_stime);
247 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
249 case LINUX_CPUCLOCK_VIRT:
251 calcru(p, &ru.ru_utime, &ru.ru_stime);
254 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
256 case LINUX_CPUCLOCK_SCHED:
258 error = kern_clock_getcpuclockid2(td, pid,
259 CPUCLOCK_WHICH_PID, &nwhich);
262 error = kern_clock_gettime(td, nwhich, &tp);
271 case CLOCK_THREAD_CPUTIME_ID:
272 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
274 tid = LINUX_CPUCLOCK_ID(args->which);
279 targettd = tdfind(tid, p->p_pid);
280 if (targettd == NULL)
283 switch (clockwhich) {
284 case LINUX_CPUCLOCK_PROF:
286 thread_lock(targettd);
287 rufetchtd(targettd, &ru);
288 thread_unlock(targettd);
291 timevaladd(&ru.ru_utime, &ru.ru_stime);
292 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
294 case LINUX_CPUCLOCK_VIRT:
296 thread_lock(targettd);
297 rufetchtd(targettd, &ru);
298 thread_unlock(targettd);
301 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
303 case LINUX_CPUCLOCK_SCHED:
304 error = kern_clock_getcpuclockid2(td, tid,
305 CPUCLOCK_WHICH_TID, &nwhich);
309 error = kern_clock_gettime(td, nwhich, &tp);
318 error = kern_clock_gettime(td, nwhich, &tp);
322 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
323 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
326 native_to_linux_timespec(<s, &tp);
328 error = copyout(<s, args->tp, sizeof lts);
330 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
332 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
337 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
340 struct l_timespec lts;
342 clockid_t nwhich = 0; /* XXX: GCC */
344 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
346 error = linux_to_native_clockid(&nwhich, args->which);
348 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
350 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
353 error = copyin(args->tp, <s, sizeof lts);
355 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
356 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
359 error = linux_to_native_timespec(&ts, <s);
361 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
363 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
367 error = kern_clock_settime(td, nwhich, &ts);
369 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
371 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
376 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
380 struct l_timespec lts;
381 int error, clockwhich;
382 clockid_t nwhich = 0; /* XXX: GCC */
386 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
388 error = linux_to_native_clockid(&nwhich, args->which);
390 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
392 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
397 * Check user supplied clock id in case of per-process
398 * or thread-specific cpu-time clock.
401 case CLOCK_THREAD_CPUTIME_ID:
402 tid = LINUX_CPUCLOCK_ID(args->which);
405 if (tdfind(tid, p->p_pid) == NULL)
410 case CLOCK_PROCESS_CPUTIME_ID:
411 pid = LINUX_CPUCLOCK_ID(args->which);
413 error = pget(pid, PGET_CANSEE, &p);
421 if (args->tp == NULL) {
422 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
423 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
428 case CLOCK_THREAD_CPUTIME_ID:
429 case CLOCK_PROCESS_CPUTIME_ID:
430 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
431 switch (clockwhich) {
432 case LINUX_CPUCLOCK_PROF:
435 case LINUX_CPUCLOCK_VIRT:
436 nwhich = CLOCK_VIRTUAL;
438 case LINUX_CPUCLOCK_SCHED:
448 error = kern_clock_getres(td, nwhich, &ts);
450 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
451 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
454 native_to_linux_timespec(<s, &ts);
456 error = copyout(<s, args->tp, sizeof lts);
458 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
460 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
465 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
467 struct timespec *rmtp;
468 struct l_timespec lrqts, lrmts;
469 struct timespec rqts, rmts;
472 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
474 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
476 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
477 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
481 if (args->rmtp != NULL)
486 error = linux_to_native_timespec(&rqts, &lrqts);
488 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
489 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
492 error = kern_nanosleep(td, &rqts, rmtp);
493 if (args->rmtp != NULL) {
494 native_to_linux_timespec(&lrmts, rmtp);
495 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
497 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
499 LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
504 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
509 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
511 struct timespec *rmtp;
512 struct l_timespec lrqts, lrmts;
513 struct timespec rqts, rmts;
516 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
517 args->flags, args->rqtp, args->rmtp);
519 if (args->flags != 0) {
520 /* XXX deal with TIMER_ABSTIME */
521 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
523 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
524 return (EINVAL); /* XXX deal with TIMER_ABSTIME */
527 if (args->which != LINUX_CLOCK_REALTIME) {
528 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
530 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
534 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
536 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
538 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
542 if (args->rmtp != NULL)
547 error = linux_to_native_timespec(&rqts, &lrqts);
549 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
551 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
554 error = kern_nanosleep(td, &rqts, rmtp);
555 if (args->rmtp != NULL) {
556 /* XXX. Not for TIMER_ABSTIME */
557 native_to_linux_timespec(&lrmts, rmtp);
558 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
560 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
561 copyout_error, error2);
562 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
568 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);