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