1c9bd843ebc00801cc418156a3893362a1dc872edinak/*
1c9bd843ebc00801cc418156a3893362a1dc872edinak * CDDL HEADER START
1c9bd843ebc00801cc418156a3893362a1dc872edinak *
1c9bd843ebc00801cc418156a3893362a1dc872edinak * The contents of this file are subject to the terms of the
1c9bd843ebc00801cc418156a3893362a1dc872edinak * Common Development and Distribution License, Version 1.0 only
1c9bd843ebc00801cc418156a3893362a1dc872edinak * (the "License"). You may not use this file except in compliance
1c9bd843ebc00801cc418156a3893362a1dc872edinak * with the License.
1c9bd843ebc00801cc418156a3893362a1dc872edinak *
1c9bd843ebc00801cc418156a3893362a1dc872edinak * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1c9bd843ebc00801cc418156a3893362a1dc872edinak * or http://www.opensolaris.org/os/licensing.
1c9bd843ebc00801cc418156a3893362a1dc872edinak * See the License for the specific language governing permissions
1c9bd843ebc00801cc418156a3893362a1dc872edinak * and limitations under the License.
1c9bd843ebc00801cc418156a3893362a1dc872edinak *
1c9bd843ebc00801cc418156a3893362a1dc872edinak * When distributing Covered Code, include this CDDL HEADER in each
1c9bd843ebc00801cc418156a3893362a1dc872edinak * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1c9bd843ebc00801cc418156a3893362a1dc872edinak * If applicable, add the following below this CDDL HEADER, with the
1c9bd843ebc00801cc418156a3893362a1dc872edinak * fields enclosed by brackets "[]" replaced with your own identifying
1c9bd843ebc00801cc418156a3893362a1dc872edinak * information: Portions Copyright [yyyy] [name of copyright owner]
1c9bd843ebc00801cc418156a3893362a1dc872edinak *
1c9bd843ebc00801cc418156a3893362a1dc872edinak * CDDL HEADER END
1c9bd843ebc00801cc418156a3893362a1dc872edinak */
1c9bd843ebc00801cc418156a3893362a1dc872edinak/*
1c9bd843ebc00801cc418156a3893362a1dc872edinak * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
1c9bd843ebc00801cc418156a3893362a1dc872edinak * Use is subject to license terms.
1c9bd843ebc00801cc418156a3893362a1dc872edinak */
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinak#pragma ident "%Z%%M% %I% %E% SMI"
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/types.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/param.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/thread.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/sysmacros.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/signal.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/cred.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/user.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/errno.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/vnode.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/mman.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/kmem.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/proc.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/pathname.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/cmn_err.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/systm.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/elf.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/vmsystm.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/debug.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/old_procfs.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/auxv.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/exec.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/prsystm.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <vm/as.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <vm/rm.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/modctl.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/systeminfo.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/machelf.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include <sys/zone.h>
1c9bd843ebc00801cc418156a3893362a1dc872edinak#include "elf_impl.h"
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinakextern void oprgetstatus(kthread_t *, prstatus_t *, zone_t *);
1c9bd843ebc00801cc418156a3893362a1dc872edinakextern void oprgetpsinfo(proc_t *, prpsinfo_t *, kthread_t *);
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinakvoid
1c9bd843ebc00801cc418156a3893362a1dc872edinaksetup_old_note_header(Phdr *v, proc_t *p)
1c9bd843ebc00801cc418156a3893362a1dc872edinak{
1c9bd843ebc00801cc418156a3893362a1dc872edinak int nlwp = p->p_lwpcnt;
1c9bd843ebc00801cc418156a3893362a1dc872edinak size_t size;
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinak v[0].p_type = PT_NOTE;
1c9bd843ebc00801cc418156a3893362a1dc872edinak v[0].p_flags = PF_R;
1c9bd843ebc00801cc418156a3893362a1dc872edinak v[0].p_filesz = (sizeof (Note) * (3 + nlwp))
1c9bd843ebc00801cc418156a3893362a1dc872edinak + roundup(sizeof (prpsinfo_t), sizeof (Word))
1c9bd843ebc00801cc418156a3893362a1dc872edinak + roundup(strlen(platform) + 1, sizeof (Word))
1c9bd843ebc00801cc418156a3893362a1dc872edinak + roundup(__KERN_NAUXV_IMPL * sizeof (aux_entry_t),
1c9bd843ebc00801cc418156a3893362a1dc872edinak sizeof (Word))
1c9bd843ebc00801cc418156a3893362a1dc872edinak + nlwp * roundup(sizeof (prstatus_t), sizeof (Word));
1c9bd843ebc00801cc418156a3893362a1dc872edinak if (prhasfp())
1c9bd843ebc00801cc418156a3893362a1dc872edinak v[0].p_filesz += nlwp * sizeof (Note)
1c9bd843ebc00801cc418156a3893362a1dc872edinak + nlwp*roundup(sizeof (prfpregset_t), sizeof (Word));
1c9bd843ebc00801cc418156a3893362a1dc872edinak if ((size = prhasx(p)? prgetprxregsize(p) : 0) != 0)
1c9bd843ebc00801cc418156a3893362a1dc872edinak v[0].p_filesz += nlwp * sizeof (Note)
1c9bd843ebc00801cc418156a3893362a1dc872edinak + nlwp * roundup(size, sizeof (Word));
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinak#if defined(__sparc)
1c9bd843ebc00801cc418156a3893362a1dc872edinak /*
1c9bd843ebc00801cc418156a3893362a1dc872edinak * Figure out the number and sizes of register windows.
1c9bd843ebc00801cc418156a3893362a1dc872edinak */
1c9bd843ebc00801cc418156a3893362a1dc872edinak {
1c9bd843ebc00801cc418156a3893362a1dc872edinak kthread_t *t = p->p_tlist;
1c9bd843ebc00801cc418156a3893362a1dc872edinak do {
1c9bd843ebc00801cc418156a3893362a1dc872edinak if ((size = prnwindows(ttolwp(t))) != 0) {
1c9bd843ebc00801cc418156a3893362a1dc872edinak size = sizeof (gwindows_t) -
1c9bd843ebc00801cc418156a3893362a1dc872edinak (SPARC_MAXREGWINDOW - size) *
1c9bd843ebc00801cc418156a3893362a1dc872edinak sizeof (struct rwindow);
1c9bd843ebc00801cc418156a3893362a1dc872edinak v[0].p_filesz += sizeof (Note) +
1c9bd843ebc00801cc418156a3893362a1dc872edinak roundup(size, sizeof (Word));
1c9bd843ebc00801cc418156a3893362a1dc872edinak }
1c9bd843ebc00801cc418156a3893362a1dc872edinak } while ((t = t->t_forw) != p->p_tlist);
1c9bd843ebc00801cc418156a3893362a1dc872edinak }
1c9bd843ebc00801cc418156a3893362a1dc872edinak#endif /* __sparc */
1c9bd843ebc00801cc418156a3893362a1dc872edinak}
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinakint
1c9bd843ebc00801cc418156a3893362a1dc872edinakwrite_old_elfnotes(proc_t *p, int sig, vnode_t *vp, offset_t offset,
1c9bd843ebc00801cc418156a3893362a1dc872edinak rlim64_t rlimit, cred_t *credp)
1c9bd843ebc00801cc418156a3893362a1dc872edinak{
1c9bd843ebc00801cc418156a3893362a1dc872edinak union {
1c9bd843ebc00801cc418156a3893362a1dc872edinak prpsinfo_t psinfo;
1c9bd843ebc00801cc418156a3893362a1dc872edinak prstatus_t prstat;
1c9bd843ebc00801cc418156a3893362a1dc872edinak prfpregset_t fpregs;
1c9bd843ebc00801cc418156a3893362a1dc872edinak#if defined(__sparc)
1c9bd843ebc00801cc418156a3893362a1dc872edinak gwindows_t gwindows;
1c9bd843ebc00801cc418156a3893362a1dc872edinak#endif /* __sparc */
1c9bd843ebc00801cc418156a3893362a1dc872edinak char xregs[1];
1c9bd843ebc00801cc418156a3893362a1dc872edinak aux_entry_t auxv[__KERN_NAUXV_IMPL];
1c9bd843ebc00801cc418156a3893362a1dc872edinak } *bigwad;
1c9bd843ebc00801cc418156a3893362a1dc872edinak int xregsize = prhasx(p)? prgetprxregsize(p) : 0;
1c9bd843ebc00801cc418156a3893362a1dc872edinak size_t bigsize = MAX(sizeof (*bigwad), (size_t)xregsize);
1c9bd843ebc00801cc418156a3893362a1dc872edinak kthread_t *t;
1c9bd843ebc00801cc418156a3893362a1dc872edinak klwp_t *lwp;
1c9bd843ebc00801cc418156a3893362a1dc872edinak user_t *up;
1c9bd843ebc00801cc418156a3893362a1dc872edinak int i;
1c9bd843ebc00801cc418156a3893362a1dc872edinak int nlwp;
1c9bd843ebc00801cc418156a3893362a1dc872edinak int error;
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinak bigwad = kmem_alloc(bigsize, KM_SLEEP);
1c9bd843ebc00801cc418156a3893362a1dc872edinak
1c9bd843ebc00801cc418156a3893362a1dc872edinak /*
1c9bd843ebc00801cc418156a3893362a1dc872edinak * The order of the elfnote entries should be same here and in
1c9bd843ebc00801cc418156a3893362a1dc872edinak * the gcore(1) command. Synchronization is needed between the
1c9bd843ebc00801cc418156a3893362a1dc872edinak * kernel and libproc's Pfgcore() function where the meat of
* the gcore(1) command lives.
*/
mutex_enter(&p->p_lock);
oprgetpsinfo(p, &bigwad->psinfo, NULL);
mutex_exit(&p->p_lock);
error = elfnote(vp, &offset, NT_PRPSINFO, sizeof (bigwad->psinfo),
(caddr_t)&bigwad->psinfo, rlimit, credp);
if (error)
goto done;
error = elfnote(vp, &offset, NT_PLATFORM, strlen(platform) + 1,
platform, rlimit, credp);
if (error)
goto done;
up = PTOU(p);
for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
bigwad->auxv[i].a_type = up->u_auxv[i].a_type;
bigwad->auxv[i].a_un.a_val = up->u_auxv[i].a_un.a_val;
}
error = elfnote(vp, &offset, NT_AUXV, sizeof (bigwad->auxv),
(caddr_t)bigwad->auxv, rlimit, credp);
if (error)
goto done;
t = curthread;
nlwp = p->p_lwpcnt;
do {
ASSERT(nlwp != 0);
nlwp--;
lwp = ttolwp(t);
mutex_enter(&p->p_lock);
if (t == curthread) {
uchar_t oldsig;
/*
* Modify t_whystop and lwp_cursig so it appears that
* the current LWP is stopped after faulting on the
* signal that caused the core dump. As a result,
* oprgetstatus() will record that signal, the saved
* lwp_siginfo, and its signal handler in the core file
* status. We restore lwp_cursig in case a subsequent
* signal was received while dumping core.
*/
oldsig = lwp->lwp_cursig;
lwp->lwp_cursig = (uchar_t)sig;
t->t_whystop = PR_FAULTED;
oprgetstatus(t, &bigwad->prstat, p->p_zone);
bigwad->prstat.pr_why = 0;
t->t_whystop = 0;
lwp->lwp_cursig = oldsig;
} else {
oprgetstatus(t, &bigwad->prstat, p->p_zone);
}
mutex_exit(&p->p_lock);
error = elfnote(vp, &offset, NT_PRSTATUS,
sizeof (bigwad->prstat), (caddr_t)&bigwad->prstat,
rlimit, credp);
if (error)
goto done;
if (prhasfp()) {
prgetprfpregs(lwp, &bigwad->fpregs);
error = elfnote(vp, &offset, NT_PRFPREG,
sizeof (bigwad->fpregs), (caddr_t)&bigwad->fpregs,
rlimit, credp);
if (error)
goto done;
}
#if defined(__sparc)
/*
* Unspilled SPARC register windows.
*/
{
size_t size = prnwindows(lwp);
if (size != 0) {
size = sizeof (gwindows_t) -
(SPARC_MAXREGWINDOW - size) *
sizeof (struct rwindow);
prgetwindows(lwp, &bigwad->gwindows);
error = elfnote(vp, &offset, NT_GWINDOWS,
size, (caddr_t)&bigwad->gwindows,
rlimit, credp);
if (error)
goto done;
}
}
#endif /* __sparc */
if (xregsize) {
prgetprxregs(lwp, bigwad->xregs);
error = elfnote(vp, &offset, NT_PRXREG,
xregsize, bigwad->xregs, rlimit, credp);
if (error)
goto done;
}
} while ((t = t->t_forw) != curthread);
ASSERT(nlwp == 0);
done:
kmem_free(bigwad, bigsize);
return (error);
}