Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.134.4.1
      1 /*	$NetBSD: linux_misc.c,v 1.134.4.1 2005/04/29 11:28:40 kent 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.134.4.1 2005/04/29 11:28:40 kent 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 			if (cookie)
    855 				off = *cookie++;
    856 			else
    857 				off += reclen;
    858 			continue;
    859 		}
    860 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    861 		if (reclen > len || resid < linux_reclen) {
    862 			/* entry too big for buffer, so just stop */
    863 			outp++;
    864 			break;
    865 		}
    866 		/*
    867 		 * Massage in place to make a Linux-shaped dirent (otherwise
    868 		 * we have to worry about touching user memory outside of
    869 		 * the copyout() call).
    870 		 */
    871 		idb.d_ino = bdp->d_fileno;
    872 		/*
    873 		 * The old readdir() call misuses the offset and reclen fields.
    874 		 */
    875 		if (oldcall) {
    876 			idb.d_off = (linux_off_t)linux_reclen;
    877 			idb.d_reclen = (u_short)bdp->d_namlen;
    878 		} else {
    879 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
    880 				compat_offseterr(vp, "linux_getdents");
    881 				error = EINVAL;
    882 				goto out;
    883 			}
    884 			idb.d_off = (linux_off_t)off;
    885 			idb.d_reclen = (u_short)linux_reclen;
    886 		}
    887 		strcpy(idb.d_name, bdp->d_name);
    888 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    889 			goto out;
    890 		/* advance past this real entry */
    891 		inp += reclen;
    892 		if (cookie)
    893 			off = *cookie++; /* each entry points to itself */
    894 		else
    895 			off += reclen;
    896 		/* advance output past Linux-shaped entry */
    897 		outp += linux_reclen;
    898 		resid -= linux_reclen;
    899 		if (oldcall)
    900 			break;
    901 	}
    902 
    903 	/* if we squished out the whole block, try again */
    904 	if (outp == (caddr_t)SCARG(uap, dent))
    905 		goto again;
    906 	fp->f_offset = off;	/* update the vnode offset */
    907 
    908 	if (oldcall)
    909 		nbytes = resid + linux_reclen;
    910 
    911 eof:
    912 	*retval = nbytes - resid;
    913 out:
    914 	VOP_UNLOCK(vp, 0);
    915 	if (cookiebuf)
    916 		free(cookiebuf, M_TEMP);
    917 	free(buf, M_TEMP);
    918 out1:
    919 	FILE_UNUSE(fp, p);
    920 	return error;
    921 }
    922 
    923 /*
    924  * Even when just using registers to pass arguments to syscalls you can
    925  * have 5 of them on the i386. So this newer version of select() does
    926  * this.
    927  */
    928 int
    929 linux_sys_select(l, v, retval)
    930 	struct lwp *l;
    931 	void *v;
    932 	register_t *retval;
    933 {
    934 	struct linux_sys_select_args /* {
    935 		syscallarg(int) nfds;
    936 		syscallarg(fd_set *) readfds;
    937 		syscallarg(fd_set *) writefds;
    938 		syscallarg(fd_set *) exceptfds;
    939 		syscallarg(struct timeval *) timeout;
    940 	} */ *uap = v;
    941 
    942 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    943 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
    944 }
    945 
    946 /*
    947  * Common code for the old and new versions of select(). A couple of
    948  * things are important:
    949  * 1) return the amount of time left in the 'timeout' parameter
    950  * 2) select never returns ERESTART on Linux, always return EINTR
    951  */
    952 int
    953 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
    954 	struct lwp *l;
    955 	register_t *retval;
    956 	int nfds;
    957 	fd_set *readfds, *writefds, *exceptfds;
    958 	struct timeval *timeout;
    959 {
    960 	struct sys_select_args bsa;
    961 	struct proc *p = l->l_proc;
    962 	struct timeval tv0, tv1, utv, *tvp;
    963 	caddr_t sg;
    964 	int error;
    965 
    966 	SCARG(&bsa, nd) = nfds;
    967 	SCARG(&bsa, in) = readfds;
    968 	SCARG(&bsa, ou) = writefds;
    969 	SCARG(&bsa, ex) = exceptfds;
    970 	SCARG(&bsa, tv) = timeout;
    971 
    972 	/*
    973 	 * Store current time for computation of the amount of
    974 	 * time left.
    975 	 */
    976 	if (timeout) {
    977 		if ((error = copyin(timeout, &utv, sizeof(utv))))
    978 			return error;
    979 		if (itimerfix(&utv)) {
    980 			/*
    981 			 * The timeval was invalid.  Convert it to something
    982 			 * valid that will act as it does under Linux.
    983 			 */
    984 			sg = stackgap_init(p, 0);
    985 			tvp = stackgap_alloc(p, &sg, sizeof(utv));
    986 			utv.tv_sec += utv.tv_usec / 1000000;
    987 			utv.tv_usec %= 1000000;
    988 			if (utv.tv_usec < 0) {
    989 				utv.tv_sec -= 1;
    990 				utv.tv_usec += 1000000;
    991 			}
    992 			if (utv.tv_sec < 0)
    993 				timerclear(&utv);
    994 			if ((error = copyout(&utv, tvp, sizeof(utv))))
    995 				return error;
    996 			SCARG(&bsa, tv) = tvp;
    997 		}
    998 		microtime(&tv0);
    999 	}
   1000 
   1001 	error = sys_select(l, &bsa, retval);
   1002 	if (error) {
   1003 		/*
   1004 		 * See fs/select.c in the Linux kernel.  Without this,
   1005 		 * Maelstrom doesn't work.
   1006 		 */
   1007 		if (error == ERESTART)
   1008 			error = EINTR;
   1009 		return error;
   1010 	}
   1011 
   1012 	if (timeout) {
   1013 		if (*retval) {
   1014 			/*
   1015 			 * Compute how much time was left of the timeout,
   1016 			 * by subtracting the current time and the time
   1017 			 * before we started the call, and subtracting
   1018 			 * that result from the user-supplied value.
   1019 			 */
   1020 			microtime(&tv1);
   1021 			timersub(&tv1, &tv0, &tv1);
   1022 			timersub(&utv, &tv1, &utv);
   1023 			if (utv.tv_sec < 0)
   1024 				timerclear(&utv);
   1025 		} else
   1026 			timerclear(&utv);
   1027 		if ((error = copyout(&utv, timeout, sizeof(utv))))
   1028 			return error;
   1029 	}
   1030 
   1031 	return 0;
   1032 }
   1033 
   1034 /*
   1035  * Get the process group of a certain process. Look it up
   1036  * and return the value.
   1037  */
   1038 int
   1039 linux_sys_getpgid(l, v, retval)
   1040 	struct lwp *l;
   1041 	void *v;
   1042 	register_t *retval;
   1043 {
   1044 	struct linux_sys_getpgid_args /* {
   1045 		syscallarg(int) pid;
   1046 	} */ *uap = v;
   1047 	struct proc *p = l->l_proc;
   1048 	struct proc *targp;
   1049 
   1050 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
   1051 		if ((targp = pfind(SCARG(uap, pid))) == 0)
   1052 			return ESRCH;
   1053 	}
   1054 	else
   1055 		targp = p;
   1056 
   1057 	retval[0] = targp->p_pgid;
   1058 	return 0;
   1059 }
   1060 
   1061 /*
   1062  * Set the 'personality' (emulation mode) for the current process. Only
   1063  * accept the Linux personality here (0). This call is needed because
   1064  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
   1065  * ELF binaries run in Linux mode, not SVR4 mode.
   1066  */
   1067 int
   1068 linux_sys_personality(l, v, retval)
   1069 	struct lwp *l;
   1070 	void *v;
   1071 	register_t *retval;
   1072 {
   1073 	struct linux_sys_personality_args /* {
   1074 		syscallarg(int) per;
   1075 	} */ *uap = v;
   1076 
   1077 	if (SCARG(uap, per) != 0)
   1078 		return EINVAL;
   1079 	retval[0] = 0;
   1080 	return 0;
   1081 }
   1082 
   1083 #if defined(__i386__) || defined(__m68k__)
   1084 /*
   1085  * The calls are here because of type conversions.
   1086  */
   1087 int
   1088 linux_sys_setreuid16(l, v, retval)
   1089 	struct lwp *l;
   1090 	void *v;
   1091 	register_t *retval;
   1092 {
   1093 	struct linux_sys_setreuid16_args /* {
   1094 		syscallarg(int) ruid;
   1095 		syscallarg(int) euid;
   1096 	} */ *uap = v;
   1097 	struct sys_setreuid_args bsa;
   1098 
   1099 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
   1100 		(uid_t)-1 : SCARG(uap, ruid);
   1101 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
   1102 		(uid_t)-1 : SCARG(uap, euid);
   1103 
   1104 	return sys_setreuid(l, &bsa, retval);
   1105 }
   1106 
   1107 int
   1108 linux_sys_setregid16(l, v, retval)
   1109 	struct lwp *l;
   1110 	void *v;
   1111 	register_t *retval;
   1112 {
   1113 	struct linux_sys_setregid16_args /* {
   1114 		syscallarg(int) rgid;
   1115 		syscallarg(int) egid;
   1116 	} */ *uap = v;
   1117 	struct sys_setregid_args bsa;
   1118 
   1119 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
   1120 		(uid_t)-1 : SCARG(uap, rgid);
   1121 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1122 		(uid_t)-1 : SCARG(uap, egid);
   1123 
   1124 	return sys_setregid(l, &bsa, retval);
   1125 }
   1126 
   1127 int
   1128 linux_sys_setresuid16(l, v, retval)
   1129 	struct lwp *l;
   1130 	void *v;
   1131 	register_t *retval;
   1132 {
   1133 	struct linux_sys_setresuid16_args /* {
   1134 		syscallarg(uid_t) ruid;
   1135 		syscallarg(uid_t) euid;
   1136 		syscallarg(uid_t) suid;
   1137 	} */ *uap = v;
   1138 	struct linux_sys_setresuid16_args lsa;
   1139 
   1140 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
   1141 		(uid_t)-1 : SCARG(uap, ruid);
   1142 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
   1143 		(uid_t)-1 : SCARG(uap, euid);
   1144 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
   1145 		(uid_t)-1 : SCARG(uap, suid);
   1146 
   1147 	return linux_sys_setresuid(l, &lsa, retval);
   1148 }
   1149 
   1150 int
   1151 linux_sys_setresgid16(l, v, retval)
   1152 	struct lwp *l;
   1153 	void *v;
   1154 	register_t *retval;
   1155 {
   1156 	struct linux_sys_setresgid16_args /* {
   1157 		syscallarg(gid_t) rgid;
   1158 		syscallarg(gid_t) egid;
   1159 		syscallarg(gid_t) sgid;
   1160 	} */ *uap = v;
   1161 	struct linux_sys_setresgid16_args lsa;
   1162 
   1163 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
   1164 		(gid_t)-1 : SCARG(uap, rgid);
   1165 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1166 		(gid_t)-1 : SCARG(uap, egid);
   1167 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
   1168 		(gid_t)-1 : SCARG(uap, sgid);
   1169 
   1170 	return linux_sys_setresgid(l, &lsa, retval);
   1171 }
   1172 
   1173 int
   1174 linux_sys_getgroups16(l, v, retval)
   1175 	struct lwp *l;
   1176 	void *v;
   1177 	register_t *retval;
   1178 {
   1179 	struct linux_sys_getgroups16_args /* {
   1180 		syscallarg(int) gidsetsize;
   1181 		syscallarg(linux_gid_t *) gidset;
   1182 	} */ *uap = v;
   1183 	struct proc *p = l->l_proc;
   1184 	caddr_t sg;
   1185 	int n, error, i;
   1186 	struct sys_getgroups_args bsa;
   1187 	gid_t *bset, *kbset;
   1188 	linux_gid_t *lset;
   1189 	struct pcred *pc = p->p_cred;
   1190 
   1191 	n = SCARG(uap, gidsetsize);
   1192 	if (n < 0)
   1193 		return EINVAL;
   1194 	error = 0;
   1195 	bset = kbset = NULL;
   1196 	lset = NULL;
   1197 	if (n > 0) {
   1198 		n = min(pc->pc_ucred->cr_ngroups, n);
   1199 		sg = stackgap_init(p, 0);
   1200 		bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
   1201 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
   1202 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1203 		if (bset == NULL || kbset == NULL || lset == NULL)
   1204 			return ENOMEM;
   1205 		SCARG(&bsa, gidsetsize) = n;
   1206 		SCARG(&bsa, gidset) = bset;
   1207 		error = sys_getgroups(l, &bsa, retval);
   1208 		if (error != 0)
   1209 			goto out;
   1210 		error = copyin(bset, kbset, n * sizeof (gid_t));
   1211 		if (error != 0)
   1212 			goto out;
   1213 		for (i = 0; i < n; i++)
   1214 			lset[i] = (linux_gid_t)kbset[i];
   1215 		error = copyout(lset, SCARG(uap, gidset),
   1216 		    n * sizeof (linux_gid_t));
   1217 	} else
   1218 		*retval = pc->pc_ucred->cr_ngroups;
   1219 out:
   1220 	if (kbset != NULL)
   1221 		free(kbset, M_TEMP);
   1222 	if (lset != NULL)
   1223 		free(lset, M_TEMP);
   1224 	return error;
   1225 }
   1226 
   1227 int
   1228 linux_sys_setgroups16(l, v, retval)
   1229 	struct lwp *l;
   1230 	void *v;
   1231 	register_t *retval;
   1232 {
   1233 	struct linux_sys_setgroups16_args /* {
   1234 		syscallarg(int) gidsetsize;
   1235 		syscallarg(linux_gid_t *) gidset;
   1236 	} */ *uap = v;
   1237 	struct proc *p = l->l_proc;
   1238 	caddr_t sg;
   1239 	int n;
   1240 	int error, i;
   1241 	struct sys_setgroups_args bsa;
   1242 	gid_t *bset, *kbset;
   1243 	linux_gid_t *lset;
   1244 
   1245 	n = SCARG(uap, gidsetsize);
   1246 	if (n < 0 || n > NGROUPS)
   1247 		return EINVAL;
   1248 	sg = stackgap_init(p, 0);
   1249 	bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
   1250 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1251 	kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
   1252 	if (lset == NULL || bset == NULL)
   1253 		return ENOMEM;
   1254 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
   1255 	if (error != 0)
   1256 		goto out;
   1257 	for (i = 0; i < n; i++)
   1258 		kbset[i] = (gid_t)lset[i];
   1259 	error = copyout(kbset, bset, n * sizeof (gid_t));
   1260 	if (error != 0)
   1261 		goto out;
   1262 	SCARG(&bsa, gidsetsize) = n;
   1263 	SCARG(&bsa, gidset) = bset;
   1264 	error = sys_setgroups(l, &bsa, retval);
   1265 
   1266 out:
   1267 	if (lset != NULL)
   1268 		free(lset, M_TEMP);
   1269 	if (kbset != NULL)
   1270 		free(kbset, M_TEMP);
   1271 
   1272 	return error;
   1273 }
   1274 
   1275 #endif /* __i386__ || __m68k__ */
   1276 
   1277 /*
   1278  * We have nonexistent fsuid equal to uid.
   1279  * If modification is requested, refuse.
   1280  */
   1281 int
   1282 linux_sys_setfsuid(l, v, retval)
   1283 	 struct lwp *l;
   1284 	 void *v;
   1285 	 register_t *retval;
   1286 {
   1287 	 struct linux_sys_setfsuid_args /* {
   1288 		 syscallarg(uid_t) uid;
   1289 	 } */ *uap = v;
   1290 	 struct proc *p = l->l_proc;
   1291 	 uid_t uid;
   1292 
   1293 	 uid = SCARG(uap, uid);
   1294 	 if (p->p_cred->p_ruid != uid)
   1295 		 return sys_nosys(l, v, retval);
   1296 	 else
   1297 		 return (0);
   1298 }
   1299 
   1300 /* XXX XXX XXX */
   1301 #ifndef alpha
   1302 int
   1303 linux_sys_getfsuid(l, v, retval)
   1304 	struct lwp *l;
   1305 	void *v;
   1306 	register_t *retval;
   1307 {
   1308 	return sys_getuid(l, v, retval);
   1309 }
   1310 #endif
   1311 
   1312 int
   1313 linux_sys_setresuid(l, v, retval)
   1314 	struct lwp *l;
   1315 	void *v;
   1316 	register_t *retval;
   1317 {
   1318 	struct linux_sys_setresuid_args /* {
   1319 		syscallarg(uid_t) ruid;
   1320 		syscallarg(uid_t) euid;
   1321 		syscallarg(uid_t) suid;
   1322 	} */ *uap = v;
   1323 
   1324 	/*
   1325 	 * Note: These checks are a little different than the NetBSD
   1326 	 * setreuid(2) call performs.  This precisely follows the
   1327 	 * behavior of the Linux kernel.
   1328 	 */
   1329 
   1330 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
   1331 			    SCARG(uap, suid),
   1332 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
   1333 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
   1334 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
   1335 }
   1336 
   1337 int
   1338 linux_sys_getresuid(l, v, retval)
   1339 	struct lwp *l;
   1340 	void *v;
   1341 	register_t *retval;
   1342 {
   1343 	struct linux_sys_getresuid_args /* {
   1344 		syscallarg(uid_t *) ruid;
   1345 		syscallarg(uid_t *) euid;
   1346 		syscallarg(uid_t *) suid;
   1347 	} */ *uap = v;
   1348 	struct proc *p = l->l_proc;
   1349 	struct pcred *pc = p->p_cred;
   1350 	int error;
   1351 
   1352 	/*
   1353 	 * Linux copies these values out to userspace like so:
   1354 	 *
   1355 	 *	1. Copy out ruid.
   1356 	 *	2. If that succeeds, copy out euid.
   1357 	 *	3. If both of those succeed, copy out suid.
   1358 	 */
   1359 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
   1360 			     sizeof(uid_t))) != 0)
   1361 		return (error);
   1362 
   1363 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
   1364 			     sizeof(uid_t))) != 0)
   1365 		return (error);
   1366 
   1367 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
   1368 }
   1369 
   1370 int
   1371 linux_sys_ptrace(l, v, retval)
   1372 	struct lwp *l;
   1373 	void *v;
   1374 	register_t *retval;
   1375 {
   1376 	struct linux_sys_ptrace_args /* {
   1377 		i386, m68k, powerpc: T=int
   1378 		alpha: T=long
   1379 		syscallarg(T) request;
   1380 		syscallarg(T) pid;
   1381 		syscallarg(T) addr;
   1382 		syscallarg(T) data;
   1383 	} */ *uap = v;
   1384 	const int *ptr;
   1385 	int request;
   1386 	int error;
   1387 
   1388 	ptr = linux_ptrace_request_map;
   1389 	request = SCARG(uap, request);
   1390 	while (*ptr != -1)
   1391 		if (*ptr++ == request) {
   1392 			struct sys_ptrace_args pta;
   1393 
   1394 			SCARG(&pta, req) = *ptr;
   1395 			SCARG(&pta, pid) = SCARG(uap, pid);
   1396 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
   1397 			SCARG(&pta, data) = SCARG(uap, data);
   1398 
   1399 			/*
   1400 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1401 			 * to continue where the process left off previously.
   1402 			 * The same thing is achieved by addr == (caddr_t) 1
   1403 			 * on NetBSD, so rewrite 'addr' appropriately.
   1404 			 */
   1405 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1406 				SCARG(&pta, addr) = (caddr_t) 1;
   1407 
   1408 			error = sys_ptrace(l, &pta, retval);
   1409 			if (error)
   1410 				return error;
   1411 			switch (request) {
   1412 			case LINUX_PTRACE_PEEKTEXT:
   1413 			case LINUX_PTRACE_PEEKDATA:
   1414 				error = copyout (retval,
   1415 				    (caddr_t)SCARG(uap, data), sizeof *retval);
   1416 				*retval = SCARG(uap, data);
   1417 				break;
   1418 			default:
   1419 				break;
   1420 			}
   1421 			return error;
   1422 		}
   1423 		else
   1424 			ptr++;
   1425 
   1426 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1427 }
   1428 
   1429 int
   1430 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
   1431 {
   1432 	struct linux_sys_reboot_args /* {
   1433 		syscallarg(int) magic1;
   1434 		syscallarg(int) magic2;
   1435 		syscallarg(int) cmd;
   1436 		syscallarg(void *) arg;
   1437 	} */ *uap = v;
   1438 	struct sys_reboot_args /* {
   1439 		syscallarg(int) opt;
   1440 		syscallarg(char *) bootstr;
   1441 	} */ sra;
   1442 	struct proc *p = l->l_proc;
   1443 	int error;
   1444 
   1445 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1446 		return(error);
   1447 
   1448 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1449 		return(EINVAL);
   1450 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1451 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1452 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1453 		return(EINVAL);
   1454 
   1455 	switch (SCARG(uap, cmd)) {
   1456 	case LINUX_REBOOT_CMD_RESTART:
   1457 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1458 		break;
   1459 	case LINUX_REBOOT_CMD_HALT:
   1460 		SCARG(&sra, opt) = RB_HALT;
   1461 		break;
   1462 	case LINUX_REBOOT_CMD_POWER_OFF:
   1463 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1464 		break;
   1465 	case LINUX_REBOOT_CMD_RESTART2:
   1466 		/* Reboot with an argument. */
   1467 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1468 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1469 		break;
   1470 	case LINUX_REBOOT_CMD_CAD_ON:
   1471 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1472 	case LINUX_REBOOT_CMD_CAD_OFF:
   1473 		return(0);
   1474 	default:
   1475 		return(EINVAL);
   1476 	}
   1477 
   1478 	return(sys_reboot(l, &sra, retval));
   1479 }
   1480 
   1481 /*
   1482  * Copy of compat_12_sys_swapon().
   1483  */
   1484 int
   1485 linux_sys_swapon(l, v, retval)
   1486 	struct lwp *l;
   1487 	void *v;
   1488 	register_t *retval;
   1489 {
   1490 	struct sys_swapctl_args ua;
   1491 	struct linux_sys_swapon_args /* {
   1492 		syscallarg(const char *) name;
   1493 	} */ *uap = v;
   1494 
   1495 	SCARG(&ua, cmd) = SWAP_ON;
   1496 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
   1497 	SCARG(&ua, misc) = 0;	/* priority */
   1498 	return (sys_swapctl(l, &ua, retval));
   1499 }
   1500 
   1501 /*
   1502  * Stop swapping to the file or block device specified by path.
   1503  */
   1504 int
   1505 linux_sys_swapoff(l, v, retval)
   1506 	struct lwp *l;
   1507 	void *v;
   1508 	register_t *retval;
   1509 {
   1510 	struct sys_swapctl_args ua;
   1511 	struct linux_sys_swapoff_args /* {
   1512 		syscallarg(const char *) path;
   1513 	} */ *uap = v;
   1514 
   1515 	SCARG(&ua, cmd) = SWAP_OFF;
   1516 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
   1517 	return (sys_swapctl(l, &ua, retval));
   1518 }
   1519 
   1520 /*
   1521  * Copy of compat_09_sys_setdomainname()
   1522  */
   1523 /* ARGSUSED */
   1524 int
   1525 linux_sys_setdomainname(l, v, retval)
   1526 	struct lwp *l;
   1527 	void *v;
   1528 	register_t *retval;
   1529 {
   1530 	struct linux_sys_setdomainname_args /* {
   1531 		syscallarg(char *) domainname;
   1532 		syscallarg(int) len;
   1533 	} */ *uap = v;
   1534 	int name[2];
   1535 
   1536 	name[0] = CTL_KERN;
   1537 	name[1] = KERN_DOMAINNAME;
   1538 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
   1539 			    SCARG(uap, len), l));
   1540 }
   1541 
   1542 /*
   1543  * sysinfo()
   1544  */
   1545 /* ARGSUSED */
   1546 int
   1547 linux_sys_sysinfo(l, v, retval)
   1548 	struct lwp *l;
   1549 	void *v;
   1550 	register_t *retval;
   1551 {
   1552 	struct linux_sys_sysinfo_args /* {
   1553 		syscallarg(struct linux_sysinfo *) arg;
   1554 	} */ *uap = v;
   1555 	struct linux_sysinfo si;
   1556 	struct loadavg *la;
   1557 
   1558 	si.uptime = time.tv_sec - boottime.tv_sec;
   1559 	la = &averunnable;
   1560 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1561 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1562 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1563 	si.totalram = ctob(physmem);
   1564 	si.freeram = uvmexp.free * uvmexp.pagesize;
   1565 	si.sharedram = 0;	/* XXX */
   1566 	si.bufferram = uvmexp.filepages * uvmexp.pagesize;
   1567 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
   1568 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1569 	si.procs = nprocs;
   1570 
   1571 	/* The following are only present in newer Linux kernels. */
   1572 	si.totalbig = 0;
   1573 	si.freebig = 0;
   1574 	si.mem_unit = 1;
   1575 
   1576 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1577 }
   1578 
   1579 #define bsd_to_linux_rlimit1(l, b, f) \
   1580     (l)->f = ((b)->f == RLIM_INFINITY || \
   1581 	     ((b)->f & 0xffffffff00000000ULL) != 0) ? \
   1582     LINUX_RLIM_INFINITY : (int32_t)(b)->f
   1583 #define bsd_to_linux_rlimit(l, b) \
   1584     bsd_to_linux_rlimit1(l, b, rlim_cur); \
   1585     bsd_to_linux_rlimit1(l, b, rlim_max)
   1586 
   1587 #define linux_to_bsd_rlimit1(b, l, f) \
   1588     (b)->f = (l)->f == LINUX_RLIM_INFINITY ? RLIM_INFINITY : (l)->f
   1589 #define linux_to_bsd_rlimit(b, l) \
   1590     linux_to_bsd_rlimit1(b, l, rlim_cur); \
   1591     linux_to_bsd_rlimit1(b, l, rlim_max)
   1592 
   1593 static int
   1594 linux_to_bsd_limit(lim)
   1595 	int lim;
   1596 {
   1597 	switch (lim) {
   1598 	case LINUX_RLIMIT_CPU:
   1599 		return RLIMIT_CPU;
   1600 	case LINUX_RLIMIT_FSIZE:
   1601 		return RLIMIT_FSIZE;
   1602 	case LINUX_RLIMIT_DATA:
   1603 		return RLIMIT_DATA;
   1604 	case LINUX_RLIMIT_STACK:
   1605 		return RLIMIT_STACK;
   1606 	case LINUX_RLIMIT_CORE:
   1607 		return RLIMIT_CORE;
   1608 	case LINUX_RLIMIT_RSS:
   1609 		return RLIMIT_RSS;
   1610 	case LINUX_RLIMIT_NPROC:
   1611 		return RLIMIT_NPROC;
   1612 	case LINUX_RLIMIT_NOFILE:
   1613 		return RLIMIT_NOFILE;
   1614 	case LINUX_RLIMIT_MEMLOCK:
   1615 		return RLIMIT_MEMLOCK;
   1616 	case LINUX_RLIMIT_AS:
   1617 	case LINUX_RLIMIT_LOCKS:
   1618 		return -EOPNOTSUPP;
   1619 	default:
   1620 		return -EINVAL;
   1621 	}
   1622 }
   1623 
   1624 
   1625 int
   1626 linux_sys_getrlimit(l, v, retval)
   1627 	struct lwp *l;
   1628 	void *v;
   1629 	register_t *retval;
   1630 {
   1631 	struct linux_sys_getrlimit_args /* {
   1632 		syscallarg(int) which;
   1633 		syscallarg(struct orlimit *) rlp;
   1634 	} */ *uap = v;
   1635 	struct proc *p = l->l_proc;
   1636 	caddr_t sg = stackgap_init(p, 0);
   1637 	struct sys_getrlimit_args ap;
   1638 	struct rlimit rl;
   1639 	struct orlimit orl;
   1640 	int error;
   1641 
   1642 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
   1643 	if ((error = SCARG(&ap, which)) < 0)
   1644 		return -error;
   1645 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
   1646 	if ((error = sys_getrlimit(l, &ap, retval)) != 0)
   1647 		return error;
   1648 	if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
   1649 		return error;
   1650 	bsd_to_linux_rlimit(&orl, &rl);
   1651 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
   1652 }
   1653 
   1654 int
   1655 linux_sys_setrlimit(l, v, retval)
   1656 	struct lwp *l;
   1657 	void *v;
   1658 	register_t *retval;
   1659 {
   1660 	struct linux_sys_setrlimit_args /* {
   1661 		syscallarg(int) which;
   1662 		syscallarg(struct orlimit *) rlp;
   1663 	} */ *uap = v;
   1664 	struct proc *p = l->l_proc;
   1665 	caddr_t sg = stackgap_init(p, 0);
   1666 	struct sys_setrlimit_args ap;
   1667 	struct rlimit rl;
   1668 	struct orlimit orl;
   1669 	int error;
   1670 
   1671 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
   1672 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
   1673 	if ((error = SCARG(&ap, which)) < 0)
   1674 		return -error;
   1675 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
   1676 		return error;
   1677 	linux_to_bsd_rlimit(&rl, &orl);
   1678 	/* XXX: alpha complains about this */
   1679 	if ((error = copyout(&rl, (void *)SCARG(&ap, rlp), sizeof(rl))) != 0)
   1680 		return error;
   1681 	return sys_setrlimit(l, &ap, retval);
   1682 }
   1683 
   1684 #ifndef __mips__
   1685 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
   1686 int
   1687 linux_sys_ugetrlimit(l, v, retval)
   1688 	struct lwp *l;
   1689 	void *v;
   1690 	register_t *retval;
   1691 {
   1692 	return linux_sys_getrlimit(l, v, retval);
   1693 }
   1694 #endif
   1695 
   1696 /*
   1697  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1698  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1699  * This is the way Linux does it and glibc depends on this behaviour.
   1700  */
   1701 int
   1702 linux_sys_nosys(l, v, retval)
   1703 	struct lwp *l;
   1704 	void *v;
   1705 	register_t *retval;
   1706 {
   1707 	return (ENOSYS);
   1708 }
   1709