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