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