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