150534Smpp/*	$NetBSD: kvm_sun2.c,v 1.5 2010/09/20 23:23:16 jym Exp $ */
250534Smpp
3322091Strasz/*-
431567Ssef * Copyright (c) 1992, 1993
579535Sru *	The Regents of the University of California.  All rights reserved.
631567Ssef *
732275Scharnier * This code is derived from software developed by the Computer Systems
831567Ssef * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
932275Scharnier * BG 91-66 and contributed to Berkeley.
1068963Sru *
11295930Sjhb * Redistribution and use in source and binary forms, with or without
1231567Ssef * modification, are permitted provided that the following conditions
13153963Sbrian * are met:
1432275Scharnier * 1. Redistributions of source code must retain the above copyright
1568963Sru *    notice, this list of conditions and the following disclaimer.
16295930Sjhb * 2. Redistributions in binary form must reproduce the above copyright
1732275Scharnier *    notice, this list of conditions and the following disclaimer in the
18153963Sbrian *    documentation and/or other materials provided with the distribution.
19165100Sru * 3. Neither the name of the University nor the names of its contributors
2031567Ssef *    may be used to endorse or promote products derived from this software
2195124Scharnier *    without specific prior written permission.
2295124Scharnier *
2395124Scharnier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2431567Ssef * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2531567Ssef * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26168569Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2731567Ssef * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2831567Ssef * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2932275Scharnier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30101283Smdodd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31148580Skeramida * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32107276Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33107276Sru * SUCH DAMAGE.
34107276Sru */
35295930Sjhb
36295930Sjhb#include <sys/cdefs.h>
37295930Sjhb#if defined(LIBC_SCCS) && !defined(lint)
38295930Sjhb#if 0
39101289Smdoddstatic char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
40107276Sru#else
41107276Sru__RCSID("$NetBSD: kvm_sun2.c,v 1.5 2010/09/20 23:23:16 jym Exp $");
42107276Sru#endif
43192025Sdds#endif /* LIBC_SCCS and not lint */
44289080Sbdrewery
45192025Sdds/*
46192025Sdds * Sun2 machine dependent routines for kvm.
47192025Sdds *
48192025Sdds * Note: This file has to build on ALL m68000 machines,
49101289Smdodd * so do NOT include any <machine / *.h> files here.
50107276Sru */
51107276Sru#include <sys/param.h>
52107276Sru#include <sys/types.h>
53101285Smdodd#include <sys/kcore.h>
54101285Smdodd
55101285Smdodd#include <unistd.h>
56101285Smdodd#include <limits.h>
57101285Smdodd#include <nlist.h>
58101285Smdodd#include <kvm.h>
59295930Sjhb#include <db.h>
60295930Sjhb
6131567Ssef#include <m68k/kcore.h>
6231567Ssef
6331567Ssef#include "kvm_private.h"
6432275Scharnier#include "kvm_m68k.h"
6531567Ssef
6632275Scharnierint   _kvm_sun2_initvtop(kvm_t *);
6732275Scharniervoid  _kvm_sun2_freevtop(kvm_t *);
6832275Scharnierint   _kvm_sun2_kvatop  (kvm_t *, vaddr_t, paddr_t *);
6932275Scharnieroff_t _kvm_sun2_pa2off  (kvm_t *, paddr_t);
70153963Sbrian
71153963Sbrianstruct kvm_ops _kvm_ops_sun2 = {
72153963Sbrian	_kvm_sun2_initvtop,
73153963Sbrian	_kvm_sun2_freevtop,
74153963Sbrian	_kvm_sun2_kvatop,
75162792Sru	_kvm_sun2_pa2off };
76153963Sbrian
77153963Sbrian#define	_kvm_pg_pa(v, s, pte)	\
78153963Sbrian	(((pte) & (s)->pg_frame) << (v)->pgshift)
79153963Sbrian
8032275Scharnier#define	_kvm_va_segnum(s, x)	\
8179755Sdd	((u_int)(x) >> (s)->segshift)
8231567Ssef#define	_kvm_pte_num_mask(v)	\
8331567Ssef	(0xf << (v)->pgshift)
84165100Sru#define	_kvm_va_pte_num(v, va)	\
8531567Ssef	(((va) & _kvm_pte_num_mask((v))) >> (v)->pgshift)
8631567Ssef
8731567Ssef/*
8831567Ssef * XXX Re-define these here, no other place for them.
8931567Ssef */
9031567Ssef#define	NKSEG		512	/* kernel segmap entries */
9131567Ssef#define	NPAGSEG		16	/* pages per segment */
9231567Ssef
9370197Sru/* Finally, our local stuff... */
9431567Ssefstruct private_vmstate {
95322090Strasz	/* Page Map Entry Group (PMEG) */
9631567Ssef	int   pmeg[NKSEG][NPAGSEG];
97322090Strasz};
98322090Strasz
9931567Ssef/*
100322090Strasz * Prepare for translation of kernel virtual addresses into offsets
101322090Strasz * into crash dump files. We use the MMU specific goop written at the
102187607Strhodes * beginning of a crash dump by dumpsys()
10331567Ssef * Note: sun2 MMU specific!
104322091Strasz */
10532275Scharnierint
10631567Ssef_kvm_sun2_initvtop(kvm_t *kd)
107288957Sbdrewery{
108288957Sbdrewery	cpu_kcore_hdr_t *h = kd->cpu_data;
10931567Ssef	char *p;
11031567Ssef
11132275Scharnier	p = kd->cpu_data;
11232275Scharnier	p += (h->page_size - sizeof(kcore_seg_t));
11332275Scharnier	kd->vmst->private = p;
11432275Scharnier
11556412Smpp	return (0);
11656412Smpp}
11731567Ssef
118void
119_kvm_sun2_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_sun2_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pap)
133{
134	cpu_kcore_hdr_t *h = kd->cpu_data;
135	struct sun2_kcore_hdr *s = &h->un._sun2;
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=0x%lx)", 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_sun2_pa2off(kvm_t *kd, paddr_t pa)
179{
180	return(kd->dump_off + pa);
181}
182