xref: /netbsd-src/lib/libkvm/kvm_m68k.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: kvm_m68k.c,v 1.9 1996/05/07 06:09:11 leo Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software developed by the Computer Systems
8  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9  * BG 91-66 and contributed to Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
43 #else
44 static char *rcsid = "$NetBSD: kvm_m68k.c,v 1.9 1996/05/07 06:09:11 leo Exp $";
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47 
48 /*
49  * m68k machine dependent routines for kvm.  Hopefully, the forthcoming
50  * vm code will one day obsolete this module.
51  */
52 
53 #include <sys/param.h>
54 #include <sys/user.h>
55 #include <sys/proc.h>
56 #include <sys/stat.h>
57 
58 #include <sys/core.h>
59 #include <sys/exec_aout.h>
60 #include <sys/kcore.h>
61 
62 #include <unistd.h>
63 #include <limits.h>
64 #include <nlist.h>
65 #include <kvm.h>
66 
67 #include <vm/vm.h>
68 #include <vm/vm_param.h>
69 
70 #include <db.h>
71 
72 #include "kvm_private.h"
73 
74 #include <machine/pte.h>
75 #include <machine/kcore.h>
76 
77 #ifndef btop
78 #define	btop(x)		(((unsigned)(x)) >> PGSHIFT)	/* XXX */
79 #define	ptob(x)		((caddr_t)((x) << PGSHIFT))	/* XXX */
80 #endif
81 
82 #define KREAD(kd, addr, p)\
83 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
84 
85 void
86 _kvm_freevtop(kd)
87 	kvm_t *kd;
88 {
89 	if (kd->vmst != 0)
90 		free(kd->vmst);
91 }
92 
93 int
94 _kvm_initvtop(kd)
95 	kvm_t *kd;
96 {
97 	return (0);
98 }
99 
100 static int
101 _kvm_vatop(kd, sta, va, pa)
102 	kvm_t *kd;
103 	st_entry_t *sta;
104 	u_long va;
105 	u_long *pa;
106 {
107 	register cpu_kcore_hdr_t *cpu_kh;
108 	register u_long addr;
109 	int p, ste, pte;
110 	int offset;
111 
112 	if (ISALIVE(kd)) {
113 		_kvm_err(kd, 0, "vatop called in live kernel!");
114 		return((off_t)0);
115 	}
116 	offset = va & PGOFSET;
117 	cpu_kh = kd->cpu_data;
118 	/*
119 	 * If we are initializing (kernel segment table pointer not yet set)
120 	 * then return pa == va to avoid infinite recursion.
121 	 */
122 	if (cpu_kh->sysseg_pa == 0) {
123 		*pa = va + cpu_kh->kernel_pa;
124 		return (NBPG - offset);
125 	}
126 	if (cpu_kh->mmutype == -2) {
127 		st_entry_t *sta2;
128 
129 		addr = (u_long)&sta[va >> SG4_SHIFT1];
130 		/*
131 		 * Can't use KREAD to read kernel segment table entries.
132 		 * Fortunately it is 1-to-1 mapped so we don't have to.
133 		 */
134 		if (sta == cpu_kh->sysseg_pa) {
135 			if (lseek(kd->pmfd, _kvm_pa2off(kd, addr), 0) == -1 ||
136 			    read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
137 				goto invalid;
138 		} else if (KREAD(kd, addr, &ste))
139 			goto invalid;
140 		if ((ste & SG_V) == 0) {
141 			_kvm_err(kd, 0, "invalid level 1 descriptor (%x)",
142 				 ste);
143 			return((off_t)0);
144 		}
145 		sta2 = (st_entry_t *)(ste & SG4_ADDR1);
146 		addr = (u_long)&sta2[(va & SG4_MASK2) >> SG4_SHIFT2];
147 		/*
148 		 * Address from level 1 STE is a physical address,
149 		 * so don't use kvm_read.
150 		 */
151 		if (lseek(kd->pmfd, _kvm_pa2off(kd, addr), 0) == -1 ||
152 		    read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
153 			goto invalid;
154 		if ((ste & SG_V) == 0) {
155 			_kvm_err(kd, 0, "invalid level 2 descriptor (%x)",
156 				 ste);
157 			return((off_t)0);
158 		}
159 		sta2 = (st_entry_t *)(ste & SG4_ADDR2);
160 		addr = (u_long)&sta2[(va & SG4_MASK3) >> SG4_SHIFT3];
161 	} else {
162 		addr = (u_long)&sta[va >> SEGSHIFT];
163 		/*
164 		 * Can't use KREAD to read kernel segment table entries.
165 		 * Fortunately it is 1-to-1 mapped so we don't have to.
166 		 */
167 		if (sta == cpu_kh->sysseg_pa) {
168 			if (lseek(kd->pmfd, _kvm_pa2off(kd, addr), 0) == -1 ||
169 			    read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
170 				goto invalid;
171 		} else if (KREAD(kd, addr, &ste))
172 			goto invalid;
173 		if ((ste & SG_V) == 0) {
174 			_kvm_err(kd, 0, "invalid segment (%x)", ste);
175 			return((off_t)0);
176 		}
177 		p = btop(va & SG_PMASK);
178 		addr = (ste & SG_FRAME) + (p * sizeof(pt_entry_t));
179 	}
180 	/*
181 	 * Address from STE is a physical address so don't use kvm_read.
182 	 */
183 	if (lseek(kd->pmfd, _kvm_pa2off(kd, addr), 0) == -1 ||
184 	    read(kd->pmfd, (char *)&pte, sizeof(pte)) < 0)
185 		goto invalid;
186 	addr = pte & PG_FRAME;
187 	if (pte == PG_NV) {
188 		_kvm_err(kd, 0, "page not valid");
189 		return (0);
190 	}
191 	*pa = addr + offset;
192 
193 	return (NBPG - offset);
194 invalid:
195 	_kvm_err(kd, 0, "invalid address (%x)", va);
196 	return (0);
197 }
198 
199 int
200 _kvm_kvatop(kd, va, pa)
201 	kvm_t *kd;
202 	u_long va;
203 	u_long *pa;
204 {
205 	register cpu_kcore_hdr_t *cpu_kh;
206 
207 	cpu_kh = kd->cpu_data;
208 	return (_kvm_vatop(kd, (u_long)cpu_kh->sysseg_pa, va, pa));
209 }
210 
211 /*
212  * Translate a physical address to a file-offset in the crash-dump.
213  */
214 off_t
215 _kvm_pa2off(kd, pa)
216 	kvm_t	*kd;
217 	u_long	pa;
218 {
219 	off_t		off;
220 	phys_ram_seg_t	*rsp;
221 	register cpu_kcore_hdr_t *cpu_kh;
222 
223 	cpu_kh = kd->cpu_data;
224 	off = 0;
225 	for (rsp = cpu_kh->ram_segs; rsp->size; rsp++) {
226 		if (pa >= rsp->start && pa < rsp->start + rsp->size) {
227 			pa -= rsp->start;
228 			break;
229 		}
230 		off += rsp->size;
231 	}
232 	return(kd->dump_off + off + pa);
233 }
234