xref: /netbsd-src/lib/libkvm/kvm_sun3x.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: kvm_sun3x.c,v 1.7 2000/10/10 20:44:17 he 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 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
43 #else
44 __RCSID("$NetBSD: kvm_sun3x.c,v 1.7 2000/10/10 20:44:17 he Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47 
48 /*
49  * Sun3x machine dependent routines for kvm.
50  *
51  * Note: This file has to build on ALL m68k machines,
52  * so do NOT include any <machine / *.h> files here.
53  */
54 
55 #include <sys/types.h>
56 #include <sys/kcore.h>
57 
58 #include <unistd.h>
59 #include <limits.h>
60 #include <nlist.h>
61 #include <kvm.h>
62 #include <db.h>
63 
64 #include <m68k/kcore.h>
65 
66 #include "kvm_private.h"
67 #include "kvm_m68k.h"
68 
69 int   _kvm_sun3x_initvtop __P((kvm_t *));
70 void  _kvm_sun3x_freevtop __P((kvm_t *));
71 int	  _kvm_sun3x_kvatop   __P((kvm_t *, u_long, u_long *));
72 off_t _kvm_sun3x_pa2off   __P((kvm_t *, u_long));
73 
74 struct kvm_ops _kvm_ops_sun3x = {
75 	_kvm_sun3x_initvtop,
76 	_kvm_sun3x_freevtop,
77 	_kvm_sun3x_kvatop,
78 	_kvm_sun3x_pa2off };
79 
80 #define	_kvm_kvas_size(h)	\
81 	(-((h)->kernbase))
82 #define	_kvm_nkptes(h, v)	\
83 	(_kvm_kvas_size((h)) >> (v)->pgshift)
84 #define	_kvm_pg_pa(pte, h)	\
85 	((pte) & (h)->pg_frame)
86 
87 /*
88  * Prepare for translation of kernel virtual addresses into offsets
89  * into crash dump files.  Nothing to do here.
90  */
91 int
92 _kvm_sun3x_initvtop(kd)
93 	kvm_t *kd;
94 {
95 	return (0);
96 }
97 
98 void
99 _kvm_sun3x_freevtop(kd)
100 	kvm_t *kd;
101 {
102 }
103 
104 /*
105  * Translate a kernel virtual address to a physical address using the
106  * mapping information in kd->vm.  Returns the result in pa, and returns
107  * the number of bytes that are contiguously available from this
108  * physical address.  This routine is used only for crashdumps.
109  */
110 int
111 _kvm_sun3x_kvatop(kd, va, pap)
112 	kvm_t *kd;
113 	u_long va;
114 	u_long *pap;
115 {
116 	cpu_kcore_hdr_t *h = kd->cpu_data;
117 	struct sun3x_kcore_hdr *s = &h->un._sun3x;
118 	struct vmstate *v = kd->vmst;
119 	int idx, len, offset, pte;
120 	u_long pteva, pa;
121 
122 	if (ISALIVE(kd)) {
123 		_kvm_err(kd, 0, "vatop called in live kernel!");
124 		return(0);
125 	}
126 
127 	if (va < h->kernbase) {
128 		_kvm_err(kd, 0, "not a kernel address");
129 		return(0);
130 	}
131 
132 	/*
133 	 * If this VA is in the contiguous range, short-cut.
134 	 * Note that this ends our recursion when we call
135 	 * kvm_read to access the kernel page table, which
136 	 * is guaranteed to be in the contiguous range.
137 	 */
138 	if (va < s->contig_end) {
139 		len = s->contig_end - va;
140 		pa = va - h->kernbase;
141 		goto done;
142 	}
143 
144 	/*
145 	 * The KVA is beyond the contiguous range, so we must
146 	 * read the PTE for this KVA from the page table.
147 	 */
148 	idx = ((va - h->kernbase) >> v->pgshift);
149 	pteva = s->kernCbase + (idx * 4);
150 	if (kvm_read(kd, pteva, &pte, 4) != 4) {
151 		_kvm_err(kd, 0, "can not read PTE!");
152 		return (0);
153 	}
154 	if ((pte & s->pg_valid) == 0) {
155 		_kvm_err(kd, 0, "page not valid (VA=0x%lx)", va);
156 		return (0);
157 	}
158 	offset = va & v->pgofset;
159 	len = (h->page_size - offset);
160 	pa = _kvm_pg_pa(pte, s) + offset;
161 
162 done:
163 	*pap = pa;
164 	return (len);
165 }
166 
167 /*
168  * Translate a physical address to a file-offset in the crash-dump.
169  */
170 off_t
171 _kvm_sun3x_pa2off(kd, pa)
172 	kvm_t	*kd;
173 	u_long	pa;
174 {
175 	off_t		off;
176 	phys_ram_seg_t	*rsp;
177 	cpu_kcore_hdr_t *h = kd->cpu_data;
178 	struct sun3x_kcore_hdr *s = &h->un._sun3x;
179 
180 	off = 0;
181 	for (rsp = s->ram_segs; rsp->size; rsp++) {
182 		if (pa >= rsp->start && pa < rsp->start + rsp->size) {
183 			pa -= rsp->start;
184 			break;
185 		}
186 		off += rsp->size;
187 	}
188 	return (kd->dump_off + off + pa);
189 }
190 
191