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