kvm_sparc.c revision 1.14 1 /* $NetBSD: kvm_sparc.c,v 1.14 1997/09/20 18:26:20 pk 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 #include <sys/cdefs.h>
41 #if defined(LIBC_SCCS) && !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93";
44 #else
45 __RCSID("$NetBSD: kvm_sparc.c,v 1.14 1997/09/20 18:26:20 pk Exp $");
46 #endif
47 #endif /* LIBC_SCCS and not lint */
48
49 /*
50 * Sparc machine dependent routines for kvm. Hopefully, the forthcoming
51 * vm code will one day obsolete this module.
52 */
53
54 #include <sys/param.h>
55 #include <sys/exec.h>
56 #include <sys/user.h>
57 #include <sys/proc.h>
58 #include <sys/stat.h>
59 #include <sys/core.h>
60 #include <sys/kcore.h>
61 #include <unistd.h>
62 #include <nlist.h>
63 #include <kvm.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_param.h>
67 #include <machine/autoconf.h>
68 #include <machine/kcore.h>
69
70 #include <limits.h>
71 #include <db.h>
72
73 #include "kvm_private.h"
74
75
76 static int cputyp = -1;
77 static int pgshift;
78 static int nptesg; /* [sun4/sun4c] only */
79
80 #define VA_VPG(va) ((cputyp == CPU_SUN4C || cputyp == CPU_SUN4M) \
81 ? VA_SUN4C_VPG(va) \
82 : VA_SUN4_VPG(va))
83
84 #define VA_OFF(va) (va & (kd->nbpg - 1))
85
86
87 void
88 _kvm_freevtop(kd)
89 kvm_t *kd;
90 {
91 if (kd->vmst != 0) {
92 _kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
93 kd->vmst = 0;
94 }
95 }
96
97 /*
98 * Prepare for translation of kernel virtual addresses into offsets
99 * into crash dump files. We use the MMU specific goop written at the
100 * front of the crash dump by pmap_dumpmmu().
101 */
102 int
103 _kvm_initvtop(kd)
104 kvm_t *kd;
105 {
106 cpu_kcore_hdr_t *cpup = kd->cpu_data;
107
108 switch (cputyp = cpup->cputype) {
109 case CPU_SUN4:
110 kd->nbpg = 8196;
111 pgshift = 13;
112 break;
113 case CPU_SUN4C:
114 case CPU_SUN4M:
115 kd->nbpg = 4096;
116 pgshift = 12;
117 break;
118 default:
119 _kvm_err(kd, kd->program, "Unsupported CPU type");
120 return (-1);
121 }
122 nptesg = NBPSG / kd->nbpg;
123 return (0);
124 }
125
126 /*
127 * Translate a kernel virtual address to a physical address using the
128 * mapping information in kd->vm. Returns the result in pa, and returns
129 * the number of bytes that are contiguously available from this
130 * physical address. This routine is used only for crashdumps.
131 */
132 int
133 _kvm_kvatop(kd, va, pa)
134 kvm_t *kd;
135 u_long va;
136 u_long *pa;
137 {
138 if (cputyp == -1)
139 if (_kvm_initvtop(kd) != 0)
140 return (-1);
141
142 return ((cputyp == CPU_SUN4M)
143 ? _kvm_kvatop4m(kd, va, pa)
144 : _kvm_kvatop44c(kd, va, pa));
145 }
146
147 /*
148 * (note: sun4 3-level MMU not yet supported)
149 */
150 int
151 _kvm_kvatop44c(kd, va, pa)
152 kvm_t *kd;
153 u_long va;
154 u_long *pa;
155 {
156 register int vr, vs, pte;
157 cpu_kcore_hdr_t *cpup = kd->cpu_data;
158 struct regmap *rp;
159 struct segmap *sp, *segmaps;
160 int *ptes;
161 int nkreg, nureg;
162 u_long kernbase = cpup->kernbase;
163
164 if (va < kernbase)
165 goto err;
166
167 /*
168 * Layout of CPU segment:
169 * cpu_kcore_hdr_t;
170 * [alignment]
171 * phys_ram_seg_t[cpup->nmemseg];
172 * segmap[cpup->nsegmap];
173 * ptes[cpup->npmegs];
174 */
175 segmaps = (struct segmap *)((long)kd->cpu_data + cpup->segmapoffset);
176 ptes = (int *)((int)kd->cpu_data + cpup->pmegoffset);
177 nkreg = ((int)((-(unsigned)kernbase) / NBPRG));
178 nureg = 256 - nkreg;
179
180 vr = VA_VREG(va);
181 vs = VA_VSEG(va);
182
183 sp = &segmaps[(vr-nureg)*NSEGRG + vs];
184 if (sp->sg_npte == 0)
185 goto err;
186 if (sp->sg_pmeg == cpup->npmeg - 1) /* =seginval */
187 goto err;
188 pte = ptes[sp->sg_pmeg * nptesg + VA_VPG(va)];
189 if ((pte & PG_V) != 0) {
190 register long p, off = VA_OFF(va);
191
192 p = (pte & PG_PFNUM) << pgshift;
193 *pa = p + off;
194 return (kd->nbpg - off);
195 }
196 err:
197 _kvm_err(kd, 0, "invalid address (%x)", va);
198 return (0);
199 }
200
201 int
202 _kvm_kvatop4m(kd, va, pa)
203 kvm_t *kd;
204 u_long va;
205 u_long *pa;
206 {
207 cpu_kcore_hdr_t *cpup = kd->cpu_data;
208 register int vr, vs;
209 int pte;
210 off_t foff;
211 struct regmap *rp;
212 struct segmap *sp, *segmaps;
213 int nkreg, nureg;
214 u_long kernbase = cpup->kernbase;
215
216 if (va < kernbase)
217 goto err;
218
219 /*
220 * Layout of CPU segment:
221 * cpu_kcore_hdr_t;
222 * [alignment]
223 * phys_ram_seg_t[cpup->nmemseg];
224 * segmap[cpup->nsegmap];
225 */
226 segmaps = (struct segmap *)((long)kd->cpu_data + cpup->segmapoffset);
227 nkreg = ((int)((-(unsigned)kernbase) / NBPRG));
228 nureg = 256 - nkreg;
229
230 vr = VA_VREG(va);
231 vs = VA_VSEG(va);
232
233 sp = &segmaps[(vr-nureg)*NSEGRG + vs];
234 if (sp->sg_npte == 0)
235 goto err;
236
237 /* XXX - assume page tables in initial kernel DATA or BSS. */
238 foff = _kvm_pa2off(kd, (u_long)&sp->sg_pte[VA_VPG(va)] - kernbase);
239 if (foff == (off_t)-1)
240 return (0);
241
242 if (lseek(kd->pmfd, foff, 0) == -1 ||
243 read(kd->pmfd, (void *)&pte, sizeof(pte)) < 0) {
244 _kvm_err(kd, kd->program, "cannot read pte for %x", va);
245 return (0);
246 }
247
248 if ((pte & SRMMU_TETYPE) == SRMMU_TEPTE) {
249 register long p, off = VA_OFF(va);
250
251 p = (pte & SRMMU_PPNMASK) << SRMMU_PPNPASHIFT;
252 *pa = p + off;
253 return (kd->nbpg - off);
254 }
255 err:
256 _kvm_err(kd, 0, "invalid address (%x)", va);
257 return (0);
258 }
259
260 /*
261 * Translate a physical address to a file-offset in the crash-dump.
262 */
263 off_t
264 _kvm_pa2off(kd, pa)
265 kvm_t *kd;
266 u_long pa;
267 {
268 cpu_kcore_hdr_t *cpup = kd->cpu_data;
269 phys_ram_seg_t *mp;
270 off_t off;
271 int nmem;
272
273 /*
274 * Layout of CPU segment:
275 * cpu_kcore_hdr_t;
276 * [alignment]
277 * phys_ram_seg_t[cpup->nmemseg];
278 */
279 mp = (phys_ram_seg_t *)((int)kd->cpu_data + cpup->memsegoffset);
280 off = 0;
281
282 /* Translate (sparse) pfnum to (packed) dump offset */
283 for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
284 if (mp->start <= pa && pa < mp->start + mp->size)
285 break;
286 off += mp->size;
287 }
288 if (nmem < 0) {
289 _kvm_err(kd, 0, "invalid address (%x)", pa);
290 return (-1);
291 }
292
293 return (kd->dump_off + off + pa - mp->start);
294 }
295
296 /*
297 * Machine-dependent initialization for ALL open kvm descriptors,
298 * not just those for a kernel crash dump. Some architectures
299 * have to deal with these NOT being constants! (i.e. m68k)
300 */
301 int
302 _kvm_mdopen(kd)
303 kvm_t *kd;
304 {
305 u_long max_uva;
306 extern struct ps_strings *__ps_strings;
307
308 max_uva = (u_long) (__ps_strings + 1);
309 kd->usrstack = max_uva;
310 kd->max_uva = max_uva;
311 kd->min_uva = 0;
312
313 return (0);
314 }
315