buildworld - Fix breakage
[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  * $DragonFly: src/usr.bin/gcore/md-sparc.c,v 1.5 2005/04/10 20:55:38 drhodus Exp $
40  */
41
42 #include <sys/user.h>
43 #include <sys/param.h>
44 #include <sys/time.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/sysctl.h>
48 #include <machine/vmparam.h>
49
50 #include <err.h>
51 #include <kvm.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include "extern.h"
56
57 #ifndef offsetof
58 #define offsetof(s, f) ((int)&((s *)0)->f)
59 #endif
60
61 static void
62 shift_page(int fd, off_t off, int ssize)
63 {
64         char buffer[NBPG];
65
66         (void)lseek(fd, (off_t)-NBPG, SEEK_END);
67         for (; ssize > 0; ssize -= NBPG) {
68                 (void)read(fd, buffer, NBPG);
69                 (void)write(fd, buffer, NBPG);
70                 (void)lseek(fd, (off_t)-2 * NBPG, SEEK_CUR);
71         }
72 }
73
74 /*
75  * Fix up the core image for the sparc.  We need to flush any register
76  * windows that are cached in the pcb out to the user stack.
77  * Also, we need to get the trap frame and possible floating point state
78  * from the top of the kernel stack and store it in the pcb.
79  */
80 void
81 md_core(kvm_t *kd, int fd, struct kinfo_proc *ki)
82 {
83         struct rwindow *rw;
84         int nsaved, cc, ssize;
85         off_t off, s;
86         u_long sp;
87         struct pcb pcb;
88         struct trapframe tf;
89
90         /*
91          * Before anything else read the trapframe.  Synchronizing here
92          * is impossible if the process is running.
93          */
94         cc = kvm_read(kd, (u_long)ki->kp_proc.p_md.md_tf,
95                       /* XXX */
96                       (void *)&tf, sizeof(tf));
97         if (cc < 0)
98                 errx(1, "kvm_read: %s (reading kernel trapframe)",
99                       kvm_geterr(kd));
100         if (cc != sizeof(tf))
101                 errx(1, "cannot read kernel trapframe");
102
103         /*
104          * Write out the real trap frame.
105          */
106         off = offsetof(struct user, u_md);
107         off += offsetof(struct md_coredump, md_tf);
108         if (lseek(fd, off, SEEK_SET) == -1)
109                 err(1, "lseek");
110         (void)write(fd, &tf, sizeof(tf));
111
112         if (ki->kp_proc.p_md.md_fpstate != 0) {
113                 /*
114                  * If floating point state is present, write it out too.
115                  * It comes right after the trapframe so we don't need to seek.
116                  */
117                 struct fpstate fs;
118                 cc = kvm_read(kd, (u_long)ki->kp_proc.p_md.md_fpstate,
119                               (void *)&fs, sizeof(fs));
120                 if (cc < 0)
121                         errx(1, "kvm_read: %s (fpu state)", kvm_geterr(kd));
122                 if (cc != sizeof(fs))
123                         errx(1, "cannot read fpu state");
124                 (void)write(fd, (char *)&fs, sizeof(fs));
125         }
126         /*
127          * Read pcb.
128          */
129         if (lseek(fd, (off_t)offsetof(struct user, u_pcb), SEEK_SET) == -1)
130                 err(1, "lseek");
131         cc = read(fd, (char *)&pcb, sizeof(pcb));
132         if (cc != sizeof(pcb)) {
133                 if (cc < 0)
134                         err(1, "read");
135                 errx(1, "couldn't read pcb from core file");
136         }
137
138         /*
139          * Write any unsaved windows to the appropriate stack locations.
140          */
141         nsaved = pcb.pcb_nsaved;
142         if (nsaved == 0)
143                 return;
144
145         rw = &pcb.pcb_rw[0];
146         off = ctob(UPAGES + ki->kp_eproc.e_vm.vm_dsize);
147         ssize = ctob(ki->kp_eproc.e_vm.vm_ssize);
148         sp = tf.tf_out[6];
149         for (; --nsaved >= 0; ++rw) {
150                 /*
151                  * Copy register window into appropriate stack location.
152                  */
153                 s = ssize - (USRSTACK - sp);
154                 if (s < 0) {
155                         if (s < -NBPG)
156                                 errx(1, "cannot copy pcb windows to stack");
157                         /*
158                          * It's possible to be missing the bottomost
159                          * page because a stack page hasn't been allocated
160                          * for the register save area.  Shift over
161                          * the stack segment by a page, and update
162                          * the u-area to reflect the new stack size.  YECH!
163                          */
164                         shift_page(fd, off, ssize);
165                         ssize += NBPG;
166                         s += NBPG;
167                         ++ki->kp_eproc.e_vm.vm_ssize;
168                         (void)lseek(fd,
169                             (off_t)offsetof(struct user, u_kproc.kp_eproc.e_vm),
170                             SEEK_SET);
171                         (void)write(fd,
172                             &ki->kp_eproc.e_vm, sizeof(ki->kp_eproc.e_vm));
173                 }
174                 if (lseek(fd, off + s, SEEK_SET) == -1)
175                         errx(1, "cannot copy pcb windows to stack");
176
177                 (void)write(fd, rw, sizeof(*rw));
178                 sp = rw->rw_in[6];
179         }
180 }