kvm_m68k.c revision 1.6 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.6 1996/03/16 10:23:51 leo 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
53 #include <sys/core.h>
54 #include <sys/exec_aout.h>
55 #include <sys/kcore.h>
56
57 #include <unistd.h>
58 #include <limits.h>
59 #include <nlist.h>
60 #include <kvm.h>
61
62 #include <vm/vm.h>
63 #include <vm/vm_param.h>
64
65 #include <db.h>
66
67 #include "kvm_private.h"
68
69 #include <machine/pte.h>
70 #include <machine/kcore.h>
71
72 #ifndef btop
73 #define btop(x) (((unsigned)(x)) >> PGSHIFT) /* XXX */
74 #define ptob(x) ((caddr_t)((x) << PGSHIFT)) /* XXX */
75 #endif
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 return (0);
93 }
94
95 static int
96 _kvm_vatop(kd, sta, va, pa)
97 kvm_t *kd;
98 st_entry_t *sta;
99 u_long va;
100 u_long *pa;
101 {
102 register cpu_kcore_hdr_t *cpu_kh;
103 register u_long addr;
104 int p, ste, pte;
105 int offset;
106
107 if (ISALIVE(kd)) {
108 _kvm_err(kd, 0, "vatop called in live kernel!");
109 return((off_t)0);
110 }
111 offset = va & PGOFSET;
112 cpu_kh = kd->cpu_hdr;
113 /*
114 * If we are initializing (kernel segment table pointer not yet set)
115 * then return pa == va to avoid infinite recursion.
116 */
117 if (cpu_kh->sysseg_pa == 0) {
118 *pa = va + cpu_kh->kernel_pa;
119 return (NBPG - offset);
120 }
121 if (cpu_kh->mmutype == -2) {
122 st_entry_t *sta2;
123
124 addr = (u_long)&sta[va >> SG4_SHIFT1];
125 /*
126 * Can't use KREAD to read kernel segment table entries.
127 * Fortunately it is 1-to-1 mapped so we don't have to.
128 */
129 if (sta == cpu_kh->sysseg_pa) {
130 if (lseek(kd->pmfd, _kvm_pa2off(kd, addr), 0) == -1 ||
131 read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
132 goto invalid;
133 } else if (KREAD(kd, addr, &ste))
134 goto invalid;
135 if ((ste & SG_V) == 0) {
136 _kvm_err(kd, 0, "invalid level 1 descriptor (%x)",
137 ste);
138 return((off_t)0);
139 }
140 sta2 = (st_entry_t *)(ste & SG4_ADDR1);
141 addr = (u_long)&sta2[(va & SG4_MASK2) >> SG4_SHIFT2];
142 /*
143 * Address from level 1 STE is a physical address,
144 * so don't use kvm_read.
145 */
146 if (lseek(kd->pmfd, _kvm_pa2off(kd, addr), 0) == -1 ||
147 read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
148 goto invalid;
149 if ((ste & SG_V) == 0) {
150 _kvm_err(kd, 0, "invalid level 2 descriptor (%x)",
151 ste);
152 return((off_t)0);
153 }
154 sta2 = (st_entry_t *)(ste & SG4_ADDR2);
155 addr = (u_long)&sta2[(va & SG4_MASK3) >> SG4_SHIFT3];
156 } else {
157 addr = (u_long)&sta[va >> SEGSHIFT];
158 /*
159 * Can't use KREAD to read kernel segment table entries.
160 * Fortunately it is 1-to-1 mapped so we don't have to.
161 */
162 if (sta == cpu_kh->sysseg_pa) {
163 if (lseek(kd->pmfd, _kvm_pa2off(kd, addr), 0) == -1 ||
164 read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
165 goto invalid;
166 } else if (KREAD(kd, addr, &ste))
167 goto invalid;
168 if ((ste & SG_V) == 0) {
169 _kvm_err(kd, 0, "invalid segment (%x)", ste);
170 return((off_t)0);
171 }
172 p = btop(va & SG_PMASK);
173 addr = (ste & SG_FRAME) + (p * sizeof(pt_entry_t));
174 }
175 /*
176 * Address from STE is a physical address so don't use kvm_read.
177 */
178 if (lseek(kd->pmfd, _kvm_pa2off(kd, addr), 0) == -1 ||
179 read(kd->pmfd, (char *)&pte, sizeof(pte)) < 0)
180 goto invalid;
181 addr = pte & PG_FRAME;
182 if (pte == PG_NV) {
183 _kvm_err(kd, 0, "page not valid");
184 return (0);
185 }
186 *pa = addr + offset;
187
188 return (NBPG - offset);
189 invalid:
190 _kvm_err(kd, 0, "invalid address (%x)", va);
191 return (0);
192 }
193
194 int
195 _kvm_kvatop(kd, va, pa)
196 kvm_t *kd;
197 u_long va;
198 u_long *pa;
199 {
200 return (_kvm_vatop(kd, (u_long)kd->cpu_hdr->sysseg_pa, va, pa));
201 }
202