1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <stdio.h> 30*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 31*7c478bd9Sstevel@tonic-gate #include <unistd.h> 32*7c478bd9Sstevel@tonic-gate #include <ctype.h> 33*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <memory.h> 36*7c478bd9Sstevel@tonic-gate #include <errno.h> 37*7c478bd9Sstevel@tonic-gate #include <dirent.h> 38*7c478bd9Sstevel@tonic-gate #include <limits.h> 39*7c478bd9Sstevel@tonic-gate #include <signal.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/uio.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/resource.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/stack.h> 46*7c478bd9Sstevel@tonic-gate #include <sys/fault.h> 47*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 48*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate #include "libproc.h" 51*7c478bd9Sstevel@tonic-gate #include "Pcontrol.h" 52*7c478bd9Sstevel@tonic-gate #include "Putil.h" 53*7c478bd9Sstevel@tonic-gate #include "P32ton.h" 54*7c478bd9Sstevel@tonic-gate #include "Pisadep.h" 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate extern sigset_t blockable_sigs; 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate static void 59*7c478bd9Sstevel@tonic-gate Pabort_agent(struct ps_prochandle *P) 60*7c478bd9Sstevel@tonic-gate { 61*7c478bd9Sstevel@tonic-gate int sysnum = P->status.pr_lwp.pr_syscall; 62*7c478bd9Sstevel@tonic-gate int stop; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate dprintf("agent LWP is asleep in syscall %d\n", sysnum); 65*7c478bd9Sstevel@tonic-gate (void) Pstop(P, 0); 66*7c478bd9Sstevel@tonic-gate stop = Psysexit(P, sysnum, TRUE); 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate if (Psetrun(P, 0, PRSABORT) == 0) { 69*7c478bd9Sstevel@tonic-gate while (Pwait(P, 0) == -1 && errno == EINTR) 70*7c478bd9Sstevel@tonic-gate continue; 71*7c478bd9Sstevel@tonic-gate (void) Psysexit(P, sysnum, stop); 72*7c478bd9Sstevel@tonic-gate dprintf("agent LWP system call aborted\n"); 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate } 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate /* 77*7c478bd9Sstevel@tonic-gate * Create the /proc agent LWP for further operations. 78*7c478bd9Sstevel@tonic-gate */ 79*7c478bd9Sstevel@tonic-gate int 80*7c478bd9Sstevel@tonic-gate Pcreate_agent(struct ps_prochandle *P) 81*7c478bd9Sstevel@tonic-gate { 82*7c478bd9Sstevel@tonic-gate int fd; 83*7c478bd9Sstevel@tonic-gate char pathname[100]; 84*7c478bd9Sstevel@tonic-gate char *fname; 85*7c478bd9Sstevel@tonic-gate struct { 86*7c478bd9Sstevel@tonic-gate long cmd; 87*7c478bd9Sstevel@tonic-gate prgregset_t regs; 88*7c478bd9Sstevel@tonic-gate } cmd; 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate /* 91*7c478bd9Sstevel@tonic-gate * If not first reference, we already have the /proc agent LWP active. 92*7c478bd9Sstevel@tonic-gate */ 93*7c478bd9Sstevel@tonic-gate if (P->agentcnt > 0) { 94*7c478bd9Sstevel@tonic-gate P->agentcnt++; 95*7c478bd9Sstevel@tonic-gate return (0); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate /* 99*7c478bd9Sstevel@tonic-gate * The agent is not available for use as a mortician or as an 100*7c478bd9Sstevel@tonic-gate * obstetrician. 101*7c478bd9Sstevel@tonic-gate */ 102*7c478bd9Sstevel@tonic-gate if (P->state == PS_DEAD || P->state == PS_UNDEAD || 103*7c478bd9Sstevel@tonic-gate P->state == PS_IDLE) { 104*7c478bd9Sstevel@tonic-gate errno = ENOENT; 105*7c478bd9Sstevel@tonic-gate return (-1); 106*7c478bd9Sstevel@tonic-gate } 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate /* 109*7c478bd9Sstevel@tonic-gate * Create the special /proc agent LWP if it doesn't already exist. 110*7c478bd9Sstevel@tonic-gate * Give it the registers of the representative LWP. 111*7c478bd9Sstevel@tonic-gate */ 112*7c478bd9Sstevel@tonic-gate (void) Pstop(P, 0); 113*7c478bd9Sstevel@tonic-gate Psync(P); 114*7c478bd9Sstevel@tonic-gate if (!(P->status.pr_lwp.pr_flags & PR_AGENT)) { 115*7c478bd9Sstevel@tonic-gate cmd.cmd = PCAGENT; 116*7c478bd9Sstevel@tonic-gate (void) memcpy(&cmd.regs, &P->status.pr_lwp.pr_reg[0], 117*7c478bd9Sstevel@tonic-gate sizeof (P->status.pr_lwp.pr_reg)); 118*7c478bd9Sstevel@tonic-gate if (write(P->ctlfd, &cmd, sizeof (cmd)) != sizeof (cmd)) 119*7c478bd9Sstevel@tonic-gate goto bad; 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate /* refresh the process status */ 123*7c478bd9Sstevel@tonic-gate (void) Pstopstatus(P, PCNULL, 0); 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate /* open the agent LWP files */ 126*7c478bd9Sstevel@tonic-gate (void) sprintf(pathname, "/proc/%d/lwp/agent/", (int)P->pid); 127*7c478bd9Sstevel@tonic-gate fname = pathname + strlen(pathname); 128*7c478bd9Sstevel@tonic-gate (void) set_minfd(); 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate /* 131*7c478bd9Sstevel@tonic-gate * It is difficult to know how to recover from the two errors 132*7c478bd9Sstevel@tonic-gate * that follow. The agent LWP exists and we need to kill it, 133*7c478bd9Sstevel@tonic-gate * but we can't because we need it active in order to kill it. 134*7c478bd9Sstevel@tonic-gate * We just hope that these failures never occur. 135*7c478bd9Sstevel@tonic-gate */ 136*7c478bd9Sstevel@tonic-gate (void) strcpy(fname, "lwpstatus"); 137*7c478bd9Sstevel@tonic-gate if ((fd = open(pathname, O_RDONLY)) < 0 || 138*7c478bd9Sstevel@tonic-gate (fd = dupfd(fd, 0)) < 0) 139*7c478bd9Sstevel@tonic-gate goto bad; 140*7c478bd9Sstevel@tonic-gate P->agentstatfd = fd; 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate (void) strcpy(fname, "lwpctl"); 143*7c478bd9Sstevel@tonic-gate if ((fd = open(pathname, O_WRONLY)) < 0 || 144*7c478bd9Sstevel@tonic-gate (fd = dupfd(fd, 0)) < 0) 145*7c478bd9Sstevel@tonic-gate goto bad; 146*7c478bd9Sstevel@tonic-gate P->agentctlfd = fd; 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate /* 149*7c478bd9Sstevel@tonic-gate * If the agent is currently asleep in a system call, attempt 150*7c478bd9Sstevel@tonic-gate * to abort the system call so it's ready to serve. 151*7c478bd9Sstevel@tonic-gate */ 152*7c478bd9Sstevel@tonic-gate if (P->status.pr_lwp.pr_flags & PR_ASLEEP) { 153*7c478bd9Sstevel@tonic-gate dprintf("Pcreate_agent: aborting agent syscall\n"); 154*7c478bd9Sstevel@tonic-gate Pabort_agent(P); 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate /* get the agent LWP status */ 158*7c478bd9Sstevel@tonic-gate P->agentcnt++; 159*7c478bd9Sstevel@tonic-gate if (Pstopstatus(P, PCNULL, 0) != 0) { 160*7c478bd9Sstevel@tonic-gate Pdestroy_agent(P); 161*7c478bd9Sstevel@tonic-gate return (-1); 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate return (0); 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate bad: 167*7c478bd9Sstevel@tonic-gate if (P->agentstatfd >= 0) 168*7c478bd9Sstevel@tonic-gate (void) close(P->agentstatfd); 169*7c478bd9Sstevel@tonic-gate if (P->agentctlfd >= 0) 170*7c478bd9Sstevel@tonic-gate (void) close(P->agentctlfd); 171*7c478bd9Sstevel@tonic-gate P->agentstatfd = -1; 172*7c478bd9Sstevel@tonic-gate P->agentctlfd = -1; 173*7c478bd9Sstevel@tonic-gate /* refresh the process status */ 174*7c478bd9Sstevel@tonic-gate (void) Pstopstatus(P, PCNULL, 0); 175*7c478bd9Sstevel@tonic-gate return (-1); 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate /* 179*7c478bd9Sstevel@tonic-gate * Decrement the /proc agent agent reference count. 180*7c478bd9Sstevel@tonic-gate * On last reference, destroy the agent. 181*7c478bd9Sstevel@tonic-gate */ 182*7c478bd9Sstevel@tonic-gate void 183*7c478bd9Sstevel@tonic-gate Pdestroy_agent(struct ps_prochandle *P) 184*7c478bd9Sstevel@tonic-gate { 185*7c478bd9Sstevel@tonic-gate if (P->agentcnt > 1) 186*7c478bd9Sstevel@tonic-gate P->agentcnt--; 187*7c478bd9Sstevel@tonic-gate else { 188*7c478bd9Sstevel@tonic-gate int flags; 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate Psync(P); /* Flush out any pending changes */ 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate (void) Pstopstatus(P, PCNULL, 0); 193*7c478bd9Sstevel@tonic-gate flags = P->status.pr_lwp.pr_flags; 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate /* 196*7c478bd9Sstevel@tonic-gate * If the agent is currently asleep in a system call, attempt 197*7c478bd9Sstevel@tonic-gate * to abort the system call so we can terminate the agent. 198*7c478bd9Sstevel@tonic-gate */ 199*7c478bd9Sstevel@tonic-gate if ((flags & (PR_AGENT|PR_ASLEEP)) == (PR_AGENT|PR_ASLEEP)) { 200*7c478bd9Sstevel@tonic-gate dprintf("Pdestroy_agent: aborting agent syscall\n"); 201*7c478bd9Sstevel@tonic-gate Pabort_agent(P); 202*7c478bd9Sstevel@tonic-gate } 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate /* 205*7c478bd9Sstevel@tonic-gate * The agent itself is destroyed by forcing it to execute 206*7c478bd9Sstevel@tonic-gate * the _lwp_exit(2) system call. Close our agent descriptors 207*7c478bd9Sstevel@tonic-gate * regardless of whether this is successful. 208*7c478bd9Sstevel@tonic-gate */ 209*7c478bd9Sstevel@tonic-gate (void) pr_lwp_exit(P); 210*7c478bd9Sstevel@tonic-gate (void) close(P->agentctlfd); 211*7c478bd9Sstevel@tonic-gate (void) close(P->agentstatfd); 212*7c478bd9Sstevel@tonic-gate P->agentctlfd = -1; 213*7c478bd9Sstevel@tonic-gate P->agentstatfd = -1; 214*7c478bd9Sstevel@tonic-gate P->agentcnt = 0; 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate /* 217*7c478bd9Sstevel@tonic-gate * Now that (hopefully) the agent has exited, refresh the 218*7c478bd9Sstevel@tonic-gate * status: the representative LWP is no longer the agent. 219*7c478bd9Sstevel@tonic-gate */ 220*7c478bd9Sstevel@tonic-gate (void) Pstopstatus(P, PCNULL, 0); 221*7c478bd9Sstevel@tonic-gate } 222*7c478bd9Sstevel@tonic-gate } 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate /* 225*7c478bd9Sstevel@tonic-gate * Execute the syscall instruction. 226*7c478bd9Sstevel@tonic-gate */ 227*7c478bd9Sstevel@tonic-gate static int 228*7c478bd9Sstevel@tonic-gate execute(struct ps_prochandle *P, int sysindex) 229*7c478bd9Sstevel@tonic-gate { 230*7c478bd9Sstevel@tonic-gate int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd; 231*7c478bd9Sstevel@tonic-gate int washeld = FALSE; 232*7c478bd9Sstevel@tonic-gate sigset_t hold; /* mask of held signals */ 233*7c478bd9Sstevel@tonic-gate int cursig; 234*7c478bd9Sstevel@tonic-gate struct { 235*7c478bd9Sstevel@tonic-gate long cmd; 236*7c478bd9Sstevel@tonic-gate siginfo_t siginfo; 237*7c478bd9Sstevel@tonic-gate } ctl; 238*7c478bd9Sstevel@tonic-gate int sentry; /* old value of stop-on-syscall-entry */ 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate sentry = Psysentry(P, sysindex, TRUE); /* set stop-on-syscall-entry */ 241*7c478bd9Sstevel@tonic-gate 242*7c478bd9Sstevel@tonic-gate /* 243*7c478bd9Sstevel@tonic-gate * If not already blocked, block all signals now. 244*7c478bd9Sstevel@tonic-gate */ 245*7c478bd9Sstevel@tonic-gate if (memcmp(&P->status.pr_lwp.pr_lwphold, &blockable_sigs, 246*7c478bd9Sstevel@tonic-gate sizeof (sigset_t)) != 0) { 247*7c478bd9Sstevel@tonic-gate hold = P->status.pr_lwp.pr_lwphold; 248*7c478bd9Sstevel@tonic-gate P->status.pr_lwp.pr_lwphold = blockable_sigs; 249*7c478bd9Sstevel@tonic-gate P->flags |= SETHOLD; 250*7c478bd9Sstevel@tonic-gate washeld = TRUE; 251*7c478bd9Sstevel@tonic-gate } 252*7c478bd9Sstevel@tonic-gate 253*7c478bd9Sstevel@tonic-gate /* 254*7c478bd9Sstevel@tonic-gate * If there is a current signal, remember it and cancel it. 255*7c478bd9Sstevel@tonic-gate */ 256*7c478bd9Sstevel@tonic-gate if ((cursig = P->status.pr_lwp.pr_cursig) != 0) { 257*7c478bd9Sstevel@tonic-gate ctl.cmd = PCSSIG; 258*7c478bd9Sstevel@tonic-gate ctl.siginfo = P->status.pr_lwp.pr_info; 259*7c478bd9Sstevel@tonic-gate } 260*7c478bd9Sstevel@tonic-gate 261*7c478bd9Sstevel@tonic-gate if (Psetrun(P, 0, PRCSIG | PRCFAULT) == -1) 262*7c478bd9Sstevel@tonic-gate goto bad; 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate while (P->state == PS_RUN) { 265*7c478bd9Sstevel@tonic-gate (void) Pwait(P, 0); 266*7c478bd9Sstevel@tonic-gate } 267*7c478bd9Sstevel@tonic-gate if (P->state != PS_STOP) 268*7c478bd9Sstevel@tonic-gate goto bad; 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate if (cursig) /* restore cursig */ 271*7c478bd9Sstevel@tonic-gate (void) write(ctlfd, &ctl, sizeof (ctl)); 272*7c478bd9Sstevel@tonic-gate if (washeld) { /* restore the signal mask if we set it */ 273*7c478bd9Sstevel@tonic-gate P->status.pr_lwp.pr_lwphold = hold; 274*7c478bd9Sstevel@tonic-gate P->flags |= SETHOLD; 275*7c478bd9Sstevel@tonic-gate } 276*7c478bd9Sstevel@tonic-gate 277*7c478bd9Sstevel@tonic-gate (void) Psysentry(P, sysindex, sentry); /* restore sysentry stop */ 278*7c478bd9Sstevel@tonic-gate 279*7c478bd9Sstevel@tonic-gate if (P->status.pr_lwp.pr_why == PR_SYSENTRY && 280*7c478bd9Sstevel@tonic-gate P->status.pr_lwp.pr_what == sysindex) 281*7c478bd9Sstevel@tonic-gate return (0); 282*7c478bd9Sstevel@tonic-gate bad: 283*7c478bd9Sstevel@tonic-gate return (-1); 284*7c478bd9Sstevel@tonic-gate } 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate /* 288*7c478bd9Sstevel@tonic-gate * Perform system call in controlled process. 289*7c478bd9Sstevel@tonic-gate */ 290*7c478bd9Sstevel@tonic-gate int 291*7c478bd9Sstevel@tonic-gate Psyscall(struct ps_prochandle *P, 292*7c478bd9Sstevel@tonic-gate sysret_t *rval, /* syscall return values */ 293*7c478bd9Sstevel@tonic-gate int sysindex, /* system call index */ 294*7c478bd9Sstevel@tonic-gate uint_t nargs, /* number of arguments to system call */ 295*7c478bd9Sstevel@tonic-gate argdes_t *argp) /* argument descriptor array */ 296*7c478bd9Sstevel@tonic-gate { 297*7c478bd9Sstevel@tonic-gate int agent_created = FALSE; 298*7c478bd9Sstevel@tonic-gate pstatus_t save_pstatus; 299*7c478bd9Sstevel@tonic-gate argdes_t *adp; /* pointer to argument descriptor */ 300*7c478bd9Sstevel@tonic-gate int i; /* general index value */ 301*7c478bd9Sstevel@tonic-gate int model; /* data model */ 302*7c478bd9Sstevel@tonic-gate int error = 0; /* syscall errno */ 303*7c478bd9Sstevel@tonic-gate int Perr = 0; /* local error number */ 304*7c478bd9Sstevel@tonic-gate int sexit; /* old value of stop-on-syscall-exit */ 305*7c478bd9Sstevel@tonic-gate prgreg_t sp; /* adjusted stack pointer */ 306*7c478bd9Sstevel@tonic-gate prgreg_t ap; /* adjusted argument pointer */ 307*7c478bd9Sstevel@tonic-gate sigset_t unblock; 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock); 310*7c478bd9Sstevel@tonic-gate 311*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = 0; /* initialize return values */ 312*7c478bd9Sstevel@tonic-gate rval->sys_rval2 = 0; 313*7c478bd9Sstevel@tonic-gate 314*7c478bd9Sstevel@tonic-gate if (sysindex <= 0 || sysindex > PRMAXSYS || nargs > MAXARGS) 315*7c478bd9Sstevel@tonic-gate goto bad1; /* programming error */ 316*7c478bd9Sstevel@tonic-gate 317*7c478bd9Sstevel@tonic-gate if (P->state == PS_DEAD || P->state == PS_UNDEAD || P->state == PS_IDLE) 318*7c478bd9Sstevel@tonic-gate goto bad1; /* dead processes can't perform system calls */ 319*7c478bd9Sstevel@tonic-gate 320*7c478bd9Sstevel@tonic-gate model = P->status.pr_dmodel; 321*7c478bd9Sstevel@tonic-gate #ifndef _LP64 322*7c478bd9Sstevel@tonic-gate /* We must be a 64-bit process to deal with a 64-bit process */ 323*7c478bd9Sstevel@tonic-gate if (model == PR_MODEL_LP64) 324*7c478bd9Sstevel@tonic-gate goto bad9; 325*7c478bd9Sstevel@tonic-gate #endif 326*7c478bd9Sstevel@tonic-gate 327*7c478bd9Sstevel@tonic-gate /* 328*7c478bd9Sstevel@tonic-gate * Create the /proc agent LWP in the process to do all the work. 329*7c478bd9Sstevel@tonic-gate * (It may already exist; nested create/destroy is permitted 330*7c478bd9Sstevel@tonic-gate * by virtue of the reference count.) 331*7c478bd9Sstevel@tonic-gate */ 332*7c478bd9Sstevel@tonic-gate if (Pcreate_agent(P) != 0) 333*7c478bd9Sstevel@tonic-gate goto bad8; 334*7c478bd9Sstevel@tonic-gate 335*7c478bd9Sstevel@tonic-gate /* 336*7c478bd9Sstevel@tonic-gate * Save agent's status to restore on exit. 337*7c478bd9Sstevel@tonic-gate */ 338*7c478bd9Sstevel@tonic-gate agent_created = TRUE; 339*7c478bd9Sstevel@tonic-gate save_pstatus = P->status; 340*7c478bd9Sstevel@tonic-gate 341*7c478bd9Sstevel@tonic-gate if (P->state != PS_STOP || /* check state of LWP */ 342*7c478bd9Sstevel@tonic-gate (P->status.pr_flags & PR_ASLEEP)) 343*7c478bd9Sstevel@tonic-gate goto bad2; 344*7c478bd9Sstevel@tonic-gate 345*7c478bd9Sstevel@tonic-gate if (Pscantext(P)) /* bad text ? */ 346*7c478bd9Sstevel@tonic-gate goto bad3; 347*7c478bd9Sstevel@tonic-gate 348*7c478bd9Sstevel@tonic-gate /* 349*7c478bd9Sstevel@tonic-gate * Validate arguments and compute the stack frame parameters. 350*7c478bd9Sstevel@tonic-gate * Begin with the current stack pointer. 351*7c478bd9Sstevel@tonic-gate */ 352*7c478bd9Sstevel@tonic-gate #ifdef _LP64 353*7c478bd9Sstevel@tonic-gate if (model == PR_MODEL_LP64) { 354*7c478bd9Sstevel@tonic-gate sp = P->status.pr_lwp.pr_reg[R_SP] + STACK_BIAS; 355*7c478bd9Sstevel@tonic-gate sp = PSTACK_ALIGN64(sp); 356*7c478bd9Sstevel@tonic-gate } else { 357*7c478bd9Sstevel@tonic-gate #endif 358*7c478bd9Sstevel@tonic-gate sp = (uint32_t)P->status.pr_lwp.pr_reg[R_SP]; 359*7c478bd9Sstevel@tonic-gate sp = PSTACK_ALIGN32(sp); 360*7c478bd9Sstevel@tonic-gate #ifdef _LP64 361*7c478bd9Sstevel@tonic-gate } 362*7c478bd9Sstevel@tonic-gate #endif 363*7c478bd9Sstevel@tonic-gate 364*7c478bd9Sstevel@tonic-gate /* 365*7c478bd9Sstevel@tonic-gate * For each AT_BYREF argument, compute the necessary 366*7c478bd9Sstevel@tonic-gate * stack space and the object's stack address. 367*7c478bd9Sstevel@tonic-gate */ 368*7c478bd9Sstevel@tonic-gate for (i = 0, adp = argp; i < nargs; i++, adp++) { 369*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = i; /* in case of error */ 370*7c478bd9Sstevel@tonic-gate switch (adp->arg_type) { 371*7c478bd9Sstevel@tonic-gate default: /* programming error */ 372*7c478bd9Sstevel@tonic-gate goto bad4; 373*7c478bd9Sstevel@tonic-gate case AT_BYVAL: /* simple argument */ 374*7c478bd9Sstevel@tonic-gate break; 375*7c478bd9Sstevel@tonic-gate case AT_BYREF: /* must allocate space */ 376*7c478bd9Sstevel@tonic-gate switch (adp->arg_inout) { 377*7c478bd9Sstevel@tonic-gate case AI_INPUT: 378*7c478bd9Sstevel@tonic-gate case AI_OUTPUT: 379*7c478bd9Sstevel@tonic-gate case AI_INOUT: 380*7c478bd9Sstevel@tonic-gate if (adp->arg_object == NULL) 381*7c478bd9Sstevel@tonic-gate goto bad5; /* programming error */ 382*7c478bd9Sstevel@tonic-gate break; 383*7c478bd9Sstevel@tonic-gate default: /* programming error */ 384*7c478bd9Sstevel@tonic-gate goto bad6; 385*7c478bd9Sstevel@tonic-gate } 386*7c478bd9Sstevel@tonic-gate /* allocate stack space for BYREF argument */ 387*7c478bd9Sstevel@tonic-gate if (adp->arg_size == 0 || adp->arg_size > MAXARGL) 388*7c478bd9Sstevel@tonic-gate goto bad7; /* programming error */ 389*7c478bd9Sstevel@tonic-gate #ifdef _LP64 390*7c478bd9Sstevel@tonic-gate if (model == PR_MODEL_LP64) 391*7c478bd9Sstevel@tonic-gate sp = PSTACK_ALIGN64(sp - adp->arg_size); 392*7c478bd9Sstevel@tonic-gate else 393*7c478bd9Sstevel@tonic-gate #endif 394*7c478bd9Sstevel@tonic-gate sp = PSTACK_ALIGN32(sp - adp->arg_size); 395*7c478bd9Sstevel@tonic-gate adp->arg_value = sp; /* stack address for object */ 396*7c478bd9Sstevel@tonic-gate break; 397*7c478bd9Sstevel@tonic-gate } 398*7c478bd9Sstevel@tonic-gate } 399*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = 0; /* in case of error */ 400*7c478bd9Sstevel@tonic-gate /* 401*7c478bd9Sstevel@tonic-gate * Point of no return. 402*7c478bd9Sstevel@tonic-gate * Perform the system call entry, adjusting %sp. 403*7c478bd9Sstevel@tonic-gate * This moves the LWP to the stopped-on-syscall-entry state 404*7c478bd9Sstevel@tonic-gate * just before the arguments to the system call are fetched. 405*7c478bd9Sstevel@tonic-gate */ 406*7c478bd9Sstevel@tonic-gate ap = Psyscall_setup(P, nargs, sysindex, sp); 407*7c478bd9Sstevel@tonic-gate P->flags |= SETREGS; /* set registers before continuing */ 408*7c478bd9Sstevel@tonic-gate dprintf("Psyscall(): execute(sysindex = %d)\n", sysindex); 409*7c478bd9Sstevel@tonic-gate 410*7c478bd9Sstevel@tonic-gate /* 411*7c478bd9Sstevel@tonic-gate * Execute the syscall instruction and stop on syscall entry. 412*7c478bd9Sstevel@tonic-gate */ 413*7c478bd9Sstevel@tonic-gate if (execute(P, sysindex) != 0 || 414*7c478bd9Sstevel@tonic-gate (!Pissyscall(P, P->status.pr_lwp.pr_reg[R_PC]) && 415*7c478bd9Sstevel@tonic-gate !Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], NULL))) 416*7c478bd9Sstevel@tonic-gate goto bad10; 417*7c478bd9Sstevel@tonic-gate 418*7c478bd9Sstevel@tonic-gate dprintf("Psyscall(): copying arguments\n"); 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate /* 421*7c478bd9Sstevel@tonic-gate * The LWP is stopped at syscall entry. 422*7c478bd9Sstevel@tonic-gate * Copy objects to stack frame for each argument. 423*7c478bd9Sstevel@tonic-gate */ 424*7c478bd9Sstevel@tonic-gate for (i = 0, adp = argp; i < nargs; i++, adp++) { 425*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = i; /* in case of error */ 426*7c478bd9Sstevel@tonic-gate if (adp->arg_type != AT_BYVAL && 427*7c478bd9Sstevel@tonic-gate adp->arg_inout != AI_OUTPUT) { 428*7c478bd9Sstevel@tonic-gate /* copy input byref parameter to process */ 429*7c478bd9Sstevel@tonic-gate if (Pwrite(P, adp->arg_object, adp->arg_size, 430*7c478bd9Sstevel@tonic-gate (uintptr_t)adp->arg_value) != adp->arg_size) 431*7c478bd9Sstevel@tonic-gate goto bad17; 432*7c478bd9Sstevel@tonic-gate } 433*7c478bd9Sstevel@tonic-gate } 434*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = 0; /* in case of error */ 435*7c478bd9Sstevel@tonic-gate if (Psyscall_copyinargs(P, nargs, argp, ap) != 0) 436*7c478bd9Sstevel@tonic-gate goto bad18; 437*7c478bd9Sstevel@tonic-gate 438*7c478bd9Sstevel@tonic-gate /* 439*7c478bd9Sstevel@tonic-gate * Complete the system call. 440*7c478bd9Sstevel@tonic-gate * This moves the LWP to the stopped-on-syscall-exit state. 441*7c478bd9Sstevel@tonic-gate */ 442*7c478bd9Sstevel@tonic-gate dprintf("Psyscall(): set running at sysentry\n"); 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate sexit = Psysexit(P, sysindex, TRUE); /* catch this syscall exit */ 445*7c478bd9Sstevel@tonic-gate do { 446*7c478bd9Sstevel@tonic-gate if (Psetrun(P, 0, 0) == -1) 447*7c478bd9Sstevel@tonic-gate goto bad21; 448*7c478bd9Sstevel@tonic-gate while (P->state == PS_RUN) 449*7c478bd9Sstevel@tonic-gate (void) Pwait(P, 0); 450*7c478bd9Sstevel@tonic-gate } while (P->state == PS_STOP && P->status.pr_lwp.pr_why != PR_SYSEXIT); 451*7c478bd9Sstevel@tonic-gate (void) Psysexit(P, sysindex, sexit); /* restore original setting */ 452*7c478bd9Sstevel@tonic-gate 453*7c478bd9Sstevel@tonic-gate /* 454*7c478bd9Sstevel@tonic-gate * If the system call was _lwp_exit(), we expect that our last call 455*7c478bd9Sstevel@tonic-gate * to Pwait() will yield ENOENT because the LWP no longer exists. 456*7c478bd9Sstevel@tonic-gate */ 457*7c478bd9Sstevel@tonic-gate if (sysindex == SYS_lwp_exit && errno == ENOENT) { 458*7c478bd9Sstevel@tonic-gate dprintf("Psyscall(): _lwp_exit successful\n"); 459*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = rval->sys_rval2 = 0; 460*7c478bd9Sstevel@tonic-gate goto out; 461*7c478bd9Sstevel@tonic-gate } 462*7c478bd9Sstevel@tonic-gate 463*7c478bd9Sstevel@tonic-gate if (P->state != PS_STOP || P->status.pr_lwp.pr_why != PR_SYSEXIT) 464*7c478bd9Sstevel@tonic-gate goto bad22; 465*7c478bd9Sstevel@tonic-gate 466*7c478bd9Sstevel@tonic-gate if (P->status.pr_lwp.pr_what != sysindex) 467*7c478bd9Sstevel@tonic-gate goto bad23; 468*7c478bd9Sstevel@tonic-gate 469*7c478bd9Sstevel@tonic-gate if (!Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], NULL)) { 470*7c478bd9Sstevel@tonic-gate dprintf("Pissyscall_prev() failed\n"); 471*7c478bd9Sstevel@tonic-gate goto bad24; 472*7c478bd9Sstevel@tonic-gate } 473*7c478bd9Sstevel@tonic-gate 474*7c478bd9Sstevel@tonic-gate dprintf("Psyscall(): caught at sysexit\n"); 475*7c478bd9Sstevel@tonic-gate 476*7c478bd9Sstevel@tonic-gate /* 477*7c478bd9Sstevel@tonic-gate * For each argument. 478*7c478bd9Sstevel@tonic-gate */ 479*7c478bd9Sstevel@tonic-gate for (i = 0, adp = argp; i < nargs; i++, adp++) { 480*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = i; /* in case of error */ 481*7c478bd9Sstevel@tonic-gate if (adp->arg_type != AT_BYVAL && 482*7c478bd9Sstevel@tonic-gate adp->arg_inout != AI_INPUT) { 483*7c478bd9Sstevel@tonic-gate /* copy output byref parameter from process */ 484*7c478bd9Sstevel@tonic-gate if (Pread(P, adp->arg_object, adp->arg_size, 485*7c478bd9Sstevel@tonic-gate (uintptr_t)adp->arg_value) != adp->arg_size) 486*7c478bd9Sstevel@tonic-gate goto bad25; 487*7c478bd9Sstevel@tonic-gate } 488*7c478bd9Sstevel@tonic-gate } 489*7c478bd9Sstevel@tonic-gate 490*7c478bd9Sstevel@tonic-gate if (Psyscall_copyoutargs(P, nargs, argp, ap) != 0) 491*7c478bd9Sstevel@tonic-gate goto bad26; 492*7c478bd9Sstevel@tonic-gate 493*7c478bd9Sstevel@tonic-gate /* 494*7c478bd9Sstevel@tonic-gate * Get the return values from the syscall. 495*7c478bd9Sstevel@tonic-gate */ 496*7c478bd9Sstevel@tonic-gate if (P->status.pr_lwp.pr_errno) { /* error return */ 497*7c478bd9Sstevel@tonic-gate error = P->status.pr_lwp.pr_errno; 498*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = -1L; 499*7c478bd9Sstevel@tonic-gate rval->sys_rval2 = -1L; 500*7c478bd9Sstevel@tonic-gate dprintf("Psyscall(%d) fails with errno %d\n", 501*7c478bd9Sstevel@tonic-gate sysindex, error); 502*7c478bd9Sstevel@tonic-gate } else { /* normal return */ 503*7c478bd9Sstevel@tonic-gate rval->sys_rval1 = P->status.pr_lwp.pr_rval1; 504*7c478bd9Sstevel@tonic-gate rval->sys_rval2 = P->status.pr_lwp.pr_rval2; 505*7c478bd9Sstevel@tonic-gate dprintf("Psyscall(%d) returns 0x%lx 0x%lx\n", sysindex, 506*7c478bd9Sstevel@tonic-gate P->status.pr_lwp.pr_rval1, P->status.pr_lwp.pr_rval2); 507*7c478bd9Sstevel@tonic-gate } 508*7c478bd9Sstevel@tonic-gate 509*7c478bd9Sstevel@tonic-gate goto out; 510*7c478bd9Sstevel@tonic-gate 511*7c478bd9Sstevel@tonic-gate bad26: Perr++; 512*7c478bd9Sstevel@tonic-gate bad25: Perr++; 513*7c478bd9Sstevel@tonic-gate bad24: Perr++; 514*7c478bd9Sstevel@tonic-gate bad23: Perr++; 515*7c478bd9Sstevel@tonic-gate bad22: Perr++; 516*7c478bd9Sstevel@tonic-gate bad21: Perr++; 517*7c478bd9Sstevel@tonic-gate Perr++; 518*7c478bd9Sstevel@tonic-gate Perr++; 519*7c478bd9Sstevel@tonic-gate bad18: Perr++; 520*7c478bd9Sstevel@tonic-gate bad17: Perr++; 521*7c478bd9Sstevel@tonic-gate Perr++; 522*7c478bd9Sstevel@tonic-gate Perr++; 523*7c478bd9Sstevel@tonic-gate Perr++; 524*7c478bd9Sstevel@tonic-gate Perr++; 525*7c478bd9Sstevel@tonic-gate Perr++; 526*7c478bd9Sstevel@tonic-gate Perr++; 527*7c478bd9Sstevel@tonic-gate bad10: Perr++; 528*7c478bd9Sstevel@tonic-gate bad9: Perr++; 529*7c478bd9Sstevel@tonic-gate bad8: Perr++; 530*7c478bd9Sstevel@tonic-gate bad7: Perr++; 531*7c478bd9Sstevel@tonic-gate bad6: Perr++; 532*7c478bd9Sstevel@tonic-gate bad5: Perr++; 533*7c478bd9Sstevel@tonic-gate bad4: Perr++; 534*7c478bd9Sstevel@tonic-gate bad3: Perr++; 535*7c478bd9Sstevel@tonic-gate bad2: Perr++; 536*7c478bd9Sstevel@tonic-gate bad1: Perr++; 537*7c478bd9Sstevel@tonic-gate error = -1; 538*7c478bd9Sstevel@tonic-gate dprintf("Psyscall(%d) fails with local error %d\n", sysindex, Perr); 539*7c478bd9Sstevel@tonic-gate 540*7c478bd9Sstevel@tonic-gate out: 541*7c478bd9Sstevel@tonic-gate /* 542*7c478bd9Sstevel@tonic-gate * Destroy the /proc agent LWP now (or just bump down the ref count). 543*7c478bd9Sstevel@tonic-gate */ 544*7c478bd9Sstevel@tonic-gate if (agent_created) { 545*7c478bd9Sstevel@tonic-gate if (P->state != PS_UNDEAD) { 546*7c478bd9Sstevel@tonic-gate P->status = save_pstatus; 547*7c478bd9Sstevel@tonic-gate P->flags |= SETREGS; 548*7c478bd9Sstevel@tonic-gate Psync(P); 549*7c478bd9Sstevel@tonic-gate } 550*7c478bd9Sstevel@tonic-gate Pdestroy_agent(P); 551*7c478bd9Sstevel@tonic-gate } 552*7c478bd9Sstevel@tonic-gate 553*7c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &unblock, NULL); 554*7c478bd9Sstevel@tonic-gate return (error); 555*7c478bd9Sstevel@tonic-gate } 556