xref: /netbsd-src/sys/compat/linux/common/linux_time.c (revision 48360965f30c307b6836d0d898d15ce6c1d9b387)
1 /*	$NetBSD: linux_time.c,v 1.34 2010/07/12 12:01:53 njoly Exp $ */
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Emmanuel Dreyfus.
9  *
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  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.34 2010/07/12 12:01:53 njoly Exp $");
34 
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/systm.h>
43 #include <sys/sched.h>
44 #include <sys/syscallargs.h>
45 #include <sys/lwp.h>
46 #include <sys/proc.h>
47 
48 #include <compat/linux/common/linux_types.h>
49 #include <compat/linux/common/linux_signal.h>
50 #include <compat/linux/common/linux_machdep.h>
51 #include <compat/linux/common/linux_sched.h>
52 #include <compat/linux/common/linux_ipc.h>
53 #include <compat/linux/common/linux_sem.h>
54 
55 #include <compat/linux/linux_syscallargs.h>
56 
57 #include <compat/common/compat_util.h>
58 
59 void native_to_linux_timespec(struct linux_timespec *, struct timespec *);
60 void linux_to_native_timespec(struct timespec *, struct linux_timespec *);
61 /*
62  * This is not implemented for alpha yet
63  */
64 #if defined (__i386__) || defined (__m68k__) || \
65     defined (__powerpc__) || defined (__mips__) || \
66     defined(__arm__) || defined(__amd64__)
67 
68 /*
69  * Linux keeps track of a system timezone in the kernel. It is readen
70  * by gettimeofday and set by settimeofday. This emulates this behavior
71  * See linux/kernel/time.c
72  */
73 struct timezone linux_sys_tz;
74 
75 int
76 linux_sys_gettimeofday(struct lwp *l, const struct linux_sys_gettimeofday_args *uap, register_t *retval)
77 {
78 	/* {
79 		syscallarg(struct timeval50 *) tz;
80 		syscallarg(struct timezone *) tzp;
81 	} */
82 	int error = 0;
83 
84 	if (SCARG(uap, tp)) {
85 		error = compat_50_sys_gettimeofday(l, (const void *)uap, retval);
86 		if (error)
87 			return (error);
88 	}
89 
90 	if (SCARG(uap, tzp)) {
91 		error = copyout(&linux_sys_tz, SCARG(uap, tzp), sizeof(linux_sys_tz));
92 		if (error)
93 			return (error);
94    }
95 
96 	return (0);
97 }
98 
99 int
100 linux_sys_settimeofday(struct lwp *l, const struct linux_sys_settimeofday_args *uap, register_t *retval)
101 {
102 	/* {
103 		syscallarg(struct timeval50 *) tp;
104 		syscallarg(struct timezone *) tzp;
105 	} */
106 	int error = 0;
107 
108 	if (SCARG(uap, tp)) {
109 		error = compat_50_sys_settimeofday(l, (const void *)uap, retval);
110 		if (error)
111 			return (error);
112 	}
113 
114 	/*
115 	 * If user is not the superuser, we returned
116 	 * after the sys_settimeofday() call.
117 	 */
118 	if (SCARG(uap, tzp)) {
119 		error = copyin(SCARG(uap, tzp), &linux_sys_tz, sizeof(linux_sys_tz));
120 		if (error)
121 			return (error);
122 	}
123 
124 	return (0);
125 }
126 
127 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */
128 
129 void
130 native_to_linux_timespec(struct linux_timespec *ltp, struct timespec *ntp)
131 {
132 	ltp->tv_sec = ntp->tv_sec;
133 	ltp->tv_nsec = ntp->tv_nsec;
134 }
135 
136 void
137 linux_to_native_timespec(struct timespec *ntp, struct linux_timespec *ltp)
138 {
139 	ntp->tv_sec = ltp->tv_sec;
140 	ntp->tv_nsec = ltp->tv_nsec;
141 }
142 
143 int
144 linux_sys_nanosleep(struct lwp *l, const struct linux_sys_nanosleep_args *uap,
145     register_t *retval)
146 {
147 	/* {
148 		syscallarg(struct linux_timespec *) rqtp;
149 		syscallarg(struct linux_timespec *) rmtp;
150 	} */
151 	struct timespec rqts, rmts;
152 	struct linux_timespec lrqts, lrmts;
153 	int error, error1;
154 
155 	error = copyin(SCARG(uap, rqtp), &lrqts, sizeof(lrqts));
156 	if (error != 0)
157 		return error;
158 	linux_to_native_timespec(&rqts, &lrqts);
159 
160 	error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : NULL);
161 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
162 		return error;
163 
164 	native_to_linux_timespec(&lrmts, &rmts);
165 	error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof(lrmts));
166 	return error1 ? error1 : error;
167 }
168 
169 int
170 linux_to_native_clockid(clockid_t *n, clockid_t l)
171 {
172 	switch (l) {
173 	case LINUX_CLOCK_REALTIME:
174 		*n = CLOCK_REALTIME;
175 		break;
176 	case LINUX_CLOCK_MONOTONIC:
177 		*n = CLOCK_MONOTONIC;
178 		break;
179 	case LINUX_CLOCK_PROCESS_CPUTIME_ID:
180 	case LINUX_CLOCK_THREAD_CPUTIME_ID:
181 	case LINUX_CLOCK_REALTIME_HR:
182 	case LINUX_CLOCK_MONOTONIC_HR:
183 	default:
184 		return EINVAL;
185 	}
186 
187 	return 0;
188 }
189 
190 int
191 linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval)
192 {
193 	/* {
194 		syscallarg(clockid_t) which;
195 		syscallarg(struct linux_timespec *)tp;
196 	} */
197 	int error;
198 	clockid_t id;
199 	struct timespec ts;
200 	struct linux_timespec lts;
201 
202 	error = linux_to_native_clockid(&id, SCARG(uap, which));
203 	if (error != 0)
204 		return error;
205 
206 	error = clock_gettime1(id, &ts);
207 	if (error != 0)
208 		return error;
209 
210 	native_to_linux_timespec(&lts, &ts);
211 	return copyout(&lts, SCARG(uap, tp), sizeof lts);
212 }
213 
214 int
215 linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_args *uap, register_t *retval)
216 {
217 	/* {
218 		syscallarg(clockid_t) which;
219 		syscallarg(struct linux_timespec *)tp;
220 	} */
221 	struct timespec ts;
222 	struct linux_timespec lts;
223 	clockid_t id;
224 	int error;
225 
226 	error = linux_to_native_clockid(&id, SCARG(uap, which));
227 	if (error != 0)
228 		return error;
229 
230 	error = copyin(SCARG(uap, tp), &lts, sizeof lts);
231 	if (error != 0)
232 		return error;
233 
234 	linux_to_native_timespec(&ts, &lts);
235 
236 	return clock_settime1(l->l_proc, id, &ts, true);
237 }
238 
239 int
240 linux_sys_clock_getres(struct lwp *l, const struct linux_sys_clock_getres_args *uap, register_t *retval)
241 {
242 	/* {
243 		syscallarg(clockid_t) which;
244 		syscallarg(struct linux_timespec *)tp;
245 	} */
246 	struct timespec ts;
247 	struct linux_timespec lts;
248 	int error;
249 	clockid_t nwhich = 0;	/* XXX: GCC */
250 
251 	error = linux_to_native_clockid(&nwhich, SCARG(uap, which));
252 	if (error != 0 || SCARG(uap, tp) == NULL)
253 		return error;
254 
255 	error = clock_getres1(nwhich, &ts);
256 	if (error != 0)
257 		return error;
258 
259 	native_to_linux_timespec(&lts, &ts);
260 	return copyout(&lts, SCARG(uap, tp), sizeof lts);
261 }
262 
263 int
264 linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval)
265 {
266 	/* {
267 		syscallarg(clockid_t) which;
268 		syscallarg(int) flags;
269 		syscallarg(struct linux_timespec) *rqtp;
270 		syscallarg(struct linux_timespec) *rmtp;
271 	} */
272 	struct linux_timespec lrqts, lrmts;
273 	struct timespec rqts, rmts;
274 	int error, error1;
275 	clockid_t nwhich;
276 
277 	if (SCARG(uap, flags) != 0)
278 		return EINVAL;		/* XXX deal with TIMER_ABSTIME */
279 
280 	error = linux_to_native_clockid(&nwhich, SCARG(uap, which));
281 	if (error != 0)
282 		return error;
283 
284 	error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts);
285 	if (error != 0)
286 		return error;
287 
288 	linux_to_native_timespec(&rqts, &lrqts);
289 
290 	error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : NULL);
291 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
292 		return error;
293 
294 	native_to_linux_timespec(&lrmts, &rmts);
295 	error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts);
296 	return error1 ? error1 : error;
297 }
298