1/*	$NetBSD: kvm_sun3.c,v 1.14 2010/09/20 23:23:16 jym Exp $	*/
2
3/*-
4 * Copyright (c) 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#include <sys/cdefs.h>
37#if defined(LIBC_SCCS) && !defined(lint)
38#if 0
39static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
40#else
41__RCSID("$NetBSD: kvm_sun3.c,v 1.14 2010/09/20 23:23:16 jym Exp $");
42#endif
43#endif /* LIBC_SCCS and not lint */
44
45/*
46 * Sun3 machine dependent routines for kvm.
47 *
48 * Note: This file has to build on ALL m68k machines,
49 * so do NOT include any <machine / *.h> files here.
50 */
51#include <sys/param.h>
52#include <sys/types.h>
53#include <sys/kcore.h>
54
55#include <unistd.h>
56#include <limits.h>
57#include <nlist.h>
58#include <kvm.h>
59#include <db.h>
60
61#include <m68k/kcore.h>
62
63#include "kvm_private.h"
64#include "kvm_m68k.h"
65
66int   _kvm_sun3_initvtop(kvm_t *);
67void  _kvm_sun3_freevtop(kvm_t *);
68int   _kvm_sun3_kvatop  (kvm_t *, vaddr_t, paddr_t *);
69off_t _kvm_sun3_pa2off  (kvm_t *, paddr_t);
70
71struct kvm_ops _kvm_ops_sun3 = {
72	_kvm_sun3_initvtop,
73	_kvm_sun3_freevtop,
74	_kvm_sun3_kvatop,
75	_kvm_sun3_pa2off };
76
77#define	_kvm_pg_pa(v, s, pte)	\
78	(((pte) & (s)->pg_frame) << (v)->pgshift)
79
80#define	_kvm_va_segnum(s, x)	\
81	((u_int)(x) >> (s)->segshift)
82#define	_kvm_pte_num_mask(v)	\
83	(0xf << (v)->pgshift)
84#define	_kvm_va_pte_num(v, va)	\
85	(((va) & _kvm_pte_num_mask((v))) >> (v)->pgshift)
86
87/*
88 * XXX Re-define these here, no other place for them.
89 */
90#define	NKSEG		256	/* kernel segmap entries */
91#define	NPAGSEG		16	/* pages per segment */
92
93/* Finally, our local stuff... */
94struct private_vmstate {
95	/* Page Map Entry Group (PMEG) */
96	int   pmeg[NKSEG][NPAGSEG];
97};
98
99/*
100 * Prepare for translation of kernel virtual addresses into offsets
101 * into crash dump files. We use the MMU specific goop written at the
102 * beginning of a crash dump by dumpsys()
103 * Note: sun3 MMU specific!
104 */
105int
106_kvm_sun3_initvtop(kvm_t *kd)
107{
108	cpu_kcore_hdr_t *h = kd->cpu_data;
109	char *p;
110
111	p = kd->cpu_data;
112	p += (h->page_size - sizeof(kcore_seg_t));
113	kd->vmst->private = p;
114
115	return (0);
116}
117
118void
119_kvm_sun3_freevtop(kvm_t *kd)
120{
121	/* This was set by pointer arithmetic, not allocation. */
122	kd->vmst->private = (void*)0;
123}
124
125/*
126 * Translate a kernel virtual address to a physical address using the
127 * mapping information in kd->vm.  Returns the result in pa, and returns
128 * the number of bytes that are contiguously available from this
129 * physical address.  This routine is used only for crash dumps.
130 */
131int
132_kvm_sun3_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pap)
133{
134	cpu_kcore_hdr_t *h = kd->cpu_data;
135	struct sun3_kcore_hdr *s = &h->un._sun3;
136	struct vmstate *v = kd->vmst;
137	struct private_vmstate *pv = v->private;
138	int pte, offset;
139	u_int segnum, sme, ptenum;
140	paddr_t pa;
141
142	if (ISALIVE(kd)) {
143		_kvm_err(kd, 0, "vatop called in live kernel!");
144		return(0);
145	}
146
147	if (va < h->kernbase) {
148		_kvm_err(kd, 0, "not a kernel address");
149		return(0);
150	}
151
152	/*
153	 * Get the segmap entry (sme) from the kernel segmap.
154	 * Note: only have segmap entries from KERNBASE to end.
155	 */
156	segnum = _kvm_va_segnum(s, va - h->kernbase);
157	ptenum = _kvm_va_pte_num(v, va);
158	offset = va & v->pgofset;
159
160	/* The segmap entry selects a PMEG. */
161	sme = s->ksegmap[segnum];
162	pte = pv->pmeg[sme][ptenum];
163
164	if ((pte & (s)->pg_valid) == 0) {
165		_kvm_err(kd, 0, "page not valid (VA=%#"PRIxVADDR")", va);
166		return (0);
167	}
168	pa = _kvm_pg_pa(v, s, pte) + offset;
169
170	*pap = pa;
171	return (h->page_size - offset);
172}
173
174/*
175 * Translate a physical address to a file-offset in the crash dump.
176 */
177off_t
178_kvm_sun3_pa2off(kvm_t *kd, paddr_t pa)
179{
180	return(kd->dump_off + pa);
181}
182