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