Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / linux / common / linux_time.c
blobbd346e402c4fd0d2000b517eb1dd1c8715366abf
1 /* $NetBSD: linux_time.c,v 1.28 2009/01/11 02:45:48 christos Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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>
41 #include <sys/time.h>
42 #include <sys/timetc.h>
43 #include <sys/systm.h>
44 #include <sys/sched.h>
45 #include <sys/syscallargs.h>
46 #include <sys/lwp.h>
47 #include <sys/proc.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;
76 int
77 linux_sys_gettimeofday(struct lwp *l, const struct linux_sys_gettimeofday_args *uap, register_t *retval)
79 /* {
80 syscallarg(struct timeval50 *) tz;
81 syscallarg(struct timezone *) tzp;
82 } */
83 int error = 0;
85 if (SCARG(uap, tp)) {
86 error = compat_50_sys_gettimeofday(l, (const void *)uap, retval);
87 if (error)
88 return (error);
91 if (SCARG(uap, tzp)) {
92 error = copyout(&linux_sys_tz, SCARG(uap, tzp), sizeof(linux_sys_tz));
93 if (error)
94 return (error);
97 return (0);
101 linux_sys_settimeofday(struct lwp *l, const struct linux_sys_settimeofday_args *uap, register_t *retval)
103 /* {
104 syscallarg(struct timeval50 *) tp;
105 syscallarg(struct timezone *) tzp;
106 } */
107 int error = 0;
109 if (SCARG(uap, tp)) {
110 error = compat_50_sys_settimeofday(l, (const void *)uap, retval);
111 if (error)
112 return (error);
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));
121 if (error)
122 return (error);
125 return (0);
128 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */
130 void
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;
137 void
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,
146 register_t *retval)
148 /* {
149 syscallarg(struct linux_timespec *) rqtp;
150 syscallarg(struct linux_timespec *) rmtp;
151 } */
152 struct timespec rqts, rmts;
153 struct linux_timespec lrqts, lrmts;
154 int error, error1;
156 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof(lrqts));
157 if (error != 0)
158 return error;
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))
163 return error;
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)
173 switch (l) {
174 case LINUX_CLOCK_REALTIME:
175 *n = CLOCK_REALTIME;
176 break;
177 case LINUX_CLOCK_MONOTONIC:
178 *n = CLOCK_MONOTONIC;
179 break;
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:
184 default:
185 return EINVAL;
188 return 0;
192 linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval)
194 /* {
195 syscallarg(clockid_t) which;
196 syscallarg(struct linux_timespec *)tp;
197 } */
198 struct timespec ts;
199 struct linux_timespec lts;
201 switch (SCARG(uap, which)) {
202 case LINUX_CLOCK_REALTIME:
203 nanotime(&ts);
204 break;
205 case LINUX_CLOCK_MONOTONIC:
206 nanouptime(&ts);
207 break;
208 default:
209 return EINVAL;
212 native_to_linux_timespec(&lts, &ts);
213 return copyout(&lts, SCARG(uap, tp), sizeof lts);
217 linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_args *uap, register_t *retval)
219 /* {
220 syscallarg(clockid_t) which;
221 syscallarg(struct linux_timespec *)tp;
222 } */
223 struct timespec ts;
224 struct linux_timespec lts;
225 int error;
227 switch (SCARG(uap, which)) {
228 case LINUX_CLOCK_REALTIME:
229 break;
230 default:
231 return EINVAL;
234 error = copyin(SCARG(uap, tp), &lts, sizeof lts);
235 if (error != 0)
236 return error;
238 linux_to_native_timespec(&ts, &lts);
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)
246 /* {
247 syscallarg(clockid_t) which;
248 syscallarg(struct linux_timespec *)tp;
249 } */
250 struct timespec ts;
251 struct linux_timespec lts;
252 int error;
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)
257 return error;
259 ts.tv_sec = 0;
260 ts.tv_nsec = 1000000000 / tc_getfrequency();
261 native_to_linux_timespec(&lts, &ts);
262 return copyout(&lts, SCARG(uap, tp), sizeof lts);
266 linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval)
268 /* {
269 syscallarg(clockid_t) which;
270 syscallarg(int) flags;
271 syscallarg(struct linux_timespec) *rqtp;
272 syscallarg(struct linux_timespec) *rmtp;
273 } */
274 struct linux_timespec lrqts, lrmts;
275 struct timespec rqts, rmts;
276 int error, error1;
278 if (SCARG(uap, flags) != 0)
279 return EINVAL; /* XXX deal with TIMER_ABSTIME */
281 if (SCARG(uap, which) != LINUX_CLOCK_REALTIME)
282 return EINVAL;
284 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts);
285 if (error != 0)
286 return error;
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))
292 return error;
294 native_to_linux_timespec(&lrmts, &rmts);
295 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts);
296 return error1 ? error1 : error;