xref: /onnv-gate/usr/src/lib/libproc/common/Psyscall.c (revision 2712:f74a135872bc)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2712Snn35248  * Common Development and Distribution License (the "License").
6*2712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2712Snn35248  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <memory.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <dirent.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate #include <signal.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/uio.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/resource.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include <sys/stack.h>
450Sstevel@tonic-gate #include <sys/fault.h>
460Sstevel@tonic-gate #include <sys/syscall.h>
470Sstevel@tonic-gate #include <sys/sysmacros.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include "libproc.h"
500Sstevel@tonic-gate #include "Pcontrol.h"
510Sstevel@tonic-gate #include "Putil.h"
520Sstevel@tonic-gate #include "P32ton.h"
530Sstevel@tonic-gate #include "Pisadep.h"
540Sstevel@tonic-gate 
550Sstevel@tonic-gate extern sigset_t blockable_sigs;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static void
Pabort_agent(struct ps_prochandle * P)580Sstevel@tonic-gate Pabort_agent(struct ps_prochandle *P)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	int sysnum = P->status.pr_lwp.pr_syscall;
610Sstevel@tonic-gate 	int stop;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	dprintf("agent LWP is asleep in syscall %d\n", sysnum);
640Sstevel@tonic-gate 	(void) Pstop(P, 0);
650Sstevel@tonic-gate 	stop = Psysexit(P, sysnum, TRUE);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (Psetrun(P, 0, PRSABORT) == 0) {
680Sstevel@tonic-gate 		while (Pwait(P, 0) == -1 && errno == EINTR)
690Sstevel@tonic-gate 			continue;
700Sstevel@tonic-gate 		(void) Psysexit(P, sysnum, stop);
710Sstevel@tonic-gate 		dprintf("agent LWP system call aborted\n");
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Create the /proc agent LWP for further operations.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate int
Pcreate_agent(struct ps_prochandle * P)790Sstevel@tonic-gate Pcreate_agent(struct ps_prochandle *P)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	int fd;
82*2712Snn35248 	char pathname[PATH_MAX];
830Sstevel@tonic-gate 	char *fname;
840Sstevel@tonic-gate 	struct {
850Sstevel@tonic-gate 		long	cmd;
860Sstevel@tonic-gate 		prgregset_t regs;
870Sstevel@tonic-gate 	} cmd;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	/*
900Sstevel@tonic-gate 	 * If not first reference, we already have the /proc agent LWP active.
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 	if (P->agentcnt > 0) {
930Sstevel@tonic-gate 		P->agentcnt++;
940Sstevel@tonic-gate 		return (0);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	/*
980Sstevel@tonic-gate 	 * The agent is not available for use as a mortician or as an
990Sstevel@tonic-gate 	 * obstetrician.
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
1020Sstevel@tonic-gate 	    P->state == PS_IDLE) {
1030Sstevel@tonic-gate 		errno = ENOENT;
1040Sstevel@tonic-gate 		return (-1);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * Create the special /proc agent LWP if it doesn't already exist.
1090Sstevel@tonic-gate 	 * Give it the registers of the representative LWP.
1100Sstevel@tonic-gate 	 */
1110Sstevel@tonic-gate 	(void) Pstop(P, 0);
1120Sstevel@tonic-gate 	Psync(P);
1130Sstevel@tonic-gate 	if (!(P->status.pr_lwp.pr_flags & PR_AGENT)) {
1140Sstevel@tonic-gate 		cmd.cmd = PCAGENT;
1150Sstevel@tonic-gate 		(void) memcpy(&cmd.regs, &P->status.pr_lwp.pr_reg[0],
1160Sstevel@tonic-gate 		    sizeof (P->status.pr_lwp.pr_reg));
1170Sstevel@tonic-gate 		if (write(P->ctlfd, &cmd, sizeof (cmd)) != sizeof (cmd))
1180Sstevel@tonic-gate 			goto bad;
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	/* refresh the process status */
1220Sstevel@tonic-gate 	(void) Pstopstatus(P, PCNULL, 0);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	/* open the agent LWP files */
125*2712Snn35248 	(void) snprintf(pathname, sizeof (pathname), "%s/%d/lwp/agent/",
126*2712Snn35248 	    procfs_path, (int)P->pid);
1270Sstevel@tonic-gate 	fname = pathname + strlen(pathname);
1280Sstevel@tonic-gate 	(void) set_minfd();
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	/*
1310Sstevel@tonic-gate 	 * It is difficult to know how to recover from the two errors
1320Sstevel@tonic-gate 	 * that follow.  The agent LWP exists and we need to kill it,
1330Sstevel@tonic-gate 	 * but we can't because we need it active in order to kill it.
1340Sstevel@tonic-gate 	 * We just hope that these failures never occur.
1350Sstevel@tonic-gate 	 */
1360Sstevel@tonic-gate 	(void) strcpy(fname, "lwpstatus");
1370Sstevel@tonic-gate 	if ((fd = open(pathname, O_RDONLY)) < 0 ||
1380Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0)
1390Sstevel@tonic-gate 		goto bad;
1400Sstevel@tonic-gate 	P->agentstatfd = fd;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	(void) strcpy(fname, "lwpctl");
1430Sstevel@tonic-gate 	if ((fd = open(pathname, O_WRONLY)) < 0 ||
1440Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0)
1450Sstevel@tonic-gate 		goto bad;
1460Sstevel@tonic-gate 	P->agentctlfd = fd;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * If the agent is currently asleep in a system call, attempt
1500Sstevel@tonic-gate 	 * to abort the system call so it's ready to serve.
1510Sstevel@tonic-gate 	 */
1520Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_flags & PR_ASLEEP) {
1530Sstevel@tonic-gate 		dprintf("Pcreate_agent: aborting agent syscall\n");
1540Sstevel@tonic-gate 		Pabort_agent(P);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/* get the agent LWP status */
1580Sstevel@tonic-gate 	P->agentcnt++;
1590Sstevel@tonic-gate 	if (Pstopstatus(P, PCNULL, 0) != 0) {
1600Sstevel@tonic-gate 		Pdestroy_agent(P);
1610Sstevel@tonic-gate 		return (-1);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	return (0);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate bad:
1670Sstevel@tonic-gate 	if (P->agentstatfd >= 0)
1680Sstevel@tonic-gate 		(void) close(P->agentstatfd);
1690Sstevel@tonic-gate 	if (P->agentctlfd >= 0)
1700Sstevel@tonic-gate 		(void) close(P->agentctlfd);
1710Sstevel@tonic-gate 	P->agentstatfd = -1;
1720Sstevel@tonic-gate 	P->agentctlfd = -1;
1730Sstevel@tonic-gate 	/* refresh the process status */
1740Sstevel@tonic-gate 	(void) Pstopstatus(P, PCNULL, 0);
1750Sstevel@tonic-gate 	return (-1);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * Decrement the /proc agent agent reference count.
1800Sstevel@tonic-gate  * On last reference, destroy the agent.
1810Sstevel@tonic-gate  */
1820Sstevel@tonic-gate void
Pdestroy_agent(struct ps_prochandle * P)1830Sstevel@tonic-gate Pdestroy_agent(struct ps_prochandle *P)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	if (P->agentcnt > 1)
1860Sstevel@tonic-gate 		P->agentcnt--;
1870Sstevel@tonic-gate 	else {
1880Sstevel@tonic-gate 		int flags;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		Psync(P); /* Flush out any pending changes */
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
1930Sstevel@tonic-gate 		flags = P->status.pr_lwp.pr_flags;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		/*
1960Sstevel@tonic-gate 		 * If the agent is currently asleep in a system call, attempt
1970Sstevel@tonic-gate 		 * to abort the system call so we can terminate the agent.
1980Sstevel@tonic-gate 		 */
1990Sstevel@tonic-gate 		if ((flags & (PR_AGENT|PR_ASLEEP)) == (PR_AGENT|PR_ASLEEP)) {
2000Sstevel@tonic-gate 			dprintf("Pdestroy_agent: aborting agent syscall\n");
2010Sstevel@tonic-gate 			Pabort_agent(P);
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		/*
2050Sstevel@tonic-gate 		 * The agent itself is destroyed by forcing it to execute
2060Sstevel@tonic-gate 		 * the _lwp_exit(2) system call.  Close our agent descriptors
2070Sstevel@tonic-gate 		 * regardless of whether this is successful.
2080Sstevel@tonic-gate 		 */
2090Sstevel@tonic-gate 		(void) pr_lwp_exit(P);
2100Sstevel@tonic-gate 		(void) close(P->agentctlfd);
2110Sstevel@tonic-gate 		(void) close(P->agentstatfd);
2120Sstevel@tonic-gate 		P->agentctlfd = -1;
2130Sstevel@tonic-gate 		P->agentstatfd = -1;
2140Sstevel@tonic-gate 		P->agentcnt = 0;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		/*
2170Sstevel@tonic-gate 		 * Now that (hopefully) the agent has exited, refresh the
2180Sstevel@tonic-gate 		 * status: the representative LWP is no longer the agent.
2190Sstevel@tonic-gate 		 */
2200Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * Execute the syscall instruction.
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate static int
execute(struct ps_prochandle * P,int sysindex)2280Sstevel@tonic-gate execute(struct ps_prochandle *P, int sysindex)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2310Sstevel@tonic-gate 	int washeld = FALSE;
2320Sstevel@tonic-gate 	sigset_t hold;		/* mask of held signals */
2330Sstevel@tonic-gate 	int cursig;
2340Sstevel@tonic-gate 	struct {
2350Sstevel@tonic-gate 		long cmd;
2360Sstevel@tonic-gate 		siginfo_t siginfo;
2370Sstevel@tonic-gate 	} ctl;
2380Sstevel@tonic-gate 	int sentry;		/* old value of stop-on-syscall-entry */
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	sentry = Psysentry(P, sysindex, TRUE);	/* set stop-on-syscall-entry */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * If not already blocked, block all signals now.
2440Sstevel@tonic-gate 	 */
2450Sstevel@tonic-gate 	if (memcmp(&P->status.pr_lwp.pr_lwphold, &blockable_sigs,
2460Sstevel@tonic-gate 	    sizeof (sigset_t)) != 0) {
2470Sstevel@tonic-gate 		hold = P->status.pr_lwp.pr_lwphold;
2480Sstevel@tonic-gate 		P->status.pr_lwp.pr_lwphold = blockable_sigs;
2490Sstevel@tonic-gate 		P->flags |= SETHOLD;
2500Sstevel@tonic-gate 		washeld = TRUE;
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/*
2540Sstevel@tonic-gate 	 * If there is a current signal, remember it and cancel it.
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	if ((cursig = P->status.pr_lwp.pr_cursig) != 0) {
2570Sstevel@tonic-gate 		ctl.cmd = PCSSIG;
2580Sstevel@tonic-gate 		ctl.siginfo = P->status.pr_lwp.pr_info;
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (Psetrun(P, 0, PRCSIG | PRCFAULT) == -1)
2620Sstevel@tonic-gate 		goto bad;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	while (P->state == PS_RUN) {
2650Sstevel@tonic-gate 		(void) Pwait(P, 0);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	if (P->state != PS_STOP)
2680Sstevel@tonic-gate 		goto bad;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (cursig)				/* restore cursig */
2710Sstevel@tonic-gate 		(void) write(ctlfd, &ctl, sizeof (ctl));
2720Sstevel@tonic-gate 	if (washeld) {		/* restore the signal mask if we set it */
2730Sstevel@tonic-gate 		P->status.pr_lwp.pr_lwphold = hold;
2740Sstevel@tonic-gate 		P->flags |= SETHOLD;
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	(void) Psysentry(P, sysindex, sentry);	/* restore sysentry stop */
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_why  == PR_SYSENTRY &&
2800Sstevel@tonic-gate 	    P->status.pr_lwp.pr_what == sysindex)
2810Sstevel@tonic-gate 		return (0);
2820Sstevel@tonic-gate bad:
2830Sstevel@tonic-gate 	return (-1);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * Perform system call in controlled process.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate int
Psyscall(struct ps_prochandle * P,sysret_t * rval,int sysindex,uint_t nargs,argdes_t * argp)2910Sstevel@tonic-gate Psyscall(struct ps_prochandle *P,
2920Sstevel@tonic-gate 	sysret_t *rval,		/* syscall return values */
2930Sstevel@tonic-gate 	int sysindex,		/* system call index */
2940Sstevel@tonic-gate 	uint_t nargs,		/* number of arguments to system call */
2950Sstevel@tonic-gate 	argdes_t *argp)		/* argument descriptor array */
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	int agent_created = FALSE;
2980Sstevel@tonic-gate 	pstatus_t save_pstatus;
2990Sstevel@tonic-gate 	argdes_t *adp;			/* pointer to argument descriptor */
3000Sstevel@tonic-gate 	int i;				/* general index value */
3010Sstevel@tonic-gate 	int model;			/* data model */
3020Sstevel@tonic-gate 	int error = 0;			/* syscall errno */
3030Sstevel@tonic-gate 	int Perr = 0;			/* local error number */
3040Sstevel@tonic-gate 	int sexit;			/* old value of stop-on-syscall-exit */
3050Sstevel@tonic-gate 	prgreg_t sp;			/* adjusted stack pointer */
3060Sstevel@tonic-gate 	prgreg_t ap;			/* adjusted argument pointer */
3070Sstevel@tonic-gate 	sigset_t unblock;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	rval->sys_rval1 = 0;		/* initialize return values */
3120Sstevel@tonic-gate 	rval->sys_rval2 = 0;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (sysindex <= 0 || sysindex > PRMAXSYS || nargs > MAXARGS)
3150Sstevel@tonic-gate 		goto bad1;	/* programming error */
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD || P->state == PS_IDLE)
3180Sstevel@tonic-gate 		goto bad1;	/* dead processes can't perform system calls */
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	model = P->status.pr_dmodel;
3210Sstevel@tonic-gate #ifndef _LP64
3220Sstevel@tonic-gate 	/* We must be a 64-bit process to deal with a 64-bit process */
3230Sstevel@tonic-gate 	if (model == PR_MODEL_LP64)
3240Sstevel@tonic-gate 		goto bad9;
3250Sstevel@tonic-gate #endif
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	/*
3280Sstevel@tonic-gate 	 * Create the /proc agent LWP in the process to do all the work.
3290Sstevel@tonic-gate 	 * (It may already exist; nested create/destroy is permitted
3300Sstevel@tonic-gate 	 * by virtue of the reference count.)
3310Sstevel@tonic-gate 	 */
3320Sstevel@tonic-gate 	if (Pcreate_agent(P) != 0)
3330Sstevel@tonic-gate 		goto bad8;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * Save agent's status to restore on exit.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	agent_created = TRUE;
3390Sstevel@tonic-gate 	save_pstatus = P->status;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (P->state != PS_STOP ||		/* check state of LWP */
3420Sstevel@tonic-gate 	    (P->status.pr_flags & PR_ASLEEP))
3430Sstevel@tonic-gate 		goto bad2;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if (Pscantext(P))			/* bad text ? */
3460Sstevel@tonic-gate 		goto bad3;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/*
3490Sstevel@tonic-gate 	 * Validate arguments and compute the stack frame parameters.
3500Sstevel@tonic-gate 	 * Begin with the current stack pointer.
3510Sstevel@tonic-gate 	 */
3520Sstevel@tonic-gate #ifdef _LP64
3530Sstevel@tonic-gate 	if (model == PR_MODEL_LP64) {
3540Sstevel@tonic-gate 		sp = P->status.pr_lwp.pr_reg[R_SP] + STACK_BIAS;
3550Sstevel@tonic-gate 		sp = PSTACK_ALIGN64(sp);
3560Sstevel@tonic-gate 	} else {
3570Sstevel@tonic-gate #endif
3580Sstevel@tonic-gate 		sp = (uint32_t)P->status.pr_lwp.pr_reg[R_SP];
3590Sstevel@tonic-gate 		sp = PSTACK_ALIGN32(sp);
3600Sstevel@tonic-gate #ifdef _LP64
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate #endif
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * For each AT_BYREF argument, compute the necessary
3660Sstevel@tonic-gate 	 * stack space and the object's stack address.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++) {
3690Sstevel@tonic-gate 		rval->sys_rval1 = i;		/* in case of error */
3700Sstevel@tonic-gate 		switch (adp->arg_type) {
3710Sstevel@tonic-gate 		default:			/* programming error */
3720Sstevel@tonic-gate 			goto bad4;
3730Sstevel@tonic-gate 		case AT_BYVAL:			/* simple argument */
3740Sstevel@tonic-gate 			break;
3750Sstevel@tonic-gate 		case AT_BYREF:			/* must allocate space */
3760Sstevel@tonic-gate 			switch (adp->arg_inout) {
3770Sstevel@tonic-gate 			case AI_INPUT:
3780Sstevel@tonic-gate 			case AI_OUTPUT:
3790Sstevel@tonic-gate 			case AI_INOUT:
3800Sstevel@tonic-gate 				if (adp->arg_object == NULL)
3810Sstevel@tonic-gate 					goto bad5;	/* programming error */
3820Sstevel@tonic-gate 				break;
3830Sstevel@tonic-gate 			default:		/* programming error */
3840Sstevel@tonic-gate 				goto bad6;
3850Sstevel@tonic-gate 			}
3860Sstevel@tonic-gate 			/* allocate stack space for BYREF argument */
3870Sstevel@tonic-gate 			if (adp->arg_size == 0 || adp->arg_size > MAXARGL)
3880Sstevel@tonic-gate 				goto bad7;	/* programming error */
3890Sstevel@tonic-gate #ifdef _LP64
3900Sstevel@tonic-gate 			if (model == PR_MODEL_LP64)
3910Sstevel@tonic-gate 				sp = PSTACK_ALIGN64(sp - adp->arg_size);
3920Sstevel@tonic-gate 			else
3930Sstevel@tonic-gate #endif
3940Sstevel@tonic-gate 				sp = PSTACK_ALIGN32(sp - adp->arg_size);
3950Sstevel@tonic-gate 			adp->arg_value = sp;	/* stack address for object */
3960Sstevel@tonic-gate 			break;
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 	rval->sys_rval1 = 0;			/* in case of error */
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * Point of no return.
4020Sstevel@tonic-gate 	 * Perform the system call entry, adjusting %sp.
4030Sstevel@tonic-gate 	 * This moves the LWP to the stopped-on-syscall-entry state
4040Sstevel@tonic-gate 	 * just before the arguments to the system call are fetched.
4050Sstevel@tonic-gate 	 */
4060Sstevel@tonic-gate 	ap = Psyscall_setup(P, nargs, sysindex, sp);
4070Sstevel@tonic-gate 	P->flags |= SETREGS;	/* set registers before continuing */
4080Sstevel@tonic-gate 	dprintf("Psyscall(): execute(sysindex = %d)\n", sysindex);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/*
4110Sstevel@tonic-gate 	 * Execute the syscall instruction and stop on syscall entry.
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	if (execute(P, sysindex) != 0 ||
4140Sstevel@tonic-gate 	    (!Pissyscall(P, P->status.pr_lwp.pr_reg[R_PC]) &&
4150Sstevel@tonic-gate 	    !Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], NULL)))
4160Sstevel@tonic-gate 		goto bad10;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	dprintf("Psyscall(): copying arguments\n");
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * The LWP is stopped at syscall entry.
4220Sstevel@tonic-gate 	 * Copy objects to stack frame for each argument.
4230Sstevel@tonic-gate 	 */
4240Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++) {
4250Sstevel@tonic-gate 		rval->sys_rval1 = i;		/* in case of error */
4260Sstevel@tonic-gate 		if (adp->arg_type != AT_BYVAL &&
4270Sstevel@tonic-gate 		    adp->arg_inout != AI_OUTPUT) {
4280Sstevel@tonic-gate 			/* copy input byref parameter to process */
4290Sstevel@tonic-gate 			if (Pwrite(P, adp->arg_object, adp->arg_size,
4300Sstevel@tonic-gate 			    (uintptr_t)adp->arg_value) != adp->arg_size)
4310Sstevel@tonic-gate 				goto bad17;
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 	rval->sys_rval1 = 0;			/* in case of error */
4350Sstevel@tonic-gate 	if (Psyscall_copyinargs(P, nargs, argp, ap) != 0)
4360Sstevel@tonic-gate 		goto bad18;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * Complete the system call.
4400Sstevel@tonic-gate 	 * This moves the LWP to the stopped-on-syscall-exit state.
4410Sstevel@tonic-gate 	 */
4420Sstevel@tonic-gate 	dprintf("Psyscall(): set running at sysentry\n");
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	sexit = Psysexit(P, sysindex, TRUE);	/* catch this syscall exit */
4450Sstevel@tonic-gate 	do {
4460Sstevel@tonic-gate 		if (Psetrun(P, 0, 0) == -1)
4470Sstevel@tonic-gate 			goto bad21;
4480Sstevel@tonic-gate 		while (P->state == PS_RUN)
4490Sstevel@tonic-gate 			(void) Pwait(P, 0);
4500Sstevel@tonic-gate 	} while (P->state == PS_STOP && P->status.pr_lwp.pr_why != PR_SYSEXIT);
4510Sstevel@tonic-gate 	(void) Psysexit(P, sysindex, sexit);	/* restore original setting */
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * If the system call was _lwp_exit(), we expect that our last call
4550Sstevel@tonic-gate 	 * to Pwait() will yield ENOENT because the LWP no longer exists.
4560Sstevel@tonic-gate 	 */
4570Sstevel@tonic-gate 	if (sysindex == SYS_lwp_exit && errno == ENOENT) {
4580Sstevel@tonic-gate 		dprintf("Psyscall(): _lwp_exit successful\n");
4590Sstevel@tonic-gate 		rval->sys_rval1 = rval->sys_rval2 = 0;
4600Sstevel@tonic-gate 		goto out;
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	if (P->state != PS_STOP || P->status.pr_lwp.pr_why != PR_SYSEXIT)
4640Sstevel@tonic-gate 		goto bad22;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_what != sysindex)
4670Sstevel@tonic-gate 		goto bad23;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (!Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], NULL)) {
4700Sstevel@tonic-gate 		dprintf("Pissyscall_prev() failed\n");
4710Sstevel@tonic-gate 		goto bad24;
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	dprintf("Psyscall(): caught at sysexit\n");
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/*
4770Sstevel@tonic-gate 	 * For each argument.
4780Sstevel@tonic-gate 	 */
4790Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++) {
4800Sstevel@tonic-gate 		rval->sys_rval1 = i;		/* in case of error */
4810Sstevel@tonic-gate 		if (adp->arg_type != AT_BYVAL &&
4820Sstevel@tonic-gate 		    adp->arg_inout != AI_INPUT) {
4830Sstevel@tonic-gate 			/* copy output byref parameter from process */
4840Sstevel@tonic-gate 			if (Pread(P, adp->arg_object, adp->arg_size,
4850Sstevel@tonic-gate 			    (uintptr_t)adp->arg_value) != adp->arg_size)
4860Sstevel@tonic-gate 				goto bad25;
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (Psyscall_copyoutargs(P, nargs, argp, ap) != 0)
4910Sstevel@tonic-gate 		goto bad26;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	/*
4940Sstevel@tonic-gate 	 * Get the return values from the syscall.
4950Sstevel@tonic-gate 	 */
4960Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_errno) {	/* error return */
4970Sstevel@tonic-gate 		error = P->status.pr_lwp.pr_errno;
4980Sstevel@tonic-gate 		rval->sys_rval1 = -1L;
4990Sstevel@tonic-gate 		rval->sys_rval2 = -1L;
5000Sstevel@tonic-gate 		dprintf("Psyscall(%d) fails with errno %d\n",
5010Sstevel@tonic-gate 		    sysindex, error);
5020Sstevel@tonic-gate 	} else {				/* normal return */
5030Sstevel@tonic-gate 		rval->sys_rval1 = P->status.pr_lwp.pr_rval1;
5040Sstevel@tonic-gate 		rval->sys_rval2 = P->status.pr_lwp.pr_rval2;
5050Sstevel@tonic-gate 		dprintf("Psyscall(%d) returns 0x%lx 0x%lx\n", sysindex,
5060Sstevel@tonic-gate 		    P->status.pr_lwp.pr_rval1, P->status.pr_lwp.pr_rval2);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	goto out;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate bad26:	Perr++;
5120Sstevel@tonic-gate bad25:	Perr++;
5130Sstevel@tonic-gate bad24:	Perr++;
5140Sstevel@tonic-gate bad23:	Perr++;
5150Sstevel@tonic-gate bad22:	Perr++;
5160Sstevel@tonic-gate bad21:	Perr++;
5170Sstevel@tonic-gate 	Perr++;
5180Sstevel@tonic-gate 	Perr++;
5190Sstevel@tonic-gate bad18:	Perr++;
5200Sstevel@tonic-gate bad17:	Perr++;
5210Sstevel@tonic-gate 	Perr++;
5220Sstevel@tonic-gate 	Perr++;
5230Sstevel@tonic-gate 	Perr++;
5240Sstevel@tonic-gate 	Perr++;
5250Sstevel@tonic-gate 	Perr++;
5260Sstevel@tonic-gate 	Perr++;
5270Sstevel@tonic-gate bad10:	Perr++;
5280Sstevel@tonic-gate bad9:	Perr++;
5290Sstevel@tonic-gate bad8:	Perr++;
5300Sstevel@tonic-gate bad7:	Perr++;
5310Sstevel@tonic-gate bad6:	Perr++;
5320Sstevel@tonic-gate bad5:	Perr++;
5330Sstevel@tonic-gate bad4:	Perr++;
5340Sstevel@tonic-gate bad3:	Perr++;
5350Sstevel@tonic-gate bad2:	Perr++;
5360Sstevel@tonic-gate bad1:	Perr++;
5370Sstevel@tonic-gate 	error = -1;
5380Sstevel@tonic-gate 	dprintf("Psyscall(%d) fails with local error %d\n", sysindex, Perr);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate out:
5410Sstevel@tonic-gate 	/*
5420Sstevel@tonic-gate 	 * Destroy the /proc agent LWP now (or just bump down the ref count).
5430Sstevel@tonic-gate 	 */
5440Sstevel@tonic-gate 	if (agent_created) {
5450Sstevel@tonic-gate 		if (P->state != PS_UNDEAD) {
5460Sstevel@tonic-gate 			P->status = save_pstatus;
5470Sstevel@tonic-gate 			P->flags |= SETREGS;
5480Sstevel@tonic-gate 			Psync(P);
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 		Pdestroy_agent(P);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
5540Sstevel@tonic-gate 	return (error);
5550Sstevel@tonic-gate }
556