Add compat linux32 sys_clock_{getres,gettime,settime} syscalls.
[netbsd-mini2440.git] / sys / compat / linux / common / linux_time.c
blobf31298f456492201feafe4440d59aa1034028647
1 /* $NetBSD: linux_time.c,v 1.22 2007/12/20 23:02:57 dsl 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.
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>
48 #include <sys/time.h>
49 #include <sys/timetc.h>
50 #include <sys/systm.h>
51 #include <sys/syscallargs.h>
52 #include <sys/lwp.h>
53 #include <sys/proc.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 *,
67 struct 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;
84 int
85 linux_sys_gettimeofday(struct lwp *l, const struct linux_sys_gettimeofday_args *uap, register_t *retval)
87 /* {
88 syscallarg(struct timeval *) tz;
89 syscallarg(struct timezone *) tzp;
90 } */
91 int error = 0;
93 if (SCARG(uap, tp)) {
94 error = sys_gettimeofday(l, (const void *)uap, retval);
95 if (error)
96 return (error);
99 if (SCARG(uap, tzp)) {
100 error = copyout(&linux_sys_tz, SCARG(uap, tzp), sizeof(linux_sys_tz));
101 if (error)
102 return (error);
105 return (0);
109 linux_sys_settimeofday(struct lwp *l, const struct linux_sys_settimeofday_args *uap, register_t *retval)
111 /* {
112 syscallarg(struct timeval *) tp;
113 syscallarg(struct timezone *) tzp;
114 } */
115 int error = 0;
117 if (SCARG(uap, tp)) {
118 error = sys_settimeofday(l, (const void *)uap, retval);
119 if (error)
120 return (error);
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));
129 if (error)
130 return (error);
133 return (0);
136 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */
138 static void
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;
145 static void
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)
155 switch (l) {
156 case LINUX_CLOCK_REALTIME:
157 *n = CLOCK_REALTIME;
158 break;
159 case LINUX_CLOCK_MONOTONIC:
160 *n = CLOCK_MONOTONIC;
161 break;
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:
166 return EINVAL;
169 return 0;
173 linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval)
175 /* {
176 syscallarg(clockid_t) which;
177 syscallarg(struct linux_timespec *)tp;
178 } */
179 struct timespec ts;
180 struct linux_timespec lts;
182 switch (SCARG(uap, which)) {
183 case LINUX_CLOCK_REALTIME:
184 nanotime(&ts);
185 break;
186 case LINUX_CLOCK_MONOTONIC:
187 nanouptime(&ts);
188 break;
189 default:
190 return EINVAL;
193 native_to_linux_timespec(&lts, &ts);
194 return copyout(&lts, SCARG(uap, tp), sizeof lts);
198 linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_args *uap, register_t *retval)
200 /* {
201 syscallarg(clockid_t) which;
202 syscallarg(struct linux_timespec *)tp;
203 } */
204 struct timespec ts;
205 struct linux_timespec lts;
206 int error;
208 switch (SCARG(uap, which)) {
209 case LINUX_CLOCK_REALTIME:
210 break;
211 default:
212 return EINVAL;
215 error = copyin(SCARG(uap, tp), &lts, sizeof lts);
216 if (error != 0)
217 return error;
219 linux_to_native_timespec(&ts, &lts);
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)
227 /* {
228 syscallarg(clockid_t) which;
229 syscallarg(struct linux_timespec *)tp;
230 } */
231 struct timespec ts;
232 struct linux_timespec lts;
233 int error;
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)
238 return error;
240 ts.tv_sec = 0;
241 ts.tv_nsec = 1000000000 / tc_getfrequency();
242 native_to_linux_timespec(&lts, &ts);
243 return copyout(&lts, SCARG(uap, tp), sizeof lts);
247 linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval)
249 /* {
250 syscallarg(clockid_t) which;
251 syscallarg(int) flags;
252 syscallarg(struct linux_timespec) *rqtp;
253 syscallarg(struct linux_timespec) *rmtp;
254 } */
255 struct linux_timespec lrqts, lrmts;
256 struct timespec rqts, rmts;
257 int error, error1;
259 if (SCARG(uap, flags) != 0)
260 return EINVAL; /* XXX deal with TIMER_ABSTIME */
262 if (SCARG(uap, which) != LINUX_CLOCK_REALTIME)
263 return EINVAL;
265 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts);
266 if (error != 0)
267 return error;
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))
273 return error;
275 native_to_linux_timespec(&lrmts, &rmts);
276 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts);
277 return error1 ? error1 : error;