kvm_sparc.c revision 1.13 1 1.13 mikel /* $NetBSD: kvm_sparc.c,v 1.13 1997/08/15 02:22:03 mikel Exp $ */
2 1.8 thorpej
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1992, 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software developed by the Computer Systems
8 1.1 cgd * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9 1.1 cgd * BG 91-66 and contributed to Berkeley.
10 1.1 cgd *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.1 cgd * 3. All advertising materials mentioning features or use of this software
20 1.1 cgd * must display the following acknowledgement:
21 1.1 cgd * This product includes software developed by the University of
22 1.1 cgd * California, Berkeley and its contributors.
23 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
24 1.1 cgd * may be used to endorse or promote products derived from this software
25 1.1 cgd * without specific prior written permission.
26 1.1 cgd *
27 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 cgd * SUCH DAMAGE.
38 1.1 cgd */
39 1.1 cgd
40 1.13 mikel #include <sys/cdefs.h>
41 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
42 1.8 thorpej #if 0
43 1.1 cgd static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93";
44 1.8 thorpej #else
45 1.13 mikel __RCSID("$NetBSD: kvm_sparc.c,v 1.13 1997/08/15 02:22:03 mikel Exp $");
46 1.8 thorpej #endif
47 1.1 cgd #endif /* LIBC_SCCS and not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Sparc machine dependent routines for kvm. Hopefully, the forthcoming
51 1.1 cgd * vm code will one day obsolete this module.
52 1.1 cgd */
53 1.1 cgd
54 1.1 cgd #include <sys/param.h>
55 1.1 cgd #include <sys/user.h>
56 1.1 cgd #include <sys/proc.h>
57 1.1 cgd #include <sys/stat.h>
58 1.10 pk #include <sys/core.h>
59 1.10 pk #include <sys/kcore.h>
60 1.1 cgd #include <unistd.h>
61 1.1 cgd #include <nlist.h>
62 1.1 cgd #include <kvm.h>
63 1.1 cgd
64 1.1 cgd #include <vm/vm.h>
65 1.1 cgd #include <vm/vm_param.h>
66 1.7 pk #include <machine/autoconf.h>
67 1.10 pk #include <machine/kcore.h>
68 1.1 cgd
69 1.1 cgd #include <limits.h>
70 1.1 cgd #include <db.h>
71 1.1 cgd
72 1.1 cgd #include "kvm_private.h"
73 1.1 cgd
74 1.1 cgd
75 1.4 deraadt static int cputyp = -1;
76 1.10 pk static int pgshift;
77 1.10 pk static int nptesg; /* [sun4/sun4c] only */
78 1.3 deraadt
79 1.9 cgd #define VA_VPG(va) ((cputyp == CPU_SUN4C || cputyp == CPU_SUN4M) \
80 1.9 cgd ? VA_SUN4C_VPG(va) \
81 1.9 cgd : VA_SUN4_VPG(va))
82 1.4 deraadt
83 1.10 pk #define VA_OFF(va) (va & (kd->nbpg - 1))
84 1.10 pk
85 1.9 cgd
86 1.10 pk void
87 1.10 pk _kvm_freevtop(kd)
88 1.10 pk kvm_t *kd;
89 1.9 cgd {
90 1.10 pk if (kd->vmst != 0) {
91 1.10 pk _kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
92 1.10 pk kd->vmst = 0;
93 1.10 pk }
94 1.9 cgd }
95 1.9 cgd
96 1.10 pk /*
97 1.10 pk * Prepare for translation of kernel virtual addresses into offsets
98 1.10 pk * into crash dump files. We use the MMU specific goop written at the
99 1.10 pk * front of the crash dump by pmap_dumpmmu().
100 1.10 pk */
101 1.10 pk int
102 1.10 pk _kvm_initvtop(kd)
103 1.3 deraadt kvm_t *kd;
104 1.3 deraadt {
105 1.10 pk cpu_kcore_hdr_t *cpup = kd->cpu_data;
106 1.9 cgd
107 1.10 pk switch (cputyp = cpup->cputype) {
108 1.10 pk case CPU_SUN4:
109 1.10 pk kd->nbpg = 8196;
110 1.10 pk pgshift = 13;
111 1.10 pk break;
112 1.10 pk case CPU_SUN4C:
113 1.10 pk case CPU_SUN4M:
114 1.10 pk kd->nbpg = 4096;
115 1.10 pk pgshift = 12;
116 1.10 pk break;
117 1.10 pk default:
118 1.10 pk _kvm_err(kd, kd->program, "Unsupported CPU type");
119 1.9 cgd return (-1);
120 1.9 cgd }
121 1.10 pk nptesg = NBPSG / kd->nbpg;
122 1.9 cgd return (0);
123 1.3 deraadt }
124 1.3 deraadt
125 1.7 pk /*
126 1.9 cgd * Translate a kernel virtual address to a physical address using the
127 1.9 cgd * mapping information in kd->vm. Returns the result in pa, and returns
128 1.9 cgd * the number of bytes that are contiguously available from this
129 1.9 cgd * physical address. This routine is used only for crashdumps.
130 1.9 cgd */
131 1.9 cgd int
132 1.9 cgd _kvm_kvatop(kd, va, pa)
133 1.9 cgd kvm_t *kd;
134 1.9 cgd u_long va;
135 1.9 cgd u_long *pa;
136 1.9 cgd {
137 1.10 pk if (cputyp == -1)
138 1.10 pk if (_kvm_initvtop(kd) != 0)
139 1.10 pk return (-1);
140 1.9 cgd
141 1.9 cgd return ((cputyp == CPU_SUN4M)
142 1.9 cgd ? _kvm_kvatop4m(kd, va, pa)
143 1.9 cgd : _kvm_kvatop44c(kd, va, pa));
144 1.9 cgd }
145 1.9 cgd
146 1.9 cgd /*
147 1.9 cgd * (note: sun4 3-level MMU not yet supported)
148 1.9 cgd */
149 1.9 cgd int
150 1.9 cgd _kvm_kvatop44c(kd, va, pa)
151 1.1 cgd kvm_t *kd;
152 1.1 cgd u_long va;
153 1.1 cgd u_long *pa;
154 1.1 cgd {
155 1.10 pk register int vr, vs, pte;
156 1.10 pk cpu_kcore_hdr_t *cpup = kd->cpu_data;
157 1.7 pk struct regmap *rp;
158 1.7 pk struct segmap *sp;
159 1.10 pk int *ptes;
160 1.1 cgd
161 1.7 pk if (va < KERNBASE)
162 1.7 pk goto err;
163 1.7 pk
164 1.10 pk /*
165 1.10 pk * Layout of CPU segment:
166 1.10 pk * cpu_kcore_hdr_t;
167 1.10 pk * [alignment]
168 1.10 pk * phys_ram_seg_t[cpup->nmemseg];
169 1.10 pk * ptes[cpup->npmegs];
170 1.10 pk */
171 1.10 pk ptes = (int *)((int)kd->cpu_data + cpup->pmegoffset);
172 1.10 pk
173 1.7 pk vr = VA_VREG(va);
174 1.7 pk vs = VA_VSEG(va);
175 1.1 cgd
176 1.10 pk sp = &cpup->segmap_store[(vr-NUREG)*NSEGRG + vs];
177 1.7 pk if (sp->sg_npte == 0)
178 1.7 pk goto err;
179 1.11 pk if (sp->sg_pmeg == cpup->npmeg - 1) /* =seginval */
180 1.7 pk goto err;
181 1.10 pk pte = ptes[sp->sg_pmeg * nptesg + VA_VPG(va)];
182 1.7 pk if ((pte & PG_V) != 0) {
183 1.10 pk register long p, off = VA_OFF(va);
184 1.7 pk
185 1.7 pk p = (pte & PG_PFNUM) << pgshift;
186 1.10 pk *pa = p + off;
187 1.7 pk return (kd->nbpg - off);
188 1.1 cgd }
189 1.7 pk err:
190 1.1 cgd _kvm_err(kd, 0, "invalid address (%x)", va);
191 1.1 cgd return (0);
192 1.1 cgd }
193 1.4 deraadt
194 1.9 cgd int
195 1.9 cgd _kvm_kvatop4m(kd, va, pa)
196 1.9 cgd kvm_t *kd;
197 1.9 cgd u_long va;
198 1.9 cgd u_long *pa;
199 1.9 cgd {
200 1.10 pk cpu_kcore_hdr_t *cpup = kd->cpu_data;
201 1.10 pk register int vr, vs;
202 1.9 cgd int pte;
203 1.9 cgd off_t foff;
204 1.9 cgd struct regmap *rp;
205 1.9 cgd struct segmap *sp;
206 1.9 cgd
207 1.9 cgd if (va < KERNBASE)
208 1.9 cgd goto err;
209 1.9 cgd
210 1.10 pk /*
211 1.10 pk * Layout of CPU segment:
212 1.10 pk * cpu_kcore_hdr_t;
213 1.10 pk * [alignment]
214 1.10 pk * phys_ram_seg_t[cpup->nmemseg];
215 1.10 pk */
216 1.10 pk
217 1.9 cgd vr = VA_VREG(va);
218 1.9 cgd vs = VA_VSEG(va);
219 1.9 cgd
220 1.10 pk sp = &cpup->segmap_store[(vr-NUREG)*NSEGRG + vs];
221 1.9 cgd if (sp->sg_npte == 0)
222 1.9 cgd goto err;
223 1.9 cgd
224 1.10 pk /* XXX - assume page tables in initial kernel DATA or BSS. */
225 1.10 pk foff = _kvm_pa2off(kd, (u_long)&sp->sg_pte[VA_VPG(va)] - KERNBASE);
226 1.10 pk if (foff == (off_t)-1)
227 1.10 pk return (0);
228 1.10 pk
229 1.9 cgd if (lseek(kd->pmfd, foff, 0) == -1 ||
230 1.9 cgd read(kd->pmfd, (void *)&pte, sizeof(pte)) < 0) {
231 1.10 pk _kvm_err(kd, kd->program, "cannot read pte for %x", va);
232 1.10 pk return (0);
233 1.9 cgd }
234 1.9 cgd
235 1.9 cgd if ((pte & SRMMU_TETYPE) == SRMMU_TEPTE) {
236 1.10 pk register long p, off = VA_OFF(va);
237 1.9 cgd
238 1.9 cgd p = (pte & SRMMU_PPNMASK) << SRMMU_PPNPASHIFT;
239 1.10 pk *pa = p + off;
240 1.9 cgd return (kd->nbpg - off);
241 1.9 cgd }
242 1.9 cgd err:
243 1.9 cgd _kvm_err(kd, 0, "invalid address (%x)", va);
244 1.9 cgd return (0);
245 1.10 pk }
246 1.10 pk
247 1.10 pk /*
248 1.10 pk * Translate a physical address to a file-offset in the crash-dump.
249 1.10 pk */
250 1.10 pk off_t
251 1.10 pk _kvm_pa2off(kd, pa)
252 1.10 pk kvm_t *kd;
253 1.10 pk u_long pa;
254 1.10 pk {
255 1.10 pk cpu_kcore_hdr_t *cpup = kd->cpu_data;
256 1.10 pk phys_ram_seg_t *mp;
257 1.10 pk off_t off;
258 1.10 pk int nmem;
259 1.10 pk
260 1.10 pk /*
261 1.10 pk * Layout of CPU segment:
262 1.10 pk * cpu_kcore_hdr_t;
263 1.10 pk * [alignment]
264 1.10 pk * phys_ram_seg_t[cpup->nmemseg];
265 1.10 pk */
266 1.10 pk mp = (phys_ram_seg_t *)((int)kd->cpu_data + cpup->memsegoffset);
267 1.10 pk off = 0;
268 1.10 pk
269 1.10 pk /* Translate (sparse) pfnum to (packed) dump offset */
270 1.10 pk for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
271 1.10 pk if (mp->start <= pa && pa < mp->start + mp->size)
272 1.10 pk break;
273 1.10 pk off += mp->size;
274 1.10 pk }
275 1.10 pk if (nmem < 0) {
276 1.10 pk _kvm_err(kd, 0, "invalid address (%x)", pa);
277 1.10 pk return (-1);
278 1.10 pk }
279 1.10 pk
280 1.10 pk return (kd->dump_off + off + pa - mp->start);
281 1.12 gwr }
282 1.12 gwr
283 1.12 gwr /*
284 1.12 gwr * Machine-dependent initialization for ALL open kvm descriptors,
285 1.12 gwr * not just those for a kernel crash dump. Some architectures
286 1.12 gwr * have to deal with these NOT being constants! (i.e. m68k)
287 1.12 gwr */
288 1.12 gwr int
289 1.12 gwr _kvm_mdopen(kd)
290 1.12 gwr kvm_t *kd;
291 1.12 gwr {
292 1.12 gwr
293 1.12 gwr kd->usrstack = USRSTACK;
294 1.12 gwr kd->min_uva = VM_MIN_ADDRESS;
295 1.12 gwr kd->max_uva = VM_MAXUSER_ADDRESS;
296 1.12 gwr
297 1.12 gwr return (0);
298 1.4 deraadt }
299