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