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