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