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/mount.h>
45 #include <sys/mutex.h>
46 #include <sys/resourcevar.h>
48 #include <sys/signal.h>
49 #include <sys/stdint.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/sysproto.h>
53 #include <sys/systm.h>
57 #include <machine/../linux32/linux.h>
58 #include <machine/../linux32/linux32_proto.h>
60 #include <machine/../linux/linux.h>
61 #include <machine/../linux/linux_proto.h>
64 #include <compat/linux/linux_dtrace.h>
65 #include <compat/linux/linux_timer.h>
68 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
71 * DTrace probes in this module.
73 LIN_SDT_PROBE_DEFINE2(time, native_to_linux_timespec, entry,
74 "struct l_timespec *", "struct timespec *");
75 LIN_SDT_PROBE_DEFINE0(time, native_to_linux_timespec, return);
76 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_timespec, entry,
77 "struct timespec *", "struct l_timespec *");
78 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_timespec, return, "int");
79 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_clockid, entry, "clockid_t *",
81 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
83 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
85 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, return, "int");
86 LIN_SDT_PROBE_DEFINE2(time, linux_clock_gettime, entry, "clockid_t",
87 "struct l_timespec *");
88 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, conversion_error, "int");
89 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
90 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
91 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, return, "int");
92 LIN_SDT_PROBE_DEFINE2(time, linux_clock_settime, entry, "clockid_t",
93 "struct l_timespec *");
94 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
95 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, settime_error, "int");
96 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
97 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, return, "int");
98 LIN_SDT_PROBE_DEFINE2(time, linux_clock_getres, entry, "clockid_t",
99 "struct l_timespec *");
100 LIN_SDT_PROBE_DEFINE0(time, linux_clock_getres, nullcall);
101 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, conversion_error, "int");
102 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, getres_error, "int");
103 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
104 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, return, "int");
105 LIN_SDT_PROBE_DEFINE2(time, linux_nanosleep, entry, "const struct l_timespec *",
106 "struct l_timespec *");
107 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, conversion_error, "int");
108 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, nanosleep_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, nanosleep_error, "int");
116 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
117 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
118 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
119 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "int");
120 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, return, "int");
122 static void native_to_linux_timespec(struct l_timespec *,
124 static int linux_to_native_timespec(struct timespec *,
125 struct l_timespec *);
126 static int linux_to_native_clockid(clockid_t *, clockid_t);
129 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
132 LIN_SDT_PROBE2(time, native_to_linux_timespec, entry, ltp, ntp);
134 ltp->tv_sec = ntp->tv_sec;
135 ltp->tv_nsec = ntp->tv_nsec;
137 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 > (l_long)999999999L) {
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 linux_to_native_clockid(clockid_t *n, clockid_t l)
161 LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
165 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
167 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
170 if (LINUX_CPUCLOCK_PERTHREAD(l))
171 *n = CLOCK_THREAD_CPUTIME_ID;
173 *n = CLOCK_PROCESS_CPUTIME_ID;
178 case LINUX_CLOCK_REALTIME:
181 case LINUX_CLOCK_MONOTONIC:
182 *n = CLOCK_MONOTONIC;
184 case LINUX_CLOCK_REALTIME_COARSE:
185 *n = CLOCK_REALTIME_FAST;
187 case LINUX_CLOCK_MONOTONIC_COARSE:
188 *n = CLOCK_MONOTONIC_FAST;
190 case LINUX_CLOCK_MONOTONIC_RAW:
191 case LINUX_CLOCK_BOOTTIME:
192 case LINUX_CLOCK_REALTIME_ALARM:
193 case LINUX_CLOCK_BOOTTIME_ALARM:
194 case LINUX_CLOCK_SGI_CYCLE:
195 case LINUX_CLOCK_TAI:
196 LIN_SDT_PROBE1(time, linux_to_native_clockid,
197 unsupported_clockid, l);
198 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
201 LIN_SDT_PROBE1(time, linux_to_native_clockid,
203 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
207 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
212 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
214 struct l_timespec lts;
217 struct thread *targettd;
219 int error, clockwhich;
220 clockid_t nwhich = 0; /* XXX: GCC */
224 LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
226 error = linux_to_native_clockid(&nwhich, args->which);
228 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
230 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
235 case CLOCK_PROCESS_CPUTIME_ID:
236 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
237 pid = LINUX_CPUCLOCK_ID(args->which);
242 error = pget(pid, PGET_CANSEE, &p);
246 switch (clockwhich) {
247 case LINUX_CPUCLOCK_PROF:
249 calcru(p, &ru.ru_utime, &ru.ru_stime);
252 timevaladd(&ru.ru_utime, &ru.ru_stime);
253 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
255 case LINUX_CPUCLOCK_VIRT:
257 calcru(p, &ru.ru_utime, &ru.ru_stime);
260 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
262 case LINUX_CPUCLOCK_SCHED:
264 error = kern_clock_getcpuclockid2(td, pid,
265 CPUCLOCK_WHICH_PID, &nwhich);
268 error = kern_clock_gettime(td, nwhich, &tp);
277 case CLOCK_THREAD_CPUTIME_ID:
278 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
280 tid = LINUX_CPUCLOCK_ID(args->which);
285 targettd = tdfind(tid, p->p_pid);
286 if (targettd == NULL)
289 switch (clockwhich) {
290 case LINUX_CPUCLOCK_PROF:
292 thread_lock(targettd);
293 rufetchtd(targettd, &ru);
294 thread_unlock(targettd);
297 timevaladd(&ru.ru_utime, &ru.ru_stime);
298 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
300 case LINUX_CPUCLOCK_VIRT:
302 thread_lock(targettd);
303 rufetchtd(targettd, &ru);
304 thread_unlock(targettd);
307 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
309 case LINUX_CPUCLOCK_SCHED:
310 error = kern_clock_getcpuclockid2(td, tid,
311 CPUCLOCK_WHICH_TID, &nwhich);
315 error = kern_clock_gettime(td, nwhich, &tp);
324 error = kern_clock_gettime(td, nwhich, &tp);
328 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
329 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
332 native_to_linux_timespec(<s, &tp);
334 error = copyout(<s, args->tp, sizeof lts);
336 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
338 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
343 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
346 struct l_timespec lts;
348 clockid_t nwhich = 0; /* XXX: GCC */
350 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
352 error = linux_to_native_clockid(&nwhich, args->which);
354 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
356 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
359 error = copyin(args->tp, <s, sizeof lts);
361 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
362 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
365 error = linux_to_native_timespec(&ts, <s);
367 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
369 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
373 error = kern_clock_settime(td, nwhich, &ts);
375 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
377 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
382 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
386 struct l_timespec lts;
387 int error, clockwhich;
388 clockid_t nwhich = 0; /* XXX: GCC */
392 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
394 error = linux_to_native_clockid(&nwhich, args->which);
396 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
398 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
403 * Check user supplied clock id in case of per-process
404 * or thread-specific cpu-time clock.
407 case CLOCK_THREAD_CPUTIME_ID:
408 tid = LINUX_CPUCLOCK_ID(args->which);
411 if (tdfind(tid, p->p_pid) == NULL)
416 case CLOCK_PROCESS_CPUTIME_ID:
417 pid = LINUX_CPUCLOCK_ID(args->which);
419 error = pget(pid, PGET_CANSEE, &p);
427 if (args->tp == NULL) {
428 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
429 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
434 case CLOCK_THREAD_CPUTIME_ID:
435 case CLOCK_PROCESS_CPUTIME_ID:
436 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
437 switch (clockwhich) {
438 case LINUX_CPUCLOCK_PROF:
441 case LINUX_CPUCLOCK_VIRT:
442 nwhich = CLOCK_VIRTUAL;
444 case LINUX_CPUCLOCK_SCHED:
454 error = kern_clock_getres(td, nwhich, &ts);
456 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
457 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
460 native_to_linux_timespec(<s, &ts);
462 error = copyout(<s, args->tp, sizeof lts);
464 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
466 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
471 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
473 struct timespec *rmtp;
474 struct l_timespec lrqts, lrmts;
475 struct timespec rqts, rmts;
478 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
480 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
482 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
483 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
487 if (args->rmtp != NULL)
492 error = linux_to_native_timespec(&rqts, &lrqts);
494 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
495 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
498 error = kern_nanosleep(td, &rqts, rmtp);
500 LIN_SDT_PROBE1(time, linux_nanosleep, nanosleep_error, error);
501 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
505 if (args->rmtp != NULL) {
506 native_to_linux_timespec(&lrmts, rmtp);
507 error = copyout(&lrmts, args->rmtp, sizeof(lrmts));
509 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
511 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
516 LIN_SDT_PROBE1(time, linux_nanosleep, return, 0);
521 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
523 struct timespec *rmtp;
524 struct l_timespec lrqts, lrmts;
525 struct timespec rqts, rmts;
528 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
529 args->flags, args->rqtp, args->rmtp);
531 if (args->flags != 0) {
532 /* XXX deal with TIMER_ABSTIME */
533 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
535 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
536 return (EINVAL); /* XXX deal with TIMER_ABSTIME */
539 if (args->which != LINUX_CLOCK_REALTIME) {
540 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
542 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
546 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
548 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
550 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
554 if (args->rmtp != NULL)
559 error = linux_to_native_timespec(&rqts, &lrqts);
561 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
563 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
566 error = kern_nanosleep(td, &rqts, rmtp);
568 LIN_SDT_PROBE1(time, linux_clock_nanosleep, nanosleep_error,
570 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
574 if (args->rmtp != NULL) {
575 native_to_linux_timespec(&lrmts, rmtp);
576 error = copyout(&lrmts, args->rmtp, sizeof lrmts );
578 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
579 copyout_error, error);
580 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
585 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, 0);