Home | History | Annotate | Line # | Download | only in ultrix
ultrix_misc.c revision 1.68.2.2
      1 /*	$NetBSD: ultrix_misc.c,v 1.68.2.2 2001/09/21 22:35:23 nathanw Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1997 Jonathan Stone (hereinafter referred to as the author)
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Jonathan Stone for
     18  *      the NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1992, 1993
     37  *	The Regents of the University of California.  All rights reserved.
     38  *
     39  * This software was developed by the Computer Systems Engineering group
     40  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     41  * contributed to Berkeley.
     42  *
     43  * All advertising materials mentioning features or use of this software
     44  * must display the following acknowledgement:
     45  *	This product includes software developed by the University of
     46  *	California, Lawrence Berkeley Laboratory.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  *
     77  *	@(#)sun_misc.c	8.1 (Berkeley) 6/18/93
     78  *
     79  * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
     80  */
     81 
     82 #if defined(_KERNEL_OPT)
     83 #include "opt_nfsserver.h"
     84 #include "opt_sysv.h"
     85 #endif
     86 
     87 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     88 
     89 /*
     90  * Ultrix compatibility module.
     91  *
     92  * Ultrix system calls that are implemented differently in BSD are
     93  * handled here.
     94  */
     95 
     96 #if defined(_KERNEL_OPT)
     97 #include "fs_nfs.h"
     98 #endif
     99 
    100 #include <sys/param.h>
    101 #include <sys/systm.h>
    102 #include <sys/namei.h>
    103 #include <sys/dirent.h>
    104 #include <sys/proc.h>
    105 #include <sys/file.h>
    106 #include <sys/filedesc.h>
    107 #include <sys/kernel.h>
    108 #include <sys/exec.h>
    109 #include <sys/malloc.h>
    110 #include <sys/mbuf.h>
    111 #include <sys/mman.h>
    112 #include <sys/mount.h>
    113 #include <sys/resource.h>
    114 #include <sys/resourcevar.h>
    115 #include <sys/signal.h>
    116 #include <sys/signalvar.h>
    117 #include <sys/socket.h>
    118 #include <sys/vnode.h>
    119 #include <sys/uio.h>
    120 #include <sys/wait.h>
    121 #include <sys/utsname.h>
    122 #include <sys/unistd.h>
    123 #include <sys/ipc.h>
    124 
    125 #include <sys/syscallargs.h>
    126 
    127 #include <compat/ultrix/ultrix_syscall.h>
    128 #include <compat/ultrix/ultrix_syscallargs.h>
    129 #include <compat/common/compat_util.h>
    130 
    131 #include <netinet/in.h>
    132 
    133 #include <miscfs/specfs/specdev.h>
    134 
    135 #include <nfs/rpcv2.h>
    136 #include <nfs/nfsproto.h>
    137 #include <nfs/nfs.h>
    138 
    139 #include <sys/conf.h>					/* iszerodev() */
    140 #include <sys/socketvar.h>				/* sosetopt() */
    141 
    142 #include <compat/ultrix/ultrix_flock.h>
    143 
    144 #ifdef __mips
    145 #include <mips/cachectl.h>
    146 #endif
    147 
    148 static int ultrix_to_bsd_flock __P((struct ultrix_flock *, struct flock *));
    149 static void bsd_to_ultrix_flock __P((struct flock *, struct ultrix_flock *));
    150 
    151 extern struct sysent ultrix_sysent[];
    152 extern const char * const ultrix_syscallnames[];
    153 extern char ultrix_sigcode[], ultrix_esigcode[];
    154 #ifdef __HAVE_SYSCALL_INTERN
    155 void syscall_intern(struct proc *);
    156 #else
    157 void syscall __P((void));
    158 #endif
    159 
    160 const struct emul emul_ultrix = {
    161 	"ultrix",
    162 	"/emul/ultrix",
    163 #ifndef __HAVE_MINIMAL_EMUL
    164 	0,
    165 	NULL,
    166 	ULTRIX_SYS_syscall,
    167 	ULTRIX_SYS_MAXSYSCALL,
    168 #endif
    169 	ultrix_sysent,
    170 	ultrix_syscallnames,
    171 	sendsig,
    172 	trapsignal,
    173 	ultrix_sigcode,
    174 	ultrix_esigcode,
    175 	setregs,
    176 	NULL,
    177 	NULL,
    178 	NULL,
    179 #ifdef __HAVE_SYSCALL_INTERN
    180 	syscall_intern,
    181 #else
    182 	syscall,
    183 #endif
    184 };
    185 
    186 #define GSI_PROG_ENV 1
    187 
    188 int
    189 ultrix_sys_getsysinfo(p, v, retval)
    190 	struct proc *p;
    191 	void *v;
    192 	register_t *retval;
    193 {
    194 	struct ultrix_sys_getsysinfo_args *uap = v;
    195 	static short progenv = 0;
    196 
    197 	switch (SCARG(uap, op)) {
    198 		/* operations implemented: */
    199 	case GSI_PROG_ENV:
    200 		if (SCARG(uap, nbytes) < sizeof(short))
    201 			return EINVAL;
    202 		*retval = 1;
    203 		return (copyout(&progenv, SCARG(uap, buffer), sizeof(short)));
    204 	default:
    205 		*retval = 0; /* info unavail */
    206 		return 0;
    207 	}
    208 }
    209 
    210 int
    211 ultrix_sys_setsysinfo(p, v, retval)
    212 	struct proc *p;
    213 	void *v;
    214 	register_t *retval;
    215 {
    216 
    217 #ifdef notyet
    218 	struct ultrix_sys_setsysinfo_args *uap = v;
    219 #endif
    220 
    221 	*retval = 0;
    222 	return 0;
    223 }
    224 
    225 int
    226 ultrix_sys_waitpid(p, v, retval)
    227 	struct proc *p;
    228 	void *v;
    229 	register_t *retval;
    230 {
    231 	struct ultrix_sys_waitpid_args *uap = v;
    232 	struct sys_wait4_args ua;
    233 
    234 	SCARG(&ua, pid) = SCARG(uap, pid);
    235 	SCARG(&ua, status) = SCARG(uap, status);
    236 	SCARG(&ua, options) = SCARG(uap, options);
    237 	SCARG(&ua, rusage) = 0;
    238 
    239 	return (sys_wait4(p, &ua, retval));
    240 }
    241 
    242 int
    243 ultrix_sys_wait3(p, v, retval)
    244 	struct proc *p;
    245 	void *v;
    246 	register_t *retval;
    247 {
    248 	struct ultrix_sys_wait3_args *uap = v;
    249 	struct sys_wait4_args ua;
    250 
    251 	SCARG(&ua, pid) = -1;
    252 	SCARG(&ua, status) = SCARG(uap, status);
    253 	SCARG(&ua, options) = SCARG(uap, options);
    254 	SCARG(&ua, rusage) = SCARG(uap, rusage);
    255 
    256 	return (sys_wait4(p, &ua, retval));
    257 }
    258 
    259 /*
    260  * Ultrix binaries pass in FD_MAX as the first arg to select().
    261  * On Ultrix, FD_MAX is 4096, which is more than the NetBSD sys_select()
    262  * can handle.
    263  * Since we can't have more than the (native) FD_MAX descriptors open,
    264  * limit nfds to at most FD_MAX.
    265  */
    266 int
    267 ultrix_sys_select(p, v, retval)
    268 	struct proc *p;
    269 	void *v;
    270 	register_t *retval;
    271 {
    272 	struct sys_select_args *uap = v;
    273 	struct timeval atv;
    274 	int error;
    275 
    276 	/* Limit number of FDs selected on to the native maximum */
    277 
    278 	if (SCARG(uap, nd) > FD_SETSIZE)
    279 		SCARG(uap, nd) = FD_SETSIZE;
    280 
    281 	/* Check for negative timeval */
    282 	if (SCARG(uap, tv)) {
    283 		error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
    284 			       sizeof(atv));
    285 		if (error)
    286 			goto done;
    287 #ifdef DEBUG
    288 		/* Ultrix clients sometimes give negative timeouts? */
    289 		if (atv.tv_sec < 0 || atv.tv_usec < 0)
    290 			printf("ultrix select( %ld, %ld): negative timeout\n",
    291 			    atv.tv_sec, atv.tv_usec);
    292 		/*tvp = (timeval *)STACKGAPBASE;*/
    293 #endif
    294 
    295 	}
    296 	error = sys_select(p, (void*) uap, retval);
    297 	if (error == EINVAL)
    298 		printf("ultrix select: bad args?\n");
    299 
    300 done:
    301 	return error;
    302 }
    303 
    304 #if defined(NFS)
    305 int
    306 async_daemon(p, v, retval)
    307 	struct proc *p;
    308 	void *v;
    309 	register_t *retval;
    310 {
    311 	struct sys_nfssvc_args ouap;
    312 
    313 	SCARG(&ouap, flag) = NFSSVC_BIOD;
    314 	SCARG(&ouap, argp) = NULL;
    315 
    316 	return (sys_nfssvc(p, &ouap, retval));
    317 }
    318 #endif /* NFS */
    319 
    320 
    321 #define	SUN__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    322 
    323 int
    324 ultrix_sys_mmap(p, v, retval)
    325 	struct proc *p;
    326 	void *v;
    327 	register_t *retval;
    328 {
    329 	struct ultrix_sys_mmap_args *uap = v;
    330 	struct sys_mmap_args ouap;
    331 	struct filedesc *fdp;
    332 	struct file *fp;
    333 	struct vnode *vp;
    334 
    335 	/*
    336 	 * Verify the arguments.
    337 	 */
    338 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    339 		return (EINVAL);			/* XXX still needed? */
    340 
    341 	if ((SCARG(uap, flags) & SUN__MAP_NEW) == 0)
    342 		return (EINVAL);
    343 
    344 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUN__MAP_NEW;
    345 	SCARG(&ouap, addr) = SCARG(uap, addr);
    346 
    347 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
    348 	    SCARG(&ouap, addr) != 0 &&
    349 	    SCARG(&ouap, addr) < (void *)round_page((vaddr_t)p->p_vmspace->vm_daddr+MAXDSIZ))
    350 		SCARG(&ouap, addr) = (void *)round_page((vaddr_t)p->p_vmspace->vm_daddr+MAXDSIZ);
    351 
    352 	SCARG(&ouap, len) = SCARG(uap, len);
    353 	SCARG(&ouap, prot) = SCARG(uap, prot);
    354 	SCARG(&ouap, fd) = SCARG(uap, fd);
    355 	SCARG(&ouap, pos) = SCARG(uap, pos);
    356 
    357 	/*
    358 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
    359 	 */
    360 	fdp = p->p_fd;
    361 	if ((fp = fd_getfile(fdp, SCARG(&ouap, fd))) != NULL &&		/*XXX*/
    362 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
    363 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
    364 	    iszerodev(vp->v_rdev)) {					/*XXX*/
    365 		SCARG(&ouap, flags) |= MAP_ANON;
    366 		SCARG(&ouap, fd) = -1;
    367 	}
    368 
    369 	return (sys_mmap(p, &ouap, retval));
    370 }
    371 
    372 int
    373 ultrix_sys_setsockopt(p, v, retval)
    374 	struct proc *p;
    375 	void *v;
    376 	register_t *retval;
    377 {
    378 	struct ultrix_sys_setsockopt_args *uap = v;
    379 	struct file *fp;
    380 	struct mbuf *m = NULL;
    381 	int error;
    382 
    383 	/* getsock() will use the descriptor for us */
    384 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp))  != 0)
    385 		return (error);
    386 #define	SO_DONTLINGER (~SO_LINGER)
    387 	if (SCARG(uap, name) == SO_DONTLINGER) {
    388 		m = m_get(M_WAIT, MT_SOOPTS);
    389 		mtod(m, struct linger *)->l_onoff = 0;
    390 		m->m_len = sizeof(struct linger);
    391 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    392 		    SO_LINGER, m);
    393 	}
    394 	if (SCARG(uap, level) == IPPROTO_IP) {
    395 #define		EMUL_IP_MULTICAST_IF		2
    396 #define		EMUL_IP_MULTICAST_TTL		3
    397 #define		EMUL_IP_MULTICAST_LOOP		4
    398 #define		EMUL_IP_ADD_MEMBERSHIP		5
    399 #define		EMUL_IP_DROP_MEMBERSHIP	6
    400 		static int ipoptxlat[] = {
    401 			IP_MULTICAST_IF,
    402 			IP_MULTICAST_TTL,
    403 			IP_MULTICAST_LOOP,
    404 			IP_ADD_MEMBERSHIP,
    405 			IP_DROP_MEMBERSHIP
    406 		};
    407 		if (SCARG(uap, name) >= EMUL_IP_MULTICAST_IF &&
    408 		    SCARG(uap, name) <= EMUL_IP_DROP_MEMBERSHIP) {
    409 			SCARG(uap, name) =
    410 			    ipoptxlat[SCARG(uap, name) - EMUL_IP_MULTICAST_IF];
    411 		}
    412 	}
    413 	if (SCARG(uap, valsize) > MLEN) {
    414 		error = EINVAL;
    415 		goto out;
    416 	}
    417 	if (SCARG(uap, val)) {
    418 		m = m_get(M_WAIT, MT_SOOPTS);
    419 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
    420 		    (u_int)SCARG(uap, valsize));
    421 		if (error) {
    422 			(void) m_free(m);
    423 			goto out;
    424 		}
    425 		m->m_len = SCARG(uap, valsize);
    426 	}
    427 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    428 	    SCARG(uap, name), m);
    429  out:
    430 	FILE_UNUSE(fp, p);
    431 	return (error);
    432 }
    433 
    434 #define	ULTRIX__SYS_NMLN	32
    435 
    436 struct ultrix_utsname {
    437 	char    sysname[ULTRIX__SYS_NMLN];
    438 	char    nodename[ULTRIX__SYS_NMLN];
    439 	char    release[ULTRIX__SYS_NMLN];
    440 	char    version[ULTRIX__SYS_NMLN];
    441 	char    machine[ULTRIX__SYS_NMLN];
    442 };
    443 
    444 int
    445 ultrix_sys_uname(p, v, retval)
    446 	struct proc *p;
    447 	void *v;
    448 	register_t *retval;
    449 {
    450 	struct ultrix_sys_uname_args *uap = v;
    451 	struct ultrix_utsname sut;
    452 	const char *cp;
    453 	char *dp, *ep;
    454 
    455 	memset(&sut, 0, sizeof(sut));
    456 
    457 	strncpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
    458 	strncpy(sut.nodename, hostname, sizeof(sut.nodename) - 1);
    459 	strncpy(sut.release, osrelease, sizeof(sut.release) - 1);
    460 	dp = sut.version;
    461 	ep = &sut.version[sizeof(sut.version) - 1];
    462 	for (cp = version; *cp && *cp != '('; cp++)
    463 		;
    464 	for (cp++; *cp && *cp != ')' && dp < ep; cp++)
    465 		*dp++ = *cp;
    466 	for (; *cp && *cp != '#'; cp++)
    467 		;
    468 	for (; *cp && *cp != ':' && dp < ep; cp++)
    469 		*dp++ = *cp;
    470 	*dp = '\0';
    471 	strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
    472 
    473 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
    474 	    sizeof(struct ultrix_utsname));
    475 }
    476 
    477 int
    478 ultrix_sys_setpgrp(p, v, retval)
    479 	struct proc *p;
    480 	void *v;
    481 	register_t *retval;
    482 {
    483 	struct ultrix_sys_setpgrp_args *uap = v;
    484 
    485 	/*
    486 	 * difference to our setpgid call is to include backwards
    487 	 * compatibility to pre-setsid() binaries. Do setsid()
    488 	 * instead of setpgid() in those cases where the process
    489 	 * tries to create a new session the old way.
    490 	 */
    491 	if (!SCARG(uap, pgid) &&
    492 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    493 		return sys_setsid(p, uap, retval);
    494 	else
    495 		return sys_setpgid(p, uap, retval);
    496 }
    497 
    498 #if defined (NFSSERVER)
    499 int
    500 ultrix_sys_nfssvc(p, v, retval)
    501 	struct proc *p;
    502 	void *v;
    503 	register_t *retval;
    504 {
    505 
    506 #if 0	/* XXX */
    507 	struct ultrix_sys_nfssvc_args *uap = v;
    508 	struct emul *e = p->p_emul;
    509 	struct sys_nfssvc_args outuap;
    510 	struct sockaddr sa;
    511 	int error;
    512 
    513 	memset(&outuap, 0, sizeof outuap);
    514 	SCARG(&outuap, fd) = SCARG(uap, fd);
    515 	SCARG(&outuap, mskval) = STACKGAPBASE;
    516 	SCARG(&outuap, msklen) = sizeof sa;
    517 	SCARG(&outuap, mtchval) = outuap.mskval + sizeof sa;
    518 	SCARG(&outuap, mtchlen) = sizeof sa;
    519 
    520 	memset(&sa, 0, sizeof sa);
    521 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
    522 		return (error);
    523 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
    524 		return (error);
    525 
    526 	return nfssvc(p, &outuap, retval);
    527 #else
    528 	return (ENOSYS);
    529 #endif
    530 }
    531 #endif /* NFSSERVER */
    532 
    533 struct ultrix_ustat {
    534 	daddr_t	f_tfree;	/* total free */
    535 	ino_t	f_tinode;	/* total inodes free */
    536 	char	f_fname[6];	/* filsys name */
    537 	char	f_fpack[6];	/* filsys pack name */
    538 };
    539 
    540 int
    541 ultrix_sys_ustat(p, v, retval)
    542 	struct proc *p;
    543 	void *v;
    544 	register_t *retval;
    545 {
    546 	struct ultrix_sys_ustat_args *uap = v;
    547 	struct ultrix_ustat us;
    548 	int error;
    549 
    550 	memset(&us, 0, sizeof us);
    551 
    552 	/*
    553 	 * XXX: should set f_tfree and f_tinode at least
    554 	 * How do we translate dev -> fstat? (and then to ultrix_ustat)
    555 	 */
    556 
    557 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
    558 		return (error);
    559 	return 0;
    560 }
    561 
    562 int
    563 ultrix_sys_quotactl(p, v, retval)
    564 	struct proc *p;
    565 	void *v;
    566 	register_t *retval;
    567 {
    568 
    569 #ifdef notyet
    570 	struct ultrix_sys_quotactl_args *uap = v;
    571 #endif
    572 
    573 	return EINVAL;
    574 }
    575 
    576 int
    577 ultrix_sys_vhangup(p, v, retval)
    578 	struct proc *p;
    579 	void *v;
    580 	register_t *retval;
    581 {
    582 
    583 	return 0;
    584 }
    585 
    586 
    587 /*
    588  * RISC Ultrix cache control syscalls
    589  */
    590 #ifdef __mips
    591 int
    592 ultrix_sys_cacheflush(p, v, retval)
    593 	struct proc *p;
    594 	void *v;
    595 	register_t *retval;
    596 {
    597 	struct ultrix_sys_cacheflush_args /* {
    598 		syscallarg(void *) addr;
    599 		syscallarg(int) nbytes;
    600 		syscallarg(int) flag;
    601 	} */ *uap = v;
    602 	vaddr_t va  = (vaddr_t)SCARG(uap, addr);
    603 	int nbytes     = SCARG(uap, nbytes);
    604 	int whichcache = SCARG(uap, whichcache);
    605 
    606 	return (mips_user_cacheflush(p, va, nbytes, whichcache));
    607 }
    608 
    609 
    610 int
    611 ultrix_sys_cachectl(p, v, retval)
    612 	struct proc *p;
    613 	void *v;
    614 	register_t *retval;
    615 {
    616 	struct ultrix_sys_cachectl_args /* {
    617 		syscallarg(void *) addr;
    618 		syscallarg(int) nbytes;
    619 		syscallarg(int) cacheop;
    620 	} */ *uap = v;
    621 	vaddr_t va  = (vaddr_t)SCARG(uap, addr);
    622 	int nbytes  = SCARG(uap, nbytes);
    623 	int cacheop = SCARG(uap, cacheop);
    624 
    625 	return mips_user_cachectl(p, va, nbytes, cacheop);
    626 }
    627 
    628 #endif	/* __mips */
    629 
    630 
    631 int
    632 ultrix_sys_exportfs(p, v, retval)
    633 	struct proc *p;
    634 	void *v;
    635 	register_t *retval;
    636 {
    637 #ifdef notyet
    638 	struct ultrix_sys_exportfs_args *uap = v;
    639 #endif
    640 
    641 	/*
    642 	 * XXX: should perhaps translate into a mount(2)
    643 	 * with MOUNT_EXPORT?
    644 	 */
    645 	return 0;
    646 }
    647 
    648 int
    649 ultrix_sys_sigpending(p, v, retval)
    650 	struct proc *p;
    651 	void *v;
    652 	register_t *retval;
    653 {
    654 	struct ultrix_sys_sigpending_args *uap = v;
    655 	sigset_t ss;
    656 	int mask;
    657 
    658 	sigpending1(p, &ss);
    659 	mask = ss.__bits[0];
    660 
    661 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
    662 }
    663 
    664 int
    665 ultrix_sys_sigreturn(p, v, retval)
    666 	struct proc *p;
    667 	void *v;
    668 	register_t *retval;
    669 {
    670 	struct ultrix_sys_sigreturn_args *uap = v;
    671 
    672 	/* struct sigcontext13 is close enough to Ultrix */
    673 
    674 	return (compat_13_sys_sigreturn(p, uap, retval));
    675 }
    676 
    677 int
    678 ultrix_sys_sigcleanup(p, v, retval)
    679 	struct proc *p;
    680 	void *v;
    681 	register_t *retval;
    682 {
    683 	struct ultrix_sys_sigcleanup_args *uap = v;
    684 
    685 	/* struct sigcontext13 is close enough to Ultrix */
    686 
    687 	return (compat_13_sys_sigreturn(p, uap, retval));
    688 }
    689 
    690 int
    691 ultrix_sys_sigsuspend(p, v, retval)
    692 	struct proc *p;
    693 	void *v;
    694 	register_t *retval;
    695 {
    696 	struct ultrix_sys_sigsuspend_args *uap = v;
    697 	int mask = SCARG(uap, mask);
    698 	sigset_t ss;
    699 
    700 	ss.__bits[0] = mask;
    701 	ss.__bits[1] = 0;
    702 	ss.__bits[2] = 0;
    703 	ss.__bits[3] = 0;
    704 
    705 	return (sigsuspend1(p, &ss));
    706 }
    707 
    708 #define ULTRIX_SV_ONSTACK 0x0001  /* take signal on signal stack */
    709 #define ULTRIX_SV_INTERRUPT 0x0002  /* do not restart system on signal return */
    710 #define ULTRIX_SV_OLDSIG 0x1000  /* Emulate old signal() for POSIX */
    711 
    712 int
    713 ultrix_sys_sigvec(p, v, retval)
    714 	struct proc *p;
    715 	void *v;
    716 	register_t *retval;
    717 {
    718 	struct ultrix_sys_sigvec_args *uap = v;
    719 	struct sigvec nsv, osv;
    720 	struct sigaction nsa, osa;
    721 	int error;
    722 
    723 	if (SCARG(uap, nsv)) {
    724 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
    725 		if (error)
    726 			return (error);
    727 		nsa.sa_handler = nsv.sv_handler;
    728 #if 0 /* documentation */
    729 		/* ONSTACK is identical */
    730 		nsa.sa_flags = nsv.sv_flags & ULTRIX_SV_ONSTACK;
    731 		if ((nsv.sv_flags & ULTRIX_SV_OLDSIG)
    732 		    /* old signal() - always restart */
    733 		    || (!(nsv.sv_flags & ULTRIX_SV_INTERRUPT))
    734 		    /* inverted meaning (same bit) */
    735 		    )
    736 			nsa.sa_flags |= SA_RESTART;
    737 #else /* optimized - assuming ULTRIX_SV_OLDSIG=>!ULTRIX_SV_INTERRUPT */
    738 		nsa.sa_flags = nsv.sv_flags & ~ULTRIX_SV_OLDSIG;
    739 		nsa.sa_flags ^= SA_RESTART;
    740 #endif
    741 		native_sigset13_to_sigset(&nsv.sv_mask, &nsa.sa_mask);
    742 	}
    743 	error = sigaction1(p, SCARG(uap, signum),
    744 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0);
    745 	if (error)
    746 		return (error);
    747 	if (SCARG(uap, osv)) {
    748 		osv.sv_handler = osa.sa_handler;
    749 		osv.sv_flags = osa.sa_flags ^ SA_RESTART;
    750 		osv.sv_flags &= (ULTRIX_SV_ONSTACK | ULTRIX_SV_INTERRUPT);
    751 		native_sigset_to_sigset13(&osa.sa_mask, &osv.sv_mask);
    752 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
    753 		if (error)
    754 			return (error);
    755 	}
    756 	return (0);
    757 }
    758 
    759 int
    760 ultrix_sys_shmsys(p, v, retval)
    761 	struct proc *p;
    762 	void *v;
    763 	register_t *retval;
    764 {
    765 
    766 #ifdef SYSVSHM
    767 
    768 	/* Ultrix SVSHM weirndess: */
    769 	struct ultrix_sys_shmsys_args *uap = v;
    770 	struct sys_shmat_args shmat_args;
    771 	struct compat_14_sys_shmctl_args shmctl_args;
    772 	struct sys_shmdt_args shmdt_args;
    773 	struct sys_shmget_args shmget_args;
    774 
    775 
    776 	switch (SCARG(uap, shmop)) {
    777 	case 0:						/* Ultrix shmat() */
    778 		SCARG(&shmat_args, shmid) = SCARG(uap, a2);
    779 		SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a3);
    780 		SCARG(&shmat_args, shmflg) = SCARG(uap, a4);
    781 		return (sys_shmat(p, &shmat_args, retval));
    782 
    783 	case 1:						/* Ultrix shmctl() */
    784 		SCARG(&shmctl_args, shmid) = SCARG(uap, a2);
    785 		SCARG(&shmctl_args, cmd) = SCARG(uap, a3);
    786 		SCARG(&shmctl_args, buf) = (struct shmid_ds14 *)SCARG(uap, a4);
    787 		return (compat_14_sys_shmctl(p, &shmctl_args, retval));
    788 
    789 	case 2:						/* Ultrix shmdt() */
    790 		SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a2);
    791 		return (sys_shmdt(p, &shmdt_args, retval));
    792 
    793 	case 3:						/* Ultrix shmget() */
    794 		SCARG(&shmget_args, key) = SCARG(uap, a2);
    795 		SCARG(&shmget_args, size) = SCARG(uap, a3);
    796 		SCARG(&shmget_args, shmflg) = SCARG(uap, a4)
    797 		    & (IPC_CREAT|IPC_EXCL|IPC_NOWAIT);
    798 		return (sys_shmget(p, &shmget_args, retval));
    799 
    800 	default:
    801 		return (EINVAL);
    802 	}
    803 #else
    804 	return (EOPNOTSUPP);
    805 #endif	/* SYSVSHM */
    806 }
    807 
    808 static int
    809 ultrix_to_bsd_flock(ufl, fl)
    810 	struct ultrix_flock *ufl;
    811 	struct flock *fl;
    812 {
    813 
    814 	fl->l_start = ufl->l_start;
    815 	fl->l_len = ufl->l_len;
    816 	fl->l_pid = ufl->l_pid;
    817 	fl->l_whence = ufl->l_whence;
    818 
    819 	switch (ufl->l_type) {
    820 	case ULTRIX_F_RDLCK:
    821 		fl->l_type = F_RDLCK;
    822 		break;
    823 	case ULTRIX_F_WRLCK:
    824 		fl->l_type = F_WRLCK;
    825 		break;
    826 	case ULTRIX_F_UNLCK:
    827 		fl->l_type = F_UNLCK;
    828 		break;
    829 	default:
    830 		return (EINVAL);
    831 	}
    832 
    833 	return (0);
    834 }
    835 
    836 static void
    837 bsd_to_ultrix_flock(fl, ufl)
    838 	struct flock *fl;
    839 	struct ultrix_flock *ufl;
    840 {
    841 
    842 	ufl->l_start = fl->l_start;
    843 	ufl->l_len = fl->l_len;
    844 	ufl->l_pid = fl->l_pid;
    845 	ufl->l_whence = fl->l_whence;
    846 
    847 	switch (fl->l_type) {
    848 	case F_RDLCK:
    849 		ufl->l_type = ULTRIX_F_RDLCK;
    850 		break;
    851 	case F_WRLCK:
    852 		ufl->l_type = ULTRIX_F_WRLCK;
    853 		break;
    854 	case F_UNLCK:
    855 		ufl->l_type = ULTRIX_F_UNLCK;
    856 		break;
    857 	}
    858 }
    859 
    860 int
    861 ultrix_sys_fcntl(p, v, retval)
    862 	struct proc *p;
    863 	void *v;
    864 	register_t *retval;
    865 {
    866 	struct ultrix_sys_fcntl_args *uap = v;
    867 	int error;
    868 	struct ultrix_flock ufl;
    869 	struct flock fl, *flp;
    870 	caddr_t sg;
    871 	struct sys_fcntl_args *args, fca;
    872 
    873 	switch (SCARG(uap, cmd)) {
    874 	case F_GETLK:
    875 	case F_SETLK:
    876 	case F_SETLKW:
    877 		error = copyin(SCARG(uap, arg), &ufl, sizeof(ufl));
    878 		if (error)
    879 			return (error);
    880 		error = ultrix_to_bsd_flock(&ufl, &fl);
    881 		if (error)
    882 			return (error);
    883 		sg = stackgap_init(p->p_emul);
    884 		flp = (struct flock *)stackgap_alloc(&sg, sizeof(*flp));
    885 		error = copyout(&fl, flp, sizeof(*flp));
    886 		if (error)
    887 			return (error);
    888 
    889 		SCARG(&fca, fd) = SCARG(uap, fd);
    890 		SCARG(&fca, cmd) = SCARG(uap, cmd);
    891 		SCARG(&fca, arg) = flp;
    892 		args = &fca;
    893 		break;
    894 	default:
    895 		args = v;
    896 		break;
    897 	}
    898 
    899 	error = sys_fcntl(p, args, retval);
    900 	if (error)
    901 		return (error);
    902 
    903 	switch (SCARG(uap, cmd)) {
    904 	case F_GETLK:
    905 		error = copyin(flp, &fl, sizeof(fl));
    906 		if (error)
    907 			return (error);
    908 		bsd_to_ultrix_flock(&fl, &ufl);
    909 		error = copyout(&ufl, SCARG(uap, arg), sizeof(ufl));
    910 		break;
    911 	}
    912 
    913 	return (error);
    914 }
    915