1/*	$NetBSD: kvm_sun3x.c,v 1.11 2010/09/20 23:23:16 jym Exp $	*/
2
3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint)
34#if 0
35static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
36#else
37__RCSID("$NetBSD: kvm_sun3x.c,v 1.11 2010/09/20 23:23:16 jym Exp $");
38#endif
39#endif /* LIBC_SCCS and not lint */
40
41/*
42 * Sun3x machine dependent routines for kvm.
43 *
44 * Note: This file has to build on ALL m68k machines,
45 * so do NOT include any <machine / *.h> files here.
46 */
47#include <sys/param.h>
48#include <sys/types.h>
49#include <sys/kcore.h>
50
51#include <unistd.h>
52#include <limits.h>
53#include <nlist.h>
54#include <kvm.h>
55#include <db.h>
56
57#include <m68k/kcore.h>
58
59#include "kvm_private.h"
60#include "kvm_m68k.h"
61
62int   _kvm_sun3x_initvtop(kvm_t *);
63void  _kvm_sun3x_freevtop(kvm_t *);
64int   _kvm_sun3x_kvatop  (kvm_t *, vaddr_t, paddr_t *);
65off_t _kvm_sun3x_pa2off  (kvm_t *, paddr_t);
66
67struct kvm_ops _kvm_ops_sun3x = {
68	_kvm_sun3x_initvtop,
69	_kvm_sun3x_freevtop,
70	_kvm_sun3x_kvatop,
71	_kvm_sun3x_pa2off };
72
73#define	_kvm_kvas_size(h)	\
74	(-((h)->kernbase))
75#define	_kvm_nkptes(h, v)	\
76	(_kvm_kvas_size((h)) >> (v)->pgshift)
77#define	_kvm_pg_pa(pte, h)	\
78	((pte) & (h)->pg_frame)
79
80/*
81 * Prepare for translation of kernel virtual addresses into offsets
82 * into crash dump files.  Nothing to do here.
83 */
84int
85_kvm_sun3x_initvtop(kvm_t *kd)
86{
87	return 0;
88}
89
90void
91_kvm_sun3x_freevtop(kvm_t *kd)
92{
93}
94
95/*
96 * Translate a kernel virtual address to a physical address using the
97 * mapping information in kd->vm.  Returns the result in pa, and returns
98 * the number of bytes that are contiguously available from this
99 * physical address.  This routine is used only for crash dumps.
100 */
101int
102_kvm_sun3x_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pap)
103{
104	cpu_kcore_hdr_t *h = kd->cpu_data;
105	struct sun3x_kcore_hdr *s = &h->un._sun3x;
106	struct vmstate *v = kd->vmst;
107	int idx, len, offset, pte;
108	u_long pteva, pa;
109
110	if (ISALIVE(kd)) {
111		_kvm_err(kd, 0, "vatop called in live kernel!");
112		return(0);
113	}
114
115	if (va < h->kernbase) {
116		_kvm_err(kd, 0, "not a kernel address");
117		return(0);
118	}
119
120	/*
121	 * If this VA is in the contiguous range, short-cut.
122	 * Note that this ends our recursion when we call
123	 * kvm_read to access the kernel page table, which
124	 * is guaranteed to be in the contiguous range.
125	 */
126	if (va < s->contig_end) {
127		len = s->contig_end - va;
128		pa = va - h->kernbase;
129		goto done;
130	}
131
132	/*
133	 * The KVA is beyond the contiguous range, so we must
134	 * read the PTE for this KVA from the page table.
135	 */
136	idx = ((va - h->kernbase) >> v->pgshift);
137	pteva = s->kernCbase + (idx * 4);
138	if (kvm_read(kd, pteva, &pte, 4) != 4) {
139		_kvm_err(kd, 0, "can not read PTE!");
140		return (0);
141	}
142	if ((pte & s->pg_valid) == 0) {
143		_kvm_err(kd, 0, "page not valid (VA=0x%lx)", va);
144		return (0);
145	}
146	offset = va & v->pgofset;
147	len = (h->page_size - offset);
148	pa = _kvm_pg_pa(pte, s) + offset;
149
150done:
151	*pap = pa;
152	return (len);
153}
154
155/*
156 * Translate a physical address to a file-offset in the crash dump.
157 */
158off_t
159_kvm_sun3x_pa2off(kvm_t *kd, paddr_t pa)
160{
161	off_t		off;
162	phys_ram_seg_t	*rsp;
163	cpu_kcore_hdr_t *h = kd->cpu_data;
164	struct sun3x_kcore_hdr *s = &h->un._sun3x;
165
166	off = 0;
167	for (rsp = s->ram_segs; rsp->size; rsp++) {
168		if (pa >= rsp->start && pa < rsp->start + rsp->size) {
169			pa -= rsp->start;
170			break;
171		}
172		off += rsp->size;
173	}
174	return (kd->dump_off + off + pa);
175}
176
177