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