dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / uts / common / exec / elf / old_notes.c
blob59e71b45cfdf92d6fe847ab53bde077916db1a0a
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
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"
59 extern void oprgetstatus(kthread_t *, prstatus_t *, zone_t *);
60 extern void oprgetpsinfo(proc_t *, prpsinfo_t *, kthread_t *);
62 void
63 setup_old_note_header(Phdr *v, proc_t *p)
65 int nlwp = p->p_lwpcnt;
66 size_t size;
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));
85 int
86 write_old_elfnotes(proc_t *p, int sig, vnode_t *vp, offset_t offset,
87 rlim64_t rlimit, cred_t *credp)
89 union {
90 prpsinfo_t psinfo;
91 prstatus_t prstat;
92 prfpregset_t fpregs;
93 char xregs[1];
94 aux_entry_t auxv[__KERN_NAUXV_IMPL];
95 } *bigwad;
96 int xregsize = prhasx(p)? prgetprxregsize(p) : 0;
97 size_t bigsize = MAX(sizeof (*bigwad), (size_t)xregsize);
98 kthread_t *t;
99 klwp_t *lwp;
100 user_t *up;
101 int i;
102 int nlwp;
103 int error;
105 bigwad = kmem_alloc(bigsize, KM_SLEEP);
108 * The order of the elfnote entries should be same here and in
109 * the gcore(1) command. Synchronization is needed between the
110 * kernel and libproc's Pfgcore() function where the meat of
111 * the gcore(1) command lives.
114 mutex_enter(&p->p_lock);
115 oprgetpsinfo(p, &bigwad->psinfo, NULL);
116 mutex_exit(&p->p_lock);
117 error = elfnote(vp, &offset, NT_PRPSINFO, sizeof (bigwad->psinfo),
118 (caddr_t)&bigwad->psinfo, rlimit, credp);
119 if (error)
120 goto done;
122 error = elfnote(vp, &offset, NT_PLATFORM, strlen(platform) + 1,
123 platform, rlimit, credp);
124 if (error)
125 goto done;
127 up = PTOU(p);
128 for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
129 bigwad->auxv[i].a_type = up->u_auxv[i].a_type;
130 bigwad->auxv[i].a_un.a_val = up->u_auxv[i].a_un.a_val;
132 error = elfnote(vp, &offset, NT_AUXV, sizeof (bigwad->auxv),
133 (caddr_t)bigwad->auxv, rlimit, credp);
134 if (error)
135 goto done;
137 t = curthread;
138 nlwp = p->p_lwpcnt;
139 do {
140 ASSERT(nlwp != 0);
141 nlwp--;
142 lwp = ttolwp(t);
144 mutex_enter(&p->p_lock);
145 if (t == curthread) {
146 uchar_t oldsig;
149 * Modify t_whystop and lwp_cursig so it appears that
150 * the current LWP is stopped after faulting on the
151 * signal that caused the core dump. As a result,
152 * oprgetstatus() will record that signal, the saved
153 * lwp_siginfo, and its signal handler in the core file
154 * status. We restore lwp_cursig in case a subsequent
155 * signal was received while dumping core.
157 oldsig = lwp->lwp_cursig;
158 lwp->lwp_cursig = (uchar_t)sig;
159 t->t_whystop = PR_FAULTED;
161 oprgetstatus(t, &bigwad->prstat, p->p_zone);
162 bigwad->prstat.pr_why = 0;
164 t->t_whystop = 0;
165 lwp->lwp_cursig = oldsig;
167 } else {
168 oprgetstatus(t, &bigwad->prstat, p->p_zone);
170 mutex_exit(&p->p_lock);
171 error = elfnote(vp, &offset, NT_PRSTATUS,
172 sizeof (bigwad->prstat), (caddr_t)&bigwad->prstat,
173 rlimit, credp);
174 if (error)
175 goto done;
177 if (prhasfp()) {
178 prgetprfpregs(lwp, &bigwad->fpregs);
179 error = elfnote(vp, &offset, NT_PRFPREG,
180 sizeof (bigwad->fpregs), (caddr_t)&bigwad->fpregs,
181 rlimit, credp);
182 if (error)
183 goto done;
187 if (xregsize) {
188 prgetprxregs(lwp, bigwad->xregs);
189 error = elfnote(vp, &offset, NT_PRXREG,
190 xregsize, bigwad->xregs, rlimit, credp);
191 if (error)
192 goto done;
194 } while ((t = t->t_forw) != curthread);
195 ASSERT(nlwp == 0);
197 done:
198 kmem_free(bigwad, bigsize);
199 return (error);