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