kvm_m68k.c revision 1.1 1 /*-
2 * Copyright (c) 1989, 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 /* from: static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"; */
40 static char *rcsid = "$Id: kvm_m68k.c,v 1.1 1994/05/09 04:09:24 cgd Exp $";
41 #endif /* LIBC_SCCS and not lint */
42
43 /*
44 * m68k machine dependent routines for kvm. Hopefully, the forthcoming
45 * vm code will one day obsolete this module.
46 */
47
48 #include <sys/param.h>
49 #include <sys/user.h>
50 #include <sys/proc.h>
51 #include <sys/stat.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 #include <machine/pte.h>
65
66 #ifndef btop
67 #define btop(x) (((unsigned)(x)) >> PGSHIFT) /* XXX */
68 #define ptob(x) ((caddr_t)((x) << PGSHIFT)) /* XXX */
69 #endif
70
71 struct vmstate {
72 u_long lowram;
73 int mmutype;
74 struct ste *Sysseg;
75 };
76
77 #define KREAD(kd, addr, p)\
78 (kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
79
80 void
81 _kvm_freevtop(kd)
82 kvm_t *kd;
83 {
84 if (kd->vmst != 0)
85 free(kd->vmst);
86 }
87
88 int
89 _kvm_initvtop(kd)
90 kvm_t *kd;
91 {
92 struct vmstate *vm;
93 struct nlist nlist[4];
94
95 vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
96 if (vm == 0)
97 return (-1);
98 kd->vmst = vm;
99
100 nlist[0].n_name = "_lowram";
101 nlist[1].n_name = "_mmutype";
102 nlist[2].n_name = "_Sysseg";
103 nlist[3].n_name = 0;
104
105 if (kvm_nlist(kd, nlist) != 0) {
106 _kvm_err(kd, kd->program, "bad namelist");
107 return (-1);
108 }
109 vm->Sysseg = 0;
110 if (KREAD(kd, (u_long)nlist[0].n_value, &vm->lowram)) {
111 _kvm_err(kd, kd->program, "cannot read lowram");
112 return (-1);
113 }
114 if (KREAD(kd, (u_long)nlist[1].n_value, &vm->mmutype)) {
115 _kvm_err(kd, kd->program, "cannot read mmutype");
116 return (-1);
117 }
118 if (KREAD(kd, (u_long)nlist[2].n_value, &vm->Sysseg)) {
119 _kvm_err(kd, kd->program, "cannot read segment table");
120 return (-1);
121 }
122 return (0);
123 }
124
125 static int
126 _kvm_vatop(kd, sta, va, pa)
127 kvm_t *kd;
128 struct ste *sta;
129 u_long va;
130 u_long *pa;
131 {
132 register struct vmstate *vm;
133 register u_long lowram;
134 register u_long addr;
135 int p, ste, pte;
136 int offset;
137
138 if (ISALIVE(kd)) {
139 _kvm_err(kd, 0, "vatop called in live kernel!");
140 return((off_t)0);
141 }
142 vm = kd->vmst;
143 offset = va & PGOFSET;
144 /*
145 * If we are initializing (kernel segment table pointer not yet set)
146 * then return pa == va to avoid infinite recursion.
147 */
148 if (vm->Sysseg == 0) {
149 *pa = va;
150 return (NBPG - offset);
151 }
152 lowram = vm->lowram;
153 if (vm->mmutype == -2) {
154 struct ste *sta2;
155
156 addr = (u_long)&sta[va >> SG4_SHIFT1];
157 /*
158 * Can't use KREAD to read kernel segment table entries.
159 * Fortunately it is 1-to-1 mapped so we don't have to.
160 */
161 if (sta == vm->Sysseg) {
162 if (lseek(kd->pmfd, (off_t)addr, 0) == -1 ||
163 read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
164 goto invalid;
165 } else if (KREAD(kd, addr, &ste))
166 goto invalid;
167 if ((ste & SG_V) == 0) {
168 _kvm_err(kd, 0, "invalid level 1 descriptor (%x)",
169 ste);
170 return((off_t)0);
171 }
172 sta2 = (struct ste *)(ste & SG4_ADDR1);
173 addr = (u_long)&sta2[(va & SG4_MASK2) >> SG4_SHIFT2];
174 /*
175 * Address from level 1 STE is a physical address,
176 * so don't use kvm_read.
177 */
178 if (lseek(kd->pmfd, (off_t)(addr - lowram), 0) == -1 ||
179 read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
180 goto invalid;
181 if ((ste & SG_V) == 0) {
182 _kvm_err(kd, 0, "invalid level 2 descriptor (%x)",
183 ste);
184 return((off_t)0);
185 }
186 sta2 = (struct ste *)(ste & SG4_ADDR2);
187 addr = (u_long)&sta2[(va & SG4_MASK3) >> SG4_SHIFT3];
188 } else {
189 addr = (u_long)&sta[va >> SEGSHIFT];
190 /*
191 * Can't use KREAD to read kernel segment table entries.
192 * Fortunately it is 1-to-1 mapped so we don't have to.
193 */
194 if (sta == vm->Sysseg) {
195 if (lseek(kd->pmfd, (off_t)addr, 0) == -1 ||
196 read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
197 goto invalid;
198 } else if (KREAD(kd, addr, &ste))
199 goto invalid;
200 if ((ste & SG_V) == 0) {
201 _kvm_err(kd, 0, "invalid segment (%x)", ste);
202 return((off_t)0);
203 }
204 p = btop(va & SG_PMASK);
205 addr = (ste & SG_FRAME) + (p * sizeof(struct pte));
206 }
207 /*
208 * Address from STE is a physical address so don't use kvm_read.
209 */
210 if (lseek(kd->pmfd, (off_t)(addr - lowram), 0) == -1 ||
211 read(kd->pmfd, (char *)&pte, sizeof(pte)) < 0)
212 goto invalid;
213 addr = pte & PG_FRAME;
214 if (pte == PG_NV) {
215 _kvm_err(kd, 0, "page not valid");
216 return (0);
217 }
218 *pa = addr - lowram + offset;
219
220 return (NBPG - offset);
221 invalid:
222 _kvm_err(kd, 0, "invalid address (%x)", va);
223 return (0);
224 }
225
226 int
227 _kvm_kvatop(kd, va, pa)
228 kvm_t *kd;
229 u_long va;
230 u_long *pa;
231 {
232 return (_kvm_vatop(kd, (u_long)kd->vmst->Sysseg, va, pa));
233 }
234
235 /*
236 * Translate a user virtual address to a physical address.
237 */
238 int
239 _kvm_uvatop(kd, p, va, pa)
240 kvm_t *kd;
241 const struct proc *p;
242 u_long va;
243 u_long *pa;
244 {
245 register struct vmspace *vms = p->p_vmspace;
246 int kva;
247
248 /*
249 * If this is a live kernel we just look it up in the kernel
250 * virtually allocated flat 4mb page table (i.e. let the kernel
251 * do the table walk). In this way, we avoid needing to know
252 * the MMU type.
253 */
254 if (ISALIVE(kd)) {
255 struct pte *ptab;
256 int pte, offset;
257
258 kva = (int)&vms->vm_pmap.pm_ptab;
259 if (KREAD(kd, kva, &ptab)) {
260 _kvm_err(kd, 0, "invalid address (%x)", va);
261 return (0);
262 }
263 kva = (int)&ptab[btop(va)];
264 if (KREAD(kd, kva, &pte) || (pte & PG_V) == 0) {
265 _kvm_err(kd, 0, "invalid address (%x)", va);
266 return (0);
267 }
268 offset = va & PGOFSET;
269 *pa = (pte & PG_FRAME) | offset;
270 return (NBPG - offset);
271 }
272 /*
273 * Otherwise, we just walk the table ourself.
274 */
275 kva = (int)&vms->vm_pmap.pm_stab;
276 if (KREAD(kd, kva, &kva)) {
277 _kvm_err(kd, 0, "invalid address (%x)", va);
278 return (0);
279 }
280 return (_kvm_vatop(kd, kva, va, pa));
281 }
282