Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.47
      1  1.47       erh /*	$NetBSD: linux_misc_notalpha.c,v 1.47 1998/10/01 04:05:55 erh Exp $	*/
      2  1.47       erh 
      3  1.47       erh /*-
      4  1.47       erh  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.47       erh  * All rights reserved.
      6  1.47       erh  *
      7  1.47       erh  * This code is derived from software contributed to The NetBSD Foundation
      8  1.47       erh  * by Eric Haszlakiewicz.
      9  1.47       erh  *
     10  1.47       erh  * Redistribution and use in source and binary forms, with or without
     11  1.47       erh  * modification, are permitted provided that the following conditions
     12  1.47       erh  * are met:
     13  1.47       erh  * 1. Redistributions of source code must retain the above copyright
     14  1.47       erh  *    notice, this list of conditions and the following disclaimer.
     15  1.47       erh  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.47       erh  *    notice, this list of conditions and the following disclaimer in the
     17  1.47       erh  *    documentation and/or other materials provided with the distribution.
     18  1.47       erh  * 3. All advertising materials mentioning features or use of this software
     19  1.47       erh  *    must display the following acknowledgement:
     20  1.47       erh  *	This product includes software developed by the NetBSD
     21  1.47       erh  *	Foundation, Inc. and its contributors.
     22  1.47       erh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.47       erh  *    contributors may be used to endorse or promote products derived
     24  1.47       erh  *    from this software without specific prior written permission.
     25  1.47       erh  *
     26  1.47       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.47       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.47       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.47       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.47       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.47       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.47       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.47       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.47       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.47       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.47       erh  * POSSIBILITY OF SUCH DAMAGE.
     37  1.47       erh  */
     38   1.1      fvdl 
     39   1.1      fvdl /*
     40   1.1      fvdl  * Copyright (c) 1995 Frank van der Linden
     41   1.1      fvdl  * All rights reserved.
     42   1.1      fvdl  *
     43   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     44   1.1      fvdl  * modification, are permitted provided that the following conditions
     45   1.1      fvdl  * are met:
     46   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     47   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     48   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     50   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     51   1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     52   1.1      fvdl  *    must display the following acknowledgement:
     53   1.1      fvdl  *      This product includes software developed for the NetBSD Project
     54   1.1      fvdl  *      by Frank van der Linden
     55   1.1      fvdl  * 4. The name of the author may not be used to endorse or promote products
     56   1.1      fvdl  *    derived from this software without specific prior written permission
     57   1.1      fvdl  *
     58   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59   1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60   1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61   1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     62   1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63   1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64   1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65   1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66   1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67   1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68   1.1      fvdl  */
     69   1.1      fvdl 
     70   1.1      fvdl #include <sys/param.h>
     71   1.1      fvdl #include <sys/systm.h>
     72   1.1      fvdl #include <sys/kernel.h>
     73  1.47       erh #include <sys/mman.h>
     74  1.47       erh #include <sys/mount.h>
     75   1.1      fvdl #include <sys/malloc.h>
     76   1.1      fvdl #include <sys/mbuf.h>
     77  1.47       erh #include <sys/namei.h>
     78   1.1      fvdl #include <sys/ptrace.h>
     79   1.1      fvdl #include <sys/resource.h>
     80   1.1      fvdl #include <sys/resourcevar.h>
     81   1.1      fvdl #include <sys/wait.h>
     82   1.1      fvdl 
     83   1.1      fvdl #include <sys/syscallargs.h>
     84   1.1      fvdl 
     85   1.1      fvdl #include <vm/vm.h>
     86   1.1      fvdl #include <vm/vm_param.h>
     87   1.1      fvdl 
     88   1.1      fvdl #include <compat/linux/linux_types.h>
     89   1.1      fvdl #include <compat/linux/linux_fcntl.h>
     90  1.47       erh #include <compat/linux/linux_misc.h>
     91   1.1      fvdl #include <compat/linux/linux_mmap.h>
     92  1.11   mycroft #include <compat/linux/linux_signal.h>
     93   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     94   1.1      fvdl #include <compat/linux/linux_util.h>
     95  1.26  christos 
     96   1.1      fvdl /*
     97  1.47       erh  * This file contains routines which are used
     98  1.47       erh  * on every linux architechture except the Alpha.
     99   1.1      fvdl  */
    100  1.21   mycroft 
    101  1.47       erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
    102  1.47       erh /* Not used on: alpha */
    103   1.1      fvdl 
    104   1.1      fvdl /*
    105  1.47       erh  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
    106  1.47       erh  * Fiddle with the timers to make it work.
    107   1.1      fvdl  */
    108   1.1      fvdl int
    109  1.47       erh linux_sys_alarm(p, v, retval)
    110   1.1      fvdl 	struct proc *p;
    111  1.20   thorpej 	void *v;
    112  1.20   thorpej 	register_t *retval;
    113  1.20   thorpej {
    114  1.47       erh 	struct linux_sys_alarm_args /* {
    115  1.47       erh 		syscallarg(unsigned int) secs;
    116  1.20   thorpej 	} */ *uap = v;
    117  1.47       erh 	int s;
    118  1.47       erh 	struct itimerval *itp, it;
    119   1.1      fvdl 
    120  1.47       erh 	itp = &p->p_realtimer;
    121  1.47       erh 	s = splclock();
    122  1.47       erh 	/*
    123  1.47       erh 	 * Clear any pending timer alarms.
    124  1.47       erh 	 */
    125  1.47       erh 	untimeout(realitexpire, p);
    126  1.47       erh 	timerclear(&itp->it_interval);
    127  1.47       erh 	if (timerisset(&itp->it_value) &&
    128  1.47       erh 	    timercmp(&itp->it_value, &time, >))
    129  1.47       erh 		timersub(&itp->it_value, &time, &itp->it_value);
    130  1.47       erh 	/*
    131  1.47       erh 	 * Return how many seconds were left (rounded up)
    132  1.47       erh 	 */
    133  1.47       erh 	retval[0] = itp->it_value.tv_sec;
    134  1.47       erh 	if (itp->it_value.tv_usec)
    135  1.47       erh 		retval[0]++;
    136   1.1      fvdl 
    137  1.47       erh 	/*
    138  1.47       erh 	 * alarm(0) just resets the timer.
    139  1.47       erh 	 */
    140  1.47       erh 	if (SCARG(uap, secs) == 0) {
    141  1.47       erh 		timerclear(&itp->it_value);
    142  1.47       erh 		splx(s);
    143  1.47       erh 		return 0;
    144  1.47       erh 	}
    145   1.1      fvdl 
    146  1.47       erh 	/*
    147  1.47       erh 	 * Check the new alarm time for sanity, and set it.
    148  1.47       erh 	 */
    149  1.47       erh 	timerclear(&it.it_interval);
    150  1.47       erh 	it.it_value.tv_sec = SCARG(uap, secs);
    151  1.47       erh 	it.it_value.tv_usec = 0;
    152  1.47       erh 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    153  1.47       erh 		splx(s);
    154  1.47       erh 		return (EINVAL);
    155  1.16      fvdl 	}
    156   1.1      fvdl 
    157  1.47       erh 	if (timerisset(&it.it_value)) {
    158  1.47       erh 		timeradd(&it.it_value, &time, &it.it_value);
    159  1.47       erh 		timeout(realitexpire, p, hzto(&it.it_value));
    160  1.16      fvdl 	}
    161  1.47       erh 	p->p_realtimer = it;
    162  1.47       erh 	splx(s);
    163   1.1      fvdl 
    164  1.16      fvdl 	return 0;
    165   1.1      fvdl }
    166   1.1      fvdl 
    167   1.1      fvdl int
    168  1.47       erh linux_sys_nice(p, v, retval)
    169   1.1      fvdl 	struct proc *p;
    170  1.20   thorpej 	void *v;
    171  1.20   thorpej 	register_t *retval;
    172  1.20   thorpej {
    173  1.47       erh 	struct linux_sys_nice_args /* {
    174  1.47       erh 		syscallarg(int) incr;
    175  1.20   thorpej 	} */ *uap = v;
    176  1.47       erh         struct sys_setpriority_args bsa;
    177  1.20   thorpej 
    178  1.47       erh         SCARG(&bsa, which) = PRIO_PROCESS;
    179  1.47       erh         SCARG(&bsa, who) = 0;
    180  1.47       erh 	SCARG(&bsa, prio) = SCARG(uap, incr);
    181  1.47       erh         return sys_setpriority(p, &bsa, retval);
    182   1.1      fvdl }
    183   1.1      fvdl 
    184   1.1      fvdl /*
    185  1.47       erh  * The old Linux readdir was only able to read one entry at a time,
    186  1.47       erh  * even though it had a 'count' argument. In fact, the emulation
    187  1.47       erh  * of the old call was better than the original, because it did handle
    188  1.47       erh  * the count arg properly. Don't bother with it anymore now, and use
    189  1.47       erh  * it to distinguish between old and new. The difference is that the
    190  1.47       erh  * newer one actually does multiple entries, and the reclen field
    191  1.47       erh  * really is the reclen, not the namelength.
    192   1.1      fvdl  */
    193   1.1      fvdl int
    194  1.47       erh linux_sys_readdir(p, v, retval)
    195   1.1      fvdl 	struct proc *p;
    196  1.20   thorpej 	void *v;
    197  1.20   thorpej 	register_t *retval;
    198  1.20   thorpej {
    199  1.47       erh 	struct linux_sys_readdir_args /* {
    200  1.47       erh 		syscallarg(int) fd;
    201  1.47       erh 		syscallarg(struct linux_dirent *) dent;
    202  1.47       erh 		syscallarg(unsigned int) count;
    203  1.20   thorpej 	} */ *uap = v;
    204   1.1      fvdl 
    205  1.47       erh 	SCARG(uap, count) = 1;
    206  1.47       erh 	return linux_sys_getdents(p, uap, retval);
    207   1.1      fvdl }
    208   1.1      fvdl 
    209   1.1      fvdl /*
    210   1.1      fvdl  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    211   1.1      fvdl  * need to deal with it.
    212   1.1      fvdl  */
    213   1.1      fvdl int
    214  1.21   mycroft linux_sys_time(p, v, retval)
    215   1.1      fvdl 	struct proc *p;
    216  1.20   thorpej 	void *v;
    217  1.20   thorpej 	register_t *retval;
    218  1.20   thorpej {
    219  1.21   mycroft 	struct linux_sys_time_args /* {
    220   1.1      fvdl 		linux_time_t *t;
    221  1.20   thorpej 	} */ *uap = v;
    222   1.1      fvdl 	struct timeval atv;
    223   1.1      fvdl 	linux_time_t tt;
    224   1.1      fvdl 	int error;
    225   1.1      fvdl 
    226   1.1      fvdl 	microtime(&atv);
    227   1.1      fvdl 
    228   1.1      fvdl 	tt = atv.tv_sec;
    229   1.1      fvdl 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    230   1.1      fvdl 		return error;
    231   1.1      fvdl 
    232   1.1      fvdl 	retval[0] = tt;
    233   1.1      fvdl 	return 0;
    234   1.1      fvdl }
    235   1.1      fvdl 
    236   1.1      fvdl /*
    237  1.47       erh  * utime(). Do conversion to things that utimes() understands,
    238  1.47       erh  * and pass it on.
    239   1.1      fvdl  */
    240   1.1      fvdl int
    241  1.47       erh linux_sys_utime(p, v, retval)
    242   1.1      fvdl 	struct proc *p;
    243  1.20   thorpej 	void *v;
    244  1.20   thorpej 	register_t *retval;
    245  1.20   thorpej {
    246  1.47       erh 	struct linux_sys_utime_args /* {
    247   1.1      fvdl 		syscallarg(char *) path;
    248  1.47       erh 		syscallarg(struct linux_utimbuf *)times;
    249  1.20   thorpej 	} */ *uap = v;
    250   1.2      fvdl 	caddr_t sg;
    251   1.2      fvdl 	int error;
    252  1.47       erh 	struct sys_utimes_args ua;
    253  1.47       erh 	struct timeval tv[2], *tvp;
    254  1.47       erh 	struct linux_utimbuf lut;
    255   1.2      fvdl 
    256   1.9  christos 	sg = stackgap_init(p->p_emul);
    257   1.9  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    258   1.2      fvdl 
    259  1.47       erh 	SCARG(&ua, path) = SCARG(uap, path);
    260   1.2      fvdl 
    261  1.47       erh 	if (SCARG(uap, times) != NULL) {
    262  1.47       erh 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    263  1.47       erh 			return error;
    264  1.47       erh 		tv[0].tv_usec = tv[1].tv_usec = 0;
    265  1.47       erh 		tv[0].tv_sec = lut.l_actime;
    266  1.47       erh 		tv[1].tv_sec = lut.l_modtime;
    267  1.47       erh 		tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
    268  1.47       erh 		if ((error = copyout(tv, tvp, sizeof tv)))
    269  1.47       erh 			return error;
    270  1.47       erh 		SCARG(&ua, tptr) = tvp;
    271  1.47       erh 	}
    272  1.47       erh 	else
    273  1.47       erh 		SCARG(&ua, tptr) = NULL;
    274   1.2      fvdl 
    275  1.47       erh 	return sys_utimes(p, &ua, retval);
    276   1.1      fvdl }
    277   1.1      fvdl 
    278  1.47       erh /*
    279  1.47       erh  * waitpid(2). Passed on to the NetBSD call, surrounded by code to
    280  1.47       erh  * reserve some space for a NetBSD-style wait status, and converting
    281  1.47       erh  * it to what Linux wants.
    282  1.47       erh  */
    283   1.1      fvdl int
    284  1.47       erh linux_sys_waitpid(p, v, retval)
    285   1.1      fvdl 	struct proc *p;
    286  1.20   thorpej 	void *v;
    287  1.20   thorpej 	register_t *retval;
    288  1.20   thorpej {
    289  1.47       erh 	struct linux_sys_waitpid_args /* {
    290  1.47       erh 		syscallarg(int) pid;
    291  1.47       erh 		syscallarg(int *) status;
    292  1.47       erh 		syscallarg(int) options;
    293  1.20   thorpej 	} */ *uap = v;
    294  1.47       erh 	struct sys_wait4_args w4a;
    295  1.47       erh 	int error, *status, tstat;
    296   1.2      fvdl 	caddr_t sg;
    297   1.2      fvdl 
    298  1.47       erh 	if (SCARG(uap, status) != NULL) {
    299  1.47       erh 		sg = stackgap_init(p->p_emul);
    300  1.47       erh 		status = (int *) stackgap_alloc(&sg, sizeof status);
    301  1.47       erh 	} else
    302  1.47       erh 		status = NULL;
    303   1.2      fvdl 
    304  1.47       erh 	SCARG(&w4a, pid) = SCARG(uap, pid);
    305  1.47       erh 	SCARG(&w4a, status) = status;
    306  1.47       erh 	SCARG(&w4a, options) = SCARG(uap, options);
    307  1.47       erh 	SCARG(&w4a, rusage) = NULL;
    308   1.2      fvdl 
    309  1.47       erh 	if ((error = sys_wait4(p, &w4a, retval)))
    310   1.2      fvdl 		return error;
    311   1.2      fvdl 
    312  1.47       erh 	sigdelset(&p->p_siglist, SIGCHLD);
    313   1.2      fvdl 
    314  1.47       erh 	if (status != NULL) {
    315  1.47       erh 		if ((error = copyin(status, &tstat, sizeof tstat)))
    316  1.47       erh 			return error;
    317   1.2      fvdl 
    318  1.47       erh 		bsd_to_linux_wstat(&tstat);
    319  1.47       erh 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    320  1.44   thorpej 	}
    321   1.1      fvdl 
    322  1.47       erh 	return 0;
    323   1.1      fvdl }
    324