xref: /freebsd/sys/compat/linux/linux_time.c (revision 4ca50eab86aec8001978a612a42f9ae8388eceab)
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_MONOTONIC_RAW:
211 	case LINUX_CLOCK_BOOTTIME:
212 	case LINUX_CLOCK_REALTIME_ALARM:
213 	case LINUX_CLOCK_BOOTTIME_ALARM:
214 	case LINUX_CLOCK_SGI_CYCLE:
215 	case LINUX_CLOCK_TAI:
216 		LIN_SDT_PROBE1(time, linux_to_native_clockid,
217 		    unsupported_clockid, l);
218 		LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
219 		return (EINVAL);
220 	default:
221 		LIN_SDT_PROBE1(time, linux_to_native_clockid,
222 		    unknown_clockid, l);
223 		LIN_SDT_PROBE1(time, linux_to_native_clockid, return, EINVAL);
224 		return (EINVAL);
225 	}
226 
227 	LIN_SDT_PROBE1(time, linux_to_native_clockid, return, 0);
228 	return (0);
229 }
230 
231 int
232 linux_to_native_timerflags(int *nflags, int flags)
233 {
234 
235 	if (flags & ~LINUX_TIMER_ABSTIME)
236 		return (EINVAL);
237 	*nflags = 0;
238 	if (flags & LINUX_TIMER_ABSTIME)
239 		*nflags |= TIMER_ABSTIME;
240 	return (0);
241 }
242 
243 int
244 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
245 {
246 	struct l_timespec lts;
247 	struct timespec tp;
248 	struct rusage ru;
249 	struct thread *targettd;
250 	struct proc *p;
251 	int error, clockwhich;
252 	clockid_t nwhich = 0;	/* XXX: GCC */
253 	pid_t pid;
254 	lwpid_t tid;
255 
256 	LIN_SDT_PROBE2(time, linux_clock_gettime, entry, args->which, args->tp);
257 
258 	error = linux_to_native_clockid(&nwhich, args->which);
259 	if (error != 0) {
260 		LIN_SDT_PROBE1(time, linux_clock_gettime, conversion_error,
261 		    error);
262 		LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
263 		return (error);
264 	}
265 
266 	switch (nwhich) {
267 	case CLOCK_PROCESS_CPUTIME_ID:
268 		clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
269 		pid = LINUX_CPUCLOCK_ID(args->which);
270 		if (pid == 0) {
271 			p = td->td_proc;
272 			PROC_LOCK(p);
273 		} else {
274 			error = pget(pid, PGET_CANSEE, &p);
275 			if (error != 0)
276 				return (EINVAL);
277 		}
278 		switch (clockwhich) {
279 		case LINUX_CPUCLOCK_PROF:
280 			PROC_STATLOCK(p);
281 			calcru(p, &ru.ru_utime, &ru.ru_stime);
282 			PROC_STATUNLOCK(p);
283 			PROC_UNLOCK(p);
284 			timevaladd(&ru.ru_utime, &ru.ru_stime);
285 			TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
286 			break;
287 		case LINUX_CPUCLOCK_VIRT:
288 			PROC_STATLOCK(p);
289 			calcru(p, &ru.ru_utime, &ru.ru_stime);
290 			PROC_STATUNLOCK(p);
291 			PROC_UNLOCK(p);
292 			TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
293 			break;
294 		case LINUX_CPUCLOCK_SCHED:
295 			PROC_UNLOCK(p);
296 			error = kern_clock_getcpuclockid2(td, pid,
297 			    CPUCLOCK_WHICH_PID, &nwhich);
298 			if (error != 0)
299 				return (EINVAL);
300 			error = kern_clock_gettime(td, nwhich, &tp);
301 			break;
302 		default:
303 			PROC_UNLOCK(p);
304 			return (EINVAL);
305 		}
306 
307 		break;
308 
309 	case CLOCK_THREAD_CPUTIME_ID:
310 		clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
311 		p = td->td_proc;
312 		tid = LINUX_CPUCLOCK_ID(args->which);
313 		if (tid == 0) {
314 			targettd = td;
315 			PROC_LOCK(p);
316 		} else {
317 			targettd = tdfind(tid, p->p_pid);
318 			if (targettd == NULL)
319 				return (EINVAL);
320 		}
321 		switch (clockwhich) {
322 		case LINUX_CPUCLOCK_PROF:
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 			timevaladd(&ru.ru_utime, &ru.ru_stime);
330 			TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
331 			break;
332 		case LINUX_CPUCLOCK_VIRT:
333 			PROC_STATLOCK(p);
334 			thread_lock(targettd);
335 			rufetchtd(targettd, &ru);
336 			thread_unlock(targettd);
337 			PROC_STATUNLOCK(p);
338 			PROC_UNLOCK(p);
339 			TIMEVAL_TO_TIMESPEC(&ru.ru_utime, &tp);
340 			break;
341 		case LINUX_CPUCLOCK_SCHED:
342 			error = kern_clock_getcpuclockid2(td, tid,
343 			    CPUCLOCK_WHICH_TID, &nwhich);
344 			PROC_UNLOCK(p);
345 			if (error != 0)
346 				return (EINVAL);
347 			error = kern_clock_gettime(td, nwhich, &tp);
348 			break;
349 		default:
350 			PROC_UNLOCK(p);
351 			return (EINVAL);
352 		}
353 		break;
354 
355 	default:
356 		error = kern_clock_gettime(td, nwhich, &tp);
357 		break;
358 	}
359 	if (error != 0) {
360 		LIN_SDT_PROBE1(time, linux_clock_gettime, gettime_error, error);
361 		LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
362 		return (error);
363 	}
364 	error = native_to_linux_timespec(&lts, &tp);
365 	if (error != 0)
366 		return (error);
367 	error = copyout(&lts, args->tp, sizeof lts);
368 	if (error != 0)
369 		LIN_SDT_PROBE1(time, linux_clock_gettime, copyout_error, error);
370 
371 	LIN_SDT_PROBE1(time, linux_clock_gettime, return, error);
372 	return (error);
373 }
374 
375 int
376 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
377 {
378 	struct timespec ts;
379 	struct l_timespec lts;
380 	int error;
381 	clockid_t nwhich = 0;	/* XXX: GCC */
382 
383 	LIN_SDT_PROBE2(time, linux_clock_settime, entry, args->which, args->tp);
384 
385 	error = linux_to_native_clockid(&nwhich, args->which);
386 	if (error != 0) {
387 		LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
388 		    error);
389 		LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
390 		return (error);
391 	}
392 	error = copyin(args->tp, &lts, sizeof lts);
393 	if (error != 0) {
394 		LIN_SDT_PROBE1(time, linux_clock_settime, copyin_error, error);
395 		LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
396 		return (error);
397 	}
398 	error = linux_to_native_timespec(&ts, &lts);
399 	if (error != 0) {
400 		LIN_SDT_PROBE1(time, linux_clock_settime, conversion_error,
401 		    error);
402 		LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
403 		return (error);
404 	}
405 
406 	error = kern_clock_settime(td, nwhich, &ts);
407 	if (error != 0)
408 		LIN_SDT_PROBE1(time, linux_clock_settime, settime_error, error);
409 
410 	LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
411 	return (error);
412 }
413 
414 int
415 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
416 {
417 	struct proc *p;
418 	struct timespec ts;
419 	struct l_timespec lts;
420 	int error, clockwhich;
421 	clockid_t nwhich = 0;	/* XXX: GCC */
422 	pid_t pid;
423 	lwpid_t tid;
424 
425 	LIN_SDT_PROBE2(time, linux_clock_getres, entry, args->which, args->tp);
426 
427 	error = linux_to_native_clockid(&nwhich, args->which);
428 	if (error != 0) {
429 		LIN_SDT_PROBE1(time, linux_clock_getres, conversion_error,
430 		    error);
431 		LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
432 		return (error);
433 	}
434 
435 	/*
436 	 * Check user supplied clock id in case of per-process
437 	 * or thread-specific cpu-time clock.
438 	 */
439 	switch (nwhich) {
440 	case CLOCK_THREAD_CPUTIME_ID:
441 		tid = LINUX_CPUCLOCK_ID(args->which);
442 		if (tid != 0) {
443 			p = td->td_proc;
444 			if (tdfind(tid, p->p_pid) == NULL)
445 				return (ESRCH);
446 			PROC_UNLOCK(p);
447 		}
448 		break;
449 	case CLOCK_PROCESS_CPUTIME_ID:
450 		pid = LINUX_CPUCLOCK_ID(args->which);
451 		if (pid != 0) {
452 			error = pget(pid, PGET_CANSEE, &p);
453 			if (error != 0)
454 				return (EINVAL);
455 			PROC_UNLOCK(p);
456 		}
457 		break;
458 	}
459 
460 	if (args->tp == NULL) {
461 		LIN_SDT_PROBE0(time, linux_clock_getres, nullcall);
462 		LIN_SDT_PROBE1(time, linux_clock_getres, return, 0);
463 	  	return (0);
464 	}
465 
466 	switch (nwhich) {
467 	case CLOCK_THREAD_CPUTIME_ID:
468 	case CLOCK_PROCESS_CPUTIME_ID:
469 		clockwhich = LINUX_CPUCLOCK_WHICH(args->which);
470 		switch (clockwhich) {
471 		case LINUX_CPUCLOCK_PROF:
472 			nwhich = CLOCK_PROF;
473 			break;
474 		case LINUX_CPUCLOCK_VIRT:
475 			nwhich = CLOCK_VIRTUAL;
476 			break;
477 		case LINUX_CPUCLOCK_SCHED:
478 			break;
479 		default:
480 			return (EINVAL);
481 		}
482 		break;
483 
484 	default:
485 		break;
486 	}
487 	error = kern_clock_getres(td, nwhich, &ts);
488 	if (error != 0) {
489 		LIN_SDT_PROBE1(time, linux_clock_getres, getres_error, error);
490 		LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
491 		return (error);
492 	}
493 	error = native_to_linux_timespec(&lts, &ts);
494 	if (error != 0)
495 		return (error);
496 	error = copyout(&lts, args->tp, sizeof lts);
497 	if (error != 0)
498 		LIN_SDT_PROBE1(time, linux_clock_getres, copyout_error, error);
499 
500 	LIN_SDT_PROBE1(time, linux_clock_getres, return, error);
501 	return (error);
502 }
503 
504 int
505 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
506 {
507 	struct timespec *rmtp;
508 	struct l_timespec lrqts, lrmts;
509 	struct timespec rqts, rmts;
510 	int error, error2;
511 
512 	LIN_SDT_PROBE2(time, linux_nanosleep, entry, args->rqtp, args->rmtp);
513 
514 	error = copyin(args->rqtp, &lrqts, sizeof lrqts);
515 	if (error != 0) {
516 		LIN_SDT_PROBE1(time, linux_nanosleep, copyin_error, error);
517 		LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
518 		return (error);
519 	}
520 
521 	if (args->rmtp != NULL)
522 		rmtp = &rmts;
523 	else
524 		rmtp = NULL;
525 
526 	error = linux_to_native_timespec(&rqts, &lrqts);
527 	if (error != 0) {
528 		LIN_SDT_PROBE1(time, linux_nanosleep, conversion_error, error);
529 		LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
530 		return (error);
531 	}
532 	error = kern_nanosleep(td, &rqts, rmtp);
533 	if (error == EINTR && args->rmtp != NULL) {
534 		error2 = native_to_linux_timespec(&lrmts, rmtp);
535 		if (error2 != 0)
536 			return (error2);
537 		error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
538 		if (error2 != 0) {
539 			LIN_SDT_PROBE1(time, linux_nanosleep, copyout_error,
540 			    error2);
541 			LIN_SDT_PROBE1(time, linux_nanosleep, return, error2);
542 			return (error2);
543 		}
544 	}
545 
546 	LIN_SDT_PROBE1(time, linux_nanosleep, return, error);
547 	return (error);
548 }
549 
550 int
551 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
552 {
553 	struct timespec *rmtp;
554 	struct l_timespec lrqts, lrmts;
555 	struct timespec rqts, rmts;
556 	int error, error2, flags;
557 	clockid_t clockid;
558 
559 	LIN_SDT_PROBE4(time, linux_clock_nanosleep, entry, args->which,
560 	    args->flags, args->rqtp, args->rmtp);
561 
562 	error = linux_to_native_timerflags(&flags, args->flags);
563 	if (error != 0) {
564 		LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_flags,
565 		    args->flags);
566 		LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
567 		return (error);
568 	}
569 
570 	error = linux_to_native_clockid(&clockid, args->which);
571 	if (error != 0) {
572 		LIN_SDT_PROBE1(time, linux_clock_nanosleep, unsupported_clockid,
573 		    args->which);
574 		LIN_SDT_PROBE1(time, linux_clock_settime, return, error);
575 		return (error);
576 	}
577 
578 	error = copyin(args->rqtp, &lrqts, sizeof(lrqts));
579 	if (error != 0) {
580 		LIN_SDT_PROBE1(time, linux_clock_nanosleep, copyin_error,
581 		    error);
582 		LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
583 		return (error);
584 	}
585 
586 	if (args->rmtp != NULL)
587 		rmtp = &rmts;
588 	else
589 		rmtp = NULL;
590 
591 	error = linux_to_native_timespec(&rqts, &lrqts);
592 	if (error != 0) {
593 		LIN_SDT_PROBE1(time, linux_clock_nanosleep, conversion_error,
594 		    error);
595 		LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
596 		return (error);
597 	}
598 	error = kern_clock_nanosleep(td, clockid, flags, &rqts, rmtp);
599 	if (error == EINTR && (flags & TIMER_ABSTIME) == 0 &&
600 	    args->rmtp != NULL) {
601 		error2 = native_to_linux_timespec(&lrmts, rmtp);
602 		if (error2 != 0)
603 			return (error2);
604 		error2 = copyout(&lrmts, args->rmtp, sizeof(lrmts));
605 		if (error2 != 0) {
606 			LIN_SDT_PROBE1(time, linux_clock_nanosleep,
607 			    copyout_error, error2);
608 			LIN_SDT_PROBE1(time, linux_clock_nanosleep,
609 			    return, error2);
610 			return (error2);
611 		}
612 	}
613 
614 	LIN_SDT_PROBE1(time, linux_clock_nanosleep, return, error);
615 	return (error);
616 }
617