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