sunos_syscall.c revision 1.5
11.5Slukem/*	$NetBSD: sunos_syscall.c,v 1.5 2003/07/15 02:43:14 lukem Exp $	*/
21.1Sscw
31.1Sscw/*-
41.1Sscw * Portions Copyright (c) 2000 The NetBSD Foundation, Inc.
51.1Sscw * All rights reserved.
61.1Sscw *
71.1Sscw * Redistribution and use in source and binary forms, with or without
81.1Sscw * modification, are permitted provided that the following conditions
91.1Sscw * are met:
101.1Sscw * 1. Redistributions of source code must retain the above copyright
111.1Sscw *    notice, this list of conditions and the following disclaimer.
121.1Sscw * 2. Redistributions in binary form must reproduce the above copyright
131.1Sscw *    notice, this list of conditions and the following disclaimer in the
141.1Sscw *    documentation and/or other materials provided with the distribution.
151.1Sscw * 3. All advertising materials mentioning features or use of this software
161.1Sscw *    must display the following acknowledgement:
171.1Sscw *        This product includes software developed by the NetBSD
181.1Sscw *        Foundation, Inc. and its contributors.
191.1Sscw * 4. Neither the name of The NetBSD Foundation nor the names of its
201.1Sscw *    contributors may be used to endorse or promote products derived
211.1Sscw *    from this software without specific prior written permission.
221.1Sscw *
231.1Sscw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
241.1Sscw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
251.1Sscw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
261.1Sscw * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
271.1Sscw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
281.1Sscw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
291.1Sscw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
301.1Sscw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
311.1Sscw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
321.1Sscw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
331.1Sscw * POSSIBILITY OF SUCH DAMAGE.
341.1Sscw */
351.1Sscw
361.1Sscw/*
371.1Sscw * Copyright (c) 1988 University of Utah.
381.1Sscw * Copyright (c) 1982, 1986, 1990, 1993
391.1Sscw *	The Regents of the University of California.  All rights reserved.
401.1Sscw *
411.1Sscw * This code is derived from software contributed to Berkeley by
421.1Sscw * the Systems Programming Group of the University of Utah Computer
431.1Sscw * Science Department.
441.1Sscw *
451.1Sscw * Redistribution and use in source and binary forms, with or without
461.1Sscw * modification, are permitted provided that the following conditions
471.1Sscw * are met:
481.1Sscw * 1. Redistributions of source code must retain the above copyright
491.1Sscw *    notice, this list of conditions and the following disclaimer.
501.1Sscw * 2. Redistributions in binary form must reproduce the above copyright
511.1Sscw *    notice, this list of conditions and the following disclaimer in the
521.1Sscw *    documentation and/or other materials provided with the distribution.
531.1Sscw * 3. All advertising materials mentioning features or use of this software
541.1Sscw *    must display the following acknowledgement:
551.1Sscw *	This product includes software developed by the University of
561.1Sscw *	California, Berkeley and its contributors.
571.1Sscw * 4. Neither the name of the University nor the names of its contributors
581.1Sscw *    may be used to endorse or promote products derived from this software
591.1Sscw *    without specific prior written permission.
601.1Sscw *
611.1Sscw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
621.1Sscw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
631.1Sscw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
641.1Sscw * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
651.1Sscw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
661.1Sscw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
671.1Sscw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
681.1Sscw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
691.1Sscw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
701.1Sscw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
711.1Sscw * SUCH DAMAGE.
721.1Sscw *
731.1Sscw * from: Utah $Hdr: trap.c 1.37 92/12/20$
741.1Sscw *
751.1Sscw *	@(#)trap.c	8.5 (Berkeley) 1/4/94
761.1Sscw */
771.5Slukem
781.5Slukem#include <sys/cdefs.h>
791.5Slukem__KERNEL_RCSID(0, "$NetBSD: sunos_syscall.c,v 1.5 2003/07/15 02:43:14 lukem Exp $");
801.1Sscw
811.1Sscw#include "opt_syscall_debug.h"
821.1Sscw#include "opt_execfmt.h"
831.1Sscw#include "opt_ktrace.h"
841.1Sscw#include "opt_systrace.h"
851.1Sscw
861.1Sscw#include <sys/param.h>
871.1Sscw#include <sys/systm.h>
881.1Sscw#include <sys/proc.h>
891.1Sscw#include <sys/acct.h>
901.1Sscw#include <sys/kernel.h>
911.1Sscw#include <sys/syscall.h>
921.1Sscw#include <sys/syslog.h>
931.1Sscw#include <sys/user.h>
941.1Sscw#ifdef KTRACE
951.1Sscw#include <sys/ktrace.h>
961.1Sscw#endif
971.1Sscw#ifdef SYSTRACE
981.1Sscw#include <sys/systrace.h>
991.1Sscw#endif
1001.1Sscw
1011.1Sscw#include <machine/psl.h>
1021.1Sscw#include <machine/cpu.h>
1031.1Sscw#include <machine/reg.h>
1041.1Sscw
1051.1Sscw#include <uvm/uvm_extern.h>
1061.1Sscw
1071.1Sscw#include <compat/sunos/sunos_syscall.h>
1081.1Sscw#include <compat/sunos/sunos_exec.h>
1091.1Sscw
1101.1Sscwvoid	sunos_syscall_intern(struct proc *);
1111.4Sthorpejstatic void sunos_syscall_plain(register_t, struct lwp *, struct frame *);
1121.4Sthorpejstatic void sunos_syscall_fancy(register_t, struct lwp *, struct frame *);
1131.1Sscw
1141.1Sscwvoid
1151.1Sscwsunos_syscall_intern(struct proc *p)
1161.1Sscw{
1171.1Sscw
1181.1Sscw#ifdef KTRACE
1191.1Sscw	if (p->p_traceflag & (KTRFAC_SYSCALL | KTRFAC_SYSRET))
1201.1Sscw		p->p_md.md_syscall = sunos_syscall_fancy;
1211.1Sscw	else
1221.1Sscw#endif
1231.1Sscw#ifdef SYSTRACE
1241.1Sscw	if (ISSET(p->p_flag, P_SYSTRACE))
1251.1Sscw		p->p_md.md_syscall = sunos_syscall_fancy;
1261.1Sscw	else
1271.1Sscw#endif
1281.1Sscw		p->p_md.md_syscall = sunos_syscall_plain;
1291.1Sscw}
1301.1Sscw
1311.1Sscwstatic void
1321.4Sthorpejsunos_syscall_plain(register_t code, struct lwp *l, struct frame *frame)
1331.1Sscw{
1341.4Sthorpej	struct proc *p = l->l_proc;
1351.1Sscw	caddr_t params;
1361.1Sscw	const struct sysent *callp;
1371.1Sscw	int error, nsys;
1381.1Sscw	size_t argsize;
1391.1Sscw	register_t args[16], rval[2];
1401.1Sscw
1411.1Sscw	nsys = p->p_emul->e_nsysent;
1421.1Sscw	callp = p->p_emul->e_sysent;
1431.1Sscw
1441.1Sscw	/*
1451.1Sscw	 * SunOS passes the syscall-number on the stack, whereas
1461.1Sscw	 * BSD passes it in D0. So, we have to get the real "code"
1471.1Sscw	 * from the stack, and clean up the stack, as SunOS glue
1481.1Sscw	 * code assumes the kernel pops the syscall argument the
1491.1Sscw	 * glue pushed on the stack. Sigh...
1501.1Sscw	 */
1511.1Sscw	code = fuword((caddr_t)frame->f_regs[SP]);
1521.1Sscw
1531.1Sscw	/*
1541.1Sscw	 * XXX
1551.1Sscw	 * Don't do this for sunos_sigreturn, as there's no stored pc
1561.1Sscw	 * on the stack to skip, the argument follows the syscall
1571.1Sscw	 * number without a gap.
1581.1Sscw	 */
1591.1Sscw	if (code != SUNOS_SYS_sigreturn) {
1601.1Sscw		frame->f_regs[SP] += sizeof (int);
1611.1Sscw		/*
1621.1Sscw		 * remember that we adjusted the SP,
1631.1Sscw		 * might have to undo this if the system call
1641.1Sscw		 * returns ERESTART.
1651.1Sscw		 */
1661.4Sthorpej		l->l_md.md_flags |= MDL_STACKADJ;
1671.1Sscw	} else
1681.4Sthorpej		l->l_md.md_flags &= ~MDL_STACKADJ;
1691.1Sscw
1701.1Sscw	params = (caddr_t)frame->f_regs[SP] + sizeof(int);
1711.1Sscw
1721.1Sscw	switch (code) {
1731.1Sscw	case SUNOS_SYS_syscall:
1741.1Sscw		/*
1751.1Sscw		 * Code is first argument, followed by actual args.
1761.1Sscw		 */
1771.1Sscw		code = fuword(params);
1781.1Sscw		params += sizeof(int);
1791.1Sscw		break;
1801.1Sscw	default:
1811.1Sscw		break;
1821.1Sscw	}
1831.1Sscw
1841.1Sscw	if (code < 0 || code >= nsys)
1851.1Sscw		callp += p->p_emul->e_nosys;		/* illegal */
1861.1Sscw	else
1871.1Sscw		callp += code;
1881.1Sscw
1891.1Sscw	argsize = callp->sy_argsize;
1901.1Sscw	if (argsize) {
1911.1Sscw		error = copyin(params, (caddr_t)args, argsize);
1921.1Sscw		if (error)
1931.1Sscw			goto bad;
1941.1Sscw	}
1951.1Sscw
1961.1Sscw#ifdef SYSCALL_DEBUG
1971.4Sthorpej	scdebug_call(l, code, args);
1981.1Sscw#endif
1991.1Sscw
2001.1Sscw	rval[0] = 0;
2011.1Sscw	rval[1] = frame->f_regs[D1];
2021.4Sthorpej	error = (*callp->sy_call)(l, args, rval);
2031.1Sscw
2041.1Sscw	switch (error) {
2051.1Sscw	case 0:
2061.1Sscw		/*
2071.1Sscw		 * Reinitialize proc pointer `p' as it may be different
2081.1Sscw		 * if this is a child returning from fork syscall.
2091.1Sscw		 */
2101.1Sscw		p = curproc;
2111.1Sscw		frame->f_regs[D0] = rval[0];
2121.1Sscw		frame->f_regs[D1] = rval[1];
2131.1Sscw		frame->f_sr &= ~PSL_C;	/* carry bit */
2141.1Sscw		break;
2151.1Sscw	case ERESTART:
2161.1Sscw		/*
2171.1Sscw		 * We always enter through a `trap' instruction, which is 2
2181.1Sscw		 * bytes, so adjust the pc by that amount.
2191.1Sscw		 */
2201.1Sscw		frame->f_pc = frame->f_pc - 2;
2211.1Sscw		break;
2221.1Sscw	case EJUSTRETURN:
2231.1Sscw		/* nothing to do */
2241.1Sscw		break;
2251.1Sscw	default:
2261.1Sscw	bad:
2271.1Sscw		frame->f_regs[D0] = error;
2281.1Sscw		frame->f_sr |= PSL_C;	/* carry bit */
2291.1Sscw		break;
2301.1Sscw	}
2311.1Sscw
2321.1Sscw	/* need new p-value for this */
2331.4Sthorpej	if (l->l_md.md_flags & MDL_STACKADJ) {
2341.4Sthorpej		l->l_md.md_flags &= ~MDL_STACKADJ;
2351.1Sscw		if (error == ERESTART)
2361.1Sscw			frame->f_regs[SP] -= sizeof (int);
2371.1Sscw	}
2381.1Sscw
2391.1Sscw#ifdef SYSCALL_DEBUG
2401.1Sscw	scdebug_ret(p, code, error, rval)
2411.1Sscw#endif
2421.1Sscw}
2431.1Sscw
2441.1Sscwstatic void
2451.4Sthorpejsunos_syscall_fancy(register_t code, struct lwp *l, struct frame *frame)
2461.1Sscw{
2471.4Sthorpej	struct proc *p = l->l_proc;
2481.1Sscw	caddr_t params;
2491.1Sscw	const struct sysent *callp;
2501.1Sscw	int error, nsys;
2511.1Sscw	size_t argsize;
2521.1Sscw	register_t args[16], rval[2];
2531.1Sscw
2541.1Sscw	nsys = p->p_emul->e_nsysent;
2551.1Sscw	callp = p->p_emul->e_sysent;
2561.1Sscw
2571.1Sscw	/*
2581.1Sscw	 * SunOS passes the syscall-number on the stack, whereas
2591.1Sscw	 * BSD passes it in D0. So, we have to get the real "code"
2601.1Sscw	 * from the stack, and clean up the stack, as SunOS glue
2611.1Sscw	 * code assumes the kernel pops the syscall argument the
2621.1Sscw	 * glue pushed on the stack. Sigh...
2631.1Sscw	 */
2641.1Sscw	code = fuword((caddr_t)frame->f_regs[SP]);
2651.1Sscw
2661.1Sscw	/*
2671.1Sscw	 * XXX
2681.1Sscw	 * Don't do this for sunos_sigreturn, as there's no stored pc
2691.1Sscw	 * on the stack to skip, the argument follows the syscall
2701.1Sscw	 * number without a gap.
2711.1Sscw	 */
2721.1Sscw	if (code != SUNOS_SYS_sigreturn) {
2731.1Sscw		frame->f_regs[SP] += sizeof (int);
2741.1Sscw		/*
2751.1Sscw		 * remember that we adjusted the SP,
2761.1Sscw		 * might have to undo this if the system call
2771.1Sscw		 * returns ERESTART.
2781.1Sscw		 */
2791.4Sthorpej		l->l_md.md_flags |= MDL_STACKADJ;
2801.1Sscw	} else
2811.4Sthorpej		l->l_md.md_flags &= ~MDL_STACKADJ;
2821.1Sscw
2831.1Sscw	params = (caddr_t)frame->f_regs[SP] + sizeof(int);
2841.1Sscw
2851.1Sscw	switch (code) {
2861.1Sscw	case SUNOS_SYS_syscall:
2871.1Sscw		/*
2881.1Sscw		 * Code is first argument, followed by actual args.
2891.1Sscw		 */
2901.1Sscw		code = fuword(params);
2911.1Sscw		params += sizeof(int);
2921.1Sscw		break;
2931.1Sscw	default:
2941.1Sscw		break;
2951.1Sscw	}
2961.1Sscw
2971.1Sscw	if (code < 0 || code >= nsys)
2981.1Sscw		callp += p->p_emul->e_nosys;		/* illegal */
2991.1Sscw	else
3001.1Sscw		callp += code;
3011.1Sscw
3021.1Sscw	argsize = callp->sy_argsize;
3031.1Sscw	if (argsize) {
3041.1Sscw		error = copyin(params, (caddr_t)args, argsize);
3051.1Sscw		if (error)
3061.1Sscw			goto bad;
3071.1Sscw	}
3081.1Sscw
3091.4Sthorpej	if ((error = trace_enter(l, code, code, NULL, args, rval)) != 0)
3101.1Sscw		goto bad;
3111.1Sscw
3121.1Sscw	rval[0] = 0;
3131.1Sscw	rval[1] = frame->f_regs[D1];
3141.4Sthorpej	error = (*callp->sy_call)(l, args, rval);
3151.1Sscw
3161.1Sscw	switch (error) {
3171.1Sscw	case 0:
3181.1Sscw		/*
3191.1Sscw		 * Reinitialize proc pointer `p' as it may be different
3201.1Sscw		 * if this is a child returning from fork syscall.
3211.1Sscw		 */
3221.1Sscw		p = curproc;
3231.1Sscw		frame->f_regs[D0] = rval[0];
3241.1Sscw		frame->f_regs[D1] = rval[1];
3251.1Sscw		frame->f_sr &= ~PSL_C;	/* carry bit */
3261.1Sscw		break;
3271.1Sscw	case ERESTART:
3281.1Sscw		/*
3291.1Sscw		 * We always enter through a `trap' instruction, which is 2
3301.1Sscw		 * bytes, so adjust the pc by that amount.
3311.1Sscw		 */
3321.1Sscw		frame->f_pc = frame->f_pc - 2;
3331.1Sscw		break;
3341.1Sscw	case EJUSTRETURN:
3351.1Sscw		/* nothing to do */
3361.1Sscw		break;
3371.1Sscw	default:
3381.1Sscw	bad:
3391.1Sscw		frame->f_regs[D0] = error;
3401.1Sscw		frame->f_sr |= PSL_C;	/* carry bit */
3411.1Sscw		break;
3421.1Sscw	}
3431.1Sscw
3441.1Sscw	/* need new p-value for this */
3451.4Sthorpej	if (l->l_md.md_flags & MDL_STACKADJ) {
3461.4Sthorpej		l->l_md.md_flags &= ~MDL_STACKADJ;
3471.1Sscw		if (error == ERESTART)
3481.1Sscw			frame->f_regs[SP] -= sizeof (int);
3491.1Sscw	}
3501.1Sscw
3511.4Sthorpej	trace_exit(l, code, args, rval, error);
3521.1Sscw}
353