xref: /netbsd-src/sys/compat/linux/common/linux_misc.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: linux_misc.c,v 1.193 2008/01/15 22:38:34 njoly Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998, 1999 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.193 2008/01/15 22:38:34 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_msync(struct lwp *l, const struct linux_sys_msync_args *uap, register_t *retval)
554 {
555 	/* {
556 		syscallarg(void *) addr;
557 		syscallarg(int) len;
558 		syscallarg(int) fl;
559 	} */
560 
561 	struct sys___msync13_args bma;
562 
563 	/* flags are ignored */
564 	SCARG(&bma, addr) = SCARG(uap, addr);
565 	SCARG(&bma, len) = SCARG(uap, len);
566 	SCARG(&bma, flags) = SCARG(uap, fl);
567 
568 	return sys___msync13(l, &bma, retval);
569 }
570 
571 int
572 linux_sys_mprotect(struct lwp *l, const struct linux_sys_mprotect_args *uap, register_t *retval)
573 {
574 	/* {
575 		syscallarg(const void *) start;
576 		syscallarg(unsigned long) len;
577 		syscallarg(int) prot;
578 	} */
579 	struct vm_map_entry *entry;
580 	struct vm_map *map;
581 	struct proc *p;
582 	vaddr_t end, start, len, stacklim;
583 	int prot, grows;
584 
585 	start = (vaddr_t)SCARG(uap, start);
586 	len = round_page(SCARG(uap, len));
587 	prot = SCARG(uap, prot);
588 	grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
589 	prot &= ~grows;
590 	end = start + len;
591 
592 	if (start & PAGE_MASK)
593 		return EINVAL;
594 	if (end < start)
595 		return EINVAL;
596 	if (end == start)
597 		return 0;
598 
599 	if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
600 		return EINVAL;
601 	if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
602 		return EINVAL;
603 
604 	p = l->l_proc;
605 	map = &p->p_vmspace->vm_map;
606 	vm_map_lock(map);
607 # ifdef notdef
608 	VM_MAP_RANGE_CHECK(map, start, end);
609 # endif
610 	if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
611 		vm_map_unlock(map);
612 		return ENOMEM;
613 	}
614 
615 	/*
616 	 * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
617 	 */
618 
619 	stacklim = (vaddr_t)p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
620 	if (grows & LINUX_PROT_GROWSDOWN) {
621 		if (USRSTACK - stacklim <= start && start < USRSTACK) {
622 			start = USRSTACK - stacklim;
623 		} else {
624 			start = entry->start;
625 		}
626 	} else if (grows & LINUX_PROT_GROWSUP) {
627 		if (USRSTACK <= end && end < USRSTACK + stacklim) {
628 			end = USRSTACK + stacklim;
629 		} else {
630 			end = entry->end;
631 		}
632 	}
633 	vm_map_unlock(map);
634 	return uvm_map_protect(map, start, end, prot, FALSE);
635 }
636 
637 /*
638  * This code is partly stolen from src/lib/libc/compat-43/times.c
639  */
640 
641 #define	CONVTCK(r)	(r.tv_sec * hz + r.tv_usec / (1000000 / hz))
642 
643 int
644 linux_sys_times(struct lwp *l, const struct linux_sys_times_args *uap, register_t *retval)
645 {
646 	/* {
647 		syscallarg(struct times *) tms;
648 	} */
649 	struct proc *p = l->l_proc;
650 	struct timeval t;
651 	int error;
652 
653 	if (SCARG(uap, tms)) {
654 		struct linux_tms ltms;
655 		struct rusage ru;
656 
657 		mutex_enter(&p->p_smutex);
658 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
659 		ltms.ltms_utime = CONVTCK(ru.ru_utime);
660 		ltms.ltms_stime = CONVTCK(ru.ru_stime);
661 		ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
662 		ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
663 		mutex_exit(&p->p_smutex);
664 
665 		if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
666 			return error;
667 	}
668 
669 	getmicrouptime(&t);
670 
671 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
672 	return 0;
673 }
674 
675 #undef CONVTCK
676 
677 /*
678  * Linux 'readdir' call. This code is mostly taken from the
679  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
680  * an attempt has been made to keep it a little cleaner (failing
681  * miserably, because of the cruft needed if count 1 is passed).
682  *
683  * The d_off field should contain the offset of the next valid entry,
684  * but in Linux it has the offset of the entry itself. We emulate
685  * that bug here.
686  *
687  * Read in BSD-style entries, convert them, and copy them out.
688  *
689  * Note that this doesn't handle union-mounted filesystems.
690  */
691 int
692 linux_sys_getdents(struct lwp *l, const struct linux_sys_getdents_args *uap, register_t *retval)
693 {
694 	/* {
695 		syscallarg(int) fd;
696 		syscallarg(struct linux_dirent *) dent;
697 		syscallarg(unsigned int) count;
698 	} */
699 	struct dirent *bdp;
700 	struct vnode *vp;
701 	char *inp, *tbuf;		/* BSD-format */
702 	int len, reclen;		/* BSD-format */
703 	char *outp;			/* Linux-format */
704 	int resid, linux_reclen = 0;	/* Linux-format */
705 	struct file *fp;
706 	struct uio auio;
707 	struct iovec aiov;
708 	struct linux_dirent idb;
709 	off_t off;		/* true file offset */
710 	int buflen, error, eofflag, nbytes, oldcall;
711 	struct vattr va;
712 	off_t *cookiebuf = NULL, *cookie;
713 	int ncookies;
714 
715 	/* getvnode() will use the descriptor for us */
716 	if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
717 		return (error);
718 
719 	if ((fp->f_flag & FREAD) == 0) {
720 		error = EBADF;
721 		goto out1;
722 	}
723 
724 	vp = (struct vnode *)fp->f_data;
725 	if (vp->v_type != VDIR) {
726 		error = EINVAL;
727 		goto out1;
728 	}
729 
730 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
731 		goto out1;
732 
733 	nbytes = SCARG(uap, count);
734 	if (nbytes == 1) {	/* emulating old, broken behaviour */
735 		nbytes = sizeof (idb);
736 		buflen = max(va.va_blocksize, nbytes);
737 		oldcall = 1;
738 	} else {
739 		buflen = min(MAXBSIZE, nbytes);
740 		if (buflen < va.va_blocksize)
741 			buflen = va.va_blocksize;
742 		oldcall = 0;
743 	}
744 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
745 
746 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
747 	off = fp->f_offset;
748 again:
749 	aiov.iov_base = tbuf;
750 	aiov.iov_len = buflen;
751 	auio.uio_iov = &aiov;
752 	auio.uio_iovcnt = 1;
753 	auio.uio_rw = UIO_READ;
754 	auio.uio_resid = buflen;
755 	auio.uio_offset = off;
756 	UIO_SETUP_SYSSPACE(&auio);
757 	/*
758          * First we read into the malloc'ed buffer, then
759          * we massage it into user space, one record at a time.
760          */
761 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
762 	    &ncookies);
763 	if (error)
764 		goto out;
765 
766 	inp = tbuf;
767 	outp = (void *)SCARG(uap, dent);
768 	resid = nbytes;
769 	if ((len = buflen - auio.uio_resid) == 0)
770 		goto eof;
771 
772 	for (cookie = cookiebuf; len > 0; len -= reclen) {
773 		bdp = (struct dirent *)inp;
774 		reclen = bdp->d_reclen;
775 		if (reclen & 3)
776 			panic("linux_readdir");
777 		if (bdp->d_fileno == 0) {
778 			inp += reclen;	/* it is a hole; squish it out */
779 			if (cookie)
780 				off = *cookie++;
781 			else
782 				off += reclen;
783 			continue;
784 		}
785 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
786 		if (reclen > len || resid < linux_reclen) {
787 			/* entry too big for buffer, so just stop */
788 			outp++;
789 			break;
790 		}
791 		/*
792 		 * Massage in place to make a Linux-shaped dirent (otherwise
793 		 * we have to worry about touching user memory outside of
794 		 * the copyout() call).
795 		 */
796 		idb.d_ino = bdp->d_fileno;
797 		/*
798 		 * The old readdir() call misuses the offset and reclen fields.
799 		 */
800 		if (oldcall) {
801 			idb.d_off = (linux_off_t)linux_reclen;
802 			idb.d_reclen = (u_short)bdp->d_namlen;
803 		} else {
804 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
805 				compat_offseterr(vp, "linux_getdents");
806 				error = EINVAL;
807 				goto out;
808 			}
809 			idb.d_off = (linux_off_t)off;
810 			idb.d_reclen = (u_short)linux_reclen;
811 		}
812 		strcpy(idb.d_name, bdp->d_name);
813 		if ((error = copyout((void *)&idb, outp, linux_reclen)))
814 			goto out;
815 		/* advance past this real entry */
816 		inp += reclen;
817 		if (cookie)
818 			off = *cookie++; /* each entry points to itself */
819 		else
820 			off += reclen;
821 		/* advance output past Linux-shaped entry */
822 		outp += linux_reclen;
823 		resid -= linux_reclen;
824 		if (oldcall)
825 			break;
826 	}
827 
828 	/* if we squished out the whole block, try again */
829 	if (outp == (void *)SCARG(uap, dent))
830 		goto again;
831 	fp->f_offset = off;	/* update the vnode offset */
832 
833 	if (oldcall)
834 		nbytes = resid + linux_reclen;
835 
836 eof:
837 	*retval = nbytes - resid;
838 out:
839 	VOP_UNLOCK(vp, 0);
840 	if (cookiebuf)
841 		free(cookiebuf, M_TEMP);
842 	free(tbuf, M_TEMP);
843 out1:
844 	FILE_UNUSE(fp, l);
845 	return error;
846 }
847 
848 /*
849  * Even when just using registers to pass arguments to syscalls you can
850  * have 5 of them on the i386. So this newer version of select() does
851  * this.
852  */
853 int
854 linux_sys_select(struct lwp *l, const struct linux_sys_select_args *uap, register_t *retval)
855 {
856 	/* {
857 		syscallarg(int) nfds;
858 		syscallarg(fd_set *) readfds;
859 		syscallarg(fd_set *) writefds;
860 		syscallarg(fd_set *) exceptfds;
861 		syscallarg(struct timeval *) timeout;
862 	} */
863 
864 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
865 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
866 }
867 
868 /*
869  * Common code for the old and new versions of select(). A couple of
870  * things are important:
871  * 1) return the amount of time left in the 'timeout' parameter
872  * 2) select never returns ERESTART on Linux, always return EINTR
873  */
874 int
875 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
876 	struct lwp *l;
877 	register_t *retval;
878 	int nfds;
879 	fd_set *readfds, *writefds, *exceptfds;
880 	struct timeval *timeout;
881 {
882 	struct timeval tv0, tv1, utv, *tv = NULL;
883 	int error;
884 
885 	/*
886 	 * Store current time for computation of the amount of
887 	 * time left.
888 	 */
889 	if (timeout) {
890 		if ((error = copyin(timeout, &utv, sizeof(utv))))
891 			return error;
892 		if (itimerfix(&utv)) {
893 			/*
894 			 * The timeval was invalid.  Convert it to something
895 			 * valid that will act as it does under Linux.
896 			 */
897 			utv.tv_sec += utv.tv_usec / 1000000;
898 			utv.tv_usec %= 1000000;
899 			if (utv.tv_usec < 0) {
900 				utv.tv_sec -= 1;
901 				utv.tv_usec += 1000000;
902 			}
903 			if (utv.tv_sec < 0)
904 				timerclear(&utv);
905 		}
906 		tv = &utv;
907 		microtime(&tv0);
908 	}
909 
910 	error = selcommon(l, retval, nfds, readfds, writefds, exceptfds,
911 	    tv, NULL);
912 
913 	if (error) {
914 		/*
915 		 * See fs/select.c in the Linux kernel.  Without this,
916 		 * Maelstrom doesn't work.
917 		 */
918 		if (error == ERESTART)
919 			error = EINTR;
920 		return error;
921 	}
922 
923 	if (timeout) {
924 		if (*retval) {
925 			/*
926 			 * Compute how much time was left of the timeout,
927 			 * by subtracting the current time and the time
928 			 * before we started the call, and subtracting
929 			 * that result from the user-supplied value.
930 			 */
931 			microtime(&tv1);
932 			timersub(&tv1, &tv0, &tv1);
933 			timersub(&utv, &tv1, &utv);
934 			if (utv.tv_sec < 0)
935 				timerclear(&utv);
936 		} else
937 			timerclear(&utv);
938 		if ((error = copyout(&utv, timeout, sizeof(utv))))
939 			return error;
940 	}
941 
942 	return 0;
943 }
944 
945 /*
946  * Set the 'personality' (emulation mode) for the current process. Only
947  * accept the Linux personality here (0). This call is needed because
948  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
949  * ELF binaries run in Linux mode, not SVR4 mode.
950  */
951 int
952 linux_sys_personality(struct lwp *l, const struct linux_sys_personality_args *uap, register_t *retval)
953 {
954 	/* {
955 		syscallarg(int) per;
956 	} */
957 
958 	if (SCARG(uap, per) != 0)
959 		return EINVAL;
960 	retval[0] = 0;
961 	return 0;
962 }
963 
964 /*
965  * We have nonexistent fsuid equal to uid.
966  * If modification is requested, refuse.
967  */
968 int
969 linux_sys_setfsuid(struct lwp *l, const struct linux_sys_setfsuid_args *uap, register_t *retval)
970 {
971 	 /* {
972 		 syscallarg(uid_t) uid;
973 	 } */
974 	 uid_t uid;
975 
976 	 uid = SCARG(uap, uid);
977 	 if (kauth_cred_getuid(l->l_cred) != uid)
978 		 return sys_nosys(l, uap, retval);
979 
980 	 *retval = uid;
981 	 return 0;
982 }
983 
984 int
985 linux_sys_setfsgid(struct lwp *l, const struct linux_sys_setfsgid_args *uap, register_t *retval)
986 {
987 	/* {
988 		syscallarg(gid_t) gid;
989 	} */
990 	gid_t gid;
991 
992 	gid = SCARG(uap, gid);
993 	if (kauth_cred_getgid(l->l_cred) != gid)
994 		return sys_nosys(l, uap, retval);
995 
996 	*retval = gid;
997 	return 0;
998 }
999 
1000 int
1001 linux_sys_setresuid(struct lwp *l, const struct linux_sys_setresuid_args *uap, register_t *retval)
1002 {
1003 	/* {
1004 		syscallarg(uid_t) ruid;
1005 		syscallarg(uid_t) euid;
1006 		syscallarg(uid_t) suid;
1007 	} */
1008 
1009 	/*
1010 	 * Note: These checks are a little different than the NetBSD
1011 	 * setreuid(2) call performs.  This precisely follows the
1012 	 * behavior of the Linux kernel.
1013 	 */
1014 
1015 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
1016 			    SCARG(uap, suid),
1017 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
1018 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
1019 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
1020 }
1021 
1022 int
1023 linux_sys_getresuid(struct lwp *l, const struct linux_sys_getresuid_args *uap, register_t *retval)
1024 {
1025 	/* {
1026 		syscallarg(uid_t *) ruid;
1027 		syscallarg(uid_t *) euid;
1028 		syscallarg(uid_t *) suid;
1029 	} */
1030 	kauth_cred_t pc = l->l_cred;
1031 	int error;
1032 	uid_t uid;
1033 
1034 	/*
1035 	 * Linux copies these values out to userspace like so:
1036 	 *
1037 	 *	1. Copy out ruid.
1038 	 *	2. If that succeeds, copy out euid.
1039 	 *	3. If both of those succeed, copy out suid.
1040 	 */
1041 	uid = kauth_cred_getuid(pc);
1042 	if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
1043 		return (error);
1044 
1045 	uid = kauth_cred_geteuid(pc);
1046 	if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
1047 		return (error);
1048 
1049 	uid = kauth_cred_getsvuid(pc);
1050 
1051 	return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
1052 }
1053 
1054 int
1055 linux_sys_ptrace(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
1056 {
1057 	/* {
1058 		i386, m68k, powerpc: T=int
1059 		alpha, amd64: T=long
1060 		syscallarg(T) request;
1061 		syscallarg(T) pid;
1062 		syscallarg(T) addr;
1063 		syscallarg(T) data;
1064 	} */
1065 #if defined(PTRACE) || defined(_LKM)
1066 	const int *ptr;
1067 	int request;
1068 	int error;
1069 #ifdef _LKM
1070 #define sys_ptrace (*sysent[SYS_ptrace].sy_call)
1071 #endif
1072 
1073 	ptr = linux_ptrace_request_map;
1074 	request = SCARG(uap, request);
1075 	while (*ptr != -1)
1076 		if (*ptr++ == request) {
1077 			struct sys_ptrace_args pta;
1078 
1079 			SCARG(&pta, req) = *ptr;
1080 			SCARG(&pta, pid) = SCARG(uap, pid);
1081 			SCARG(&pta, addr) = (void *)SCARG(uap, addr);
1082 			SCARG(&pta, data) = SCARG(uap, data);
1083 
1084 			/*
1085 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1086 			 * to continue where the process left off previously.
1087 			 * The same thing is achieved by addr == (void *) 1
1088 			 * on NetBSD, so rewrite 'addr' appropriately.
1089 			 */
1090 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1091 				SCARG(&pta, addr) = (void *) 1;
1092 
1093 			error = sys_ptrace(l, &pta, retval);
1094 			if (error)
1095 				return error;
1096 			switch (request) {
1097 			case LINUX_PTRACE_PEEKTEXT:
1098 			case LINUX_PTRACE_PEEKDATA:
1099 				error = copyout (retval,
1100 				    (void *)SCARG(uap, data),
1101 				    sizeof *retval);
1102 				*retval = SCARG(uap, data);
1103 				break;
1104 			default:
1105 				break;
1106 			}
1107 			return error;
1108 		}
1109 		else
1110 			ptr++;
1111 
1112 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1113 #else
1114 	return ENOSYS;
1115 #endif /* PTRACE || _LKM */
1116 }
1117 
1118 int
1119 linux_sys_reboot(struct lwp *l, const struct linux_sys_reboot_args *uap, register_t *retval)
1120 {
1121 	/* {
1122 		syscallarg(int) magic1;
1123 		syscallarg(int) magic2;
1124 		syscallarg(int) cmd;
1125 		syscallarg(void *) arg;
1126 	} */
1127 	struct sys_reboot_args /* {
1128 		syscallarg(int) opt;
1129 		syscallarg(char *) bootstr;
1130 	} */ sra;
1131 	int error;
1132 
1133 	if ((error = kauth_authorize_system(l->l_cred,
1134 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1135 		return(error);
1136 
1137 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1138 		return(EINVAL);
1139 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1140 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1141 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1142 		return(EINVAL);
1143 
1144 	switch (SCARG(uap, cmd)) {
1145 	case LINUX_REBOOT_CMD_RESTART:
1146 		SCARG(&sra, opt) = RB_AUTOBOOT;
1147 		break;
1148 	case LINUX_REBOOT_CMD_HALT:
1149 		SCARG(&sra, opt) = RB_HALT;
1150 		break;
1151 	case LINUX_REBOOT_CMD_POWER_OFF:
1152 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1153 		break;
1154 	case LINUX_REBOOT_CMD_RESTART2:
1155 		/* Reboot with an argument. */
1156 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1157 		SCARG(&sra, bootstr) = SCARG(uap, arg);
1158 		break;
1159 	case LINUX_REBOOT_CMD_CAD_ON:
1160 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
1161 	case LINUX_REBOOT_CMD_CAD_OFF:
1162 		return(0);
1163 	default:
1164 		return(EINVAL);
1165 	}
1166 
1167 	return(sys_reboot(l, &sra, retval));
1168 }
1169 
1170 /*
1171  * Copy of compat_12_sys_swapon().
1172  */
1173 int
1174 linux_sys_swapon(struct lwp *l, const struct linux_sys_swapon_args *uap, register_t *retval)
1175 {
1176 	/* {
1177 		syscallarg(const char *) name;
1178 	} */
1179 	struct sys_swapctl_args ua;
1180 
1181 	SCARG(&ua, cmd) = SWAP_ON;
1182 	SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
1183 	SCARG(&ua, misc) = 0;	/* priority */
1184 	return (sys_swapctl(l, &ua, retval));
1185 }
1186 
1187 /*
1188  * Stop swapping to the file or block device specified by path.
1189  */
1190 int
1191 linux_sys_swapoff(struct lwp *l, const struct linux_sys_swapoff_args *uap, register_t *retval)
1192 {
1193 	/* {
1194 		syscallarg(const char *) path;
1195 	} */
1196 	struct sys_swapctl_args ua;
1197 
1198 	SCARG(&ua, cmd) = SWAP_OFF;
1199 	SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
1200 	return (sys_swapctl(l, &ua, retval));
1201 }
1202 
1203 /*
1204  * Copy of compat_09_sys_setdomainname()
1205  */
1206 /* ARGSUSED */
1207 int
1208 linux_sys_setdomainname(struct lwp *l, const struct linux_sys_setdomainname_args *uap, register_t *retval)
1209 {
1210 	/* {
1211 		syscallarg(char *) domainname;
1212 		syscallarg(int) len;
1213 	} */
1214 	int name[2];
1215 
1216 	name[0] = CTL_KERN;
1217 	name[1] = KERN_DOMAINNAME;
1218 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
1219 			    SCARG(uap, len), l));
1220 }
1221 
1222 /*
1223  * sysinfo()
1224  */
1225 /* ARGSUSED */
1226 int
1227 linux_sys_sysinfo(struct lwp *l, const struct linux_sys_sysinfo_args *uap, register_t *retval)
1228 {
1229 	/* {
1230 		syscallarg(struct linux_sysinfo *) arg;
1231 	} */
1232 	struct linux_sysinfo si;
1233 	struct loadavg *la;
1234 
1235 	si.uptime = time_uptime;
1236 	la = &averunnable;
1237 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1238 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1239 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1240 	si.totalram = ctob((u_long)physmem);
1241 	si.freeram = (u_long)uvmexp.free * uvmexp.pagesize;
1242 	si.sharedram = 0;	/* XXX */
1243 	si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
1244 	si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
1245 	si.freeswap =
1246 	    (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1247 	si.procs = nprocs;
1248 
1249 	/* The following are only present in newer Linux kernels. */
1250 	si.totalbig = 0;
1251 	si.freebig = 0;
1252 	si.mem_unit = 1;
1253 
1254 	return (copyout(&si, SCARG(uap, arg), sizeof si));
1255 }
1256 
1257 int
1258 linux_sys_getrlimit(struct lwp *l, const struct linux_sys_getrlimit_args *uap, register_t *retval)
1259 {
1260 	/* {
1261 		syscallarg(int) which;
1262 # ifdef LINUX_LARGEFILE64
1263 		syscallarg(struct rlimit *) rlp;
1264 # else
1265 		syscallarg(struct orlimit *) rlp;
1266 # endif
1267 	} */
1268 # ifdef LINUX_LARGEFILE64
1269 	struct rlimit orl;
1270 # else
1271 	struct orlimit orl;
1272 # endif
1273 	int which;
1274 
1275 	which = linux_to_bsd_limit(SCARG(uap, which));
1276 	if (which < 0)
1277 		return -which;
1278 
1279 	bsd_to_linux_rlimit(&orl, &l->l_proc->p_rlimit[which]);
1280 
1281 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1282 }
1283 
1284 int
1285 linux_sys_setrlimit(struct lwp *l, const struct linux_sys_setrlimit_args *uap, register_t *retval)
1286 {
1287 	/* {
1288 		syscallarg(int) which;
1289 # ifdef LINUX_LARGEFILE64
1290 		syscallarg(struct rlimit *) rlp;
1291 # else
1292 		syscallarg(struct orlimit *) rlp;
1293 # endif
1294 	} */
1295 	struct rlimit rl;
1296 # ifdef LINUX_LARGEFILE64
1297 	struct rlimit orl;
1298 # else
1299 	struct orlimit orl;
1300 # endif
1301 	int error;
1302 	int which;
1303 
1304 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1305 		return error;
1306 
1307 	which = linux_to_bsd_limit(SCARG(uap, which));
1308 	if (which < 0)
1309 		return -which;
1310 
1311 	linux_to_bsd_rlimit(&rl, &orl);
1312 	return dosetrlimit(l, l->l_proc, which, &rl);
1313 }
1314 
1315 # if !defined(__mips__) && !defined(__amd64__)
1316 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1317 int
1318 linux_sys_ugetrlimit(struct lwp *l, const struct linux_sys_ugetrlimit_args *uap, register_t *retval)
1319 {
1320 	return linux_sys_getrlimit(l, (const void *)uap, retval);
1321 }
1322 # endif
1323 
1324 /*
1325  * This gets called for unsupported syscalls. The difference to sys_nosys()
1326  * is that process does not get SIGSYS, the call just returns with ENOSYS.
1327  * This is the way Linux does it and glibc depends on this behaviour.
1328  */
1329 int
1330 linux_sys_nosys(struct lwp *l, const void *v, register_t *retval)
1331 {
1332 	return (ENOSYS);
1333 }
1334 
1335 int
1336 linux_sys_getpriority(struct lwp *l, const struct linux_sys_getpriority_args *uap, register_t *retval)
1337 {
1338         /* {
1339                 syscallarg(int) which;
1340                 syscallarg(int) who;
1341         } */
1342         struct sys_getpriority_args bsa;
1343         int error;
1344 
1345         SCARG(&bsa, which) = SCARG(uap, which);
1346         SCARG(&bsa, who) = SCARG(uap, who);
1347 
1348         if ((error = sys_getpriority(l, &bsa, retval)))
1349                 return error;
1350 
1351         *retval = NZERO - *retval;
1352 
1353         return 0;
1354 }
1355 
1356 #endif /* !COMPAT_LINUX32 */
1357