xref: /netbsd/lib/libkvm/kvm_m68k_cmn.c (revision c35f9c6d)
1 /*	$NetBSD: kvm_m68k_cmn.c,v 1.18 2014/03/04 06:38:08 matt 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.18 2014/03/04 06:38:08 matt 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/param.h>
89 #include <sys/types.h>
90 #include <sys/kcore.h>
91 
92 #include <unistd.h>
93 #include <limits.h>
94 #include <nlist.h>
95 #include <kvm.h>
96 #include <db.h>
97 
98 #include <m68k/cpu.h>
99 #include <m68k/kcore.h>
100 #include <m68k/m68k.h>
101 
102 #include "kvm_private.h"
103 #include "kvm_m68k.h"
104 
105 int   _kvm_cmn_initvtop(kvm_t *);
106 void  _kvm_cmn_freevtop(kvm_t *);
107 int   _kvm_cmn_kvatop(kvm_t *, vaddr_t, paddr_t *);
108 off_t _kvm_cmn_pa2off(kvm_t *, paddr_t);
109 
110 struct kvm_ops _kvm_ops_cmn = {
111 	_kvm_cmn_initvtop,
112 	_kvm_cmn_freevtop,
113 	_kvm_cmn_kvatop,
114 	_kvm_cmn_pa2off };
115 
116 static int vatop_030(kvm_t *, uint32_t, vaddr_t, paddr_t *);
117 static int vatop_040(kvm_t *, uint32_t, vaddr_t, paddr_t *);
118 
119 #define	_kvm_btop(v, a)	(((unsigned)(a)) >> (v)->pgshift)
120 
121 void
_kvm_cmn_freevtop(kvm_t * kd)122 _kvm_cmn_freevtop(kvm_t *kd)
123 {
124 	/* No private state information to keep. */
125 }
126 
127 int
_kvm_cmn_initvtop(kvm_t * kd)128 _kvm_cmn_initvtop(kvm_t *kd)
129 {
130 	/* No private state information to keep. */
131 	return (0);
132 }
133 
134 int
_kvm_cmn_kvatop(kvm_t * kd,vaddr_t va,paddr_t * pa)135 _kvm_cmn_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pa)
136 {
137 	cpu_kcore_hdr_t *h = kd->cpu_data;
138 	struct m68k_kcore_hdr *m = &h->un._m68k;
139 	int (*vtopf)(kvm_t *, uint32_t, vaddr_t, paddr_t *);
140 
141 	if (ISALIVE(kd)) {
142 		_kvm_err(kd, 0, "vatop called in live kernel!");
143 		return (0);
144 	}
145 
146 	/*
147 	 * 68040 and 68060 use same translation functions,
148 	 * as do 68030, 68851, HP MMU.
149 	 */
150 	if (m->mmutype == MMU_68040 || m->mmutype == MMU_68060)
151 		vtopf = vatop_040;
152 	else
153 		vtopf = vatop_030;
154 
155 	return ((*vtopf)(kd, m->sysseg_pa, va, pa));
156 }
157 
158 /*
159  * Translate a physical address to a file-offset in the crash dump.
160  */
161 off_t
_kvm_cmn_pa2off(kvm_t * kd,u_long pa)162 _kvm_cmn_pa2off(kvm_t *kd, u_long pa)
163 {
164 	cpu_kcore_hdr_t *h = kd->cpu_data;
165 	struct m68k_kcore_hdr *m = &h->un._m68k;
166 	phys_ram_seg_t *rsp;
167 	off_t off;
168 	int i;
169 
170 	off = 0;
171 	rsp = m->ram_segs;
172 	for (i = 0; i < M68K_NPHYS_RAM_SEGS && rsp[i].size != 0; i++) {
173 		if (pa >= rsp[i].start &&
174 		    pa < (rsp[i].start + rsp[i].size)) {
175 			pa -= rsp[i].start;
176 			break;
177 		}
178 		off += rsp[i].size;
179 	}
180 	return (kd->dump_off + off + pa);
181 }
182 
183 /*****************************************************************
184  * Local stuff...
185  */
186 
187 static int
vatop_030(kvm_t * kd,uint32_t stpa,vaddr_t va,paddr_t * pa)188 vatop_030(kvm_t *kd, uint32_t stpa, vaddr_t va, paddr_t *pa)
189 {
190 	cpu_kcore_hdr_t *h = kd->cpu_data;
191 	struct m68k_kcore_hdr *m = &h->un._m68k;
192 	struct vmstate *vm = kd->vmst;
193 	paddr_t addr;
194 	uint32_t ste, pte;
195 	u_int p, offset;
196 
197 	offset = va & vm->pgofset;
198 
199 	/*
200 	 * We may be called before address translation is initialized.
201 	 * This is typically used to find the dump magic number.  This
202 	 * means we do not yet have the kernel page tables available,
203 	 * so we must to a simple relocation.
204 	 */
205 	if (va < m->relocend) {
206 		*pa = (va - h->kernbase) + m->reloc;
207 		return (h->page_size - offset);
208 	}
209 
210 	addr = stpa + ((va >> m->sg_ishift) * sizeof(u_int32_t));
211 
212 	/*
213 	 * Can't use KREAD to read kernel segment table entries.
214 	 * Fortunately it is 1-to-1 mapped so we don't have to.
215 	 */
216 	if (stpa == m->sysseg_pa) {
217 		if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
218 		    _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
219 			goto invalid;
220 	} else if (KREAD(kd, addr, &ste))
221 		goto invalid;
222 	if ((ste & m->sg_v) == 0) {
223 		_kvm_err(kd, 0, "invalid segment (%x)", ste);
224 		return(0);
225 	}
226 	p = _kvm_btop(vm, va & m->sg_pmask);
227 	addr = (ste & m->sg_frame) + (p * sizeof(u_int32_t));
228 
229 	/*
230 	 * Address from STE is a physical address so don't use kvm_read.
231 	 */
232 	if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
233 	    _kvm_cmn_pa2off(kd, addr)) != sizeof(pte))
234 		goto invalid;
235 	addr = pte & m->pg_frame;
236 	if ((pte & m->pg_v) == 0) {
237 		_kvm_err(kd, 0, "page not valid");
238 		return (0);
239 	}
240 	*pa = addr + offset;
241 
242 	return (h->page_size - offset);
243 invalid:
244 	_kvm_err(kd, 0, "invalid address (%lx)", va);
245 	return (0);
246 }
247 
248 static int
vatop_040(kvm_t * kd,uint32_t stpa,vaddr_t va,paddr_t * pa)249 vatop_040(kvm_t *kd, uint32_t stpa, vaddr_t va, paddr_t *pa)
250 {
251 	cpu_kcore_hdr_t *h = kd->cpu_data;
252 	struct m68k_kcore_hdr *m = &h->un._m68k;
253 	struct vmstate *vm = kd->vmst;
254 	paddr_t addr;
255 	uint32_t stpa2;
256 	uint32_t ste, pte;
257 	u_int offset;
258 
259 	offset = va & vm->pgofset;
260 
261 	/*
262 	 * We may be called before address translation is initialized.
263 	 * This is typically used to find the dump magic number.  This
264 	 * means we do not yet have the kernel page tables available,
265 	 * so we must to a simple relocation.
266 	 */
267 	if (va < m->relocend) {
268 		*pa = (va - h->kernbase) + m->reloc;
269 		return (h->page_size - offset);
270 	}
271 
272 	addr = stpa + ((va >> m->sg40_shift1) * sizeof(u_int32_t));
273 
274 	/*
275 	 * Can't use KREAD to read kernel segment table entries.
276 	 * Fortunately it is 1-to-1 mapped so we don't have to.
277 	 */
278 	if (stpa == m->sysseg_pa) {
279 		if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
280 		    _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
281 			goto invalid;
282 	} else if (KREAD(kd, addr, &ste))
283 		goto invalid;
284 	if ((ste & m->sg_v) == 0) {
285 		_kvm_err(kd, 0, "invalid level 1 descriptor (%x)",
286 				 ste);
287 		return((off_t)0);
288 	}
289 	stpa2 = (ste & m->sg40_addr1);
290 	addr = stpa2 + (((va & m->sg40_mask2) >> m->sg40_shift2) *
291 	    sizeof(u_int32_t));
292 
293 	/*
294 	 * Address from level 1 STE is a physical address,
295 	 * so don't use kvm_read.
296 	 */
297 	if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
298 	    _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
299 		goto invalid;
300 	if ((ste & m->sg_v) == 0) {
301 		_kvm_err(kd, 0, "invalid level 2 descriptor (%x)",
302 				 ste);
303 		return((off_t)0);
304 	}
305 	stpa2 = (ste & m->sg40_addr2);
306 	addr = stpa2 + (((va & m->sg40_mask3) >> m->sg40_shift3) *
307 	    sizeof(u_int32_t));
308 
309 	/*
310 	 * Address from STE is a physical address so don't use kvm_read.
311 	 */
312 	if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
313 	    _kvm_cmn_pa2off(kd, addr)) != sizeof(pte))
314 		goto invalid;
315 	addr = pte & m->pg_frame;
316 	if ((pte & m->pg_v) == 0) {
317 		_kvm_err(kd, 0, "page not valid");
318 		return (0);
319 	}
320 	*pa = addr + offset;
321 
322 	return (h->page_size - offset);
323 
324 invalid:
325 	_kvm_err(kd, 0, "invalid address (%lx)", va);
326 	return (0);
327 }
328