1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/param.h> 31*0Sstevel@tonic-gate #include <sys/thread.h> 32*0Sstevel@tonic-gate #include <sys/sysmacros.h> 33*0Sstevel@tonic-gate #include <sys/signal.h> 34*0Sstevel@tonic-gate #include <sys/cred.h> 35*0Sstevel@tonic-gate #include <sys/user.h> 36*0Sstevel@tonic-gate #include <sys/errno.h> 37*0Sstevel@tonic-gate #include <sys/vnode.h> 38*0Sstevel@tonic-gate #include <sys/mman.h> 39*0Sstevel@tonic-gate #include <sys/kmem.h> 40*0Sstevel@tonic-gate #include <sys/proc.h> 41*0Sstevel@tonic-gate #include <sys/pathname.h> 42*0Sstevel@tonic-gate #include <sys/cmn_err.h> 43*0Sstevel@tonic-gate #include <sys/systm.h> 44*0Sstevel@tonic-gate #include <sys/elf.h> 45*0Sstevel@tonic-gate #include <sys/vmsystm.h> 46*0Sstevel@tonic-gate #include <sys/debug.h> 47*0Sstevel@tonic-gate #include <sys/old_procfs.h> 48*0Sstevel@tonic-gate #include <sys/auxv.h> 49*0Sstevel@tonic-gate #include <sys/exec.h> 50*0Sstevel@tonic-gate #include <sys/prsystm.h> 51*0Sstevel@tonic-gate #include <vm/as.h> 52*0Sstevel@tonic-gate #include <vm/rm.h> 53*0Sstevel@tonic-gate #include <sys/modctl.h> 54*0Sstevel@tonic-gate #include <sys/systeminfo.h> 55*0Sstevel@tonic-gate #include <sys/machelf.h> 56*0Sstevel@tonic-gate #include <sys/zone.h> 57*0Sstevel@tonic-gate #include "elf_impl.h" 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate extern void oprgetstatus(kthread_t *, prstatus_t *, zone_t *); 60*0Sstevel@tonic-gate extern void oprgetpsinfo(proc_t *, prpsinfo_t *, kthread_t *); 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate void 63*0Sstevel@tonic-gate setup_old_note_header(Phdr *v, proc_t *p) 64*0Sstevel@tonic-gate { 65*0Sstevel@tonic-gate int nlwp = p->p_lwpcnt; 66*0Sstevel@tonic-gate size_t size; 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate v[0].p_type = PT_NOTE; 69*0Sstevel@tonic-gate v[0].p_flags = PF_R; 70*0Sstevel@tonic-gate v[0].p_filesz = (sizeof (Note) * (3 + nlwp)) 71*0Sstevel@tonic-gate + roundup(sizeof (prpsinfo_t), sizeof (Word)) 72*0Sstevel@tonic-gate + roundup(strlen(platform) + 1, sizeof (Word)) 73*0Sstevel@tonic-gate + roundup(__KERN_NAUXV_IMPL * sizeof (aux_entry_t), 74*0Sstevel@tonic-gate sizeof (Word)) 75*0Sstevel@tonic-gate + nlwp * roundup(sizeof (prstatus_t), sizeof (Word)); 76*0Sstevel@tonic-gate if (prhasfp()) 77*0Sstevel@tonic-gate v[0].p_filesz += nlwp * sizeof (Note) 78*0Sstevel@tonic-gate + nlwp*roundup(sizeof (prfpregset_t), sizeof (Word)); 79*0Sstevel@tonic-gate if ((size = prhasx(p)? prgetprxregsize(p) : 0) != 0) 80*0Sstevel@tonic-gate v[0].p_filesz += nlwp * sizeof (Note) 81*0Sstevel@tonic-gate + nlwp * roundup(size, sizeof (Word)); 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate #if defined(__sparc) 84*0Sstevel@tonic-gate /* 85*0Sstevel@tonic-gate * Figure out the number and sizes of register windows. 86*0Sstevel@tonic-gate */ 87*0Sstevel@tonic-gate { 88*0Sstevel@tonic-gate kthread_t *t = p->p_tlist; 89*0Sstevel@tonic-gate do { 90*0Sstevel@tonic-gate if ((size = prnwindows(ttolwp(t))) != 0) { 91*0Sstevel@tonic-gate size = sizeof (gwindows_t) - 92*0Sstevel@tonic-gate (SPARC_MAXREGWINDOW - size) * 93*0Sstevel@tonic-gate sizeof (struct rwindow); 94*0Sstevel@tonic-gate v[0].p_filesz += sizeof (Note) + 95*0Sstevel@tonic-gate roundup(size, sizeof (Word)); 96*0Sstevel@tonic-gate } 97*0Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate #endif /* __sparc */ 100*0Sstevel@tonic-gate } 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate int 103*0Sstevel@tonic-gate write_old_elfnotes(proc_t *p, int sig, vnode_t *vp, offset_t offset, 104*0Sstevel@tonic-gate rlim64_t rlimit, cred_t *credp) 105*0Sstevel@tonic-gate { 106*0Sstevel@tonic-gate union { 107*0Sstevel@tonic-gate prpsinfo_t psinfo; 108*0Sstevel@tonic-gate prstatus_t prstat; 109*0Sstevel@tonic-gate prfpregset_t fpregs; 110*0Sstevel@tonic-gate #if defined(__sparc) 111*0Sstevel@tonic-gate gwindows_t gwindows; 112*0Sstevel@tonic-gate #endif /* __sparc */ 113*0Sstevel@tonic-gate char xregs[1]; 114*0Sstevel@tonic-gate aux_entry_t auxv[__KERN_NAUXV_IMPL]; 115*0Sstevel@tonic-gate } *bigwad; 116*0Sstevel@tonic-gate int xregsize = prhasx(p)? prgetprxregsize(p) : 0; 117*0Sstevel@tonic-gate size_t bigsize = MAX(sizeof (*bigwad), (size_t)xregsize); 118*0Sstevel@tonic-gate kthread_t *t; 119*0Sstevel@tonic-gate klwp_t *lwp; 120*0Sstevel@tonic-gate user_t *up; 121*0Sstevel@tonic-gate int i; 122*0Sstevel@tonic-gate int nlwp; 123*0Sstevel@tonic-gate int error; 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate bigwad = kmem_alloc(bigsize, KM_SLEEP); 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate /* 128*0Sstevel@tonic-gate * The order of the elfnote entries should be same here and in 129*0Sstevel@tonic-gate * the gcore(1) command. Synchronization is needed between the 130*0Sstevel@tonic-gate * kernel and libproc's Pfgcore() function where the meat of 131*0Sstevel@tonic-gate * the gcore(1) command lives. 132*0Sstevel@tonic-gate */ 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 135*0Sstevel@tonic-gate oprgetpsinfo(p, &bigwad->psinfo, NULL); 136*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 137*0Sstevel@tonic-gate error = elfnote(vp, &offset, NT_PRPSINFO, sizeof (bigwad->psinfo), 138*0Sstevel@tonic-gate (caddr_t)&bigwad->psinfo, rlimit, credp); 139*0Sstevel@tonic-gate if (error) 140*0Sstevel@tonic-gate goto done; 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate error = elfnote(vp, &offset, NT_PLATFORM, strlen(platform) + 1, 143*0Sstevel@tonic-gate platform, rlimit, credp); 144*0Sstevel@tonic-gate if (error) 145*0Sstevel@tonic-gate goto done; 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate up = PTOU(p); 148*0Sstevel@tonic-gate for (i = 0; i < __KERN_NAUXV_IMPL; i++) { 149*0Sstevel@tonic-gate bigwad->auxv[i].a_type = up->u_auxv[i].a_type; 150*0Sstevel@tonic-gate bigwad->auxv[i].a_un.a_val = up->u_auxv[i].a_un.a_val; 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate error = elfnote(vp, &offset, NT_AUXV, sizeof (bigwad->auxv), 153*0Sstevel@tonic-gate (caddr_t)bigwad->auxv, rlimit, credp); 154*0Sstevel@tonic-gate if (error) 155*0Sstevel@tonic-gate goto done; 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate t = curthread; 158*0Sstevel@tonic-gate nlwp = p->p_lwpcnt; 159*0Sstevel@tonic-gate do { 160*0Sstevel@tonic-gate ASSERT(nlwp != 0); 161*0Sstevel@tonic-gate nlwp--; 162*0Sstevel@tonic-gate lwp = ttolwp(t); 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 165*0Sstevel@tonic-gate if (t == curthread) { 166*0Sstevel@tonic-gate uchar_t oldsig; 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /* 169*0Sstevel@tonic-gate * Modify t_whystop and lwp_cursig so it appears that 170*0Sstevel@tonic-gate * the current LWP is stopped after faulting on the 171*0Sstevel@tonic-gate * signal that caused the core dump. As a result, 172*0Sstevel@tonic-gate * oprgetstatus() will record that signal, the saved 173*0Sstevel@tonic-gate * lwp_siginfo, and its signal handler in the core file 174*0Sstevel@tonic-gate * status. We restore lwp_cursig in case a subsequent 175*0Sstevel@tonic-gate * signal was received while dumping core. 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate oldsig = lwp->lwp_cursig; 178*0Sstevel@tonic-gate lwp->lwp_cursig = (uchar_t)sig; 179*0Sstevel@tonic-gate t->t_whystop = PR_FAULTED; 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate oprgetstatus(t, &bigwad->prstat, p->p_zone); 182*0Sstevel@tonic-gate bigwad->prstat.pr_why = 0; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate t->t_whystop = 0; 185*0Sstevel@tonic-gate lwp->lwp_cursig = oldsig; 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate } else { 188*0Sstevel@tonic-gate oprgetstatus(t, &bigwad->prstat, p->p_zone); 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 191*0Sstevel@tonic-gate error = elfnote(vp, &offset, NT_PRSTATUS, 192*0Sstevel@tonic-gate sizeof (bigwad->prstat), (caddr_t)&bigwad->prstat, 193*0Sstevel@tonic-gate rlimit, credp); 194*0Sstevel@tonic-gate if (error) 195*0Sstevel@tonic-gate goto done; 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate if (prhasfp()) { 198*0Sstevel@tonic-gate prgetprfpregs(lwp, &bigwad->fpregs); 199*0Sstevel@tonic-gate error = elfnote(vp, &offset, NT_PRFPREG, 200*0Sstevel@tonic-gate sizeof (bigwad->fpregs), (caddr_t)&bigwad->fpregs, 201*0Sstevel@tonic-gate rlimit, credp); 202*0Sstevel@tonic-gate if (error) 203*0Sstevel@tonic-gate goto done; 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate #if defined(__sparc) 207*0Sstevel@tonic-gate /* 208*0Sstevel@tonic-gate * Unspilled SPARC register windows. 209*0Sstevel@tonic-gate */ 210*0Sstevel@tonic-gate { 211*0Sstevel@tonic-gate size_t size = prnwindows(lwp); 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate if (size != 0) { 214*0Sstevel@tonic-gate size = sizeof (gwindows_t) - 215*0Sstevel@tonic-gate (SPARC_MAXREGWINDOW - size) * 216*0Sstevel@tonic-gate sizeof (struct rwindow); 217*0Sstevel@tonic-gate prgetwindows(lwp, &bigwad->gwindows); 218*0Sstevel@tonic-gate error = elfnote(vp, &offset, NT_GWINDOWS, 219*0Sstevel@tonic-gate size, (caddr_t)&bigwad->gwindows, 220*0Sstevel@tonic-gate rlimit, credp); 221*0Sstevel@tonic-gate if (error) 222*0Sstevel@tonic-gate goto done; 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate #endif /* __sparc */ 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate if (xregsize) { 228*0Sstevel@tonic-gate prgetprxregs(lwp, bigwad->xregs); 229*0Sstevel@tonic-gate error = elfnote(vp, &offset, NT_PRXREG, 230*0Sstevel@tonic-gate xregsize, bigwad->xregs, rlimit, credp); 231*0Sstevel@tonic-gate if (error) 232*0Sstevel@tonic-gate goto done; 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate } while ((t = t->t_forw) != curthread); 235*0Sstevel@tonic-gate ASSERT(nlwp == 0); 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate done: 238*0Sstevel@tonic-gate kmem_free(bigwad, bigsize); 239*0Sstevel@tonic-gate return (error); 240*0Sstevel@tonic-gate } 241