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