Home | History | Annotate | Line # | Download | only in common
kern_time_50.c revision 1.7
      1 /*	$NetBSD: kern_time_50.c,v 1.7 2009/03/29 19:21:19 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: kern_time_50.c,v 1.7 2009/03/29 19:21:19 christos Exp $");
     40 
     41 #ifdef _KERNEL_OPT
     42 #include "opt_ntp.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/namei.h>
     48 #include <sys/filedesc.h>
     49 #include <sys/kernel.h>
     50 #include <sys/file.h>
     51 #include <sys/stat.h>
     52 #include <sys/socketvar.h>
     53 #include <sys/vnode.h>
     54 #include <sys/mount.h>
     55 #include <sys/proc.h>
     56 #include <sys/uio.h>
     57 #include <sys/dirent.h>
     58 #include <sys/malloc.h>
     59 #include <sys/kauth.h>
     60 #include <sys/time.h>
     61 #include <sys/timex.h>
     62 #include <sys/timetc.h>
     63 #include <sys/aio.h>
     64 #include <sys/poll.h>
     65 #include <sys/syscallargs.h>
     66 #include <sys/resource.h>
     67 
     68 #include <compat/common/compat_util.h>
     69 #include <compat/sys/time.h>
     70 #include <compat/sys/timex.h>
     71 #include <compat/sys/resource.h>
     72 #include <compat/sys/clockctl.h>
     73 
     74 static int
     75 compat_50_kevent_fetch_timeout(const void *src, void *dest, size_t length)
     76 {
     77 	struct timespec50 ts50;
     78 	int error;
     79 
     80 	KASSERT(length == sizeof(struct timespec));
     81 
     82 	error = copyin(src, &ts50, sizeof(ts50));
     83 	if (error)
     84 		return error;
     85 	timespec50_to_timespec(&ts50, (struct timespec *)dest);
     86 	return 0;
     87 }
     88 
     89 int
     90 compat_50_sys_kevent(struct lwp *l, const struct compat_50_sys_kevent_args *uap,
     91     register_t *retval)
     92 {
     93 	/* {
     94 		syscallarg(int) fd;
     95 		syscallarg(keventp_t) changelist;
     96 		syscallarg(size_t) nchanges;
     97 		syscallarg(keventp_t) eventlist;
     98 		syscallarg(size_t) nevents;
     99 		syscallarg(struct timespec50) timeout;
    100 	} */
    101 	static const struct kevent_ops compat_50_kevent_ops = {
    102 		.keo_private = NULL,
    103 		.keo_fetch_timeout = compat_50_kevent_fetch_timeout,
    104 		.keo_fetch_changes = kevent_fetch_changes,
    105 		.keo_put_events = kevent_put_events,
    106 	};
    107 
    108 	return kevent1(retval, SCARG(uap, fd), SCARG(uap, changelist),
    109 	    SCARG(uap, nchanges), SCARG(uap, eventlist), SCARG(uap, nevents),
    110 	    (const struct timespec *)(const void *)SCARG(uap, timeout),
    111 	    &compat_50_kevent_ops);
    112 }
    113 
    114 int
    115 compat_50_sys_clock_gettime(struct lwp *l,
    116     const struct compat_50_sys_clock_gettime_args *uap, register_t *retval)
    117 {
    118 	/* {
    119 		syscallarg(clockid_t) clock_id;
    120 		syscallarg(struct timespec50 *) tp;
    121 	} */
    122 	clockid_t clock_id;
    123 	struct timespec ats;
    124 	struct timespec50 ats50;
    125 
    126 	clock_id = SCARG(uap, clock_id);
    127 	switch (clock_id) {
    128 	case CLOCK_REALTIME:
    129 		nanotime(&ats);
    130 		break;
    131 	case CLOCK_MONOTONIC:
    132 		nanouptime(&ats);
    133 		break;
    134 	default:
    135 		return (EINVAL);
    136 	}
    137 	timespec_to_timespec50(&ats, &ats50);
    138 
    139 	return copyout(&ats50, SCARG(uap, tp), sizeof(ats50));
    140 }
    141 
    142 /* ARGSUSED */
    143 int
    144 compat_50_sys_clock_settime(struct lwp *l,
    145     const struct compat_50_sys_clock_settime_args *uap, register_t *retval)
    146 {
    147 	/* {
    148 		syscallarg(clockid_t) clock_id;
    149 		syscallarg(const struct timespec50 *) tp;
    150 	} */
    151 	int error;
    152 	struct timespec ats;
    153 	struct timespec50 ats50;
    154 
    155 	error = copyin(SCARG(uap, tp), &ats50, sizeof(ats50));
    156 	if (error)
    157 		return error;
    158 	timespec50_to_timespec(&ats50, &ats);
    159 
    160 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats,
    161 	    true);
    162 }
    163 
    164 
    165 int
    166 compat_50_sys_clock_getres(struct lwp *l,
    167     const struct compat_50_sys_clock_getres_args *uap, register_t *retval)
    168 {
    169 	/* {
    170 		syscallarg(clockid_t) clock_id;
    171 		syscallarg(struct timespec50 *) tp;
    172 	} */
    173 	clockid_t clock_id;
    174 	struct timespec50 ats50;
    175 	int error = 0;
    176 
    177 	clock_id = SCARG(uap, clock_id);
    178 	switch (clock_id) {
    179 	case CLOCK_REALTIME:
    180 	case CLOCK_MONOTONIC:
    181 		ats50.tv_sec = 0;
    182 		if (tc_getfrequency() > 1000000000)
    183 			ats50.tv_nsec = 1;
    184 		else
    185 			ats50.tv_nsec = 1000000000 / tc_getfrequency();
    186 		break;
    187 	default:
    188 		return (EINVAL);
    189 	}
    190 
    191 	if (SCARG(uap, tp))
    192 		error = copyout(&ats50, SCARG(uap, tp), sizeof(*SCARG(uap, tp)));
    193 
    194 	return error;
    195 }
    196 
    197 /* ARGSUSED */
    198 int
    199 compat_50_sys_nanosleep(struct lwp *l,
    200     const struct compat_50_sys_nanosleep_args *uap, register_t *retval)
    201 {
    202 	/* {
    203 		syscallarg(struct timespec50 *) rqtp;
    204 		syscallarg(struct timespec50 *) rmtp;
    205 	} */
    206 	struct timespec rmt, rqt;
    207 	struct timespec50 rmt50, rqt50;
    208 	int error, error1;
    209 
    210 	error = copyin(SCARG(uap, rqtp), &rqt50, sizeof(rqt50));
    211 	if (error)
    212 		return error;
    213 	timespec50_to_timespec(&rqt50, &rqt);
    214 
    215 	error = nanosleep1(l, &rqt, SCARG(uap, rmtp) ? &rmt : NULL);
    216 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    217 		return error;
    218 
    219 	timespec_to_timespec50(&rmt, &rmt50);
    220 	error1 = copyout(&rmt50, SCARG(uap, rmtp), sizeof(*SCARG(uap, rmtp)));
    221 	return error1 ? error1 : error;
    222 }
    223 
    224 /* ARGSUSED */
    225 int
    226 compat_50_sys_gettimeofday(struct lwp *l,
    227     const struct compat_50_sys_gettimeofday_args *uap, register_t *retval)
    228 {
    229 	/* {
    230 		syscallarg(struct timeval50 *) tp;
    231 		syscallarg(void *) tzp;		really "struct timezone *";
    232 	} */
    233 	struct timeval atv;
    234 	struct timeval50 atv50;
    235 	int error = 0;
    236 	struct timezone tzfake;
    237 
    238 	if (SCARG(uap, tp)) {
    239 		microtime(&atv);
    240 		timeval_to_timeval50(&atv, &atv50);
    241 		error = copyout(&atv50, SCARG(uap, tp), sizeof(*SCARG(uap, tp)));
    242 		if (error)
    243 			return error;
    244 	}
    245 	if (SCARG(uap, tzp)) {
    246 		/*
    247 		 * NetBSD has no kernel notion of time zone, so we just
    248 		 * fake up a timezone struct and return it if demanded.
    249 		 */
    250 		tzfake.tz_minuteswest = 0;
    251 		tzfake.tz_dsttime = 0;
    252 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    253 	}
    254 	return error;
    255 }
    256 
    257 /* ARGSUSED */
    258 int
    259 compat_50_sys_settimeofday(struct lwp *l,
    260     const struct compat_50_sys_settimeofday_args *uap, register_t *retval)
    261 {
    262 	/* {
    263 		syscallarg(const struct timeval50 *) tv;
    264 		syscallarg(const void *) tzp; really "const struct timezone *";
    265 	} */
    266 	struct timeval50 atv50;
    267 	struct timeval atv;
    268 	int error = copyin(SCARG(uap, tv), &atv50, sizeof(atv50));
    269 	if (error)
    270 		return error;
    271 	timeval50_to_timeval(&atv50, &atv);
    272 	return settimeofday1(&atv, false, SCARG(uap, tzp), l, true);
    273 }
    274 
    275 /* ARGSUSED */
    276 int
    277 compat_50_sys_adjtime(struct lwp *l,
    278     const struct compat_50_sys_adjtime_args *uap, register_t *retval)
    279 {
    280 	/* {
    281 		syscallarg(const struct timeval50 *) delta;
    282 		syscallarg(struct timeval50 *) olddelta;
    283 	} */
    284 	int error;
    285 	struct timeval50 delta50, olddelta50;
    286 	struct timeval delta, olddelta;
    287 
    288 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    289 	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
    290 		return error;
    291 
    292 	if (SCARG(uap, delta)) {
    293 		error = copyin(SCARG(uap, delta), &delta50,
    294 		    sizeof(*SCARG(uap, delta)));
    295 		if (error)
    296 			return (error);
    297 		timeval50_to_timeval(&delta50, &delta);
    298 	}
    299 	adjtime1(SCARG(uap, delta) ? &delta : NULL,
    300 	    SCARG(uap, olddelta) ? &olddelta : NULL, l->l_proc);
    301 	if (SCARG(uap, olddelta)) {
    302 		timeval_to_timeval50(&olddelta, &olddelta50);
    303 		error = copyout(&olddelta50, SCARG(uap, olddelta),
    304 		    sizeof(*SCARG(uap, olddelta)));
    305 	}
    306 	return error;
    307 }
    308 
    309 /* BSD routine to set/arm an interval timer. */
    310 /* ARGSUSED */
    311 int
    312 compat_50_sys_getitimer(struct lwp *l,
    313     const struct compat_50_sys_getitimer_args *uap, register_t *retval)
    314 {
    315 	/* {
    316 		syscallarg(int) which;
    317 		syscallarg(struct itimerval50 *) itv;
    318 	} */
    319 	struct proc *p = l->l_proc;
    320 	struct itimerval aitv;
    321 	struct itimerval50 aitv50;
    322 	int error;
    323 
    324 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    325 	if (error)
    326 		return error;
    327 	itimerval_to_itimerval50(&aitv, &aitv50);
    328 	return copyout(&aitv50, SCARG(uap, itv), sizeof(*SCARG(uap, itv)));
    329 }
    330 
    331 int
    332 compat_50_sys_setitimer(struct lwp *l,
    333     const struct compat_50_sys_setitimer_args *uap, register_t *retval)
    334 {
    335 	/* {
    336 		syscallarg(int) which;
    337 		syscallarg(const struct itimerval50 *) itv;
    338 		syscallarg(struct itimerval50 *) oitv;
    339 	} */
    340 	struct proc *p = l->l_proc;
    341 	int which = SCARG(uap, which);
    342 	struct compat_50_sys_getitimer_args getargs;
    343 	const struct itimerval50 *itvp;
    344 	struct itimerval50 aitv50;
    345 	struct itimerval aitv;
    346 	int error;
    347 
    348 	if ((u_int)which > ITIMER_PROF)
    349 		return (EINVAL);
    350 	itvp = SCARG(uap, itv);
    351 	if (itvp &&
    352 	    (error = copyin(itvp, &aitv50, sizeof(aitv50)) != 0))
    353 		return (error);
    354 	itimerval50_to_itimerval(&aitv50, &aitv);
    355 	if (SCARG(uap, oitv) != NULL) {
    356 		SCARG(&getargs, which) = which;
    357 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    358 		if ((error = compat_50_sys_getitimer(l, &getargs, retval)) != 0)
    359 			return (error);
    360 	}
    361 	if (itvp == 0)
    362 		return (0);
    363 
    364 	return dosetitimer(p, which, &aitv);
    365 }
    366 
    367 int
    368 compat_50_sys_aio_suspend(struct lwp *l,
    369     const struct compat_50_sys_aio_suspend_args *uap, register_t *retval)
    370 {
    371 	/* {
    372 		syscallarg(const struct aiocb *const[]) list;
    373 		syscallarg(int) nent;
    374 		syscallarg(const struct timespec50 *) timeout;
    375 	} */
    376 #ifdef notyet
    377 	struct aiocb **list;
    378 	struct timespec ts;
    379 	struct timespec50 ts50;
    380 	int error, nent;
    381 
    382 	nent = SCARG(uap, nent);
    383 	if (nent <= 0 || nent > aio_listio_max)
    384 		return EAGAIN;
    385 
    386 	if (SCARG(uap, timeout)) {
    387 		/* Convert timespec to ticks */
    388 		error = copyin(SCARG(uap, timeout), &ts50,
    389 		    sizeof(*SCARG(uap, timeout)));
    390 		if (error)
    391 			return error;
    392 		timespec50_to_timespec(&ts50, &ts);
    393 	}
    394 	list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
    395 	error = copyin(SCARG(uap, list), list, nent * sizeof(struct aiocb));
    396 	if (error)
    397 		goto out;
    398 	error = aio_suspend1(l, list, nent, SCARG(uap, timeout) ? &ts : NULL);
    399 out:
    400 	kmem_free(list, nent * sizeof(struct aio_job));
    401 	return error;
    402 #else
    403 	return ENOSYS;
    404 #endif
    405 }
    406 
    407 int
    408 compat_50_sys_select(struct lwp *l, const struct compat_50_sys_select_args *uap, register_t *retval)
    409 {
    410 	/* {
    411 		syscallarg(int)			nd;
    412 		syscallarg(fd_set *)		in;
    413 		syscallarg(fd_set *)		ou;
    414 		syscallarg(fd_set *)		ex;
    415 		syscallarg(struct timeval50 *)	tv;
    416 	} */
    417 	struct timespec ats, *ts = NULL;
    418 	struct timeval50 atv50;
    419 	int error;
    420 
    421 	if (SCARG(uap, tv)) {
    422 		error = copyin(SCARG(uap, tv), (void *)&atv50, sizeof(atv50));
    423 		if (error)
    424 			return error;
    425 		ats.tv_sec = atv50.tv_sec;
    426 		ats.tv_nsec = atv50.tv_usec * 1000;
    427 		ts = &ats;
    428 	}
    429 
    430 	return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
    431 	    SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
    432 }
    433 
    434 int
    435 compat_50_sys_pselect(struct lwp *l,
    436     const struct compat_50_sys_pselect_args *uap, register_t *retval)
    437 {
    438 	/* {
    439 		syscallarg(int)				nd;
    440 		syscallarg(fd_set *)			in;
    441 		syscallarg(fd_set *)			ou;
    442 		syscallarg(fd_set *)			ex;
    443 		syscallarg(const struct timespec50 *)	ts;
    444 		syscallarg(sigset_t *)			mask;
    445 	} */
    446 	struct timespec50	ats50;
    447 	struct timespec	ats, *ts = NULL;
    448 	sigset_t	amask, *mask = NULL;
    449 	int		error;
    450 
    451 	if (SCARG(uap, ts)) {
    452 		error = copyin(SCARG(uap, ts), &ats50, sizeof(ats50));
    453 		if (error)
    454 			return error;
    455 		timespec50_to_timespec(&ats50, &ats);
    456 		ts = &ats;
    457 	}
    458 	if (SCARG(uap, mask) != NULL) {
    459 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    460 		if (error)
    461 			return error;
    462 		mask = &amask;
    463 	}
    464 
    465 	return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
    466 	    SCARG(uap, ou), SCARG(uap, ex), ts, mask);
    467 }
    468 int
    469 compat_50_sys_pollts(struct lwp *l, const struct compat_50_sys_pollts_args *uap,
    470     register_t *retval)
    471 {
    472 	/* {
    473 		syscallarg(struct pollfd *)		fds;
    474 		syscallarg(u_int)			nfds;
    475 		syscallarg(const struct timespec50 *)	ts;
    476 		syscallarg(const sigset_t *)		mask;
    477 	} */
    478 	struct timespec	ats, *ts = NULL;
    479 	struct timespec50 ats50;
    480 	sigset_t	amask, *mask = NULL;
    481 	int		error;
    482 
    483 	if (SCARG(uap, ts)) {
    484 		error = copyin(SCARG(uap, ts), &ats50, sizeof(ats50));
    485 		if (error)
    486 			return error;
    487 		timespec50_to_timespec(&ats50, &ats);
    488 		ts = &ats;
    489 	}
    490 	if (SCARG(uap, mask)) {
    491 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    492 		if (error)
    493 			return error;
    494 		mask = &amask;
    495 	}
    496 
    497 	return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
    498 	    ts, mask);
    499 }
    500 
    501 int
    502 compat_50_sys__lwp_park(struct lwp *l,
    503     const struct compat_50_sys__lwp_park_args *uap, register_t *retval)
    504 {
    505 	/* {
    506 		syscallarg(const struct timespec50 *)	ts;
    507 		syscallarg(lwpid_t)			unpark;
    508 		syscallarg(const void *)		hint;
    509 		syscallarg(const void *)		unparkhint;
    510 	} */
    511 	struct timespec ts, *tsp;
    512 	struct timespec50 ts50;
    513 	int error;
    514 
    515 	if (SCARG(uap, ts) == NULL)
    516 		tsp = NULL;
    517 	else {
    518 		error = copyin(SCARG(uap, ts), &ts50, sizeof(ts50));
    519 		if (error != 0)
    520 			return error;
    521 		timespec50_to_timespec(&ts50, &ts);
    522 		tsp = &ts;
    523 	}
    524 
    525 	if (SCARG(uap, unpark) != 0) {
    526 		error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
    527 		if (error != 0)
    528 			return error;
    529 	}
    530 
    531 	return lwp_park(tsp, SCARG(uap, hint));
    532 }
    533 
    534 int
    535 compat_50_sys_mq_timedsend(struct lwp *l,
    536     const struct compat_50_sys_mq_timedsend_args *uap, register_t *retval)
    537 {
    538 	/* {
    539 		syscallarg(mqd_t) mqdes;
    540 		syscallarg(const char *) msg_ptr;
    541 		syscallarg(size_t) msg_len;
    542 		syscallarg(unsigned) msg_prio;
    543 		syscallarg(const struct timespec50 *) abs_timeout;
    544 	} */
    545 	int t;
    546 	int error;
    547 	struct timespec50 ts50;
    548 	struct timespec ts;
    549 
    550 	/* Get and convert time value */
    551 	if (SCARG(uap, abs_timeout)) {
    552 		error = copyin(SCARG(uap, abs_timeout), &ts50, sizeof(ts50));
    553 		if (error)
    554 			return error;
    555 		timespec50_to_timespec(&ts50, &ts);
    556 		error = abstimeout2timo(&ts, &t);
    557 		if (error)
    558 			return error;
    559 	} else
    560 		t = 0;
    561 
    562 	return mq_send1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    563 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), t);
    564 }
    565 
    566 int
    567 compat_50_sys_mq_timedreceive(struct lwp *l,
    568     const struct compat_50_sys_mq_timedreceive_args *uap, register_t *retval)
    569 {
    570 	/* {
    571 		syscallarg(mqd_t) mqdes;
    572 		syscallarg(char *) msg_ptr;
    573 		syscallarg(size_t) msg_len;
    574 		syscallarg(unsigned *) msg_prio;
    575 		syscallarg(const struct timespec50 *) abs_timeout;
    576 	} */
    577 	int error, t;
    578 	ssize_t mlen;
    579 	struct timespec ts;
    580 	struct timespec50 ts50;
    581 
    582 	/* Get and convert time value */
    583 	if (SCARG(uap, abs_timeout)) {
    584 		error = copyin(SCARG(uap, abs_timeout), &ts50, sizeof(ts50));
    585 		if (error)
    586 			return error;
    587 
    588 		timespec50_to_timespec(&ts50, &ts);
    589 		error = abstimeout2timo(&ts, &t);
    590 		if (error)
    591 			return error;
    592 	} else
    593 		t = 0;
    594 
    595 	error = mq_receive1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    596 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), t, &mlen);
    597 	if (error == 0)
    598 		*retval = mlen;
    599 
    600 	return error;
    601 }
    602 
    603 static int
    604 tscopyin(const void *u, void *s, size_t len)
    605 {
    606 	struct timespec50 ts50;
    607 	KASSERT(len == sizeof(ts50));
    608 	int error = copyin(u, &ts50, len);
    609 	if (error)
    610 		return error;
    611 	timespec50_to_timespec(&ts50, s);
    612 	return 0;
    613 }
    614 
    615 static int
    616 tscopyout(const void *s, void *u, size_t len)
    617 {
    618 	struct timespec50 ts50;
    619 	KASSERT(len == sizeof(ts50));
    620 	timespec_to_timespec50(s, &ts50);
    621 	int error = copyout(&ts50, u, len);
    622 	if (error)
    623 		return error;
    624 	return 0;
    625 }
    626 
    627 int
    628 compat_50_sys___sigtimedwait(struct lwp *l,
    629     const struct compat_50_sys___sigtimedwait_args *uap, register_t *retval)
    630 {
    631 
    632 	return __sigtimedwait1(l,
    633 	    (const struct sys_____sigtimedwait50_args *)uap, retval, copyout,
    634 	    tscopyin, tscopyout);
    635 }
    636 
    637 void
    638 rusage_to_rusage50(const struct rusage *ru, struct rusage50 *ru50)
    639 {
    640 	(void)memcpy(&ru50->ru_first, &ru->ru_first,
    641 	    (char *)&ru50->ru_last - (char *)&ru50->ru_first +
    642 	    sizeof(ru50->ru_last));
    643 	ru50->ru_maxrss = ru->ru_maxrss;
    644 	timeval_to_timeval50(&ru->ru_utime, &ru50->ru_utime);
    645 	timeval_to_timeval50(&ru->ru_stime, &ru50->ru_stime);
    646 }
    647 
    648 int
    649 compat_50_sys_getrusage(struct lwp *l,
    650     const struct compat_50_sys_getrusage_args *uap, register_t *retval)
    651 {
    652 	/* {
    653 		syscallarg(int) who;
    654 		syscallarg(struct rusage50 *) rusage;
    655 	} */
    656 	struct rusage ru;
    657 	struct rusage50 ru50;
    658 	struct proc *p = l->l_proc;
    659 
    660 	switch (SCARG(uap, who)) {
    661 	case RUSAGE_SELF:
    662 		mutex_enter(p->p_lock);
    663 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
    664 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
    665 		mutex_exit(p->p_lock);
    666 		break;
    667 
    668 	case RUSAGE_CHILDREN:
    669 		mutex_enter(p->p_lock);
    670 		memcpy(&ru, &p->p_stats->p_cru, sizeof(ru));
    671 		mutex_exit(p->p_lock);
    672 		break;
    673 
    674 	default:
    675 		return EINVAL;
    676 	}
    677 	rusage_to_rusage50(&ru, &ru50);
    678 	return copyout(&ru50, SCARG(uap, rusage), sizeof(ru50));
    679 }
    680 
    681 
    682 /* Return the time remaining until a POSIX timer fires. */
    683 int
    684 compat_50_sys_timer_gettime(struct lwp *l,
    685     const struct compat_50_sys_timer_gettime_args *uap, register_t *retval)
    686 {
    687 	/* {
    688 		syscallarg(timer_t) timerid;
    689 		syscallarg(struct itimerspec50 *) value;
    690 	} */
    691 	struct itimerspec its;
    692 	struct itimerspec50 its50;
    693 	int error;
    694 
    695 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    696 	    &its)) != 0)
    697 		return error;
    698 	itimerspec_to_itimerspec50(&its, &its50);
    699 
    700 	return copyout(&its50, SCARG(uap, value), sizeof(its50));
    701 }
    702 
    703 /* Set and arm a POSIX realtime timer */
    704 int
    705 compat_50_sys_timer_settime(struct lwp *l,
    706     const struct compat_50_sys_timer_settime_args *uap, register_t *retval)
    707 {
    708 	/* {
    709 		syscallarg(timer_t) timerid;
    710 		syscallarg(int) flags;
    711 		syscallarg(const struct itimerspec50 *) value;
    712 		syscallarg(struct itimerspec50 *) ovalue;
    713 	} */
    714 	int error;
    715 	struct itimerspec value, ovalue, *ovp = NULL;
    716 	struct itimerspec50 value50, ovalue50;
    717 
    718 	if ((error = copyin(SCARG(uap, value), &value50, sizeof(value50))) != 0)
    719 		return error;
    720 
    721 	itimerspec50_to_itimerspec(&value50, &value);
    722 	if (SCARG(uap, ovalue))
    723 		ovp = &ovalue;
    724 
    725 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    726 	    SCARG(uap, flags), l->l_proc)) != 0)
    727 		return error;
    728 
    729 	if (ovp) {
    730 		itimerspec_to_itimerspec50(&ovalue, &ovalue50);
    731 		return copyout(&ovalue50, SCARG(uap, ovalue), sizeof(ovalue50));
    732 	}
    733 	return 0;
    734 }
    735 
    736 /*
    737  * ntp_gettime() - NTP user application interface
    738  */
    739 int
    740 compat_50_sys___ntp_gettime30(struct lwp *l,
    741     const struct compat_50_sys___ntp_gettime30_args *uap, register_t *retval)
    742 {
    743 #ifdef NTP
    744 	/* {
    745 		syscallarg(struct ntptimeval *) ntvp;
    746 	} */
    747 	struct ntptimeval ntv;
    748 	struct ntptimeval50 ntv50;
    749 	int error;
    750 
    751 	if (SCARG(uap, ntvp)) {
    752 		ntp_gettime(&ntv);
    753 		timespec_to_timespec50(&ntv.time, &ntv50.time);
    754 		ntv50.maxerror = ntv.maxerror;
    755 		ntv50.esterror = ntv.esterror;
    756 		ntv50.tai = ntv.tai;
    757 		ntv50.time_state = ntv.time_state;
    758 
    759 		error = copyout(&ntv50, SCARG(uap, ntvp), sizeof(ntv50));
    760 		if (error)
    761 			return error;
    762 	}
    763 	*retval = ntp_timestatus();
    764 	return 0;
    765 #else
    766 	return ENOSYS;
    767 #endif
    768 }
    769 int
    770 compat50_clockctlioctl(dev_t dev, u_long cmd, void *data, int flags,
    771     struct lwp *l)
    772 {
    773 	int error = 0;
    774 
    775 	switch (cmd) {
    776 	case CLOCKCTL_OSETTIMEOFDAY: {
    777 		struct timeval50 tv50;
    778 		struct timeval tv;
    779 		struct clockctl50_settimeofday *args = data;
    780 
    781 		error = copyin(args->tv, &tv50, sizeof(tv50));
    782 		if (error)
    783 			return (error);
    784 		timeval50_to_timeval(&tv50, &tv);
    785 		error = settimeofday1(&tv, false, args->tzp, l, false);
    786 		break;
    787 	}
    788 	case CLOCKCTL_OADJTIME: {
    789 		struct timeval atv, oldatv;
    790 		struct timeval50 atv50;
    791 		struct clockctl50_adjtime *args = data;
    792 
    793 		if (args->delta) {
    794 			error = copyin(args->delta, &atv50, sizeof(atv50));
    795 			if (error)
    796 				return (error);
    797 			timeval50_to_timeval(&atv50, &atv);
    798 		}
    799 		adjtime1(args->delta ? &atv : NULL,
    800 		    args->olddelta ? &oldatv : NULL, l->l_proc);
    801 		if (args->olddelta) {
    802 			timeval_to_timeval50(&oldatv, &atv50);
    803 			error = copyout(&atv50, args->olddelta, sizeof(atv50));
    804 		}
    805 		break;
    806 	}
    807 	case CLOCKCTL_OCLOCK_SETTIME: {
    808 		struct timespec50 tp50;
    809 		struct timespec tp;
    810 		struct clockctl50_clock_settime *args = data;
    811 
    812 		error = copyin(args->tp, &tp50, sizeof(tp50));
    813 		if (error)
    814 			return (error);
    815 		timespec50_to_timespec(&tp50, &tp);
    816 		error = clock_settime1(l->l_proc, args->clock_id, &tp, true);
    817 		break;
    818 	}
    819 	default:
    820 		error = EINVAL;
    821 	}
    822 
    823 	return (error);
    824 }
    825 int
    826 compat_50_sys_wait4(struct lwp *l, const struct compat_50_sys_wait4_args *uap, register_t *retval)
    827 {
    828 	/* {
    829 		syscallarg(int)			pid;
    830 		syscallarg(int *)		status;
    831 		syscallarg(int)			options;
    832 		syscallarg(struct rusage50 *)	rusage;
    833 	} */
    834 	int		status, error;
    835 	int		was_zombie;
    836 	struct rusage	ru;
    837 	struct rusage50	ru50;
    838 	int pid = SCARG(uap, pid);
    839 
    840 	error = do_sys_wait(l, &pid, &status, SCARG(uap, options),
    841 	    SCARG(uap, rusage) != NULL ? &ru : NULL, &was_zombie);
    842 
    843 	retval[0] = pid;
    844 	if (pid == 0)
    845 		return error;
    846 
    847 	if (SCARG(uap, rusage)) {
    848 		rusage_to_rusage50(&ru, &ru50);
    849 		error = copyout(&ru50, SCARG(uap, rusage), sizeof(ru50));
    850 	}
    851 
    852 	if (error == 0 && SCARG(uap, status))
    853 		error = copyout(&status, SCARG(uap, status), sizeof(status));
    854 
    855 	return error;
    856 }
    857