xref: /netbsd-src/sys/compat/linux/common/linux_misc.c (revision a04395531661c5e8d314125d5ae77d4cbedd5d73)
1 /*	$NetBSD: linux_misc.c,v 1.252 2021/09/07 11:43:04 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.252 2021/09/07 11:43:04 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/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 		memset(&ltms, 0, sizeof(ltms));
633 
634 		mutex_enter(p->p_lock);
635 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
636 		ltms.ltms_utime = CONVTCK(ru.ru_utime);
637 		ltms.ltms_stime = CONVTCK(ru.ru_stime);
638 		ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
639 		ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
640 		mutex_exit(p->p_lock);
641 
642 		if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
643 			return error;
644 	}
645 
646 	getmicrouptime(&t);
647 
648 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
649 	return 0;
650 }
651 
652 #undef CONVTCK
653 
654 /*
655  * Linux 'readdir' call. This code is mostly taken from the
656  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
657  * an attempt has been made to keep it a little cleaner (failing
658  * miserably, because of the cruft needed if count 1 is passed).
659  *
660  * The d_off field should contain the offset of the next valid entry,
661  * but in Linux it has the offset of the entry itself. We emulate
662  * that bug here.
663  *
664  * Read in BSD-style entries, convert them, and copy them out.
665  *
666  * Note that this doesn't handle union-mounted filesystems.
667  */
668 int
669 linux_sys_getdents(struct lwp *l, const struct linux_sys_getdents_args *uap, register_t *retval)
670 {
671 	/* {
672 		syscallarg(int) fd;
673 		syscallarg(struct linux_dirent *) dent;
674 		syscallarg(unsigned int) count;
675 	} */
676 	struct dirent *bdp;
677 	struct vnode *vp;
678 	char *inp, *tbuf;		/* BSD-format */
679 	int len, reclen;		/* BSD-format */
680 	char *outp;			/* Linux-format */
681 	int resid, linux_reclen = 0;	/* Linux-format */
682 	struct file *fp;
683 	struct uio auio;
684 	struct iovec aiov;
685 	struct linux_dirent idb;
686 	off_t off;		/* true file offset */
687 	int buflen, error, eofflag, nbytes, oldcall;
688 	struct vattr va;
689 	off_t *cookiebuf = NULL, *cookie;
690 	int ncookies;
691 
692 	/* fd_getvnode() will use the descriptor for us */
693 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
694 		return (error);
695 
696 	if ((fp->f_flag & FREAD) == 0) {
697 		error = EBADF;
698 		goto out1;
699 	}
700 
701 	vp = (struct vnode *)fp->f_data;
702 	if (vp->v_type != VDIR) {
703 		error = ENOTDIR;
704 		goto out1;
705 	}
706 
707 	vn_lock(vp, LK_SHARED | LK_RETRY);
708 	error = VOP_GETATTR(vp, &va, l->l_cred);
709 	VOP_UNLOCK(vp);
710 	if (error)
711 		goto out1;
712 
713 	nbytes = SCARG(uap, count);
714 	if (nbytes == 1) {	/* emulating old, broken behaviour */
715 		nbytes = sizeof (idb);
716 		buflen = uimax(va.va_blocksize, nbytes);
717 		oldcall = 1;
718 	} else {
719 		buflen = uimin(MAXBSIZE, nbytes);
720 		if (buflen < va.va_blocksize)
721 			buflen = va.va_blocksize;
722 		oldcall = 0;
723 	}
724 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
725 
726 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
727 	off = fp->f_offset;
728 again:
729 	aiov.iov_base = tbuf;
730 	aiov.iov_len = buflen;
731 	auio.uio_iov = &aiov;
732 	auio.uio_iovcnt = 1;
733 	auio.uio_rw = UIO_READ;
734 	auio.uio_resid = buflen;
735 	auio.uio_offset = off;
736 	UIO_SETUP_SYSSPACE(&auio);
737 	/*
738          * First we read into the malloc'ed buffer, then
739          * we massage it into user space, one record at a time.
740          */
741 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
742 	    &ncookies);
743 	if (error)
744 		goto out;
745 
746 	inp = tbuf;
747 	outp = (void *)SCARG(uap, dent);
748 	resid = nbytes;
749 	if ((len = buflen - auio.uio_resid) == 0)
750 		goto eof;
751 
752 	for (cookie = cookiebuf; len > 0; len -= reclen) {
753 		bdp = (struct dirent *)inp;
754 		reclen = bdp->d_reclen;
755 		if (reclen & 3) {
756 			error = EIO;
757 			goto out;
758 		}
759 		if (bdp->d_fileno == 0) {
760 			inp += reclen;	/* it is a hole; squish it out */
761 			if (cookie)
762 				off = *cookie++;
763 			else
764 				off += reclen;
765 			continue;
766 		}
767 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
768 		if (reclen > len || resid < linux_reclen) {
769 			/* entry too big for buffer, so just stop */
770 			outp++;
771 			break;
772 		}
773 		/*
774 		 * Massage in place to make a Linux-shaped dirent (otherwise
775 		 * we have to worry about touching user memory outside of
776 		 * the copyout() call).
777 		 */
778 		memset(&idb, 0, sizeof(idb));
779 		idb.d_ino = bdp->d_fileno;
780 		/*
781 		 * The old readdir() call misuses the offset and reclen fields.
782 		 */
783 		if (oldcall) {
784 			idb.d_off = (linux_off_t)linux_reclen;
785 			idb.d_reclen = (u_short)bdp->d_namlen;
786 		} else {
787 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
788 				compat_offseterr(vp, "linux_getdents");
789 				error = EINVAL;
790 				goto out;
791 			}
792 			idb.d_off = (linux_off_t)off;
793 			idb.d_reclen = (u_short)linux_reclen;
794 			/* Linux puts d_type at the end of each record */
795 			*((char *)&idb + idb.d_reclen - 1) = bdp->d_type;
796 		}
797 		memcpy(idb.d_name, bdp->d_name,
798 		    MIN(sizeof(idb.d_name), bdp->d_namlen + 1));
799 		if ((error = copyout((void *)&idb, outp, linux_reclen)))
800 			goto out;
801 		/* advance past this real entry */
802 		inp += reclen;
803 		if (cookie)
804 			off = *cookie++; /* each entry points to itself */
805 		else
806 			off += reclen;
807 		/* advance output past Linux-shaped entry */
808 		outp += linux_reclen;
809 		resid -= linux_reclen;
810 		if (oldcall)
811 			break;
812 	}
813 
814 	/* if we squished out the whole block, try again */
815 	if (outp == (void *)SCARG(uap, dent)) {
816 		if (cookiebuf)
817 			free(cookiebuf, M_TEMP);
818 		cookiebuf = NULL;
819 		goto again;
820 	}
821 	fp->f_offset = off;	/* update the vnode offset */
822 
823 	if (oldcall)
824 		nbytes = resid + linux_reclen;
825 
826 eof:
827 	*retval = nbytes - resid;
828 out:
829 	VOP_UNLOCK(vp);
830 	if (cookiebuf)
831 		free(cookiebuf, M_TEMP);
832 	free(tbuf, M_TEMP);
833 out1:
834 	fd_putfile(SCARG(uap, fd));
835 	return error;
836 }
837 
838 /*
839  * Even when just using registers to pass arguments to syscalls you can
840  * have 5 of them on the i386. So this newer version of select() does
841  * this.
842  */
843 int
844 linux_sys_select(struct lwp *l, const struct linux_sys_select_args *uap, register_t *retval)
845 {
846 	/* {
847 		syscallarg(int) nfds;
848 		syscallarg(fd_set *) readfds;
849 		syscallarg(fd_set *) writefds;
850 		syscallarg(fd_set *) exceptfds;
851 		syscallarg(struct timeval50 *) timeout;
852 	} */
853 
854 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
855 	    SCARG(uap, writefds), SCARG(uap, exceptfds),
856 	    (struct linux_timeval *)SCARG(uap, timeout));
857 }
858 
859 /*
860  * Common code for the old and new versions of select(). A couple of
861  * things are important:
862  * 1) return the amount of time left in the 'timeout' parameter
863  * 2) select never returns ERESTART on Linux, always return EINTR
864  */
865 int
866 linux_select1(struct lwp *l, register_t *retval, int nfds, fd_set *readfds,
867     fd_set *writefds, fd_set *exceptfds, struct linux_timeval *timeout)
868 {
869 	struct timespec ts0, ts1, uts, *ts = NULL;
870 	struct linux_timeval ltv;
871 	int error;
872 
873 	/*
874 	 * Store current time for computation of the amount of
875 	 * time left.
876 	 */
877 	if (timeout) {
878 		if ((error = copyin(timeout, &ltv, sizeof(ltv))))
879 			return error;
880 		uts.tv_sec = ltv.tv_sec;
881 		uts.tv_nsec = (long)((unsigned long)ltv.tv_usec * 1000);
882 		if (itimespecfix(&uts)) {
883 			/*
884 			 * The timeval was invalid.  Convert it to something
885 			 * valid that will act as it does under Linux.
886 			 */
887 			uts.tv_sec += uts.tv_nsec / 1000000000;
888 			uts.tv_nsec %= 1000000000;
889 			if (uts.tv_nsec < 0) {
890 				uts.tv_sec -= 1;
891 				uts.tv_nsec += 1000000000;
892 			}
893 			if (uts.tv_sec < 0)
894 				timespecclear(&uts);
895 		}
896 		ts = &uts;
897 		nanotime(&ts0);
898 	}
899 
900 	error = selcommon(retval, nfds, readfds, writefds, exceptfds, ts, NULL);
901 
902 	if (error) {
903 		/*
904 		 * See fs/select.c in the Linux kernel.  Without this,
905 		 * Maelstrom doesn't work.
906 		 */
907 		if (error == ERESTART)
908 			error = EINTR;
909 		return error;
910 	}
911 
912 	if (timeout) {
913 		if (*retval) {
914 			/*
915 			 * Compute how much time was left of the timeout,
916 			 * by subtracting the current time and the time
917 			 * before we started the call, and subtracting
918 			 * that result from the user-supplied value.
919 			 */
920 			nanotime(&ts1);
921 			timespecsub(&ts1, &ts0, &ts1);
922 			timespecsub(&uts, &ts1, &uts);
923 			if (uts.tv_sec < 0)
924 				timespecclear(&uts);
925 		} else
926 			timespecclear(&uts);
927 		ltv.tv_sec = uts.tv_sec;
928 		ltv.tv_usec = uts.tv_nsec / 1000;
929 		if ((error = copyout(&ltv, timeout, sizeof(ltv))))
930 			return error;
931 	}
932 
933 	return 0;
934 }
935 
936 /*
937  * Derived from FreeBSD's sys/compat/linux/linux_misc.c:linux_pselect6()
938  * which was contributed by Dmitry Chagin
939  * https://svnweb.freebsd.org/base?view=revision&revision=283403
940  */
941 int
942 linux_sys_pselect6(struct lwp *l,
943 	const struct linux_sys_pselect6_args *uap, register_t *retval)
944 {
945 	/* {
946 		syscallarg(int) nfds;
947 		syscallarg(fd_set *) readfds;
948 		syscallarg(fd_set *) writefds;
949 		syscallarg(fd_set *) exceptfds;
950 		syscallarg(struct timespec *) timeout;
951 		syscallarg(linux_sized_sigset_t *) ss;
952 	} */
953 	struct timespec uts, ts0, ts1, *tsp;
954 	linux_sized_sigset_t lsss;
955 	struct linux_timespec lts;
956 	linux_sigset_t lss;
957 	sigset_t *ssp;
958 	sigset_t ss;
959 	int error;
960 
961 	ssp = NULL;
962 	if (SCARG(uap, ss) != NULL) {
963 		if ((error = copyin(SCARG(uap, ss), &lsss, sizeof(lsss))) != 0)
964 			return (error);
965 		if (lsss.ss_len != sizeof(lss))
966 			return (EINVAL);
967 		if (lsss.ss != NULL) {
968 			if ((error = copyin(lsss.ss, &lss, sizeof(lss))) != 0)
969 				return (error);
970 			linux_to_native_sigset(&ss, &lss);
971 			ssp = &ss;
972 		}
973 	}
974 
975 	if (SCARG(uap, timeout) != NULL) {
976 		error = copyin(SCARG(uap, timeout), &lts, sizeof(lts));
977 		if (error != 0)
978 			return (error);
979 		linux_to_native_timespec(&uts, &lts);
980 
981 		if (itimespecfix(&uts))
982 			return (EINVAL);
983 
984 		nanotime(&ts0);
985 		tsp = &uts;
986 	} else {
987 		tsp = NULL;
988 	}
989 
990 	error = selcommon(retval, SCARG(uap, nfds), SCARG(uap, readfds),
991 	    SCARG(uap, writefds), SCARG(uap, exceptfds), tsp, ssp);
992 
993 	if (error == 0 && tsp != NULL) {
994 		if (retval != 0) {
995 			/*
996 			 * Compute how much time was left of the timeout,
997 			 * by subtracting the current time and the time
998 			 * before we started the call, and subtracting
999 			 * that result from the user-supplied value.
1000 			 */
1001 			nanotime(&ts1);
1002 			timespecsub(&ts1, &ts0, &ts1);
1003 			timespecsub(&uts, &ts1, &uts);
1004 			if (uts.tv_sec < 0)
1005 				timespecclear(&uts);
1006 		} else {
1007 			timespecclear(&uts);
1008 		}
1009 
1010 		native_to_linux_timespec(&lts, &uts);
1011 		error = copyout(&lts, SCARG(uap, timeout), sizeof(lts));
1012 	}
1013 
1014 	return (error);
1015 }
1016 
1017 int
1018 linux_sys_ppoll(struct lwp *l,
1019 	const struct linux_sys_ppoll_args *uap, register_t *retval)
1020 {
1021 	/* {
1022 		syscallarg(struct pollfd *) fds;
1023 		syscallarg(u_int) nfds;
1024 		syscallarg(struct linux_timespec *) timeout;
1025 		syscallarg(linux_sigset_t *) sigset;
1026 	} */
1027 	struct linux_timespec lts0, *lts;
1028 	struct timespec ts0, *ts = NULL;
1029 	linux_sigset_t lsigmask0, *lsigmask;
1030 	sigset_t sigmask0, *sigmask = NULL;
1031 	int error;
1032 
1033 	lts = SCARG(uap, timeout);
1034 	if (lts) {
1035 		if ((error = copyin(lts, &lts0, sizeof(lts0))) != 0)
1036 			return error;
1037 		linux_to_native_timespec(&ts0, &lts0);
1038 		ts = &ts0;
1039 	}
1040 
1041 	lsigmask = SCARG(uap, sigset);
1042 	if (lsigmask) {
1043 		if ((error = copyin(lsigmask, &lsigmask0, sizeof(lsigmask0))))
1044 			return error;
1045 		linux_to_native_sigset(&sigmask0, &lsigmask0);
1046 		sigmask = &sigmask0;
1047 	}
1048 
1049 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds),
1050 	    ts, sigmask);
1051 }
1052 
1053 /*
1054  * Set the 'personality' (emulation mode) for the current process. Only
1055  * accept the Linux personality here (0). This call is needed because
1056  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1057  * ELF binaries run in Linux mode, not SVR4 mode.
1058  */
1059 int
1060 linux_sys_personality(struct lwp *l, const struct linux_sys_personality_args *uap, register_t *retval)
1061 {
1062 	/* {
1063 		syscallarg(unsigned long) per;
1064 	} */
1065 	struct linux_emuldata *led;
1066 	int per;
1067 
1068 	per = SCARG(uap, per);
1069 	led = l->l_emuldata;
1070 	if (per == LINUX_PER_QUERY) {
1071 		retval[0] = led->led_personality;
1072 		return 0;
1073 	}
1074 
1075 	switch (per & LINUX_PER_MASK) {
1076 	case LINUX_PER_LINUX:
1077 	case LINUX_PER_LINUX32:
1078 		led->led_personality = per;
1079 		break;
1080 
1081 	default:
1082 		return EINVAL;
1083 	}
1084 
1085 	retval[0] = per;
1086 	return 0;
1087 }
1088 
1089 /*
1090  * We have nonexistent fsuid equal to uid.
1091  * If modification is requested, refuse.
1092  */
1093 int
1094 linux_sys_setfsuid(struct lwp *l, const struct linux_sys_setfsuid_args *uap, register_t *retval)
1095 {
1096 	 /* {
1097 		 syscallarg(uid_t) uid;
1098 	 } */
1099 	 uid_t uid;
1100 
1101 	 uid = SCARG(uap, uid);
1102 	 if (kauth_cred_getuid(l->l_cred) != uid)
1103 		 return sys_nosys(l, uap, retval);
1104 
1105 	 *retval = uid;
1106 	 return 0;
1107 }
1108 
1109 int
1110 linux_sys_setfsgid(struct lwp *l, const struct linux_sys_setfsgid_args *uap, register_t *retval)
1111 {
1112 	/* {
1113 		syscallarg(gid_t) gid;
1114 	} */
1115 	gid_t gid;
1116 
1117 	gid = SCARG(uap, gid);
1118 	if (kauth_cred_getgid(l->l_cred) != gid)
1119 		return sys_nosys(l, uap, retval);
1120 
1121 	*retval = gid;
1122 	return 0;
1123 }
1124 
1125 int
1126 linux_sys_setresuid(struct lwp *l, const struct linux_sys_setresuid_args *uap, register_t *retval)
1127 {
1128 	/* {
1129 		syscallarg(uid_t) ruid;
1130 		syscallarg(uid_t) euid;
1131 		syscallarg(uid_t) suid;
1132 	} */
1133 
1134 	/*
1135 	 * Note: These checks are a little different than the NetBSD
1136 	 * setreuid(2) call performs.  This precisely follows the
1137 	 * behavior of the Linux kernel.
1138 	 */
1139 
1140 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
1141 			    SCARG(uap, suid),
1142 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
1143 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
1144 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
1145 }
1146 
1147 int
1148 linux_sys_getresuid(struct lwp *l, const struct linux_sys_getresuid_args *uap, register_t *retval)
1149 {
1150 	/* {
1151 		syscallarg(uid_t *) ruid;
1152 		syscallarg(uid_t *) euid;
1153 		syscallarg(uid_t *) suid;
1154 	} */
1155 	kauth_cred_t pc = l->l_cred;
1156 	int error;
1157 	uid_t uid;
1158 
1159 	/*
1160 	 * Linux copies these values out to userspace like so:
1161 	 *
1162 	 *	1. Copy out ruid.
1163 	 *	2. If that succeeds, copy out euid.
1164 	 *	3. If both of those succeed, copy out suid.
1165 	 */
1166 	uid = kauth_cred_getuid(pc);
1167 	if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
1168 		return (error);
1169 
1170 	uid = kauth_cred_geteuid(pc);
1171 	if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
1172 		return (error);
1173 
1174 	uid = kauth_cred_getsvuid(pc);
1175 
1176 	return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
1177 }
1178 
1179 int
1180 linux_sys_ptrace(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
1181 {
1182 	/* {
1183 		i386, m68k, powerpc: T=int
1184 		alpha, amd64: T=long
1185 		syscallarg(T) request;
1186 		syscallarg(T) pid;
1187 		syscallarg(T) addr;
1188 		syscallarg(T) data;
1189 	} */
1190 	const int *ptr;
1191 	int request;
1192 	int error;
1193 
1194 	ptr = linux_ptrace_request_map;
1195 	request = SCARG(uap, request);
1196 	while (*ptr != -1)
1197 		if (*ptr++ == request) {
1198 			struct sys_ptrace_args pta;
1199 
1200 			SCARG(&pta, req) = *ptr;
1201 			SCARG(&pta, pid) = SCARG(uap, pid);
1202 			SCARG(&pta, addr) = (void *)SCARG(uap, addr);
1203 			SCARG(&pta, data) = SCARG(uap, data);
1204 
1205 			/*
1206 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1207 			 * to continue where the process left off previously.
1208  			 * The same thing is achieved by addr == (void *) 1
1209 			 * on NetBSD, so rewrite 'addr' appropriately.
1210 			 */
1211 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1212 				SCARG(&pta, addr) = (void *) 1;
1213 
1214 			error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
1215 			if (error)
1216 				return error;
1217 			switch (request) {
1218 			case LINUX_PTRACE_PEEKTEXT:
1219 			case LINUX_PTRACE_PEEKDATA:
1220 				error = copyout (retval,
1221 				    (void *)SCARG(uap, data),
1222 				    sizeof *retval);
1223 				*retval = SCARG(uap, data);
1224 				break;
1225 			default:
1226 				break;
1227 			}
1228 			return error;
1229 		}
1230 		else
1231 			ptr++;
1232 
1233 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1234 }
1235 
1236 int
1237 linux_sys_reboot(struct lwp *l, const struct linux_sys_reboot_args *uap, register_t *retval)
1238 {
1239 	/* {
1240 		syscallarg(int) magic1;
1241 		syscallarg(int) magic2;
1242 		syscallarg(int) cmd;
1243 		syscallarg(void *) arg;
1244 	} */
1245 	struct sys_reboot_args /* {
1246 		syscallarg(int) opt;
1247 		syscallarg(char *) bootstr;
1248 	} */ sra;
1249 	int error;
1250 
1251 	if ((error = kauth_authorize_system(l->l_cred,
1252 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1253 		return(error);
1254 
1255 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1256 		return(EINVAL);
1257 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1258 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1259 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1260 		return(EINVAL);
1261 
1262 	switch ((unsigned long)SCARG(uap, cmd)) {
1263 	case LINUX_REBOOT_CMD_RESTART:
1264 		SCARG(&sra, opt) = RB_AUTOBOOT;
1265 		break;
1266 	case LINUX_REBOOT_CMD_HALT:
1267 		SCARG(&sra, opt) = RB_HALT;
1268 		break;
1269 	case LINUX_REBOOT_CMD_POWER_OFF:
1270 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1271 		break;
1272 	case LINUX_REBOOT_CMD_RESTART2:
1273 		/* Reboot with an argument. */
1274 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1275 		SCARG(&sra, bootstr) = SCARG(uap, arg);
1276 		break;
1277 	case LINUX_REBOOT_CMD_CAD_ON:
1278 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
1279 	case LINUX_REBOOT_CMD_CAD_OFF:
1280 		return(0);
1281 	default:
1282 		return(EINVAL);
1283 	}
1284 
1285 	return(sys_reboot(l, &sra, retval));
1286 }
1287 
1288 /*
1289  * Copy of compat_12_sys_swapon().
1290  */
1291 int
1292 linux_sys_swapon(struct lwp *l, const struct linux_sys_swapon_args *uap, register_t *retval)
1293 {
1294 	/* {
1295 		syscallarg(const char *) name;
1296 	} */
1297 	struct sys_swapctl_args ua;
1298 
1299 	SCARG(&ua, cmd) = SWAP_ON;
1300 	SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
1301 	SCARG(&ua, misc) = 0;	/* priority */
1302 	return (sys_swapctl(l, &ua, retval));
1303 }
1304 
1305 /*
1306  * Stop swapping to the file or block device specified by path.
1307  */
1308 int
1309 linux_sys_swapoff(struct lwp *l, const struct linux_sys_swapoff_args *uap, register_t *retval)
1310 {
1311 	/* {
1312 		syscallarg(const char *) path;
1313 	} */
1314 	struct sys_swapctl_args ua;
1315 
1316 	SCARG(&ua, cmd) = SWAP_OFF;
1317 	SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
1318 	return (sys_swapctl(l, &ua, retval));
1319 }
1320 
1321 /*
1322  * Copy of compat_09_sys_setdomainname()
1323  */
1324 /* ARGSUSED */
1325 int
1326 linux_sys_setdomainname(struct lwp *l, const struct linux_sys_setdomainname_args *uap, register_t *retval)
1327 {
1328 	/* {
1329 		syscallarg(char *) domainname;
1330 		syscallarg(int) len;
1331 	} */
1332 	int name[2];
1333 
1334 	name[0] = CTL_KERN;
1335 	name[1] = KERN_DOMAINNAME;
1336 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
1337 			    SCARG(uap, len), l));
1338 }
1339 
1340 /*
1341  * sysinfo()
1342  */
1343 /* ARGSUSED */
1344 int
1345 linux_sys_sysinfo(struct lwp *l, const struct linux_sys_sysinfo_args *uap, register_t *retval)
1346 {
1347 	/* {
1348 		syscallarg(struct linux_sysinfo *) arg;
1349 	} */
1350 	struct linux_sysinfo si;
1351 	struct loadavg *la;
1352 	int64_t filepg;
1353 
1354 	memset(&si, 0, sizeof(si));
1355 	si.uptime = time_uptime;
1356 	la = &averunnable;
1357 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1358 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1359 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1360 	si.totalram = ctob((u_long)physmem);
1361 	/* uvm_availmem() may sync the counters. */
1362 	si.freeram = (u_long)uvm_availmem(true) * uvmexp.pagesize;
1363 	filepg = cpu_count_get(CPU_COUNT_FILECLEAN) +
1364 	    cpu_count_get(CPU_COUNT_FILEDIRTY) +
1365 	    cpu_count_get(CPU_COUNT_FILEUNKNOWN) -
1366 	    cpu_count_get(CPU_COUNT_EXECPAGES);
1367 	si.sharedram = 0;	/* XXX */
1368 	si.bufferram = (u_long)(filepg * uvmexp.pagesize);
1369 	si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
1370 	si.freeswap =
1371 	    (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1372 	si.procs = atomic_load_relaxed(&nprocs);
1373 
1374 	/* The following are only present in newer Linux kernels. */
1375 	si.totalbig = 0;
1376 	si.freebig = 0;
1377 	si.mem_unit = 1;
1378 
1379 	return (copyout(&si, SCARG(uap, arg), sizeof si));
1380 }
1381 
1382 int
1383 linux_sys_getrlimit(struct lwp *l, const struct linux_sys_getrlimit_args *uap, register_t *retval)
1384 {
1385 	/* {
1386 		syscallarg(int) which;
1387 # ifdef LINUX_LARGEFILE64
1388 		syscallarg(struct rlimit *) rlp;
1389 # else
1390 		syscallarg(struct orlimit *) rlp;
1391 # endif
1392 	} */
1393 # ifdef LINUX_LARGEFILE64
1394 	struct rlimit orl;
1395 # else
1396 	struct orlimit orl;
1397 # endif
1398 	int which;
1399 
1400 	which = linux_to_bsd_limit(SCARG(uap, which));
1401 	if (which < 0)
1402 		return -which;
1403 
1404 	memset(&orl, 0, sizeof(orl));
1405 	bsd_to_linux_rlimit(&orl, &l->l_proc->p_rlimit[which]);
1406 
1407 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1408 }
1409 
1410 int
1411 linux_sys_setrlimit(struct lwp *l, const struct linux_sys_setrlimit_args *uap, register_t *retval)
1412 {
1413 	/* {
1414 		syscallarg(int) which;
1415 # ifdef LINUX_LARGEFILE64
1416 		syscallarg(struct rlimit *) rlp;
1417 # else
1418 		syscallarg(struct orlimit *) rlp;
1419 # endif
1420 	} */
1421 	struct rlimit rl;
1422 # ifdef LINUX_LARGEFILE64
1423 	struct rlimit orl;
1424 # else
1425 	struct orlimit orl;
1426 # endif
1427 	int error;
1428 	int which;
1429 
1430 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1431 		return error;
1432 
1433 	which = linux_to_bsd_limit(SCARG(uap, which));
1434 	if (which < 0)
1435 		return -which;
1436 
1437 	linux_to_bsd_rlimit(&rl, &orl);
1438 	return dosetrlimit(l, l->l_proc, which, &rl);
1439 }
1440 
1441 # if !defined(__mips__) && !defined(__amd64__)
1442 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1443 int
1444 linux_sys_ugetrlimit(struct lwp *l, const struct linux_sys_ugetrlimit_args *uap, register_t *retval)
1445 {
1446 	return linux_sys_getrlimit(l, (const void *)uap, retval);
1447 }
1448 # endif
1449 
1450 /*
1451  * This gets called for unsupported syscalls. The difference to sys_nosys()
1452  * is that process does not get SIGSYS, the call just returns with ENOSYS.
1453  * This is the way Linux does it and glibc depends on this behaviour.
1454  */
1455 int
1456 linux_sys_nosys(struct lwp *l, const void *v, register_t *retval)
1457 {
1458 	return (ENOSYS);
1459 }
1460 
1461 int
1462 linux_sys_getpriority(struct lwp *l, const struct linux_sys_getpriority_args *uap, register_t *retval)
1463 {
1464         /* {
1465                 syscallarg(int) which;
1466                 syscallarg(int) who;
1467         } */
1468         struct sys_getpriority_args bsa;
1469         int error;
1470 
1471         SCARG(&bsa, which) = SCARG(uap, which);
1472         SCARG(&bsa, who) = SCARG(uap, who);
1473 
1474         if ((error = sys_getpriority(l, &bsa, retval)))
1475                 return error;
1476 
1477         *retval = NZERO - *retval;
1478 
1479         return 0;
1480 }
1481 
1482 int
1483 linux_do_sys_utimensat(struct lwp *l, int fd, const char *path, struct timespec *tsp, int flags, register_t *retval)
1484 {
1485 	int follow, error;
1486 
1487 	follow = (flags & LINUX_AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
1488 
1489 	if (path == NULL && fd != AT_FDCWD) {
1490 		file_t *fp;
1491 
1492 		/* fd_getvnode() will use the descriptor for us */
1493 		if ((error = fd_getvnode(fd, &fp)) != 0)
1494 			return error;
1495 		error = do_sys_utimensat(l, AT_FDCWD, fp->f_data, NULL, 0,
1496 		    tsp, UIO_SYSSPACE);
1497 		fd_putfile(fd);
1498 		return error;
1499 	}
1500 
1501 	return do_sys_utimensat(l, fd, NULL, path, follow, tsp, UIO_SYSSPACE);
1502 }
1503 
1504 int
1505 linux_sys_utimensat(struct lwp *l, const struct linux_sys_utimensat_args *uap,
1506 	register_t *retval)
1507 {
1508 	/* {
1509 		syscallarg(int) fd;
1510 		syscallarg(const char *) path;
1511 		syscallarg(const struct linux_timespec *) times;
1512 		syscallarg(int) flag;
1513 	} */
1514 	int error;
1515 	struct linux_timespec lts[2];
1516 	struct timespec *tsp = NULL, ts[2];
1517 
1518 	if (SCARG(uap, times)) {
1519 		error = copyin(SCARG(uap, times), &lts, sizeof(lts));
1520 		if (error != 0)
1521 			return error;
1522 		linux_to_native_timespec(&ts[0], &lts[0]);
1523 		linux_to_native_timespec(&ts[1], &lts[1]);
1524 		tsp = ts;
1525 	}
1526 
1527 	return linux_do_sys_utimensat(l, SCARG(uap, fd), SCARG(uap, path),
1528 	    tsp, SCARG(uap, flag), retval);
1529 }
1530 
1531 int
1532 linux_sys_futex(struct lwp *l, const struct linux_sys_futex_args *uap,
1533 	register_t *retval)
1534 {
1535 	/* {
1536 		syscallarg(int *) uaddr;
1537 		syscallarg(int) op;
1538 		syscallarg(int) val;
1539 		syscallarg(const struct linux_timespec *) timeout;
1540 		syscallarg(int *) uaddr2;
1541 		syscallarg(int) val3;
1542 	} */
1543 	struct linux_timespec lts;
1544 	struct timespec ts, *tsp = NULL;
1545 	int val2 = 0;
1546 	int error;
1547 
1548 	/*
1549 	 * Linux overlays the "timeout" field and the "val2" field.
1550 	 * "timeout" is only valid for FUTEX_WAIT and FUTEX_WAIT_BITSET
1551 	 * on Linux.
1552 	 */
1553 	const int op = (SCARG(uap, op) & FUTEX_CMD_MASK);
1554 	if ((op == FUTEX_WAIT || op == FUTEX_WAIT_BITSET) &&
1555 	    SCARG(uap, timeout) != NULL) {
1556 		if ((error = copyin(SCARG(uap, timeout),
1557 		    &lts, sizeof(lts))) != 0) {
1558 			return error;
1559 		}
1560 		linux_to_native_timespec(&ts, &lts);
1561 		tsp = &ts;
1562 	} else {
1563 		val2 = (int)(uintptr_t)SCARG(uap, timeout);
1564 	}
1565 
1566 	return linux_do_futex(SCARG(uap, uaddr), SCARG(uap, op),
1567 	    SCARG(uap, val), tsp, SCARG(uap, uaddr2), val2,
1568 	    SCARG(uap, val3), retval);
1569 }
1570 
1571 int
1572 linux_do_futex(int *uaddr, int op, int val, struct timespec *timeout,
1573     int *uaddr2, int val2, int val3, register_t *retval)
1574 {
1575 	/*
1576 	 * Always clear FUTEX_PRIVATE_FLAG for Linux processes.
1577 	 * NetBSD-native futexes exist in different namespace
1578 	 * depending on FUTEX_PRIVATE_FLAG.  This appears not
1579 	 * to be the case in Linux, and some futex users will
1580 	 * mix private and non-private ops on the same futex
1581 	 * object.
1582 	 */
1583 	return do_futex(uaddr, op & ~FUTEX_PRIVATE_FLAG,
1584 			val, timeout, uaddr2, val2, val3, retval);
1585 }
1586