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