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