xref: /netbsd/lib/libkvm/kvm_sparc64.c (revision bf9ec67e)
1 /*	$NetBSD: kvm_sparc64.c,v 1.7 2001/08/05 03:33:15 matt 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.7 2001/08/05 03:33:15 matt 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 
70 #include <limits.h>
71 #include <db.h>
72 
73 #include "kvm_private.h"
74 
75 int _kvm_kvatop __P((kvm_t *, u_long, u_long *));
76 
77 void
78 _kvm_freevtop(kd)
79 	kvm_t *kd;
80 {
81 	if (kd->vmst != 0) {
82 		_kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
83 		kd->vmst = 0;
84 	}
85 }
86 
87 /*
88  * Prepare for translation of kernel virtual addresses into offsets
89  * into crash dump files. We use the MMU specific goop written at the
90  * front of the crash dump by pmap_dumpmmu().
91  *
92  * We should read in and cache the ksegs here to speed up operations...
93  */
94 int
95 _kvm_initvtop(kd)
96 	kvm_t *kd;
97 {
98 	kd->nbpg = 0x2000;
99 
100 	return (0);
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_kvatop(kd, va, pa)
111 	kvm_t *kd;
112 	u_long va;
113 	u_long *pa;
114 {
115 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
116 	u_long kernbase = cpup->kernbase;
117 	uint64_t *pseg, *pdir, *ptbl;
118 	int64_t data;
119 
120 	if (va < kernbase)
121 		goto lose;
122 
123 	/* Handle the wired 4MB TTEs */
124 	if (va > cpup->ktextbase && va < (cpup->ktextbase + cpup->ktextsz)) {
125 		u_long vaddr;
126 
127 		vaddr = va - cpup->ktextbase;
128 		*pa = cpup->ktextp + vaddr;
129 		return (cpup->ktextsz - vaddr);
130 	}
131 
132 	if (va > cpup->kdatabase && va < (cpup->kdatabase + cpup->kdatasz)) {
133 		u_long vaddr;
134 
135 		vaddr = va - cpup->kdatabase;
136 		*pa = cpup->kdatap + vaddr;
137 		return (cpup->kdatasz - vaddr);
138 	}
139 
140 
141 	/*
142 	 * Parse kernel page table.
143 	 */
144 	pseg = (uint64_t *)(u_long)cpup->segmapoffset;
145 	if (pread(kd->pmfd, &pdir, sizeof(pdir),
146 		_kvm_pa2off(kd, (u_long)&pseg[va_to_seg(va)]))
147 		!= sizeof(pdir)) {
148 		_kvm_syserr(kd, 0, "could not read L1 PTE");
149 		goto lose;
150 	}
151 
152 	if (!pdir) {
153 		_kvm_err(kd, 0, "invalid L1 PTE");
154 		goto lose;
155 	}
156 
157 	if (pread(kd->pmfd, &ptbl, sizeof(ptbl),
158 		_kvm_pa2off(kd, (u_long)&pdir[va_to_dir(va)]))
159 		!= sizeof(ptbl)) {
160 		_kvm_syserr(kd, 0, "could not read L2 PTE");
161 		goto lose;
162 	}
163 
164 	if (!ptbl) {
165 		_kvm_err(kd, 0, "invalid L2 PTE");
166 		goto lose;
167 	}
168 
169 	if (pread(kd->pmfd, &data, sizeof(data),
170 		_kvm_pa2off(kd, (u_long)&ptbl[va_to_pte(va)]))
171 		!= sizeof(data)) {
172 		_kvm_syserr(kd, 0, "could not read TTE");
173 		goto lose;
174 	}
175 
176 	if (data >= 0) {
177 		_kvm_err(kd, 0, "invalid L2 TTE");
178 		goto lose;
179 	}
180 
181 	/*
182 	 * Calculate page offsets and things.
183 	 *
184 	 * XXXX -- We could support multiple page sizes.
185 	 */
186 	va = va & (kd->nbpg - 1);
187 	data &= TLB_PA_MASK;
188 	*pa = data + va;
189 
190 	/*
191 	 * Parse and trnslate our TTE.
192 	 */
193 
194 	return (kd->nbpg - va);
195 
196 lose:
197 	*pa = -1;
198 	_kvm_err(kd, 0, "invalid address (%lx)", va);
199 	return (0);
200 }
201 
202 
203 /*
204  * Translate a physical address to a file-offset in the crash-dump.
205  */
206 off_t
207 _kvm_pa2off(kd, pa)
208 	kvm_t   *kd;
209 	u_long  pa;
210 {
211 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
212 	phys_ram_seg_t *mp;
213 	off_t off;
214 	int nmem;
215 
216 	/*
217 	 * Layout of CPU segment:
218 	 *	cpu_kcore_hdr_t;
219 	 *	[alignment]
220 	 *	phys_ram_seg_t[cpup->nmemseg];
221 	 */
222 	mp = (phys_ram_seg_t *)((long)kd->cpu_data + cpup->memsegoffset);
223 	off = 0;
224 
225 	/* Translate (sparse) pfnum to (packed) dump offset */
226 	for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
227 		if (mp->start <= pa && pa < mp->start + mp->size)
228 			break;
229 		off += mp->size;
230 	}
231 	if (nmem < 0) {
232 		_kvm_err(kd, 0, "invalid address (%lx)", pa);
233 		return (-1);
234 	}
235 
236 	return (kd->dump_off + off + pa - mp->start);
237 }
238 
239 /*
240  * Machine-dependent initialization for ALL open kvm descriptors,
241  * not just those for a kernel crash dump.  Some architectures
242  * have to deal with these NOT being constants!  (i.e. m68k)
243  */
244 int
245 _kvm_mdopen(kd)
246 	kvm_t	*kd;
247 {
248 	u_long max_uva;
249 	extern struct ps_strings *__ps_strings;
250 
251 	max_uva = (u_long) (__ps_strings + 1);
252 	kd->usrstack = max_uva;
253 	kd->max_uva  = max_uva;
254 	kd->min_uva  = 0;
255 
256 	return (0);
257 }
258