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