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