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