Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.135.2.2
      1 /*	$NetBSD: linux_misc.c,v 1.135.2.2 2005/11/01 22:31:17 tron 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.2.2 2005/11/01 22:31:17 tron 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 	struct vm_map_entry *entry;
    673 	struct vm_map *map;
    674 	struct proc *p;
    675 	vaddr_t end, start, len, stacklim;
    676 	int prot, grows;
    677 
    678 	start = (vaddr_t)SCARG(uap, start);
    679 	len = round_page(SCARG(uap, len));
    680 	prot = SCARG(uap, prot);
    681 	grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
    682 	prot &= ~grows;
    683 	end = start + len;
    684 
    685 	if (start & PAGE_MASK)
    686 		return EINVAL;
    687 	if (end < start)
    688 		return EINVAL;
    689 	if (end == start)
    690 		return 0;
    691 
    692 	if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
    693 		return EINVAL;
    694 	if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
    695 		return EINVAL;
    696 
    697 	p = l->l_proc;
    698 	map = &p->p_vmspace->vm_map;
    699 	vm_map_lock(map);
    700 #ifdef notdef
    701 	VM_MAP_RANGE_CHECK(map, start, end);
    702 #endif
    703 	if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
    704 		vm_map_unlock(map);
    705 		return ENOMEM;
    706 	}
    707 
    708 	/*
    709 	 * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
    710 	 */
    711 
    712 	stacklim = (vaddr_t)p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
    713 	if (grows & LINUX_PROT_GROWSDOWN) {
    714 		if (USRSTACK - stacklim <= start && start < USRSTACK) {
    715 			start = USRSTACK - stacklim;
    716 		} else {
    717 			start = entry->start;
    718 		}
    719 	} else if (grows & LINUX_PROT_GROWSUP) {
    720 		if (USRSTACK <= end && end < USRSTACK + stacklim) {
    721 			end = USRSTACK + stacklim;
    722 		} else {
    723 			end = entry->end;
    724 		}
    725 	}
    726 	vm_map_unlock(map);
    727 	return uvm_map_protect(map, start, end, prot, FALSE);
    728 }
    729 
    730 /*
    731  * This code is partly stolen from src/lib/libc/compat-43/times.c
    732  */
    733 
    734 #define	CONVTCK(r)	(r.tv_sec * hz + r.tv_usec / (1000000 / hz))
    735 
    736 int
    737 linux_sys_times(l, v, retval)
    738 	struct lwp *l;
    739 	void *v;
    740 	register_t *retval;
    741 {
    742 	struct linux_sys_times_args /* {
    743 		syscallarg(struct times *) tms;
    744 	} */ *uap = v;
    745 	struct proc *p = l->l_proc;
    746 	struct timeval t;
    747 	int error, s;
    748 
    749 	if (SCARG(uap, tms)) {
    750 		struct linux_tms ltms;
    751 		struct rusage ru;
    752 
    753 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    754 		ltms.ltms_utime = CONVTCK(ru.ru_utime);
    755 		ltms.ltms_stime = CONVTCK(ru.ru_stime);
    756 
    757 		ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    758 		ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    759 
    760 		if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    761 			return error;
    762 	}
    763 
    764 	s = splclock();
    765 	timersub(&time, &boottime, &t);
    766 	splx(s);
    767 
    768 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    769 	return 0;
    770 }
    771 
    772 #undef CONVTCK
    773 
    774 /*
    775  * Linux 'readdir' call. This code is mostly taken from the
    776  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    777  * an attempt has been made to keep it a little cleaner (failing
    778  * miserably, because of the cruft needed if count 1 is passed).
    779  *
    780  * The d_off field should contain the offset of the next valid entry,
    781  * but in Linux it has the offset of the entry itself. We emulate
    782  * that bug here.
    783  *
    784  * Read in BSD-style entries, convert them, and copy them out.
    785  *
    786  * Note that this doesn't handle union-mounted filesystems.
    787  */
    788 int
    789 linux_sys_getdents(l, v, retval)
    790 	struct lwp *l;
    791 	void *v;
    792 	register_t *retval;
    793 {
    794 	struct linux_sys_getdents_args /* {
    795 		syscallarg(int) fd;
    796 		syscallarg(struct linux_dirent *) dent;
    797 		syscallarg(unsigned int) count;
    798 	} */ *uap = v;
    799 	struct proc *p = l->l_proc;
    800 	struct dirent *bdp;
    801 	struct vnode *vp;
    802 	caddr_t	inp, buf;		/* BSD-format */
    803 	int len, reclen;		/* BSD-format */
    804 	caddr_t outp;			/* Linux-format */
    805 	int resid, linux_reclen = 0;	/* Linux-format */
    806 	struct file *fp;
    807 	struct uio auio;
    808 	struct iovec aiov;
    809 	struct linux_dirent idb;
    810 	off_t off;		/* true file offset */
    811 	int buflen, error, eofflag, nbytes, oldcall;
    812 	struct vattr va;
    813 	off_t *cookiebuf = NULL, *cookie;
    814 	int ncookies;
    815 
    816 	/* getvnode() will use the descriptor for us */
    817 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    818 		return (error);
    819 
    820 	if ((fp->f_flag & FREAD) == 0) {
    821 		error = EBADF;
    822 		goto out1;
    823 	}
    824 
    825 	vp = (struct vnode *)fp->f_data;
    826 	if (vp->v_type != VDIR) {
    827 		error = EINVAL;
    828 		goto out1;
    829 	}
    830 
    831 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    832 		goto out1;
    833 
    834 	nbytes = SCARG(uap, count);
    835 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    836 		nbytes = sizeof (idb);
    837 		buflen = max(va.va_blocksize, nbytes);
    838 		oldcall = 1;
    839 	} else {
    840 		buflen = min(MAXBSIZE, nbytes);
    841 		if (buflen < va.va_blocksize)
    842 			buflen = va.va_blocksize;
    843 		oldcall = 0;
    844 	}
    845 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    846 
    847 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    848 	off = fp->f_offset;
    849 again:
    850 	aiov.iov_base = buf;
    851 	aiov.iov_len = buflen;
    852 	auio.uio_iov = &aiov;
    853 	auio.uio_iovcnt = 1;
    854 	auio.uio_rw = UIO_READ;
    855 	auio.uio_segflg = UIO_SYSSPACE;
    856 	auio.uio_procp = NULL;
    857 	auio.uio_resid = buflen;
    858 	auio.uio_offset = off;
    859 	/*
    860          * First we read into the malloc'ed buffer, then
    861          * we massage it into user space, one record at a time.
    862          */
    863 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    864 	    &ncookies);
    865 	if (error)
    866 		goto out;
    867 
    868 	inp = buf;
    869 	outp = (caddr_t)SCARG(uap, dent);
    870 	resid = nbytes;
    871 	if ((len = buflen - auio.uio_resid) == 0)
    872 		goto eof;
    873 
    874 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    875 		bdp = (struct dirent *)inp;
    876 		reclen = bdp->d_reclen;
    877 		if (reclen & 3)
    878 			panic("linux_readdir");
    879 		if (bdp->d_fileno == 0) {
    880 			inp += reclen;	/* it is a hole; squish it out */
    881 			if (cookie)
    882 				off = *cookie++;
    883 			else
    884 				off += reclen;
    885 			continue;
    886 		}
    887 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    888 		if (reclen > len || resid < linux_reclen) {
    889 			/* entry too big for buffer, so just stop */
    890 			outp++;
    891 			break;
    892 		}
    893 		/*
    894 		 * Massage in place to make a Linux-shaped dirent (otherwise
    895 		 * we have to worry about touching user memory outside of
    896 		 * the copyout() call).
    897 		 */
    898 		idb.d_ino = bdp->d_fileno;
    899 		/*
    900 		 * The old readdir() call misuses the offset and reclen fields.
    901 		 */
    902 		if (oldcall) {
    903 			idb.d_off = (linux_off_t)linux_reclen;
    904 			idb.d_reclen = (u_short)bdp->d_namlen;
    905 		} else {
    906 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
    907 				compat_offseterr(vp, "linux_getdents");
    908 				error = EINVAL;
    909 				goto out;
    910 			}
    911 			idb.d_off = (linux_off_t)off;
    912 			idb.d_reclen = (u_short)linux_reclen;
    913 		}
    914 		strcpy(idb.d_name, bdp->d_name);
    915 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    916 			goto out;
    917 		/* advance past this real entry */
    918 		inp += reclen;
    919 		if (cookie)
    920 			off = *cookie++; /* each entry points to itself */
    921 		else
    922 			off += reclen;
    923 		/* advance output past Linux-shaped entry */
    924 		outp += linux_reclen;
    925 		resid -= linux_reclen;
    926 		if (oldcall)
    927 			break;
    928 	}
    929 
    930 	/* if we squished out the whole block, try again */
    931 	if (outp == (caddr_t)SCARG(uap, dent))
    932 		goto again;
    933 	fp->f_offset = off;	/* update the vnode offset */
    934 
    935 	if (oldcall)
    936 		nbytes = resid + linux_reclen;
    937 
    938 eof:
    939 	*retval = nbytes - resid;
    940 out:
    941 	VOP_UNLOCK(vp, 0);
    942 	if (cookiebuf)
    943 		free(cookiebuf, M_TEMP);
    944 	free(buf, M_TEMP);
    945 out1:
    946 	FILE_UNUSE(fp, p);
    947 	return error;
    948 }
    949 
    950 /*
    951  * Even when just using registers to pass arguments to syscalls you can
    952  * have 5 of them on the i386. So this newer version of select() does
    953  * this.
    954  */
    955 int
    956 linux_sys_select(l, v, retval)
    957 	struct lwp *l;
    958 	void *v;
    959 	register_t *retval;
    960 {
    961 	struct linux_sys_select_args /* {
    962 		syscallarg(int) nfds;
    963 		syscallarg(fd_set *) readfds;
    964 		syscallarg(fd_set *) writefds;
    965 		syscallarg(fd_set *) exceptfds;
    966 		syscallarg(struct timeval *) timeout;
    967 	} */ *uap = v;
    968 
    969 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    970 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
    971 }
    972 
    973 /*
    974  * Common code for the old and new versions of select(). A couple of
    975  * things are important:
    976  * 1) return the amount of time left in the 'timeout' parameter
    977  * 2) select never returns ERESTART on Linux, always return EINTR
    978  */
    979 int
    980 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
    981 	struct lwp *l;
    982 	register_t *retval;
    983 	int nfds;
    984 	fd_set *readfds, *writefds, *exceptfds;
    985 	struct timeval *timeout;
    986 {
    987 	struct sys_select_args bsa;
    988 	struct proc *p = l->l_proc;
    989 	struct timeval tv0, tv1, utv, *tvp;
    990 	caddr_t sg;
    991 	int error;
    992 
    993 	SCARG(&bsa, nd) = nfds;
    994 	SCARG(&bsa, in) = readfds;
    995 	SCARG(&bsa, ou) = writefds;
    996 	SCARG(&bsa, ex) = exceptfds;
    997 	SCARG(&bsa, tv) = timeout;
    998 
    999 	/*
   1000 	 * Store current time for computation of the amount of
   1001 	 * time left.
   1002 	 */
   1003 	if (timeout) {
   1004 		if ((error = copyin(timeout, &utv, sizeof(utv))))
   1005 			return error;
   1006 		if (itimerfix(&utv)) {
   1007 			/*
   1008 			 * The timeval was invalid.  Convert it to something
   1009 			 * valid that will act as it does under Linux.
   1010 			 */
   1011 			sg = stackgap_init(p, 0);
   1012 			tvp = stackgap_alloc(p, &sg, sizeof(utv));
   1013 			utv.tv_sec += utv.tv_usec / 1000000;
   1014 			utv.tv_usec %= 1000000;
   1015 			if (utv.tv_usec < 0) {
   1016 				utv.tv_sec -= 1;
   1017 				utv.tv_usec += 1000000;
   1018 			}
   1019 			if (utv.tv_sec < 0)
   1020 				timerclear(&utv);
   1021 			if ((error = copyout(&utv, tvp, sizeof(utv))))
   1022 				return error;
   1023 			SCARG(&bsa, tv) = tvp;
   1024 		}
   1025 		microtime(&tv0);
   1026 	}
   1027 
   1028 	error = sys_select(l, &bsa, retval);
   1029 	if (error) {
   1030 		/*
   1031 		 * See fs/select.c in the Linux kernel.  Without this,
   1032 		 * Maelstrom doesn't work.
   1033 		 */
   1034 		if (error == ERESTART)
   1035 			error = EINTR;
   1036 		return error;
   1037 	}
   1038 
   1039 	if (timeout) {
   1040 		if (*retval) {
   1041 			/*
   1042 			 * Compute how much time was left of the timeout,
   1043 			 * by subtracting the current time and the time
   1044 			 * before we started the call, and subtracting
   1045 			 * that result from the user-supplied value.
   1046 			 */
   1047 			microtime(&tv1);
   1048 			timersub(&tv1, &tv0, &tv1);
   1049 			timersub(&utv, &tv1, &utv);
   1050 			if (utv.tv_sec < 0)
   1051 				timerclear(&utv);
   1052 		} else
   1053 			timerclear(&utv);
   1054 		if ((error = copyout(&utv, timeout, sizeof(utv))))
   1055 			return error;
   1056 	}
   1057 
   1058 	return 0;
   1059 }
   1060 
   1061 /*
   1062  * Get the process group of a certain process. Look it up
   1063  * and return the value.
   1064  */
   1065 int
   1066 linux_sys_getpgid(l, v, retval)
   1067 	struct lwp *l;
   1068 	void *v;
   1069 	register_t *retval;
   1070 {
   1071 	struct linux_sys_getpgid_args /* {
   1072 		syscallarg(int) pid;
   1073 	} */ *uap = v;
   1074 	struct proc *p = l->l_proc;
   1075 	struct proc *targp;
   1076 
   1077 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
   1078 		if ((targp = pfind(SCARG(uap, pid))) == 0)
   1079 			return ESRCH;
   1080 	}
   1081 	else
   1082 		targp = p;
   1083 
   1084 	retval[0] = targp->p_pgid;
   1085 	return 0;
   1086 }
   1087 
   1088 /*
   1089  * Set the 'personality' (emulation mode) for the current process. Only
   1090  * accept the Linux personality here (0). This call is needed because
   1091  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
   1092  * ELF binaries run in Linux mode, not SVR4 mode.
   1093  */
   1094 int
   1095 linux_sys_personality(l, v, retval)
   1096 	struct lwp *l;
   1097 	void *v;
   1098 	register_t *retval;
   1099 {
   1100 	struct linux_sys_personality_args /* {
   1101 		syscallarg(int) per;
   1102 	} */ *uap = v;
   1103 
   1104 	if (SCARG(uap, per) != 0)
   1105 		return EINVAL;
   1106 	retval[0] = 0;
   1107 	return 0;
   1108 }
   1109 
   1110 #if defined(__i386__) || defined(__m68k__)
   1111 /*
   1112  * The calls are here because of type conversions.
   1113  */
   1114 int
   1115 linux_sys_setreuid16(l, v, retval)
   1116 	struct lwp *l;
   1117 	void *v;
   1118 	register_t *retval;
   1119 {
   1120 	struct linux_sys_setreuid16_args /* {
   1121 		syscallarg(int) ruid;
   1122 		syscallarg(int) euid;
   1123 	} */ *uap = v;
   1124 	struct sys_setreuid_args bsa;
   1125 
   1126 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
   1127 		(uid_t)-1 : SCARG(uap, ruid);
   1128 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
   1129 		(uid_t)-1 : SCARG(uap, euid);
   1130 
   1131 	return sys_setreuid(l, &bsa, retval);
   1132 }
   1133 
   1134 int
   1135 linux_sys_setregid16(l, v, retval)
   1136 	struct lwp *l;
   1137 	void *v;
   1138 	register_t *retval;
   1139 {
   1140 	struct linux_sys_setregid16_args /* {
   1141 		syscallarg(int) rgid;
   1142 		syscallarg(int) egid;
   1143 	} */ *uap = v;
   1144 	struct sys_setregid_args bsa;
   1145 
   1146 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
   1147 		(uid_t)-1 : SCARG(uap, rgid);
   1148 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1149 		(uid_t)-1 : SCARG(uap, egid);
   1150 
   1151 	return sys_setregid(l, &bsa, retval);
   1152 }
   1153 
   1154 int
   1155 linux_sys_setresuid16(l, v, retval)
   1156 	struct lwp *l;
   1157 	void *v;
   1158 	register_t *retval;
   1159 {
   1160 	struct linux_sys_setresuid16_args /* {
   1161 		syscallarg(uid_t) ruid;
   1162 		syscallarg(uid_t) euid;
   1163 		syscallarg(uid_t) suid;
   1164 	} */ *uap = v;
   1165 	struct linux_sys_setresuid16_args lsa;
   1166 
   1167 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
   1168 		(uid_t)-1 : SCARG(uap, ruid);
   1169 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
   1170 		(uid_t)-1 : SCARG(uap, euid);
   1171 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
   1172 		(uid_t)-1 : SCARG(uap, suid);
   1173 
   1174 	return linux_sys_setresuid(l, &lsa, retval);
   1175 }
   1176 
   1177 int
   1178 linux_sys_setresgid16(l, v, retval)
   1179 	struct lwp *l;
   1180 	void *v;
   1181 	register_t *retval;
   1182 {
   1183 	struct linux_sys_setresgid16_args /* {
   1184 		syscallarg(gid_t) rgid;
   1185 		syscallarg(gid_t) egid;
   1186 		syscallarg(gid_t) sgid;
   1187 	} */ *uap = v;
   1188 	struct linux_sys_setresgid16_args lsa;
   1189 
   1190 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
   1191 		(gid_t)-1 : SCARG(uap, rgid);
   1192 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1193 		(gid_t)-1 : SCARG(uap, egid);
   1194 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
   1195 		(gid_t)-1 : SCARG(uap, sgid);
   1196 
   1197 	return linux_sys_setresgid(l, &lsa, retval);
   1198 }
   1199 
   1200 int
   1201 linux_sys_getgroups16(l, v, retval)
   1202 	struct lwp *l;
   1203 	void *v;
   1204 	register_t *retval;
   1205 {
   1206 	struct linux_sys_getgroups16_args /* {
   1207 		syscallarg(int) gidsetsize;
   1208 		syscallarg(linux_gid_t *) gidset;
   1209 	} */ *uap = v;
   1210 	struct proc *p = l->l_proc;
   1211 	caddr_t sg;
   1212 	int n, error, i;
   1213 	struct sys_getgroups_args bsa;
   1214 	gid_t *bset, *kbset;
   1215 	linux_gid_t *lset;
   1216 	struct pcred *pc = p->p_cred;
   1217 
   1218 	n = SCARG(uap, gidsetsize);
   1219 	if (n < 0)
   1220 		return EINVAL;
   1221 	error = 0;
   1222 	bset = kbset = NULL;
   1223 	lset = NULL;
   1224 	if (n > 0) {
   1225 		n = min(pc->pc_ucred->cr_ngroups, n);
   1226 		sg = stackgap_init(p, 0);
   1227 		bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
   1228 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
   1229 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1230 		if (bset == NULL || kbset == NULL || lset == NULL)
   1231 			return ENOMEM;
   1232 		SCARG(&bsa, gidsetsize) = n;
   1233 		SCARG(&bsa, gidset) = bset;
   1234 		error = sys_getgroups(l, &bsa, retval);
   1235 		if (error != 0)
   1236 			goto out;
   1237 		error = copyin(bset, kbset, n * sizeof (gid_t));
   1238 		if (error != 0)
   1239 			goto out;
   1240 		for (i = 0; i < n; i++)
   1241 			lset[i] = (linux_gid_t)kbset[i];
   1242 		error = copyout(lset, SCARG(uap, gidset),
   1243 		    n * sizeof (linux_gid_t));
   1244 	} else
   1245 		*retval = pc->pc_ucred->cr_ngroups;
   1246 out:
   1247 	if (kbset != NULL)
   1248 		free(kbset, M_TEMP);
   1249 	if (lset != NULL)
   1250 		free(lset, M_TEMP);
   1251 	return error;
   1252 }
   1253 
   1254 int
   1255 linux_sys_setgroups16(l, v, retval)
   1256 	struct lwp *l;
   1257 	void *v;
   1258 	register_t *retval;
   1259 {
   1260 	struct linux_sys_setgroups16_args /* {
   1261 		syscallarg(int) gidsetsize;
   1262 		syscallarg(linux_gid_t *) gidset;
   1263 	} */ *uap = v;
   1264 	struct proc *p = l->l_proc;
   1265 	caddr_t sg;
   1266 	int n;
   1267 	int error, i;
   1268 	struct sys_setgroups_args bsa;
   1269 	gid_t *bset, *kbset;
   1270 	linux_gid_t *lset;
   1271 
   1272 	n = SCARG(uap, gidsetsize);
   1273 	if (n < 0 || n > NGROUPS)
   1274 		return EINVAL;
   1275 	sg = stackgap_init(p, 0);
   1276 	bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
   1277 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1278 	kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
   1279 	if (lset == NULL || bset == NULL)
   1280 		return ENOMEM;
   1281 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
   1282 	if (error != 0)
   1283 		goto out;
   1284 	for (i = 0; i < n; i++)
   1285 		kbset[i] = (gid_t)lset[i];
   1286 	error = copyout(kbset, bset, n * sizeof (gid_t));
   1287 	if (error != 0)
   1288 		goto out;
   1289 	SCARG(&bsa, gidsetsize) = n;
   1290 	SCARG(&bsa, gidset) = bset;
   1291 	error = sys_setgroups(l, &bsa, retval);
   1292 
   1293 out:
   1294 	if (lset != NULL)
   1295 		free(lset, M_TEMP);
   1296 	if (kbset != NULL)
   1297 		free(kbset, M_TEMP);
   1298 
   1299 	return error;
   1300 }
   1301 
   1302 #endif /* __i386__ || __m68k__ */
   1303 
   1304 /*
   1305  * We have nonexistent fsuid equal to uid.
   1306  * If modification is requested, refuse.
   1307  */
   1308 int
   1309 linux_sys_setfsuid(l, v, retval)
   1310 	 struct lwp *l;
   1311 	 void *v;
   1312 	 register_t *retval;
   1313 {
   1314 	 struct linux_sys_setfsuid_args /* {
   1315 		 syscallarg(uid_t) uid;
   1316 	 } */ *uap = v;
   1317 	 struct proc *p = l->l_proc;
   1318 	 uid_t uid;
   1319 
   1320 	 uid = SCARG(uap, uid);
   1321 	 if (p->p_cred->p_ruid != uid)
   1322 		 return sys_nosys(l, v, retval);
   1323 	 else
   1324 		 return (0);
   1325 }
   1326 
   1327 /* XXX XXX XXX */
   1328 #ifndef alpha
   1329 int
   1330 linux_sys_getfsuid(l, v, retval)
   1331 	struct lwp *l;
   1332 	void *v;
   1333 	register_t *retval;
   1334 {
   1335 	return sys_getuid(l, v, retval);
   1336 }
   1337 #endif
   1338 
   1339 int
   1340 linux_sys_setresuid(l, v, retval)
   1341 	struct lwp *l;
   1342 	void *v;
   1343 	register_t *retval;
   1344 {
   1345 	struct linux_sys_setresuid_args /* {
   1346 		syscallarg(uid_t) ruid;
   1347 		syscallarg(uid_t) euid;
   1348 		syscallarg(uid_t) suid;
   1349 	} */ *uap = v;
   1350 
   1351 	/*
   1352 	 * Note: These checks are a little different than the NetBSD
   1353 	 * setreuid(2) call performs.  This precisely follows the
   1354 	 * behavior of the Linux kernel.
   1355 	 */
   1356 
   1357 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
   1358 			    SCARG(uap, suid),
   1359 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
   1360 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
   1361 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
   1362 }
   1363 
   1364 int
   1365 linux_sys_getresuid(l, v, retval)
   1366 	struct lwp *l;
   1367 	void *v;
   1368 	register_t *retval;
   1369 {
   1370 	struct linux_sys_getresuid_args /* {
   1371 		syscallarg(uid_t *) ruid;
   1372 		syscallarg(uid_t *) euid;
   1373 		syscallarg(uid_t *) suid;
   1374 	} */ *uap = v;
   1375 	struct proc *p = l->l_proc;
   1376 	struct pcred *pc = p->p_cred;
   1377 	int error;
   1378 
   1379 	/*
   1380 	 * Linux copies these values out to userspace like so:
   1381 	 *
   1382 	 *	1. Copy out ruid.
   1383 	 *	2. If that succeeds, copy out euid.
   1384 	 *	3. If both of those succeed, copy out suid.
   1385 	 */
   1386 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
   1387 			     sizeof(uid_t))) != 0)
   1388 		return (error);
   1389 
   1390 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
   1391 			     sizeof(uid_t))) != 0)
   1392 		return (error);
   1393 
   1394 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
   1395 }
   1396 
   1397 int
   1398 linux_sys_ptrace(l, v, retval)
   1399 	struct lwp *l;
   1400 	void *v;
   1401 	register_t *retval;
   1402 {
   1403 	struct linux_sys_ptrace_args /* {
   1404 		i386, m68k, powerpc: T=int
   1405 		alpha: T=long
   1406 		syscallarg(T) request;
   1407 		syscallarg(T) pid;
   1408 		syscallarg(T) addr;
   1409 		syscallarg(T) data;
   1410 	} */ *uap = v;
   1411 	const int *ptr;
   1412 	int request;
   1413 	int error;
   1414 
   1415 	ptr = linux_ptrace_request_map;
   1416 	request = SCARG(uap, request);
   1417 	while (*ptr != -1)
   1418 		if (*ptr++ == request) {
   1419 			struct sys_ptrace_args pta;
   1420 
   1421 			SCARG(&pta, req) = *ptr;
   1422 			SCARG(&pta, pid) = SCARG(uap, pid);
   1423 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
   1424 			SCARG(&pta, data) = SCARG(uap, data);
   1425 
   1426 			/*
   1427 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1428 			 * to continue where the process left off previously.
   1429 			 * The same thing is achieved by addr == (caddr_t) 1
   1430 			 * on NetBSD, so rewrite 'addr' appropriately.
   1431 			 */
   1432 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1433 				SCARG(&pta, addr) = (caddr_t) 1;
   1434 
   1435 			error = sys_ptrace(l, &pta, retval);
   1436 			if (error)
   1437 				return error;
   1438 			switch (request) {
   1439 			case LINUX_PTRACE_PEEKTEXT:
   1440 			case LINUX_PTRACE_PEEKDATA:
   1441 				error = copyout (retval,
   1442 				    (caddr_t)SCARG(uap, data), sizeof *retval);
   1443 				*retval = SCARG(uap, data);
   1444 				break;
   1445 			default:
   1446 				break;
   1447 			}
   1448 			return error;
   1449 		}
   1450 		else
   1451 			ptr++;
   1452 
   1453 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1454 }
   1455 
   1456 int
   1457 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
   1458 {
   1459 	struct linux_sys_reboot_args /* {
   1460 		syscallarg(int) magic1;
   1461 		syscallarg(int) magic2;
   1462 		syscallarg(int) cmd;
   1463 		syscallarg(void *) arg;
   1464 	} */ *uap = v;
   1465 	struct sys_reboot_args /* {
   1466 		syscallarg(int) opt;
   1467 		syscallarg(char *) bootstr;
   1468 	} */ sra;
   1469 	struct proc *p = l->l_proc;
   1470 	int error;
   1471 
   1472 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1473 		return(error);
   1474 
   1475 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1476 		return(EINVAL);
   1477 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1478 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1479 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1480 		return(EINVAL);
   1481 
   1482 	switch (SCARG(uap, cmd)) {
   1483 	case LINUX_REBOOT_CMD_RESTART:
   1484 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1485 		break;
   1486 	case LINUX_REBOOT_CMD_HALT:
   1487 		SCARG(&sra, opt) = RB_HALT;
   1488 		break;
   1489 	case LINUX_REBOOT_CMD_POWER_OFF:
   1490 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1491 		break;
   1492 	case LINUX_REBOOT_CMD_RESTART2:
   1493 		/* Reboot with an argument. */
   1494 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1495 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1496 		break;
   1497 	case LINUX_REBOOT_CMD_CAD_ON:
   1498 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1499 	case LINUX_REBOOT_CMD_CAD_OFF:
   1500 		return(0);
   1501 	default:
   1502 		return(EINVAL);
   1503 	}
   1504 
   1505 	return(sys_reboot(l, &sra, retval));
   1506 }
   1507 
   1508 /*
   1509  * Copy of compat_12_sys_swapon().
   1510  */
   1511 int
   1512 linux_sys_swapon(l, v, retval)
   1513 	struct lwp *l;
   1514 	void *v;
   1515 	register_t *retval;
   1516 {
   1517 	struct sys_swapctl_args ua;
   1518 	struct linux_sys_swapon_args /* {
   1519 		syscallarg(const char *) name;
   1520 	} */ *uap = v;
   1521 
   1522 	SCARG(&ua, cmd) = SWAP_ON;
   1523 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
   1524 	SCARG(&ua, misc) = 0;	/* priority */
   1525 	return (sys_swapctl(l, &ua, retval));
   1526 }
   1527 
   1528 /*
   1529  * Stop swapping to the file or block device specified by path.
   1530  */
   1531 int
   1532 linux_sys_swapoff(l, v, retval)
   1533 	struct lwp *l;
   1534 	void *v;
   1535 	register_t *retval;
   1536 {
   1537 	struct sys_swapctl_args ua;
   1538 	struct linux_sys_swapoff_args /* {
   1539 		syscallarg(const char *) path;
   1540 	} */ *uap = v;
   1541 
   1542 	SCARG(&ua, cmd) = SWAP_OFF;
   1543 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
   1544 	return (sys_swapctl(l, &ua, retval));
   1545 }
   1546 
   1547 /*
   1548  * Copy of compat_09_sys_setdomainname()
   1549  */
   1550 /* ARGSUSED */
   1551 int
   1552 linux_sys_setdomainname(l, v, retval)
   1553 	struct lwp *l;
   1554 	void *v;
   1555 	register_t *retval;
   1556 {
   1557 	struct linux_sys_setdomainname_args /* {
   1558 		syscallarg(char *) domainname;
   1559 		syscallarg(int) len;
   1560 	} */ *uap = v;
   1561 	int name[2];
   1562 
   1563 	name[0] = CTL_KERN;
   1564 	name[1] = KERN_DOMAINNAME;
   1565 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
   1566 			    SCARG(uap, len), l));
   1567 }
   1568 
   1569 /*
   1570  * sysinfo()
   1571  */
   1572 /* ARGSUSED */
   1573 int
   1574 linux_sys_sysinfo(l, v, retval)
   1575 	struct lwp *l;
   1576 	void *v;
   1577 	register_t *retval;
   1578 {
   1579 	struct linux_sys_sysinfo_args /* {
   1580 		syscallarg(struct linux_sysinfo *) arg;
   1581 	} */ *uap = v;
   1582 	struct linux_sysinfo si;
   1583 	struct loadavg *la;
   1584 
   1585 	si.uptime = time.tv_sec - boottime.tv_sec;
   1586 	la = &averunnable;
   1587 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1588 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1589 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1590 	si.totalram = ctob(physmem);
   1591 	si.freeram = uvmexp.free * uvmexp.pagesize;
   1592 	si.sharedram = 0;	/* XXX */
   1593 	si.bufferram = uvmexp.filepages * uvmexp.pagesize;
   1594 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
   1595 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1596 	si.procs = nprocs;
   1597 
   1598 	/* The following are only present in newer Linux kernels. */
   1599 	si.totalbig = 0;
   1600 	si.freebig = 0;
   1601 	si.mem_unit = 1;
   1602 
   1603 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1604 }
   1605 
   1606 #define bsd_to_linux_rlimit1(l, b, f) \
   1607     (l)->f = ((b)->f == RLIM_INFINITY || \
   1608 	     ((b)->f & 0xffffffff00000000ULL) != 0) ? \
   1609     LINUX_RLIM_INFINITY : (int32_t)(b)->f
   1610 #define bsd_to_linux_rlimit(l, b) \
   1611     bsd_to_linux_rlimit1(l, b, rlim_cur); \
   1612     bsd_to_linux_rlimit1(l, b, rlim_max)
   1613 
   1614 #define linux_to_bsd_rlimit1(b, l, f) \
   1615     (b)->f = (l)->f == LINUX_RLIM_INFINITY ? RLIM_INFINITY : (l)->f
   1616 #define linux_to_bsd_rlimit(b, l) \
   1617     linux_to_bsd_rlimit1(b, l, rlim_cur); \
   1618     linux_to_bsd_rlimit1(b, l, rlim_max)
   1619 
   1620 static int
   1621 linux_to_bsd_limit(lim)
   1622 	int lim;
   1623 {
   1624 	switch (lim) {
   1625 	case LINUX_RLIMIT_CPU:
   1626 		return RLIMIT_CPU;
   1627 	case LINUX_RLIMIT_FSIZE:
   1628 		return RLIMIT_FSIZE;
   1629 	case LINUX_RLIMIT_DATA:
   1630 		return RLIMIT_DATA;
   1631 	case LINUX_RLIMIT_STACK:
   1632 		return RLIMIT_STACK;
   1633 	case LINUX_RLIMIT_CORE:
   1634 		return RLIMIT_CORE;
   1635 	case LINUX_RLIMIT_RSS:
   1636 		return RLIMIT_RSS;
   1637 	case LINUX_RLIMIT_NPROC:
   1638 		return RLIMIT_NPROC;
   1639 	case LINUX_RLIMIT_NOFILE:
   1640 		return RLIMIT_NOFILE;
   1641 	case LINUX_RLIMIT_MEMLOCK:
   1642 		return RLIMIT_MEMLOCK;
   1643 	case LINUX_RLIMIT_AS:
   1644 	case LINUX_RLIMIT_LOCKS:
   1645 		return -EOPNOTSUPP;
   1646 	default:
   1647 		return -EINVAL;
   1648 	}
   1649 }
   1650 
   1651 
   1652 int
   1653 linux_sys_getrlimit(l, v, retval)
   1654 	struct lwp *l;
   1655 	void *v;
   1656 	register_t *retval;
   1657 {
   1658 	struct linux_sys_getrlimit_args /* {
   1659 		syscallarg(int) which;
   1660 		syscallarg(struct orlimit *) rlp;
   1661 	} */ *uap = v;
   1662 	struct proc *p = l->l_proc;
   1663 	caddr_t sg = stackgap_init(p, 0);
   1664 	struct sys_getrlimit_args ap;
   1665 	struct rlimit rl;
   1666 	struct orlimit orl;
   1667 	int error;
   1668 
   1669 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
   1670 	if ((error = SCARG(&ap, which)) < 0)
   1671 		return -error;
   1672 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
   1673 	if ((error = sys_getrlimit(l, &ap, retval)) != 0)
   1674 		return error;
   1675 	if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
   1676 		return error;
   1677 	bsd_to_linux_rlimit(&orl, &rl);
   1678 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
   1679 }
   1680 
   1681 int
   1682 linux_sys_setrlimit(l, v, retval)
   1683 	struct lwp *l;
   1684 	void *v;
   1685 	register_t *retval;
   1686 {
   1687 	struct linux_sys_setrlimit_args /* {
   1688 		syscallarg(int) which;
   1689 		syscallarg(struct orlimit *) rlp;
   1690 	} */ *uap = v;
   1691 	struct proc *p = l->l_proc;
   1692 	caddr_t sg = stackgap_init(p, 0);
   1693 	struct sys_setrlimit_args ap;
   1694 	struct rlimit rl;
   1695 	struct orlimit orl;
   1696 	int error;
   1697 
   1698 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
   1699 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
   1700 	if ((error = SCARG(&ap, which)) < 0)
   1701 		return -error;
   1702 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
   1703 		return error;
   1704 	linux_to_bsd_rlimit(&rl, &orl);
   1705 	/* XXX: alpha complains about this */
   1706 	if ((error = copyout(&rl, (void *)SCARG(&ap, rlp), sizeof(rl))) != 0)
   1707 		return error;
   1708 	return sys_setrlimit(l, &ap, retval);
   1709 }
   1710 
   1711 #ifndef __mips__
   1712 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
   1713 int
   1714 linux_sys_ugetrlimit(l, v, retval)
   1715 	struct lwp *l;
   1716 	void *v;
   1717 	register_t *retval;
   1718 {
   1719 	return linux_sys_getrlimit(l, v, retval);
   1720 }
   1721 #endif
   1722 
   1723 /*
   1724  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1725  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1726  * This is the way Linux does it and glibc depends on this behaviour.
   1727  */
   1728 int
   1729 linux_sys_nosys(l, v, retval)
   1730 	struct lwp *l;
   1731 	void *v;
   1732 	register_t *retval;
   1733 {
   1734 	return (ENOSYS);
   1735 }
   1736