kvm_sparc.c revision 1.8 1 /* $NetBSD: kvm_sparc.c,v 1.8 1996/03/18 22:34:02 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software developed by the Computer Systems
8 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9 * BG 91-66 and contributed to Berkeley.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93";
43 #else
44 static char *rcsid = "$NetBSD: kvm_sparc.c,v 1.8 1996/03/18 22:34:02 thorpej Exp $";
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 /*
49 * Sparc machine dependent routines for kvm. Hopefully, the forthcoming
50 * vm code will one day obsolete this module.
51 */
52
53 #include <sys/param.h>
54 #include <sys/user.h>
55 #include <sys/proc.h>
56 #include <sys/stat.h>
57 #include <sys/sysctl.h>
58 #include <sys/device.h>
59 #include <unistd.h>
60 #include <nlist.h>
61 #include <kvm.h>
62
63 #include <vm/vm.h>
64 #include <vm/vm_param.h>
65 #include <machine/autoconf.h>
66
67 #include <limits.h>
68 #include <db.h>
69
70 #include "kvm_private.h"
71
72 #define MA_SIZE 32 /* XXX */
73 struct vmstate {
74 struct {
75 int x_seginval;
76 int x_npmemarr;
77 struct memarr x_pmemarr[MA_SIZE];
78 struct segmap x_segmap_store[NKREG*NSEGRG];
79 } x;
80 #define seginval x.x_seginval
81 #define npmemarr x.x_npmemarr
82 #define pmemarr x.x_pmemarr
83 #define segmap_store x.x_segmap_store
84 int *pte;
85 };
86 #define NPMEG(vm) ((vm)->seginval+1)
87
88 static int cputyp = -1;
89
90 static int pgshift, nptesg;
91
92 #define VA_VPG(va) (cputyp==CPU_SUN4C ? VA_SUN4C_VPG(va) : VA_SUN4_VPG(va))
93
94 static void
95 _kvm_mustinit(kd)
96 kvm_t *kd;
97 {
98 if (cputyp != -1)
99 return;
100 for (pgshift = 12; (1 << pgshift) != kd->nbpg; pgshift++)
101 ;
102 nptesg = NBPSG / kd->nbpg;
103
104 #if 1
105 if (cputyp == -1) {
106 if (kd->nbpg == 8192)
107 cputyp = CPU_SUN4;
108 else
109 cputyp = CPU_SUN4C;
110 }
111 #endif
112 }
113
114 void
115 _kvm_freevtop(kd)
116 kvm_t *kd;
117 {
118 if (kd->vmst != 0) {
119 if (kd->vmst->pte != 0)
120 free(kd->vmst->pte);
121 free(kd->vmst);
122 kd->vmst = 0;
123 }
124 }
125
126 /*
127 * Prepare for translation of kernel virtual addresses into offsets
128 * into crash dump files. We use the MMU specific goop written at the
129 * and of crash dump by pmap_dumpmmu().
130 * (note: sun4/sun4c 2-level MMU specific)
131 */
132 int
133 _kvm_initvtop(kd)
134 kvm_t *kd;
135 {
136 register int i;
137 register int off;
138 register struct vmstate *vm;
139 struct stat st;
140 struct nlist nlist[5];
141
142 _kvm_mustinit(kd);
143
144 if ((vm = kd->vmst) == 0) {
145 kd->vmst = vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
146 if (vm == 0)
147 return (-1);
148 }
149
150 if (fstat(kd->pmfd, &st) < 0)
151 return (-1);
152 /*
153 * Read segment table.
154 */
155
156 off = st.st_size - roundup(sizeof(vm->x), kd->nbpg);
157 errno = 0;
158 if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
159 read(kd->pmfd, (char *)&vm->x, sizeof(vm->x)) < 0) {
160 _kvm_err(kd, kd->program, "cannot read segment map");
161 return (-1);
162 }
163
164 vm->pte = (int *)_kvm_malloc(kd, NPMEG(vm) * nptesg * sizeof(int));
165 if (vm->pte == 0) {
166 free(kd->vmst);
167 kd->vmst = 0;
168 return (-1);
169 }
170
171 /*
172 * Read PMEGs.
173 */
174 off = st.st_size - roundup(sizeof(vm->x), kd->nbpg) -
175 roundup(NPMEG(vm) * nptesg * sizeof(int), kd->nbpg);
176
177 errno = 0;
178 if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
179 read(kd->pmfd, (char *)vm->pte, NPMEG(vm) * nptesg * sizeof(int)) < 0) {
180 _kvm_err(kd, kd->program, "cannot read PMEG table");
181 return (-1);
182 }
183
184 return (0);
185 }
186
187 #define VA_OFF(va) (va & (kd->nbpg - 1))
188
189 /*
190 * Translate a kernel virtual address to a physical address using the
191 * mapping information in kd->vm. Returns the result in pa, and returns
192 * the number of bytes that are contiguously available from this
193 * physical address. This routine is used only for crashdumps.
194 */
195 int
196 _kvm_kvatop(kd, va, pa)
197 kvm_t *kd;
198 u_long va;
199 u_long *pa;
200 {
201 register int vr, vs, pte, off, nmem;
202 register struct vmstate *vm = kd->vmst;
203 struct regmap *rp;
204 struct segmap *sp;
205 struct memarr *mp;
206
207 _kvm_mustinit(kd);
208
209 if (va < KERNBASE)
210 goto err;
211
212 vr = VA_VREG(va);
213 vs = VA_VSEG(va);
214
215 sp = &vm->segmap_store[(vr-NUREG)*NSEGRG + vs];
216 if (sp->sg_npte == 0)
217 goto err;
218 if (sp->sg_pmeg == vm->seginval)
219 goto err;
220 pte = vm->pte[sp->sg_pmeg * nptesg + VA_VPG(va)];
221 if ((pte & PG_V) != 0) {
222 register long p, dumpoff = 0;
223
224 off = VA_OFF(va);
225 p = (pte & PG_PFNUM) << pgshift;
226 /* Translate (sparse) pfnum to (packed) dump offset */
227 for (mp = vm->pmemarr, nmem = vm->npmemarr; --nmem >= 0; mp++) {
228 if (mp->addr <= p && p < mp->addr + mp->len)
229 break;
230 dumpoff += mp->len;
231 }
232 if (nmem < 0)
233 goto err;
234 *pa = (dumpoff + p - mp->addr) | off;
235 return (kd->nbpg - off);
236 }
237 err:
238 _kvm_err(kd, 0, "invalid address (%x)", va);
239 return (0);
240 }
241
242 #if 0
243 static int
244 getcputyp()
245 {
246 int mib[2];
247 size_t size;
248
249 mib[0] = CTL_HW;
250 mib[1] = HW_CLASS;
251 size = sizeof cputyp;
252 if (sysctl(mib, 2, &cputyp, &size, NULL, 0) == -1)
253 return (-1);
254 }
255 #endif
256