Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: linux_misc.c,v 1.268 2026/09/20 13:43:51 riastradh 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.268 2026/09/20 13:43:51 riastradh 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/epoll.h>
     68 #include <sys/eventfd.h>
     69 #include <sys/file.h>
     70 #include <sys/stat.h>
     71 #include <sys/filedesc.h>
     72 #include <sys/ioctl.h>
     73 #include <sys/kernel.h>
     74 #include <sys/malloc.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/mman.h>
     77 #include <sys/mount.h>
     78 #include <sys/poll.h>
     79 #include <sys/prot.h>
     80 #include <sys/reboot.h>
     81 #include <sys/resource.h>
     82 #include <sys/resourcevar.h>
     83 #include <sys/select.h>
     84 #include <sys/signal.h>
     85 #include <sys/signalvar.h>
     86 #include <sys/socket.h>
     87 #include <sys/time.h>
     88 #include <sys/times.h>
     89 #include <sys/vnode.h>
     90 #include <sys/uio.h>
     91 #include <sys/wait.h>
     92 #include <sys/utsname.h>
     93 #include <sys/unistd.h>
     94 #include <sys/vfs_syscalls.h>
     95 #include <sys/swap.h>		/* for SWAP_ON */
     96 #include <sys/sysctl.h>		/* for KERN_DOMAINNAME */
     97 #include <sys/kauth.h>
     98 #include <sys/futex.h>
     99 
    100 #include <sys/ptrace.h>
    101 #include <machine/ptrace.h>
    102 
    103 #include <sys/syscall.h>
    104 #include <sys/syscallargs.h>
    105 
    106 #include <compat/sys/resource.h>
    107 
    108 #include <compat/linux/common/linux_machdep.h>
    109 #include <compat/linux/common/linux_types.h>
    110 #include <compat/linux/common/linux_signal.h>
    111 #include <compat/linux/common/linux_ipc.h>
    112 #include <compat/linux/common/linux_sem.h>
    113 
    114 #include <compat/linux/common/linux_fcntl.h>
    115 #include <compat/linux/common/linux_mmap.h>
    116 #include <compat/linux/common/linux_dirent.h>
    117 #include <compat/linux/common/linux_util.h>
    118 #include <compat/linux/common/linux_misc.h>
    119 #include <compat/linux/common/linux_statfs.h>
    120 #include <compat/linux/common/linux_limit.h>
    121 #include <compat/linux/common/linux_ptrace.h>
    122 #include <compat/linux/common/linux_reboot.h>
    123 #include <compat/linux/common/linux_emuldata.h>
    124 #include <compat/linux/common/linux_sched.h>
    125 
    126 #include <compat/linux/linux_syscallargs.h>
    127 
    128 const int linux_ptrace_request_map[] = {
    129 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
    130 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
    131 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
    132 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
    133 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
    134 	LINUX_PTRACE_CONT,	PT_CONTINUE,
    135 	LINUX_PTRACE_KILL,	PT_KILL,
    136 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
    137 	LINUX_PTRACE_DETACH,	PT_DETACH,
    138 # ifdef PT_STEP
    139 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
    140 # endif
    141 	LINUX_PTRACE_SYSCALL,	PT_SYSCALL,
    142 	-1
    143 };
    144 
    145 const struct linux_mnttypes linux_fstypes[] = {
    146 	{ MOUNT_FFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    147 	{ MOUNT_NFS,		LINUX_NFS_SUPER_MAGIC 		},
    148 	{ MOUNT_MFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    149 	{ MOUNT_MSDOS,		LINUX_MSDOS_SUPER_MAGIC		},
    150 	{ MOUNT_LFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    151 	{ MOUNT_FDESC,		LINUX_DEFAULT_SUPER_MAGIC	},
    152 	{ MOUNT_NULL,		LINUX_DEFAULT_SUPER_MAGIC	},
    153 	{ MOUNT_OVERLAY,	LINUX_DEFAULT_SUPER_MAGIC	},
    154 	{ MOUNT_UMAP,		LINUX_DEFAULT_SUPER_MAGIC	},
    155 	{ MOUNT_KERNFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    156 	{ MOUNT_PROCFS,		LINUX_PROC_SUPER_MAGIC		},
    157 	{ MOUNT_AFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    158 	{ MOUNT_CD9660,		LINUX_ISOFS_SUPER_MAGIC		},
    159 	{ MOUNT_UNION,		LINUX_DEFAULT_SUPER_MAGIC	},
    160 	{ MOUNT_ADOSFS,		LINUX_ADFS_SUPER_MAGIC		},
    161 	{ MOUNT_EXT2FS,		LINUX_EXT2_SUPER_MAGIC		},
    162 	{ MOUNT_CFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    163 	{ MOUNT_CODA,		LINUX_CODA_SUPER_MAGIC		},
    164 	{ MOUNT_FILECORE,	LINUX_DEFAULT_SUPER_MAGIC	},
    165 	{ MOUNT_NTFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    166 	{ MOUNT_SMBFS,		LINUX_SMB_SUPER_MAGIC		},
    167 	{ MOUNT_PTYFS,		LINUX_DEVPTS_SUPER_MAGIC	},
    168 	{ MOUNT_TMPFS,		LINUX_TMPFS_SUPER_MAGIC		}
    169 };
    170 const int linux_fstypes_cnt = sizeof(linux_fstypes) / sizeof(linux_fstypes[0]);
    171 
    172 #ifdef DEBUG_LINUX
    173 #define DPRINTF(a, ...)	uprintf(a, __VA_ARGS__)
    174 #else
    175 #define DPRINTF(a, ...)
    176 #endif
    177 
    178 /* Local linux_misc.c functions: */
    179 static void linux_to_bsd_mmap_args(struct sys_mmap_args *,
    180     const struct linux_sys_mmap_args *);
    181 static int linux_mmap(struct lwp *, const struct linux_sys_mmap_args *,
    182     register_t *, off_t);
    183 static int linux_to_native_wait_options(int);
    184 
    185 /*
    186  * The information on a terminated (or stopped) process needs
    187  * to be converted in order for Linux binaries to get a valid signal
    188  * number out of it.
    189  */
    190 int
    191 bsd_to_linux_wstat(int st)
    192 {
    193 
    194 	int sig;
    195 
    196 	if (WIFSIGNALED(st)) {
    197 		sig = WTERMSIG(st);
    198 		if (sig >= 0 && sig < NSIG)
    199 			st= (st & ~0177) | native_to_linux_signo[sig];
    200 	} else if (WIFSTOPPED(st)) {
    201 		sig = WSTOPSIG(st);
    202 		if (sig >= 0 && sig < NSIG)
    203 			st = (st & ~0xff00) |
    204 			    (native_to_linux_signo[sig] << 8);
    205 	}
    206 	return st;
    207 }
    208 
    209 /*
    210  * wait4(2).  Passed on to the NetBSD call, surrounded by code to
    211  * reserve some space for a NetBSD-style wait status, and converting
    212  * it to what Linux wants.
    213  */
    214 int
    215 linux_sys_wait4(struct lwp *l, const struct linux_sys_wait4_args *uap, register_t *retval)
    216 {
    217 	/* {
    218 		syscallarg(int) pid;
    219 		syscallarg(int *) status;
    220 		syscallarg(int) options;
    221 		syscallarg(struct rusage50 *) rusage;
    222 	} */
    223 	int error, status, options, linux_options, pid = SCARG(uap, pid);
    224 	struct rusage50 ru50;
    225 	struct rusage ru;
    226 	proc_t *p;
    227 
    228 	linux_options = SCARG(uap, options);
    229 	if (linux_options & ~(LINUX_WAIT4_KNOWNFLAGS))
    230 		return (EINVAL);
    231 
    232 	options = linux_to_native_wait_options(linux_options);
    233 # ifdef DIAGNOSTIC
    234 	if (linux_options & LINUX_WNOTHREAD)
    235 		printf("WARNING: %s: linux process %d.%d called "
    236 		       "waitpid with __WNOTHREAD set!\n",
    237 		       __FILE__, l->l_proc->p_pid, l->l_lid);
    238 
    239 # endif
    240 
    241 	error = do_sys_wait(&pid, &status, options,
    242 	    SCARG(uap, rusage) != NULL ? &ru : NULL);
    243 
    244 	retval[0] = pid;
    245 	if (pid == 0)
    246 		return error;
    247 
    248 	p = curproc;
    249 	mutex_enter(p->p_lock);
    250 	sigdelset(&p->p_sigpend.sp_set, SIGCHLD); /* XXXAD ksiginfo leak */
    251 	mutex_exit(p->p_lock);
    252 
    253 	if (SCARG(uap, rusage) != NULL) {
    254 		rusage_to_rusage50(&ru, &ru50);
    255 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
    256 	}
    257 
    258 	if (error == 0 && SCARG(uap, status) != NULL) {
    259 		status = bsd_to_linux_wstat(status);
    260 		error = copyout(&status, SCARG(uap, status), sizeof status);
    261 	}
    262 
    263 	return error;
    264 }
    265 
    266 /*
    267  * waitid(2).  Converting arguments to the NetBSD equivalent and
    268  * calling it.
    269  */
    270 int
    271 linux_sys_waitid(struct lwp *l, const struct linux_sys_waitid_args *uap, register_t *retval)
    272 {
    273 	/* {
    274 		syscallarg(int) idtype;
    275 		syscallarg(id_t) id;
    276 		syscallarg(linux_siginfo_t *) infop;
    277 		syscallarg(int) options;
    278 		syscallarg(struct rusage50 *) rusage;
    279 	} */
    280 	int error, linux_options, options, linux_idtype, status;
    281 	pid_t pid;
    282 	idtype_t idtype;
    283 	id_t id;
    284 	siginfo_t info;
    285 	linux_siginfo_t linux_info;
    286 	struct wrusage wru;
    287 	struct rusage50 ru50;
    288 
    289 	linux_idtype = SCARG(uap, idtype);
    290 	switch (linux_idtype) {
    291 	case LINUX_P_ALL:
    292 		idtype = P_ALL;
    293 		break;
    294 	case LINUX_P_PID:
    295 		idtype = P_PID;
    296 		break;
    297 	case LINUX_P_PGID:
    298 		idtype = P_PGID;
    299 		break;
    300 	case LINUX_P_PIDFD:
    301 		return EOPNOTSUPP;
    302 	default:
    303 		return EINVAL;
    304 	}
    305 
    306 	linux_options = SCARG(uap, options);
    307 	if (linux_options & ~(LINUX_WAITID_KNOWNFLAGS))
    308 		return EINVAL;
    309 
    310 	options = linux_to_native_wait_options(linux_options);
    311 	id = SCARG(uap, id);
    312 
    313 	error = do_sys_waitid(idtype, id, &pid, &status, options, &wru, &info);
    314 	if (pid == 0 && options & WNOHANG) {
    315 		info.si_signo = 0;
    316 		info.si_pid = 0;
    317 	}
    318 
    319 	if (error == 0 && SCARG(uap, infop) != NULL) {
    320 		/* POSIX says that this NULL check is a bug, but Linux does this. */
    321 		native_to_linux_siginfo(&linux_info, &info._info);
    322 		error = copyout(&linux_info, SCARG(uap, infop), sizeof(linux_info));
    323 	}
    324 
    325 	if (error == 0 && SCARG(uap, rusage) != NULL) {
    326 		rusage_to_rusage50(&wru.wru_children, &ru50);
    327 		error = copyout(&ru50, SCARG(uap, rusage), sizeof(ru50));
    328 	}
    329 
    330 	return error;
    331 }
    332 
    333 /*
    334  * Convert the options argument for wait4(2) and waitid(2) from what
    335  * Linux wants to what NetBSD wants.
    336  */
    337 static int
    338 linux_to_native_wait_options(int linux_options)
    339 {
    340 	int options = 0;
    341 
    342 	if (linux_options & LINUX_WNOHANG)
    343 		options |= WNOHANG;
    344 	if (linux_options & LINUX_WUNTRACED)
    345 		options |= WUNTRACED;
    346 	if (linux_options & LINUX_WEXITED)
    347 		options |= WEXITED;
    348 	if (linux_options & LINUX_WCONTINUED)
    349 		options |= WCONTINUED;
    350 	if (linux_options & LINUX_WNOWAIT)
    351 		options |= WNOWAIT;
    352 	if (linux_options & LINUX_WALL)
    353 		options |= WALLSIG;
    354 	if (linux_options & LINUX_WCLONE)
    355 		options |= WALTSIG;
    356 
    357 	return options;
    358 }
    359 
    360 /*
    361  * Linux brk(2).  Like native, but always return the new break value.
    362  */
    363 int
    364 linux_sys_brk(struct lwp *l, const struct linux_sys_brk_args *uap, register_t *retval)
    365 {
    366 	/* {
    367 		syscallarg(char *) nsize;
    368 	} */
    369 	struct proc *p = l->l_proc;
    370 	struct vmspace *vm = p->p_vmspace;
    371 	struct sys_obreak_args oba;
    372 
    373 	memset(&oba, 0, sizeof(oba));
    374 	SCARG(&oba, nsize) = SCARG(uap, nsize);
    375 
    376 	(void) sys_obreak(l, &oba, retval);
    377 	retval[0] = (register_t)((char *)vm->vm_daddr + ptoa(vm->vm_dsize));
    378 	return 0;
    379 }
    380 
    381 /*
    382  * Implement the fs stat functions. Straightforward.
    383  */
    384 int
    385 linux_sys_statfs(struct lwp *l, const struct linux_sys_statfs_args *uap, register_t *retval)
    386 {
    387 	/* {
    388 		syscallarg(const char *) path;
    389 		syscallarg(struct linux_statfs *) sp;
    390 	} */
    391 	struct statvfs *sb;
    392 	struct linux_statfs ltmp;
    393 	int error;
    394 
    395 	sb = STATVFSBUF_GET();
    396 	error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb);
    397 	if (error == 0) {
    398 		bsd_to_linux_statfs(sb, &ltmp);
    399 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    400 	}
    401 	STATVFSBUF_PUT(sb);
    402 
    403 	return error;
    404 }
    405 
    406 int
    407 linux_sys_fstatfs(struct lwp *l, const struct linux_sys_fstatfs_args *uap, register_t *retval)
    408 {
    409 	/* {
    410 		syscallarg(int) fd;
    411 		syscallarg(struct linux_statfs *) sp;
    412 	} */
    413 	struct statvfs *sb;
    414 	struct linux_statfs ltmp;
    415 	int error;
    416 
    417 	sb = STATVFSBUF_GET();
    418 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
    419 	if (error == 0) {
    420 		bsd_to_linux_statfs(sb, &ltmp);
    421 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    422 	}
    423 	STATVFSBUF_PUT(sb);
    424 
    425 	return error;
    426 }
    427 
    428 /*
    429  * uname(). Just copy the info from the various strings stored in the
    430  * kernel, and put it in the Linux utsname structure. That structure
    431  * is almost the same as the NetBSD one, only it has fields 65 characters
    432  * long, and an extra domainname field.
    433  */
    434 int
    435 linux_sys_uname(struct lwp *l, const struct linux_sys_uname_args *uap, register_t *retval)
    436 {
    437 	/* {
    438 		syscallarg(struct linux_utsname *) up;
    439 	} */
    440 	struct linux_utsname luts;
    441 
    442 	memset(&luts, 0, sizeof(luts));
    443 	strlcpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    444 	strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    445 	strlcpy(luts.l_release, linux_release, sizeof(luts.l_release));
    446 	strlcpy(luts.l_version, linux_version, sizeof(luts.l_version));
    447 	strlcpy(luts.l_machine, LINUX_UNAME_ARCH, sizeof(luts.l_machine));
    448 	strlcpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    449 
    450 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    451 }
    452 
    453 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
    454 /* Used indirectly on: arm, i386, m68k */
    455 
    456 /*
    457  * New type Linux mmap call.
    458  * Only called directly on machines with >= 6 free regs.
    459  */
    460 int
    461 linux_sys_mmap(struct lwp *l, const struct linux_sys_mmap_args *uap, register_t *retval)
    462 {
    463 	/* {
    464 		syscallarg(unsigned long) addr;
    465 		syscallarg(size_t) len;
    466 		syscallarg(int) prot;
    467 		syscallarg(int) flags;
    468 		syscallarg(int) fd;
    469 		syscallarg(linux_off_t) offset;
    470 	} */
    471 
    472 	if (SCARG(uap, offset) & PAGE_MASK)
    473 		return EINVAL;
    474 
    475 	return linux_mmap(l, uap, retval, SCARG(uap, offset));
    476 }
    477 
    478 /*
    479  * Guts of most architectures' mmap64() implementations.  This shares
    480  * its list of arguments with linux_sys_mmap().
    481  *
    482  * The difference in linux_sys_mmap2() is that "offset" is actually
    483  * (offset / pagesize), not an absolute byte count.  This translation
    484  * to pagesize offsets is done inside glibc between the mmap64() call
    485  * point, and the actual syscall.
    486  */
    487 int
    488 linux_sys_mmap2(struct lwp *l, const struct linux_sys_mmap2_args *uap, register_t *retval)
    489 {
    490 	/* {
    491 		syscallarg(unsigned long) addr;
    492 		syscallarg(size_t) len;
    493 		syscallarg(int) prot;
    494 		syscallarg(int) flags;
    495 		syscallarg(int) fd;
    496 		syscallarg(linux_off_t) offset;
    497 	} */
    498 
    499 	return linux_mmap(l, uap, retval,
    500 	    ((off_t)SCARG(uap, offset)) << PAGE_SHIFT);
    501 }
    502 
    503 /*
    504  * Massage arguments and call system mmap(2).
    505  */
    506 static int
    507 linux_mmap(struct lwp *l, const struct linux_sys_mmap_args *uap, register_t *retval, off_t offset)
    508 {
    509 	struct sys_mmap_args cma;
    510 	int error;
    511 	size_t mmoff=0;
    512 
    513 	memset(&cma, 0, sizeof(cma));
    514 	linux_to_bsd_mmap_args(&cma, uap);
    515 	SCARG(&cma, pos) = offset;
    516 
    517 	if (SCARG(uap, flags) & LINUX_MAP_GROWSDOWN) {
    518 		/*
    519 		 * Request for stack-like memory segment. On linux, this
    520 		 * works by mmap()ping (small) segment, which is automatically
    521 		 * extended when page fault happens below the currently
    522 		 * allocated area. We emulate this by allocating (typically
    523 		 * bigger) segment sized at current stack size limit, and
    524 		 * offsetting the requested and returned address accordingly.
    525 		 * Since physical pages are only allocated on-demand, this
    526 		 * is effectively identical.
    527 		 */
    528 		rlim_t ssl = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
    529 
    530 		if (SCARG(&cma, len) < ssl) {
    531 			/* Compute the address offset */
    532 			mmoff = round_page(ssl) - SCARG(uap, len);
    533 
    534 			if (SCARG(&cma, addr))
    535 				SCARG(&cma, addr) = (char *)SCARG(&cma, addr) - mmoff;
    536 
    537 			SCARG(&cma, len) = (size_t) ssl;
    538 		}
    539 	}
    540 
    541 	error = sys_mmap(l, &cma, retval);
    542 	if (error)
    543 		return (error);
    544 
    545 	/* Shift the returned address for stack-like segment if necessary */
    546 	retval[0] += mmoff;
    547 
    548 	return (0);
    549 }
    550 
    551 static void
    552 linux_to_bsd_mmap_args(struct sys_mmap_args *cma, const struct linux_sys_mmap_args *uap)
    553 {
    554 	int flags = MAP_TRYFIXED, fl = SCARG(uap, flags);
    555 
    556 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
    557 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
    558 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
    559 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
    560 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_LOCKED, MAP_WIRED);
    561 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
    562 
    563 	SCARG(cma, addr) = (void *)SCARG(uap, addr);
    564 	SCARG(cma, len) = SCARG(uap, len);
    565 	SCARG(cma, prot) = SCARG(uap, prot);
    566 	if (SCARG(cma, prot) & VM_PROT_WRITE) /* XXX */
    567 		SCARG(cma, prot) |= VM_PROT_READ;
    568 	SCARG(cma, flags) = flags;
    569 	SCARG(cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
    570 	SCARG(cma, PAD) = 0;
    571 }
    572 
    573 #define	LINUX_MREMAP_MAYMOVE	1
    574 #define	LINUX_MREMAP_FIXED	2
    575 
    576 int
    577 linux_sys_mremap(struct lwp *l, const struct linux_sys_mremap_args *uap, register_t *retval)
    578 {
    579 	/* {
    580 		syscallarg(void *) old_address;
    581 		syscallarg(size_t) old_size;
    582 		syscallarg(size_t) new_size;
    583 		syscallarg(u_long) flags;
    584 	} */
    585 
    586 	struct proc *p;
    587 	struct vm_map *map;
    588 	vaddr_t oldva;
    589 	vaddr_t newva;
    590 	size_t oldsize;
    591 	size_t newsize;
    592 	int flags;
    593 	int uvmflags;
    594 	int error;
    595 
    596 	flags = SCARG(uap, flags);
    597 	oldva = (vaddr_t)SCARG(uap, old_address);
    598 	oldsize = round_page(SCARG(uap, old_size));
    599 	newsize = round_page(SCARG(uap, new_size));
    600 	if ((flags & ~(LINUX_MREMAP_FIXED|LINUX_MREMAP_MAYMOVE)) != 0) {
    601 		error = EINVAL;
    602 		goto done;
    603 	}
    604 	if ((flags & LINUX_MREMAP_FIXED) != 0) {
    605 		if ((flags & LINUX_MREMAP_MAYMOVE) == 0) {
    606 			error = EINVAL;
    607 			goto done;
    608 		}
    609 #if 0 /* notyet */
    610 		newva = SCARG(uap, new_address);
    611 		uvmflags = MAP_FIXED;
    612 #else /* notyet */
    613 		error = EOPNOTSUPP;
    614 		goto done;
    615 #endif /* notyet */
    616 	} else if ((flags & LINUX_MREMAP_MAYMOVE) != 0) {
    617 		uvmflags = 0;
    618 	} else {
    619 		newva = oldva;
    620 		uvmflags = MAP_FIXED;
    621 	}
    622 	p = l->l_proc;
    623 	map = &p->p_vmspace->vm_map;
    624 	error = uvm_mremap(map, oldva, oldsize, map, &newva, newsize, p,
    625 	    uvmflags);
    626 
    627 done:
    628 	*retval = (error != 0) ? 0 : (register_t)newva;
    629 	return error;
    630 }
    631 
    632 #ifdef USRSTACK
    633 int
    634 linux_sys_mprotect(struct lwp *l, const struct linux_sys_mprotect_args *uap, register_t *retval)
    635 {
    636 	/* {
    637 		syscallarg(const void *) start;
    638 		syscallarg(unsigned long) len;
    639 		syscallarg(int) prot;
    640 	} */
    641 	struct vm_map_entry *entry;
    642 	struct vm_map *map;
    643 	struct proc *p;
    644 	vaddr_t end, start, len, stacklim;
    645 	int prot, grows;
    646 
    647 	start = (vaddr_t)SCARG(uap, start);
    648 	len = round_page(SCARG(uap, len));
    649 	prot = SCARG(uap, prot);
    650 	grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
    651 	prot &= ~grows;
    652 	end = start + len;
    653 
    654 	if (start & PAGE_MASK)
    655 		return EINVAL;
    656 	if (end < start)
    657 		return EINVAL;
    658 	if (end == start)
    659 		return 0;
    660 
    661 	if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
    662 		return EINVAL;
    663 	if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
    664 		return EINVAL;
    665 
    666 	p = l->l_proc;
    667 	map = &p->p_vmspace->vm_map;
    668 	vm_map_lock(map);
    669 # ifdef notdef
    670 	VM_MAP_RANGE_CHECK(map, start, end);
    671 # endif
    672 	if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
    673 		vm_map_unlock(map);
    674 		return ENOMEM;
    675 	}
    676 
    677 	/*
    678 	 * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
    679 	 */
    680 
    681 	stacklim = (vaddr_t)p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
    682 	if (grows & LINUX_PROT_GROWSDOWN) {
    683 		if (USRSTACK - stacklim <= start && start < USRSTACK) {
    684 			start = USRSTACK - stacklim;
    685 		} else {
    686 			start = entry->start;
    687 		}
    688 	} else if (grows & LINUX_PROT_GROWSUP) {
    689 		if (USRSTACK <= end && end < USRSTACK + stacklim) {
    690 			end = USRSTACK + stacklim;
    691 		} else {
    692 			end = entry->end;
    693 		}
    694 	}
    695 	vm_map_unlock(map);
    696 	return uvm_map_protect_user(l, start, end, prot);
    697 }
    698 #endif /* USRSTACK */
    699 
    700 /*
    701  * This code is partly stolen from src/lib/libc/compat-43/times.c
    702  */
    703 
    704 #define	CONVTCK(r)	(r.tv_sec * hz + r.tv_usec / (1000000 / hz))
    705 
    706 int
    707 linux_sys_times(struct lwp *l, const struct linux_sys_times_args *uap, register_t *retval)
    708 {
    709 	/* {
    710 		syscallarg(struct times *) tms;
    711 	} */
    712 	struct proc *p = l->l_proc;
    713 	struct timeval t;
    714 	int error;
    715 
    716 	if (SCARG(uap, tms)) {
    717 		struct linux_tms ltms;
    718 		struct rusage ru;
    719 
    720 		memset(&ltms, 0, sizeof(ltms));
    721 
    722 		mutex_enter(p->p_lock);
    723 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
    724 		ltms.ltms_utime = CONVTCK(ru.ru_utime);
    725 		ltms.ltms_stime = CONVTCK(ru.ru_stime);
    726 		ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    727 		ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    728 		mutex_exit(p->p_lock);
    729 
    730 		if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    731 			return error;
    732 	}
    733 
    734 	getmicrouptime(&t);
    735 
    736 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    737 	return 0;
    738 }
    739 
    740 #undef CONVTCK
    741 
    742 #if !defined(__aarch64__)
    743 /*
    744  * Linux 'readdir' call. This code is mostly taken from the
    745  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    746  * an attempt has been made to keep it a little cleaner (failing
    747  * miserably, because of the cruft needed if count 1 is passed).
    748  *
    749  * The d_off field should contain the offset of the next valid entry,
    750  * but in Linux it has the offset of the entry itself. We emulate
    751  * that bug here.
    752  *
    753  * Read in BSD-style entries, convert them, and copy them out.
    754  *
    755  * Note that this doesn't handle union-mounted filesystems.
    756  */
    757 int
    758 linux_sys_getdents(struct lwp *l, const struct linux_sys_getdents_args *uap, register_t *retval)
    759 {
    760 	/* {
    761 		syscallarg(int) fd;
    762 		syscallarg(struct linux_dirent *) dent;
    763 		syscallarg(unsigned int) count;
    764 	} */
    765 	struct dirent *bdp;
    766 	struct vnode *vp;
    767 	char *inp, *tbuf;		/* BSD-format */
    768 	int len, reclen;		/* BSD-format */
    769 	char *outp;			/* Linux-format */
    770 	int resid, linux_reclen = 0;	/* Linux-format */
    771 	struct file *fp;
    772 	struct uio auio;
    773 	struct iovec aiov;
    774 	struct linux_dirent idb;
    775 	off_t off;		/* true file offset */
    776 	int buflen, error, eofflag, nbytes, oldcall;
    777 	struct vattr va;
    778 	off_t *cookiebuf = NULL, *cookie;
    779 	int ncookies;
    780 
    781 	/* fd_getvnode() will use the descriptor for us */
    782 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    783 		return (error);
    784 
    785 	if ((fp->f_flag & FREAD) == 0) {
    786 		error = EBADF;
    787 		goto out1;
    788 	}
    789 
    790 	vp = (struct vnode *)fp->f_data;
    791 	if (vp->v_type != VDIR) {
    792 		error = ENOTDIR;
    793 		goto out1;
    794 	}
    795 
    796 	vn_lock(vp, LK_SHARED | LK_RETRY);
    797 	error = VOP_GETATTR(vp, &va, l->l_cred);
    798 	VOP_UNLOCK(vp);
    799 	if (error)
    800 		goto out1;
    801 
    802 	nbytes = SCARG(uap, count);
    803 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    804 		nbytes = sizeof (idb);
    805 		buflen = uimax(va.va_blocksize, nbytes);
    806 		oldcall = 1;
    807 	} else {
    808 		buflen = uimin(MAXBSIZE, nbytes);
    809 		if (buflen < va.va_blocksize)
    810 			buflen = va.va_blocksize;
    811 		oldcall = 0;
    812 	}
    813 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    814 
    815 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    816 	off = fp->f_offset;
    817 again:
    818 	aiov.iov_base = tbuf;
    819 	aiov.iov_len = buflen;
    820 	auio.uio_iov = &aiov;
    821 	auio.uio_iovcnt = 1;
    822 	auio.uio_rw = UIO_READ;
    823 	auio.uio_resid = buflen;
    824 	auio.uio_offset = off;
    825 	UIO_SETUP_SYSSPACE(&auio);
    826 	/*
    827          * First we read into the malloc'ed buffer, then
    828          * we massage it into user space, one record at a time.
    829          */
    830 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    831 	    &ncookies);
    832 	if (error)
    833 		goto out;
    834 
    835 	inp = tbuf;
    836 	outp = (void *)SCARG(uap, dent);
    837 	resid = nbytes;
    838 	if ((len = buflen - auio.uio_resid) == 0)
    839 		goto eof;
    840 
    841 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    842 		bdp = (struct dirent *)inp;
    843 		reclen = bdp->d_reclen;
    844 		if (reclen & 3) {
    845 			error = EIO;
    846 			goto out;
    847 		}
    848 		if (bdp->d_fileno == 0) {
    849 			inp += reclen;	/* it is a hole; squish it out */
    850 			if (cookie)
    851 				off = *cookie++;
    852 			else
    853 				off += reclen;
    854 			continue;
    855 		}
    856 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    857 		if (reclen > len || resid < linux_reclen) {
    858 			/* entry too big for buffer, so just stop */
    859 			outp++;
    860 			break;
    861 		}
    862 		/*
    863 		 * Massage in place to make a Linux-shaped dirent (otherwise
    864 		 * we have to worry about touching user memory outside of
    865 		 * the copyout() call).
    866 		 */
    867 		memset(&idb, 0, sizeof(idb));
    868 		idb.d_ino = bdp->d_fileno;
    869 		/*
    870 		 * The old readdir() call misuses the offset and reclen fields.
    871 		 */
    872 		if (oldcall) {
    873 			idb.d_off = (linux_off_t)linux_reclen;
    874 			idb.d_reclen = (u_short)bdp->d_namlen;
    875 		} else {
    876 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
    877 				compat_offseterr(vp, "linux_getdents");
    878 				error = EINVAL;
    879 				goto out;
    880 			}
    881 			idb.d_off = (linux_off_t)off;
    882 			idb.d_reclen = (u_short)linux_reclen;
    883 			/* Linux puts d_type at the end of each record */
    884 			*((char *)&idb + idb.d_reclen - 1) = bdp->d_type;
    885 		}
    886 		memcpy(idb.d_name, bdp->d_name,
    887 		    MIN(sizeof(idb.d_name), bdp->d_namlen + 1));
    888 		if ((error = copyout((void *)&idb, outp, linux_reclen)))
    889 			goto out;
    890 		/* advance past this real entry */
    891 		inp += reclen;
    892 		if (cookie)
    893 			off = *cookie++; /* each entry points to itself */
    894 		else
    895 			off += reclen;
    896 		/* advance output past Linux-shaped entry */
    897 		outp += linux_reclen;
    898 		resid -= linux_reclen;
    899 		if (oldcall)
    900 			break;
    901 	}
    902 
    903 	/* if we squished out the whole block, try again */
    904 	if (outp == (void *)SCARG(uap, dent)) {
    905 		if (cookiebuf)
    906 			free(cookiebuf, M_TEMP);
    907 		cookiebuf = NULL;
    908 		goto again;
    909 	}
    910 	fp->f_offset = off;	/* update the vnode offset */
    911 
    912 	if (oldcall)
    913 		nbytes = resid + linux_reclen;
    914 
    915 eof:
    916 	*retval = nbytes - resid;
    917 out:
    918 	VOP_UNLOCK(vp);
    919 	if (cookiebuf)
    920 		free(cookiebuf, M_TEMP);
    921 	free(tbuf, M_TEMP);
    922 out1:
    923 	fd_putfile(SCARG(uap, fd));
    924 	return error;
    925 }
    926 #endif
    927 
    928 #if !defined(__aarch64__)
    929 /*
    930  * Even when just using registers to pass arguments to syscalls you can
    931  * have 5 of them on the i386. So this newer version of select() does
    932  * this.
    933  */
    934 int
    935 linux_sys_select(struct lwp *l, const struct linux_sys_select_args *uap, register_t *retval)
    936 {
    937 	/* {
    938 		syscallarg(int) nfds;
    939 		syscallarg(fd_set *) readfds;
    940 		syscallarg(fd_set *) writefds;
    941 		syscallarg(fd_set *) exceptfds;
    942 		syscallarg(struct timeval50 *) timeout;
    943 	} */
    944 
    945 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    946 	    SCARG(uap, writefds), SCARG(uap, exceptfds),
    947 	    (struct linux_timeval *)SCARG(uap, timeout));
    948 }
    949 
    950 /*
    951  * Common code for the old and new versions of select(). A couple of
    952  * things are important:
    953  * 1) return the amount of time left in the 'timeout' parameter
    954  * 2) select never returns ERESTART on Linux, always return EINTR
    955  */
    956 int
    957 linux_select1(struct lwp *l, register_t *retval, int nfds, fd_set *readfds,
    958     fd_set *writefds, fd_set *exceptfds, struct linux_timeval *timeout)
    959 {
    960 	struct timespec ts0, ts1, uts, *ts = NULL;
    961 	struct linux_timeval ltv;
    962 	int error;
    963 
    964 	/*
    965 	 * Store current time for computation of the amount of
    966 	 * time left.
    967 	 */
    968 	if (timeout) {
    969 		if ((error = copyin(timeout, &ltv, sizeof(ltv))))
    970 			return error;
    971 		uts.tv_sec = ltv.tv_sec;
    972 		uts.tv_nsec = (long)((unsigned long)ltv.tv_usec * 1000);
    973 		if (itimespecfix(&uts)) {
    974 			/*
    975 			 * The timeval was invalid.  Convert it to something
    976 			 * valid that will act as it does under Linux.
    977 			 */
    978 			uts.tv_sec += uts.tv_nsec / 1000000000;
    979 			uts.tv_nsec %= 1000000000;
    980 			if (uts.tv_nsec < 0) {
    981 				uts.tv_sec -= 1;
    982 				uts.tv_nsec += 1000000000;
    983 			}
    984 			if (uts.tv_sec < 0)
    985 				timespecclear(&uts);
    986 		}
    987 		ts = &uts;
    988 		nanotime(&ts0);
    989 	}
    990 
    991 	error = selcommon(retval, nfds, readfds, writefds, exceptfds, ts, NULL);
    992 
    993 	if (error) {
    994 		/*
    995 		 * See fs/select.c in the Linux kernel.  Without this,
    996 		 * Maelstrom doesn't work.
    997 		 */
    998 		if (error == ERESTART)
    999 			error = EINTR;
   1000 		return error;
   1001 	}
   1002 
   1003 	if (timeout) {
   1004 		if (*retval) {
   1005 			/*
   1006 			 * Compute how much time was left of the timeout,
   1007 			 * by subtracting the current time and the time
   1008 			 * before we started the call, and subtracting
   1009 			 * that result from the user-supplied value.
   1010 			 */
   1011 			nanotime(&ts1);
   1012 			timespecsub(&ts1, &ts0, &ts1);
   1013 			timespecsub(&uts, &ts1, &uts);
   1014 			if (uts.tv_sec < 0)
   1015 				timespecclear(&uts);
   1016 		} else
   1017 			timespecclear(&uts);
   1018 		ltv.tv_sec = uts.tv_sec;
   1019 		ltv.tv_usec = uts.tv_nsec / 1000;
   1020 		if ((error = copyout(&ltv, timeout, sizeof(ltv))))
   1021 			return error;
   1022 	}
   1023 
   1024 	return 0;
   1025 }
   1026 #endif
   1027 
   1028 /*
   1029  * Derived from FreeBSD's sys/compat/linux/linux_misc.c:linux_pselect6()
   1030  * which was contributed by Dmitry Chagin
   1031  * https://svnweb.freebsd.org/base?view=revision&revision=283403
   1032  */
   1033 int
   1034 linux_sys_pselect6(struct lwp *l,
   1035 	const struct linux_sys_pselect6_args *uap, register_t *retval)
   1036 {
   1037 	/* {
   1038 		syscallarg(int) nfds;
   1039 		syscallarg(fd_set *) readfds;
   1040 		syscallarg(fd_set *) writefds;
   1041 		syscallarg(fd_set *) exceptfds;
   1042 		syscallarg(struct timespec *) timeout;
   1043 		syscallarg(linux_sized_sigset_t *) ss;
   1044 	} */
   1045 	struct timespec uts, ts0, ts1, *tsp;
   1046 	linux_sized_sigset_t lsss;
   1047 	struct linux_timespec lts;
   1048 	linux_sigset_t lss;
   1049 	sigset_t *ssp;
   1050 	sigset_t ss;
   1051 	int error;
   1052 
   1053 	ssp = NULL;
   1054 	if (SCARG(uap, ss) != NULL) {
   1055 		if ((error = copyin(SCARG(uap, ss), &lsss, sizeof(lsss))) != 0)
   1056 			return (error);
   1057 		if (lsss.ss_len != sizeof(lss))
   1058 			return (EINVAL);
   1059 		if (lsss.ss != NULL) {
   1060 			if ((error = copyin(lsss.ss, &lss, sizeof(lss))) != 0)
   1061 				return (error);
   1062 			linux_to_native_sigset(&ss, &lss);
   1063 			ssp = &ss;
   1064 		}
   1065 	}
   1066 
   1067 	if (SCARG(uap, timeout) != NULL) {
   1068 		error = copyin(SCARG(uap, timeout), &lts, sizeof(lts));
   1069 		if (error != 0)
   1070 			return (error);
   1071 		linux_to_native_timespec(&uts, &lts);
   1072 
   1073 		if (itimespecfix(&uts))
   1074 			return (EINVAL);
   1075 
   1076 		nanotime(&ts0);
   1077 		tsp = &uts;
   1078 	} else {
   1079 		tsp = NULL;
   1080 	}
   1081 
   1082 	error = selcommon(retval, SCARG(uap, nfds), SCARG(uap, readfds),
   1083 	    SCARG(uap, writefds), SCARG(uap, exceptfds), tsp, ssp);
   1084 
   1085 	if (error == 0 && tsp != NULL) {
   1086 		if (retval != 0) {
   1087 			/*
   1088 			 * Compute how much time was left of the timeout,
   1089 			 * by subtracting the current time and the time
   1090 			 * before we started the call, and subtracting
   1091 			 * that result from the user-supplied value.
   1092 			 */
   1093 			nanotime(&ts1);
   1094 			timespecsub(&ts1, &ts0, &ts1);
   1095 			timespecsub(&uts, &ts1, &uts);
   1096 			if (uts.tv_sec < 0)
   1097 				timespecclear(&uts);
   1098 		} else {
   1099 			timespecclear(&uts);
   1100 		}
   1101 
   1102 		native_to_linux_timespec(&lts, &uts);
   1103 		error = copyout(&lts, SCARG(uap, timeout), sizeof(lts));
   1104 	}
   1105 
   1106 	return (error);
   1107 }
   1108 
   1109 int
   1110 linux_sys_ppoll(struct lwp *l,
   1111 	const struct linux_sys_ppoll_args *uap, register_t *retval)
   1112 {
   1113 	/* {
   1114 		syscallarg(struct pollfd *) fds;
   1115 		syscallarg(u_int) nfds;
   1116 		syscallarg(struct linux_timespec *) timeout;
   1117 		syscallarg(linux_sigset_t *) sigset;
   1118 	} */
   1119 	struct linux_timespec lts0, *lts;
   1120 	struct timespec ts0, *ts = NULL;
   1121 	linux_sigset_t lsigmask0, *lsigmask;
   1122 	sigset_t sigmask0, *sigmask = NULL;
   1123 	int error;
   1124 
   1125 	lts = SCARG(uap, timeout);
   1126 	if (lts) {
   1127 		if ((error = copyin(lts, &lts0, sizeof(lts0))) != 0)
   1128 			return error;
   1129 		linux_to_native_timespec(&ts0, &lts0);
   1130 		ts = &ts0;
   1131 	}
   1132 
   1133 	lsigmask = SCARG(uap, sigset);
   1134 	if (lsigmask) {
   1135 		if ((error = copyin(lsigmask, &lsigmask0, sizeof(lsigmask0))))
   1136 			return error;
   1137 		linux_to_native_sigset(&sigmask0, &lsigmask0);
   1138 		sigmask = &sigmask0;
   1139 	}
   1140 
   1141 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds),
   1142 	    ts, sigmask);
   1143 }
   1144 
   1145 /*
   1146  * Set the 'personality' (emulation mode) for the current process. Only
   1147  * accept the Linux personality here (0). This call is needed because
   1148  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
   1149  * ELF binaries run in Linux mode, not SVR4 mode.
   1150  */
   1151 int
   1152 linux_sys_personality(struct lwp *l, const struct linux_sys_personality_args *uap, register_t *retval)
   1153 {
   1154 	/* {
   1155 		syscallarg(unsigned long) per;
   1156 	} */
   1157 	struct linux_emuldata *led;
   1158 	int per;
   1159 
   1160 	per = SCARG(uap, per);
   1161 	led = l->l_emuldata;
   1162 	if (per == LINUX_PER_QUERY) {
   1163 		retval[0] = led->led_personality;
   1164 		return 0;
   1165 	}
   1166 
   1167 	switch (per & LINUX_PER_MASK) {
   1168 	case LINUX_PER_LINUX:
   1169 	case LINUX_PER_LINUX32:
   1170 		led->led_personality = per;
   1171 		break;
   1172 
   1173 	default:
   1174 		return EINVAL;
   1175 	}
   1176 
   1177 	retval[0] = per;
   1178 	return 0;
   1179 }
   1180 
   1181 /*
   1182  * We have nonexistent fsuid equal to uid.
   1183  * If modification is requested, refuse.
   1184  */
   1185 int
   1186 linux_sys_setfsuid(struct lwp *l, const struct linux_sys_setfsuid_args *uap, register_t *retval)
   1187 {
   1188 	 /* {
   1189 		 syscallarg(uid_t) uid;
   1190 	 } */
   1191 	 uid_t uid;
   1192 
   1193 	 uid = SCARG(uap, uid);
   1194 	 if (kauth_cred_getuid(l->l_cred) != uid)
   1195 		 return sys_nosys(l, uap, retval);
   1196 
   1197 	 *retval = uid;
   1198 	 return 0;
   1199 }
   1200 
   1201 int
   1202 linux_sys_setfsgid(struct lwp *l, const struct linux_sys_setfsgid_args *uap, register_t *retval)
   1203 {
   1204 	/* {
   1205 		syscallarg(gid_t) gid;
   1206 	} */
   1207 	gid_t gid;
   1208 
   1209 	gid = SCARG(uap, gid);
   1210 	if (kauth_cred_getgid(l->l_cred) != gid)
   1211 		return sys_nosys(l, uap, retval);
   1212 
   1213 	*retval = gid;
   1214 	return 0;
   1215 }
   1216 
   1217 int
   1218 linux_sys_setresuid(struct lwp *l, const struct linux_sys_setresuid_args *uap, register_t *retval)
   1219 {
   1220 	/* {
   1221 		syscallarg(uid_t) ruid;
   1222 		syscallarg(uid_t) euid;
   1223 		syscallarg(uid_t) suid;
   1224 	} */
   1225 
   1226 	/*
   1227 	 * Note: These checks are a little different than the NetBSD
   1228 	 * setreuid(2) call performs.  This precisely follows the
   1229 	 * behavior of the Linux kernel.
   1230 	 */
   1231 
   1232 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
   1233 			    SCARG(uap, suid),
   1234 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
   1235 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
   1236 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
   1237 }
   1238 
   1239 int
   1240 linux_sys_getresuid(struct lwp *l, const struct linux_sys_getresuid_args *uap, register_t *retval)
   1241 {
   1242 	/* {
   1243 		syscallarg(uid_t *) ruid;
   1244 		syscallarg(uid_t *) euid;
   1245 		syscallarg(uid_t *) suid;
   1246 	} */
   1247 	kauth_cred_t pc = l->l_cred;
   1248 	int error;
   1249 	uid_t uid;
   1250 
   1251 	/*
   1252 	 * Linux copies these values out to userspace like so:
   1253 	 *
   1254 	 *	1. Copy out ruid.
   1255 	 *	2. If that succeeds, copy out euid.
   1256 	 *	3. If both of those succeed, copy out suid.
   1257 	 */
   1258 	uid = kauth_cred_getuid(pc);
   1259 	if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
   1260 		return (error);
   1261 
   1262 	uid = kauth_cred_geteuid(pc);
   1263 	if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
   1264 		return (error);
   1265 
   1266 	uid = kauth_cred_getsvuid(pc);
   1267 
   1268 	return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
   1269 }
   1270 
   1271 int
   1272 linux_sys_ptrace(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
   1273 {
   1274 	/* {
   1275 		i386, m68k, powerpc: T=int
   1276 		alpha, amd64: T=long
   1277 		syscallarg(T) request;
   1278 		syscallarg(T) pid;
   1279 		syscallarg(T) addr;
   1280 		syscallarg(T) data;
   1281 	} */
   1282 	const int *ptr;
   1283 	int request;
   1284 	int error;
   1285 
   1286 	ptr = linux_ptrace_request_map;
   1287 	request = SCARG(uap, request);
   1288 	while (*ptr != -1)
   1289 		if (*ptr++ == request) {
   1290 			struct sys_ptrace_args pta;
   1291 
   1292 			memset(&pta, 0, sizeof(pta));
   1293 			SCARG(&pta, req) = *ptr;
   1294 			SCARG(&pta, pid) = SCARG(uap, pid);
   1295 			SCARG(&pta, addr) = (void *)SCARG(uap, addr);
   1296 			SCARG(&pta, data) = SCARG(uap, data);
   1297 
   1298 			/*
   1299 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1300 			 * to continue where the process left off previously.
   1301  			 * The same thing is achieved by addr == (void *) 1
   1302 			 * on NetBSD, so rewrite 'addr' appropriately.
   1303 			 */
   1304 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1305 				SCARG(&pta, addr) = (void *) 1;
   1306 
   1307 			error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
   1308 			if (error)
   1309 				return error;
   1310 			switch (request) {
   1311 			case LINUX_PTRACE_PEEKTEXT:
   1312 			case LINUX_PTRACE_PEEKDATA:
   1313 				error = copyout (retval,
   1314 				    (void *)SCARG(uap, data),
   1315 				    sizeof *retval);
   1316 				*retval = SCARG(uap, data);
   1317 				break;
   1318 			default:
   1319 				break;
   1320 			}
   1321 			return error;
   1322 		}
   1323 		else
   1324 			ptr++;
   1325 
   1326 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1327 }
   1328 
   1329 int
   1330 linux_sys_reboot(struct lwp *l, const struct linux_sys_reboot_args *uap, register_t *retval)
   1331 {
   1332 	/* {
   1333 		syscallarg(int) magic1;
   1334 		syscallarg(int) magic2;
   1335 		syscallarg(int) cmd;
   1336 		syscallarg(void *) arg;
   1337 	} */
   1338 	struct sys_reboot_args /* {
   1339 		syscallarg(int) opt;
   1340 		syscallarg(char *) bootstr;
   1341 	} */ sra;
   1342 	int error;
   1343 
   1344 	if ((error = kauth_authorize_system(l->l_cred,
   1345 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
   1346 		return(error);
   1347 
   1348 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1349 		return(EINVAL);
   1350 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1351 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1352 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1353 		return(EINVAL);
   1354 
   1355 	memset(&sra, 0, sizeof(sra));
   1356 
   1357 	switch ((unsigned long)SCARG(uap, cmd)) {
   1358 	case LINUX_REBOOT_CMD_RESTART:
   1359 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1360 		break;
   1361 	case LINUX_REBOOT_CMD_HALT:
   1362 		SCARG(&sra, opt) = RB_HALT;
   1363 		break;
   1364 	case LINUX_REBOOT_CMD_POWER_OFF:
   1365 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1366 		break;
   1367 	case LINUX_REBOOT_CMD_RESTART2:
   1368 		/* Reboot with an argument. */
   1369 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1370 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1371 		break;
   1372 	case LINUX_REBOOT_CMD_CAD_ON:
   1373 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1374 	case LINUX_REBOOT_CMD_CAD_OFF:
   1375 		return(0);
   1376 	default:
   1377 		return(EINVAL);
   1378 	}
   1379 
   1380 	return(sys_reboot(l, &sra, retval));
   1381 }
   1382 
   1383 /*
   1384  * Copy of compat_12_sys_swapon().
   1385  */
   1386 int
   1387 linux_sys_swapon(struct lwp *l, const struct linux_sys_swapon_args *uap, register_t *retval)
   1388 {
   1389 	/* {
   1390 		syscallarg(const char *) name;
   1391 	} */
   1392 	struct sys_swapctl_args ua;
   1393 
   1394 	memset(&ua, 0, sizeof(ua));
   1395 	SCARG(&ua, cmd) = SWAP_ON;
   1396 	SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
   1397 	SCARG(&ua, misc) = 0;	/* priority */
   1398 	return (sys_swapctl(l, &ua, retval));
   1399 }
   1400 
   1401 /*
   1402  * Stop swapping to the file or block device specified by path.
   1403  */
   1404 int
   1405 linux_sys_swapoff(struct lwp *l, const struct linux_sys_swapoff_args *uap, register_t *retval)
   1406 {
   1407 	/* {
   1408 		syscallarg(const char *) path;
   1409 	} */
   1410 	struct sys_swapctl_args ua;
   1411 
   1412 	memset(&ua, 0, sizeof(ua));
   1413 	SCARG(&ua, cmd) = SWAP_OFF;
   1414 	SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
   1415 	return (sys_swapctl(l, &ua, retval));
   1416 }
   1417 
   1418 /*
   1419  * Copy of compat_09_sys_setdomainname()
   1420  */
   1421 /* ARGSUSED */
   1422 int
   1423 linux_sys_setdomainname(struct lwp *l, const struct linux_sys_setdomainname_args *uap, register_t *retval)
   1424 {
   1425 	/* {
   1426 		syscallarg(char *) domainname;
   1427 		syscallarg(int) len;
   1428 	} */
   1429 	int name[2];
   1430 
   1431 	name[0] = CTL_KERN;
   1432 	name[1] = KERN_DOMAINNAME;
   1433 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
   1434 			    SCARG(uap, len), l));
   1435 }
   1436 
   1437 /*
   1438  * sysinfo()
   1439  */
   1440 /* ARGSUSED */
   1441 int
   1442 linux_sys_sysinfo(struct lwp *l, const struct linux_sys_sysinfo_args *uap, register_t *retval)
   1443 {
   1444 	/* {
   1445 		syscallarg(struct linux_sysinfo *) arg;
   1446 	} */
   1447 	struct linux_sysinfo si;
   1448 	struct loadavg *la;
   1449 	int64_t filepg;
   1450 
   1451 	memset(&si, 0, sizeof(si));
   1452 	si.uptime = time_uptime;
   1453 	la = &averunnable;
   1454 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1455 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1456 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1457 	si.totalram = ctob((u_long)physmem);
   1458 	/* uvm_availmem() may sync the counters. */
   1459 	si.freeram = (u_long)uvm_availmem(true) * uvmexp.pagesize;
   1460 	filepg = cpu_count_get(CPU_COUNT_FILECLEAN) +
   1461 	    cpu_count_get(CPU_COUNT_FILEDIRTY) +
   1462 	    cpu_count_get(CPU_COUNT_FILEUNKNOWN) -
   1463 	    cpu_count_get(CPU_COUNT_EXECPAGES);
   1464 	si.sharedram = 0;	/* XXX */
   1465 	si.bufferram = (u_long)(filepg * uvmexp.pagesize);
   1466 	si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
   1467 	si.freeswap =
   1468 	    (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1469 	si.procs = atomic_load_relaxed(&nprocs);
   1470 
   1471 	/* The following are only present in newer Linux kernels. */
   1472 	si.totalbig = 0;
   1473 	si.freebig = 0;
   1474 	si.mem_unit = 1;
   1475 
   1476 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1477 }
   1478 
   1479 int
   1480 linux_sys_getrlimit(struct lwp *l, const struct linux_sys_getrlimit_args *uap, register_t *retval)
   1481 {
   1482 	/* {
   1483 		syscallarg(int) which;
   1484 # ifdef LINUX_LARGEFILE64
   1485 		syscallarg(struct rlimit *) rlp;
   1486 # else
   1487 		syscallarg(struct orlimit *) rlp;
   1488 # endif
   1489 	} */
   1490 # ifdef LINUX_LARGEFILE64
   1491 	struct rlimit orl;
   1492 # else
   1493 	struct orlimit orl;
   1494 # endif
   1495 	int which;
   1496 
   1497 	which = linux_to_bsd_limit(SCARG(uap, which));
   1498 	if (which < 0)
   1499 		return -which;
   1500 
   1501 	memset(&orl, 0, sizeof(orl));
   1502 	bsd_to_linux_rlimit(&orl, &l->l_proc->p_rlimit[which]);
   1503 
   1504 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
   1505 }
   1506 
   1507 int
   1508 linux_sys_setrlimit(struct lwp *l, const struct linux_sys_setrlimit_args *uap, register_t *retval)
   1509 {
   1510 	/* {
   1511 		syscallarg(int) which;
   1512 # ifdef LINUX_LARGEFILE64
   1513 		syscallarg(struct rlimit *) rlp;
   1514 # else
   1515 		syscallarg(struct orlimit *) rlp;
   1516 # endif
   1517 	} */
   1518 	struct rlimit rl;
   1519 # ifdef LINUX_LARGEFILE64
   1520 	struct rlimit orl;
   1521 # else
   1522 	struct orlimit orl;
   1523 # endif
   1524 	int error;
   1525 	int which;
   1526 
   1527 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
   1528 		return error;
   1529 
   1530 	which = linux_to_bsd_limit(SCARG(uap, which));
   1531 	if (which < 0)
   1532 		return -which;
   1533 
   1534 	linux_to_bsd_rlimit(&rl, &orl);
   1535 	return dosetrlimit(l, l->l_proc, which, &rl);
   1536 }
   1537 
   1538 # if !defined(__aarch64__) && !defined(__mips__) && !defined(__amd64__)
   1539 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
   1540 int
   1541 linux_sys_ugetrlimit(struct lwp *l, const struct linux_sys_ugetrlimit_args *uap, register_t *retval)
   1542 {
   1543 	return linux_sys_getrlimit(l, (const void *)uap, retval);
   1544 }
   1545 # endif
   1546 
   1547 int
   1548 linux_sys_prlimit64(struct lwp *l, const struct linux_sys_prlimit64_args *uap, register_t *retval)
   1549 {
   1550 	/* {
   1551 		syscallarg(pid_t) pid;
   1552 		syscallarg(int) witch;
   1553 		syscallarg(struct rlimit *) new_rlp;
   1554 		syscallarg(struct rlimit *) old_rlp;
   1555 	}; */
   1556 	struct rlimit rl, nrl, orl;
   1557 	struct rlimit *p;
   1558 	int which;
   1559 	int error;
   1560 
   1561 	/* XXX: Cannot operate any process other than its own */
   1562 	if (SCARG(uap, pid) != 0)
   1563 		return EPERM;
   1564 
   1565 	which = linux_to_bsd_limit(SCARG(uap, which));
   1566 	if (which < 0)
   1567 		return -which;
   1568 
   1569 	p = SCARG(uap, old_rlp);
   1570 	if (p != NULL) {
   1571 		memset(&orl, 0, sizeof(orl));
   1572 		bsd_to_linux_rlimit64(&orl, &l->l_proc->p_rlimit[which]);
   1573 		if ((error = copyout(&orl, p, sizeof(orl))) != 0)
   1574 			return error;
   1575 	}
   1576 
   1577 	p = SCARG(uap, new_rlp);
   1578 	if (p != NULL) {
   1579 		if ((error = copyin(p, &nrl, sizeof(nrl))) != 0)
   1580 			return error;
   1581 
   1582 		linux_to_bsd_rlimit(&rl, &nrl);
   1583 		return dosetrlimit(l, l->l_proc, which, &rl);
   1584 	}
   1585 
   1586 	return 0;
   1587 }
   1588 
   1589 /*
   1590  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1591  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1592  * This is the way Linux does it and glibc depends on this behaviour.
   1593  */
   1594 int
   1595 linux_sys_nosys(struct lwp *l, const void *v, register_t *retval)
   1596 {
   1597 	return (ENOSYS);
   1598 }
   1599 
   1600 int
   1601 linux_sys_getpriority(struct lwp *l, const struct linux_sys_getpriority_args *uap, register_t *retval)
   1602 {
   1603         /* {
   1604                 syscallarg(int) which;
   1605                 syscallarg(int) who;
   1606         } */
   1607         struct sys_getpriority_args bsa;
   1608         int error;
   1609 
   1610 	memset(&bsa, 0, sizeof(bsa));
   1611         SCARG(&bsa, which) = SCARG(uap, which);
   1612         SCARG(&bsa, who) = SCARG(uap, who);
   1613 
   1614         if ((error = sys_getpriority(l, &bsa, retval)))
   1615                 return error;
   1616 
   1617         *retval = NZERO - *retval;
   1618 
   1619         return 0;
   1620 }
   1621 
   1622 int
   1623 linux_do_sys_utimensat(struct lwp *l, int fd, const char *path, struct timespec *tsp, int flags, register_t *retval)
   1624 {
   1625 	int follow, error;
   1626 
   1627 	follow = (flags & LINUX_AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
   1628 
   1629 	if (path == NULL && fd != AT_FDCWD) {
   1630 		file_t *fp;
   1631 
   1632 		/* fd_getvnode() will use the descriptor for us */
   1633 		if ((error = fd_getvnode(fd, &fp)) != 0)
   1634 			return error;
   1635 		error = do_sys_utimensat(l, AT_FDCWD, fp->f_data, NULL, 0,
   1636 		    tsp, UIO_SYSSPACE);
   1637 		fd_putfile(fd);
   1638 		return error;
   1639 	}
   1640 
   1641 	return do_sys_utimensat(l, fd, NULL, path, follow, tsp, UIO_SYSSPACE);
   1642 }
   1643 
   1644 int
   1645 linux_sys_utimensat(struct lwp *l, const struct linux_sys_utimensat_args *uap,
   1646 	register_t *retval)
   1647 {
   1648 	/* {
   1649 		syscallarg(int) fd;
   1650 		syscallarg(const char *) path;
   1651 		syscallarg(const struct linux_timespec *) times;
   1652 		syscallarg(int) flag;
   1653 	} */
   1654 	int error;
   1655 	struct linux_timespec lts[2];
   1656 	struct timespec *tsp = NULL, ts[2];
   1657 
   1658 	if (SCARG(uap, times)) {
   1659 		error = copyin(SCARG(uap, times), &lts, sizeof(lts));
   1660 		if (error != 0)
   1661 			return error;
   1662 		linux_to_native_timespec(&ts[0], &lts[0]);
   1663 		linux_to_native_timespec(&ts[1], &lts[1]);
   1664 		tsp = ts;
   1665 	}
   1666 
   1667 	return linux_do_sys_utimensat(l, SCARG(uap, fd), SCARG(uap, path),
   1668 	    tsp, SCARG(uap, flag), retval);
   1669 }
   1670 
   1671 int
   1672 linux_sys_futex(struct lwp *l, const struct linux_sys_futex_args *uap,
   1673 	register_t *retval)
   1674 {
   1675 	/* {
   1676 		syscallarg(int *) uaddr;
   1677 		syscallarg(int) op;
   1678 		syscallarg(int) val;
   1679 		syscallarg(const struct linux_timespec *) timeout;
   1680 		syscallarg(int *) uaddr2;
   1681 		syscallarg(int) val3;
   1682 	} */
   1683 	struct linux_timespec lts;
   1684 	struct timespec ts, *tsp = NULL;
   1685 	int val2 = 0;
   1686 	int error;
   1687 
   1688 	/*
   1689 	 * Linux overlays the "timeout" field and the "val2" field.
   1690 	 * "timeout" is only valid for FUTEX_WAIT and FUTEX_WAIT_BITSET
   1691 	 * on Linux.
   1692 	 */
   1693 	const int op = (SCARG(uap, op) & FUTEX_CMD_MASK);
   1694 	if ((op == FUTEX_WAIT || op == FUTEX_WAIT_BITSET) &&
   1695 	    SCARG(uap, timeout) != NULL) {
   1696 		if ((error = copyin(SCARG(uap, timeout),
   1697 		    &lts, sizeof(lts))) != 0) {
   1698 			return error;
   1699 		}
   1700 		linux_to_native_timespec(&ts, &lts);
   1701 		tsp = &ts;
   1702 	} else {
   1703 		val2 = (int)(uintptr_t)SCARG(uap, timeout);
   1704 	}
   1705 
   1706 	return linux_do_futex(SCARG(uap, uaddr), SCARG(uap, op),
   1707 	    SCARG(uap, val), tsp, SCARG(uap, uaddr2), val2,
   1708 	    SCARG(uap, val3), retval);
   1709 }
   1710 
   1711 int
   1712 linux_do_futex(int *uaddr, int op, int val, struct timespec *timeout,
   1713     int *uaddr2, int val2, int val3, register_t *retval)
   1714 {
   1715 	/*
   1716 	 * Always clear FUTEX_PRIVATE_FLAG for Linux processes.
   1717 	 * NetBSD-native futexes exist in different namespace
   1718 	 * depending on FUTEX_PRIVATE_FLAG.  This appears not
   1719 	 * to be the case in Linux, and some futex users will
   1720 	 * mix private and non-private ops on the same futex
   1721 	 * object.
   1722 	 */
   1723 	return do_futex(uaddr, op & ~FUTEX_PRIVATE_FLAG,
   1724 			val, timeout, uaddr2, val2, val3, retval);
   1725 }
   1726 
   1727 #define	LINUX_EFD_SEMAPHORE	0x0001
   1728 #define	LINUX_EFD_CLOEXEC	LINUX_O_CLOEXEC
   1729 #define	LINUX_EFD_NONBLOCK	LINUX_O_NONBLOCK
   1730 
   1731 static int
   1732 linux_do_eventfd2(struct lwp *l, unsigned int initval, int flags,
   1733     register_t *retval)
   1734 {
   1735 	int nflags = 0;
   1736 
   1737 	if (flags & ~(LINUX_EFD_SEMAPHORE | LINUX_EFD_CLOEXEC |
   1738 		      LINUX_EFD_NONBLOCK)) {
   1739 		return EINVAL;
   1740 	}
   1741 	if (flags & LINUX_EFD_SEMAPHORE) {
   1742 		nflags |= EFD_SEMAPHORE;
   1743 	}
   1744 	if (flags & LINUX_EFD_CLOEXEC) {
   1745 		nflags |= EFD_CLOEXEC;
   1746 	}
   1747 	if (flags & LINUX_EFD_NONBLOCK) {
   1748 		nflags |= EFD_NONBLOCK;
   1749 	}
   1750 
   1751 	return do_eventfd(l, initval, nflags, retval);
   1752 }
   1753 
   1754 int
   1755 linux_sys_eventfd(struct lwp *l, const struct linux_sys_eventfd_args *uap,
   1756     register_t *retval)
   1757 {
   1758 	/* {
   1759 		syscallarg(unsigned int) initval;
   1760 	} */
   1761 
   1762 	return linux_do_eventfd2(l, SCARG(uap, initval), 0, retval);
   1763 }
   1764 
   1765 int
   1766 linux_sys_eventfd2(struct lwp *l, const struct linux_sys_eventfd2_args *uap,
   1767     register_t *retval)
   1768 {
   1769 	/* {
   1770 		syscallarg(unsigned int) initval;
   1771 		syscallarg(int) flags;
   1772 	} */
   1773 
   1774 	return linux_do_eventfd2(l, SCARG(uap, initval), SCARG(uap, flags),
   1775 				 retval);
   1776 }
   1777 
   1778 #ifndef __aarch64__
   1779 /*
   1780  * epoll_create(2).  Check size and call sys_epoll_create1.
   1781  */
   1782 int
   1783 linux_sys_epoll_create(struct lwp *l,
   1784     const struct linux_sys_epoll_create_args *uap, register_t *retval)
   1785 {
   1786 	/* {
   1787 		syscallarg(int) size;
   1788 	} */
   1789 	struct sys_epoll_create1_args ca;
   1790 
   1791 	/*
   1792 	 * SCARG(uap, size) is unused.  Linux just tests it and then
   1793 	 * forgets it as well.
   1794 	 */
   1795 	if (SCARG(uap, size) <= 0)
   1796 		return EINVAL;
   1797 
   1798 	memset(&ca, 0, sizeof(ca));
   1799 	SCARG(&ca, flags) = 0;
   1800 	return sys_epoll_create1(l, &ca, retval);
   1801 }
   1802 #endif /* !__aarch64__ */
   1803 
   1804 /*
   1805  * epoll_create1(2).  Translate the flags and call sys_epoll_create1.
   1806  */
   1807 int
   1808 linux_sys_epoll_create1(struct lwp *l,
   1809     const struct linux_sys_epoll_create1_args *uap, register_t *retval)
   1810 {
   1811 	/* {
   1812 		syscallarg(int) flags;
   1813 	} */
   1814 	struct sys_epoll_create1_args ca;
   1815 
   1816         if ((SCARG(uap, flags) & ~(LINUX_O_CLOEXEC)) != 0)
   1817 		return EINVAL;
   1818 
   1819 	memset(&ca, 0, sizeof(ca));
   1820 	SCARG(&ca, flags) = 0;
   1821 	if ((SCARG(uap, flags) & LINUX_O_CLOEXEC) != 0)
   1822 		SCARG(&ca, flags) |= EPOLL_CLOEXEC;
   1823 
   1824 	return sys_epoll_create1(l, &ca, retval);
   1825 }
   1826 
   1827 /*
   1828  * epoll_ctl(2).  Copyin event and translate it if necessary and then
   1829  * call epoll_ctl_common().
   1830  */
   1831 int
   1832 linux_sys_epoll_ctl(struct lwp *l, const struct linux_sys_epoll_ctl_args *uap,
   1833     register_t *retval)
   1834 {
   1835 	/* {
   1836 		syscallarg(int) epfd;
   1837 		syscallarg(int) op;
   1838 		syscallarg(int) fd;
   1839 		syscallarg(struct linux_epoll_event *) event;
   1840 	} */
   1841 	struct linux_epoll_event lee;
   1842 	struct epoll_event ee;
   1843 	struct epoll_event *eep;
   1844 	int error;
   1845 
   1846 	if (SCARG(uap, op) != EPOLL_CTL_DEL) {
   1847 		error = copyin(SCARG(uap, event), &lee, sizeof(lee));
   1848 		if (error != 0)
   1849 			return error;
   1850 
   1851 		/*
   1852 		 * On some architectures, struct linux_epoll_event and
   1853 		 * struct epoll_event are packed differently... but otherwise
   1854 		 * the contents are the same.
   1855 		 */
   1856 		ee.events = lee.events;
   1857 		ee.data = lee.data;
   1858 
   1859 		eep = &ee;
   1860 	} else
   1861 		eep = NULL;
   1862 
   1863 	return epoll_ctl_common(l, retval, SCARG(uap, epfd), SCARG(uap, op),
   1864 	    SCARG(uap, fd), eep);
   1865 }
   1866 
   1867 #ifndef __aarch64__
   1868 /*
   1869  * epoll_wait(2).  Call sys_epoll_pwait().
   1870  */
   1871 int
   1872 linux_sys_epoll_wait(struct lwp *l,
   1873     const struct linux_sys_epoll_wait_args *uap, register_t *retval)
   1874 {
   1875 	/* {
   1876 		syscallarg(int) epfd;
   1877 		syscallarg(struct linux_epoll_event *) events;
   1878 		syscallarg(int) maxevents;
   1879 		syscallarg(int) timeout;
   1880 	} */
   1881 	struct linux_sys_epoll_pwait_args ea;
   1882 
   1883 	memset(&ea, 0, sizeof(ea));
   1884 	SCARG(&ea, epfd) = SCARG(uap, epfd);
   1885 	SCARG(&ea, events) = SCARG(uap, events);
   1886 	SCARG(&ea, maxevents) = SCARG(uap, maxevents);
   1887 	SCARG(&ea, timeout) = SCARG(uap, timeout);
   1888 	SCARG(&ea, sigmask) = NULL;
   1889 
   1890 	return linux_sys_epoll_pwait(l, &ea, retval);
   1891 }
   1892 #endif /* !__aarch64__ */
   1893 
   1894 /*
   1895  * Main body of epoll_pwait2(2).  Translate timeout and sigmask and
   1896  * call epoll_wait_common.
   1897  */
   1898 static int
   1899 linux_epoll_pwait2_common(struct lwp *l, register_t *retval, int epfd,
   1900     struct linux_epoll_event *events, int maxevents,
   1901     struct linux_timespec *timeout, const linux_sigset_t *sigmask)
   1902 {
   1903 	struct timespec ts, *tsp;
   1904 	linux_sigset_t lss;
   1905 	sigset_t ss, *ssp;
   1906 	struct epoll_event *eep;
   1907 	struct linux_epoll_event *leep;
   1908 	int i, error;
   1909 
   1910 	if (maxevents <= 0 || maxevents > EPOLL_MAX_EVENTS)
   1911 		return EINVAL;
   1912 
   1913 	if (timeout != NULL) {
   1914 		linux_to_native_timespec(&ts, timeout);
   1915 		tsp = &ts;
   1916 	} else
   1917 		tsp = NULL;
   1918 
   1919 	if (sigmask != NULL) {
   1920 		error = copyin(sigmask, &lss, sizeof(lss));
   1921 		if (error != 0)
   1922 			return error;
   1923 
   1924 		linux_to_native_sigset(&ss, &lss);
   1925 		ssp = &ss;
   1926 	} else
   1927 		ssp = NULL;
   1928 
   1929 	eep = kmem_alloc(maxevents * sizeof(*eep), KM_SLEEP);
   1930 
   1931 	error = epoll_wait_common(l, retval, epfd, eep, maxevents, tsp,
   1932 	    ssp);
   1933 	if (error == 0 && *retval > 0) {
   1934 		leep = kmem_alloc((*retval) * sizeof(*leep), KM_SLEEP);
   1935 
   1936 		/* Translate the events (because of packing). */
   1937 		for (i = 0; i < *retval; i++) {
   1938 			leep[i].events = eep[i].events;
   1939 			leep[i].data = eep[i].data;
   1940 		}
   1941 
   1942 		error = copyout(leep, events, (*retval) * sizeof(*leep));
   1943 		kmem_free(leep, (*retval) * sizeof(*leep));
   1944 	}
   1945 
   1946 	kmem_free(eep, maxevents * sizeof(*eep));
   1947 	return error;
   1948 }
   1949 
   1950 /*
   1951  * epoll_pwait(2).  Translate timeout and call sys_epoll_pwait2.
   1952  */
   1953 int
   1954 linux_sys_epoll_pwait(struct lwp *l,
   1955     const struct linux_sys_epoll_pwait_args *uap, register_t *retval)
   1956 {
   1957 	/* {
   1958 		syscallarg(int) epfd;
   1959 		syscallarg(struct linux_epoll_event *) events;
   1960 		syscallarg(int) maxevents;
   1961 		syscallarg(int) timeout;
   1962 		syscallarg(linux_sigset_t *) sigmask;
   1963 	} */
   1964         struct linux_timespec lts, *ltsp;
   1965 	const int timeout = SCARG(uap, timeout);
   1966 
   1967 	if (timeout >= 0) {
   1968 		/* Convert from milliseconds to timespec. */
   1969 		lts.tv_sec = timeout / 1000;
   1970 		lts.tv_nsec = (timeout % 1000) * 1000000;
   1971 
   1972 	        ltsp = &lts;
   1973 	} else
   1974 		ltsp = NULL;
   1975 
   1976 	return linux_epoll_pwait2_common(l, retval, SCARG(uap, epfd),
   1977 	    SCARG(uap, events), SCARG(uap, maxevents), ltsp,
   1978 	    SCARG(uap, sigmask));
   1979 }
   1980 
   1981 
   1982 /*
   1983  * epoll_pwait2(2).  Copyin timeout and call linux_epoll_pwait2_common().
   1984  */
   1985 int
   1986 linux_sys_epoll_pwait2(struct lwp *l,
   1987     const struct linux_sys_epoll_pwait2_args *uap, register_t *retval)
   1988 {
   1989 	/* {
   1990 		syscallarg(int) epfd;
   1991 		syscallarg(struct linux_epoll_event *) events;
   1992 		syscallarg(int) maxevents;
   1993 	        syscallarg(struct linux_timespec *) timeout;
   1994 		syscallarg(linux_sigset_t *) sigmask;
   1995 	} */
   1996 	struct linux_timespec lts, *ltsp;
   1997 	int error;
   1998 
   1999 	if (SCARG(uap, timeout) != NULL) {
   2000 		error = copyin(SCARG(uap, timeout), &lts, sizeof(lts));
   2001 		if (error != 0)
   2002 			return error;
   2003 
   2004 		ltsp = &lts;
   2005 	} else
   2006 		ltsp = NULL;
   2007 
   2008 	return linux_epoll_pwait2_common(l, retval, SCARG(uap, epfd),
   2009 	    SCARG(uap, events), SCARG(uap, maxevents), ltsp,
   2010 	    SCARG(uap, sigmask));
   2011 }
   2012 
   2013 #define	LINUX_MFD_CLOEXEC	0x0001U
   2014 #define	LINUX_MFD_ALLOW_SEALING	0x0002U
   2015 #define	LINUX_MFD_HUGETLB	0x0004U
   2016 #define	LINUX_MFD_NOEXEC_SEAL	0x0008U
   2017 #define	LINUX_MFD_EXEC		0x0010U
   2018 #define	LINUX_MFD_HUGE_FLAGS	(0x3f << 26)
   2019 
   2020 #define	LINUX_MFD_ALL_FLAGS	(LINUX_MFD_CLOEXEC|LINUX_MFD_ALLOW_SEALING \
   2021 				|LINUX_MFD_HUGETLB|LINUX_MFD_NOEXEC_SEAL \
   2022 				|LINUX_MFD_EXEC|LINUX_MFD_HUGE_FLAGS)
   2023 #define	LINUX_MFD_KNOWN_FLAGS	(LINUX_MFD_CLOEXEC|LINUX_MFD_ALLOW_SEALING)
   2024 
   2025 #define LINUX_MFD_NAME_MAX	249
   2026 
   2027 /*
   2028  * memfd_create(2).  Do some error checking and then call NetBSD's
   2029  * version.
   2030  */
   2031 int
   2032 linux_sys_memfd_create(struct lwp *l,
   2033     const struct linux_sys_memfd_create_args *uap, register_t *retval)
   2034 {
   2035 	/* {
   2036 		syscallarg(const char *) name;
   2037 		syscallarg(unsigned int) flags;
   2038 	} */
   2039 	int error;
   2040 	char *pbuf;
   2041 	struct sys_memfd_create_args muap;
   2042 	const unsigned int lflags = SCARG(uap, flags);
   2043 
   2044 	KASSERT(LINUX_MFD_NAME_MAX < NAME_MAX); /* sanity check */
   2045 
   2046 	if (lflags & ~LINUX_MFD_ALL_FLAGS)
   2047 		return EINVAL;
   2048 	if ((lflags & LINUX_MFD_HUGE_FLAGS) != 0 &&
   2049 	    (lflags & LINUX_MFD_HUGETLB) == 0)
   2050 		return EINVAL;
   2051 	if ((lflags & LINUX_MFD_HUGETLB) && (lflags & LINUX_MFD_ALLOW_SEALING))
   2052 		return EINVAL;
   2053 
   2054 	/* Linux has a stricter limit for name size */
   2055 	pbuf = PNBUF_GET();
   2056 	error = copyinstr(SCARG(uap, name), pbuf, LINUX_MFD_NAME_MAX+1, NULL);
   2057 	PNBUF_PUT(pbuf);
   2058 	pbuf = NULL;
   2059 	if (error != 0) {
   2060 		if (error == ENAMETOOLONG)
   2061 			error = EINVAL;
   2062 		return error;
   2063 	}
   2064 
   2065 	if (lflags & ~LINUX_MFD_KNOWN_FLAGS) {
   2066 		DPRINTF("%s: ignored flags %#x\n", __func__,
   2067 		    lflags & ~LINUX_MFD_KNOWN_FLAGS);
   2068 	}
   2069 
   2070 	memset(&muap, 0, sizeof(muap));
   2071 	SCARG(&muap, name) = SCARG(uap, name);
   2072 	SCARG(&muap, flags) = lflags & LINUX_MFD_KNOWN_FLAGS;
   2073 
   2074 	return sys_memfd_create(l, &muap, retval);
   2075 }
   2076 
   2077 #define	LINUX_CLOSE_RANGE_UNSHARE	0x02U
   2078 #define	LINUX_CLOSE_RANGE_CLOEXEC	0x04U
   2079 
   2080 /*
   2081  * close_range(2).
   2082  */
   2083 int
   2084 linux_sys_close_range(struct lwp *l,
   2085     const struct linux_sys_close_range_args *uap, register_t *retval)
   2086 {
   2087 	/* {
   2088 		syscallarg(unsigned int) first;
   2089 		syscallarg(unsigned int) last;
   2090 		syscallarg(unsigned int) flags;
   2091 	} */
   2092 	unsigned int fd, last;
   2093 	file_t *fp;
   2094 	filedesc_t *fdp;
   2095 	const unsigned int flags = SCARG(uap, flags);
   2096 
   2097 	if (flags & ~(LINUX_CLOSE_RANGE_CLOEXEC|LINUX_CLOSE_RANGE_UNSHARE))
   2098 		return EINVAL;
   2099 	if (SCARG(uap, first) > SCARG(uap, last))
   2100 		return EINVAL;
   2101 
   2102 	if (flags & LINUX_CLOSE_RANGE_UNSHARE) {
   2103 		fdp = fd_copy();
   2104 		fd_free();
   2105 	        l->l_proc->p_fd = fdp;
   2106 	        l->l_fd = fdp;
   2107 	}
   2108 
   2109 	last = MIN(SCARG(uap, last), l->l_proc->p_fd->fd_lastfile);
   2110 	for (fd = SCARG(uap, first); fd <= last; fd++) {
   2111 		fp = fd_getfile(fd);
   2112 		if (fp == NULL)
   2113 			continue;
   2114 
   2115 		if (flags & LINUX_CLOSE_RANGE_CLOEXEC) {
   2116 			fd_set_exclose(l, fd, true);
   2117 			fd_putfile(fd);
   2118 		} else
   2119 			fd_close(fd);
   2120 	}
   2121 
   2122 	return 0;
   2123 }
   2124 
   2125 /*
   2126  * readahead(2).  Call posix_fadvise with POSIX_FADV_WILLNEED with some extra
   2127  * error checking.
   2128  */
   2129 int
   2130 linux_sys_readahead(struct lwp *l, const struct linux_sys_readahead_args *uap,
   2131     register_t *retval)
   2132 {
   2133 	/* {
   2134 		syscallarg(int) fd;
   2135 		syscallarg(off_t) offset;
   2136 		syscallarg(size_t) count;
   2137 	} */
   2138 	file_t *fp;
   2139 	int error = 0;
   2140 	const int fd = SCARG(uap, fd);
   2141 
   2142 	fp = fd_getfile(fd);
   2143 	if (fp == NULL)
   2144 		return EBADF;
   2145 	if ((fp->f_flag & FREAD) == 0)
   2146 		error = EBADF;
   2147 	else if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG)
   2148 		error = EINVAL;
   2149 	fd_putfile(fd);
   2150 	if (error != 0)
   2151 		return error;
   2152 
   2153 	return do_posix_fadvise(fd, SCARG(uap, offset), SCARG(uap, count),
   2154 	    POSIX_FADV_WILLNEED);
   2155 }
   2156 
   2157 int
   2158 linux_sys_getcpu(lwp_t *l, const struct linux_sys_getcpu_args *uap,
   2159     register_t *retval)
   2160 {
   2161 	/* {
   2162 		syscallarg(unsigned int *) cpu;
   2163 		syscallarg(unsigned int *) node;
   2164 		syscallarg(struct linux_getcpu_cache *) tcache;
   2165 	}*/
   2166 	int error;
   2167 
   2168 	if (SCARG(uap, cpu)) {
   2169 		u_int cpu_id = l->l_cpu->ci_data.cpu_index;
   2170 		error = copyout(&cpu_id, SCARG(uap, cpu), sizeof(cpu_id));
   2171 		if (error)
   2172 			return error;
   2173 
   2174 	}
   2175 
   2176 	// TO-DO: Test on a NUMA machine if the node_id returned is correct
   2177 	if (SCARG(uap, node)) {
   2178 		u_int node_id = l->l_cpu->ci_data.cpu_numa_id;
   2179 		error = copyout(&node_id, SCARG(uap, node), sizeof(node_id));
   2180 		if (error)
   2181 			return error;
   2182 	}
   2183 
   2184 	return 0;
   2185 }
   2186