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