Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_time.c revision 1.25.6.2
      1  1.25.6.2   reinoud /*	$NetBSD: netbsd32_time.c,v 1.25.6.2 2007/03/29 19:27:42 reinoud Exp $	*/
      2       1.1       mrg 
      3       1.1       mrg /*
      4       1.1       mrg  * Copyright (c) 1998, 2001 Matthew R. Green
      5       1.1       mrg  * All rights reserved.
      6       1.1       mrg  *
      7       1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8       1.1       mrg  * modification, are permitted provided that the following conditions
      9       1.1       mrg  * are met:
     10       1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11       1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12       1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15       1.1       mrg  * 3. The name of the author may not be used to endorse or promote products
     16       1.1       mrg  *    derived from this software without specific prior written permission.
     17       1.1       mrg  *
     18       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19       1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20       1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21       1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22       1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23       1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24       1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25       1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26       1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27       1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28       1.1       mrg  * SUCH DAMAGE.
     29       1.1       mrg  */
     30       1.3     lukem 
     31       1.3     lukem #include <sys/cdefs.h>
     32  1.25.6.2   reinoud __KERNEL_RCSID(0, "$NetBSD: netbsd32_time.c,v 1.25.6.2 2007/03/29 19:27:42 reinoud Exp $");
     33       1.1       mrg 
     34       1.2       mrg #if defined(_KERNEL_OPT)
     35       1.1       mrg #include "opt_ntp.h"
     36      1.21  drochner #include "opt_compat_netbsd.h"
     37       1.1       mrg #endif
     38       1.1       mrg 
     39       1.1       mrg #include <sys/param.h>
     40       1.1       mrg #include <sys/systm.h>
     41       1.1       mrg #include <sys/mount.h>
     42       1.1       mrg #include <sys/time.h>
     43       1.1       mrg #include <sys/timex.h>
     44      1.13      cube #include <sys/timevar.h>
     45      1.22    kardel #include <sys/timetc.h>
     46       1.1       mrg #include <sys/proc.h>
     47       1.5   thorpej #include <sys/pool.h>
     48       1.1       mrg #include <sys/resourcevar.h>
     49      1.12  christos #include <sys/dirent.h>
     50      1.20      elad #include <sys/kauth.h>
     51       1.1       mrg 
     52       1.1       mrg #include <compat/netbsd32/netbsd32.h>
     53       1.1       mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     54       1.1       mrg #include <compat/netbsd32/netbsd32_conv.h>
     55       1.1       mrg 
     56       1.1       mrg #ifdef NTP
     57      1.19        he 
     58       1.1       mrg int
     59       1.5   thorpej netbsd32_ntp_gettime(l, v, retval)
     60       1.5   thorpej 	struct lwp *l;
     61       1.1       mrg 	void *v;
     62       1.1       mrg 	register_t *retval;
     63       1.1       mrg {
     64       1.1       mrg 	struct netbsd32_ntp_gettime_args /* {
     65       1.1       mrg 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
     66       1.1       mrg 	} */ *uap = v;
     67       1.1       mrg 	struct netbsd32_ntptimeval ntv32;
     68       1.1       mrg 	struct ntptimeval ntv;
     69       1.1       mrg 	int error = 0;
     70       1.1       mrg 
     71  1.25.6.2   reinoud 	if (SCARG_P32(uap, ntvp)) {
     72      1.21  drochner 		ntp_gettime(&ntv);
     73       1.1       mrg 
     74      1.21  drochner 		ntv32.time.tv_sec = ntv.time.tv_sec;
     75      1.21  drochner 		ntv32.time.tv_nsec = ntv.time.tv_nsec;
     76       1.1       mrg 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
     77       1.1       mrg 		ntv32.esterror = (netbsd32_long)ntv.esterror;
     78      1.21  drochner 		ntv32.tai = (netbsd32_long)ntv.tai;
     79      1.21  drochner 		ntv32.time_state = ntv.time_state;
     80  1.25.6.2   reinoud 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
     81       1.1       mrg 	}
     82       1.1       mrg 	if (!error) {
     83      1.21  drochner 		*retval = ntp_timestatus();
     84      1.21  drochner 	}
     85       1.1       mrg 
     86      1.21  drochner 	return (error);
     87      1.21  drochner }
     88       1.1       mrg 
     89      1.21  drochner #ifdef COMPAT_30
     90      1.21  drochner int
     91      1.21  drochner compat_30_netbsd32_ntp_gettime(l, v, retval)
     92      1.21  drochner 	struct lwp *l;
     93      1.21  drochner 	void *v;
     94      1.21  drochner 	register_t *retval;
     95      1.21  drochner {
     96      1.21  drochner 	struct compat_30_netbsd32_ntp_gettime_args /* {
     97      1.21  drochner 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
     98      1.21  drochner 	} */ *uap = v;
     99      1.21  drochner 	struct netbsd32_ntptimeval30 ntv32;
    100      1.21  drochner 	struct ntptimeval ntv;
    101      1.21  drochner 	int error = 0;
    102       1.1       mrg 
    103  1.25.6.2   reinoud 	if (SCARG_P32(uap, ntvp)) {
    104      1.21  drochner 		ntp_gettime(&ntv);
    105       1.1       mrg 
    106      1.21  drochner 		ntv32.time.tv_sec = ntv.time.tv_sec;
    107      1.21  drochner 		ntv32.time.tv_usec = ntv.time.tv_nsec / 1000;
    108      1.21  drochner 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
    109      1.21  drochner 		ntv32.esterror = (netbsd32_long)ntv.esterror;
    110  1.25.6.2   reinoud 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
    111      1.21  drochner 	}
    112      1.21  drochner 	if (!error) {
    113      1.21  drochner 		*retval = ntp_timestatus();
    114       1.1       mrg 	}
    115      1.21  drochner 
    116       1.1       mrg 	return (error);
    117       1.1       mrg }
    118      1.21  drochner #endif
    119       1.1       mrg 
    120       1.1       mrg int
    121       1.5   thorpej netbsd32_ntp_adjtime(l, v, retval)
    122       1.5   thorpej 	struct lwp *l;
    123       1.1       mrg 	void *v;
    124       1.1       mrg 	register_t *retval;
    125       1.1       mrg {
    126       1.1       mrg 	struct netbsd32_ntp_adjtime_args /* {
    127       1.1       mrg 		syscallarg(netbsd32_timexp_t) tp;
    128       1.1       mrg 	} */ *uap = v;
    129       1.1       mrg 	struct netbsd32_timex ntv32;
    130       1.1       mrg 	struct timex ntv;
    131       1.1       mrg 	int error = 0;
    132       1.1       mrg 	int modes;
    133       1.1       mrg 
    134  1.25.6.2   reinoud 	if ((error = copyin(SCARG_P32(uap, tp), &ntv32, sizeof(ntv32))))
    135       1.1       mrg 		return (error);
    136      1.22    kardel 
    137       1.1       mrg 	netbsd32_to_timex(&ntv32, &ntv);
    138       1.1       mrg 
    139       1.1       mrg 	/*
    140       1.1       mrg 	 * Update selected clock variables - only the superuser can
    141       1.1       mrg 	 * change anything. Note that there is no error checking here on
    142       1.1       mrg 	 * the assumption the superuser should know what it is doing.
    143       1.1       mrg 	 */
    144       1.1       mrg 	modes = ntv.modes;
    145      1.24      elad 	if (modes != 0 && (error = kauth_authorize_system(l->l_cred,
    146      1.24      elad 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL, NULL,
    147      1.24      elad 	    NULL)))
    148       1.1       mrg 		return (error);
    149       1.1       mrg 
    150      1.22    kardel 	ntp_adjtime1(&ntv);
    151       1.1       mrg 
    152       1.1       mrg 	netbsd32_from_timex(&ntv, &ntv32);
    153  1.25.6.2   reinoud 	error = copyout(&ntv32, SCARG_P32(uap, tp), sizeof(ntv32));
    154       1.1       mrg 	if (!error) {
    155      1.22    kardel 		*retval = ntp_timestatus();
    156       1.1       mrg 	}
    157       1.1       mrg 	return error;
    158       1.1       mrg }
    159      1.21  drochner #else /* !NTP */
    160       1.1       mrg int
    161       1.5   thorpej netbsd32_ntp_gettime(l, v, retval)
    162       1.5   thorpej 	struct lwp *l;
    163       1.1       mrg 	void *v;
    164       1.1       mrg 	register_t *retval;
    165       1.1       mrg {
    166       1.1       mrg 
    167       1.1       mrg 	return (ENOSYS);
    168       1.1       mrg }
    169       1.1       mrg 
    170      1.21  drochner #ifdef COMPAT_30
    171      1.21  drochner int
    172      1.21  drochner compat_30_netbsd32_ntp_gettime(l, v, retval)
    173      1.21  drochner 	struct lwp *l;
    174      1.21  drochner 	void *v;
    175      1.21  drochner 	register_t *retval;
    176      1.21  drochner {
    177      1.21  drochner 
    178      1.21  drochner 	return (ENOSYS);
    179      1.21  drochner }
    180      1.21  drochner #endif
    181      1.21  drochner 
    182       1.1       mrg int
    183       1.5   thorpej netbsd32_ntp_adjtime(l, v, retval)
    184       1.5   thorpej 	struct lwp *l;
    185       1.1       mrg 	void *v;
    186       1.1       mrg 	register_t *retval;
    187       1.1       mrg {
    188       1.1       mrg 
    189       1.1       mrg 	return (ENOSYS);
    190       1.1       mrg }
    191      1.21  drochner #endif /* NTP */
    192       1.1       mrg 
    193       1.1       mrg int
    194       1.5   thorpej netbsd32_setitimer(l, v, retval)
    195       1.5   thorpej 	struct lwp *l;
    196       1.1       mrg 	void *v;
    197       1.1       mrg 	register_t *retval;
    198       1.1       mrg {
    199       1.1       mrg 	struct netbsd32_setitimer_args /* {
    200       1.1       mrg 		syscallarg(int) which;
    201       1.1       mrg 		syscallarg(const netbsd32_itimervalp_t) itv;
    202       1.1       mrg 		syscallarg(netbsd32_itimervalp_t) oitv;
    203       1.1       mrg 	} */ *uap = v;
    204       1.5   thorpej 	struct proc *p = l->l_proc;
    205      1.10      cube 	struct netbsd32_itimerval s32it, *itv32;
    206       1.1       mrg 	int which = SCARG(uap, which);
    207       1.1       mrg 	struct netbsd32_getitimer_args getargs;
    208       1.1       mrg 	struct itimerval aitv;
    209      1.10      cube 	int error;
    210       1.1       mrg 
    211       1.1       mrg 	if ((u_int)which > ITIMER_PROF)
    212       1.1       mrg 		return (EINVAL);
    213  1.25.6.2   reinoud 	itv32 = SCARG_P32(uap, itv);
    214      1.10      cube 	if (itv32) {
    215      1.10      cube 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
    216      1.10      cube 			return (error);
    217      1.10      cube 		netbsd32_to_itimerval(&s32it, &aitv);
    218      1.10      cube 	}
    219  1.25.6.1   reinoud 	if (SCARG_P32(uap, oitv) != 0) {
    220       1.1       mrg 		SCARG(&getargs, which) = which;
    221       1.1       mrg 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    222       1.5   thorpej 		if ((error = netbsd32_getitimer(l, &getargs, retval)) != 0)
    223       1.1       mrg 			return (error);
    224       1.1       mrg 	}
    225      1.10      cube 	if (itv32 == 0)
    226      1.10      cube 		return 0;
    227       1.5   thorpej 
    228      1.10      cube 	return dosetitimer(p, which, &aitv);
    229       1.1       mrg }
    230       1.1       mrg 
    231       1.1       mrg int
    232       1.5   thorpej netbsd32_getitimer(l, v, retval)
    233       1.5   thorpej 	struct lwp *l;
    234       1.1       mrg 	void *v;
    235       1.1       mrg 	register_t *retval;
    236       1.1       mrg {
    237       1.1       mrg 	struct netbsd32_getitimer_args /* {
    238       1.1       mrg 		syscallarg(int) which;
    239       1.1       mrg 		syscallarg(netbsd32_itimervalp_t) itv;
    240       1.1       mrg 	} */ *uap = v;
    241       1.5   thorpej 	struct proc *p = l->l_proc;
    242       1.1       mrg 	struct netbsd32_itimerval s32it;
    243       1.1       mrg 	struct itimerval aitv;
    244      1.10      cube 	int error;
    245       1.1       mrg 
    246      1.10      cube 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    247      1.10      cube 	if (error)
    248      1.10      cube 		return error;
    249       1.5   thorpej 
    250       1.1       mrg 	netbsd32_from_itimerval(&aitv, &s32it);
    251  1.25.6.2   reinoud 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
    252       1.1       mrg }
    253       1.1       mrg 
    254       1.1       mrg int
    255       1.5   thorpej netbsd32_gettimeofday(l, v, retval)
    256       1.5   thorpej 	struct lwp *l;
    257       1.1       mrg 	void *v;
    258       1.1       mrg 	register_t *retval;
    259       1.1       mrg {
    260       1.1       mrg 	struct netbsd32_gettimeofday_args /* {
    261       1.1       mrg 		syscallarg(netbsd32_timevalp_t) tp;
    262       1.1       mrg 		syscallarg(netbsd32_timezonep_t) tzp;
    263       1.1       mrg 	} */ *uap = v;
    264       1.1       mrg 	struct timeval atv;
    265       1.1       mrg 	struct netbsd32_timeval tv32;
    266       1.1       mrg 	int error = 0;
    267       1.1       mrg 	struct netbsd32_timezone tzfake;
    268       1.1       mrg 
    269  1.25.6.1   reinoud 	if (SCARG_P32(uap, tp)) {
    270       1.1       mrg 		microtime(&atv);
    271       1.1       mrg 		netbsd32_from_timeval(&atv, &tv32);
    272  1.25.6.2   reinoud 		error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
    273       1.1       mrg 		if (error)
    274       1.1       mrg 			return (error);
    275       1.1       mrg 	}
    276  1.25.6.1   reinoud 	if (SCARG_P32(uap, tzp)) {
    277       1.1       mrg 		/*
    278       1.1       mrg 		 * NetBSD has no kernel notion of time zone, so we just
    279       1.1       mrg 		 * fake up a timezone struct and return it if demanded.
    280       1.1       mrg 		 */
    281       1.1       mrg 		tzfake.tz_minuteswest = 0;
    282       1.1       mrg 		tzfake.tz_dsttime = 0;
    283  1.25.6.2   reinoud 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
    284       1.1       mrg 	}
    285       1.1       mrg 	return (error);
    286       1.1       mrg }
    287       1.1       mrg 
    288       1.1       mrg int
    289       1.5   thorpej netbsd32_settimeofday(l, v, retval)
    290       1.5   thorpej 	struct lwp *l;
    291       1.1       mrg 	void *v;
    292       1.1       mrg 	register_t *retval;
    293       1.1       mrg {
    294       1.1       mrg 	struct netbsd32_settimeofday_args /* {
    295       1.1       mrg 		syscallarg(const netbsd32_timevalp_t) tv;
    296       1.1       mrg 		syscallarg(const netbsd32_timezonep_t) tzp;
    297       1.1       mrg 	} */ *uap = v;
    298       1.1       mrg 	struct netbsd32_timeval atv32;
    299       1.1       mrg 	struct timeval atv;
    300      1.16  christos 	struct timespec ats;
    301       1.1       mrg 	int error;
    302       1.5   thorpej 	struct proc *p = l->l_proc;
    303       1.1       mrg 
    304      1.16  christos 	/* Verify all parameters before changing time. */
    305      1.24      elad 	if ((error = kauth_authorize_system(l->l_cred,
    306      1.24      elad 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, NULL, NULL,
    307      1.24      elad 	    NULL)) != 0)
    308      1.16  christos 		return error;
    309      1.16  christos 
    310       1.1       mrg 	/*
    311       1.1       mrg 	 * NetBSD has no kernel notion of time zone, and only an
    312       1.1       mrg 	 * obsolete program would try to set it, so we log a warning.
    313       1.1       mrg 	 */
    314  1.25.6.1   reinoud 	if (SCARG_P32(uap, tzp))
    315       1.1       mrg 		printf("pid %d attempted to set the "
    316       1.8     perry 		    "(obsolete) kernel time zone\n", p->p_pid);
    317      1.16  christos 
    318  1.25.6.1   reinoud 	if (SCARG_P32(uap, tv) == 0)
    319      1.16  christos 		return 0;
    320      1.16  christos 
    321  1.25.6.2   reinoud 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
    322      1.16  christos 		return error;
    323      1.16  christos 
    324      1.16  christos 	netbsd32_to_timeval(&atv32, &atv);
    325      1.16  christos 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
    326      1.16  christos 	return settime(p, &ats);
    327       1.1       mrg }
    328       1.1       mrg 
    329       1.1       mrg int
    330       1.5   thorpej netbsd32_adjtime(l, v, retval)
    331       1.5   thorpej 	struct lwp *l;
    332       1.1       mrg 	void *v;
    333       1.1       mrg 	register_t *retval;
    334       1.1       mrg {
    335       1.1       mrg 	struct netbsd32_adjtime_args /* {
    336       1.1       mrg 		syscallarg(const netbsd32_timevalp_t) delta;
    337       1.1       mrg 		syscallarg(netbsd32_timevalp_t) olddelta;
    338       1.1       mrg 	} */ *uap = v;
    339       1.1       mrg 	struct netbsd32_timeval atv;
    340      1.22    kardel 	int error;
    341       1.1       mrg 
    342      1.24      elad 	if ((error = kauth_authorize_system(l->l_cred,
    343      1.24      elad 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
    344      1.24      elad 	    NULL)) != 0)
    345       1.1       mrg 		return (error);
    346      1.23        ad 
    347      1.22    kardel #ifdef __HAVE_TIMECOUNTER
    348      1.22    kardel 	{
    349      1.22    kardel 		extern int time_adjusted;     /* in kern_ntptime.c */
    350      1.22    kardel 		extern int64_t time_adjtime;  /* in kern_ntptime.c */
    351  1.25.6.1   reinoud 		if (SCARG_P32(uap, olddelta)) {
    352      1.22    kardel 			atv.tv_sec = time_adjtime / 1000000;
    353      1.22    kardel 			atv.tv_usec = time_adjtime % 1000000;
    354      1.22    kardel 			if (atv.tv_usec < 0) {
    355      1.22    kardel 				atv.tv_usec += 1000000;
    356      1.22    kardel 				atv.tv_sec--;
    357      1.22    kardel 			}
    358      1.22    kardel 			(void) copyout(&atv,
    359  1.25.6.2   reinoud 				       SCARG_P32(uap, olddelta),
    360      1.22    kardel 				       sizeof(atv));
    361      1.22    kardel 			if (error)
    362      1.22    kardel 				return (error);
    363      1.22    kardel 		}
    364      1.22    kardel 
    365  1.25.6.1   reinoud 		if (SCARG_P32(uap, delta)) {
    366  1.25.6.2   reinoud 			error = copyin(SCARG_P32(uap, delta), &atv,
    367      1.22    kardel 				       sizeof(struct timeval));
    368      1.22    kardel 			if (error)
    369      1.22    kardel 				return (error);
    370      1.22    kardel 
    371      1.22    kardel 			time_adjtime = (int64_t)atv.tv_sec * 1000000 +
    372      1.22    kardel 				atv.tv_usec;
    373      1.22    kardel 
    374      1.22    kardel 			if (time_adjtime)
    375      1.22    kardel 				/* We need to save the system time during shutdown */
    376      1.22    kardel 				time_adjusted |= 1;
    377      1.22    kardel 		}
    378      1.22    kardel 	}
    379      1.22    kardel #else /* !__HAVE_TIMECOUNTER */
    380      1.22    kardel 	{
    381      1.22    kardel 		int32_t ndelta, ntickdelta, odelta;
    382      1.22    kardel 		extern long bigadj, timedelta;
    383      1.22    kardel 		extern int tickdelta;
    384      1.22    kardel 		int s;
    385  1.25.6.2   reinoud 		error = copyin(SCARG_P32(uap, delta), &atv,
    386      1.22    kardel 			       sizeof(struct timeval));
    387      1.22    kardel 		if (error)
    388      1.22    kardel 			return (error);
    389      1.22    kardel 		/*
    390      1.22    kardel 		 * Compute the total correction and the rate at which to apply it.
    391      1.22    kardel 		 * Round the adjustment down to a whole multiple of the per-tick
    392      1.22    kardel 		 * delta, so that after some number of incremental changes in
    393      1.22    kardel 		 * hardclock(), tickdelta will become zero, lest the correction
    394      1.22    kardel 		 * overshoot and start taking us away from the desired final time.
    395      1.22    kardel 		 */
    396      1.22    kardel 		ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
    397      1.22    kardel 		if (ndelta > bigadj)
    398      1.22    kardel 			ntickdelta = 10 * tickadj;
    399      1.22    kardel 		else
    400      1.22    kardel 			ntickdelta = tickadj;
    401      1.22    kardel 		if (ndelta % ntickdelta)
    402      1.22    kardel 			ndelta = ndelta / ntickdelta * ntickdelta;
    403       1.1       mrg 
    404      1.22    kardel 		/*
    405      1.22    kardel 		 * To make hardclock()'s job easier, make the per-tick delta negative
    406      1.22    kardel 		 * if we want time to run slower; then hardclock can simply compute
    407      1.22    kardel 		 * tick + tickdelta, and subtract tickdelta from timedelta.
    408      1.22    kardel 		 */
    409      1.22    kardel 		if (ndelta < 0)
    410      1.22    kardel 			ntickdelta = -ntickdelta;
    411      1.22    kardel 		s = splclock();
    412      1.22    kardel 		odelta = timedelta;
    413      1.22    kardel 		timedelta = ndelta;
    414      1.22    kardel 		tickdelta = ntickdelta;
    415      1.22    kardel 		splx(s);
    416       1.1       mrg 
    417  1.25.6.1   reinoud 		if (SCARG_P32(uap, olddelta)) {
    418      1.22    kardel 			atv.tv_sec = odelta / 1000000;
    419      1.22    kardel 			atv.tv_usec = odelta % 1000000;
    420      1.22    kardel 			(void) copyout(&atv,
    421  1.25.6.2   reinoud 				       SCARG_P32(uap, olddelta), sizeof(atv));
    422      1.22    kardel 		}
    423       1.1       mrg 	}
    424      1.22    kardel #endif /* !__HAVE_TIMECOUNTER */
    425       1.1       mrg 	return (0);
    426       1.1       mrg }
    427       1.1       mrg 
    428       1.1       mrg int
    429       1.5   thorpej netbsd32_clock_gettime(l, v, retval)
    430       1.5   thorpej 	struct lwp *l;
    431       1.1       mrg 	void *v;
    432       1.1       mrg 	register_t *retval;
    433       1.1       mrg {
    434       1.1       mrg 	struct netbsd32_clock_gettime_args /* {
    435       1.1       mrg 		syscallarg(netbsd32_clockid_t) clock_id;
    436       1.1       mrg 		syscallarg(netbsd32_timespecp_t) tp;
    437       1.1       mrg 	} */ *uap = v;
    438       1.1       mrg 	clockid_t clock_id;
    439       1.1       mrg 	struct timespec ats;
    440       1.1       mrg 	struct netbsd32_timespec ts32;
    441       1.1       mrg 
    442       1.1       mrg 	clock_id = SCARG(uap, clock_id);
    443       1.1       mrg 	if (clock_id != CLOCK_REALTIME)
    444       1.1       mrg 		return (EINVAL);
    445       1.1       mrg 
    446      1.14    simonb 	nanotime(&ats);
    447       1.1       mrg 	netbsd32_from_timespec(&ats, &ts32);
    448       1.1       mrg 
    449  1.25.6.2   reinoud 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    450       1.1       mrg }
    451       1.1       mrg 
    452       1.1       mrg int
    453       1.5   thorpej netbsd32_clock_settime(l, v, retval)
    454       1.5   thorpej 	struct lwp *l;
    455       1.1       mrg 	void *v;
    456       1.1       mrg 	register_t *retval;
    457       1.1       mrg {
    458       1.1       mrg 	struct netbsd32_clock_settime_args /* {
    459       1.1       mrg 		syscallarg(netbsd32_clockid_t) clock_id;
    460       1.1       mrg 		syscallarg(const netbsd32_timespecp_t) tp;
    461       1.1       mrg 	} */ *uap = v;
    462       1.1       mrg 	struct netbsd32_timespec ts32;
    463       1.1       mrg 	clockid_t clock_id;
    464       1.1       mrg 	struct timespec ats;
    465       1.1       mrg 	int error;
    466       1.1       mrg 
    467      1.24      elad 	if ((error = kauth_authorize_system(l->l_cred,
    468      1.24      elad 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, NULL, NULL,
    469      1.24      elad 	    NULL)) != 0)
    470       1.1       mrg 		return (error);
    471       1.1       mrg 
    472       1.1       mrg 	clock_id = SCARG(uap, clock_id);
    473       1.1       mrg 	if (clock_id != CLOCK_REALTIME)
    474       1.1       mrg 		return (EINVAL);
    475       1.1       mrg 
    476  1.25.6.2   reinoud 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
    477       1.1       mrg 		return (error);
    478       1.1       mrg 
    479       1.1       mrg 	netbsd32_to_timespec(&ts32, &ats);
    480      1.23        ad 	return settime(l->l_proc, &ats);
    481       1.1       mrg }
    482       1.1       mrg 
    483       1.1       mrg int
    484       1.5   thorpej netbsd32_clock_getres(l, v, retval)
    485       1.5   thorpej 	struct lwp *l;
    486       1.1       mrg 	void *v;
    487       1.1       mrg 	register_t *retval;
    488       1.1       mrg {
    489       1.1       mrg 	struct netbsd32_clock_getres_args /* {
    490       1.1       mrg 		syscallarg(netbsd32_clockid_t) clock_id;
    491       1.1       mrg 		syscallarg(netbsd32_timespecp_t) tp;
    492       1.1       mrg 	} */ *uap = v;
    493       1.1       mrg 	struct netbsd32_timespec ts32;
    494       1.1       mrg 	clockid_t clock_id;
    495       1.1       mrg 	struct timespec ts;
    496       1.1       mrg 	int error = 0;
    497       1.1       mrg 
    498       1.1       mrg 	clock_id = SCARG(uap, clock_id);
    499       1.1       mrg 	if (clock_id != CLOCK_REALTIME)
    500       1.1       mrg 		return (EINVAL);
    501       1.1       mrg 
    502  1.25.6.1   reinoud 	if (SCARG_P32(uap, tp)) {
    503       1.1       mrg 		ts.tv_sec = 0;
    504       1.1       mrg 		ts.tv_nsec = 1000000000 / hz;
    505       1.1       mrg 
    506       1.1       mrg 		netbsd32_from_timespec(&ts, &ts32);
    507  1.25.6.2   reinoud 		error = copyout(&ts, SCARG_P32(uap, tp), sizeof(ts));
    508       1.1       mrg 	}
    509       1.1       mrg 
    510       1.1       mrg 	return error;
    511       1.1       mrg }
    512       1.1       mrg 
    513       1.1       mrg int
    514       1.5   thorpej netbsd32_nanosleep(l, v, retval)
    515       1.5   thorpej 	struct lwp *l;
    516       1.1       mrg 	void *v;
    517       1.1       mrg 	register_t *retval;
    518       1.1       mrg {
    519       1.1       mrg 	struct netbsd32_nanosleep_args /* {
    520       1.1       mrg 		syscallarg(const netbsd32_timespecp_t) rqtp;
    521       1.1       mrg 		syscallarg(netbsd32_timespecp_t) rmtp;
    522       1.1       mrg 	} */ *uap = v;
    523       1.1       mrg 	static int nanowait;
    524       1.1       mrg 	struct netbsd32_timespec ts32;
    525       1.1       mrg 	struct timespec rqt;
    526       1.1       mrg 	struct timespec rmt;
    527      1.22    kardel 	struct timeval atv, utv, ctime;
    528      1.22    kardel 	int error, timo;
    529       1.1       mrg 
    530  1.25.6.2   reinoud 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
    531       1.1       mrg 	if (error)
    532       1.1       mrg 		return (error);
    533       1.1       mrg 
    534       1.1       mrg 	netbsd32_to_timespec(&ts32, &rqt);
    535       1.7    atatat 	TIMESPEC_TO_TIMEVAL(&atv,&rqt);
    536       1.1       mrg 	if (itimerfix(&atv))
    537       1.1       mrg 		return (EINVAL);
    538       1.1       mrg 
    539      1.22    kardel 	getmicrotime(&ctime);
    540      1.22    kardel 	timeradd(&atv,&ctime,&atv);
    541       1.1       mrg 	timo = hzto(&atv);
    542       1.8     perry 	/*
    543       1.1       mrg 	 * Avoid inadvertantly sleeping forever
    544       1.1       mrg 	 */
    545       1.1       mrg 	if (timo == 0)
    546       1.1       mrg 		timo = 1;
    547       1.1       mrg 
    548       1.1       mrg 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
    549       1.1       mrg 	if (error == ERESTART)
    550       1.1       mrg 		error = EINTR;
    551       1.1       mrg 	if (error == EWOULDBLOCK)
    552       1.1       mrg 		error = 0;
    553       1.1       mrg 
    554  1.25.6.1   reinoud 	if (SCARG_P32(uap, rmtp)) {
    555       1.9  christos 		int error1;
    556       1.1       mrg 
    557      1.22    kardel 		getmicrotime(&utv);
    558       1.1       mrg 
    559       1.1       mrg 		timersub(&atv, &utv, &utv);
    560       1.1       mrg 		if (utv.tv_sec < 0)
    561       1.1       mrg 			timerclear(&utv);
    562       1.1       mrg 
    563       1.1       mrg 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
    564       1.1       mrg 		netbsd32_from_timespec(&rmt, &ts32);
    565  1.25.6.2   reinoud 		error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
    566       1.9  christos 		if (error1)
    567       1.9  christos 			return (error1);
    568       1.1       mrg 	}
    569       1.1       mrg 
    570       1.1       mrg 	return error;
    571       1.1       mrg }
    572      1.11      cube 
    573      1.11      cube static int
    574      1.11      cube netbsd32_timer_create_fetch(const void *src, void *dst, size_t size)
    575      1.11      cube {
    576      1.11      cube 	struct sigevent *evp = dst;
    577      1.11      cube 	struct netbsd32_sigevent ev32;
    578      1.11      cube 	int error;
    579      1.11      cube 
    580      1.11      cube 	error = copyin(src, &ev32, sizeof(ev32));
    581      1.11      cube 	if (error)
    582      1.11      cube 		return error;
    583      1.11      cube 
    584      1.11      cube 	netbsd32_to_sigevent(&ev32, evp);
    585      1.11      cube 	return 0;
    586      1.11      cube }
    587      1.11      cube 
    588      1.11      cube int
    589      1.11      cube netbsd32_timer_create(struct lwp *l, void *v, register_t *retval)
    590      1.11      cube {
    591      1.11      cube 	struct netbsd32_timer_create_args /* {
    592      1.11      cube 		syscallarg(netbsd32_clockid_t) clock_id;
    593      1.11      cube 		syscallarg(netbsd32_sigeventp_t) evp;
    594      1.11      cube 		syscallarg(netbsd32_timerp_t) timerid;
    595      1.11      cube 	} */ *uap = v;
    596      1.11      cube 
    597  1.25.6.2   reinoud 	return timer_create1(SCARG_P32(uap, timerid),
    598  1.25.6.2   reinoud 	    SCARG(uap, clock_id), SCARG_P32(uap, evp),
    599      1.23        ad 	    netbsd32_timer_create_fetch, l);
    600      1.11      cube }
    601      1.11      cube 
    602      1.11      cube int
    603      1.11      cube netbsd32_timer_delete(struct lwp *l, void *v, register_t *retval)
    604      1.11      cube {
    605      1.11      cube 	struct netbsd32_timer_delete_args /* {
    606      1.11      cube 		syscallarg(netbsd32_timer_t) timerid;
    607      1.11      cube 	} */ *uap = v;
    608      1.11      cube 	struct sys_timer_delete_args ua;
    609      1.11      cube 
    610      1.11      cube 	NETBSD32TO64_UAP(timerid);
    611      1.11      cube 	return sys_timer_delete(l, (void *)&ua, retval);
    612      1.11      cube }
    613      1.11      cube 
    614      1.11      cube int
    615      1.11      cube netbsd32_timer_settime(struct lwp *l, void *v, register_t *retval)
    616      1.11      cube {
    617      1.11      cube 	struct netbsd32_timer_settime_args /* {
    618      1.11      cube 		syscallarg(netbsd32_timer_t) timerid;
    619      1.11      cube 		syscallarg(int) flags;
    620      1.11      cube 		syscallarg(const netbsd32_itimerspecp_t) value;
    621      1.11      cube 		syscallarg(netbsd32_itimerspecp_t) ovalue;
    622      1.11      cube 	} */ *uap = v;
    623      1.11      cube 	int error;
    624      1.11      cube 	struct itimerspec value, ovalue, *ovp = NULL;
    625      1.11      cube 	struct netbsd32_itimerspec its32;
    626      1.11      cube 
    627  1.25.6.2   reinoud 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
    628      1.11      cube 		return (error);
    629      1.11      cube 	netbsd32_to_timespec(&its32.it_interval, &value.it_interval);
    630      1.11      cube 	netbsd32_to_timespec(&its32.it_value, &value.it_value);
    631      1.11      cube 
    632  1.25.6.1   reinoud 	if (SCARG_P32(uap, ovalue))
    633      1.11      cube 		ovp = &ovalue;
    634      1.11      cube 
    635      1.11      cube 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    636      1.11      cube 	    SCARG(uap, flags), l->l_proc)) != 0)
    637      1.11      cube 		return error;
    638      1.11      cube 
    639      1.11      cube 	if (ovp) {
    640      1.11      cube 		netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval);
    641      1.11      cube 		netbsd32_from_timespec(&ovp->it_value, &its32.it_value);
    642  1.25.6.2   reinoud 		return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
    643      1.11      cube 	}
    644      1.11      cube 	return 0;
    645      1.11      cube }
    646      1.11      cube 
    647      1.11      cube int
    648      1.11      cube netbsd32_timer_gettime(struct lwp *l, void *v, register_t *retval)
    649      1.11      cube {
    650      1.11      cube 	struct netbsd32_timer_gettime_args /* {
    651      1.11      cube 		syscallarg(netbsd32_timer_t) timerid;
    652      1.11      cube 		syscallarg(netbsd32_itimerspecp_t) value;
    653      1.11      cube 	} */ *uap = v;
    654      1.11      cube 	int error;
    655      1.11      cube 	struct itimerspec its;
    656      1.11      cube 	struct netbsd32_itimerspec its32;
    657      1.11      cube 
    658      1.11      cube 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    659      1.11      cube 	    &its)) != 0)
    660      1.11      cube 		return error;
    661      1.11      cube 
    662      1.11      cube 	netbsd32_from_timespec(&its.it_interval, &its32.it_interval);
    663      1.11      cube 	netbsd32_from_timespec(&its.it_value, &its32.it_value);
    664      1.11      cube 
    665  1.25.6.2   reinoud 	return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
    666      1.11      cube }
    667      1.11      cube 
    668      1.11      cube int
    669      1.11      cube netbsd32_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
    670      1.11      cube {
    671      1.11      cube 	struct netbsd32_timer_getoverrun_args /* {
    672      1.11      cube 		syscallarg(netbsd32_timer_t) timerid;
    673      1.11      cube 	} */ *uap = v;
    674      1.11      cube 	struct sys_timer_getoverrun_args ua;
    675      1.11      cube 
    676      1.11      cube 	NETBSD32TO64_UAP(timerid);
    677      1.11      cube 	return sys_timer_getoverrun(l, (void *)&ua, retval);
    678      1.11      cube }
    679