]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_time.c
MFC r317324:
[FreeBSD/FreeBSD.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
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/ucred.h>
44 #include <sys/limits.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/resourcevar.h>
48 #include <sys/sdt.h>
49 #include <sys/signal.h>
50 #include <sys/stdint.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysproto.h>
53 #include <sys/time.h>
54 #include <sys/systm.h>
55 #include <sys/proc.h>
56
57 #ifdef COMPAT_LINUX32
58 #include <machine/../linux32/linux.h>
59 #include <machine/../linux32/linux32_proto.h>
60 #else
61 #include <machine/../linux/linux.h>
62 #include <machine/../linux/linux_proto.h>
63 #endif
64
65 #include <compat/linux/linux_dtrace.h>
66 #include <compat/linux/linux_timer.h>
67
68 /* DTrace init */
69 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
70
71 /**
72  * DTrace probes in this module.
73  */
74 LIN_SDT_PROBE_DEFINE2(time, native_to_linux_timespec, entry,
75     "struct l_timespec *", "struct timespec *");
76 LIN_SDT_PROBE_DEFINE0(time, native_to_linux_timespec, return);
77 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_timespec, entry,
78     "struct timespec *", "struct l_timespec *");
79 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_timespec, return, "int");
80 LIN_SDT_PROBE_DEFINE2(time, linux_to_native_clockid, entry, "clockid_t *",
81     "clockid_t");
82 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unsupported_clockid,
83     "clockid_t");
84 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, unknown_clockid,
85     "clockid_t");
86 LIN_SDT_PROBE_DEFINE1(time, linux_to_native_clockid, return, "int");
87 LIN_SDT_PROBE_DEFINE2(time, linux_clock_gettime, entry, "clockid_t",
88     "struct l_timespec *");
89 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, conversion_error, "int");
90 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, gettime_error, "int");
91 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, copyout_error, "int");
92 LIN_SDT_PROBE_DEFINE1(time, linux_clock_gettime, return, "int");
93 LIN_SDT_PROBE_DEFINE2(time, linux_clock_settime, entry, "clockid_t",
94     "struct l_timespec *");
95 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, conversion_error, "int");
96 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, settime_error, "int");
97 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, copyin_error, "int");
98 LIN_SDT_PROBE_DEFINE1(time, linux_clock_settime, return, "int");
99 LIN_SDT_PROBE_DEFINE2(time, linux_clock_getres, entry, "clockid_t",
100     "struct l_timespec *");
101 LIN_SDT_PROBE_DEFINE0(time, linux_clock_getres, nullcall);
102 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, conversion_error, "int");
103 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, getres_error, "int");
104 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, copyout_error, "int");
105 LIN_SDT_PROBE_DEFINE1(time, linux_clock_getres, return, "int");
106 LIN_SDT_PROBE_DEFINE2(time, linux_nanosleep, entry, "const struct l_timespec *",
107     "struct l_timespec *");
108 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, conversion_error, "int");
109 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyout_error, "int");
110 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, copyin_error, "int");
111 LIN_SDT_PROBE_DEFINE1(time, linux_nanosleep, return, "int");
112 LIN_SDT_PROBE_DEFINE4(time, linux_clock_nanosleep, entry, "clockid_t", "int",
113     "struct l_timespec *", "struct l_timespec *");
114 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, conversion_error, "int");
115 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyout_error, "int");
116 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, copyin_error, "int");
117 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_flags, "int");
118 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, unsupported_clockid, "int");
119 LIN_SDT_PROBE_DEFINE1(time, linux_clock_nanosleep, return, "int");
120
121
122 int
123 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
124 {
125
126         LIN_SDT_PROBE2(time, native_to_linux_timespec, entry, ltp, ntp);
127 #ifdef COMPAT_LINUX32
128         if (ntp->tv_sec > INT_MAX || ntp->tv_sec < INT_MIN)
129                 return (EOVERFLOW);
130 #endif
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         return (0);
136 }
137
138 int
139 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
140 {
141
142         LIN_SDT_PROBE2(time, linux_to_native_timespec, entry, ntp, ltp);
143
144         if (ltp->tv_sec < 0 || ltp->tv_nsec < 0 || ltp->tv_nsec > 999999999) {
145                 LIN_SDT_PROBE1(time, linux_to_native_timespec, return, EINVAL);
146                 return (EINVAL);
147         }
148         ntp->tv_sec = ltp->tv_sec;
149         ntp->tv_nsec = ltp->tv_nsec;
150
151         LIN_SDT_PROBE1(time, linux_to_native_timespec, return, 0);
152         return (0);
153 }
154
155 int
156 native_to_linux_itimerspec(struct l_itimerspec *ltp, struct itimerspec *ntp)
157 {
158         int error;
159
160         error = native_to_linux_timespec(&ltp->it_interval, &ntp->it_interval);
161         if (error == 0)
162                 error = native_to_linux_timespec(&ltp->it_value, &ntp->it_interval);
163         return (error);
164 }
165
166 int
167 linux_to_native_itimerspec(struct itimerspec *ntp, struct l_itimerspec *ltp)
168 {
169         int error;
170
171         error = linux_to_native_timespec(&ntp->it_interval, &ltp->it_interval);
172         if (error == 0)
173                 error = linux_to_native_timespec(&ntp->it_value, &ltp->it_value);
174         return (error);
175 }
176
177 int
178 linux_to_native_clockid(clockid_t *n, clockid_t l)
179 {
180
181         LIN_SDT_PROBE2(time, linux_to_native_clockid, entry, n, l);
182
183         if (l < 0) {
184                 /* cpu-clock */
185                 if ((l & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD)
186                         return (EINVAL);
187                 if (LINUX_CPUCLOCK_WHICH(l) >= LINUX_CPUCLOCK_MAX)
188                         return (EINVAL);
189
190                 if (LINUX_CPUCLOCK_PERTHREAD(l))
191                         *n = CLOCK_THREAD_CPUTIME_ID;
192                 else
193                         *n = CLOCK_PROCESS_CPUTIME_ID;
194                 return (0);
195         }
196
197         switch (l) {
198         case LINUX_CLOCK_REALTIME:
199                 *n = CLOCK_REALTIME;
200                 break;
201         case LINUX_CLOCK_MONOTONIC:
202                 *n = CLOCK_MONOTONIC;
203                 break;
204         case LINUX_CLOCK_REALTIME_COARSE:
205                 *n = CLOCK_REALTIME_FAST;
206                 break;
207         case LINUX_CLOCK_MONOTONIC_COARSE:
208                 *n = CLOCK_MONOTONIC_FAST;
209                 break;
210         case LINUX_CLOCK_BOOTTIME:
211                 *n = CLOCK_UPTIME;
212                 break;
213         case LINUX_CLOCK_MONOTONIC_RAW:
214         case LINUX_CLOCK_REALTIME_ALARM:
215         case LINUX_CLOCK_BOOTTIME_ALARM:
216         case LINUX_CLOCK_SGI_CYCLE:
217         case LINUX_CLOCK_TAI:
218                 LIN_SDT_PROBE1(time, linux_to_native_clockid,
219                     unsupported_clockid, l);
220                 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
221                 return (EINVAL);
222         default:
223                 LIN_SDT_PROBE1(time, linux_to_native_clockid,
224                     unknown_clockid, l);
225                 LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
226                 return (EINVAL);
227         }
228
229         LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
230         return (0);
231 }
232
233 int
234 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
235 {
236         struct l_timespec lts;
237         struct timespec tp;
238         struct rusage ru;
239         struct thread *targettd;
240         struct proc *p;
241         int error, clockwhich;
242         clockid_t nwhich = 0;   /* XXX: GCC */
243         pid_t pid;
244         lwpid_t tid;
245
246         LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
247
248         error = linux_to_native_clockid(&nwhich, args->which);
249         if (error != 0) {
250                 LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
251                     error);
252                 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
253                 return (error);
254         }
255
256         switch (nwhich) {
257         case CLOCK_PROCESS_CPUTIME_ID:
258                 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
259                 pid = LINUX_CPUCLOCK_ID(args->which);
260                 if (pid == 0) {
261                         p = td->td_proc;
262                         PROC_LOCK(p);
263                 } else {
264                         error = pget(pid, PGET_CANSEE, &p);
265                         if (error != 0)
266                                 return (EINVAL);
267                 }
268                 switch (clockwhich) {
269                 case LINUX_CPUCLOCK_PROF:
270                         PROC_STATLOCK(p);
271                         calcru(p, &ru.ru_utime, &ru.ru_stime);
272                         PROC_STATUNLOCK(p);
273                         PROC_UNLOCK(p);
274                         timevaladd(&ru.ru_utime, &ru.ru_stime);
275                         TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
276                         break;
277                 case LINUX_CPUCLOCK_VIRT:
278                         PROC_STATLOCK(p);
279                         calcru(p, &ru.ru_utime, &ru.ru_stime);
280                         PROC_STATUNLOCK(p);
281                         PROC_UNLOCK(p);
282                         TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
283                         break;
284                 case LINUX_CPUCLOCK_SCHED:
285                         PROC_UNLOCK(p);
286                         error = kern_clock_getcpuclockid2(td, pid,
287                             CPUCLOCK_WHICH_PID, &nwhich);
288                         if (error != 0)
289                                 return (EINVAL);
290                         error = kern_clock_gettime(td, nwhich, &tp);
291                         break;
292                 default:
293                         PROC_UNLOCK(p);
294                         return (EINVAL);
295                 }
296
297                 break;
298
299         case CLOCK_THREAD_CPUTIME_ID:
300                 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
301                 p = td->td_proc;
302                 tid = LINUX_CPUCLOCK_ID(args->which);
303                 if (tid == 0) {
304                         targettd = td;
305                         PROC_LOCK(p);
306                 } else {
307                         targettd = tdfind(tid, p->p_pid);
308                         if (targettd == NULL)
309                                 return (EINVAL);
310                 }
311                 switch (clockwhich) {
312                 case LINUX_CPUCLOCK_PROF:
313                         PROC_STATLOCK(p);
314                         thread_lock(targettd);
315                         rufetchtd(targettd, &ru);
316                         thread_unlock(targettd);
317                         PROC_STATUNLOCK(p);
318                         PROC_UNLOCK(p);
319                         timevaladd(&ru.ru_utime, &ru.ru_stime);
320                         TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
321                         break;
322                 case LINUX_CPUCLOCK_VIRT:
323                         PROC_STATLOCK(p);
324                         thread_lock(targettd);
325                         rufetchtd(targettd, &ru);
326                         thread_unlock(targettd);
327                         PROC_STATUNLOCK(p);
328                         PROC_UNLOCK(p);
329                         TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
330                         break;
331                 case LINUX_CPUCLOCK_SCHED:
332                         error = kern_clock_getcpuclockid2(td, tid,
333                             CPUCLOCK_WHICH_TID, &nwhich);
334                         PROC_UNLOCK(p);
335                         if (error != 0)
336                                 return (EINVAL);
337                         error = kern_clock_gettime(td, nwhich, &tp);
338                         break;
339                 default:
340                         PROC_UNLOCK(p);
341                         return (EINVAL);
342                 }
343                 break;
344
345         default:
346                 error = kern_clock_gettime(td, nwhich, &tp);
347                 break;
348         }
349         if (error != 0) {
350                 LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
351                 LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
352                 return (error);
353         }
354         error = native_to_linux_timespec(&lts, &tp);
355         if (error != 0)
356                 return (error);
357         error = copyout(&lts, args->tp, sizeof lts);
358         if (error != 0)
359                 LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
360
361         LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
362         return (error);
363 }
364
365 int
366 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
367 {
368         struct timespec ts;
369         struct l_timespec lts;
370         int error;
371         clockid_t nwhich = 0;   /* XXX: GCC */
372
373         LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
374
375         error = linux_to_native_clockid(&nwhich, args->which);
376         if (error != 0) {
377                 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
378                     error);
379                 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
380                 return (error);
381         }
382         error = copyin(args->tp, &lts, sizeof lts);
383         if (error != 0) {
384                 LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
385                 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
386                 return (error);
387         }
388         error = linux_to_native_timespec(&ts, &lts);
389         if (error != 0) {
390                 LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
391                     error);
392                 LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
393                 return (error);
394         }
395
396         error = kern_clock_settime(td, nwhich, &ts);
397         if (error != 0)
398                 LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
399
400         LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
401         return (error);
402 }
403
404 int
405 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
406 {
407         struct proc *p;
408         struct timespec ts;
409         struct l_timespec lts;
410         int error, clockwhich;
411         clockid_t nwhich = 0;   /* XXX: GCC */
412         pid_t pid;
413         lwpid_t tid;
414
415         LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
416
417         error = linux_to_native_clockid(&nwhich, args->which);
418         if (error != 0) {
419                 LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
420                     error);
421                 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
422                 return (error);
423         }
424
425         /*
426          * Check user supplied clock id in case of per-process 
427          * or thread-specific cpu-time clock.
428          */
429         switch (nwhich) {
430         case CLOCK_THREAD_CPUTIME_ID:
431                 tid = LINUX_CPUCLOCK_ID(args->which);
432                 if (tid != 0) {
433                         p = td->td_proc;
434                         if (tdfind(tid, p->p_pid) == NULL)
435                                 return (ESRCH);
436                         PROC_UNLOCK(p);
437                 }
438                 break;
439         case CLOCK_PROCESS_CPUTIME_ID:
440                 pid = LINUX_CPUCLOCK_ID(args->which);
441                 if (pid != 0) {
442                         error = pget(pid, PGET_CANSEE, &p);
443                         if (error != 0)
444                                 return (EINVAL);
445                         PROC_UNLOCK(p);
446                 }
447                 break;
448         }
449
450         if (args->tp == NULL) {
451                 LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
452                 LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
453                 return (0);
454         }
455
456         switch (nwhich) {
457         case CLOCK_THREAD_CPUTIME_ID:
458         case CLOCK_PROCESS_CPUTIME_ID:
459                 clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
460                 switch (clockwhich) {
461                 case LINUX_CPUCLOCK_PROF:
462                         nwhich = CLOCK_PROF;
463                         break;
464                 case LINUX_CPUCLOCK_VIRT:
465                         nwhich = CLOCK_VIRTUAL;
466                         break;
467                 case LINUX_CPUCLOCK_SCHED:
468                         break;
469                 default:
470                         return (EINVAL);
471                 }
472                 break;
473
474         default:
475                 break;
476         }
477         error = kern_clock_getres(td, nwhich, &ts);
478         if (error != 0) {
479                 LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
480                 LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
481                 return (error);
482         }
483         error = native_to_linux_timespec(&lts, &ts);
484         if (error != 0)
485                 return (error);
486         error = copyout(&lts, args->tp, sizeof lts);
487         if (error != 0)
488                 LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
489
490         LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
491         return (error);
492 }
493
494 int
495 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
496 {
497         struct timespec *rmtp;
498         struct l_timespec lrqts, lrmts;
499         struct timespec rqts, rmts;
500         int error, error2;
501
502         LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
503
504         error = copyin(args->rqtp, &lrqts, sizeof lrqts);
505         if (error != 0) {
506                 LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
507                 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
508                 return (error);
509         }
510
511         if (args->rmtp != NULL)
512                 rmtp = &rmts;
513         else
514                 rmtp = NULL;
515
516         error = linux_to_native_timespec(&rqts, &lrqts);
517         if (error != 0) {
518                 LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
519                 LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
520                 return (error);
521         }
522         error = kern_nanosleep(td, &rqts, rmtp);
523         if (error == EINTR && args->rmtp != NULL) {
524                 error2 = native_to_linux_timespec(&lrmts, rmtp);
525                 if (error2 != 0)
526                         return (error2);
527                 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
528                 if (error2 != 0) {
529                         LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
530                             error2);
531                         LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
532                         return (error2);
533                 }
534         }
535
536         LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
537         return (error);
538 }
539
540 int
541 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
542 {
543         struct timespec *rmtp;
544         struct l_timespec lrqts, lrmts;
545         struct timespec rqts, rmts;
546         int error, error2;
547
548         LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
549             args->flags, args->rqtp, args->rmtp);
550
551         if (args->flags != 0) {
552                 /* XXX deal with TIMER_ABSTIME */
553                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
554                     args->flags);
555                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
556                 return (EINVAL);        /* XXX deal with TIMER_ABSTIME */
557         }
558
559         if (args->which != LINUX_CLOCK_REALTIME) {
560                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
561                     args->which);
562                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, EINVAL);
563                 return (EINVAL);
564         }
565
566         error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
567         if (error != 0) {
568                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
569                     error);
570                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
571                 return (error);
572         }
573
574         if (args->rmtp != NULL)
575                 rmtp = &rmts;
576         else
577                 rmtp = NULL;
578
579         error = linux_to_native_timespec(&rqts, &lrqts);
580         if (error != 0) {
581                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
582                     error);
583                 LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
584                 return (error);
585         }
586         error = kern_nanosleep(td, &rqts, rmtp);
587         if (error == EINTR && args->rmtp != NULL) {
588                 /* XXX. Not for TIMER_ABSTIME */
589                 error2 = native_to_linux_timespec(&lrmts, rmtp);
590                 if (error2 != 0)
591                         return (error2);
592                 error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
593                 if (error2 != 0) {
594                         LIN_SDT_PROBE1(time, linux_clock_nanosleep,
595                             copyout_error, error2);
596                         LIN_SDT_PROBE1(time, linux_clock_nanosleep,
597                             return, error2);
598                         return (error2);
599                 }
600         }
601
602         LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
603         return (error);
604 }