Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.83.2.16
      1 /*	$NetBSD: linux_misc.c,v 1.83.2.16 2002/07/08 21:08:28 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/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.83.2.16 2002/07/08 21:08:28 nathanw Exp $");
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/namei.h>
     72 #include <sys/lwp.h>
     73 #include <sys/proc.h>
     74 #include <sys/dirent.h>
     75 #include <sys/file.h>
     76 #include <sys/stat.h>
     77 #include <sys/filedesc.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/kernel.h>
     80 #include <sys/malloc.h>
     81 #include <sys/mbuf.h>
     82 #include <sys/mman.h>
     83 #include <sys/mount.h>
     84 #include <sys/reboot.h>
     85 #include <sys/resource.h>
     86 #include <sys/resourcevar.h>
     87 #include <sys/signal.h>
     88 #include <sys/signalvar.h>
     89 #include <sys/socket.h>
     90 #include <sys/time.h>
     91 #include <sys/times.h>
     92 #include <sys/vnode.h>
     93 #include <sys/uio.h>
     94 #include <sys/wait.h>
     95 #include <sys/utsname.h>
     96 #include <sys/unistd.h>
     97 #include <sys/swap.h>		/* for SWAP_ON */
     98 #include <sys/sysctl.h>		/* for KERN_DOMAINNAME */
     99 
    100 #include <sys/ptrace.h>
    101 #include <machine/ptrace.h>
    102 
    103 #include <sys/sa.h>
    104 #include <sys/syscallargs.h>
    105 
    106 #include <compat/linux/common/linux_types.h>
    107 #include <compat/linux/common/linux_signal.h>
    108 
    109 #include <compat/linux/linux_syscallargs.h>
    110 
    111 #include <compat/linux/common/linux_fcntl.h>
    112 #include <compat/linux/common/linux_mmap.h>
    113 #include <compat/linux/common/linux_dirent.h>
    114 #include <compat/linux/common/linux_util.h>
    115 #include <compat/linux/common/linux_misc.h>
    116 #include <compat/linux/common/linux_ptrace.h>
    117 #include <compat/linux/common/linux_reboot.h>
    118 #include <compat/linux/common/linux_emuldata.h>
    119 
    120 const int linux_ptrace_request_map[] = {
    121 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
    122 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
    123 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
    124 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
    125 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
    126 	LINUX_PTRACE_CONT,	PT_CONTINUE,
    127 	LINUX_PTRACE_KILL,	PT_KILL,
    128 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
    129 	LINUX_PTRACE_DETACH,	PT_DETACH,
    130 #ifdef PT_STEP
    131 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
    132 #endif
    133 	-1
    134 };
    135 
    136 const static struct mnttypes {
    137 	char *bsd;
    138 	int linux;
    139 } fstypes[] = {
    140 	{ MOUNT_FFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    141 	{ MOUNT_NFS,		LINUX_NFS_SUPER_MAGIC 		},
    142 	{ MOUNT_MFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    143 	{ MOUNT_MSDOS,		LINUX_MSDOS_SUPER_MAGIC		},
    144 	{ MOUNT_LFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    145 	{ MOUNT_FDESC,		LINUX_DEFAULT_SUPER_MAGIC	},
    146 	{ MOUNT_PORTAL,		LINUX_DEFAULT_SUPER_MAGIC	},
    147 	{ MOUNT_NULL,		LINUX_DEFAULT_SUPER_MAGIC	},
    148 	{ MOUNT_OVERLAY,	LINUX_DEFAULT_SUPER_MAGIC	},
    149 	{ MOUNT_UMAP,		LINUX_DEFAULT_SUPER_MAGIC	},
    150 	{ MOUNT_KERNFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    151 	{ MOUNT_PROCFS,		LINUX_PROC_SUPER_MAGIC		},
    152 	{ MOUNT_AFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    153 	{ MOUNT_CD9660,		LINUX_ISOFS_SUPER_MAGIC		},
    154 	{ MOUNT_UNION,		LINUX_DEFAULT_SUPER_MAGIC	},
    155 	{ MOUNT_ADOSFS,		LINUX_ADFS_SUPER_MAGIC		},
    156 	{ MOUNT_EXT2FS,		LINUX_EXT2_SUPER_MAGIC		},
    157 	{ MOUNT_CFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    158 	{ MOUNT_CODA,		LINUX_CODA_SUPER_MAGIC		},
    159 	{ MOUNT_FILECORE,	LINUX_DEFAULT_SUPER_MAGIC	},
    160 	{ MOUNT_NTFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    161 	{ MOUNT_SMBFS,		LINUX_SMB_SUPER_MAGIC		}
    162 };
    163 #define FSTYPESSIZE (sizeof(fstypes) / sizeof(fstypes[0]))
    164 
    165 #ifdef DEBUG_LINUX
    166 #define DPRINTF(a)	uprintf a
    167 #else
    168 #define DPRINTF(a)
    169 #endif
    170 
    171 /* Local linux_misc.c functions: */
    172 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
    173 static int linux_to_bsd_limit __P((int));
    174 
    175 /*
    176  * The information on a terminated (or stopped) process needs
    177  * to be converted in order for Linux binaries to get a valid signal
    178  * number out of it.
    179  */
    180 void
    181 bsd_to_linux_wstat(st)
    182 	int *st;
    183 {
    184 
    185 	int sig;
    186 
    187 	if (WIFSIGNALED(*st)) {
    188 		sig = WTERMSIG(*st);
    189 		if (sig >= 0 && sig < NSIG)
    190 			*st= (*st& ~0177) | native_to_linux_signo[sig];
    191 	} else if (WIFSTOPPED(*st)) {
    192 		sig = WSTOPSIG(*st);
    193 		if (sig >= 0 && sig < NSIG)
    194 			*st = (*st & ~0xff00) |
    195 			    (native_to_linux_signo[sig] << 8);
    196 	}
    197 }
    198 
    199 /*
    200  * This is very much the same as waitpid()
    201  */
    202 int
    203 linux_sys_wait4(l, v, retval)
    204 	struct lwp *l;
    205 	void *v;
    206 	register_t *retval;
    207 {
    208 	struct linux_sys_wait4_args /* {
    209 		syscallarg(int) pid;
    210 		syscallarg(int *) status;
    211 		syscallarg(int) options;
    212 		syscallarg(struct rusage *) rusage;
    213 	} */ *uap = v;
    214 	struct proc *p = l->l_proc;
    215 	struct sys_wait4_args w4a;
    216 	int error, *status, tstat, options, linux_options;
    217 	caddr_t sg;
    218 
    219 	if (SCARG(uap, status) != NULL) {
    220 		sg = stackgap_init(p, 0);
    221 		status = (int *) stackgap_alloc(p, &sg, sizeof *status);
    222 	} else
    223 		status = NULL;
    224 
    225 	linux_options = SCARG(uap, options);
    226 	options = 0;
    227 	if (linux_options &
    228 	    ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WALL|
    229 	      LINUX_WAIT4_WCLONE))
    230 		return (EINVAL);
    231 
    232 	if (linux_options & LINUX_WAIT4_WNOHANG)
    233 		options |= WNOHANG;
    234 	if (linux_options & LINUX_WAIT4_WUNTRACED)
    235 		options |= WUNTRACED;
    236 	if (linux_options & LINUX_WAIT4_WALL)
    237 		options |= WALLSIG;
    238 	if (linux_options & LINUX_WAIT4_WCLONE)
    239 		options |= WALTSIG;
    240 
    241 	SCARG(&w4a, pid) = SCARG(uap, pid);
    242 	SCARG(&w4a, status) = status;
    243 	SCARG(&w4a, options) = options;
    244 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
    245 
    246 	if ((error = sys_wait4(l, &w4a, retval)))
    247 		return error;
    248 
    249 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
    250 
    251 	if (status != NULL) {
    252 		if ((error = copyin(status, &tstat, sizeof tstat)))
    253 			return error;
    254 
    255 		bsd_to_linux_wstat(&tstat);
    256 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    257 	}
    258 
    259 	return 0;
    260 }
    261 
    262 /*
    263  * Linux brk(2). The check if the new address is >= the old one is
    264  * done in the kernel in Linux. NetBSD does it in the library.
    265  */
    266 int
    267 linux_sys_brk(l, v, retval)
    268 	struct lwp *l;
    269 	void *v;
    270 	register_t *retval;
    271 {
    272 	struct linux_sys_brk_args /* {
    273 		syscallarg(char *) nsize;
    274 	} */ *uap = v;
    275 	struct proc *p = l->l_proc;
    276 	char *nbrk = SCARG(uap, nsize);
    277 	struct sys_obreak_args oba;
    278 	struct vmspace *vm = p->p_vmspace;
    279 	struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
    280 
    281 	SCARG(&oba, nsize) = nbrk;
    282 
    283 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(l, &oba, retval) == 0)
    284 		ed->p_break = (char*)nbrk;
    285 	else
    286 		nbrk = ed->p_break;
    287 
    288 	retval[0] = (register_t)nbrk;
    289 
    290 	return 0;
    291 }
    292 
    293 /*
    294  * Convert BSD statfs structure to Linux statfs structure.
    295  * The Linux structure has less fields, and it also wants
    296  * the length of a name in a dir entry in a field, which
    297  * we fake (probably the wrong way).
    298  */
    299 static void
    300 bsd_to_linux_statfs(bsp, lsp)
    301 	struct statfs *bsp;
    302 	struct linux_statfs *lsp;
    303 {
    304 	int i;
    305 
    306 	for (i = 0; i < FSTYPESSIZE; i++)
    307 		if (strcmp(bsp->f_fstypename, fstypes[i].bsd) == 0)
    308 			break;
    309 
    310 	if (i == FSTYPESSIZE) {
    311 		DPRINTF(("unhandled fstype in linux emulation: %s\n",
    312 		    bsp->f_fstypename));
    313 		lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
    314 	} else {
    315 		lsp->l_ftype = fstypes[i].linux;
    316 	}
    317 
    318 	lsp->l_fbsize = bsp->f_bsize;
    319 	lsp->l_fblocks = bsp->f_blocks;
    320 	lsp->l_fbfree = bsp->f_bfree;
    321 	lsp->l_fbavail = bsp->f_bavail;
    322 	lsp->l_ffiles = bsp->f_files;
    323 	lsp->l_fffree = bsp->f_ffree;
    324 	/* Linux sets the fsid to 0..., we don't */
    325 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
    326 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
    327 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
    328 	(void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
    329 }
    330 
    331 /*
    332  * Implement the fs stat functions. Straightforward.
    333  */
    334 int
    335 linux_sys_statfs(l, v, retval)
    336 	struct lwp *l;
    337 	void *v;
    338 	register_t *retval;
    339 {
    340 	struct linux_sys_statfs_args /* {
    341 		syscallarg(const char *) path;
    342 		syscallarg(struct linux_statfs *) sp;
    343 	} */ *uap = v;
    344 	struct proc *p = l->l_proc;
    345 	struct statfs btmp, *bsp;
    346 	struct linux_statfs ltmp;
    347 	struct sys_statfs_args bsa;
    348 	caddr_t sg;
    349 	int error;
    350 
    351 	sg = stackgap_init(p, 0);
    352 	bsp = (struct statfs *) stackgap_alloc(p, &sg, sizeof (struct statfs));
    353 
    354 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    355 
    356 	SCARG(&bsa, path) = SCARG(uap, path);
    357 	SCARG(&bsa, buf) = bsp;
    358 
    359 	if ((error = sys_statfs(l, &bsa, retval)))
    360 		return error;
    361 
    362 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    363 		return error;
    364 
    365 	bsd_to_linux_statfs(&btmp, &ltmp);
    366 
    367 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    368 }
    369 
    370 int
    371 linux_sys_fstatfs(l, v, retval)
    372 	struct lwp *l;
    373 	void *v;
    374 	register_t *retval;
    375 {
    376 	struct linux_sys_fstatfs_args /* {
    377 		syscallarg(int) fd;
    378 		syscallarg(struct linux_statfs *) sp;
    379 	} */ *uap = v;
    380 	struct proc *p = l->l_proc;
    381 	struct statfs btmp, *bsp;
    382 	struct linux_statfs ltmp;
    383 	struct sys_fstatfs_args bsa;
    384 	caddr_t sg;
    385 	int error;
    386 
    387 	sg = stackgap_init(p, 0);
    388 	bsp = (struct statfs *) stackgap_alloc(p, &sg, sizeof (struct statfs));
    389 
    390 	SCARG(&bsa, fd) = SCARG(uap, fd);
    391 	SCARG(&bsa, buf) = bsp;
    392 
    393 	if ((error = sys_fstatfs(l, &bsa, retval)))
    394 		return error;
    395 
    396 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    397 		return error;
    398 
    399 	bsd_to_linux_statfs(&btmp, &ltmp);
    400 
    401 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    402 }
    403 
    404 /*
    405  * uname(). Just copy the info from the various strings stored in the
    406  * kernel, and put it in the Linux utsname structure. That structure
    407  * is almost the same as the NetBSD one, only it has fields 65 characters
    408  * long, and an extra domainname field.
    409  */
    410 int
    411 linux_sys_uname(l, v, retval)
    412 	struct lwp *l;
    413 	void *v;
    414 	register_t *retval;
    415 {
    416 	struct linux_sys_uname_args /* {
    417 		syscallarg(struct linux_utsname *) up;
    418 	} */ *uap = v;
    419 	struct linux_utsname luts;
    420 
    421 	strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    422 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    423 	strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
    424 	strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
    425 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    426 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    427 
    428 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    429 }
    430 
    431 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
    432 /* Used indirectly on: arm, i386, m68k */
    433 
    434 /*
    435  * New type Linux mmap call.
    436  * Only called directly on machines with >= 6 free regs.
    437  */
    438 int
    439 linux_sys_mmap(l, v, retval)
    440 	struct lwp *l;
    441 	void *v;
    442 	register_t *retval;
    443 {
    444 	struct linux_sys_mmap_args /* {
    445 		syscallarg(unsigned long) addr;
    446 		syscallarg(size_t) len;
    447 		syscallarg(int) prot;
    448 		syscallarg(int) flags;
    449 		syscallarg(int) fd;
    450 		syscallarg(linux_off_t) offset;
    451 	} */ *uap = v;
    452 	struct sys_mmap_args cma;
    453 	int flags, fl = SCARG(uap, flags);
    454 
    455 	flags = 0;
    456 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
    457 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
    458 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
    459 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
    460 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
    461 
    462 	SCARG(&cma, addr) = (void *)SCARG(uap, addr);
    463 	SCARG(&cma, len) = SCARG(uap, len);
    464 	SCARG(&cma, prot) = SCARG(uap, prot);
    465 	if (SCARG(&cma, prot) & VM_PROT_WRITE) /* XXX */
    466 		SCARG(&cma, prot) |= VM_PROT_READ;
    467 	SCARG(&cma, flags) = flags;
    468 	SCARG(&cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
    469 	SCARG(&cma, pad) = 0;
    470 	SCARG(&cma, pos) = (off_t)SCARG(uap, offset);
    471 
    472 	return sys_mmap(l, &cma, retval);
    473 }
    474 
    475 int
    476 linux_sys_mremap(l, v, retval)
    477 	struct lwp *l;
    478 	void *v;
    479 	register_t *retval;
    480 {
    481 	struct linux_sys_mremap_args /* {
    482 		syscallarg(void *) old_address;
    483 		syscallarg(size_t) old_size;
    484 		syscallarg(size_t) new_size;
    485 		syscallarg(u_long) flags;
    486 	} */ *uap = v;
    487 	struct sys_munmap_args mua;
    488 	size_t old_size, new_size;
    489 	int error;
    490 
    491 	old_size = round_page(SCARG(uap, old_size));
    492 	new_size = round_page(SCARG(uap, new_size));
    493 
    494 	/*
    495 	 * Growing mapped region.
    496 	 */
    497 	if (new_size > old_size) {
    498 		/*
    499 		 * XXX Implement me.  What we probably want to do is
    500 		 * XXX dig out the guts of the old mapping, mmap that
    501 		 * XXX object again with the new size, then munmap
    502 		 * XXX the old mapping.
    503 		 */
    504 		*retval = 0;
    505 		return (ENOMEM);
    506 	}
    507 
    508 	/*
    509 	 * Shrinking mapped region.
    510 	 */
    511 	if (new_size < old_size) {
    512 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
    513 		    new_size;
    514 		SCARG(&mua, len) = old_size - new_size;
    515 		error = sys_munmap(l, &mua, retval);
    516 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
    517 		return (error);
    518 	}
    519 
    520 	/*
    521 	 * No change.
    522 	 */
    523 	*retval = (register_t)SCARG(uap, old_address);
    524 	return (0);
    525 }
    526 
    527 int
    528 linux_sys_msync(l, v, retval)
    529 	struct lwp *l;
    530 	void *v;
    531 	register_t *retval;
    532 {
    533 	struct linux_sys_msync_args /* {
    534 		syscallarg(caddr_t) addr;
    535 		syscallarg(int) len;
    536 		syscallarg(int) fl;
    537 	} */ *uap = v;
    538 
    539 	struct sys___msync13_args bma;
    540 
    541 	/* flags are ignored */
    542 	SCARG(&bma, addr) = SCARG(uap, addr);
    543 	SCARG(&bma, len) = SCARG(uap, len);
    544 	SCARG(&bma, flags) = SCARG(uap, fl);
    545 
    546 	return sys___msync13(l, &bma, retval);
    547 }
    548 
    549 int
    550 linux_sys_mprotect(l, v, retval)
    551 	struct lwp *l;
    552 	void *v;
    553 	register_t *retval;
    554 {
    555 	struct linux_sys_mprotect_args /* {
    556 		syscallarg(const void *) start;
    557 		syscallarg(unsigned long) len;
    558 		syscallarg(int) prot;
    559 	} */ *uap = v;
    560 	unsigned long end, start = (unsigned long)SCARG(uap, start), len;
    561 	int prot = SCARG(uap, prot);
    562 	struct vm_map_entry *entry;
    563 	struct vm_map *map = &l->l_proc->p_vmspace->vm_map;
    564 
    565 	if (start & PAGE_MASK)
    566 		return EINVAL;
    567 
    568 	len = round_page(SCARG(uap, len));
    569 	end = start + len;
    570 
    571 	if (end < start)
    572 		return EINVAL;
    573 	else if (end == start)
    574 		return 0;
    575 
    576 	if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
    577 		return EINVAL;
    578 
    579 	vm_map_lock(map);
    580 #ifdef notdef
    581 	VM_MAP_RANGE_CHECK(map, start, end);
    582 #endif
    583 	if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
    584 		vm_map_unlock(map);
    585 		return EFAULT;
    586 	}
    587 	vm_map_unlock(map);
    588 	return uvm_map_protect(map, start, end, prot, FALSE);
    589 }
    590 
    591 /*
    592  * This code is partly stolen from src/lib/libc/compat-43/times.c
    593  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
    594  */
    595 
    596 #define CLK_TCK 100
    597 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
    598 
    599 int
    600 linux_sys_times(l, v, retval)
    601 	struct lwp *l;
    602 	void *v;
    603 	register_t *retval;
    604 {
    605 	struct linux_sys_times_args /* {
    606 		syscallarg(struct times *) tms;
    607 	} */ *uap = v;
    608 	struct proc *p = l->l_proc;
    609 	struct timeval t;
    610 	struct linux_tms ltms;
    611 	struct rusage ru;
    612 	int error, s;
    613 
    614 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    615 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
    616 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
    617 
    618 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    619 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    620 
    621 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    622 		return error;
    623 
    624 	s = splclock();
    625 	timersub(&time, &boottime, &t);
    626 	splx(s);
    627 
    628 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    629 	return 0;
    630 }
    631 
    632 /*
    633  * Linux 'readdir' call. This code is mostly taken from the
    634  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    635  * an attempt has been made to keep it a little cleaner (failing
    636  * miserably, because of the cruft needed if count 1 is passed).
    637  *
    638  * The d_off field should contain the offset of the next valid entry,
    639  * but in Linux it has the offset of the entry itself. We emulate
    640  * that bug here.
    641  *
    642  * Read in BSD-style entries, convert them, and copy them out.
    643  *
    644  * Note that this doesn't handle union-mounted filesystems.
    645  */
    646 int
    647 linux_sys_getdents(l, v, retval)
    648 	struct lwp *l;
    649 	void *v;
    650 	register_t *retval;
    651 {
    652 	struct linux_sys_getdents_args /* {
    653 		syscallarg(int) fd;
    654 		syscallarg(struct linux_dirent *) dent;
    655 		syscallarg(unsigned int) count;
    656 	} */ *uap = v;
    657 	struct proc *p = l->l_proc;
    658 	struct dirent *bdp;
    659 	struct vnode *vp;
    660 	caddr_t	inp, buf;		/* BSD-format */
    661 	int len, reclen;		/* BSD-format */
    662 	caddr_t outp;			/* Linux-format */
    663 	int resid, linux_reclen = 0;	/* Linux-format */
    664 	struct file *fp;
    665 	struct uio auio;
    666 	struct iovec aiov;
    667 	struct linux_dirent idb;
    668 	off_t off;		/* true file offset */
    669 	int buflen, error, eofflag, nbytes, oldcall;
    670 	struct vattr va;
    671 	off_t *cookiebuf = NULL, *cookie;
    672 	int ncookies;
    673 
    674 	/* getvnode() will use the descriptor for us */
    675 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    676 		return (error);
    677 
    678 	if ((fp->f_flag & FREAD) == 0) {
    679 		error = EBADF;
    680 		goto out1;
    681 	}
    682 
    683 	vp = (struct vnode *)fp->f_data;
    684 	if (vp->v_type != VDIR) {
    685 		error = EINVAL;
    686 		goto out1;
    687 	}
    688 
    689 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    690 		goto out1;
    691 
    692 	nbytes = SCARG(uap, count);
    693 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    694 		nbytes = sizeof (idb);
    695 		buflen = max(va.va_blocksize, nbytes);
    696 		oldcall = 1;
    697 	} else {
    698 		buflen = min(MAXBSIZE, nbytes);
    699 		if (buflen < va.va_blocksize)
    700 			buflen = va.va_blocksize;
    701 		oldcall = 0;
    702 	}
    703 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    704 
    705 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    706 	off = fp->f_offset;
    707 again:
    708 	aiov.iov_base = buf;
    709 	aiov.iov_len = buflen;
    710 	auio.uio_iov = &aiov;
    711 	auio.uio_iovcnt = 1;
    712 	auio.uio_rw = UIO_READ;
    713 	auio.uio_segflg = UIO_SYSSPACE;
    714 	auio.uio_procp = p;
    715 	auio.uio_resid = buflen;
    716 	auio.uio_offset = off;
    717 	/*
    718          * First we read into the malloc'ed buffer, then
    719          * we massage it into user space, one record at a time.
    720          */
    721 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    722 	    &ncookies);
    723 	if (error)
    724 		goto out;
    725 
    726 	inp = buf;
    727 	outp = (caddr_t)SCARG(uap, dent);
    728 	resid = nbytes;
    729 	if ((len = buflen - auio.uio_resid) == 0)
    730 		goto eof;
    731 
    732 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    733 		bdp = (struct dirent *)inp;
    734 		reclen = bdp->d_reclen;
    735 		if (reclen & 3)
    736 			panic("linux_readdir");
    737 		if (bdp->d_fileno == 0) {
    738 			inp += reclen;	/* it is a hole; squish it out */
    739 			off = *cookie++;
    740 			continue;
    741 		}
    742 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    743 		if (reclen > len || resid < linux_reclen) {
    744 			/* entry too big for buffer, so just stop */
    745 			outp++;
    746 			break;
    747 		}
    748 		/*
    749 		 * Massage in place to make a Linux-shaped dirent (otherwise
    750 		 * we have to worry about touching user memory outside of
    751 		 * the copyout() call).
    752 		 */
    753 		idb.d_ino = bdp->d_fileno;
    754 		/*
    755 		 * The old readdir() call misuses the offset and reclen fields.
    756 		 */
    757 		if (oldcall) {
    758 			idb.d_off = (linux_off_t)linux_reclen;
    759 			idb.d_reclen = (u_short)bdp->d_namlen;
    760 		} else {
    761 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
    762 				compat_offseterr(vp, "linux_getdents");
    763 				error = EINVAL;
    764 				goto out;
    765 			}
    766 			idb.d_off = (linux_off_t)off;
    767 			idb.d_reclen = (u_short)linux_reclen;
    768 		}
    769 		strcpy(idb.d_name, bdp->d_name);
    770 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    771 			goto out;
    772 		/* advance past this real entry */
    773 		inp += reclen;
    774 		off = *cookie++;	/* each entry points to itself */
    775 		/* advance output past Linux-shaped entry */
    776 		outp += linux_reclen;
    777 		resid -= linux_reclen;
    778 		if (oldcall)
    779 			break;
    780 	}
    781 
    782 	/* if we squished out the whole block, try again */
    783 	if (outp == (caddr_t)SCARG(uap, dent))
    784 		goto again;
    785 	fp->f_offset = off;	/* update the vnode offset */
    786 
    787 	if (oldcall)
    788 		nbytes = resid + linux_reclen;
    789 
    790 eof:
    791 	*retval = nbytes - resid;
    792 out:
    793 	VOP_UNLOCK(vp, 0);
    794 	if (cookiebuf)
    795 		free(cookiebuf, M_TEMP);
    796 	free(buf, M_TEMP);
    797 out1:
    798 	FILE_UNUSE(fp, p);
    799 	return error;
    800 }
    801 
    802 /*
    803  * Even when just using registers to pass arguments to syscalls you can
    804  * have 5 of them on the i386. So this newer version of select() does
    805  * this.
    806  */
    807 int
    808 linux_sys_select(l, v, retval)
    809 	struct lwp *l;
    810 	void *v;
    811 	register_t *retval;
    812 {
    813 	struct linux_sys_select_args /* {
    814 		syscallarg(int) nfds;
    815 		syscallarg(fd_set *) readfds;
    816 		syscallarg(fd_set *) writefds;
    817 		syscallarg(fd_set *) exceptfds;
    818 		syscallarg(struct timeval *) timeout;
    819 	} */ *uap = v;
    820 
    821 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    822 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
    823 }
    824 
    825 /*
    826  * Common code for the old and new versions of select(). A couple of
    827  * things are important:
    828  * 1) return the amount of time left in the 'timeout' parameter
    829  * 2) select never returns ERESTART on Linux, always return EINTR
    830  */
    831 int
    832 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
    833 	struct lwp *l;
    834 	register_t *retval;
    835 	int nfds;
    836 	fd_set *readfds, *writefds, *exceptfds;
    837 	struct timeval *timeout;
    838 {
    839 	struct sys_select_args bsa;
    840 	struct proc *p = l->l_proc;
    841 	struct timeval tv0, tv1, utv, *tvp;
    842 	caddr_t sg;
    843 	int error;
    844 
    845 	SCARG(&bsa, nd) = nfds;
    846 	SCARG(&bsa, in) = readfds;
    847 	SCARG(&bsa, ou) = writefds;
    848 	SCARG(&bsa, ex) = exceptfds;
    849 	SCARG(&bsa, tv) = timeout;
    850 
    851 	/*
    852 	 * Store current time for computation of the amount of
    853 	 * time left.
    854 	 */
    855 	if (timeout) {
    856 		if ((error = copyin(timeout, &utv, sizeof(utv))))
    857 			return error;
    858 		if (itimerfix(&utv)) {
    859 			/*
    860 			 * The timeval was invalid.  Convert it to something
    861 			 * valid that will act as it does under Linux.
    862 			 */
    863 			sg = stackgap_init(p, 0);
    864 			tvp = stackgap_alloc(p, &sg, sizeof(utv));
    865 			utv.tv_sec += utv.tv_usec / 1000000;
    866 			utv.tv_usec %= 1000000;
    867 			if (utv.tv_usec < 0) {
    868 				utv.tv_sec -= 1;
    869 				utv.tv_usec += 1000000;
    870 			}
    871 			if (utv.tv_sec < 0)
    872 				timerclear(&utv);
    873 			if ((error = copyout(&utv, tvp, sizeof(utv))))
    874 				return error;
    875 			SCARG(&bsa, tv) = tvp;
    876 		}
    877 		microtime(&tv0);
    878 	}
    879 
    880 	error = sys_select(l, &bsa, retval);
    881 	if (error) {
    882 		/*
    883 		 * See fs/select.c in the Linux kernel.  Without this,
    884 		 * Maelstrom doesn't work.
    885 		 */
    886 		if (error == ERESTART)
    887 			error = EINTR;
    888 		return error;
    889 	}
    890 
    891 	if (timeout) {
    892 		if (*retval) {
    893 			/*
    894 			 * Compute how much time was left of the timeout,
    895 			 * by subtracting the current time and the time
    896 			 * before we started the call, and subtracting
    897 			 * that result from the user-supplied value.
    898 			 */
    899 			microtime(&tv1);
    900 			timersub(&tv1, &tv0, &tv1);
    901 			timersub(&utv, &tv1, &utv);
    902 			if (utv.tv_sec < 0)
    903 				timerclear(&utv);
    904 		} else
    905 			timerclear(&utv);
    906 		if ((error = copyout(&utv, timeout, sizeof(utv))))
    907 			return error;
    908 	}
    909 
    910 	return 0;
    911 }
    912 
    913 /*
    914  * Get the process group of a certain process. Look it up
    915  * and return the value.
    916  */
    917 int
    918 linux_sys_getpgid(l, v, retval)
    919 	struct lwp *l;
    920 	void *v;
    921 	register_t *retval;
    922 {
    923 	struct linux_sys_getpgid_args /* {
    924 		syscallarg(int) pid;
    925 	} */ *uap = v;
    926 	struct proc *p = l->l_proc;
    927 	struct proc *targp;
    928 
    929 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
    930 		if ((targp = pfind(SCARG(uap, pid))) == 0)
    931 			return ESRCH;
    932 	}
    933 	else
    934 		targp = p;
    935 
    936 	retval[0] = targp->p_pgid;
    937 	return 0;
    938 }
    939 
    940 /*
    941  * Set the 'personality' (emulation mode) for the current process. Only
    942  * accept the Linux personality here (0). This call is needed because
    943  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
    944  * ELF binaries run in Linux mode, not SVR4 mode.
    945  */
    946 int
    947 linux_sys_personality(l, v, retval)
    948 	struct lwp *l;
    949 	void *v;
    950 	register_t *retval;
    951 {
    952 	struct linux_sys_personality_args /* {
    953 		syscallarg(int) per;
    954 	} */ *uap = v;
    955 
    956 	if (SCARG(uap, per) != 0)
    957 		return EINVAL;
    958 	retval[0] = 0;
    959 	return 0;
    960 }
    961 
    962 #if defined(__i386__) || defined(__m68k__)
    963 /*
    964  * The calls are here because of type conversions.
    965  */
    966 int
    967 linux_sys_setreuid16(l, v, retval)
    968 	struct lwp *l;
    969 	void *v;
    970 	register_t *retval;
    971 {
    972 	struct linux_sys_setreuid16_args /* {
    973 		syscallarg(int) ruid;
    974 		syscallarg(int) euid;
    975 	} */ *uap = v;
    976 	struct sys_setreuid_args bsa;
    977 
    978 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    979 		(uid_t)-1 : SCARG(uap, ruid);
    980 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    981 		(uid_t)-1 : SCARG(uap, euid);
    982 
    983 	return sys_setreuid(l, &bsa, retval);
    984 }
    985 
    986 int
    987 linux_sys_setregid16(l, v, retval)
    988 	struct lwp *l;
    989 	void *v;
    990 	register_t *retval;
    991 {
    992 	struct linux_sys_setregid16_args /* {
    993 		syscallarg(int) rgid;
    994 		syscallarg(int) egid;
    995 	} */ *uap = v;
    996 	struct sys_setregid_args bsa;
    997 
    998 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    999 		(uid_t)-1 : SCARG(uap, rgid);
   1000 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1001 		(uid_t)-1 : SCARG(uap, egid);
   1002 
   1003 	return sys_setregid(l, &bsa, retval);
   1004 }
   1005 
   1006 int
   1007 linux_sys_setresuid16(l, v, retval)
   1008 	struct lwp *l;
   1009 	void *v;
   1010 	register_t *retval;
   1011 {
   1012 	struct linux_sys_setresuid16_args /* {
   1013 		syscallarg(uid_t) ruid;
   1014 		syscallarg(uid_t) euid;
   1015 		syscallarg(uid_t) suid;
   1016 	} */ *uap = v;
   1017 	struct linux_sys_setresuid16_args lsa;
   1018 
   1019 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
   1020 		(uid_t)-1 : SCARG(uap, ruid);
   1021 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
   1022 		(uid_t)-1 : SCARG(uap, euid);
   1023 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
   1024 		(uid_t)-1 : SCARG(uap, suid);
   1025 
   1026 	return linux_sys_setresuid(l, &lsa, retval);
   1027 }
   1028 
   1029 int
   1030 linux_sys_setresgid16(l, v, retval)
   1031 	struct lwp *l;
   1032 	void *v;
   1033 	register_t *retval;
   1034 {
   1035 	struct linux_sys_setresgid16_args /* {
   1036 		syscallarg(gid_t) rgid;
   1037 		syscallarg(gid_t) egid;
   1038 		syscallarg(gid_t) sgid;
   1039 	} */ *uap = v;
   1040 	struct linux_sys_setresgid16_args lsa;
   1041 
   1042 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
   1043 		(gid_t)-1 : SCARG(uap, rgid);
   1044 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1045 		(gid_t)-1 : SCARG(uap, egid);
   1046 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
   1047 		(gid_t)-1 : SCARG(uap, sgid);
   1048 
   1049 	return linux_sys_setresgid(l, &lsa, retval);
   1050 }
   1051 
   1052 int
   1053 linux_sys_getgroups16(l, v, retval)
   1054 	struct lwp *l;
   1055 	void *v;
   1056 	register_t *retval;
   1057 {
   1058 	struct linux_sys_getgroups16_args /* {
   1059 		syscallarg(int) gidsetsize;
   1060 		syscallarg(linux_gid_t *) gidset;
   1061 	} */ *uap = v;
   1062 	struct proc *p = l->l_proc;
   1063 	caddr_t sg;
   1064 	int n, error, i;
   1065 	struct sys_getgroups_args bsa;
   1066 	gid_t *bset, *kbset;
   1067 	linux_gid_t *lset;
   1068 	struct pcred *pc = p->p_cred;
   1069 
   1070 	n = SCARG(uap, gidsetsize);
   1071 	if (n < 0)
   1072 		return EINVAL;
   1073 	error = 0;
   1074 	bset = kbset = NULL;
   1075 	lset = NULL;
   1076 	if (n > 0) {
   1077 		n = min(pc->pc_ucred->cr_ngroups, n);
   1078 		sg = stackgap_init(p, 0);
   1079 		bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
   1080 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
   1081 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1082 		if (bset == NULL || kbset == NULL || lset == NULL)
   1083 			return ENOMEM;
   1084 		SCARG(&bsa, gidsetsize) = n;
   1085 		SCARG(&bsa, gidset) = bset;
   1086 		error = sys_getgroups(l, &bsa, retval);
   1087 		if (error != 0)
   1088 			goto out;
   1089 		error = copyin(bset, kbset, n * sizeof (gid_t));
   1090 		if (error != 0)
   1091 			goto out;
   1092 		for (i = 0; i < n; i++)
   1093 			lset[i] = (linux_gid_t)kbset[i];
   1094 		error = copyout(lset, SCARG(uap, gidset),
   1095 		    n * sizeof (linux_gid_t));
   1096 	} else
   1097 		*retval = pc->pc_ucred->cr_ngroups;
   1098 out:
   1099 	if (kbset != NULL)
   1100 		free(kbset, M_TEMP);
   1101 	if (lset != NULL)
   1102 		free(lset, M_TEMP);
   1103 	return error;
   1104 }
   1105 
   1106 int
   1107 linux_sys_setgroups16(l, v, retval)
   1108 	struct lwp *l;
   1109 	void *v;
   1110 	register_t *retval;
   1111 {
   1112 	struct linux_sys_setgroups16_args /* {
   1113 		syscallarg(int) gidsetsize;
   1114 		syscallarg(linux_gid_t *) gidset;
   1115 	} */ *uap = v;
   1116 	struct proc *p = l->l_proc;
   1117 	caddr_t sg;
   1118 	int n;
   1119 	int error, i;
   1120 	struct sys_setgroups_args bsa;
   1121 	gid_t *bset, *kbset;
   1122 	linux_gid_t *lset;
   1123 
   1124 	n = SCARG(uap, gidsetsize);
   1125 	if (n < 0 || n > NGROUPS)
   1126 		return EINVAL;
   1127 	sg = stackgap_init(p, 0);
   1128 	bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
   1129 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1130 	kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
   1131 	if (lset == NULL || bset == NULL)
   1132 		return ENOMEM;
   1133 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
   1134 	if (error != 0)
   1135 		goto out;
   1136 	for (i = 0; i < n; i++)
   1137 		kbset[i] = (gid_t)lset[i];
   1138 	error = copyout(kbset, bset, n * sizeof (gid_t));
   1139 	if (error != 0)
   1140 		goto out;
   1141 	SCARG(&bsa, gidsetsize) = n;
   1142 	SCARG(&bsa, gidset) = bset;
   1143 	error = sys_setgroups(l, &bsa, retval);
   1144 
   1145 out:
   1146 	if (lset != NULL)
   1147 		free(lset, M_TEMP);
   1148 	if (kbset != NULL)
   1149 		free(kbset, M_TEMP);
   1150 
   1151 	return error;
   1152 }
   1153 
   1154 #endif /* __i386__ || __m68k__ */
   1155 
   1156 /*
   1157  * We have nonexistent fsuid equal to uid.
   1158  * If modification is requested, refuse.
   1159  */
   1160 int
   1161 linux_sys_setfsuid(l, v, retval)
   1162 	 struct lwp *l;
   1163 	 void *v;
   1164 	 register_t *retval;
   1165 {
   1166 	 struct linux_sys_setfsuid_args /* {
   1167 		 syscallarg(uid_t) uid;
   1168 	 } */ *uap = v;
   1169 	 struct proc *p = l->l_proc;
   1170 	 uid_t uid;
   1171 
   1172 	 uid = SCARG(uap, uid);
   1173 	 if (p->p_cred->p_ruid != uid)
   1174 		 return sys_nosys(l, v, retval);
   1175 	 else
   1176 		 return (0);
   1177 }
   1178 
   1179 /* XXX XXX XXX */
   1180 #ifndef alpha
   1181 int
   1182 linux_sys_getfsuid(l, v, retval)
   1183 	struct lwp *l;
   1184 	void *v;
   1185 	register_t *retval;
   1186 {
   1187 	return sys_getuid(l, v, retval);
   1188 }
   1189 #endif
   1190 
   1191 int
   1192 linux_sys_setresuid(l, v, retval)
   1193 	struct lwp *l;
   1194 	void *v;
   1195 	register_t *retval;
   1196 {
   1197 	struct linux_sys_setresuid_args /* {
   1198 		syscallarg(uid_t) ruid;
   1199 		syscallarg(uid_t) euid;
   1200 		syscallarg(uid_t) suid;
   1201 	} */ *uap = v;
   1202 	struct proc *p = l->l_proc;
   1203 	struct pcred *pc = p->p_cred;
   1204 	uid_t ruid, euid, suid;
   1205 	int error;
   1206 
   1207 	ruid = SCARG(uap, ruid);
   1208 	euid = SCARG(uap, euid);
   1209 	suid = SCARG(uap, suid);
   1210 
   1211 	/*
   1212 	 * Note: These checks are a little different than the NetBSD
   1213 	 * setreuid(2) call performs.  This precisely follows the
   1214 	 * behavior of the Linux kernel.
   1215 	 */
   1216 	if (ruid != (uid_t)-1 &&
   1217 	    ruid != pc->p_ruid &&
   1218 	    ruid != pc->pc_ucred->cr_uid &&
   1219 	    ruid != pc->p_svuid &&
   1220 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1221 		return (error);
   1222 
   1223 	if (euid != (uid_t)-1 &&
   1224 	    euid != pc->p_ruid &&
   1225 	    euid != pc->pc_ucred->cr_uid &&
   1226 	    euid != pc->p_svuid &&
   1227 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1228 		return (error);
   1229 
   1230 	if (suid != (uid_t)-1 &&
   1231 	    suid != pc->p_ruid &&
   1232 	    suid != pc->pc_ucred->cr_uid &&
   1233 	    suid != pc->p_svuid &&
   1234 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1235 		return (error);
   1236 
   1237 	/*
   1238 	 * Now assign the new real, effective, and saved UIDs.
   1239 	 * Note that Linux, unlike NetBSD in setreuid(2), does not
   1240 	 * set the saved UID in this call unless the user specifies
   1241 	 * it.
   1242 	 */
   1243 	if (ruid != (uid_t)-1) {
   1244 		(void)chgproccnt(pc->p_ruid, -1);
   1245 		(void)chgproccnt(ruid, 1);
   1246 		pc->p_ruid = ruid;
   1247 	}
   1248 
   1249 	if (euid != (uid_t)-1) {
   1250 		pc->pc_ucred = crcopy(pc->pc_ucred);
   1251 		pc->pc_ucred->cr_uid = euid;
   1252 	}
   1253 
   1254 	if (suid != (uid_t)-1)
   1255 		pc->p_svuid = suid;
   1256 
   1257 	if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
   1258 		p->p_flag |= P_SUGID;
   1259 	return (0);
   1260 }
   1261 
   1262 int
   1263 linux_sys_getresuid(l, v, retval)
   1264 	struct lwp *l;
   1265 	void *v;
   1266 	register_t *retval;
   1267 {
   1268 	struct linux_sys_getresuid_args /* {
   1269 		syscallarg(uid_t *) ruid;
   1270 		syscallarg(uid_t *) euid;
   1271 		syscallarg(uid_t *) suid;
   1272 	} */ *uap = v;
   1273 	struct proc *p = l->l_proc;
   1274 	struct pcred *pc = p->p_cred;
   1275 	int error;
   1276 
   1277 	/*
   1278 	 * Linux copies these values out to userspace like so:
   1279 	 *
   1280 	 *	1. Copy out ruid.
   1281 	 *	2. If that succeeds, copy out euid.
   1282 	 *	3. If both of those succeed, copy out suid.
   1283 	 */
   1284 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
   1285 			     sizeof(uid_t))) != 0)
   1286 		return (error);
   1287 
   1288 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
   1289 			     sizeof(uid_t))) != 0)
   1290 		return (error);
   1291 
   1292 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
   1293 }
   1294 
   1295 int
   1296 linux_sys_ptrace(l, v, retval)
   1297 	struct lwp *l;
   1298 	void *v;
   1299 	register_t *retval;
   1300 {
   1301 	struct linux_sys_ptrace_args /* {
   1302 		i386, m68k, powerpc: T=int
   1303 		alpha: T=long
   1304 		syscallarg(T) request;
   1305 		syscallarg(T) pid;
   1306 		syscallarg(T) addr;
   1307 		syscallarg(T) data;
   1308 	} */ *uap = v;
   1309 	const int *ptr;
   1310 	int request;
   1311 	int error;
   1312 
   1313 	ptr = linux_ptrace_request_map;
   1314 	request = SCARG(uap, request);
   1315 	while (*ptr != -1)
   1316 		if (*ptr++ == request) {
   1317 			struct sys_ptrace_args pta;
   1318 
   1319 			SCARG(&pta, req) = *ptr;
   1320 			SCARG(&pta, pid) = SCARG(uap, pid);
   1321 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
   1322 			SCARG(&pta, data) = SCARG(uap, data);
   1323 
   1324 			/*
   1325 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1326 			 * to continue where the process left off previously.
   1327 			 * The same thing is achieved by addr == (caddr_t) 1
   1328 			 * on NetBSD, so rewrite 'addr' appropriately.
   1329 			 */
   1330 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1331 				SCARG(&pta, addr) = (caddr_t) 1;
   1332 
   1333 			error = sys_ptrace(l, &pta, retval);
   1334 			if (error)
   1335 				return error;
   1336 			switch (request) {
   1337 			case LINUX_PTRACE_PEEKTEXT:
   1338 			case LINUX_PTRACE_PEEKDATA:
   1339 				error = copyout (retval,
   1340 				    (caddr_t)SCARG(uap, data), sizeof *retval);
   1341 				*retval = SCARG(uap, data);
   1342 				break;
   1343 			default:
   1344 				break;
   1345 			}
   1346 			return error;
   1347 		}
   1348 		else
   1349 			ptr++;
   1350 
   1351 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1352 }
   1353 
   1354 int
   1355 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
   1356 {
   1357 	struct linux_sys_reboot_args /* {
   1358 		syscallarg(int) magic1;
   1359 		syscallarg(int) magic2;
   1360 		syscallarg(int) cmd;
   1361 		syscallarg(void *) arg;
   1362 	} */ *uap = v;
   1363 	struct sys_reboot_args /* {
   1364 		syscallarg(int) opt;
   1365 		syscallarg(char *) bootstr;
   1366 	} */ sra;
   1367 	struct proc *p = l->l_proc;
   1368 	int error;
   1369 
   1370 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1371 		return(error);
   1372 
   1373 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1374 		return(EINVAL);
   1375 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1376 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1377 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1378 		return(EINVAL);
   1379 
   1380 	switch (SCARG(uap, cmd)) {
   1381 	case LINUX_REBOOT_CMD_RESTART:
   1382 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1383 		break;
   1384 	case LINUX_REBOOT_CMD_HALT:
   1385 		SCARG(&sra, opt) = RB_HALT;
   1386 		break;
   1387 	case LINUX_REBOOT_CMD_POWER_OFF:
   1388 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1389 		break;
   1390 	case LINUX_REBOOT_CMD_RESTART2:
   1391 		/* Reboot with an argument. */
   1392 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1393 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1394 		break;
   1395 	case LINUX_REBOOT_CMD_CAD_ON:
   1396 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1397 	case LINUX_REBOOT_CMD_CAD_OFF:
   1398 		return(0);
   1399 	default:
   1400 		return(EINVAL);
   1401 	}
   1402 
   1403 	return(sys_reboot(l, &sra, retval));
   1404 }
   1405 
   1406 /*
   1407  * Copy of compat_12_sys_swapon().
   1408  */
   1409 int
   1410 linux_sys_swapon(l, v, retval)
   1411 	struct lwp *l;
   1412 	void *v;
   1413 	register_t *retval;
   1414 {
   1415 	struct sys_swapctl_args ua;
   1416 	struct linux_sys_swapon_args /* {
   1417 		syscallarg(const char *) name;
   1418 	} */ *uap = v;
   1419 
   1420 	SCARG(&ua, cmd) = SWAP_ON;
   1421 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
   1422 	SCARG(&ua, misc) = 0;	/* priority */
   1423 	return (sys_swapctl(l, &ua, retval));
   1424 }
   1425 
   1426 /*
   1427  * Stop swapping to the file or block device specified by path.
   1428  */
   1429 int
   1430 linux_sys_swapoff(l, v, retval)
   1431 	struct lwp *l;
   1432 	void *v;
   1433 	register_t *retval;
   1434 {
   1435 	struct sys_swapctl_args ua;
   1436 	struct linux_sys_swapoff_args /* {
   1437 		syscallarg(const char *) path;
   1438 	} */ *uap = v;
   1439 
   1440 	SCARG(&ua, cmd) = SWAP_OFF;
   1441 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
   1442 	return (sys_swapctl(l, &ua, retval));
   1443 }
   1444 
   1445 /*
   1446  * Copy of compat_09_sys_setdomainname()
   1447  */
   1448 /* ARGSUSED */
   1449 int
   1450 linux_sys_setdomainname(l, v, retval)
   1451 	struct lwp *l;
   1452 	void *v;
   1453 	register_t *retval;
   1454 {
   1455 	struct linux_sys_setdomainname_args /* {
   1456 		syscallarg(char *) domainname;
   1457 		syscallarg(int) len;
   1458 	} */ *uap = v;
   1459 	struct proc *p = l->l_proc;
   1460 	int name;
   1461 	int error;
   1462 
   1463 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1464 		return (error);
   1465 	name = KERN_DOMAINNAME;
   1466 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
   1467 			    SCARG(uap, len), p));
   1468 }
   1469 
   1470 /*
   1471  * sysinfo()
   1472  */
   1473 /* ARGSUSED */
   1474 int
   1475 linux_sys_sysinfo(l, v, retval)
   1476 	struct lwp *l;
   1477 	void *v;
   1478 	register_t *retval;
   1479 {
   1480 	struct linux_sys_sysinfo_args /* {
   1481 		syscallarg(struct linux_sysinfo *) arg;
   1482 	} */ *uap = v;
   1483 	struct linux_sysinfo si;
   1484 	struct loadavg *la;
   1485 
   1486 	si.uptime = time.tv_sec - boottime.tv_sec;
   1487 	la = &averunnable;
   1488 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1489 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1490 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1491 	si.totalram = ctob(physmem);
   1492 	si.freeram = uvmexp.free * uvmexp.pagesize;
   1493 	si.sharedram = 0;	/* XXX */
   1494 	si.bufferram = uvmexp.filepages * uvmexp.pagesize;
   1495 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
   1496 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1497 	si.procs = nprocs;
   1498 
   1499 	/* The following are only present in newer Linux kernels. */
   1500 	si.totalbig = 0;
   1501 	si.freebig = 0;
   1502 	si.mem_unit = 1;
   1503 
   1504 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1505 }
   1506 
   1507 #define bsd_to_linux_rlimit1(l, b, f) \
   1508     (l)->f = ((b)->f == RLIM_INFINITY || ((b)->f & 0xffffffff00000000) != 0) ? \
   1509     LINUX_RLIM_INFINITY : (int32_t)(b)->f
   1510 #define bsd_to_linux_rlimit(l, b) \
   1511     bsd_to_linux_rlimit1(l, b, rlim_cur); \
   1512     bsd_to_linux_rlimit1(l, b, rlim_max)
   1513 
   1514 #define linux_to_bsd_rlimit1(b, l, f) \
   1515     (b)->f = (l)->f == LINUX_RLIM_INFINITY ? RLIM_INFINITY : (b)->f
   1516 #define linux_to_bsd_rlimit(b, l) \
   1517     linux_to_bsd_rlimit1(b, l, rlim_cur); \
   1518     linux_to_bsd_rlimit1(b, l, rlim_max)
   1519 
   1520 static int
   1521 linux_to_bsd_limit(lim)
   1522 	int lim;
   1523 {
   1524 	switch (lim) {
   1525 	case LINUX_RLIMIT_CPU:
   1526 		return RLIMIT_CPU;
   1527 	case LINUX_RLIMIT_FSIZE:
   1528 		return RLIMIT_FSIZE;
   1529 	case LINUX_RLIMIT_DATA:
   1530 		return RLIMIT_DATA;
   1531 	case LINUX_RLIMIT_STACK:
   1532 		return RLIMIT_STACK;
   1533 	case LINUX_RLIMIT_CORE:
   1534 		return RLIMIT_CORE;
   1535 	case LINUX_RLIMIT_RSS:
   1536 		return RLIMIT_RSS;
   1537 	case LINUX_RLIMIT_NPROC:
   1538 		return RLIMIT_NPROC;
   1539 	case LINUX_RLIMIT_NOFILE:
   1540 		return RLIMIT_NOFILE;
   1541 	case LINUX_RLIMIT_MEMLOCK:
   1542 		return RLIMIT_MEMLOCK;
   1543 	case LINUX_RLIMIT_AS:
   1544 	case LINUX_RLIMIT_LOCKS:
   1545 		return -EOPNOTSUPP;
   1546 	default:
   1547 		return -EINVAL;
   1548 	}
   1549 }
   1550 
   1551 
   1552 int
   1553 linux_sys_getrlimit(l, v, retval)
   1554 	struct lwp *l;
   1555 	void *v;
   1556 	register_t *retval;
   1557 {
   1558 	struct linux_sys_getrlimit_args /* {
   1559 		syscallarg(int) which;
   1560 		syscallarg(struct orlimit *) rlp;
   1561 	} */ *uap = v;
   1562 	struct proc *p = l->l_proc;
   1563 	caddr_t sg = stackgap_init(p, 0);
   1564 	struct sys_getrlimit_args ap;
   1565 	struct rlimit rl;
   1566 	struct orlimit orl;
   1567 	int error;
   1568 
   1569 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
   1570 	if ((error = SCARG(&ap, which)) < 0)
   1571 		return -error;
   1572 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
   1573 	if ((error = sys_getrlimit(l, &ap, retval)) != 0)
   1574 		return error;
   1575 	if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
   1576 		return error;
   1577 	bsd_to_linux_rlimit(&orl, &rl);
   1578 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
   1579 }
   1580 
   1581 int
   1582 linux_sys_setrlimit(l, v, retval)
   1583 	struct lwp *l;
   1584 	void *v;
   1585 	register_t *retval;
   1586 {
   1587 	struct linux_sys_setrlimit_args /* {
   1588 		syscallarg(int) which;
   1589 		syscallarg(struct orlimit *) rlp;
   1590 	} */ *uap = v;
   1591 	struct proc *p = l->l_proc;
   1592 	caddr_t sg = stackgap_init(p, 0);
   1593 	struct sys_setrlimit_args ap;
   1594 	struct rlimit rl;
   1595 	struct orlimit orl;
   1596 	int error;
   1597 
   1598 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
   1599 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
   1600 	if ((error = SCARG(&ap, which)) < 0)
   1601 		return -error;
   1602 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
   1603 		return error;
   1604 	linux_to_bsd_rlimit(&rl, &orl);
   1605 	/* XXX: alpha complains about this */
   1606 	if ((error = copyout(&rl, (void *)SCARG(&ap, rlp), sizeof(rl))) != 0)
   1607 		return error;
   1608 	return sys_setrlimit(l, &ap, retval);
   1609 }
   1610 
   1611 #ifndef __mips__
   1612 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
   1613 int
   1614 linux_sys_ugetrlimit(l, v, retval)
   1615 	struct lwp *l;
   1616 	void *v;
   1617 	register_t *retval;
   1618 {
   1619 	return linux_sys_getrlimit(l, v, retval);
   1620 }
   1621 #endif
   1622 
   1623 /*
   1624  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1625  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1626  * This is the way Linux does it and glibc depends on this behaviour.
   1627  */
   1628 int
   1629 linux_sys_nosys(l, v, retval)
   1630 	struct lwp *l;
   1631 	void *v;
   1632 	register_t *retval;
   1633 {
   1634 	return (ENOSYS);
   1635 }
   1636