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