Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.83.2.5
      1 /*	$NetBSD: linux_misc.c,v 1.83.2.5 2001/09/21 22:35:19 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_WALL|
    188 	      LINUX_WAIT4_WCLONE))
    189 		return (EINVAL);
    190 
    191 	if (linux_options & LINUX_WAIT4_WNOHANG)
    192 		options |= WNOHANG;
    193 	if (linux_options & LINUX_WAIT4_WUNTRACED)
    194 		options |= WUNTRACED;
    195 	if (linux_options & LINUX_WAIT4_WALL)
    196 		options |= WALLSIG;
    197 	if (linux_options & LINUX_WAIT4_WCLONE)
    198 		options |= WALTSIG;
    199 
    200 	SCARG(&w4a, pid) = SCARG(uap, pid);
    201 	SCARG(&w4a, status) = status;
    202 	SCARG(&w4a, options) = options;
    203 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
    204 
    205 	if ((error = sys_wait4(l, &w4a, retval)))
    206 		return error;
    207 
    208 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
    209 
    210 	if (status != NULL) {
    211 		if ((error = copyin(status, &tstat, sizeof tstat)))
    212 			return error;
    213 
    214 		bsd_to_linux_wstat(&tstat);
    215 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    216 	}
    217 
    218 	return 0;
    219 }
    220 
    221 /*
    222  * Linux brk(2). The check if the new address is >= the old one is
    223  * done in the kernel in Linux. NetBSD does it in the library.
    224  */
    225 int
    226 linux_sys_brk(l, v, retval)
    227 	struct lwp *l;
    228 	void *v;
    229 	register_t *retval;
    230 {
    231 	struct linux_sys_brk_args /* {
    232 		syscallarg(char *) nsize;
    233 	} */ *uap = v;
    234 	struct proc *p = l->l_proc;
    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(l, &oba, retval) == 0)
    243 		ed->p_break = (char *)nbrk;
    244 	else
    245 		nbrk = ed->p_break;
    246 
    247 	retval[0] = (register_t)nbrk;
    248 	return 0;
    249 }
    250 
    251 /*
    252  * Convert BSD statfs structure to Linux statfs structure.
    253  * The Linux structure has less fields, and it also wants
    254  * the length of a name in a dir entry in a field, which
    255  * we fake (probably the wrong way).
    256  */
    257 static void
    258 bsd_to_linux_statfs(bsp, lsp)
    259 	struct statfs *bsp;
    260 	struct linux_statfs *lsp;
    261 {
    262 
    263 	lsp->l_ftype = bsp->f_type;
    264 	lsp->l_fbsize = bsp->f_bsize;
    265 	lsp->l_fblocks = bsp->f_blocks;
    266 	lsp->l_fbfree = bsp->f_bfree;
    267 	lsp->l_fbavail = bsp->f_bavail;
    268 	lsp->l_ffiles = bsp->f_files;
    269 	lsp->l_fffree = bsp->f_ffree;
    270 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
    271 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
    272 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
    273 }
    274 
    275 /*
    276  * Implement the fs stat functions. Straightforward.
    277  */
    278 int
    279 linux_sys_statfs(l, v, retval)
    280 	struct lwp *l;
    281 	void *v;
    282 	register_t *retval;
    283 {
    284 	struct linux_sys_statfs_args /* {
    285 		syscallarg(const char *) path;
    286 		syscallarg(struct linux_statfs *) sp;
    287 	} */ *uap = v;
    288 	struct proc *p = l->l_proc;
    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(l, &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(l, v, retval)
    316 	struct lwp *l;
    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 proc *p = l->l_proc;
    325 	struct statfs btmp, *bsp;
    326 	struct linux_statfs ltmp;
    327 	struct sys_fstatfs_args bsa;
    328 	caddr_t sg;
    329 	int error;
    330 
    331 	sg = stackgap_init(p->p_emul);
    332 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    333 
    334 	SCARG(&bsa, fd) = SCARG(uap, fd);
    335 	SCARG(&bsa, buf) = bsp;
    336 
    337 	if ((error = sys_fstatfs(l, &bsa, retval)))
    338 		return error;
    339 
    340 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    341 		return error;
    342 
    343 	bsd_to_linux_statfs(&btmp, &ltmp);
    344 
    345 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    346 }
    347 
    348 char linux_sysname[] = "Linux";
    349 char linux_release[] = "2.0.38";
    350 char linux_version[] = "#0 Sun Apr 1 11:11:11 MET 2000";
    351 
    352 /*
    353  * uname(). Just copy the info from the various strings stored in the
    354  * kernel, and put it in the Linux utsname structure. That structure
    355  * is almost the same as the NetBSD one, only it has fields 65 characters
    356  * long, and an extra domainname field.
    357  */
    358 int
    359 linux_sys_uname(l, v, retval)
    360 	struct lwp *l;
    361 	void *v;
    362 	register_t *retval;
    363 {
    364 	struct linux_sys_uname_args /* {
    365 		syscallarg(struct linux_utsname *) up;
    366 	} */ *uap = v;
    367 	struct linux_utsname luts;
    368 
    369 	strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    370 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    371 	strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
    372 	strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
    373 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    374 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    375 
    376 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    377 }
    378 
    379 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
    380 /* Used indirectly on: arm, i386, m68k */
    381 
    382 /*
    383  * New type Linux mmap call.
    384  * Only called directly on machines with >= 6 free regs.
    385  */
    386 int
    387 linux_sys_mmap(l, v, retval)
    388 	struct lwp *l;
    389 	void *v;
    390 	register_t *retval;
    391 {
    392 	struct linux_sys_mmap_args /* {
    393 		syscallarg(unsigned long) addr;
    394 		syscallarg(size_t) len;
    395 		syscallarg(int) prot;
    396 		syscallarg(int) flags;
    397 		syscallarg(int) fd;
    398 		syscallarg(linux_off_t) offset;
    399 	} */ *uap = v;
    400 	struct sys_mmap_args cma;
    401 	int flags;
    402 
    403 	flags = 0;
    404 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
    405 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
    406 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
    407 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
    408 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
    409 
    410 	SCARG(&cma,addr) = (void *)SCARG(uap, addr);
    411 	SCARG(&cma,len) = SCARG(uap, len);
    412 	SCARG(&cma,prot) = SCARG(uap, prot);
    413 	if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
    414 		SCARG(&cma,prot) |= VM_PROT_READ;
    415 	SCARG(&cma,flags) = flags;
    416 	SCARG(&cma,fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
    417 	SCARG(&cma,pad) = 0;
    418 	SCARG(&cma,pos) = (off_t)SCARG(uap, offset);
    419 
    420 	return sys_mmap(l, &cma, retval);
    421 }
    422 
    423 int
    424 linux_sys_mremap(l, v, retval)
    425 	struct lwp *l;
    426 	void *v;
    427 	register_t *retval;
    428 {
    429 	struct linux_sys_mremap_args /* {
    430 		syscallarg(void *) old_address;
    431 		syscallarg(size_t) old_size;
    432 		syscallarg(size_t) new_size;
    433 		syscallarg(u_long) flags;
    434 	} */ *uap = v;
    435 	struct sys_munmap_args mua;
    436 	size_t old_size, new_size;
    437 	int error;
    438 
    439 	old_size = round_page(SCARG(uap, old_size));
    440 	new_size = round_page(SCARG(uap, new_size));
    441 
    442 	/*
    443 	 * Growing mapped region.
    444 	 */
    445 	if (new_size > old_size) {
    446 		/*
    447 		 * XXX Implement me.  What we probably want to do is
    448 		 * XXX dig out the guts of the old mapping, mmap that
    449 		 * XXX object again with the new size, then munmap
    450 		 * XXX the old mapping.
    451 		 */
    452 		*retval = 0;
    453 		return (ENOMEM);
    454 	}
    455 
    456 	/*
    457 	 * Shrinking mapped region.
    458 	 */
    459 	if (new_size < old_size) {
    460 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
    461 		    new_size;
    462 		SCARG(&mua, len) = old_size - new_size;
    463 		error = sys_munmap(l, &mua, retval);
    464 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
    465 		return (error);
    466 	}
    467 
    468 	/*
    469 	 * No change.
    470 	 */
    471 	*retval = (register_t)SCARG(uap, old_address);
    472 	return (0);
    473 }
    474 
    475 int
    476 linux_sys_msync(l, v, retval)
    477 	struct lwp *l;
    478 	void *v;
    479 	register_t *retval;
    480 {
    481 	struct linux_sys_msync_args /* {
    482 		syscallarg(caddr_t) addr;
    483 		syscallarg(int) len;
    484 		syscallarg(int) fl;
    485 	} */ *uap = v;
    486 
    487 	struct sys___msync13_args bma;
    488 
    489 	/* flags are ignored */
    490 	SCARG(&bma, addr) = SCARG(uap, addr);
    491 	SCARG(&bma, len) = SCARG(uap, len);
    492 	SCARG(&bma, flags) = SCARG(uap, fl);
    493 
    494 	return sys___msync13(l, &bma, retval);
    495 }
    496 
    497 /*
    498  * This code is partly stolen from src/lib/libc/compat-43/times.c
    499  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
    500  */
    501 
    502 #define CLK_TCK 100
    503 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
    504 
    505 int
    506 linux_sys_times(l, v, retval)
    507 	struct lwp *l;
    508 	void *v;
    509 	register_t *retval;
    510 {
    511 	struct linux_sys_times_args /* {
    512 		syscallarg(struct times *) tms;
    513 	} */ *uap = v;
    514 	struct proc *p = l->l_proc;
    515 	struct timeval t;
    516 	struct linux_tms ltms;
    517 	struct rusage ru;
    518 	int error, s;
    519 
    520 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    521 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
    522 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
    523 
    524 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    525 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    526 
    527 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    528 		return error;
    529 
    530 	s = splclock();
    531 	timersub(&time, &boottime, &t);
    532 	splx(s);
    533 
    534 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    535 	return 0;
    536 }
    537 
    538 /*
    539  * Linux 'readdir' call. This code is mostly taken from the
    540  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    541  * an attempt has been made to keep it a little cleaner (failing
    542  * miserably, because of the cruft needed if count 1 is passed).
    543  *
    544  * The d_off field should contain the offset of the next valid entry,
    545  * but in Linux it has the offset of the entry itself. We emulate
    546  * that bug here.
    547  *
    548  * Read in BSD-style entries, convert them, and copy them out.
    549  *
    550  * Note that this doesn't handle union-mounted filesystems.
    551  */
    552 int
    553 linux_sys_getdents(l, v, retval)
    554 	struct lwp *l;
    555 	void *v;
    556 	register_t *retval;
    557 {
    558 	struct linux_sys_getdents_args /* {
    559 		syscallarg(int) fd;
    560 		syscallarg(struct linux_dirent *) dent;
    561 		syscallarg(unsigned int) count;
    562 	} */ *uap = v;
    563 	struct proc *p = l->l_proc;
    564 	struct dirent *bdp;
    565 	struct vnode *vp;
    566 	caddr_t	inp, buf;		/* BSD-format */
    567 	int len, reclen;		/* BSD-format */
    568 	caddr_t outp;			/* Linux-format */
    569 	int resid, linux_reclen = 0;	/* Linux-format */
    570 	struct file *fp;
    571 	struct uio auio;
    572 	struct iovec aiov;
    573 	struct linux_dirent idb;
    574 	off_t off;		/* true file offset */
    575 	int buflen, error, eofflag, nbytes, oldcall;
    576 	struct vattr va;
    577 	off_t *cookiebuf = NULL, *cookie;
    578 	int ncookies;
    579 
    580 	/* getvnode() will use the descriptor for us */
    581 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    582 		return (error);
    583 
    584 	if ((fp->f_flag & FREAD) == 0) {
    585 		error = EBADF;
    586 		goto out1;
    587 	}
    588 
    589 	vp = (struct vnode *)fp->f_data;
    590 	if (vp->v_type != VDIR) {
    591 		error = EINVAL;
    592 		goto out1;
    593 	}
    594 
    595 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    596 		goto out1;
    597 
    598 	nbytes = SCARG(uap, count);
    599 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    600 		nbytes = sizeof (struct linux_dirent);
    601 		buflen = max(va.va_blocksize, nbytes);
    602 		oldcall = 1;
    603 	} else {
    604 		buflen = min(MAXBSIZE, nbytes);
    605 		if (buflen < va.va_blocksize)
    606 			buflen = va.va_blocksize;
    607 		oldcall = 0;
    608 	}
    609 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    610 
    611 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    612 	off = fp->f_offset;
    613 again:
    614 	aiov.iov_base = buf;
    615 	aiov.iov_len = buflen;
    616 	auio.uio_iov = &aiov;
    617 	auio.uio_iovcnt = 1;
    618 	auio.uio_rw = UIO_READ;
    619 	auio.uio_segflg = UIO_SYSSPACE;
    620 	auio.uio_procp = p;
    621 	auio.uio_resid = buflen;
    622 	auio.uio_offset = off;
    623 	/*
    624          * First we read into the malloc'ed buffer, then
    625          * we massage it into user space, one record at a time.
    626          */
    627 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    628 	    &ncookies);
    629 	if (error)
    630 		goto out;
    631 
    632 	inp = buf;
    633 	outp = (caddr_t)SCARG(uap, dent);
    634 	resid = nbytes;
    635 	if ((len = buflen - auio.uio_resid) == 0)
    636 		goto eof;
    637 
    638 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    639 		bdp = (struct dirent *)inp;
    640 		reclen = bdp->d_reclen;
    641 		if (reclen & 3)
    642 			panic("linux_readdir");
    643 		if (bdp->d_fileno == 0) {
    644 			inp += reclen;	/* it is a hole; squish it out */
    645 			off = *cookie++;
    646 			continue;
    647 		}
    648 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    649 		if (reclen > len || resid < linux_reclen) {
    650 			/* entry too big for buffer, so just stop */
    651 			outp++;
    652 			break;
    653 		}
    654 		/*
    655 		 * Massage in place to make a Linux-shaped dirent (otherwise
    656 		 * we have to worry about touching user memory outside of
    657 		 * the copyout() call).
    658 		 */
    659 		idb.d_ino = (linux_ino_t)bdp->d_fileno;
    660 		/*
    661 		 * The old readdir() call misuses the offset and reclen fields.
    662 		 */
    663 		if (oldcall) {
    664 			idb.d_off = (linux_off_t)linux_reclen;
    665 			idb.d_reclen = (u_short)bdp->d_namlen;
    666 		} else {
    667 			if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
    668 				compat_offseterr(vp, "linux_getdents");
    669 				error = EINVAL;
    670 				goto out;
    671 			}
    672 			idb.d_off = (linux_off_t)off;
    673 			idb.d_reclen = (u_short)linux_reclen;
    674 		}
    675 		strcpy(idb.d_name, bdp->d_name);
    676 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    677 			goto out;
    678 		/* advance past this real entry */
    679 		inp += reclen;
    680 		off = *cookie++;	/* each entry points to itself */
    681 		/* advance output past Linux-shaped entry */
    682 		outp += linux_reclen;
    683 		resid -= linux_reclen;
    684 		if (oldcall)
    685 			break;
    686 	}
    687 
    688 	/* if we squished out the whole block, try again */
    689 	if (outp == (caddr_t)SCARG(uap, dent))
    690 		goto again;
    691 	fp->f_offset = off;	/* update the vnode offset */
    692 
    693 	if (oldcall)
    694 		nbytes = resid + linux_reclen;
    695 
    696 eof:
    697 	*retval = nbytes - resid;
    698 out:
    699 	VOP_UNLOCK(vp, 0);
    700 	if (cookiebuf)
    701 		free(cookiebuf, M_TEMP);
    702 	free(buf, M_TEMP);
    703  out1:
    704 	FILE_UNUSE(fp, p);
    705 	return error;
    706 }
    707 
    708 /*
    709  * Even when just using registers to pass arguments to syscalls you can
    710  * have 5 of them on the i386. So this newer version of select() does
    711  * this.
    712  */
    713 int
    714 linux_sys_select(l, v, retval)
    715 	struct lwp *l;
    716 	void *v;
    717 	register_t *retval;
    718 {
    719 	struct linux_sys_select_args /* {
    720 		syscallarg(int) nfds;
    721 		syscallarg(fd_set *) readfds;
    722 		syscallarg(fd_set *) writefds;
    723 		syscallarg(fd_set *) exceptfds;
    724 		syscallarg(struct timeval *) timeout;
    725 	} */ *uap = v;
    726 
    727 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    728 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
    729 }
    730 
    731 /*
    732  * Common code for the old and new versions of select(). A couple of
    733  * things are important:
    734  * 1) return the amount of time left in the 'timeout' parameter
    735  * 2) select never returns ERESTART on Linux, always return EINTR
    736  */
    737 int
    738 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
    739 	struct lwp *l;
    740 	register_t *retval;
    741 	int nfds;
    742 	fd_set *readfds, *writefds, *exceptfds;
    743 	struct timeval *timeout;
    744 {
    745 	struct sys_select_args bsa;
    746 	struct proc *p = l->l_proc;
    747 	struct timeval tv0, tv1, utv, *tvp;
    748 	caddr_t sg;
    749 	int error;
    750 
    751 	SCARG(&bsa, nd) = nfds;
    752 	SCARG(&bsa, in) = readfds;
    753 	SCARG(&bsa, ou) = writefds;
    754 	SCARG(&bsa, ex) = exceptfds;
    755 	SCARG(&bsa, tv) = timeout;
    756 
    757 	/*
    758 	 * Store current time for computation of the amount of
    759 	 * time left.
    760 	 */
    761 	if (timeout) {
    762 		if ((error = copyin(timeout, &utv, sizeof(utv))))
    763 			return error;
    764 		if (itimerfix(&utv)) {
    765 			/*
    766 			 * The timeval was invalid.  Convert it to something
    767 			 * valid that will act as it does under Linux.
    768 			 */
    769 			sg = stackgap_init(p->p_emul);
    770 			tvp = stackgap_alloc(&sg, sizeof(utv));
    771 			utv.tv_sec += utv.tv_usec / 1000000;
    772 			utv.tv_usec %= 1000000;
    773 			if (utv.tv_usec < 0) {
    774 				utv.tv_sec -= 1;
    775 				utv.tv_usec += 1000000;
    776 			}
    777 			if (utv.tv_sec < 0)
    778 				timerclear(&utv);
    779 			if ((error = copyout(&utv, tvp, sizeof(utv))))
    780 				return error;
    781 			SCARG(&bsa, tv) = tvp;
    782 		}
    783 		microtime(&tv0);
    784 	}
    785 
    786 	error = sys_select(l, &bsa, retval);
    787 	if (error) {
    788 		/*
    789 		 * See fs/select.c in the Linux kernel.  Without this,
    790 		 * Maelstrom doesn't work.
    791 		 */
    792 		if (error == ERESTART)
    793 			error = EINTR;
    794 		return error;
    795 	}
    796 
    797 	if (timeout) {
    798 		if (*retval) {
    799 			/*
    800 			 * Compute how much time was left of the timeout,
    801 			 * by subtracting the current time and the time
    802 			 * before we started the call, and subtracting
    803 			 * that result from the user-supplied value.
    804 			 */
    805 			microtime(&tv1);
    806 			timersub(&tv1, &tv0, &tv1);
    807 			timersub(&utv, &tv1, &utv);
    808 			if (utv.tv_sec < 0)
    809 				timerclear(&utv);
    810 		} else
    811 			timerclear(&utv);
    812 		if ((error = copyout(&utv, timeout, sizeof(utv))))
    813 			return error;
    814 	}
    815 
    816 	return 0;
    817 }
    818 
    819 /*
    820  * Get the process group of a certain process. Look it up
    821  * and return the value.
    822  */
    823 int
    824 linux_sys_getpgid(l, v, retval)
    825 	struct lwp *l;
    826 	void *v;
    827 	register_t *retval;
    828 {
    829 	struct linux_sys_getpgid_args /* {
    830 		syscallarg(int) pid;
    831 	} */ *uap = v;
    832 	struct proc *p = l->l_proc;
    833 	struct proc *targp;
    834 
    835 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
    836 		if ((targp = pfind(SCARG(uap, pid))) == 0)
    837 			return ESRCH;
    838 	}
    839 	else
    840 		targp = p;
    841 
    842 	retval[0] = targp->p_pgid;
    843 	return 0;
    844 }
    845 
    846 /*
    847  * Set the 'personality' (emulation mode) for the current process. Only
    848  * accept the Linux personality here (0). This call is needed because
    849  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
    850  * ELF binaries run in Linux mode, not SVR4 mode.
    851  */
    852 int
    853 linux_sys_personality(l, v, retval)
    854 	struct lwp *l;
    855 	void *v;
    856 	register_t *retval;
    857 {
    858 	struct linux_sys_personality_args /* {
    859 		syscallarg(int) per;
    860 	} */ *uap = v;
    861 
    862 	if (SCARG(uap, per) != 0)
    863 		return EINVAL;
    864 	retval[0] = 0;
    865 	return 0;
    866 }
    867 
    868 #if defined(__i386__) || defined(__m68k__)
    869 /*
    870  * The calls are here because of type conversions.
    871  */
    872 int
    873 linux_sys_setreuid16(l, v, retval)
    874 	struct lwp *l;
    875 	void *v;
    876 	register_t *retval;
    877 {
    878 	struct linux_sys_setreuid16_args /* {
    879 		syscallarg(int) ruid;
    880 		syscallarg(int) euid;
    881 	} */ *uap = v;
    882 	struct sys_setreuid_args bsa;
    883 
    884 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    885 		(uid_t)-1 : SCARG(uap, ruid);
    886 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    887 		(uid_t)-1 : SCARG(uap, euid);
    888 
    889 	return sys_setreuid(l, &bsa, retval);
    890 }
    891 
    892 int
    893 linux_sys_setregid16(l, v, retval)
    894 	struct lwp *l;
    895 	void *v;
    896 	register_t *retval;
    897 {
    898 	struct linux_sys_setregid16_args /* {
    899 		syscallarg(int) rgid;
    900 		syscallarg(int) egid;
    901 	} */ *uap = v;
    902 	struct sys_setregid_args bsa;
    903 
    904 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    905 		(uid_t)-1 : SCARG(uap, rgid);
    906 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    907 		(uid_t)-1 : SCARG(uap, egid);
    908 
    909 	return sys_setregid(l, &bsa, retval);
    910 }
    911 
    912 int
    913 linux_sys_setresuid16(l, v, retval)
    914 	struct lwp *l;
    915 	void *v;
    916 	register_t *retval;
    917 {
    918 	struct linux_sys_setresuid16_args /* {
    919 		syscallarg(uid_t) ruid;
    920 		syscallarg(uid_t) euid;
    921 		syscallarg(uid_t) suid;
    922 	} */ *uap = v;
    923 	struct linux_sys_setresuid16_args lsa;
    924 
    925 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    926 		(uid_t)-1 : SCARG(uap, ruid);
    927 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    928 		(uid_t)-1 : SCARG(uap, euid);
    929 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
    930 		(uid_t)-1 : SCARG(uap, suid);
    931 
    932 	return linux_sys_setresuid(l, &lsa, retval);
    933 }
    934 
    935 int
    936 linux_sys_setresgid16(l, v, retval)
    937 	struct lwp *l;
    938 	void *v;
    939 	register_t *retval;
    940 {
    941 	struct linux_sys_setresgid16_args /* {
    942 		syscallarg(gid_t) rgid;
    943 		syscallarg(gid_t) egid;
    944 		syscallarg(gid_t) sgid;
    945 	} */ *uap = v;
    946 	struct linux_sys_setresgid16_args lsa;
    947 
    948 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    949 		(gid_t)-1 : SCARG(uap, rgid);
    950 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    951 		(gid_t)-1 : SCARG(uap, egid);
    952 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
    953 		(gid_t)-1 : SCARG(uap, sgid);
    954 
    955 	return linux_sys_setresgid(l, &lsa, retval);
    956 }
    957 
    958 int
    959 linux_sys_getgroups16(l, v, retval)
    960 	struct lwp *l;
    961 	void *v;
    962 	register_t *retval;
    963 {
    964 	struct linux_sys_getgroups16_args /* {
    965 		syscallarg(int) gidsetsize;
    966 		syscallarg(linux_gid_t *) gidset;
    967 	} */ *uap = v;
    968 	struct proc *p = l->l_proc;
    969 	caddr_t sg;
    970 	int n, error, i;
    971 	struct sys_getgroups_args bsa;
    972 	gid_t *bset, *kbset;
    973 	linux_gid_t *lset;
    974 	struct pcred *pc = p->p_cred;
    975 
    976 	n = SCARG(uap, gidsetsize);
    977 	if (n < 0)
    978 		return EINVAL;
    979 	error = 0;
    980 	bset = kbset = NULL;
    981 	lset = NULL;
    982 	if (n > 0) {
    983 		n = min(pc->pc_ucred->cr_ngroups, n);
    984 		sg = stackgap_init(p->p_emul);
    985 		bset = stackgap_alloc(&sg, n * sizeof (gid_t));
    986 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
    987 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
    988 		if (bset == NULL || kbset == NULL || lset == NULL)
    989 			return ENOMEM;
    990 		SCARG(&bsa, gidsetsize) = n;
    991 		SCARG(&bsa, gidset) = bset;
    992 		error = sys_getgroups(l, &bsa, retval);
    993 		if (error != 0)
    994 			goto out;
    995 		error = copyin(bset, kbset, n * sizeof (gid_t));
    996 		if (error != 0)
    997 			goto out;
    998 		for (i = 0; i < n; i++)
    999 			lset[i] = (linux_gid_t)kbset[i];
   1000 		error = copyout(lset, SCARG(uap, gidset),
   1001 		    n * sizeof (linux_gid_t));
   1002 	} else
   1003 		*retval = pc->pc_ucred->cr_ngroups;
   1004 out:
   1005 	if (kbset != NULL)
   1006 		free(kbset, M_TEMP);
   1007 	if (lset != NULL)
   1008 		free(lset, M_TEMP);
   1009 	return error;
   1010 }
   1011 
   1012 int
   1013 linux_sys_setgroups16(l, v, retval)
   1014 	struct lwp *l;
   1015 	void *v;
   1016 	register_t *retval;
   1017 {
   1018 	struct linux_sys_setgroups16_args /* {
   1019 		syscallarg(int) gidsetsize;
   1020 		syscallarg(linux_gid_t *) gidset;
   1021 	} */ *uap = v;
   1022 	struct proc *p = l->l_proc;
   1023 	caddr_t sg;
   1024 	int n;
   1025 	int error, i;
   1026 	struct sys_setgroups_args bsa;
   1027 	gid_t *bset, *kbset;
   1028 	linux_gid_t *lset;
   1029 
   1030 	n = SCARG(uap, gidsetsize);
   1031 	if (n < 0 || n > NGROUPS)
   1032 		return EINVAL;
   1033 	sg = stackgap_init(p->p_emul);
   1034 	bset = stackgap_alloc(&sg, n * sizeof (gid_t));
   1035 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1036 	kbset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1037 	if (lset == NULL || bset == NULL)
   1038 		return ENOMEM;
   1039 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
   1040 	if (error != 0)
   1041 		goto out;
   1042 	for (i = 0; i < n; i++)
   1043 		kbset[i] = (gid_t)lset[i];
   1044 	error = copyout(kbset, bset, n * sizeof (gid_t));
   1045 	if (error != 0)
   1046 		goto out;
   1047 	SCARG(&bsa, gidsetsize) = n;
   1048 	SCARG(&bsa, gidset) = bset;
   1049 	error = sys_setgroups(l, &bsa, retval);
   1050 
   1051 out:
   1052 	if (lset != NULL)
   1053 		free(lset, M_TEMP);
   1054 	if (kbset != NULL)
   1055 		free(kbset, M_TEMP);
   1056 
   1057 	return error;
   1058 }
   1059 
   1060 #endif /* __i386__ || __m68k__ */
   1061 
   1062 /*
   1063  * We have nonexistent fsuid equal to uid.
   1064  * If modification is requested, refuse.
   1065  */
   1066 int
   1067 linux_sys_setfsuid(l, v, retval)
   1068 	 struct lwp *l;
   1069 	 void *v;
   1070 	 register_t *retval;
   1071 {
   1072 	 struct linux_sys_setfsuid_args /* {
   1073 		 syscallarg(uid_t) uid;
   1074 	 } */ *uap = v;
   1075 	 struct proc *p = l->l_proc;
   1076 	 uid_t uid;
   1077 
   1078 	 uid = SCARG(uap, uid);
   1079 	 if (p->p_cred->p_ruid != uid)
   1080 		 return sys_nosys(l, v, retval);
   1081 	 else
   1082 		 return (0);
   1083 }
   1084 
   1085 /* XXX XXX XXX */
   1086 #ifndef alpha
   1087 int
   1088 linux_sys_getfsuid(l, v, retval)
   1089 	struct lwp *l;
   1090 	void *v;
   1091 	register_t *retval;
   1092 {
   1093 	return sys_getuid(l, v, retval);
   1094 }
   1095 #endif
   1096 
   1097 int
   1098 linux_sys___sysctl(l, v, retval)
   1099 	struct lwp *l;
   1100 	void *v;
   1101 	register_t *retval;
   1102 {
   1103 	struct linux_sys___sysctl_args /* {
   1104 		syscallarg(struct linux___sysctl *) lsp;
   1105 	} */ *uap = v;
   1106 	struct linux___sysctl ls;
   1107 	struct sys___sysctl_args bsa;
   1108 	int error;
   1109 
   1110 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
   1111 		return error;
   1112 	SCARG(&bsa, name) = ls.name;
   1113 	SCARG(&bsa, namelen) = ls.namelen;
   1114 	SCARG(&bsa, old) = ls.old;
   1115 	SCARG(&bsa, oldlenp) = ls.oldlenp;
   1116 	SCARG(&bsa, new) = ls.new;
   1117 	SCARG(&bsa, newlen) = ls.newlen;
   1118 
   1119 	return sys___sysctl(l, &bsa, retval);
   1120 }
   1121 
   1122 int
   1123 linux_sys_setresuid(l, v, retval)
   1124 	struct lwp *l;
   1125 	void *v;
   1126 	register_t *retval;
   1127 {
   1128 	struct linux_sys_setresuid_args /* {
   1129 		syscallarg(uid_t) ruid;
   1130 		syscallarg(uid_t) euid;
   1131 		syscallarg(uid_t) suid;
   1132 	} */ *uap = v;
   1133 	struct proc *p = l->l_proc;
   1134 	struct pcred *pc = p->p_cred;
   1135 	uid_t ruid, euid, suid;
   1136 	int error;
   1137 
   1138 	ruid = SCARG(uap, ruid);
   1139 	euid = SCARG(uap, euid);
   1140 	suid = SCARG(uap, suid);
   1141 
   1142 	/*
   1143 	 * Note: These checks are a little different than the NetBSD
   1144 	 * setreuid(2) call performs.  This precisely follows the
   1145 	 * behavior of the Linux kernel.
   1146 	 */
   1147 	if (ruid != (uid_t)-1 &&
   1148 	    ruid != pc->p_ruid &&
   1149 	    ruid != pc->pc_ucred->cr_uid &&
   1150 	    ruid != pc->p_svuid &&
   1151 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1152 		return (error);
   1153 
   1154 	if (euid != (uid_t)-1 &&
   1155 	    euid != pc->p_ruid &&
   1156 	    euid != pc->pc_ucred->cr_uid &&
   1157 	    euid != pc->p_svuid &&
   1158 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1159 		return (error);
   1160 
   1161 	if (suid != (uid_t)-1 &&
   1162 	    suid != pc->p_ruid &&
   1163 	    suid != pc->pc_ucred->cr_uid &&
   1164 	    suid != pc->p_svuid &&
   1165 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1166 		return (error);
   1167 
   1168 	/*
   1169 	 * Now assign the new real, effective, and saved UIDs.
   1170 	 * Note that Linux, unlike NetBSD in setreuid(2), does not
   1171 	 * set the saved UID in this call unless the user specifies
   1172 	 * it.
   1173 	 */
   1174 	if (ruid != (uid_t)-1) {
   1175 		(void)chgproccnt(pc->p_ruid, -1);
   1176 		(void)chgproccnt(ruid, 1);
   1177 		pc->p_ruid = ruid;
   1178 	}
   1179 
   1180 	if (euid != (uid_t)-1) {
   1181 		pc->pc_ucred = crcopy(pc->pc_ucred);
   1182 		pc->pc_ucred->cr_uid = euid;
   1183 	}
   1184 
   1185 	if (suid != (uid_t)-1)
   1186 		pc->p_svuid = suid;
   1187 
   1188 	if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
   1189 		p->p_flag |= P_SUGID;
   1190 	return (0);
   1191 }
   1192 
   1193 int
   1194 linux_sys_getresuid(l, v, retval)
   1195 	struct lwp *l;
   1196 	void *v;
   1197 	register_t *retval;
   1198 {
   1199 	struct linux_sys_getresuid_args /* {
   1200 		syscallarg(uid_t *) ruid;
   1201 		syscallarg(uid_t *) euid;
   1202 		syscallarg(uid_t *) suid;
   1203 	} */ *uap = v;
   1204 	struct proc *p = l->l_proc;
   1205 	struct pcred *pc = p->p_cred;
   1206 	int error;
   1207 
   1208 	/*
   1209 	 * Linux copies these values out to userspace like so:
   1210 	 *
   1211 	 *	1. Copy out ruid.
   1212 	 *	2. If that succeeds, copy out euid.
   1213 	 *	3. If both of those succeed, copy out suid.
   1214 	 */
   1215 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
   1216 			     sizeof(uid_t))) != 0)
   1217 		return (error);
   1218 
   1219 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
   1220 			     sizeof(uid_t))) != 0)
   1221 		return (error);
   1222 
   1223 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
   1224 }
   1225 
   1226 int
   1227 linux_sys_ptrace(l, v, retval)
   1228 	struct lwp *l;
   1229 	void *v;
   1230 	register_t *retval;
   1231 {
   1232 	struct linux_sys_ptrace_args /* {
   1233 		i386, m68k, powerpc: T=int
   1234 		alpha: T=long
   1235 		syscallarg(T) request;
   1236 		syscallarg(T) pid;
   1237 		syscallarg(T) addr;
   1238 		syscallarg(T) data;
   1239 	} */ *uap = v;
   1240 	const int *ptr;
   1241 	int request;
   1242 	int error;
   1243 
   1244 	ptr = linux_ptrace_request_map;
   1245 	request = SCARG(uap, request);
   1246 	while (*ptr != -1)
   1247 		if (*ptr++ == request) {
   1248 			struct sys_ptrace_args pta;
   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 where the process left off previously.
   1258 			 * The same thing is achieved by addr == (caddr_t) 1
   1259 			 * on NetBSD, so rewrite 'addr' appropriately.
   1260 			 */
   1261 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1262 				SCARG(&pta, addr) = (caddr_t) 1;
   1263 
   1264 			error = sys_ptrace(l, &pta, retval);
   1265 			if (error)
   1266 				return error;
   1267 			switch (request) {
   1268 			case LINUX_PTRACE_PEEKTEXT:
   1269 			case LINUX_PTRACE_PEEKDATA:
   1270 				error = copyout (retval,
   1271 				    (caddr_t)SCARG(uap, data), sizeof *retval);
   1272 				*retval = SCARG(uap, data);
   1273 				break;
   1274 			default:
   1275 				break;
   1276 			}
   1277 			return error;
   1278 		}
   1279 		else
   1280 			ptr++;
   1281 
   1282 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1283 }
   1284 
   1285 int
   1286 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
   1287 {
   1288 	struct linux_sys_reboot_args /* {
   1289 		syscallarg(int) magic1;
   1290 		syscallarg(int) magic2;
   1291 		syscallarg(int) cmd;
   1292 		syscallarg(void *) arg;
   1293 	} */ *uap = v;
   1294 	struct sys_reboot_args /* {
   1295 		syscallarg(int) opt;
   1296 		syscallarg(char *) bootstr;
   1297 	} */ sra;
   1298 	struct proc *p = l->l_proc;
   1299 	int error;
   1300 
   1301 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1302 		return(error);
   1303 
   1304 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1305 		return(EINVAL);
   1306 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1307 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1308 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1309 		return(EINVAL);
   1310 
   1311 	switch (SCARG(uap, cmd)) {
   1312 	case LINUX_REBOOT_CMD_RESTART:
   1313 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1314 		break;
   1315 	case LINUX_REBOOT_CMD_HALT:
   1316 		SCARG(&sra, opt) = RB_HALT;
   1317 		break;
   1318 	case LINUX_REBOOT_CMD_POWER_OFF:
   1319 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1320 		break;
   1321 	case LINUX_REBOOT_CMD_RESTART2:
   1322 		/* Reboot with an argument. */
   1323 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1324 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1325 		break;
   1326 	case LINUX_REBOOT_CMD_CAD_ON:
   1327 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1328 	case LINUX_REBOOT_CMD_CAD_OFF:
   1329 		return(0);
   1330 	default:
   1331 		return(EINVAL);
   1332 	}
   1333 
   1334 	return(sys_reboot(l, &sra, retval));
   1335 }
   1336 
   1337 /*
   1338  * Copy of compat_12_sys_swapon().
   1339  */
   1340 int
   1341 linux_sys_swapon(l, v, retval)
   1342 	struct lwp *l;
   1343 	void *v;
   1344 	register_t *retval;
   1345 {
   1346 	struct sys_swapctl_args ua;
   1347 	struct linux_sys_swapon_args /* {
   1348 		syscallarg(const char *) name;
   1349 	} */ *uap = v;
   1350 
   1351 	SCARG(&ua, cmd) = SWAP_ON;
   1352 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
   1353 	SCARG(&ua, misc) = 0;	/* priority */
   1354 	return (sys_swapctl(l, &ua, retval));
   1355 }
   1356 
   1357 /*
   1358  * Stop swapping to the file or block device specified by path.
   1359  */
   1360 int
   1361 linux_sys_swapoff(l, v, retval)
   1362 	struct lwp *l;
   1363 	void *v;
   1364 	register_t *retval;
   1365 {
   1366 	struct sys_swapctl_args ua;
   1367 	struct linux_sys_swapoff_args /* {
   1368 		syscallarg(const char *) path;
   1369 	} */ *uap = v;
   1370 
   1371 	SCARG(&ua, cmd) = SWAP_OFF;
   1372 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
   1373 	return (sys_swapctl(l, &ua, retval));
   1374 }
   1375 
   1376 /*
   1377  * Copy of compat_09_sys_setdomainname()
   1378  */
   1379 /* ARGSUSED */
   1380 int
   1381 linux_sys_setdomainname(l, v, retval)
   1382 	struct lwp *l;
   1383 	void *v;
   1384 	register_t *retval;
   1385 {
   1386 	struct linux_sys_setdomainname_args /* {
   1387 		syscallarg(char *) domainname;
   1388 		syscallarg(int) len;
   1389 	} */ *uap = v;
   1390 	struct proc *p = l->l_proc;
   1391 	int name;
   1392 	int error;
   1393 
   1394 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1395 		return (error);
   1396 	name = KERN_DOMAINNAME;
   1397 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
   1398 			    SCARG(uap, len), p));
   1399 }
   1400 
   1401 /*
   1402  * sysinfo()
   1403  */
   1404 /* ARGSUSED */
   1405 int
   1406 linux_sys_sysinfo(l, v, retval)
   1407 	struct lwp *l;
   1408 	void *v;
   1409 	register_t *retval;
   1410 {
   1411 	struct linux_sys_sysinfo_args /* {
   1412 		syscallarg(struct linux_sysinfo *) arg;
   1413 	} */ *uap = v;
   1414 	struct linux_sysinfo si;
   1415 	struct loadavg *la;
   1416 
   1417 	si.uptime = time.tv_sec - boottime.tv_sec;
   1418 	la = &averunnable;
   1419 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1420 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1421 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1422 	si.totalram = ctob(physmem);
   1423 	si.freeram = uvmexp.free * uvmexp.pagesize;
   1424 	si.sharedram = 0;	/* XXX */
   1425 	si.bufferram = uvmexp.vnodepages * uvmexp.pagesize;
   1426 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
   1427 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1428 	si.procs = nprocs;
   1429 
   1430 	/* The following are only present in newer Linux kernels. */
   1431 	si.totalbig = 0;
   1432 	si.freebig = 0;
   1433 	si.mem_unit = 1;
   1434 
   1435 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1436 }
   1437 
   1438 /*
   1439  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1440  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1441  * This is the way Linux does it and glibc depends on this behaviour.
   1442  */
   1443 int
   1444 linux_sys_nosys(l, v, retval)
   1445 	struct lwp *l;
   1446 	void *v;
   1447 	register_t *retval;
   1448 {
   1449 	return (ENOSYS);
   1450 }
   1451