xref: /netbsd-src/lib/libkvm/kvm_sparc.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
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 	struct regmap regmap[NKREG];
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 XXX
131 	if (kd->vmst == 0) {
132 		kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
133 		if (kd->vmst == 0)
134 			return (-1);
135 		kd->vmst->pmeg = (int *)_kvm_malloc(kd,
136 		    NPMEG * nptesg * sizeof(int));
137 		if (kd->vmst->pmeg == 0) {
138 			free(kd->vmst);
139 			kd->vmst = 0;
140 			return (-1);
141 		}
142 	}
143 
144 	if (fstat(kd->pmfd, &st) < 0)
145 		return (-1);
146 	/*
147 	 * Read segment table.
148 	 */
149 	off = st.st_size - roundup(sizeof(vm->regmap), kd->nbpg);
150 	errno = 0;
151 	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
152 	    read(kd->pmfd, (char *)vm->regmap, sizeof(vm->regmap)) < 0) {
153 		_kvm_err(kd, kd->program, "cannot read segment map");
154 		return (-1);
155 	}
156 	/*
157 	 * Read PMEGs.
158 	 */
159 	off = st.st_size - roundup(NPMEG * nptesg * sizeof(int), kd->nbpg) +
160 	    ((sizeof(vm->regmap) + kd->nbpg - 1) >> pgshift);
161 	errno = 0;
162 	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
163 	    read(kd->pmfd, (char *)vm->pmeg, NPMEG * nptesg * sizeof(int)) < 0) {
164 		_kvm_err(kd, kd->program, "cannot read PMEG table");
165 		return (-1);
166 	}
167 	/*
168 	 * Make pmap_stod be an identity map so we can bootstrap it in.
169 	 * We assume it's in the first contiguous chunk of physical memory.
170 	 */
171 	for (i = 0; i < BTSIZE; ++i)
172 		vm->pmap_stod[i] = i << 4;
173 
174 	/*
175 	 * It's okay to do this nlist separately from the one kvm_getprocs()
176 	 * does, since the only time we could gain anything by combining
177 	 * them is if we do a kvm_getprocs() on a dead kernel, which is
178 	 * not too common.
179 	 */
180 	nlist[0].n_name = "_pmap_stod";
181 	nlist[1].n_name = 0;
182 	(void)kvm_nlist(kd, nlist);
183 
184 	/*
185 	 * a kernel compiled only for the sun4 will not contain the symbol
186 	 * pmap_stod. Instead, we are happy to use the identity map
187 	 * initialized earlier.
188 	 * If we are not a sun4, the lack of this symbol is fatal.
189 	 */
190 	if (nlist[0].n_value != 0) {
191 		if (kvm_read(kd, (u_long)nlist[0].n_value,
192 		    (char *)vm->pmap_stod, sizeof(vm->pmap_stod))
193 		    != sizeof(vm->pmap_stod)) {
194 			_kvm_err(kd, kd->program, "cannot read pmap_stod");
195 			return (-1);
196 		}
197 	} else {
198 		if (cputyp != CPU_SUN4) {
199 			_kvm_err(kd, kd->program, "pmap_stod: no such symbol");
200 			return (-1);
201 		}
202 	}
203 #endif
204 
205 	return (0);
206 }
207 
208 #define VA_OFF(va) (va & (kd->nbpg - 1))
209 
210 /*
211  * Translate a kernel virtual address to a physical address using the
212  * mapping information in kd->vm.  Returns the result in pa, and returns
213  * the number of bytes that are contiguously available from this
214  * physical address.  This routine is used only for crashdumps.
215  */
216 int
217 _kvm_kvatop(kd, va, pa)
218 	kvm_t *kd;
219 	u_long va;
220 	u_long *pa;
221 {
222 #if XXX
223 	register struct vmstate *vm;
224 	register int s;
225 	register int pte;
226 	register int off;
227 
228 	_kvm_mustinit(kd);
229 
230 	if (va >= KERNBASE) {
231 		vm = kd->vmst;
232 		s = vm->regmap[VA_VREG(va) - NUREG];
233 		pte = vm->pmeg[VA_VPG(va) + nptesg * s];
234 		if ((pte & PG_V) != 0) {
235 			off = VA_OFF(va);
236 			*pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM)
237 			       << pgshift) | off;
238 
239 			return (kd->nbpg - off);
240 		}
241 	}
242 #endif
243 	_kvm_err(kd, 0, "invalid address (%x)", va);
244 	return (0);
245 }
246 
247 #if 0
248 static int
249 getcputyp()
250 {
251 	int mib[2];
252 	size_t size;
253 
254 	mib[0] = CTL_HW;
255 	mib[1] = HW_CLASS;
256 	size = sizeof cputyp;
257 	if (sysctl(mib, 2, &cputyp, &size, NULL, 0) == -1)
258 		return (-1);
259 }
260 #endif
261