xref: /netbsd-src/lib/libkvm/kvm_sun3.c (revision e375a9464f7c6a00756c974dc43a656e7fb7f42f)
1*e375a946Schristos /*	$NetBSD: kvm_sun3.c,v 1.15 2011/09/14 12:37:55 christos Exp $	*/
2346e67f8Sthorpej 
3d416f9adSgwr /*-
4d416f9adSgwr  * Copyright (c) 1992, 1993
5d416f9adSgwr  *	The Regents of the University of California.  All rights reserved.
6d416f9adSgwr  *
7d416f9adSgwr  * This code is derived from software developed by the Computer Systems
8d416f9adSgwr  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9d416f9adSgwr  * BG 91-66 and contributed to Berkeley.
10d416f9adSgwr  *
11d416f9adSgwr  * Redistribution and use in source and binary forms, with or without
12d416f9adSgwr  * modification, are permitted provided that the following conditions
13d416f9adSgwr  * are met:
14d416f9adSgwr  * 1. Redistributions of source code must retain the above copyright
15d416f9adSgwr  *    notice, this list of conditions and the following disclaimer.
16d416f9adSgwr  * 2. Redistributions in binary form must reproduce the above copyright
17d416f9adSgwr  *    notice, this list of conditions and the following disclaimer in the
18d416f9adSgwr  *    documentation and/or other materials provided with the distribution.
19eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
20d416f9adSgwr  *    may be used to endorse or promote products derived from this software
21d416f9adSgwr  *    without specific prior written permission.
22d416f9adSgwr  *
23d416f9adSgwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24d416f9adSgwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25d416f9adSgwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26d416f9adSgwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27d416f9adSgwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28d416f9adSgwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29d416f9adSgwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30d416f9adSgwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31d416f9adSgwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32d416f9adSgwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33d416f9adSgwr  * SUCH DAMAGE.
34d416f9adSgwr  */
35d416f9adSgwr 
36b4119f6bSmikel #include <sys/cdefs.h>
3782118b75Sgwr #if defined(LIBC_SCCS) && !defined(lint)
3882118b75Sgwr #if 0
3982118b75Sgwr static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
4082118b75Sgwr #else
41*e375a946Schristos __RCSID("$NetBSD: kvm_sun3.c,v 1.15 2011/09/14 12:37:55 christos Exp $");
4282118b75Sgwr #endif
4382118b75Sgwr #endif /* LIBC_SCCS and not lint */
4482118b75Sgwr 
45d416f9adSgwr /*
4682118b75Sgwr  * Sun3 machine dependent routines for kvm.
4795883471Sgwr  *
4895883471Sgwr  * Note: This file has to build on ALL m68k machines,
4995883471Sgwr  * so do NOT include any <machine / *.h> files here.
50d416f9adSgwr  */
51*e375a946Schristos #include <sys/param.h>
5295883471Sgwr #include <sys/types.h>
5382118b75Sgwr #include <sys/kcore.h>
5482118b75Sgwr 
55d416f9adSgwr #include <unistd.h>
5682118b75Sgwr #include <limits.h>
57d416f9adSgwr #include <nlist.h>
58d416f9adSgwr #include <kvm.h>
5982118b75Sgwr #include <db.h>
60d416f9adSgwr 
61b6c0c9a2Sthorpej #include <m68k/kcore.h>
62b6c0c9a2Sthorpej 
63d416f9adSgwr #include "kvm_private.h"
6495883471Sgwr #include "kvm_m68k.h"
65d416f9adSgwr 
666dc46b92Sjym int   _kvm_sun3_initvtop(kvm_t *);
676dc46b92Sjym void  _kvm_sun3_freevtop(kvm_t *);
68962a341dSjym int   _kvm_sun3_kvatop  (kvm_t *, vaddr_t, paddr_t *);
69962a341dSjym off_t _kvm_sun3_pa2off  (kvm_t *, paddr_t);
7095883471Sgwr 
7195883471Sgwr struct kvm_ops _kvm_ops_sun3 = {
7295883471Sgwr 	_kvm_sun3_initvtop,
7395883471Sgwr 	_kvm_sun3_freevtop,
7495883471Sgwr 	_kvm_sun3_kvatop,
7595883471Sgwr 	_kvm_sun3_pa2off };
7695883471Sgwr 
77b6c0c9a2Sthorpej #define	_kvm_pg_pa(v, s, pte)	\
78b6c0c9a2Sthorpej 	(((pte) & (s)->pg_frame) << (v)->pgshift)
79b6c0c9a2Sthorpej 
80b6c0c9a2Sthorpej #define	_kvm_va_segnum(s, x)	\
81b6c0c9a2Sthorpej 	((u_int)(x) >> (s)->segshift)
82b6c0c9a2Sthorpej #define	_kvm_pte_num_mask(v)	\
83b6c0c9a2Sthorpej 	(0xf << (v)->pgshift)
84b6c0c9a2Sthorpej #define	_kvm_va_pte_num(v, va)	\
85b6c0c9a2Sthorpej 	(((va) & _kvm_pte_num_mask((v))) >> (v)->pgshift)
86b6c0c9a2Sthorpej 
8795883471Sgwr /*
88b6c0c9a2Sthorpej  * XXX Re-define these here, no other place for them.
8995883471Sgwr  */
9095883471Sgwr #define	NKSEG		256	/* kernel segmap entries */
9195883471Sgwr #define	NPAGSEG		16	/* pages per segment */
9295883471Sgwr 
9395883471Sgwr /* Finally, our local stuff... */
94b6c0c9a2Sthorpej struct private_vmstate {
9582118b75Sgwr 	/* Page Map Entry Group (PMEG) */
9682118b75Sgwr 	int   pmeg[NKSEG][NPAGSEG];
97d416f9adSgwr };
98d416f9adSgwr 
9982118b75Sgwr /*
10082118b75Sgwr  * Prepare for translation of kernel virtual addresses into offsets
10182118b75Sgwr  * into crash dump files. We use the MMU specific goop written at the
10282118b75Sgwr  * beginning of a crash dump by dumpsys()
10382118b75Sgwr  * Note: sun3 MMU specific!
10482118b75Sgwr  */
10582118b75Sgwr int
_kvm_sun3_initvtop(kvm_t * kd)1066dc46b92Sjym _kvm_sun3_initvtop(kvm_t *kd)
10782118b75Sgwr {
108b6c0c9a2Sthorpej 	cpu_kcore_hdr_t *h = kd->cpu_data;
109b6c0c9a2Sthorpej 	char *p;
11082118b75Sgwr 
11182118b75Sgwr 	p = kd->cpu_data;
112b6c0c9a2Sthorpej 	p += (h->page_size - sizeof(kcore_seg_t));
113b6c0c9a2Sthorpej 	kd->vmst->private = p;
11482118b75Sgwr 
11582118b75Sgwr 	return (0);
11682118b75Sgwr }
11782118b75Sgwr 
118d416f9adSgwr void
_kvm_sun3_freevtop(kvm_t * kd)1196dc46b92Sjym _kvm_sun3_freevtop(kvm_t *kd)
120d416f9adSgwr {
12182118b75Sgwr 	/* This was set by pointer arithmetic, not allocation. */
122b6c0c9a2Sthorpej 	kd->vmst->private = (void*)0;
123d416f9adSgwr }
124d416f9adSgwr 
125d416f9adSgwr /*
126d416f9adSgwr  * Translate a kernel virtual address to a physical address using the
127d416f9adSgwr  * mapping information in kd->vm.  Returns the result in pa, and returns
128d416f9adSgwr  * the number of bytes that are contiguously available from this
129d416f9adSgwr  * physical address.  This routine is used only for crash dumps.
130d416f9adSgwr  */
131d416f9adSgwr int
_kvm_sun3_kvatop(kvm_t * kd,vaddr_t va,paddr_t * pap)132962a341dSjym _kvm_sun3_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pap)
133d416f9adSgwr {
134b6c0c9a2Sthorpej 	cpu_kcore_hdr_t *h = kd->cpu_data;
135b6c0c9a2Sthorpej 	struct sun3_kcore_hdr *s = &h->un._sun3;
136b6c0c9a2Sthorpej 	struct vmstate *v = kd->vmst;
137b6c0c9a2Sthorpej 	struct private_vmstate *pv = v->private;
13882118b75Sgwr 	int pte, offset;
139b6c0c9a2Sthorpej 	u_int segnum, sme, ptenum;
140962a341dSjym 	paddr_t pa;
14182118b75Sgwr 
14282118b75Sgwr 	if (ISALIVE(kd)) {
14382118b75Sgwr 		_kvm_err(kd, 0, "vatop called in live kernel!");
144b6c0c9a2Sthorpej 		return(0);
14582118b75Sgwr 	}
146d416f9adSgwr 
147b6c0c9a2Sthorpej 	if (va < h->kernbase) {
14882118b75Sgwr 		_kvm_err(kd, 0, "not a kernel address");
149b6c0c9a2Sthorpej 		return(0);
150d416f9adSgwr 	}
151d416f9adSgwr 
15282118b75Sgwr 	/*
15382118b75Sgwr 	 * Get the segmap entry (sme) from the kernel segmap.
15482118b75Sgwr 	 * Note: only have segmap entries from KERNBASE to end.
15582118b75Sgwr 	 */
156b6c0c9a2Sthorpej 	segnum = _kvm_va_segnum(s, va - h->kernbase);
157b6c0c9a2Sthorpej 	ptenum = _kvm_va_pte_num(v, va);
158b6c0c9a2Sthorpej 	offset = va & v->pgofset;
15982118b75Sgwr 
16082118b75Sgwr 	/* The segmap entry selects a PMEG. */
161b6c0c9a2Sthorpej 	sme = s->ksegmap[segnum];
162b6c0c9a2Sthorpej 	pte = pv->pmeg[sme][ptenum];
16382118b75Sgwr 
164b6c0c9a2Sthorpej 	if ((pte & (s)->pg_valid) == 0) {
165962a341dSjym 		_kvm_err(kd, 0, "page not valid (VA=%#"PRIxVADDR")", va);
166d416f9adSgwr 		return (0);
167d416f9adSgwr 	}
168b6c0c9a2Sthorpej 	pa = _kvm_pg_pa(v, s, pte) + offset;
169d416f9adSgwr 
17082118b75Sgwr 	*pap = pa;
171b6c0c9a2Sthorpej 	return (h->page_size - offset);
17282118b75Sgwr }
17382118b75Sgwr 
17482118b75Sgwr /*
175b976c559Swiz  * Translate a physical address to a file-offset in the crash dump.
17682118b75Sgwr  */
17782118b75Sgwr off_t
_kvm_sun3_pa2off(kvm_t * kd,paddr_t pa)178962a341dSjym _kvm_sun3_pa2off(kvm_t *kd, paddr_t pa)
17982118b75Sgwr {
18082118b75Sgwr 	return(kd->dump_off + pa);
181d416f9adSgwr }
182