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