Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.96
      1 /*	$NetBSD: linux_misc.c,v 1.96 2001/12/09 03:07:43 chs 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/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.96 2001/12/09 03:07:43 chs Exp $");
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/namei.h>
     72 #include <sys/proc.h>
     73 #include <sys/dirent.h>
     74 #include <sys/file.h>
     75 #include <sys/stat.h>
     76 #include <sys/filedesc.h>
     77 #include <sys/ioctl.h>
     78 #include <sys/kernel.h>
     79 #include <sys/malloc.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/mman.h>
     82 #include <sys/mount.h>
     83 #include <sys/reboot.h>
     84 #include <sys/resource.h>
     85 #include <sys/resourcevar.h>
     86 #include <sys/signal.h>
     87 #include <sys/signalvar.h>
     88 #include <sys/socket.h>
     89 #include <sys/time.h>
     90 #include <sys/times.h>
     91 #include <sys/vnode.h>
     92 #include <sys/uio.h>
     93 #include <sys/wait.h>
     94 #include <sys/utsname.h>
     95 #include <sys/unistd.h>
     96 #include <sys/swap.h>		/* for SWAP_ON */
     97 #include <sys/sysctl.h>		/* for KERN_DOMAINNAME */
     98 
     99 #include <sys/ptrace.h>
    100 #include <machine/ptrace.h>
    101 
    102 #include <sys/syscallargs.h>
    103 
    104 #include <compat/linux/common/linux_types.h>
    105 #include <compat/linux/common/linux_signal.h>
    106 
    107 #include <compat/linux/linux_syscallargs.h>
    108 
    109 #include <compat/linux/common/linux_fcntl.h>
    110 #include <compat/linux/common/linux_mmap.h>
    111 #include <compat/linux/common/linux_dirent.h>
    112 #include <compat/linux/common/linux_util.h>
    113 #include <compat/linux/common/linux_misc.h>
    114 #include <compat/linux/common/linux_ptrace.h>
    115 #include <compat/linux/common/linux_reboot.h>
    116 #include <compat/linux/common/linux_emuldata.h>
    117 
    118 const int linux_ptrace_request_map[] = {
    119 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
    120 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
    121 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
    122 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
    123 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
    124 	LINUX_PTRACE_CONT,	PT_CONTINUE,
    125 	LINUX_PTRACE_KILL,	PT_KILL,
    126 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
    127 	LINUX_PTRACE_DETACH,	PT_DETACH,
    128 #ifdef PT_STEP
    129 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
    130 #endif
    131 	-1
    132 };
    133 
    134 /* Local linux_misc.c functions: */
    135 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
    136 
    137 /*
    138  * The information on a terminated (or stopped) process needs
    139  * to be converted in order for Linux binaries to get a valid signal
    140  * number out of it.
    141  */
    142 void
    143 bsd_to_linux_wstat(st)
    144 	int *st;
    145 {
    146 
    147 	int sig;
    148 
    149 	if (WIFSIGNALED(*st)) {
    150 		sig = WTERMSIG(*st);
    151 		if (sig >= 0 && sig < NSIG)
    152 			*st= (*st& ~0177) | native_to_linux_sig[sig];
    153 	} else if (WIFSTOPPED(*st)) {
    154 		sig = WSTOPSIG(*st);
    155 		if (sig >= 0 && sig < NSIG)
    156 			*st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
    157 	}
    158 }
    159 
    160 /*
    161  * This is very much the same as waitpid()
    162  */
    163 int
    164 linux_sys_wait4(p, v, retval)
    165 	struct proc *p;
    166 	void *v;
    167 	register_t *retval;
    168 {
    169 	struct linux_sys_wait4_args /* {
    170 		syscallarg(int) pid;
    171 		syscallarg(int *) status;
    172 		syscallarg(int) options;
    173 		syscallarg(struct rusage *) rusage;
    174 	} */ *uap = v;
    175 	struct sys_wait4_args w4a;
    176 	int error, *status, tstat, options, linux_options;
    177 	caddr_t sg;
    178 
    179 	if (SCARG(uap, status) != NULL) {
    180 		sg = stackgap_init(p->p_emul);
    181 		status = (int *) stackgap_alloc(&sg, sizeof *status);
    182 	} else
    183 		status = NULL;
    184 
    185 	linux_options = SCARG(uap, options);
    186 	options = 0;
    187 	if (linux_options &
    188 	    ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WALL|
    189 	      LINUX_WAIT4_WCLONE))
    190 		return (EINVAL);
    191 
    192 	if (linux_options & LINUX_WAIT4_WNOHANG)
    193 		options |= WNOHANG;
    194 	if (linux_options & LINUX_WAIT4_WUNTRACED)
    195 		options |= WUNTRACED;
    196 	if (linux_options & LINUX_WAIT4_WALL)
    197 		options |= WALLSIG;
    198 	if (linux_options & LINUX_WAIT4_WCLONE)
    199 		options |= WALTSIG;
    200 
    201 	SCARG(&w4a, pid) = SCARG(uap, pid);
    202 	SCARG(&w4a, status) = status;
    203 	SCARG(&w4a, options) = options;
    204 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
    205 
    206 	if ((error = sys_wait4(p, &w4a, retval)))
    207 		return error;
    208 
    209 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
    210 
    211 	if (status != NULL) {
    212 		if ((error = copyin(status, &tstat, sizeof tstat)))
    213 			return error;
    214 
    215 		bsd_to_linux_wstat(&tstat);
    216 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    217 	}
    218 
    219 	return 0;
    220 }
    221 
    222 /*
    223  * Linux brk(2). The check if the new address is >= the old one is
    224  * done in the kernel in Linux. NetBSD does it in the library.
    225  */
    226 int
    227 linux_sys_brk(p, v, retval)
    228 	struct proc *p;
    229 	void *v;
    230 	register_t *retval;
    231 {
    232 	struct linux_sys_brk_args /* {
    233 		syscallarg(char *) nsize;
    234 	} */ *uap = v;
    235 	char *nbrk = SCARG(uap, nsize);
    236 	struct sys_obreak_args oba;
    237 	struct vmspace *vm = p->p_vmspace;
    238 	struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
    239 
    240 	SCARG(&oba, nsize) = nbrk;
    241 
    242 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
    243 		ed->p_break = (char*)nbrk;
    244 	else
    245 		nbrk = ed->p_break;
    246 
    247 	retval[0] = (register_t)nbrk;
    248 
    249 	return 0;
    250 }
    251 
    252 /*
    253  * Convert BSD statfs structure to Linux statfs structure.
    254  * The Linux structure has less fields, and it also wants
    255  * the length of a name in a dir entry in a field, which
    256  * we fake (probably the wrong way).
    257  */
    258 static void
    259 bsd_to_linux_statfs(bsp, lsp)
    260 	struct statfs *bsp;
    261 	struct linux_statfs *lsp;
    262 {
    263 
    264 	lsp->l_ftype = bsp->f_type;
    265 	lsp->l_fbsize = bsp->f_bsize;
    266 	lsp->l_fblocks = bsp->f_blocks;
    267 	lsp->l_fbfree = bsp->f_bfree;
    268 	lsp->l_fbavail = bsp->f_bavail;
    269 	lsp->l_ffiles = bsp->f_files;
    270 	lsp->l_fffree = bsp->f_ffree;
    271 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
    272 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
    273 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
    274 }
    275 
    276 /*
    277  * Implement the fs stat functions. Straightforward.
    278  */
    279 int
    280 linux_sys_statfs(p, v, retval)
    281 	struct proc *p;
    282 	void *v;
    283 	register_t *retval;
    284 {
    285 	struct linux_sys_statfs_args /* {
    286 		syscallarg(const char *) path;
    287 		syscallarg(struct linux_statfs *) sp;
    288 	} */ *uap = v;
    289 	struct statfs btmp, *bsp;
    290 	struct linux_statfs ltmp;
    291 	struct sys_statfs_args bsa;
    292 	caddr_t sg;
    293 	int error;
    294 
    295 	sg = stackgap_init(p->p_emul);
    296 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    297 
    298 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    299 
    300 	SCARG(&bsa, path) = SCARG(uap, path);
    301 	SCARG(&bsa, buf) = bsp;
    302 
    303 	if ((error = sys_statfs(p, &bsa, retval)))
    304 		return error;
    305 
    306 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    307 		return error;
    308 
    309 	bsd_to_linux_statfs(&btmp, &ltmp);
    310 
    311 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    312 }
    313 
    314 int
    315 linux_sys_fstatfs(p, v, retval)
    316 	struct proc *p;
    317 	void *v;
    318 	register_t *retval;
    319 {
    320 	struct linux_sys_fstatfs_args /* {
    321 		syscallarg(int) fd;
    322 		syscallarg(struct linux_statfs *) sp;
    323 	} */ *uap = v;
    324 	struct statfs btmp, *bsp;
    325 	struct linux_statfs ltmp;
    326 	struct sys_fstatfs_args bsa;
    327 	caddr_t sg;
    328 	int error;
    329 
    330 	sg = stackgap_init(p->p_emul);
    331 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    332 
    333 	SCARG(&bsa, fd) = SCARG(uap, fd);
    334 	SCARG(&bsa, buf) = bsp;
    335 
    336 	if ((error = sys_fstatfs(p, &bsa, retval)))
    337 		return error;
    338 
    339 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    340 		return error;
    341 
    342 	bsd_to_linux_statfs(&btmp, &ltmp);
    343 
    344 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    345 }
    346 
    347 char linux_sysname[] = "Linux";
    348 char linux_release[] = "2.0.38";
    349 char linux_version[] = "#0 Sun Apr 1 11:11:11 MET 2000";
    350 
    351 /*
    352  * uname(). Just copy the info from the various strings stored in the
    353  * kernel, and put it in the Linux utsname structure. That structure
    354  * is almost the same as the NetBSD one, only it has fields 65 characters
    355  * long, and an extra domainname field.
    356  */
    357 int
    358 linux_sys_uname(p, v, retval)
    359 	struct proc *p;
    360 	void *v;
    361 	register_t *retval;
    362 {
    363 	struct linux_sys_uname_args /* {
    364 		syscallarg(struct linux_utsname *) up;
    365 	} */ *uap = v;
    366 	struct linux_utsname luts;
    367 
    368 	strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    369 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    370 	strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
    371 	strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
    372 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    373 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    374 
    375 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    376 }
    377 
    378 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
    379 /* Used indirectly on: arm, i386, m68k */
    380 
    381 /*
    382  * New type Linux mmap call.
    383  * Only called directly on machines with >= 6 free regs.
    384  */
    385 int
    386 linux_sys_mmap(p, v, retval)
    387 	struct proc *p;
    388 	void *v;
    389 	register_t *retval;
    390 {
    391 	struct linux_sys_mmap_args /* {
    392 		syscallarg(unsigned long) addr;
    393 		syscallarg(size_t) len;
    394 		syscallarg(int) prot;
    395 		syscallarg(int) flags;
    396 		syscallarg(int) fd;
    397 		syscallarg(linux_off_t) offset;
    398 	} */ *uap = v;
    399 	struct sys_mmap_args cma;
    400 	int flags;
    401 
    402 	flags = 0;
    403 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
    404 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
    405 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
    406 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
    407 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
    408 
    409 	SCARG(&cma,addr) = (void *)SCARG(uap, addr);
    410 	SCARG(&cma,len) = SCARG(uap, len);
    411 	SCARG(&cma,prot) = SCARG(uap, prot);
    412 	if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
    413 		SCARG(&cma,prot) |= VM_PROT_READ;
    414 	SCARG(&cma,flags) = flags;
    415 	SCARG(&cma,fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
    416 	SCARG(&cma,pad) = 0;
    417 	SCARG(&cma,pos) = (off_t)SCARG(uap, offset);
    418 
    419 	return sys_mmap(p, &cma, retval);
    420 }
    421 
    422 int
    423 linux_sys_mremap(p, v, retval)
    424 	struct proc *p;
    425 	void *v;
    426 	register_t *retval;
    427 {
    428 	struct linux_sys_mremap_args /* {
    429 		syscallarg(void *) old_address;
    430 		syscallarg(size_t) old_size;
    431 		syscallarg(size_t) new_size;
    432 		syscallarg(u_long) flags;
    433 	} */ *uap = v;
    434 	struct sys_munmap_args mua;
    435 	size_t old_size, new_size;
    436 	int error;
    437 
    438 	old_size = round_page(SCARG(uap, old_size));
    439 	new_size = round_page(SCARG(uap, new_size));
    440 
    441 	/*
    442 	 * Growing mapped region.
    443 	 */
    444 	if (new_size > old_size) {
    445 		/*
    446 		 * XXX Implement me.  What we probably want to do is
    447 		 * XXX dig out the guts of the old mapping, mmap that
    448 		 * XXX object again with the new size, then munmap
    449 		 * XXX the old mapping.
    450 		 */
    451 		*retval = 0;
    452 		return (ENOMEM);
    453 	}
    454 
    455 	/*
    456 	 * Shrinking mapped region.
    457 	 */
    458 	if (new_size < old_size) {
    459 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
    460 		    new_size;
    461 		SCARG(&mua, len) = old_size - new_size;
    462 		error = sys_munmap(p, &mua, retval);
    463 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
    464 		return (error);
    465 	}
    466 
    467 	/*
    468 	 * No change.
    469 	 */
    470 	*retval = (register_t)SCARG(uap, old_address);
    471 	return (0);
    472 }
    473 
    474 int
    475 linux_sys_msync(p, v, retval)
    476 	struct proc *p;
    477 	void *v;
    478 	register_t *retval;
    479 {
    480 	struct linux_sys_msync_args /* {
    481 		syscallarg(caddr_t) addr;
    482 		syscallarg(int) len;
    483 		syscallarg(int) fl;
    484 	} */ *uap = v;
    485 
    486 	struct sys___msync13_args bma;
    487 
    488 	/* flags are ignored */
    489 	SCARG(&bma, addr) = SCARG(uap, addr);
    490 	SCARG(&bma, len) = SCARG(uap, len);
    491 	SCARG(&bma, flags) = SCARG(uap, fl);
    492 
    493 	return sys___msync13(p, &bma, retval);
    494 }
    495 
    496 /*
    497  * This code is partly stolen from src/lib/libc/compat-43/times.c
    498  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
    499  */
    500 
    501 #define CLK_TCK 100
    502 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
    503 
    504 int
    505 linux_sys_times(p, v, retval)
    506 	struct proc *p;
    507 	void *v;
    508 	register_t *retval;
    509 {
    510 	struct linux_sys_times_args /* {
    511 		syscallarg(struct times *) tms;
    512 	} */ *uap = v;
    513 	struct timeval t;
    514 	struct linux_tms ltms;
    515 	struct rusage ru;
    516 	int error, s;
    517 
    518 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    519 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
    520 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
    521 
    522 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    523 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    524 
    525 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    526 		return error;
    527 
    528 	s = splclock();
    529 	timersub(&time, &boottime, &t);
    530 	splx(s);
    531 
    532 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    533 	return 0;
    534 }
    535 
    536 /*
    537  * Linux 'readdir' call. This code is mostly taken from the
    538  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    539  * an attempt has been made to keep it a little cleaner (failing
    540  * miserably, because of the cruft needed if count 1 is passed).
    541  *
    542  * The d_off field should contain the offset of the next valid entry,
    543  * but in Linux it has the offset of the entry itself. We emulate
    544  * that bug here.
    545  *
    546  * Read in BSD-style entries, convert them, and copy them out.
    547  *
    548  * Note that this doesn't handle union-mounted filesystems.
    549  */
    550 int
    551 linux_sys_getdents(p, v, retval)
    552 	struct proc *p;
    553 	void *v;
    554 	register_t *retval;
    555 {
    556 	struct linux_sys_getdents_args /* {
    557 		syscallarg(int) fd;
    558 		syscallarg(struct linux_dirent *) dent;
    559 		syscallarg(unsigned int) count;
    560 	} */ *uap = v;
    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(p, v, retval)
    712 	struct proc *p;
    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(p, 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(p, retval, nfds, readfds, writefds, exceptfds, timeout)
    736 	struct proc *p;
    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 timeval tv0, tv1, utv, *tvp;
    744 	caddr_t sg;
    745 	int error;
    746 
    747 	SCARG(&bsa, nd) = nfds;
    748 	SCARG(&bsa, in) = readfds;
    749 	SCARG(&bsa, ou) = writefds;
    750 	SCARG(&bsa, ex) = exceptfds;
    751 	SCARG(&bsa, tv) = timeout;
    752 
    753 	/*
    754 	 * Store current time for computation of the amount of
    755 	 * time left.
    756 	 */
    757 	if (timeout) {
    758 		if ((error = copyin(timeout, &utv, sizeof(utv))))
    759 			return error;
    760 		if (itimerfix(&utv)) {
    761 			/*
    762 			 * The timeval was invalid.  Convert it to something
    763 			 * valid that will act as it does under Linux.
    764 			 */
    765 			sg = stackgap_init(p->p_emul);
    766 			tvp = stackgap_alloc(&sg, sizeof(utv));
    767 			utv.tv_sec += utv.tv_usec / 1000000;
    768 			utv.tv_usec %= 1000000;
    769 			if (utv.tv_usec < 0) {
    770 				utv.tv_sec -= 1;
    771 				utv.tv_usec += 1000000;
    772 			}
    773 			if (utv.tv_sec < 0)
    774 				timerclear(&utv);
    775 			if ((error = copyout(&utv, tvp, sizeof(utv))))
    776 				return error;
    777 			SCARG(&bsa, tv) = tvp;
    778 		}
    779 		microtime(&tv0);
    780 	}
    781 
    782 	error = sys_select(p, &bsa, retval);
    783 	if (error) {
    784 		/*
    785 		 * See fs/select.c in the Linux kernel.  Without this,
    786 		 * Maelstrom doesn't work.
    787 		 */
    788 		if (error == ERESTART)
    789 			error = EINTR;
    790 		return error;
    791 	}
    792 
    793 	if (timeout) {
    794 		if (*retval) {
    795 			/*
    796 			 * Compute how much time was left of the timeout,
    797 			 * by subtracting the current time and the time
    798 			 * before we started the call, and subtracting
    799 			 * that result from the user-supplied value.
    800 			 */
    801 			microtime(&tv1);
    802 			timersub(&tv1, &tv0, &tv1);
    803 			timersub(&utv, &tv1, &utv);
    804 			if (utv.tv_sec < 0)
    805 				timerclear(&utv);
    806 		} else
    807 			timerclear(&utv);
    808 		if ((error = copyout(&utv, timeout, sizeof(utv))))
    809 			return error;
    810 	}
    811 
    812 	return 0;
    813 }
    814 
    815 /*
    816  * Get the process group of a certain process. Look it up
    817  * and return the value.
    818  */
    819 int
    820 linux_sys_getpgid(p, v, retval)
    821 	struct proc *p;
    822 	void *v;
    823 	register_t *retval;
    824 {
    825 	struct linux_sys_getpgid_args /* {
    826 		syscallarg(int) pid;
    827 	} */ *uap = v;
    828 	struct proc *targp;
    829 
    830 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
    831 		if ((targp = pfind(SCARG(uap, pid))) == 0)
    832 			return ESRCH;
    833 	}
    834 	else
    835 		targp = p;
    836 
    837 	retval[0] = targp->p_pgid;
    838 	return 0;
    839 }
    840 
    841 /*
    842  * Set the 'personality' (emulation mode) for the current process. Only
    843  * accept the Linux personality here (0). This call is needed because
    844  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
    845  * ELF binaries run in Linux mode, not SVR4 mode.
    846  */
    847 int
    848 linux_sys_personality(p, v, retval)
    849 	struct proc *p;
    850 	void *v;
    851 	register_t *retval;
    852 {
    853 	struct linux_sys_personality_args /* {
    854 		syscallarg(int) per;
    855 	} */ *uap = v;
    856 
    857 	if (SCARG(uap, per) != 0)
    858 		return EINVAL;
    859 	retval[0] = 0;
    860 	return 0;
    861 }
    862 
    863 #if defined(__i386__) || defined(__m68k__)
    864 /*
    865  * The calls are here because of type conversions.
    866  */
    867 int
    868 linux_sys_setreuid16(p, v, retval)
    869 	struct proc *p;
    870 	void *v;
    871 	register_t *retval;
    872 {
    873 	struct linux_sys_setreuid16_args /* {
    874 		syscallarg(int) ruid;
    875 		syscallarg(int) euid;
    876 	} */ *uap = v;
    877 	struct sys_setreuid_args bsa;
    878 
    879 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    880 		(uid_t)-1 : SCARG(uap, ruid);
    881 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    882 		(uid_t)-1 : SCARG(uap, euid);
    883 
    884 	return sys_setreuid(p, &bsa, retval);
    885 }
    886 
    887 int
    888 linux_sys_setregid16(p, v, retval)
    889 	struct proc *p;
    890 	void *v;
    891 	register_t *retval;
    892 {
    893 	struct linux_sys_setregid16_args /* {
    894 		syscallarg(int) rgid;
    895 		syscallarg(int) egid;
    896 	} */ *uap = v;
    897 	struct sys_setregid_args bsa;
    898 
    899 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    900 		(uid_t)-1 : SCARG(uap, rgid);
    901 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    902 		(uid_t)-1 : SCARG(uap, egid);
    903 
    904 	return sys_setregid(p, &bsa, retval);
    905 }
    906 
    907 int
    908 linux_sys_setresuid16(p, v, retval)
    909 	struct proc *p;
    910 	void *v;
    911 	register_t *retval;
    912 {
    913 	struct linux_sys_setresuid16_args /* {
    914 		syscallarg(uid_t) ruid;
    915 		syscallarg(uid_t) euid;
    916 		syscallarg(uid_t) suid;
    917 	} */ *uap = v;
    918 	struct linux_sys_setresuid16_args lsa;
    919 
    920 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    921 		(uid_t)-1 : SCARG(uap, ruid);
    922 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    923 		(uid_t)-1 : SCARG(uap, euid);
    924 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
    925 		(uid_t)-1 : SCARG(uap, suid);
    926 
    927 	return linux_sys_setresuid(p, &lsa, retval);
    928 }
    929 
    930 int
    931 linux_sys_setresgid16(p, v, retval)
    932 	struct proc *p;
    933 	void *v;
    934 	register_t *retval;
    935 {
    936 	struct linux_sys_setresgid16_args /* {
    937 		syscallarg(gid_t) rgid;
    938 		syscallarg(gid_t) egid;
    939 		syscallarg(gid_t) sgid;
    940 	} */ *uap = v;
    941 	struct linux_sys_setresgid16_args lsa;
    942 
    943 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    944 		(gid_t)-1 : SCARG(uap, rgid);
    945 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    946 		(gid_t)-1 : SCARG(uap, egid);
    947 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
    948 		(gid_t)-1 : SCARG(uap, sgid);
    949 
    950 	return linux_sys_setresgid(p, &lsa, retval);
    951 }
    952 
    953 int
    954 linux_sys_getgroups16(p, v, retval)
    955 	struct proc *p;
    956 	void *v;
    957 	register_t *retval;
    958 {
    959 	struct linux_sys_getgroups16_args /* {
    960 		syscallarg(int) gidsetsize;
    961 		syscallarg(linux_gid_t *) gidset;
    962 	} */ *uap = v;
    963 	caddr_t sg;
    964 	int n, error, i;
    965 	struct sys_getgroups_args bsa;
    966 	gid_t *bset, *kbset;
    967 	linux_gid_t *lset;
    968 	struct pcred *pc = p->p_cred;
    969 
    970 	n = SCARG(uap, gidsetsize);
    971 	if (n < 0)
    972 		return EINVAL;
    973 	error = 0;
    974 	bset = kbset = NULL;
    975 	lset = NULL;
    976 	if (n > 0) {
    977 		n = min(pc->pc_ucred->cr_ngroups, n);
    978 		sg = stackgap_init(p->p_emul);
    979 		bset = stackgap_alloc(&sg, n * sizeof (gid_t));
    980 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
    981 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
    982 		if (bset == NULL || kbset == NULL || lset == NULL)
    983 			return ENOMEM;
    984 		SCARG(&bsa, gidsetsize) = n;
    985 		SCARG(&bsa, gidset) = bset;
    986 		error = sys_getgroups(p, &bsa, retval);
    987 		if (error != 0)
    988 			goto out;
    989 		error = copyin(bset, kbset, n * sizeof (gid_t));
    990 		if (error != 0)
    991 			goto out;
    992 		for (i = 0; i < n; i++)
    993 			lset[i] = (linux_gid_t)kbset[i];
    994 		error = copyout(lset, SCARG(uap, gidset),
    995 		    n * sizeof (linux_gid_t));
    996 	} else
    997 		*retval = pc->pc_ucred->cr_ngroups;
    998 out:
    999 	if (kbset != NULL)
   1000 		free(kbset, M_TEMP);
   1001 	if (lset != NULL)
   1002 		free(lset, M_TEMP);
   1003 	return error;
   1004 }
   1005 
   1006 int
   1007 linux_sys_setgroups16(p, v, retval)
   1008 	struct proc *p;
   1009 	void *v;
   1010 	register_t *retval;
   1011 {
   1012 	struct linux_sys_setgroups16_args /* {
   1013 		syscallarg(int) gidsetsize;
   1014 		syscallarg(linux_gid_t *) gidset;
   1015 	} */ *uap = v;
   1016 	caddr_t sg;
   1017 	int n;
   1018 	int error, i;
   1019 	struct sys_setgroups_args bsa;
   1020 	gid_t *bset, *kbset;
   1021 	linux_gid_t *lset;
   1022 
   1023 	n = SCARG(uap, gidsetsize);
   1024 	if (n < 0 || n > NGROUPS)
   1025 		return EINVAL;
   1026 	sg = stackgap_init(p->p_emul);
   1027 	bset = stackgap_alloc(&sg, n * sizeof (gid_t));
   1028 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1029 	kbset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1030 	if (lset == NULL || bset == NULL)
   1031 		return ENOMEM;
   1032 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
   1033 	if (error != 0)
   1034 		goto out;
   1035 	for (i = 0; i < n; i++)
   1036 		kbset[i] = (gid_t)lset[i];
   1037 	error = copyout(kbset, bset, n * sizeof (gid_t));
   1038 	if (error != 0)
   1039 		goto out;
   1040 	SCARG(&bsa, gidsetsize) = n;
   1041 	SCARG(&bsa, gidset) = bset;
   1042 	error = sys_setgroups(p, &bsa, retval);
   1043 
   1044 out:
   1045 	if (lset != NULL)
   1046 		free(lset, M_TEMP);
   1047 	if (kbset != NULL)
   1048 		free(kbset, M_TEMP);
   1049 
   1050 	return error;
   1051 }
   1052 
   1053 #endif /* __i386__ || __m68k__ */
   1054 
   1055 /*
   1056  * We have nonexistent fsuid equal to uid.
   1057  * If modification is requested, refuse.
   1058  */
   1059 int
   1060 linux_sys_setfsuid(p, v, retval)
   1061 	 struct proc *p;
   1062 	 void *v;
   1063 	 register_t *retval;
   1064 {
   1065 	 struct linux_sys_setfsuid_args /* {
   1066 		 syscallarg(uid_t) uid;
   1067 	 } */ *uap = v;
   1068 	 uid_t uid;
   1069 
   1070 	 uid = SCARG(uap, uid);
   1071 	 if (p->p_cred->p_ruid != uid)
   1072 		 return sys_nosys(p, v, retval);
   1073 	 else
   1074 		 return (0);
   1075 }
   1076 
   1077 /* XXX XXX XXX */
   1078 #ifndef alpha
   1079 int
   1080 linux_sys_getfsuid(p, v, retval)
   1081 	struct proc *p;
   1082 	void *v;
   1083 	register_t *retval;
   1084 {
   1085 	return sys_getuid(p, v, retval);
   1086 }
   1087 #endif
   1088 
   1089 int
   1090 linux_sys___sysctl(p, v, retval)
   1091 	struct proc *p;
   1092 	void *v;
   1093 	register_t *retval;
   1094 {
   1095 	struct linux_sys___sysctl_args /* {
   1096 		syscallarg(struct linux___sysctl *) lsp;
   1097 	} */ *uap = v;
   1098 	struct linux___sysctl ls;
   1099 	struct sys___sysctl_args bsa;
   1100 	int error;
   1101 
   1102 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
   1103 		return error;
   1104 	SCARG(&bsa, name) = ls.name;
   1105 	SCARG(&bsa, namelen) = ls.namelen;
   1106 	SCARG(&bsa, old) = ls.old;
   1107 	SCARG(&bsa, oldlenp) = ls.oldlenp;
   1108 	SCARG(&bsa, new) = ls.new;
   1109 	SCARG(&bsa, newlen) = ls.newlen;
   1110 
   1111 	return sys___sysctl(p, &bsa, retval);
   1112 }
   1113 
   1114 int
   1115 linux_sys_setresuid(p, v, retval)
   1116 	struct proc *p;
   1117 	void *v;
   1118 	register_t *retval;
   1119 {
   1120 	struct linux_sys_setresuid_args /* {
   1121 		syscallarg(uid_t) ruid;
   1122 		syscallarg(uid_t) euid;
   1123 		syscallarg(uid_t) suid;
   1124 	} */ *uap = v;
   1125 	struct pcred *pc = p->p_cred;
   1126 	uid_t ruid, euid, suid;
   1127 	int error;
   1128 
   1129 	ruid = SCARG(uap, ruid);
   1130 	euid = SCARG(uap, euid);
   1131 	suid = SCARG(uap, suid);
   1132 
   1133 	/*
   1134 	 * Note: These checks are a little different than the NetBSD
   1135 	 * setreuid(2) call performs.  This precisely follows the
   1136 	 * behavior of the Linux kernel.
   1137 	 */
   1138 	if (ruid != (uid_t)-1 &&
   1139 	    ruid != pc->p_ruid &&
   1140 	    ruid != pc->pc_ucred->cr_uid &&
   1141 	    ruid != pc->p_svuid &&
   1142 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1143 		return (error);
   1144 
   1145 	if (euid != (uid_t)-1 &&
   1146 	    euid != pc->p_ruid &&
   1147 	    euid != pc->pc_ucred->cr_uid &&
   1148 	    euid != pc->p_svuid &&
   1149 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1150 		return (error);
   1151 
   1152 	if (suid != (uid_t)-1 &&
   1153 	    suid != pc->p_ruid &&
   1154 	    suid != pc->pc_ucred->cr_uid &&
   1155 	    suid != pc->p_svuid &&
   1156 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1157 		return (error);
   1158 
   1159 	/*
   1160 	 * Now assign the new real, effective, and saved UIDs.
   1161 	 * Note that Linux, unlike NetBSD in setreuid(2), does not
   1162 	 * set the saved UID in this call unless the user specifies
   1163 	 * it.
   1164 	 */
   1165 	if (ruid != (uid_t)-1) {
   1166 		(void)chgproccnt(pc->p_ruid, -1);
   1167 		(void)chgproccnt(ruid, 1);
   1168 		pc->p_ruid = ruid;
   1169 	}
   1170 
   1171 	if (euid != (uid_t)-1) {
   1172 		pc->pc_ucred = crcopy(pc->pc_ucred);
   1173 		pc->pc_ucred->cr_uid = euid;
   1174 	}
   1175 
   1176 	if (suid != (uid_t)-1)
   1177 		pc->p_svuid = suid;
   1178 
   1179 	if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
   1180 		p->p_flag |= P_SUGID;
   1181 	return (0);
   1182 }
   1183 
   1184 int
   1185 linux_sys_getresuid(p, v, retval)
   1186 	struct proc *p;
   1187 	void *v;
   1188 	register_t *retval;
   1189 {
   1190 	struct linux_sys_getresuid_args /* {
   1191 		syscallarg(uid_t *) ruid;
   1192 		syscallarg(uid_t *) euid;
   1193 		syscallarg(uid_t *) suid;
   1194 	} */ *uap = v;
   1195 	struct pcred *pc = p->p_cred;
   1196 	int error;
   1197 
   1198 	/*
   1199 	 * Linux copies these values out to userspace like so:
   1200 	 *
   1201 	 *	1. Copy out ruid.
   1202 	 *	2. If that succeeds, copy out euid.
   1203 	 *	3. If both of those succeed, copy out suid.
   1204 	 */
   1205 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
   1206 			     sizeof(uid_t))) != 0)
   1207 		return (error);
   1208 
   1209 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
   1210 			     sizeof(uid_t))) != 0)
   1211 		return (error);
   1212 
   1213 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
   1214 }
   1215 
   1216 int
   1217 linux_sys_ptrace(p, v, retval)
   1218 	struct proc *p;
   1219 	void *v;
   1220 	register_t *retval;
   1221 {
   1222 	struct linux_sys_ptrace_args /* {
   1223 		i386, m68k, powerpc: T=int
   1224 		alpha: T=long
   1225 		syscallarg(T) request;
   1226 		syscallarg(T) pid;
   1227 		syscallarg(T) addr;
   1228 		syscallarg(T) data;
   1229 	} */ *uap = v;
   1230 	const int *ptr;
   1231 	int request;
   1232 	int error;
   1233 
   1234 	ptr = linux_ptrace_request_map;
   1235 	request = SCARG(uap, request);
   1236 	while (*ptr != -1)
   1237 		if (*ptr++ == request) {
   1238 			struct sys_ptrace_args pta;
   1239 
   1240 			SCARG(&pta, req) = *ptr;
   1241 			SCARG(&pta, pid) = SCARG(uap, pid);
   1242 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
   1243 			SCARG(&pta, data) = SCARG(uap, data);
   1244 
   1245 			/*
   1246 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1247 			 * to continue where the process left off previously.
   1248 			 * The same thing is achieved by addr == (caddr_t) 1
   1249 			 * on NetBSD, so rewrite 'addr' appropriately.
   1250 			 */
   1251 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1252 				SCARG(&pta, addr) = (caddr_t) 1;
   1253 
   1254 			error = sys_ptrace(p, &pta, retval);
   1255 			if (error)
   1256 				return error;
   1257 			switch (request) {
   1258 			case LINUX_PTRACE_PEEKTEXT:
   1259 			case LINUX_PTRACE_PEEKDATA:
   1260 				error = copyout (retval,
   1261 				    (caddr_t)SCARG(uap, data), sizeof *retval);
   1262 				*retval = SCARG(uap, data);
   1263 				break;
   1264 			default:
   1265 				break;
   1266 			}
   1267 			return error;
   1268 		}
   1269 		else
   1270 			ptr++;
   1271 
   1272 	return LINUX_SYS_PTRACE_ARCH(p, uap, retval);
   1273 }
   1274 
   1275 int
   1276 linux_sys_reboot(struct proc *p, void *v, register_t *retval)
   1277 {
   1278 	struct linux_sys_reboot_args /* {
   1279 		syscallarg(int) magic1;
   1280 		syscallarg(int) magic2;
   1281 		syscallarg(int) cmd;
   1282 		syscallarg(void *) arg;
   1283 	} */ *uap = v;
   1284 	struct sys_reboot_args /* {
   1285 		syscallarg(int) opt;
   1286 		syscallarg(char *) bootstr;
   1287 	} */ sra;
   1288 	int error;
   1289 
   1290 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1291 		return(error);
   1292 
   1293 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1294 		return(EINVAL);
   1295 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1296 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1297 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1298 		return(EINVAL);
   1299 
   1300 	switch (SCARG(uap, cmd)) {
   1301 	case LINUX_REBOOT_CMD_RESTART:
   1302 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1303 		break;
   1304 	case LINUX_REBOOT_CMD_HALT:
   1305 		SCARG(&sra, opt) = RB_HALT;
   1306 		break;
   1307 	case LINUX_REBOOT_CMD_POWER_OFF:
   1308 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1309 		break;
   1310 	case LINUX_REBOOT_CMD_RESTART2:
   1311 		/* Reboot with an argument. */
   1312 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1313 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1314 		break;
   1315 	case LINUX_REBOOT_CMD_CAD_ON:
   1316 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1317 	case LINUX_REBOOT_CMD_CAD_OFF:
   1318 		return(0);
   1319 	default:
   1320 		return(EINVAL);
   1321 	}
   1322 
   1323 	return(sys_reboot(p, &sra, retval));
   1324 }
   1325 
   1326 /*
   1327  * Copy of compat_12_sys_swapon().
   1328  */
   1329 int
   1330 linux_sys_swapon(p, v, retval)
   1331 	struct proc *p;
   1332 	void *v;
   1333 	register_t *retval;
   1334 {
   1335 	struct sys_swapctl_args ua;
   1336 	struct linux_sys_swapon_args /* {
   1337 		syscallarg(const char *) name;
   1338 	} */ *uap = v;
   1339 
   1340 	SCARG(&ua, cmd) = SWAP_ON;
   1341 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
   1342 	SCARG(&ua, misc) = 0;	/* priority */
   1343 	return (sys_swapctl(p, &ua, retval));
   1344 }
   1345 
   1346 /*
   1347  * Stop swapping to the file or block device specified by path.
   1348  */
   1349 int
   1350 linux_sys_swapoff(p, v, retval)
   1351 	struct proc *p;
   1352 	void *v;
   1353 	register_t *retval;
   1354 {
   1355 	struct sys_swapctl_args ua;
   1356 	struct linux_sys_swapoff_args /* {
   1357 		syscallarg(const char *) path;
   1358 	} */ *uap = v;
   1359 
   1360 	SCARG(&ua, cmd) = SWAP_OFF;
   1361 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
   1362 	return (sys_swapctl(p, &ua, retval));
   1363 }
   1364 
   1365 /*
   1366  * Copy of compat_09_sys_setdomainname()
   1367  */
   1368 /* ARGSUSED */
   1369 int
   1370 linux_sys_setdomainname(p, v, retval)
   1371 	struct proc *p;
   1372 	void *v;
   1373 	register_t *retval;
   1374 {
   1375 	struct linux_sys_setdomainname_args /* {
   1376 		syscallarg(char *) domainname;
   1377 		syscallarg(int) len;
   1378 	} */ *uap = v;
   1379 	int name;
   1380 	int error;
   1381 
   1382 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1383 		return (error);
   1384 	name = KERN_DOMAINNAME;
   1385 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
   1386 			    SCARG(uap, len), p));
   1387 }
   1388 
   1389 /*
   1390  * sysinfo()
   1391  */
   1392 /* ARGSUSED */
   1393 int
   1394 linux_sys_sysinfo(p, v, retval)
   1395 	struct proc *p;
   1396 	void *v;
   1397 	register_t *retval;
   1398 {
   1399 	struct linux_sys_sysinfo_args /* {
   1400 		syscallarg(struct linux_sysinfo *) arg;
   1401 	} */ *uap = v;
   1402 	struct linux_sysinfo si;
   1403 	struct loadavg *la;
   1404 
   1405 	si.uptime = time.tv_sec - boottime.tv_sec;
   1406 	la = &averunnable;
   1407 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1408 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1409 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1410 	si.totalram = ctob(physmem);
   1411 	si.freeram = uvmexp.free * uvmexp.pagesize;
   1412 	si.sharedram = 0;	/* XXX */
   1413 	si.bufferram = uvmexp.filepages * uvmexp.pagesize;
   1414 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
   1415 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1416 	si.procs = nprocs;
   1417 
   1418 	/* The following are only present in newer Linux kernels. */
   1419 	si.totalbig = 0;
   1420 	si.freebig = 0;
   1421 	si.mem_unit = 1;
   1422 
   1423 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1424 }
   1425 
   1426 /*
   1427  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1428  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1429  * This is the way Linux does it and glibc depends on this behaviour.
   1430  */
   1431 int
   1432 linux_sys_nosys(p, v, retval)
   1433 	struct proc *p;
   1434 	void *v;
   1435 	register_t *retval;
   1436 {
   1437 	return (ENOSYS);
   1438 }
   1439