xref: /netbsd-src/sys/compat/linux/common/linux_misc.c (revision 292a31115cd425403e51da039f4022d7c8f00032)
1 /*	$NetBSD: linux_misc.c,v 1.220 2012/09/19 21:19:15 pooka 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.220 2012/09/19 21:19:15 pooka 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 #ifndef COMPAT_LINUX32
117 #include <compat/linux/common/linux_statfs.h>
118 #include <compat/linux/common/linux_limit.h>
119 #endif
120 #include <compat/linux/common/linux_ptrace.h>
121 #include <compat/linux/common/linux_reboot.h>
122 #include <compat/linux/common/linux_emuldata.h>
123 #include <compat/linux/common/linux_sched.h>
124 
125 #include <compat/linux/linux_syscallargs.h>
126 
127 #ifndef COMPAT_LINUX32
128 const int linux_ptrace_request_map[] = {
129 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
130 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
131 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
132 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
133 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
134 	LINUX_PTRACE_CONT,	PT_CONTINUE,
135 	LINUX_PTRACE_KILL,	PT_KILL,
136 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
137 	LINUX_PTRACE_DETACH,	PT_DETACH,
138 # ifdef PT_STEP
139 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
140 # endif
141 	LINUX_PTRACE_SYSCALL,	PT_SYSCALL,
142 	-1
143 };
144 
145 const struct linux_mnttypes linux_fstypes[] = {
146 	{ MOUNT_FFS,		LINUX_DEFAULT_SUPER_MAGIC	},
147 	{ MOUNT_NFS,		LINUX_NFS_SUPER_MAGIC 		},
148 	{ MOUNT_MFS,		LINUX_DEFAULT_SUPER_MAGIC	},
149 	{ MOUNT_MSDOS,		LINUX_MSDOS_SUPER_MAGIC		},
150 	{ MOUNT_LFS,		LINUX_DEFAULT_SUPER_MAGIC	},
151 	{ MOUNT_FDESC,		LINUX_DEFAULT_SUPER_MAGIC	},
152 	{ MOUNT_NULL,		LINUX_DEFAULT_SUPER_MAGIC	},
153 	{ MOUNT_OVERLAY,	LINUX_DEFAULT_SUPER_MAGIC	},
154 	{ MOUNT_UMAP,		LINUX_DEFAULT_SUPER_MAGIC	},
155 	{ MOUNT_KERNFS,		LINUX_DEFAULT_SUPER_MAGIC	},
156 	{ MOUNT_PROCFS,		LINUX_PROC_SUPER_MAGIC		},
157 	{ MOUNT_AFS,		LINUX_DEFAULT_SUPER_MAGIC	},
158 	{ MOUNT_CD9660,		LINUX_ISOFS_SUPER_MAGIC		},
159 	{ MOUNT_UNION,		LINUX_DEFAULT_SUPER_MAGIC	},
160 	{ MOUNT_ADOSFS,		LINUX_ADFS_SUPER_MAGIC		},
161 	{ MOUNT_EXT2FS,		LINUX_EXT2_SUPER_MAGIC		},
162 	{ MOUNT_CFS,		LINUX_DEFAULT_SUPER_MAGIC	},
163 	{ MOUNT_CODA,		LINUX_CODA_SUPER_MAGIC		},
164 	{ MOUNT_FILECORE,	LINUX_DEFAULT_SUPER_MAGIC	},
165 	{ MOUNT_NTFS,		LINUX_DEFAULT_SUPER_MAGIC	},
166 	{ MOUNT_SMBFS,		LINUX_SMB_SUPER_MAGIC		},
167 	{ MOUNT_PTYFS,		LINUX_DEVPTS_SUPER_MAGIC	},
168 	{ MOUNT_TMPFS,		LINUX_TMPFS_SUPER_MAGIC		}
169 };
170 const int linux_fstypes_cnt = sizeof(linux_fstypes) / sizeof(linux_fstypes[0]);
171 
172 # ifdef DEBUG_LINUX
173 #define DPRINTF(a)	uprintf a
174 # else
175 #define DPRINTF(a)
176 # endif
177 
178 /* Local linux_misc.c functions: */
179 static void linux_to_bsd_mmap_args(struct sys_mmap_args *,
180     const struct linux_sys_mmap_args *);
181 static int linux_mmap(struct lwp *, const struct linux_sys_mmap_args *,
182     register_t *, off_t);
183 
184 
185 /*
186  * The information on a terminated (or stopped) process needs
187  * to be converted in order for Linux binaries to get a valid signal
188  * number out of it.
189  */
190 int
191 bsd_to_linux_wstat(int st)
192 {
193 
194 	int sig;
195 
196 	if (WIFSIGNALED(st)) {
197 		sig = WTERMSIG(st);
198 		if (sig >= 0 && sig < NSIG)
199 			st= (st & ~0177) | native_to_linux_signo[sig];
200 	} else if (WIFSTOPPED(st)) {
201 		sig = WSTOPSIG(st);
202 		if (sig >= 0 && sig < NSIG)
203 			st = (st & ~0xff00) |
204 			    (native_to_linux_signo[sig] << 8);
205 	}
206 	return st;
207 }
208 
209 /*
210  * wait4(2).  Passed on to the NetBSD call, surrounded by code to
211  * reserve some space for a NetBSD-style wait status, and converting
212  * it to what Linux wants.
213  */
214 int
215 linux_sys_wait4(struct lwp *l, const struct linux_sys_wait4_args *uap, register_t *retval)
216 {
217 	/* {
218 		syscallarg(int) pid;
219 		syscallarg(int *) status;
220 		syscallarg(int) options;
221 		syscallarg(struct rusage50 *) rusage;
222 	} */
223 	int error, status, options, linux_options, pid = SCARG(uap, pid);
224 	struct rusage50 ru50;
225 	struct rusage ru;
226 	proc_t *p;
227 
228 	linux_options = SCARG(uap, options);
229 	options = WOPTSCHECKED;
230 	if (linux_options & ~(LINUX_WAIT4_KNOWNFLAGS))
231 		return (EINVAL);
232 
233 	if (linux_options & LINUX_WAIT4_WNOHANG)
234 		options |= WNOHANG;
235 	if (linux_options & LINUX_WAIT4_WUNTRACED)
236 		options |= WUNTRACED;
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!",
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 	strlcpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
356 	strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
357 	strlcpy(luts.l_release, linux_release, sizeof(luts.l_release));
358 	strlcpy(luts.l_version, linux_version, sizeof(luts.l_version));
359 	strlcpy(luts.l_machine, LINUX_UNAME_ARCH, sizeof(luts.l_machine));
360 	strlcpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
361 
362 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
363 }
364 
365 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
366 /* Used indirectly on: arm, i386, m68k */
367 
368 /*
369  * New type Linux mmap call.
370  * Only called directly on machines with >= 6 free regs.
371  */
372 int
373 linux_sys_mmap(struct lwp *l, const struct linux_sys_mmap_args *uap, register_t *retval)
374 {
375 	/* {
376 		syscallarg(unsigned long) addr;
377 		syscallarg(size_t) len;
378 		syscallarg(int) prot;
379 		syscallarg(int) flags;
380 		syscallarg(int) fd;
381 		syscallarg(linux_off_t) offset;
382 	} */
383 
384 	if (SCARG(uap, offset) & PAGE_MASK)
385 		return EINVAL;
386 
387 	return linux_mmap(l, uap, retval, SCARG(uap, offset));
388 }
389 
390 /*
391  * Guts of most architectures' mmap64() implementations.  This shares
392  * its list of arguments with linux_sys_mmap().
393  *
394  * The difference in linux_sys_mmap2() is that "offset" is actually
395  * (offset / pagesize), not an absolute byte count.  This translation
396  * to pagesize offsets is done inside glibc between the mmap64() call
397  * point, and the actual syscall.
398  */
399 int
400 linux_sys_mmap2(struct lwp *l, const struct linux_sys_mmap2_args *uap, register_t *retval)
401 {
402 	/* {
403 		syscallarg(unsigned long) addr;
404 		syscallarg(size_t) len;
405 		syscallarg(int) prot;
406 		syscallarg(int) flags;
407 		syscallarg(int) fd;
408 		syscallarg(linux_off_t) offset;
409 	} */
410 
411 	return linux_mmap(l, uap, retval,
412 	    ((off_t)SCARG(uap, offset)) << PAGE_SHIFT);
413 }
414 
415 /*
416  * Massage arguments and call system mmap(2).
417  */
418 static int
419 linux_mmap(struct lwp *l, const struct linux_sys_mmap_args *uap, register_t *retval, off_t offset)
420 {
421 	struct sys_mmap_args cma;
422 	int error;
423 	size_t mmoff=0;
424 
425 	linux_to_bsd_mmap_args(&cma, uap);
426 	SCARG(&cma, pos) = offset;
427 
428 	if (SCARG(uap, flags) & LINUX_MAP_GROWSDOWN) {
429 		/*
430 		 * Request for stack-like memory segment. On linux, this
431 		 * works by mmap()ping (small) segment, which is automatically
432 		 * extended when page fault happens below the currently
433 		 * allocated area. We emulate this by allocating (typically
434 		 * bigger) segment sized at current stack size limit, and
435 		 * offsetting the requested and returned address accordingly.
436 		 * Since physical pages are only allocated on-demand, this
437 		 * is effectively identical.
438 		 */
439 		rlim_t ssl = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
440 
441 		if (SCARG(&cma, len) < ssl) {
442 			/* Compute the address offset */
443 			mmoff = round_page(ssl) - SCARG(uap, len);
444 
445 			if (SCARG(&cma, addr))
446 				SCARG(&cma, addr) = (char *)SCARG(&cma, addr) - mmoff;
447 
448 			SCARG(&cma, len) = (size_t) ssl;
449 		}
450 	}
451 
452 	error = sys_mmap(l, &cma, retval);
453 	if (error)
454 		return (error);
455 
456 	/* Shift the returned address for stack-like segment if necessary */
457 	retval[0] += mmoff;
458 
459 	return (0);
460 }
461 
462 static void
463 linux_to_bsd_mmap_args(struct sys_mmap_args *cma, const struct linux_sys_mmap_args *uap)
464 {
465 	int flags = MAP_TRYFIXED, fl = SCARG(uap, flags);
466 
467 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
468 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
469 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
470 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
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 int
543 linux_sys_mprotect(struct lwp *l, const struct linux_sys_mprotect_args *uap, register_t *retval)
544 {
545 	/* {
546 		syscallarg(const void *) start;
547 		syscallarg(unsigned long) len;
548 		syscallarg(int) prot;
549 	} */
550 	struct vm_map_entry *entry;
551 	struct vm_map *map;
552 	struct proc *p;
553 	vaddr_t end, start, len, stacklim;
554 	int prot, grows;
555 
556 	start = (vaddr_t)SCARG(uap, start);
557 	len = round_page(SCARG(uap, len));
558 	prot = SCARG(uap, prot);
559 	grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
560 	prot &= ~grows;
561 	end = start + len;
562 
563 	if (start & PAGE_MASK)
564 		return EINVAL;
565 	if (end < start)
566 		return EINVAL;
567 	if (end == start)
568 		return 0;
569 
570 	if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
571 		return EINVAL;
572 	if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
573 		return EINVAL;
574 
575 	p = l->l_proc;
576 	map = &p->p_vmspace->vm_map;
577 	vm_map_lock(map);
578 # ifdef notdef
579 	VM_MAP_RANGE_CHECK(map, start, end);
580 # endif
581 	if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
582 		vm_map_unlock(map);
583 		return ENOMEM;
584 	}
585 
586 	/*
587 	 * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
588 	 */
589 
590 	stacklim = (vaddr_t)p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
591 	if (grows & LINUX_PROT_GROWSDOWN) {
592 		if (USRSTACK - stacklim <= start && start < USRSTACK) {
593 			start = USRSTACK - stacklim;
594 		} else {
595 			start = entry->start;
596 		}
597 	} else if (grows & LINUX_PROT_GROWSUP) {
598 		if (USRSTACK <= end && end < USRSTACK + stacklim) {
599 			end = USRSTACK + stacklim;
600 		} else {
601 			end = entry->end;
602 		}
603 	}
604 	vm_map_unlock(map);
605 	return uvm_map_protect(map, start, end, prot, FALSE);
606 }
607 
608 /*
609  * This code is partly stolen from src/lib/libc/compat-43/times.c
610  */
611 
612 #define	CONVTCK(r)	(r.tv_sec * hz + r.tv_usec / (1000000 / hz))
613 
614 int
615 linux_sys_times(struct lwp *l, const struct linux_sys_times_args *uap, register_t *retval)
616 {
617 	/* {
618 		syscallarg(struct times *) tms;
619 	} */
620 	struct proc *p = l->l_proc;
621 	struct timeval t;
622 	int error;
623 
624 	if (SCARG(uap, tms)) {
625 		struct linux_tms ltms;
626 		struct rusage ru;
627 
628 		mutex_enter(p->p_lock);
629 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
630 		ltms.ltms_utime = CONVTCK(ru.ru_utime);
631 		ltms.ltms_stime = CONVTCK(ru.ru_stime);
632 		ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
633 		ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
634 		mutex_exit(p->p_lock);
635 
636 		if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
637 			return error;
638 	}
639 
640 	getmicrouptime(&t);
641 
642 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
643 	return 0;
644 }
645 
646 #undef CONVTCK
647 
648 /*
649  * Linux 'readdir' call. This code is mostly taken from the
650  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
651  * an attempt has been made to keep it a little cleaner (failing
652  * miserably, because of the cruft needed if count 1 is passed).
653  *
654  * The d_off field should contain the offset of the next valid entry,
655  * but in Linux it has the offset of the entry itself. We emulate
656  * that bug here.
657  *
658  * Read in BSD-style entries, convert them, and copy them out.
659  *
660  * Note that this doesn't handle union-mounted filesystems.
661  */
662 int
663 linux_sys_getdents(struct lwp *l, const struct linux_sys_getdents_args *uap, register_t *retval)
664 {
665 	/* {
666 		syscallarg(int) fd;
667 		syscallarg(struct linux_dirent *) dent;
668 		syscallarg(unsigned int) count;
669 	} */
670 	struct dirent *bdp;
671 	struct vnode *vp;
672 	char *inp, *tbuf;		/* BSD-format */
673 	int len, reclen;		/* BSD-format */
674 	char *outp;			/* Linux-format */
675 	int resid, linux_reclen = 0;	/* Linux-format */
676 	struct file *fp;
677 	struct uio auio;
678 	struct iovec aiov;
679 	struct linux_dirent idb;
680 	off_t off;		/* true file offset */
681 	int buflen, error, eofflag, nbytes, oldcall;
682 	struct vattr va;
683 	off_t *cookiebuf = NULL, *cookie;
684 	int ncookies;
685 
686 	/* fd_getvnode() will use the descriptor for us */
687 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
688 		return (error);
689 
690 	if ((fp->f_flag & FREAD) == 0) {
691 		error = EBADF;
692 		goto out1;
693 	}
694 
695 	vp = (struct vnode *)fp->f_data;
696 	if (vp->v_type != VDIR) {
697 		error = ENOTDIR;
698 		goto out1;
699 	}
700 
701 	vn_lock(vp, LK_SHARED | LK_RETRY);
702 	error = VOP_GETATTR(vp, &va, l->l_cred);
703 	VOP_UNLOCK(vp);
704 	if (error)
705 		goto out1;
706 
707 	nbytes = SCARG(uap, count);
708 	if (nbytes == 1) {	/* emulating old, broken behaviour */
709 		nbytes = sizeof (idb);
710 		buflen = max(va.va_blocksize, nbytes);
711 		oldcall = 1;
712 	} else {
713 		buflen = min(MAXBSIZE, nbytes);
714 		if (buflen < va.va_blocksize)
715 			buflen = va.va_blocksize;
716 		oldcall = 0;
717 	}
718 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
719 
720 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
721 	off = fp->f_offset;
722 again:
723 	aiov.iov_base = tbuf;
724 	aiov.iov_len = buflen;
725 	auio.uio_iov = &aiov;
726 	auio.uio_iovcnt = 1;
727 	auio.uio_rw = UIO_READ;
728 	auio.uio_resid = buflen;
729 	auio.uio_offset = off;
730 	UIO_SETUP_SYSSPACE(&auio);
731 	/*
732          * First we read into the malloc'ed buffer, then
733          * we massage it into user space, one record at a time.
734          */
735 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
736 	    &ncookies);
737 	if (error)
738 		goto out;
739 
740 	inp = tbuf;
741 	outp = (void *)SCARG(uap, dent);
742 	resid = nbytes;
743 	if ((len = buflen - auio.uio_resid) == 0)
744 		goto eof;
745 
746 	for (cookie = cookiebuf; len > 0; len -= reclen) {
747 		bdp = (struct dirent *)inp;
748 		reclen = bdp->d_reclen;
749 		if (reclen & 3)
750 			panic("linux_readdir");
751 		if (bdp->d_fileno == 0) {
752 			inp += reclen;	/* it is a hole; squish it out */
753 			if (cookie)
754 				off = *cookie++;
755 			else
756 				off += reclen;
757 			continue;
758 		}
759 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
760 		if (reclen > len || resid < linux_reclen) {
761 			/* entry too big for buffer, so just stop */
762 			outp++;
763 			break;
764 		}
765 		/*
766 		 * Massage in place to make a Linux-shaped dirent (otherwise
767 		 * we have to worry about touching user memory outside of
768 		 * the copyout() call).
769 		 */
770 		idb.d_ino = bdp->d_fileno;
771 		/*
772 		 * The old readdir() call misuses the offset and reclen fields.
773 		 */
774 		if (oldcall) {
775 			idb.d_off = (linux_off_t)linux_reclen;
776 			idb.d_reclen = (u_short)bdp->d_namlen;
777 		} else {
778 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
779 				compat_offseterr(vp, "linux_getdents");
780 				error = EINVAL;
781 				goto out;
782 			}
783 			idb.d_off = (linux_off_t)off;
784 			idb.d_reclen = (u_short)linux_reclen;
785 		}
786 		strcpy(idb.d_name, bdp->d_name);
787 		idb.d_name[strlen(idb.d_name) + 1] = bdp->d_type;
788 		if ((error = copyout((void *)&idb, outp, linux_reclen)))
789 			goto out;
790 		/* advance past this real entry */
791 		inp += reclen;
792 		if (cookie)
793 			off = *cookie++; /* each entry points to itself */
794 		else
795 			off += reclen;
796 		/* advance output past Linux-shaped entry */
797 		outp += linux_reclen;
798 		resid -= linux_reclen;
799 		if (oldcall)
800 			break;
801 	}
802 
803 	/* if we squished out the whole block, try again */
804 	if (outp == (void *)SCARG(uap, dent)) {
805 		if (cookiebuf)
806 			free(cookiebuf, M_TEMP);
807 		cookiebuf = NULL;
808 		goto again;
809 	}
810 	fp->f_offset = off;	/* update the vnode offset */
811 
812 	if (oldcall)
813 		nbytes = resid + linux_reclen;
814 
815 eof:
816 	*retval = nbytes - resid;
817 out:
818 	VOP_UNLOCK(vp);
819 	if (cookiebuf)
820 		free(cookiebuf, M_TEMP);
821 	free(tbuf, M_TEMP);
822 out1:
823 	fd_putfile(SCARG(uap, fd));
824 	return error;
825 }
826 
827 /*
828  * Even when just using registers to pass arguments to syscalls you can
829  * have 5 of them on the i386. So this newer version of select() does
830  * this.
831  */
832 int
833 linux_sys_select(struct lwp *l, const struct linux_sys_select_args *uap, register_t *retval)
834 {
835 	/* {
836 		syscallarg(int) nfds;
837 		syscallarg(fd_set *) readfds;
838 		syscallarg(fd_set *) writefds;
839 		syscallarg(fd_set *) exceptfds;
840 		syscallarg(struct timeval50 *) timeout;
841 	} */
842 
843 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
844 	    SCARG(uap, writefds), SCARG(uap, exceptfds),
845 	    (struct linux_timeval *)SCARG(uap, timeout));
846 }
847 
848 /*
849  * Common code for the old and new versions of select(). A couple of
850  * things are important:
851  * 1) return the amount of time left in the 'timeout' parameter
852  * 2) select never returns ERESTART on Linux, always return EINTR
853  */
854 int
855 linux_select1(struct lwp *l, register_t *retval, int nfds, fd_set *readfds,
856     fd_set *writefds, fd_set *exceptfds, struct linux_timeval *timeout)
857 {
858 	struct timespec ts0, ts1, uts, *ts = NULL;
859 	struct linux_timeval ltv;
860 	int error;
861 
862 	/*
863 	 * Store current time for computation of the amount of
864 	 * time left.
865 	 */
866 	if (timeout) {
867 		if ((error = copyin(timeout, &ltv, sizeof(ltv))))
868 			return error;
869 		uts.tv_sec = ltv.tv_sec;
870 		uts.tv_nsec = ltv.tv_usec * 1000;
871 		if (itimespecfix(&uts)) {
872 			/*
873 			 * The timeval was invalid.  Convert it to something
874 			 * valid that will act as it does under Linux.
875 			 */
876 			uts.tv_sec += uts.tv_nsec / 1000000000;
877 			uts.tv_nsec %= 1000000000;
878 			if (uts.tv_nsec < 0) {
879 				uts.tv_sec -= 1;
880 				uts.tv_nsec += 1000000000;
881 			}
882 			if (uts.tv_sec < 0)
883 				timespecclear(&uts);
884 		}
885 		ts = &uts;
886 		nanotime(&ts0);
887 	}
888 
889 	error = selcommon(retval, nfds, readfds, writefds, exceptfds, ts, NULL);
890 
891 	if (error) {
892 		/*
893 		 * See fs/select.c in the Linux kernel.  Without this,
894 		 * Maelstrom doesn't work.
895 		 */
896 		if (error == ERESTART)
897 			error = EINTR;
898 		return error;
899 	}
900 
901 	if (timeout) {
902 		if (*retval) {
903 			/*
904 			 * Compute how much time was left of the timeout,
905 			 * by subtracting the current time and the time
906 			 * before we started the call, and subtracting
907 			 * that result from the user-supplied value.
908 			 */
909 			nanotime(&ts1);
910 			timespecsub(&ts1, &ts0, &ts1);
911 			timespecsub(&uts, &ts1, &uts);
912 			if (uts.tv_sec < 0)
913 				timespecclear(&uts);
914 		} else
915 			timespecclear(&uts);
916 		ltv.tv_sec = uts.tv_sec;
917 		ltv.tv_usec = uts.tv_nsec / 1000;
918 		if ((error = copyout(&ltv, timeout, sizeof(ltv))))
919 			return error;
920 	}
921 
922 	return 0;
923 }
924 
925 int
926 linux_sys_ppoll(struct lwp *l,
927 	const struct linux_sys_ppoll_args *uap, register_t *retval)
928 {
929 	/* {
930 		syscallarg(struct pollfd *) fds;
931 		syscallarg(int) nfds;
932 		syscallarg(struct linux_timespec *) timeout;
933 		syscallarg(linux_sigset_t *) sigset;
934 	} */
935 	struct linux_timespec lts0, *lts;
936 	struct timespec ts0, *ts = NULL;
937 	linux_sigset_t lsigmask0, *lsigmask;
938 	sigset_t sigmask0, *sigmask = NULL;
939 	int error;
940 
941 	lts = SCARG(uap, timeout);
942 	if (lts) {
943 		if ((error = copyin(lts, &lts0, sizeof(lts0))) != 0)
944 			return error;
945 		linux_to_native_timespec(&ts0, &lts0);
946 		ts = &ts0;
947 	}
948 
949 	lsigmask = SCARG(uap, sigset);
950 	if (lsigmask) {
951 		if ((error = copyin(lsigmask, &lsigmask0, sizeof(lsigmask0))))
952 			return error;
953 		linux_to_native_sigset(&sigmask0, &lsigmask0);
954 		sigmask = &sigmask0;
955 	}
956 
957 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds),
958 	    ts, sigmask);
959 }
960 
961 /*
962  * Set the 'personality' (emulation mode) for the current process. Only
963  * accept the Linux personality here (0). This call is needed because
964  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
965  * ELF binaries run in Linux mode, not SVR4 mode.
966  */
967 int
968 linux_sys_personality(struct lwp *l, const struct linux_sys_personality_args *uap, register_t *retval)
969 {
970 	/* {
971 		syscallarg(unsigned long) per;
972 	} */
973 	struct linux_emuldata *led;
974 	int per;
975 
976 	per = SCARG(uap, per);
977 	led = l->l_emuldata;
978 	if (per == LINUX_PER_QUERY) {
979 		retval[0] = led->led_personality;
980 		return 0;
981 	}
982 
983 	switch (per & LINUX_PER_MASK) {
984 	case LINUX_PER_LINUX:
985 	case LINUX_PER_LINUX32:
986 		led->led_personality = per;
987 		break;
988 
989 	default:
990 		return EINVAL;
991 	}
992 
993 	retval[0] = per;
994 	return 0;
995 }
996 
997 /*
998  * We have nonexistent fsuid equal to uid.
999  * If modification is requested, refuse.
1000  */
1001 int
1002 linux_sys_setfsuid(struct lwp *l, const struct linux_sys_setfsuid_args *uap, register_t *retval)
1003 {
1004 	 /* {
1005 		 syscallarg(uid_t) uid;
1006 	 } */
1007 	 uid_t uid;
1008 
1009 	 uid = SCARG(uap, uid);
1010 	 if (kauth_cred_getuid(l->l_cred) != uid)
1011 		 return sys_nosys(l, uap, retval);
1012 
1013 	 *retval = uid;
1014 	 return 0;
1015 }
1016 
1017 int
1018 linux_sys_setfsgid(struct lwp *l, const struct linux_sys_setfsgid_args *uap, register_t *retval)
1019 {
1020 	/* {
1021 		syscallarg(gid_t) gid;
1022 	} */
1023 	gid_t gid;
1024 
1025 	gid = SCARG(uap, gid);
1026 	if (kauth_cred_getgid(l->l_cred) != gid)
1027 		return sys_nosys(l, uap, retval);
1028 
1029 	*retval = gid;
1030 	return 0;
1031 }
1032 
1033 int
1034 linux_sys_setresuid(struct lwp *l, const struct linux_sys_setresuid_args *uap, register_t *retval)
1035 {
1036 	/* {
1037 		syscallarg(uid_t) ruid;
1038 		syscallarg(uid_t) euid;
1039 		syscallarg(uid_t) suid;
1040 	} */
1041 
1042 	/*
1043 	 * Note: These checks are a little different than the NetBSD
1044 	 * setreuid(2) call performs.  This precisely follows the
1045 	 * behavior of the Linux kernel.
1046 	 */
1047 
1048 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
1049 			    SCARG(uap, suid),
1050 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
1051 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
1052 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
1053 }
1054 
1055 int
1056 linux_sys_getresuid(struct lwp *l, const struct linux_sys_getresuid_args *uap, register_t *retval)
1057 {
1058 	/* {
1059 		syscallarg(uid_t *) ruid;
1060 		syscallarg(uid_t *) euid;
1061 		syscallarg(uid_t *) suid;
1062 	} */
1063 	kauth_cred_t pc = l->l_cred;
1064 	int error;
1065 	uid_t uid;
1066 
1067 	/*
1068 	 * Linux copies these values out to userspace like so:
1069 	 *
1070 	 *	1. Copy out ruid.
1071 	 *	2. If that succeeds, copy out euid.
1072 	 *	3. If both of those succeed, copy out suid.
1073 	 */
1074 	uid = kauth_cred_getuid(pc);
1075 	if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
1076 		return (error);
1077 
1078 	uid = kauth_cred_geteuid(pc);
1079 	if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
1080 		return (error);
1081 
1082 	uid = kauth_cred_getsvuid(pc);
1083 
1084 	return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
1085 }
1086 
1087 int
1088 linux_sys_ptrace(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
1089 {
1090 	/* {
1091 		i386, m68k, powerpc: T=int
1092 		alpha, amd64: T=long
1093 		syscallarg(T) request;
1094 		syscallarg(T) pid;
1095 		syscallarg(T) addr;
1096 		syscallarg(T) data;
1097 	} */
1098 	const int *ptr;
1099 	int request;
1100 	int error;
1101 
1102 	ptr = linux_ptrace_request_map;
1103 	request = SCARG(uap, request);
1104 	while (*ptr != -1)
1105 		if (*ptr++ == request) {
1106 			struct sys_ptrace_args pta;
1107 
1108 			SCARG(&pta, req) = *ptr;
1109 			SCARG(&pta, pid) = SCARG(uap, pid);
1110 			SCARG(&pta, addr) = (void *)SCARG(uap, addr);
1111 			SCARG(&pta, data) = SCARG(uap, data);
1112 
1113 			/*
1114 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1115 			 * to continue where the process left off previously.
1116  			 * The same thing is achieved by addr == (void *) 1
1117 			 * on NetBSD, so rewrite 'addr' appropriately.
1118 			 */
1119 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1120 				SCARG(&pta, addr) = (void *) 1;
1121 
1122 			error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
1123 			if (error)
1124 				return error;
1125 			switch (request) {
1126 			case LINUX_PTRACE_PEEKTEXT:
1127 			case LINUX_PTRACE_PEEKDATA:
1128 				error = copyout (retval,
1129 				    (void *)SCARG(uap, data),
1130 				    sizeof *retval);
1131 				*retval = SCARG(uap, data);
1132 				break;
1133 			default:
1134 				break;
1135 			}
1136 			return error;
1137 		}
1138 		else
1139 			ptr++;
1140 
1141 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1142 }
1143 
1144 int
1145 linux_sys_reboot(struct lwp *l, const struct linux_sys_reboot_args *uap, register_t *retval)
1146 {
1147 	/* {
1148 		syscallarg(int) magic1;
1149 		syscallarg(int) magic2;
1150 		syscallarg(int) cmd;
1151 		syscallarg(void *) arg;
1152 	} */
1153 	struct sys_reboot_args /* {
1154 		syscallarg(int) opt;
1155 		syscallarg(char *) bootstr;
1156 	} */ sra;
1157 	int error;
1158 
1159 	if ((error = kauth_authorize_system(l->l_cred,
1160 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1161 		return(error);
1162 
1163 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1164 		return(EINVAL);
1165 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1166 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1167 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1168 		return(EINVAL);
1169 
1170 	switch ((unsigned long)SCARG(uap, cmd)) {
1171 	case LINUX_REBOOT_CMD_RESTART:
1172 		SCARG(&sra, opt) = RB_AUTOBOOT;
1173 		break;
1174 	case LINUX_REBOOT_CMD_HALT:
1175 		SCARG(&sra, opt) = RB_HALT;
1176 		break;
1177 	case LINUX_REBOOT_CMD_POWER_OFF:
1178 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1179 		break;
1180 	case LINUX_REBOOT_CMD_RESTART2:
1181 		/* Reboot with an argument. */
1182 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1183 		SCARG(&sra, bootstr) = SCARG(uap, arg);
1184 		break;
1185 	case LINUX_REBOOT_CMD_CAD_ON:
1186 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
1187 	case LINUX_REBOOT_CMD_CAD_OFF:
1188 		return(0);
1189 	default:
1190 		return(EINVAL);
1191 	}
1192 
1193 	return(sys_reboot(l, &sra, retval));
1194 }
1195 
1196 /*
1197  * Copy of compat_12_sys_swapon().
1198  */
1199 int
1200 linux_sys_swapon(struct lwp *l, const struct linux_sys_swapon_args *uap, register_t *retval)
1201 {
1202 	/* {
1203 		syscallarg(const char *) name;
1204 	} */
1205 	struct sys_swapctl_args ua;
1206 
1207 	SCARG(&ua, cmd) = SWAP_ON;
1208 	SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
1209 	SCARG(&ua, misc) = 0;	/* priority */
1210 	return (sys_swapctl(l, &ua, retval));
1211 }
1212 
1213 /*
1214  * Stop swapping to the file or block device specified by path.
1215  */
1216 int
1217 linux_sys_swapoff(struct lwp *l, const struct linux_sys_swapoff_args *uap, register_t *retval)
1218 {
1219 	/* {
1220 		syscallarg(const char *) path;
1221 	} */
1222 	struct sys_swapctl_args ua;
1223 
1224 	SCARG(&ua, cmd) = SWAP_OFF;
1225 	SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
1226 	return (sys_swapctl(l, &ua, retval));
1227 }
1228 
1229 /*
1230  * Copy of compat_09_sys_setdomainname()
1231  */
1232 /* ARGSUSED */
1233 int
1234 linux_sys_setdomainname(struct lwp *l, const struct linux_sys_setdomainname_args *uap, register_t *retval)
1235 {
1236 	/* {
1237 		syscallarg(char *) domainname;
1238 		syscallarg(int) len;
1239 	} */
1240 	int name[2];
1241 
1242 	name[0] = CTL_KERN;
1243 	name[1] = KERN_DOMAINNAME;
1244 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
1245 			    SCARG(uap, len), l));
1246 }
1247 
1248 /*
1249  * sysinfo()
1250  */
1251 /* ARGSUSED */
1252 int
1253 linux_sys_sysinfo(struct lwp *l, const struct linux_sys_sysinfo_args *uap, register_t *retval)
1254 {
1255 	/* {
1256 		syscallarg(struct linux_sysinfo *) arg;
1257 	} */
1258 	struct linux_sysinfo si;
1259 	struct loadavg *la;
1260 
1261 	si.uptime = time_uptime;
1262 	la = &averunnable;
1263 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1264 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1265 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1266 	si.totalram = ctob((u_long)physmem);
1267 	si.freeram = (u_long)uvmexp.free * uvmexp.pagesize;
1268 	si.sharedram = 0;	/* XXX */
1269 	si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
1270 	si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
1271 	si.freeswap =
1272 	    (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1273 	si.procs = nprocs;
1274 
1275 	/* The following are only present in newer Linux kernels. */
1276 	si.totalbig = 0;
1277 	si.freebig = 0;
1278 	si.mem_unit = 1;
1279 
1280 	return (copyout(&si, SCARG(uap, arg), sizeof si));
1281 }
1282 
1283 int
1284 linux_sys_getrlimit(struct lwp *l, const struct linux_sys_getrlimit_args *uap, register_t *retval)
1285 {
1286 	/* {
1287 		syscallarg(int) which;
1288 # ifdef LINUX_LARGEFILE64
1289 		syscallarg(struct rlimit *) rlp;
1290 # else
1291 		syscallarg(struct orlimit *) rlp;
1292 # endif
1293 	} */
1294 # ifdef LINUX_LARGEFILE64
1295 	struct rlimit orl;
1296 # else
1297 	struct orlimit orl;
1298 # endif
1299 	int which;
1300 
1301 	which = linux_to_bsd_limit(SCARG(uap, which));
1302 	if (which < 0)
1303 		return -which;
1304 
1305 	bsd_to_linux_rlimit(&orl, &l->l_proc->p_rlimit[which]);
1306 
1307 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1308 }
1309 
1310 int
1311 linux_sys_setrlimit(struct lwp *l, const struct linux_sys_setrlimit_args *uap, register_t *retval)
1312 {
1313 	/* {
1314 		syscallarg(int) which;
1315 # ifdef LINUX_LARGEFILE64
1316 		syscallarg(struct rlimit *) rlp;
1317 # else
1318 		syscallarg(struct orlimit *) rlp;
1319 # endif
1320 	} */
1321 	struct rlimit rl;
1322 # ifdef LINUX_LARGEFILE64
1323 	struct rlimit orl;
1324 # else
1325 	struct orlimit orl;
1326 # endif
1327 	int error;
1328 	int which;
1329 
1330 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1331 		return error;
1332 
1333 	which = linux_to_bsd_limit(SCARG(uap, which));
1334 	if (which < 0)
1335 		return -which;
1336 
1337 	linux_to_bsd_rlimit(&rl, &orl);
1338 	return dosetrlimit(l, l->l_proc, which, &rl);
1339 }
1340 
1341 # if !defined(__mips__) && !defined(__amd64__)
1342 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1343 int
1344 linux_sys_ugetrlimit(struct lwp *l, const struct linux_sys_ugetrlimit_args *uap, register_t *retval)
1345 {
1346 	return linux_sys_getrlimit(l, (const void *)uap, retval);
1347 }
1348 # endif
1349 
1350 /*
1351  * This gets called for unsupported syscalls. The difference to sys_nosys()
1352  * is that process does not get SIGSYS, the call just returns with ENOSYS.
1353  * This is the way Linux does it and glibc depends on this behaviour.
1354  */
1355 int
1356 linux_sys_nosys(struct lwp *l, const void *v, register_t *retval)
1357 {
1358 	return (ENOSYS);
1359 }
1360 
1361 int
1362 linux_sys_getpriority(struct lwp *l, const struct linux_sys_getpriority_args *uap, register_t *retval)
1363 {
1364         /* {
1365                 syscallarg(int) which;
1366                 syscallarg(int) who;
1367         } */
1368         struct sys_getpriority_args bsa;
1369         int error;
1370 
1371         SCARG(&bsa, which) = SCARG(uap, which);
1372         SCARG(&bsa, who) = SCARG(uap, who);
1373 
1374         if ((error = sys_getpriority(l, &bsa, retval)))
1375                 return error;
1376 
1377         *retval = NZERO - *retval;
1378 
1379         return 0;
1380 }
1381 #endif /* !COMPAT_LINUX32 */
1382