xref: /netbsd-src/lib/libkvm/kvm_sparc.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software developed by the Computer Systems
6  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7  * BG 91-66 and contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
40 #endif /* LIBC_SCCS and not lint */
41 
42 /*
43  * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
44  * vm code will one day obsolete this module.
45  */
46 
47 #include <sys/param.h>
48 #include <sys/user.h>
49 #include <sys/proc.h>
50 #include <sys/stat.h>
51 #include <sys/sysctl.h>
52 #include <unistd.h>
53 #include <nlist.h>
54 #include <kvm.h>
55 
56 #include <vm/vm.h>
57 #include <vm/vm_param.h>
58 
59 #include <limits.h>
60 #include <db.h>
61 
62 #include "kvm_private.h"
63 
64 #define NPMEG 128
65 
66 /* XXX from sparc/pmap.c */
67 #define MAXMEM  (128 * 1024 * 1024)     /* no more than 128 MB phys mem */
68 #define NPGBANK 16                      /* 2^4 pages per bank (64K / bank) */
69 #define BSHIFT  4                       /* log2(NPGBANK) */
70 #define BOFFSET (NPGBANK - 1)
71 #define BTSIZE  (MAXMEM / 4096 / NPGBANK)
72 #define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET))
73 
74 struct vmstate {
75 	pmeg_t segmap[NKSEG];
76 	int *pmeg;
77 	int pmap_stod[BTSIZE];              /* dense to sparse */
78 };
79 
80 static int cputyp = -1;
81 
82 static int pgshift, nptesg;
83 
84 #define VA_VPG(va)	(cputyp==CPU_SUN4C ? VA_SUN4C_VPG(va) : VA_SUN4_VPG(va))
85 
86 static void
87 _kvm_mustinit(kd)
88 	kvm_t *kd;
89 {
90 	if (cputyp != -1)
91 		return;
92 	for (pgshift = 12; (1 << pgshift) != kd->nbpg; pgshift++)
93 		;
94 	nptesg = NBPSG / kd->nbpg;
95 
96 #if 1
97 	if (cputyp == -1) {
98 		if (kd->nbpg == 8192)
99 			cputyp = CPU_SUN4;
100 		else
101 			cputyp = CPU_SUN4C;
102 	}
103 #endif
104 }
105 
106 void
107 _kvm_freevtop(kd)
108 	kvm_t *kd;
109 {
110 	if (kd->vmst != 0) {
111 		if (kd->vmst->pmeg != 0)
112 			free(kd->vmst->pmeg);
113 		free(kd->vmst);
114 		kd->vmst  = 0;
115 	}
116 }
117 
118 int
119 _kvm_initvtop(kd)
120 	kvm_t *kd;
121 {
122 	register int i;
123 	register int off;
124 	register struct vmstate *vm;
125 	struct stat st;
126 	struct nlist nlist[2];
127 
128 	_kvm_mustinit(kd);
129 
130 	if (kd->vmst == 0) {
131 		kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
132 		if (kd->vmst == 0)
133 			return (-1);
134 		kd->vmst->pmeg = (int *)_kvm_malloc(kd,
135 		    NPMEG * nptesg * sizeof(int));
136 		if (kd->vmst->pmeg == 0) {
137 			free(kd->vmst);
138 			kd->vmst = 0;
139 			return (-1);
140 		}
141 	}
142 
143 	if (fstat(kd->pmfd, &st) < 0)
144 		return (-1);
145 	/*
146 	 * Read segment table.
147 	 */
148 	off = st.st_size - roundup(sizeof(vm->segmap), kd->nbpg);
149 	errno = 0;
150 	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
151 	    read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) {
152 		_kvm_err(kd, kd->program, "cannot read segment map");
153 		return (-1);
154 	}
155 	/*
156 	 * Read PMEGs.
157 	 */
158 	off = st.st_size - roundup(NPMEG * nptesg * sizeof(int), kd->nbpg) +
159 	    ((sizeof(vm->segmap) + kd->nbpg - 1) >> pgshift);
160 	errno = 0;
161 	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
162 	    read(kd->pmfd, (char *)vm->pmeg, NPMEG * nptesg * sizeof(int)) < 0) {
163 		_kvm_err(kd, kd->program, "cannot read PMEG table");
164 		return (-1);
165 	}
166 	/*
167 	 * Make pmap_stod be an identity map so we can bootstrap it in.
168 	 * We assume it's in the first contiguous chunk of physical memory.
169 	 */
170 	for (i = 0; i < BTSIZE; ++i)
171 		vm->pmap_stod[i] = i << 4;
172 
173 	/*
174 	 * It's okay to do this nlist separately from the one kvm_getprocs()
175 	 * does, since the only time we could gain anything by combining
176 	 * them is if we do a kvm_getprocs() on a dead kernel, which is
177 	 * not too common.
178 	 */
179 	nlist[0].n_name = "_pmap_stod";
180 	nlist[1].n_name = 0;
181 	(void)kvm_nlist(kd, nlist);
182 
183 	/*
184 	 * a kernel compiled only for the sun4 will not contain the symbol
185 	 * pmap_stod. Instead, we are happy to use the identity map
186 	 * initialized earlier.
187 	 * If we are not a sun4, the lack of this symbol is fatal.
188 	 */
189 	if (nlist[0].n_value != 0) {
190 		if (kvm_read(kd, (u_long)nlist[0].n_value,
191 		    (char *)vm->pmap_stod, sizeof(vm->pmap_stod))
192 		    != sizeof(vm->pmap_stod)) {
193 			_kvm_err(kd, kd->program, "cannot read pmap_stod");
194 			return (-1);
195 		}
196 	} else {
197 		if (cputyp != CPU_SUN4) {
198 			_kvm_err(kd, kd->program, "pmap_stod: no such symbol");
199 			return (-1);
200 		}
201 	}
202 
203 	return (0);
204 }
205 
206 #define VA_OFF(va) (va & (kd->nbpg - 1))
207 
208 /*
209  * Translate a kernel virtual address to a physical address using the
210  * mapping information in kd->vm.  Returns the result in pa, and returns
211  * the number of bytes that are contiguously available from this
212  * physical address.  This routine is used only for crashdumps.
213  */
214 int
215 _kvm_kvatop(kd, va, pa)
216 	kvm_t *kd;
217 	u_long va;
218 	u_long *pa;
219 {
220 	register struct vmstate *vm;
221 	register int s;
222 	register int pte;
223 	register int off;
224 
225 	_kvm_mustinit(kd);
226 
227 	if (va >= KERNBASE) {
228 		vm = kd->vmst;
229 		s = vm->segmap[VA_VSEG(va) - NUSEG];
230 		pte = vm->pmeg[VA_VPG(va) + nptesg * s];
231 		if ((pte & PG_V) != 0) {
232 			off = VA_OFF(va);
233 			*pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM)
234 			       << pgshift) | off;
235 
236 			return (kd->nbpg - off);
237 		}
238 	}
239 	_kvm_err(kd, 0, "invalid address (%x)", va);
240 	return (0);
241 }
242 
243 #if 0
244 static int
245 getcputyp()
246 {
247 	int mib[2];
248 	size_t size;
249 
250 	mib[0] = CTL_HW;
251 	mib[1] = HW_CLASS;
252 	size = sizeof cputyp;
253 	if (sysctl(mib, 2, &cputyp, &size, NULL, 0) == -1)
254 		return (-1);
255 }
256 #endif
257