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