Home | History | Annotate | Line # | Download | only in common
linux32_time.c revision 1.25
      1  1.25     njoly /*	$NetBSD: linux32_time.c,v 1.25 2008/12/12 23:36:18 njoly Exp $ */
      2   1.1      manu 
      3   1.1      manu /*-
      4   1.1      manu  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
      5   1.1      manu  *
      6   1.1      manu  * Redistribution and use in source and binary forms, with or without
      7   1.1      manu  * modification, are permitted provided that the following conditions
      8   1.1      manu  * are met:
      9   1.1      manu  * 1. Redistributions of source code must retain the above copyright
     10   1.1      manu  *    notice, this list of conditions and the following disclaimer.
     11   1.1      manu  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1      manu  *    notice, this list of conditions and the following disclaimer in the
     13   1.1      manu  *    documentation and/or other materials provided with the distribution.
     14   1.1      manu  * 3. All advertising materials mentioning features or use of this software
     15   1.1      manu  *    must display the following acknowledgement:
     16   1.1      manu  *	This product includes software developed by Emmanuel Dreyfus
     17   1.1      manu  * 4. The name of the author may not be used to endorse or promote
     18   1.1      manu  *    products derived from this software without specific prior written
     19   1.1      manu  *    permission.
     20   1.1      manu  *
     21   1.1      manu  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22   1.1      manu  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23   1.1      manu  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.1      manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25   1.1      manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.1      manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.1      manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.1      manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1      manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.1      manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.1      manu  * POSSIBILITY OF SUCH DAMAGE.
     32   1.1      manu  */
     33   1.1      manu 
     34   1.1      manu #include <sys/cdefs.h>
     35   1.1      manu 
     36  1.25     njoly __KERNEL_RCSID(0, "$NetBSD: linux32_time.c,v 1.25 2008/12/12 23:36:18 njoly Exp $");
     37   1.1      manu 
     38   1.1      manu #include <sys/types.h>
     39   1.1      manu #include <sys/param.h>
     40   1.1      manu #include <sys/fstypes.h>
     41   1.1      manu #include <sys/signal.h>
     42   1.1      manu #include <sys/dirent.h>
     43   1.4       skd #include <sys/kauth.h>
     44   1.1      manu #include <sys/kernel.h>
     45   1.1      manu #include <sys/fcntl.h>
     46  1.12       dsl #include <sys/namei.h>
     47   1.1      manu #include <sys/select.h>
     48   1.1      manu #include <sys/proc.h>
     49  1.13       dsl #include <sys/resourcevar.h>
     50   1.1      manu #include <sys/ucred.h>
     51   1.1      manu #include <sys/swap.h>
     52  1.12       dsl #include <sys/vfs_syscalls.h>
     53  1.18     njoly #include <sys/timetc.h>
     54   1.1      manu 
     55   1.1      manu #include <machine/types.h>
     56   1.1      manu 
     57   1.1      manu #include <sys/syscallargs.h>
     58   1.1      manu 
     59   1.1      manu #include <compat/netbsd32/netbsd32.h>
     60   1.1      manu #include <compat/netbsd32/netbsd32_conv.h>
     61   1.1      manu #include <compat/netbsd32/netbsd32_syscallargs.h>
     62   1.1      manu 
     63   1.1      manu #include <compat/linux/common/linux_types.h>
     64   1.1      manu #include <compat/linux/common/linux_signal.h>
     65   1.1      manu #include <compat/linux/common/linux_machdep.h>
     66   1.1      manu #include <compat/linux/common/linux_misc.h>
     67   1.1      manu #include <compat/linux/common/linux_oldolduname.h>
     68  1.18     njoly #include <compat/linux/common/linux_sched.h>
     69  1.23        ad #include <compat/linux/common/linux_ipc.h>
     70  1.23        ad #include <compat/linux/common/linux_sem.h>
     71   1.1      manu #include <compat/linux/linux_syscallargs.h>
     72   1.1      manu 
     73   1.1      manu #include <compat/linux32/common/linux32_types.h>
     74   1.1      manu #include <compat/linux32/common/linux32_signal.h>
     75   1.1      manu #include <compat/linux32/common/linux32_machdep.h>
     76   1.1      manu #include <compat/linux32/common/linux32_sysctl.h>
     77   1.1      manu #include <compat/linux32/common/linux32_socketcall.h>
     78   1.1      manu #include <compat/linux32/linux32_syscallargs.h>
     79   1.1      manu 
     80   1.1      manu extern struct timezone linux_sys_tz;
     81  1.18     njoly 
     82  1.18     njoly static __inline void
     83  1.18     njoly native_to_linux32_timespec(struct linux32_timespec *, struct timespec *);
     84  1.18     njoly static __inline void
     85  1.18     njoly linux32_to_native_timespec(struct timespec *, struct linux32_timespec *);
     86  1.18     njoly 
     87   1.1      manu int
     88  1.16       dsl linux32_sys_gettimeofday(struct lwp *l, const struct linux32_sys_gettimeofday_args *uap, register_t *retval)
     89   1.1      manu {
     90  1.16       dsl 	/* {
     91   1.1      manu 		syscallarg(netbsd32_timevalp_t) tp;
     92   1.1      manu 		syscallarg(netbsd32_timezonep_t) tzp;
     93  1.16       dsl 	} */
     94   1.1      manu 	struct timeval tv;
     95   1.1      manu 	struct netbsd32_timeval tv32;
     96   1.1      manu 	int error;
     97   1.1      manu 
     98  1.10       dsl 	if (SCARG_P32(uap, tp) != NULL) {
     99   1.1      manu 		microtime(&tv);
    100   1.1      manu 		netbsd32_from_timeval(&tv, &tv32);
    101  1.10       dsl 		if ((error = copyout(&tv32, SCARG_P32(uap, tp),
    102   1.1      manu 		    sizeof(tv32))) != 0)
    103   1.1      manu 			return error;
    104   1.1      manu 	}
    105   1.1      manu 
    106   1.1      manu 	/* timezone size does not change */
    107  1.10       dsl 	if (SCARG_P32(uap, tzp) != NULL) {
    108  1.10       dsl 		if ((error = copyout(&linux_sys_tz, SCARG_P32(uap, tzp),
    109   1.1      manu 		    sizeof(linux_sys_tz))) != 0)
    110   1.1      manu 			return error;
    111   1.1      manu 	}
    112   1.1      manu 
    113   1.1      manu 	return 0;
    114   1.1      manu }
    115   1.1      manu 
    116   1.1      manu int
    117  1.16       dsl linux32_sys_settimeofday(struct lwp *l, const struct linux32_sys_settimeofday_args *uap, register_t *retval)
    118   1.1      manu {
    119  1.16       dsl 	/* {
    120   1.1      manu 		syscallarg(netbsd32_timevalp_t) tp;
    121   1.1      manu 		syscallarg(netbsd32_timezonep_t) tzp;
    122  1.16       dsl 	} */
    123   1.1      manu 	struct linux_sys_settimeofday_args ua;
    124   1.1      manu 
    125   1.1      manu 	NETBSD32TOP_UAP(tp, struct timeval);
    126   1.1      manu 	NETBSD32TOP_UAP(tzp, struct timezone);
    127   1.1      manu 
    128   1.1      manu 	return linux_sys_settimeofday(l, &ua, retval);
    129   1.1      manu }
    130   1.1      manu 
    131   1.1      manu int
    132  1.16       dsl linux32_sys_time(struct lwp *l, const struct linux32_sys_time_args *uap, register_t *retval)
    133   1.1      manu {
    134  1.16       dsl 	/* {
    135   1.1      manu 		syscallcarg(linux32_timep_t) t;
    136  1.16       dsl 	} */
    137   1.2      manu         struct timeval atv;
    138   1.2      manu         linux32_time_t tt;
    139   1.2      manu         int error;
    140   1.2      manu 
    141   1.2      manu         microtime(&atv);
    142   1.2      manu 
    143   1.2      manu         tt = (linux32_time_t)atv.tv_sec;
    144   1.2      manu 
    145  1.10       dsl         if (SCARG_P32(uap, t) && (error = copyout(&tt,
    146  1.10       dsl 	    SCARG_P32(uap, t), sizeof(tt))))
    147   1.2      manu                 return error;
    148   1.1      manu 
    149   1.2      manu         retval[0] = tt;
    150   1.1      manu 
    151   1.2      manu         return 0;
    152   1.1      manu }
    153   1.1      manu 
    154   1.1      manu 
    155  1.20  christos #define	CONVTCK(r)	(r.tv_sec * hz + r.tv_usec / (1000000 / hz))
    156  1.20  christos 
    157   1.1      manu int
    158  1.16       dsl linux32_sys_times(struct lwp *l, const struct linux32_sys_times_args *uap, register_t *retval)
    159   1.1      manu {
    160  1.16       dsl 	/* {
    161   1.1      manu 		syscallarg(linux32_tmsp_t) tms;
    162  1.16       dsl 	} */
    163  1.20  christos 	struct proc *p = l->l_proc;
    164  1.20  christos 	struct timeval t;
    165  1.20  christos 	int error;
    166   1.1      manu 
    167  1.20  christos 	if (SCARG_P32(uap, tms)) {
    168  1.20  christos 		struct linux32_tms ltms32;
    169  1.20  christos 		struct rusage ru;
    170  1.20  christos 
    171  1.20  christos 		mutex_enter(p->p_lock);
    172  1.20  christos 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
    173  1.20  christos 		ltms32.ltms32_utime = CONVTCK(ru.ru_utime);
    174  1.20  christos 		ltms32.ltms32_stime = CONVTCK(ru.ru_stime);
    175  1.20  christos 		ltms32.ltms32_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    176  1.20  christos 		ltms32.ltms32_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    177  1.20  christos 		mutex_exit(p->p_lock);
    178  1.13       dsl 
    179  1.21  christos 		error = copyout(&ltms32, SCARG_P32(uap, tms), sizeof(ltms32));
    180  1.21  christos 		if (error)
    181  1.20  christos 			return error;
    182  1.20  christos 	}
    183  1.13       dsl 
    184  1.20  christos 	getmicrouptime(&t);
    185  1.13       dsl 
    186  1.20  christos 	retval[0] = ((linux32_clock_t)(CONVTCK(t)));
    187  1.20  christos 	return 0;
    188  1.20  christos }
    189   1.1      manu 
    190  1.20  christos #undef CONVTCK
    191   1.1      manu 
    192   1.1      manu int
    193  1.16       dsl linux32_sys_stime(struct lwp *l, const struct linux32_sys_stime_args *uap, register_t *retval)
    194   1.1      manu {
    195  1.16       dsl 	/* {
    196   1.1      manu 		syscallarg(linux32_timep_t) t;
    197  1.16       dsl 	} */
    198   1.1      manu 	struct timespec ts;
    199   1.1      manu 	linux32_time_t tt32;
    200   1.1      manu 	int error;
    201   1.1      manu 
    202  1.10       dsl 	if ((error = copyin(&tt32, SCARG_P32(uap, t), sizeof tt32)) != 0)
    203  1.10       dsl 		return error;
    204   1.1      manu 
    205   1.1      manu 	ts.tv_sec = (long)tt32;
    206   1.1      manu 	ts.tv_nsec = 0;
    207   1.1      manu 
    208   1.5        ad 	return settime(l->l_proc, &ts);
    209   1.1      manu }
    210   1.1      manu 
    211   1.1      manu int
    212  1.16       dsl linux32_sys_utime(struct lwp *l, const struct linux32_sys_utime_args *uap, register_t *retval)
    213   1.1      manu {
    214  1.16       dsl 	/* {
    215   1.1      manu 		syscallarg(const netbsd32_charp) path;
    216   1.1      manu 		syscallarg(linux32_utimbufp_t) times;
    217  1.16       dsl 	} */
    218   1.1      manu         struct timeval tv[2], *tvp;
    219   1.1      manu         struct linux32_utimbuf lut;
    220   1.1      manu         int error;
    221   1.1      manu 
    222  1.10       dsl         if (SCARG_P32(uap, times) != NULL) {
    223  1.10       dsl                 if ((error = copyin(SCARG_P32(uap, times), &lut, sizeof lut)))
    224   1.1      manu                         return error;
    225   1.1      manu 
    226   1.1      manu                 tv[0].tv_sec = (long)lut.l_actime;
    227   1.1      manu                 tv[0].tv_usec = 0;
    228   1.1      manu                 tv[1].tv_sec = (long)lut.l_modtime;
    229   1.1      manu 		tv[1].tv_usec = 0;
    230  1.12       dsl                 tvp = tv;
    231   1.1      manu         } else {
    232  1.12       dsl 		tvp = NULL;
    233   1.1      manu 	}
    234   1.1      manu 
    235  1.12       dsl         return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
    236  1.12       dsl 			    tvp, UIO_SYSSPACE);
    237  1.18     njoly }
    238  1.18     njoly 
    239  1.18     njoly static __inline void
    240  1.18     njoly native_to_linux32_timespec(struct linux32_timespec *ltp, struct timespec *ntp)
    241  1.18     njoly {
    242  1.18     njoly 	ltp->tv_sec = ntp->tv_sec;
    243  1.18     njoly 	ltp->tv_nsec = ntp->tv_nsec;
    244  1.18     njoly }
    245  1.18     njoly 
    246  1.18     njoly static __inline void
    247  1.18     njoly linux32_to_native_timespec(struct timespec *ntp, struct linux32_timespec *ltp)
    248  1.18     njoly {
    249  1.18     njoly 	ntp->tv_sec = ltp->tv_sec;
    250  1.18     njoly 	ntp->tv_nsec = ltp->tv_nsec;
    251  1.18     njoly }
    252  1.18     njoly 
    253  1.18     njoly int
    254  1.22     njoly linux32_sys_nanosleep(struct lwp *l,
    255  1.22     njoly     const struct linux32_sys_nanosleep_args *uap, register_t *retval)
    256  1.22     njoly {
    257  1.22     njoly 	/* {
    258  1.22     njoly 		syscallarg(linux32_timespecp_t) rqtp;
    259  1.22     njoly 		syscallarg(linux32_timespecp_t) rmtp;
    260  1.22     njoly 	} */
    261  1.22     njoly 	struct timespec rqts, rmts;
    262  1.22     njoly 	struct linux32_timespec lrqts, lrmts;
    263  1.22     njoly 	int error, error1;
    264  1.22     njoly 
    265  1.22     njoly 	error = copyin(SCARG_P32(uap, rqtp), &lrqts, sizeof(lrqts));
    266  1.22     njoly 	if (error != 0)
    267  1.22     njoly 		return error;
    268  1.22     njoly 	linux32_to_native_timespec(&rqts, &lrqts);
    269  1.22     njoly 
    270  1.22     njoly 	error = nanosleep1(l, &rqts, SCARG_P32(uap, rmtp) ? &rmts : NULL);
    271  1.22     njoly 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    272  1.22     njoly 		return error;
    273  1.22     njoly 
    274  1.22     njoly 	native_to_linux32_timespec(&lrmts, &rmts);
    275  1.22     njoly 	error1 = copyout(&lrmts, SCARG_P32(uap, rmtp), sizeof(lrmts));
    276  1.22     njoly 	return error1 ? error1 : error;
    277  1.22     njoly }
    278  1.22     njoly 
    279  1.22     njoly int
    280  1.18     njoly linux32_sys_clock_settime(struct lwp *l,
    281  1.18     njoly     const struct linux32_sys_clock_settime_args *uap, register_t *retval)
    282  1.18     njoly {
    283  1.18     njoly 	/* {
    284  1.18     njoly 		syscallarg(clockid_t) which;
    285  1.18     njoly 		syscallarg(linux32_timespecp_t) tp;
    286  1.18     njoly 	} */
    287  1.18     njoly 	int error;
    288  1.18     njoly 	struct timespec ts;
    289  1.18     njoly 	struct linux32_timespec lts;
    290  1.18     njoly 
    291  1.18     njoly 	switch (SCARG(uap, which)) {
    292  1.18     njoly 	case LINUX_CLOCK_REALTIME:
    293  1.18     njoly 		break;
    294  1.18     njoly 	default:
    295  1.18     njoly 		return EINVAL;
    296  1.18     njoly 	}
    297  1.18     njoly 
    298  1.18     njoly 	if ((error = copyin(SCARG_P32(uap, tp), &lts, sizeof lts)))
    299  1.18     njoly 		return error;
    300  1.18     njoly 
    301  1.18     njoly 	linux32_to_native_timespec(&ts, &lts);
    302  1.18     njoly 	return settime(l->l_proc, &ts);
    303  1.18     njoly }
    304  1.18     njoly 
    305  1.18     njoly int
    306  1.18     njoly linux32_sys_clock_gettime(struct lwp *l,
    307  1.18     njoly     const struct linux32_sys_clock_gettime_args *uap, register_t *retval)
    308  1.18     njoly {
    309  1.18     njoly 	/* {
    310  1.18     njoly 		syscallarg(clockid_t) which;
    311  1.18     njoly 		syscallarg(linux32_timespecp_t) tp;
    312  1.18     njoly 	} */
    313  1.18     njoly 	struct timespec ts;
    314  1.18     njoly 	struct linux32_timespec lts;
    315  1.18     njoly 
    316  1.18     njoly 	switch (SCARG(uap, which)) {
    317  1.18     njoly 	case LINUX_CLOCK_REALTIME:
    318  1.18     njoly 		nanotime(&ts);
    319  1.18     njoly 		break;
    320  1.18     njoly 	case LINUX_CLOCK_MONOTONIC:
    321  1.18     njoly 		nanouptime(&ts);
    322  1.18     njoly 		break;
    323  1.18     njoly 	default:
    324  1.18     njoly 		return EINVAL;
    325  1.18     njoly 	}
    326  1.18     njoly 
    327  1.18     njoly 	native_to_linux32_timespec(&lts, &ts);
    328  1.18     njoly 	return copyout(&lts, SCARG_P32(uap, tp), sizeof lts);
    329  1.18     njoly }
    330  1.18     njoly 
    331  1.18     njoly int
    332  1.18     njoly linux32_sys_clock_getres(struct lwp *l,
    333  1.18     njoly     const struct linux32_sys_clock_getres_args *uap, register_t *retval)
    334  1.18     njoly {
    335  1.18     njoly 	/* {
    336  1.18     njoly 		syscallarg(clockid_t) which;
    337  1.18     njoly 		syscallarg(linux32_timespecp_t) tp;
    338  1.18     njoly 	} */
    339  1.18     njoly 	int error;
    340  1.18     njoly 	clockid_t id;
    341  1.18     njoly 	struct timespec ts;
    342  1.18     njoly 	struct linux32_timespec lts;
    343  1.18     njoly 
    344  1.18     njoly 	error = linux_to_native_clockid(&id, SCARG(uap, which));
    345  1.18     njoly 	if (error != 0 || SCARG_P32(uap, tp) == NULL)
    346  1.18     njoly 		return error;
    347  1.18     njoly 
    348  1.18     njoly 	ts.tv_sec = 0;
    349  1.18     njoly 	ts.tv_nsec = 1000000000 / tc_getfrequency();
    350  1.18     njoly 	native_to_linux32_timespec(&lts, &ts);
    351  1.18     njoly 	return copyout(&lts, SCARG_P32(uap, tp), sizeof lts);
    352  1.18     njoly 
    353  1.18     njoly 	return 0;
    354  1.18     njoly }
    355  1.25     njoly 
    356  1.25     njoly int
    357  1.25     njoly linux32_sys_clock_nanosleep(struct lwp *l,
    358  1.25     njoly     const struct linux32_sys_clock_nanosleep_args *uap, register_t *retval)
    359  1.25     njoly {
    360  1.25     njoly 	/* {
    361  1.25     njoly 		syscallarg(clockid_t) which;
    362  1.25     njoly 		syscallarg(int) flags;
    363  1.25     njoly 		syscallarg(linux32_timespecp_t) rqtp;
    364  1.25     njoly 		syscallarg(linux32_timespecp_t) rmtp;
    365  1.25     njoly 	} */
    366  1.25     njoly 	struct linux32_timespec lrqts, lrmts;
    367  1.25     njoly 	struct timespec rqts, rmts;
    368  1.25     njoly 	int error, error1;
    369  1.25     njoly 
    370  1.25     njoly 	if (SCARG(uap, flags) != 0)
    371  1.25     njoly 		return EINVAL;          /* XXX deal with TIMER_ABSTIME */
    372  1.25     njoly 	if (SCARG(uap, which) != LINUX_CLOCK_REALTIME)
    373  1.25     njoly 		return EINVAL;
    374  1.25     njoly 
    375  1.25     njoly 	error = copyin(SCARG_P32(uap, rqtp), &lrqts, sizeof lrqts);
    376  1.25     njoly 	if (error != 0)
    377  1.25     njoly 		return error;
    378  1.25     njoly 	linux32_to_native_timespec(&rqts, &lrqts);
    379  1.25     njoly 
    380  1.25     njoly 	error = nanosleep1(l, &rqts, SCARG_P32(uap, rmtp) ? &rmts : 0);
    381  1.25     njoly 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    382  1.25     njoly 		return error;
    383  1.25     njoly 
    384  1.25     njoly 	native_to_linux32_timespec(&lrmts, &rmts);
    385  1.25     njoly 	error1 = copyout(&lrmts, SCARG_P32(uap, rmtp), sizeof lrmts);
    386  1.25     njoly 	return error1 ? error1 : error;
    387  1.25     njoly }
    388