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