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