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