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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/thread.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/signal.h>
327c478bd9Sstevel@tonic-gate #include <sys/cred.h>
337c478bd9Sstevel@tonic-gate #include <sys/user.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/proc.h>
397c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
407c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
417c478bd9Sstevel@tonic-gate #include <sys/systm.h>
427c478bd9Sstevel@tonic-gate #include <sys/elf.h>
437c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
457c478bd9Sstevel@tonic-gate #include <sys/old_procfs.h>
467c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
477c478bd9Sstevel@tonic-gate #include <sys/exec.h>
487c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
497c478bd9Sstevel@tonic-gate #include <vm/as.h>
507c478bd9Sstevel@tonic-gate #include <vm/rm.h>
517c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
527c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
537c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
547c478bd9Sstevel@tonic-gate #include <sys/zone.h>
557c478bd9Sstevel@tonic-gate #include "elf_impl.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate extern void oprgetstatus(kthread_t *, prstatus_t *, zone_t *);
587c478bd9Sstevel@tonic-gate extern void oprgetpsinfo(proc_t *, prpsinfo_t *, kthread_t *);
597c478bd9Sstevel@tonic-gate 
60*ed093b41SRobert Mustacchi /*
61*ed093b41SRobert Mustacchi  * Historically the system dumped the xreg note when on SPARC. Because we no
62*ed093b41SRobert Mustacchi  * longer support SPARC we do not dump the old note form of the xregs for any
63*ed093b41SRobert Mustacchi  * additional platforms. Please do not add this back unless it's for SPARC's
64*ed093b41SRobert Mustacchi  * future resurrection.
65*ed093b41SRobert Mustacchi  */
667c478bd9Sstevel@tonic-gate void
setup_old_note_header(Phdr * v,proc_t * p)677c478bd9Sstevel@tonic-gate setup_old_note_header(Phdr *v, proc_t *p)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	int nlwp = p->p_lwpcnt;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	v[0].p_type = PT_NOTE;
727c478bd9Sstevel@tonic-gate 	v[0].p_flags = PF_R;
737c478bd9Sstevel@tonic-gate 	v[0].p_filesz = (sizeof (Note) * (3 + nlwp))
747c478bd9Sstevel@tonic-gate 	    + roundup(sizeof (prpsinfo_t), sizeof (Word))
757c478bd9Sstevel@tonic-gate 	    + roundup(strlen(platform) + 1, sizeof (Word))
767c478bd9Sstevel@tonic-gate 	    + roundup(__KERN_NAUXV_IMPL * sizeof (aux_entry_t),
777c478bd9Sstevel@tonic-gate 	    sizeof (Word))
787c478bd9Sstevel@tonic-gate 	    + nlwp * roundup(sizeof (prstatus_t), sizeof (Word));
79*ed093b41SRobert Mustacchi 	if (prhasfp()) {
80*ed093b41SRobert Mustacchi 		v[0].p_filesz += nlwp * sizeof (Note) +
81*ed093b41SRobert Mustacchi 		    nlwp * roundup(sizeof (prfpregset_t), sizeof (Word));
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate int
write_old_elfnotes(proc_t * p,int sig,vnode_t * vp,offset_t offset,rlim64_t rlimit,cred_t * credp)867c478bd9Sstevel@tonic-gate write_old_elfnotes(proc_t *p, int sig, vnode_t *vp, offset_t offset,
877c478bd9Sstevel@tonic-gate     rlim64_t rlimit, cred_t *credp)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	union {
907c478bd9Sstevel@tonic-gate 		prpsinfo_t	psinfo;
917c478bd9Sstevel@tonic-gate 		prstatus_t	prstat;
927c478bd9Sstevel@tonic-gate 		prfpregset_t	fpregs;
937c478bd9Sstevel@tonic-gate 		aux_entry_t	auxv[__KERN_NAUXV_IMPL];
947c478bd9Sstevel@tonic-gate 	} *bigwad;
95*ed093b41SRobert Mustacchi 	size_t bigsize = sizeof (*bigwad);
967c478bd9Sstevel@tonic-gate 	kthread_t *t;
977c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
987c478bd9Sstevel@tonic-gate 	user_t *up;
997c478bd9Sstevel@tonic-gate 	int i;
1007c478bd9Sstevel@tonic-gate 	int nlwp;
1017c478bd9Sstevel@tonic-gate 	int error;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	bigwad = kmem_alloc(bigsize, KM_SLEEP);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * The order of the elfnote entries should be same here and in
1077c478bd9Sstevel@tonic-gate 	 * the gcore(1) command.  Synchronization is needed between the
1087c478bd9Sstevel@tonic-gate 	 * kernel and libproc's Pfgcore() function where the meat of
1097c478bd9Sstevel@tonic-gate 	 * the gcore(1) command lives.
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1137c478bd9Sstevel@tonic-gate 	oprgetpsinfo(p, &bigwad->psinfo, NULL);
1147c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1157c478bd9Sstevel@tonic-gate 	error = elfnote(vp, &offset, NT_PRPSINFO, sizeof (bigwad->psinfo),
1167c478bd9Sstevel@tonic-gate 	    (caddr_t)&bigwad->psinfo, rlimit, credp);
1177c478bd9Sstevel@tonic-gate 	if (error)
1187c478bd9Sstevel@tonic-gate 		goto done;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	error = elfnote(vp, &offset, NT_PLATFORM, strlen(platform) + 1,
1217c478bd9Sstevel@tonic-gate 	    platform, rlimit, credp);
1227c478bd9Sstevel@tonic-gate 	if (error)
1237c478bd9Sstevel@tonic-gate 		goto done;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	up = PTOU(p);
1267c478bd9Sstevel@tonic-gate 	for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
1277c478bd9Sstevel@tonic-gate 		bigwad->auxv[i].a_type = up->u_auxv[i].a_type;
1287c478bd9Sstevel@tonic-gate 		bigwad->auxv[i].a_un.a_val = up->u_auxv[i].a_un.a_val;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	error = elfnote(vp, &offset, NT_AUXV, sizeof (bigwad->auxv),
1317c478bd9Sstevel@tonic-gate 	    (caddr_t)bigwad->auxv, rlimit, credp);
1327c478bd9Sstevel@tonic-gate 	if (error)
1337c478bd9Sstevel@tonic-gate 		goto done;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	t = curthread;
1367c478bd9Sstevel@tonic-gate 	nlwp = p->p_lwpcnt;
1377c478bd9Sstevel@tonic-gate 	do {
1387c478bd9Sstevel@tonic-gate 		ASSERT(nlwp != 0);
1397c478bd9Sstevel@tonic-gate 		nlwp--;
1407c478bd9Sstevel@tonic-gate 		lwp = ttolwp(t);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1437c478bd9Sstevel@tonic-gate 		if (t == curthread) {
1447c478bd9Sstevel@tonic-gate 			uchar_t oldsig;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 			/*
1477c478bd9Sstevel@tonic-gate 			 * Modify t_whystop and lwp_cursig so it appears that
1487c478bd9Sstevel@tonic-gate 			 * the current LWP is stopped after faulting on the
1497c478bd9Sstevel@tonic-gate 			 * signal that caused the core dump.  As a result,
1507c478bd9Sstevel@tonic-gate 			 * oprgetstatus() will record that signal, the saved
1517c478bd9Sstevel@tonic-gate 			 * lwp_siginfo, and its signal handler in the core file
1527c478bd9Sstevel@tonic-gate 			 * status.  We restore lwp_cursig in case a subsequent
1537c478bd9Sstevel@tonic-gate 			 * signal was received while dumping core.
1547c478bd9Sstevel@tonic-gate 			 */
1557c478bd9Sstevel@tonic-gate 			oldsig = lwp->lwp_cursig;
1567c478bd9Sstevel@tonic-gate 			lwp->lwp_cursig = (uchar_t)sig;
1577c478bd9Sstevel@tonic-gate 			t->t_whystop = PR_FAULTED;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 			oprgetstatus(t, &bigwad->prstat, p->p_zone);
1607c478bd9Sstevel@tonic-gate 			bigwad->prstat.pr_why = 0;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 			t->t_whystop = 0;
1637c478bd9Sstevel@tonic-gate 			lwp->lwp_cursig = oldsig;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		} else {
1667c478bd9Sstevel@tonic-gate 			oprgetstatus(t, &bigwad->prstat, p->p_zone);
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1697c478bd9Sstevel@tonic-gate 		error = elfnote(vp, &offset, NT_PRSTATUS,
1707c478bd9Sstevel@tonic-gate 		    sizeof (bigwad->prstat), (caddr_t)&bigwad->prstat,
1717c478bd9Sstevel@tonic-gate 		    rlimit, credp);
1727c478bd9Sstevel@tonic-gate 		if (error)
1737c478bd9Sstevel@tonic-gate 			goto done;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if (prhasfp()) {
1767c478bd9Sstevel@tonic-gate 			prgetprfpregs(lwp, &bigwad->fpregs);
1777c478bd9Sstevel@tonic-gate 			error = elfnote(vp, &offset, NT_PRFPREG,
1787c478bd9Sstevel@tonic-gate 			    sizeof (bigwad->fpregs), (caddr_t)&bigwad->fpregs,
1797c478bd9Sstevel@tonic-gate 			    rlimit, credp);
1807c478bd9Sstevel@tonic-gate 			if (error)
1817c478bd9Sstevel@tonic-gate 				goto done;
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != curthread);
1847c478bd9Sstevel@tonic-gate 	ASSERT(nlwp == 0);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate done:
1877c478bd9Sstevel@tonic-gate 	kmem_free(bigwad, bigsize);
1887c478bd9Sstevel@tonic-gate 	return (error);
1897c478bd9Sstevel@tonic-gate }
190