2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A#include <ctype.h>
2N/A#include <fcntl.h>
2N/A#include <string.h>
2N/A#include <memory.h>
2N/A#include <errno.h>
2N/A#include <dirent.h>
2N/A#include <limits.h>
2N/A#include <signal.h>
2N/A#include <sys/types.h>
2N/A#include <sys/uio.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/resource.h>
2N/A#include <sys/param.h>
2N/A#include <sys/stack.h>
2N/A#include <sys/fault.h>
2N/A#include <sys/syscall.h>
2N/A#include <sys/sysmacros.h>
2N/A
2N/A#include "libproc.h"
2N/A#include "Pcontrol.h"
2N/A#include "Putil.h"
2N/A#include "P32ton.h"
2N/A#include "Pisadep.h"
2N/A
2N/Aextern sigset_t blockable_sigs;
2N/A
2N/Astatic void
2N/APabort_agent(struct ps_prochandle *P)
2N/A{
2N/A int sysnum = P->status.pr_lwp.pr_syscall;
2N/A int stop;
2N/A
2N/A dprintf("agent LWP is asleep in syscall %d\n", sysnum);
2N/A (void) Pstop(P, 0);
2N/A stop = Psysexit(P, sysnum, TRUE);
2N/A
2N/A if (Psetrun(P, 0, PRSABORT) == 0) {
2N/A while (Pwait(P, 0) == -1 && errno == EINTR)
2N/A continue;
2N/A (void) Psysexit(P, sysnum, stop);
2N/A dprintf("agent LWP system call aborted\n");
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Create the /proc agent LWP for further operations.
2N/A */
2N/Aint
2N/APcreate_agent(struct ps_prochandle *P)
2N/A{
2N/A int fd;
2N/A char pathname[PATH_MAX];
2N/A char *fname;
2N/A struct {
2N/A long cmd;
2N/A prgregset_t regs;
2N/A } cmd;
2N/A
2N/A /*
2N/A * If not first reference, we already have the /proc agent LWP active.
2N/A */
2N/A if (P->agentcnt > 0) {
2N/A P->agentcnt++;
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * The agent is not available for use as a mortician or as an
2N/A * obstetrician.
2N/A */
2N/A if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
2N/A P->state == PS_IDLE) {
2N/A errno = ENOENT;
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * Create the special /proc agent LWP if it doesn't already exist.
2N/A * Give it the registers of the representative LWP.
2N/A */
2N/A (void) Pstop(P, 0);
2N/A Psync(P);
2N/A if (!(P->status.pr_lwp.pr_flags & PR_AGENT)) {
2N/A cmd.cmd = PCAGENT;
2N/A (void) memcpy(&cmd.regs, &P->status.pr_lwp.pr_reg[0],
2N/A sizeof (P->status.pr_lwp.pr_reg));
2N/A if (write(P->ctlfd, &cmd, sizeof (cmd)) != sizeof (cmd))
2N/A goto bad;
2N/A }
2N/A
2N/A /* refresh the process status */
2N/A (void) Pstopstatus(P, PCNULL, 0);
2N/A
2N/A /* open the agent LWP files */
2N/A (void) snprintf(pathname, sizeof (pathname), "%s/%d/lwp/agent/",
2N/A procfs_path, (int)P->pid);
2N/A fname = pathname + strlen(pathname);
2N/A (void) set_minfd();
2N/A
2N/A /*
2N/A * It is difficult to know how to recover from the two errors
2N/A * that follow. The agent LWP exists and we need to kill it,
2N/A * but we can't because we need it active in order to kill it.
2N/A * We just hope that these failures never occur.
2N/A */
2N/A (void) strcpy(fname, "lwpstatus");
2N/A if ((fd = open(pathname, O_RDONLY)) < 0 ||
2N/A (fd = dupfd(fd, 0)) < 0)
2N/A goto bad;
2N/A P->agentstatfd = fd;
2N/A
2N/A (void) strcpy(fname, "lwpctl");
2N/A if ((fd = open(pathname, O_WRONLY)) < 0 ||
2N/A (fd = dupfd(fd, 0)) < 0)
2N/A goto bad;
2N/A P->agentctlfd = fd;
2N/A
2N/A /*
2N/A * If the agent is currently asleep in a system call, attempt
2N/A * to abort the system call so it's ready to serve.
2N/A */
2N/A if (P->status.pr_lwp.pr_flags & PR_ASLEEP) {
2N/A dprintf("Pcreate_agent: aborting agent syscall\n");
2N/A Pabort_agent(P);
2N/A }
2N/A
2N/A /* get the agent LWP status */
2N/A P->agentcnt++;
2N/A if (Pstopstatus(P, PCNULL, 0) != 0) {
2N/A Pdestroy_agent(P);
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A
2N/Abad:
2N/A if (P->agentstatfd >= 0)
2N/A (void) close(P->agentstatfd);
2N/A if (P->agentctlfd >= 0)
2N/A (void) close(P->agentctlfd);
2N/A P->agentstatfd = -1;
2N/A P->agentctlfd = -1;
2N/A /* refresh the process status */
2N/A (void) Pstopstatus(P, PCNULL, 0);
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * Decrement the /proc agent agent reference count.
2N/A * On last reference, destroy the agent.
2N/A */
2N/Avoid
2N/APdestroy_agent(struct ps_prochandle *P)
2N/A{
2N/A if (P->agentcnt > 1)
2N/A P->agentcnt--;
2N/A else {
2N/A int flags;
2N/A
2N/A Psync(P); /* Flush out any pending changes */
2N/A
2N/A (void) Pstopstatus(P, PCNULL, 0);
2N/A flags = P->status.pr_lwp.pr_flags;
2N/A
2N/A /*
2N/A * If the agent is currently asleep in a system call, attempt
2N/A * to abort the system call so we can terminate the agent.
2N/A */
2N/A if ((flags & (PR_AGENT|PR_ASLEEP)) == (PR_AGENT|PR_ASLEEP)) {
2N/A dprintf("Pdestroy_agent: aborting agent syscall\n");
2N/A Pabort_agent(P);
2N/A }
2N/A
2N/A /*
2N/A * The agent itself is destroyed by forcing it to execute
2N/A * the _lwp_exit(2) system call. Close our agent descriptors
2N/A * regardless of whether this is successful.
2N/A */
2N/A (void) pr_lwp_exit(P);
2N/A (void) close(P->agentctlfd);
2N/A (void) close(P->agentstatfd);
2N/A P->agentctlfd = -1;
2N/A P->agentstatfd = -1;
2N/A P->agentcnt = 0;
2N/A
2N/A /*
2N/A * Now that (hopefully) the agent has exited, refresh the
2N/A * status: the representative LWP is no longer the agent.
2N/A */
2N/A (void) Pstopstatus(P, PCNULL, 0);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Execute the syscall instruction.
2N/A */
2N/Astatic int
2N/Aexecute(struct ps_prochandle *P, int sysindex)
2N/A{
2N/A int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2N/A int washeld = FALSE;
2N/A sigset_t hold; /* mask of held signals */
2N/A int cursig;
2N/A struct {
2N/A long cmd;
2N/A siginfo_t siginfo;
2N/A } ctl;
2N/A int sentry; /* old value of stop-on-syscall-entry */
2N/A
2N/A sentry = Psysentry(P, sysindex, TRUE); /* set stop-on-syscall-entry */
2N/A
2N/A /*
2N/A * If not already blocked, block all signals now.
2N/A */
2N/A if (memcmp(&P->status.pr_lwp.pr_lwphold, &blockable_sigs,
2N/A sizeof (sigset_t)) != 0) {
2N/A hold = P->status.pr_lwp.pr_lwphold;
2N/A P->status.pr_lwp.pr_lwphold = blockable_sigs;
2N/A P->flags |= SETHOLD;
2N/A washeld = TRUE;
2N/A }
2N/A
2N/A /*
2N/A * If there is a current signal, remember it and cancel it.
2N/A */
2N/A if ((cursig = P->status.pr_lwp.pr_cursig) != 0) {
2N/A ctl.cmd = PCSSIG;
2N/A ctl.siginfo = P->status.pr_lwp.pr_info;
2N/A }
2N/A
2N/A if (Psetrun(P, 0, PRCSIG | PRCFAULT) == -1)
2N/A goto bad;
2N/A
2N/A while (P->state == PS_RUN) {
2N/A (void) Pwait(P, 0);
2N/A }
2N/A if (P->state != PS_STOP)
2N/A goto bad;
2N/A
2N/A if (cursig) /* restore cursig */
2N/A (void) write(ctlfd, &ctl, sizeof (ctl));
2N/A if (washeld) { /* restore the signal mask if we set it */
2N/A P->status.pr_lwp.pr_lwphold = hold;
2N/A P->flags |= SETHOLD;
2N/A }
2N/A
2N/A (void) Psysentry(P, sysindex, sentry); /* restore sysentry stop */
2N/A
2N/A if (P->status.pr_lwp.pr_why == PR_SYSENTRY &&
2N/A P->status.pr_lwp.pr_what == sysindex)
2N/A return (0);
2N/Abad:
2N/A return (-1);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Perform system call in controlled process.
2N/A */
2N/Aint
2N/APsyscall(struct ps_prochandle *P,
2N/A sysret_t *rval, /* syscall return values */
2N/A int sysindex, /* system call index */
2N/A uint_t nargs, /* number of arguments to system call */
2N/A argdes_t *argp) /* argument descriptor array */
2N/A{
2N/A int agent_created = FALSE;
2N/A pstatus_t save_pstatus;
2N/A argdes_t *adp; /* pointer to argument descriptor */
2N/A int i; /* general index value */
2N/A int model; /* data model */
2N/A int error = 0; /* syscall errno */
2N/A int Perr = 0; /* local error number */
2N/A int sexit; /* old value of stop-on-syscall-exit */
2N/A prgreg_t sp; /* adjusted stack pointer */
2N/A prgreg_t ap; /* adjusted argument pointer */
2N/A sigset_t unblock;
2N/A
2N/A (void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
2N/A
2N/A rval->sys_rval1 = 0; /* initialize return values */
2N/A rval->sys_rval2 = 0;
2N/A
2N/A if (sysindex <= 0 || sysindex > PRMAXSYS || nargs > MAXARGS)
2N/A goto bad1; /* programming error */
2N/A
2N/A if (P->state == PS_DEAD || P->state == PS_UNDEAD || P->state == PS_IDLE)
2N/A goto bad1; /* dead processes can't perform system calls */
2N/A
2N/A model = P->status.pr_dmodel;
2N/A#ifndef _LP64
2N/A /* We must be a 64-bit process to deal with a 64-bit process */
2N/A if (model == PR_MODEL_LP64)
2N/A goto bad9;
2N/A#endif
2N/A
2N/A /*
2N/A * Create the /proc agent LWP in the process to do all the work.
2N/A * (It may already exist; nested create/destroy is permitted
2N/A * by virtue of the reference count.)
2N/A */
2N/A if (Pcreate_agent(P) != 0)
2N/A goto bad8;
2N/A
2N/A /*
2N/A * Save agent's status to restore on exit.
2N/A */
2N/A agent_created = TRUE;
2N/A save_pstatus = P->status;
2N/A
2N/A if (P->state != PS_STOP || /* check state of LWP */
2N/A (P->status.pr_flags & PR_ASLEEP))
2N/A goto bad2;
2N/A
2N/A if (Pscantext(P)) /* bad text ? */
2N/A goto bad3;
2N/A
2N/A /*
2N/A * Validate arguments and compute the stack frame parameters.
2N/A * Begin with the current stack pointer.
2N/A */
2N/A#ifdef _LP64
2N/A if (model == PR_MODEL_LP64) {
2N/A sp = P->status.pr_lwp.pr_reg[R_SP] + STACK_BIAS;
2N/A sp = PSTACK_ALIGN64(sp);
2N/A } else {
2N/A#endif
2N/A sp = (uint32_t)P->status.pr_lwp.pr_reg[R_SP];
2N/A sp = PSTACK_ALIGN32(sp);
2N/A#ifdef _LP64
2N/A }
2N/A#endif
2N/A
2N/A /*
2N/A * For each AT_BYREF argument, compute the necessary
2N/A * stack space and the object's stack address.
2N/A */
2N/A for (i = 0, adp = argp; i < nargs; i++, adp++) {
2N/A rval->sys_rval1 = i; /* in case of error */
2N/A switch (adp->arg_type) {
2N/A default: /* programming error */
2N/A goto bad4;
2N/A case AT_BYVAL: /* simple argument */
2N/A break;
2N/A case AT_BYREF: /* must allocate space */
2N/A switch (adp->arg_inout) {
2N/A case AI_INPUT:
2N/A case AI_OUTPUT:
2N/A case AI_INOUT:
2N/A if (adp->arg_object == NULL)
2N/A goto bad5; /* programming error */
2N/A break;
2N/A default: /* programming error */
2N/A goto bad6;
2N/A }
2N/A /* allocate stack space for BYREF argument */
2N/A if (adp->arg_size == 0 || adp->arg_size > MAXARGL)
2N/A goto bad7; /* programming error */
2N/A#ifdef _LP64
2N/A if (model == PR_MODEL_LP64)
2N/A sp = PSTACK_ALIGN64(sp - adp->arg_size);
2N/A else
2N/A#endif
2N/A sp = PSTACK_ALIGN32(sp - adp->arg_size);
2N/A adp->arg_value = sp; /* stack address for object */
2N/A break;
2N/A }
2N/A }
2N/A rval->sys_rval1 = 0; /* in case of error */
2N/A /*
2N/A * Point of no return.
2N/A * Perform the system call entry, adjusting %sp.
2N/A * This moves the LWP to the stopped-on-syscall-entry state
2N/A * just before the arguments to the system call are fetched.
2N/A */
2N/A ap = Psyscall_setup(P, nargs, sysindex, sp);
2N/A P->flags |= SETREGS; /* set registers before continuing */
2N/A dprintf("Psyscall(): execute(sysindex = %d)\n", sysindex);
2N/A
2N/A /*
2N/A * Execute the syscall instruction and stop on syscall entry.
2N/A */
2N/A if (execute(P, sysindex) != 0 ||
2N/A (!Pissyscall(P, P->status.pr_lwp.pr_reg[R_PC]) &&
2N/A !Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], NULL)))
2N/A goto bad10;
2N/A
2N/A dprintf("Psyscall(): copying arguments\n");
2N/A
2N/A /*
2N/A * The LWP is stopped at syscall entry.
2N/A * Copy objects to stack frame for each argument.
2N/A */
2N/A for (i = 0, adp = argp; i < nargs; i++, adp++) {
2N/A rval->sys_rval1 = i; /* in case of error */
2N/A if (adp->arg_type != AT_BYVAL &&
2N/A adp->arg_inout != AI_OUTPUT) {
2N/A /* copy input byref parameter to process */
2N/A if (Pwrite(P, adp->arg_object, adp->arg_size,
2N/A (uintptr_t)adp->arg_value) != adp->arg_size)
2N/A goto bad17;
2N/A }
2N/A }
2N/A rval->sys_rval1 = 0; /* in case of error */
2N/A if (Psyscall_copyinargs(P, nargs, argp, ap) != 0)
2N/A goto bad18;
2N/A
2N/A /*
2N/A * Complete the system call.
2N/A * This moves the LWP to the stopped-on-syscall-exit state.
2N/A */
2N/A dprintf("Psyscall(): set running at sysentry\n");
2N/A
2N/A sexit = Psysexit(P, sysindex, TRUE); /* catch this syscall exit */
2N/A do {
2N/A if (Psetrun(P, 0, 0) == -1)
2N/A goto bad21;
2N/A while (P->state == PS_RUN)
2N/A (void) Pwait(P, 0);
2N/A } while (P->state == PS_STOP && P->status.pr_lwp.pr_why != PR_SYSEXIT);
2N/A (void) Psysexit(P, sysindex, sexit); /* restore original setting */
2N/A
2N/A /*
2N/A * If the system call was _lwp_exit(), we expect that our last call
2N/A * to Pwait() will yield ENOENT because the LWP no longer exists.
2N/A */
2N/A if (sysindex == SYS_lwp_exit && errno == ENOENT) {
2N/A dprintf("Psyscall(): _lwp_exit successful\n");
2N/A rval->sys_rval1 = rval->sys_rval2 = 0;
2N/A goto out;
2N/A }
2N/A
2N/A if (P->state != PS_STOP || P->status.pr_lwp.pr_why != PR_SYSEXIT)
2N/A goto bad22;
2N/A
2N/A if (P->status.pr_lwp.pr_what != sysindex)
2N/A goto bad23;
2N/A
2N/A if (!Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], NULL)) {
2N/A dprintf("Pissyscall_prev() failed\n");
2N/A goto bad24;
2N/A }
2N/A
2N/A dprintf("Psyscall(): caught at sysexit\n");
2N/A
2N/A /*
2N/A * For each argument.
2N/A */
2N/A for (i = 0, adp = argp; i < nargs; i++, adp++) {
2N/A rval->sys_rval1 = i; /* in case of error */
2N/A if (adp->arg_type != AT_BYVAL &&
2N/A adp->arg_inout != AI_INPUT) {
2N/A /* copy output byref parameter from process */
2N/A if (Pread(P, adp->arg_object, adp->arg_size,
2N/A (uintptr_t)adp->arg_value) != adp->arg_size)
2N/A goto bad25;
2N/A }
2N/A }
2N/A
2N/A if (Psyscall_copyoutargs(P, nargs, argp, ap) != 0)
2N/A goto bad26;
2N/A
2N/A /*
2N/A * Get the return values from the syscall.
2N/A */
2N/A if (P->status.pr_lwp.pr_errno) { /* error return */
2N/A error = P->status.pr_lwp.pr_errno;
2N/A rval->sys_rval1 = -1L;
2N/A rval->sys_rval2 = -1L;
2N/A dprintf("Psyscall(%d) fails with errno %d\n",
2N/A sysindex, error);
2N/A } else { /* normal return */
2N/A rval->sys_rval1 = P->status.pr_lwp.pr_rval1;
2N/A rval->sys_rval2 = P->status.pr_lwp.pr_rval2;
2N/A dprintf("Psyscall(%d) returns 0x%lx 0x%lx\n", sysindex,
2N/A P->status.pr_lwp.pr_rval1, P->status.pr_lwp.pr_rval2);
2N/A }
2N/A
2N/A goto out;
2N/A
2N/Abad26: Perr++;
2N/Abad25: Perr++;
2N/Abad24: Perr++;
2N/Abad23: Perr++;
2N/Abad22: Perr++;
2N/Abad21: Perr++;
2N/A Perr++;
2N/A Perr++;
2N/Abad18: Perr++;
2N/Abad17: Perr++;
2N/A Perr++;
2N/A Perr++;
2N/A Perr++;
2N/A Perr++;
2N/A Perr++;
2N/A Perr++;
2N/Abad10: Perr++;
2N/Abad9: Perr++;
2N/Abad8: Perr++;
2N/Abad7: Perr++;
2N/Abad6: Perr++;
2N/Abad5: Perr++;
2N/Abad4: Perr++;
2N/Abad3: Perr++;
2N/Abad2: Perr++;
2N/Abad1: Perr++;
2N/A error = -1;
2N/A dprintf("Psyscall(%d) fails with local error %d\n", sysindex, Perr);
2N/A
2N/Aout:
2N/A /*
2N/A * Destroy the /proc agent LWP now (or just bump down the ref count).
2N/A */
2N/A if (agent_created) {
2N/A if (P->state != PS_UNDEAD) {
2N/A P->status = save_pstatus;
2N/A P->flags |= SETREGS;
2N/A Psync(P);
2N/A }
2N/A Pdestroy_agent(P);
2N/A }
2N/A
2N/A (void) sigprocmask(SIG_SETMASK, &unblock, NULL);
2N/A return (error);
2N/A}