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