1 /* $NetBSD: linux_time.c,v 1.25.6.1 2008/11/20 03:03:05 snj 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.6.1 2008/11/20 03:03:05 snj 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_sys_nanosleep(struct lwp *l, const struct linux_sys_nanosleep_args *uap,
148 register_t *retval)
149 {
150 /* {
151 syscallarg(struct linux_timespec *) rqtp;
152 syscallarg(struct linux_timespec *) rmtp;
153 } */
154 struct timespec rqts, rmts;
155 struct linux_timespec lrqts, lrmts;
156 int error, error1;
157
158 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof(lrqts));
159 if (error != 0)
160 return error;
161 linux_to_native_timespec(&rqts, &lrqts);
162
163 error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : NULL);
164 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
165 return error;
166
167 native_to_linux_timespec(&lrmts, &rmts);
168 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof(lrmts));
169 return error1 ? error1 : error;
170 }
171
172 int
173 linux_to_native_clockid(clockid_t *n, clockid_t l)
174 {
175 switch (l) {
176 case LINUX_CLOCK_REALTIME:
177 *n = CLOCK_REALTIME;
178 break;
179 case LINUX_CLOCK_MONOTONIC:
180 *n = CLOCK_MONOTONIC;
181 break;
182 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
183 case LINUX_CLOCK_THREAD_CPUTIME_ID:
184 case LINUX_CLOCK_REALTIME_HR:
185 case LINUX_CLOCK_MONOTONIC_HR:
186 return EINVAL;
187 }
188
189 return 0;
190 }
191
192 int
193 linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval)
194 {
195 /* {
196 syscallarg(clockid_t) which;
197 syscallarg(struct linux_timespec *)tp;
198 } */
199 struct timespec ts;
200 struct linux_timespec lts;
201
202 switch (SCARG(uap, which)) {
203 case LINUX_CLOCK_REALTIME:
204 nanotime(&ts);
205 break;
206 case LINUX_CLOCK_MONOTONIC:
207 nanouptime(&ts);
208 break;
209 default:
210 return EINVAL;
211 }
212
213 native_to_linux_timespec(<s, &ts);
214 return copyout(<s, SCARG(uap, tp), sizeof lts);
215 }
216
217 int
218 linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_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
228 switch (SCARG(uap, which)) {
229 case LINUX_CLOCK_REALTIME:
230 break;
231 default:
232 return EINVAL;
233 }
234
235 error = copyin(SCARG(uap, tp), <s, sizeof lts);
236 if (error != 0)
237 return error;
238
239 linux_to_native_timespec(&ts, <s);
240
241 return settime(l->l_proc, &ts);
242 }
243
244 int
245 linux_sys_clock_getres(struct lwp *l, const struct linux_sys_clock_getres_args *uap, register_t *retval)
246 {
247 /* {
248 syscallarg(clockid_t) which;
249 syscallarg(struct linux_timespec *)tp;
250 } */
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(<s, &ts);
263 return copyout(<s, SCARG(uap, tp), sizeof lts);
264 }
265
266 int
267 linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval)
268 {
269 /* {
270 syscallarg(clockid_t) which;
271 syscallarg(int) flags;
272 syscallarg(struct linux_timespec) *rqtp;
273 syscallarg(struct linux_timespec) *rmtp;
274 } */
275 struct linux_timespec lrqts, lrmts;
276 struct timespec rqts, rmts;
277 int error, error1;
278
279 if (SCARG(uap, flags) != 0)
280 return EINVAL; /* XXX deal with TIMER_ABSTIME */
281
282 if (SCARG(uap, which) != LINUX_CLOCK_REALTIME)
283 return EINVAL;
284
285 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts);
286 if (error != 0)
287 return error;
288
289 linux_to_native_timespec(&rqts, &lrqts);
290
291 error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : 0);
292 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
293 return error;
294
295 native_to_linux_timespec(&lrmts, &rmts);
296 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts);
297 return error1 ? error1 : error;
298 }
Cache object: b0345c9f93a3cac75d797f063d4bfa9a
|