xref: /original-bsd/lib/libkvm/kvm_sparc.c (revision c3e32dec)
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  * %sccs.include.redist.c%
10  */
11 
12 #if defined(LIBC_SCCS) && !defined(lint)
13 static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 06/04/93";
14 #endif /* LIBC_SCCS and not lint */
15 
16 /*
17  * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
18  * vm code will one day obsolete this module.
19  */
20 
21 #include <sys/param.h>
22 #include <sys/user.h>
23 #include <sys/proc.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <nlist.h>
27 #include <kvm.h>
28 
29 #include <vm/vm.h>
30 #include <vm/vm_param.h>
31 
32 #include <limits.h>
33 #include <db.h>
34 
35 #include "kvm_private.h"
36 
37 #define NPMEG 128
38 
39 /* XXX from sparc/pmap.c */
40 #define MAXMEM  (128 * 1024 * 1024)     /* no more than 128 MB phys mem */
41 #define NPGBANK 16                      /* 2^4 pages per bank (64K / bank) */
42 #define BSHIFT  4                       /* log2(NPGBANK) */
43 #define BOFFSET (NPGBANK - 1)
44 #define BTSIZE  (MAXMEM / NBPG / NPGBANK)
45 #define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET))
46 
47 struct vmstate {
48 	pmeg_t segmap[NKSEG];
49 	int pmeg[NPMEG][NPTESG];
50 	int pmap_stod[BTSIZE];              /* dense to sparse */
51 };
52 
53 void
54 _kvm_freevtop(kd)
55 	kvm_t *kd;
56 {
57 	if (kd->vmst != 0)
58 		free(kd->vmst);
59 }
60 
61 int
62 _kvm_initvtop(kd)
63 	kvm_t *kd;
64 {
65 	register int i;
66 	register int off;
67 	register struct vmstate *vm;
68 	struct stat st;
69 	struct nlist nlist[2];
70 
71 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
72 	if (vm == 0)
73 		return (-1);
74 
75 	kd->vmst = vm;
76 
77 	if (fstat(kd->pmfd, &st) < 0)
78 		return (-1);
79 	/*
80 	 * Read segment table.
81 	 */
82 	off = st.st_size - ctob(btoc(sizeof(vm->segmap)));
83 	errno = 0;
84 	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
85 	    read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) {
86 		_kvm_err(kd, kd->program, "cannot read segment map");
87 		return (-1);
88 	}
89 	/*
90 	 * Read PMEGs.
91 	 */
92 	off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) +
93 	    btoc(sizeof(vm->segmap)));
94 	errno = 0;
95 	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
96 	    read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) {
97 		_kvm_err(kd, kd->program, "cannot read PMEG table");
98 		return (-1);
99 	}
100 	/*
101 	 * Make pmap_stod be an identity map so we can bootstrap it in.
102 	 * We assume it's in the first contiguous chunk of physical memory.
103 	 */
104 	for (i = 0; i < BTSIZE; ++i)
105 		vm->pmap_stod[i] = i << 4;
106 
107 	/*
108 	 * It's okay to do this nlist separately from the one kvm_getprocs()
109 	 * does, since the only time we could gain anything by combining
110 	 * them is if we do a kvm_getprocs() on a dead kernel, which is
111 	 * not too common.
112 	 */
113 	nlist[0].n_name = "_pmap_stod";
114 	nlist[1].n_name = 0;
115 	if (kvm_nlist(kd, nlist) != 0) {
116 		_kvm_err(kd, kd->program, "pmap_stod: no such symbol");
117 		return (-1);
118 	}
119 	if (kvm_read(kd, (u_long)nlist[0].n_value,
120 		     (char *)vm->pmap_stod, sizeof(vm->pmap_stod))
121 	    != sizeof(vm->pmap_stod)) {
122 		_kvm_err(kd, kd->program, "cannot read pmap_stod");
123 		return (-1);
124 	}
125 	return (0);
126 }
127 
128 #define VA_OFF(va) (va & (NBPG - 1))
129 
130 /*
131  * Translate a user virtual address to a physical address.
132  */
133 int
134 _kvm_uvatop(kd, p, va, pa)
135 	kvm_t *kd;
136 	const struct proc *p;
137 	u_long va;
138 	u_long *pa;
139 {
140 	int kva, pte;
141 	register int off, frame;
142 	register struct vmspace *vms = p->p_vmspace;
143 
144 	if ((u_long)vms < KERNBASE) {
145 		_kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc");
146 		return (0);
147 	}
148 	if (va >= KERNBASE)
149 		return (0);
150 	/*
151 	 * Get the PTE.  This takes two steps.  We read the
152 	 * base address of the table, then we index it.
153 	 * Note that the index pte table is indexed by
154 	 * virtual segment rather than physical segment.
155 	 */
156 	kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)];
157 	if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0)
158 		goto invalid;
159 	kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va);
160 	if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) {
161 		off = VA_OFF(va);
162 		/*
163 		 * /dev/mem adheres to the hardware model of physical memory
164 		 * (with holes in the address space), while crashdumps
165 		 * adhere to the contiguous software model.
166 		 */
167 		if (ISALIVE(kd))
168 			frame = pte & PG_PFNUM;
169 		else
170 			frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM);
171 		*pa = (frame << PGSHIFT) | off;
172 		return (NBPG - off);
173 	}
174 invalid:
175 	_kvm_err(kd, 0, "invalid address (%x)", va);
176 	return (0);
177 }
178 
179 /*
180  * Translate a kernel virtual address to a physical address using the
181  * mapping information in kd->vm.  Returns the result in pa, and returns
182  * the number of bytes that are contiguously available from this
183  * physical address.  This routine is used only for crashdumps.
184  */
185 int
186 _kvm_kvatop(kd, va, pa)
187 	kvm_t *kd;
188 	u_long va;
189 	u_long *pa;
190 {
191 	register struct vmstate *vm;
192 	register int s;
193 	register int pte;
194 	register int off;
195 
196 	if (va >= KERNBASE) {
197 		vm = kd->vmst;
198 		s = vm->segmap[VA_VSEG(va) - NUSEG];
199 		pte = vm->pmeg[s][VA_VPG(va)];
200 		if ((pte & PG_V) != 0) {
201 			off = VA_OFF(va);
202 			*pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM)
203 			       << PGSHIFT) | off;
204 
205 			return (NBPG - off);
206 		}
207 	}
208 	_kvm_err(kd, 0, "invalid address (%x)", va);
209 	return (0);
210 }
211