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