xref: /netbsd-src/sys/compat/linux/common/linux_time.c (revision 7c3f385475147b6e1c4753f2bee961630e2dfc40)
1 /*	$NetBSD: linux_time.c,v 1.23 2008/04/17 17:47:23 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  * 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.23 2008/04/17 17:47:23 njoly 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 #include <compat/linux/common/linux_ipc.h>
60 #include <compat/linux/common/linux_sem.h>
61 
62 #include <compat/linux/linux_syscallargs.h>
63 
64 #include <compat/common/compat_util.h>
65 
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 *);
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(struct lwp *l, const struct linux_sys_gettimeofday_args *uap, register_t *retval)
86 {
87 	/* {
88 		syscallarg(struct timeval *) tz;
89 		syscallarg(struct timezone *) tzp;
90 	} */
91 	int error = 0;
92 
93 	if (SCARG(uap, tp)) {
94 		error = sys_gettimeofday(l, (const void *)uap, retval);
95 		if (error)
96 			return (error);
97 	}
98 
99 	if (SCARG(uap, tzp)) {
100 		error = copyout(&linux_sys_tz, SCARG(uap, tzp), sizeof(linux_sys_tz));
101 		if (error)
102 			return (error);
103    }
104 
105 	return (0);
106 }
107 
108 int
109 linux_sys_settimeofday(struct lwp *l, const struct linux_sys_settimeofday_args *uap, register_t *retval)
110 {
111 	/* {
112 		syscallarg(struct timeval *) tp;
113 		syscallarg(struct timezone *) tzp;
114 	} */
115 	int error = 0;
116 
117 	if (SCARG(uap, tp)) {
118 		error = sys_settimeofday(l, (const void *)uap, retval);
119 		if (error)
120 			return (error);
121 	}
122 
123 	/*
124 	 * If user is not the superuser, we returned
125 	 * after the sys_settimeofday() call.
126 	 */
127 	if (SCARG(uap, tzp)) {
128 		error = copyin(SCARG(uap, tzp), &linux_sys_tz, sizeof(linux_sys_tz));
129 		if (error)
130 			return (error);
131 	}
132 
133 	return (0);
134 }
135 
136 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */
137 
138 static void
139 native_to_linux_timespec(struct linux_timespec *ltp, struct timespec *ntp)
140 {
141 	ltp->tv_sec = ntp->tv_sec;
142 	ltp->tv_nsec = ntp->tv_nsec;
143 }
144 
145 static void
146 linux_to_native_timespec(struct timespec *ntp, struct linux_timespec *ltp)
147 {
148 	ntp->tv_sec = ltp->tv_sec;
149 	ntp->tv_nsec = ltp->tv_nsec;
150 }
151 
152 int
153 linux_to_native_clockid(clockid_t *n, clockid_t l)
154 {
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;
167 	}
168 
169 	return 0;
170 }
171 
172 int
173 linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval)
174 {
175 	/* {
176 		syscallarg(clockid_t) which;
177 		syscallarg(struct linux_timespec *)tp;
178 	} */
179 	struct timespec ts;
180 	struct linux_timespec lts;
181 
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;
191 	}
192 
193 	native_to_linux_timespec(&lts, &ts);
194 	return copyout(&lts, SCARG(uap, tp), sizeof lts);
195 }
196 
197 int
198 linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_args *uap, register_t *retval)
199 {
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;
207 
208 	switch (SCARG(uap, which)) {
209 	case LINUX_CLOCK_REALTIME:
210 		break;
211 	default:
212 		return EINVAL;
213 	}
214 
215 	error = copyin(SCARG(uap, tp), &lts, sizeof lts);
216 	if (error != 0)
217 		return error;
218 
219 	linux_to_native_timespec(&ts, &lts);
220 
221 	return settime(l->l_proc, &ts);
222 }
223 
224 int
225 linux_sys_clock_getres(struct lwp *l, const struct linux_sys_clock_getres_args *uap, register_t *retval)
226 {
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 */
235 
236 	error = linux_to_native_clockid(&nwhich, SCARG(uap, which));
237 	if (error != 0 || SCARG(uap, tp) == NULL)
238 		return error;
239 
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);
244 }
245 
246 int
247 linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval)
248 {
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;
258 
259 	if (SCARG(uap, flags) != 0)
260 		return EINVAL;		/* XXX deal with TIMER_ABSTIME */
261 
262 	if (SCARG(uap, which) != LINUX_CLOCK_REALTIME)
263 		return EINVAL;
264 
265 	error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts);
266 	if (error != 0)
267 		return error;
268 
269 	linux_to_native_timespec(&rqts, &lrqts);
270 
271 	error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : 0);
272 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
273 		return error;
274 
275 	native_to_linux_timespec(&lrmts, &rmts);
276 	error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts);
277 	return error1 ? error1 : error;
278 }
279