kvm_sparc.c revision 1.10 1 /* $NetBSD: kvm_sparc.c,v 1.10 1996/11/09 23:47:34 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 #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.10 1996/11/09 23:47:34 pk 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/core.h>
58 #include <sys/kcore.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 #include <machine/kcore.h>
67
68 #include <limits.h>
69 #include <db.h>
70
71 #include "kvm_private.h"
72
73
74 static int cputyp = -1;
75 static int pgshift;
76 static int nptesg; /* [sun4/sun4c] only */
77
78 #define VA_VPG(va) ((cputyp == CPU_SUN4C || cputyp == CPU_SUN4M) \
79 ? VA_SUN4C_VPG(va) \
80 : VA_SUN4_VPG(va))
81
82 #define VA_OFF(va) (va & (kd->nbpg - 1))
83
84
85 void
86 _kvm_freevtop(kd)
87 kvm_t *kd;
88 {
89 if (kd->vmst != 0) {
90 _kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
91 kd->vmst = 0;
92 }
93 }
94
95 /*
96 * Prepare for translation of kernel virtual addresses into offsets
97 * into crash dump files. We use the MMU specific goop written at the
98 * front of the crash dump by pmap_dumpmmu().
99 */
100 int
101 _kvm_initvtop(kd)
102 kvm_t *kd;
103 {
104 cpu_kcore_hdr_t *cpup = kd->cpu_data;
105
106 switch (cputyp = cpup->cputype) {
107 case CPU_SUN4:
108 kd->nbpg = 8196;
109 pgshift = 13;
110 break;
111 case CPU_SUN4C:
112 case CPU_SUN4M:
113 kd->nbpg = 4096;
114 pgshift = 12;
115 break;
116 default:
117 _kvm_err(kd, kd->program, "Unsupported CPU type");
118 return (-1);
119 }
120 nptesg = NBPSG / kd->nbpg;
121 return (0);
122 }
123
124 /*
125 * Translate a kernel virtual address to a physical address using the
126 * mapping information in kd->vm. Returns the result in pa, and returns
127 * the number of bytes that are contiguously available from this
128 * physical address. This routine is used only for crashdumps.
129 */
130 int
131 _kvm_kvatop(kd, va, pa)
132 kvm_t *kd;
133 u_long va;
134 u_long *pa;
135 {
136 if (cputyp == -1)
137 if (_kvm_initvtop(kd) != 0)
138 return (-1);
139
140 return ((cputyp == CPU_SUN4M)
141 ? _kvm_kvatop4m(kd, va, pa)
142 : _kvm_kvatop44c(kd, va, pa));
143 }
144
145 /*
146 * (note: sun4 3-level MMU not yet supported)
147 */
148 int
149 _kvm_kvatop44c(kd, va, pa)
150 kvm_t *kd;
151 u_long va;
152 u_long *pa;
153 {
154 register int vr, vs, pte;
155 cpu_kcore_hdr_t *cpup = kd->cpu_data;
156 struct regmap *rp;
157 struct segmap *sp;
158 int *ptes;
159
160 if (va < KERNBASE)
161 goto err;
162
163 /*
164 * Layout of CPU segment:
165 * cpu_kcore_hdr_t;
166 * [alignment]
167 * phys_ram_seg_t[cpup->nmemseg];
168 * ptes[cpup->npmegs];
169 */
170 ptes = (int *)((int)kd->cpu_data + cpup->pmegoffset);
171
172 vr = VA_VREG(va);
173 vs = VA_VSEG(va);
174
175 sp = &cpup->segmap_store[(vr-NUREG)*NSEGRG + vs];
176 if (sp->sg_npte == 0)
177 goto err;
178 if (sp->sg_pmeg == cpup->npmeg /* =seginval */)
179 goto err;
180 pte = ptes[sp->sg_pmeg * nptesg + VA_VPG(va)];
181 if ((pte & PG_V) != 0) {
182 register long p, off = VA_OFF(va);
183
184 p = (pte & PG_PFNUM) << pgshift;
185 *pa = p + off;
186 return (kd->nbpg - off);
187 }
188 err:
189 _kvm_err(kd, 0, "invalid address (%x)", va);
190 return (0);
191 }
192
193 int
194 _kvm_kvatop4m(kd, va, pa)
195 kvm_t *kd;
196 u_long va;
197 u_long *pa;
198 {
199 cpu_kcore_hdr_t *cpup = kd->cpu_data;
200 register int vr, vs;
201 int pte;
202 off_t foff;
203 struct regmap *rp;
204 struct segmap *sp;
205
206 if (va < KERNBASE)
207 goto err;
208
209 /*
210 * Layout of CPU segment:
211 * cpu_kcore_hdr_t;
212 * [alignment]
213 * phys_ram_seg_t[cpup->nmemseg];
214 */
215
216 vr = VA_VREG(va);
217 vs = VA_VSEG(va);
218
219 sp = &cpup->segmap_store[(vr-NUREG)*NSEGRG + vs];
220 if (sp->sg_npte == 0)
221 goto err;
222
223 /* XXX - assume page tables in initial kernel DATA or BSS. */
224 foff = _kvm_pa2off(kd, (u_long)&sp->sg_pte[VA_VPG(va)] - KERNBASE);
225 if (foff == (off_t)-1)
226 return (0);
227
228 if (lseek(kd->pmfd, foff, 0) == -1 ||
229 read(kd->pmfd, (void *)&pte, sizeof(pte)) < 0) {
230 _kvm_err(kd, kd->program, "cannot read pte for %x", va);
231 return (0);
232 }
233
234 if ((pte & SRMMU_TETYPE) == SRMMU_TEPTE) {
235 register long p, off = VA_OFF(va);
236
237 p = (pte & SRMMU_PPNMASK) << SRMMU_PPNPASHIFT;
238 *pa = p + off;
239 return (kd->nbpg - off);
240 }
241 err:
242 _kvm_err(kd, 0, "invalid address (%x)", va);
243 return (0);
244 }
245
246 /*
247 * Translate a physical address to a file-offset in the crash-dump.
248 */
249 off_t
250 _kvm_pa2off(kd, pa)
251 kvm_t *kd;
252 u_long pa;
253 {
254 cpu_kcore_hdr_t *cpup = kd->cpu_data;
255 phys_ram_seg_t *mp;
256 off_t off;
257 int nmem;
258
259 /*
260 * Layout of CPU segment:
261 * cpu_kcore_hdr_t;
262 * [alignment]
263 * phys_ram_seg_t[cpup->nmemseg];
264 */
265 mp = (phys_ram_seg_t *)((int)kd->cpu_data + cpup->memsegoffset);
266 off = 0;
267
268 /* Translate (sparse) pfnum to (packed) dump offset */
269 for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
270 if (mp->start <= pa && pa < mp->start + mp->size)
271 break;
272 off += mp->size;
273 }
274 if (nmem < 0) {
275 _kvm_err(kd, 0, "invalid address (%x)", pa);
276 return (-1);
277 }
278
279 return (kd->dump_off + off + pa - mp->start);
280 }
281