Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.50.2.1
      1  1.50.2.1        he /*	$NetBSD: linux_misc_notalpha.c,v 1.50.2.1 2000/09/09 15:54:59 he Exp $	*/
      2      1.47       erh 
      3      1.47       erh /*-
      4      1.49      fvdl  * Copyright (c) 1995, 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.49      fvdl  * by Frank van der Linden and 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.1      fvdl  */
     38       1.1      fvdl 
     39       1.1      fvdl #include <sys/param.h>
     40       1.1      fvdl #include <sys/systm.h>
     41       1.1      fvdl #include <sys/kernel.h>
     42      1.47       erh #include <sys/mman.h>
     43      1.47       erh #include <sys/mount.h>
     44       1.1      fvdl #include <sys/malloc.h>
     45       1.1      fvdl #include <sys/mbuf.h>
     46      1.47       erh #include <sys/namei.h>
     47       1.1      fvdl #include <sys/ptrace.h>
     48       1.1      fvdl #include <sys/resource.h>
     49       1.1      fvdl #include <sys/resourcevar.h>
     50       1.1      fvdl #include <sys/wait.h>
     51       1.1      fvdl 
     52       1.1      fvdl #include <sys/syscallargs.h>
     53       1.1      fvdl 
     54       1.1      fvdl #include <vm/vm.h>
     55       1.1      fvdl #include <vm/vm_param.h>
     56       1.1      fvdl 
     57      1.48  christos #include <compat/linux/common/linux_types.h>
     58      1.48  christos #include <compat/linux/common/linux_fcntl.h>
     59      1.48  christos #include <compat/linux/common/linux_misc.h>
     60      1.48  christos #include <compat/linux/common/linux_mmap.h>
     61      1.48  christos #include <compat/linux/common/linux_signal.h>
     62      1.48  christos #include <compat/linux/common/linux_util.h>
     63      1.48  christos 
     64       1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     65      1.26  christos 
     66       1.1      fvdl /*
     67      1.47       erh  * This file contains routines which are used
     68      1.47       erh  * on every linux architechture except the Alpha.
     69       1.1      fvdl  */
     70      1.21   mycroft 
     71      1.47       erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     72      1.47       erh /* Not used on: alpha */
     73       1.1      fvdl 
     74       1.1      fvdl /*
     75      1.47       erh  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     76      1.47       erh  * Fiddle with the timers to make it work.
     77       1.1      fvdl  */
     78       1.1      fvdl int
     79      1.47       erh linux_sys_alarm(p, v, retval)
     80       1.1      fvdl 	struct proc *p;
     81      1.20   thorpej 	void *v;
     82      1.20   thorpej 	register_t *retval;
     83      1.20   thorpej {
     84      1.47       erh 	struct linux_sys_alarm_args /* {
     85      1.47       erh 		syscallarg(unsigned int) secs;
     86      1.20   thorpej 	} */ *uap = v;
     87      1.47       erh 	int s;
     88      1.47       erh 	struct itimerval *itp, it;
     89       1.1      fvdl 
     90      1.47       erh 	itp = &p->p_realtimer;
     91      1.47       erh 	s = splclock();
     92      1.47       erh 	/*
     93      1.47       erh 	 * Clear any pending timer alarms.
     94      1.47       erh 	 */
     95      1.47       erh 	untimeout(realitexpire, p);
     96      1.47       erh 	timerclear(&itp->it_interval);
     97      1.47       erh 	if (timerisset(&itp->it_value) &&
     98      1.47       erh 	    timercmp(&itp->it_value, &time, >))
     99      1.47       erh 		timersub(&itp->it_value, &time, &itp->it_value);
    100      1.47       erh 	/*
    101      1.47       erh 	 * Return how many seconds were left (rounded up)
    102      1.47       erh 	 */
    103      1.47       erh 	retval[0] = itp->it_value.tv_sec;
    104      1.47       erh 	if (itp->it_value.tv_usec)
    105      1.47       erh 		retval[0]++;
    106       1.1      fvdl 
    107      1.47       erh 	/*
    108      1.47       erh 	 * alarm(0) just resets the timer.
    109      1.47       erh 	 */
    110      1.47       erh 	if (SCARG(uap, secs) == 0) {
    111      1.47       erh 		timerclear(&itp->it_value);
    112      1.47       erh 		splx(s);
    113      1.47       erh 		return 0;
    114      1.47       erh 	}
    115       1.1      fvdl 
    116      1.47       erh 	/*
    117      1.47       erh 	 * Check the new alarm time for sanity, and set it.
    118      1.47       erh 	 */
    119      1.47       erh 	timerclear(&it.it_interval);
    120      1.47       erh 	it.it_value.tv_sec = SCARG(uap, secs);
    121      1.47       erh 	it.it_value.tv_usec = 0;
    122      1.47       erh 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    123      1.47       erh 		splx(s);
    124      1.47       erh 		return (EINVAL);
    125      1.16      fvdl 	}
    126       1.1      fvdl 
    127      1.47       erh 	if (timerisset(&it.it_value)) {
    128      1.47       erh 		timeradd(&it.it_value, &time, &it.it_value);
    129      1.47       erh 		timeout(realitexpire, p, hzto(&it.it_value));
    130      1.16      fvdl 	}
    131      1.47       erh 	p->p_realtimer = it;
    132      1.47       erh 	splx(s);
    133       1.1      fvdl 
    134      1.16      fvdl 	return 0;
    135       1.1      fvdl }
    136       1.1      fvdl 
    137       1.1      fvdl int
    138      1.47       erh linux_sys_nice(p, v, retval)
    139       1.1      fvdl 	struct proc *p;
    140      1.20   thorpej 	void *v;
    141      1.20   thorpej 	register_t *retval;
    142      1.20   thorpej {
    143      1.47       erh 	struct linux_sys_nice_args /* {
    144      1.47       erh 		syscallarg(int) incr;
    145      1.20   thorpej 	} */ *uap = v;
    146      1.47       erh         struct sys_setpriority_args bsa;
    147      1.20   thorpej 
    148      1.47       erh         SCARG(&bsa, which) = PRIO_PROCESS;
    149      1.47       erh         SCARG(&bsa, who) = 0;
    150      1.47       erh 	SCARG(&bsa, prio) = SCARG(uap, incr);
    151      1.47       erh         return sys_setpriority(p, &bsa, retval);
    152       1.1      fvdl }
    153       1.1      fvdl 
    154       1.1      fvdl /*
    155      1.47       erh  * The old Linux readdir was only able to read one entry at a time,
    156      1.47       erh  * even though it had a 'count' argument. In fact, the emulation
    157      1.47       erh  * of the old call was better than the original, because it did handle
    158      1.47       erh  * the count arg properly. Don't bother with it anymore now, and use
    159      1.47       erh  * it to distinguish between old and new. The difference is that the
    160      1.47       erh  * newer one actually does multiple entries, and the reclen field
    161      1.47       erh  * really is the reclen, not the namelength.
    162       1.1      fvdl  */
    163       1.1      fvdl int
    164      1.47       erh linux_sys_readdir(p, v, retval)
    165       1.1      fvdl 	struct proc *p;
    166      1.20   thorpej 	void *v;
    167      1.20   thorpej 	register_t *retval;
    168      1.20   thorpej {
    169      1.47       erh 	struct linux_sys_readdir_args /* {
    170      1.47       erh 		syscallarg(int) fd;
    171      1.47       erh 		syscallarg(struct linux_dirent *) dent;
    172      1.47       erh 		syscallarg(unsigned int) count;
    173      1.20   thorpej 	} */ *uap = v;
    174       1.1      fvdl 
    175      1.47       erh 	SCARG(uap, count) = 1;
    176      1.47       erh 	return linux_sys_getdents(p, uap, retval);
    177       1.1      fvdl }
    178       1.1      fvdl 
    179       1.1      fvdl /*
    180       1.1      fvdl  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    181       1.1      fvdl  * need to deal with it.
    182       1.1      fvdl  */
    183       1.1      fvdl int
    184      1.21   mycroft linux_sys_time(p, v, retval)
    185       1.1      fvdl 	struct proc *p;
    186      1.20   thorpej 	void *v;
    187      1.20   thorpej 	register_t *retval;
    188      1.20   thorpej {
    189      1.21   mycroft 	struct linux_sys_time_args /* {
    190       1.1      fvdl 		linux_time_t *t;
    191      1.20   thorpej 	} */ *uap = v;
    192       1.1      fvdl 	struct timeval atv;
    193       1.1      fvdl 	linux_time_t tt;
    194       1.1      fvdl 	int error;
    195       1.1      fvdl 
    196       1.1      fvdl 	microtime(&atv);
    197       1.1      fvdl 
    198       1.1      fvdl 	tt = atv.tv_sec;
    199       1.1      fvdl 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    200       1.1      fvdl 		return error;
    201       1.1      fvdl 
    202       1.1      fvdl 	retval[0] = tt;
    203       1.1      fvdl 	return 0;
    204       1.1      fvdl }
    205       1.1      fvdl 
    206       1.1      fvdl /*
    207      1.47       erh  * utime(). Do conversion to things that utimes() understands,
    208      1.47       erh  * and pass it on.
    209       1.1      fvdl  */
    210       1.1      fvdl int
    211      1.47       erh linux_sys_utime(p, v, retval)
    212       1.1      fvdl 	struct proc *p;
    213      1.20   thorpej 	void *v;
    214      1.20   thorpej 	register_t *retval;
    215      1.20   thorpej {
    216      1.47       erh 	struct linux_sys_utime_args /* {
    217      1.50  christos 		syscallarg(const char *) path;
    218      1.47       erh 		syscallarg(struct linux_utimbuf *)times;
    219      1.20   thorpej 	} */ *uap = v;
    220       1.2      fvdl 	caddr_t sg;
    221       1.2      fvdl 	int error;
    222      1.47       erh 	struct sys_utimes_args ua;
    223      1.47       erh 	struct timeval tv[2], *tvp;
    224      1.47       erh 	struct linux_utimbuf lut;
    225       1.2      fvdl 
    226       1.9  christos 	sg = stackgap_init(p->p_emul);
    227  1.50.2.1        he 	tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
    228       1.9  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    229       1.2      fvdl 
    230      1.47       erh 	SCARG(&ua, path) = SCARG(uap, path);
    231       1.2      fvdl 
    232      1.47       erh 	if (SCARG(uap, times) != NULL) {
    233      1.47       erh 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    234      1.47       erh 			return error;
    235      1.47       erh 		tv[0].tv_usec = tv[1].tv_usec = 0;
    236      1.47       erh 		tv[0].tv_sec = lut.l_actime;
    237      1.47       erh 		tv[1].tv_sec = lut.l_modtime;
    238      1.47       erh 		if ((error = copyout(tv, tvp, sizeof tv)))
    239      1.47       erh 			return error;
    240      1.47       erh 		SCARG(&ua, tptr) = tvp;
    241      1.47       erh 	}
    242      1.47       erh 	else
    243      1.47       erh 		SCARG(&ua, tptr) = NULL;
    244       1.2      fvdl 
    245      1.47       erh 	return sys_utimes(p, &ua, retval);
    246       1.1      fvdl }
    247       1.1      fvdl 
    248      1.47       erh /*
    249      1.47       erh  * waitpid(2). Passed on to the NetBSD call, surrounded by code to
    250      1.47       erh  * reserve some space for a NetBSD-style wait status, and converting
    251      1.47       erh  * it to what Linux wants.
    252      1.47       erh  */
    253       1.1      fvdl int
    254      1.47       erh linux_sys_waitpid(p, v, retval)
    255       1.1      fvdl 	struct proc *p;
    256      1.20   thorpej 	void *v;
    257      1.20   thorpej 	register_t *retval;
    258      1.20   thorpej {
    259      1.47       erh 	struct linux_sys_waitpid_args /* {
    260      1.47       erh 		syscallarg(int) pid;
    261      1.47       erh 		syscallarg(int *) status;
    262      1.47       erh 		syscallarg(int) options;
    263      1.20   thorpej 	} */ *uap = v;
    264      1.47       erh 	struct sys_wait4_args w4a;
    265      1.47       erh 	int error, *status, tstat;
    266       1.2      fvdl 	caddr_t sg;
    267       1.2      fvdl 
    268      1.47       erh 	if (SCARG(uap, status) != NULL) {
    269      1.47       erh 		sg = stackgap_init(p->p_emul);
    270      1.47       erh 		status = (int *) stackgap_alloc(&sg, sizeof status);
    271      1.47       erh 	} else
    272      1.47       erh 		status = NULL;
    273       1.2      fvdl 
    274      1.47       erh 	SCARG(&w4a, pid) = SCARG(uap, pid);
    275      1.47       erh 	SCARG(&w4a, status) = status;
    276      1.47       erh 	SCARG(&w4a, options) = SCARG(uap, options);
    277      1.47       erh 	SCARG(&w4a, rusage) = NULL;
    278       1.2      fvdl 
    279      1.47       erh 	if ((error = sys_wait4(p, &w4a, retval)))
    280       1.2      fvdl 		return error;
    281       1.2      fvdl 
    282      1.47       erh 	sigdelset(&p->p_siglist, SIGCHLD);
    283       1.2      fvdl 
    284      1.47       erh 	if (status != NULL) {
    285      1.47       erh 		if ((error = copyin(status, &tstat, sizeof tstat)))
    286      1.47       erh 			return error;
    287       1.2      fvdl 
    288      1.47       erh 		bsd_to_linux_wstat(&tstat);
    289      1.47       erh 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    290      1.44   thorpej 	}
    291       1.1      fvdl 
    292      1.47       erh 	return 0;
    293       1.1      fvdl }
    294