Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / gcore / md-sparc.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * @(#)md-sparc.c       8.1 (Berkeley) 6/6/93
38  */
39
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/file.h>
43 #include <sys/stat.h>
44 #include <sys/proc.h>
45 #include <sys/user.h>
46 #include <sys/sysctl.h>
47 #include <machine/vmparam.h>
48
49 #include <err.h>
50 #include <kvm.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include "extern.h"
55
56 #ifndef offsetof
57 #define offsetof(s, f) ((int)&((s *)0)->f)
58 #endif
59
60 static void
61 shift_page(fd, off, ssize)
62         register int fd;
63         register off_t off;
64         register int ssize;
65 {
66         char buffer[NBPG];
67
68         (void)lseek(fd, (off_t)-NBPG, SEEK_END);
69         for (; ssize > 0; ssize -= NBPG) {
70                 (void)read(fd, buffer, NBPG);
71                 (void)write(fd, buffer, NBPG);
72                 (void)lseek(fd, (off_t)-2 * NBPG, SEEK_CUR);
73         }
74 }
75
76 /*
77  * Fix up the core image for the sparc.  We need to flush any register
78  * windows that are cached in the pcb out to the user stack.
79  * Also, we need to get the trap frame and possible floating point state
80  * from the top of the kernel stack and store it in the pcb.
81  */
82 void
83 md_core(kd, fd, ki)
84         kvm_t *kd;
85         int fd;
86         struct kinfo_proc *ki;
87 {
88         register struct rwindow *rw;
89         register int nsaved, cc, ssize;
90         register off_t off, s;
91         register u_long sp;
92         struct pcb pcb;
93         struct trapframe tf;
94
95         /*
96          * Before anything else read the trapframe.  Synchronizing here
97          * is impossible if the process is running.
98          */
99         cc = kvm_read(kd, (u_long)ki->kp_proc.p_md.md_tf,
100                       /* XXX */
101                       (void *)&tf, sizeof(tf));
102         if (cc < 0)
103                 errx(1, "kvm_read: %s (reading kernel trapframe)",
104                       kvm_geterr(kd));
105         if (cc != sizeof(tf))
106                 errx(1, "cannot read kernel trapframe");
107
108         /*
109          * Write out the real trap frame.
110          */
111         off = offsetof(struct user, u_md);
112         off += offsetof(struct md_coredump, md_tf);
113         if (lseek(fd, off, SEEK_SET) == -1)
114                 err(1, "lseek");
115         (void)write(fd, &tf, sizeof(tf));
116
117         if (ki->kp_proc.p_md.md_fpstate != 0) {
118                 /*
119                  * If floating point state is present, write it out too.
120                  * It comes right after the trapframe so we don't need to seek.
121                  */
122                 struct fpstate fs;
123                 cc = kvm_read(kd, (u_long)ki->kp_proc.p_md.md_fpstate,
124                               (void *)&fs, sizeof(fs));
125                 if (cc < 0)
126                         errx(1, "kvm_read: %s (fpu state)", kvm_geterr(kd));
127                 if (cc != sizeof(fs))
128                         errx(1, "cannot read fpu state");
129                 (void)write(fd, (char *)&fs, sizeof(fs));
130         }
131         /*
132          * Read pcb.
133          */
134         if (lseek(fd, (off_t)offsetof(struct user, u_pcb), SEEK_SET) == -1)
135                 err(1, "lseek");
136         cc = read(fd, (char *)&pcb, sizeof(pcb));
137         if (cc != sizeof(pcb)) {
138                 if (cc < 0)
139                         err(1, "read");
140                 errx(1, "couldn't read pcb from core file");
141         }
142
143         /*
144          * Write any unsaved windows to the appropriate stack locations.
145          */
146         nsaved = pcb.pcb_nsaved;
147         if (nsaved == 0)
148                 return;
149
150         rw = &pcb.pcb_rw[0];
151         off = ctob(UPAGES + ki->kp_eproc.e_vm.vm_dsize);
152         ssize = ctob(ki->kp_eproc.e_vm.vm_ssize);
153         sp = tf.tf_out[6];
154         for (; --nsaved >= 0; ++rw) {
155                 /*
156                  * Copy register window into appropriate stack location.
157                  */
158                 s = ssize - (USRSTACK - sp);
159                 if (s < 0) {
160                         if (s < -NBPG)
161                                 errx(1, "cannot copy pcb windows to stack");
162                         /*
163                          * It's possible to be missing the bottomost
164                          * page because a stack page hasn't been allocated
165                          * for the register save area.  Shift over
166                          * the stack segment by a page, and update
167                          * the u-area to reflect the new stack size.  YECH!
168                          */
169                         shift_page(fd, off, ssize);
170                         ssize += NBPG;
171                         s += NBPG;
172                         ++ki->kp_eproc.e_vm.vm_ssize;
173                         (void)lseek(fd,
174                             (off_t)offsetof(struct user, u_kproc.kp_eproc.e_vm),
175                             SEEK_SET);
176                         (void)write(fd,
177                             &ki->kp_eproc.e_vm, sizeof(ki->kp_eproc.e_vm));
178                 }
179                 if (lseek(fd, off + s, SEEK_SET) == -1)
180                         errx(1, "cannot copy pcb windows to stack");
181
182                 (void)write(fd, rw, sizeof(*rw));
183                 sp = rw->rw_in[6];
184         }
185 }