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