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