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