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, copyout_error, "int");
109 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
110 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, return, "int");
111 LIN_SDT_PROBE_DEFINE4(time, linux_clock_nanosleep, entry, "clockid_t", "int",
112 "struct l_timespec *", "struct l_timespec *");
113 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, conversion_error, "int");
114 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
115 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
116 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
117 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "int");
118 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, return, "int");
122 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
125 LIN_SDT_PROBE2(time, native_to_linux_timespec, entry, ltp, ntp);
127 ltp->tv_sec = ntp->tv_sec;
128 ltp->tv_nsec = ntp->tv_nsec;
130 LIN_SDT_PROBE0(time, native_to_linux_timespec, return);
134 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
137 LIN_SDT_PROBE2(time, linux_to_native_timespec, entry, ntp, ltp);
139 if (ltp->tv_sec < 0 || ltp->tv_nsec > (l_long)999999999L) {
140 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, EINVAL);
143 ntp->tv_sec = ltp->tv_sec;
144 ntp->tv_nsec = ltp->tv_nsec;
146 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, 0);
151 linux_to_native_clockid(clockid_t *n, clockid_t l)
154 LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
158 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
160 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
163 if (LINUX_CPUCLOCK_PERTHREAD(l))
164 *n = CLOCK_THREAD_CPUTIME_ID;
166 *n = CLOCK_PROCESS_CPUTIME_ID;
171 case LINUX_CLOCK_REALTIME:
174 case LINUX_CLOCK_MONOTONIC:
175 *n = CLOCK_MONOTONIC;
177 case LINUX_CLOCK_REALTIME_COARSE:
178 *n = CLOCK_REALTIME_FAST;
180 case LINUX_CLOCK_MONOTONIC_COARSE:
181 *n = CLOCK_MONOTONIC_FAST;
183 case LINUX_CLOCK_MONOTONIC_RAW:
184 case LINUX_CLOCK_BOOTTIME:
185 case LINUX_CLOCK_REALTIME_ALARM:
186 case LINUX_CLOCK_BOOTTIME_ALARM:
187 case LINUX_CLOCK_SGI_CYCLE:
188 case LINUX_CLOCK_TAI:
189 LIN_SDT_PROBE1(time, linux_to_native_clockid,
190 unsupported_clockid, l);
191 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
194 LIN_SDT_PROBE1(time, linux_to_native_clockid,
196 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
200 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
205 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
207 struct l_timespec lts;
210 struct thread *targettd;
212 int error, clockwhich;
213 clockid_t nwhich = 0; /* XXX: GCC */
217 LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
219 error = linux_to_native_clockid(&nwhich, args->which);
221 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
223 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
228 case CLOCK_PROCESS_CPUTIME_ID:
229 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
230 pid = LINUX_CPUCLOCK_ID(args->which);
235 error = pget(pid, PGET_CANSEE, &p);
239 switch (clockwhich) {
240 case LINUX_CPUCLOCK_PROF:
242 calcru(p, &ru.ru_utime, &ru.ru_stime);
245 timevaladd(&ru.ru_utime, &ru.ru_stime);
246 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
248 case LINUX_CPUCLOCK_VIRT:
250 calcru(p, &ru.ru_utime, &ru.ru_stime);
253 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
255 case LINUX_CPUCLOCK_SCHED:
257 error = kern_clock_getcpuclockid2(td, pid,
258 CPUCLOCK_WHICH_PID, &nwhich);
261 error = kern_clock_gettime(td, nwhich, &tp);
270 case CLOCK_THREAD_CPUTIME_ID:
271 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
273 tid = LINUX_CPUCLOCK_ID(args->which);
278 targettd = tdfind(tid, p->p_pid);
279 if (targettd == NULL)
282 switch (clockwhich) {
283 case LINUX_CPUCLOCK_PROF:
285 thread_lock(targettd);
286 rufetchtd(targettd, &ru);
287 thread_unlock(targettd);
290 timevaladd(&ru.ru_utime, &ru.ru_stime);
291 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
293 case LINUX_CPUCLOCK_VIRT:
295 thread_lock(targettd);
296 rufetchtd(targettd, &ru);
297 thread_unlock(targettd);
300 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
302 case LINUX_CPUCLOCK_SCHED:
303 error = kern_clock_getcpuclockid2(td, tid,
304 CPUCLOCK_WHICH_TID, &nwhich);
308 error = kern_clock_gettime(td, nwhich, &tp);
317 error = kern_clock_gettime(td, nwhich, &tp);
321 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
322 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
325 native_to_linux_timespec(<s, &tp);
327 error = copyout(<s, args->tp, sizeof lts);
329 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
331 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
336 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
339 struct l_timespec lts;
341 clockid_t nwhich = 0; /* XXX: GCC */
343 LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
345 error = linux_to_native_clockid(&nwhich, args->which);
347 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
349 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
352 error = copyin(args->tp, <s, sizeof lts);
354 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
355 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
358 error = linux_to_native_timespec(&ts, <s);
360 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
362 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
366 error = kern_clock_settime(td, nwhich, &ts);
368 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
370 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
375 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
379 struct l_timespec lts;
380 int error, clockwhich;
381 clockid_t nwhich = 0; /* XXX: GCC */
385 LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
387 error = linux_to_native_clockid(&nwhich, args->which);
389 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
391 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
396 * Check user supplied clock id in case of per-process
397 * or thread-specific cpu-time clock.
400 case CLOCK_THREAD_CPUTIME_ID:
401 tid = LINUX_CPUCLOCK_ID(args->which);
404 if (tdfind(tid, p->p_pid) == NULL)
409 case CLOCK_PROCESS_CPUTIME_ID:
410 pid = LINUX_CPUCLOCK_ID(args->which);
412 error = pget(pid, PGET_CANSEE, &p);
420 if (args->tp == NULL) {
421 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
422 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
427 case CLOCK_THREAD_CPUTIME_ID:
428 case CLOCK_PROCESS_CPUTIME_ID:
429 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
430 switch (clockwhich) {
431 case LINUX_CPUCLOCK_PROF:
434 case LINUX_CPUCLOCK_VIRT:
435 nwhich = CLOCK_VIRTUAL;
437 case LINUX_CPUCLOCK_SCHED:
447 error = kern_clock_getres(td, nwhich, &ts);
449 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
450 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
453 native_to_linux_timespec(<s, &ts);
455 error = copyout(<s, args->tp, sizeof lts);
457 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
459 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
464 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
466 struct timespec *rmtp;
467 struct l_timespec lrqts, lrmts;
468 struct timespec rqts, rmts;
471 LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
473 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
475 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
476 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
480 if (args->rmtp != NULL)
485 error = linux_to_native_timespec(&rqts, &lrqts);
487 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
488 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
491 error = kern_nanosleep(td, &rqts, rmtp);
492 if (args->rmtp != NULL) {
493 native_to_linux_timespec(&lrmts, rmtp);
494 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
496 LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
498 LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
503 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
508 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
510 struct timespec *rmtp;
511 struct l_timespec lrqts, lrmts;
512 struct timespec rqts, rmts;
515 LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
516 args->flags, args->rqtp, args->rmtp);
518 if (args->flags != 0) {
519 /* XXX deal with TIMER_ABSTIME */
520 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
522 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
523 return (EINVAL); /* XXX deal with TIMER_ABSTIME */
526 if (args->which != LINUX_CLOCK_REALTIME) {
527 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
529 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
533 error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
535 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
537 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
541 if (args->rmtp != NULL)
546 error = linux_to_native_timespec(&rqts, &lrqts);
548 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
550 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
553 error = kern_nanosleep(td, &rqts, rmtp);
554 if (args->rmtp != NULL) {
555 /* XXX. Not for TIMER_ABSTIME */
556 native_to_linux_timespec(&lrmts, rmtp);
557 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
559 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
560 copyout_error, error2);
561 LIN_SDT_PROBE1(time, linux_clock_nanosleep,
567 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);