xref: /titanic_50/usr/src/lib/libproc/common/Psyscall.c (revision f6dcd367bb2ab63785ea4839111eb4846066404d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
229acbbeafSnn35248  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25*f6dcd367SJoshua M. Clulow /*
26*f6dcd367SJoshua M. Clulow  * Copyright (c) 2013, Joyent Inc. All rights reserved.
27*f6dcd367SJoshua M. Clulow  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <memory.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <dirent.h>
387c478bd9Sstevel@tonic-gate #include <limits.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/uio.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/resource.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/stack.h>
467c478bd9Sstevel@tonic-gate #include <sys/fault.h>
477c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
487c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include "libproc.h"
517c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
527c478bd9Sstevel@tonic-gate #include "Putil.h"
537c478bd9Sstevel@tonic-gate #include "P32ton.h"
547c478bd9Sstevel@tonic-gate #include "Pisadep.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate extern sigset_t blockable_sigs;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static void
597c478bd9Sstevel@tonic-gate Pabort_agent(struct ps_prochandle *P)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	int sysnum = P->status.pr_lwp.pr_syscall;
627c478bd9Sstevel@tonic-gate 	int stop;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	dprintf("agent LWP is asleep in syscall %d\n", sysnum);
657c478bd9Sstevel@tonic-gate 	(void) Pstop(P, 0);
667c478bd9Sstevel@tonic-gate 	stop = Psysexit(P, sysnum, TRUE);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (Psetrun(P, 0, PRSABORT) == 0) {
697c478bd9Sstevel@tonic-gate 		while (Pwait(P, 0) == -1 && errno == EINTR)
707c478bd9Sstevel@tonic-gate 			continue;
717c478bd9Sstevel@tonic-gate 		(void) Psysexit(P, sysnum, stop);
727c478bd9Sstevel@tonic-gate 		dprintf("agent LWP system call aborted\n");
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Create the /proc agent LWP for further operations.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate int
807c478bd9Sstevel@tonic-gate Pcreate_agent(struct ps_prochandle *P)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	int fd;
839acbbeafSnn35248 	char pathname[PATH_MAX];
847c478bd9Sstevel@tonic-gate 	char *fname;
857c478bd9Sstevel@tonic-gate 	struct {
867c478bd9Sstevel@tonic-gate 		long	cmd;
877c478bd9Sstevel@tonic-gate 		prgregset_t regs;
887c478bd9Sstevel@tonic-gate 	} cmd;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * If not first reference, we already have the /proc agent LWP active.
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	if (P->agentcnt > 0) {
947c478bd9Sstevel@tonic-gate 		P->agentcnt++;
957c478bd9Sstevel@tonic-gate 		return (0);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/*
997c478bd9Sstevel@tonic-gate 	 * The agent is not available for use as a mortician or as an
1007c478bd9Sstevel@tonic-gate 	 * obstetrician.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
1037c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE) {
1047c478bd9Sstevel@tonic-gate 		errno = ENOENT;
1057c478bd9Sstevel@tonic-gate 		return (-1);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * Create the special /proc agent LWP if it doesn't already exist.
1107c478bd9Sstevel@tonic-gate 	 * Give it the registers of the representative LWP.
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 	(void) Pstop(P, 0);
1137c478bd9Sstevel@tonic-gate 	Psync(P);
1147c478bd9Sstevel@tonic-gate 	if (!(P->status.pr_lwp.pr_flags & PR_AGENT)) {
1157c478bd9Sstevel@tonic-gate 		cmd.cmd = PCAGENT;
1167c478bd9Sstevel@tonic-gate 		(void) memcpy(&cmd.regs, &P->status.pr_lwp.pr_reg[0],
1177c478bd9Sstevel@tonic-gate 		    sizeof (P->status.pr_lwp.pr_reg));
1187c478bd9Sstevel@tonic-gate 		if (write(P->ctlfd, &cmd, sizeof (cmd)) != sizeof (cmd))
1197c478bd9Sstevel@tonic-gate 			goto bad;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/* refresh the process status */
1237c478bd9Sstevel@tonic-gate 	(void) Pstopstatus(P, PCNULL, 0);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	/* open the agent LWP files */
1269acbbeafSnn35248 	(void) snprintf(pathname, sizeof (pathname), "%s/%d/lwp/agent/",
1279acbbeafSnn35248 	    procfs_path, (int)P->pid);
1287c478bd9Sstevel@tonic-gate 	fname = pathname + strlen(pathname);
1297c478bd9Sstevel@tonic-gate 	(void) set_minfd();
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * It is difficult to know how to recover from the two errors
1337c478bd9Sstevel@tonic-gate 	 * that follow.  The agent LWP exists and we need to kill it,
1347c478bd9Sstevel@tonic-gate 	 * but we can't because we need it active in order to kill it.
1357c478bd9Sstevel@tonic-gate 	 * We just hope that these failures never occur.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "lwpstatus");
1387c478bd9Sstevel@tonic-gate 	if ((fd = open(pathname, O_RDONLY)) < 0 ||
1397c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0)
1407c478bd9Sstevel@tonic-gate 		goto bad;
1417c478bd9Sstevel@tonic-gate 	P->agentstatfd = fd;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "lwpctl");
1447c478bd9Sstevel@tonic-gate 	if ((fd = open(pathname, O_WRONLY)) < 0 ||
1457c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0)
1467c478bd9Sstevel@tonic-gate 		goto bad;
1477c478bd9Sstevel@tonic-gate 	P->agentctlfd = fd;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * If the agent is currently asleep in a system call, attempt
1517c478bd9Sstevel@tonic-gate 	 * to abort the system call so it's ready to serve.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_flags & PR_ASLEEP) {
1547c478bd9Sstevel@tonic-gate 		dprintf("Pcreate_agent: aborting agent syscall\n");
1557c478bd9Sstevel@tonic-gate 		Pabort_agent(P);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* get the agent LWP status */
1597c478bd9Sstevel@tonic-gate 	P->agentcnt++;
1607c478bd9Sstevel@tonic-gate 	if (Pstopstatus(P, PCNULL, 0) != 0) {
1617c478bd9Sstevel@tonic-gate 		Pdestroy_agent(P);
1627c478bd9Sstevel@tonic-gate 		return (-1);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (0);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate bad:
1687c478bd9Sstevel@tonic-gate 	if (P->agentstatfd >= 0)
1697c478bd9Sstevel@tonic-gate 		(void) close(P->agentstatfd);
1707c478bd9Sstevel@tonic-gate 	if (P->agentctlfd >= 0)
1717c478bd9Sstevel@tonic-gate 		(void) close(P->agentctlfd);
1727c478bd9Sstevel@tonic-gate 	P->agentstatfd = -1;
1737c478bd9Sstevel@tonic-gate 	P->agentctlfd = -1;
1747c478bd9Sstevel@tonic-gate 	/* refresh the process status */
1757c478bd9Sstevel@tonic-gate 	(void) Pstopstatus(P, PCNULL, 0);
1767c478bd9Sstevel@tonic-gate 	return (-1);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Decrement the /proc agent agent reference count.
1817c478bd9Sstevel@tonic-gate  * On last reference, destroy the agent.
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate void
1847c478bd9Sstevel@tonic-gate Pdestroy_agent(struct ps_prochandle *P)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	if (P->agentcnt > 1)
1877c478bd9Sstevel@tonic-gate 		P->agentcnt--;
1887c478bd9Sstevel@tonic-gate 	else {
1897c478bd9Sstevel@tonic-gate 		int flags;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		Psync(P); /* Flush out any pending changes */
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
1947c478bd9Sstevel@tonic-gate 		flags = P->status.pr_lwp.pr_flags;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		/*
1977c478bd9Sstevel@tonic-gate 		 * If the agent is currently asleep in a system call, attempt
1987c478bd9Sstevel@tonic-gate 		 * to abort the system call so we can terminate the agent.
1997c478bd9Sstevel@tonic-gate 		 */
2007c478bd9Sstevel@tonic-gate 		if ((flags & (PR_AGENT|PR_ASLEEP)) == (PR_AGENT|PR_ASLEEP)) {
2017c478bd9Sstevel@tonic-gate 			dprintf("Pdestroy_agent: aborting agent syscall\n");
2027c478bd9Sstevel@tonic-gate 			Pabort_agent(P);
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		/*
2067c478bd9Sstevel@tonic-gate 		 * The agent itself is destroyed by forcing it to execute
2077c478bd9Sstevel@tonic-gate 		 * the _lwp_exit(2) system call.  Close our agent descriptors
2087c478bd9Sstevel@tonic-gate 		 * regardless of whether this is successful.
2097c478bd9Sstevel@tonic-gate 		 */
2107c478bd9Sstevel@tonic-gate 		(void) pr_lwp_exit(P);
2117c478bd9Sstevel@tonic-gate 		(void) close(P->agentctlfd);
2127c478bd9Sstevel@tonic-gate 		(void) close(P->agentstatfd);
2137c478bd9Sstevel@tonic-gate 		P->agentctlfd = -1;
2147c478bd9Sstevel@tonic-gate 		P->agentstatfd = -1;
2157c478bd9Sstevel@tonic-gate 		P->agentcnt = 0;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		/*
2187c478bd9Sstevel@tonic-gate 		 * Now that (hopefully) the agent has exited, refresh the
2197c478bd9Sstevel@tonic-gate 		 * status: the representative LWP is no longer the agent.
2207c478bd9Sstevel@tonic-gate 		 */
2217c478bd9Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * Execute the syscall instruction.
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate static int
2297c478bd9Sstevel@tonic-gate execute(struct ps_prochandle *P, int sysindex)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2327c478bd9Sstevel@tonic-gate 	int washeld = FALSE;
2337c478bd9Sstevel@tonic-gate 	sigset_t hold;		/* mask of held signals */
2347c478bd9Sstevel@tonic-gate 	int cursig;
2357c478bd9Sstevel@tonic-gate 	struct {
2367c478bd9Sstevel@tonic-gate 		long cmd;
2377c478bd9Sstevel@tonic-gate 		siginfo_t siginfo;
2387c478bd9Sstevel@tonic-gate 	} ctl;
2397c478bd9Sstevel@tonic-gate 	int sentry;		/* old value of stop-on-syscall-entry */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	sentry = Psysentry(P, sysindex, TRUE);	/* set stop-on-syscall-entry */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * If not already blocked, block all signals now.
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate 	if (memcmp(&P->status.pr_lwp.pr_lwphold, &blockable_sigs,
2477c478bd9Sstevel@tonic-gate 	    sizeof (sigset_t)) != 0) {
2487c478bd9Sstevel@tonic-gate 		hold = P->status.pr_lwp.pr_lwphold;
2497c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_lwphold = blockable_sigs;
2507c478bd9Sstevel@tonic-gate 		P->flags |= SETHOLD;
2517c478bd9Sstevel@tonic-gate 		washeld = TRUE;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * If there is a current signal, remember it and cancel it.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	if ((cursig = P->status.pr_lwp.pr_cursig) != 0) {
2587c478bd9Sstevel@tonic-gate 		ctl.cmd = PCSSIG;
2597c478bd9Sstevel@tonic-gate 		ctl.siginfo = P->status.pr_lwp.pr_info;
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (Psetrun(P, 0, PRCSIG | PRCFAULT) == -1)
2637c478bd9Sstevel@tonic-gate 		goto bad;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	while (P->state == PS_RUN) {
2667c478bd9Sstevel@tonic-gate 		(void) Pwait(P, 0);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP)
2697c478bd9Sstevel@tonic-gate 		goto bad;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (cursig)				/* restore cursig */
2727c478bd9Sstevel@tonic-gate 		(void) write(ctlfd, &ctl, sizeof (ctl));
2737c478bd9Sstevel@tonic-gate 	if (washeld) {		/* restore the signal mask if we set it */
2747c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_lwphold = hold;
2757c478bd9Sstevel@tonic-gate 		P->flags |= SETHOLD;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	(void) Psysentry(P, sysindex, sentry);	/* restore sysentry stop */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_why  == PR_SYSENTRY &&
2817c478bd9Sstevel@tonic-gate 	    P->status.pr_lwp.pr_what == sysindex)
2827c478bd9Sstevel@tonic-gate 		return (0);
2837c478bd9Sstevel@tonic-gate bad:
2847c478bd9Sstevel@tonic-gate 	return (-1);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Perform system call in controlled process.
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate int
2927c478bd9Sstevel@tonic-gate Psyscall(struct ps_prochandle *P,
2937c478bd9Sstevel@tonic-gate 	sysret_t *rval,		/* syscall return values */
2947c478bd9Sstevel@tonic-gate 	int sysindex,		/* system call index */
2957c478bd9Sstevel@tonic-gate 	uint_t nargs,		/* number of arguments to system call */
2967c478bd9Sstevel@tonic-gate 	argdes_t *argp)		/* argument descriptor array */
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	int agent_created = FALSE;
2997c478bd9Sstevel@tonic-gate 	pstatus_t save_pstatus;
3007c478bd9Sstevel@tonic-gate 	argdes_t *adp;			/* pointer to argument descriptor */
3017c478bd9Sstevel@tonic-gate 	int i;				/* general index value */
3027c478bd9Sstevel@tonic-gate 	int model;			/* data model */
3037c478bd9Sstevel@tonic-gate 	int error = 0;			/* syscall errno */
3047c478bd9Sstevel@tonic-gate 	int Perr = 0;			/* local error number */
3057c478bd9Sstevel@tonic-gate 	int sexit;			/* old value of stop-on-syscall-exit */
3067c478bd9Sstevel@tonic-gate 	prgreg_t sp;			/* adjusted stack pointer */
3077c478bd9Sstevel@tonic-gate 	prgreg_t ap;			/* adjusted argument pointer */
3087c478bd9Sstevel@tonic-gate 	sigset_t unblock;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	rval->sys_rval1 = 0;		/* initialize return values */
3137c478bd9Sstevel@tonic-gate 	rval->sys_rval2 = 0;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (sysindex <= 0 || sysindex > PRMAXSYS || nargs > MAXARGS)
3167c478bd9Sstevel@tonic-gate 		goto bad1;	/* programming error */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD || P->state == PS_IDLE)
3197c478bd9Sstevel@tonic-gate 		goto bad1;	/* dead processes can't perform system calls */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	model = P->status.pr_dmodel;
3227c478bd9Sstevel@tonic-gate #ifndef _LP64
3237c478bd9Sstevel@tonic-gate 	/* We must be a 64-bit process to deal with a 64-bit process */
3247c478bd9Sstevel@tonic-gate 	if (model == PR_MODEL_LP64)
3257c478bd9Sstevel@tonic-gate 		goto bad9;
3267c478bd9Sstevel@tonic-gate #endif
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Create the /proc agent LWP in the process to do all the work.
3307c478bd9Sstevel@tonic-gate 	 * (It may already exist; nested create/destroy is permitted
3317c478bd9Sstevel@tonic-gate 	 * by virtue of the reference count.)
3327c478bd9Sstevel@tonic-gate 	 */
3337c478bd9Sstevel@tonic-gate 	if (Pcreate_agent(P) != 0)
3347c478bd9Sstevel@tonic-gate 		goto bad8;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * Save agent's status to restore on exit.
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 	agent_created = TRUE;
3407c478bd9Sstevel@tonic-gate 	save_pstatus = P->status;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP ||		/* check state of LWP */
3437c478bd9Sstevel@tonic-gate 	    (P->status.pr_flags & PR_ASLEEP))
3447c478bd9Sstevel@tonic-gate 		goto bad2;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (Pscantext(P))			/* bad text ? */
3477c478bd9Sstevel@tonic-gate 		goto bad3;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Validate arguments and compute the stack frame parameters.
3517c478bd9Sstevel@tonic-gate 	 * Begin with the current stack pointer.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate #ifdef _LP64
3547c478bd9Sstevel@tonic-gate 	if (model == PR_MODEL_LP64) {
3557c478bd9Sstevel@tonic-gate 		sp = P->status.pr_lwp.pr_reg[R_SP] + STACK_BIAS;
356*f6dcd367SJoshua M. Clulow #if defined(__amd64)
357*f6dcd367SJoshua M. Clulow 		/*
358*f6dcd367SJoshua M. Clulow 		 * To offset the expense of computerised subtraction, the AMD64
359*f6dcd367SJoshua M. Clulow 		 * ABI allows a process the use of a 128-byte area beyond the
360*f6dcd367SJoshua M. Clulow 		 * location pointed to by %rsp.  We must advance the agent's
361*f6dcd367SJoshua M. Clulow 		 * stack pointer by at least the size of this region or else it
362*f6dcd367SJoshua M. Clulow 		 * may corrupt this temporary storage.
363*f6dcd367SJoshua M. Clulow 		 */
364*f6dcd367SJoshua M. Clulow 		sp -= STACK_RESERVE64;
365*f6dcd367SJoshua M. Clulow #endif
3667c478bd9Sstevel@tonic-gate 		sp = PSTACK_ALIGN64(sp);
3677c478bd9Sstevel@tonic-gate 	} else {
3687c478bd9Sstevel@tonic-gate #endif
3697c478bd9Sstevel@tonic-gate 		sp = (uint32_t)P->status.pr_lwp.pr_reg[R_SP];
3707c478bd9Sstevel@tonic-gate 		sp = PSTACK_ALIGN32(sp);
3717c478bd9Sstevel@tonic-gate #ifdef _LP64
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * For each AT_BYREF argument, compute the necessary
3777c478bd9Sstevel@tonic-gate 	 * stack space and the object's stack address.
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++) {
3807c478bd9Sstevel@tonic-gate 		rval->sys_rval1 = i;		/* in case of error */
3817c478bd9Sstevel@tonic-gate 		switch (adp->arg_type) {
3827c478bd9Sstevel@tonic-gate 		default:			/* programming error */
3837c478bd9Sstevel@tonic-gate 			goto bad4;
3847c478bd9Sstevel@tonic-gate 		case AT_BYVAL:			/* simple argument */
3857c478bd9Sstevel@tonic-gate 			break;
3867c478bd9Sstevel@tonic-gate 		case AT_BYREF:			/* must allocate space */
3877c478bd9Sstevel@tonic-gate 			switch (adp->arg_inout) {
3887c478bd9Sstevel@tonic-gate 			case AI_INPUT:
3897c478bd9Sstevel@tonic-gate 			case AI_OUTPUT:
3907c478bd9Sstevel@tonic-gate 			case AI_INOUT:
3917c478bd9Sstevel@tonic-gate 				if (adp->arg_object == NULL)
3927c478bd9Sstevel@tonic-gate 					goto bad5;	/* programming error */
3937c478bd9Sstevel@tonic-gate 				break;
3947c478bd9Sstevel@tonic-gate 			default:		/* programming error */
3957c478bd9Sstevel@tonic-gate 				goto bad6;
3967c478bd9Sstevel@tonic-gate 			}
3977c478bd9Sstevel@tonic-gate 			/* allocate stack space for BYREF argument */
3987c478bd9Sstevel@tonic-gate 			if (adp->arg_size == 0 || adp->arg_size > MAXARGL)
3997c478bd9Sstevel@tonic-gate 				goto bad7;	/* programming error */
4007c478bd9Sstevel@tonic-gate #ifdef _LP64
4017c478bd9Sstevel@tonic-gate 			if (model == PR_MODEL_LP64)
4027c478bd9Sstevel@tonic-gate 				sp = PSTACK_ALIGN64(sp - adp->arg_size);
4037c478bd9Sstevel@tonic-gate 			else
4047c478bd9Sstevel@tonic-gate #endif
4057c478bd9Sstevel@tonic-gate 				sp = PSTACK_ALIGN32(sp - adp->arg_size);
4067c478bd9Sstevel@tonic-gate 			adp->arg_value = sp;	/* stack address for object */
4077c478bd9Sstevel@tonic-gate 			break;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	rval->sys_rval1 = 0;			/* in case of error */
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * Point of no return.
4137c478bd9Sstevel@tonic-gate 	 * Perform the system call entry, adjusting %sp.
4147c478bd9Sstevel@tonic-gate 	 * This moves the LWP to the stopped-on-syscall-entry state
4157c478bd9Sstevel@tonic-gate 	 * just before the arguments to the system call are fetched.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	ap = Psyscall_setup(P, nargs, sysindex, sp);
4187c478bd9Sstevel@tonic-gate 	P->flags |= SETREGS;	/* set registers before continuing */
4197c478bd9Sstevel@tonic-gate 	dprintf("Psyscall(): execute(sysindex = %d)\n", sysindex);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * Execute the syscall instruction and stop on syscall entry.
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	if (execute(P, sysindex) != 0 ||
4257c478bd9Sstevel@tonic-gate 	    (!Pissyscall(P, P->status.pr_lwp.pr_reg[R_PC]) &&
4267c478bd9Sstevel@tonic-gate 	    !Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], NULL)))
4277c478bd9Sstevel@tonic-gate 		goto bad10;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	dprintf("Psyscall(): copying arguments\n");
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * The LWP is stopped at syscall entry.
4337c478bd9Sstevel@tonic-gate 	 * Copy objects to stack frame for each argument.
4347c478bd9Sstevel@tonic-gate 	 */
4357c478bd9Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++) {
4367c478bd9Sstevel@tonic-gate 		rval->sys_rval1 = i;		/* in case of error */
4377c478bd9Sstevel@tonic-gate 		if (adp->arg_type != AT_BYVAL &&
4387c478bd9Sstevel@tonic-gate 		    adp->arg_inout != AI_OUTPUT) {
4397c478bd9Sstevel@tonic-gate 			/* copy input byref parameter to process */
4407c478bd9Sstevel@tonic-gate 			if (Pwrite(P, adp->arg_object, adp->arg_size,
4417c478bd9Sstevel@tonic-gate 			    (uintptr_t)adp->arg_value) != adp->arg_size)
4427c478bd9Sstevel@tonic-gate 				goto bad17;
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	rval->sys_rval1 = 0;			/* in case of error */
4467c478bd9Sstevel@tonic-gate 	if (Psyscall_copyinargs(P, nargs, argp, ap) != 0)
4477c478bd9Sstevel@tonic-gate 		goto bad18;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * Complete the system call.
4517c478bd9Sstevel@tonic-gate 	 * This moves the LWP to the stopped-on-syscall-exit state.
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	dprintf("Psyscall(): set running at sysentry\n");
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	sexit = Psysexit(P, sysindex, TRUE);	/* catch this syscall exit */
4567c478bd9Sstevel@tonic-gate 	do {
4577c478bd9Sstevel@tonic-gate 		if (Psetrun(P, 0, 0) == -1)
4587c478bd9Sstevel@tonic-gate 			goto bad21;
4597c478bd9Sstevel@tonic-gate 		while (P->state == PS_RUN)
4607c478bd9Sstevel@tonic-gate 			(void) Pwait(P, 0);
4617c478bd9Sstevel@tonic-gate 	} while (P->state == PS_STOP && P->status.pr_lwp.pr_why != PR_SYSEXIT);
4627c478bd9Sstevel@tonic-gate 	(void) Psysexit(P, sysindex, sexit);	/* restore original setting */
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * If the system call was _lwp_exit(), we expect that our last call
4667c478bd9Sstevel@tonic-gate 	 * to Pwait() will yield ENOENT because the LWP no longer exists.
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	if (sysindex == SYS_lwp_exit && errno == ENOENT) {
4697c478bd9Sstevel@tonic-gate 		dprintf("Psyscall(): _lwp_exit successful\n");
4707c478bd9Sstevel@tonic-gate 		rval->sys_rval1 = rval->sys_rval2 = 0;
4717c478bd9Sstevel@tonic-gate 		goto out;
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP || P->status.pr_lwp.pr_why != PR_SYSEXIT)
4757c478bd9Sstevel@tonic-gate 		goto bad22;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_what != sysindex)
4787c478bd9Sstevel@tonic-gate 		goto bad23;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (!Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], NULL)) {
4817c478bd9Sstevel@tonic-gate 		dprintf("Pissyscall_prev() failed\n");
4827c478bd9Sstevel@tonic-gate 		goto bad24;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	dprintf("Psyscall(): caught at sysexit\n");
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * For each argument.
4897c478bd9Sstevel@tonic-gate 	 */
4907c478bd9Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++) {
4917c478bd9Sstevel@tonic-gate 		rval->sys_rval1 = i;		/* in case of error */
4927c478bd9Sstevel@tonic-gate 		if (adp->arg_type != AT_BYVAL &&
4937c478bd9Sstevel@tonic-gate 		    adp->arg_inout != AI_INPUT) {
4947c478bd9Sstevel@tonic-gate 			/* copy output byref parameter from process */
4957c478bd9Sstevel@tonic-gate 			if (Pread(P, adp->arg_object, adp->arg_size,
4967c478bd9Sstevel@tonic-gate 			    (uintptr_t)adp->arg_value) != adp->arg_size)
4977c478bd9Sstevel@tonic-gate 				goto bad25;
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (Psyscall_copyoutargs(P, nargs, argp, ap) != 0)
5027c478bd9Sstevel@tonic-gate 		goto bad26;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * Get the return values from the syscall.
5067c478bd9Sstevel@tonic-gate 	 */
5077c478bd9Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_errno) {	/* error return */
5087c478bd9Sstevel@tonic-gate 		error = P->status.pr_lwp.pr_errno;
5097c478bd9Sstevel@tonic-gate 		rval->sys_rval1 = -1L;
5107c478bd9Sstevel@tonic-gate 		rval->sys_rval2 = -1L;
5117c478bd9Sstevel@tonic-gate 		dprintf("Psyscall(%d) fails with errno %d\n",
5127c478bd9Sstevel@tonic-gate 		    sysindex, error);
5137c478bd9Sstevel@tonic-gate 	} else {				/* normal return */
5147c478bd9Sstevel@tonic-gate 		rval->sys_rval1 = P->status.pr_lwp.pr_rval1;
5157c478bd9Sstevel@tonic-gate 		rval->sys_rval2 = P->status.pr_lwp.pr_rval2;
5167c478bd9Sstevel@tonic-gate 		dprintf("Psyscall(%d) returns 0x%lx 0x%lx\n", sysindex,
5177c478bd9Sstevel@tonic-gate 		    P->status.pr_lwp.pr_rval1, P->status.pr_lwp.pr_rval2);
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	goto out;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate bad26:	Perr++;
5237c478bd9Sstevel@tonic-gate bad25:	Perr++;
5247c478bd9Sstevel@tonic-gate bad24:	Perr++;
5257c478bd9Sstevel@tonic-gate bad23:	Perr++;
5267c478bd9Sstevel@tonic-gate bad22:	Perr++;
5277c478bd9Sstevel@tonic-gate bad21:	Perr++;
5287c478bd9Sstevel@tonic-gate 	Perr++;
5297c478bd9Sstevel@tonic-gate 	Perr++;
5307c478bd9Sstevel@tonic-gate bad18:	Perr++;
5317c478bd9Sstevel@tonic-gate bad17:	Perr++;
5327c478bd9Sstevel@tonic-gate 	Perr++;
5337c478bd9Sstevel@tonic-gate 	Perr++;
5347c478bd9Sstevel@tonic-gate 	Perr++;
5357c478bd9Sstevel@tonic-gate 	Perr++;
5367c478bd9Sstevel@tonic-gate 	Perr++;
5377c478bd9Sstevel@tonic-gate 	Perr++;
5387c478bd9Sstevel@tonic-gate bad10:	Perr++;
5397c478bd9Sstevel@tonic-gate bad9:	Perr++;
5407c478bd9Sstevel@tonic-gate bad8:	Perr++;
5417c478bd9Sstevel@tonic-gate bad7:	Perr++;
5427c478bd9Sstevel@tonic-gate bad6:	Perr++;
5437c478bd9Sstevel@tonic-gate bad5:	Perr++;
5447c478bd9Sstevel@tonic-gate bad4:	Perr++;
5457c478bd9Sstevel@tonic-gate bad3:	Perr++;
5467c478bd9Sstevel@tonic-gate bad2:	Perr++;
5477c478bd9Sstevel@tonic-gate bad1:	Perr++;
5487c478bd9Sstevel@tonic-gate 	error = -1;
5497c478bd9Sstevel@tonic-gate 	dprintf("Psyscall(%d) fails with local error %d\n", sysindex, Perr);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate out:
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * Destroy the /proc agent LWP now (or just bump down the ref count).
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	if (agent_created) {
5567c478bd9Sstevel@tonic-gate 		if (P->state != PS_UNDEAD) {
5577c478bd9Sstevel@tonic-gate 			P->status = save_pstatus;
5587c478bd9Sstevel@tonic-gate 			P->flags |= SETREGS;
5597c478bd9Sstevel@tonic-gate 			Psync(P);
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		Pdestroy_agent(P);
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
5657c478bd9Sstevel@tonic-gate 	return (error);
5667c478bd9Sstevel@tonic-gate }
567