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