Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.83.2.2
      1 /*	$NetBSD: linux_misc.c,v 1.83.2.2 2001/04/09 01:55:42 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
      9  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Linux compatibility module. Try to deal with various Linux system calls.
     42  */
     43 
     44 /*
     45  * These functions have been moved to multiarch to allow
     46  * selection of which machines include them to be
     47  * determined by the individual files.linux_<arch> files.
     48  *
     49  * Function in multiarch:
     50  *	linux_sys_break			: linux_break.c
     51  *	linux_sys_alarm			: linux_misc_notalpha.c
     52  *	linux_sys_getresgid		: linux_misc_notalpha.c
     53  *	linux_sys_nice			: linux_misc_notalpha.c
     54  *	linux_sys_readdir		: linux_misc_notalpha.c
     55  *	linux_sys_setresgid		: linux_misc_notalpha.c
     56  *	linux_sys_time			: linux_misc_notalpha.c
     57  *	linux_sys_utime			: linux_misc_notalpha.c
     58  *	linux_sys_waitpid		: linux_misc_notalpha.c
     59  *	linux_sys_old_mmap		: linux_oldmmap.c
     60  *	linux_sys_oldolduname		: linux_oldolduname.c
     61  *	linux_sys_oldselect		: linux_oldselect.c
     62  *	linux_sys_olduname		: linux_olduname.c
     63  *	linux_sys_pipe			: linux_pipe.c
     64  */
     65 
     66 #include <sys/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/namei.h>
     69 #include <sys/lwp.h>
     70 #include <sys/proc.h>
     71 #include <sys/dirent.h>
     72 #include <sys/file.h>
     73 #include <sys/stat.h>
     74 #include <sys/filedesc.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/kernel.h>
     77 #include <sys/malloc.h>
     78 #include <sys/mbuf.h>
     79 #include <sys/mman.h>
     80 #include <sys/mount.h>
     81 #include <sys/reboot.h>
     82 #include <sys/resource.h>
     83 #include <sys/resourcevar.h>
     84 #include <sys/signal.h>
     85 #include <sys/signalvar.h>
     86 #include <sys/socket.h>
     87 #include <sys/time.h>
     88 #include <sys/times.h>
     89 #include <sys/vnode.h>
     90 #include <sys/uio.h>
     91 #include <sys/wait.h>
     92 #include <sys/utsname.h>
     93 #include <sys/unistd.h>
     94 #include <sys/swap.h>		/* for SWAP_ON */
     95 #include <sys/sysctl.h>		/* for KERN_DOMAINNAME */
     96 
     97 #include <sys/ptrace.h>
     98 #include <machine/ptrace.h>
     99 
    100 #include <sys/syscallargs.h>
    101 
    102 #include <compat/linux/common/linux_types.h>
    103 #include <compat/linux/common/linux_signal.h>
    104 
    105 #include <compat/linux/linux_syscallargs.h>
    106 
    107 #include <compat/linux/common/linux_fcntl.h>
    108 #include <compat/linux/common/linux_mmap.h>
    109 #include <compat/linux/common/linux_dirent.h>
    110 #include <compat/linux/common/linux_util.h>
    111 #include <compat/linux/common/linux_misc.h>
    112 #include <compat/linux/common/linux_ptrace.h>
    113 #include <compat/linux/common/linux_reboot.h>
    114 #include <compat/linux/common/linux_emuldata.h>
    115 
    116 const int linux_ptrace_request_map[] = {
    117 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
    118 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
    119 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
    120 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
    121 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
    122 	LINUX_PTRACE_CONT,	PT_CONTINUE,
    123 	LINUX_PTRACE_KILL,	PT_KILL,
    124 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
    125 	LINUX_PTRACE_DETACH,	PT_DETACH,
    126 #ifdef PT_STEP
    127 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
    128 #endif
    129 	-1
    130 };
    131 
    132 /* Local linux_misc.c functions: */
    133 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
    134 
    135 /*
    136  * The information on a terminated (or stopped) process needs
    137  * to be converted in order for Linux binaries to get a valid signal
    138  * number out of it.
    139  */
    140 void
    141 bsd_to_linux_wstat(st)
    142 	int *st;
    143 {
    144 
    145 	int sig;
    146 
    147 	if (WIFSIGNALED(*st)) {
    148 		sig = WTERMSIG(*st);
    149 		if (sig >= 0 && sig < NSIG)
    150 			*st= (*st& ~0177) | native_to_linux_sig[sig];
    151 	} else if (WIFSTOPPED(*st)) {
    152 		sig = WSTOPSIG(*st);
    153 		if (sig >= 0 && sig < NSIG)
    154 			*st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
    155 	}
    156 }
    157 
    158 /*
    159  * This is very much the same as waitpid()
    160  */
    161 int
    162 linux_sys_wait4(l, v, retval)
    163 	struct lwp *l;
    164 	void *v;
    165 	register_t *retval;
    166 {
    167 	struct linux_sys_wait4_args /* {
    168 		syscallarg(int) pid;
    169 		syscallarg(int *) status;
    170 		syscallarg(int) options;
    171 		syscallarg(struct rusage *) rusage;
    172 	} */ *uap = v;
    173 	struct proc *p = l->l_proc;
    174 	struct sys_wait4_args w4a;
    175 	int error, *status, tstat, options, linux_options;
    176 	caddr_t sg;
    177 
    178 	if (SCARG(uap, status) != NULL) {
    179 		sg = stackgap_init(p->p_emul);
    180 		status = (int *) stackgap_alloc(&sg, sizeof *status);
    181 	} else
    182 		status = NULL;
    183 
    184 	linux_options = SCARG(uap, options);
    185 	options = 0;
    186 	if (linux_options &
    187 	    ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WCLONE))
    188 		return (EINVAL);
    189 
    190 	if (linux_options & LINUX_WAIT4_WNOHANG)
    191 		options |= WNOHANG;
    192 	if (linux_options & LINUX_WAIT4_WUNTRACED)
    193 		options |= WUNTRACED;
    194 	if (linux_options & LINUX_WAIT4_WCLONE)
    195 		options |= WALTSIG;
    196 
    197 	SCARG(&w4a, pid) = SCARG(uap, pid);
    198 	SCARG(&w4a, status) = status;
    199 	SCARG(&w4a, options) = options;
    200 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
    201 
    202 	if ((error = sys_wait4(l, &w4a, retval)))
    203 		return error;
    204 
    205 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
    206 
    207 	if (status != NULL) {
    208 		if ((error = copyin(status, &tstat, sizeof tstat)))
    209 			return error;
    210 
    211 		bsd_to_linux_wstat(&tstat);
    212 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    213 	}
    214 
    215 	return 0;
    216 }
    217 
    218 /*
    219  * Linux brk(2). The check if the new address is >= the old one is
    220  * done in the kernel in Linux. NetBSD does it in the library.
    221  */
    222 int
    223 linux_sys_brk(l, v, retval)
    224 	struct lwp *l;
    225 	void *v;
    226 	register_t *retval;
    227 {
    228 	struct linux_sys_brk_args /* {
    229 		syscallarg(char *) nsize;
    230 	} */ *uap = v;
    231 	struct proc *p = l->l_proc;
    232 	char *nbrk = SCARG(uap, nsize);
    233 	struct sys_obreak_args oba;
    234 	struct vmspace *vm = p->p_vmspace;
    235 	struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
    236 
    237 	SCARG(&oba, nsize) = nbrk;
    238 
    239 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(l, &oba, retval) == 0)
    240 		ed->p_break = (char *)nbrk;
    241 	else
    242 		nbrk = ed->p_break;
    243 
    244 	retval[0] = (register_t)nbrk;
    245 	return 0;
    246 }
    247 
    248 /*
    249  * Convert BSD statfs structure to Linux statfs structure.
    250  * The Linux structure has less fields, and it also wants
    251  * the length of a name in a dir entry in a field, which
    252  * we fake (probably the wrong way).
    253  */
    254 static void
    255 bsd_to_linux_statfs(bsp, lsp)
    256 	struct statfs *bsp;
    257 	struct linux_statfs *lsp;
    258 {
    259 
    260 	lsp->l_ftype = bsp->f_type;
    261 	lsp->l_fbsize = bsp->f_bsize;
    262 	lsp->l_fblocks = bsp->f_blocks;
    263 	lsp->l_fbfree = bsp->f_bfree;
    264 	lsp->l_fbavail = bsp->f_bavail;
    265 	lsp->l_ffiles = bsp->f_files;
    266 	lsp->l_fffree = bsp->f_ffree;
    267 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
    268 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
    269 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
    270 }
    271 
    272 /*
    273  * Implement the fs stat functions. Straightforward.
    274  */
    275 int
    276 linux_sys_statfs(l, v, retval)
    277 	struct lwp *l;
    278 	void *v;
    279 	register_t *retval;
    280 {
    281 	struct linux_sys_statfs_args /* {
    282 		syscallarg(const char *) path;
    283 		syscallarg(struct linux_statfs *) sp;
    284 	} */ *uap = v;
    285 	struct proc *p = l->l_proc;
    286 	struct statfs btmp, *bsp;
    287 	struct linux_statfs ltmp;
    288 	struct sys_statfs_args bsa;
    289 	caddr_t sg;
    290 	int error;
    291 
    292 	sg = stackgap_init(p->p_emul);
    293 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    294 
    295 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    296 
    297 	SCARG(&bsa, path) = SCARG(uap, path);
    298 	SCARG(&bsa, buf) = bsp;
    299 
    300 	if ((error = sys_statfs(l, &bsa, retval)))
    301 		return error;
    302 
    303 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    304 		return error;
    305 
    306 	bsd_to_linux_statfs(&btmp, &ltmp);
    307 
    308 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    309 }
    310 
    311 int
    312 linux_sys_fstatfs(l, v, retval)
    313 	struct lwp *l;
    314 	void *v;
    315 	register_t *retval;
    316 {
    317 	struct linux_sys_fstatfs_args /* {
    318 		syscallarg(int) fd;
    319 		syscallarg(struct linux_statfs *) sp;
    320 	} */ *uap = v;
    321 	struct proc *p = l->l_proc;
    322 	struct statfs btmp, *bsp;
    323 	struct linux_statfs ltmp;
    324 	struct sys_fstatfs_args bsa;
    325 	caddr_t sg;
    326 	int error;
    327 
    328 	sg = stackgap_init(p->p_emul);
    329 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    330 
    331 	SCARG(&bsa, fd) = SCARG(uap, fd);
    332 	SCARG(&bsa, buf) = bsp;
    333 
    334 	if ((error = sys_fstatfs(l, &bsa, retval)))
    335 		return error;
    336 
    337 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    338 		return error;
    339 
    340 	bsd_to_linux_statfs(&btmp, &ltmp);
    341 
    342 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    343 }
    344 
    345 char linux_sysname[] = "Linux";
    346 char linux_release[] = "2.0.38";
    347 char linux_version[] = "#0 Sun Apr 1 11:11:11 MET 2000";
    348 
    349 /*
    350  * uname(). Just copy the info from the various strings stored in the
    351  * kernel, and put it in the Linux utsname structure. That structure
    352  * is almost the same as the NetBSD one, only it has fields 65 characters
    353  * long, and an extra domainname field.
    354  */
    355 int
    356 linux_sys_uname(l, v, retval)
    357 	struct lwp *l;
    358 	void *v;
    359 	register_t *retval;
    360 {
    361 	struct linux_sys_uname_args /* {
    362 		syscallarg(struct linux_utsname *) up;
    363 	} */ *uap = v;
    364 	struct linux_utsname luts;
    365 
    366 	strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    367 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    368 	strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
    369 	strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
    370 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    371 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    372 
    373 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    374 }
    375 
    376 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
    377 /* Used indirectly on: arm, i386, m68k */
    378 
    379 /*
    380  * New type Linux mmap call.
    381  * Only called directly on machines with >= 6 free regs.
    382  */
    383 int
    384 linux_sys_mmap(l, v, retval)
    385 	struct lwp *l;
    386 	void *v;
    387 	register_t *retval;
    388 {
    389 	struct linux_sys_mmap_args /* {
    390 		syscallarg(unsigned long) addr;
    391 		syscallarg(size_t) len;
    392 		syscallarg(int) prot;
    393 		syscallarg(int) flags;
    394 		syscallarg(int) fd;
    395 		syscallarg(off_t) offset;
    396 	} */ *uap = v;
    397 	struct sys_mmap_args cma;
    398 	int flags;
    399 
    400 	flags = 0;
    401 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
    402 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
    403 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
    404 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
    405 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
    406 
    407 	SCARG(&cma,addr) = (void *)SCARG(uap, addr);
    408 	SCARG(&cma,len) = SCARG(uap, len);
    409 	SCARG(&cma,prot) = SCARG(uap, prot);
    410 	if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
    411 		SCARG(&cma,prot) |= VM_PROT_READ;
    412 	SCARG(&cma,flags) = flags;
    413 	SCARG(&cma,fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
    414 	SCARG(&cma,pad) = 0;
    415 	SCARG(&cma,pos) = SCARG(uap, offset);
    416 
    417 	return sys_mmap(l, &cma, retval);
    418 }
    419 
    420 int
    421 linux_sys_mremap(l, v, retval)
    422 	struct lwp *l;
    423 	void *v;
    424 	register_t *retval;
    425 {
    426 	struct linux_sys_mremap_args /* {
    427 		syscallarg(void *) old_address;
    428 		syscallarg(size_t) old_size;
    429 		syscallarg(size_t) new_size;
    430 		syscallarg(u_long) flags;
    431 	} */ *uap = v;
    432 	struct sys_munmap_args mua;
    433 	size_t old_size, new_size;
    434 	int error;
    435 
    436 	old_size = round_page(SCARG(uap, old_size));
    437 	new_size = round_page(SCARG(uap, new_size));
    438 
    439 	/*
    440 	 * Growing mapped region.
    441 	 */
    442 	if (new_size > old_size) {
    443 		/*
    444 		 * XXX Implement me.  What we probably want to do is
    445 		 * XXX dig out the guts of the old mapping, mmap that
    446 		 * XXX object again with the new size, then munmap
    447 		 * XXX the old mapping.
    448 		 */
    449 		*retval = 0;
    450 		return (ENOMEM);
    451 	}
    452 
    453 	/*
    454 	 * Shrinking mapped region.
    455 	 */
    456 	if (new_size < old_size) {
    457 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
    458 		    new_size;
    459 		SCARG(&mua, len) = old_size - new_size;
    460 		error = sys_munmap(l, &mua, retval);
    461 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
    462 		return (error);
    463 	}
    464 
    465 	/*
    466 	 * No change.
    467 	 */
    468 	*retval = (register_t)SCARG(uap, old_address);
    469 	return (0);
    470 }
    471 
    472 int
    473 linux_sys_msync(l, v, retval)
    474 	struct lwp *l;
    475 	void *v;
    476 	register_t *retval;
    477 {
    478 	struct linux_sys_msync_args /* {
    479 		syscallarg(caddr_t) addr;
    480 		syscallarg(int) len;
    481 		syscallarg(int) fl;
    482 	} */ *uap = v;
    483 
    484 	struct sys___msync13_args bma;
    485 
    486 	/* flags are ignored */
    487 	SCARG(&bma, addr) = SCARG(uap, addr);
    488 	SCARG(&bma, len) = SCARG(uap, len);
    489 	SCARG(&bma, flags) = SCARG(uap, fl);
    490 
    491 	return sys___msync13(l, &bma, retval);
    492 }
    493 
    494 /*
    495  * This code is partly stolen from src/lib/libc/compat-43/times.c
    496  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
    497  */
    498 
    499 #define CLK_TCK 100
    500 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
    501 
    502 int
    503 linux_sys_times(l, v, retval)
    504 	struct lwp *l;
    505 	void *v;
    506 	register_t *retval;
    507 {
    508 	struct linux_sys_times_args /* {
    509 		syscallarg(struct times *) tms;
    510 	} */ *uap = v;
    511 	struct proc *p = l->l_proc;
    512 	struct timeval t;
    513 	struct linux_tms ltms;
    514 	struct rusage ru;
    515 	int error, s;
    516 
    517 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    518 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
    519 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
    520 
    521 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    522 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    523 
    524 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    525 		return error;
    526 
    527 	s = splclock();
    528 	timersub(&time, &boottime, &t);
    529 	splx(s);
    530 
    531 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    532 	return 0;
    533 }
    534 
    535 /*
    536  * Linux 'readdir' call. This code is mostly taken from the
    537  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    538  * an attempt has been made to keep it a little cleaner (failing
    539  * miserably, because of the cruft needed if count 1 is passed).
    540  *
    541  * The d_off field should contain the offset of the next valid entry,
    542  * but in Linux it has the offset of the entry itself. We emulate
    543  * that bug here.
    544  *
    545  * Read in BSD-style entries, convert them, and copy them out.
    546  *
    547  * Note that this doesn't handle union-mounted filesystems.
    548  */
    549 int
    550 linux_sys_getdents(l, v, retval)
    551 	struct lwp *l;
    552 	void *v;
    553 	register_t *retval;
    554 {
    555 	struct linux_sys_getdents_args /* {
    556 		syscallarg(int) fd;
    557 		syscallarg(struct linux_dirent *) dent;
    558 		syscallarg(unsigned int) count;
    559 	} */ *uap = v;
    560 	struct proc *p = l->l_proc;
    561 	struct dirent *bdp;
    562 	struct vnode *vp;
    563 	caddr_t	inp, buf;		/* BSD-format */
    564 	int len, reclen;		/* BSD-format */
    565 	caddr_t outp;			/* Linux-format */
    566 	int resid, linux_reclen = 0;	/* Linux-format */
    567 	struct file *fp;
    568 	struct uio auio;
    569 	struct iovec aiov;
    570 	struct linux_dirent idb;
    571 	off_t off;		/* true file offset */
    572 	int buflen, error, eofflag, nbytes, oldcall;
    573 	struct vattr va;
    574 	off_t *cookiebuf = NULL, *cookie;
    575 	int ncookies;
    576 
    577 	/* getvnode() will use the descriptor for us */
    578 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    579 		return (error);
    580 
    581 	if ((fp->f_flag & FREAD) == 0) {
    582 		error = EBADF;
    583 		goto out1;
    584 	}
    585 
    586 	vp = (struct vnode *)fp->f_data;
    587 	if (vp->v_type != VDIR) {
    588 		error = EINVAL;
    589 		goto out1;
    590 	}
    591 
    592 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    593 		goto out1;
    594 
    595 	nbytes = SCARG(uap, count);
    596 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    597 		nbytes = sizeof (struct linux_dirent);
    598 		buflen = max(va.va_blocksize, nbytes);
    599 		oldcall = 1;
    600 	} else {
    601 		buflen = min(MAXBSIZE, nbytes);
    602 		if (buflen < va.va_blocksize)
    603 			buflen = va.va_blocksize;
    604 		oldcall = 0;
    605 	}
    606 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    607 
    608 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    609 	off = fp->f_offset;
    610 again:
    611 	aiov.iov_base = buf;
    612 	aiov.iov_len = buflen;
    613 	auio.uio_iov = &aiov;
    614 	auio.uio_iovcnt = 1;
    615 	auio.uio_rw = UIO_READ;
    616 	auio.uio_segflg = UIO_SYSSPACE;
    617 	auio.uio_procp = p;
    618 	auio.uio_resid = buflen;
    619 	auio.uio_offset = off;
    620 	/*
    621          * First we read into the malloc'ed buffer, then
    622          * we massage it into user space, one record at a time.
    623          */
    624 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    625 	    &ncookies);
    626 	if (error)
    627 		goto out;
    628 
    629 	inp = buf;
    630 	outp = (caddr_t)SCARG(uap, dent);
    631 	resid = nbytes;
    632 	if ((len = buflen - auio.uio_resid) == 0)
    633 		goto eof;
    634 
    635 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    636 		bdp = (struct dirent *)inp;
    637 		reclen = bdp->d_reclen;
    638 		if (reclen & 3)
    639 			panic("linux_readdir");
    640 		if (bdp->d_fileno == 0) {
    641 			inp += reclen;	/* it is a hole; squish it out */
    642 			off = *cookie++;
    643 			continue;
    644 		}
    645 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    646 		if (reclen > len || resid < linux_reclen) {
    647 			/* entry too big for buffer, so just stop */
    648 			outp++;
    649 			break;
    650 		}
    651 		/*
    652 		 * Massage in place to make a Linux-shaped dirent (otherwise
    653 		 * we have to worry about touching user memory outside of
    654 		 * the copyout() call).
    655 		 */
    656 		idb.d_ino = (linux_ino_t)bdp->d_fileno;
    657 		/*
    658 		 * The old readdir() call misuses the offset and reclen fields.
    659 		 */
    660 		if (oldcall) {
    661 			idb.d_off = (linux_off_t)linux_reclen;
    662 			idb.d_reclen = (u_short)bdp->d_namlen;
    663 		} else {
    664 			if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
    665 				compat_offseterr(vp, "linux_getdents");
    666 				error = EINVAL;
    667 				goto out;
    668 			}
    669 			idb.d_off = (linux_off_t)off;
    670 			idb.d_reclen = (u_short)linux_reclen;
    671 		}
    672 		strcpy(idb.d_name, bdp->d_name);
    673 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    674 			goto out;
    675 		/* advance past this real entry */
    676 		inp += reclen;
    677 		off = *cookie++;	/* each entry points to itself */
    678 		/* advance output past Linux-shaped entry */
    679 		outp += linux_reclen;
    680 		resid -= linux_reclen;
    681 		if (oldcall)
    682 			break;
    683 	}
    684 
    685 	/* if we squished out the whole block, try again */
    686 	if (outp == (caddr_t)SCARG(uap, dent))
    687 		goto again;
    688 	fp->f_offset = off;	/* update the vnode offset */
    689 
    690 	if (oldcall)
    691 		nbytes = resid + linux_reclen;
    692 
    693 eof:
    694 	*retval = nbytes - resid;
    695 out:
    696 	VOP_UNLOCK(vp, 0);
    697 	if (cookiebuf)
    698 		free(cookiebuf, M_TEMP);
    699 	free(buf, M_TEMP);
    700  out1:
    701 	FILE_UNUSE(fp, p);
    702 	return error;
    703 }
    704 
    705 /*
    706  * Even when just using registers to pass arguments to syscalls you can
    707  * have 5 of them on the i386. So this newer version of select() does
    708  * this.
    709  */
    710 int
    711 linux_sys_select(l, v, retval)
    712 	struct lwp *l;
    713 	void *v;
    714 	register_t *retval;
    715 {
    716 	struct linux_sys_select_args /* {
    717 		syscallarg(int) nfds;
    718 		syscallarg(fd_set *) readfds;
    719 		syscallarg(fd_set *) writefds;
    720 		syscallarg(fd_set *) exceptfds;
    721 		syscallarg(struct timeval *) timeout;
    722 	} */ *uap = v;
    723 
    724 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    725 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
    726 }
    727 
    728 /*
    729  * Common code for the old and new versions of select(). A couple of
    730  * things are important:
    731  * 1) return the amount of time left in the 'timeout' parameter
    732  * 2) select never returns ERESTART on Linux, always return EINTR
    733  */
    734 int
    735 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
    736 	struct lwp *l;
    737 	register_t *retval;
    738 	int nfds;
    739 	fd_set *readfds, *writefds, *exceptfds;
    740 	struct timeval *timeout;
    741 {
    742 	struct sys_select_args bsa;
    743 	struct proc *p = l->l_proc;
    744 	struct timeval tv0, tv1, utv, *tvp;
    745 	caddr_t sg;
    746 	int error;
    747 
    748 	SCARG(&bsa, nd) = nfds;
    749 	SCARG(&bsa, in) = readfds;
    750 	SCARG(&bsa, ou) = writefds;
    751 	SCARG(&bsa, ex) = exceptfds;
    752 	SCARG(&bsa, tv) = timeout;
    753 
    754 	/*
    755 	 * Store current time for computation of the amount of
    756 	 * time left.
    757 	 */
    758 	if (timeout) {
    759 		if ((error = copyin(timeout, &utv, sizeof(utv))))
    760 			return error;
    761 		if (itimerfix(&utv)) {
    762 			/*
    763 			 * The timeval was invalid.  Convert it to something
    764 			 * valid that will act as it does under Linux.
    765 			 */
    766 			sg = stackgap_init(p->p_emul);
    767 			tvp = stackgap_alloc(&sg, sizeof(utv));
    768 			utv.tv_sec += utv.tv_usec / 1000000;
    769 			utv.tv_usec %= 1000000;
    770 			if (utv.tv_usec < 0) {
    771 				utv.tv_sec -= 1;
    772 				utv.tv_usec += 1000000;
    773 			}
    774 			if (utv.tv_sec < 0)
    775 				timerclear(&utv);
    776 			if ((error = copyout(&utv, tvp, sizeof(utv))))
    777 				return error;
    778 			SCARG(&bsa, tv) = tvp;
    779 		}
    780 		microtime(&tv0);
    781 	}
    782 
    783 	error = sys_select(l, &bsa, retval);
    784 	if (error) {
    785 		/*
    786 		 * See fs/select.c in the Linux kernel.  Without this,
    787 		 * Maelstrom doesn't work.
    788 		 */
    789 		if (error == ERESTART)
    790 			error = EINTR;
    791 		return error;
    792 	}
    793 
    794 	if (timeout) {
    795 		if (*retval) {
    796 			/*
    797 			 * Compute how much time was left of the timeout,
    798 			 * by subtracting the current time and the time
    799 			 * before we started the call, and subtracting
    800 			 * that result from the user-supplied value.
    801 			 */
    802 			microtime(&tv1);
    803 			timersub(&tv1, &tv0, &tv1);
    804 			timersub(&utv, &tv1, &utv);
    805 			if (utv.tv_sec < 0)
    806 				timerclear(&utv);
    807 		} else
    808 			timerclear(&utv);
    809 		if ((error = copyout(&utv, timeout, sizeof(utv))))
    810 			return error;
    811 	}
    812 
    813 	return 0;
    814 }
    815 
    816 /*
    817  * Get the process group of a certain process. Look it up
    818  * and return the value.
    819  */
    820 int
    821 linux_sys_getpgid(l, v, retval)
    822 	struct lwp *l;
    823 	void *v;
    824 	register_t *retval;
    825 {
    826 	struct linux_sys_getpgid_args /* {
    827 		syscallarg(int) pid;
    828 	} */ *uap = v;
    829 	struct proc *p = l->l_proc;
    830 	struct proc *targp;
    831 
    832 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
    833 		if ((targp = pfind(SCARG(uap, pid))) == 0)
    834 			return ESRCH;
    835 	}
    836 	else
    837 		targp = p;
    838 
    839 	retval[0] = targp->p_pgid;
    840 	return 0;
    841 }
    842 
    843 /*
    844  * Set the 'personality' (emulation mode) for the current process. Only
    845  * accept the Linux personality here (0). This call is needed because
    846  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
    847  * ELF binaries run in Linux mode, not SVR4 mode.
    848  */
    849 int
    850 linux_sys_personality(l, v, retval)
    851 	struct lwp *l;
    852 	void *v;
    853 	register_t *retval;
    854 {
    855 	struct linux_sys_personality_args /* {
    856 		syscallarg(int) per;
    857 	} */ *uap = v;
    858 
    859 	if (SCARG(uap, per) != 0)
    860 		return EINVAL;
    861 	retval[0] = 0;
    862 	return 0;
    863 }
    864 
    865 #if defined(__i386__) || defined(__m68k__)
    866 /*
    867  * The calls are here because of type conversions.
    868  */
    869 int
    870 linux_sys_setreuid16(l, v, retval)
    871 	struct lwp *l;
    872 	void *v;
    873 	register_t *retval;
    874 {
    875 	struct linux_sys_setreuid16_args /* {
    876 		syscallarg(int) ruid;
    877 		syscallarg(int) euid;
    878 	} */ *uap = v;
    879 	struct sys_setreuid_args bsa;
    880 
    881 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    882 		(uid_t)-1 : SCARG(uap, ruid);
    883 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    884 		(uid_t)-1 : SCARG(uap, euid);
    885 
    886 	return sys_setreuid(l, &bsa, retval);
    887 }
    888 
    889 int
    890 linux_sys_setregid16(l, v, retval)
    891 	struct lwp *l;
    892 	void *v;
    893 	register_t *retval;
    894 {
    895 	struct linux_sys_setregid16_args /* {
    896 		syscallarg(int) rgid;
    897 		syscallarg(int) egid;
    898 	} */ *uap = v;
    899 	struct sys_setregid_args bsa;
    900 
    901 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    902 		(uid_t)-1 : SCARG(uap, rgid);
    903 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    904 		(uid_t)-1 : SCARG(uap, egid);
    905 
    906 	return sys_setregid(l, &bsa, retval);
    907 }
    908 
    909 int
    910 linux_sys_setresuid16(l, v, retval)
    911 	struct lwp *l;
    912 	void *v;
    913 	register_t *retval;
    914 {
    915 	struct linux_sys_setresuid16_args /* {
    916 		syscallarg(uid_t) ruid;
    917 		syscallarg(uid_t) euid;
    918 		syscallarg(uid_t) suid;
    919 	} */ *uap = v;
    920 	struct linux_sys_setresuid16_args lsa;
    921 
    922 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    923 		(uid_t)-1 : SCARG(uap, ruid);
    924 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    925 		(uid_t)-1 : SCARG(uap, euid);
    926 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
    927 		(uid_t)-1 : SCARG(uap, suid);
    928 
    929 	return linux_sys_setresuid(l, &lsa, retval);
    930 }
    931 
    932 int
    933 linux_sys_setresgid16(l, v, retval)
    934 	struct lwp *l;
    935 	void *v;
    936 	register_t *retval;
    937 {
    938 	struct linux_sys_setresgid16_args /* {
    939 		syscallarg(gid_t) rgid;
    940 		syscallarg(gid_t) egid;
    941 		syscallarg(gid_t) sgid;
    942 	} */ *uap = v;
    943 	struct linux_sys_setresgid16_args lsa;
    944 
    945 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    946 		(gid_t)-1 : SCARG(uap, rgid);
    947 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    948 		(gid_t)-1 : SCARG(uap, egid);
    949 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
    950 		(gid_t)-1 : SCARG(uap, sgid);
    951 
    952 	return linux_sys_setresgid(l, &lsa, retval);
    953 }
    954 
    955 int
    956 linux_sys_getgroups16(l, v, retval)
    957 	struct lwp *l;
    958 	void *v;
    959 	register_t *retval;
    960 {
    961 	struct linux_sys_getgroups16_args /* {
    962 		syscallarg(int) gidsetsize;
    963 		syscallarg(linux_gid_t *) gidset;
    964 	} */ *uap = v;
    965 	struct proc *p = l->l_proc;
    966 	caddr_t sg;
    967 	int n, error, i;
    968 	struct sys_getgroups_args bsa;
    969 	gid_t *bset, *kbset;
    970 	linux_gid_t *lset;
    971 	struct pcred *pc = p->p_cred;
    972 
    973 	n = SCARG(uap, gidsetsize);
    974 	if (n < 0)
    975 		return EINVAL;
    976 	error = 0;
    977 	bset = kbset = NULL;
    978 	lset = NULL;
    979 	if (n > 0) {
    980 		n = min(pc->pc_ucred->cr_ngroups, n);
    981 		sg = stackgap_init(p->p_emul);
    982 		bset = stackgap_alloc(&sg, n * sizeof (gid_t));
    983 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
    984 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
    985 		if (bset == NULL || kbset == NULL || lset == NULL)
    986 			return ENOMEM;
    987 		SCARG(&bsa, gidsetsize) = n;
    988 		SCARG(&bsa, gidset) = bset;
    989 		error = sys_getgroups(l, &bsa, retval);
    990 		if (error != 0)
    991 			goto out;
    992 		error = copyin(bset, kbset, n * sizeof (gid_t));
    993 		if (error != 0)
    994 			goto out;
    995 		for (i = 0; i < n; i++)
    996 			lset[i] = (linux_gid_t)kbset[i];
    997 		error = copyout(lset, SCARG(uap, gidset),
    998 		    n * sizeof (linux_gid_t));
    999 	} else
   1000 		*retval = pc->pc_ucred->cr_ngroups;
   1001 out:
   1002 	if (kbset != NULL)
   1003 		free(kbset, M_TEMP);
   1004 	if (lset != NULL)
   1005 		free(lset, M_TEMP);
   1006 	return error;
   1007 }
   1008 
   1009 int
   1010 linux_sys_setgroups16(l, v, retval)
   1011 	struct lwp *l;
   1012 	void *v;
   1013 	register_t *retval;
   1014 {
   1015 	struct linux_sys_setgroups16_args /* {
   1016 		syscallarg(int) gidsetsize;
   1017 		syscallarg(linux_gid_t *) gidset;
   1018 	} */ *uap = v;
   1019 	struct proc *p = l->l_proc;
   1020 	caddr_t sg;
   1021 	int n;
   1022 	int error, i;
   1023 	struct sys_setgroups_args bsa;
   1024 	gid_t *bset, *kbset;
   1025 	linux_gid_t *lset;
   1026 
   1027 	n = SCARG(uap, gidsetsize);
   1028 	if (n < 0 || n > NGROUPS)
   1029 		return EINVAL;
   1030 	sg = stackgap_init(p->p_emul);
   1031 	bset = stackgap_alloc(&sg, n * sizeof (gid_t));
   1032 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1033 	kbset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1034 	if (lset == NULL || bset == NULL)
   1035 		return ENOMEM;
   1036 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
   1037 	if (error != 0)
   1038 		goto out;
   1039 	for (i = 0; i < n; i++)
   1040 		kbset[i] = (gid_t)lset[i];
   1041 	error = copyout(kbset, bset, n * sizeof (gid_t));
   1042 	if (error != 0)
   1043 		goto out;
   1044 	SCARG(&bsa, gidsetsize) = n;
   1045 	SCARG(&bsa, gidset) = bset;
   1046 	error = sys_setgroups(l, &bsa, retval);
   1047 
   1048 out:
   1049 	if (lset != NULL)
   1050 		free(lset, M_TEMP);
   1051 	if (kbset != NULL)
   1052 		free(kbset, M_TEMP);
   1053 
   1054 	return error;
   1055 }
   1056 
   1057 #endif /* __i386__ || __m68k__ */
   1058 
   1059 /*
   1060  * We have nonexistent fsuid equal to uid.
   1061  * If modification is requested, refuse.
   1062  */
   1063 int
   1064 linux_sys_setfsuid(l, v, retval)
   1065 	 struct lwp *l;
   1066 	 void *v;
   1067 	 register_t *retval;
   1068 {
   1069 	 struct linux_sys_setfsuid_args /* {
   1070 		 syscallarg(uid_t) uid;
   1071 	 } */ *uap = v;
   1072 	 struct proc *p = l->l_proc;
   1073 	 uid_t uid;
   1074 
   1075 	 uid = SCARG(uap, uid);
   1076 	 if (p->p_cred->p_ruid != uid)
   1077 		 return sys_nosys(l, v, retval);
   1078 	 else
   1079 		 return (0);
   1080 }
   1081 
   1082 /* XXX XXX XXX */
   1083 #ifndef alpha
   1084 int
   1085 linux_sys_getfsuid(l, v, retval)
   1086 	struct lwp *l;
   1087 	void *v;
   1088 	register_t *retval;
   1089 {
   1090 	return sys_getuid(l, v, retval);
   1091 }
   1092 #endif
   1093 
   1094 int
   1095 linux_sys___sysctl(l, v, retval)
   1096 	struct lwp *l;
   1097 	void *v;
   1098 	register_t *retval;
   1099 {
   1100 	struct linux_sys___sysctl_args /* {
   1101 		syscallarg(struct linux___sysctl *) lsp;
   1102 	} */ *uap = v;
   1103 	struct linux___sysctl ls;
   1104 	struct sys___sysctl_args bsa;
   1105 	int error;
   1106 
   1107 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
   1108 		return error;
   1109 	SCARG(&bsa, name) = ls.name;
   1110 	SCARG(&bsa, namelen) = ls.namelen;
   1111 	SCARG(&bsa, old) = ls.old;
   1112 	SCARG(&bsa, oldlenp) = ls.oldlenp;
   1113 	SCARG(&bsa, new) = ls.new;
   1114 	SCARG(&bsa, newlen) = ls.newlen;
   1115 
   1116 	return sys___sysctl(l, &bsa, retval);
   1117 }
   1118 
   1119 int
   1120 linux_sys_setresuid(l, v, retval)
   1121 	struct lwp *l;
   1122 	void *v;
   1123 	register_t *retval;
   1124 {
   1125 	struct linux_sys_setresuid_args /* {
   1126 		syscallarg(uid_t) ruid;
   1127 		syscallarg(uid_t) euid;
   1128 		syscallarg(uid_t) suid;
   1129 	} */ *uap = v;
   1130 	struct proc *p = l->l_proc;
   1131 	struct pcred *pc = p->p_cred;
   1132 	uid_t ruid, euid, suid;
   1133 	int error;
   1134 
   1135 	ruid = SCARG(uap, ruid);
   1136 	euid = SCARG(uap, euid);
   1137 	suid = SCARG(uap, suid);
   1138 
   1139 	/*
   1140 	 * Note: These checks are a little different than the NetBSD
   1141 	 * setreuid(2) call performs.  This precisely follows the
   1142 	 * behavior of the Linux kernel.
   1143 	 */
   1144 	if (ruid != (uid_t)-1 &&
   1145 	    ruid != pc->p_ruid &&
   1146 	    ruid != pc->pc_ucred->cr_uid &&
   1147 	    ruid != pc->p_svuid &&
   1148 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1149 		return (error);
   1150 
   1151 	if (euid != (uid_t)-1 &&
   1152 	    euid != pc->p_ruid &&
   1153 	    euid != pc->pc_ucred->cr_uid &&
   1154 	    euid != pc->p_svuid &&
   1155 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1156 		return (error);
   1157 
   1158 	if (suid != (uid_t)-1 &&
   1159 	    suid != pc->p_ruid &&
   1160 	    suid != pc->pc_ucred->cr_uid &&
   1161 	    suid != pc->p_svuid &&
   1162 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1163 		return (error);
   1164 
   1165 	/*
   1166 	 * Now assign the new real, effective, and saved UIDs.
   1167 	 * Note that Linux, unlike NetBSD in setreuid(2), does not
   1168 	 * set the saved UID in this call unless the user specifies
   1169 	 * it.
   1170 	 */
   1171 	if (ruid != (uid_t)-1) {
   1172 		(void)chgproccnt(pc->p_ruid, -1);
   1173 		(void)chgproccnt(ruid, 1);
   1174 		pc->p_ruid = ruid;
   1175 	}
   1176 
   1177 	if (euid != (uid_t)-1) {
   1178 		pc->pc_ucred = crcopy(pc->pc_ucred);
   1179 		pc->pc_ucred->cr_uid = euid;
   1180 	}
   1181 
   1182 	if (suid != (uid_t)-1)
   1183 		pc->p_svuid = suid;
   1184 
   1185 	if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
   1186 		p->p_flag |= P_SUGID;
   1187 	return (0);
   1188 }
   1189 
   1190 int
   1191 linux_sys_getresuid(l, v, retval)
   1192 	struct lwp *l;
   1193 	void *v;
   1194 	register_t *retval;
   1195 {
   1196 	struct linux_sys_getresuid_args /* {
   1197 		syscallarg(uid_t *) ruid;
   1198 		syscallarg(uid_t *) euid;
   1199 		syscallarg(uid_t *) suid;
   1200 	} */ *uap = v;
   1201 	struct proc *p = l->l_proc;
   1202 	struct pcred *pc = p->p_cred;
   1203 	int error;
   1204 
   1205 	/*
   1206 	 * Linux copies these values out to userspace like so:
   1207 	 *
   1208 	 *	1. Copy out ruid.
   1209 	 *	2. If that succeeds, copy out euid.
   1210 	 *	3. If both of those succeed, copy out suid.
   1211 	 */
   1212 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
   1213 			     sizeof(uid_t))) != 0)
   1214 		return (error);
   1215 
   1216 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
   1217 			     sizeof(uid_t))) != 0)
   1218 		return (error);
   1219 
   1220 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
   1221 }
   1222 
   1223 int
   1224 linux_sys_ptrace(l, v, retval)
   1225 	struct lwp *l;
   1226 	void *v;
   1227 	register_t *retval;
   1228 {
   1229 	struct linux_sys_ptrace_args /* {
   1230 		i386, m68k: T=int
   1231 		alpha: T=long
   1232 		syscallarg(T) request;
   1233 		syscallarg(T) pid;
   1234 		syscallarg(T) addr;
   1235 		syscallarg(T) data;
   1236 	} */ *uap = v;
   1237 	struct proc *p = l->l_proc;
   1238 	const int *ptr;
   1239 	int request;
   1240 
   1241 	ptr = linux_ptrace_request_map;
   1242 	request = SCARG(uap, request);
   1243 	while (*ptr != -1)
   1244 		if (*ptr++ == request) {
   1245 			struct sys_ptrace_args pta;
   1246 			caddr_t sg;
   1247 
   1248 			sg = stackgap_init(p->p_emul);
   1249 
   1250 			SCARG(&pta, req) = *ptr;
   1251 			SCARG(&pta, pid) = SCARG(uap, pid);
   1252 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
   1253 			SCARG(&pta, data) = SCARG(uap, data);
   1254 
   1255 			/*
   1256 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1257 			 * to continue as the process left off previously,
   1258 			 * i.e. same as if NetBSD ptrace called with
   1259 			 * addr == (caddr_t) 1.
   1260 			 */
   1261 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1262 				SCARG(&pta, addr) = (caddr_t) 1;
   1263 
   1264 			return sys_ptrace(l, &pta, retval);
   1265 		}
   1266 		else
   1267 			ptr++;
   1268 
   1269 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1270 }
   1271 
   1272 int
   1273 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
   1274 {
   1275 	struct linux_sys_reboot_args /* {
   1276 		syscallarg(int) magic1;
   1277 		syscallarg(int) magic2;
   1278 		syscallarg(int) cmd;
   1279 		syscallarg(void *) arg;
   1280 	} */ *uap = v;
   1281 	struct sys_reboot_args /* {
   1282 		syscallarg(int) opt;
   1283 		syscallarg(char *) bootstr;
   1284 	} */ sra;
   1285 	struct proc *p = l->l_proc;
   1286 	int error;
   1287 
   1288 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1289 		return(error);
   1290 
   1291 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1292 		return(EINVAL);
   1293 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1294 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1295 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1296 		return(EINVAL);
   1297 
   1298 	switch (SCARG(uap, cmd)) {
   1299 	case LINUX_REBOOT_CMD_RESTART:
   1300 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1301 		break;
   1302 	case LINUX_REBOOT_CMD_HALT:
   1303 		SCARG(&sra, opt) = RB_HALT;
   1304 		break;
   1305 	case LINUX_REBOOT_CMD_POWER_OFF:
   1306 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1307 		break;
   1308 	case LINUX_REBOOT_CMD_RESTART2:
   1309 		/* Reboot with an argument. */
   1310 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1311 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1312 		break;
   1313 	case LINUX_REBOOT_CMD_CAD_ON:
   1314 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1315 	case LINUX_REBOOT_CMD_CAD_OFF:
   1316 		return(0);
   1317 	default:
   1318 		return(EINVAL);
   1319 	}
   1320 
   1321 	return(sys_reboot(l, &sra, retval));
   1322 }
   1323 
   1324 /*
   1325  * Copy of compat_12_sys_swapon().
   1326  */
   1327 int
   1328 linux_sys_swapon(l, v, retval)
   1329 	struct lwp *l;
   1330 	void *v;
   1331 	register_t *retval;
   1332 {
   1333 	struct sys_swapctl_args ua;
   1334 	struct linux_sys_swapon_args /* {
   1335 		syscallarg(const char *) name;
   1336 	} */ *uap = v;
   1337 
   1338 	SCARG(&ua, cmd) = SWAP_ON;
   1339 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
   1340 	SCARG(&ua, misc) = 0;	/* priority */
   1341 	return (sys_swapctl(l, &ua, retval));
   1342 }
   1343 
   1344 /*
   1345  * Stop swapping to the file or block device specified by path.
   1346  */
   1347 int
   1348 linux_sys_swapoff(l, v, retval)
   1349 	struct lwp *l;
   1350 	void *v;
   1351 	register_t *retval;
   1352 {
   1353 	struct sys_swapctl_args ua;
   1354 	struct linux_sys_swapoff_args /* {
   1355 		syscallarg(const char *) path;
   1356 	} */ *uap = v;
   1357 
   1358 	SCARG(&ua, cmd) = SWAP_OFF;
   1359 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
   1360 	return (sys_swapctl(l, &ua, retval));
   1361 }
   1362 
   1363 /*
   1364  * Copy of compat_09_sys_setdomainname()
   1365  */
   1366 /* ARGSUSED */
   1367 int
   1368 linux_sys_setdomainname(l, v, retval)
   1369 	struct lwp *l;
   1370 	void *v;
   1371 	register_t *retval;
   1372 {
   1373 	struct linux_sys_setdomainname_args /* {
   1374 		syscallarg(char *) domainname;
   1375 		syscallarg(int) len;
   1376 	} */ *uap = v;
   1377 	struct proc *p = l->l_proc;
   1378 	int name;
   1379 	int error;
   1380 
   1381 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1382 		return (error);
   1383 	name = KERN_DOMAINNAME;
   1384 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
   1385 			    SCARG(uap, len), p));
   1386 }
   1387 
   1388 /*
   1389  * sysinfo()
   1390  */
   1391 /* ARGSUSED */
   1392 int
   1393 linux_sys_sysinfo(l, v, retval)
   1394 	struct lwp *l;
   1395 	void *v;
   1396 	register_t *retval;
   1397 {
   1398 	struct linux_sys_sysinfo_args /* {
   1399 		syscallarg(struct linux_sysinfo *) arg;
   1400 	} */ *uap = v;
   1401 	struct linux_sysinfo si;
   1402 	struct loadavg *la;
   1403 
   1404 	si.uptime = time.tv_sec - boottime.tv_sec;
   1405 	la = &averunnable;
   1406 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1407 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1408 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1409 	si.totalram = ctob(physmem);
   1410 	si.freeram = uvmexp.free * uvmexp.pagesize;
   1411 	si.sharedram = 0;	/* XXX */
   1412 	si.bufferram = uvmexp.vnodepages * uvmexp.pagesize;
   1413 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
   1414 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1415 	si.procs = nprocs;
   1416 
   1417 	/* The following are only present in newer Linux kernels. */
   1418 	si.totalbig = 0;
   1419 	si.freebig = 0;
   1420 	si.mem_unit = 1;
   1421 
   1422 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1423 }
   1424 
   1425 /*
   1426  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1427  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1428  * This is the way Linux does it and glibc depends on this behaviour.
   1429  */
   1430 int
   1431 linux_sys_nosys(l, v, retval)
   1432 	struct lwp *l;
   1433 	void *v;
   1434 	register_t *retval;
   1435 {
   1436 	return (ENOSYS);
   1437 }
   1438