xref: /netbsd-src/lib/libkvm/kvm_m68k_cmn.c (revision c1987519cfb898fe4da251127df04929fda914c2)
1*c1987519Smatt /*	$NetBSD: kvm_m68k_cmn.c,v 1.18 2014/03/04 06:38:08 matt Exp $	*/
2eb7c1594Sagc 
3eb7c1594Sagc /*-
4eb7c1594Sagc  * Copyright (c) 1989, 1992, 1993
5eb7c1594Sagc  *	The Regents of the University of California.  All rights reserved.
6eb7c1594Sagc  *
7eb7c1594Sagc  * This code is derived from software developed by the Computer Systems
8eb7c1594Sagc  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9eb7c1594Sagc  * BG 91-66 and contributed to Berkeley.
10eb7c1594Sagc  *
11eb7c1594Sagc  * Redistribution and use in source and binary forms, with or without
12eb7c1594Sagc  * modification, are permitted provided that the following conditions
13eb7c1594Sagc  * are met:
14eb7c1594Sagc  * 1. Redistributions of source code must retain the above copyright
15eb7c1594Sagc  *    notice, this list of conditions and the following disclaimer.
16eb7c1594Sagc  * 2. Redistributions in binary form must reproduce the above copyright
17eb7c1594Sagc  *    notice, this list of conditions and the following disclaimer in the
18eb7c1594Sagc  *    documentation and/or other materials provided with the distribution.
19eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
20eb7c1594Sagc  *    may be used to endorse or promote products derived from this software
21eb7c1594Sagc  *    without specific prior written permission.
22eb7c1594Sagc  *
23eb7c1594Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24eb7c1594Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25eb7c1594Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26eb7c1594Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27eb7c1594Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28eb7c1594Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29eb7c1594Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30eb7c1594Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31eb7c1594Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32eb7c1594Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33eb7c1594Sagc  * SUCH DAMAGE.
34eb7c1594Sagc  */
3595883471Sgwr 
3695883471Sgwr /*-
37b6c0c9a2Sthorpej  * Copyright (c) 1997 Jason R. Thorpe.  All rights reserved.
3895883471Sgwr  *
3995883471Sgwr  * This code is derived from software developed by the Computer Systems
4095883471Sgwr  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
4195883471Sgwr  * BG 91-66 and contributed to Berkeley.
4295883471Sgwr  *
4395883471Sgwr  * Redistribution and use in source and binary forms, with or without
4495883471Sgwr  * modification, are permitted provided that the following conditions
4595883471Sgwr  * are met:
4695883471Sgwr  * 1. Redistributions of source code must retain the above copyright
4795883471Sgwr  *    notice, this list of conditions and the following disclaimer.
4895883471Sgwr  * 2. Redistributions in binary form must reproduce the above copyright
4995883471Sgwr  *    notice, this list of conditions and the following disclaimer in the
5095883471Sgwr  *    documentation and/or other materials provided with the distribution.
5195883471Sgwr  * 3. All advertising materials mentioning features or use of this software
5295883471Sgwr  *    must display the following acknowledgement:
5395883471Sgwr  *	This product includes software developed by the University of
5495883471Sgwr  *	California, Berkeley and its contributors.
5595883471Sgwr  * 4. Neither the name of the University nor the names of its contributors
5695883471Sgwr  *    may be used to endorse or promote products derived from this software
5795883471Sgwr  *    without specific prior written permission.
5895883471Sgwr  *
5995883471Sgwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6095883471Sgwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6195883471Sgwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6295883471Sgwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6395883471Sgwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6495883471Sgwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6595883471Sgwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6695883471Sgwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6795883471Sgwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6895883471Sgwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6995883471Sgwr  * SUCH DAMAGE.
7095883471Sgwr  */
7195883471Sgwr 
72b4119f6bSmikel #include <sys/cdefs.h>
7395883471Sgwr #if defined(LIBC_SCCS) && !defined(lint)
7495883471Sgwr #if 0
7595883471Sgwr static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
7695883471Sgwr #else
77*c1987519Smatt __RCSID("$NetBSD: kvm_m68k_cmn.c,v 1.18 2014/03/04 06:38:08 matt Exp $");
7895883471Sgwr #endif
7995883471Sgwr #endif /* LIBC_SCCS and not lint */
8095883471Sgwr 
8195883471Sgwr /*
8295883471Sgwr  * Common m68k machine dependent routines for kvm.
8395883471Sgwr  *
8495883471Sgwr  * Note: This file has to build on ALL m68k machines,
8595883471Sgwr  * so do NOT include any <machine / *.h> files here.
8695883471Sgwr  */
8795883471Sgwr 
8880867328Schristos #include <sys/param.h>
8995883471Sgwr #include <sys/types.h>
9095883471Sgwr #include <sys/kcore.h>
9195883471Sgwr 
9295883471Sgwr #include <unistd.h>
9395883471Sgwr #include <limits.h>
9495883471Sgwr #include <nlist.h>
9595883471Sgwr #include <kvm.h>
9695883471Sgwr #include <db.h>
9795883471Sgwr 
9895883471Sgwr #include <m68k/cpu.h>
9995883471Sgwr #include <m68k/kcore.h>
100*c1987519Smatt #include <m68k/m68k.h>
10195883471Sgwr 
10295883471Sgwr #include "kvm_private.h"
10395883471Sgwr #include "kvm_m68k.h"
10495883471Sgwr 
1056dc46b92Sjym int   _kvm_cmn_initvtop(kvm_t *);
1066dc46b92Sjym void  _kvm_cmn_freevtop(kvm_t *);
107962a341dSjym int   _kvm_cmn_kvatop(kvm_t *, vaddr_t, paddr_t *);
108962a341dSjym off_t _kvm_cmn_pa2off(kvm_t *, paddr_t);
10995883471Sgwr 
11095883471Sgwr struct kvm_ops _kvm_ops_cmn = {
11195883471Sgwr 	_kvm_cmn_initvtop,
11295883471Sgwr 	_kvm_cmn_freevtop,
11395883471Sgwr 	_kvm_cmn_kvatop,
11495883471Sgwr 	_kvm_cmn_pa2off };
11595883471Sgwr 
116962a341dSjym static int vatop_030(kvm_t *, uint32_t, vaddr_t, paddr_t *);
117962a341dSjym static int vatop_040(kvm_t *, uint32_t, vaddr_t, paddr_t *);
11895883471Sgwr 
119b6c0c9a2Sthorpej #define	_kvm_btop(v, a)	(((unsigned)(a)) >> (v)->pgshift)
12095883471Sgwr 
12195883471Sgwr void
_kvm_cmn_freevtop(kvm_t * kd)1226dc46b92Sjym _kvm_cmn_freevtop(kvm_t *kd)
12395883471Sgwr {
124b6c0c9a2Sthorpej 	/* No private state information to keep. */
12595883471Sgwr }
12695883471Sgwr 
12795883471Sgwr int
_kvm_cmn_initvtop(kvm_t * kd)1286dc46b92Sjym _kvm_cmn_initvtop(kvm_t *kd)
12995883471Sgwr {
130b6c0c9a2Sthorpej 	/* No private state information to keep. */
13195883471Sgwr 	return (0);
13295883471Sgwr }
13395883471Sgwr 
13495883471Sgwr int
_kvm_cmn_kvatop(kvm_t * kd,vaddr_t va,paddr_t * pa)135962a341dSjym _kvm_cmn_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pa)
13695883471Sgwr {
137b6c0c9a2Sthorpej 	cpu_kcore_hdr_t *h = kd->cpu_data;
138b6c0c9a2Sthorpej 	struct m68k_kcore_hdr *m = &h->un._m68k;
139962a341dSjym 	int (*vtopf)(kvm_t *, uint32_t, vaddr_t, paddr_t *);
14095883471Sgwr 
14195883471Sgwr 	if (ISALIVE(kd)) {
14295883471Sgwr 		_kvm_err(kd, 0, "vatop called in live kernel!");
14395883471Sgwr 		return (0);
14495883471Sgwr 	}
14595883471Sgwr 
146b6c0c9a2Sthorpej 	/*
147c8aff50dSkleink 	 * 68040 and 68060 use same translation functions,
148b6c0c9a2Sthorpej 	 * as do 68030, 68851, HP MMU.
149b6c0c9a2Sthorpej 	 */
150b6c0c9a2Sthorpej 	if (m->mmutype == MMU_68040 || m->mmutype == MMU_68060)
15195883471Sgwr 		vtopf = vatop_040;
152b6c0c9a2Sthorpej 	else
153b6c0c9a2Sthorpej 		vtopf = vatop_030;
15495883471Sgwr 
155b6c0c9a2Sthorpej 	return ((*vtopf)(kd, m->sysseg_pa, va, pa));
15695883471Sgwr }
15795883471Sgwr 
15895883471Sgwr /*
159b976c559Swiz  * Translate a physical address to a file-offset in the crash dump.
16095883471Sgwr  */
16195883471Sgwr off_t
_kvm_cmn_pa2off(kvm_t * kd,u_long pa)1626dc46b92Sjym _kvm_cmn_pa2off(kvm_t *kd, u_long pa)
16395883471Sgwr {
164b6c0c9a2Sthorpej 	cpu_kcore_hdr_t *h = kd->cpu_data;
165b6c0c9a2Sthorpej 	struct m68k_kcore_hdr *m = &h->un._m68k;
16695883471Sgwr 	phys_ram_seg_t *rsp;
167b6c0c9a2Sthorpej 	off_t off;
168b6c0c9a2Sthorpej 	int i;
16995883471Sgwr 
17095883471Sgwr 	off = 0;
171b6c0c9a2Sthorpej 	rsp = m->ram_segs;
172b6c0c9a2Sthorpej 	for (i = 0; i < M68K_NPHYS_RAM_SEGS && rsp[i].size != 0; i++) {
173b6c0c9a2Sthorpej 		if (pa >= rsp[i].start &&
174b6c0c9a2Sthorpej 		    pa < (rsp[i].start + rsp[i].size)) {
175ac3595cbSscottr 			pa -= rsp[i].start;
17695883471Sgwr 			break;
17795883471Sgwr 		}
178ac3595cbSscottr 		off += rsp[i].size;
17995883471Sgwr 	}
18095883471Sgwr 	return (kd->dump_off + off + pa);
18195883471Sgwr }
18295883471Sgwr 
18395883471Sgwr /*****************************************************************
18495883471Sgwr  * Local stuff...
18595883471Sgwr  */
18695883471Sgwr 
18795883471Sgwr static int
vatop_030(kvm_t * kd,uint32_t stpa,vaddr_t va,paddr_t * pa)188962a341dSjym vatop_030(kvm_t *kd, uint32_t stpa, vaddr_t va, paddr_t *pa)
18995883471Sgwr {
190b6c0c9a2Sthorpej 	cpu_kcore_hdr_t *h = kd->cpu_data;
191b6c0c9a2Sthorpej 	struct m68k_kcore_hdr *m = &h->un._m68k;
192b6c0c9a2Sthorpej 	struct vmstate *vm = kd->vmst;
193962a341dSjym 	paddr_t addr;
1946dc46b92Sjym 	uint32_t ste, pte;
195b6c0c9a2Sthorpej 	u_int p, offset;
19695883471Sgwr 
197b6c0c9a2Sthorpej 	offset = va & vm->pgofset;
19895883471Sgwr 
19995883471Sgwr 	/*
200b6c0c9a2Sthorpej 	 * We may be called before address translation is initialized.
201b6c0c9a2Sthorpej 	 * This is typically used to find the dump magic number.  This
202b6c0c9a2Sthorpej 	 * means we do not yet have the kernel page tables available,
203b6c0c9a2Sthorpej 	 * so we must to a simple relocation.
20495883471Sgwr 	 */
205b6c0c9a2Sthorpej 	if (va < m->relocend) {
206b6c0c9a2Sthorpej 		*pa = (va - h->kernbase) + m->reloc;
207b6c0c9a2Sthorpej 		return (h->page_size - offset);
20895883471Sgwr 	}
20995883471Sgwr 
210b6c0c9a2Sthorpej 	addr = stpa + ((va >> m->sg_ishift) * sizeof(u_int32_t));
211b6c0c9a2Sthorpej 
21295883471Sgwr 	/*
21395883471Sgwr 	 * Can't use KREAD to read kernel segment table entries.
21495883471Sgwr 	 * Fortunately it is 1-to-1 mapped so we don't have to.
21595883471Sgwr 	 */
216b6c0c9a2Sthorpej 	if (stpa == m->sysseg_pa) {
217a7a2d171Sad 		if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
21855c7ea7cSthorpej 		    _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
21995883471Sgwr 			goto invalid;
22095883471Sgwr 	} else if (KREAD(kd, addr, &ste))
22195883471Sgwr 		goto invalid;
222b6c0c9a2Sthorpej 	if ((ste & m->sg_v) == 0) {
22395883471Sgwr 		_kvm_err(kd, 0, "invalid segment (%x)", ste);
224b6c0c9a2Sthorpej 		return(0);
22595883471Sgwr 	}
226b6c0c9a2Sthorpej 	p = _kvm_btop(vm, va & m->sg_pmask);
227b6c0c9a2Sthorpej 	addr = (ste & m->sg_frame) + (p * sizeof(u_int32_t));
22895883471Sgwr 
22995883471Sgwr 	/*
23095883471Sgwr 	 * Address from STE is a physical address so don't use kvm_read.
23195883471Sgwr 	 */
232a7a2d171Sad 	if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
233a7a2d171Sad 	    _kvm_cmn_pa2off(kd, addr)) != sizeof(pte))
23495883471Sgwr 		goto invalid;
235b6c0c9a2Sthorpej 	addr = pte & m->pg_frame;
236b6c0c9a2Sthorpej 	if ((pte & m->pg_v) == 0) {
23795883471Sgwr 		_kvm_err(kd, 0, "page not valid");
23895883471Sgwr 		return (0);
23995883471Sgwr 	}
24095883471Sgwr 	*pa = addr + offset;
24195883471Sgwr 
242b6c0c9a2Sthorpej 	return (h->page_size - offset);
24395883471Sgwr invalid:
244dd0d61e3She 	_kvm_err(kd, 0, "invalid address (%lx)", va);
24595883471Sgwr 	return (0);
24695883471Sgwr }
24795883471Sgwr 
24895883471Sgwr static int
vatop_040(kvm_t * kd,uint32_t stpa,vaddr_t va,paddr_t * pa)249962a341dSjym vatop_040(kvm_t *kd, uint32_t stpa, vaddr_t va, paddr_t *pa)
25095883471Sgwr {
251b6c0c9a2Sthorpej 	cpu_kcore_hdr_t *h = kd->cpu_data;
252b6c0c9a2Sthorpej 	struct m68k_kcore_hdr *m = &h->un._m68k;
253b6c0c9a2Sthorpej 	struct vmstate *vm = kd->vmst;
254962a341dSjym 	paddr_t addr;
2556dc46b92Sjym 	uint32_t stpa2;
2566dc46b92Sjym 	uint32_t ste, pte;
257f10f7ea1Sbriggs 	u_int offset;
25895883471Sgwr 
259b6c0c9a2Sthorpej 	offset = va & vm->pgofset;
260b6c0c9a2Sthorpej 
26195883471Sgwr 	/*
262b6c0c9a2Sthorpej 	 * We may be called before address translation is initialized.
263b6c0c9a2Sthorpej 	 * This is typically used to find the dump magic number.  This
264b6c0c9a2Sthorpej 	 * means we do not yet have the kernel page tables available,
265b6c0c9a2Sthorpej 	 * so we must to a simple relocation.
26695883471Sgwr 	 */
267b6c0c9a2Sthorpej 	if (va < m->relocend) {
268b6c0c9a2Sthorpej 		*pa = (va - h->kernbase) + m->reloc;
269b6c0c9a2Sthorpej 		return (h->page_size - offset);
27095883471Sgwr 	}
27195883471Sgwr 
272b6c0c9a2Sthorpej 	addr = stpa + ((va >> m->sg40_shift1) * sizeof(u_int32_t));
273b6c0c9a2Sthorpej 
27495883471Sgwr 	/*
27595883471Sgwr 	 * Can't use KREAD to read kernel segment table entries.
27695883471Sgwr 	 * Fortunately it is 1-to-1 mapped so we don't have to.
27795883471Sgwr 	 */
278b6c0c9a2Sthorpej 	if (stpa == m->sysseg_pa) {
279a7a2d171Sad 		if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
28055c7ea7cSthorpej 		    _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
28195883471Sgwr 			goto invalid;
28295883471Sgwr 	} else if (KREAD(kd, addr, &ste))
28395883471Sgwr 		goto invalid;
284b6c0c9a2Sthorpej 	if ((ste & m->sg_v) == 0) {
28595883471Sgwr 		_kvm_err(kd, 0, "invalid level 1 descriptor (%x)",
28695883471Sgwr 				 ste);
28795883471Sgwr 		return((off_t)0);
28895883471Sgwr 	}
289b6c0c9a2Sthorpej 	stpa2 = (ste & m->sg40_addr1);
290b6c0c9a2Sthorpej 	addr = stpa2 + (((va & m->sg40_mask2) >> m->sg40_shift2) *
291b6c0c9a2Sthorpej 	    sizeof(u_int32_t));
292b6c0c9a2Sthorpej 
29395883471Sgwr 	/*
29495883471Sgwr 	 * Address from level 1 STE is a physical address,
29595883471Sgwr 	 * so don't use kvm_read.
29695883471Sgwr 	 */
297a7a2d171Sad 	if (_kvm_pread(kd, kd->pmfd, &ste, sizeof(ste),
298a7a2d171Sad 	    _kvm_cmn_pa2off(kd, addr)) != sizeof(ste))
29995883471Sgwr 		goto invalid;
300b6c0c9a2Sthorpej 	if ((ste & m->sg_v) == 0) {
30195883471Sgwr 		_kvm_err(kd, 0, "invalid level 2 descriptor (%x)",
30295883471Sgwr 				 ste);
30395883471Sgwr 		return((off_t)0);
30495883471Sgwr 	}
305b6c0c9a2Sthorpej 	stpa2 = (ste & m->sg40_addr2);
306b6c0c9a2Sthorpej 	addr = stpa2 + (((va & m->sg40_mask3) >> m->sg40_shift3) *
307b6c0c9a2Sthorpej 	    sizeof(u_int32_t));
30895883471Sgwr 
30995883471Sgwr 	/*
31095883471Sgwr 	 * Address from STE is a physical address so don't use kvm_read.
31195883471Sgwr 	 */
312a7a2d171Sad 	if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
313a7a2d171Sad 	    _kvm_cmn_pa2off(kd, addr)) != sizeof(pte))
31495883471Sgwr 		goto invalid;
315b6c0c9a2Sthorpej 	addr = pte & m->pg_frame;
316b6c0c9a2Sthorpej 	if ((pte & m->pg_v) == 0) {
31795883471Sgwr 		_kvm_err(kd, 0, "page not valid");
31895883471Sgwr 		return (0);
31995883471Sgwr 	}
32095883471Sgwr 	*pa = addr + offset;
32195883471Sgwr 
322b6c0c9a2Sthorpej 	return (h->page_size - offset);
323b6c0c9a2Sthorpej 
32495883471Sgwr invalid:
325dd0d61e3She 	_kvm_err(kd, 0, "invalid address (%lx)", va);
32695883471Sgwr 	return (0);
32795883471Sgwr }
328