xref: /netbsd-src/lib/libkvm/kvm_sparc64.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: kvm_sparc64.c,v 1.9 2003/05/16 10:24:56 wiz 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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 #if defined(LIBC_SCCS) && !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
44 #else
45 __RCSID("$NetBSD: kvm_sparc64.c,v 1.9 2003/05/16 10:24:56 wiz Exp $");
46 #endif
47 #endif /* LIBC_SCCS and not lint */
48 
49 /*
50  * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
51  * vm code will one day obsolete this module.
52  */
53 
54 #include <sys/param.h>
55 #include <sys/exec.h>
56 #include <sys/user.h>
57 #include <sys/proc.h>
58 #include <sys/stat.h>
59 #include <sys/core.h>
60 #include <sys/kcore.h>
61 #include <unistd.h>
62 #include <nlist.h>
63 #include <kvm.h>
64 
65 #include <uvm/uvm_extern.h>
66 
67 #include <machine/pmap.h>
68 #include <machine/kcore.h>
69 #include <machine/vmparam.h>
70 
71 #include <limits.h>
72 #include <db.h>
73 
74 #include "kvm_private.h"
75 
76 int _kvm_kvatop __P((kvm_t *, u_long, u_long *));
77 
78 void
79 _kvm_freevtop(kd)
80 	kvm_t *kd;
81 {
82 	if (kd->vmst != 0) {
83 		_kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
84 		kd->vmst = 0;
85 	}
86 }
87 
88 /*
89  * Prepare for translation of kernel virtual addresses into offsets
90  * into crash dump files. We use the MMU specific goop written at the
91  * front of the crash dump by pmap_dumpmmu().
92  *
93  * We should read in and cache the ksegs here to speed up operations...
94  */
95 int
96 _kvm_initvtop(kd)
97 	kvm_t *kd;
98 {
99 	kd->nbpg = 0x2000;
100 
101 	return (0);
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 crash dumps.
109  */
110 int
111 _kvm_kvatop(kd, va, pa)
112 	kvm_t *kd;
113 	u_long va;
114 	u_long *pa;
115 {
116 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
117 	u_long kernbase = cpup->kernbase;
118 	uint64_t *pseg, *pdir, *ptbl;
119 	int64_t data;
120 
121 	if (va < kernbase)
122 		goto lose;
123 
124 	/* Handle the wired 4MB TTEs */
125 	if (va > cpup->ktextbase && va < (cpup->ktextbase + cpup->ktextsz)) {
126 		u_long vaddr;
127 
128 		vaddr = va - cpup->ktextbase;
129 		*pa = cpup->ktextp + vaddr;
130 		return (cpup->ktextsz - vaddr);
131 	}
132 
133 	if (va > cpup->kdatabase && va < (cpup->kdatabase + cpup->kdatasz)) {
134 		u_long vaddr;
135 
136 		vaddr = va - cpup->kdatabase;
137 		*pa = cpup->kdatap + vaddr;
138 		return (cpup->kdatasz - vaddr);
139 	}
140 
141 
142 	/*
143 	 * Parse kernel page table.
144 	 */
145 	pseg = (uint64_t *)(u_long)cpup->segmapoffset;
146 	if (pread(kd->pmfd, &pdir, sizeof(pdir),
147 		_kvm_pa2off(kd, (u_long)&pseg[va_to_seg(va)]))
148 		!= sizeof(pdir)) {
149 		_kvm_syserr(kd, 0, "could not read L1 PTE");
150 		goto lose;
151 	}
152 
153 	if (!pdir) {
154 		_kvm_err(kd, 0, "invalid L1 PTE");
155 		goto lose;
156 	}
157 
158 	if (pread(kd->pmfd, &ptbl, sizeof(ptbl),
159 		_kvm_pa2off(kd, (u_long)&pdir[va_to_dir(va)]))
160 		!= sizeof(ptbl)) {
161 		_kvm_syserr(kd, 0, "could not read L2 PTE");
162 		goto lose;
163 	}
164 
165 	if (!ptbl) {
166 		_kvm_err(kd, 0, "invalid L2 PTE");
167 		goto lose;
168 	}
169 
170 	if (pread(kd->pmfd, &data, sizeof(data),
171 		_kvm_pa2off(kd, (u_long)&ptbl[va_to_pte(va)]))
172 		!= sizeof(data)) {
173 		_kvm_syserr(kd, 0, "could not read TTE");
174 		goto lose;
175 	}
176 
177 	if (data >= 0) {
178 		_kvm_err(kd, 0, "invalid L2 TTE");
179 		goto lose;
180 	}
181 
182 	/*
183 	 * Calculate page offsets and things.
184 	 *
185 	 * XXXX -- We could support multiple page sizes.
186 	 */
187 	va = va & (kd->nbpg - 1);
188 	data &= TLB_PA_MASK;
189 	*pa = data + va;
190 
191 	/*
192 	 * Parse and trnslate our TTE.
193 	 */
194 
195 	return (kd->nbpg - va);
196 
197 lose:
198 	*pa = -1;
199 	_kvm_err(kd, 0, "invalid address (%lx)", va);
200 	return (0);
201 }
202 
203 
204 /*
205  * Translate a physical address to a file-offset in the crash dump.
206  */
207 off_t
208 _kvm_pa2off(kd, pa)
209 	kvm_t   *kd;
210 	u_long  pa;
211 {
212 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
213 	phys_ram_seg_t *mp;
214 	off_t off;
215 	int nmem;
216 
217 	/*
218 	 * Layout of CPU segment:
219 	 *	cpu_kcore_hdr_t;
220 	 *	[alignment]
221 	 *	phys_ram_seg_t[cpup->nmemseg];
222 	 */
223 	mp = (phys_ram_seg_t *)((long)kd->cpu_data + cpup->memsegoffset);
224 	off = 0;
225 
226 	/* Translate (sparse) pfnum to (packed) dump offset */
227 	for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
228 		if (mp->start <= pa && pa < mp->start + mp->size)
229 			break;
230 		off += mp->size;
231 	}
232 	if (nmem < 0) {
233 		_kvm_err(kd, 0, "invalid address (%lx)", pa);
234 		return (-1);
235 	}
236 
237 	return (kd->dump_off + off + pa - mp->start);
238 }
239 
240 /*
241  * Machine-dependent initialization for ALL open kvm descriptors,
242  * not just those for a kernel crash dump.  Some architectures
243  * have to deal with these NOT being constants!  (i.e. m68k)
244  */
245 int
246 _kvm_mdopen(kd)
247 	kvm_t	*kd;
248 {
249 	u_long max_uva;
250 	extern struct ps_strings *__ps_strings;
251 
252 	max_uva = (u_long) (__ps_strings + 1);
253 	kd->usrstack = max_uva;
254 	kd->max_uva  = max_uva;
255 	kd->min_uva  = 0;
256 
257 	return (0);
258 }
259