xref: /illumos-gate/usr/src/uts/common/exec/elf/old_notes.c (revision 201ceb75ab95f9bf1f42ea1dc9ab363b43ba47cf)
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 /*
61  * Historically the system dumped the xreg note when on SPARC. Because we no
62  * longer support SPARC we do not dump the old note form of the xregs for any
63  * additional platforms. Please do not add this back unless it's for SPARC's
64  * future resurrection.
65  */
66 void
67 setup_old_note_header(Phdr *v, proc_t *p)
68 {
69 	int nlwp = p->p_lwpcnt;
70 
71 	v[0].p_type = PT_NOTE;
72 	v[0].p_flags = PF_R;
73 	v[0].p_filesz = (sizeof (Note) * (3 + nlwp))
74 	    + roundup(sizeof (prpsinfo_t), sizeof (Word))
75 	    + roundup(strlen(platform) + 1, sizeof (Word))
76 	    + roundup(__KERN_NAUXV_IMPL * sizeof (aux_entry_t),
77 	    sizeof (Word))
78 	    + nlwp * roundup(sizeof (prstatus_t), sizeof (Word));
79 	if (prhasfp()) {
80 		v[0].p_filesz += nlwp * sizeof (Note) +
81 		    nlwp * roundup(sizeof (prfpregset_t), sizeof (Word));
82 	}
83 }
84 
85 int
86 write_old_elfnotes(proc_t *p, int sig, vnode_t *vp, offset_t offset,
87     rlim64_t rlimit, cred_t *credp)
88 {
89 	union {
90 		prpsinfo_t	psinfo;
91 		prstatus_t	prstat;
92 		prfpregset_t	fpregs;
93 		aux_entry_t	auxv[__KERN_NAUXV_IMPL];
94 	} *bigwad;
95 	size_t bigsize = sizeof (*bigwad);
96 	kthread_t *t;
97 	klwp_t *lwp;
98 	user_t *up;
99 	int i;
100 	int nlwp;
101 	int error;
102 
103 	bigwad = kmem_alloc(bigsize, KM_SLEEP);
104 
105 	/*
106 	 * The order of the elfnote entries should be same here and in
107 	 * the gcore(1) command.  Synchronization is needed between the
108 	 * kernel and libproc's Pfgcore() function where the meat of
109 	 * the gcore(1) command lives.
110 	 */
111 
112 	mutex_enter(&p->p_lock);
113 	oprgetpsinfo(p, &bigwad->psinfo, NULL);
114 	mutex_exit(&p->p_lock);
115 	error = elfnote(vp, &offset, NT_PRPSINFO, sizeof (bigwad->psinfo),
116 	    (caddr_t)&bigwad->psinfo, rlimit, credp);
117 	if (error)
118 		goto done;
119 
120 	error = elfnote(vp, &offset, NT_PLATFORM, strlen(platform) + 1,
121 	    platform, rlimit, credp);
122 	if (error)
123 		goto done;
124 
125 	up = PTOU(p);
126 	for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
127 		bigwad->auxv[i].a_type = up->u_auxv[i].a_type;
128 		bigwad->auxv[i].a_un.a_val = up->u_auxv[i].a_un.a_val;
129 	}
130 	error = elfnote(vp, &offset, NT_AUXV, sizeof (bigwad->auxv),
131 	    (caddr_t)bigwad->auxv, rlimit, credp);
132 	if (error)
133 		goto done;
134 
135 	t = curthread;
136 	nlwp = p->p_lwpcnt;
137 	do {
138 		ASSERT(nlwp != 0);
139 		nlwp--;
140 		lwp = ttolwp(t);
141 
142 		mutex_enter(&p->p_lock);
143 		if (t == curthread) {
144 			uchar_t oldsig;
145 
146 			/*
147 			 * Modify t_whystop and lwp_cursig so it appears that
148 			 * the current LWP is stopped after faulting on the
149 			 * signal that caused the core dump.  As a result,
150 			 * oprgetstatus() will record that signal, the saved
151 			 * lwp_siginfo, and its signal handler in the core file
152 			 * status.  We restore lwp_cursig in case a subsequent
153 			 * signal was received while dumping core.
154 			 */
155 			oldsig = lwp->lwp_cursig;
156 			lwp->lwp_cursig = (uchar_t)sig;
157 			t->t_whystop = PR_FAULTED;
158 
159 			oprgetstatus(t, &bigwad->prstat, p->p_zone);
160 			bigwad->prstat.pr_why = 0;
161 
162 			t->t_whystop = 0;
163 			lwp->lwp_cursig = oldsig;
164 
165 		} else {
166 			oprgetstatus(t, &bigwad->prstat, p->p_zone);
167 		}
168 		mutex_exit(&p->p_lock);
169 		error = elfnote(vp, &offset, NT_PRSTATUS,
170 		    sizeof (bigwad->prstat), (caddr_t)&bigwad->prstat,
171 		    rlimit, credp);
172 		if (error)
173 			goto done;
174 
175 		if (prhasfp()) {
176 			prgetprfpregs(lwp, &bigwad->fpregs);
177 			error = elfnote(vp, &offset, NT_PRFPREG,
178 			    sizeof (bigwad->fpregs), (caddr_t)&bigwad->fpregs,
179 			    rlimit, credp);
180 			if (error)
181 				goto done;
182 		}
183 	} while ((t = t->t_forw) != curthread);
184 	ASSERT(nlwp == 0);
185 
186 done:
187 	kmem_free(bigwad, bigsize);
188 	return (error);
189 }
190