Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.183.12.1
      1 /*	$NetBSD: linux_misc.c,v 1.183.12.1 2007/10/25 22:36:57 bouyer 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.12.1 2007/10/25 22:36:57 bouyer 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 	strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    377 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    378 	strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
    379 	strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
    380 	strncpy(luts.l_machine, LINUX_UNAME_ARCH, sizeof(luts.l_machine));
    381 	strncpy(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, l)))
    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 int
   1036 linux_sys_setreuid16(l, v, retval)
   1037 	struct lwp *l;
   1038 	void *v;
   1039 	register_t *retval;
   1040 {
   1041 	struct linux_sys_setreuid16_args /* {
   1042 		syscallarg(int) ruid;
   1043 		syscallarg(int) euid;
   1044 	} */ *uap = v;
   1045 	struct sys_setreuid_args bsa;
   1046 
   1047 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
   1048 		(uid_t)-1 : SCARG(uap, ruid);
   1049 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
   1050 		(uid_t)-1 : SCARG(uap, euid);
   1051 
   1052 	return sys_setreuid(l, &bsa, retval);
   1053 }
   1054 
   1055 int
   1056 linux_sys_setregid16(l, v, retval)
   1057 	struct lwp *l;
   1058 	void *v;
   1059 	register_t *retval;
   1060 {
   1061 	struct linux_sys_setregid16_args /* {
   1062 		syscallarg(int) rgid;
   1063 		syscallarg(int) egid;
   1064 	} */ *uap = v;
   1065 	struct sys_setregid_args bsa;
   1066 
   1067 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
   1068 		(uid_t)-1 : SCARG(uap, rgid);
   1069 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1070 		(uid_t)-1 : SCARG(uap, egid);
   1071 
   1072 	return sys_setregid(l, &bsa, retval);
   1073 }
   1074 
   1075 int
   1076 linux_sys_setresuid16(l, v, retval)
   1077 	struct lwp *l;
   1078 	void *v;
   1079 	register_t *retval;
   1080 {
   1081 	struct linux_sys_setresuid16_args /* {
   1082 		syscallarg(uid_t) ruid;
   1083 		syscallarg(uid_t) euid;
   1084 		syscallarg(uid_t) suid;
   1085 	} */ *uap = v;
   1086 	struct linux_sys_setresuid16_args lsa;
   1087 
   1088 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
   1089 		(uid_t)-1 : SCARG(uap, ruid);
   1090 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
   1091 		(uid_t)-1 : SCARG(uap, euid);
   1092 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
   1093 		(uid_t)-1 : SCARG(uap, suid);
   1094 
   1095 	return linux_sys_setresuid(l, &lsa, retval);
   1096 }
   1097 
   1098 int
   1099 linux_sys_setresgid16(l, v, retval)
   1100 	struct lwp *l;
   1101 	void *v;
   1102 	register_t *retval;
   1103 {
   1104 	struct linux_sys_setresgid16_args /* {
   1105 		syscallarg(gid_t) rgid;
   1106 		syscallarg(gid_t) egid;
   1107 		syscallarg(gid_t) sgid;
   1108 	} */ *uap = v;
   1109 	struct linux_sys_setresgid16_args lsa;
   1110 
   1111 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
   1112 		(gid_t)-1 : SCARG(uap, rgid);
   1113 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1114 		(gid_t)-1 : SCARG(uap, egid);
   1115 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
   1116 		(gid_t)-1 : SCARG(uap, sgid);
   1117 
   1118 	return linux_sys_setresgid(l, &lsa, retval);
   1119 }
   1120 
   1121 int
   1122 linux_sys_getgroups16(l, v, retval)
   1123 	struct lwp *l;
   1124 	void *v;
   1125 	register_t *retval;
   1126 {
   1127 	struct linux_sys_getgroups16_args /* {
   1128 		syscallarg(int) gidsetsize;
   1129 		syscallarg(linux_gid_t *) gidset;
   1130 	} */ *uap = v;
   1131 	linux_gid_t lset[16];
   1132 	linux_gid_t *gidset;
   1133 	unsigned int ngrps;
   1134 	int i, n, j;
   1135 	int error;
   1136 
   1137 	ngrps = kauth_cred_ngroups(l->l_cred);
   1138 	*retval = ngrps;
   1139 	if (SCARG(uap, gidsetsize) == 0)
   1140 		return 0;
   1141 	if (SCARG(uap, gidsetsize) < ngrps)
   1142 		return EINVAL;
   1143 
   1144 	gidset = SCARG(uap, gidset);
   1145 	for (i = 0; i < (n = ngrps); i += n, gidset += n) {
   1146 		n -= i;
   1147 		if (n > __arraycount(lset))
   1148 			n = __arraycount(lset);
   1149 		for (j = 0; j < n; j++)
   1150 			lset[j] = kauth_cred_group(l->l_cred, i + j);
   1151 		error = copyout(lset, gidset, n * sizeof(lset[0]));
   1152 		if (error != 0)
   1153 			return error;
   1154 	}
   1155 
   1156 	return 0;
   1157 }
   1158 
   1159 /*
   1160  * It is very unlikly that any problem using 16bit groups is written
   1161  * to allow for more than 16 of them, so don't bother trying to
   1162  * support that.
   1163  */
   1164 #define COMPAT_NGROUPS16 16
   1165 
   1166 int
   1167 linux_sys_setgroups16(l, v, retval)
   1168 	struct lwp *l;
   1169 	void *v;
   1170 	register_t *retval;
   1171 {
   1172 	struct linux_sys_setgroups16_args /* {
   1173 		syscallarg(int) gidsetsize;
   1174 		syscallarg(linux_gid_t *) gidset;
   1175 	} */ *uap = v;
   1176 	linux_gid_t lset[COMPAT_NGROUPS16];
   1177 	kauth_cred_t ncred;
   1178 	int error;
   1179 	gid_t grbuf[COMPAT_NGROUPS16];
   1180 	unsigned int i, ngroups = SCARG(uap, gidsetsize);
   1181 
   1182 	if (ngroups > COMPAT_NGROUPS16)
   1183 		return EINVAL;
   1184 	error = copyin(SCARG(uap, gidset), lset, ngroups);
   1185 	if (error != 0)
   1186 		return error;
   1187 
   1188 	for (i = 0; i < ngroups; i++)
   1189 		grbuf[i] = lset[i];
   1190 
   1191 	ncred = kauth_cred_alloc();
   1192 	error = kauth_cred_setgroups(ncred, grbuf, SCARG(uap, gidsetsize),
   1193 	    -1, UIO_SYSSPACE);
   1194 	if (error != 0) {
   1195 		kauth_cred_free(ncred);
   1196 		return error;
   1197 	}
   1198 
   1199 	return kauth_proc_setgroups(l, ncred);
   1200 }
   1201 
   1202 #endif /* __i386__ || __m68k__ || COMPAT_LINUX32 */
   1203 
   1204 #ifndef COMPAT_LINUX32
   1205 /*
   1206  * We have nonexistent fsuid equal to uid.
   1207  * If modification is requested, refuse.
   1208  */
   1209 int
   1210 linux_sys_setfsuid(l, v, retval)
   1211 	 struct lwp *l;
   1212 	 void *v;
   1213 	 register_t *retval;
   1214 {
   1215 	 struct linux_sys_setfsuid_args /* {
   1216 		 syscallarg(uid_t) uid;
   1217 	 } */ *uap = v;
   1218 	 uid_t uid;
   1219 
   1220 	 uid = SCARG(uap, uid);
   1221 	 if (kauth_cred_getuid(l->l_cred) != uid)
   1222 		 return sys_nosys(l, v, retval);
   1223 	 else
   1224 		 return (0);
   1225 }
   1226 
   1227 /* XXX XXX XXX */
   1228 # ifndef alpha
   1229 int
   1230 linux_sys_getfsuid(l, v, retval)
   1231 	struct lwp *l;
   1232 	void *v;
   1233 	register_t *retval;
   1234 {
   1235 	return sys_getuid(l, v, retval);
   1236 }
   1237 # endif
   1238 
   1239 int
   1240 linux_sys_setresuid(struct lwp *l, void *v, register_t *retval)
   1241 {
   1242 	struct linux_sys_setresuid_args /* {
   1243 		syscallarg(uid_t) ruid;
   1244 		syscallarg(uid_t) euid;
   1245 		syscallarg(uid_t) suid;
   1246 	} */ *uap = v;
   1247 
   1248 	/*
   1249 	 * Note: These checks are a little different than the NetBSD
   1250 	 * setreuid(2) call performs.  This precisely follows the
   1251 	 * behavior of the Linux kernel.
   1252 	 */
   1253 
   1254 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
   1255 			    SCARG(uap, suid),
   1256 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
   1257 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
   1258 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
   1259 }
   1260 
   1261 int
   1262 linux_sys_getresuid(struct lwp *l, void *v, register_t *retval)
   1263 {
   1264 	struct linux_sys_getresuid_args /* {
   1265 		syscallarg(uid_t *) ruid;
   1266 		syscallarg(uid_t *) euid;
   1267 		syscallarg(uid_t *) suid;
   1268 	} */ *uap = v;
   1269 	kauth_cred_t pc = l->l_cred;
   1270 	int error;
   1271 	uid_t uid;
   1272 
   1273 	/*
   1274 	 * Linux copies these values out to userspace like so:
   1275 	 *
   1276 	 *	1. Copy out ruid.
   1277 	 *	2. If that succeeds, copy out euid.
   1278 	 *	3. If both of those succeed, copy out suid.
   1279 	 */
   1280 	uid = kauth_cred_getuid(pc);
   1281 	if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
   1282 		return (error);
   1283 
   1284 	uid = kauth_cred_geteuid(pc);
   1285 	if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
   1286 		return (error);
   1287 
   1288 	uid = kauth_cred_getsvuid(pc);
   1289 
   1290 	return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
   1291 }
   1292 
   1293 int
   1294 linux_sys_ptrace(l, v, retval)
   1295 	struct lwp *l;
   1296 	void *v;
   1297 	register_t *retval;
   1298 {
   1299 #if defined(PTRACE) || defined(_LKM)
   1300 	struct linux_sys_ptrace_args /* {
   1301 		i386, m68k, powerpc: T=int
   1302 		alpha, amd64: T=long
   1303 		syscallarg(T) request;
   1304 		syscallarg(T) pid;
   1305 		syscallarg(T) addr;
   1306 		syscallarg(T) data;
   1307 	} */ *uap = v;
   1308 	const int *ptr;
   1309 	int request;
   1310 	int error;
   1311 #ifdef _LKM
   1312 #define sys_ptrace (*sysent[SYS_ptrace].sy_call)
   1313 #endif
   1314 
   1315 	ptr = linux_ptrace_request_map;
   1316 	request = SCARG(uap, request);
   1317 	while (*ptr != -1)
   1318 		if (*ptr++ == request) {
   1319 			struct sys_ptrace_args pta;
   1320 
   1321 			SCARG(&pta, req) = *ptr;
   1322 			SCARG(&pta, pid) = SCARG(uap, pid);
   1323 			SCARG(&pta, addr) = (void *)SCARG(uap, addr);
   1324 			SCARG(&pta, data) = SCARG(uap, data);
   1325 
   1326 			/*
   1327 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1328 			 * to continue where the process left off previously.
   1329 			 * The same thing is achieved by addr == (void *) 1
   1330 			 * on NetBSD, so rewrite 'addr' appropriately.
   1331 			 */
   1332 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1333 				SCARG(&pta, addr) = (void *) 1;
   1334 
   1335 			error = sys_ptrace(l, &pta, retval);
   1336 			if (error)
   1337 				return error;
   1338 			switch (request) {
   1339 			case LINUX_PTRACE_PEEKTEXT:
   1340 			case LINUX_PTRACE_PEEKDATA:
   1341 				error = copyout (retval,
   1342 				    (void *)SCARG(uap, data),
   1343 				    sizeof *retval);
   1344 				*retval = SCARG(uap, data);
   1345 				break;
   1346 			default:
   1347 				break;
   1348 			}
   1349 			return error;
   1350 		}
   1351 		else
   1352 			ptr++;
   1353 
   1354 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1355 #else
   1356 	return ENOSYS;
   1357 #endif /* PTRACE || _LKM */
   1358 }
   1359 
   1360 int
   1361 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
   1362 {
   1363 	struct linux_sys_reboot_args /* {
   1364 		syscallarg(int) magic1;
   1365 		syscallarg(int) magic2;
   1366 		syscallarg(int) cmd;
   1367 		syscallarg(void *) arg;
   1368 	} */ *uap = v;
   1369 	struct sys_reboot_args /* {
   1370 		syscallarg(int) opt;
   1371 		syscallarg(char *) bootstr;
   1372 	} */ sra;
   1373 	int error;
   1374 
   1375 	if ((error = kauth_authorize_system(l->l_cred,
   1376 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
   1377 		return(error);
   1378 
   1379 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1380 		return(EINVAL);
   1381 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1382 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1383 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1384 		return(EINVAL);
   1385 
   1386 	switch (SCARG(uap, cmd)) {
   1387 	case LINUX_REBOOT_CMD_RESTART:
   1388 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1389 		break;
   1390 	case LINUX_REBOOT_CMD_HALT:
   1391 		SCARG(&sra, opt) = RB_HALT;
   1392 		break;
   1393 	case LINUX_REBOOT_CMD_POWER_OFF:
   1394 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1395 		break;
   1396 	case LINUX_REBOOT_CMD_RESTART2:
   1397 		/* Reboot with an argument. */
   1398 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1399 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1400 		break;
   1401 	case LINUX_REBOOT_CMD_CAD_ON:
   1402 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1403 	case LINUX_REBOOT_CMD_CAD_OFF:
   1404 		return(0);
   1405 	default:
   1406 		return(EINVAL);
   1407 	}
   1408 
   1409 	return(sys_reboot(l, &sra, retval));
   1410 }
   1411 
   1412 /*
   1413  * Copy of compat_12_sys_swapon().
   1414  */
   1415 int
   1416 linux_sys_swapon(l, v, retval)
   1417 	struct lwp *l;
   1418 	void *v;
   1419 	register_t *retval;
   1420 {
   1421 	struct sys_swapctl_args ua;
   1422 	struct linux_sys_swapon_args /* {
   1423 		syscallarg(const char *) name;
   1424 	} */ *uap = v;
   1425 
   1426 	SCARG(&ua, cmd) = SWAP_ON;
   1427 	SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
   1428 	SCARG(&ua, misc) = 0;	/* priority */
   1429 	return (sys_swapctl(l, &ua, retval));
   1430 }
   1431 
   1432 /*
   1433  * Stop swapping to the file or block device specified by path.
   1434  */
   1435 int
   1436 linux_sys_swapoff(l, v, retval)
   1437 	struct lwp *l;
   1438 	void *v;
   1439 	register_t *retval;
   1440 {
   1441 	struct sys_swapctl_args ua;
   1442 	struct linux_sys_swapoff_args /* {
   1443 		syscallarg(const char *) path;
   1444 	} */ *uap = v;
   1445 
   1446 	SCARG(&ua, cmd) = SWAP_OFF;
   1447 	SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
   1448 	return (sys_swapctl(l, &ua, retval));
   1449 }
   1450 
   1451 /*
   1452  * Copy of compat_09_sys_setdomainname()
   1453  */
   1454 /* ARGSUSED */
   1455 int
   1456 linux_sys_setdomainname(struct lwp *l, void *v, register_t *retval)
   1457 {
   1458 	struct linux_sys_setdomainname_args /* {
   1459 		syscallarg(char *) domainname;
   1460 		syscallarg(int) len;
   1461 	} */ *uap = v;
   1462 	int name[2];
   1463 
   1464 	name[0] = CTL_KERN;
   1465 	name[1] = KERN_DOMAINNAME;
   1466 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
   1467 			    SCARG(uap, len), l));
   1468 }
   1469 
   1470 /*
   1471  * sysinfo()
   1472  */
   1473 /* ARGSUSED */
   1474 int
   1475 linux_sys_sysinfo(struct lwp *l, void *v, register_t *retval)
   1476 {
   1477 	struct linux_sys_sysinfo_args /* {
   1478 		syscallarg(struct linux_sysinfo *) arg;
   1479 	} */ *uap = v;
   1480 	struct linux_sysinfo si;
   1481 	struct loadavg *la;
   1482 
   1483 	si.uptime = time_uptime;
   1484 	la = &averunnable;
   1485 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1486 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1487 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1488 	si.totalram = ctob((u_long)physmem);
   1489 	si.freeram = (u_long)uvmexp.free * uvmexp.pagesize;
   1490 	si.sharedram = 0;	/* XXX */
   1491 	si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
   1492 	si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
   1493 	si.freeswap =
   1494 	    (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1495 	si.procs = nprocs;
   1496 
   1497 	/* The following are only present in newer Linux kernels. */
   1498 	si.totalbig = 0;
   1499 	si.freebig = 0;
   1500 	si.mem_unit = 1;
   1501 
   1502 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1503 }
   1504 
   1505 int
   1506 linux_sys_getrlimit(l, v, retval)
   1507 	struct lwp *l;
   1508 	void *v;
   1509 	register_t *retval;
   1510 {
   1511 	struct linux_sys_getrlimit_args /* {
   1512 		syscallarg(int) which;
   1513 # ifdef LINUX_LARGEFILE64
   1514 		syscallarg(struct rlimit *) rlp;
   1515 # else
   1516 		syscallarg(struct orlimit *) rlp;
   1517 # endif
   1518 	} */ *uap = v;
   1519 # ifdef LINUX_LARGEFILE64
   1520 	struct rlimit orl;
   1521 # else
   1522 	struct orlimit orl;
   1523 # endif
   1524 	int which;
   1525 
   1526 	which = linux_to_bsd_limit(SCARG(uap, which));
   1527 	if (which < 0)
   1528 		return -which;
   1529 
   1530 	bsd_to_linux_rlimit(&orl, &l->l_proc->p_rlimit[which]);
   1531 
   1532 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
   1533 }
   1534 
   1535 int
   1536 linux_sys_setrlimit(l, v, retval)
   1537 	struct lwp *l;
   1538 	void *v;
   1539 	register_t *retval;
   1540 {
   1541 	struct linux_sys_setrlimit_args /* {
   1542 		syscallarg(int) which;
   1543 # ifdef LINUX_LARGEFILE64
   1544 		syscallarg(struct rlimit *) rlp;
   1545 # else
   1546 		syscallarg(struct orlimit *) rlp;
   1547 # endif
   1548 	} */ *uap = v;
   1549 	struct rlimit rl;
   1550 # ifdef LINUX_LARGEFILE64
   1551 	struct rlimit orl;
   1552 # else
   1553 	struct orlimit orl;
   1554 # endif
   1555 	int error;
   1556 	int which;
   1557 
   1558 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
   1559 		return error;
   1560 
   1561 	which = linux_to_bsd_limit(SCARG(uap, which));
   1562 	if (which < 0)
   1563 		return -which;
   1564 
   1565 	linux_to_bsd_rlimit(&rl, &orl);
   1566 	return dosetrlimit(l, l->l_proc, which, &rl);
   1567 }
   1568 
   1569 # if !defined(__mips__) && !defined(__amd64__)
   1570 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
   1571 int
   1572 linux_sys_ugetrlimit(l, v, retval)
   1573 	struct lwp *l;
   1574 	void *v;
   1575 	register_t *retval;
   1576 {
   1577 	return linux_sys_getrlimit(l, v, retval);
   1578 }
   1579 # endif
   1580 
   1581 /*
   1582  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1583  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1584  * This is the way Linux does it and glibc depends on this behaviour.
   1585  */
   1586 int
   1587 linux_sys_nosys(struct lwp *l, void *v,
   1588     register_t *retval)
   1589 {
   1590 	return (ENOSYS);
   1591 }
   1592 
   1593 int
   1594 linux_sys_getpriority(l, v, retval)
   1595         struct lwp *l;
   1596         void *v;
   1597         register_t *retval;
   1598 {
   1599         struct linux_sys_getpriority_args /* {
   1600                 syscallarg(int) which;
   1601                 syscallarg(int) who;
   1602         } */ *uap = v;
   1603         struct sys_getpriority_args bsa;
   1604         int error;
   1605 
   1606         SCARG(&bsa, which) = SCARG(uap, which);
   1607         SCARG(&bsa, who) = SCARG(uap, who);
   1608 
   1609         if ((error = sys_getpriority(l, &bsa, retval)))
   1610                 return error;
   1611 
   1612         *retval = NZERO - *retval;
   1613 
   1614         return 0;
   1615 }
   1616 
   1617 #endif /* !COMPAT_LINUX32 */
   1618