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