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