kvm_m68k_cmn.c revision 1.14 1 /* $NetBSD: kvm_m68k_cmn.c,v 1.14 2010/09/19 02:07:00 jym Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 /*-
37 * Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
38 *
39 * This code is derived from software developed by the Computer Systems
40 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
41 * BG 91-66 and contributed to Berkeley.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72 #include <sys/cdefs.h>
73 #if defined(LIBC_SCCS) && !defined(lint)
74 #if 0
75 static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93";
76 #else
77 __RCSID("$NetBSD: kvm_m68k_cmn.c,v 1.14 2010/09/19 02:07:00 jym Exp $");
78 #endif
79 #endif /* LIBC_SCCS and not lint */
80
81 /*
82 * Common m68k machine dependent routines for kvm.
83 *
84 * Note: This file has to build on ALL m68k machines,
85 * so do NOT include any <machine / *.h> files here.
86 */
87
88 #include <sys/types.h>
89 #include <sys/kcore.h>
90
91 #include <unistd.h>
92 #include <limits.h>
93 #include <nlist.h>
94 #include <kvm.h>
95 #include <db.h>
96
97 #include <m68k/cpu.h>
98 #include <m68k/kcore.h>
99
100 #include "kvm_private.h"
101 #include "kvm_m68k.h"
102
103 int _kvm_cmn_initvtop(kvm_t *);
104 void _kvm_cmn_freevtop(kvm_t *);
105 int _kvm_cmn_kvatop(kvm_t *, u_long, u_long *);
106 off_t _kvm_cmn_pa2off(kvm_t *, u_long);
107
108 struct kvm_ops _kvm_ops_cmn = {
109 _kvm_cmn_initvtop,
110 _kvm_cmn_freevtop,
111 _kvm_cmn_kvatop,
112 _kvm_cmn_pa2off };
113
114 static int vatop_030(kvm_t *, uint32_t, u_long, u_long *);
115 static int vatop_040(kvm_t *, uint32_t, u_long, u_long *);
116
117 #define _kvm_btop(v, a) (((unsigned)(a)) >> (v)->pgshift)
118
119 #define KREAD(kd, addr, p)\
120 (kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
121
122 void
123 _kvm_cmn_freevtop(kvm_t *kd)
124 {
125 /* No private state information to keep. */
126 }
127
128 int
129 _kvm_cmn_initvtop(kvm_t *kd)
130 {
131 /* No private state information to keep. */
132 return (0);
133 }
134
135 int
136 _kvm_cmn_kvatop(kvm_t *kd, u_long va, u_long *pa)
137 {
138 cpu_kcore_hdr_t *h = kd->cpu_data;
139 struct m68k_kcore_hdr *m = &h->un._m68k;
140 int (*vtopf)(kvm_t *, uint32_t, u_long, u_long *);
141
142 if (ISALIVE(kd)) {
143 _kvm_err(kd, 0, "vatop called in live kernel!");
144 return (0);
145 }
146
147 /*
148 * 68040 and 68060 use same translation functions,
149 * as do 68030, 68851, HP MMU.
150 */
151 if (m->mmutype == MMU_68040 || m->mmutype == MMU_68060)
152 vtopf = vatop_040;
153 else
154 vtopf = vatop_030;
155
156 return ((*vtopf)(kd, m->sysseg_pa, va, pa));
157 }
158
159 /*
160 * Translate a physical address to a file-offset in the crash dump.
161 */
162 off_t
163 _kvm_cmn_pa2off(kvm_t *kd, u_long pa)
164 {
165 cpu_kcore_hdr_t *h = kd->cpu_data;
166 struct m68k_kcore_hdr *m = &h->un._m68k;
167 phys_ram_seg_t *rsp;
168 off_t off;
169 int i;
170
171 off = 0;
172 rsp = m->ram_segs;
173 for (i = 0; i < M68K_NPHYS_RAM_SEGS && rsp[i].size != 0; i++) {
174 if (pa >= rsp[i].start &&
175 pa < (rsp[i].start + rsp[i].size)) {
176 pa -= rsp[i].start;
177 break;
178 }
179 off += rsp[i].size;
180 }
181 return (kd->dump_off + off + pa);
182 }
183
184 /*****************************************************************
185 * Local stuff...
186 */
187
188 static int
189 vatop_030(kvm_t *kd, uint32_t stpa, u_long va, u_long *pa)
190 {
191 cpu_kcore_hdr_t *h = kd->cpu_data;
192 struct m68k_kcore_hdr *m = &h->un._m68k;
193 struct vmstate *vm = kd->vmst;
194 u_long addr;
195 uint32_t ste, pte;
196 u_int p, offset;
197
198 offset = va & vm->pgofset;
199
200 /*
201 * We may be called before address translation is initialized.
202 * This is typically used to find the dump magic number. This
203 * means we do not yet have the kernel page tables available,
204 * so we must to a simple relocation.
205 */
206 if (va < m->relocend) {
207 *pa = (va - h->kernbase) + m->reloc;
208 return (h->page_size - offset);
209 }
210
211 addr = stpa + ((va >> m->sg_ishift) * sizeof(u_int32_t));
212
213 /*
214 * Can't use KREAD to read kernel segment table entries.
215 * Fortunately it is 1-to-1 mapped so we don't have to.
216 */
217 if (stpa == m->sysseg_pa) {
218 if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
219 _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
220 goto invalid;
221 } else if (KREAD(kd, addr, &ste))
222 goto invalid;
223 if ((ste & m->sg_v) == 0) {
224 _kvm_err(kd, 0, "invalid segment (%x)", ste);
225 return(0);
226 }
227 p = _kvm_btop(vm, va & m->sg_pmask);
228 addr = (ste & m->sg_frame) + (p * sizeof(u_int32_t));
229
230 /*
231 * Address from STE is a physical address so don't use kvm_read.
232 */
233 if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
234 _kvm_cmn_pa2off(kd, addr)) != sizeof(pte))
235 goto invalid;
236 addr = pte & m->pg_frame;
237 if ((pte & m->pg_v) == 0) {
238 _kvm_err(kd, 0, "page not valid");
239 return (0);
240 }
241 *pa = addr + offset;
242
243 return (h->page_size - offset);
244 invalid:
245 _kvm_err(kd, 0, "invalid address (%lx)", va);
246 return (0);
247 }
248
249 static int
250 vatop_040(kvm_t *kd, uint32_t stpa, u_long va, u_long *pa)
251 {
252 cpu_kcore_hdr_t *h = kd->cpu_data;
253 struct m68k_kcore_hdr *m = &h->un._m68k;
254 struct vmstate *vm = kd->vmst;
255 u_long addr;
256 uint32_t stpa2;
257 uint32_t ste, pte;
258 u_int offset;
259
260 offset = va & vm->pgofset;
261
262 /*
263 * We may be called before address translation is initialized.
264 * This is typically used to find the dump magic number. This
265 * means we do not yet have the kernel page tables available,
266 * so we must to a simple relocation.
267 */
268 if (va < m->relocend) {
269 *pa = (va - h->kernbase) + m->reloc;
270 return (h->page_size - offset);
271 }
272
273 addr = stpa + ((va >> m->sg40_shift1) * sizeof(u_int32_t));
274
275 /*
276 * Can't use KREAD to read kernel segment table entries.
277 * Fortunately it is 1-to-1 mapped so we don't have to.
278 */
279 if (stpa == m->sysseg_pa) {
280 if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
281 _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
282 goto invalid;
283 } else if (KREAD(kd, addr, &ste))
284 goto invalid;
285 if ((ste & m->sg_v) == 0) {
286 _kvm_err(kd, 0, "invalid level 1 descriptor (%x)",
287 ste);
288 return((off_t)0);
289 }
290 stpa2 = (ste & m->sg40_addr1);
291 addr = stpa2 + (((va & m->sg40_mask2) >> m->sg40_shift2) *
292 sizeof(u_int32_t));
293
294 /*
295 * Address from level 1 STE is a physical address,
296 * so don't use kvm_read.
297 */
298 if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
299 _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
300 goto invalid;
301 if ((ste & m->sg_v) == 0) {
302 _kvm_err(kd, 0, "invalid level 2 descriptor (%x)",
303 ste);
304 return((off_t)0);
305 }
306 stpa2 = (ste & m->sg40_addr2);
307 addr = stpa2 + (((va & m->sg40_mask3) >> m->sg40_shift3) *
308 sizeof(u_int32_t));
309
310 /*
311 * Address from STE is a physical address so don't use kvm_read.
312 */
313 if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
314 _kvm_cmn_pa2off(kd, addr)) != sizeof(pte))
315 goto invalid;
316 addr = pte & m->pg_frame;
317 if ((pte & m->pg_v) == 0) {
318 _kvm_err(kd, 0, "page not valid");
319 return (0);
320 }
321 *pa = addr + offset;
322
323 return (h->page_size - offset);
324
325 invalid:
326 _kvm_err(kd, 0, "invalid address (%lx)", va);
327 return (0);
328 }
329