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