1 /* $NetBSD: linux_time.c,v 1.22 2007/12/20 23:02:57 dsl 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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.22 2007/12/20 23:02:57 dsl Exp $");
42 #include <sys/param.h>
43 #include <sys/ucred.h>
44 #include <sys/kauth.h>
45 #include <sys/mount.h>
46 #include <sys/signal.h>
47 #include <sys/stdint.h>
49 #include <sys/timetc.h>
50 #include <sys/systm.h>
51 #include <sys/syscallargs.h>
55 #include <compat/linux/common/linux_types.h>
56 #include <compat/linux/common/linux_signal.h>
57 #include <compat/linux/common/linux_machdep.h>
58 #include <compat/linux/common/linux_sched.h>
59 #include <compat/linux/common/linux_ipc.h>
60 #include <compat/linux/common/linux_sem.h>
62 #include <compat/linux/linux_syscallargs.h>
64 #include <compat/common/compat_util.h>
66 static void native_to_linux_timespec(struct linux_timespec
*,
68 static void linux_to_native_timespec(struct timespec
*,
69 struct linux_timespec
*);
71 * This is not implemented for alpha yet
73 #if defined (__i386__) || defined (__m68k__) || \
74 defined (__powerpc__) || defined (__mips__) || \
75 defined(__arm__) || defined(__amd64__)
78 * Linux keeps track of a system timezone in the kernel. It is readen
79 * by gettimeofday and set by settimeofday. This emulates this behavior
80 * See linux/kernel/time.c
82 struct timezone linux_sys_tz
;
85 linux_sys_gettimeofday(struct lwp
*l
, const struct linux_sys_gettimeofday_args
*uap
, register_t
*retval
)
88 syscallarg(struct timeval *) tz;
89 syscallarg(struct timezone *) tzp;
94 error
= sys_gettimeofday(l
, (const void *)uap
, retval
);
99 if (SCARG(uap
, tzp
)) {
100 error
= copyout(&linux_sys_tz
, SCARG(uap
, tzp
), sizeof(linux_sys_tz
));
109 linux_sys_settimeofday(struct lwp
*l
, const struct linux_sys_settimeofday_args
*uap
, register_t
*retval
)
112 syscallarg(struct timeval *) tp;
113 syscallarg(struct timezone *) tzp;
117 if (SCARG(uap
, tp
)) {
118 error
= sys_settimeofday(l
, (const void *)uap
, retval
);
124 * If user is not the superuser, we returned
125 * after the sys_settimeofday() call.
127 if (SCARG(uap
, tzp
)) {
128 error
= copyin(SCARG(uap
, tzp
), &linux_sys_tz
, sizeof(linux_sys_tz
));
136 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */
139 native_to_linux_timespec(struct linux_timespec
*ltp
, struct timespec
*ntp
)
141 ltp
->tv_sec
= ntp
->tv_sec
;
142 ltp
->tv_nsec
= ntp
->tv_nsec
;
146 linux_to_native_timespec(struct timespec
*ntp
, struct linux_timespec
*ltp
)
148 ntp
->tv_sec
= ltp
->tv_sec
;
149 ntp
->tv_nsec
= ltp
->tv_nsec
;
153 linux_to_native_clockid(clockid_t
*n
, clockid_t l
)
156 case LINUX_CLOCK_REALTIME
:
159 case LINUX_CLOCK_MONOTONIC
:
160 *n
= CLOCK_MONOTONIC
;
162 case LINUX_CLOCK_PROCESS_CPUTIME_ID
:
163 case LINUX_CLOCK_THREAD_CPUTIME_ID
:
164 case LINUX_CLOCK_REALTIME_HR
:
165 case LINUX_CLOCK_MONOTONIC_HR
:
173 linux_sys_clock_gettime(struct lwp
*l
, const struct linux_sys_clock_gettime_args
*uap
, register_t
*retval
)
176 syscallarg(clockid_t) which;
177 syscallarg(struct linux_timespec *)tp;
180 struct linux_timespec lts
;
182 switch (SCARG(uap
, which
)) {
183 case LINUX_CLOCK_REALTIME
:
186 case LINUX_CLOCK_MONOTONIC
:
193 native_to_linux_timespec(<s
, &ts
);
194 return copyout(<s
, SCARG(uap
, tp
), sizeof lts
);
198 linux_sys_clock_settime(struct lwp
*l
, const struct linux_sys_clock_settime_args
*uap
, register_t
*retval
)
201 syscallarg(clockid_t) which;
202 syscallarg(struct linux_timespec *)tp;
205 struct linux_timespec lts
;
208 switch (SCARG(uap
, which
)) {
209 case LINUX_CLOCK_REALTIME
:
215 error
= copyin(SCARG(uap
, tp
), <s
, sizeof lts
);
219 linux_to_native_timespec(&ts
, <s
);
221 return settime(l
->l_proc
, &ts
);
225 linux_sys_clock_getres(struct lwp
*l
, const struct linux_sys_clock_getres_args
*uap
, register_t
*retval
)
228 syscallarg(clockid_t) which;
229 syscallarg(struct linux_timespec *)tp;
232 struct linux_timespec lts
;
234 clockid_t nwhich
= 0; /* XXX: GCC */
236 error
= linux_to_native_clockid(&nwhich
, SCARG(uap
, which
));
237 if (error
!= 0 || SCARG(uap
, tp
) == NULL
)
241 ts
.tv_nsec
= 1000000000 / tc_getfrequency();
242 native_to_linux_timespec(<s
, &ts
);
243 return copyout(<s
, SCARG(uap
, tp
), sizeof lts
);
247 linux_sys_clock_nanosleep(struct lwp
*l
, const struct linux_sys_clock_nanosleep_args
*uap
, register_t
*retval
)
250 syscallarg(clockid_t) which;
251 syscallarg(int) flags;
252 syscallarg(struct linux_timespec) *rqtp;
253 syscallarg(struct linux_timespec) *rmtp;
255 struct linux_timespec lrqts
, lrmts
;
256 struct timespec rqts
, rmts
;
259 if (SCARG(uap
, flags
) != 0)
260 return EINVAL
; /* XXX deal with TIMER_ABSTIME */
262 if (SCARG(uap
, which
) != LINUX_CLOCK_REALTIME
)
265 error
= copyin(SCARG(uap
, rqtp
), &lrqts
, sizeof lrqts
);
269 linux_to_native_timespec(&rqts
, &lrqts
);
271 error
= nanosleep1(l
, &rqts
, SCARG(uap
, rmtp
) ? &rmts
: 0);
272 if (SCARG(uap
, rmtp
) == NULL
|| (error
!= 0 && error
!= EINTR
))
275 native_to_linux_timespec(&lrmts
, &rmts
);
276 error1
= copyout(&lrmts
, SCARG(uap
, rmtp
), sizeof lrmts
);
277 return error1
? error1
: error
;