]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/compat/linux/linux_time.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / compat / linux / linux_time.c
1 /*      $NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos 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 __FBSDID("$FreeBSD$");
34 #if 0
35 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $");
36 #endif
37
38 #include "opt_compat.h"
39 #include "opt_kdtrace.h"
40
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/ucred.h>
44 #include <sys/mount.h>
45 #include <sys/sdt.h>
46 #include <sys/signal.h>
47 #include <sys/stdint.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysproto.h>
50 #include <sys/time.h>
51 #include <sys/systm.h>
52 #include <sys/proc.h>
53
54 #ifdef COMPAT_LINUX32
55 #include <machine/../linux32/linux.h>
56 #include <machine/../linux32/linux32_proto.h>
57 #else
58 #include <machine/../linux/linux.h>
59 #include <machine/../linux/linux_proto.h>
60 #endif
61
62 #include <compat/linux/linux_dtrace.h>
63
64 /* DTrace init */
65 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
66
67 /**
68  * DTrace probes in this module.
69  */
70 LIN_SDT_PROBE_DEFINE2(time, native_to_linux_timespec, entry,
71     "struct l_timespec *", "struct timespec *");
72 LIN_SDT_PROBE_DEFINE0(time, native_to_linux_timespec, return);
73 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_timespec, entry,
74     "struct timespec *", "struct l_timespec *");
75 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_timespec, return, "int");
76 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_clockid, entry, "clockid_t *",
77     "clockid_t");
78 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
79     "clockid_t");
80 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
81     "clockid_t");
82 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, return, "int");
83 LIN_SDT_PROBE_DEFINE2(time, linux_clock_gettime, entry, "clockid_t",
84     "struct l_timespec *");
85 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, conversion_error, "int");
86 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
87 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
88 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, return, "int");
89 LIN_SDT_PROBE_DEFINE2(time, linux_clock_settime, entry, "clockid_t",
90     "struct l_timespec *");
91 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
92 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, settime_error, "int");
93 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
94 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, return, "int");
95 LIN_SDT_PROBE_DEFINE2(time, linux_clock_getres, entry, "clockid_t",
96     "struct l_timespec *");
97 LIN_SDT_PROBE_DEFINE0(time, linux_clock_getres, nullcall);
98 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, conversion_error, "int");
99 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, getres_error, "int");
100 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
101 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, return, "int");
102 LIN_SDT_PROBE_DEFINE2(time, linux_nanosleep, entry, "const struct l_timespec *",
103     "struct l_timespec *");
104 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, conversion_error, "int");
105 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, nanosleep_error, "int");
106 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
107 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
108 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, return, "int");
109 LIN_SDT_PROBE_DEFINE4(time, linux_clock_nanosleep, entry, "clockid_t", "int",
110     "struct l_timespec *", "struct l_timespec *");
111 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, conversion_error, "int");
112 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, nanosleep_error, "int");
113 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
114 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
115 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
116 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "int");
117 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, return, "int");
118
119 static void native_to_linux_timespec(struct l_timespec *,
120                                      struct timespec *);
121 static int linux_to_native_timespec(struct timespec *,
122                                      struct l_timespec *);
123 static int linux_to_native_clockid(clockid_t *, clockid_t);
124
125 static void
126 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
127 {
128
129         LIN_SDT_PROBE2(time, native_to_linux_timespec, entry, ltp, ntp);
130
131         ltp->tv_sec = ntp->tv_sec;
132         ltp->tv_nsec = ntp->tv_nsec;
133
134         LIN_SDT_PROBE0(time, native_to_linux_timespec, return);
135 }
136
137 static int
138 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
139 {
140
141         LIN_SDT_PROBE2(time, linux_to_native_timespec, entry, ntp, ltp);
142
143         if (ltp->tv_sec < 0 || ltp->tv_nsec > (l_long)999999999L) {
144                 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, EINVAL);
145                 return (EINVAL);
146         }
147         ntp->tv_sec = ltp->tv_sec;
148         ntp->tv_nsec = ltp->tv_nsec;
149
150         LIN_SDT_PROBE1(time, linux_to_native_timespec, return, 0);
151         return (0);
152 }
153
154 static int
155 linux_to_native_clockid(clockid_t *n, clockid_t l)
156 {
157
158         LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
159
160         switch (l) {
161         case LINUX_CLOCK_REALTIME:
162                 *n = CLOCK_REALTIME;
163                 break;
164         case LINUX_CLOCK_MONOTONIC:
165                 *n = CLOCK_MONOTONIC;
166                 break;
167         case LINUX_CLOCK_PROCESS_CPUTIME_ID:
168         case LINUX_CLOCK_THREAD_CPUTIME_ID:
169         case LINUX_CLOCK_REALTIME_HR:
170         case LINUX_CLOCK_MONOTONIC_HR:
171                 LIN_SDT_PROBE1(time, linux_to_native_clockid,
172                     unsupported_clockid, l);
173                 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
174                 return (EINVAL);
175                 break;
176         default:
177                 LIN_SDT_PROBE1(time, linux_to_native_clockid,
178                     unknown_clockid, l);
179                 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
180                 return (EINVAL);
181                 break;
182         }
183
184         LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
185         return (0);
186 }
187
188 int
189 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
190 {
191         struct l_timespec lts;
192         int error;
193         clockid_t nwhich = 0;   /* XXX: GCC */
194         struct timespec tp;
195
196         LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
197
198         error = linux_to_native_clockid(&nwhich, args->which);
199         if (error != 0) {
200                 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
201                     error);
202                 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
203                 return (error);
204         }
205         error = kern_clock_gettime(td, nwhich, &tp);
206         if (error != 0) {
207                 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
208                 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
209                 return (error);
210         }
211         native_to_linux_timespec(&lts, &tp);
212
213         error = copyout(&lts, args->tp, sizeof lts);
214         if (error != 0)
215                 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
216
217         LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
218         return (error);
219 }
220
221 int
222 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
223 {
224         struct timespec ts;
225         struct l_timespec lts;
226         int error;
227         clockid_t nwhich = 0;   /* XXX: GCC */
228
229         LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
230
231         error = linux_to_native_clockid(&nwhich, args->which);
232         if (error != 0) {
233                 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
234                     error);
235                 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
236                 return (error);
237         }
238         error = copyin(args->tp, &lts, sizeof lts);
239         if (error != 0) {
240                 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
241                 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
242                 return (error);
243         }
244         error = linux_to_native_timespec(&ts, &lts);
245         if (error != 0) {
246                 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
247                     error);
248                 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
249                 return (error);
250         }
251
252         error = kern_clock_settime(td, nwhich, &ts);
253         if (error != 0)
254                 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
255
256         LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
257         return (error);
258 }
259
260 int
261 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
262 {
263         struct timespec ts;
264         struct l_timespec lts;
265         int error;
266         clockid_t nwhich = 0;   /* XXX: GCC */
267
268         LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
269
270         if (args->tp == NULL) {
271                 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
272                 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
273                 return (0);
274         }
275
276         error = linux_to_native_clockid(&nwhich, args->which);
277         if (error != 0) {
278                 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
279                     error);
280                 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
281                 return (error);
282         }
283         error = kern_clock_getres(td, nwhich, &ts);
284         if (error != 0) {
285                 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
286                 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
287                 return (error);
288         }
289         native_to_linux_timespec(&lts, &ts);
290
291         error = copyout(&lts, args->tp, sizeof lts);
292         if (error != 0)
293                 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
294
295         LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
296         return (error);
297 }
298
299 int
300 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
301 {
302         struct timespec *rmtp;
303         struct l_timespec lrqts, lrmts;
304         struct timespec rqts, rmts;
305         int error;
306
307         LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
308
309         error = copyin(args->rqtp, &lrqts, sizeof lrqts);
310         if (error != 0) {
311                 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
312                 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
313                 return (error);
314         }
315
316         if (args->rmtp != NULL)
317                 rmtp = &rmts;
318         else
319                 rmtp = NULL;
320
321         error = linux_to_native_timespec(&rqts, &lrqts);
322         if (error != 0) {
323                 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
324                 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
325                 return (error);
326         }
327         error = kern_nanosleep(td, &rqts, rmtp);
328         if (error != 0) {
329                 LIN_SDT_PROBE1(time, linux_nanosleep, nanosleep_error, error);
330                 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
331                 return (error);
332         }
333
334         if (args->rmtp != NULL) {
335                 native_to_linux_timespec(&lrmts, rmtp);
336                 error = copyout(&lrmts, args->rmtp, sizeof(lrmts));
337                 if (error != 0) {
338                         LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
339                             error);
340                         LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
341                         return (error);
342                 }
343         }
344
345         LIN_SDT_PROBE1(time, linux_nanosleep, return, 0);
346         return (0);
347 }
348
349 int
350 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
351 {
352         struct timespec *rmtp;
353         struct l_timespec lrqts, lrmts;
354         struct timespec rqts, rmts;
355         int error;
356
357         LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
358             args->flags, args->rqtp, args->rmtp);
359
360         if (args->flags != 0) {
361                 /* XXX deal with TIMER_ABSTIME */
362                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
363                     args->flags);
364                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
365                 return (EINVAL);        /* XXX deal with TIMER_ABSTIME */
366         }
367
368         if (args->which != LINUX_CLOCK_REALTIME) {
369                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
370                     args->which);
371                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
372                 return (EINVAL);
373         }
374
375         error = copyin(args->rqtp, &lrqts, sizeof lrqts);
376         if (error != 0) {
377                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
378                     error);
379                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
380                 return (error);
381         }
382
383         if (args->rmtp != NULL)
384                 rmtp = &rmts;
385         else
386                 rmtp = NULL;
387
388         error = linux_to_native_timespec(&rqts, &lrqts);
389         if (error != 0) {
390                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
391                     error);
392                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
393                 return (error);
394         }
395         error = kern_nanosleep(td, &rqts, rmtp);
396         if (error != 0) {
397                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, nanosleep_error,
398                     error);
399                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
400                 return (error);
401         }
402
403         if (args->rmtp != NULL) {
404                 native_to_linux_timespec(&lrmts, rmtp);
405                 error = copyout(&lrmts, args->rmtp, sizeof lrmts );
406                 if (error != 0) {
407                         LIN_SDT_PROBE1(time, linux_clock_nanosleep,
408                             copyout_error, error);
409                         LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
410                         return (error);
411                 }
412         }
413
414         LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, 0);
415         return (0);
416 }