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