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