1 /* $NetBSD: linux_time.c,v 1.28 2009/01/11 02:45:48 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 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.28 2009/01/11 02:45:48 christos Exp $");
35 #include <sys/param.h>
36 #include <sys/ucred.h>
37 #include <sys/kauth.h>
38 #include <sys/mount.h>
39 #include <sys/signal.h>
40 #include <sys/stdint.h>
42 #include <sys/timetc.h>
43 #include <sys/systm.h>
44 #include <sys/sched.h>
45 #include <sys/syscallargs.h>
49 #include <compat/linux/common/linux_types.h>
50 #include <compat/linux/common/linux_signal.h>
51 #include <compat/linux/common/linux_machdep.h>
52 #include <compat/linux/common/linux_sched.h>
53 #include <compat/linux/common/linux_ipc.h>
54 #include <compat/linux/common/linux_sem.h>
56 #include <compat/linux/linux_syscallargs.h>
58 #include <compat/common/compat_util.h>
60 void native_to_linux_timespec(struct linux_timespec
*, struct timespec
*);
61 void linux_to_native_timespec(struct timespec
*, struct linux_timespec
*);
63 * This is not implemented for alpha yet
65 #if defined (__i386__) || defined (__m68k__) || \
66 defined (__powerpc__) || defined (__mips__) || \
67 defined(__arm__) || defined(__amd64__)
70 * Linux keeps track of a system timezone in the kernel. It is readen
71 * by gettimeofday and set by settimeofday. This emulates this behavior
72 * See linux/kernel/time.c
74 struct timezone linux_sys_tz
;
77 linux_sys_gettimeofday(struct lwp
*l
, const struct linux_sys_gettimeofday_args
*uap
, register_t
*retval
)
80 syscallarg(struct timeval50 *) tz;
81 syscallarg(struct timezone *) tzp;
86 error
= compat_50_sys_gettimeofday(l
, (const void *)uap
, retval
);
91 if (SCARG(uap
, tzp
)) {
92 error
= copyout(&linux_sys_tz
, SCARG(uap
, tzp
), sizeof(linux_sys_tz
));
101 linux_sys_settimeofday(struct lwp
*l
, const struct linux_sys_settimeofday_args
*uap
, register_t
*retval
)
104 syscallarg(struct timeval50 *) tp;
105 syscallarg(struct timezone *) tzp;
109 if (SCARG(uap
, tp
)) {
110 error
= compat_50_sys_settimeofday(l
, (const void *)uap
, retval
);
116 * If user is not the superuser, we returned
117 * after the sys_settimeofday() call.
119 if (SCARG(uap
, tzp
)) {
120 error
= copyin(SCARG(uap
, tzp
), &linux_sys_tz
, sizeof(linux_sys_tz
));
128 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */
131 native_to_linux_timespec(struct linux_timespec
*ltp
, struct timespec
*ntp
)
133 ltp
->tv_sec
= ntp
->tv_sec
;
134 ltp
->tv_nsec
= ntp
->tv_nsec
;
138 linux_to_native_timespec(struct timespec
*ntp
, struct linux_timespec
*ltp
)
140 ntp
->tv_sec
= ltp
->tv_sec
;
141 ntp
->tv_nsec
= ltp
->tv_nsec
;
145 linux_sys_nanosleep(struct lwp
*l
, const struct linux_sys_nanosleep_args
*uap
,
149 syscallarg(struct linux_timespec *) rqtp;
150 syscallarg(struct linux_timespec *) rmtp;
152 struct timespec rqts
, rmts
;
153 struct linux_timespec lrqts
, lrmts
;
156 error
= copyin(SCARG(uap
, rqtp
), &lrqts
, sizeof(lrqts
));
159 linux_to_native_timespec(&rqts
, &lrqts
);
161 error
= nanosleep1(l
, &rqts
, SCARG(uap
, rmtp
) ? &rmts
: NULL
);
162 if (SCARG(uap
, rmtp
) == NULL
|| (error
!= 0 && error
!= EINTR
))
165 native_to_linux_timespec(&lrmts
, &rmts
);
166 error1
= copyout(&lrmts
, SCARG(uap
, rmtp
), sizeof(lrmts
));
167 return error1
? error1
: error
;
171 linux_to_native_clockid(clockid_t
*n
, clockid_t l
)
174 case LINUX_CLOCK_REALTIME
:
177 case LINUX_CLOCK_MONOTONIC
:
178 *n
= CLOCK_MONOTONIC
;
180 case LINUX_CLOCK_PROCESS_CPUTIME_ID
:
181 case LINUX_CLOCK_THREAD_CPUTIME_ID
:
182 case LINUX_CLOCK_REALTIME_HR
:
183 case LINUX_CLOCK_MONOTONIC_HR
:
192 linux_sys_clock_gettime(struct lwp
*l
, const struct linux_sys_clock_gettime_args
*uap
, register_t
*retval
)
195 syscallarg(clockid_t) which;
196 syscallarg(struct linux_timespec *)tp;
199 struct linux_timespec lts
;
201 switch (SCARG(uap
, which
)) {
202 case LINUX_CLOCK_REALTIME
:
205 case LINUX_CLOCK_MONOTONIC
:
212 native_to_linux_timespec(<s
, &ts
);
213 return copyout(<s
, SCARG(uap
, tp
), sizeof lts
);
217 linux_sys_clock_settime(struct lwp
*l
, const struct linux_sys_clock_settime_args
*uap
, register_t
*retval
)
220 syscallarg(clockid_t) which;
221 syscallarg(struct linux_timespec *)tp;
224 struct linux_timespec lts
;
227 switch (SCARG(uap
, which
)) {
228 case LINUX_CLOCK_REALTIME
:
234 error
= copyin(SCARG(uap
, tp
), <s
, sizeof lts
);
238 linux_to_native_timespec(&ts
, <s
);
240 return settime(l
->l_proc
, &ts
);
244 linux_sys_clock_getres(struct lwp
*l
, const struct linux_sys_clock_getres_args
*uap
, register_t
*retval
)
247 syscallarg(clockid_t) which;
248 syscallarg(struct linux_timespec *)tp;
251 struct linux_timespec lts
;
253 clockid_t nwhich
= 0; /* XXX: GCC */
255 error
= linux_to_native_clockid(&nwhich
, SCARG(uap
, which
));
256 if (error
!= 0 || SCARG(uap
, tp
) == NULL
)
260 ts
.tv_nsec
= 1000000000 / tc_getfrequency();
261 native_to_linux_timespec(<s
, &ts
);
262 return copyout(<s
, SCARG(uap
, tp
), sizeof lts
);
266 linux_sys_clock_nanosleep(struct lwp
*l
, const struct linux_sys_clock_nanosleep_args
*uap
, register_t
*retval
)
269 syscallarg(clockid_t) which;
270 syscallarg(int) flags;
271 syscallarg(struct linux_timespec) *rqtp;
272 syscallarg(struct linux_timespec) *rmtp;
274 struct linux_timespec lrqts
, lrmts
;
275 struct timespec rqts
, rmts
;
278 if (SCARG(uap
, flags
) != 0)
279 return EINVAL
; /* XXX deal with TIMER_ABSTIME */
281 if (SCARG(uap
, which
) != LINUX_CLOCK_REALTIME
)
284 error
= copyin(SCARG(uap
, rqtp
), &lrqts
, sizeof lrqts
);
288 linux_to_native_timespec(&rqts
, &lrqts
);
290 error
= nanosleep1(l
, &rqts
, SCARG(uap
, rmtp
) ? &rmts
: 0);
291 if (SCARG(uap
, rmtp
) == NULL
|| (error
!= 0 && error
!= EINTR
))
294 native_to_linux_timespec(&lrmts
, &rmts
);
295 error1
= copyout(&lrmts
, SCARG(uap
, rmtp
), sizeof lrmts
);
296 return error1
? error1
: error
;