xref: /illumos-gate/usr/src/uts/common/exec/elf/old_notes.c (revision 69b1fd3f24d0ee2e682883606201c61f52085805)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/thread.h>
32 #include <sys/sysmacros.h>
33 #include <sys/signal.h>
34 #include <sys/cred.h>
35 #include <sys/user.h>
36 #include <sys/errno.h>
37 #include <sys/vnode.h>
38 #include <sys/mman.h>
39 #include <sys/kmem.h>
40 #include <sys/proc.h>
41 #include <sys/pathname.h>
42 #include <sys/cmn_err.h>
43 #include <sys/systm.h>
44 #include <sys/elf.h>
45 #include <sys/vmsystm.h>
46 #include <sys/debug.h>
47 #include <sys/old_procfs.h>
48 #include <sys/auxv.h>
49 #include <sys/exec.h>
50 #include <sys/prsystm.h>
51 #include <vm/as.h>
52 #include <vm/rm.h>
53 #include <sys/modctl.h>
54 #include <sys/systeminfo.h>
55 #include <sys/machelf.h>
56 #include <sys/zone.h>
57 #include "elf_impl.h"
58 
59 extern void oprgetstatus(kthread_t *, prstatus_t *, zone_t *);
60 extern void oprgetpsinfo(proc_t *, prpsinfo_t *, kthread_t *);
61 
62 void
63 setup_old_note_header(Phdr *v, proc_t *p)
64 {
65 	int nlwp = p->p_lwpcnt;
66 	size_t size;
67 
68 	v[0].p_type = PT_NOTE;
69 	v[0].p_flags = PF_R;
70 	v[0].p_filesz = (sizeof (Note) * (3 + nlwp))
71 	    + roundup(sizeof (prpsinfo_t), sizeof (Word))
72 	    + roundup(strlen(platform) + 1, sizeof (Word))
73 	    + roundup(__KERN_NAUXV_IMPL * sizeof (aux_entry_t),
74 	    sizeof (Word))
75 	    + nlwp * roundup(sizeof (prstatus_t), sizeof (Word));
76 	if (prhasfp())
77 		v[0].p_filesz += nlwp * sizeof (Note)
78 		    + nlwp*roundup(sizeof (prfpregset_t), sizeof (Word));
79 	if ((size = prhasx(p)? prgetprxregsize(p) : 0) != 0)
80 		v[0].p_filesz += nlwp * sizeof (Note)
81 		    + nlwp * roundup(size, sizeof (Word));
82 
83 #if defined(__sparc)
84 	/*
85 	 * Figure out the number and sizes of register windows.
86 	 */
87 	{
88 		kthread_t *t = p->p_tlist;
89 		do {
90 			if ((size = prnwindows(ttolwp(t))) != 0) {
91 				size = sizeof (gwindows_t) -
92 				    (SPARC_MAXREGWINDOW - size) *
93 				    sizeof (struct rwindow);
94 				v[0].p_filesz += sizeof (Note) +
95 				    roundup(size, sizeof (Word));
96 			}
97 		} while ((t = t->t_forw) != p->p_tlist);
98 	}
99 #endif /* __sparc */
100 }
101 
102 int
103 write_old_elfnotes(proc_t *p, int sig, vnode_t *vp, offset_t offset,
104     rlim64_t rlimit, cred_t *credp)
105 {
106 	union {
107 		prpsinfo_t	psinfo;
108 		prstatus_t	prstat;
109 		prfpregset_t	fpregs;
110 #if defined(__sparc)
111 		gwindows_t	gwindows;
112 #endif /* __sparc */
113 		char		xregs[1];
114 		aux_entry_t	auxv[__KERN_NAUXV_IMPL];
115 	} *bigwad;
116 	int xregsize = prhasx(p)? prgetprxregsize(p) : 0;
117 	size_t bigsize = MAX(sizeof (*bigwad), (size_t)xregsize);
118 	kthread_t *t;
119 	klwp_t *lwp;
120 	user_t *up;
121 	int i;
122 	int nlwp;
123 	int error;
124 
125 	bigwad = kmem_alloc(bigsize, KM_SLEEP);
126 
127 	/*
128 	 * The order of the elfnote entries should be same here and in
129 	 * the gcore(1) command.  Synchronization is needed between the
130 	 * kernel and libproc's Pfgcore() function where the meat of
131 	 * the gcore(1) command lives.
132 	 */
133 
134 	mutex_enter(&p->p_lock);
135 	oprgetpsinfo(p, &bigwad->psinfo, NULL);
136 	mutex_exit(&p->p_lock);
137 	error = elfnote(vp, &offset, NT_PRPSINFO, sizeof (bigwad->psinfo),
138 	    (caddr_t)&bigwad->psinfo, rlimit, credp);
139 	if (error)
140 		goto done;
141 
142 	error = elfnote(vp, &offset, NT_PLATFORM, strlen(platform) + 1,
143 	    platform, rlimit, credp);
144 	if (error)
145 		goto done;
146 
147 	up = PTOU(p);
148 	for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
149 		bigwad->auxv[i].a_type = up->u_auxv[i].a_type;
150 		bigwad->auxv[i].a_un.a_val = up->u_auxv[i].a_un.a_val;
151 	}
152 	error = elfnote(vp, &offset, NT_AUXV, sizeof (bigwad->auxv),
153 	    (caddr_t)bigwad->auxv, rlimit, credp);
154 	if (error)
155 		goto done;
156 
157 	t = curthread;
158 	nlwp = p->p_lwpcnt;
159 	do {
160 		ASSERT(nlwp != 0);
161 		nlwp--;
162 		lwp = ttolwp(t);
163 
164 		mutex_enter(&p->p_lock);
165 		if (t == curthread) {
166 			uchar_t oldsig;
167 
168 			/*
169 			 * Modify t_whystop and lwp_cursig so it appears that
170 			 * the current LWP is stopped after faulting on the
171 			 * signal that caused the core dump.  As a result,
172 			 * oprgetstatus() will record that signal, the saved
173 			 * lwp_siginfo, and its signal handler in the core file
174 			 * status.  We restore lwp_cursig in case a subsequent
175 			 * signal was received while dumping core.
176 			 */
177 			oldsig = lwp->lwp_cursig;
178 			lwp->lwp_cursig = (uchar_t)sig;
179 			t->t_whystop = PR_FAULTED;
180 
181 			oprgetstatus(t, &bigwad->prstat, p->p_zone);
182 			bigwad->prstat.pr_why = 0;
183 
184 			t->t_whystop = 0;
185 			lwp->lwp_cursig = oldsig;
186 
187 		} else {
188 			oprgetstatus(t, &bigwad->prstat, p->p_zone);
189 		}
190 		mutex_exit(&p->p_lock);
191 		error = elfnote(vp, &offset, NT_PRSTATUS,
192 		    sizeof (bigwad->prstat), (caddr_t)&bigwad->prstat,
193 		    rlimit, credp);
194 		if (error)
195 			goto done;
196 
197 		if (prhasfp()) {
198 			prgetprfpregs(lwp, &bigwad->fpregs);
199 			error = elfnote(vp, &offset, NT_PRFPREG,
200 			    sizeof (bigwad->fpregs), (caddr_t)&bigwad->fpregs,
201 			    rlimit, credp);
202 			if (error)
203 				goto done;
204 		}
205 
206 #if defined(__sparc)
207 		/*
208 		 * Unspilled SPARC register windows.
209 		 */
210 		{
211 			size_t size = prnwindows(lwp);
212 
213 			if (size != 0) {
214 				size = sizeof (gwindows_t) -
215 				    (SPARC_MAXREGWINDOW - size) *
216 				    sizeof (struct rwindow);
217 				prgetwindows(lwp, &bigwad->gwindows);
218 				error = elfnote(vp, &offset, NT_GWINDOWS,
219 				    size, (caddr_t)&bigwad->gwindows,
220 				    rlimit, credp);
221 				if (error)
222 					goto done;
223 			}
224 		}
225 #endif /* __sparc */
226 
227 		if (xregsize) {
228 			prgetprxregs(lwp, bigwad->xregs);
229 			error = elfnote(vp, &offset, NT_PRXREG,
230 			    xregsize, bigwad->xregs, rlimit, credp);
231 			if (error)
232 				goto done;
233 		}
234 	} while ((t = t->t_forw) != curthread);
235 	ASSERT(nlwp == 0);
236 
237 done:
238 	kmem_free(bigwad, bigsize);
239 	return (error);
240 }
241