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