Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.215
      1 /*	$NetBSD: linux_misc.c,v 1.215 2010/06/24 13:03:07 hannken Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 1999, 2008 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Linux compatibility module. Try to deal with various Linux system calls.
     35  */
     36 
     37 /*
     38  * These functions have been moved to multiarch to allow
     39  * selection of which machines include them to be
     40  * determined by the individual files.linux_<arch> files.
     41  *
     42  * Function in multiarch:
     43  *	linux_sys_break			: linux_break.c
     44  *	linux_sys_alarm			: linux_misc_notalpha.c
     45  *	linux_sys_getresgid		: linux_misc_notalpha.c
     46  *	linux_sys_nice			: linux_misc_notalpha.c
     47  *	linux_sys_readdir		: linux_misc_notalpha.c
     48  *	linux_sys_setresgid		: linux_misc_notalpha.c
     49  *	linux_sys_time			: linux_misc_notalpha.c
     50  *	linux_sys_utime			: linux_misc_notalpha.c
     51  *	linux_sys_waitpid		: linux_misc_notalpha.c
     52  *	linux_sys_old_mmap		: linux_oldmmap.c
     53  *	linux_sys_oldolduname		: linux_oldolduname.c
     54  *	linux_sys_oldselect		: linux_oldselect.c
     55  *	linux_sys_olduname		: linux_olduname.c
     56  *	linux_sys_pipe			: linux_pipe.c
     57  */
     58 
     59 #include <sys/cdefs.h>
     60 __KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.215 2010/06/24 13:03:07 hannken Exp $");
     61 
     62 #include <sys/param.h>
     63 #include <sys/systm.h>
     64 #include <sys/namei.h>
     65 #include <sys/proc.h>
     66 #include <sys/dirent.h>
     67 #include <sys/file.h>
     68 #include <sys/stat.h>
     69 #include <sys/filedesc.h>
     70 #include <sys/ioctl.h>
     71 #include <sys/kernel.h>
     72 #include <sys/malloc.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/mman.h>
     75 #include <sys/mount.h>
     76 #include <sys/prot.h>
     77 #include <sys/reboot.h>
     78 #include <sys/resource.h>
     79 #include <sys/resourcevar.h>
     80 #include <sys/select.h>
     81 #include <sys/signal.h>
     82 #include <sys/signalvar.h>
     83 #include <sys/socket.h>
     84 #include <sys/time.h>
     85 #include <sys/times.h>
     86 #include <sys/vnode.h>
     87 #include <sys/uio.h>
     88 #include <sys/wait.h>
     89 #include <sys/utsname.h>
     90 #include <sys/unistd.h>
     91 #include <sys/vfs_syscalls.h>
     92 #include <sys/swap.h>		/* for SWAP_ON */
     93 #include <sys/sysctl.h>		/* for KERN_DOMAINNAME */
     94 #include <sys/kauth.h>
     95 
     96 #include <sys/ptrace.h>
     97 #include <machine/ptrace.h>
     98 
     99 #include <sys/syscall.h>
    100 #include <sys/syscallargs.h>
    101 
    102 #include <compat/sys/resource.h>
    103 
    104 #include <compat/linux/common/linux_machdep.h>
    105 #include <compat/linux/common/linux_types.h>
    106 #include <compat/linux/common/linux_signal.h>
    107 #include <compat/linux/common/linux_ipc.h>
    108 #include <compat/linux/common/linux_sem.h>
    109 
    110 #include <compat/linux/linux_syscallargs.h>
    111 
    112 #include <compat/linux/common/linux_fcntl.h>
    113 #include <compat/linux/common/linux_mmap.h>
    114 #include <compat/linux/common/linux_dirent.h>
    115 #include <compat/linux/common/linux_util.h>
    116 #include <compat/linux/common/linux_misc.h>
    117 #ifndef COMPAT_LINUX32
    118 #include <compat/linux/common/linux_statfs.h>
    119 #include <compat/linux/common/linux_limit.h>
    120 #endif
    121 #include <compat/linux/common/linux_ptrace.h>
    122 #include <compat/linux/common/linux_reboot.h>
    123 #include <compat/linux/common/linux_emuldata.h>
    124 
    125 #ifndef COMPAT_LINUX32
    126 const int linux_ptrace_request_map[] = {
    127 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
    128 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
    129 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
    130 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
    131 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
    132 	LINUX_PTRACE_CONT,	PT_CONTINUE,
    133 	LINUX_PTRACE_KILL,	PT_KILL,
    134 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
    135 	LINUX_PTRACE_DETACH,	PT_DETACH,
    136 # ifdef PT_STEP
    137 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
    138 # endif
    139 	LINUX_PTRACE_SYSCALL,	PT_SYSCALL,
    140 	-1
    141 };
    142 
    143 const struct linux_mnttypes linux_fstypes[] = {
    144 	{ MOUNT_FFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    145 	{ MOUNT_NFS,		LINUX_NFS_SUPER_MAGIC 		},
    146 	{ MOUNT_MFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    147 	{ MOUNT_MSDOS,		LINUX_MSDOS_SUPER_MAGIC		},
    148 	{ MOUNT_LFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    149 	{ MOUNT_FDESC,		LINUX_DEFAULT_SUPER_MAGIC	},
    150 	{ MOUNT_NULL,		LINUX_DEFAULT_SUPER_MAGIC	},
    151 	{ MOUNT_OVERLAY,	LINUX_DEFAULT_SUPER_MAGIC	},
    152 	{ MOUNT_UMAP,		LINUX_DEFAULT_SUPER_MAGIC	},
    153 	{ MOUNT_KERNFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    154 	{ MOUNT_PROCFS,		LINUX_PROC_SUPER_MAGIC		},
    155 	{ MOUNT_AFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    156 	{ MOUNT_CD9660,		LINUX_ISOFS_SUPER_MAGIC		},
    157 	{ MOUNT_UNION,		LINUX_DEFAULT_SUPER_MAGIC	},
    158 	{ MOUNT_ADOSFS,		LINUX_ADFS_SUPER_MAGIC		},
    159 	{ MOUNT_EXT2FS,		LINUX_EXT2_SUPER_MAGIC		},
    160 	{ MOUNT_CFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    161 	{ MOUNT_CODA,		LINUX_CODA_SUPER_MAGIC		},
    162 	{ MOUNT_FILECORE,	LINUX_DEFAULT_SUPER_MAGIC	},
    163 	{ MOUNT_NTFS,		LINUX_DEFAULT_SUPER_MAGIC	},
    164 	{ MOUNT_SMBFS,		LINUX_SMB_SUPER_MAGIC		},
    165 	{ MOUNT_PTYFS,		LINUX_DEVPTS_SUPER_MAGIC	},
    166 	{ MOUNT_TMPFS,		LINUX_TMPFS_SUPER_MAGIC		}
    167 };
    168 const int linux_fstypes_cnt = sizeof(linux_fstypes) / sizeof(linux_fstypes[0]);
    169 
    170 # ifdef DEBUG_LINUX
    171 #define DPRINTF(a)	uprintf a
    172 # else
    173 #define DPRINTF(a)
    174 # endif
    175 
    176 /* Local linux_misc.c functions: */
    177 static void linux_to_bsd_mmap_args(struct sys_mmap_args *,
    178     const struct linux_sys_mmap_args *);
    179 static int linux_mmap(struct lwp *, const struct linux_sys_mmap_args *,
    180     register_t *, off_t);
    181 
    182 
    183 /*
    184  * The information on a terminated (or stopped) process needs
    185  * to be converted in order for Linux binaries to get a valid signal
    186  * number out of it.
    187  */
    188 int
    189 bsd_to_linux_wstat(int st)
    190 {
    191 
    192 	int sig;
    193 
    194 	if (WIFSIGNALED(st)) {
    195 		sig = WTERMSIG(st);
    196 		if (sig >= 0 && sig < NSIG)
    197 			st= (st & ~0177) | native_to_linux_signo[sig];
    198 	} else if (WIFSTOPPED(st)) {
    199 		sig = WSTOPSIG(st);
    200 		if (sig >= 0 && sig < NSIG)
    201 			st = (st & ~0xff00) |
    202 			    (native_to_linux_signo[sig] << 8);
    203 	}
    204 	return st;
    205 }
    206 
    207 /*
    208  * wait4(2).  Passed on to the NetBSD call, surrounded by code to
    209  * reserve some space for a NetBSD-style wait status, and converting
    210  * it to what Linux wants.
    211  */
    212 int
    213 linux_sys_wait4(struct lwp *l, const struct linux_sys_wait4_args *uap, register_t *retval)
    214 {
    215 	/* {
    216 		syscallarg(int) pid;
    217 		syscallarg(int *) status;
    218 		syscallarg(int) options;
    219 		syscallarg(struct rusage50 *) rusage;
    220 	} */
    221 	int error, status, options, linux_options, pid = SCARG(uap, pid);
    222 	struct rusage50 ru50;
    223 	struct rusage ru;
    224 	proc_t *p;
    225 
    226 	linux_options = SCARG(uap, options);
    227 	options = WOPTSCHECKED;
    228 	if (linux_options & ~(LINUX_WAIT4_KNOWNFLAGS))
    229 		return (EINVAL);
    230 
    231 	if (linux_options & LINUX_WAIT4_WNOHANG)
    232 		options |= WNOHANG;
    233 	if (linux_options & LINUX_WAIT4_WUNTRACED)
    234 		options |= WUNTRACED;
    235 	if (linux_options & LINUX_WAIT4_WALL)
    236 		options |= WALLSIG;
    237 	if (linux_options & LINUX_WAIT4_WCLONE)
    238 		options |= WALTSIG;
    239 # ifdef DIAGNOSTIC
    240 	if (linux_options & LINUX_WAIT4_WNOTHREAD)
    241 		printf("WARNING: %s: linux process %d.%d called "
    242 		       "waitpid with __WNOTHREAD set!",
    243 		       __FILE__, l->l_proc->p_pid, l->l_lid);
    244 
    245 # endif
    246 
    247 	error = do_sys_wait(&pid, &status, options,
    248 	    SCARG(uap, rusage) != NULL ? &ru : NULL);
    249 
    250 	retval[0] = pid;
    251 	if (pid == 0)
    252 		return error;
    253 
    254 	p = curproc;
    255 	mutex_enter(p->p_lock);
    256 	sigdelset(&p->p_sigpend.sp_set, SIGCHLD); /* XXXAD ksiginfo leak */
    257 	mutex_exit(p->p_lock);
    258 
    259 	if (SCARG(uap, rusage) != NULL) {
    260 		rusage_to_rusage50(&ru, &ru50);
    261 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
    262 	}
    263 
    264 	if (error == 0 && SCARG(uap, status) != NULL) {
    265 		status = bsd_to_linux_wstat(status);
    266 		error = copyout(&status, SCARG(uap, status), sizeof status);
    267 	}
    268 
    269 	return error;
    270 }
    271 
    272 /*
    273  * Linux brk(2). The check if the new address is >= the old one is
    274  * done in the kernel in Linux. NetBSD does it in the library.
    275  */
    276 int
    277 linux_sys_brk(struct lwp *l, const struct linux_sys_brk_args *uap, register_t *retval)
    278 {
    279 	/* {
    280 		syscallarg(char *) nsize;
    281 	} */
    282 	struct proc *p = l->l_proc;
    283 	char *nbrk = SCARG(uap, nsize);
    284 	struct sys_obreak_args oba;
    285 	struct vmspace *vm = p->p_vmspace;
    286 	struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
    287 
    288 	SCARG(&oba, nsize) = nbrk;
    289 
    290 	if ((void *) nbrk > vm->vm_daddr && sys_obreak(l, &oba, retval) == 0)
    291 		ed->s->p_break = (char*)nbrk;
    292 	else
    293 		nbrk = ed->s->p_break;
    294 
    295 	retval[0] = (register_t)nbrk;
    296 
    297 	return 0;
    298 }
    299 
    300 /*
    301  * Implement the fs stat functions. Straightforward.
    302  */
    303 int
    304 linux_sys_statfs(struct lwp *l, const struct linux_sys_statfs_args *uap, register_t *retval)
    305 {
    306 	/* {
    307 		syscallarg(const char *) path;
    308 		syscallarg(struct linux_statfs *) sp;
    309 	} */
    310 	struct statvfs *sb;
    311 	struct linux_statfs ltmp;
    312 	int error;
    313 
    314 	sb = STATVFSBUF_GET();
    315 	error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb);
    316 	if (error == 0) {
    317 		bsd_to_linux_statfs(sb, &ltmp);
    318 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    319 	}
    320 	STATVFSBUF_PUT(sb);
    321 
    322 	return error;
    323 }
    324 
    325 int
    326 linux_sys_fstatfs(struct lwp *l, const struct linux_sys_fstatfs_args *uap, register_t *retval)
    327 {
    328 	/* {
    329 		syscallarg(int) fd;
    330 		syscallarg(struct linux_statfs *) sp;
    331 	} */
    332 	struct statvfs *sb;
    333 	struct linux_statfs ltmp;
    334 	int error;
    335 
    336 	sb = STATVFSBUF_GET();
    337 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
    338 	if (error == 0) {
    339 		bsd_to_linux_statfs(sb, &ltmp);
    340 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    341 	}
    342 	STATVFSBUF_PUT(sb);
    343 
    344 	return error;
    345 }
    346 
    347 /*
    348  * uname(). Just copy the info from the various strings stored in the
    349  * kernel, and put it in the Linux utsname structure. That structure
    350  * is almost the same as the NetBSD one, only it has fields 65 characters
    351  * long, and an extra domainname field.
    352  */
    353 int
    354 linux_sys_uname(struct lwp *l, const struct linux_sys_uname_args *uap, register_t *retval)
    355 {
    356 	/* {
    357 		syscallarg(struct linux_utsname *) up;
    358 	} */
    359 	struct linux_utsname luts;
    360 
    361 	strlcpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    362 	strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    363 	strlcpy(luts.l_release, linux_release, sizeof(luts.l_release));
    364 	strlcpy(luts.l_version, linux_version, sizeof(luts.l_version));
    365 	strlcpy(luts.l_machine, LINUX_UNAME_ARCH, sizeof(luts.l_machine));
    366 	strlcpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    367 
    368 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    369 }
    370 
    371 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
    372 /* Used indirectly on: arm, i386, m68k */
    373 
    374 /*
    375  * New type Linux mmap call.
    376  * Only called directly on machines with >= 6 free regs.
    377  */
    378 int
    379 linux_sys_mmap(struct lwp *l, const struct linux_sys_mmap_args *uap, register_t *retval)
    380 {
    381 	/* {
    382 		syscallarg(unsigned long) addr;
    383 		syscallarg(size_t) len;
    384 		syscallarg(int) prot;
    385 		syscallarg(int) flags;
    386 		syscallarg(int) fd;
    387 		syscallarg(linux_off_t) offset;
    388 	} */
    389 
    390 	if (SCARG(uap, offset) & PAGE_MASK)
    391 		return EINVAL;
    392 
    393 	return linux_mmap(l, uap, retval, SCARG(uap, offset));
    394 }
    395 
    396 /*
    397  * Guts of most architectures' mmap64() implementations.  This shares
    398  * its list of arguments with linux_sys_mmap().
    399  *
    400  * The difference in linux_sys_mmap2() is that "offset" is actually
    401  * (offset / pagesize), not an absolute byte count.  This translation
    402  * to pagesize offsets is done inside glibc between the mmap64() call
    403  * point, and the actual syscall.
    404  */
    405 int
    406 linux_sys_mmap2(struct lwp *l, const struct linux_sys_mmap2_args *uap, register_t *retval)
    407 {
    408 	/* {
    409 		syscallarg(unsigned long) addr;
    410 		syscallarg(size_t) len;
    411 		syscallarg(int) prot;
    412 		syscallarg(int) flags;
    413 		syscallarg(int) fd;
    414 		syscallarg(linux_off_t) offset;
    415 	} */
    416 
    417 	return linux_mmap(l, uap, retval,
    418 	    ((off_t)SCARG(uap, offset)) << PAGE_SHIFT);
    419 }
    420 
    421 /*
    422  * Massage arguments and call system mmap(2).
    423  */
    424 static int
    425 linux_mmap(struct lwp *l, const struct linux_sys_mmap_args *uap, register_t *retval, off_t offset)
    426 {
    427 	struct sys_mmap_args cma;
    428 	int error;
    429 	size_t mmoff=0;
    430 
    431 	linux_to_bsd_mmap_args(&cma, uap);
    432 	SCARG(&cma, pos) = offset;
    433 
    434 	if (SCARG(uap, flags) & LINUX_MAP_GROWSDOWN) {
    435 		/*
    436 		 * Request for stack-like memory segment. On linux, this
    437 		 * works by mmap()ping (small) segment, which is automatically
    438 		 * extended when page fault happens below the currently
    439 		 * allocated area. We emulate this by allocating (typically
    440 		 * bigger) segment sized at current stack size limit, and
    441 		 * offsetting the requested and returned address accordingly.
    442 		 * Since physical pages are only allocated on-demand, this
    443 		 * is effectively identical.
    444 		 */
    445 		rlim_t ssl = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
    446 
    447 		if (SCARG(&cma, len) < ssl) {
    448 			/* Compute the address offset */
    449 			mmoff = round_page(ssl) - SCARG(uap, len);
    450 
    451 			if (SCARG(&cma, addr))
    452 				SCARG(&cma, addr) = (char *)SCARG(&cma, addr) - mmoff;
    453 
    454 			SCARG(&cma, len) = (size_t) ssl;
    455 		}
    456 	}
    457 
    458 	error = sys_mmap(l, &cma, retval);
    459 	if (error)
    460 		return (error);
    461 
    462 	/* Shift the returned address for stack-like segment if necessary */
    463 	retval[0] += mmoff;
    464 
    465 	return (0);
    466 }
    467 
    468 static void
    469 linux_to_bsd_mmap_args(struct sys_mmap_args *cma, const struct linux_sys_mmap_args *uap)
    470 {
    471 	int flags = MAP_TRYFIXED, fl = SCARG(uap, flags);
    472 
    473 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
    474 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
    475 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
    476 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
    477 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
    478 
    479 	SCARG(cma, addr) = (void *)SCARG(uap, addr);
    480 	SCARG(cma, len) = SCARG(uap, len);
    481 	SCARG(cma, prot) = SCARG(uap, prot);
    482 	if (SCARG(cma, prot) & VM_PROT_WRITE) /* XXX */
    483 		SCARG(cma, prot) |= VM_PROT_READ;
    484 	SCARG(cma, flags) = flags;
    485 	SCARG(cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
    486 	SCARG(cma, PAD) = 0;
    487 }
    488 
    489 #define	LINUX_MREMAP_MAYMOVE	1
    490 #define	LINUX_MREMAP_FIXED	2
    491 
    492 int
    493 linux_sys_mremap(struct lwp *l, const struct linux_sys_mremap_args *uap, register_t *retval)
    494 {
    495 	/* {
    496 		syscallarg(void *) old_address;
    497 		syscallarg(size_t) old_size;
    498 		syscallarg(size_t) new_size;
    499 		syscallarg(u_long) flags;
    500 	} */
    501 
    502 	struct proc *p;
    503 	struct vm_map *map;
    504 	vaddr_t oldva;
    505 	vaddr_t newva;
    506 	size_t oldsize;
    507 	size_t newsize;
    508 	int flags;
    509 	int uvmflags;
    510 	int error;
    511 
    512 	flags = SCARG(uap, flags);
    513 	oldva = (vaddr_t)SCARG(uap, old_address);
    514 	oldsize = round_page(SCARG(uap, old_size));
    515 	newsize = round_page(SCARG(uap, new_size));
    516 	if ((flags & ~(LINUX_MREMAP_FIXED|LINUX_MREMAP_MAYMOVE)) != 0) {
    517 		error = EINVAL;
    518 		goto done;
    519 	}
    520 	if ((flags & LINUX_MREMAP_FIXED) != 0) {
    521 		if ((flags & LINUX_MREMAP_MAYMOVE) == 0) {
    522 			error = EINVAL;
    523 			goto done;
    524 		}
    525 #if 0 /* notyet */
    526 		newva = SCARG(uap, new_address);
    527 		uvmflags = MAP_FIXED;
    528 #else /* notyet */
    529 		error = EOPNOTSUPP;
    530 		goto done;
    531 #endif /* notyet */
    532 	} else if ((flags & LINUX_MREMAP_MAYMOVE) != 0) {
    533 		uvmflags = 0;
    534 	} else {
    535 		newva = oldva;
    536 		uvmflags = MAP_FIXED;
    537 	}
    538 	p = l->l_proc;
    539 	map = &p->p_vmspace->vm_map;
    540 	error = uvm_mremap(map, oldva, oldsize, map, &newva, newsize, p,
    541 	    uvmflags);
    542 
    543 done:
    544 	*retval = (error != 0) ? 0 : (register_t)newva;
    545 	return error;
    546 }
    547 
    548 int
    549 linux_sys_mprotect(struct lwp *l, const struct linux_sys_mprotect_args *uap, register_t *retval)
    550 {
    551 	/* {
    552 		syscallarg(const void *) start;
    553 		syscallarg(unsigned long) len;
    554 		syscallarg(int) prot;
    555 	} */
    556 	struct vm_map_entry *entry;
    557 	struct vm_map *map;
    558 	struct proc *p;
    559 	vaddr_t end, start, len, stacklim;
    560 	int prot, grows;
    561 
    562 	start = (vaddr_t)SCARG(uap, start);
    563 	len = round_page(SCARG(uap, len));
    564 	prot = SCARG(uap, prot);
    565 	grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
    566 	prot &= ~grows;
    567 	end = start + len;
    568 
    569 	if (start & PAGE_MASK)
    570 		return EINVAL;
    571 	if (end < start)
    572 		return EINVAL;
    573 	if (end == start)
    574 		return 0;
    575 
    576 	if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
    577 		return EINVAL;
    578 	if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
    579 		return EINVAL;
    580 
    581 	p = l->l_proc;
    582 	map = &p->p_vmspace->vm_map;
    583 	vm_map_lock(map);
    584 # ifdef notdef
    585 	VM_MAP_RANGE_CHECK(map, start, end);
    586 # endif
    587 	if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
    588 		vm_map_unlock(map);
    589 		return ENOMEM;
    590 	}
    591 
    592 	/*
    593 	 * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
    594 	 */
    595 
    596 	stacklim = (vaddr_t)p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
    597 	if (grows & LINUX_PROT_GROWSDOWN) {
    598 		if (USRSTACK - stacklim <= start && start < USRSTACK) {
    599 			start = USRSTACK - stacklim;
    600 		} else {
    601 			start = entry->start;
    602 		}
    603 	} else if (grows & LINUX_PROT_GROWSUP) {
    604 		if (USRSTACK <= end && end < USRSTACK + stacklim) {
    605 			end = USRSTACK + stacklim;
    606 		} else {
    607 			end = entry->end;
    608 		}
    609 	}
    610 	vm_map_unlock(map);
    611 	return uvm_map_protect(map, start, end, prot, FALSE);
    612 }
    613 
    614 /*
    615  * This code is partly stolen from src/lib/libc/compat-43/times.c
    616  */
    617 
    618 #define	CONVTCK(r)	(r.tv_sec * hz + r.tv_usec / (1000000 / hz))
    619 
    620 int
    621 linux_sys_times(struct lwp *l, const struct linux_sys_times_args *uap, register_t *retval)
    622 {
    623 	/* {
    624 		syscallarg(struct times *) tms;
    625 	} */
    626 	struct proc *p = l->l_proc;
    627 	struct timeval t;
    628 	int error;
    629 
    630 	if (SCARG(uap, tms)) {
    631 		struct linux_tms ltms;
    632 		struct rusage ru;
    633 
    634 		mutex_enter(p->p_lock);
    635 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
    636 		ltms.ltms_utime = CONVTCK(ru.ru_utime);
    637 		ltms.ltms_stime = CONVTCK(ru.ru_stime);
    638 		ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    639 		ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    640 		mutex_exit(p->p_lock);
    641 
    642 		if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    643 			return error;
    644 	}
    645 
    646 	getmicrouptime(&t);
    647 
    648 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    649 	return 0;
    650 }
    651 
    652 #undef CONVTCK
    653 
    654 /*
    655  * Linux 'readdir' call. This code is mostly taken from the
    656  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    657  * an attempt has been made to keep it a little cleaner (failing
    658  * miserably, because of the cruft needed if count 1 is passed).
    659  *
    660  * The d_off field should contain the offset of the next valid entry,
    661  * but in Linux it has the offset of the entry itself. We emulate
    662  * that bug here.
    663  *
    664  * Read in BSD-style entries, convert them, and copy them out.
    665  *
    666  * Note that this doesn't handle union-mounted filesystems.
    667  */
    668 int
    669 linux_sys_getdents(struct lwp *l, const struct linux_sys_getdents_args *uap, register_t *retval)
    670 {
    671 	/* {
    672 		syscallarg(int) fd;
    673 		syscallarg(struct linux_dirent *) dent;
    674 		syscallarg(unsigned int) count;
    675 	} */
    676 	struct dirent *bdp;
    677 	struct vnode *vp;
    678 	char *inp, *tbuf;		/* BSD-format */
    679 	int len, reclen;		/* BSD-format */
    680 	char *outp;			/* Linux-format */
    681 	int resid, linux_reclen = 0;	/* Linux-format */
    682 	struct file *fp;
    683 	struct uio auio;
    684 	struct iovec aiov;
    685 	struct linux_dirent idb;
    686 	off_t off;		/* true file offset */
    687 	int buflen, error, eofflag, nbytes, oldcall;
    688 	struct vattr va;
    689 	off_t *cookiebuf = NULL, *cookie;
    690 	int ncookies;
    691 
    692 	/* fd_getvnode() will use the descriptor for us */
    693 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    694 		return (error);
    695 
    696 	if ((fp->f_flag & FREAD) == 0) {
    697 		error = EBADF;
    698 		goto out1;
    699 	}
    700 
    701 	vp = (struct vnode *)fp->f_data;
    702 	if (vp->v_type != VDIR) {
    703 		error = ENOTDIR;
    704 		goto out1;
    705 	}
    706 
    707 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
    708 		goto out1;
    709 
    710 	nbytes = SCARG(uap, count);
    711 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    712 		nbytes = sizeof (idb);
    713 		buflen = max(va.va_blocksize, nbytes);
    714 		oldcall = 1;
    715 	} else {
    716 		buflen = min(MAXBSIZE, nbytes);
    717 		if (buflen < va.va_blocksize)
    718 			buflen = va.va_blocksize;
    719 		oldcall = 0;
    720 	}
    721 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    722 
    723 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    724 	off = fp->f_offset;
    725 again:
    726 	aiov.iov_base = tbuf;
    727 	aiov.iov_len = buflen;
    728 	auio.uio_iov = &aiov;
    729 	auio.uio_iovcnt = 1;
    730 	auio.uio_rw = UIO_READ;
    731 	auio.uio_resid = buflen;
    732 	auio.uio_offset = off;
    733 	UIO_SETUP_SYSSPACE(&auio);
    734 	/*
    735          * First we read into the malloc'ed buffer, then
    736          * we massage it into user space, one record at a time.
    737          */
    738 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    739 	    &ncookies);
    740 	if (error)
    741 		goto out;
    742 
    743 	inp = tbuf;
    744 	outp = (void *)SCARG(uap, dent);
    745 	resid = nbytes;
    746 	if ((len = buflen - auio.uio_resid) == 0)
    747 		goto eof;
    748 
    749 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    750 		bdp = (struct dirent *)inp;
    751 		reclen = bdp->d_reclen;
    752 		if (reclen & 3)
    753 			panic("linux_readdir");
    754 		if (bdp->d_fileno == 0) {
    755 			inp += reclen;	/* it is a hole; squish it out */
    756 			if (cookie)
    757 				off = *cookie++;
    758 			else
    759 				off += reclen;
    760 			continue;
    761 		}
    762 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    763 		if (reclen > len || resid < linux_reclen) {
    764 			/* entry too big for buffer, so just stop */
    765 			outp++;
    766 			break;
    767 		}
    768 		/*
    769 		 * Massage in place to make a Linux-shaped dirent (otherwise
    770 		 * we have to worry about touching user memory outside of
    771 		 * the copyout() call).
    772 		 */
    773 		idb.d_ino = bdp->d_fileno;
    774 		/*
    775 		 * The old readdir() call misuses the offset and reclen fields.
    776 		 */
    777 		if (oldcall) {
    778 			idb.d_off = (linux_off_t)linux_reclen;
    779 			idb.d_reclen = (u_short)bdp->d_namlen;
    780 		} else {
    781 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
    782 				compat_offseterr(vp, "linux_getdents");
    783 				error = EINVAL;
    784 				goto out;
    785 			}
    786 			idb.d_off = (linux_off_t)off;
    787 			idb.d_reclen = (u_short)linux_reclen;
    788 		}
    789 		strcpy(idb.d_name, bdp->d_name);
    790 		if ((error = copyout((void *)&idb, outp, linux_reclen)))
    791 			goto out;
    792 		/* advance past this real entry */
    793 		inp += reclen;
    794 		if (cookie)
    795 			off = *cookie++; /* each entry points to itself */
    796 		else
    797 			off += reclen;
    798 		/* advance output past Linux-shaped entry */
    799 		outp += linux_reclen;
    800 		resid -= linux_reclen;
    801 		if (oldcall)
    802 			break;
    803 	}
    804 
    805 	/* if we squished out the whole block, try again */
    806 	if (outp == (void *)SCARG(uap, dent)) {
    807 		if (cookiebuf)
    808 			free(cookiebuf, M_TEMP);
    809 		cookiebuf = NULL;
    810 		goto again;
    811 	}
    812 	fp->f_offset = off;	/* update the vnode offset */
    813 
    814 	if (oldcall)
    815 		nbytes = resid + linux_reclen;
    816 
    817 eof:
    818 	*retval = nbytes - resid;
    819 out:
    820 	VOP_UNLOCK(vp);
    821 	if (cookiebuf)
    822 		free(cookiebuf, M_TEMP);
    823 	free(tbuf, M_TEMP);
    824 out1:
    825 	fd_putfile(SCARG(uap, fd));
    826 	return error;
    827 }
    828 
    829 /*
    830  * Even when just using registers to pass arguments to syscalls you can
    831  * have 5 of them on the i386. So this newer version of select() does
    832  * this.
    833  */
    834 int
    835 linux_sys_select(struct lwp *l, const struct linux_sys_select_args *uap, register_t *retval)
    836 {
    837 	/* {
    838 		syscallarg(int) nfds;
    839 		syscallarg(fd_set *) readfds;
    840 		syscallarg(fd_set *) writefds;
    841 		syscallarg(fd_set *) exceptfds;
    842 		syscallarg(struct timeval50 *) timeout;
    843 	} */
    844 
    845 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    846 	    SCARG(uap, writefds), SCARG(uap, exceptfds),
    847 	    (struct linux_timeval *)SCARG(uap, timeout));
    848 }
    849 
    850 /*
    851  * Common code for the old and new versions of select(). A couple of
    852  * things are important:
    853  * 1) return the amount of time left in the 'timeout' parameter
    854  * 2) select never returns ERESTART on Linux, always return EINTR
    855  */
    856 int
    857 linux_select1(struct lwp *l, register_t *retval, int nfds, fd_set *readfds,
    858     fd_set *writefds, fd_set *exceptfds, struct linux_timeval *timeout)
    859 {
    860 	struct timespec ts0, ts1, uts, *ts = NULL;
    861 	struct linux_timeval ltv;
    862 	int error;
    863 
    864 	/*
    865 	 * Store current time for computation of the amount of
    866 	 * time left.
    867 	 */
    868 	if (timeout) {
    869 		if ((error = copyin(timeout, &ltv, sizeof(ltv))))
    870 			return error;
    871 		uts.tv_sec = ltv.tv_sec;
    872 		uts.tv_nsec = ltv.tv_usec * 1000;
    873 		if (itimespecfix(&uts)) {
    874 			/*
    875 			 * The timeval was invalid.  Convert it to something
    876 			 * valid that will act as it does under Linux.
    877 			 */
    878 			uts.tv_sec += uts.tv_nsec / 1000000000;
    879 			uts.tv_nsec %= 1000000000;
    880 			if (uts.tv_nsec < 0) {
    881 				uts.tv_sec -= 1;
    882 				uts.tv_nsec += 1000000000;
    883 			}
    884 			if (uts.tv_sec < 0)
    885 				timespecclear(&uts);
    886 		}
    887 		ts = &uts;
    888 		nanotime(&ts0);
    889 	}
    890 
    891 	error = selcommon(retval, nfds, readfds, writefds, exceptfds, ts, NULL);
    892 
    893 	if (error) {
    894 		/*
    895 		 * See fs/select.c in the Linux kernel.  Without this,
    896 		 * Maelstrom doesn't work.
    897 		 */
    898 		if (error == ERESTART)
    899 			error = EINTR;
    900 		return error;
    901 	}
    902 
    903 	if (timeout) {
    904 		if (*retval) {
    905 			/*
    906 			 * Compute how much time was left of the timeout,
    907 			 * by subtracting the current time and the time
    908 			 * before we started the call, and subtracting
    909 			 * that result from the user-supplied value.
    910 			 */
    911 			nanotime(&ts1);
    912 			timespecsub(&ts1, &ts0, &ts1);
    913 			timespecsub(&uts, &ts1, &uts);
    914 			if (uts.tv_sec < 0)
    915 				timespecclear(&uts);
    916 		} else
    917 			timespecclear(&uts);
    918 		ltv.tv_sec = uts.tv_sec;
    919 		ltv.tv_usec = uts.tv_nsec / 1000;
    920 		if ((error = copyout(&ltv, timeout, sizeof(ltv))))
    921 			return error;
    922 	}
    923 
    924 	return 0;
    925 }
    926 
    927 /*
    928  * Set the 'personality' (emulation mode) for the current process. Only
    929  * accept the Linux personality here (0). This call is needed because
    930  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
    931  * ELF binaries run in Linux mode, not SVR4 mode.
    932  */
    933 int
    934 linux_sys_personality(struct lwp *l, const struct linux_sys_personality_args *uap, register_t *retval)
    935 {
    936 	/* {
    937 		syscallarg(unsigned long) per;
    938 	} */
    939 
    940 	switch (SCARG(uap, per)) {
    941 	case LINUX_PER_LINUX:
    942 	case LINUX_PER_QUERY:
    943 		break;
    944 	default:
    945 		return EINVAL;
    946 	}
    947 
    948 	retval[0] = LINUX_PER_LINUX;
    949 	return 0;
    950 }
    951 
    952 /*
    953  * We have nonexistent fsuid equal to uid.
    954  * If modification is requested, refuse.
    955  */
    956 int
    957 linux_sys_setfsuid(struct lwp *l, const struct linux_sys_setfsuid_args *uap, register_t *retval)
    958 {
    959 	 /* {
    960 		 syscallarg(uid_t) uid;
    961 	 } */
    962 	 uid_t uid;
    963 
    964 	 uid = SCARG(uap, uid);
    965 	 if (kauth_cred_getuid(l->l_cred) != uid)
    966 		 return sys_nosys(l, uap, retval);
    967 
    968 	 *retval = uid;
    969 	 return 0;
    970 }
    971 
    972 int
    973 linux_sys_setfsgid(struct lwp *l, const struct linux_sys_setfsgid_args *uap, register_t *retval)
    974 {
    975 	/* {
    976 		syscallarg(gid_t) gid;
    977 	} */
    978 	gid_t gid;
    979 
    980 	gid = SCARG(uap, gid);
    981 	if (kauth_cred_getgid(l->l_cred) != gid)
    982 		return sys_nosys(l, uap, retval);
    983 
    984 	*retval = gid;
    985 	return 0;
    986 }
    987 
    988 int
    989 linux_sys_setresuid(struct lwp *l, const struct linux_sys_setresuid_args *uap, register_t *retval)
    990 {
    991 	/* {
    992 		syscallarg(uid_t) ruid;
    993 		syscallarg(uid_t) euid;
    994 		syscallarg(uid_t) suid;
    995 	} */
    996 
    997 	/*
    998 	 * Note: These checks are a little different than the NetBSD
    999 	 * setreuid(2) call performs.  This precisely follows the
   1000 	 * behavior of the Linux kernel.
   1001 	 */
   1002 
   1003 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
   1004 			    SCARG(uap, suid),
   1005 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
   1006 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
   1007 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
   1008 }
   1009 
   1010 int
   1011 linux_sys_getresuid(struct lwp *l, const struct linux_sys_getresuid_args *uap, register_t *retval)
   1012 {
   1013 	/* {
   1014 		syscallarg(uid_t *) ruid;
   1015 		syscallarg(uid_t *) euid;
   1016 		syscallarg(uid_t *) suid;
   1017 	} */
   1018 	kauth_cred_t pc = l->l_cred;
   1019 	int error;
   1020 	uid_t uid;
   1021 
   1022 	/*
   1023 	 * Linux copies these values out to userspace like so:
   1024 	 *
   1025 	 *	1. Copy out ruid.
   1026 	 *	2. If that succeeds, copy out euid.
   1027 	 *	3. If both of those succeed, copy out suid.
   1028 	 */
   1029 	uid = kauth_cred_getuid(pc);
   1030 	if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
   1031 		return (error);
   1032 
   1033 	uid = kauth_cred_geteuid(pc);
   1034 	if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
   1035 		return (error);
   1036 
   1037 	uid = kauth_cred_getsvuid(pc);
   1038 
   1039 	return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
   1040 }
   1041 
   1042 int
   1043 linux_sys_ptrace(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
   1044 {
   1045 	/* {
   1046 		i386, m68k, powerpc: T=int
   1047 		alpha, amd64: T=long
   1048 		syscallarg(T) request;
   1049 		syscallarg(T) pid;
   1050 		syscallarg(T) addr;
   1051 		syscallarg(T) data;
   1052 	} */
   1053 	const int *ptr;
   1054 	int request;
   1055 	int error;
   1056 
   1057 	ptr = linux_ptrace_request_map;
   1058 	request = SCARG(uap, request);
   1059 	while (*ptr != -1)
   1060 		if (*ptr++ == request) {
   1061 			struct sys_ptrace_args pta;
   1062 
   1063 			SCARG(&pta, req) = *ptr;
   1064 			SCARG(&pta, pid) = SCARG(uap, pid);
   1065 			SCARG(&pta, addr) = (void *)SCARG(uap, addr);
   1066 			SCARG(&pta, data) = SCARG(uap, data);
   1067 
   1068 			/*
   1069 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1070 			 * to continue where the process left off previously.
   1071  			 * The same thing is achieved by addr == (void *) 1
   1072 			 * on NetBSD, so rewrite 'addr' appropriately.
   1073 			 */
   1074 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1075 				SCARG(&pta, addr) = (void *) 1;
   1076 
   1077 			error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
   1078 			if (error)
   1079 				return error;
   1080 			switch (request) {
   1081 			case LINUX_PTRACE_PEEKTEXT:
   1082 			case LINUX_PTRACE_PEEKDATA:
   1083 				error = copyout (retval,
   1084 				    (void *)SCARG(uap, data),
   1085 				    sizeof *retval);
   1086 				*retval = SCARG(uap, data);
   1087 				break;
   1088 			default:
   1089 				break;
   1090 			}
   1091 			return error;
   1092 		}
   1093 		else
   1094 			ptr++;
   1095 
   1096 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1097 }
   1098 
   1099 int
   1100 linux_sys_reboot(struct lwp *l, const struct linux_sys_reboot_args *uap, register_t *retval)
   1101 {
   1102 	/* {
   1103 		syscallarg(int) magic1;
   1104 		syscallarg(int) magic2;
   1105 		syscallarg(int) cmd;
   1106 		syscallarg(void *) arg;
   1107 	} */
   1108 	struct sys_reboot_args /* {
   1109 		syscallarg(int) opt;
   1110 		syscallarg(char *) bootstr;
   1111 	} */ sra;
   1112 	int error;
   1113 
   1114 	if ((error = kauth_authorize_system(l->l_cred,
   1115 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
   1116 		return(error);
   1117 
   1118 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1119 		return(EINVAL);
   1120 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1121 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1122 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1123 		return(EINVAL);
   1124 
   1125 	switch ((unsigned long)SCARG(uap, cmd)) {
   1126 	case LINUX_REBOOT_CMD_RESTART:
   1127 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1128 		break;
   1129 	case LINUX_REBOOT_CMD_HALT:
   1130 		SCARG(&sra, opt) = RB_HALT;
   1131 		break;
   1132 	case LINUX_REBOOT_CMD_POWER_OFF:
   1133 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1134 		break;
   1135 	case LINUX_REBOOT_CMD_RESTART2:
   1136 		/* Reboot with an argument. */
   1137 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1138 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1139 		break;
   1140 	case LINUX_REBOOT_CMD_CAD_ON:
   1141 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1142 	case LINUX_REBOOT_CMD_CAD_OFF:
   1143 		return(0);
   1144 	default:
   1145 		return(EINVAL);
   1146 	}
   1147 
   1148 	return(sys_reboot(l, &sra, retval));
   1149 }
   1150 
   1151 /*
   1152  * Copy of compat_12_sys_swapon().
   1153  */
   1154 int
   1155 linux_sys_swapon(struct lwp *l, const struct linux_sys_swapon_args *uap, register_t *retval)
   1156 {
   1157 	/* {
   1158 		syscallarg(const char *) name;
   1159 	} */
   1160 	struct sys_swapctl_args ua;
   1161 
   1162 	SCARG(&ua, cmd) = SWAP_ON;
   1163 	SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
   1164 	SCARG(&ua, misc) = 0;	/* priority */
   1165 	return (sys_swapctl(l, &ua, retval));
   1166 }
   1167 
   1168 /*
   1169  * Stop swapping to the file or block device specified by path.
   1170  */
   1171 int
   1172 linux_sys_swapoff(struct lwp *l, const struct linux_sys_swapoff_args *uap, register_t *retval)
   1173 {
   1174 	/* {
   1175 		syscallarg(const char *) path;
   1176 	} */
   1177 	struct sys_swapctl_args ua;
   1178 
   1179 	SCARG(&ua, cmd) = SWAP_OFF;
   1180 	SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
   1181 	return (sys_swapctl(l, &ua, retval));
   1182 }
   1183 
   1184 /*
   1185  * Copy of compat_09_sys_setdomainname()
   1186  */
   1187 /* ARGSUSED */
   1188 int
   1189 linux_sys_setdomainname(struct lwp *l, const struct linux_sys_setdomainname_args *uap, register_t *retval)
   1190 {
   1191 	/* {
   1192 		syscallarg(char *) domainname;
   1193 		syscallarg(int) len;
   1194 	} */
   1195 	int name[2];
   1196 
   1197 	name[0] = CTL_KERN;
   1198 	name[1] = KERN_DOMAINNAME;
   1199 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
   1200 			    SCARG(uap, len), l));
   1201 }
   1202 
   1203 /*
   1204  * sysinfo()
   1205  */
   1206 /* ARGSUSED */
   1207 int
   1208 linux_sys_sysinfo(struct lwp *l, const struct linux_sys_sysinfo_args *uap, register_t *retval)
   1209 {
   1210 	/* {
   1211 		syscallarg(struct linux_sysinfo *) arg;
   1212 	} */
   1213 	struct linux_sysinfo si;
   1214 	struct loadavg *la;
   1215 
   1216 	si.uptime = time_uptime;
   1217 	la = &averunnable;
   1218 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1219 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1220 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1221 	si.totalram = ctob((u_long)physmem);
   1222 	si.freeram = (u_long)uvmexp.free * uvmexp.pagesize;
   1223 	si.sharedram = 0;	/* XXX */
   1224 	si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
   1225 	si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
   1226 	si.freeswap =
   1227 	    (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1228 	si.procs = nprocs;
   1229 
   1230 	/* The following are only present in newer Linux kernels. */
   1231 	si.totalbig = 0;
   1232 	si.freebig = 0;
   1233 	si.mem_unit = 1;
   1234 
   1235 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1236 }
   1237 
   1238 int
   1239 linux_sys_getrlimit(struct lwp *l, const struct linux_sys_getrlimit_args *uap, register_t *retval)
   1240 {
   1241 	/* {
   1242 		syscallarg(int) which;
   1243 # ifdef LINUX_LARGEFILE64
   1244 		syscallarg(struct rlimit *) rlp;
   1245 # else
   1246 		syscallarg(struct orlimit *) rlp;
   1247 # endif
   1248 	} */
   1249 # ifdef LINUX_LARGEFILE64
   1250 	struct rlimit orl;
   1251 # else
   1252 	struct orlimit orl;
   1253 # endif
   1254 	int which;
   1255 
   1256 	which = linux_to_bsd_limit(SCARG(uap, which));
   1257 	if (which < 0)
   1258 		return -which;
   1259 
   1260 	bsd_to_linux_rlimit(&orl, &l->l_proc->p_rlimit[which]);
   1261 
   1262 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
   1263 }
   1264 
   1265 int
   1266 linux_sys_setrlimit(struct lwp *l, const struct linux_sys_setrlimit_args *uap, register_t *retval)
   1267 {
   1268 	/* {
   1269 		syscallarg(int) which;
   1270 # ifdef LINUX_LARGEFILE64
   1271 		syscallarg(struct rlimit *) rlp;
   1272 # else
   1273 		syscallarg(struct orlimit *) rlp;
   1274 # endif
   1275 	} */
   1276 	struct rlimit rl;
   1277 # ifdef LINUX_LARGEFILE64
   1278 	struct rlimit orl;
   1279 # else
   1280 	struct orlimit orl;
   1281 # endif
   1282 	int error;
   1283 	int which;
   1284 
   1285 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
   1286 		return error;
   1287 
   1288 	which = linux_to_bsd_limit(SCARG(uap, which));
   1289 	if (which < 0)
   1290 		return -which;
   1291 
   1292 	linux_to_bsd_rlimit(&rl, &orl);
   1293 	return dosetrlimit(l, l->l_proc, which, &rl);
   1294 }
   1295 
   1296 # if !defined(__mips__) && !defined(__amd64__)
   1297 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
   1298 int
   1299 linux_sys_ugetrlimit(struct lwp *l, const struct linux_sys_ugetrlimit_args *uap, register_t *retval)
   1300 {
   1301 	return linux_sys_getrlimit(l, (const void *)uap, retval);
   1302 }
   1303 # endif
   1304 
   1305 /*
   1306  * This gets called for unsupported syscalls. The difference to sys_nosys()
   1307  * is that process does not get SIGSYS, the call just returns with ENOSYS.
   1308  * This is the way Linux does it and glibc depends on this behaviour.
   1309  */
   1310 int
   1311 linux_sys_nosys(struct lwp *l, const void *v, register_t *retval)
   1312 {
   1313 	return (ENOSYS);
   1314 }
   1315 
   1316 int
   1317 linux_sys_getpriority(struct lwp *l, const struct linux_sys_getpriority_args *uap, register_t *retval)
   1318 {
   1319         /* {
   1320                 syscallarg(int) which;
   1321                 syscallarg(int) who;
   1322         } */
   1323         struct sys_getpriority_args bsa;
   1324         int error;
   1325 
   1326         SCARG(&bsa, which) = SCARG(uap, which);
   1327         SCARG(&bsa, who) = SCARG(uap, who);
   1328 
   1329         if ((error = sys_getpriority(l, &bsa, retval)))
   1330                 return error;
   1331 
   1332         *retval = NZERO - *retval;
   1333 
   1334         return 0;
   1335 }
   1336 
   1337 #endif /* !COMPAT_LINUX32 */
   1338