xref: /openbsd-src/lib/libkvm/kvm_sparc64.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: kvm_sparc64.c,v 1.3 2002/02/16 21:27:26 millert Exp $	*/
2 /*	$NetBSD: kvm_sparc64.c,v 1.7 2001/08/05 03:33:15 matt Exp $	*/
3 
4 /*-
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software developed by the Computer Systems
9  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
10  * BG 91-66 and contributed to Berkeley.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #include <sys/cdefs.h>
42 
43 /*
44  * Sparc 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/exec.h>
50 #include <sys/user.h>
51 #include <sys/proc.h>
52 #include <sys/stat.h>
53 #include <sys/core.h>
54 #include <sys/kcore.h>
55 #include <unistd.h>
56 #include <nlist.h>
57 #include <kvm.h>
58 
59 #include <uvm/uvm_extern.h>
60 
61 #include <machine/vmparam.h>
62 #include <machine/pmap.h>
63 #include <machine/kcore.h>
64 
65 #include <limits.h>
66 #include <db.h>
67 
68 #include "kvm_private.h"
69 
70 int _kvm_kvatop(kvm_t *, u_long, u_long *);
71 
72 void
73 _kvm_freevtop(kd)
74 	kvm_t *kd;
75 {
76 	if (kd->vmst != 0) {
77 		_kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
78 		kd->vmst = 0;
79 	}
80 }
81 
82 /*
83  * Prepare for translation of kernel virtual addresses into offsets
84  * into crash dump files. We use the MMU specific goop written at the
85  * front of the crash dump by pmap_dumpmmu().
86  *
87  * We should read in and cache the ksegs here to speed up operations...
88  */
89 int
90 _kvm_initvtop(kd)
91 	kvm_t *kd;
92 {
93 	kd->nbpg = 0x2000;
94 
95 	return (0);
96 }
97 
98 /*
99  * Translate a kernel virtual address to a physical address using the
100  * mapping information in kd->vm.  Returns the result in pa, and returns
101  * the number of bytes that are contiguously available from this
102  * physical address.  This routine is used only for crashdumps.
103  */
104 int
105 _kvm_kvatop(kd, va, pa)
106 	kvm_t *kd;
107 	u_long va;
108 	u_long *pa;
109 {
110 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
111 	u_long kernbase = cpup->kernbase;
112 	uint64_t *pseg, *pdir, *ptbl;
113 	int64_t data;
114 
115 	if (va < kernbase)
116 		goto lose;
117 
118 	/* Handle the wired 4MB TTEs */
119 	if (va > cpup->ktextbase && va < (cpup->ktextbase + cpup->ktextsz)) {
120 		u_long vaddr;
121 
122 		vaddr = va - cpup->ktextbase;
123 		*pa = cpup->ktextp + vaddr;
124 		return (cpup->ktextsz - vaddr);
125 	}
126 
127 	if (va > cpup->kdatabase && va < (cpup->kdatabase + cpup->kdatasz)) {
128 		u_long vaddr;
129 
130 		vaddr = va - cpup->kdatabase;
131 		*pa = cpup->kdatap + vaddr;
132 		return (cpup->kdatasz - vaddr);
133 	}
134 
135 
136 	/*
137 	 * Parse kernel page table.
138 	 */
139 	pseg = (uint64_t *)(u_long)cpup->segmapoffset;
140 	if (pread(kd->pmfd, &pdir, sizeof(pdir),
141 		_kvm_pa2off(kd, (u_long)&pseg[va_to_seg(va)]))
142 		!= sizeof(pdir)) {
143 		_kvm_syserr(kd, 0, "could not read L1 PTE");
144 		goto lose;
145 	}
146 
147 	if (!pdir) {
148 		_kvm_err(kd, 0, "invalid L1 PTE");
149 		goto lose;
150 	}
151 
152 	if (pread(kd->pmfd, &ptbl, sizeof(ptbl),
153 		_kvm_pa2off(kd, (u_long)&pdir[va_to_dir(va)]))
154 		!= sizeof(ptbl)) {
155 		_kvm_syserr(kd, 0, "could not read L2 PTE");
156 		goto lose;
157 	}
158 
159 	if (!ptbl) {
160 		_kvm_err(kd, 0, "invalid L2 PTE");
161 		goto lose;
162 	}
163 
164 	if (pread(kd->pmfd, &data, sizeof(data),
165 		_kvm_pa2off(kd, (u_long)&ptbl[va_to_pte(va)]))
166 		!= sizeof(data)) {
167 		_kvm_syserr(kd, 0, "could not read TTE");
168 		goto lose;
169 	}
170 
171 	if (data >= 0) {
172 		_kvm_err(kd, 0, "invalid L2 TTE");
173 		goto lose;
174 	}
175 
176 	/*
177 	 * Calculate page offsets and things.
178 	 *
179 	 * XXXX -- We could support multiple page sizes.
180 	 */
181 	va = va & (kd->nbpg - 1);
182 	data &= TLB_PA_MASK;
183 	*pa = data + va;
184 
185 	/*
186 	 * Parse and trnslate our TTE.
187 	 */
188 
189 	return (kd->nbpg - va);
190 
191 lose:
192 	*pa = -1;
193 	_kvm_err(kd, 0, "invalid address (%lx)", va);
194 	return (0);
195 }
196 
197 
198 /*
199  * Translate a physical address to a file-offset in the crash-dump.
200  */
201 off_t
202 _kvm_pa2off(kd, pa)
203 	kvm_t   *kd;
204 	u_long  pa;
205 {
206 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
207 	phys_ram_seg_t *mp;
208 	off_t off;
209 	int nmem;
210 
211 	/*
212 	 * Layout of CPU segment:
213 	 *	cpu_kcore_hdr_t;
214 	 *	[alignment]
215 	 *	phys_ram_seg_t[cpup->nmemseg];
216 	 */
217 	mp = (phys_ram_seg_t *)((long)kd->cpu_data + cpup->memsegoffset);
218 	off = 0;
219 
220 	/* Translate (sparse) pfnum to (packed) dump offset */
221 	for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
222 		if (mp->start <= pa && pa < mp->start + mp->size)
223 			break;
224 		off += mp->size;
225 	}
226 	if (nmem < 0) {
227 		_kvm_err(kd, 0, "invalid address (%lx)", pa);
228 		return (-1);
229 	}
230 
231 	return (kd->dump_off + off + pa - mp->start);
232 }
233 
234